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"
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 bool TraverseCXXRecordDecl(CXXRecordDecl *RD) {
return true; }
72 UsageOfThis.TraverseStmt(
const_cast<Stmt *
>(
Node.getBody()));
74 return UsageOfThis.Used;
80 isDefinition(), isUserProvided(),
82 isExpansionInSystemHeader(), isVirtual(), isStatic(),
83 hasTrivialBody(), isOverloadedOperator(), cxxConstructorDecl(),
84 cxxDestructorDecl(), cxxConversionDecl(), isTemplate(),
88 hasAnyDependentBases())
91 isInsideMacroDefinition(),
92 hasCanonicalDecl(isInsideMacroDefinition()), usesThis())))
99 const LangOptions &LangOpts,
101 if (SourceMgr.getFileID(
Range.getBegin()) !=
102 SourceMgr.getFileID(
Range.getEnd()))
105 return Lexer::getSourceText(CharSourceRange(
Range,
true), SourceMgr,
110 SourceManager &SourceMgr,
111 const LangOptions &LangOpts) {
113 const auto FTL = TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
116 SourceRange
Range{FTL.getRParenLoc().getLocWithOffset(1),
117 FTL.getLocalRangeEnd()};
122 if (
Offset == StringRef::npos)
125 SourceLocation Start =
Range.getBegin().getLocWithOffset(
Offset);
126 return {Start, Start.getLocWithOffset(strlen(
"const") - 1)};
130 const MatchFinder::MatchResult &Result) {
131 const auto *Definition = Result.Nodes.getNodeAs<CXXMethodDecl>(
"x");
135 DiagnosticBuilder Diag =
136 diag(Definition->getLocation(),
"method %0 can be made static")
140 if (Definition->getMethodQualifiers().hasVolatile() ||
141 Definition->getMethodQualifiers().hasRestrict() ||
142 Definition->getRefQualifier() != RQ_None)
145 const CXXMethodDecl *Declaration = Definition->getCanonicalDecl();
147 if (Definition->isConst()) {
151 *Result.SourceManager,
152 Result.Context->getLangOpts());
154 if (DefConst.isInvalid())
157 if (Declaration != Definition) {
159 Declaration->getTypeSourceInfo(), *Result.SourceManager,
160 Result.Context->getLangOpts());
162 if (DeclConst.isInvalid())
164 Diag << FixItHint::CreateRemoval(DeclConst);
168 Diag << FixItHint::CreateRemoval(DefConst);
170 Diag << FixItHint::CreateInsertion(Declaration->getBeginLoc(),
"static ");
CodeCompletionBuilder Builder
CharSourceRange Range
SourceRange for the file name.
::clang::DynTypedNode Node
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)