[PATCH] D63817: [clangd] No longer getting template instantiations from header files in Main AST.Added another check to DeclTrackingASTConsumer to check that a Decl is actually in the main file.
Johan Vikström via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 26 05:49:16 PDT 2019
jvikstrom created this revision.
jvikstrom added reviewers: hokein, ilya-biryukov, sammccall.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay.
Herald added a project: clang.
[clangd] No longer getting template instantiations from header files in Main AST.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D63817
Files:
clang-tools-extra/clangd/ClangdUnit.cpp
clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
Index: clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
+++ clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
@@ -83,6 +83,23 @@
EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
}
+TEST(ClangdUnitTest, DoesNotGetIncludedTopDecls) {
+ TestTU TU;
+ TU.HeaderCode = R"cpp(
+ template<class T>
+ struct H {
+ H() {}
+ };
+ )cpp";
+ TU.Code = R"cpp(
+ int main() {
+ H<int> h;
+ }
+ )cpp";
+ auto AST = TU.build();
+ EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
+}
+
TEST(ClangdUnitTest, TokensAfterPreamble) {
TestTU TU;
TU.AdditionalFiles["foo.h"] = R"(
Index: clang-tools-extra/clangd/ClangdUnit.cpp
===================================================================
--- clang-tools-extra/clangd/ClangdUnit.cpp
+++ clang-tools-extra/clangd/ClangdUnit.cpp
@@ -61,6 +61,7 @@
}
class DeclTrackingASTConsumer : public ASTConsumer {
+ SourceManager *SM;
public:
DeclTrackingASTConsumer(std::vector<Decl *> &TopLevelDecls)
: TopLevelDecls(TopLevelDecls) {}
@@ -70,6 +71,11 @@
if (D->isFromASTFile())
continue;
+ if (!SM->isInMainFile(D->getLocation()))
+ // This decl comes from another file and should not be included in the
+ // top level decls.
+ continue;
+
// ObjCMethodDecl are not actually top-level decls.
if (isa<ObjCMethodDecl>(D))
continue;
@@ -79,6 +85,10 @@
return true;
}
+ virtual void Initialize(ASTContext &Context) override {
+ SM = &Context.getSourceManager();
+ }
+
private:
std::vector<Decl *> &TopLevelDecls;
};
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63817.206643.patch
Type: text/x-patch
Size: 1767 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190626/055958f3/attachment-0001.bin>
More information about the cfe-commits
mailing list