Debian Codegraph @

llvm-project/clang-tools-extra/clang-tidy/bugprone/FoldInitTypeCheck.cpp

raw
1 //===--- FoldInitTypeCheck.cpp - clang-tidy--------------------------------===//
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 #include "FoldInitTypeCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12
13 using namespace clang::ast_matchers;
14
15 namespace clang::tidy::bugprone {
16
17 void FoldInitTypeCheck::registerMatchers(MatchFinder *Finder) {
18 // We match functions of interest and bind the iterator and init value types.
19 // Note: Right now we check only builtin types.
20 const auto BuiltinTypeWithId = [](const char *ID) {
21 return hasCanonicalType(builtinType().bind(ID));
22 };
23 const auto IteratorWithValueType = [&BuiltinTypeWithId](const char *ID) {
24 return anyOf(
25 // Pointer types.
26 pointsTo(BuiltinTypeWithId(ID)),
27 // Iterator types.
28 recordType(hasDeclaration(has(typedefNameDecl(
29 hasName("value_type"), hasType(BuiltinTypeWithId(ID)))))));
30 };
31
32 const auto IteratorParam = parmVarDecl(
33 hasType(hasCanonicalType(IteratorWithValueType("IterValueType"))));
34 const auto Iterator2Param = parmVarDecl(
35 hasType(hasCanonicalType(IteratorWithValueType("Iter2ValueType"))));
36 const auto InitParam = parmVarDecl(hasType(BuiltinTypeWithId("InitType")));
37
38 // std::accumulate, std::reduce.
39 Finder->addMatcher(
40 callExpr(callee(functionDecl(
41 hasAnyName("::std::accumulate", "::std::reduce"),
42 hasParameter(0, IteratorParam), hasParameter(2, InitParam))),
43 argumentCountIs(3))
44 .bind("Call"),
45 this);
46 // std::inner_product.
47 Finder->addMatcher(
48 callExpr(callee(functionDecl(hasName("::std::inner_product"),
49 hasParameter(0, IteratorParam),
50 hasParameter(2, Iterator2Param),
51 hasParameter(3, InitParam))),
52 argumentCountIs(4))
53 .bind("Call"),
54 this);
55 // std::reduce with a policy.
56 Finder->addMatcher(
57 callExpr(callee(functionDecl(hasName("::std::reduce"),
58 hasParameter(1, IteratorParam),
59 hasParameter(3, InitParam))),
60 argumentCountIs(4))
61 .bind("Call"),
62 this);
63 // std::inner_product with a policy.
64 Finder->addMatcher(
65 callExpr(callee(functionDecl(hasName("::std::inner_product"),
66 hasParameter(1, IteratorParam),
67 hasParameter(3, Iterator2Param),
68 hasParameter(4, InitParam))),
69 argumentCountIs(5))
70 .bind("Call"),
71 this);
72 }
73
74 /// Returns true if ValueType is allowed to fold into InitType, i.e. if:
75 /// static_cast<InitType>(ValueType{some_value})
76 /// does not result in trucation.
77 static bool isValidBuiltinFold(const BuiltinType &ValueType,
78 const BuiltinType &InitType,
79 const ASTContext &Context) {
80 const auto ValueTypeSize = Context.getTypeSize(&ValueType);
81 const auto InitTypeSize = Context.getTypeSize(&InitType);
82 // It's OK to fold a float into a float of bigger or equal size, but not OK to
83 // fold into an int.
84 if (ValueType.isFloatingPoint())
85 return InitType.isFloatingPoint() && InitTypeSize >= ValueTypeSize;
86 // It's OK to fold an int into:
87 // - an int of the same size and signedness.
88 // - a bigger int, regardless of signedness.
89 // - FIXME: should it be a warning to fold into floating point?
90 if (ValueType.isInteger()) {
91 if (InitType.isInteger()) {
92 if (InitType.isSignedInteger() == ValueType.isSignedInteger())
93 return InitTypeSize >= ValueTypeSize;
94 return InitTypeSize > ValueTypeSize;
95 }
96 if (InitType.isFloatingPoint())
97 return InitTypeSize >= ValueTypeSize;
98 }
99 return false;
100 }
101
102 /// Prints a diagnostic if IterValueType doe snot fold into IterValueType (see
103 // isValidBuiltinFold for details).
104 void FoldInitTypeCheck::doCheck(const BuiltinType &IterValueType,
105 const BuiltinType &InitType,
106 const ASTContext &Context,
107 const CallExpr &CallNode) {
108 if (!isValidBuiltinFold(IterValueType, InitType, Context)) {
109 diag(CallNode.getExprLoc(), "folding type %0 into type %1 might result in "
110 "loss of precision")
111 << IterValueType.desugar() << InitType.desugar();
112 }
113 }
114
115 void FoldInitTypeCheck::check(const MatchFinder::MatchResult &Result) {
116 // Given the iterator and init value type retrieved by the matchers,
117 // we check that the ::value_type of the iterator is compatible with
118 // the init value type.
119 const auto *InitType = Result.Nodes.getNodeAs<BuiltinType>("InitType");
120 const auto *IterValueType =
121 Result.Nodes.getNodeAs<BuiltinType>("IterValueType");
122 assert(InitType != nullptr);
123 assert(IterValueType != nullptr);
124
125 const auto *CallNode = Result.Nodes.getNodeAs<CallExpr>("Call");
126 assert(CallNode != nullptr);
127
128 doCheck(*IterValueType, *InitType, *Result.Context, *CallNode);
129
130 if (const auto *Iter2ValueType =
131 Result.Nodes.getNodeAs<BuiltinType>("Iter2ValueType"))
132 doCheck(*Iter2ValueType, *InitType, *Result.Context, *CallNode);
133 }
134
135 } // namespace clang::tidy::bugprone