Author: jvikstrom Date: Mon Aug 12 06:01:11 2019 New Revision: 368563 URL: http://llvm.org/viewvc/llvm-project?rev=368563&view=rev Log: [clangd] Remove highlightings coming from non topLevelDecls from included files.
Summary: It is possible to write include code from other files so that the decls from there do not become topLevelDecls (For example by including methods for a class). These Decls are not filtered by topLevelDecls and therefore SemanticHighlighting must manually check that every SLoc belongs in the main file. Otherwise there can be highlightings appearing in places where they should not. Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66083 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=368563&r1=368562&r2=368563&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Mon Aug 12 06:01:11 2019 @@ -231,6 +231,12 @@ private: // FIXME: skip tokens inside macros for now. return; + // Non top level decls that are included from a header are not filtered by + // topLevelDecls. (example: method declarations being included from another + // file for a class from another file) + if (!isInsideMainFile(Loc, SM)) + return; + auto R = getTokenRange(SM, Ctx.getLangOpts(), Loc); if (!R) { // R should always have a value, if it doesn't something is very wrong. Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=368563&r1=368562&r2=368563&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Mon Aug 12 06:01:11 2019 @@ -51,9 +51,15 @@ std::vector<HighlightingToken> getExpect return ExpectedTokens; } -void checkHighlightings(llvm::StringRef Code) { +void checkHighlightings(llvm::StringRef Code, + std::vector<std::pair</*FileName*/ llvm::StringRef, + /*FileContent*/ llvm::StringRef>> + AdditionalFiles = {}) { Annotations Test(Code); - auto AST = TestTU::withCode(Test.code()).build(); + auto TU = TestTU::withCode(Test.code()); + for (auto File : AdditionalFiles) + TU.AdditionalFiles.insert({File.first, File.second}); + auto AST = TU.build(); std::vector<HighlightingToken> ActualTokens = getSemanticHighlightings(AST); EXPECT_THAT(ActualTokens, getExpectedTokens(Test)) << Code; } @@ -321,6 +327,17 @@ TEST(SemanticHighlighting, GetsCorrectTo for (const auto &TestCase : TestCases) { checkHighlightings(TestCase); } + + checkHighlightings(R"cpp( + class $Class[[A]] { + #include "imp.h" + }; + #endif + )cpp", + {{"imp.h", R"cpp( + int someMethod(); + void otherMethod(); + )cpp"}}); } TEST(SemanticHighlighting, GeneratesHighlightsWhenFileChange) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits