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