11#include "clang/AST/ASTContext.h"
12#include "clang/AST/DeclCXX.h"
13#include "clang/AST/RecursiveASTVisitor.h"
14#include "clang/ASTMatchers/ASTMatchFinder.h"
15#include "clang/Basic/SourceLocation.h"
16#include "clang/Lex/Lexer.h"
25AST_MATCHER(CXXMethodDecl, hasTrivialBody) {
return Node.hasTrivialBody(); }
28 return Node.hasAnyDependentBases();
32 return Node.getTemplatedKind() != FunctionDecl::TK_NonTemplate;
36 return Node.isDependentContext();
39AST_MATCHER(CXXMethodDecl, isInsideMacroDefinition) {
40 const ASTContext &Ctxt = Finder->getASTContext();
41 return Lexer::makeFileCharRange(
42 CharSourceRange::getCharRange(
43 Node.getTypeSourceInfo()->getTypeLoc().getSourceRange()),
44 Ctxt.getSourceManager(), Ctxt.getLangOpts())
49 ast_matchers::internal::Matcher<CXXMethodDecl>, InnerMatcher) {
50 return InnerMatcher.matches(*Node.getCanonicalDecl(), Finder, Builder);
54 class FindUsageOfThis :
public RecursiveASTVisitor<FindUsageOfThis> {
58 bool VisitCXXThisExpr(
const CXXThisExpr *E) {
63 bool VisitUnresolvedMemberExpr(
const UnresolvedMemberExpr *E) {
64 if (E->isImplicitAccess()) {
73 bool TraverseCXXRecordDecl(CXXRecordDecl *RD) {
return true; }
78 UsageOfThis.TraverseStmt(Node.getBody());
80 return UsageOfThis.Used;
84 const auto *
Method = &Node;
85 const DeclContext::lookup_result LookupResult =
86 Method->getParent()->lookup(
Method->getNameInfo().getName());
87 if (LookupResult.isSingleResult())
90 const auto HasSameParameterTypes = [](
const CXXMethodDecl &MD1,
91 const CXXMethodDecl &MD2) {
92 if (MD1.getNumParams() != MD2.getNumParams())
94 for (
unsigned I = 0, E = MD1.getNumParams(); I < E; ++I)
95 if (MD1.getParamDecl(I)->getType().getCanonicalType() !=
96 MD2.getParamDecl(I)->getType().getCanonicalType())
102 LookupResult, [Method, HasSameParameterTypes](
const Decl *D) {
103 const auto *Overload = dyn_cast<CXXMethodDecl>(D);
104 return Overload && Overload !=
Method && !Overload->isConst() &&
105 HasSameParameterTypes(*Method, *Overload);
111 MatchFinder *Finder) {
114 isDefinition(), isUserProvided(),
116 isVirtual(), isStatic(), hasTrivialBody(), isOverloadedOperator(),
117 cxxConstructorDecl(), cxxDestructorDecl(), cxxConversionDecl(),
118 isExplicitObjectMemberFunction(), isTemplate(),
119 isDependentContext(), allOf(isConst(), hasNonConstOverload()),
122 hasAnyDependentBases())
125 isInsideMacroDefinition(),
126 hasCanonicalDecl(isInsideMacroDefinition()), usesThis())))
133 const LangOptions &LangOpts,
135 if (SourceMgr.getFileID(Range.getBegin()) !=
136 SourceMgr.getFileID(Range.getEnd()))
139 return Lexer::getSourceText(CharSourceRange(Range,
true), SourceMgr,
144 const SourceManager &SourceMgr,
145 const LangOptions &LangOpts) {
147 const auto FTL = TSI->getTypeLoc().getAsAdjusted<FunctionTypeLoc>();
150 const SourceRange Range{FTL.getRParenLoc().getLocWithOffset(1),
151 FTL.getLocalRangeEnd()};
155 const size_t Offset = Text.find(
"const");
156 if (Offset == StringRef::npos)
159 const SourceLocation Start = Range.getBegin().getLocWithOffset(Offset);
160 return {Start, Start.getLocWithOffset(strlen(
"const") - 1)};
164 const MatchFinder::MatchResult &Result) {
165 const auto *Definition = Result.Nodes.getNodeAs<CXXMethodDecl>(
"x");
169 const DiagnosticBuilder Diag =
170 diag(Definition->getLocation(),
"method %0 can be made static")
174 if (Definition->getMethodQualifiers().hasVolatile() ||
175 Definition->getMethodQualifiers().hasRestrict() ||
176 Definition->getRefQualifier() != RQ_None)
179 const CXXMethodDecl *Declaration = Definition->getCanonicalDecl();
181 if (Definition->isConst()) {
185 Definition->getTypeSourceInfo(), *Result.SourceManager,
186 Result.Context->getLangOpts());
188 if (DefConst.isInvalid())
191 if (Declaration != Definition) {
193 Declaration->getTypeSourceInfo(), *Result.SourceManager,
194 Result.Context->getLangOpts());
196 if (DeclConst.isInvalid())
198 Diag << FixItHint::CreateRemoval(DeclConst);
202 Diag << FixItHint::CreateRemoval(DefConst);
204 Diag << FixItHint::CreateInsertion(Declaration->getBeginLoc(),
"static ");
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
AST_MATCHER_P(Stmt, isStatementIdenticalToBoundNode, std::string, ID)
AST_MATCHER(BinaryOperator, isRelationalOperator)
static SourceRange getLocationOfConst(const TypeSourceInfo *TSI, const SourceManager &SourceMgr, const LangOptions &LangOpts)
static StringRef getStringFromRange(const SourceManager &SourceMgr, const LangOptions &LangOpts, SourceRange Range)
Obtain the original source code text from a SourceRange.