Skip to content

Commit

Permalink
[clangd] No longer getting template instantiations from header files …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
jvikstrom committed Jul 1, 2019
1 parent 92e78b7 commit 881aab4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clang-tools-extra/clangd/ClangdUnit.cpp
Expand Up @@ -67,7 +67,8 @@ class DeclTrackingASTConsumer : public ASTConsumer {

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

// ObjCMethodDecl are not actually top-level decls.
Expand Down
20 changes: 20 additions & 0 deletions clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
Expand Up @@ -83,6 +83,26 @@ TEST(ClangdUnitTest, TopLevelDecls) {
EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
}

TEST(ClangdUnitTest, DoesNotGetIncludedTopDecls) {
TestTU TU;
TU.HeaderCode = R"cpp(
#define LL void foo(){}
template<class T>
struct H {
H() {}
LL
};
)cpp";
TU.Code = R"cpp(
int main() {
H<int> h;
h.foo();
}
)cpp";
auto AST = TU.build();
EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
}

TEST(ClangdUnitTest, TokensAfterPreamble) {
TestTU TU;
TU.AdditionalFiles["foo.h"] = R"(
Expand Down

0 comments on commit 881aab4

Please sign in to comment.