[PATCH] D65510: [clangd] Fix implicit template instatiations appearing as topLevelDecls.
Johan Vikström via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 5 05:23:12 PDT 2019
jvikstrom updated this revision to Diff 213331.
jvikstrom marked an inline comment as done.
jvikstrom added a comment.
Formatted.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D65510/new/
https://reviews.llvm.org/D65510
Files:
clang-tools-extra/clangd/AST.h
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
@@ -103,6 +103,20 @@
EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
}
+TEST(ClangdUnitTest, DoesNotGetImplicitTemplateTopDecls) {
+ TestTU TU;
+ TU.Code = R"cpp(
+ template<typename T>
+ void f(T) {}
+ void s() {
+ f(10UL);
+ }
+ )cpp";
+ auto AST = TU.build();
+ EXPECT_THAT(AST.getLocalTopLevelDecls(),
+ ElementsAre(DeclNamed("f"), DeclNamed("s")));
+}
+
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
@@ -19,8 +19,11 @@
#include "index/CanonicalIncludes.h"
#include "index/Index.h"
#include "clang/AST/ASTContext.h"
+#include "clang/AST/Decl.h"
+#include "clang/AST/DeclCXX.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/SourceManager.h"
+#include "clang/Basic/Specifiers.h"
#include "clang/Basic/TokenKinds.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/CompilerInvocation.h"
@@ -60,6 +63,12 @@
return Vec.capacity() * sizeof(T);
}
+template <class T> bool isImplicitTemplateInstantiation(const Decl *D) {
+ if (const auto *TD = dyn_cast<T>(D))
+ return TD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation;
+ return false;
+}
+
class DeclTrackingASTConsumer : public ASTConsumer {
public:
DeclTrackingASTConsumer(std::vector<Decl *> &TopLevelDecls)
@@ -70,6 +79,10 @@
auto &SM = D->getASTContext().getSourceManager();
if (!isInsideMainFile(D->getLocation(), SM))
continue;
+ if (isImplicitTemplateInstantiation<FunctionDecl>(D) ||
+ isImplicitTemplateInstantiation<CXXRecordDecl>(D) ||
+ isImplicitTemplateInstantiation<VarDecl>(D))
+ continue;
// ObjCMethodDecl are not actually top-level decls.
if (isa<ObjCMethodDecl>(D))
Index: clang-tools-extra/clangd/AST.h
===================================================================
--- clang-tools-extra/clangd/AST.h
+++ clang-tools-extra/clangd/AST.h
@@ -81,7 +81,7 @@
/// Example: shortenNamespace("ns1::MyClass<ns1::OtherClass>", "ns1")
/// --> "MyClass<ns1::OtherClass>"
std::string shortenNamespace(const llvm::StringRef OriginalName,
- const llvm::StringRef CurrentNamespace);
+ const llvm::StringRef CurrentNamespace);
} // namespace clangd
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65510.213331.patch
Type: text/x-patch
Size: 2785 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190805/95fe3d78/attachment-0001.bin>
More information about the cfe-commits
mailing list