kadircet updated this revision to Diff 176774. kadircet marked an inline comment as done. kadircet added a comment.
- Revert rL346488 <https://reviews.llvm.org/rL346488> partially. Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D55275/new/ https://reviews.llvm.org/D55275 Files: clangd/AST.cpp unittests/clangd/SymbolCollectorTests.cpp Index: unittests/clangd/SymbolCollectorTests.cpp =================================================================== --- unittests/clangd/SymbolCollectorTests.cpp +++ unittests/clangd/SymbolCollectorTests.cpp @@ -614,6 +614,18 @@ DeclURI(TestHeaderURI)))); } +TEST_F(SymbolCollectorTest, SymbolFormedByCLI) { + Annotations Header(R"( + #ifdef NAME + class $expansion[[NAME]] {}; + #endif + )"); + runSymbolCollector(Header.code(), /*Main=*/"", /*ExtraArgs=*/{"-DNAME=name"}); + EXPECT_THAT(Symbols, UnorderedElementsAre(AllOf( + QName("name"), DeclRange(Header.range("expansion")), + DeclURI(TestHeaderURI)))); +} + TEST_F(SymbolCollectorTest, IgnoreSymbolsInMainFile) { const std::string Header = R"( class Foo {}; Index: clangd/AST.cpp =================================================================== --- clangd/AST.cpp +++ clangd/AST.cpp @@ -23,9 +23,11 @@ namespace clangd { // Returns true if the complete name of decl \p D is spelled in the source code. -// This is not the case for symbols formed via macro concatenation. -// (We used to attempt to treat names spelled on the command-line this way too, -// but the preamble doesn't preserve the required information). +// This is not the case for: +// * 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 "<command line>". bool isSpelledInSourceCode(const Decl *D) { const auto &SM = D->getASTContext().getSourceManager(); auto Loc = D->getLocation(); @@ -33,7 +35,8 @@ // macros, we should use the location where the whole definition occurs. if (Loc.isMacroID()) { std::string PrintLoc = SM.getSpellingLoc(Loc).printToString(SM); - if (StringRef(PrintLoc).startswith("<scratch")) + if (StringRef(PrintLoc).startswith("<scratch") || + StringRef(PrintLoc).startswith("<command line>")) return false; } return true;
Index: unittests/clangd/SymbolCollectorTests.cpp =================================================================== --- unittests/clangd/SymbolCollectorTests.cpp +++ unittests/clangd/SymbolCollectorTests.cpp @@ -614,6 +614,18 @@ DeclURI(TestHeaderURI)))); } +TEST_F(SymbolCollectorTest, SymbolFormedByCLI) { + Annotations Header(R"( + #ifdef NAME + class $expansion[[NAME]] {}; + #endif + )"); + runSymbolCollector(Header.code(), /*Main=*/"", /*ExtraArgs=*/{"-DNAME=name"}); + EXPECT_THAT(Symbols, UnorderedElementsAre(AllOf( + QName("name"), DeclRange(Header.range("expansion")), + DeclURI(TestHeaderURI)))); +} + TEST_F(SymbolCollectorTest, IgnoreSymbolsInMainFile) { const std::string Header = R"( class Foo {}; Index: clangd/AST.cpp =================================================================== --- clangd/AST.cpp +++ clangd/AST.cpp @@ -23,9 +23,11 @@ namespace clangd { // Returns true if the complete name of decl \p D is spelled in the source code. -// This is not the case for symbols formed via macro concatenation. -// (We used to attempt to treat names spelled on the command-line this way too, -// but the preamble doesn't preserve the required information). +// This is not the case for: +// * 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 "<command line>". bool isSpelledInSourceCode(const Decl *D) { const auto &SM = D->getASTContext().getSourceManager(); auto Loc = D->getLocation(); @@ -33,7 +35,8 @@ // macros, we should use the location where the whole definition occurs. if (Loc.isMacroID()) { std::string PrintLoc = SM.getSpellingLoc(Loc).printToString(SM); - if (StringRef(PrintLoc).startswith("<scratch")) + if (StringRef(PrintLoc).startswith("<scratch") || + StringRef(PrintLoc).startswith("<command line>")) return false; } return true;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits