From 4c601c4d378d81bcd4377826be6216abdb1cd7c7 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Mon, 27 Mar 2017 18:47:12 -0700 Subject: [PATCH] wip --- src/code_completion.cc | 4 +- src/command_line.cc | 2 + src/compilation_database_loader.cc | 123 +++++++++++++++++++++++++++-- src/indexer.cpp | 4 +- src/platform.h | 1 + src/platform_linux.cc | 21 +++-- 6 files changed, 142 insertions(+), 13 deletions(-) diff --git a/src/code_completion.cc b/src/code_completion.cc index da9b1d71..e5e33e46 100644 --- a/src/code_completion.cc +++ b/src/code_completion.cc @@ -182,6 +182,8 @@ CompletionSession::CompletionSession(const CompilationEntry& file, WorkingFiles* std::vector unsaved = working_files->AsUnsavedFiles(); std::vector args = file.args; + args.push_back("-x"); + args.push_back("c++"); args.push_back("-fparse-all-comments"); std::string sent_args = ""; @@ -387,4 +389,4 @@ CompletionSession* CompletionManager::GetOrOpenSession(const std::string& filena } sessions.push_back(MakeUnique(*entry, working_files)); return sessions[sessions.size() - 1].get(); -} \ No newline at end of file +} diff --git a/src/command_line.cc b/src/command_line.cc index cfe3f314..6cd9a06f 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -333,6 +333,8 @@ void QueryDbMainLoop( project->entries = LoadCompilationEntriesFromDirectory(path); std::cerr << "Loaded compilation entries (" << project->entries.size() << " files)" << std::endl; + //for (int i = 0; i < 10; ++i) + //std::cerr << project->entries[i].filename << std::endl; for (int i = 0; i < project->entries.size(); ++i) { const CompilationEntry& entry = project->entries[i]; std::string filepath = entry.filename; diff --git a/src/compilation_database_loader.cc b/src/compilation_database_loader.cc index 0db29f19..ddd4638b 100644 --- a/src/compilation_database_loader.cc +++ b/src/compilation_database_loader.cc @@ -1,11 +1,13 @@ #include "compilation_database_loader.h" -#include +#include "libclangmm/Utility.h" +#include "platform.h" +#include "utils.h" + #include -#include "libclangmm/Utility.h" +#include -#include "utils.h" // See http://stackoverflow.com/a/2072890 bool EndsWith(const std::string& value, const std::string& ending) { @@ -49,6 +51,83 @@ std::vector LoadFromDirectoryListing(const std::string& projec return result; } + +// https://github.com/Andersbakken/rtags/blob/6b16b81ea93aeff4a58930b44b2a0a207b456192/src/Source.cpp +static const char *kValueArgs[] = { + "--param", + "-G", + "-I", + "-MF", + "-MQ", + "-MT", + "-T", + "-V", + "-Xanalyzer", + "-Xassembler", + "-Xclang", + "-Xlinker", + "-Xpreprocessor", + "-arch", + "-b", + "-gcc-toolchain", + "-imacros", + "-imultilib", + "-include", + "-iprefix", + "-isysroot", + "-ivfsoverlay", + "-iwithprefix", + "-iwithprefixbefore", + "-o", + "-target", + "-x" +}; +static const char *kBlacklist[] = { + "--param", + "-M", + "-MD", + "-MF", + "-MG", + "-MM", + "-MMD", + "-MP", + "-MQ", + "-MT", + "-Og", + "-Wa,--32", + "-Wa,--64", + "-Wl,--incremental-full", + "-Wl,--incremental-patch,1", + "-Wl,--no-incremental", + "-fbuild-session-file=", + "-fbuild-session-timestamp=", + "-fembed-bitcode", + "-fembed-bitcode-marker", + "-fmodules-validate-once-per-build-session", + "-fno-delete-null-pointer-checks", + "-fno-use-linker-plugin" + "-fno-var-tracking", + "-fno-var-tracking-assignments", + "-fno-enforce-eh-specs", + "-fvar-tracking", + "-fvar-tracking-assignments", + "-fvar-tracking-assignments-toggle", + "-gcc-toolchain", + "-march=", + "-masm=", + "-mcpu=", + "-mfpmath=", + "-mtune=", + "-s", + + //"-B", + //"-f", + //"-pipe", + //"-W", + "/", + "..", +}; + std::vector LoadCompilationEntriesFromDirectory(const std::string& project_directory) { CXCompilationDatabase_Error cx_db_load_error; CXCompilationDatabase cx_db = clang_CompilationDatabase_fromDirectory(project_directory.c_str(), &cx_db_load_error); @@ -65,13 +144,47 @@ std::vector LoadCompilationEntriesFromDirectory(const std::str CXCompileCommand cx_command = clang_CompileCommands_getCommand(cx_commands, i); CompilationEntry entry; + // TODO: remove ComplationEntry::directory entry.directory = clang::ToString(clang_CompileCommand_getDirectory(cx_command)); entry.filename = clang::ToString(clang_CompileCommand_getFilename(cx_command)); + std::string normalized = entry.directory + "/" + entry.filename; + entry.filename = NormalizePath(normalized); + unsigned int num_args = clang_CompileCommand_getNumArgs(cx_command); entry.args.reserve(num_args); for (unsigned int j = 0; j < num_args; ++j) { - entry.args.push_back(clang::ToString(clang_CompileCommand_getArg(cx_command, j))); + std::string arg = clang::ToString(clang_CompileCommand_getArg(cx_command, j)); + + + bool bad = false; + for (auto& entry : kValueArgs) { + if (StartsWith(arg, entry)) { + bad = true; + continue; + } + } + if (bad) { + ++j; + continue; + } + + + for (auto& entry : kBlacklist) { + if (StartsWith(arg, entry)) { + bad = true; + continue; + } + } + if (bad) { + continue; + } + + + + entry.args.push_back(arg); + + //if (StartsWith(arg, "-I") || StartsWith(arg, "-D") || StartsWith(arg, "-std")) } @@ -82,4 +195,4 @@ std::vector LoadCompilationEntriesFromDirectory(const std::str clang_CompilationDatabase_dispose(cx_db); return result; -} \ No newline at end of file +} diff --git a/src/indexer.cpp b/src/indexer.cpp index 465e5719..05f4a850 100644 --- a/src/indexer.cpp +++ b/src/indexer.cpp @@ -1205,8 +1205,8 @@ void emptyIndexEntityReference(CXClientData client_data, IndexedFile Parse(std::string filename, std::vector args, bool dump_ast) { - clang_enableStackTraces(); - clang_toggleCrashRecovery(1); + //clang_enableStackTraces(); + //clang_toggleCrashRecovery(1); args.push_back("-std=c++11"); args.push_back("-fms-compatibility"); diff --git a/src/platform.h b/src/platform.h index f9b336c1..2be31207 100644 --- a/src/platform.h +++ b/src/platform.h @@ -25,3 +25,4 @@ std::unique_ptr CreatePlatformSharedMemory( void PlatformInit(); std::string GetWorkingDirectory(); +std::string NormalizePath(const std::string& path); diff --git a/src/platform_linux.cc b/src/platform_linux.cc index c9f4588e..a19dbc8d 100644 --- a/src/platform_linux.cc +++ b/src/platform_linux.cc @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -105,8 +106,6 @@ struct PlatformSharedMemoryLinux : public PlatformSharedMemory { } }; -#undef CHECKED - std::unique_ptr CreatePlatformMutex(const std::string& name) { std::string name2 = "/" + name; return MakeUnique(name2); @@ -118,12 +117,24 @@ std::unique_ptr CreatePlatformScopedMutexLock( static_cast(mutex)->sem_); } -void PlatformInit() { -} - std::unique_ptr CreatePlatformSharedMemory( const std::string& name, size_t size) { std::string name2 = "/" + name; return MakeUnique(name2, size); } + +void PlatformInit() { +} + +std::string NormalizePath(const std::string& path) { + errno = 0; + char name[PATH_MAX + 1]; + realpath(path.c_str(), name); + if (errno) + return path; + return name; +} + +#undef CHECKED + #endif