From dd778bcb2388bdc643e1d5eb5ce60c03cee2719b Mon Sep 17 00:00:00 2001 From: Ivan Donchevskii Date: Thu, 31 Jan 2019 10:30:58 +0100 Subject: Clang: Use full paths in compilation database for symbol collector We we FilePath and NativeFilePath so that compiler warns us if we mix them up. Change-Id: I33d7abc7e4e724dff2a9b2b9b23deea8b358ccfd Reviewed-by: Marco Bubke --- .../clangpchmanagerbackend/source/pchcreator.cpp | 17 +++--- .../clangpchmanagerbackend/source/pchcreator.h | 2 +- .../source/clangquerygatherer.cpp | 7 +-- .../clangrefactoringbackend/source/clangtool.cpp | 67 +++++++--------------- .../clangrefactoringbackend/source/clangtool.h | 48 +++++----------- .../source/refactoringcompilationdatabase.cpp | 26 ++++----- .../source/refactoringcompilationdatabase.h | 6 +- .../source/refactoringserver.cpp | 14 ++--- 8 files changed, 68 insertions(+), 119 deletions(-) (limited to 'src/tools') diff --git a/src/tools/clangpchmanagerbackend/source/pchcreator.cpp b/src/tools/clangpchmanagerbackend/source/pchcreator.cpp index 777252dece..4333d5706a 100644 --- a/src/tools/clangpchmanagerbackend/source/pchcreator.cpp +++ b/src/tools/clangpchmanagerbackend/source/pchcreator.cpp @@ -94,10 +94,9 @@ FilePath PchCreator::generatePchFilePath() const ".pch"}}; } -std::vector PchCreator::generateClangCompilerArguments( - const PchTask &pchTask, - FilePathView sourceFilePath, - FilePathView pchOutputPath) +Utils::SmallStringVector PchCreator::generateClangCompilerArguments(const PchTask &pchTask, + FilePathView sourceFilePath, + FilePathView pchOutputPath) { CommandLineBuilder builder{pchTask, pchTask.toolChainArguments, @@ -114,10 +113,12 @@ void PchCreator::generatePch(PchTask &&pchTask) auto content = generatePchIncludeFileContent(pchTask.includes); auto pchOutputPath = generatePchFilePath(); - m_clangTool.addFile(m_environment.pchBuildDirectory().toStdString(), - "dummy.h", - Utils::SmallStringView(content), - generateClangCompilerArguments(pchTask, "dummy.h", pchOutputPath)); + FilePath headerFilePath{m_environment.pchBuildDirectory().toStdString(), "dummy.h"}; + Utils::SmallStringVector commandLine = generateClangCompilerArguments(pchTask, + headerFilePath, + pchOutputPath); + + m_clangTool.addFile(std::move(headerFilePath), std::move(content), std::move(commandLine)); bool success = generatePch(); diff --git a/src/tools/clangpchmanagerbackend/source/pchcreator.h b/src/tools/clangpchmanagerbackend/source/pchcreator.h index 1e36e18b6e..37d0c7c8a1 100644 --- a/src/tools/clangpchmanagerbackend/source/pchcreator.h +++ b/src/tools/clangpchmanagerbackend/source/pchcreator.h @@ -84,7 +84,7 @@ public: bool generatePch(); FilePath generatePchFilePath() const; - static std::vector generateClangCompilerArguments(const PchTask &pchTask, + static Utils::SmallStringVector generateClangCompilerArguments(const PchTask &pchTask, FilePathView includePchHeaderPath, FilePathView pchPath); diff --git a/src/tools/clangrefactoringbackend/source/clangquerygatherer.cpp b/src/tools/clangrefactoringbackend/source/clangquerygatherer.cpp index 21cd0d26e5..42fb7d88bc 100644 --- a/src/tools/clangrefactoringbackend/source/clangquerygatherer.cpp +++ b/src/tools/clangrefactoringbackend/source/clangquerygatherer.cpp @@ -50,10 +50,9 @@ ClangQueryGatherer::createSourceRangesForSource( { ClangQuery clangQuery(*filePathCache, std::move(query)); - clangQuery.addFile(std::string(source.filePath.directory()), - std::string(source.filePath.name()), - std::string(source.unsavedFileContent), - std::vector(source.commandLineArguments)); + clangQuery.addFile(std::move(source.filePath), + std::move(source.unsavedFileContent), + std::move(source.commandLineArguments)); clangQuery.addUnsavedFiles(unsaved); diff --git a/src/tools/clangrefactoringbackend/source/clangtool.cpp b/src/tools/clangrefactoringbackend/source/clangtool.cpp index 66f85c6d36..5ae87793db 100644 --- a/src/tools/clangrefactoringbackend/source/clangtool.cpp +++ b/src/tools/clangrefactoringbackend/source/clangtool.cpp @@ -27,6 +27,8 @@ #include +#include + namespace ClangBackEnd { namespace { @@ -44,67 +46,37 @@ String toNativePath(String &&path) } } -void ClangTool::addFile(std::string &&directory, - std::string &&fileName, - std::string &&content, - std::vector &&commandLine) +void ClangTool::addFile(FilePath &&filePath, + Utils::SmallString &&content, + Utils::SmallStringVector &&commandLine) { - m_fileContents.emplace_back(toNativePath(std::move(directory)), - std::move(fileName), - std::move(content), - std::move(commandLine)); + NativeFilePath nativeFilePath{filePath}; - const auto &fileContent = m_fileContents.back(); + m_compilationDatabase.addFile(nativeFilePath, std::move(commandLine)); + m_sourceFilePaths.push_back(Utils::SmallStringView{nativeFilePath}); - m_compilationDatabase.addFile(fileContent.directory, fileContent.fileName, fileContent.commandLine); - m_sourceFilePaths.push_back(fileContent.filePath); + m_fileContents.emplace_back(std::move(nativeFilePath), std::move(content)); } void ClangTool::addFiles(const FilePaths &filePaths, const Utils::SmallStringVector &arguments) { for (const FilePath &filePath : filePaths) { - std::vector commandLine(arguments.begin(), arguments.end()); - commandLine.push_back(std::string(filePath.name())); - - addFile(filePath.directory(), - filePath.name(), - {}, - std::move(commandLine)); - } -} - -template -void ClangTool::addFiles(const Container &filePaths, - const Utils::SmallStringVector &arguments) -{ - for (const typename Container::value_type &filePath : filePaths) { - auto found = std::find(filePath.rbegin(), filePath.rend(), '/'); - - auto fileNameBegin = found.base(); + std::string filePathStr(filePath.path()); + auto commandLine = arguments; + NativeFilePath nativeFilePath{filePath}; - std::vector commandLine(arguments.begin(), arguments.end()); - commandLine.push_back(std::string(filePath)); + commandLine.push_back(nativeFilePath.path()); - addFile({filePath.begin(), std::prev(fileNameBegin)}, - {fileNameBegin, filePath.end()}, - {}, - std::move(commandLine)); + addFile(filePath.clone(), {}, std::move(commandLine)); } } -template -void ClangTool::addFiles(const Utils::SmallStringVector &filePaths, - const Utils::SmallStringVector &arguments); -template -void ClangTool::addFiles(const Utils::PathStringVector &filePaths, - const Utils::SmallStringVector &arguments); - void ClangTool::addUnsavedFiles(const V2::FileContainers &unsavedFiles) { m_unsavedFileContents.reserve(m_unsavedFileContents.size() + unsavedFiles.size()); - auto convertToUnsavedFileContent = [] (const V2::FileContainer &unsavedFile) { - return UnsavedFileContent{toNativePath(unsavedFile.filePath.path().clone()), + auto convertToUnsavedFileContent = [](const V2::FileContainer &unsavedFile) { + return UnsavedFileContent{NativeFilePath{unsavedFile.filePath}, unsavedFile.unsavedFileContent.clone()}; }; @@ -120,7 +92,12 @@ llvm::StringRef toStringRef(const String &string) { return llvm::StringRef(string.data(), string.size()); } + +llvm::StringRef toStringRef(const NativeFilePath &path) +{ + return llvm::StringRef(path.path().data(), path.path().size()); } +} // namespace clang::tooling::ClangTool ClangTool::createTool() const { @@ -128,7 +105,7 @@ clang::tooling::ClangTool ClangTool::createTool() const for (const auto &fileContent : m_fileContents) { if (!fileContent.content.empty()) - tool.mapVirtualFile(fileContent.filePath, fileContent.content); + tool.mapVirtualFile(toStringRef(fileContent.filePath), fileContent.content); } for (const auto &unsavedFileContent : m_unsavedFileContents) diff --git a/src/tools/clangrefactoringbackend/source/clangtool.h b/src/tools/clangrefactoringbackend/source/clangtool.h index 8b4e47df68..e3ff835799 100644 --- a/src/tools/clangrefactoringbackend/source/clangtool.h +++ b/src/tools/clangrefactoringbackend/source/clangtool.h @@ -30,6 +30,7 @@ #include #include +#include #include #include @@ -43,50 +44,34 @@ namespace ClangBackEnd { struct FileContent { - FileContent(const std::string &directory, - const std::string &fileName, - const std::string &content, - const std::vector &commandLine) - : directory(directory), - fileName(fileName), - filePath(directory + nativeSeparator + fileName), - content(content), - commandLine(commandLine) + FileContent(NativeFilePath &&filePath, const Utils::SmallString &content) + : filePath(std::move(filePath)) + , content(std::move(content)) {} - std::string directory; - std::string fileName; - std::string filePath; + NativeFilePath filePath; std::string content; - std::vector commandLine; }; struct UnsavedFileContent { - UnsavedFileContent(Utils::PathString &&filePath, - Utils::SmallString &&content) - : filePath(std::move(filePath)), - content(std::move(content)) + UnsavedFileContent(NativeFilePath &&filePath, Utils::SmallString &&content) + : filePath(std::move(filePath)) + , content(std::move(content)) {} - Utils::PathString filePath; + NativeFilePath filePath; Utils::SmallString content; }; class ClangTool { public: - void addFile(std::string &&directory, - std::string &&fileName, - std::string &&content, - std::vector &&commandLine); - - template - void addFiles(const Container &filePaths, - const Utils::SmallStringVector &arguments); - void addFiles(const FilePaths &filePaths, - const Utils::SmallStringVector &arguments); + void addFile(FilePath &&filePath, + Utils::SmallString &&content, + Utils::SmallStringVector &&commandLine); + void addFiles(const FilePaths &filePaths, const Utils::SmallStringVector &arguments); void addUnsavedFiles(const V2::FileContainers &unsavedFiles); @@ -102,11 +87,4 @@ private: std::vector m_unsavedFileContents; }; -extern template -void ClangTool::addFiles(const Utils::SmallStringVector &filePaths, - const Utils::SmallStringVector &arguments); -extern template -void ClangTool::addFiles(const Utils::PathStringVector &filePaths, - const Utils::SmallStringVector &arguments); - } // namespace ClangBackEnd diff --git a/src/tools/clangrefactoringbackend/source/refactoringcompilationdatabase.cpp b/src/tools/clangrefactoringbackend/source/refactoringcompilationdatabase.cpp index c5906aaae4..1e1609d1a4 100644 --- a/src/tools/clangrefactoringbackend/source/refactoringcompilationdatabase.cpp +++ b/src/tools/clangrefactoringbackend/source/refactoringcompilationdatabase.cpp @@ -27,20 +27,15 @@ #include "clangrefactoringbackend_global.h" +#include +#include + namespace ClangBackEnd { RefactoringCompilationDatabase::RefactoringCompilationDatabase() { } -namespace { - -std::string concatFilePath(const clang::tooling::CompileCommand &compileCommand) -{ - return compileCommand.Directory + nativeSeparator + compileCommand.Filename; -} -} - std::vector RefactoringCompilationDatabase::getCompileCommands(llvm::StringRef filePath) const { @@ -50,7 +45,7 @@ RefactoringCompilationDatabase::getCompileCommands(llvm::StringRef filePath) con m_compileCommands.end(), std::back_inserter(foundCommands), [&] (const clang::tooling::CompileCommand &compileCommand) { - return filePath == concatFilePath(compileCommand); + return filePath == compileCommand.Filename; }); return foundCommands; @@ -66,7 +61,7 @@ RefactoringCompilationDatabase::getAllFiles() const m_compileCommands.end(), std::back_inserter(filePaths), [&] (const clang::tooling::CompileCommand &compileCommand) { - return concatFilePath(compileCommand); + return compileCommand.Filename; }); return filePaths; @@ -78,12 +73,13 @@ RefactoringCompilationDatabase::getAllCompileCommands() const return m_compileCommands; } -void RefactoringCompilationDatabase::addFile(const std::string &directory, - const std::string &fileName, - const std::vector &commandLine) +void RefactoringCompilationDatabase::addFile(NativeFilePathView filePath, + Utils::SmallStringVector &&commandLine) { - - m_compileCommands.emplace_back(directory, fileName, commandLine, llvm::StringRef()); + m_compileCommands.emplace_back(std::string(filePath.directory()), + std::string(filePath), + std::vector(commandLine), + llvm::StringRef()); } } // namespace ClangBackEnd diff --git a/src/tools/clangrefactoringbackend/source/refactoringcompilationdatabase.h b/src/tools/clangrefactoringbackend/source/refactoringcompilationdatabase.h index ca2d133819..b25e43e483 100644 --- a/src/tools/clangrefactoringbackend/source/refactoringcompilationdatabase.h +++ b/src/tools/clangrefactoringbackend/source/refactoringcompilationdatabase.h @@ -27,6 +27,8 @@ #include "clang/Tooling/CompilationDatabase.h" +#include + namespace ClangBackEnd { @@ -39,9 +41,7 @@ public: std::vector getAllFiles() const override; std::vector getAllCompileCommands() const override; - void addFile(const std::string &directory, - const std::string &fileName, - const std::vector &commandLine); + void addFile(NativeFilePathView filePath, Utils::SmallStringVector &&commandLine); private: std::vector m_compileCommands; diff --git a/src/tools/clangrefactoringbackend/source/refactoringserver.cpp b/src/tools/clangrefactoringbackend/source/refactoringserver.cpp index 4b51ee0d69..83aa5a31fd 100644 --- a/src/tools/clangrefactoringbackend/source/refactoringserver.cpp +++ b/src/tools/clangrefactoringbackend/source/refactoringserver.cpp @@ -62,10 +62,9 @@ void RefactoringServer::requestSourceLocationsForRenamingMessage(RequestSourceLo { SymbolFinder symbolFinder(message.line, message.column, m_filePathCache); - symbolFinder.addFile(std::string(message.filePath.directory()), - std::string(message.filePath.name()), - std::string(message.unsavedContent), - std::vector(message.commandLine)); + symbolFinder.addFile(std::move(message.filePath), + std::move(message.unsavedContent), + std::move(message.commandLine)); symbolFinder.findSymbol(); @@ -79,10 +78,9 @@ void RefactoringServer::requestSourceRangesAndDiagnosticsForQueryMessage( { ClangQuery clangQuery(m_filePathCache, message.takeQuery()); - clangQuery.addFile(std::string(message.source.filePath.directory()), - std::string(message.source.filePath.name()), - std::string(message.source.unsavedFileContent), - std::vector(message.source.commandLineArguments)); + clangQuery.addFile(std::move(message.source.filePath), + std::move(message.source.unsavedFileContent), + std::move(message.source.commandLineArguments)); clangQuery.findLocations(); -- cgit v1.2.1