11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/Lex/Lexer.h" 25 return isa<RecordDecl>(TargetDecl) || isa<ClassTemplateDecl>(TargetDecl) ||
26 isa<FunctionDecl>(TargetDecl) || isa<VarDecl>(TargetDecl) ||
27 isa<FunctionTemplateDecl>(TargetDecl) || isa<EnumDecl>(TargetDecl) ||
28 isa<EnumConstantDecl>(TargetDecl);
31 void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) {
32 Finder->addMatcher(usingDecl(isExpansionInMainFile()).bind(
"using"),
this);
33 auto DeclMatcher = hasDeclaration(namedDecl().bind(
"used"));
34 Finder->addMatcher(loc(enumType(DeclMatcher)),
this);
35 Finder->addMatcher(loc(recordType(DeclMatcher)),
this);
36 Finder->addMatcher(loc(templateSpecializationType(DeclMatcher)),
this);
37 Finder->addMatcher(declRefExpr().bind(
"used"),
this);
38 Finder->addMatcher(callExpr(callee(unresolvedLookupExpr().bind(
"used"))),
41 callExpr(hasDeclaration(functionDecl(hasAnyTemplateArgument(
42 anyOf(refersToTemplate(templateName().bind(
"used")),
43 refersToDeclaration(functionDecl().bind(
"used"))))))),
45 Finder->addMatcher(loc(templateSpecializationType(hasAnyTemplateArgument(
46 templateArgument().bind(
"used")))),
50 void UnusedUsingDeclsCheck::check(
const MatchFinder::MatchResult &Result) {
51 if (Result.Context->getDiagnostics().hasUncompilableErrorOccurred())
54 if (
const auto *Using = Result.Nodes.getNodeAs<UsingDecl>(
"using")) {
56 if (Using->getLocation().isMacroID())
60 if (isa<CXXRecordDecl>(Using->getDeclContext()))
66 if (isa<FunctionDecl>(Using->getDeclContext()))
69 UsingDeclContext Context(Using);
70 Context.UsingDeclRange = CharSourceRange::getCharRange(
72 Lexer::findLocationAfterToken(
73 Using->getLocEnd(), tok::semi, *Result.SourceManager, getLangOpts(),
75 for (
const auto *UsingShadow : Using->shadows()) {
76 const auto *TargetDecl = UsingShadow->getTargetDecl()->getCanonicalDecl();
78 Context.UsingTargetDecls.insert(TargetDecl);
80 if (!Context.UsingTargetDecls.empty())
81 Contexts.push_back(Context);
89 if (
const auto *Used = Result.Nodes.getNodeAs<NamedDecl>(
"used")) {
90 if (
const auto *FD = dyn_cast<FunctionDecl>(Used)) {
91 removeFromFoundDecls(FD->getPrimaryTemplate());
92 }
else if (
const auto *Specialization =
93 dyn_cast<ClassTemplateSpecializationDecl>(Used)) {
94 Used = Specialization->getSpecializedTemplate();
96 removeFromFoundDecls(Used);
100 if (
const auto *Used = Result.Nodes.getNodeAs<TemplateArgument>(
"used")) {
102 if (Used->getKind() == TemplateArgument::Template) {
103 if (
const auto *TD = Used->getAsTemplate().getAsTemplateDecl())
104 removeFromFoundDecls(TD);
105 }
else if (Used->getKind() == TemplateArgument::Type) {
106 if (
auto *RD = Used->getAsType()->getAsCXXRecordDecl())
107 removeFromFoundDecls(RD);
112 if (
const auto *Used = Result.Nodes.getNodeAs<TemplateName>(
"used")) {
113 removeFromFoundDecls(Used->getAsTemplateDecl());
117 if (
const auto *DRE = Result.Nodes.getNodeAs<DeclRefExpr>(
"used")) {
118 if (
const auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) {
119 if (
const auto *FDT = FD->getPrimaryTemplate())
120 removeFromFoundDecls(FDT);
122 removeFromFoundDecls(FD);
123 }
else if (
const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
124 removeFromFoundDecls(VD);
125 }
else if (
const auto *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
126 removeFromFoundDecls(ECD);
127 if (
const auto *ET = ECD->getType()->getAs<EnumType>())
128 removeFromFoundDecls(ET->getDecl());
132 if (
const auto *ULE = Result.Nodes.getNodeAs<UnresolvedLookupExpr>(
"used")) {
133 for (
const NamedDecl *ND : ULE->decls()) {
134 if (
const auto *USD = dyn_cast<UsingShadowDecl>(ND))
135 removeFromFoundDecls(USD->getTargetDecl()->getCanonicalDecl());
140 void UnusedUsingDeclsCheck::removeFromFoundDecls(
const Decl *D) {
148 for (
auto &Context : Contexts) {
149 if (Context.UsingTargetDecls.count(D->getCanonicalDecl()) > 0)
150 Context.IsUsed =
true;
154 void UnusedUsingDeclsCheck::onEndOfTranslationUnit() {
155 for (
const auto &Context : Contexts) {
156 if (!Context.IsUsed) {
157 diag(Context.FoundUsingDecl->getLocation(),
"using decl %0 is unused")
158 << Context.FoundUsingDecl
159 << FixItHint::CreateRemoval(Context.UsingDeclRange);
static bool ShouldCheckDecl(const Decl *TargetDecl)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//