nridge created this revision. nridge added a reviewer: sammccall. Herald added subscribers: cfe-commits, usaxena95, kadircet, arphaman, jkorous, MaskRay, ilya-biryukov. Herald added a project: clang.
Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D76098 Files: clang-tools-extra/clangd/XRefs.cpp clang-tools-extra/clangd/unittests/XRefsTests.cpp Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -624,11 +624,12 @@ TEST(LocateSymbol, Textual) { const char *Tests[] = { R"cpp(// Comment - struct [[MyClass]] {}; - // Comment mentioning M^yClass - )cpp", + struct [[MyClass]] {}; + // Comment mentioning M^yClass + )cpp", R"cpp(// String - struct [[MyClass]] {}; + struct MyClass {}; + // Not triggered for string literal tokens. const char* s = "String literal mentioning M^yClass"; )cpp", R"cpp(// Ifdef'ed out code @@ -680,7 +681,7 @@ EXPECT_EQ(Results[0].PreferredDeclaration.range, *WantDecl) << Test; } } -} +} // namespace TEST(LocateSymbol, Ambiguous) { auto T = Annotations(R"cpp( Index: clang-tools-extra/clangd/XRefs.cpp =================================================================== --- clang-tools-extra/clangd/XRefs.cpp +++ clang-tools-extra/clangd/XRefs.cpp @@ -355,6 +355,20 @@ return !WordExpandedTokens.empty(); } +enum TokenFlavor { Identifier, Comment, Other }; + +TokenFlavor getTokenFlavor(SourceLocation Loc, const SourceManager &SM, + const LangOptions &LangOpts) { + Token Tok; + if (Lexer::getRawToken(Loc, Tok, SM, LangOpts)) + return Other; + if (Tok.is(tok::TokenKind::raw_identifier)) + return Identifier; + if (Tok.is(tok::TokenKind::comment)) + return Comment; + return Other; +} + } // namespace std::vector<LocatedSymbol> @@ -363,6 +377,11 @@ const std::string &MainFilePath) { const auto &SM = AST.getSourceManager(); + TokenFlavor Flavor = getTokenFlavor(Loc, SM, AST.getLangOpts()); + // Only consider comment and (raw) identifier tokens. + if (!(Flavor == TokenFlavor::Comment || Flavor == TokenFlavor::Identifier)) + return {}; + // Get the raw word at the specified location. unsigned Pos; FileID File; @@ -374,12 +393,14 @@ unsigned WordOffset = Word.data() - Code.data(); SourceLocation WordStart = SM.getComposedLoc(File, WordOffset); - // Do not consider tokens that survived preprocessing. - // We are erring on the safe side here, as a user may expect to get - // accurate (as opposed to textual-heuristic) results for such tokens. + // If this is an identifier token, do not consider if it it survived + // preprocessing. We are erring on the safe side here, as a user may expect to + // get accurate (as opposed to textual-heuristic) results for such tokens. // FIXME: Relax this for dependent code. - if (tokenSurvivedPreprocessing(WordStart, AST.getTokens())) + if (Flavor == TokenFlavor::Identifier && + tokenSurvivedPreprocessing(WordStart, AST.getTokens())) { return {}; + } // Additionally filter for signals that the word is likely to be an // identifier. This avoids triggering on e.g. random words in a comment.
Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -624,11 +624,12 @@ TEST(LocateSymbol, Textual) { const char *Tests[] = { R"cpp(// Comment - struct [[MyClass]] {}; - // Comment mentioning M^yClass - )cpp", + struct [[MyClass]] {}; + // Comment mentioning M^yClass + )cpp", R"cpp(// String - struct [[MyClass]] {}; + struct MyClass {}; + // Not triggered for string literal tokens. const char* s = "String literal mentioning M^yClass"; )cpp", R"cpp(// Ifdef'ed out code @@ -680,7 +681,7 @@ EXPECT_EQ(Results[0].PreferredDeclaration.range, *WantDecl) << Test; } } -} +} // namespace TEST(LocateSymbol, Ambiguous) { auto T = Annotations(R"cpp( Index: clang-tools-extra/clangd/XRefs.cpp =================================================================== --- clang-tools-extra/clangd/XRefs.cpp +++ clang-tools-extra/clangd/XRefs.cpp @@ -355,6 +355,20 @@ return !WordExpandedTokens.empty(); } +enum TokenFlavor { Identifier, Comment, Other }; + +TokenFlavor getTokenFlavor(SourceLocation Loc, const SourceManager &SM, + const LangOptions &LangOpts) { + Token Tok; + if (Lexer::getRawToken(Loc, Tok, SM, LangOpts)) + return Other; + if (Tok.is(tok::TokenKind::raw_identifier)) + return Identifier; + if (Tok.is(tok::TokenKind::comment)) + return Comment; + return Other; +} + } // namespace std::vector<LocatedSymbol> @@ -363,6 +377,11 @@ const std::string &MainFilePath) { const auto &SM = AST.getSourceManager(); + TokenFlavor Flavor = getTokenFlavor(Loc, SM, AST.getLangOpts()); + // Only consider comment and (raw) identifier tokens. + if (!(Flavor == TokenFlavor::Comment || Flavor == TokenFlavor::Identifier)) + return {}; + // Get the raw word at the specified location. unsigned Pos; FileID File; @@ -374,12 +393,14 @@ unsigned WordOffset = Word.data() - Code.data(); SourceLocation WordStart = SM.getComposedLoc(File, WordOffset); - // Do not consider tokens that survived preprocessing. - // We are erring on the safe side here, as a user may expect to get - // accurate (as opposed to textual-heuristic) results for such tokens. + // If this is an identifier token, do not consider if it it survived + // preprocessing. We are erring on the safe side here, as a user may expect to + // get accurate (as opposed to textual-heuristic) results for such tokens. // FIXME: Relax this for dependent code. - if (tokenSurvivedPreprocessing(WordStart, AST.getTokens())) + if (Flavor == TokenFlavor::Identifier && + tokenSurvivedPreprocessing(WordStart, AST.getTokens())) { return {}; + } // Additionally filter for signals that the word is likely to be an // identifier. This avoids triggering on e.g. random words in a comment.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits