[clangd] Don't treat top-level decls as "local" if they are from the preamble.

Summary:
These get passed to HandleTopLevelDecl() if they happen to have been
deserialized for any reason. We don't want to treat them as part of the
main file.

Reviewers: ilya-biryukov

Subscribers: ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits

Differential Revision: https://reviews.llvm.org/D54303

llvm-svn: 346503
This commit is contained in:
Sam McCall 2018-11-09 15:35:00 +00:00
parent f3cae3ab4c
commit 9ac2f7a1f8
2 changed files with 25 additions and 0 deletions

View File

@ -57,6 +57,9 @@ public:
bool HandleTopLevelDecl(DeclGroupRef DG) override { bool HandleTopLevelDecl(DeclGroupRef DG) override {
for (Decl *D : DG) { for (Decl *D : DG) {
if (D->isFromASTFile())
continue;
// ObjCMethodDecl are not actually top-level decls. // ObjCMethodDecl are not actually top-level decls.
if (isa<ObjCMethodDecl>(D)) if (isa<ObjCMethodDecl>(D))
continue; continue;

View File

@ -257,6 +257,28 @@ Bar* bar;
} }
} }
MATCHER_P(DeclNamed, Name, "") {
if (NamedDecl *ND = dyn_cast<NamedDecl>(arg))
if (ND->getName() == Name)
return true;
if (auto *Stream = result_listener->stream()) {
llvm::raw_os_ostream OS(*Stream);
arg->dump(OS);
}
return false;
}
TEST(ClangdUnitTest, TopLevelDecls) {
TestTU TU;
TU.HeaderCode = R"(
int header1();
int header2;
)";
TU.Code = "int main();";
auto AST = TU.build();
EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
}
} // namespace } // namespace
} // namespace clangd } // namespace clangd
} // namespace clang } // namespace clang