11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
15 using namespace clang::ast_matchers;
20 void UnusedAliasDeclsCheck::registerMatchers(MatchFinder *
Finder) {
23 if (!getLangOpts().CPlusPlus11)
28 Finder->addMatcher(namespaceAliasDecl(isExpansionInMainFile()).bind(
"alias"),
30 Finder->addMatcher(nestedNameSpecifier().bind(
"nns"),
this);
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(),
44 if (
const auto *NestedName =
45 Result.Nodes.getNodeAs<NestedNameSpecifier>(
"nns")) {
46 if (
const auto *AliasDecl = NestedName->getAsNamespaceAlias()) {
47 FoundDecls[AliasDecl] = CharSourceRange();
52 void UnusedAliasDeclsCheck::onEndOfTranslationUnit() {
53 for (
const auto &FoundDecl : FoundDecls) {
54 if (!FoundDecl.second.isValid())
56 diag(FoundDecl.first->getLocation(),
"namespace alias decl '%0' is unused")
57 << FoundDecl.first->getName()
58 << FixItHint::CreateRemoval(FoundDecl.second);
std::unique_ptr< ast_matchers::MatchFinder > Finder