clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

UnusedAliasDeclsCheck.cpp
Go to the documentation of this file.
1 //===--- UnusedAliasDeclsCheck.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 "UnusedAliasDeclsCheck.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
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->getLocStart(),
37 Lexer::findLocationAfterToken(
38 AliasDecl->getLocEnd(), tok::semi, *Result.SourceManager,
39 Result.Context->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->getName()
58 << FixItHint::CreateRemoval(FoundDecl.second);
59 }
60 }
61
62 } // namespace tidy
63 } // namespace clang
Finder
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
UnusedAliasDeclsCheck.h
Result
const NamedDecl * Result
Definition: USRFinder.cpp:121