10#include "clang/AST/ASTContext.h"
11#include "clang/AST/DeclCXX.h"
12#include "clang/AST/RecursiveASTVisitor.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Basic/SourceLocation.h"
15#include "clang/Lex/Lexer.h"
23AST_MATCHER(CXXMethodDecl, isStatic) {
return Node.isStatic(); }
25AST_MATCHER(CXXMethodDecl, hasTrivialBody) {
return Node.hasTrivialBody(); }
28 return Node.isOverloadedOperator();
32 return Node.hasAnyDependentBases();
36 return Node.getTemplatedKind() != FunctionDecl::TK_NonTemplate;
40 return Node.isDependentContext();
43AST_MATCHER(CXXMethodDecl, isInsideMacroDefinition) {
44 const ASTContext &Ctxt = Finder->getASTContext();
45 return Lexer::makeFileCharRange(
46 CharSourceRange::getCharRange(
47 Node.getTypeSourceInfo()->getTypeLoc().getSourceRange()),
48 Ctxt.getSourceManager(), Ctxt.getLangOpts())
53 ast_matchers::internal::Matcher<CXXMethodDecl>, InnerMatcher) {
54 return InnerMatcher.matches(*Node.getCanonicalDecl(), Finder, Builder);
58 class FindUsageOfThis :
public RecursiveASTVisitor<FindUsageOfThis> {
62 bool VisitCXXThisExpr(
const CXXThisExpr *E) {
67 bool VisitUnresolvedMemberExpr(
const UnresolvedMemberExpr *E) {
68 if (E->isImplicitAccess()) {
77 bool TraverseCXXRecordDecl(CXXRecordDecl *RD) {
return true; }
82 UsageOfThis.TraverseStmt(Node.getBody());
84 return UsageOfThis.Used;
88 const auto *
Method = &Node;
89 const DeclContext::lookup_result LookupResult =
90 Method->getParent()->lookup(
Method->getNameInfo().getName());
91 if (LookupResult.isSingleResult())
94 auto HasSameParameterTypes = [](
const CXXMethodDecl &MD1,
95 const CXXMethodDecl &MD2) {
96 if (MD1.getNumParams() != MD2.getNumParams())
98 for (
unsigned I = 0, E = MD1.getNumParams(); I < E; ++I)
99 if (MD1.getParamDecl(I)->getType().getCanonicalType() !=
100 MD2.getParamDecl(I)->getType().getCanonicalType())
106 LookupResult, [Method, HasSameParameterTypes](
const Decl *D) {
107 const auto *Overload = dyn_cast<CXXMethodDecl>(D);
108 return Overload && Overload !=
Method && !Overload->isConst() &&
109 HasSameParameterTypes(*Method, *Overload);
115 MatchFinder *Finder) {
118 isDefinition(), isUserProvided(),
120 isVirtual(), isStatic(), hasTrivialBody(), isOverloadedOperator(),
121 cxxConstructorDecl(), cxxDestructorDecl(), cxxConversionDecl(),
122 isExplicitObjectMemberFunction(), isTemplate(),
123 isDependentContext(), allOf(isConst(), hasNonConstOverload()),
126 hasAnyDependentBases())
129 isInsideMacroDefinition(),
130 hasCanonicalDecl(isInsideMacroDefinition()), usesThis())))
137 const LangOptions &LangOpts,
139 if (SourceMgr.getFileID(Range.getBegin()) !=
140 SourceMgr.getFileID(Range.getEnd()))
143 return Lexer::getSourceText(CharSourceRange(Range,
true), SourceMgr,
148 SourceManager &SourceMgr,
149 const LangOptions &LangOpts) {
151 const auto FTL = TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
154 const SourceRange Range{FTL.getRParenLoc().getLocWithOffset(1),
155 FTL.getLocalRangeEnd()};
159 const size_t Offset = Text.find(
"const");
160 if (Offset == StringRef::npos)
163 const SourceLocation Start = Range.getBegin().getLocWithOffset(Offset);
164 return {Start, Start.getLocWithOffset(strlen(
"const") - 1)};
168 const MatchFinder::MatchResult &Result) {
169 const auto *Definition = Result.Nodes.getNodeAs<CXXMethodDecl>(
"x");
173 const DiagnosticBuilder Diag =
174 diag(Definition->getLocation(),
"method %0 can be made static")
178 if (Definition->getMethodQualifiers().hasVolatile() ||
179 Definition->getMethodQualifiers().hasRestrict() ||
180 Definition->getRefQualifier() != RQ_None)
183 const CXXMethodDecl *Declaration = Definition->getCanonicalDecl();
185 if (Definition->isConst()) {
189 Definition->getTypeSourceInfo(), *Result.SourceManager,
190 Result.Context->getLangOpts());
192 if (DefConst.isInvalid())
195 if (Declaration != Definition) {
197 Declaration->getTypeSourceInfo(), *Result.SourceManager,
198 Result.Context->getLangOpts());
200 if (DeclConst.isInvalid())
202 Diag << FixItHint::CreateRemoval(DeclConst);
206 Diag << FixItHint::CreateRemoval(DefConst);
208 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, SourceManager &SourceMgr, const LangOptions &LangOpts)
static StringRef getStringFromRange(SourceManager &SourceMgr, const LangOptions &LangOpts, SourceRange Range)
Obtain the original source code text from a SourceRange.