clang-tools 23.0.0git
ConvertMemberFunctionsToStaticCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
21namespace {
22
23AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); }
24
25AST_MATCHER(CXXMethodDecl, hasTrivialBody) { return Node.hasTrivialBody(); }
26
27AST_MATCHER(CXXMethodDecl, isOverloadedOperator) {
28 return Node.isOverloadedOperator();
29}
30
31AST_MATCHER(CXXRecordDecl, hasAnyDependentBases) {
32 return Node.hasAnyDependentBases();
33}
34
35AST_MATCHER(CXXMethodDecl, isTemplate) {
36 return Node.getTemplatedKind() != FunctionDecl::TK_NonTemplate;
37}
38
39AST_MATCHER(CXXMethodDecl, isDependentContext) {
40 return Node.isDependentContext();
41}
42
43AST_MATCHER(CXXMethodDecl, isInsideMacroDefinition) {
44 const ASTContext &Ctxt = Finder->getASTContext();
45 return Lexer::makeFileCharRange(
46 CharSourceRange::getCharRange(
47 Node.getTypeSourceInfo()->getTypeLoc().getSourceRange()),
48 Ctxt.getSourceManager(), Ctxt.getLangOpts())
49 .isInvalid();
50}
51
52AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl,
53 ast_matchers::internal::Matcher<CXXMethodDecl>, InnerMatcher) {
54 return InnerMatcher.matches(*Node.getCanonicalDecl(), Finder, Builder);
55}
56
57AST_MATCHER(CXXMethodDecl, usesThis) {
58 class FindUsageOfThis : public RecursiveASTVisitor<FindUsageOfThis> {
59 public:
60 bool Used = false;
61
62 bool VisitCXXThisExpr(const CXXThisExpr *E) {
63 Used = true;
64 return false; // Stop traversal.
65 }
66
67 bool VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *E) {
68 if (E->isImplicitAccess()) {
69 Used = true;
70 return false;
71 }
72 return true; // Continue traversal.
73 }
74
75 // If we enter a class declaration, don't traverse into it as any usages of
76 // `this` will correspond to the nested class.
77 bool TraverseCXXRecordDecl(CXXRecordDecl *RD) { return true; }
78
79 } UsageOfThis;
80
81 // TraverseStmt does not modify its argument.
82 UsageOfThis.TraverseStmt(Node.getBody());
83
84 return UsageOfThis.Used;
85}
86
87AST_MATCHER(CXXMethodDecl, hasNonConstOverload) {
88 const auto *Method = &Node;
89 const DeclContext::lookup_result LookupResult =
90 Method->getParent()->lookup(Method->getNameInfo().getName());
91 if (LookupResult.isSingleResult())
92 return false;
93
94 auto HasSameParameterTypes = [](const CXXMethodDecl &MD1,
95 const CXXMethodDecl &MD2) {
96 if (MD1.getNumParams() != MD2.getNumParams())
97 return false;
98 for (unsigned I = 0, E = MD1.getNumParams(); I < E; ++I)
99 if (MD1.getParamDecl(I)->getType().getCanonicalType() !=
100 MD2.getParamDecl(I)->getType().getCanonicalType())
101 return false;
102 return true;
103 };
104
105 return llvm::any_of(
106 LookupResult, [Method, HasSameParameterTypes](const Decl *D) {
107 const auto *Overload = dyn_cast<CXXMethodDecl>(D);
108 return Overload && Overload != Method && !Overload->isConst() &&
109 HasSameParameterTypes(*Method, *Overload);
110 });
111}
112} // namespace
113
115 MatchFinder *Finder) {
116 Finder->addMatcher(
117 cxxMethodDecl(
118 isDefinition(), isUserProvided(),
119 unless(anyOf(
120 isVirtual(), isStatic(), hasTrivialBody(), isOverloadedOperator(),
121 cxxConstructorDecl(), cxxDestructorDecl(), cxxConversionDecl(),
122 isExplicitObjectMemberFunction(), isTemplate(),
123 isDependentContext(), allOf(isConst(), hasNonConstOverload()),
124 ofClass(anyOf(
125 isLambda(),
126 hasAnyDependentBases()) // Method might become virtual
127 // depending on template base class.
128 ),
129 isInsideMacroDefinition(),
130 hasCanonicalDecl(isInsideMacroDefinition()), usesThis())))
131 .bind("x"),
132 this);
133}
134
135/// Obtain the original source code text from a SourceRange.
136static StringRef getStringFromRange(SourceManager &SourceMgr,
137 const LangOptions &LangOpts,
138 SourceRange Range) {
139 if (SourceMgr.getFileID(Range.getBegin()) !=
140 SourceMgr.getFileID(Range.getEnd()))
141 return {};
142
143 return Lexer::getSourceText(CharSourceRange(Range, true), SourceMgr,
144 LangOpts);
145}
146
147static SourceRange getLocationOfConst(const TypeSourceInfo *TSI,
148 SourceManager &SourceMgr,
149 const LangOptions &LangOpts) {
150 assert(TSI);
151 const auto FTL = TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
152 assert(FTL);
153
154 const SourceRange Range{FTL.getRParenLoc().getLocWithOffset(1),
155 FTL.getLocalRangeEnd()};
156 // Inside Range, there might be other keywords and trailing return types.
157 // Find the exact position of "const".
158 const StringRef Text = getStringFromRange(SourceMgr, LangOpts, Range);
159 const size_t Offset = Text.find("const");
160 if (Offset == StringRef::npos)
161 return {};
162
163 const SourceLocation Start = Range.getBegin().getLocWithOffset(Offset);
164 return {Start, Start.getLocWithOffset(strlen("const") - 1)};
165}
166
168 const MatchFinder::MatchResult &Result) {
169 const auto *Definition = Result.Nodes.getNodeAs<CXXMethodDecl>("x");
170
171 // TODO: For out-of-line declarations, don't modify the source if the header
172 // is excluded by the -header-filter option.
173 const DiagnosticBuilder Diag =
174 diag(Definition->getLocation(), "method %0 can be made static")
175 << Definition;
176
177 // TODO: Would need to remove those in a fix-it.
178 if (Definition->getMethodQualifiers().hasVolatile() ||
179 Definition->getMethodQualifiers().hasRestrict() ||
180 Definition->getRefQualifier() != RQ_None)
181 return;
182
183 const CXXMethodDecl *Declaration = Definition->getCanonicalDecl();
184
185 if (Definition->isConst()) {
186 // Make sure that we either remove 'const' on both declaration and
187 // definition or emit no fix-it at all.
188 const SourceRange DefConst = getLocationOfConst(
189 Definition->getTypeSourceInfo(), *Result.SourceManager,
190 Result.Context->getLangOpts());
191
192 if (DefConst.isInvalid())
193 return;
194
195 if (Declaration != Definition) {
196 const SourceRange DeclConst = getLocationOfConst(
197 Declaration->getTypeSourceInfo(), *Result.SourceManager,
198 Result.Context->getLangOpts());
199
200 if (DeclConst.isInvalid())
201 return;
202 Diag << FixItHint::CreateRemoval(DeclConst);
203 }
204
205 // Remove existing 'const' from both declaration and definition.
206 Diag << FixItHint::CreateRemoval(DefConst);
207 }
208 Diag << FixItHint::CreateInsertion(Declaration->getBeginLoc(), "static ");
209}
210
211} // namespace clang::tidy::readability
void check(const ast_matchers::MatchFinder::MatchResult &Result) 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.