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 void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) {
22   Finder->addMatcher(usingDecl(isExpansionInMainFile()).bind("using"), this);
23   auto DeclMatcher = hasDeclaration(namedDecl().bind("used"));
24   Finder->addMatcher(loc(recordType(DeclMatcher)), this);
25   Finder->addMatcher(loc(templateSpecializationType(DeclMatcher)), this);
26   Finder->addMatcher(declRefExpr().bind("used"), this);
27 }
28 
29 void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
30   if (const auto *Using = Result.Nodes.getNodeAs<UsingDecl>("using")) {
31     // FIXME: Implement the correct behavior for using declarations with more
32     // than one shadow.
33     if (Using->shadow_size() != 1)
34       return;
35     const auto *TargetDecl =
36         Using->shadow_begin()->getTargetDecl()->getCanonicalDecl();
37 
38     // Ignores using-declarations defined in macros.
39     if (TargetDecl->getLocation().isMacroID())
40       return;
41 
42     // Ignores using-declarations defined in class definition.
43     if (isa<CXXRecordDecl>(TargetDecl->getDeclContext()))
44       return;
45 
46     if (!isa<RecordDecl>(TargetDecl) && !isa<ClassTemplateDecl>(TargetDecl) &&
47         !isa<FunctionDecl>(TargetDecl) && !isa<VarDecl>(TargetDecl) &&
48         !isa<FunctionTemplateDecl>(TargetDecl))
49       return;
50 
51     FoundDecls[TargetDecl] = Using;
52     FoundRanges[TargetDecl] = CharSourceRange::getCharRange(
53         Using->getLocStart(),
54         Lexer::findLocationAfterToken(
55             Using->getLocEnd(), tok::semi, *Result.SourceManager,
56             Result.Context->getLangOpts(),
57             /*SkipTrailingWhitespaceAndNewLine=*/true));
58     return;
59   }
60 
61   // Mark using declarations as used by setting FoundDecls' value to zero. As
62   // the AST is walked in order, usages are only marked after a the
63   // corresponding using declaration has been found.
64   // FIXME: This currently doesn't look at whether the type reference is
65   // actually found with the help of the using declaration.
66   if (const auto *Used = Result.Nodes.getNodeAs<NamedDecl>("used")) {
67     if (const auto *Specialization =
68             dyn_cast<ClassTemplateSpecializationDecl>(Used))
69       Used = Specialization->getSpecializedTemplate();
70     removeFromFoundDecls(Used);
71     return;
72   }
73 
74   if (const auto *DRE = Result.Nodes.getNodeAs<DeclRefExpr>("used")) {
75     if (const auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) {
76       if (const auto *FDT = FD->getPrimaryTemplate())
77         removeFromFoundDecls(FDT);
78       else
79         removeFromFoundDecls(FD);
80     } else if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
81       removeFromFoundDecls(VD);
82     }
83   }
84 }
85 
86 void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) {
87   auto I = FoundDecls.find(D->getCanonicalDecl());
88   if (I != FoundDecls.end())
89     I->second = nullptr;
90 }
91 
92 void UnusedUsingDeclsCheck::onEndOfTranslationUnit() {
93   for (const auto &FoundDecl : FoundDecls) {
94     if (FoundDecl.second == nullptr)
95       continue;
96     diag(FoundDecl.second->getLocation(), "using decl %0 is unused")
97         << FoundDecl.second
98         << FixItHint::CreateRemoval(FoundRanges[FoundDecl.first]);
99   }
100   FoundDecls.clear();
101 }
102 
103 } // namespace misc
104 } // namespace tidy
105 } // namespace clang
106