diff --git a/src/lib/data/indexer/interprocess/shared_types/SharedIndexerCommand.cpp b/src/lib/data/indexer/interprocess/shared_types/SharedIndexerCommand.cpp index 0dda4d43..0b82034f 100644 --- a/src/lib/data/indexer/interprocess/shared_types/SharedIndexerCommand.cpp +++ b/src/lib/data/indexer/interprocess/shared_types/SharedIndexerCommand.cpp @@ -27,6 +27,7 @@ void SharedIndexerCommand::fromLocal(IndexerCommand* indexerCommand) IndexerCommandCxxEmpty* cmd = dynamic_cast(indexerCommand); setType(CXX_EMPTY); + setWorkingDirectory(cmd->getWorkingDirectory()); setLanguageStandard(cmd->getLanguageStandard()); setCompilerFlags(cmd->getCompilerFlags()); setSystemHeaderSearchPaths(cmd->getSystemHeaderSearchPaths()); @@ -69,6 +70,7 @@ std::shared_ptr SharedIndexerCommand::fromShared(const SharedInd indexerCommand.getSourceFilePath(), indexerCommand.getIndexedPaths(), indexerCommand.getExcludedPaths(), + indexerCommand.getWorkingDirectory(), indexerCommand.getLanguageStandard(), indexerCommand.getSystemHeaderSearchPaths(), indexerCommand.getFrameworkSearchhPaths(), diff --git a/src/lib_cxx/data/indexer/IndexerCommandCxx.cpp b/src/lib_cxx/data/indexer/IndexerCommandCxx.cpp index 86e2166c..7fef9032 100644 --- a/src/lib_cxx/data/indexer/IndexerCommandCxx.cpp +++ b/src/lib_cxx/data/indexer/IndexerCommandCxx.cpp @@ -4,11 +4,13 @@ IndexerCommandCxx::IndexerCommandCxx( const FilePath& sourceFilePath, const std::set& indexedPaths, const std::set& excludedPaths, + const FilePath& workingDirectory, const std::vector& systemHeaderSearchPaths, const std::vector& frameworkSearchPaths, const std::vector& compilerFlags ) : IndexerCommand(sourceFilePath, indexedPaths, excludedPaths) + , m_workingDirectory(workingDirectory) , m_systemHeaderSearchPaths(systemHeaderSearchPaths) , m_frameworkSearchPaths(frameworkSearchPaths) , m_compilerFlags(compilerFlags) @@ -51,3 +53,8 @@ std::vector IndexerCommandCxx::getCompilerFlags() const { return m_compilerFlags; } + +FilePath IndexerCommandCxx::getWorkingDirectory() const +{ + return m_workingDirectory; +} diff --git a/src/lib_cxx/data/indexer/IndexerCommandCxx.h b/src/lib_cxx/data/indexer/IndexerCommandCxx.h index fadc1860..b60a9e2e 100644 --- a/src/lib_cxx/data/indexer/IndexerCommandCxx.h +++ b/src/lib_cxx/data/indexer/IndexerCommandCxx.h @@ -16,6 +16,7 @@ public: const FilePath& sourceFilePath, const std::set& indexedPaths, const std::set& excludedPaths, + const FilePath& workingDirectory, const std::vector& systemHeaderSearchPaths, const std::vector& frameworkSearchPaths, const std::vector& compilerFlags); @@ -25,8 +26,11 @@ public: std::vector getSystemHeaderSearchPaths() const; std::vector getFrameworkSearchPaths() const; std::vector getCompilerFlags() const; + FilePath getWorkingDirectory() const; + private: + FilePath m_workingDirectory; std::vector m_systemHeaderSearchPaths; std::vector m_frameworkSearchPaths; std::vector m_compilerFlags; diff --git a/src/lib_cxx/data/indexer/IndexerCommandCxxCdb.cpp b/src/lib_cxx/data/indexer/IndexerCommandCxxCdb.cpp index 5652cd9a..955dd81d 100644 --- a/src/lib_cxx/data/indexer/IndexerCommandCxxCdb.cpp +++ b/src/lib_cxx/data/indexer/IndexerCommandCxxCdb.cpp @@ -48,8 +48,7 @@ IndexerCommandCxxCdb::IndexerCommandCxxCdb( const std::vector& systemHeaderSearchPaths, const std::vector& frameworkSearchPaths ) - : IndexerCommandCxx(sourceFilePath, indexedPaths, excludedPaths, systemHeaderSearchPaths, frameworkSearchPaths, compilerFlags) - , m_workingDirectory(workingDirectory) + : IndexerCommandCxx(sourceFilePath, indexedPaths, excludedPaths, workingDirectory, systemHeaderSearchPaths, frameworkSearchPaths, compilerFlags) { } @@ -57,13 +56,3 @@ IndexerCommandType IndexerCommandCxxCdb::getIndexerCommandType() const { return getStaticIndexerCommandType(); } - -size_t IndexerCommandCxxCdb::getByteSize(size_t stringSize) const -{ - return IndexerCommandCxx::getByteSize(stringSize) + m_workingDirectory.str().size(); -} - -FilePath IndexerCommandCxxCdb::getWorkingDirectory() const -{ - return m_workingDirectory; -} diff --git a/src/lib_cxx/data/indexer/IndexerCommandCxxCdb.h b/src/lib_cxx/data/indexer/IndexerCommandCxxCdb.h index 6eac94c8..67baedd7 100644 --- a/src/lib_cxx/data/indexer/IndexerCommandCxxCdb.h +++ b/src/lib_cxx/data/indexer/IndexerCommandCxxCdb.h @@ -30,12 +30,6 @@ public: const std::vector& frameworkSearchPaths); virtual IndexerCommandType getIndexerCommandType() const override; - virtual size_t getByteSize(size_t stringSize) const override; - - FilePath getWorkingDirectory() const; - -private: - FilePath m_workingDirectory; }; #endif // INDEXER_COMMAND_CXX_CDB_H diff --git a/src/lib_cxx/data/indexer/IndexerCommandCxxEmpty.cpp b/src/lib_cxx/data/indexer/IndexerCommandCxxEmpty.cpp index 1da0b9fc..41c38050 100644 --- a/src/lib_cxx/data/indexer/IndexerCommandCxxEmpty.cpp +++ b/src/lib_cxx/data/indexer/IndexerCommandCxxEmpty.cpp @@ -9,12 +9,13 @@ IndexerCommandCxxEmpty::IndexerCommandCxxEmpty( const FilePath& sourceFilePath, const std::set& indexedPaths, const std::set& excludedPaths, + const FilePath& workingDirectory, const std::string& languageStandard, const std::vector& systemHeaderSearchPaths, const std::vector& frameworkSearchPaths, const std::vector& compilerFlags ) - : IndexerCommandCxx(sourceFilePath, indexedPaths, excludedPaths, systemHeaderSearchPaths, frameworkSearchPaths, compilerFlags) + : IndexerCommandCxx(sourceFilePath, indexedPaths, excludedPaths, workingDirectory, systemHeaderSearchPaths, frameworkSearchPaths, compilerFlags) , m_languageStandard(languageStandard) { } diff --git a/src/lib_cxx/data/indexer/IndexerCommandCxxEmpty.h b/src/lib_cxx/data/indexer/IndexerCommandCxxEmpty.h index af72fc99..07f34dd1 100644 --- a/src/lib_cxx/data/indexer/IndexerCommandCxxEmpty.h +++ b/src/lib_cxx/data/indexer/IndexerCommandCxxEmpty.h @@ -15,6 +15,7 @@ public: const FilePath& sourceFilePath, const std::set& indexedPaths, const std::set& excludedPaths, + const FilePath& workingDirectory, const std::string& languageStandard, const std::vector& systemHeaderSearchPaths, const std::vector& frameworkSearchPaths, diff --git a/src/lib_cxx/data/parser/cxx/CxxParser.cpp b/src/lib_cxx/data/parser/cxx/CxxParser.cpp index 9b599885..0abe1e7f 100644 --- a/src/lib_cxx/data/parser/cxx/CxxParser.cpp +++ b/src/lib_cxx/data/parser/cxx/CxxParser.cpp @@ -14,21 +14,23 @@ #include "utility/file/FileRegister.h" #include "utility/logging/logging.h" #include "utility/text/TextAccess.h" +#include "utility/utilityString.h" +#include "utility/utility.h" namespace { - static std::vector getSyntaxOnlyToolArgs(const std::vector &ExtraArgs, llvm::StringRef FileName) + std::vector prependSyntaxOnlyToolArgs(const std::vector& args) { - std::vector Args; - Args.push_back("clang-tool"); - Args.push_back("-fsyntax-only"); - Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end()); - Args.push_back(FileName.str()); - return Args; + return utility::concat({ "clang-tool", "-fsyntax-only" }, args); + } + + std::vector appendFilePath(const std::vector& args, llvm::StringRef fileName) + { + return utility::concat(args, { fileName.str() }); } // custom implementation of clang::runToolOnCodeWithArgs which also sets our custon DiagnosticConsumer - static bool runToolOnCodeWithArgs( + bool runToolOnCodeWithArgs( clang::DiagnosticConsumer* DiagConsumer, clang::FrontendAction *ToolAction, const llvm::Twine &Code, @@ -40,7 +42,7 @@ namespace llvm::SmallString<16> FileNameStorage; llvm::StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); llvm::IntrusiveRefCntPtr Files(new clang::FileManager(clang::FileSystemOptions())); - clang::tooling::ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), ToolAction, Files.get()); + clang::tooling::ToolInvocation Invocation(prependSyntaxOnlyToolArgs(appendFilePath(Args, FileNameRef)), ToolAction, Files.get()); llvm::SmallString<1024> CodeStorage; Invocation.mapVirtualFile(FileNameRef, Code.toNullTerminatedStringRef(CodeStorage)); @@ -73,14 +75,15 @@ void CxxParser::buildIndex(std::shared_ptr indexerCommand) clang::tooling::CompileCommand compileCommand; compileCommand.Filename = indexerCommand->getSourceFilePath().str(); compileCommand.Directory = indexerCommand->getWorkingDirectory().str(); - compileCommand.CommandLine = indexerCommand->getCompilerFlags(); + compileCommand.CommandLine = utility::concat(indexerCommand->getCompilerFlags(), getCommandlineArgumentsEssential( + std::vector(), indexerCommand->getSystemHeaderSearchPaths(), indexerCommand->getFrameworkSearchPaths() + )); + if (!utility::isPrefix("-", compileCommand.CommandLine.front())) { - std::vector args = getCommandlineArgumentsEssential( - std::vector(), indexerCommand->getSystemHeaderSearchPaths(), indexerCommand->getFrameworkSearchPaths() - ); - compileCommand.CommandLine.insert(compileCommand.CommandLine.end(), args.begin(), args.end()); + compileCommand.CommandLine.erase(compileCommand.CommandLine.begin()); } + compileCommand.CommandLine = prependSyntaxOnlyToolArgs(compileCommand.CommandLine); CxxCompilationDatabaseSingle compilationDatabase(compileCommand); runTool(&compilationDatabase, indexerCommand->getSourceFilePath()); @@ -88,9 +91,13 @@ void CxxParser::buildIndex(std::shared_ptr indexerCommand) void CxxParser::buildIndex(std::shared_ptr indexerCommand) { - std::shared_ptr compilationDatabase = getCompilationDatabase(indexerCommand); + clang::tooling::CompileCommand compileCommand; + compileCommand.Filename = indexerCommand->getSourceFilePath().str(); + compileCommand.Directory = indexerCommand->getWorkingDirectory().str(); + compileCommand.CommandLine = prependSyntaxOnlyToolArgs(appendFilePath(getCommandlineArguments(indexerCommand), indexerCommand->getSourceFilePath().str())); - runTool(compilationDatabase.get(), indexerCommand->getSourceFilePath()); + CxxCompilationDatabaseSingle compilationDatabase(compileCommand); + runTool(&compilationDatabase, indexerCommand->getSourceFilePath()); } void CxxParser::buildIndex(const std::string& fileName, std::shared_ptr fileContent, std::vector compilerFlags) @@ -179,37 +186,6 @@ std::vector CxxParser::getCommandlineArguments(std::shared_ptr CxxParser::getCompilationDatabase( - std::shared_ptr indexerCommand -) const { - // Commandline flags passed to the programm. Everything after '--' will be interpreted by the ClangTool. - std::vector args = getCommandlineArguments(indexerCommand); - args.insert(args.begin(), "app"); - args.insert(args.begin() + 1, "--"); - - int argc = args.size(); - const char** argv = new const char*[argc]; - for (size_t i = 0; i < args.size(); i++) - { - argv[i] = args[i].c_str(); - } - - std::string errorMessage; - std::shared_ptr compilationDatabase( - clang::tooling::FixedCompilationDatabase::loadFromCommandLine(argc, argv, errorMessage) - ); - - delete[] argv; - - if (!compilationDatabase) - { - LOG_ERROR("Failed to load compilation database"); - return nullptr; - } - - return compilationDatabase; -} - std::shared_ptr CxxParser::getDiagnostics(std::shared_ptr canonicalFilePathCache, bool logErrors) const { llvm::IntrusiveRefCntPtr options = new clang::DiagnosticOptions(); diff --git a/src/lib_cxx/data/parser/cxx/CxxParser.h b/src/lib_cxx/data/parser/cxx/CxxParser.h index c3e14c36..4053cd4c 100644 --- a/src/lib_cxx/data/parser/cxx/CxxParser.h +++ b/src/lib_cxx/data/parser/cxx/CxxParser.h @@ -36,7 +36,6 @@ private: const std::vector& systemHeaderSearchPaths, const std::vector& frameworkSearchPaths) const; std::vector getCommandlineArguments(std::shared_ptr indexerCommand) const; - std::shared_ptr getCompilationDatabase(std::shared_ptr indexerCommand) const; std::shared_ptr getDiagnostics(std::shared_ptr canonicalFilePathCache, bool logErrors) const; diff --git a/src/lib_cxx/project/SourceGroupCxxCdb.cpp b/src/lib_cxx/project/SourceGroupCxxCdb.cpp index d38d6c50..b3660f38 100644 --- a/src/lib_cxx/project/SourceGroupCxxCdb.cpp +++ b/src/lib_cxx/project/SourceGroupCxxCdb.cpp @@ -68,8 +68,7 @@ std::vector> SourceGroupCxxCdb::getIndexerComman utility::append(frameworkSearchPaths, m_settings->getFrameworkSearchPathsExpandedAndAbsolute()); utility::append(frameworkSearchPaths, appSettings->getFrameworkSearchPathsExpanded()); - std::vector compilerFlags; - utility::append(compilerFlags, m_settings->getCompilerFlags()); + const std::vector compilerFlags = m_settings->getCompilerFlags(); std::set indexedPaths = getIndexedPaths(); std::set excludedPaths = getExcludedPaths(); @@ -108,15 +107,12 @@ std::vector> SourceGroupCxxCdb::getIndexerComman if (filesToIndex.find(sourcePath) != filesToIndex.end() && sourceFilePaths.find(sourcePath) != sourceFilePaths.end()) { - std::vector currentCompilerFlags = compilerFlags; - currentCompilerFlags.insert(currentCompilerFlags.end(), command.CommandLine.begin(), command.CommandLine.end()); - indexerCommands.push_back(std::make_shared( sourcePath, indexedPaths, excludedPaths, FilePath(command.Directory), - currentCompilerFlags, + utility::concat(command.CommandLine, compilerFlags), systemHeaderSearchPaths, frameworkSearchPaths )); diff --git a/src/lib_cxx/project/SourceGroupCxxEmpty.cpp b/src/lib_cxx/project/SourceGroupCxxEmpty.cpp index 10dc4b0c..6abca03c 100644 --- a/src/lib_cxx/project/SourceGroupCxxEmpty.cpp +++ b/src/lib_cxx/project/SourceGroupCxxEmpty.cpp @@ -63,6 +63,7 @@ std::vector> SourceGroupCxxEmpty::getIndexerComm sourcePath, indexedPaths, excludedPaths, + m_settings->getProjectDirectoryPath(), m_settings->getStandard(), systemHeaderSearchPaths, frameworkSearchPaths, diff --git a/src/test/CxxIndexSampleProjectsTestSuite.h b/src/test/CxxIndexSampleProjectsTestSuite.h index 9423043c..19d923a6 100644 --- a/src/test/CxxIndexSampleProjectsTestSuite.h +++ b/src/test/CxxIndexSampleProjectsTestSuite.h @@ -122,8 +122,9 @@ private: std::shared_ptr parseCode(const FilePath& sourceFilePath, const FilePath& projectDataSrcRoot) { - std::set indexedPaths = { projectDataSrcRoot }; - std::set excludedPaths = { }; + const std::set indexedPaths = { projectDataSrcRoot }; + const std::set excludedPaths = {}; + const FilePath workingDirectory("."); std::shared_ptr fileRegister = std::make_shared( FileRegisterStateData(), @@ -140,6 +141,7 @@ private: sourceFilePath, indexedPaths, excludedPaths, + workingDirectory, "c++1z", utility::concat(std::vector { projectDataSrcRoot }, ApplicationSettings::getInstance()->getHeaderSearchPathsExpanded()), ApplicationSettings::getInstance()->getFrameworkSearchPathsExpanded(), diff --git a/src/test/CxxParserTestSuite.h b/src/test/CxxParserTestSuite.h index d777bf51..a78be47a 100644 --- a/src/test/CxxParserTestSuite.h +++ b/src/test/CxxParserTestSuite.h @@ -3990,13 +3990,15 @@ public: void test_cxx_parser_parses_multiple_files() { - std::set indexedPaths; - indexedPaths.insert(FilePath("data/CxxParserTestSuite/")); + const std::set indexedPaths = { FilePath("data/CxxParserTestSuite/") }; + const std::set excludedPaths; + const FilePath workingDirectory("."); std::shared_ptr indexerCommand = std::make_shared( FilePath("data/CxxParserTestSuite/code.cpp"), indexedPaths, - std::set(), + excludedPaths, + workingDirectory, "c++1z", std::vector(), std::vector(),