Author: sammccall Date: Fri Feb 16 01:41:43 2018 New Revision: 325326 URL: http://llvm.org/viewvc/llvm-project?rev=325326&view=rev Log: [clangd] TestFS cleanup: getVirtualBlahBlah -> testPath/testRoot. Remove SmallString micro-opt for more ergonomic tests. NFC
Modified: clang-tools-extra/trunk/unittests/clangd/ClangdTests.cpp clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp clang-tools-extra/trunk/unittests/clangd/TUSchedulerTests.cpp clang-tools-extra/trunk/unittests/clangd/TestFS.cpp clang-tools-extra/trunk/unittests/clangd/TestFS.h clang-tools-extra/trunk/unittests/clangd/URITests.cpp clang-tools-extra/trunk/unittests/clangd/XRefsTests.cpp Modified: clang-tools-extra/trunk/unittests/clangd/ClangdTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/ClangdTests.cpp?rev=325326&r1=325325&r2=325326&view=diff ============================================================================== --- clang-tools-extra/trunk/unittests/clangd/ClangdTests.cpp (original) +++ clang-tools-extra/trunk/unittests/clangd/ClangdTests.cpp Fri Feb 16 01:41:43 2018 @@ -114,13 +114,10 @@ protected: ClangdServer Server(CDB, DiagConsumer, FS, getDefaultAsyncThreadsCount(), /*StorePreamblesInMemory=*/true); for (const auto &FileWithContents : ExtraFiles) - FS.Files[getVirtualTestFilePath(FileWithContents.first)] = - FileWithContents.second; - - auto SourceFilename = getVirtualTestFilePath(SourceFileRelPath); + FS.Files[testPath(FileWithContents.first)] = FileWithContents.second; + auto SourceFilename = testPath(SourceFileRelPath); FS.ExpectedFile = SourceFilename; - Server.addDocument(SourceFilename, SourceContents); auto Result = dumpASTWithoutMemoryLocs(Server, SourceFilename); EXPECT_TRUE(Server.blockUntilIdleForTest()) << "Waiting for diagnostics"; @@ -176,10 +173,9 @@ TEST_F(ClangdVFSTest, Reparse) { int b = a; )cpp"; - auto FooCpp = getVirtualTestFilePath("foo.cpp"); - auto FooH = getVirtualTestFilePath("foo.h"); + auto FooCpp = testPath("foo.cpp"); - FS.Files[FooH] = "int a;"; + FS.Files[testPath("foo.h")] = "int a;"; FS.Files[FooCpp] = SourceContents; FS.ExpectedFile = FooCpp; @@ -215,8 +211,8 @@ TEST_F(ClangdVFSTest, ReparseOnHeaderCha int b = a; )cpp"; - auto FooCpp = getVirtualTestFilePath("foo.cpp"); - auto FooH = getVirtualTestFilePath("foo.h"); + auto FooCpp = testPath("foo.cpp"); + auto FooH = testPath("foo.h"); FS.Files[FooH] = "int a;"; FS.Files[FooCpp] = SourceContents; @@ -252,7 +248,7 @@ TEST_F(ClangdVFSTest, CheckVersions) { /*AsyncThreadsCount=*/0, /*StorePreamblesInMemory=*/true); - auto FooCpp = getVirtualTestFilePath("foo.cpp"); + auto FooCpp = testPath("foo.cpp"); const auto SourceContents = "int a;"; FS.Files[FooCpp] = SourceContents; FS.ExpectedFile = FooCpp; @@ -309,7 +305,7 @@ TEST_F(ClangdVFSTest, SearchLibDir) { llvm::sys::path::append(StringPath, IncludeDir, "string"); FS.Files[StringPath] = "class mock_string {};"; - auto FooCpp = getVirtualTestFilePath("foo.cpp"); + auto FooCpp = testPath("foo.cpp"); const auto SourceContents = R"cpp( #include <string> mock_string x; @@ -340,7 +336,7 @@ TEST_F(ClangdVFSTest, ForceReparseCompil // No need to sync reparses, because reparses are performed on the calling // thread. - auto FooCpp = getVirtualTestFilePath("foo.cpp"); + auto FooCpp = testPath("foo.cpp"); const auto SourceContents1 = R"cpp( template <class T> struct foo { T x; }; @@ -388,13 +384,13 @@ TEST_F(ClangdVFSTest, MemoryUsage) { // No need to sync reparses, because reparses are performed on the calling // thread. - Path FooCpp = getVirtualTestFilePath("foo.cpp").str(); + Path FooCpp = testPath("foo.cpp"); const auto SourceContents = R"cpp( struct Something { int method(); }; )cpp"; - Path BarCpp = getVirtualTestFilePath("bar.cpp").str(); + Path BarCpp = testPath("bar.cpp"); FS.Files[FooCpp] = ""; FS.Files[BarCpp] = ""; @@ -423,11 +419,11 @@ TEST_F(ClangdVFSTest, InvalidCompileComm /*AsyncThreadsCount=*/0, /*StorePreamblesInMemory=*/true); - auto FooCpp = getVirtualTestFilePath("foo.cpp"); + auto FooCpp = testPath("foo.cpp"); // clang cannot create CompilerInvocation if we pass two files in the // CompileCommand. We pass the file in ExtraFlags once and CDB adds another // one in getCompileCommand(). - CDB.ExtraClangFlags.push_back(FooCpp.str()); + CDB.ExtraClangFlags.push_back(FooCpp); // Clang can't parse command args in that case, but we shouldn't crash. Server.addDocument(FooCpp, "int main() {}"); @@ -478,16 +474,13 @@ int d; unsigned MaxLineForFileRequests = 7; unsigned MaxColumnForFileRequests = 10; - std::vector<SmallString<32>> FilePaths; - FilePaths.reserve(FilesCount); - for (unsigned I = 0; I < FilesCount; ++I) - FilePaths.push_back(getVirtualTestFilePath(std::string("Foo") + - std::to_string(I) + ".cpp")); - // Mark all of those files as existing. + std::vector<std::string> FilePaths; MockFSProvider FS; - for (auto &&FilePath : FilePaths) - FS.Files[FilePath] = ""; - + for (unsigned I = 0; I < FilesCount; ++I) { + std::string Name = std::string("Foo") + std::to_string(I) + ".cpp"; + FS.Files[Name] = ""; + FilePaths.push_back(testPath(Name)); + } struct FileStat { unsigned HitsWithoutErrors = 0; @@ -698,9 +691,9 @@ TEST_F(ClangdVFSTest, CheckSourceHeaderS int b = a; )cpp"; - auto FooCpp = getVirtualTestFilePath("foo.cpp"); - auto FooH = getVirtualTestFilePath("foo.h"); - auto Invalid = getVirtualTestFilePath("main.cpp"); + auto FooCpp = testPath("foo.cpp"); + auto FooH = testPath("foo.h"); + auto Invalid = testPath("main.cpp"); FS.Files[FooCpp] = SourceContents; FS.Files[FooH] = "int a;"; @@ -721,8 +714,8 @@ TEST_F(ClangdVFSTest, CheckSourceHeaderS // Test with header file in capital letters and different extension, source // file with different extension - auto FooC = getVirtualTestFilePath("bar.c"); - auto FooHH = getVirtualTestFilePath("bar.HH"); + auto FooC = testPath("bar.c"); + auto FooHH = testPath("bar.HH"); FS.Files[FooC] = SourceContents; FS.Files[FooHH] = "int a;"; @@ -732,8 +725,8 @@ TEST_F(ClangdVFSTest, CheckSourceHeaderS ASSERT_EQ(PathResult.getValue(), FooHH); // Test with both capital letters - auto Foo2C = getVirtualTestFilePath("foo2.C"); - auto Foo2HH = getVirtualTestFilePath("foo2.HH"); + auto Foo2C = testPath("foo2.C"); + auto Foo2HH = testPath("foo2.HH"); FS.Files[Foo2C] = SourceContents; FS.Files[Foo2HH] = "int a;"; @@ -742,8 +735,8 @@ TEST_F(ClangdVFSTest, CheckSourceHeaderS ASSERT_EQ(PathResult.getValue(), Foo2HH); // Test with source file as capital letter and .hxx header file - auto Foo3C = getVirtualTestFilePath("foo3.C"); - auto Foo3HXX = getVirtualTestFilePath("foo3.hxx"); + auto Foo3C = testPath("foo3.C"); + auto Foo3HXX = testPath("foo3.hxx"); SourceContents = R"c( #include "foo3.hxx" @@ -808,7 +801,7 @@ int c; int d; )cpp"; - auto FooCpp = getVirtualTestFilePath("foo.cpp"); + auto FooCpp = testPath("foo.cpp"); MockFSProvider FS; FS.Files[FooCpp] = ""; Modified: clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp?rev=325326&r1=325325&r2=325326&view=diff ============================================================================== --- clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp (original) +++ clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp Fri Feb 16 01:41:43 2018 @@ -119,7 +119,7 @@ CompletionList completions(StringRef Tex IgnoreDiagnostics DiagConsumer; ClangdServer Server(CDB, DiagConsumer, FS, getDefaultAsyncThreadsCount(), /*StorePreamblesInMemory=*/true); - auto File = getVirtualTestFilePath("foo.cpp"); + auto File = testPath("foo.cpp"); Annotations Test(Text); Server.addDocument(File, Test.code()); EXPECT_TRUE(Server.blockUntilIdleForTest()) << "Waiting for preamble"; @@ -344,7 +344,7 @@ TEST(CompletionTest, CheckContentsOverri MockCompilationDatabase CDB; ClangdServer Server(CDB, DiagConsumer, FS, getDefaultAsyncThreadsCount(), /*StorePreamblesInMemory=*/true); - auto File = getVirtualTestFilePath("foo.cpp"); + auto File = testPath("foo.cpp"); Server.addDocument(File, "ignored text!"); Annotations Example("int cbc; int b = ^;"); @@ -543,9 +543,9 @@ TEST(CompletionTest, IndexSuppressesPrea ClangdServer Server(CDB, DiagConsumer, FS, getDefaultAsyncThreadsCount(), /*StorePreamblesInMemory=*/true); - FS.Files[getVirtualTestFilePath("bar.h")] = + FS.Files[testPath("bar.h")] = R"cpp(namespace ns { struct preamble { int member; }; })cpp"; - auto File = getVirtualTestFilePath("foo.cpp"); + auto File = testPath("foo.cpp"); Annotations Test(R"cpp( #include "bar.h" namespace ns { int local; } @@ -580,12 +580,12 @@ TEST(CompletionTest, DynamicIndexMultiFi /*StorePreamblesInMemory=*/true, /*BuildDynamicSymbolIndex=*/true); - Server.addDocument(getVirtualTestFilePath("foo.cpp"), R"cpp( + Server.addDocument(testPath("foo.cpp"), R"cpp( namespace ns { class XYZ {}; void foo(int x) {} } )cpp"); ASSERT_TRUE(Server.blockUntilIdleForTest()) << "Waiting for preamble"; - auto File = getVirtualTestFilePath("bar.cpp"); + auto File = testPath("bar.cpp"); Annotations Test(R"cpp( namespace ns { class XXX {}; @@ -622,7 +622,7 @@ SignatureHelp signatures(StringRef Text) IgnoreDiagnostics DiagConsumer; ClangdServer Server(CDB, DiagConsumer, FS, getDefaultAsyncThreadsCount(), /*StorePreamblesInMemory=*/true); - auto File = getVirtualTestFilePath("foo.cpp"); + auto File = testPath("foo.cpp"); Annotations Test(Text); Server.addDocument(File, Test.code()); EXPECT_TRUE(Server.blockUntilIdleForTest()) << "Waiting for preamble"; Modified: clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp?rev=325326&r1=325325&r2=325326&view=diff ============================================================================== --- clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp (original) +++ clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp Fri Feb 16 01:41:43 2018 @@ -83,8 +83,8 @@ public: class SymbolCollectorTest : public ::testing::Test { public: SymbolCollectorTest() - : TestHeaderName(getVirtualTestFilePath("symbol.h").str()), - TestFileName(getVirtualTestFilePath("symbol.cc").str()) { + : TestHeaderName(testPath("symbol.h")), + TestFileName(testPath("symbol.cc")) { TestHeaderURI = URI::createFile(TestHeaderName).toString(); TestFileURI = URI::createFile(TestFileName).toString(); } @@ -218,9 +218,8 @@ TEST_F(SymbolCollectorTest, SymbolRelati CollectorOpts.IndexMainFiles = false; TestHeaderName = "x.h"; TestFileName = "x.cpp"; - TestHeaderURI = - URI::createFile(getVirtualTestFilePath(TestHeaderName)).toString(); - CollectorOpts.FallbackDir = getVirtualTestRoot(); + TestHeaderURI = URI::createFile(testPath(TestHeaderName)).toString(); + CollectorOpts.FallbackDir = testRoot(); runSymbolCollector("class Foo {};", /*Main=*/""); EXPECT_THAT(Symbols, UnorderedElementsAre(AllOf(QName("Foo"), DeclURI(TestHeaderURI)))); @@ -231,8 +230,8 @@ TEST_F(SymbolCollectorTest, CustomURISch CollectorOpts.IndexMainFiles = false; // Use test URI scheme from URITests.cpp CollectorOpts.URISchemes.insert(CollectorOpts.URISchemes.begin(), "unittest"); - TestHeaderName = getVirtualTestFilePath("test-root/x.h").str(); - TestFileName = getVirtualTestFilePath("test-root/x.cpp").str(); + TestHeaderName = testPath("test-root/x.h"); + TestFileName = testPath("test-root/x.cpp"); runSymbolCollector("class Foo {};", /*Main=*/""); EXPECT_THAT(Symbols, UnorderedElementsAre(AllOf(QName("Foo"), DeclURI("unittest:x.h")))); Modified: clang-tools-extra/trunk/unittests/clangd/TUSchedulerTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/TUSchedulerTests.cpp?rev=325326&r1=325325&r2=325326&view=diff ============================================================================== --- clang-tools-extra/trunk/unittests/clangd/TUSchedulerTests.cpp (original) +++ clang-tools-extra/trunk/unittests/clangd/TUSchedulerTests.cpp Fri Feb 16 01:41:43 2018 @@ -33,12 +33,9 @@ protected: std::move(Contents)}; } - void changeFile(PathRef File, std::string Contents) { - Files[File] = Contents; - } + llvm::StringMap<std::string> Files; private: - llvm::StringMap<std::string> Files; MockCompilationDatabase CDB; }; @@ -47,11 +44,11 @@ TEST_F(TUSchedulerTests, MissingFiles) { /*StorePreamblesInMemory=*/true, /*ASTParsedCallback=*/nullptr); - auto Added = getVirtualTestFilePath("added.cpp"); - changeFile(Added, ""); + auto Added = testPath("added.cpp"); + Files[Added] = ""; - auto Missing = getVirtualTestFilePath("missing.cpp"); - changeFile(Missing, ""); + auto Missing = testPath("missing.cpp"); + Files[Missing] = ""; S.update(Added, getInputs(Added, ""), ignoreUpdate); @@ -106,9 +103,9 @@ TEST_F(TUSchedulerTests, ManyUpdates) { std::vector<std::string> Files; for (int I = 0; I < FilesCount; ++I) { - Files.push_back( - getVirtualTestFilePath("foo" + std::to_string(I) + ".cpp").str()); - changeFile(Files.back(), ""); + std::string Name = "foo" + std::to_string(I) + ".cpp"; + Files.push_back(testPath(Name)); + this->Files[Files.back()] = ""; } llvm::StringRef Contents1 = R"cpp(int a;)cpp"; Modified: clang-tools-extra/trunk/unittests/clangd/TestFS.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/TestFS.cpp?rev=325326&r1=325325&r2=325326&view=diff ============================================================================== --- clang-tools-extra/trunk/unittests/clangd/TestFS.cpp (original) +++ clang-tools-extra/trunk/unittests/clangd/TestFS.cpp Fri Feb 16 01:41:43 2018 @@ -12,14 +12,17 @@ namespace clang { namespace clangd { +using namespace llvm; + IntrusiveRefCntPtr<vfs::FileSystem> -buildTestFS(llvm::StringMap<std::string> const &Files) { +buildTestFS(StringMap<std::string> const &Files) { IntrusiveRefCntPtr<vfs::InMemoryFileSystem> MemFS( new vfs::InMemoryFileSystem); - for (auto &FileAndContents : Files) + for (auto &FileAndContents : Files) { MemFS->addFile(FileAndContents.first(), time_t(), - llvm::MemoryBuffer::getMemBuffer(FileAndContents.second, - FileAndContents.first())); + MemoryBuffer::getMemBuffer(FileAndContents.second, + FileAndContents.first())); + } return MemFS; } @@ -38,20 +41,20 @@ MockCompilationDatabase::MockCompilation // -ffreestanding avoids implicit stdc-predef.h. } -llvm::Optional<tooling::CompileCommand> +Optional<tooling::CompileCommand> MockCompilationDatabase::getCompileCommand(PathRef File) const { if (ExtraClangFlags.empty()) - return llvm::None; + return None; auto CommandLine = ExtraClangFlags; - auto FileName = llvm::sys::path::filename(File); + auto FileName = sys::path::filename(File); CommandLine.insert(CommandLine.begin(), "clang"); CommandLine.insert(CommandLine.end(), UseRelPaths ? FileName : File); - return {tooling::CompileCommand(llvm::sys::path::parent_path(File), FileName, + return {tooling::CompileCommand(sys::path::parent_path(File), FileName, std::move(CommandLine), "")}; } -const char *getVirtualTestRoot() { +const char *testRoot() { #ifdef LLVM_ON_WIN32 return "C:\\clangd-test"; #else @@ -59,12 +62,12 @@ const char *getVirtualTestRoot() { #endif } -llvm::SmallString<32> getVirtualTestFilePath(PathRef File) { - assert(llvm::sys::path::is_relative(File) && "FileName should be relative"); +std::string testPath(PathRef File) { + assert(sys::path::is_relative(File) && "FileName should be relative"); - llvm::SmallString<32> Path; - llvm::sys::path::append(Path, getVirtualTestRoot(), File); - return Path; + SmallString<32> Path; + sys::path::append(Path, testRoot(), File); + return Path.str(); } } // namespace clangd Modified: clang-tools-extra/trunk/unittests/clangd/TestFS.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/TestFS.h?rev=325326&r1=325325&r2=325326&view=diff ============================================================================== --- clang-tools-extra/trunk/unittests/clangd/TestFS.h (original) +++ clang-tools-extra/trunk/unittests/clangd/TestFS.h Fri Feb 16 01:41:43 2018 @@ -29,7 +29,8 @@ public: Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> getTaggedFileSystem(PathRef File) override; - llvm::Optional<SmallString<32>> ExpectedFile; + llvm::Optional<std::string> ExpectedFile; + // If relative paths are used, they are resolved with testPath(). llvm::StringMap<std::string> Files; VFSTag Tag = VFSTag(); }; @@ -49,10 +50,10 @@ public: }; // Returns an absolute (fake) test directory for this OS. -const char *getVirtualTestRoot(); +const char *testRoot(); // Returns a suitable absolute path for this OS. -llvm::SmallString<32> getVirtualTestFilePath(PathRef File); +std::string testPath(PathRef File); } // namespace clangd } // namespace clang Modified: clang-tools-extra/trunk/unittests/clangd/URITests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/URITests.cpp?rev=325326&r1=325325&r2=325326&view=diff ============================================================================== --- clang-tools-extra/trunk/unittests/clangd/URITests.cpp (original) +++ clang-tools-extra/trunk/unittests/clangd/URITests.cpp Fri Feb 16 01:41:43 2018 @@ -176,10 +176,10 @@ TEST(URITest, Resolve) { } TEST(URITest, Platform) { - auto Path = getVirtualTestFilePath("x"); + auto Path = testPath("x"); auto U = URI::create(Path, "file"); EXPECT_TRUE(static_cast<bool>(U)); - EXPECT_THAT(resolveOrDie(*U), Path.str()); + EXPECT_THAT(resolveOrDie(*U), Path); } TEST(URITest, ResolveFailed) { Modified: clang-tools-extra/trunk/unittests/clangd/XRefsTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/XRefsTests.cpp?rev=325326&r1=325325&r2=325326&view=diff ============================================================================== --- clang-tools-extra/trunk/unittests/clangd/XRefsTests.cpp (original) +++ clang-tools-extra/trunk/unittests/clangd/XRefsTests.cpp Fri Feb 16 01:41:43 2018 @@ -46,9 +46,8 @@ class IgnoreDiagnostics : public Diagnos // FIXME: this is duplicated with FileIndexTests. Share it. ParsedAST build(StringRef Code) { - auto TestFile = getVirtualTestFilePath("Foo.cpp"); - auto CI = - createInvocationFromCommandLine({"clang", "-xc++", TestFile.c_str()}); + auto CI = createInvocationFromCommandLine( + {"clang", "-xc++", testPath("Foo.cpp").c_str()}); auto Buf = MemoryBuffer::getMemBuffer(Code); auto AST = ParsedAST::Build(std::move(CI), nullptr, std::move(Buf), std::make_shared<PCHContainerOperations>(), @@ -246,7 +245,7 @@ int baz = f^oo; ClangdServer Server(CDB, DiagConsumer, FS, /*AsyncThreadsCount=*/0, /*StorePreambleInMemory=*/true); - auto FooCpp = getVirtualTestFilePath("foo.cpp"); + auto FooCpp = testPath("foo.cpp"); FS.Files[FooCpp] = ""; Server.addDocument(FooCpp, SourceAnnotations.code()); @@ -254,9 +253,9 @@ int baz = f^oo; runFindDefinitions(Server, FooCpp, SourceAnnotations.point()); EXPECT_TRUE(bool(Locations)) << "findDefinitions returned an error"; - EXPECT_THAT(Locations->Value, - ElementsAre(Location{URIForFile{FooCpp.str()}, - SourceAnnotations.range()})); + EXPECT_THAT( + Locations->Value, + ElementsAre(Location{URIForFile{FooCpp}, SourceAnnotations.range()})); } } // namespace _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits