aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang16/tools/extra/clang-tidy/bugprone/ImplicitWideningOfMultiplicationResultCheck.cpp
blob: e27012a91c3e55297963c214dfed2aba9a919dde (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//===--- ImplicitWideningOfMultiplicationResultCheck.cpp - clang-tidy -----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "ImplicitWideningOfMultiplicationResultCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include <optional>

using namespace clang::ast_matchers;

namespace clang {
namespace {
AST_MATCHER(ImplicitCastExpr, isPartOfExplicitCast) {
  return Node.isPartOfExplicitCast();
}
} // namespace
} // namespace clang

namespace clang::tidy::bugprone {

static const Expr *getLHSOfMulBinOp(const Expr *E) {
  assert(E == E->IgnoreParens() && "Already skipped all parens!");
  // Is this:  long r = int(x) * int(y);  ?
  // FIXME: shall we skip brackets/casts/etc?
  const auto *BO = dyn_cast<BinaryOperator>(E);
  if (!BO || BO->getOpcode() != BO_Mul)
    // FIXME: what about:  long r = int(x) + (int(y) * int(z));  ?
    return nullptr;
  return BO->getLHS()->IgnoreParens();
}

ImplicitWideningOfMultiplicationResultCheck::
    ImplicitWideningOfMultiplicationResultCheck(StringRef Name,
                                                ClangTidyContext *Context)
    : ClangTidyCheck(Name, Context),
      UseCXXStaticCastsInCppSources(
          Options.get("UseCXXStaticCastsInCppSources", true)),
      UseCXXHeadersInCppSources(Options.get("UseCXXHeadersInCppSources", true)),
      IncludeInserter(Options.getLocalOrGlobal("IncludeStyle",
                                               utils::IncludeSorter::IS_LLVM),
                      areDiagsSelfContained()) {}

void ImplicitWideningOfMultiplicationResultCheck::registerPPCallbacks(
    const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
  IncludeInserter.registerPreprocessor(PP);
}

void ImplicitWideningOfMultiplicationResultCheck::storeOptions(
    ClangTidyOptions::OptionMap &Opts) {
  Options.store(Opts, "UseCXXStaticCastsInCppSources",
                UseCXXStaticCastsInCppSources);
  Options.store(Opts, "UseCXXHeadersInCppSources", UseCXXHeadersInCppSources);
  Options.store(Opts, "IncludeStyle", IncludeInserter.getStyle());
}

std::optional<FixItHint>
ImplicitWideningOfMultiplicationResultCheck::includeStddefHeader(
    SourceLocation File) {
  return IncludeInserter.createIncludeInsertion(
      Result->SourceManager->getFileID(File),
      ShouldUseCXXHeader ? "<cstddef>" : "<stddef.h>");
}

void ImplicitWideningOfMultiplicationResultCheck::handleImplicitCastExpr(
    const ImplicitCastExpr *ICE) {
  ASTContext *Context = Result->Context;

  const Expr *E = ICE->getSubExpr()->IgnoreParens();
  QualType Ty = ICE->getType();
  QualType ETy = E->getType();

  assert(!ETy->isDependentType() && !Ty->isDependentType() &&
         "Don't expect to ever get here in template Context.");

  // This must be a widening cast. Else we do not care.
  unsigned SrcWidth = Context->getIntWidth(ETy);
  unsigned TgtWidth = Context->getIntWidth(Ty);
  if (TgtWidth <= SrcWidth)
    return;

  // Does the index expression look like it might be unintentionally computed
  // in a narrower-than-wanted type?
  const Expr *LHS = getLHSOfMulBinOp(E);
  if (!LHS)
    return;

  // Ok, looks like we should diagnose this.
  diag(E->getBeginLoc(), "performing an implicit widening conversion to type "
                         "%0 of a multiplication performed in type %1")
      << Ty << E->getType();

  {
    auto Diag = diag(E->getBeginLoc(),
                     "make conversion explicit to silence this warning",
                     DiagnosticIDs::Note)
                << E->getSourceRange();

    if (ShouldUseCXXStaticCast)
      Diag << FixItHint::CreateInsertion(
                  E->getBeginLoc(), "static_cast<" + Ty.getAsString() + ">(")
           << FixItHint::CreateInsertion(E->getEndLoc(), ")");
    else
      Diag << FixItHint::CreateInsertion(E->getBeginLoc(),
                                         "(" + Ty.getAsString() + ")(")
           << FixItHint::CreateInsertion(E->getEndLoc(), ")");
    Diag << includeStddefHeader(E->getBeginLoc());
  }

  QualType WideExprTy;
  // Get Ty of the same signedness as ExprTy, because we only want to suggest
  // to widen the computation, but not change it's signedness domain.
  if (Ty->isSignedIntegerType() == ETy->isSignedIntegerType())
    WideExprTy = Ty;
  else if (Ty->isSignedIntegerType()) {
    assert(ETy->isUnsignedIntegerType() &&
           "Expected source type to be signed.");
    WideExprTy = Context->getCorrespondingUnsignedType(Ty);
  } else {
    assert(Ty->isUnsignedIntegerType() &&
           "Expected target type to be unsigned.");
    assert(ETy->isSignedIntegerType() &&
           "Expected source type to be unsigned.");
    WideExprTy = Context->getCorrespondingSignedType(Ty);
  }

  {
    auto Diag = diag(E->getBeginLoc(), "perform multiplication in a wider type",
                     DiagnosticIDs::Note)
                << LHS->getSourceRange();

    if (ShouldUseCXXStaticCast)
      Diag << FixItHint::CreateInsertion(LHS->getBeginLoc(),
                                         "static_cast<" +
                                             WideExprTy.getAsString() + ">(")
           << FixItHint::CreateInsertion(LHS->getEndLoc(), ")");
    else
      Diag << FixItHint::CreateInsertion(LHS->getBeginLoc(),
                                         "(" + WideExprTy.getAsString() + ")");
    Diag << includeStddefHeader(LHS->getBeginLoc());
  }
}

void ImplicitWideningOfMultiplicationResultCheck::handlePointerOffsetting(
    const Expr *E) {
  ASTContext *Context = Result->Context;

  // We are looking for a pointer offset operation,
  // with one hand being a pointer, and another one being an offset.
  const Expr *PointerExpr, *IndexExpr;
  if (const auto *BO = dyn_cast<BinaryOperator>(E)) {
    PointerExpr = BO->getLHS();
    IndexExpr = BO->getRHS();
  } else if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
    PointerExpr = ASE->getLHS();
    IndexExpr = ASE->getRHS();
  } else
    return;

  if (IndexExpr->getType()->isPointerType())
    std::swap(PointerExpr, IndexExpr);

  if (!PointerExpr->getType()->isPointerType() ||
      IndexExpr->getType()->isPointerType())
    return;

  IndexExpr = IndexExpr->IgnoreParens();

  QualType IndexExprType = IndexExpr->getType();

  // If the index expression's type is not known (i.e. we are in a template),
  // we can't do anything here.
  if (IndexExprType->isDependentType())
    return;

  QualType SSizeTy = Context->getPointerDiffType();
  QualType USizeTy = Context->getSizeType();
  QualType SizeTy = IndexExprType->isSignedIntegerType() ? SSizeTy : USizeTy;
  // FIXME: is there a way to actually get the QualType for size_t/ptrdiff_t?
  // Note that SizeTy.getAsString() will be unsigned long/..., NOT size_t!
  StringRef TyAsString =
      IndexExprType->isSignedIntegerType() ? "ptrdiff_t" : "size_t";

  // So, is size_t actually wider than the result of the multiplication?
  if (Context->getIntWidth(IndexExprType) >= Context->getIntWidth(SizeTy))
    return;

  // Does the index expression look like it might be unintentionally computed
  // in a narrower-than-wanted type?
  const Expr *LHS = getLHSOfMulBinOp(IndexExpr);
  if (!LHS)
    return;

  // Ok, looks like we should diagnose this.
  diag(E->getBeginLoc(),
       "result of multiplication in type %0 is used as a pointer offset after "
       "an implicit widening conversion to type '%1'")
      << IndexExprType << TyAsString;

  {
    auto Diag = diag(IndexExpr->getBeginLoc(),
                     "make conversion explicit to silence this warning",
                     DiagnosticIDs::Note)
                << IndexExpr->getSourceRange();

    if (ShouldUseCXXStaticCast)
      Diag << FixItHint::CreateInsertion(
                  IndexExpr->getBeginLoc(),
                  (Twine("static_cast<") + TyAsString + ">(").str())
           << FixItHint::CreateInsertion(IndexExpr->getEndLoc(), ")");
    else
      Diag << FixItHint::CreateInsertion(IndexExpr->getBeginLoc(),
                                         (Twine("(") + TyAsString + ")(").str())
           << FixItHint::CreateInsertion(IndexExpr->getEndLoc(), ")");
    Diag << includeStddefHeader(IndexExpr->getBeginLoc());
  }

  {
    auto Diag =
        diag(IndexExpr->getBeginLoc(), "perform multiplication in a wider type",
             DiagnosticIDs::Note)
        << LHS->getSourceRange();

    if (ShouldUseCXXStaticCast)
      Diag << FixItHint::CreateInsertion(
                  LHS->getBeginLoc(),
                  (Twine("static_cast<") + TyAsString + ">(").str())
           << FixItHint::CreateInsertion(LHS->getEndLoc(), ")");
    else
      Diag << FixItHint::CreateInsertion(LHS->getBeginLoc(),
                                         (Twine("(") + TyAsString + ")").str());
    Diag << includeStddefHeader(LHS->getBeginLoc());
  }
}

void ImplicitWideningOfMultiplicationResultCheck::registerMatchers(
    MatchFinder *Finder) {
  Finder->addMatcher(implicitCastExpr(unless(anyOf(isInTemplateInstantiation(),
                                                   isPartOfExplicitCast())),
                                      hasCastKind(CK_IntegralCast))
                         .bind("x"),
                     this);
  Finder->addMatcher(
      arraySubscriptExpr(unless(isInTemplateInstantiation())).bind("x"), this);
  Finder->addMatcher(binaryOperator(unless(isInTemplateInstantiation()),
                                    hasType(isAnyPointer()),
                                    hasAnyOperatorName("+", "-", "+=", "-="))
                         .bind("x"),
                     this);
}

void ImplicitWideningOfMultiplicationResultCheck::check(
    const MatchFinder::MatchResult &Result) {
  this->Result = &Result;
  ShouldUseCXXStaticCast =
      UseCXXStaticCastsInCppSources && Result.Context->getLangOpts().CPlusPlus;
  ShouldUseCXXHeader =
      UseCXXHeadersInCppSources && Result.Context->getLangOpts().CPlusPlus;

  if (const auto *MatchedDecl = Result.Nodes.getNodeAs<ImplicitCastExpr>("x"))
    handleImplicitCastExpr(MatchedDecl);
  else if (const auto *MatchedDecl =
               Result.Nodes.getNodeAs<ArraySubscriptExpr>("x"))
    handlePointerOffsetting(MatchedDecl);
  else if (const auto *MatchedDecl =
               Result.Nodes.getNodeAs<BinaryOperator>("x"))
    handlePointerOffsetting(MatchedDecl);
}

} // namespace clang::tidy::bugprone