hokein created this revision. hokein added a reviewer: ioeric. Herald added subscribers: jkorous-apple, ilya-biryukov, klimek.
Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D42913 Files: clangd/index/SymbolCollector.cpp unittests/clangd/SymbolCollectorTests.cpp Index: unittests/clangd/SymbolCollectorTests.cpp =================================================================== --- unittests/clangd/SymbolCollectorTests.cpp +++ unittests/clangd/SymbolCollectorTests.cpp @@ -82,16 +82,20 @@ class SymbolCollectorTest : public ::testing::Test { public: - bool runSymbolCollector(StringRef HeaderCode, StringRef MainCode) { + bool runSymbolCollector(StringRef HeaderCode, StringRef MainCode, + const std::vector<std::string> &ExtraArgs = {}) { llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( new vfs::InMemoryFileSystem); llvm::IntrusiveRefCntPtr<FileManager> Files( new FileManager(FileSystemOptions(), InMemoryFileSystem)); auto Factory = llvm::make_unique<SymbolIndexActionFactory>(CollectorOpts); + std::vector<std::string> Args = {"symbol_collector", "-fsyntax-only", + "-std=c++11", TestFileName}; + Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end()); tooling::ToolInvocation Invocation( - {"symbol_collector", "-fsyntax-only", "-std=c++11", TestFileName}, + Args, Factory->create(), Files.get(), std::make_shared<PCHContainerOperations>()); @@ -264,6 +268,24 @@ CPath(TestFileName)))); } +TEST_F(SymbolCollectorTest, SymbolFormedByCLI) { + CollectorOpts.IndexMainFiles = false; + + Annotations Header(R"( + #ifdef NAME + $expansion[[class NAME {}]]; + #endif + )"); + + runSymbolCollector(Header.code(), /*Main=*/"", + /*ExtraArgs=*/{"-DNAME=name"}); + EXPECT_THAT(Symbols, + UnorderedElementsAre( + AllOf(QName("name"), + LocationOffsets(Header.offsetRange("expansion")), + CPath(TestHeaderName)))); +} + TEST_F(SymbolCollectorTest, IgnoreSymbolsInMainFile) { CollectorOpts.IndexMainFiles = false; const std::string Header = R"( Index: clangd/index/SymbolCollector.cpp =================================================================== --- clangd/index/SymbolCollector.cpp +++ clangd/index/SymbolCollector.cpp @@ -124,10 +124,15 @@ SourceLocation Loc = SM.getSpellingLoc(D->getLocation()); if (D->getLocation().isMacroID()) { - // The symbol is formed via macro concatenation, the spelling location will - // be "<scratch space>", which is not interesting to us, use the expansion - // location instead. - if (llvm::StringRef(Loc.printToString(SM)).startswith("<scratch")) { + // We use the expansion location for the following symbols, as spelling + // locations of these symbols are not interesting to us: + // * symbols formed via macro concatenation, the spelling location will + // be "<scratch space>" + // * symbols controlled and defined by a compile command-line option + // `-DName=foo`, the spelling location will be "<commandline>". + std::string PrintLoc = Loc.printToString(SM); + if (llvm::StringRef(PrintLoc).startswith("<scratch") || + llvm::StringRef(PrintLoc).startswith("<command line>")) { FilePathStorage = makeAbsolutePath( SM, SM.getFilename(SM.getExpansionLoc(D->getLocation())), FallbackDir);
Index: unittests/clangd/SymbolCollectorTests.cpp =================================================================== --- unittests/clangd/SymbolCollectorTests.cpp +++ unittests/clangd/SymbolCollectorTests.cpp @@ -82,16 +82,20 @@ class SymbolCollectorTest : public ::testing::Test { public: - bool runSymbolCollector(StringRef HeaderCode, StringRef MainCode) { + bool runSymbolCollector(StringRef HeaderCode, StringRef MainCode, + const std::vector<std::string> &ExtraArgs = {}) { llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( new vfs::InMemoryFileSystem); llvm::IntrusiveRefCntPtr<FileManager> Files( new FileManager(FileSystemOptions(), InMemoryFileSystem)); auto Factory = llvm::make_unique<SymbolIndexActionFactory>(CollectorOpts); + std::vector<std::string> Args = {"symbol_collector", "-fsyntax-only", + "-std=c++11", TestFileName}; + Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end()); tooling::ToolInvocation Invocation( - {"symbol_collector", "-fsyntax-only", "-std=c++11", TestFileName}, + Args, Factory->create(), Files.get(), std::make_shared<PCHContainerOperations>()); @@ -264,6 +268,24 @@ CPath(TestFileName)))); } +TEST_F(SymbolCollectorTest, SymbolFormedByCLI) { + CollectorOpts.IndexMainFiles = false; + + Annotations Header(R"( + #ifdef NAME + $expansion[[class NAME {}]]; + #endif + )"); + + runSymbolCollector(Header.code(), /*Main=*/"", + /*ExtraArgs=*/{"-DNAME=name"}); + EXPECT_THAT(Symbols, + UnorderedElementsAre( + AllOf(QName("name"), + LocationOffsets(Header.offsetRange("expansion")), + CPath(TestHeaderName)))); +} + TEST_F(SymbolCollectorTest, IgnoreSymbolsInMainFile) { CollectorOpts.IndexMainFiles = false; const std::string Header = R"( Index: clangd/index/SymbolCollector.cpp =================================================================== --- clangd/index/SymbolCollector.cpp +++ clangd/index/SymbolCollector.cpp @@ -124,10 +124,15 @@ SourceLocation Loc = SM.getSpellingLoc(D->getLocation()); if (D->getLocation().isMacroID()) { - // The symbol is formed via macro concatenation, the spelling location will - // be "<scratch space>", which is not interesting to us, use the expansion - // location instead. - if (llvm::StringRef(Loc.printToString(SM)).startswith("<scratch")) { + // We use the expansion location for the following symbols, as spelling + // locations of these symbols are not interesting to us: + // * symbols formed via macro concatenation, the spelling location will + // be "<scratch space>" + // * symbols controlled and defined by a compile command-line option + // `-DName=foo`, the spelling location will be "<commandline>". + std::string PrintLoc = Loc.printToString(SM); + if (llvm::StringRef(PrintLoc).startswith("<scratch") || + llvm::StringRef(PrintLoc).startswith("<command line>")) { FilePathStorage = makeAbsolutePath( SM, SM.getFilename(SM.getExpansionLoc(D->getLocation())), FallbackDir);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits