clang-tools 18.0.0git
ImplicitWideningOfMultiplicationResultCheck.cpp
Go to the documentation of this file.
1//===--- ImplicitWideningOfMultiplicationResultCheck.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/ASTMatchers/ASTMatchFinder.h"
12#include <optional>
13
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace {
18AST_MATCHER(ImplicitCastExpr, isPartOfExplicitCast) {
19 return Node.isPartOfExplicitCast();
20}
21} // namespace
22} // namespace clang
23
24namespace clang::tidy::bugprone {
25
26static const Expr *getLHSOfMulBinOp(const Expr *E) {
27 assert(E == E->IgnoreParens() && "Already skipped all parens!");
28 // Is this: long r = int(x) * int(y); ?
29 // FIXME: shall we skip brackets/casts/etc?
30 const auto *BO = dyn_cast<BinaryOperator>(E);
31 if (!BO || BO->getOpcode() != BO_Mul)
32 // FIXME: what about: long r = int(x) + (int(y) * int(z)); ?
33 return nullptr;
34 return BO->getLHS()->IgnoreParens();
35}
36
39 ClangTidyContext *Context)
40 : ClangTidyCheck(Name, Context),
41 UseCXXStaticCastsInCppSources(
42 Options.get("UseCXXStaticCastsInCppSources", true)),
43 UseCXXHeadersInCppSources(Options.get("UseCXXHeadersInCppSources", true)),
44 IncludeInserter(Options.getLocalOrGlobal("IncludeStyle",
45 utils::IncludeSorter::IS_LLVM),
46 areDiagsSelfContained()) {}
47
49 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
50 IncludeInserter.registerPreprocessor(PP);
51}
52
55 Options.store(Opts, "UseCXXStaticCastsInCppSources",
56 UseCXXStaticCastsInCppSources);
57 Options.store(Opts, "UseCXXHeadersInCppSources", UseCXXHeadersInCppSources);
58 Options.store(Opts, "IncludeStyle", IncludeInserter.getStyle());
59}
60
61std::optional<FixItHint>
62ImplicitWideningOfMultiplicationResultCheck::includeStddefHeader(
63 SourceLocation File) {
64 return IncludeInserter.createIncludeInsertion(
65 Result->SourceManager->getFileID(File),
66 ShouldUseCXXHeader ? "<cstddef>" : "<stddef.h>");
67}
68
69void ImplicitWideningOfMultiplicationResultCheck::handleImplicitCastExpr(
70 const ImplicitCastExpr *ICE) {
71 ASTContext *Context = Result->Context;
72
73 const Expr *E = ICE->getSubExpr()->IgnoreParens();
74 QualType Ty = ICE->getType();
75 QualType ETy = E->getType();
76
77 assert(!ETy->isDependentType() && !Ty->isDependentType() &&
78 "Don't expect to ever get here in template Context.");
79
80 // This must be a widening cast. Else we do not care.
81 unsigned SrcWidth = Context->getIntWidth(ETy);
82 unsigned TgtWidth = Context->getIntWidth(Ty);
83 if (TgtWidth <= SrcWidth)
84 return;
85
86 // Does the index expression look like it might be unintentionally computed
87 // in a narrower-than-wanted type?
88 const Expr *LHS = getLHSOfMulBinOp(E);
89 if (!LHS)
90 return;
91
92 // Ok, looks like we should diagnose this.
93 diag(E->getBeginLoc(), "performing an implicit widening conversion to type "
94 "%0 of a multiplication performed in type %1")
95 << Ty << E->getType();
96
97 {
98 auto Diag = diag(E->getBeginLoc(),
99 "make conversion explicit to silence this warning",
100 DiagnosticIDs::Note)
101 << E->getSourceRange();
102
103 if (ShouldUseCXXStaticCast)
104 Diag << FixItHint::CreateInsertion(
105 E->getBeginLoc(), "static_cast<" + Ty.getAsString() + ">(")
106 << FixItHint::CreateInsertion(E->getEndLoc(), ")");
107 else
108 Diag << FixItHint::CreateInsertion(E->getBeginLoc(),
109 "(" + Ty.getAsString() + ")(")
110 << FixItHint::CreateInsertion(E->getEndLoc(), ")");
111 Diag << includeStddefHeader(E->getBeginLoc());
112 }
113
114 QualType WideExprTy;
115 // Get Ty of the same signedness as ExprTy, because we only want to suggest
116 // to widen the computation, but not change it's signedness domain.
117 if (Ty->isSignedIntegerType() == ETy->isSignedIntegerType())
118 WideExprTy = Ty;
119 else if (Ty->isSignedIntegerType()) {
120 assert(ETy->isUnsignedIntegerType() &&
121 "Expected source type to be signed.");
122 WideExprTy = Context->getCorrespondingUnsignedType(Ty);
123 } else {
124 assert(Ty->isUnsignedIntegerType() &&
125 "Expected target type to be unsigned.");
126 assert(ETy->isSignedIntegerType() &&
127 "Expected source type to be unsigned.");
128 WideExprTy = Context->getCorrespondingSignedType(Ty);
129 }
130
131 {
132 auto Diag = diag(E->getBeginLoc(), "perform multiplication in a wider type",
133 DiagnosticIDs::Note)
134 << LHS->getSourceRange();
135
136 if (ShouldUseCXXStaticCast)
137 Diag << FixItHint::CreateInsertion(LHS->getBeginLoc(),
138 "static_cast<" +
139 WideExprTy.getAsString() + ">(")
140 << FixItHint::CreateInsertion(LHS->getEndLoc(), ")");
141 else
142 Diag << FixItHint::CreateInsertion(LHS->getBeginLoc(),
143 "(" + WideExprTy.getAsString() + ")");
144 Diag << includeStddefHeader(LHS->getBeginLoc());
145 }
146}
147
148void ImplicitWideningOfMultiplicationResultCheck::handlePointerOffsetting(
149 const Expr *E) {
150 ASTContext *Context = Result->Context;
151
152 // We are looking for a pointer offset operation,
153 // with one hand being a pointer, and another one being an offset.
154 const Expr *PointerExpr = nullptr, *IndexExpr = nullptr;
155 if (const auto *BO = dyn_cast<BinaryOperator>(E)) {
156 PointerExpr = BO->getLHS();
157 IndexExpr = BO->getRHS();
158 } else if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
159 PointerExpr = ASE->getLHS();
160 IndexExpr = ASE->getRHS();
161 } else
162 return;
163
164 if (IndexExpr->getType()->isPointerType())
165 std::swap(PointerExpr, IndexExpr);
166
167 if (!PointerExpr->getType()->isPointerType() ||
168 IndexExpr->getType()->isPointerType())
169 return;
170
171 IndexExpr = IndexExpr->IgnoreParens();
172
173 QualType IndexExprType = IndexExpr->getType();
174
175 // If the index expression's type is not known (i.e. we are in a template),
176 // we can't do anything here.
177 if (IndexExprType->isDependentType())
178 return;
179
180 QualType SSizeTy = Context->getPointerDiffType();
181 QualType USizeTy = Context->getSizeType();
182 QualType SizeTy = IndexExprType->isSignedIntegerType() ? SSizeTy : USizeTy;
183 // FIXME: is there a way to actually get the QualType for size_t/ptrdiff_t?
184 // Note that SizeTy.getAsString() will be unsigned long/..., NOT size_t!
185 StringRef TyAsString =
186 IndexExprType->isSignedIntegerType() ? "ptrdiff_t" : "size_t";
187
188 // So, is size_t actually wider than the result of the multiplication?
189 if (Context->getIntWidth(IndexExprType) >= Context->getIntWidth(SizeTy))
190 return;
191
192 // Does the index expression look like it might be unintentionally computed
193 // in a narrower-than-wanted type?
194 const Expr *LHS = getLHSOfMulBinOp(IndexExpr);
195 if (!LHS)
196 return;
197
198 // Ok, looks like we should diagnose this.
199 diag(E->getBeginLoc(),
200 "result of multiplication in type %0 is used as a pointer offset after "
201 "an implicit widening conversion to type '%1'")
202 << IndexExprType << TyAsString;
203
204 {
205 auto Diag = diag(IndexExpr->getBeginLoc(),
206 "make conversion explicit to silence this warning",
207 DiagnosticIDs::Note)
208 << IndexExpr->getSourceRange();
209
210 if (ShouldUseCXXStaticCast)
211 Diag << FixItHint::CreateInsertion(
212 IndexExpr->getBeginLoc(),
213 (Twine("static_cast<") + TyAsString + ">(").str())
214 << FixItHint::CreateInsertion(IndexExpr->getEndLoc(), ")");
215 else
216 Diag << FixItHint::CreateInsertion(IndexExpr->getBeginLoc(),
217 (Twine("(") + TyAsString + ")(").str())
218 << FixItHint::CreateInsertion(IndexExpr->getEndLoc(), ")");
219 Diag << includeStddefHeader(IndexExpr->getBeginLoc());
220 }
221
222 {
223 auto Diag =
224 diag(IndexExpr->getBeginLoc(), "perform multiplication in a wider type",
225 DiagnosticIDs::Note)
226 << LHS->getSourceRange();
227
228 if (ShouldUseCXXStaticCast)
229 Diag << FixItHint::CreateInsertion(
230 LHS->getBeginLoc(),
231 (Twine("static_cast<") + TyAsString + ">(").str())
232 << FixItHint::CreateInsertion(LHS->getEndLoc(), ")");
233 else
234 Diag << FixItHint::CreateInsertion(LHS->getBeginLoc(),
235 (Twine("(") + TyAsString + ")").str());
236 Diag << includeStddefHeader(LHS->getBeginLoc());
237 }
238}
239
241 MatchFinder *Finder) {
242 Finder->addMatcher(implicitCastExpr(unless(anyOf(isInTemplateInstantiation(),
243 isPartOfExplicitCast())),
244 hasCastKind(CK_IntegralCast))
245 .bind("x"),
246 this);
247 Finder->addMatcher(
248 arraySubscriptExpr(unless(isInTemplateInstantiation())).bind("x"), this);
249 Finder->addMatcher(binaryOperator(unless(isInTemplateInstantiation()),
250 hasType(isAnyPointer()),
251 hasAnyOperatorName("+", "-", "+=", "-="))
252 .bind("x"),
253 this);
254}
255
257 const MatchFinder::MatchResult &Result) {
258 this->Result = &Result;
259 ShouldUseCXXStaticCast =
260 UseCXXStaticCastsInCppSources && Result.Context->getLangOpts().CPlusPlus;
261 ShouldUseCXXHeader =
262 UseCXXHeadersInCppSources && Result.Context->getLangOpts().CPlusPlus;
263
264 if (const auto *MatchedDecl = Result.Nodes.getNodeAs<ImplicitCastExpr>("x"))
265 handleImplicitCastExpr(MatchedDecl);
266 else if (const auto *MatchedDecl =
267 Result.Nodes.getNodeAs<ArraySubscriptExpr>("x"))
268 handlePointerOffsetting(MatchedDecl);
269 else if (const auto *MatchedDecl =
270 Result.Nodes.getNodeAs<BinaryOperator>("x"))
271 handlePointerOffsetting(MatchedDecl);
272}
273
274} // namespace clang::tidy::bugprone
const Expr * E
llvm::StringRef Name
::clang::DynTypedNode Node
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerPreprocessor(Preprocessor *PP)
Registers this with the Preprocessor PP, must be called before this class is used.
std::optional< FixItHint > createIncludeInsertion(FileID FileID, llvm::StringRef Header)
Creates a Header inclusion directive fixit in the File FileID.
IncludeSorter::IncludeStyle getStyle() const
AST_MATCHER(Decl, declHasNoReturnAttr)
matches a Decl if it has a "no return" attribute of any kind
static const Expr * getLHSOfMulBinOp(const Expr *E)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::StringMap< ClangTidyValue > OptionMap