10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/ASTMatchersMacros.h"
13#include "clang/Lex/Lexer.h"
21AST_MATCHER(ImplicitCastExpr, isPartOfExplicitCast) {
22 return Node.isPartOfExplicitCast();
24AST_MATCHER(Expr, containsErrors) {
return Node.containsErrors(); }
28 assert(E == E->IgnoreParens() &&
"Already skipped all parens!");
31 const auto *BO = dyn_cast<BinaryOperator>(E);
32 if (!BO || BO->getOpcode() != BO_Mul)
35 return BO->getLHS()->IgnoreParens();
42 UseCXXStaticCastsInCppSources(
43 Options.get(
"UseCXXStaticCastsInCppSources", true)),
44 UseCXXHeadersInCppSources(Options.get(
"UseCXXHeadersInCppSources", true)),
45 IgnoreConstantIntExpr(Options.get(
"IgnoreConstantIntExpr", false)),
46 IncludeInserter(Options.getLocalOrGlobal(
"IncludeStyle",
47 utils::IncludeSorter::IS_LLVM),
48 areDiagsSelfContained()) {}
51 const SourceManager &SM, Preprocessor *
PP, Preprocessor *ModuleExpanderPP) {
52 IncludeInserter.registerPreprocessor(
PP);
57 Options.store(Opts,
"UseCXXStaticCastsInCppSources",
58 UseCXXStaticCastsInCppSources);
59 Options.store(Opts,
"UseCXXHeadersInCppSources", UseCXXHeadersInCppSources);
60 Options.store(Opts,
"IgnoreConstantIntExpr", IgnoreConstantIntExpr);
61 Options.store(Opts,
"IncludeStyle", IncludeInserter.getStyle());
64std::optional<FixItHint>
65ImplicitWideningOfMultiplicationResultCheck::includeStddefHeader(
66 SourceLocation File) {
67 return IncludeInserter.createIncludeInsertion(
68 Result->SourceManager->getFileID(File),
69 ShouldUseCXXHeader ?
"<cstddef>" :
"<stddef.h>");
72void ImplicitWideningOfMultiplicationResultCheck::handleImplicitCastExpr(
73 const ImplicitCastExpr *ICE) {
74 const ASTContext *Context = Result->Context;
76 const Expr *E = ICE->getSubExpr()->IgnoreParens();
77 const QualType Ty = ICE->getType();
78 const QualType ETy = E->getType();
80 assert(!ETy->isDependentType() && !Ty->isDependentType() &&
81 "Don't expect to ever get here in template Context.");
84 const unsigned SrcWidth = Context->getIntWidth(ETy);
85 const unsigned TgtWidth = Context->getIntWidth(Ty);
86 if (TgtWidth <= SrcWidth)
91 if (IgnoreConstantIntExpr && ETy->isIntegerType() &&
92 !ETy->isUnsignedIntegerType()) {
93 if (
const auto ConstExprResult = E->getIntegerConstantExpr(*Context)) {
94 const auto TypeSize = Context->getTypeSize(ETy);
95 const llvm::APSInt WidenedResult = ConstExprResult->extOrTrunc(TypeSize);
96 if (WidenedResult <= llvm::APSInt::getMaxValue(TypeSize,
false) &&
97 WidenedResult >= llvm::APSInt::getMinValue(TypeSize,
false))
109 diag(E->getBeginLoc(),
"performing an implicit widening conversion to type "
110 "%0 of a multiplication performed in type %1")
111 << Ty << E->getType();
114 auto Diag = diag(E->getBeginLoc(),
115 "make conversion explicit to silence this warning",
117 << E->getSourceRange();
118 const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
119 E->getEndLoc(), 0, *Result->SourceManager, getLangOpts());
120 if (ShouldUseCXXStaticCast)
121 Diag << FixItHint::CreateInsertion(
122 E->getBeginLoc(),
"static_cast<" + Ty.getAsString() +
">(")
123 << FixItHint::CreateInsertion(EndLoc,
")");
125 Diag << FixItHint::CreateInsertion(E->getBeginLoc(),
126 "(" + Ty.getAsString() +
")(")
127 << FixItHint::CreateInsertion(EndLoc,
")");
128 Diag << includeStddefHeader(E->getBeginLoc());
134 if (Ty->isSignedIntegerType() == ETy->isSignedIntegerType())
136 else if (Ty->isSignedIntegerType()) {
137 assert(ETy->isUnsignedIntegerType() &&
138 "Expected source type to be signed.");
139 WideExprTy = Context->getCorrespondingUnsignedType(Ty);
141 assert(Ty->isUnsignedIntegerType() &&
142 "Expected target type to be unsigned.");
143 assert(ETy->isSignedIntegerType() &&
144 "Expected source type to be unsigned.");
145 WideExprTy = Context->getCorrespondingSignedType(Ty);
149 auto Diag = diag(E->getBeginLoc(),
"perform multiplication in a wider type",
151 << LHS->getSourceRange();
153 if (ShouldUseCXXStaticCast)
154 Diag << FixItHint::CreateInsertion(LHS->getBeginLoc(),
156 WideExprTy.getAsString() +
">(")
157 << FixItHint::CreateInsertion(
158 Lexer::getLocForEndOfToken(LHS->getEndLoc(), 0,
159 *Result->SourceManager,
163 Diag << FixItHint::CreateInsertion(LHS->getBeginLoc(),
164 "(" + WideExprTy.getAsString() +
")");
165 Diag << includeStddefHeader(LHS->getBeginLoc());
169void ImplicitWideningOfMultiplicationResultCheck::handlePointerOffsetting(
171 const ASTContext *Context = Result->Context;
175 const Expr *PointerExpr =
nullptr, *IndexExpr =
nullptr;
176 if (
const auto *BO = dyn_cast<BinaryOperator>(E)) {
177 PointerExpr = BO->getLHS();
178 IndexExpr = BO->getRHS();
179 }
else if (
const auto *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
180 PointerExpr = ASE->getLHS();
181 IndexExpr = ASE->getRHS();
185 if (IndexExpr->getType()->isPointerType())
186 std::swap(PointerExpr, IndexExpr);
188 if (!PointerExpr->getType()->isPointerType() ||
189 IndexExpr->getType()->isPointerType())
192 IndexExpr = IndexExpr->IgnoreParens();
194 const QualType IndexExprType = IndexExpr->getType();
198 if (IndexExprType->isDependentType())
201 const QualType SSizeTy = Context->getPointerDiffType();
202 const QualType USizeTy = Context->getSizeType();
203 const QualType SizeTy =
204 IndexExprType->isSignedIntegerType() ? SSizeTy : USizeTy;
207 const StringRef TyAsString =
208 IndexExprType->isSignedIntegerType() ?
"ptrdiff_t" :
"size_t";
211 if (Context->getIntWidth(IndexExprType) >= Context->getIntWidth(SizeTy))
221 diag(E->getBeginLoc(),
222 "result of multiplication in type %0 is used as a pointer offset after "
223 "an implicit widening conversion to type '%1'")
224 << IndexExprType << TyAsString;
227 auto Diag = diag(IndexExpr->getBeginLoc(),
228 "make conversion explicit to silence this warning",
230 << IndexExpr->getSourceRange();
231 const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
232 IndexExpr->getEndLoc(), 0, *Result->SourceManager, getLangOpts());
233 if (ShouldUseCXXStaticCast)
234 Diag << FixItHint::CreateInsertion(
235 IndexExpr->getBeginLoc(),
236 (Twine(
"static_cast<") + TyAsString +
">(").str())
237 << FixItHint::CreateInsertion(EndLoc,
")");
239 Diag << FixItHint::CreateInsertion(IndexExpr->getBeginLoc(),
240 (Twine(
"(") + TyAsString +
")(").str())
241 << FixItHint::CreateInsertion(EndLoc,
")");
242 Diag << includeStddefHeader(IndexExpr->getBeginLoc());
247 diag(IndexExpr->getBeginLoc(),
"perform multiplication in a wider type",
249 << LHS->getSourceRange();
251 if (ShouldUseCXXStaticCast)
252 Diag << FixItHint::CreateInsertion(
254 (Twine(
"static_cast<") + TyAsString +
">(").str())
255 << FixItHint::CreateInsertion(
256 Lexer::getLocForEndOfToken(IndexExpr->getEndLoc(), 0,
257 *Result->SourceManager,
261 Diag << FixItHint::CreateInsertion(LHS->getBeginLoc(),
262 (Twine(
"(") + TyAsString +
")").str());
263 Diag << includeStddefHeader(LHS->getBeginLoc());
268 MatchFinder *Finder) {
269 Finder->addMatcher(implicitCastExpr(unless(anyOf(containsErrors(),
270 isInTemplateInstantiation(),
271 isPartOfExplicitCast())),
272 hasCastKind(CK_IntegralCast))
276 arraySubscriptExpr(unless(isInTemplateInstantiation())).bind(
"x"),
this);
277 Finder->addMatcher(binaryOperator(unless(isInTemplateInstantiation()),
278 hasType(isAnyPointer()),
279 hasAnyOperatorName(
"+",
"-",
"+=",
"-="))
285 const MatchFinder::MatchResult &Result) {
286 this->Result = &Result;
287 ShouldUseCXXStaticCast =
288 UseCXXStaticCastsInCppSources && Result.Context->getLangOpts().CPlusPlus;
290 UseCXXHeadersInCppSources && Result.Context->getLangOpts().CPlusPlus;
292 if (
const auto *MatchedDecl = Result.Nodes.getNodeAs<ImplicitCastExpr>(
"x"))
293 handleImplicitCastExpr(MatchedDecl);
294 else if (
const auto *MatchedDecl =
295 Result.Nodes.getNodeAs<ArraySubscriptExpr>(
"x"))
296 handlePointerOffsetting(MatchedDecl);
297 else if (
const auto *MatchedDecl =
298 Result.Nodes.getNodeAs<BinaryOperator>(
"x"))
299 handlePointerOffsetting(MatchedDecl);
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
ImplicitWideningOfMultiplicationResultCheck(StringRef Name, ClangTidyContext *Context)
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
static const Expr * getLHSOfMulBinOp(const Expr *E)
AST_MATCHER(BinaryOperator, isRelationalOperator)
llvm::StringMap< ClangTidyValue > OptionMap