| 1 | //===--- NarrowingConversionsCheck.h - clang-tidy----------------*- C++ -*-===// |
| 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 | |
| 9 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_NARROWING_CONVERSIONS_H |
| 10 | #define |
| 11 | |
| 12 | #include "../ClangTidyCheck.h" |
| 13 | |
| 14 | namespace clang::tidy::bugprone { |
| 15 | |
| 16 | /// Checks for narrowing conversions, e.g: |
| 17 | /// int i = 0; |
| 18 | /// i += 0.1; |
| 19 | /// |
| 20 | /// For the user-facing documentation see: |
| 21 | /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/narrowing-conversions.html |
| 22 | class NarrowingConversionsCheck : public ClangTidyCheck { |
| 23 | public: |
| 24 | NarrowingConversionsCheck(StringRef Name, ClangTidyContext *Context); |
| 25 | |
| 26 | void storeOptions(ClangTidyOptions::OptionMap &Opts) override; |
| 27 | |
| 28 | void registerMatchers(ast_matchers::MatchFinder *Finder) override; |
| 29 | void check(const ast_matchers::MatchFinder::MatchResult &Result) override; |
| 30 | |
| 31 | private: |
| 32 | void diagNarrowType(SourceLocation SourceLoc, const Expr &Lhs, |
| 33 | const Expr &Rhs); |
| 34 | |
| 35 | void diagNarrowTypeToSignedInt(SourceLocation SourceLoc, const Expr &Lhs, |
| 36 | const Expr &Rhs); |
| 37 | |
| 38 | void diagNarrowIntegerConstant(SourceLocation SourceLoc, const Expr &Lhs, |
| 39 | const Expr &Rhs, const llvm::APSInt &Value); |
| 40 | |
| 41 | void diagNarrowIntegerConstantToSignedInt(SourceLocation SourceLoc, |
| 42 | const Expr &Lhs, const Expr &Rhs, |
| 43 | const llvm::APSInt &Value, |
| 44 | const uint64_t HexBits); |
| 45 | |
| 46 | void diagNarrowConstant(SourceLocation SourceLoc, const Expr &Lhs, |
| 47 | const Expr &Rhs); |
| 48 | |
| 49 | void diagConstantCast(SourceLocation SourceLoc, const Expr &Lhs, |
| 50 | const Expr &Rhs); |
| 51 | |
| 52 | void diagNarrowTypeOrConstant(const ASTContext &Context, |
| 53 | SourceLocation SourceLoc, const Expr &Lhs, |
| 54 | const Expr &Rhs); |
| 55 | |
| 56 | void handleIntegralCast(const ASTContext &Context, SourceLocation SourceLoc, |
| 57 | const Expr &Lhs, const Expr &Rhs); |
| 58 | |
| 59 | void handleIntegralToBoolean(const ASTContext &Context, |
| 60 | SourceLocation SourceLoc, const Expr &Lhs, |
| 61 | const Expr &Rhs); |
| 62 | |
| 63 | void handleIntegralToFloating(const ASTContext &Context, |
| 64 | SourceLocation SourceLoc, const Expr &Lhs, |
| 65 | const Expr &Rhs); |
| 66 | |
| 67 | void handleFloatingToIntegral(const ASTContext &Context, |
| 68 | SourceLocation SourceLoc, const Expr &Lhs, |
| 69 | const Expr &Rhs); |
| 70 | |
| 71 | void handleFloatingToBoolean(const ASTContext &Context, |
| 72 | SourceLocation SourceLoc, const Expr &Lhs, |
| 73 | const Expr &Rhs); |
| 74 | |
| 75 | void handleBooleanToSignedIntegral(const ASTContext &Context, |
| 76 | SourceLocation SourceLoc, const Expr &Lhs, |
| 77 | const Expr &Rhs); |
| 78 | |
| 79 | void handleFloatingCast(const ASTContext &Context, SourceLocation SourceLoc, |
| 80 | const Expr &Lhs, const Expr &Rhs); |
| 81 | |
| 82 | void handleBinaryOperator(const ASTContext &Context, SourceLocation SourceLoc, |
| 83 | const Expr &Lhs, const Expr &Rhs); |
| 84 | |
| 85 | bool handleConditionalOperator(const ASTContext &Context, const Expr &Lhs, |
| 86 | const Expr &Rhs); |
| 87 | |
| 88 | void handleImplicitCast(const ASTContext &Context, |
| 89 | const ImplicitCastExpr &Cast); |
| 90 | |
| 91 | void handleBinaryOperator(const ASTContext &Context, |
| 92 | const BinaryOperator &Op); |
| 93 | |
| 94 | bool isWarningInhibitedByEquivalentSize(const ASTContext &Context, |
| 95 | const BuiltinType &FromType, |
| 96 | const BuiltinType &ToType) const; |
| 97 | |
| 98 | const bool WarnOnIntegerNarrowingConversion; |
| 99 | const bool WarnOnIntegerToFloatingPointNarrowingConversion; |
| 100 | const bool WarnOnFloatingPointNarrowingConversion; |
| 101 | const bool WarnWithinTemplateInstantiation; |
| 102 | const bool WarnOnEquivalentBitWidth; |
| 103 | const StringRef IgnoreConversionFromTypes; |
| 104 | const bool PedanticMode; |
| 105 | }; |
| 106 | |
| 107 | } // namespace clang::tidy::bugprone |
| 108 | |
| 109 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_NARROWING_CONVERSIONS_H |
| 110 | |