diff --git a/src/libs/clangsupport/commandlinebuilder.h b/src/libs/clangsupport/commandlinebuilder.h index 9b5bced9328..03d042322d9 100644 --- a/src/libs/clangsupport/commandlinebuilder.h +++ b/src/libs/clangsupport/commandlinebuilder.h @@ -25,7 +25,7 @@ #pragma once -#include "filepathview.h" +#include "filepath.h" #include #include @@ -35,7 +35,7 @@ namespace ClangBackEnd { -template> +template class CommandLineBuilder { public: @@ -213,7 +213,7 @@ public: { for (const IncludeSearchPath &path : projectIncludeSearchPaths) { commandLine.emplace_back("-I"); - commandLine.emplace_back(path.path); + commandLine.emplace_back(NativeFilePath(FilePathView(path.path))); } } @@ -228,7 +228,7 @@ public: for (const IncludeSearchPath &path : systemIncludeSearchPaths) { if (path.type != IncludeSearchPathType::BuiltIn) { commandLine.emplace_back(includeOption(path.type)); - commandLine.emplace_back(path.path); + commandLine.emplace_back(NativeFilePath(FilePathView(path.path))); } } } @@ -238,7 +238,7 @@ public: for (const IncludeSearchPath &path : systemIncludeSearchPaths) { if (path.type == IncludeSearchPathType::BuiltIn) { commandLine.emplace_back(includeOption(path.type)); - commandLine.emplace_back(path.path); + commandLine.emplace_back(NativeFilePath(FilePathView(path.path))); } } } @@ -247,23 +247,24 @@ public: { if (!outputPath.isEmpty()) { commandLine.emplace_back("-o"); - commandLine.emplace_back(outputPath); + commandLine.emplace_back(NativeFilePath(outputPath)); } } void addSourcePath(FilePathView sourcePath) { if (!sourcePath.isEmpty()) - commandLine.emplace_back(sourcePath); + commandLine.emplace_back(NativeFilePath(sourcePath)); } void addIncludePchPath(FilePathView includePchPath) { + if (!includePchPath.isEmpty()) { commandLine.emplace_back("-Xclang"); commandLine.emplace_back("-include-pch"); commandLine.emplace_back("-Xclang"); - commandLine.emplace_back(includePchPath); + commandLine.emplace_back(NativeFilePath(includePchPath)); } } diff --git a/src/libs/clangsupport/filepath.h b/src/libs/clangsupport/filepath.h index e08f3f4996b..72076841945 100644 --- a/src/libs/clangsupport/filepath.h +++ b/src/libs/clangsupport/filepath.h @@ -28,7 +28,7 @@ #include "clangsupport_global.h" #include "filepathview.h" -#include "filepathview.h" +#include "nativefilepath.h" #include #include @@ -37,6 +37,8 @@ namespace ClangBackEnd { +class NativeFilePath; + class FilePath : public Utils::PathString { using size_type = Utils::PathString::size_type; diff --git a/src/libs/clangsupport/nativefilepath.h b/src/libs/clangsupport/nativefilepath.h index 31b6778ed04..fefaa9c2057 100644 --- a/src/libs/clangsupport/nativefilepath.h +++ b/src/libs/clangsupport/nativefilepath.h @@ -26,13 +26,15 @@ #pragma once #include "filepathview.h" -#include "filepathview.h" +#include "filepath.h" #include #include namespace ClangBackEnd { +class FilePath; + class NativeFilePath { using size_type = Utils::PathString::size_type; @@ -50,8 +52,13 @@ public: NativeFilePath(NativeFilePathView filePathView) : m_path(filePathView.toStringView()), m_slashIndex(filePathView.slashIndex()) - { - } + {} + + explicit NativeFilePath(FilePathView filePathView) { *this = fromFilePath(filePathView); } + + explicit NativeFilePath(const FilePath &filePath) + : NativeFilePath{FilePathView{filePath}} + {} template NativeFilePath(const char(&string)[Size]) noexcept diff --git a/src/libs/utils/smallstringview.h b/src/libs/utils/smallstringview.h index d821edbf9b8..6052ff8777f 100644 --- a/src/libs/utils/smallstringview.h +++ b/src/libs/utils/smallstringview.h @@ -90,8 +90,7 @@ public: SmallStringView(const String &string) noexcept : m_pointer(string.data()), m_size(string.size()) - { - } + {} static SmallStringView fromUtf8(const char *const characterPointer) diff --git a/src/tools/clangpchmanagerbackend/source/pchcreator.cpp b/src/tools/clangpchmanagerbackend/source/pchcreator.cpp index 777252dece5..4333d5706ac 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 1e36e18b6e1..37d0c7c8a1e 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 21cd0d26e54..42fb7d88bc2 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 66f85c6d36d..5ae87793dbd 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())); + std::string filePathStr(filePath.path()); + auto commandLine = arguments; + NativeFilePath nativeFilePath{filePath}; - addFile(filePath.directory(), - filePath.name(), - {}, - std::move(commandLine)); + commandLine.push_back(nativeFilePath.path()); + + addFile(filePath.clone(), {}, 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::vector commandLine(arguments.begin(), arguments.end()); - commandLine.push_back(std::string(filePath)); - - addFile({filePath.begin(), std::prev(fileNameBegin)}, - {fileNameBegin, filePath.end()}, - {}, - 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 8b4e47df682..e3ff8357998 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 c5906aaae47..1e1609d1a4f 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 ca2d1338191..b25e43e4834 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 4b51ee0d69d..83aa5a31fd8 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(); diff --git a/tests/unit/unittest/clangquery-test.cpp b/tests/unit/unittest/clangquery-test.cpp index 8c486a47d78..35842e01439 100644 --- a/tests/unit/unittest/clangquery-test.cpp +++ b/tests/unit/unittest/clangquery-test.cpp @@ -94,7 +94,9 @@ TEST_F(ClangQuerySlowTest, RootSourceRangeForSimpleFunctionDeclarationRange) TEST_F(ClangQuerySlowTest, SourceRangeInUnsavedFileDeclarationRange) { ::ClangQuery query(filePathCache); - query.addFile(TESTDATA_DIR, "query_simplefunction.cpp", "#include \"unsaved.h\"", {"cc", toNativePath(TESTDATA_DIR"/query_simplefunction.cpp"), "-std=c++14"}); + query.addFile({TESTDATA_DIR "/query_simplefunction.cpp"}, + "#include \"unsaved.h\"", + {"cc", toNativePath(TESTDATA_DIR "/query_simplefunction.cpp").path(), "-std=c++14"}); query.setQuery("functionDecl()"); ClangBackEnd::V2::FileContainer unsavedFile{{TESTDATA_DIR, "unsaved.h"}, "void unsaved();", {}}; query.addUnsavedFiles({unsavedFile}); @@ -108,7 +110,9 @@ TEST_F(ClangQuerySlowTest, SourceRangeInUnsavedFileDeclarationRange) TEST_F(ClangQuerySlowTest, FileIsNotExistingButTheUnsavedDataIsParsed) { ::ClangQuery query(filePathCache); - query.addFile(TESTDATA_DIR, "foo.cpp", "void f() {}", {"cc", toNativePath(TESTDATA_DIR"/foo.cpp"), "-std=c++14"}); + query.addFile({TESTDATA_DIR "/foo.cpp"}, + "void f() {}", + {"cc", toNativePath(TESTDATA_DIR "/foo.cpp").path(), "-std=c++14"}); query.setQuery("functionDecl()"); query.findLocations(); @@ -120,9 +124,13 @@ TEST_F(ClangQuerySlowTest, FileIsNotExistingButTheUnsavedDataIsParsed) TEST_F(ClangQuerySlowTest, DISABLED_SourceRangeInUnsavedFileDeclarationRangeOverride) // seems not to work in Clang { ::ClangQuery query(filePathCache); - query.addFile(TESTDATA_DIR, "query_simplefunction.cpp", "void f() {}", {"cc", toNativePath(TESTDATA_DIR"/query_simplefunction.cpp"), "-std=c++14"}); + query.addFile({TESTDATA_DIR "/query_simplefunction.cpp"}, + "void f() {}", + {"cc", toNativePath(TESTDATA_DIR "/query_simplefunction.cpp").path(), "-std=c++14"}); query.setQuery("functionDecl()"); - ClangBackEnd::V2::FileContainer unsavedFile{{TESTDATA_DIR, "query_simplefunction.cpp"}, "void unsaved();", {}}; + ClangBackEnd::V2::FileContainer unsavedFile{{TESTDATA_DIR "/query_simplefunction.cpp"}, + "void unsaved();", + {}}; query.addUnsavedFiles({unsavedFile}); query.findLocations(); @@ -215,8 +223,15 @@ TEST_F(ClangQuerySlowTest, DiagnosticForWrongArgumenType) void ClangQuery::SetUp() { - simpleFunctionQuery.addFile(TESTDATA_DIR, "query_simplefunction.cpp", "", {"cc", toNativePath(TESTDATA_DIR"/query_simplefunction.cpp"), "-std=c++14"}); - simpleClassQuery.addFile(TESTDATA_DIR, "query_simpleclass.cpp", "", {"cc", "query_simpleclass.cpp", "-std=c++14"}); - -} + simpleFunctionQuery.addFile({TESTDATA_DIR "/query_simplefunction.cpp"}, + "", + {"cc", + toNativePath(TESTDATA_DIR "/query_simplefunction.cpp").path(), + "-std=c++14"}); + simpleClassQuery.addFile({TESTDATA_DIR "/query_simpleclass.cpp"}, + "", + {"cc", + toNativePath(TESTDATA_DIR "/query_simpleclass.cpp").path(), + "-std=c++14"}); } +} // namespace diff --git a/tests/unit/unittest/clangquerygatherer-test.cpp b/tests/unit/unittest/clangquerygatherer-test.cpp index 9c575f6a0f5..b0ea9cb3e0e 100644 --- a/tests/unit/unittest/clangquerygatherer-test.cpp +++ b/tests/unit/unittest/clangquerygatherer-test.cpp @@ -87,13 +87,22 @@ protected: Utils::SmallString sourceContent{"#include \"query_simplefunction.h\"\nvoid f() {}"}; FileContainer source{{TESTDATA_DIR, "query_simplefunction.cpp"}, sourceContent.clone(), - {"cc", toNativePath(TESTDATA_DIR"/query_simplefunction.cpp"), "-I", TESTDATA_DIR}}; + {"cc", + toNativePath(TESTDATA_DIR "/query_simplefunction.cpp").path(), + "-I", + TESTDATA_DIR}}; FileContainer source2{{TESTDATA_DIR, "query_simplefunction2.cpp"}, {}, - {"cc", toNativePath(TESTDATA_DIR"/query_simplefunction2.cpp"), "-I", TESTDATA_DIR}}; + {"cc", + toNativePath(TESTDATA_DIR "/query_simplefunction2.cpp").path(), + "-I", + TESTDATA_DIR}}; FileContainer source3{{TESTDATA_DIR, "query_simplefunction3.cpp"}, {}, - {"cc", toNativePath(TESTDATA_DIR"/query_simplefunction3.cpp"), "-I", TESTDATA_DIR}}; + {"cc", + toNativePath(TESTDATA_DIR "/query_simplefunction3.cpp").path(), + "-I", + TESTDATA_DIR}}; Utils::SmallString unsavedContent{"void f();"}; FileContainer unsaved{{TESTDATA_DIR, "query_simplefunction.h"}, unsavedContent.clone(), diff --git a/tests/unit/unittest/commandlinebuilder-test.cpp b/tests/unit/unittest/commandlinebuilder-test.cpp index c3dad70de3c..9de48fe0d55 100644 --- a/tests/unit/unittest/commandlinebuilder-test.cpp +++ b/tests/unit/unittest/commandlinebuilder-test.cpp @@ -25,6 +25,8 @@ #include "googletest.h" +#include "filesystem-utilities.h" + #include #include #include @@ -136,7 +138,7 @@ TYPED_TEST(CommandLineBuilder, CTask) ASSERT_THAT( builder.commandLine, - ElementsAre("clang", "-x", "c-header", "-std=c11", "-nostdinc", "-nostdinc++", "/source/file.c")); + ElementsAre("clang", "-x", "c-header", "-std=c11", "-nostdinc", "-nostdinc++", toNativePath("/source/file.c").path())); } TYPED_TEST(CommandLineBuilder, ObjectiveCTask) @@ -154,7 +156,7 @@ TYPED_TEST(CommandLineBuilder, ObjectiveCTask) "-std=c11", "-nostdinc", "-nostdinc++", - "/source/file.c")); + toNativePath("/source/file.c").path())); } TYPED_TEST(CommandLineBuilder, CppTask) @@ -171,7 +173,7 @@ TYPED_TEST(CommandLineBuilder, CppTask) "-std=c++98", "-nostdinc", "-nostdinc++", - "/source/file.cpp")); + toNativePath("/source/file.cpp").path())); } TYPED_TEST(CommandLineBuilder, ObjectiveCppTask) @@ -189,7 +191,7 @@ TYPED_TEST(CommandLineBuilder, ObjectiveCppTask) "-std=c++98", "-nostdinc", "-nostdinc++", - "/source/file.cpp")); + toNativePath("/source/file.cpp").path())); } TYPED_TEST(CommandLineBuilder, Cpp98) @@ -422,18 +424,18 @@ TYPED_TEST(CommandLineBuilder, IncludesOrder) "-nostdinc", "-nostdinc++", "-I", - "/include/foo", + toNativePath("/include/foo").path(), "-I", - "/include/bar", + toNativePath("/include/bar").path(), "-F", - "/system/foo", + toNativePath("/system/foo").path(), "-isystem", - "/system/bar", + toNativePath("/system/bar").path(), "-isystem", - "/builtin/foo", + toNativePath("/builtin/foo").path(), "-isystem", - "/builtin/bar", - "/source/file.cpp")); + toNativePath("/builtin/bar").path(), + toNativePath("/source/file.cpp").path())); } TYPED_TEST(CommandLineBuilder, EmptySourceFile) @@ -455,7 +457,7 @@ TYPED_TEST(CommandLineBuilder, SourceFile) "-std=c++98", "-nostdinc", "-nostdinc++", - "/source/file.cpp")); + toNativePath("/source/file.cpp").path())); } @@ -470,7 +472,7 @@ TYPED_TEST(CommandLineBuilder, EmptyOutputFile) "-std=c++98", "-nostdinc", "-nostdinc++", - "/source/file.cpp")); + toNativePath("/source/file.cpp").path())); } TYPED_TEST(CommandLineBuilder, OutputFile) @@ -485,8 +487,8 @@ TYPED_TEST(CommandLineBuilder, OutputFile) "-nostdinc", "-nostdinc++", "-o", - "/output/file.o", - "/source/file.cpp")); + toNativePath("/output/file.o").path(), + toNativePath("/source/file.cpp").path())); } TYPED_TEST(CommandLineBuilder, IncludePchPath) @@ -503,10 +505,10 @@ TYPED_TEST(CommandLineBuilder, IncludePchPath) "-Xclang", "-include-pch", "-Xclang", - "/pch/file.pch", + toNativePath("/pch/file.pch").path(), "-o", - "/output/file.o", - "/source/file.cpp")); + toNativePath("/output/file.o").path(), + toNativePath("/source/file.cpp").path())); } TYPED_TEST(CommandLineBuilder, CompilerMacros) diff --git a/tests/unit/unittest/filesystem-utilities.h b/tests/unit/unittest/filesystem-utilities.h index 3bbafdf3686..9a4fd8e2db7 100644 --- a/tests/unit/unittest/filesystem-utilities.h +++ b/tests/unit/unittest/filesystem-utilities.h @@ -26,32 +26,27 @@ #pragma once #include -#include -#include +#include -// use std::filesystem::path if it is supported by all compilers -static const char nativeSeparator = Utils::HostOsInfo::isWindowsHost() ? '\\' : '/'; - - -template -std::string toNativePath(const char (&text)[Size]) +template +ClangBackEnd::NativeFilePath toNativePath(const char (&text)[Size]) { - std::string path = text; + ClangBackEnd::FilePath path = text; - if (Utils::HostOsInfo::isWindowsHost()) - std::replace(path.begin(), path.end(), '/', '\\'); - - return path; + return ClangBackEnd::NativeFilePath{path}; } -inline -std::string toNativePath(const QString &qStringPath) +inline ClangBackEnd::NativeFilePath toNativePath(const QString &text) { - auto path = qStringPath.toStdString(); + ClangBackEnd::FilePath path{text}; - if (Utils::HostOsInfo::isWindowsHost()) - std::replace(path.begin(), path.end(), '/', '\\'); - - return path; + return ClangBackEnd::NativeFilePath{path}; +} + +inline ClangBackEnd::NativeFilePath toNativePath(Utils::SmallStringView text) +{ + ClangBackEnd::FilePath path{text}; + + return ClangBackEnd::NativeFilePath{path}; } diff --git a/tests/unit/unittest/gtest-creator-printing.cpp b/tests/unit/unittest/gtest-creator-printing.cpp index 3901cb0d305..30660376e1f 100644 --- a/tests/unit/unittest/gtest-creator-printing.cpp +++ b/tests/unit/unittest/gtest-creator-printing.cpp @@ -69,10 +69,24 @@ #include #include +#include + namespace { ClangBackEnd::FilePathCaching *filePathCache = nullptr; } +namespace clang { +namespace tooling { +struct CompileCommand; + +std::ostream &operator<<(std::ostream &out, const CompileCommand &command) +{ + return out << "(" << command.Directory << ", " << command.Filename << ", " + << command.CommandLine << ", " << command.Output << ")"; +} +} // namespace tooling +} // namespace clang + void PrintTo(const Utf8String &text, ::std::ostream *os) { *os << text; diff --git a/tests/unit/unittest/gtest-creator-printing.h b/tests/unit/unittest/gtest-creator-printing.h index ec54af30850..d0f0013f609 100644 --- a/tests/unit/unittest/gtest-creator-printing.h +++ b/tests/unit/unittest/gtest-creator-printing.h @@ -40,6 +40,14 @@ class Utf8String; void PrintTo(const Utf8String &text, ::std::ostream *os); +namespace clang { +namespace tooling { +struct CompileCommand; + +std::ostream &operator<<(std::ostream &out, const CompileCommand &command); +} // namespace tooling +} // namespace clang + namespace Core { class LocatorFilterEntry; diff --git a/tests/unit/unittest/nativefilepath-test.cpp b/tests/unit/unittest/nativefilepath-test.cpp index 864c91dc608..327cff42dc6 100644 --- a/tests/unit/unittest/nativefilepath-test.cpp +++ b/tests/unit/unittest/nativefilepath-test.cpp @@ -25,6 +25,7 @@ #include "googletest.h" +#include #include namespace { diff --git a/tests/unit/unittest/pchcreator-test.cpp b/tests/unit/unittest/pchcreator-test.cpp index a75ff4267ab..8a1e1ffa8ef 100644 --- a/tests/unit/unittest/pchcreator-test.cpp +++ b/tests/unit/unittest/pchcreator-test.cpp @@ -26,6 +26,7 @@ #include "googletest.h" #include "fakeprocess.h" +#include "filesystem-utilities.h" #include "mockclangpathwatcher.h" #include "mockpchmanagerclient.h" @@ -144,11 +145,11 @@ TEST_F(PchCreator, CreateProjectPartClangCompilerArguments) "-nostdinc", "-nostdinc++", "-I", - TESTDATA_DIR "/builddependencycollector/project", + toNativePath(TESTDATA_DIR "/builddependencycollector/project").path(), "-isystem", - TESTDATA_DIR "/builddependencycollector/external", + toNativePath(TESTDATA_DIR "/builddependencycollector/external").path(), "-isystem", - TESTDATA_DIR "/builddependencycollector/system", + toNativePath(TESTDATA_DIR "/builddependencycollector/system").path(), "-o", "project.pch", "project.h")); @@ -170,11 +171,11 @@ TEST_F(PchCreator, CreateProjectPartClangCompilerArgumentsWithSystemPch) "-nostdinc", "-nostdinc++", "-I", - TESTDATA_DIR "/builddependencycollector/project", + toNativePath(TESTDATA_DIR "/builddependencycollector/project").path(), "-isystem", - TESTDATA_DIR "/builddependencycollector/external", + toNativePath(TESTDATA_DIR "/builddependencycollector/external").path(), "-isystem", - TESTDATA_DIR "/builddependencycollector/system", + toNativePath(TESTDATA_DIR "/builddependencycollector/system").path(), "-Xclang", "-include-pch", "-Xclang", diff --git a/tests/unit/unittest/refactoringcompilationdatabase-test.cpp b/tests/unit/unittest/refactoringcompilationdatabase-test.cpp index b635d174a98..59f88f8b53d 100644 --- a/tests/unit/unittest/refactoringcompilationdatabase-test.cpp +++ b/tests/unit/unittest/refactoringcompilationdatabase-test.cpp @@ -24,8 +24,10 @@ ****************************************************************************/ #include "googletest.h" +#include "filesystem-utilities.h" #include +#include #include @@ -38,16 +40,16 @@ using testing::PrintToString; namespace { -MATCHER_P3(IsCompileCommand, directory, fileName, commandLine, - std::string(negation ? "isn't" : "is") - + " compile command with directory "+ PrintToString(directory) - + ", file name " + PrintToString(fileName) - + " and command line " + PrintToString(commandLine) - ) +MATCHER_P3(IsCompileCommand, + directory, + filePath, + commandLine, + std::string(negation ? "isn't" : "is") + " compile command with directory " + + PrintToString(directory) + ", file name " + PrintToString(filePath) + + " and command line " + PrintToString(commandLine)) { - if (arg.Directory != directory - || arg.Filename != fileName - || arg.CommandLine != commandLine) + if (arg.Directory != std::string(directory) || arg.Filename != std::string(filePath) + || arg.CommandLine != commandLine) return false; return true; @@ -56,7 +58,11 @@ MATCHER_P3(IsCompileCommand, directory, fileName, commandLine, class RefactoringCompilationDatabase : public ::testing::Test { protected: - void SetUp(); + RefactoringCompilationDatabase() + { + database.addFile(ClangBackEnd::NativeFilePathView{temporarySourceFilePath}, + {"cc", toNativePath(temporaryDirectoryPath + "/data.cpp").path(), "-DNO_DEBUG"}); + } protected: ClangBackEnd::RefactoringCompilationDatabase database; @@ -76,18 +82,28 @@ TEST_F(RefactoringCompilationDatabase, CompileCommandForFilePath) { auto compileCommands = database.getAllCompileCommands(); - ASSERT_THAT(compileCommands, Contains(IsCompileCommand(temporaryDirectoryPath, - "data.cpp", - std::vector{"cc", "data.cpp", "-DNO_DEBUG"}))); + ASSERT_THAT(compileCommands, + Contains(IsCompileCommand( + temporaryDirectoryPath, + toNativePath(temporaryDirectoryPath + "/data.cpp").path(), + std::vector{ + "cc", + std::string(toNativePath(temporaryDirectoryPath + "/data.cpp").path()), + "-DNO_DEBUG"}))); } TEST_F(RefactoringCompilationDatabase, NoCompileCommandForFilePath) { auto compileCommands = database.getAllCompileCommands(); - ASSERT_THAT(compileCommands, Not(Contains(IsCompileCommand(temporaryDirectoryPath, - "data.cpp2", - std::vector{"cc", "data.cpp", "-DNO_DEBUG"})))); + ASSERT_THAT(compileCommands, + Not(Contains(IsCompileCommand( + temporaryDirectoryPath, + toNativePath(temporaryDirectoryPath + "/data.cpp2").path(), + std::vector{ + "cc", + std::string(toNativePath(temporaryDirectoryPath + "/data.cpp").path()), + "-DNO_DEBUG"})))); } TEST_F(RefactoringCompilationDatabase, FilePaths) @@ -96,10 +112,4 @@ TEST_F(RefactoringCompilationDatabase, FilePaths) ASSERT_THAT(filePaths, Contains(std::string(temporarySourceFilePath))); } - -void RefactoringCompilationDatabase::SetUp() -{ - database.addFile(std::string(temporaryDirectoryPath), "data.cpp", {"cc", "data.cpp", "-DNO_DEBUG"}); -} - -} +} // namespace diff --git a/tests/unit/unittest/refactoringserver-test.cpp b/tests/unit/unittest/refactoringserver-test.cpp index b8b97437e4a..56f5e6950dd 100644 --- a/tests/unit/unittest/refactoringserver-test.cpp +++ b/tests/unit/unittest/refactoringserver-test.cpp @@ -102,8 +102,9 @@ protected: Utils::SmallString sourceContent{"void f()\n {}"}; FileContainer source{{TESTDATA_DIR, "query_simplefunction.cpp"}, sourceContent.clone(), - {"cc", toNativePath(TESTDATA_DIR"/query_simplefunction.cpp")}}; - QTemporaryFile temporaryFile{Utils::TemporaryDirectory::masterDirectoryPath() + "/clangQuery-XXXXXX.cpp"}; + {"cc", toNativePath(TESTDATA_DIR "/query_simplefunction.cpp").path()}}; + QTemporaryFile temporaryFile{Utils::TemporaryDirectory::masterDirectoryPath() + + "/clangQuery-XXXXXX.cpp"}; int processingSlotCount = 2; }; @@ -266,10 +267,11 @@ TEST_F(RefactoringServer, PollTimerNotIsActiveAfterCanceling) TEST_F(RefactoringServerSlowTest, ForValidRequestSourceRangesAndDiagnosticsGetSourceRange) { - RequestSourceRangesAndDiagnosticsForQueryMessage message("functionDecl()", - {FilePath(temporaryFile.fileName()), - "void f() {}", - {"cc", toNativePath(temporaryFile.fileName())}}); + RequestSourceRangesAndDiagnosticsForQueryMessage message( + "functionDecl()", + {FilePath(temporaryFile.fileName()), + "void f() {}", + {"cc", toNativePath(temporaryFile.fileName()).path()}}); EXPECT_CALL(mockRefactoringClient, sourceRangesAndDiagnosticsForQueryMessage( @@ -285,10 +287,11 @@ TEST_F(RefactoringServerSlowTest, ForValidRequestSourceRangesAndDiagnosticsGetSo TEST_F(RefactoringServerSlowTest, ForInvalidRequestSourceRangesAndDiagnosticsGetDiagnostics) { - RequestSourceRangesAndDiagnosticsForQueryMessage message("func()", - {FilePath(temporaryFile.fileName()), - "void f() {}", - {"cc", toNativePath(temporaryFile.fileName())}}); + RequestSourceRangesAndDiagnosticsForQueryMessage message( + "func()", + {FilePath(temporaryFile.fileName()), + "void f() {}", + {"cc", toNativePath(temporaryFile.fileName()).path()}}); EXPECT_CALL(mockRefactoringClient, sourceRangesAndDiagnosticsForQueryMessage( diff --git a/tests/unit/unittest/sourcerangeextractor-test.cpp b/tests/unit/unittest/sourcerangeextractor-test.cpp index ae9d670bf4b..171db9b956d 100644 --- a/tests/unit/unittest/sourcerangeextractor-test.cpp +++ b/tests/unit/unittest/sourcerangeextractor-test.cpp @@ -57,7 +57,9 @@ protected: void TearDown() override; protected: - TestClangTool clangTool{TESTDATA_DIR, "sourcerangeextractor_location.cpp", "", {"cc", "sourcerangeextractor_location.cpp"}}; + TestClangTool clangTool{{TESTDATA_DIR "/sourcerangeextractor_location.cpp"}, + "", + {"cc", "sourcerangeextractor_location.cpp"}}; ClangBackEnd::SourceRangesContainer sourceRangesContainer; const clang::SourceManager &sourceManager{clangTool.sourceManager()}; Sqlite::Database database{":memory:", Sqlite::JournalMode::Memory}; diff --git a/tests/unit/unittest/symbolfinder-test.cpp b/tests/unit/unittest/symbolfinder-test.cpp index 6ab0bd06cd9..1dad3184d68 100644 --- a/tests/unit/unittest/symbolfinder-test.cpp +++ b/tests/unit/unittest/symbolfinder-test.cpp @@ -31,28 +31,30 @@ #include "filesystem-utilities.h" -using ClangBackEnd::SymbolFinder; using ClangBackEnd::FileContent; - +using ClangBackEnd::FilePathView; +using ClangBackEnd::NativeFilePath; +using ClangBackEnd::NativeFilePathView; +using ClangBackEnd::SymbolFinder; namespace { -MATCHER_P2(IsSourceLocation, line, column, - std::string(negation ? "isn't" : "is") - + "line " + PrintToString(line) - + ", column " + PrintToString(column) - ) +inline bool operator==(const ClangBackEnd::NativeFilePath &first, const std::string &second) +{ + return first.path() == second; +} +MATCHER_P2(IsSourceLocation, + line, + column, + std::string(negation ? "isn't" : "is") + "line " + PrintToString(line) + ", column " + + PrintToString(column)) { return arg.line == uint(line) && arg.column == uint(column); } -MATCHER_P(StrEq, text, - std::string(negation ? "isn't" : "is") - + " text " + PrintToString(text) - ) +MATCHER_P(StrEq, text, std::string(negation ? "isn't" : "is") + " text " + PrintToString(text)) { return std::string(arg.data(), arg.size()) == std::string(text); } - using Finder = SymbolFinder; class SymbolFinder : public testing::Test @@ -65,7 +67,7 @@ using SymbolFinderSlowTest = SymbolFinder; TEST_F(SymbolFinder, FileContentFilePath) { - FileContent fileContent(toNativePath("/tmp"), "data.cpp", "int variable;", {"cc", "data.cpp"}); + FileContent fileContent(NativeFilePath{FilePathView{"/tmp/data.cpp"}}, "int variable;"); ASSERT_THAT(fileContent.filePath, toNativePath("/tmp/data.cpp")); } @@ -73,7 +75,9 @@ TEST_F(SymbolFinder, FileContentFilePath) TEST_F(SymbolFinderSlowTest, FindName) { Finder finder(1, 5, filePathCaching); - finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "int variable;", {"cc", "renamevariable.cpp"}); + finder.addFile({TESTDATA_DIR "/renamevariable.cpp"}, + "int variable;", + {"cc", toNativePath(TESTDATA_DIR "/renamevariable.cpp").path()}); finder.findSymbol(); @@ -83,7 +87,9 @@ TEST_F(SymbolFinderSlowTest, FindName) TEST_F(SymbolFinderSlowTest, FindNameInUnsavedFile) { Finder finder(1, 5, filePathCaching); - finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "int newVariable;", {"cc", "renamevariable.cpp"}); + finder.addFile({TESTDATA_DIR "/renamevariable.cpp"}, + "int newVariable;", + {"cc", toNativePath(TESTDATA_DIR "/renamevariable.cpp").path()}); finder.findSymbol(); @@ -93,7 +99,9 @@ TEST_F(SymbolFinderSlowTest, FindNameInUnsavedFile) TEST_F(SymbolFinderSlowTest, FindUsrs) { Finder finder(1, 5, filePathCaching); - finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "int variable;", {"cc", "renamevariable.cpp", "-std=c++14"}); + finder.addFile({TESTDATA_DIR "/renamevariable.cpp"}, + "int variable;", + {"cc", toNativePath(TESTDATA_DIR "/renamevariable.cpp").path(), "-std=c++14"}); finder.findSymbol(); @@ -103,7 +111,9 @@ TEST_F(SymbolFinderSlowTest, FindUsrs) TEST_F(SymbolFinderSlowTest, VariableDeclarationSourceLocations) { Finder finder(1, 5, filePathCaching); - finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "", {"cc", "renamevariable.cpp", "-std=c++14"}); + finder.addFile({TESTDATA_DIR "/renamevariable.cpp"}, + "", + {"cc", toNativePath(TESTDATA_DIR "/renamevariable.cpp").path(), "-std=c++14"}); finder.findSymbol(); @@ -115,7 +125,9 @@ TEST_F(SymbolFinderSlowTest, VariableDeclarationSourceLocations) TEST_F(SymbolFinderSlowTest, VariableUsageSourceLocations) { Finder finder(3, 9, filePathCaching); - finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "", {"cc", "renamevariable.cpp", "-std=c++14"}); + finder.addFile({TESTDATA_DIR "/renamevariable.cpp"}, + "", + {"cc", toNativePath(TESTDATA_DIR "/renamevariable.cpp").path(), "-std=c++14"}); finder.findSymbol(); @@ -127,7 +139,9 @@ TEST_F(SymbolFinderSlowTest, VariableUsageSourceLocations) TEST_F(SymbolFinderSlowTest, TemplateMemberVariableDeclarationSourceLocations) { Finder finder(8, 18, filePathCaching); - finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "", {"cc", "renamevariable.cpp", "-std=c++14"}); + finder.addFile({TESTDATA_DIR "/renamevariable.cpp"}, + "", + {"cc", toNativePath(TESTDATA_DIR "/renamevariable.cpp").path(), "-std=c++14"}); finder.findSymbol(); @@ -140,7 +154,9 @@ TEST_F(SymbolFinderSlowTest, TemplateMemberVariableDeclarationSourceLocations) TEST_F(SymbolFinderSlowTest, TemplateMemberVariableUsageSourceLocations) { Finder finder(15, 14, filePathCaching); - finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "", {"cc", "renamevariable.cpp", "-std=c++14"}); + finder.addFile({TESTDATA_DIR "/renamevariable.cpp"}, + "", + {"cc", toNativePath(TESTDATA_DIR "/renamevariable.cpp").path(), "-std=c++14"}); finder.findSymbol(); @@ -153,7 +169,9 @@ TEST_F(SymbolFinderSlowTest, TemplateMemberVariableUsageSourceLocations) TEST_F(SymbolFinderSlowTest, TemplateMemberVariableUsageInLambdaSourceLocations) { Finder finder(18, 19, filePathCaching); - finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "", {"cc", "renamevariable.cpp", "-std=c++14"}); + finder.addFile({TESTDATA_DIR "/renamevariable.cpp"}, + "", + {"cc", toNativePath(TESTDATA_DIR "/renamevariable.cpp").path(), "-std=c++14"}); finder.findSymbol(); @@ -166,7 +184,9 @@ TEST_F(SymbolFinderSlowTest, TemplateMemberVariableUsageInLambdaSourceLocations) TEST_F(SymbolFinderSlowTest, CursorOverMacroDefintionSymbolName) { Finder finder(1, 9, filePathCaching); - finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"}); + finder.addFile({TESTDATA_DIR "/symbolfinder_macro.cpp"}, + "", + {"cc", toNativePath(TESTDATA_DIR "/symbolfinder_macro.cpp").path()}); finder.findSymbol(); @@ -176,7 +196,9 @@ TEST_F(SymbolFinderSlowTest, CursorOverMacroDefintionSymbolName) TEST_F(SymbolFinderSlowTest, CursorOverMacroExpansionSymbolName) { Finder finder(10, 10, filePathCaching); - finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"}); + finder.addFile({TESTDATA_DIR "/symbolfinder_macro.cpp"}, + "", + {"cc", toNativePath(TESTDATA_DIR "/symbolfinder_macro.cpp").path()}); finder.findSymbol(); @@ -186,7 +208,9 @@ TEST_F(SymbolFinderSlowTest, CursorOverMacroExpansionSymbolName) TEST_F(SymbolFinderSlowTest, FindMacroDefinition) { Finder finder(1, 9, filePathCaching); - finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"}); + finder.addFile({TESTDATA_DIR "/symbolfinder_macro.cpp"}, + "", + {"cc", toNativePath(TESTDATA_DIR "/symbolfinder_macro.cpp").path()}); finder.findSymbol(); @@ -197,7 +221,9 @@ TEST_F(SymbolFinderSlowTest, FindMacroDefinition) TEST_F(SymbolFinderSlowTest, FindMacroExpansion) { Finder finder(1, 9, filePathCaching); - finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"}); + finder.addFile({TESTDATA_DIR "/symbolfinder_macro.cpp"}, + "", + {"cc", toNativePath(TESTDATA_DIR "/symbolfinder_macro.cpp").path()}); finder.findSymbol(); @@ -208,7 +234,9 @@ TEST_F(SymbolFinderSlowTest, FindMacroExpansion) TEST_F(SymbolFinderSlowTest, DoNotFindUndedefinedMacroExpansion) { Finder finder(1, 9, filePathCaching); - finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"}); + finder.addFile({TESTDATA_DIR "/symbolfinder_macro.cpp"}, + "", + {"cc", toNativePath(TESTDATA_DIR "/symbolfinder_macro.cpp").path()}); finder.findSymbol(); @@ -219,7 +247,9 @@ TEST_F(SymbolFinderSlowTest, DoNotFindUndedefinedMacroExpansion) TEST_F(SymbolFinderSlowTest, FindMacroDefinitionFromMacroExpansion) { Finder finder(10, 10, filePathCaching); - finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"}); + finder.addFile({TESTDATA_DIR "/symbolfinder_macro.cpp"}, + "", + {"cc", toNativePath(TESTDATA_DIR "/symbolfinder_macro.cpp").path()}); finder.findSymbol(); @@ -231,7 +261,9 @@ TEST_F(SymbolFinderSlowTest, FindMacroDefinitionFromMacroExpansion) TEST_F(SymbolFinderSlowTest, FindMacroExpansionBeforeMacroExpansionWithCursor) { Finder finder(12, 10, filePathCaching); - finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"}); + finder.addFile({TESTDATA_DIR "/symbolfinder_macro.cpp"}, + "", + {"cc", toNativePath(TESTDATA_DIR "/symbolfinder_macro.cpp").path()}); finder.findSymbol(); @@ -242,7 +274,9 @@ TEST_F(SymbolFinderSlowTest, FindMacroExpansionBeforeMacroExpansionWithCursor) TEST_F(SymbolFinderSlowTest, FindMacroExpansionAfterMacroExpansionWithCursor) { Finder finder(10, 10, filePathCaching); - finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"}); + finder.addFile({TESTDATA_DIR "/symbolfinder_macro.cpp"}, + "", + {"cc", toNativePath(TESTDATA_DIR "/symbolfinder_macro.cpp").path()}); finder.findSymbol(); @@ -250,4 +284,4 @@ TEST_F(SymbolFinderSlowTest, FindMacroExpansionAfterMacroExpansionWithCursor) Contains(IsSourceLocation(12, 10))); } -} +} // namespace diff --git a/tests/unit/unittest/symbolindexer-test.cpp b/tests/unit/unittest/symbolindexer-test.cpp index 104c17b11b3..ce0fadfdf8c 100644 --- a/tests/unit/unittest/symbolindexer-test.cpp +++ b/tests/unit/unittest/symbolindexer-test.cpp @@ -24,6 +24,7 @@ ****************************************************************************/ #include "googletest.h" +#include "filesystem-utilities.h" #include "mockclangpathwatcher.h" #include "mocksymbolscollector.h" #include "mocksymbolstorage.h" @@ -273,15 +274,15 @@ TEST_F(SymbolIndexer, UpdateProjectPartsCallsAddFilesInCollector) "-DBAR=1", "-DFOO=1", "-I", - "/project/includes", + toNativePath("/project/includes").path(), "-I", - "/other/project/includes", + toNativePath("/other/project/includes").path(), "-isystem", - TESTDATA_DIR, + toNativePath(TESTDATA_DIR).path(), "-isystem", - "/other/includes", + toNativePath("/other/includes").path(), "-isystem", - "/includes"))); + toNativePath("/includes").path()))); indexer.updateProjectParts({projectPart1}); } @@ -303,19 +304,19 @@ TEST_F(SymbolIndexer, UpdateProjectPartsCallsAddFilesWithPrecompiledHeaderInColl "-DBAR=1", "-DFOO=1", "-I", - "/project/includes", + toNativePath("/project/includes").path(), "-I", - "/other/project/includes", + toNativePath("/other/project/includes").path(), "-isystem", - TESTDATA_DIR, + toNativePath(TESTDATA_DIR).path(), "-isystem", - "/other/includes", + toNativePath("/other/includes").path(), "-isystem", - "/includes", + toNativePath("/includes").path(), "-Xclang", "-include-pch", "-Xclang", - "/path/to/pch"))); + toNativePath("/path/to/pch").path()))); indexer.updateProjectParts({projectPart1}); } @@ -336,15 +337,15 @@ TEST_F(SymbolIndexer, UpdateProjectPartsCallsAddFilesWithoutPrecompiledHeaderInC "-DBAR=1", "-DFOO=1", "-I", - "/project/includes", + toNativePath("/project/includes").path(), "-I", - "/other/project/includes", + toNativePath("/other/project/includes").path(), "-isystem", - TESTDATA_DIR, + toNativePath(TESTDATA_DIR).path(), "-isystem", - "/other/includes", + toNativePath("/other/includes").path(), "-isystem", - "/includes"))); + toNativePath("/includes").path()))); indexer.updateProjectParts({projectPart1}); } @@ -516,15 +517,15 @@ TEST_F(SymbolIndexer, UpdateProjectPartsCallsInOrderWithoutProjectPartArtifact) "-DBAR=1", "-DFOO=1", "-I", - "/project/includes", + toNativePath("/project/includes").path(), "-I", - "/other/project/includes", + toNativePath("/other/project/includes").path(), "-isystem", - TESTDATA_DIR, + toNativePath(TESTDATA_DIR).path(), "-isystem", - "/other/includes", + toNativePath("/other/includes").path(), "-isystem", - "/includes"))); + toNativePath("/includes").path()))); EXPECT_CALL(mockCollector, collectSymbols()); EXPECT_CALL(mockSqliteTransactionBackend, immediateBegin()); EXPECT_CALL(mockSymbolStorage, addSymbolsAndSourceLocations(symbolEntries, sourceLocations)); @@ -568,15 +569,15 @@ TEST_F(SymbolIndexer, UpdateProjectPartsCallsInOrderWithProjectPartArtifact) "-DBAR=1", "-DFOO=1", "-I", - "/project/includes", + toNativePath("/project/includes").path(), "-I", - "/other/project/includes", + toNativePath("/other/project/includes").path(), "-isystem", - TESTDATA_DIR, + toNativePath(TESTDATA_DIR).path(), "-isystem", - "/other/includes", + toNativePath("/other/includes").path(), "-isystem", - "/includes"))); + toNativePath("/includes").path()))); EXPECT_CALL(mockCollector, collectSymbols()); EXPECT_CALL(mockSqliteTransactionBackend, immediateBegin()); EXPECT_CALL(mockSymbolStorage, addSymbolsAndSourceLocations(symbolEntries, sourceLocations)); @@ -624,15 +625,15 @@ TEST_F(SymbolIndexer, UpdateChangedPathCallsInOrder) "-DBAR=1", "-DFOO=1", "-I", - "/project/includes", + toNativePath("/project/includes").path(), "-I", - "/other/project/includes", + toNativePath("/other/project/includes").path(), "-isystem", - TESTDATA_DIR, + toNativePath(TESTDATA_DIR).path(), "-isystem", - "/other/includes", + toNativePath("/other/includes").path(), "-isystem", - "/includes"))); + toNativePath("/includes").path()))); EXPECT_CALL(mockCollector, collectSymbols()); EXPECT_CALL(mockSqliteTransactionBackend, immediateBegin()); EXPECT_CALL(mockSymbolStorage, addSymbolsAndSourceLocations(symbolEntries, sourceLocations)); @@ -686,19 +687,19 @@ TEST_F(SymbolIndexer, UpdateChangedPathIsUsingPrecompiledHeader) "-DBAR=1", "-DFOO=1", "-I", - "/project/includes", + toNativePath("/project/includes").path(), "-I", - "/other/project/includes", + toNativePath("/other/project/includes").path(), "-isystem", - TESTDATA_DIR, + toNativePath(TESTDATA_DIR).path(), "-isystem", - "/other/includes", + toNativePath("/other/includes").path(), "-isystem", - "/includes", + toNativePath("/includes").path(), "-Xclang", "-include-pch", "-Xclang", - "/path/to/pch"))); + toNativePath("/path/to/pch").path()))); indexer.pathsChanged({sourceFileIds[0]}); } @@ -721,15 +722,15 @@ TEST_F(SymbolIndexer, UpdateChangedPathIsNotUsingPrecompiledHeaderIfItNotExists) "-DBAR=1", "-DFOO=1", "-I", - "/project/includes", + toNativePath("/project/includes").path(), "-I", - "/other/project/includes", + toNativePath("/other/project/includes").path(), "-isystem", - TESTDATA_DIR, + toNativePath(TESTDATA_DIR).path(), "-isystem", - "/other/includes", + toNativePath("/other/includes").path(), "-isystem", - "/includes"))); + toNativePath("/includes").path()))); indexer.pathsChanged({sourceFileIds[0]}); } diff --git a/tests/unit/unittest/testclangtool.cpp b/tests/unit/unittest/testclangtool.cpp index de41e42a814..bea5afeb4c8 100644 --- a/tests/unit/unittest/testclangtool.cpp +++ b/tests/unit/unittest/testclangtool.cpp @@ -25,12 +25,11 @@ #include "testclangtool.h" -TestClangTool::TestClangTool(std::string &&directory, - std::string &&fileName, - std::string &&content, - std::vector &&commandLine) +TestClangTool::TestClangTool(ClangBackEnd::FilePath &&filePath, + Utils::SmallString &&content, + Utils::SmallStringVector &&commandLine) { - addFile(std::move(directory), std::move(fileName), std::move(content), std::move(commandLine)); + addFile(std::move(filePath), std::move(content), std::move(commandLine)); auto clangTool = createTool(); diff --git a/tests/unit/unittest/testclangtool.h b/tests/unit/unittest/testclangtool.h index f8ae0caf165..448c4e51ae8 100644 --- a/tests/unit/unittest/testclangtool.h +++ b/tests/unit/unittest/testclangtool.h @@ -30,10 +30,9 @@ class TestClangTool : public ClangBackEnd::ClangTool { public: - TestClangTool(std::string &&directory, - std::string &&fileName, - std::string &&content, - std::vector &&commandLine); + TestClangTool(ClangBackEnd::FilePath &&filePath, + Utils::SmallString &&content, + Utils::SmallStringVector &&commandLine); const clang::ASTUnit *ast() const; const clang::SourceManager &sourceManager() const;