clang-tools  3.9.0
UnusedUsingDeclsCheck.cpp
Go to the documentation of this file.
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 }
41 
42 void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
43  if (const auto *Using = Result.Nodes.getNodeAs<UsingDecl>("using")) {
44  // Ignores using-declarations defined in macros.
45  if (Using->getLocation().isMacroID())
46  return;
47 
48  // Ignores using-declarations defined in class definition.
49  if (isa<CXXRecordDecl>(Using->getDeclContext()))
50  return;
51 
52  // FIXME: We ignore using-decls defined in function definitions at the
53  // moment because of false positives caused by ADL and different function
54  // scopes.
55  if (isa<FunctionDecl>(Using->getDeclContext()))
56  return;
57 
58  UsingDeclContext Context(Using);
59  Context.UsingDeclRange = CharSourceRange::getCharRange(
60  Using->getLocStart(),
61  Lexer::findLocationAfterToken(
62  Using->getLocEnd(), tok::semi, *Result.SourceManager,
63  Result.Context->getLangOpts(),
64  /*SkipTrailingWhitespaceAndNewLine=*/true));
65  for (const auto *UsingShadow : Using->shadows()) {
66  const auto *TargetDecl = UsingShadow->getTargetDecl()->getCanonicalDecl();
67  if (ShouldCheckDecl(TargetDecl))
68  Context.UsingTargetDecls.insert(TargetDecl);
69  }
70  if (!Context.UsingTargetDecls.empty())
71  Contexts.push_back(Context);
72  return;
73  }
74 
75  // Mark using declarations as used by setting FoundDecls' value to zero. As
76  // the AST is walked in order, usages are only marked after a the
77  // corresponding using declaration has been found.
78  // FIXME: This currently doesn't look at whether the type reference is
79  // actually found with the help of the using declaration.
80  if (const auto *Used = Result.Nodes.getNodeAs<NamedDecl>("used")) {
81  if (const auto *Specialization =
82  dyn_cast<ClassTemplateSpecializationDecl>(Used))
83  Used = Specialization->getSpecializedTemplate();
84  removeFromFoundDecls(Used);
85  return;
86  }
87 
88  if (const auto *DRE = Result.Nodes.getNodeAs<DeclRefExpr>("used")) {
89  if (const auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) {
90  if (const auto *FDT = FD->getPrimaryTemplate())
91  removeFromFoundDecls(FDT);
92  else
93  removeFromFoundDecls(FD);
94  } else if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
95  removeFromFoundDecls(VD);
96  } else if (const auto *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
97  removeFromFoundDecls(ECD);
98  if (const auto *ET = ECD->getType()->getAs<EnumType>())
99  removeFromFoundDecls(ET->getDecl());
100  }
101  }
102  // Check the uninstantiated template function usage.
103  if (const auto *ULE = Result.Nodes.getNodeAs<UnresolvedLookupExpr>("used")) {
104  for (const NamedDecl* ND : ULE->decls()) {
105  if (const auto *USD = dyn_cast<UsingShadowDecl>(ND))
106  removeFromFoundDecls(USD->getTargetDecl()->getCanonicalDecl());
107  }
108  }
109 }
110 
111 void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) {
112  // FIXME: Currently, we don't handle the using-decls being used in different
113  // scopes (such as different namespaces, different functions). Instead of
114  // giving an incorrect message, we mark all of them as used.
115  //
116  // FIXME: Use a more efficient way to find a matching context.
117  for (auto &Context : Contexts) {
118  if (Context.UsingTargetDecls.count(D->getCanonicalDecl()) > 0)
119  Context.IsUsed = true;
120  }
121 }
122 
123 void UnusedUsingDeclsCheck::onEndOfTranslationUnit() {
124  for (const auto &Context : Contexts) {
125  if (!Context.IsUsed) {
126  diag(Context.FoundUsingDecl->getLocation(), "using decl %0 is unused")
127  << Context.FoundUsingDecl
128  << FixItHint::CreateRemoval(Context.UsingDeclRange);
129  }
130  }
131  Contexts.clear();
132 }
133 
134 } // namespace misc
135 } // namespace tidy
136 } // namespace clang
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:210
static bool ShouldCheckDecl(const Decl *TargetDecl)
ClangTidyContext & Context
Definition: ClangTidy.cpp:93
const NamedDecl * Result
Definition: USRFinder.cpp:137