10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
20 void UnusedAliasDeclsCheck::registerMatchers(MatchFinder *Finder) {
23 Finder->addMatcher(namespaceAliasDecl(isExpansionInMainFile()).bind(
"alias"),
25 Finder->addMatcher(nestedNameSpecifier().bind(
"nns"),
this);
29 if (
const auto *AliasDecl = Result.Nodes.getNodeAs<NamedDecl>(
"alias")) {
30 FoundDecls[AliasDecl] = CharSourceRange::getCharRange(
31 AliasDecl->getBeginLoc(),
32 Lexer::findLocationAfterToken(
33 AliasDecl->getEndLoc(), tok::semi, *Result.SourceManager,
39 if (
const auto *NestedName =
40 Result.Nodes.getNodeAs<NestedNameSpecifier>(
"nns")) {
41 if (
const auto *AliasDecl = NestedName->getAsNamespaceAlias()) {
42 FoundDecls[AliasDecl] = CharSourceRange();
47 void UnusedAliasDeclsCheck::onEndOfTranslationUnit() {
48 for (
const auto &FoundDecl : FoundDecls) {
49 if (!FoundDecl.second.isValid())
51 diag(FoundDecl.first->getLocation(),
"namespace alias decl %0 is unused")
52 << FoundDecl.first << FixItHint::CreateRemoval(FoundDecl.second);