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"
21AST_MATCHER(CXXMethodDecl, isStatic) {
return Node.isStatic(); }
23AST_MATCHER(CXXMethodDecl, hasTrivialBody) {
return Node.hasTrivialBody(); }
26 return Node.isOverloadedOperator();
30 return Node.hasAnyDependentBases();
34 return Node.getTemplatedKind() != FunctionDecl::TK_NonTemplate;
38 return Node.isDependentContext();
42 const ASTContext &Ctxt = Finder->getASTContext();
43 return clang::Lexer::makeFileCharRange(
44 clang::CharSourceRange::getCharRange(
45 Node.getTypeSourceInfo()->getTypeLoc().getSourceRange()),
46 Ctxt.getSourceManager(), Ctxt.getLangOpts())
51 ast_matchers::internal::Matcher<CXXMethodDecl>, InnerMatcher) {
52 return InnerMatcher.matches(*Node.getCanonicalDecl(), Finder,
Builder);
67 UsageOfThis.TraverseStmt(
const_cast<Stmt *
>(Node.getBody()));
69 return UsageOfThis.Used;
75 isDefinition(), isUserProvided(),
77 isExpansionInSystemHeader(), isVirtual(), isStatic(),
78 hasTrivialBody(), isOverloadedOperator(), cxxConstructorDecl(),
79 cxxDestructorDecl(), cxxConversionDecl(), isTemplate(),
83 hasAnyDependentBases())
86 isInsideMacroDefinition(),
87 hasCanonicalDecl(isInsideMacroDefinition()), usesThis())))
94 const LangOptions &LangOpts,
96 if (SourceMgr.getFileID(
Range.getBegin()) !=
97 SourceMgr.getFileID(
Range.getEnd()))
100 return Lexer::getSourceText(CharSourceRange(
Range,
true), SourceMgr,
105 SourceManager &SourceMgr,
106 const LangOptions &LangOpts) {
108 const auto FTL = TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
111 SourceRange
Range{FTL.getRParenLoc().getLocWithOffset(1),
112 FTL.getLocalRangeEnd()};
117 if (
Offset == StringRef::npos)
120 SourceLocation Start =
Range.getBegin().getLocWithOffset(
Offset);
121 return {Start, Start.getLocWithOffset(strlen(
"const") - 1)};
125 const MatchFinder::MatchResult &Result) {
126 const auto *Definition = Result.Nodes.getNodeAs<CXXMethodDecl>(
"x");
130 DiagnosticBuilder Diag =
131 diag(Definition->getLocation(),
"method %0 can be made static")
135 if (Definition->getMethodQualifiers().hasVolatile() ||
136 Definition->getMethodQualifiers().hasRestrict() ||
137 Definition->getRefQualifier() != RQ_None)
140 const CXXMethodDecl *Declaration = Definition->getCanonicalDecl();
142 if (Definition->isConst()) {
146 *Result.SourceManager,
147 Result.Context->getLangOpts());
149 if (DefConst.isInvalid())
152 if (Declaration != Definition) {
154 Declaration->getTypeSourceInfo(), *Result.SourceManager,
155 Result.Context->getLangOpts());
157 if (DeclConst.isInvalid())
159 Diag << FixItHint::CreateRemoval(DeclConst);
163 Diag << FixItHint::CreateRemoval(DefConst);
165 Diag << FixItHint::CreateInsertion(Declaration->getBeginLoc(),
"static ");
CodeCompletionBuilder Builder
CharSourceRange Range
SourceRange for the file name.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
bool VisitCXXThisExpr(const CXXThisExpr *E)
AST_MATCHER(CXXMethodDecl, isStatic)
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.
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)