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 clang::Lexer::makeFileCharRange(
46 clang::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) {
69 bool TraverseCXXRecordDecl(CXXRecordDecl *RD) {
return true; }
74 UsageOfThis.TraverseStmt(Node.getBody());
76 return UsageOfThis.Used;
82 MatchFinder *Finder) {
85 isDefinition(), isUserProvided(),
87 isExpansionInSystemHeader(), isVirtual(), isStatic(),
88 hasTrivialBody(), isOverloadedOperator(), cxxConstructorDecl(),
89 cxxDestructorDecl(), cxxConversionDecl(),
90 isExplicitObjectMemberFunction(), isTemplate(),
94 hasAnyDependentBases())
97 isInsideMacroDefinition(),
98 hasCanonicalDecl(isInsideMacroDefinition()), usesThis())))
105 const LangOptions &LangOpts,
107 if (SourceMgr.getFileID(Range.getBegin()) !=
108 SourceMgr.getFileID(Range.getEnd()))
111 return Lexer::getSourceText(CharSourceRange(Range,
true), SourceMgr,
116 SourceManager &SourceMgr,
117 const LangOptions &LangOpts) {
119 const auto FTL = TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
122 const SourceRange Range{FTL.getRParenLoc().getLocWithOffset(1),
123 FTL.getLocalRangeEnd()};
127 const size_t Offset = Text.find(
"const");
128 if (Offset == StringRef::npos)
131 const SourceLocation Start = Range.getBegin().getLocWithOffset(Offset);
132 return {Start, Start.getLocWithOffset(strlen(
"const") - 1)};
136 const MatchFinder::MatchResult &Result) {
137 const auto *Definition = Result.Nodes.getNodeAs<CXXMethodDecl>(
"x");
141 const DiagnosticBuilder Diag =
142 diag(Definition->getLocation(),
"method %0 can be made static")
146 if (Definition->getMethodQualifiers().hasVolatile() ||
147 Definition->getMethodQualifiers().hasRestrict() ||
148 Definition->getRefQualifier() != RQ_None)
151 const CXXMethodDecl *Declaration = Definition->getCanonicalDecl();
153 if (Definition->isConst()) {
157 Definition->getTypeSourceInfo(), *Result.SourceManager,
158 Result.Context->getLangOpts());
160 if (DefConst.isInvalid())
163 if (Declaration != Definition) {
165 Declaration->getTypeSourceInfo(), *Result.SourceManager,
166 Result.Context->getLangOpts());
168 if (DeclConst.isInvalid())
170 Diag << FixItHint::CreateRemoval(DeclConst);
174 Diag << FixItHint::CreateRemoval(DefConst);
176 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.