Author: Haojian Wu Date: 2020-04-02T09:29:20+02:00 New Revision: b42006596905bea7bef54759ba3a5d1a7dd418fa
URL: https://github.com/llvm/llvm-project/commit/b42006596905bea7bef54759ba3a5d1a7dd418fa DIFF: https://github.com/llvm/llvm-project/commit/b42006596905bea7bef54759ba3a5d1a7dd418fa.diff LOG: [clangd] Fix an assertion crash in ReferenceFinder. Summary: The assertion is almost correct, but it fails on refs from non-preamble Reviewers: sammccall Reviewed By: sammccall Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D77222 Added: Modified: clang-tools-extra/clangd/XRefs.cpp clang-tools-extra/clangd/unittests/XRefsTests.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp index 7e27be38bcc3..3bf8d0e818e8 100644 --- a/clang-tools-extra/clangd/XRefs.cpp +++ b/clang-tools-extra/clangd/XRefs.cpp @@ -583,13 +583,11 @@ class ReferenceFinder : public index::IndexDataConsumer { SourceLocation Loc, index::IndexDataConsumer::ASTNodeInfo ASTNode) override { assert(D->isCanonicalDecl() && "expect D to be a canonical declaration"); - if (!CanonicalTargets.count(D)) + const SourceManager &SM = AST.getSourceManager(); + if (!CanonicalTargets.count(D) || !isInsideMainFile(Loc, SM)) return true; const auto &TB = AST.getTokens(); - const SourceManager &SM = AST.getSourceManager(); Loc = SM.getFileLoc(Loc); - // We are only traversing decls *inside* the main file, so this should hold. - assert(isInsideMainFile(Loc, SM)); if (const auto *Tok = TB.spelledTokenAt(Loc)) References.push_back({*Tok, Roles}); return true; diff --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp index 6b568456ba02..ce7f76ccf4f4 100644 --- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -1121,6 +1121,30 @@ TEST(FindReferences, WithinAST) { } } +TEST(FindReferences, MainFileReferencesOnly) { + llvm::StringRef Test = + R"cpp( + void test() { + int [[fo^o]] = 1; + // refs not from main file should not be included. + #include "foo.inc" + })cpp"; + + Annotations Code(Test); + auto TU = TestTU::withCode(Code.code()); + TU.AdditionalFiles["foo.inc"] = R"cpp( + foo = 3; + )cpp"; + auto AST = TU.build(); + + std::vector<Matcher<Location>> ExpectedLocations; + for (const auto &R : Code.ranges()) + ExpectedLocations.push_back(RangeIs(R)); + EXPECT_THAT(findReferences(AST, Code.point(), 0).References, + ElementsAreArray(ExpectedLocations)) + << Test; +} + TEST(FindReferences, ExplicitSymbols) { const char *Tests[] = { R"cpp( _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits