clang-tools 17.0.0git
ConvertMemberFunctionsToStatic.cpp
Go to the documentation of this file.
1//===--- ConvertMemberFunctionsToStatic.cpp - clang-tidy ------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
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"
16
17using namespace clang::ast_matchers;
18
20
21AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); }
22
23AST_MATCHER(CXXMethodDecl, hasTrivialBody) { return Node.hasTrivialBody(); }
24
25AST_MATCHER(CXXMethodDecl, isOverloadedOperator) {
26 return Node.isOverloadedOperator();
27}
28
29AST_MATCHER(CXXRecordDecl, hasAnyDependentBases) {
30 return Node.hasAnyDependentBases();
31}
32
33AST_MATCHER(CXXMethodDecl, isTemplate) {
34 return Node.getTemplatedKind() != FunctionDecl::TK_NonTemplate;
35}
36
37AST_MATCHER(CXXMethodDecl, isDependentContext) {
38 return Node.isDependentContext();
39}
40
41AST_MATCHER(CXXMethodDecl, isInsideMacroDefinition) {
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())
47 .isInvalid();
48}
49
50AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl,
51 ast_matchers::internal::Matcher<CXXMethodDecl>, InnerMatcher) {
52 return InnerMatcher.matches(*Node.getCanonicalDecl(), Finder, Builder);
53}
54
55AST_MATCHER(CXXMethodDecl, usesThis) {
56 class FindUsageOfThis : public RecursiveASTVisitor<FindUsageOfThis> {
57 public:
58 bool Used = false;
59
60 bool VisitCXXThisExpr(const CXXThisExpr *E) {
61 Used = true;
62 return false; // Stop traversal.
63 }
64 } UsageOfThis;
65
66 // TraverseStmt does not modify its argument.
67 UsageOfThis.TraverseStmt(const_cast<Stmt *>(Node.getBody()));
68
69 return UsageOfThis.Used;
70}
71
73 Finder->addMatcher(
74 cxxMethodDecl(
75 isDefinition(), isUserProvided(),
76 unless(anyOf(
77 isExpansionInSystemHeader(), isVirtual(), isStatic(),
78 hasTrivialBody(), isOverloadedOperator(), cxxConstructorDecl(),
79 cxxDestructorDecl(), cxxConversionDecl(), isTemplate(),
80 isDependentContext(),
81 ofClass(anyOf(
82 isLambda(),
83 hasAnyDependentBases()) // Method might become virtual
84 // depending on template base class.
85 ),
86 isInsideMacroDefinition(),
87 hasCanonicalDecl(isInsideMacroDefinition()), usesThis())))
88 .bind("x"),
89 this);
90}
91
92/// Obtain the original source code text from a SourceRange.
93static StringRef getStringFromRange(SourceManager &SourceMgr,
94 const LangOptions &LangOpts,
95 SourceRange Range) {
96 if (SourceMgr.getFileID(Range.getBegin()) !=
97 SourceMgr.getFileID(Range.getEnd()))
98 return {};
99
100 return Lexer::getSourceText(CharSourceRange(Range, true), SourceMgr,
101 LangOpts);
102}
103
104static SourceRange getLocationOfConst(const TypeSourceInfo *TSI,
105 SourceManager &SourceMgr,
106 const LangOptions &LangOpts) {
107 assert(TSI);
108 const auto FTL = TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
109 assert(FTL);
110
111 SourceRange Range{FTL.getRParenLoc().getLocWithOffset(1),
112 FTL.getLocalRangeEnd()};
113 // Inside Range, there might be other keywords and trailing return types.
114 // Find the exact position of "const".
115 StringRef Text = getStringFromRange(SourceMgr, LangOpts, Range);
116 size_t Offset = Text.find("const");
117 if (Offset == StringRef::npos)
118 return {};
119
120 SourceLocation Start = Range.getBegin().getLocWithOffset(Offset);
121 return {Start, Start.getLocWithOffset(strlen("const") - 1)};
122}
123
125 const MatchFinder::MatchResult &Result) {
126 const auto *Definition = Result.Nodes.getNodeAs<CXXMethodDecl>("x");
127
128 // TODO: For out-of-line declarations, don't modify the source if the header
129 // is excluded by the -header-filter option.
130 DiagnosticBuilder Diag =
131 diag(Definition->getLocation(), "method %0 can be made static")
132 << Definition;
133
134 // TODO: Would need to remove those in a fix-it.
135 if (Definition->getMethodQualifiers().hasVolatile() ||
136 Definition->getMethodQualifiers().hasRestrict() ||
137 Definition->getRefQualifier() != RQ_None)
138 return;
139
140 const CXXMethodDecl *Declaration = Definition->getCanonicalDecl();
141
142 if (Definition->isConst()) {
143 // Make sure that we either remove 'const' on both declaration and
144 // definition or emit no fix-it at all.
145 SourceRange DefConst = getLocationOfConst(Definition->getTypeSourceInfo(),
146 *Result.SourceManager,
147 Result.Context->getLangOpts());
148
149 if (DefConst.isInvalid())
150 return;
151
152 if (Declaration != Definition) {
153 SourceRange DeclConst = getLocationOfConst(
154 Declaration->getTypeSourceInfo(), *Result.SourceManager,
155 Result.Context->getLangOpts());
156
157 if (DeclConst.isInvalid())
158 return;
159 Diag << FixItHint::CreateRemoval(DeclConst);
160 }
161
162 // Remove existing 'const' from both declaration and definition.
163 Diag << FixItHint::CreateRemoval(DefConst);
164 }
165 Diag << FixItHint::CreateInsertion(Declaration->getBeginLoc(), "static ");
166}
167
168} // namespace clang::tidy::readability
const Expr * E
size_t Offset
CodeCompletionBuilder Builder
std::string Text
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.
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)