llvm-project/clang-tools-extra/clang-tidy/bugprone/ImplicitWideningOfMultiplicationResultCheck.h
Chris Warner f964e8a3f9
[clang-tidy] bugprone-implicit-widening ignores const exprs that fit (#98352)
Add an option to the
`bugprone-implicit-widening-of-multiplication-result` clang-tidy checker
to suppress warnings when the expression is made up of all compile-time
constants (literals, `constexpr` values or results, etc.) and the result
of the multiplication is guaranteed to fit in both the source and
destination types.

This currently only works for signed integer types:

* For unsigned integers, the well-defined rollover behavior on overflow
prevents the checker from detecting that the expression does not
actually fit in the source expr type, and would produce false negatives.
I have a somewhat-hacky stab at addressing this I'd like to submit as a
follow-on PR
* For floating-point types, there's probably a little additional work to
be done to `Expr` to calculate the result at compile time

Even still, this seems like a helpful addition just for signed types,
and additional types can be added.
2024-07-15 21:36:00 +02:00

51 lines
2.0 KiB
C++

//===--- ImplicitWideningOfMultiplicationResultCheck.h ----------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_IMPLICITWIDENINGOFMULTIPLICATIONRESULTCHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_IMPLICITWIDENINGOFMULTIPLICATIONRESULTCHECK_H
#include "../ClangTidyCheck.h"
#include "../utils/IncludeInserter.h"
#include <optional>
namespace clang::tidy::bugprone {
/// Diagnoses instances of an implicit widening of multiplication result.
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/implicit-widening-of-multiplication-result.html
class ImplicitWideningOfMultiplicationResultCheck : public ClangTidyCheck {
const ast_matchers::MatchFinder::MatchResult *Result;
bool ShouldUseCXXStaticCast;
bool ShouldUseCXXHeader;
std::optional<FixItHint> includeStddefHeader(SourceLocation File);
void handleImplicitCastExpr(const ImplicitCastExpr *ICE);
void handlePointerOffsetting(const Expr *E);
public:
ImplicitWideningOfMultiplicationResultCheck(StringRef Name,
ClangTidyContext *Context);
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
Preprocessor *ModuleExpanderPP) override;
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
private:
const bool UseCXXStaticCastsInCppSources;
const bool UseCXXHeadersInCppSources;
const bool IgnoreConstantIntExpr;
utils::IncludeInserter IncludeInserter;
};
} // namespace clang::tidy::bugprone
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_IMPLICITWIDENINGOFMULTIPLICATIONRESULTCHECK_H