2016-04-19 21:48:39 +08:00
|
|
|
//===--- UnusedUsingDeclsCheck.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
|
2016-04-19 21:48:39 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "UnusedUsingDeclsCheck.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 {
|
|
|
|
namespace misc {
|
|
|
|
|
2016-05-30 15:42:22 +08:00
|
|
|
// A function that helps to tell whether a TargetDecl in a UsingDecl will be
|
2016-06-27 22:47:39 +08:00
|
|
|
// checked. Only variable, function, function template, class template, class,
|
|
|
|
// enum declaration and enum constant declaration are considered.
|
2016-05-20 16:34:32 +08:00
|
|
|
static bool ShouldCheckDecl(const Decl *TargetDecl) {
|
|
|
|
return isa<RecordDecl>(TargetDecl) || isa<ClassTemplateDecl>(TargetDecl) ||
|
|
|
|
isa<FunctionDecl>(TargetDecl) || isa<VarDecl>(TargetDecl) ||
|
2016-06-27 22:47:39 +08:00
|
|
|
isa<FunctionTemplateDecl>(TargetDecl) || isa<EnumDecl>(TargetDecl) ||
|
|
|
|
isa<EnumConstantDecl>(TargetDecl);
|
2016-05-20 16:34:32 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 21:48:39 +08:00
|
|
|
void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
Finder->addMatcher(usingDecl(isExpansionInMainFile()).bind("using"), this);
|
2016-04-20 17:48:56 +08:00
|
|
|
auto DeclMatcher = hasDeclaration(namedDecl().bind("used"));
|
2016-07-04 20:01:56 +08:00
|
|
|
Finder->addMatcher(loc(enumType(DeclMatcher)), this);
|
2016-04-20 17:48:56 +08:00
|
|
|
Finder->addMatcher(loc(recordType(DeclMatcher)), this);
|
|
|
|
Finder->addMatcher(loc(templateSpecializationType(DeclMatcher)), this);
|
2016-05-09 21:37:12 +08:00
|
|
|
Finder->addMatcher(declRefExpr().bind("used"), this);
|
2016-05-18 19:49:34 +08:00
|
|
|
Finder->addMatcher(callExpr(callee(unresolvedLookupExpr().bind("used"))),
|
|
|
|
this);
|
2016-08-02 19:26:35 +08:00
|
|
|
Finder->addMatcher(
|
|
|
|
callExpr(hasDeclaration(functionDecl(hasAnyTemplateArgument(
|
|
|
|
anyOf(refersToTemplate(templateName().bind("used")),
|
|
|
|
refersToDeclaration(functionDecl().bind("used"))))))),
|
|
|
|
this);
|
2016-11-08 15:50:19 +08:00
|
|
|
Finder->addMatcher(loc(templateSpecializationType(hasAnyTemplateArgument(
|
|
|
|
templateArgument().bind("used")))),
|
|
|
|
this);
|
2016-04-19 21:48:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
|
2017-02-09 18:41:27 +08:00
|
|
|
if (Result.Context->getDiagnostics().hasUncompilableErrorOccurred())
|
|
|
|
return;
|
|
|
|
|
2016-04-19 21:48:39 +08:00
|
|
|
if (const auto *Using = Result.Nodes.getNodeAs<UsingDecl>("using")) {
|
2016-05-30 15:42:22 +08:00
|
|
|
// Ignores using-declarations defined in macros.
|
|
|
|
if (Using->getLocation().isMacroID())
|
2016-06-03 16:05:11 +08:00
|
|
|
return;
|
2016-05-30 15:42:22 +08:00
|
|
|
|
|
|
|
// Ignores using-declarations defined in class definition.
|
|
|
|
if (isa<CXXRecordDecl>(Using->getDeclContext()))
|
2016-06-03 16:05:11 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// FIXME: We ignore using-decls defined in function definitions at the
|
|
|
|
// moment because of false positives caused by ADL and different function
|
|
|
|
// scopes.
|
|
|
|
if (isa<FunctionDecl>(Using->getDeclContext()))
|
|
|
|
return;
|
2016-05-30 15:42:22 +08:00
|
|
|
|
2016-05-20 16:34:32 +08:00
|
|
|
UsingDeclContext Context(Using);
|
|
|
|
Context.UsingDeclRange = CharSourceRange::getCharRange(
|
2018-08-10 06:42:26 +08:00
|
|
|
Using->getBeginLoc(),
|
2016-04-19 21:48:39 +08:00
|
|
|
Lexer::findLocationAfterToken(
|
2018-08-10 06:43:02 +08:00
|
|
|
Using->getEndLoc(), tok::semi, *Result.SourceManager, getLangOpts(),
|
2016-04-19 21:48:39 +08:00
|
|
|
/*SkipTrailingWhitespaceAndNewLine=*/true));
|
2016-05-20 16:34:32 +08:00
|
|
|
for (const auto *UsingShadow : Using->shadows()) {
|
|
|
|
const auto *TargetDecl = UsingShadow->getTargetDecl()->getCanonicalDecl();
|
|
|
|
if (ShouldCheckDecl(TargetDecl))
|
|
|
|
Context.UsingTargetDecls.insert(TargetDecl);
|
|
|
|
}
|
|
|
|
if (!Context.UsingTargetDecls.empty())
|
|
|
|
Contexts.push_back(Context);
|
2016-04-19 21:48:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Mark using declarations as used by setting FoundDecls' value to zero. As
|
|
|
|
// the AST is walked in order, usages are only marked after a the
|
|
|
|
// corresponding using declaration has been found.
|
|
|
|
// FIXME: This currently doesn't look at whether the type reference is
|
|
|
|
// actually found with the help of the using declaration.
|
|
|
|
if (const auto *Used = Result.Nodes.getNodeAs<NamedDecl>("used")) {
|
2016-08-02 19:26:35 +08:00
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(Used)) {
|
|
|
|
removeFromFoundDecls(FD->getPrimaryTemplate());
|
|
|
|
} else if (const auto *Specialization =
|
|
|
|
dyn_cast<ClassTemplateSpecializationDecl>(Used)) {
|
2016-04-20 17:48:56 +08:00
|
|
|
Used = Specialization->getSpecializedTemplate();
|
2016-08-02 19:26:35 +08:00
|
|
|
}
|
2016-05-09 21:37:12 +08:00
|
|
|
removeFromFoundDecls(Used);
|
|
|
|
return;
|
2016-04-19 21:48:39 +08:00
|
|
|
}
|
2016-05-09 21:37:12 +08:00
|
|
|
|
2016-10-11 21:50:34 +08:00
|
|
|
if (const auto *Used = Result.Nodes.getNodeAs<TemplateArgument>("used")) {
|
|
|
|
// FIXME: Support non-type template parameters.
|
|
|
|
if (Used->getKind() == TemplateArgument::Template) {
|
|
|
|
if (const auto *TD = Used->getAsTemplate().getAsTemplateDecl())
|
|
|
|
removeFromFoundDecls(TD);
|
|
|
|
} else if (Used->getKind() == TemplateArgument::Type) {
|
|
|
|
if (auto *RD = Used->getAsType()->getAsCXXRecordDecl())
|
|
|
|
removeFromFoundDecls(RD);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-02 19:26:35 +08:00
|
|
|
if (const auto *Used = Result.Nodes.getNodeAs<TemplateName>("used")) {
|
|
|
|
removeFromFoundDecls(Used->getAsTemplateDecl());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-09 21:37:12 +08:00
|
|
|
if (const auto *DRE = Result.Nodes.getNodeAs<DeclRefExpr>("used")) {
|
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) {
|
|
|
|
if (const auto *FDT = FD->getPrimaryTemplate())
|
|
|
|
removeFromFoundDecls(FDT);
|
|
|
|
else
|
|
|
|
removeFromFoundDecls(FD);
|
|
|
|
} else if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
|
|
|
|
removeFromFoundDecls(VD);
|
2016-06-27 22:47:39 +08:00
|
|
|
} else if (const auto *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
|
|
|
|
removeFromFoundDecls(ECD);
|
2016-07-04 20:01:56 +08:00
|
|
|
if (const auto *ET = ECD->getType()->getAs<EnumType>())
|
|
|
|
removeFromFoundDecls(ET->getDecl());
|
2016-05-09 21:37:12 +08:00
|
|
|
}
|
|
|
|
}
|
2016-05-18 19:49:34 +08:00
|
|
|
// Check the uninstantiated template function usage.
|
|
|
|
if (const auto *ULE = Result.Nodes.getNodeAs<UnresolvedLookupExpr>("used")) {
|
2016-11-08 15:50:19 +08:00
|
|
|
for (const NamedDecl *ND : ULE->decls()) {
|
2016-05-18 19:49:34 +08:00
|
|
|
if (const auto *USD = dyn_cast<UsingShadowDecl>(ND))
|
|
|
|
removeFromFoundDecls(USD->getTargetDecl()->getCanonicalDecl());
|
|
|
|
}
|
|
|
|
}
|
2016-05-09 21:37:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) {
|
2016-08-02 19:26:35 +08:00
|
|
|
if (!D)
|
|
|
|
return;
|
2016-06-03 16:05:11 +08:00
|
|
|
// FIXME: Currently, we don't handle the using-decls being used in different
|
|
|
|
// scopes (such as different namespaces, different functions). Instead of
|
|
|
|
// giving an incorrect message, we mark all of them as used.
|
|
|
|
//
|
|
|
|
// FIXME: Use a more efficient way to find a matching context.
|
2016-05-20 16:34:32 +08:00
|
|
|
for (auto &Context : Contexts) {
|
2016-06-03 16:05:11 +08:00
|
|
|
if (Context.UsingTargetDecls.count(D->getCanonicalDecl()) > 0)
|
2016-05-20 16:34:32 +08:00
|
|
|
Context.IsUsed = true;
|
|
|
|
}
|
2016-04-19 21:48:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnusedUsingDeclsCheck::onEndOfTranslationUnit() {
|
2016-05-20 16:34:32 +08:00
|
|
|
for (const auto &Context : Contexts) {
|
|
|
|
if (!Context.IsUsed) {
|
|
|
|
diag(Context.FoundUsingDecl->getLocation(), "using decl %0 is unused")
|
[clang-tidy] Add fix descriptions to clang-tidy checks.
Summary:
Motivation/Context: in the code review system integrating with clang-tidy,
clang-tidy doesn't provide a human-readable description of the fix. Usually
developers have to preview a code diff (before vs after apply the fix) to
understand what the fix does before applying a fix.
This patch proposes that each clang-tidy check provides a short and
actional fix description that can be shown in the UI, so that users can know
what the fix does without previewing diff.
This patch extends clang-tidy framework to support fix descriptions (will add implementations for
existing checks in the future). Fix descriptions and fixes are emitted via diagnostic::Note (rather than
attaching the main warning diagnostic).
Before this patch:
```
void MyCheck::check(...) {
...
diag(loc, "my check warning") << FixtItHint::CreateReplacement(...);
}
```
After:
```
void MyCheck::check(...) {
...
diag(loc, "my check warning"); // Emit a check warning
diag(loc, "fix description", DiagnosticIDs::Note) << FixtItHint::CreateReplacement(...); // Emit a diagnostic note and a fix
}
```
Reviewers: sammccall, alexfh
Reviewed By: alexfh
Subscribers: MyDeveloperDay, Eugene.Zelenko, aaron.ballman, JonasToth, xazax.hun, jdoerfert, cfe-commits
Tags: #clang-tools-extra, #clang
Differential Revision: https://reviews.llvm.org/D59932
llvm-svn: 358576
2019-04-17 20:53:59 +08:00
|
|
|
<< Context.FoundUsingDecl;
|
|
|
|
// Emit a fix and a fix description of the check;
|
|
|
|
diag(Context.FoundUsingDecl->getLocation(),
|
|
|
|
/*FixDescription=*/"remove the using", DiagnosticIDs::Note)
|
2016-05-20 16:34:32 +08:00
|
|
|
<< FixItHint::CreateRemoval(Context.UsingDeclRange);
|
|
|
|
}
|
2016-04-19 21:48:39 +08:00
|
|
|
}
|
2016-05-20 16:34:32 +08:00
|
|
|
Contexts.clear();
|
2016-04-19 21:48:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace misc
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|