10#include "clang/AST/ASTContext.h"
11#include "clang/AST/Decl.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Lex/Lexer.h"
21AST_MATCHER_P(DeducedTemplateSpecializationType, refsToTemplatedDecl,
22 clang::ast_matchers::internal::Matcher<NamedDecl>, DeclMatcher) {
23 if (
const auto *TD =
Node.getTemplateName().getAsTemplateDecl())
24 return DeclMatcher.matches(*TD, Finder,
Builder);
34 return isa<RecordDecl>(TargetDecl) || isa<ClassTemplateDecl>(TargetDecl) ||
35 isa<FunctionDecl>(TargetDecl) || isa<VarDecl>(TargetDecl) ||
36 isa<FunctionTemplateDecl>(TargetDecl) || isa<EnumDecl>(TargetDecl) ||
37 isa<EnumConstantDecl>(TargetDecl);
43 HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
46 Finder->addMatcher(usingDecl(isExpansionInMainFile()).bind(
"using"),
this);
47 auto DeclMatcher = hasDeclaration(namedDecl().bind(
"used"));
48 Finder->addMatcher(loc(templateSpecializationType(DeclMatcher)),
this);
49 Finder->addMatcher(loc(deducedTemplateSpecializationType(
50 refsToTemplatedDecl(namedDecl().bind(
"used")))),
52 Finder->addMatcher(callExpr(callee(unresolvedLookupExpr().bind(
"used"))),
55 callExpr(hasDeclaration(functionDecl(
56 forEachTemplateArgument(templateArgument().bind(
"used"))))),
58 Finder->addMatcher(loc(templateSpecializationType(forEachTemplateArgument(
59 templateArgument().bind(
"used")))),
61 Finder->addMatcher(userDefinedLiteral().bind(
"used"),
this);
63 loc(elaboratedType(unless(hasQualifier(nestedNameSpecifier())),
64 hasUnqualifiedDesugaredType(type().bind(
"usedType")))),
69 auto ThroughShadowMatcher = throughUsingDecl(namedDecl().bind(
"usedShadow"));
70 Finder->addMatcher(declRefExpr(ThroughShadowMatcher),
this);
71 Finder->addMatcher(loc(usingType(ThroughShadowMatcher)),
this);
75 if (Result.Context->getDiagnostics().hasUncompilableErrorOccurred())
79 if (
auto MainFile = Result.SourceManager->getFileEntryRefForID(
80 Result.SourceManager->getMainFileID());
84 if (
const auto *Using = Result.Nodes.getNodeAs<UsingDecl>(
"using")) {
86 if (Using->getLocation().isMacroID())
90 if (isa<CXXRecordDecl>(Using->getDeclContext()))
96 if (isa<FunctionDecl>(Using->getDeclContext()))
99 UsingDeclContext Context(Using);
100 Context.UsingDeclRange = CharSourceRange::getCharRange(
101 Using->getBeginLoc(),
102 Lexer::findLocationAfterToken(
103 Using->getEndLoc(), tok::semi, *Result.SourceManager,
getLangOpts(),
105 for (
const auto *UsingShadow : Using->shadows()) {
106 const auto *TargetDecl = UsingShadow->getTargetDecl()->getCanonicalDecl();
108 Context.UsingTargetDecls.insert(TargetDecl);
109 UsingTargetDeclsCache.insert(TargetDecl);
112 if (!Context.UsingTargetDecls.empty())
113 Contexts.push_back(Context);
118 auto RemoveNamedDecl = [&](
const NamedDecl *Used) {
119 removeFromFoundDecls(Used);
121 if (
const auto *FD = dyn_cast<FunctionDecl>(Used)) {
122 removeFromFoundDecls(FD->getPrimaryTemplate());
125 if (
const auto *Specialization =
126 dyn_cast<ClassTemplateSpecializationDecl>(Used)) {
127 removeFromFoundDecls(Specialization->getSpecializedTemplate());
130 if (
const auto *ECD = dyn_cast<EnumConstantDecl>(Used)) {
131 if (
const auto *ET = ECD->getType()->getAs<EnumType>())
132 removeFromFoundDecls(ET->getDecl());
137 if (
const auto *Used = Result.Nodes.getNodeAs<NamedDecl>(
"used")) {
138 RemoveNamedDecl(Used);
142 if (
const auto *T = Result.Nodes.getNodeAs<
Type>(
"usedType")) {
143 if (
const auto *ND = T->getAsTagDecl())
148 if (
const auto *UsedShadow =
149 Result.Nodes.getNodeAs<UsingShadowDecl>(
"usedShadow")) {
150 removeFromFoundDecls(UsedShadow->getTargetDecl());
154 if (
const auto *Used = Result.Nodes.getNodeAs<TemplateArgument>(
"used")) {
155 if (Used->getKind() == TemplateArgument::Template) {
156 if (
const auto *TD = Used->getAsTemplate().getAsTemplateDecl())
157 removeFromFoundDecls(TD);
161 if (Used->getKind() == TemplateArgument::Type) {
162 if (
auto *RD = Used->getAsType()->getAsCXXRecordDecl())
163 removeFromFoundDecls(RD);
167 if (Used->getKind() == TemplateArgument::Declaration) {
168 RemoveNamedDecl(Used->getAsDecl());
173 if (
const auto *DRE = Result.Nodes.getNodeAs<DeclRefExpr>(
"used")) {
174 RemoveNamedDecl(DRE->getDecl());
178 if (
const auto *ULE = Result.Nodes.getNodeAs<UnresolvedLookupExpr>(
"used")) {
179 for (
const NamedDecl *ND : ULE->decls()) {
180 if (
const auto *USD = dyn_cast<UsingShadowDecl>(ND))
181 removeFromFoundDecls(USD->getTargetDecl()->getCanonicalDecl());
186 if (
const auto *UDL = Result.Nodes.getNodeAs<UserDefinedLiteral>(
"used"))
187 removeFromFoundDecls(UDL->getCalleeDecl());
190void UnusedUsingDeclsCheck::removeFromFoundDecls(
const Decl *D) {
193 const Decl *CanonicalDecl = D->getCanonicalDecl();
194 if (!UsingTargetDeclsCache.contains(CanonicalDecl))
199 for (
auto &Context : Contexts) {
202 if (Context.UsingTargetDecls.contains(CanonicalDecl))
203 Context.IsUsed =
true;
208 for (
const auto &Context : Contexts) {
209 if (!Context.IsUsed) {
210 diag(Context.FoundUsingDecl->getLocation(),
"using decl %0 is unused")
211 << Context.FoundUsingDecl;
213 diag(Context.FoundUsingDecl->getLocation(),
214 "remove the using", DiagnosticIDs::Note)
215 << FixItHint::CreateRemoval(Context.UsingDeclRange);
219 UsingTargetDeclsCache.clear();
const FunctionDecl * Decl
llvm::SmallString< 256U > Name
CodeCompletionBuilder Builder
::clang::DynTypedNode Node
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
UnusedUsingDeclsCheck(StringRef Name, ClangTidyContext *Context)
void onEndOfTranslationUnit() override
AST_MATCHER_P(UserDefinedLiteral, hasLiteral, clang::ast_matchers::internal::Matcher< Expr >, InnerMatcher)
static bool shouldCheckDecl(const Decl *TargetDecl)
bool isFileExtension(StringRef FileName, const FileExtensionsSet &FileExtensions)
Decides whether a file has one of the specified file extensions.