11 #include "clang/AST/ASTContext.h" 12 #include "clang/AST/RecursiveASTVisitor.h" 13 #include "clang/ASTMatchers/ASTMatchFinder.h" 14 #include "clang/Lex/Lexer.h" 15 #include "llvm/ADT/STLExtras.h" 16 #include <unordered_set> 26 if (
const auto *
MD = dyn_cast<CXXMethodDecl>(Function))
27 return MD->size_overridden_methods() > 0 ||
MD->hasAttr<OverrideAttr>();
32 void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) {
34 functionDecl(isDefinition(), hasBody(stmt()), hasAnyParameter(decl()))
41 const T *PrevNode,
const T *Node,
44 return CharSourceRange::getCharRange(Node->getBeginLoc(),
45 NextNode->getBeginLoc());
48 return CharSourceRange::getTokenRange(
49 Lexer::getLocForEndOfToken(PrevNode->getEndLoc(), 0,
50 *Result.SourceManager,
51 Result.Context->getLangOpts()),
54 return CharSourceRange::getTokenRange(Node->getSourceRange());
58 const FunctionDecl *Function,
unsigned Index) {
60 Result, Index > 0 ? Function->getParamDecl(Index - 1) :
nullptr,
61 Function->getParamDecl(Index),
62 Index + 1 < Function->getNumParams() ? Function->getParamDecl(Index + 1)
67 const CallExpr *Call,
unsigned Index) {
69 Result, Index > 0 ? Call->getArg(Index - 1) :
nullptr,
71 Index + 1 < Call->getNumArgs() ? Call->getArg(Index + 1) :
nullptr));
75 :
public RecursiveASTVisitor<IndexerVisitor> {
79 const std::unordered_set<const CallExpr *> &
81 return Index[Fn->getCanonicalDecl()].Calls;
84 const std::unordered_set<const DeclRefExpr *> &
86 return Index[Fn->getCanonicalDecl()].OtherRefs;
92 if (
const auto *Fn = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
93 Fn = Fn->getCanonicalDecl();
94 Index[Fn].OtherRefs.insert(DeclRef);
101 dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl())) {
102 Fn = Fn->getCanonicalDecl();
103 if (
const auto *Ref =
104 dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) {
105 Index[Fn].OtherRefs.erase(Ref);
107 Index[Fn].Calls.insert(Call);
114 std::unordered_set<const CallExpr *> Calls;
115 std::unordered_set<const DeclRefExpr *> OtherRefs;
118 std::unordered_map<const FunctionDecl *, IndexEntry>
Index;
121 UnusedParametersCheck::~UnusedParametersCheck() =
default;
123 UnusedParametersCheck::UnusedParametersCheck(StringRef
Name,
126 StrictMode(Options.getLocalOrGlobal(
"StrictMode", 0) != 0) {}
132 void UnusedParametersCheck::warnOnUnusedParameter(
133 const MatchFinder::MatchResult &
Result,
const FunctionDecl *Function,
134 unsigned ParamIndex) {
135 const auto *Param = Function->getParamDecl(ParamIndex);
136 auto MyDiag =
diag(Param->getLocation(),
"parameter %0 is unused") << Param;
139 Indexer = llvm::make_unique<IndexerVisitor>(*Result.Context);
143 if (Function->isExternallyVisible() ||
144 !Result.SourceManager->isInMainFile(Function->getLocation()) ||
146 SourceRange RemovalRange(Param->getLocation());
150 MyDiag << FixItHint::CreateReplacement(
151 RemovalRange, (Twine(
" /*") + Param->getName() +
"*/").str());
156 for (
const FunctionDecl *FD : Function->redecls())
157 if (FD->param_size())
161 for (
const CallExpr *Call : Indexer->getFnCalls(Function))
162 if (ParamIndex < Call->getNumArgs())
167 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>(
"function");
168 if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
170 if (
const auto *Method = dyn_cast<CXXMethodDecl>(Function))
171 if (Method->isLambdaStaticInvoker())
173 for (
unsigned i = 0, e = Function->getNumParams(); i != e; ++i) {
174 const auto *Param = Function->getParamDecl(i);
175 if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
176 Param->hasAttr<UnusedAttr>())
182 (Function->getBody()->child_begin() !=
183 Function->getBody()->child_end()) ||
184 (isa<CXXConstructorDecl>(Function) &&
185 cast<CXXConstructorDecl>(Function)->getNumCtorInitializers() > 0))
186 warnOnUnusedParameter(Result, Function, i);
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 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)
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
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.
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