clang-tools 6.0.0

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 namespace misc {
20
21 void UnusedAliasDeclsCheck::registerMatchers(MatchFinder *Finder) {
22 // Only register the matchers for C++11; the functionality currently does not
23 // provide any benefit to other languages, despite being benign.
24 if (!getLangOpts().CPlusPlus11)
25 return;
26
27 // We cannot do anything about headers (yet), as the alias declarations
28 // used in one header could be used by some other translation unit.
29 Finder->addMatcher(namespaceAliasDecl(isExpansionInMainFile()).bind("alias"),
30 this);
31 Finder->addMatcher(nestedNameSpecifier().bind("nns"), this);
32 }
33
34 void UnusedAliasDeclsCheck::check(const MatchFinder::MatchResult &Result) {
35 if (const auto *AliasDecl = Result.Nodes.getNodeAs<NamedDecl>("alias")) {
36 FoundDecls[AliasDecl] = CharSourceRange::getCharRange(
37 AliasDecl->getLocStart(),
38 Lexer::findLocationAfterToken(
39 AliasDecl->getLocEnd(), tok::semi, *Result.SourceManager,
40 getLangOpts(),
41 /*SkipTrailingWhitespaceAndNewLine=*/true));
42 return;
43 }
44
45 if (const auto *NestedName =
46 Result.Nodes.getNodeAs<NestedNameSpecifier>("nns")) {
47 if (const auto *AliasDecl = NestedName->getAsNamespaceAlias()) {
48 FoundDecls[AliasDecl] = CharSourceRange();
49 }
50 }
51 }
52
53 void UnusedAliasDeclsCheck::onEndOfTranslationUnit() {
54 for (const auto &FoundDecl : FoundDecls) {
55 if (!FoundDecl.second.isValid())
56 continue;
57 diag(FoundDecl.first->getLocation(), "namespace alias decl %0 is unused")
58 << FoundDecl.first << FixItHint::CreateRemoval(FoundDecl.second);
59 }
60 }
61
62 } // namespace misc
63 } // namespace tidy
64 } // namespace clang
clang::ast_matchers
Definition: ClangTidyDiagnosticConsumer.h:27
UnusedAliasDeclsCheck.h
clang
Definition: AndroidTidyModule.cpp:28