10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
22 AST_MATCHER_P(DeducedTemplateSpecializationType, refsToTemplatedDecl,
23 clang::ast_matchers::internal::Matcher<NamedDecl>, DeclMatcher) {
24 if (
const auto *TD = Node.getTemplateName().getAsTemplateDecl())
25 return DeclMatcher.matches(*TD, Finder,
Builder);
35 return isa<RecordDecl>(TargetDecl) || isa<ClassTemplateDecl>(TargetDecl) ||
36 isa<FunctionDecl>(TargetDecl) || isa<VarDecl>(TargetDecl) ||
37 isa<FunctionTemplateDecl>(TargetDecl) || isa<EnumDecl>(TargetDecl) ||
38 isa<EnumConstantDecl>(TargetDecl);
41 void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) {
42 Finder->addMatcher(usingDecl(isExpansionInMainFile()).bind(
"using"),
this);
43 auto DeclMatcher = hasDeclaration(namedDecl().bind(
"used"));
44 Finder->addMatcher(loc(templateSpecializationType(DeclMatcher)),
this);
45 Finder->addMatcher(loc(deducedTemplateSpecializationType(
46 refsToTemplatedDecl(namedDecl().bind(
"used")))),
48 Finder->addMatcher(callExpr(callee(unresolvedLookupExpr().bind(
"used"))),
51 callExpr(hasDeclaration(functionDecl(
52 forEachTemplateArgument(templateArgument().bind(
"used"))))),
54 Finder->addMatcher(loc(templateSpecializationType(forEachTemplateArgument(
55 templateArgument().bind(
"used")))),
60 auto ThroughShadowMatcher = throughUsingDecl(namedDecl().bind(
"usedShadow"));
61 Finder->addMatcher(declRefExpr(ThroughShadowMatcher),
this);
62 Finder->addMatcher(loc(usingType(ThroughShadowMatcher)),
this);
66 if (Result.Context->getDiagnostics().hasUncompilableErrorOccurred())
69 if (
const auto *Using = Result.Nodes.getNodeAs<UsingDecl>(
"using")) {
71 if (Using->getLocation().isMacroID())
75 if (isa<CXXRecordDecl>(Using->getDeclContext()))
81 if (isa<FunctionDecl>(Using->getDeclContext()))
84 UsingDeclContext Context(Using);
85 Context.UsingDeclRange = CharSourceRange::getCharRange(
87 Lexer::findLocationAfterToken(
88 Using->getEndLoc(), tok::semi, *Result.SourceManager, getLangOpts(),
90 for (
const auto *UsingShadow : Using->shadows()) {
91 const auto *TargetDecl = UsingShadow->getTargetDecl()->getCanonicalDecl();
93 Context.UsingTargetDecls.insert(TargetDecl);
95 if (!Context.UsingTargetDecls.empty())
96 Contexts.push_back(Context);
101 auto RemoveNamedDecl = [&](
const NamedDecl *Used) {
102 removeFromFoundDecls(Used);
104 if (
const auto *FD = dyn_cast<FunctionDecl>(Used)) {
105 removeFromFoundDecls(FD->getPrimaryTemplate());
106 }
else if (
const auto *Specialization =
107 dyn_cast<ClassTemplateSpecializationDecl>(Used)) {
108 removeFromFoundDecls(Specialization->getSpecializedTemplate());
109 }
else if (
const auto *FD = dyn_cast<FunctionDecl>(Used)) {
110 if (
const auto *FDT = FD->getPrimaryTemplate())
111 removeFromFoundDecls(FDT);
112 }
else if (
const auto *ECD = dyn_cast<EnumConstantDecl>(Used)) {
113 if (
const auto *ET = ECD->getType()->getAs<EnumType>())
114 removeFromFoundDecls(ET->getDecl());
119 if (
const auto *Used = Result.Nodes.getNodeAs<NamedDecl>(
"used")) {
120 RemoveNamedDecl(Used);
124 if (
const auto *UsedShadow =
125 Result.Nodes.getNodeAs<UsingShadowDecl>(
"usedShadow")) {
126 removeFromFoundDecls(UsedShadow->getTargetDecl());
130 if (
const auto *Used = Result.Nodes.getNodeAs<TemplateArgument>(
"used")) {
131 if (Used->getKind() == TemplateArgument::Template) {
132 if (
const auto *TD = Used->getAsTemplate().getAsTemplateDecl())
133 removeFromFoundDecls(TD);
135 if (
auto *RD = Used->getAsType()->getAsCXXRecordDecl())
136 removeFromFoundDecls(RD);
137 }
else if (Used->getKind() == TemplateArgument::Declaration) {
138 RemoveNamedDecl(Used->getAsDecl());
143 if (
const auto *DRE = Result.Nodes.getNodeAs<DeclRefExpr>(
"used")) {
144 RemoveNamedDecl(DRE->getDecl());
148 if (
const auto *ULE = Result.Nodes.getNodeAs<UnresolvedLookupExpr>(
"used")) {
149 for (
const NamedDecl *ND : ULE->decls()) {
150 if (
const auto *USD = dyn_cast<UsingShadowDecl>(ND))
151 removeFromFoundDecls(USD->getTargetDecl()->getCanonicalDecl());
156 void UnusedUsingDeclsCheck::removeFromFoundDecls(
const Decl *
D) {
164 for (
auto &Context : Contexts) {
165 if (Context.UsingTargetDecls.contains(
D->getCanonicalDecl()))
166 Context.IsUsed =
true;
170 void UnusedUsingDeclsCheck::onEndOfTranslationUnit() {
171 for (
const auto &Context : Contexts) {
172 if (!Context.IsUsed) {
173 diag(Context.FoundUsingDecl->getLocation(),
"using decl %0 is unused")
174 << Context.FoundUsingDecl;
176 diag(Context.FoundUsingDecl->getLocation(),
177 "remove the using", DiagnosticIDs::Note)
178 << FixItHint::CreateRemoval(Context.UsingDeclRange);