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;
84 isDefinition(), isUserProvided(),
86 isExpansionInSystemHeader(), isVirtual(), isStatic(),
87 hasTrivialBody(), isOverloadedOperator(), cxxConstructorDecl(),
88 cxxDestructorDecl(), cxxConversionDecl(),
89 isExplicitObjectMemberFunction(), isTemplate(),
93 hasAnyDependentBases())
96 isInsideMacroDefinition(),
97 hasCanonicalDecl(isInsideMacroDefinition()), usesThis())))
104 const LangOptions &LangOpts,
106 if (SourceMgr.getFileID(Range.getBegin()) !=
107 SourceMgr.getFileID(Range.getEnd()))
110 return Lexer::getSourceText(CharSourceRange(Range,
true), SourceMgr,
115 SourceManager &SourceMgr,
116 const LangOptions &LangOpts) {
118 const auto FTL = TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
121 SourceRange Range{FTL.getRParenLoc().getLocWithOffset(1),
122 FTL.getLocalRangeEnd()};
126 size_t Offset = Text.find(
"const");
127 if (Offset == StringRef::npos)
130 SourceLocation Start = Range.getBegin().getLocWithOffset(Offset);
131 return {Start, Start.getLocWithOffset(strlen(
"const") - 1)};
135 const MatchFinder::MatchResult &Result) {
136 const auto *Definition = Result.Nodes.getNodeAs<CXXMethodDecl>(
"x");
140 DiagnosticBuilder Diag =
141 diag(Definition->getLocation(),
"method %0 can be made static")
145 if (Definition->getMethodQualifiers().hasVolatile() ||
146 Definition->getMethodQualifiers().hasRestrict() ||
147 Definition->getRefQualifier() != RQ_None)
150 const CXXMethodDecl *Declaration = Definition->getCanonicalDecl();
152 if (Definition->isConst()) {
156 *Result.SourceManager,
157 Result.Context->getLangOpts());
159 if (DefConst.isInvalid())
162 if (Declaration != Definition) {
164 Declaration->getTypeSourceInfo(), *Result.SourceManager,
165 Result.Context->getLangOpts());
167 if (DeclConst.isInvalid())
169 Diag << FixItHint::CreateRemoval(DeclConst);
173 Diag << FixItHint::CreateRemoval(DefConst);
175 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.