clang-tools  10.0.0svn
UnusedAliasDeclsCheck.cpp
Go to the documentation of this file.
1 //===--- UnusedAliasDeclsCheck.cpp - clang-tidy----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace misc {
19 
20 void UnusedAliasDeclsCheck::registerMatchers(MatchFinder *Finder) {
21  // Only register the matchers for C++11; the functionality currently does not
22  // provide any benefit to other languages, despite being benign.
23  if (!getLangOpts().CPlusPlus11)
24  return;
25 
26  // We cannot do anything about headers (yet), as the alias declarations
27  // used in one header could be used by some other translation unit.
28  Finder->addMatcher(namespaceAliasDecl(isExpansionInMainFile()).bind("alias"),
29  this);
30  Finder->addMatcher(nestedNameSpecifier().bind("nns"), this);
31 }
32 
33 void UnusedAliasDeclsCheck::check(const MatchFinder::MatchResult &Result) {
34  if (const auto *AliasDecl = Result.Nodes.getNodeAs<NamedDecl>("alias")) {
35  FoundDecls[AliasDecl] = CharSourceRange::getCharRange(
36  AliasDecl->getBeginLoc(),
37  Lexer::findLocationAfterToken(
38  AliasDecl->getEndLoc(), tok::semi, *Result.SourceManager,
39  getLangOpts(),
40  /*SkipTrailingWhitespaceAndNewLine=*/true));
41  return;
42  }
43 
44  if (const auto *NestedName =
45  Result.Nodes.getNodeAs<NestedNameSpecifier>("nns")) {
46  if (const auto *AliasDecl = NestedName->getAsNamespaceAlias()) {
47  FoundDecls[AliasDecl] = CharSourceRange();
48  }
49  }
50 }
51 
52 void UnusedAliasDeclsCheck::onEndOfTranslationUnit() {
53  for (const auto &FoundDecl : FoundDecls) {
54  if (!FoundDecl.second.isValid())
55  continue;
56  diag(FoundDecl.first->getLocation(), "namespace alias decl %0 is unused")
57  << FoundDecl.first << FixItHint::CreateRemoval(FoundDecl.second);
58  }
59 }
60 
61 } // namespace misc
62 } // namespace tidy
63 } // namespace clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//