From c615535d33a69e038d47079318c2290b3b71ee8b Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Mon, 17 Apr 2017 21:06:01 -0700 Subject: [PATCH] vscode settings can now include an additional set of clang arguments to apply. --- src/code_completion.cc | 9 +++++---- src/code_completion.h | 5 +++-- src/command_line.cc | 4 ++-- src/indexer.cc | 4 ++-- src/indexer.h | 3 ++- src/language_server_api.h | 3 ++- src/libclangmm/TranslationUnit.cc | 4 ++++ src/libclangmm/TranslationUnit.h | 4 +++- src/test.cc | 3 ++- 9 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/code_completion.cc b/src/code_completion.cc index 6c4a88f6..5982cf5b 100644 --- a/src/code_completion.cc +++ b/src/code_completion.cc @@ -324,7 +324,7 @@ void CompletionMain(CompletionManager* completion_manager) { } // namespace -CompletionSession::CompletionSession(const CompilationEntry& file, WorkingFiles* working_files) : file(file) { +CompletionSession::CompletionSession(const CompilationEntry& file, IndexerConfig* config, WorkingFiles* working_files) : file(file) { std::vector unsaved = working_files->AsUnsavedFiles(); std::vector args = file.args; @@ -339,7 +339,7 @@ CompletionSession::CompletionSession(const CompilationEntry& file, WorkingFiles* // TODO: I think we crash when there are syntax errors. active_index = MakeUnique(0 /*excludeDeclarationsFromPCH*/, 0 /*displayDiagnostics*/); - active = MakeUnique(*active_index, file.filename, args, unsaved, Flags()); + active = MakeUnique(config, *active_index, file.filename, args, unsaved, Flags()); std::cerr << "Done creating active; did_fail=" << active->did_fail << std::endl; //if (active->did_fail) { // std::cerr << "Failed to create translation unit; trying again..." << std::endl; @@ -366,7 +366,8 @@ void CompletionSession::Refresh(std::vector& unsaved) { active->ReparseTranslationUnit(unsaved); } -CompletionManager::CompletionManager(Project* project, WorkingFiles* working_files) : project(project), working_files(working_files) { +CompletionManager::CompletionManager(IndexerConfig* config, Project* project, WorkingFiles* working_files) + : config(config), project(project), working_files(working_files) { new std::thread([&]() { CompletionMain(this); }); @@ -397,6 +398,6 @@ CompletionSession* CompletionManager::GetOrOpenSession(const std::string& filena else { std::cerr << "Found compilation entry" << std::endl; } - sessions.push_back(MakeUnique(*entry, working_files)); + sessions.push_back(MakeUnique(*entry, config, working_files)); return sessions[sessions.size() - 1].get(); } diff --git a/src/code_completion.h b/src/code_completion.h index 5ef9d61e..50b5c399 100644 --- a/src/code_completion.h +++ b/src/code_completion.h @@ -24,7 +24,7 @@ struct CompletionSession { //std::unique_ptr updated; //std::unique_ptr updated_index; - CompletionSession(const CompilationEntry& file, WorkingFiles* working_files); + CompletionSession(const CompilationEntry& file, IndexerConfig* config, WorkingFiles* working_files); ~CompletionSession(); // Refresh file index. @@ -33,6 +33,7 @@ struct CompletionSession { struct CompletionManager { std::vector> sessions; + IndexerConfig* config; Project* project; WorkingFiles* working_files; @@ -43,7 +44,7 @@ struct CompletionManager { }; AtomicObject completion_request; - CompletionManager(Project* project, WorkingFiles* working_files); + CompletionManager(IndexerConfig* config, Project* project, WorkingFiles* working_files); // Start a code completion at the given location. |on_complete| will run when // completion results are available. |on_complete| may run on any thread. diff --git a/src/command_line.cc b/src/command_line.cc index a46bf639..df5e998c 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -902,7 +902,7 @@ bool IndexMain_DoIndex(IndexerConfig* config, } // Parse request and send a response. - std::vector> indexes = Parse(file_consumer_shared, index_request->path, index_request->args); + std::vector> indexes = Parse(config, file_consumer_shared, index_request->path, index_request->args); time.ResetAndPrint("Parsing/indexing " + index_request->path); for (auto& current_index : indexes) { @@ -1601,7 +1601,7 @@ void QueryDbMain(IndexerConfig* config) { Project project; WorkingFiles working_files; - CompletionManager completion_manager(&project, &working_files); + CompletionManager completion_manager(config, &project, &working_files); FileConsumer::SharedState file_consumer_shared; // Start indexer threads. diff --git a/src/indexer.cc b/src/indexer.cc index 89243454..2885d17f 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -1419,13 +1419,13 @@ void indexEntityReference(CXClientData client_data, -std::vector> Parse(FileConsumer::SharedState* file_consumer_shared, std::string filename, std::vector args, bool dump_ast) { +std::vector> Parse(IndexerConfig* config, FileConsumer::SharedState* file_consumer_shared, std::string filename, std::vector args, bool dump_ast) { filename = NormalizePath(filename); clang::Index index(0 /*excludeDeclarationsFromPCH*/, 0 /*displayDiagnostics*/); std::vector unsaved_files; - clang::TranslationUnit tu(index, filename, args, unsaved_files, CXTranslationUnit_KeepGoing); + clang::TranslationUnit tu(config, index, filename, args, unsaved_files, CXTranslationUnit_KeepGoing); if (dump_ast) Dump(tu.document_cursor()); diff --git a/src/indexer.h b/src/indexer.h index b6062631..9461cc25 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -4,6 +4,7 @@ #include "position.h" #include "serializer.h" #include "utils.h" +#include "language_server_api.h" #include "libclangmm/clangmm.h" #include "libclangmm/Utility.h" @@ -456,5 +457,5 @@ struct IndexedFile { std::string ToString(); }; -std::vector> Parse(FileConsumer::SharedState* file_consumer_shared, std::string filename, std::vector args, bool dump_ast = false); +std::vector> Parse(IndexerConfig* config, FileConsumer::SharedState* file_consumer_shared, std::string filename, std::vector args, bool dump_ast = false); void IndexInit(); diff --git a/src/language_server_api.h b/src/language_server_api.h index 2c785856..b87bf217 100644 --- a/src/language_server_api.h +++ b/src/language_server_api.h @@ -61,8 +61,9 @@ struct IndexerConfig { NonElidedVector blacklist; int indexerCount = 1; int maxWorkspaceSearchResults = 1000; + std::vector extraClangArguments; }; -MAKE_REFLECT_STRUCT(IndexerConfig, cacheDirectory, whitelist, blacklist, indexerCount, maxWorkspaceSearchResults); +MAKE_REFLECT_STRUCT(IndexerConfig, cacheDirectory, whitelist, blacklist, indexerCount, maxWorkspaceSearchResults, extraClangArguments); diff --git a/src/libclangmm/TranslationUnit.cc b/src/libclangmm/TranslationUnit.cc index 14f5d3e3..e84257fc 100644 --- a/src/libclangmm/TranslationUnit.cc +++ b/src/libclangmm/TranslationUnit.cc @@ -10,6 +10,7 @@ namespace clang { TranslationUnit::TranslationUnit( + IndexerConfig* config, Index &index, const std::string& filepath, const std::vector& arguments, @@ -24,6 +25,9 @@ TranslationUnit::TranslationUnit( for (const auto& arg : platform_args) args.push_back(arg.c_str()); + for (const std::string& arg : config->extraClangArguments) + args.push_back(arg.c_str()); + std::cerr << "Parsing " << filepath << " with args "; for (const auto& arg : args) std::cerr << arg << " "; diff --git a/src/libclangmm/TranslationUnit.h b/src/libclangmm/TranslationUnit.h index 8196a005..11e6dc97 100644 --- a/src/libclangmm/TranslationUnit.h +++ b/src/libclangmm/TranslationUnit.h @@ -10,11 +10,13 @@ #include "Tokens.h" #include "CodeCompleteResults.h" #include "Cursor.h" +#include "../language_server_api.h" namespace clang { class TranslationUnit { public: - TranslationUnit(Index &index, + TranslationUnit(IndexerConfig* config, + Index &index, const std::string &filepath, const std::vector& arguments, std::vector unsaved_files, diff --git a/src/test.cc b/src/test.cc index e0bea964..a2f9eaa6 100644 --- a/src/test.cc +++ b/src/test.cc @@ -130,11 +130,12 @@ void RunTests() { // Parse expected output from the test, parse it into JSON document. std::unordered_map all_expected_output = ParseTestExpectation(path); + IndexerConfig config; FileConsumer::SharedState file_consumer_shared; // Run test. std::cout << "[START] " << path << std::endl; - std::vector> dbs = Parse(&file_consumer_shared, path, { + std::vector> dbs = Parse(&config, &file_consumer_shared, path, { "-xc++", "-std=c++11", "-IC:/Users/jacob/Desktop/superindex/indexer/third_party/",