Skip to content

Commit 881aab4

Browse files
committed
[clangd] No longer getting template instantiations from header files in Main AST.
Previous implementation to filter decls not in the main file did not work in the case where a template was instantiated from a header in the main file. It would than include that function/class in topLevelDecls. Differential Revision: https://reviews.llvm.org/D63817 llvm-svn: 364747
1 parent 92e78b7 commit 881aab4

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

clang-tools-extra/clangd/ClangdUnit.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ class DeclTrackingASTConsumer : public ASTConsumer {
6767

6868
bool HandleTopLevelDecl(DeclGroupRef DG) override {
6969
for (Decl *D : DG) {
70-
if (D->isFromASTFile())
70+
auto &SM = D->getASTContext().getSourceManager();
71+
if (!SM.isWrittenInMainFile(SM.getExpansionLoc(D->getLocation())))
7172
continue;
7273

7374
// ObjCMethodDecl are not actually top-level decls.

clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,26 @@ TEST(ClangdUnitTest, TopLevelDecls) {
8383
EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
8484
}
8585

86+
TEST(ClangdUnitTest, DoesNotGetIncludedTopDecls) {
87+
TestTU TU;
88+
TU.HeaderCode = R"cpp(
89+
#define LL void foo(){}
90+
template<class T>
91+
struct H {
92+
H() {}
93+
LL
94+
};
95+
)cpp";
96+
TU.Code = R"cpp(
97+
int main() {
98+
H<int> h;
99+
h.foo();
100+
}
101+
)cpp";
102+
auto AST = TU.build();
103+
EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
104+
}
105+
86106
TEST(ClangdUnitTest, TokensAfterPreamble) {
87107
TestTU TU;
88108
TU.AdditionalFiles["foo.h"] = R"(

0 commit comments

Comments
 (0)