clang-tools  14.0.0git
NarrowingConversionsCheck.h
Go to the documentation of this file.
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
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  bool isWarningInhibitedByEquivalentSize(const ASTContext &Context,
97  const BuiltinType &FromType,
98  const BuiltinType &ToType) const;
99 
100  const bool WarnOnIntegerNarrowingConversion;
101  const bool WarnOnFloatingPointNarrowingConversion;
102  const bool WarnWithinTemplateInstantiation;
103  const bool WarnOnEquivalentBitWidth;
104  const std::string IgnoreConversionFromTypes;
105  const bool PedanticMode;
106 };
107 
108 } // namespace cppcoreguidelines
109 } // namespace tidy
110 } // namespace clang
111 
112 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_NARROWING_CONVERSIONS_H
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::cppcoreguidelines::NarrowingConversionsCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: NarrowingConversionsCheck.cpp:591
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
clang::tidy::cppcoreguidelines::NarrowingConversionsCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: NarrowingConversionsCheck.cpp:48
clang::tidy::cppcoreguidelines::NarrowingConversionsCheck
Checks for narrowing conversions, e.g: int i = 0; i += 0.1;.
Definition: NarrowingConversionsCheck.h:24
clang::tidy::cppcoreguidelines::NarrowingConversionsCheck::NarrowingConversionsCheck
NarrowingConversionsCheck(StringRef Name, ClangTidyContext *Context)
Definition: NarrowingConversionsCheck.cpp:35
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:72
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::cppcoreguidelines::NarrowingConversionsCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: NarrowingConversionsCheck.cpp:69