1 //===--- UnusedUsingDeclsCheck.cpp - clang-tidy----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "UnusedUsingDeclsCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
14 
15 using namespace clang::ast_matchers;
16 
17 namespace clang {
18 namespace tidy {
19 namespace misc {
20 
21 // A function that helps to tell whether a TargetDecl in a UsingDecl will be
22 // checked. Only variable, function, function template, class template, class,
23 // enum declaration and enum constant declaration are considered.
24 static bool ShouldCheckDecl(const Decl *TargetDecl) {
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);
29 }
30 
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"))),
39                      this);
40   Finder->addMatcher(
41       callExpr(hasDeclaration(functionDecl(hasAnyTemplateArgument(
42           anyOf(refersToTemplate(templateName().bind("used")),
43                 refersToDeclaration(functionDecl().bind("used"))))))),
44       this);
45   Finder->addMatcher(loc(templateSpecializationType(hasAnyTemplateArgument(
46                          templateArgument().bind("used")))),
47                      this);
48 }
49 
50 void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
51   if (const auto *Using = Result.Nodes.getNodeAs<UsingDecl>("using")) {
52     // Ignores using-declarations defined in macros.
53     if (Using->getLocation().isMacroID())
54       return;
55 
56     // Ignores using-declarations defined in class definition.
57     if (isa<CXXRecordDecl>(Using->getDeclContext()))
58       return;
59 
60     // FIXME: We ignore using-decls defined in function definitions at the
61     // moment because of false positives caused by ADL and different function
62     // scopes.
63     if (isa<FunctionDecl>(Using->getDeclContext()))
64       return;
65 
66     UsingDeclContext Context(Using);
67     Context.UsingDeclRange = CharSourceRange::getCharRange(
68         Using->getLocStart(),
69         Lexer::findLocationAfterToken(
70             Using->getLocEnd(), tok::semi, *Result.SourceManager, getLangOpts(),
71             /*SkipTrailingWhitespaceAndNewLine=*/true));
72     for (const auto *UsingShadow : Using->shadows()) {
73       const auto *TargetDecl = UsingShadow->getTargetDecl()->getCanonicalDecl();
74       if (ShouldCheckDecl(TargetDecl))
75         Context.UsingTargetDecls.insert(TargetDecl);
76     }
77     if (!Context.UsingTargetDecls.empty())
78       Contexts.push_back(Context);
79     return;
80   }
81   // Mark using declarations as used by setting FoundDecls' value to zero. As
82   // the AST is walked in order, usages are only marked after a the
83   // corresponding using declaration has been found.
84   // FIXME: This currently doesn't look at whether the type reference is
85   // actually found with the help of the using declaration.
86   if (const auto *Used = Result.Nodes.getNodeAs<NamedDecl>("used")) {
87     if (const auto *FD = dyn_cast<FunctionDecl>(Used)) {
88       removeFromFoundDecls(FD->getPrimaryTemplate());
89     } else if (const auto *Specialization =
90                    dyn_cast<ClassTemplateSpecializationDecl>(Used)) {
91       Used = Specialization->getSpecializedTemplate();
92     }
93     removeFromFoundDecls(Used);
94     return;
95   }
96 
97   if (const auto *Used = Result.Nodes.getNodeAs<TemplateArgument>("used")) {
98     // FIXME: Support non-type template parameters.
99     if (Used->getKind() == TemplateArgument::Template) {
100       if (const auto *TD = Used->getAsTemplate().getAsTemplateDecl())
101         removeFromFoundDecls(TD);
102     } else if (Used->getKind() == TemplateArgument::Type) {
103       if (auto *RD = Used->getAsType()->getAsCXXRecordDecl())
104         removeFromFoundDecls(RD);
105     }
106     return;
107   }
108 
109   if (const auto *Used = Result.Nodes.getNodeAs<TemplateName>("used")) {
110     removeFromFoundDecls(Used->getAsTemplateDecl());
111     return;
112   }
113 
114   if (const auto *DRE = Result.Nodes.getNodeAs<DeclRefExpr>("used")) {
115     if (const auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) {
116       if (const auto *FDT = FD->getPrimaryTemplate())
117         removeFromFoundDecls(FDT);
118       else
119         removeFromFoundDecls(FD);
120     } else if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
121       removeFromFoundDecls(VD);
122     } else if (const auto *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
123       removeFromFoundDecls(ECD);
124       if (const auto *ET = ECD->getType()->getAs<EnumType>())
125         removeFromFoundDecls(ET->getDecl());
126     }
127   }
128   // Check the uninstantiated template function usage.
129   if (const auto *ULE = Result.Nodes.getNodeAs<UnresolvedLookupExpr>("used")) {
130     for (const NamedDecl *ND : ULE->decls()) {
131       if (const auto *USD = dyn_cast<UsingShadowDecl>(ND))
132         removeFromFoundDecls(USD->getTargetDecl()->getCanonicalDecl());
133     }
134   }
135 }
136 
137 void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) {
138   if (!D)
139     return;
140   // FIXME: Currently, we don't handle the using-decls being used in different
141   // scopes (such as different namespaces, different functions). Instead of
142   // giving an incorrect message, we mark all of them as used.
143   //
144   // FIXME: Use a more efficient way to find a matching context.
145   for (auto &Context : Contexts) {
146     if (Context.UsingTargetDecls.count(D->getCanonicalDecl()) > 0)
147       Context.IsUsed = true;
148   }
149 }
150 
151 void UnusedUsingDeclsCheck::onEndOfTranslationUnit() {
152   for (const auto &Context : Contexts) {
153     if (!Context.IsUsed) {
154       diag(Context.FoundUsingDecl->getLocation(), "using decl %0 is unused")
155           << Context.FoundUsingDecl
156           << FixItHint::CreateRemoval(Context.UsingDeclRange);
157     }
158   }
159   Contexts.clear();
160 }
161 
162 } // namespace misc
163 } // namespace tidy
164 } // namespace clang
165