10 #include "clang/AST/ASTContext.h" 11 #include "clang/AST/RecursiveASTVisitor.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/Lex/Lexer.h" 14 #include "llvm/ADT/STLExtras.h" 15 #include <unordered_set> 25 if (
const auto *
MD = dyn_cast<CXXMethodDecl>(Function))
26 return MD->size_overridden_methods() > 0 ||
MD->hasAttr<OverrideAttr>();
31 void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) {
33 functionDecl(isDefinition(), hasBody(stmt()), hasAnyParameter(decl()))
40 const T *PrevNode,
const T *Node,
43 return CharSourceRange::getCharRange(Node->getBeginLoc(),
44 NextNode->getBeginLoc());
48 Lexer::getLocForEndOfToken(PrevNode->getEndLoc(), 0,
49 *Result.SourceManager,
50 Result.Context->getLangOpts()),
57 const FunctionDecl *Function,
unsigned Index) {
59 Result, Index > 0 ? Function->getParamDecl(Index - 1) :
nullptr,
60 Function->getParamDecl(Index),
61 Index + 1 < Function->getNumParams() ? Function->getParamDecl(Index + 1)
66 const CallExpr *Call,
unsigned Index) {
68 Result, Index > 0 ? Call->getArg(Index - 1) :
nullptr,
70 Index + 1 < Call->getNumArgs() ? Call->getArg(Index + 1) :
nullptr));
74 :
public RecursiveASTVisitor<IndexerVisitor> {
78 const std::unordered_set<const CallExpr *> &
80 return Index[Fn->getCanonicalDecl()].Calls;
83 const std::unordered_set<const DeclRefExpr *> &
85 return Index[Fn->getCanonicalDecl()].OtherRefs;
91 if (
const auto *Fn = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
92 Fn = Fn->getCanonicalDecl();
93 Index[Fn].OtherRefs.insert(DeclRef);
100 dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl())) {
101 Fn = Fn->getCanonicalDecl();
102 if (
const auto *Ref =
103 dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) {
104 Index[Fn].OtherRefs.erase(Ref);
106 Index[Fn].Calls.insert(Call);
113 std::unordered_set<const CallExpr *> Calls;
114 std::unordered_set<const DeclRefExpr *> OtherRefs;
117 std::unordered_map<const FunctionDecl *, IndexEntry>
Index;
120 UnusedParametersCheck::~UnusedParametersCheck() =
default;
122 UnusedParametersCheck::UnusedParametersCheck(StringRef
Name,
125 StrictMode(Options.getLocalOrGlobal(
"StrictMode", 0) != 0) {}
131 void UnusedParametersCheck::warnOnUnusedParameter(
132 const MatchFinder::MatchResult &
Result,
const FunctionDecl *Function,
133 unsigned ParamIndex) {
134 const auto *Param = Function->getParamDecl(ParamIndex);
135 auto MyDiag =
diag(Param->getLocation(),
"parameter %0 is unused") << Param;
138 Indexer = llvm::make_unique<IndexerVisitor>(*Result.Context);
142 if (Function->isExternallyVisible() ||
143 !Result.SourceManager->isInMainFile(Function->getLocation()) ||
147 if (!Result.Context->getLangOpts().CPlusPlus)
150 SourceRange RemovalRange(Param->getLocation());
154 MyDiag << FixItHint::CreateReplacement(
155 RemovalRange, (Twine(
" /*") + Param->getName() +
"*/").str());
160 for (
const FunctionDecl *FD : Function->redecls())
161 if (FD->param_size())
165 for (
const CallExpr *Call : Indexer->getFnCalls(Function))
166 if (ParamIndex < Call->getNumArgs())
171 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>(
"function");
172 if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
174 if (
const auto *Method = dyn_cast<CXXMethodDecl>(Function))
175 if (Method->isLambdaStaticInvoker())
177 for (
unsigned i = 0, e = Function->getNumParams(); i != e; ++i) {
178 const auto *Param = Function->getParamDecl(i);
179 if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
180 Param->hasAttr<UnusedAttr>())
186 (Function->getBody()->child_begin() !=
187 Function->getBody()->child_end()) ||
188 (isa<CXXConstructorDecl>(Function) &&
189 cast<CXXConstructorDecl>(Function)->getNumCtorInitializers() > 0))
190 warnOnUnusedParameter(Result, Function, i);
static bool isOverrideMethod(const CXXMethodDecl *MD)
Finds out if the given method overrides some method.
IndexerVisitor(ASTContext &Ctx)
static FixItHint removeArgument(const MatchFinder::MatchResult &Result, const CallExpr *Call, unsigned Index)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
bool shouldTraversePostOrder() const
Base class for all clang-tidy checks.
const std::unordered_set< const CallExpr * > & getFnCalls(const FunctionDecl *Fn)
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
bool WalkUpFromDeclRefExpr(DeclRefExpr *DeclRef)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static GeneratorRegistry::Add< MDGenerator > MD(MDGenerator::Format, "Generator for MD output.")
const std::unordered_set< const DeclRefExpr * > & getOtherRefs(const FunctionDecl *Fn)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
static CharSourceRange removeNode(const MatchFinder::MatchResult &Result, const T *PrevNode, const T *Node, const T *NextNode)
bool WalkUpFromCallExpr(CallExpr *Call)
const DeclRefExpr * DeclRef
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
static FixItHint removeParameter(const MatchFinder::MatchResult &Result, const FunctionDecl *Function, unsigned Index)
const SymbolIndex * Index