2015-08-01 00:08:10 +08:00
|
|
|
//===--- UnusedAliasDeclsCheck.cpp - clang-tidy----------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-08-01 00:08:10 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "UnusedAliasDeclsCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2016-05-03 02:00:29 +08:00
|
|
|
namespace misc {
|
2015-08-01 00:08:10 +08:00
|
|
|
|
|
|
|
void UnusedAliasDeclsCheck::registerMatchers(MatchFinder *Finder) {
|
2015-08-31 23:28:57 +08:00
|
|
|
// We cannot do anything about headers (yet), as the alias declarations
|
|
|
|
// used in one header could be used by some other translation unit.
|
|
|
|
Finder->addMatcher(namespaceAliasDecl(isExpansionInMainFile()).bind("alias"),
|
|
|
|
this);
|
|
|
|
Finder->addMatcher(nestedNameSpecifier().bind("nns"), this);
|
2015-08-01 00:08:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnusedAliasDeclsCheck::check(const MatchFinder::MatchResult &Result) {
|
2015-08-04 06:02:08 +08:00
|
|
|
if (const auto *AliasDecl = Result.Nodes.getNodeAs<NamedDecl>("alias")) {
|
2015-08-01 00:08:10 +08:00
|
|
|
FoundDecls[AliasDecl] = CharSourceRange::getCharRange(
|
2018-08-10 06:42:26 +08:00
|
|
|
AliasDecl->getBeginLoc(),
|
2015-08-01 00:08:10 +08:00
|
|
|
Lexer::findLocationAfterToken(
|
2018-08-10 06:43:02 +08:00
|
|
|
AliasDecl->getEndLoc(), tok::semi, *Result.SourceManager,
|
2016-09-24 10:13:45 +08:00
|
|
|
getLangOpts(),
|
2015-08-01 00:08:10 +08:00
|
|
|
/*SkipTrailingWhitespaceAndNewLine=*/true));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto *NestedName =
|
|
|
|
Result.Nodes.getNodeAs<NestedNameSpecifier>("nns")) {
|
|
|
|
if (const auto *AliasDecl = NestedName->getAsNamespaceAlias()) {
|
|
|
|
FoundDecls[AliasDecl] = CharSourceRange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnusedAliasDeclsCheck::onEndOfTranslationUnit() {
|
|
|
|
for (const auto &FoundDecl : FoundDecls) {
|
|
|
|
if (!FoundDecl.second.isValid())
|
|
|
|
continue;
|
2016-04-07 22:55:25 +08:00
|
|
|
diag(FoundDecl.first->getLocation(), "namespace alias decl %0 is unused")
|
|
|
|
<< FoundDecl.first << FixItHint::CreateRemoval(FoundDecl.second);
|
2015-08-01 00:08:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:00:29 +08:00
|
|
|
} // namespace misc
|
2015-08-01 00:08:10 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|