[PATCH] D148158: [include-cleaner] Handle incomplete template specializations
Kadir Cetinkaya via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 12 12:55:47 PDT 2023
kadircet created this revision.
kadircet added a reviewer: sammccall.
Herald added a project: All.
kadircet requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.
Instantiation pattern is null for incomplete template types and using
specializaiton decl results in not seeing re-declarations.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D148158
Files:
clang-tools-extra/include-cleaner/lib/WalkAST.cpp
clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
Index: clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
===================================================================
--- clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
@@ -144,6 +144,9 @@
ElementsAre(Decl::CXXRecord));
// Implicit instantiations references most relevant template.
+ EXPECT_THAT(
+ testWalk("template<typename> struct $explicit^Foo {};", "^Foo<int> x();"),
+ ElementsAre(Decl::Kind::ClassTemplate));
EXPECT_THAT(
testWalk("template<typename> struct $explicit^Foo {};", "^Foo<int> x;"),
ElementsAre(Decl::CXXRecord));
@@ -154,9 +157,15 @@
ElementsAre(Decl::ClassTemplateSpecialization));
EXPECT_THAT(testWalk(R"cpp(
template<typename> struct Foo {};
- template<typename T> struct $explicit^Foo<T*> { void x(); };)cpp",
+ template<typename T> struct $explicit^Foo<T*> {};)cpp",
"^Foo<int *> x;"),
ElementsAre(Decl::ClassTemplatePartialSpecialization));
+ // Incomplete templates don't have a specific specialization associated.
+ EXPECT_THAT(testWalk(R"cpp(
+ template<typename> struct $explicit^Foo {};
+ template<typename T> struct Foo<T*> {};)cpp",
+ "^Foo<int *> x();"),
+ ElementsAre(Decl::Kind::ClassTemplate));
EXPECT_THAT(testWalk(R"cpp(
template<typename> struct $explicit^Foo {};
template struct Foo<int>;)cpp",
Index: clang-tools-extra/include-cleaner/lib/WalkAST.cpp
===================================================================
--- clang-tools-extra/include-cleaner/lib/WalkAST.cpp
+++ clang-tools-extra/include-cleaner/lib/WalkAST.cpp
@@ -21,6 +21,7 @@
#include "clang/AST/TypeLoc.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/Specifiers.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/Support/Casting.h"
@@ -83,7 +84,7 @@
// This is the underlying decl used by TemplateSpecializationType, can be
// null when type is dependent if so fallback to primary template.
CXXRecordDecl *TD = TST->getAsCXXRecordDecl();
- if (!TD)
+ if (!TD || TD->getTemplateSpecializationKind() == TSK_Undeclared)
return ND;
// We ignore explicit instantiations. This might imply marking the wrong
// declaration as used in specific cases, but seems like the right trade-off
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148158.512944.patch
Type: text/x-patch
Size: 2501 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230412/da66420b/attachment.bin>
More information about the cfe-commits
mailing list