/src/llvm-project/clang/lib/Sema/SemaChecking.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- SemaChecking.cpp - Extra Semantic Checking -------------------------===// |
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 | | // This file implements extra semantic analysis beyond what is enforced |
10 | | // by the C type system. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "clang/AST/APValue.h" |
15 | | #include "clang/AST/ASTContext.h" |
16 | | #include "clang/AST/Attr.h" |
17 | | #include "clang/AST/AttrIterator.h" |
18 | | #include "clang/AST/CharUnits.h" |
19 | | #include "clang/AST/Decl.h" |
20 | | #include "clang/AST/DeclBase.h" |
21 | | #include "clang/AST/DeclCXX.h" |
22 | | #include "clang/AST/DeclObjC.h" |
23 | | #include "clang/AST/DeclarationName.h" |
24 | | #include "clang/AST/EvaluatedExprVisitor.h" |
25 | | #include "clang/AST/Expr.h" |
26 | | #include "clang/AST/ExprCXX.h" |
27 | | #include "clang/AST/ExprObjC.h" |
28 | | #include "clang/AST/ExprOpenMP.h" |
29 | | #include "clang/AST/FormatString.h" |
30 | | #include "clang/AST/NSAPI.h" |
31 | | #include "clang/AST/NonTrivialTypeVisitor.h" |
32 | | #include "clang/AST/OperationKinds.h" |
33 | | #include "clang/AST/RecordLayout.h" |
34 | | #include "clang/AST/Stmt.h" |
35 | | #include "clang/AST/TemplateBase.h" |
36 | | #include "clang/AST/Type.h" |
37 | | #include "clang/AST/TypeLoc.h" |
38 | | #include "clang/AST/UnresolvedSet.h" |
39 | | #include "clang/Basic/AddressSpaces.h" |
40 | | #include "clang/Basic/CharInfo.h" |
41 | | #include "clang/Basic/Diagnostic.h" |
42 | | #include "clang/Basic/IdentifierTable.h" |
43 | | #include "clang/Basic/LLVM.h" |
44 | | #include "clang/Basic/LangOptions.h" |
45 | | #include "clang/Basic/OpenCLOptions.h" |
46 | | #include "clang/Basic/OperatorKinds.h" |
47 | | #include "clang/Basic/PartialDiagnostic.h" |
48 | | #include "clang/Basic/SourceLocation.h" |
49 | | #include "clang/Basic/SourceManager.h" |
50 | | #include "clang/Basic/Specifiers.h" |
51 | | #include "clang/Basic/SyncScope.h" |
52 | | #include "clang/Basic/TargetBuiltins.h" |
53 | | #include "clang/Basic/TargetCXXABI.h" |
54 | | #include "clang/Basic/TargetInfo.h" |
55 | | #include "clang/Basic/TypeTraits.h" |
56 | | #include "clang/Lex/Lexer.h" // TODO: Extract static functions to fix layering. |
57 | | #include "clang/Sema/Initialization.h" |
58 | | #include "clang/Sema/Lookup.h" |
59 | | #include "clang/Sema/Ownership.h" |
60 | | #include "clang/Sema/Scope.h" |
61 | | #include "clang/Sema/ScopeInfo.h" |
62 | | #include "clang/Sema/Sema.h" |
63 | | #include "clang/Sema/SemaInternal.h" |
64 | | #include "llvm/ADT/APFloat.h" |
65 | | #include "llvm/ADT/APInt.h" |
66 | | #include "llvm/ADT/APSInt.h" |
67 | | #include "llvm/ADT/ArrayRef.h" |
68 | | #include "llvm/ADT/DenseMap.h" |
69 | | #include "llvm/ADT/FoldingSet.h" |
70 | | #include "llvm/ADT/STLExtras.h" |
71 | | #include "llvm/ADT/SmallBitVector.h" |
72 | | #include "llvm/ADT/SmallPtrSet.h" |
73 | | #include "llvm/ADT/SmallString.h" |
74 | | #include "llvm/ADT/SmallVector.h" |
75 | | #include "llvm/ADT/StringExtras.h" |
76 | | #include "llvm/ADT/StringRef.h" |
77 | | #include "llvm/ADT/StringSet.h" |
78 | | #include "llvm/ADT/StringSwitch.h" |
79 | | #include "llvm/Support/AtomicOrdering.h" |
80 | | #include "llvm/Support/Casting.h" |
81 | | #include "llvm/Support/Compiler.h" |
82 | | #include "llvm/Support/ConvertUTF.h" |
83 | | #include "llvm/Support/ErrorHandling.h" |
84 | | #include "llvm/Support/Format.h" |
85 | | #include "llvm/Support/Locale.h" |
86 | | #include "llvm/Support/MathExtras.h" |
87 | | #include "llvm/Support/SaveAndRestore.h" |
88 | | #include "llvm/Support/raw_ostream.h" |
89 | | #include "llvm/TargetParser/RISCVTargetParser.h" |
90 | | #include "llvm/TargetParser/Triple.h" |
91 | | #include <algorithm> |
92 | | #include <bitset> |
93 | | #include <cassert> |
94 | | #include <cctype> |
95 | | #include <cstddef> |
96 | | #include <cstdint> |
97 | | #include <functional> |
98 | | #include <limits> |
99 | | #include <optional> |
100 | | #include <string> |
101 | | #include <tuple> |
102 | | #include <utility> |
103 | | |
104 | | using namespace clang; |
105 | | using namespace sema; |
106 | | |
107 | | SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, |
108 | 0 | unsigned ByteNo) const { |
109 | 0 | return SL->getLocationOfByte(ByteNo, getSourceManager(), LangOpts, |
110 | 0 | Context.getTargetInfo()); |
111 | 0 | } |
112 | | |
113 | | static constexpr unsigned short combineFAPK(Sema::FormatArgumentPassingKind A, |
114 | 0 | Sema::FormatArgumentPassingKind B) { |
115 | 0 | return (A << 8) | B; |
116 | 0 | } |
117 | | |
118 | | /// Checks that a call expression's argument count is at least the desired |
119 | | /// number. This is useful when doing custom type-checking on a variadic |
120 | | /// function. Returns true on error. |
121 | | static bool checkArgCountAtLeast(Sema &S, CallExpr *Call, |
122 | 0 | unsigned MinArgCount) { |
123 | 0 | unsigned ArgCount = Call->getNumArgs(); |
124 | 0 | if (ArgCount >= MinArgCount) |
125 | 0 | return false; |
126 | | |
127 | 0 | return S.Diag(Call->getEndLoc(), diag::err_typecheck_call_too_few_args) |
128 | 0 | << 0 /*function call*/ << MinArgCount << ArgCount |
129 | 0 | << /*is non object*/ 0 << Call->getSourceRange(); |
130 | 0 | } |
131 | | |
132 | | /// Checks that a call expression's argument count is at most the desired |
133 | | /// number. This is useful when doing custom type-checking on a variadic |
134 | | /// function. Returns true on error. |
135 | 0 | static bool checkArgCountAtMost(Sema &S, CallExpr *Call, unsigned MaxArgCount) { |
136 | 0 | unsigned ArgCount = Call->getNumArgs(); |
137 | 0 | if (ArgCount <= MaxArgCount) |
138 | 0 | return false; |
139 | 0 | return S.Diag(Call->getEndLoc(), |
140 | 0 | diag::err_typecheck_call_too_many_args_at_most) |
141 | 0 | << 0 /*function call*/ << MaxArgCount << ArgCount |
142 | 0 | << /*is non object*/ 0 << Call->getSourceRange(); |
143 | 0 | } |
144 | | |
145 | | /// Checks that a call expression's argument count is in the desired range. This |
146 | | /// is useful when doing custom type-checking on a variadic function. Returns |
147 | | /// true on error. |
148 | | static bool checkArgCountRange(Sema &S, CallExpr *Call, unsigned MinArgCount, |
149 | 0 | unsigned MaxArgCount) { |
150 | 0 | return checkArgCountAtLeast(S, Call, MinArgCount) || |
151 | 0 | checkArgCountAtMost(S, Call, MaxArgCount); |
152 | 0 | } |
153 | | |
154 | | /// Checks that a call expression's argument count is the desired number. |
155 | | /// This is useful when doing custom type-checking. Returns true on error. |
156 | 0 | static bool checkArgCount(Sema &S, CallExpr *Call, unsigned DesiredArgCount) { |
157 | 0 | unsigned ArgCount = Call->getNumArgs(); |
158 | 0 | if (ArgCount == DesiredArgCount) |
159 | 0 | return false; |
160 | | |
161 | 0 | if (checkArgCountAtLeast(S, Call, DesiredArgCount)) |
162 | 0 | return true; |
163 | 0 | assert(ArgCount > DesiredArgCount && "should have diagnosed this"); |
164 | | |
165 | | // Highlight all the excess arguments. |
166 | 0 | SourceRange Range(Call->getArg(DesiredArgCount)->getBeginLoc(), |
167 | 0 | Call->getArg(ArgCount - 1)->getEndLoc()); |
168 | |
|
169 | 0 | return S.Diag(Range.getBegin(), diag::err_typecheck_call_too_many_args) |
170 | 0 | << 0 /*function call*/ << DesiredArgCount << ArgCount |
171 | 0 | << /*is non object*/ 0 << Call->getArg(1)->getSourceRange(); |
172 | 0 | } |
173 | | |
174 | 0 | static bool convertArgumentToType(Sema &S, Expr *&Value, QualType Ty) { |
175 | 0 | if (Value->isTypeDependent()) |
176 | 0 | return false; |
177 | | |
178 | 0 | InitializedEntity Entity = |
179 | 0 | InitializedEntity::InitializeParameter(S.Context, Ty, false); |
180 | 0 | ExprResult Result = |
181 | 0 | S.PerformCopyInitialization(Entity, SourceLocation(), Value); |
182 | 0 | if (Result.isInvalid()) |
183 | 0 | return true; |
184 | 0 | Value = Result.get(); |
185 | 0 | return false; |
186 | 0 | } |
187 | | |
188 | | /// Check that the first argument to __builtin_annotation is an integer |
189 | | /// and the second argument is a non-wide string literal. |
190 | 0 | static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) { |
191 | 0 | if (checkArgCount(S, TheCall, 2)) |
192 | 0 | return true; |
193 | | |
194 | | // First argument should be an integer. |
195 | 0 | Expr *ValArg = TheCall->getArg(0); |
196 | 0 | QualType Ty = ValArg->getType(); |
197 | 0 | if (!Ty->isIntegerType()) { |
198 | 0 | S.Diag(ValArg->getBeginLoc(), diag::err_builtin_annotation_first_arg) |
199 | 0 | << ValArg->getSourceRange(); |
200 | 0 | return true; |
201 | 0 | } |
202 | | |
203 | | // Second argument should be a constant string. |
204 | 0 | Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts(); |
205 | 0 | StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg); |
206 | 0 | if (!Literal || !Literal->isOrdinary()) { |
207 | 0 | S.Diag(StrArg->getBeginLoc(), diag::err_builtin_annotation_second_arg) |
208 | 0 | << StrArg->getSourceRange(); |
209 | 0 | return true; |
210 | 0 | } |
211 | | |
212 | 0 | TheCall->setType(Ty); |
213 | 0 | return false; |
214 | 0 | } |
215 | | |
216 | 0 | static bool SemaBuiltinMSVCAnnotation(Sema &S, CallExpr *TheCall) { |
217 | | // We need at least one argument. |
218 | 0 | if (TheCall->getNumArgs() < 1) { |
219 | 0 | S.Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least) |
220 | 0 | << 0 << 1 << TheCall->getNumArgs() << /*is non object*/ 0 |
221 | 0 | << TheCall->getCallee()->getSourceRange(); |
222 | 0 | return true; |
223 | 0 | } |
224 | | |
225 | | // All arguments should be wide string literals. |
226 | 0 | for (Expr *Arg : TheCall->arguments()) { |
227 | 0 | auto *Literal = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
228 | 0 | if (!Literal || !Literal->isWide()) { |
229 | 0 | S.Diag(Arg->getBeginLoc(), diag::err_msvc_annotation_wide_str) |
230 | 0 | << Arg->getSourceRange(); |
231 | 0 | return true; |
232 | 0 | } |
233 | 0 | } |
234 | | |
235 | 0 | return false; |
236 | 0 | } |
237 | | |
238 | | /// Check that the argument to __builtin_addressof is a glvalue, and set the |
239 | | /// result type to the corresponding pointer type. |
240 | 0 | static bool SemaBuiltinAddressof(Sema &S, CallExpr *TheCall) { |
241 | 0 | if (checkArgCount(S, TheCall, 1)) |
242 | 0 | return true; |
243 | | |
244 | 0 | ExprResult Arg(TheCall->getArg(0)); |
245 | 0 | QualType ResultType = S.CheckAddressOfOperand(Arg, TheCall->getBeginLoc()); |
246 | 0 | if (ResultType.isNull()) |
247 | 0 | return true; |
248 | | |
249 | 0 | TheCall->setArg(0, Arg.get()); |
250 | 0 | TheCall->setType(ResultType); |
251 | 0 | return false; |
252 | 0 | } |
253 | | |
254 | | /// Check that the argument to __builtin_function_start is a function. |
255 | 0 | static bool SemaBuiltinFunctionStart(Sema &S, CallExpr *TheCall) { |
256 | 0 | if (checkArgCount(S, TheCall, 1)) |
257 | 0 | return true; |
258 | | |
259 | 0 | ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(0)); |
260 | 0 | if (Arg.isInvalid()) |
261 | 0 | return true; |
262 | | |
263 | 0 | TheCall->setArg(0, Arg.get()); |
264 | 0 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>( |
265 | 0 | Arg.get()->getAsBuiltinConstantDeclRef(S.getASTContext())); |
266 | |
|
267 | 0 | if (!FD) { |
268 | 0 | S.Diag(TheCall->getBeginLoc(), diag::err_function_start_invalid_type) |
269 | 0 | << TheCall->getSourceRange(); |
270 | 0 | return true; |
271 | 0 | } |
272 | | |
273 | 0 | return !S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, |
274 | 0 | TheCall->getBeginLoc()); |
275 | 0 | } |
276 | | |
277 | | /// Check the number of arguments and set the result type to |
278 | | /// the argument type. |
279 | 0 | static bool SemaBuiltinPreserveAI(Sema &S, CallExpr *TheCall) { |
280 | 0 | if (checkArgCount(S, TheCall, 1)) |
281 | 0 | return true; |
282 | | |
283 | 0 | TheCall->setType(TheCall->getArg(0)->getType()); |
284 | 0 | return false; |
285 | 0 | } |
286 | | |
287 | | /// Check that the value argument for __builtin_is_aligned(value, alignment) and |
288 | | /// __builtin_aligned_{up,down}(value, alignment) is an integer or a pointer |
289 | | /// type (but not a function pointer) and that the alignment is a power-of-two. |
290 | 0 | static bool SemaBuiltinAlignment(Sema &S, CallExpr *TheCall, unsigned ID) { |
291 | 0 | if (checkArgCount(S, TheCall, 2)) |
292 | 0 | return true; |
293 | | |
294 | 0 | clang::Expr *Source = TheCall->getArg(0); |
295 | 0 | bool IsBooleanAlignBuiltin = ID == Builtin::BI__builtin_is_aligned; |
296 | |
|
297 | 0 | auto IsValidIntegerType = [](QualType Ty) { |
298 | 0 | return Ty->isIntegerType() && !Ty->isEnumeralType() && !Ty->isBooleanType(); |
299 | 0 | }; |
300 | 0 | QualType SrcTy = Source->getType(); |
301 | | // We should also be able to use it with arrays (but not functions!). |
302 | 0 | if (SrcTy->canDecayToPointerType() && SrcTy->isArrayType()) { |
303 | 0 | SrcTy = S.Context.getDecayedType(SrcTy); |
304 | 0 | } |
305 | 0 | if ((!SrcTy->isPointerType() && !IsValidIntegerType(SrcTy)) || |
306 | 0 | SrcTy->isFunctionPointerType()) { |
307 | | // FIXME: this is not quite the right error message since we don't allow |
308 | | // floating point types, or member pointers. |
309 | 0 | S.Diag(Source->getExprLoc(), diag::err_typecheck_expect_scalar_operand) |
310 | 0 | << SrcTy; |
311 | 0 | return true; |
312 | 0 | } |
313 | | |
314 | 0 | clang::Expr *AlignOp = TheCall->getArg(1); |
315 | 0 | if (!IsValidIntegerType(AlignOp->getType())) { |
316 | 0 | S.Diag(AlignOp->getExprLoc(), diag::err_typecheck_expect_int) |
317 | 0 | << AlignOp->getType(); |
318 | 0 | return true; |
319 | 0 | } |
320 | 0 | Expr::EvalResult AlignResult; |
321 | 0 | unsigned MaxAlignmentBits = S.Context.getIntWidth(SrcTy) - 1; |
322 | | // We can't check validity of alignment if it is value dependent. |
323 | 0 | if (!AlignOp->isValueDependent() && |
324 | 0 | AlignOp->EvaluateAsInt(AlignResult, S.Context, |
325 | 0 | Expr::SE_AllowSideEffects)) { |
326 | 0 | llvm::APSInt AlignValue = AlignResult.Val.getInt(); |
327 | 0 | llvm::APSInt MaxValue( |
328 | 0 | llvm::APInt::getOneBitSet(MaxAlignmentBits + 1, MaxAlignmentBits)); |
329 | 0 | if (AlignValue < 1) { |
330 | 0 | S.Diag(AlignOp->getExprLoc(), diag::err_alignment_too_small) << 1; |
331 | 0 | return true; |
332 | 0 | } |
333 | 0 | if (llvm::APSInt::compareValues(AlignValue, MaxValue) > 0) { |
334 | 0 | S.Diag(AlignOp->getExprLoc(), diag::err_alignment_too_big) |
335 | 0 | << toString(MaxValue, 10); |
336 | 0 | return true; |
337 | 0 | } |
338 | 0 | if (!AlignValue.isPowerOf2()) { |
339 | 0 | S.Diag(AlignOp->getExprLoc(), diag::err_alignment_not_power_of_two); |
340 | 0 | return true; |
341 | 0 | } |
342 | 0 | if (AlignValue == 1) { |
343 | 0 | S.Diag(AlignOp->getExprLoc(), diag::warn_alignment_builtin_useless) |
344 | 0 | << IsBooleanAlignBuiltin; |
345 | 0 | } |
346 | 0 | } |
347 | | |
348 | 0 | ExprResult SrcArg = S.PerformCopyInitialization( |
349 | 0 | InitializedEntity::InitializeParameter(S.Context, SrcTy, false), |
350 | 0 | SourceLocation(), Source); |
351 | 0 | if (SrcArg.isInvalid()) |
352 | 0 | return true; |
353 | 0 | TheCall->setArg(0, SrcArg.get()); |
354 | 0 | ExprResult AlignArg = |
355 | 0 | S.PerformCopyInitialization(InitializedEntity::InitializeParameter( |
356 | 0 | S.Context, AlignOp->getType(), false), |
357 | 0 | SourceLocation(), AlignOp); |
358 | 0 | if (AlignArg.isInvalid()) |
359 | 0 | return true; |
360 | 0 | TheCall->setArg(1, AlignArg.get()); |
361 | | // For align_up/align_down, the return type is the same as the (potentially |
362 | | // decayed) argument type including qualifiers. For is_aligned(), the result |
363 | | // is always bool. |
364 | 0 | TheCall->setType(IsBooleanAlignBuiltin ? S.Context.BoolTy : SrcTy); |
365 | 0 | return false; |
366 | 0 | } |
367 | | |
368 | | static bool SemaBuiltinOverflow(Sema &S, CallExpr *TheCall, |
369 | 0 | unsigned BuiltinID) { |
370 | 0 | if (checkArgCount(S, TheCall, 3)) |
371 | 0 | return true; |
372 | | |
373 | 0 | std::pair<unsigned, const char *> Builtins[] = { |
374 | 0 | { Builtin::BI__builtin_add_overflow, "ckd_add" }, |
375 | 0 | { Builtin::BI__builtin_sub_overflow, "ckd_sub" }, |
376 | 0 | { Builtin::BI__builtin_mul_overflow, "ckd_mul" }, |
377 | 0 | }; |
378 | |
|
379 | 0 | bool CkdOperation = llvm::any_of(Builtins, [&](const std::pair<unsigned, |
380 | 0 | const char *> &P) { |
381 | 0 | return BuiltinID == P.first && TheCall->getExprLoc().isMacroID() && |
382 | 0 | Lexer::getImmediateMacroName(TheCall->getExprLoc(), |
383 | 0 | S.getSourceManager(), S.getLangOpts()) == P.second; |
384 | 0 | }); |
385 | |
|
386 | 0 | auto ValidCkdIntType = [](QualType QT) { |
387 | | // A valid checked integer type is an integer type other than a plain char, |
388 | | // bool, a bit-precise type, or an enumeration type. |
389 | 0 | if (const auto *BT = QT.getCanonicalType()->getAs<BuiltinType>()) |
390 | 0 | return (BT->getKind() >= BuiltinType::Short && |
391 | 0 | BT->getKind() <= BuiltinType::Int128) || ( |
392 | 0 | BT->getKind() >= BuiltinType::UShort && |
393 | 0 | BT->getKind() <= BuiltinType::UInt128) || |
394 | 0 | BT->getKind() == BuiltinType::UChar || |
395 | 0 | BT->getKind() == BuiltinType::SChar; |
396 | 0 | return false; |
397 | 0 | }; |
398 | | |
399 | | // First two arguments should be integers. |
400 | 0 | for (unsigned I = 0; I < 2; ++I) { |
401 | 0 | ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(I)); |
402 | 0 | if (Arg.isInvalid()) return true; |
403 | 0 | TheCall->setArg(I, Arg.get()); |
404 | |
|
405 | 0 | QualType Ty = Arg.get()->getType(); |
406 | 0 | bool IsValid = CkdOperation ? ValidCkdIntType(Ty) : Ty->isIntegerType(); |
407 | 0 | if (!IsValid) { |
408 | 0 | S.Diag(Arg.get()->getBeginLoc(), diag::err_overflow_builtin_must_be_int) |
409 | 0 | << CkdOperation << Ty << Arg.get()->getSourceRange(); |
410 | 0 | return true; |
411 | 0 | } |
412 | 0 | } |
413 | | |
414 | | // Third argument should be a pointer to a non-const integer. |
415 | | // IRGen correctly handles volatile, restrict, and address spaces, and |
416 | | // the other qualifiers aren't possible. |
417 | 0 | { |
418 | 0 | ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(2)); |
419 | 0 | if (Arg.isInvalid()) return true; |
420 | 0 | TheCall->setArg(2, Arg.get()); |
421 | |
|
422 | 0 | QualType Ty = Arg.get()->getType(); |
423 | 0 | const auto *PtrTy = Ty->getAs<PointerType>(); |
424 | 0 | if (!PtrTy || |
425 | 0 | !PtrTy->getPointeeType()->isIntegerType() || |
426 | 0 | (!ValidCkdIntType(PtrTy->getPointeeType()) && CkdOperation) || |
427 | 0 | PtrTy->getPointeeType().isConstQualified()) { |
428 | 0 | S.Diag(Arg.get()->getBeginLoc(), |
429 | 0 | diag::err_overflow_builtin_must_be_ptr_int) |
430 | 0 | << CkdOperation << Ty << Arg.get()->getSourceRange(); |
431 | 0 | return true; |
432 | 0 | } |
433 | 0 | } |
434 | | |
435 | | // Disallow signed bit-precise integer args larger than 128 bits to mul |
436 | | // function until we improve backend support. |
437 | 0 | if (BuiltinID == Builtin::BI__builtin_mul_overflow) { |
438 | 0 | for (unsigned I = 0; I < 3; ++I) { |
439 | 0 | const auto Arg = TheCall->getArg(I); |
440 | | // Third argument will be a pointer. |
441 | 0 | auto Ty = I < 2 ? Arg->getType() : Arg->getType()->getPointeeType(); |
442 | 0 | if (Ty->isBitIntType() && Ty->isSignedIntegerType() && |
443 | 0 | S.getASTContext().getIntWidth(Ty) > 128) |
444 | 0 | return S.Diag(Arg->getBeginLoc(), |
445 | 0 | diag::err_overflow_builtin_bit_int_max_size) |
446 | 0 | << 128; |
447 | 0 | } |
448 | 0 | } |
449 | | |
450 | 0 | return false; |
451 | 0 | } |
452 | | |
453 | | namespace { |
454 | | struct BuiltinDumpStructGenerator { |
455 | | Sema &S; |
456 | | CallExpr *TheCall; |
457 | | SourceLocation Loc = TheCall->getBeginLoc(); |
458 | | SmallVector<Expr *, 32> Actions; |
459 | | DiagnosticErrorTrap ErrorTracker; |
460 | | PrintingPolicy Policy; |
461 | | |
462 | | BuiltinDumpStructGenerator(Sema &S, CallExpr *TheCall) |
463 | | : S(S), TheCall(TheCall), ErrorTracker(S.getDiagnostics()), |
464 | 0 | Policy(S.Context.getPrintingPolicy()) { |
465 | 0 | Policy.AnonymousTagLocations = false; |
466 | 0 | } |
467 | | |
468 | 0 | Expr *makeOpaqueValueExpr(Expr *Inner) { |
469 | 0 | auto *OVE = new (S.Context) |
470 | 0 | OpaqueValueExpr(Loc, Inner->getType(), Inner->getValueKind(), |
471 | 0 | Inner->getObjectKind(), Inner); |
472 | 0 | Actions.push_back(OVE); |
473 | 0 | return OVE; |
474 | 0 | } |
475 | | |
476 | 0 | Expr *getStringLiteral(llvm::StringRef Str) { |
477 | 0 | Expr *Lit = S.Context.getPredefinedStringLiteralFromCache(Str); |
478 | | // Wrap the literal in parentheses to attach a source location. |
479 | 0 | return new (S.Context) ParenExpr(Loc, Loc, Lit); |
480 | 0 | } |
481 | | |
482 | | bool callPrintFunction(llvm::StringRef Format, |
483 | 0 | llvm::ArrayRef<Expr *> Exprs = {}) { |
484 | 0 | SmallVector<Expr *, 8> Args; |
485 | 0 | assert(TheCall->getNumArgs() >= 2); |
486 | 0 | Args.reserve((TheCall->getNumArgs() - 2) + /*Format*/ 1 + Exprs.size()); |
487 | 0 | Args.assign(TheCall->arg_begin() + 2, TheCall->arg_end()); |
488 | 0 | Args.push_back(getStringLiteral(Format)); |
489 | 0 | Args.insert(Args.end(), Exprs.begin(), Exprs.end()); |
490 | | |
491 | | // Register a note to explain why we're performing the call. |
492 | 0 | Sema::CodeSynthesisContext Ctx; |
493 | 0 | Ctx.Kind = Sema::CodeSynthesisContext::BuildingBuiltinDumpStructCall; |
494 | 0 | Ctx.PointOfInstantiation = Loc; |
495 | 0 | Ctx.CallArgs = Args.data(); |
496 | 0 | Ctx.NumCallArgs = Args.size(); |
497 | 0 | S.pushCodeSynthesisContext(Ctx); |
498 | |
|
499 | 0 | ExprResult RealCall = |
500 | 0 | S.BuildCallExpr(/*Scope=*/nullptr, TheCall->getArg(1), |
501 | 0 | TheCall->getBeginLoc(), Args, TheCall->getRParenLoc()); |
502 | |
|
503 | 0 | S.popCodeSynthesisContext(); |
504 | 0 | if (!RealCall.isInvalid()) |
505 | 0 | Actions.push_back(RealCall.get()); |
506 | | // Bail out if we've hit any errors, even if we managed to build the |
507 | | // call. We don't want to produce more than one error. |
508 | 0 | return RealCall.isInvalid() || ErrorTracker.hasErrorOccurred(); |
509 | 0 | } |
510 | | |
511 | 0 | Expr *getIndentString(unsigned Depth) { |
512 | 0 | if (!Depth) |
513 | 0 | return nullptr; |
514 | | |
515 | 0 | llvm::SmallString<32> Indent; |
516 | 0 | Indent.resize(Depth * Policy.Indentation, ' '); |
517 | 0 | return getStringLiteral(Indent); |
518 | 0 | } |
519 | | |
520 | 0 | Expr *getTypeString(QualType T) { |
521 | 0 | return getStringLiteral(T.getAsString(Policy)); |
522 | 0 | } |
523 | | |
524 | 0 | bool appendFormatSpecifier(QualType T, llvm::SmallVectorImpl<char> &Str) { |
525 | 0 | llvm::raw_svector_ostream OS(Str); |
526 | | |
527 | | // Format 'bool', 'char', 'signed char', 'unsigned char' as numbers, rather |
528 | | // than trying to print a single character. |
529 | 0 | if (auto *BT = T->getAs<BuiltinType>()) { |
530 | 0 | switch (BT->getKind()) { |
531 | 0 | case BuiltinType::Bool: |
532 | 0 | OS << "%d"; |
533 | 0 | return true; |
534 | 0 | case BuiltinType::Char_U: |
535 | 0 | case BuiltinType::UChar: |
536 | 0 | OS << "%hhu"; |
537 | 0 | return true; |
538 | 0 | case BuiltinType::Char_S: |
539 | 0 | case BuiltinType::SChar: |
540 | 0 | OS << "%hhd"; |
541 | 0 | return true; |
542 | 0 | default: |
543 | 0 | break; |
544 | 0 | } |
545 | 0 | } |
546 | | |
547 | 0 | analyze_printf::PrintfSpecifier Specifier; |
548 | 0 | if (Specifier.fixType(T, S.getLangOpts(), S.Context, /*IsObjCLiteral=*/false)) { |
549 | | // We were able to guess how to format this. |
550 | 0 | if (Specifier.getConversionSpecifier().getKind() == |
551 | 0 | analyze_printf::PrintfConversionSpecifier::sArg) { |
552 | | // Wrap double-quotes around a '%s' specifier and limit its maximum |
553 | | // length. Ideally we'd also somehow escape special characters in the |
554 | | // contents but printf doesn't support that. |
555 | | // FIXME: '%s' formatting is not safe in general. |
556 | 0 | OS << '"'; |
557 | 0 | Specifier.setPrecision(analyze_printf::OptionalAmount(32u)); |
558 | 0 | Specifier.toString(OS); |
559 | 0 | OS << '"'; |
560 | | // FIXME: It would be nice to include a '...' if the string doesn't fit |
561 | | // in the length limit. |
562 | 0 | } else { |
563 | 0 | Specifier.toString(OS); |
564 | 0 | } |
565 | 0 | return true; |
566 | 0 | } |
567 | | |
568 | 0 | if (T->isPointerType()) { |
569 | | // Format all pointers with '%p'. |
570 | 0 | OS << "%p"; |
571 | 0 | return true; |
572 | 0 | } |
573 | | |
574 | 0 | return false; |
575 | 0 | } |
576 | | |
577 | 0 | bool dumpUnnamedRecord(const RecordDecl *RD, Expr *E, unsigned Depth) { |
578 | 0 | Expr *IndentLit = getIndentString(Depth); |
579 | 0 | Expr *TypeLit = getTypeString(S.Context.getRecordType(RD)); |
580 | 0 | if (IndentLit ? callPrintFunction("%s%s", {IndentLit, TypeLit}) |
581 | 0 | : callPrintFunction("%s", {TypeLit})) |
582 | 0 | return true; |
583 | | |
584 | 0 | return dumpRecordValue(RD, E, IndentLit, Depth); |
585 | 0 | } |
586 | | |
587 | | // Dump a record value. E should be a pointer or lvalue referring to an RD. |
588 | | bool dumpRecordValue(const RecordDecl *RD, Expr *E, Expr *RecordIndent, |
589 | 0 | unsigned Depth) { |
590 | | // FIXME: Decide what to do if RD is a union. At least we should probably |
591 | | // turn off printing `const char*` members with `%s`, because that is very |
592 | | // likely to crash if that's not the active member. Whatever we decide, we |
593 | | // should document it. |
594 | | |
595 | | // Build an OpaqueValueExpr so we can refer to E more than once without |
596 | | // triggering re-evaluation. |
597 | 0 | Expr *RecordArg = makeOpaqueValueExpr(E); |
598 | 0 | bool RecordArgIsPtr = RecordArg->getType()->isPointerType(); |
599 | |
|
600 | 0 | if (callPrintFunction(" {\n")) |
601 | 0 | return true; |
602 | | |
603 | | // Dump each base class, regardless of whether they're aggregates. |
604 | 0 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
605 | 0 | for (const auto &Base : CXXRD->bases()) { |
606 | 0 | QualType BaseType = |
607 | 0 | RecordArgIsPtr ? S.Context.getPointerType(Base.getType()) |
608 | 0 | : S.Context.getLValueReferenceType(Base.getType()); |
609 | 0 | ExprResult BasePtr = S.BuildCStyleCastExpr( |
610 | 0 | Loc, S.Context.getTrivialTypeSourceInfo(BaseType, Loc), Loc, |
611 | 0 | RecordArg); |
612 | 0 | if (BasePtr.isInvalid() || |
613 | 0 | dumpUnnamedRecord(Base.getType()->getAsRecordDecl(), BasePtr.get(), |
614 | 0 | Depth + 1)) |
615 | 0 | return true; |
616 | 0 | } |
617 | 0 | } |
618 | | |
619 | 0 | Expr *FieldIndentArg = getIndentString(Depth + 1); |
620 | | |
621 | | // Dump each field. |
622 | 0 | for (auto *D : RD->decls()) { |
623 | 0 | auto *IFD = dyn_cast<IndirectFieldDecl>(D); |
624 | 0 | auto *FD = IFD ? IFD->getAnonField() : dyn_cast<FieldDecl>(D); |
625 | 0 | if (!FD || FD->isUnnamedBitfield() || FD->isAnonymousStructOrUnion()) |
626 | 0 | continue; |
627 | | |
628 | 0 | llvm::SmallString<20> Format = llvm::StringRef("%s%s %s "); |
629 | 0 | llvm::SmallVector<Expr *, 5> Args = {FieldIndentArg, |
630 | 0 | getTypeString(FD->getType()), |
631 | 0 | getStringLiteral(FD->getName())}; |
632 | |
|
633 | 0 | if (FD->isBitField()) { |
634 | 0 | Format += ": %zu "; |
635 | 0 | QualType SizeT = S.Context.getSizeType(); |
636 | 0 | llvm::APInt BitWidth(S.Context.getIntWidth(SizeT), |
637 | 0 | FD->getBitWidthValue(S.Context)); |
638 | 0 | Args.push_back(IntegerLiteral::Create(S.Context, BitWidth, SizeT, Loc)); |
639 | 0 | } |
640 | |
|
641 | 0 | Format += "="; |
642 | |
|
643 | 0 | ExprResult Field = |
644 | 0 | IFD ? S.BuildAnonymousStructUnionMemberReference( |
645 | 0 | CXXScopeSpec(), Loc, IFD, |
646 | 0 | DeclAccessPair::make(IFD, AS_public), RecordArg, Loc) |
647 | 0 | : S.BuildFieldReferenceExpr( |
648 | 0 | RecordArg, RecordArgIsPtr, Loc, CXXScopeSpec(), FD, |
649 | 0 | DeclAccessPair::make(FD, AS_public), |
650 | 0 | DeclarationNameInfo(FD->getDeclName(), Loc)); |
651 | 0 | if (Field.isInvalid()) |
652 | 0 | return true; |
653 | | |
654 | 0 | auto *InnerRD = FD->getType()->getAsRecordDecl(); |
655 | 0 | auto *InnerCXXRD = dyn_cast_or_null<CXXRecordDecl>(InnerRD); |
656 | 0 | if (InnerRD && (!InnerCXXRD || InnerCXXRD->isAggregate())) { |
657 | | // Recursively print the values of members of aggregate record type. |
658 | 0 | if (callPrintFunction(Format, Args) || |
659 | 0 | dumpRecordValue(InnerRD, Field.get(), FieldIndentArg, Depth + 1)) |
660 | 0 | return true; |
661 | 0 | } else { |
662 | 0 | Format += " "; |
663 | 0 | if (appendFormatSpecifier(FD->getType(), Format)) { |
664 | | // We know how to print this field. |
665 | 0 | Args.push_back(Field.get()); |
666 | 0 | } else { |
667 | | // We don't know how to print this field. Print out its address |
668 | | // with a format specifier that a smart tool will be able to |
669 | | // recognize and treat specially. |
670 | 0 | Format += "*%p"; |
671 | 0 | ExprResult FieldAddr = |
672 | 0 | S.BuildUnaryOp(nullptr, Loc, UO_AddrOf, Field.get()); |
673 | 0 | if (FieldAddr.isInvalid()) |
674 | 0 | return true; |
675 | 0 | Args.push_back(FieldAddr.get()); |
676 | 0 | } |
677 | 0 | Format += "\n"; |
678 | 0 | if (callPrintFunction(Format, Args)) |
679 | 0 | return true; |
680 | 0 | } |
681 | 0 | } |
682 | | |
683 | 0 | return RecordIndent ? callPrintFunction("%s}\n", RecordIndent) |
684 | 0 | : callPrintFunction("}\n"); |
685 | 0 | } |
686 | | |
687 | 0 | Expr *buildWrapper() { |
688 | 0 | auto *Wrapper = PseudoObjectExpr::Create(S.Context, TheCall, Actions, |
689 | 0 | PseudoObjectExpr::NoResult); |
690 | 0 | TheCall->setType(Wrapper->getType()); |
691 | 0 | TheCall->setValueKind(Wrapper->getValueKind()); |
692 | 0 | return Wrapper; |
693 | 0 | } |
694 | | }; |
695 | | } // namespace |
696 | | |
697 | 0 | static ExprResult SemaBuiltinDumpStruct(Sema &S, CallExpr *TheCall) { |
698 | 0 | if (checkArgCountAtLeast(S, TheCall, 2)) |
699 | 0 | return ExprError(); |
700 | | |
701 | 0 | ExprResult PtrArgResult = S.DefaultLvalueConversion(TheCall->getArg(0)); |
702 | 0 | if (PtrArgResult.isInvalid()) |
703 | 0 | return ExprError(); |
704 | 0 | TheCall->setArg(0, PtrArgResult.get()); |
705 | | |
706 | | // First argument should be a pointer to a struct. |
707 | 0 | QualType PtrArgType = PtrArgResult.get()->getType(); |
708 | 0 | if (!PtrArgType->isPointerType() || |
709 | 0 | !PtrArgType->getPointeeType()->isRecordType()) { |
710 | 0 | S.Diag(PtrArgResult.get()->getBeginLoc(), |
711 | 0 | diag::err_expected_struct_pointer_argument) |
712 | 0 | << 1 << TheCall->getDirectCallee() << PtrArgType; |
713 | 0 | return ExprError(); |
714 | 0 | } |
715 | 0 | QualType Pointee = PtrArgType->getPointeeType(); |
716 | 0 | const RecordDecl *RD = Pointee->getAsRecordDecl(); |
717 | | // Try to instantiate the class template as appropriate; otherwise, access to |
718 | | // its data() may lead to a crash. |
719 | 0 | if (S.RequireCompleteType(PtrArgResult.get()->getBeginLoc(), Pointee, |
720 | 0 | diag::err_incomplete_type)) |
721 | 0 | return ExprError(); |
722 | | // Second argument is a callable, but we can't fully validate it until we try |
723 | | // calling it. |
724 | 0 | QualType FnArgType = TheCall->getArg(1)->getType(); |
725 | 0 | if (!FnArgType->isFunctionType() && !FnArgType->isFunctionPointerType() && |
726 | 0 | !FnArgType->isBlockPointerType() && |
727 | 0 | !(S.getLangOpts().CPlusPlus && FnArgType->isRecordType())) { |
728 | 0 | auto *BT = FnArgType->getAs<BuiltinType>(); |
729 | 0 | switch (BT ? BT->getKind() : BuiltinType::Void) { |
730 | 0 | case BuiltinType::Dependent: |
731 | 0 | case BuiltinType::Overload: |
732 | 0 | case BuiltinType::BoundMember: |
733 | 0 | case BuiltinType::PseudoObject: |
734 | 0 | case BuiltinType::UnknownAny: |
735 | 0 | case BuiltinType::BuiltinFn: |
736 | | // This might be a callable. |
737 | 0 | break; |
738 | | |
739 | 0 | default: |
740 | 0 | S.Diag(TheCall->getArg(1)->getBeginLoc(), |
741 | 0 | diag::err_expected_callable_argument) |
742 | 0 | << 2 << TheCall->getDirectCallee() << FnArgType; |
743 | 0 | return ExprError(); |
744 | 0 | } |
745 | 0 | } |
746 | | |
747 | 0 | BuiltinDumpStructGenerator Generator(S, TheCall); |
748 | | |
749 | | // Wrap parentheses around the given pointer. This is not necessary for |
750 | | // correct code generation, but it means that when we pretty-print the call |
751 | | // arguments in our diagnostics we will produce '(&s)->n' instead of the |
752 | | // incorrect '&s->n'. |
753 | 0 | Expr *PtrArg = PtrArgResult.get(); |
754 | 0 | PtrArg = new (S.Context) |
755 | 0 | ParenExpr(PtrArg->getBeginLoc(), |
756 | 0 | S.getLocForEndOfToken(PtrArg->getEndLoc()), PtrArg); |
757 | 0 | if (Generator.dumpUnnamedRecord(RD, PtrArg, 0)) |
758 | 0 | return ExprError(); |
759 | | |
760 | 0 | return Generator.buildWrapper(); |
761 | 0 | } |
762 | | |
763 | 0 | static bool SemaBuiltinCallWithStaticChain(Sema &S, CallExpr *BuiltinCall) { |
764 | 0 | if (checkArgCount(S, BuiltinCall, 2)) |
765 | 0 | return true; |
766 | | |
767 | 0 | SourceLocation BuiltinLoc = BuiltinCall->getBeginLoc(); |
768 | 0 | Expr *Builtin = BuiltinCall->getCallee()->IgnoreImpCasts(); |
769 | 0 | Expr *Call = BuiltinCall->getArg(0); |
770 | 0 | Expr *Chain = BuiltinCall->getArg(1); |
771 | |
|
772 | 0 | if (Call->getStmtClass() != Stmt::CallExprClass) { |
773 | 0 | S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_not_call) |
774 | 0 | << Call->getSourceRange(); |
775 | 0 | return true; |
776 | 0 | } |
777 | | |
778 | 0 | auto CE = cast<CallExpr>(Call); |
779 | 0 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
780 | 0 | S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_block_call) |
781 | 0 | << Call->getSourceRange(); |
782 | 0 | return true; |
783 | 0 | } |
784 | | |
785 | 0 | const Decl *TargetDecl = CE->getCalleeDecl(); |
786 | 0 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) |
787 | 0 | if (FD->getBuiltinID()) { |
788 | 0 | S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_builtin_call) |
789 | 0 | << Call->getSourceRange(); |
790 | 0 | return true; |
791 | 0 | } |
792 | | |
793 | 0 | if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens())) { |
794 | 0 | S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_pdtor_call) |
795 | 0 | << Call->getSourceRange(); |
796 | 0 | return true; |
797 | 0 | } |
798 | | |
799 | 0 | ExprResult ChainResult = S.UsualUnaryConversions(Chain); |
800 | 0 | if (ChainResult.isInvalid()) |
801 | 0 | return true; |
802 | 0 | if (!ChainResult.get()->getType()->isPointerType()) { |
803 | 0 | S.Diag(BuiltinLoc, diag::err_second_argument_to_cwsc_not_pointer) |
804 | 0 | << Chain->getSourceRange(); |
805 | 0 | return true; |
806 | 0 | } |
807 | | |
808 | 0 | QualType ReturnTy = CE->getCallReturnType(S.Context); |
809 | 0 | QualType ArgTys[2] = { ReturnTy, ChainResult.get()->getType() }; |
810 | 0 | QualType BuiltinTy = S.Context.getFunctionType( |
811 | 0 | ReturnTy, ArgTys, FunctionProtoType::ExtProtoInfo()); |
812 | 0 | QualType BuiltinPtrTy = S.Context.getPointerType(BuiltinTy); |
813 | |
|
814 | 0 | Builtin = |
815 | 0 | S.ImpCastExprToType(Builtin, BuiltinPtrTy, CK_BuiltinFnToFnPtr).get(); |
816 | |
|
817 | 0 | BuiltinCall->setType(CE->getType()); |
818 | 0 | BuiltinCall->setValueKind(CE->getValueKind()); |
819 | 0 | BuiltinCall->setObjectKind(CE->getObjectKind()); |
820 | 0 | BuiltinCall->setCallee(Builtin); |
821 | 0 | BuiltinCall->setArg(1, ChainResult.get()); |
822 | |
|
823 | 0 | return false; |
824 | 0 | } |
825 | | |
826 | | namespace { |
827 | | |
828 | | class ScanfDiagnosticFormatHandler |
829 | | : public analyze_format_string::FormatStringHandler { |
830 | | // Accepts the argument index (relative to the first destination index) of the |
831 | | // argument whose size we want. |
832 | | using ComputeSizeFunction = |
833 | | llvm::function_ref<std::optional<llvm::APSInt>(unsigned)>; |
834 | | |
835 | | // Accepts the argument index (relative to the first destination index), the |
836 | | // destination size, and the source size). |
837 | | using DiagnoseFunction = |
838 | | llvm::function_ref<void(unsigned, unsigned, unsigned)>; |
839 | | |
840 | | ComputeSizeFunction ComputeSizeArgument; |
841 | | DiagnoseFunction Diagnose; |
842 | | |
843 | | public: |
844 | | ScanfDiagnosticFormatHandler(ComputeSizeFunction ComputeSizeArgument, |
845 | | DiagnoseFunction Diagnose) |
846 | 0 | : ComputeSizeArgument(ComputeSizeArgument), Diagnose(Diagnose) {} |
847 | | |
848 | | bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, |
849 | | const char *StartSpecifier, |
850 | 0 | unsigned specifierLen) override { |
851 | 0 | if (!FS.consumesDataArgument()) |
852 | 0 | return true; |
853 | | |
854 | 0 | unsigned NulByte = 0; |
855 | 0 | switch ((FS.getConversionSpecifier().getKind())) { |
856 | 0 | default: |
857 | 0 | return true; |
858 | 0 | case analyze_format_string::ConversionSpecifier::sArg: |
859 | 0 | case analyze_format_string::ConversionSpecifier::ScanListArg: |
860 | 0 | NulByte = 1; |
861 | 0 | break; |
862 | 0 | case analyze_format_string::ConversionSpecifier::cArg: |
863 | 0 | break; |
864 | 0 | } |
865 | | |
866 | 0 | analyze_format_string::OptionalAmount FW = FS.getFieldWidth(); |
867 | 0 | if (FW.getHowSpecified() != |
868 | 0 | analyze_format_string::OptionalAmount::HowSpecified::Constant) |
869 | 0 | return true; |
870 | | |
871 | 0 | unsigned SourceSize = FW.getConstantAmount() + NulByte; |
872 | |
|
873 | 0 | std::optional<llvm::APSInt> DestSizeAPS = |
874 | 0 | ComputeSizeArgument(FS.getArgIndex()); |
875 | 0 | if (!DestSizeAPS) |
876 | 0 | return true; |
877 | | |
878 | 0 | unsigned DestSize = DestSizeAPS->getZExtValue(); |
879 | |
|
880 | 0 | if (DestSize < SourceSize) |
881 | 0 | Diagnose(FS.getArgIndex(), DestSize, SourceSize); |
882 | |
|
883 | 0 | return true; |
884 | 0 | } |
885 | | }; |
886 | | |
887 | | class EstimateSizeFormatHandler |
888 | | : public analyze_format_string::FormatStringHandler { |
889 | | size_t Size; |
890 | | /// Whether the format string contains Linux kernel's format specifier |
891 | | /// extension. |
892 | | bool IsKernelCompatible = true; |
893 | | |
894 | | public: |
895 | | EstimateSizeFormatHandler(StringRef Format) |
896 | | : Size(std::min(Format.find(0), Format.size()) + |
897 | 0 | 1 /* null byte always written by sprintf */) {} |
898 | | |
899 | | bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, |
900 | | const char *, unsigned SpecifierLen, |
901 | 0 | const TargetInfo &) override { |
902 | |
|
903 | 0 | const size_t FieldWidth = computeFieldWidth(FS); |
904 | 0 | const size_t Precision = computePrecision(FS); |
905 | | |
906 | | // The actual format. |
907 | 0 | switch (FS.getConversionSpecifier().getKind()) { |
908 | | // Just a char. |
909 | 0 | case analyze_format_string::ConversionSpecifier::cArg: |
910 | 0 | case analyze_format_string::ConversionSpecifier::CArg: |
911 | 0 | Size += std::max(FieldWidth, (size_t)1); |
912 | 0 | break; |
913 | | // Just an integer. |
914 | 0 | case analyze_format_string::ConversionSpecifier::dArg: |
915 | 0 | case analyze_format_string::ConversionSpecifier::DArg: |
916 | 0 | case analyze_format_string::ConversionSpecifier::iArg: |
917 | 0 | case analyze_format_string::ConversionSpecifier::oArg: |
918 | 0 | case analyze_format_string::ConversionSpecifier::OArg: |
919 | 0 | case analyze_format_string::ConversionSpecifier::uArg: |
920 | 0 | case analyze_format_string::ConversionSpecifier::UArg: |
921 | 0 | case analyze_format_string::ConversionSpecifier::xArg: |
922 | 0 | case analyze_format_string::ConversionSpecifier::XArg: |
923 | 0 | Size += std::max(FieldWidth, Precision); |
924 | 0 | break; |
925 | | |
926 | | // %g style conversion switches between %f or %e style dynamically. |
927 | | // %g removes trailing zeros, and does not print decimal point if there are |
928 | | // no digits that follow it. Thus %g can print a single digit. |
929 | | // FIXME: If it is alternative form: |
930 | | // For g and G conversions, trailing zeros are not removed from the result. |
931 | 0 | case analyze_format_string::ConversionSpecifier::gArg: |
932 | 0 | case analyze_format_string::ConversionSpecifier::GArg: |
933 | 0 | Size += 1; |
934 | 0 | break; |
935 | | |
936 | | // Floating point number in the form '[+]ddd.ddd'. |
937 | 0 | case analyze_format_string::ConversionSpecifier::fArg: |
938 | 0 | case analyze_format_string::ConversionSpecifier::FArg: |
939 | 0 | Size += std::max(FieldWidth, 1 /* integer part */ + |
940 | 0 | (Precision ? 1 + Precision |
941 | 0 | : 0) /* period + decimal */); |
942 | 0 | break; |
943 | | |
944 | | // Floating point number in the form '[-]d.ddde[+-]dd'. |
945 | 0 | case analyze_format_string::ConversionSpecifier::eArg: |
946 | 0 | case analyze_format_string::ConversionSpecifier::EArg: |
947 | 0 | Size += |
948 | 0 | std::max(FieldWidth, |
949 | 0 | 1 /* integer part */ + |
950 | 0 | (Precision ? 1 + Precision : 0) /* period + decimal */ + |
951 | 0 | 1 /* e or E letter */ + 2 /* exponent */); |
952 | 0 | break; |
953 | | |
954 | | // Floating point number in the form '[-]0xh.hhhhp±dd'. |
955 | 0 | case analyze_format_string::ConversionSpecifier::aArg: |
956 | 0 | case analyze_format_string::ConversionSpecifier::AArg: |
957 | 0 | Size += |
958 | 0 | std::max(FieldWidth, |
959 | 0 | 2 /* 0x */ + 1 /* integer part */ + |
960 | 0 | (Precision ? 1 + Precision : 0) /* period + decimal */ + |
961 | 0 | 1 /* p or P letter */ + 1 /* + or - */ + 1 /* value */); |
962 | 0 | break; |
963 | | |
964 | | // Just a string. |
965 | 0 | case analyze_format_string::ConversionSpecifier::sArg: |
966 | 0 | case analyze_format_string::ConversionSpecifier::SArg: |
967 | 0 | Size += FieldWidth; |
968 | 0 | break; |
969 | | |
970 | | // Just a pointer in the form '0xddd'. |
971 | 0 | case analyze_format_string::ConversionSpecifier::pArg: |
972 | | // Linux kernel has its own extesion for `%p` specifier. |
973 | | // Kernel Document: |
974 | | // https://docs.kernel.org/core-api/printk-formats.html#pointer-types |
975 | 0 | IsKernelCompatible = false; |
976 | 0 | Size += std::max(FieldWidth, 2 /* leading 0x */ + Precision); |
977 | 0 | break; |
978 | | |
979 | | // A plain percent. |
980 | 0 | case analyze_format_string::ConversionSpecifier::PercentArg: |
981 | 0 | Size += 1; |
982 | 0 | break; |
983 | | |
984 | 0 | default: |
985 | 0 | break; |
986 | 0 | } |
987 | | |
988 | 0 | Size += FS.hasPlusPrefix() || FS.hasSpacePrefix(); |
989 | |
|
990 | 0 | if (FS.hasAlternativeForm()) { |
991 | 0 | switch (FS.getConversionSpecifier().getKind()) { |
992 | | // For o conversion, it increases the precision, if and only if necessary, |
993 | | // to force the first digit of the result to be a zero |
994 | | // (if the value and precision are both 0, a single 0 is printed) |
995 | 0 | case analyze_format_string::ConversionSpecifier::oArg: |
996 | | // For b conversion, a nonzero result has 0b prefixed to it. |
997 | 0 | case analyze_format_string::ConversionSpecifier::bArg: |
998 | | // For x (or X) conversion, a nonzero result has 0x (or 0X) prefixed to |
999 | | // it. |
1000 | 0 | case analyze_format_string::ConversionSpecifier::xArg: |
1001 | 0 | case analyze_format_string::ConversionSpecifier::XArg: |
1002 | | // Note: even when the prefix is added, if |
1003 | | // (prefix_width <= FieldWidth - formatted_length) holds, |
1004 | | // the prefix does not increase the format |
1005 | | // size. e.g.(("%#3x", 0xf) is "0xf") |
1006 | | |
1007 | | // If the result is zero, o, b, x, X adds nothing. |
1008 | 0 | break; |
1009 | | // For a, A, e, E, f, F, g, and G conversions, |
1010 | | // the result of converting a floating-point number always contains a |
1011 | | // decimal-point |
1012 | 0 | case analyze_format_string::ConversionSpecifier::aArg: |
1013 | 0 | case analyze_format_string::ConversionSpecifier::AArg: |
1014 | 0 | case analyze_format_string::ConversionSpecifier::eArg: |
1015 | 0 | case analyze_format_string::ConversionSpecifier::EArg: |
1016 | 0 | case analyze_format_string::ConversionSpecifier::fArg: |
1017 | 0 | case analyze_format_string::ConversionSpecifier::FArg: |
1018 | 0 | case analyze_format_string::ConversionSpecifier::gArg: |
1019 | 0 | case analyze_format_string::ConversionSpecifier::GArg: |
1020 | 0 | Size += (Precision ? 0 : 1); |
1021 | 0 | break; |
1022 | | // For other conversions, the behavior is undefined. |
1023 | 0 | default: |
1024 | 0 | break; |
1025 | 0 | } |
1026 | 0 | } |
1027 | 0 | assert(SpecifierLen <= Size && "no underflow"); |
1028 | 0 | Size -= SpecifierLen; |
1029 | 0 | return true; |
1030 | 0 | } |
1031 | | |
1032 | 0 | size_t getSizeLowerBound() const { return Size; } |
1033 | 0 | bool isKernelCompatible() const { return IsKernelCompatible; } |
1034 | | |
1035 | | private: |
1036 | 0 | static size_t computeFieldWidth(const analyze_printf::PrintfSpecifier &FS) { |
1037 | 0 | const analyze_format_string::OptionalAmount &FW = FS.getFieldWidth(); |
1038 | 0 | size_t FieldWidth = 0; |
1039 | 0 | if (FW.getHowSpecified() == analyze_format_string::OptionalAmount::Constant) |
1040 | 0 | FieldWidth = FW.getConstantAmount(); |
1041 | 0 | return FieldWidth; |
1042 | 0 | } |
1043 | | |
1044 | 0 | static size_t computePrecision(const analyze_printf::PrintfSpecifier &FS) { |
1045 | 0 | const analyze_format_string::OptionalAmount &FW = FS.getPrecision(); |
1046 | 0 | size_t Precision = 0; |
1047 | | |
1048 | | // See man 3 printf for default precision value based on the specifier. |
1049 | 0 | switch (FW.getHowSpecified()) { |
1050 | 0 | case analyze_format_string::OptionalAmount::NotSpecified: |
1051 | 0 | switch (FS.getConversionSpecifier().getKind()) { |
1052 | 0 | default: |
1053 | 0 | break; |
1054 | 0 | case analyze_format_string::ConversionSpecifier::dArg: // %d |
1055 | 0 | case analyze_format_string::ConversionSpecifier::DArg: // %D |
1056 | 0 | case analyze_format_string::ConversionSpecifier::iArg: // %i |
1057 | 0 | Precision = 1; |
1058 | 0 | break; |
1059 | 0 | case analyze_format_string::ConversionSpecifier::oArg: // %d |
1060 | 0 | case analyze_format_string::ConversionSpecifier::OArg: // %D |
1061 | 0 | case analyze_format_string::ConversionSpecifier::uArg: // %d |
1062 | 0 | case analyze_format_string::ConversionSpecifier::UArg: // %D |
1063 | 0 | case analyze_format_string::ConversionSpecifier::xArg: // %d |
1064 | 0 | case analyze_format_string::ConversionSpecifier::XArg: // %D |
1065 | 0 | Precision = 1; |
1066 | 0 | break; |
1067 | 0 | case analyze_format_string::ConversionSpecifier::fArg: // %f |
1068 | 0 | case analyze_format_string::ConversionSpecifier::FArg: // %F |
1069 | 0 | case analyze_format_string::ConversionSpecifier::eArg: // %e |
1070 | 0 | case analyze_format_string::ConversionSpecifier::EArg: // %E |
1071 | 0 | case analyze_format_string::ConversionSpecifier::gArg: // %g |
1072 | 0 | case analyze_format_string::ConversionSpecifier::GArg: // %G |
1073 | 0 | Precision = 6; |
1074 | 0 | break; |
1075 | 0 | case analyze_format_string::ConversionSpecifier::pArg: // %d |
1076 | 0 | Precision = 1; |
1077 | 0 | break; |
1078 | 0 | } |
1079 | 0 | break; |
1080 | 0 | case analyze_format_string::OptionalAmount::Constant: |
1081 | 0 | Precision = FW.getConstantAmount(); |
1082 | 0 | break; |
1083 | 0 | default: |
1084 | 0 | break; |
1085 | 0 | } |
1086 | 0 | return Precision; |
1087 | 0 | } |
1088 | | }; |
1089 | | |
1090 | | } // namespace |
1091 | | |
1092 | | static bool ProcessFormatStringLiteral(const Expr *FormatExpr, |
1093 | | StringRef &FormatStrRef, size_t &StrLen, |
1094 | 0 | ASTContext &Context) { |
1095 | 0 | if (const auto *Format = dyn_cast<StringLiteral>(FormatExpr); |
1096 | 0 | Format && (Format->isOrdinary() || Format->isUTF8())) { |
1097 | 0 | FormatStrRef = Format->getString(); |
1098 | 0 | const ConstantArrayType *T = |
1099 | 0 | Context.getAsConstantArrayType(Format->getType()); |
1100 | 0 | assert(T && "String literal not of constant array type!"); |
1101 | 0 | size_t TypeSize = T->getSize().getZExtValue(); |
1102 | | // In case there's a null byte somewhere. |
1103 | 0 | StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, FormatStrRef.find(0)); |
1104 | 0 | return true; |
1105 | 0 | } |
1106 | 0 | return false; |
1107 | 0 | } |
1108 | | |
1109 | | void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, |
1110 | 0 | CallExpr *TheCall) { |
1111 | 0 | if (TheCall->isValueDependent() || TheCall->isTypeDependent() || |
1112 | 0 | isConstantEvaluatedContext()) |
1113 | 0 | return; |
1114 | | |
1115 | 0 | bool UseDABAttr = false; |
1116 | 0 | const FunctionDecl *UseDecl = FD; |
1117 | |
|
1118 | 0 | const auto *DABAttr = FD->getAttr<DiagnoseAsBuiltinAttr>(); |
1119 | 0 | if (DABAttr) { |
1120 | 0 | UseDecl = DABAttr->getFunction(); |
1121 | 0 | assert(UseDecl && "Missing FunctionDecl in DiagnoseAsBuiltin attribute!"); |
1122 | 0 | UseDABAttr = true; |
1123 | 0 | } |
1124 | | |
1125 | 0 | unsigned BuiltinID = UseDecl->getBuiltinID(/*ConsiderWrappers=*/true); |
1126 | |
|
1127 | 0 | if (!BuiltinID) |
1128 | 0 | return; |
1129 | | |
1130 | 0 | const TargetInfo &TI = getASTContext().getTargetInfo(); |
1131 | 0 | unsigned SizeTypeWidth = TI.getTypeWidth(TI.getSizeType()); |
1132 | |
|
1133 | 0 | auto TranslateIndex = [&](unsigned Index) -> std::optional<unsigned> { |
1134 | | // If we refer to a diagnose_as_builtin attribute, we need to change the |
1135 | | // argument index to refer to the arguments of the called function. Unless |
1136 | | // the index is out of bounds, which presumably means it's a variadic |
1137 | | // function. |
1138 | 0 | if (!UseDABAttr) |
1139 | 0 | return Index; |
1140 | 0 | unsigned DABIndices = DABAttr->argIndices_size(); |
1141 | 0 | unsigned NewIndex = Index < DABIndices |
1142 | 0 | ? DABAttr->argIndices_begin()[Index] |
1143 | 0 | : Index - DABIndices + FD->getNumParams(); |
1144 | 0 | if (NewIndex >= TheCall->getNumArgs()) |
1145 | 0 | return std::nullopt; |
1146 | 0 | return NewIndex; |
1147 | 0 | }; |
1148 | |
|
1149 | 0 | auto ComputeExplicitObjectSizeArgument = |
1150 | 0 | [&](unsigned Index) -> std::optional<llvm::APSInt> { |
1151 | 0 | std::optional<unsigned> IndexOptional = TranslateIndex(Index); |
1152 | 0 | if (!IndexOptional) |
1153 | 0 | return std::nullopt; |
1154 | 0 | unsigned NewIndex = *IndexOptional; |
1155 | 0 | Expr::EvalResult Result; |
1156 | 0 | Expr *SizeArg = TheCall->getArg(NewIndex); |
1157 | 0 | if (!SizeArg->EvaluateAsInt(Result, getASTContext())) |
1158 | 0 | return std::nullopt; |
1159 | 0 | llvm::APSInt Integer = Result.Val.getInt(); |
1160 | 0 | Integer.setIsUnsigned(true); |
1161 | 0 | return Integer; |
1162 | 0 | }; |
1163 | |
|
1164 | 0 | auto ComputeSizeArgument = |
1165 | 0 | [&](unsigned Index) -> std::optional<llvm::APSInt> { |
1166 | | // If the parameter has a pass_object_size attribute, then we should use its |
1167 | | // (potentially) more strict checking mode. Otherwise, conservatively assume |
1168 | | // type 0. |
1169 | 0 | int BOSType = 0; |
1170 | | // This check can fail for variadic functions. |
1171 | 0 | if (Index < FD->getNumParams()) { |
1172 | 0 | if (const auto *POS = |
1173 | 0 | FD->getParamDecl(Index)->getAttr<PassObjectSizeAttr>()) |
1174 | 0 | BOSType = POS->getType(); |
1175 | 0 | } |
1176 | |
|
1177 | 0 | std::optional<unsigned> IndexOptional = TranslateIndex(Index); |
1178 | 0 | if (!IndexOptional) |
1179 | 0 | return std::nullopt; |
1180 | 0 | unsigned NewIndex = *IndexOptional; |
1181 | |
|
1182 | 0 | if (NewIndex >= TheCall->getNumArgs()) |
1183 | 0 | return std::nullopt; |
1184 | | |
1185 | 0 | const Expr *ObjArg = TheCall->getArg(NewIndex); |
1186 | 0 | uint64_t Result; |
1187 | 0 | if (!ObjArg->tryEvaluateObjectSize(Result, getASTContext(), BOSType)) |
1188 | 0 | return std::nullopt; |
1189 | | |
1190 | | // Get the object size in the target's size_t width. |
1191 | 0 | return llvm::APSInt::getUnsigned(Result).extOrTrunc(SizeTypeWidth); |
1192 | 0 | }; |
1193 | |
|
1194 | 0 | auto ComputeStrLenArgument = |
1195 | 0 | [&](unsigned Index) -> std::optional<llvm::APSInt> { |
1196 | 0 | std::optional<unsigned> IndexOptional = TranslateIndex(Index); |
1197 | 0 | if (!IndexOptional) |
1198 | 0 | return std::nullopt; |
1199 | 0 | unsigned NewIndex = *IndexOptional; |
1200 | |
|
1201 | 0 | const Expr *ObjArg = TheCall->getArg(NewIndex); |
1202 | 0 | uint64_t Result; |
1203 | 0 | if (!ObjArg->tryEvaluateStrLen(Result, getASTContext())) |
1204 | 0 | return std::nullopt; |
1205 | | // Add 1 for null byte. |
1206 | 0 | return llvm::APSInt::getUnsigned(Result + 1).extOrTrunc(SizeTypeWidth); |
1207 | 0 | }; |
1208 | |
|
1209 | 0 | std::optional<llvm::APSInt> SourceSize; |
1210 | 0 | std::optional<llvm::APSInt> DestinationSize; |
1211 | 0 | unsigned DiagID = 0; |
1212 | 0 | bool IsChkVariant = false; |
1213 | |
|
1214 | 0 | auto GetFunctionName = [&]() { |
1215 | 0 | StringRef FunctionName = getASTContext().BuiltinInfo.getName(BuiltinID); |
1216 | | // Skim off the details of whichever builtin was called to produce a better |
1217 | | // diagnostic, as it's unlikely that the user wrote the __builtin |
1218 | | // explicitly. |
1219 | 0 | if (IsChkVariant) { |
1220 | 0 | FunctionName = FunctionName.drop_front(std::strlen("__builtin___")); |
1221 | 0 | FunctionName = FunctionName.drop_back(std::strlen("_chk")); |
1222 | 0 | } else { |
1223 | 0 | FunctionName.consume_front("__builtin_"); |
1224 | 0 | } |
1225 | 0 | return FunctionName; |
1226 | 0 | }; |
1227 | |
|
1228 | 0 | switch (BuiltinID) { |
1229 | 0 | default: |
1230 | 0 | return; |
1231 | 0 | case Builtin::BI__builtin_strcpy: |
1232 | 0 | case Builtin::BIstrcpy: { |
1233 | 0 | DiagID = diag::warn_fortify_strlen_overflow; |
1234 | 0 | SourceSize = ComputeStrLenArgument(1); |
1235 | 0 | DestinationSize = ComputeSizeArgument(0); |
1236 | 0 | break; |
1237 | 0 | } |
1238 | | |
1239 | 0 | case Builtin::BI__builtin___strcpy_chk: { |
1240 | 0 | DiagID = diag::warn_fortify_strlen_overflow; |
1241 | 0 | SourceSize = ComputeStrLenArgument(1); |
1242 | 0 | DestinationSize = ComputeExplicitObjectSizeArgument(2); |
1243 | 0 | IsChkVariant = true; |
1244 | 0 | break; |
1245 | 0 | } |
1246 | | |
1247 | 0 | case Builtin::BIscanf: |
1248 | 0 | case Builtin::BIfscanf: |
1249 | 0 | case Builtin::BIsscanf: { |
1250 | 0 | unsigned FormatIndex = 1; |
1251 | 0 | unsigned DataIndex = 2; |
1252 | 0 | if (BuiltinID == Builtin::BIscanf) { |
1253 | 0 | FormatIndex = 0; |
1254 | 0 | DataIndex = 1; |
1255 | 0 | } |
1256 | |
|
1257 | 0 | const auto *FormatExpr = |
1258 | 0 | TheCall->getArg(FormatIndex)->IgnoreParenImpCasts(); |
1259 | |
|
1260 | 0 | StringRef FormatStrRef; |
1261 | 0 | size_t StrLen; |
1262 | 0 | if (!ProcessFormatStringLiteral(FormatExpr, FormatStrRef, StrLen, Context)) |
1263 | 0 | return; |
1264 | | |
1265 | 0 | auto Diagnose = [&](unsigned ArgIndex, unsigned DestSize, |
1266 | 0 | unsigned SourceSize) { |
1267 | 0 | DiagID = diag::warn_fortify_scanf_overflow; |
1268 | 0 | unsigned Index = ArgIndex + DataIndex; |
1269 | 0 | StringRef FunctionName = GetFunctionName(); |
1270 | 0 | DiagRuntimeBehavior(TheCall->getArg(Index)->getBeginLoc(), TheCall, |
1271 | 0 | PDiag(DiagID) << FunctionName << (Index + 1) |
1272 | 0 | << DestSize << SourceSize); |
1273 | 0 | }; |
1274 | |
|
1275 | 0 | auto ShiftedComputeSizeArgument = [&](unsigned Index) { |
1276 | 0 | return ComputeSizeArgument(Index + DataIndex); |
1277 | 0 | }; |
1278 | 0 | ScanfDiagnosticFormatHandler H(ShiftedComputeSizeArgument, Diagnose); |
1279 | 0 | const char *FormatBytes = FormatStrRef.data(); |
1280 | 0 | analyze_format_string::ParseScanfString(H, FormatBytes, |
1281 | 0 | FormatBytes + StrLen, getLangOpts(), |
1282 | 0 | Context.getTargetInfo()); |
1283 | | |
1284 | | // Unlike the other cases, in this one we have already issued the diagnostic |
1285 | | // here, so no need to continue (because unlike the other cases, here the |
1286 | | // diagnostic refers to the argument number). |
1287 | 0 | return; |
1288 | 0 | } |
1289 | | |
1290 | 0 | case Builtin::BIsprintf: |
1291 | 0 | case Builtin::BI__builtin___sprintf_chk: { |
1292 | 0 | size_t FormatIndex = BuiltinID == Builtin::BIsprintf ? 1 : 3; |
1293 | 0 | auto *FormatExpr = TheCall->getArg(FormatIndex)->IgnoreParenImpCasts(); |
1294 | |
|
1295 | 0 | StringRef FormatStrRef; |
1296 | 0 | size_t StrLen; |
1297 | 0 | if (ProcessFormatStringLiteral(FormatExpr, FormatStrRef, StrLen, Context)) { |
1298 | 0 | EstimateSizeFormatHandler H(FormatStrRef); |
1299 | 0 | const char *FormatBytes = FormatStrRef.data(); |
1300 | 0 | if (!analyze_format_string::ParsePrintfString( |
1301 | 0 | H, FormatBytes, FormatBytes + StrLen, getLangOpts(), |
1302 | 0 | Context.getTargetInfo(), false)) { |
1303 | 0 | DiagID = H.isKernelCompatible() |
1304 | 0 | ? diag::warn_format_overflow |
1305 | 0 | : diag::warn_format_overflow_non_kprintf; |
1306 | 0 | SourceSize = llvm::APSInt::getUnsigned(H.getSizeLowerBound()) |
1307 | 0 | .extOrTrunc(SizeTypeWidth); |
1308 | 0 | if (BuiltinID == Builtin::BI__builtin___sprintf_chk) { |
1309 | 0 | DestinationSize = ComputeExplicitObjectSizeArgument(2); |
1310 | 0 | IsChkVariant = true; |
1311 | 0 | } else { |
1312 | 0 | DestinationSize = ComputeSizeArgument(0); |
1313 | 0 | } |
1314 | 0 | break; |
1315 | 0 | } |
1316 | 0 | } |
1317 | 0 | return; |
1318 | 0 | } |
1319 | 0 | case Builtin::BI__builtin___memcpy_chk: |
1320 | 0 | case Builtin::BI__builtin___memmove_chk: |
1321 | 0 | case Builtin::BI__builtin___memset_chk: |
1322 | 0 | case Builtin::BI__builtin___strlcat_chk: |
1323 | 0 | case Builtin::BI__builtin___strlcpy_chk: |
1324 | 0 | case Builtin::BI__builtin___strncat_chk: |
1325 | 0 | case Builtin::BI__builtin___strncpy_chk: |
1326 | 0 | case Builtin::BI__builtin___stpncpy_chk: |
1327 | 0 | case Builtin::BI__builtin___memccpy_chk: |
1328 | 0 | case Builtin::BI__builtin___mempcpy_chk: { |
1329 | 0 | DiagID = diag::warn_builtin_chk_overflow; |
1330 | 0 | SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 2); |
1331 | 0 | DestinationSize = |
1332 | 0 | ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); |
1333 | 0 | IsChkVariant = true; |
1334 | 0 | break; |
1335 | 0 | } |
1336 | | |
1337 | 0 | case Builtin::BI__builtin___snprintf_chk: |
1338 | 0 | case Builtin::BI__builtin___vsnprintf_chk: { |
1339 | 0 | DiagID = diag::warn_builtin_chk_overflow; |
1340 | 0 | SourceSize = ComputeExplicitObjectSizeArgument(1); |
1341 | 0 | DestinationSize = ComputeExplicitObjectSizeArgument(3); |
1342 | 0 | IsChkVariant = true; |
1343 | 0 | break; |
1344 | 0 | } |
1345 | | |
1346 | 0 | case Builtin::BIstrncat: |
1347 | 0 | case Builtin::BI__builtin_strncat: |
1348 | 0 | case Builtin::BIstrncpy: |
1349 | 0 | case Builtin::BI__builtin_strncpy: |
1350 | 0 | case Builtin::BIstpncpy: |
1351 | 0 | case Builtin::BI__builtin_stpncpy: { |
1352 | | // Whether these functions overflow depends on the runtime strlen of the |
1353 | | // string, not just the buffer size, so emitting the "always overflow" |
1354 | | // diagnostic isn't quite right. We should still diagnose passing a buffer |
1355 | | // size larger than the destination buffer though; this is a runtime abort |
1356 | | // in _FORTIFY_SOURCE mode, and is quite suspicious otherwise. |
1357 | 0 | DiagID = diag::warn_fortify_source_size_mismatch; |
1358 | 0 | SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); |
1359 | 0 | DestinationSize = ComputeSizeArgument(0); |
1360 | 0 | break; |
1361 | 0 | } |
1362 | | |
1363 | 0 | case Builtin::BImemcpy: |
1364 | 0 | case Builtin::BI__builtin_memcpy: |
1365 | 0 | case Builtin::BImemmove: |
1366 | 0 | case Builtin::BI__builtin_memmove: |
1367 | 0 | case Builtin::BImemset: |
1368 | 0 | case Builtin::BI__builtin_memset: |
1369 | 0 | case Builtin::BImempcpy: |
1370 | 0 | case Builtin::BI__builtin_mempcpy: { |
1371 | 0 | DiagID = diag::warn_fortify_source_overflow; |
1372 | 0 | SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); |
1373 | 0 | DestinationSize = ComputeSizeArgument(0); |
1374 | 0 | break; |
1375 | 0 | } |
1376 | 0 | case Builtin::BIsnprintf: |
1377 | 0 | case Builtin::BI__builtin_snprintf: |
1378 | 0 | case Builtin::BIvsnprintf: |
1379 | 0 | case Builtin::BI__builtin_vsnprintf: { |
1380 | 0 | DiagID = diag::warn_fortify_source_size_mismatch; |
1381 | 0 | SourceSize = ComputeExplicitObjectSizeArgument(1); |
1382 | 0 | const auto *FormatExpr = TheCall->getArg(2)->IgnoreParenImpCasts(); |
1383 | 0 | StringRef FormatStrRef; |
1384 | 0 | size_t StrLen; |
1385 | 0 | if (SourceSize && |
1386 | 0 | ProcessFormatStringLiteral(FormatExpr, FormatStrRef, StrLen, Context)) { |
1387 | 0 | EstimateSizeFormatHandler H(FormatStrRef); |
1388 | 0 | const char *FormatBytes = FormatStrRef.data(); |
1389 | 0 | if (!analyze_format_string::ParsePrintfString( |
1390 | 0 | H, FormatBytes, FormatBytes + StrLen, getLangOpts(), |
1391 | 0 | Context.getTargetInfo(), /*isFreeBSDKPrintf=*/false)) { |
1392 | 0 | llvm::APSInt FormatSize = |
1393 | 0 | llvm::APSInt::getUnsigned(H.getSizeLowerBound()) |
1394 | 0 | .extOrTrunc(SizeTypeWidth); |
1395 | 0 | if (FormatSize > *SourceSize && *SourceSize != 0) { |
1396 | 0 | unsigned TruncationDiagID = |
1397 | 0 | H.isKernelCompatible() ? diag::warn_format_truncation |
1398 | 0 | : diag::warn_format_truncation_non_kprintf; |
1399 | 0 | SmallString<16> SpecifiedSizeStr; |
1400 | 0 | SmallString<16> FormatSizeStr; |
1401 | 0 | SourceSize->toString(SpecifiedSizeStr, /*Radix=*/10); |
1402 | 0 | FormatSize.toString(FormatSizeStr, /*Radix=*/10); |
1403 | 0 | DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, |
1404 | 0 | PDiag(TruncationDiagID) |
1405 | 0 | << GetFunctionName() << SpecifiedSizeStr |
1406 | 0 | << FormatSizeStr); |
1407 | 0 | } |
1408 | 0 | } |
1409 | 0 | } |
1410 | 0 | DestinationSize = ComputeSizeArgument(0); |
1411 | 0 | } |
1412 | 0 | } |
1413 | | |
1414 | 0 | if (!SourceSize || !DestinationSize || |
1415 | 0 | llvm::APSInt::compareValues(*SourceSize, *DestinationSize) <= 0) |
1416 | 0 | return; |
1417 | | |
1418 | 0 | StringRef FunctionName = GetFunctionName(); |
1419 | |
|
1420 | 0 | SmallString<16> DestinationStr; |
1421 | 0 | SmallString<16> SourceStr; |
1422 | 0 | DestinationSize->toString(DestinationStr, /*Radix=*/10); |
1423 | 0 | SourceSize->toString(SourceStr, /*Radix=*/10); |
1424 | 0 | DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, |
1425 | 0 | PDiag(DiagID) |
1426 | 0 | << FunctionName << DestinationStr << SourceStr); |
1427 | 0 | } |
1428 | | |
1429 | | static bool SemaBuiltinSEHScopeCheck(Sema &SemaRef, CallExpr *TheCall, |
1430 | | Scope::ScopeFlags NeededScopeFlags, |
1431 | 0 | unsigned DiagID) { |
1432 | | // Scopes aren't available during instantiation. Fortunately, builtin |
1433 | | // functions cannot be template args so they cannot be formed through template |
1434 | | // instantiation. Therefore checking once during the parse is sufficient. |
1435 | 0 | if (SemaRef.inTemplateInstantiation()) |
1436 | 0 | return false; |
1437 | | |
1438 | 0 | Scope *S = SemaRef.getCurScope(); |
1439 | 0 | while (S && !S->isSEHExceptScope()) |
1440 | 0 | S = S->getParent(); |
1441 | 0 | if (!S || !(S->getFlags() & NeededScopeFlags)) { |
1442 | 0 | auto *DRE = cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
1443 | 0 | SemaRef.Diag(TheCall->getExprLoc(), DiagID) |
1444 | 0 | << DRE->getDecl()->getIdentifier(); |
1445 | 0 | return true; |
1446 | 0 | } |
1447 | | |
1448 | 0 | return false; |
1449 | 0 | } |
1450 | | |
1451 | 0 | static inline bool isBlockPointer(Expr *Arg) { |
1452 | 0 | return Arg->getType()->isBlockPointerType(); |
1453 | 0 | } |
1454 | | |
1455 | | /// OpenCL C v2.0, s6.13.17.2 - Checks that the block parameters are all local |
1456 | | /// void*, which is a requirement of device side enqueue. |
1457 | 0 | static bool checkOpenCLBlockArgs(Sema &S, Expr *BlockArg) { |
1458 | 0 | const BlockPointerType *BPT = |
1459 | 0 | cast<BlockPointerType>(BlockArg->getType().getCanonicalType()); |
1460 | 0 | ArrayRef<QualType> Params = |
1461 | 0 | BPT->getPointeeType()->castAs<FunctionProtoType>()->getParamTypes(); |
1462 | 0 | unsigned ArgCounter = 0; |
1463 | 0 | bool IllegalParams = false; |
1464 | | // Iterate through the block parameters until either one is found that is not |
1465 | | // a local void*, or the block is valid. |
1466 | 0 | for (ArrayRef<QualType>::iterator I = Params.begin(), E = Params.end(); |
1467 | 0 | I != E; ++I, ++ArgCounter) { |
1468 | 0 | if (!(*I)->isPointerType() || !(*I)->getPointeeType()->isVoidType() || |
1469 | 0 | (*I)->getPointeeType().getQualifiers().getAddressSpace() != |
1470 | 0 | LangAS::opencl_local) { |
1471 | | // Get the location of the error. If a block literal has been passed |
1472 | | // (BlockExpr) then we can point straight to the offending argument, |
1473 | | // else we just point to the variable reference. |
1474 | 0 | SourceLocation ErrorLoc; |
1475 | 0 | if (isa<BlockExpr>(BlockArg)) { |
1476 | 0 | BlockDecl *BD = cast<BlockExpr>(BlockArg)->getBlockDecl(); |
1477 | 0 | ErrorLoc = BD->getParamDecl(ArgCounter)->getBeginLoc(); |
1478 | 0 | } else if (isa<DeclRefExpr>(BlockArg)) { |
1479 | 0 | ErrorLoc = cast<DeclRefExpr>(BlockArg)->getBeginLoc(); |
1480 | 0 | } |
1481 | 0 | S.Diag(ErrorLoc, |
1482 | 0 | diag::err_opencl_enqueue_kernel_blocks_non_local_void_args); |
1483 | 0 | IllegalParams = true; |
1484 | 0 | } |
1485 | 0 | } |
1486 | |
|
1487 | 0 | return IllegalParams; |
1488 | 0 | } |
1489 | | |
1490 | 0 | static bool checkOpenCLSubgroupExt(Sema &S, CallExpr *Call) { |
1491 | | // OpenCL device can support extension but not the feature as extension |
1492 | | // requires subgroup independent forward progress, but subgroup independent |
1493 | | // forward progress is optional in OpenCL C 3.0 __opencl_c_subgroups feature. |
1494 | 0 | if (!S.getOpenCLOptions().isSupported("cl_khr_subgroups", S.getLangOpts()) && |
1495 | 0 | !S.getOpenCLOptions().isSupported("__opencl_c_subgroups", |
1496 | 0 | S.getLangOpts())) { |
1497 | 0 | S.Diag(Call->getBeginLoc(), diag::err_opencl_requires_extension) |
1498 | 0 | << 1 << Call->getDirectCallee() |
1499 | 0 | << "cl_khr_subgroups or __opencl_c_subgroups"; |
1500 | 0 | return true; |
1501 | 0 | } |
1502 | 0 | return false; |
1503 | 0 | } |
1504 | | |
1505 | 0 | static bool SemaOpenCLBuiltinNDRangeAndBlock(Sema &S, CallExpr *TheCall) { |
1506 | 0 | if (checkArgCount(S, TheCall, 2)) |
1507 | 0 | return true; |
1508 | | |
1509 | 0 | if (checkOpenCLSubgroupExt(S, TheCall)) |
1510 | 0 | return true; |
1511 | | |
1512 | | // First argument is an ndrange_t type. |
1513 | 0 | Expr *NDRangeArg = TheCall->getArg(0); |
1514 | 0 | if (NDRangeArg->getType().getUnqualifiedType().getAsString() != "ndrange_t") { |
1515 | 0 | S.Diag(NDRangeArg->getBeginLoc(), diag::err_opencl_builtin_expected_type) |
1516 | 0 | << TheCall->getDirectCallee() << "'ndrange_t'"; |
1517 | 0 | return true; |
1518 | 0 | } |
1519 | | |
1520 | 0 | Expr *BlockArg = TheCall->getArg(1); |
1521 | 0 | if (!isBlockPointer(BlockArg)) { |
1522 | 0 | S.Diag(BlockArg->getBeginLoc(), diag::err_opencl_builtin_expected_type) |
1523 | 0 | << TheCall->getDirectCallee() << "block"; |
1524 | 0 | return true; |
1525 | 0 | } |
1526 | 0 | return checkOpenCLBlockArgs(S, BlockArg); |
1527 | 0 | } |
1528 | | |
1529 | | /// OpenCL C v2.0, s6.13.17.6 - Check the argument to the |
1530 | | /// get_kernel_work_group_size |
1531 | | /// and get_kernel_preferred_work_group_size_multiple builtin functions. |
1532 | 0 | static bool SemaOpenCLBuiltinKernelWorkGroupSize(Sema &S, CallExpr *TheCall) { |
1533 | 0 | if (checkArgCount(S, TheCall, 1)) |
1534 | 0 | return true; |
1535 | | |
1536 | 0 | Expr *BlockArg = TheCall->getArg(0); |
1537 | 0 | if (!isBlockPointer(BlockArg)) { |
1538 | 0 | S.Diag(BlockArg->getBeginLoc(), diag::err_opencl_builtin_expected_type) |
1539 | 0 | << TheCall->getDirectCallee() << "block"; |
1540 | 0 | return true; |
1541 | 0 | } |
1542 | 0 | return checkOpenCLBlockArgs(S, BlockArg); |
1543 | 0 | } |
1544 | | |
1545 | | /// Diagnose integer type and any valid implicit conversion to it. |
1546 | | static bool checkOpenCLEnqueueIntType(Sema &S, Expr *E, |
1547 | | const QualType &IntType); |
1548 | | |
1549 | | static bool checkOpenCLEnqueueLocalSizeArgs(Sema &S, CallExpr *TheCall, |
1550 | 0 | unsigned Start, unsigned End) { |
1551 | 0 | bool IllegalParams = false; |
1552 | 0 | for (unsigned I = Start; I <= End; ++I) |
1553 | 0 | IllegalParams |= checkOpenCLEnqueueIntType(S, TheCall->getArg(I), |
1554 | 0 | S.Context.getSizeType()); |
1555 | 0 | return IllegalParams; |
1556 | 0 | } |
1557 | | |
1558 | | /// OpenCL v2.0, s6.13.17.1 - Check that sizes are provided for all |
1559 | | /// 'local void*' parameter of passed block. |
1560 | | static bool checkOpenCLEnqueueVariadicArgs(Sema &S, CallExpr *TheCall, |
1561 | | Expr *BlockArg, |
1562 | 0 | unsigned NumNonVarArgs) { |
1563 | 0 | const BlockPointerType *BPT = |
1564 | 0 | cast<BlockPointerType>(BlockArg->getType().getCanonicalType()); |
1565 | 0 | unsigned NumBlockParams = |
1566 | 0 | BPT->getPointeeType()->castAs<FunctionProtoType>()->getNumParams(); |
1567 | 0 | unsigned TotalNumArgs = TheCall->getNumArgs(); |
1568 | | |
1569 | | // For each argument passed to the block, a corresponding uint needs to |
1570 | | // be passed to describe the size of the local memory. |
1571 | 0 | if (TotalNumArgs != NumBlockParams + NumNonVarArgs) { |
1572 | 0 | S.Diag(TheCall->getBeginLoc(), |
1573 | 0 | diag::err_opencl_enqueue_kernel_local_size_args); |
1574 | 0 | return true; |
1575 | 0 | } |
1576 | | |
1577 | | // Check that the sizes of the local memory are specified by integers. |
1578 | 0 | return checkOpenCLEnqueueLocalSizeArgs(S, TheCall, NumNonVarArgs, |
1579 | 0 | TotalNumArgs - 1); |
1580 | 0 | } |
1581 | | |
1582 | | /// OpenCL C v2.0, s6.13.17 - Enqueue kernel function contains four different |
1583 | | /// overload formats specified in Table 6.13.17.1. |
1584 | | /// int enqueue_kernel(queue_t queue, |
1585 | | /// kernel_enqueue_flags_t flags, |
1586 | | /// const ndrange_t ndrange, |
1587 | | /// void (^block)(void)) |
1588 | | /// int enqueue_kernel(queue_t queue, |
1589 | | /// kernel_enqueue_flags_t flags, |
1590 | | /// const ndrange_t ndrange, |
1591 | | /// uint num_events_in_wait_list, |
1592 | | /// clk_event_t *event_wait_list, |
1593 | | /// clk_event_t *event_ret, |
1594 | | /// void (^block)(void)) |
1595 | | /// int enqueue_kernel(queue_t queue, |
1596 | | /// kernel_enqueue_flags_t flags, |
1597 | | /// const ndrange_t ndrange, |
1598 | | /// void (^block)(local void*, ...), |
1599 | | /// uint size0, ...) |
1600 | | /// int enqueue_kernel(queue_t queue, |
1601 | | /// kernel_enqueue_flags_t flags, |
1602 | | /// const ndrange_t ndrange, |
1603 | | /// uint num_events_in_wait_list, |
1604 | | /// clk_event_t *event_wait_list, |
1605 | | /// clk_event_t *event_ret, |
1606 | | /// void (^block)(local void*, ...), |
1607 | | /// uint size0, ...) |
1608 | 0 | static bool SemaOpenCLBuiltinEnqueueKernel(Sema &S, CallExpr *TheCall) { |
1609 | 0 | unsigned NumArgs = TheCall->getNumArgs(); |
1610 | |
|
1611 | 0 | if (NumArgs < 4) { |
1612 | 0 | S.Diag(TheCall->getBeginLoc(), |
1613 | 0 | diag::err_typecheck_call_too_few_args_at_least) |
1614 | 0 | << 0 << 4 << NumArgs << /*is non object*/ 0; |
1615 | 0 | return true; |
1616 | 0 | } |
1617 | | |
1618 | 0 | Expr *Arg0 = TheCall->getArg(0); |
1619 | 0 | Expr *Arg1 = TheCall->getArg(1); |
1620 | 0 | Expr *Arg2 = TheCall->getArg(2); |
1621 | 0 | Expr *Arg3 = TheCall->getArg(3); |
1622 | | |
1623 | | // First argument always needs to be a queue_t type. |
1624 | 0 | if (!Arg0->getType()->isQueueT()) { |
1625 | 0 | S.Diag(TheCall->getArg(0)->getBeginLoc(), |
1626 | 0 | diag::err_opencl_builtin_expected_type) |
1627 | 0 | << TheCall->getDirectCallee() << S.Context.OCLQueueTy; |
1628 | 0 | return true; |
1629 | 0 | } |
1630 | | |
1631 | | // Second argument always needs to be a kernel_enqueue_flags_t enum value. |
1632 | 0 | if (!Arg1->getType()->isIntegerType()) { |
1633 | 0 | S.Diag(TheCall->getArg(1)->getBeginLoc(), |
1634 | 0 | diag::err_opencl_builtin_expected_type) |
1635 | 0 | << TheCall->getDirectCallee() << "'kernel_enqueue_flags_t' (i.e. uint)"; |
1636 | 0 | return true; |
1637 | 0 | } |
1638 | | |
1639 | | // Third argument is always an ndrange_t type. |
1640 | 0 | if (Arg2->getType().getUnqualifiedType().getAsString() != "ndrange_t") { |
1641 | 0 | S.Diag(TheCall->getArg(2)->getBeginLoc(), |
1642 | 0 | diag::err_opencl_builtin_expected_type) |
1643 | 0 | << TheCall->getDirectCallee() << "'ndrange_t'"; |
1644 | 0 | return true; |
1645 | 0 | } |
1646 | | |
1647 | | // With four arguments, there is only one form that the function could be |
1648 | | // called in: no events and no variable arguments. |
1649 | 0 | if (NumArgs == 4) { |
1650 | | // check that the last argument is the right block type. |
1651 | 0 | if (!isBlockPointer(Arg3)) { |
1652 | 0 | S.Diag(Arg3->getBeginLoc(), diag::err_opencl_builtin_expected_type) |
1653 | 0 | << TheCall->getDirectCallee() << "block"; |
1654 | 0 | return true; |
1655 | 0 | } |
1656 | | // we have a block type, check the prototype |
1657 | 0 | const BlockPointerType *BPT = |
1658 | 0 | cast<BlockPointerType>(Arg3->getType().getCanonicalType()); |
1659 | 0 | if (BPT->getPointeeType()->castAs<FunctionProtoType>()->getNumParams() > 0) { |
1660 | 0 | S.Diag(Arg3->getBeginLoc(), |
1661 | 0 | diag::err_opencl_enqueue_kernel_blocks_no_args); |
1662 | 0 | return true; |
1663 | 0 | } |
1664 | 0 | return false; |
1665 | 0 | } |
1666 | | // we can have block + varargs. |
1667 | 0 | if (isBlockPointer(Arg3)) |
1668 | 0 | return (checkOpenCLBlockArgs(S, Arg3) || |
1669 | 0 | checkOpenCLEnqueueVariadicArgs(S, TheCall, Arg3, 4)); |
1670 | | // last two cases with either exactly 7 args or 7 args and varargs. |
1671 | 0 | if (NumArgs >= 7) { |
1672 | | // check common block argument. |
1673 | 0 | Expr *Arg6 = TheCall->getArg(6); |
1674 | 0 | if (!isBlockPointer(Arg6)) { |
1675 | 0 | S.Diag(Arg6->getBeginLoc(), diag::err_opencl_builtin_expected_type) |
1676 | 0 | << TheCall->getDirectCallee() << "block"; |
1677 | 0 | return true; |
1678 | 0 | } |
1679 | 0 | if (checkOpenCLBlockArgs(S, Arg6)) |
1680 | 0 | return true; |
1681 | | |
1682 | | // Forth argument has to be any integer type. |
1683 | 0 | if (!Arg3->getType()->isIntegerType()) { |
1684 | 0 | S.Diag(TheCall->getArg(3)->getBeginLoc(), |
1685 | 0 | diag::err_opencl_builtin_expected_type) |
1686 | 0 | << TheCall->getDirectCallee() << "integer"; |
1687 | 0 | return true; |
1688 | 0 | } |
1689 | | // check remaining common arguments. |
1690 | 0 | Expr *Arg4 = TheCall->getArg(4); |
1691 | 0 | Expr *Arg5 = TheCall->getArg(5); |
1692 | | |
1693 | | // Fifth argument is always passed as a pointer to clk_event_t. |
1694 | 0 | if (!Arg4->isNullPointerConstant(S.Context, |
1695 | 0 | Expr::NPC_ValueDependentIsNotNull) && |
1696 | 0 | !Arg4->getType()->getPointeeOrArrayElementType()->isClkEventT()) { |
1697 | 0 | S.Diag(TheCall->getArg(4)->getBeginLoc(), |
1698 | 0 | diag::err_opencl_builtin_expected_type) |
1699 | 0 | << TheCall->getDirectCallee() |
1700 | 0 | << S.Context.getPointerType(S.Context.OCLClkEventTy); |
1701 | 0 | return true; |
1702 | 0 | } |
1703 | | |
1704 | | // Sixth argument is always passed as a pointer to clk_event_t. |
1705 | 0 | if (!Arg5->isNullPointerConstant(S.Context, |
1706 | 0 | Expr::NPC_ValueDependentIsNotNull) && |
1707 | 0 | !(Arg5->getType()->isPointerType() && |
1708 | 0 | Arg5->getType()->getPointeeType()->isClkEventT())) { |
1709 | 0 | S.Diag(TheCall->getArg(5)->getBeginLoc(), |
1710 | 0 | diag::err_opencl_builtin_expected_type) |
1711 | 0 | << TheCall->getDirectCallee() |
1712 | 0 | << S.Context.getPointerType(S.Context.OCLClkEventTy); |
1713 | 0 | return true; |
1714 | 0 | } |
1715 | | |
1716 | 0 | if (NumArgs == 7) |
1717 | 0 | return false; |
1718 | | |
1719 | 0 | return checkOpenCLEnqueueVariadicArgs(S, TheCall, Arg6, 7); |
1720 | 0 | } |
1721 | | |
1722 | | // None of the specific case has been detected, give generic error |
1723 | 0 | S.Diag(TheCall->getBeginLoc(), |
1724 | 0 | diag::err_opencl_enqueue_kernel_incorrect_args); |
1725 | 0 | return true; |
1726 | 0 | } |
1727 | | |
1728 | | /// Returns OpenCL access qual. |
1729 | 0 | static OpenCLAccessAttr *getOpenCLArgAccess(const Decl *D) { |
1730 | 0 | return D->getAttr<OpenCLAccessAttr>(); |
1731 | 0 | } |
1732 | | |
1733 | | /// Returns true if pipe element type is different from the pointer. |
1734 | 0 | static bool checkOpenCLPipeArg(Sema &S, CallExpr *Call) { |
1735 | 0 | const Expr *Arg0 = Call->getArg(0); |
1736 | | // First argument type should always be pipe. |
1737 | 0 | if (!Arg0->getType()->isPipeType()) { |
1738 | 0 | S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_first_arg) |
1739 | 0 | << Call->getDirectCallee() << Arg0->getSourceRange(); |
1740 | 0 | return true; |
1741 | 0 | } |
1742 | 0 | OpenCLAccessAttr *AccessQual = |
1743 | 0 | getOpenCLArgAccess(cast<DeclRefExpr>(Arg0)->getDecl()); |
1744 | | // Validates the access qualifier is compatible with the call. |
1745 | | // OpenCL v2.0 s6.13.16 - The access qualifiers for pipe should only be |
1746 | | // read_only and write_only, and assumed to be read_only if no qualifier is |
1747 | | // specified. |
1748 | 0 | switch (Call->getDirectCallee()->getBuiltinID()) { |
1749 | 0 | case Builtin::BIread_pipe: |
1750 | 0 | case Builtin::BIreserve_read_pipe: |
1751 | 0 | case Builtin::BIcommit_read_pipe: |
1752 | 0 | case Builtin::BIwork_group_reserve_read_pipe: |
1753 | 0 | case Builtin::BIsub_group_reserve_read_pipe: |
1754 | 0 | case Builtin::BIwork_group_commit_read_pipe: |
1755 | 0 | case Builtin::BIsub_group_commit_read_pipe: |
1756 | 0 | if (!(!AccessQual || AccessQual->isReadOnly())) { |
1757 | 0 | S.Diag(Arg0->getBeginLoc(), |
1758 | 0 | diag::err_opencl_builtin_pipe_invalid_access_modifier) |
1759 | 0 | << "read_only" << Arg0->getSourceRange(); |
1760 | 0 | return true; |
1761 | 0 | } |
1762 | 0 | break; |
1763 | 0 | case Builtin::BIwrite_pipe: |
1764 | 0 | case Builtin::BIreserve_write_pipe: |
1765 | 0 | case Builtin::BIcommit_write_pipe: |
1766 | 0 | case Builtin::BIwork_group_reserve_write_pipe: |
1767 | 0 | case Builtin::BIsub_group_reserve_write_pipe: |
1768 | 0 | case Builtin::BIwork_group_commit_write_pipe: |
1769 | 0 | case Builtin::BIsub_group_commit_write_pipe: |
1770 | 0 | if (!(AccessQual && AccessQual->isWriteOnly())) { |
1771 | 0 | S.Diag(Arg0->getBeginLoc(), |
1772 | 0 | diag::err_opencl_builtin_pipe_invalid_access_modifier) |
1773 | 0 | << "write_only" << Arg0->getSourceRange(); |
1774 | 0 | return true; |
1775 | 0 | } |
1776 | 0 | break; |
1777 | 0 | default: |
1778 | 0 | break; |
1779 | 0 | } |
1780 | 0 | return false; |
1781 | 0 | } |
1782 | | |
1783 | | /// Returns true if pipe element type is different from the pointer. |
1784 | 0 | static bool checkOpenCLPipePacketType(Sema &S, CallExpr *Call, unsigned Idx) { |
1785 | 0 | const Expr *Arg0 = Call->getArg(0); |
1786 | 0 | const Expr *ArgIdx = Call->getArg(Idx); |
1787 | 0 | const PipeType *PipeTy = cast<PipeType>(Arg0->getType()); |
1788 | 0 | const QualType EltTy = PipeTy->getElementType(); |
1789 | 0 | const PointerType *ArgTy = ArgIdx->getType()->getAs<PointerType>(); |
1790 | | // The Idx argument should be a pointer and the type of the pointer and |
1791 | | // the type of pipe element should also be the same. |
1792 | 0 | if (!ArgTy || |
1793 | 0 | !S.Context.hasSameType( |
1794 | 0 | EltTy, ArgTy->getPointeeType()->getCanonicalTypeInternal())) { |
1795 | 0 | S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg) |
1796 | 0 | << Call->getDirectCallee() << S.Context.getPointerType(EltTy) |
1797 | 0 | << ArgIdx->getType() << ArgIdx->getSourceRange(); |
1798 | 0 | return true; |
1799 | 0 | } |
1800 | 0 | return false; |
1801 | 0 | } |
1802 | | |
1803 | | // Performs semantic analysis for the read/write_pipe call. |
1804 | | // \param S Reference to the semantic analyzer. |
1805 | | // \param Call A pointer to the builtin call. |
1806 | | // \return True if a semantic error has been found, false otherwise. |
1807 | 0 | static bool SemaBuiltinRWPipe(Sema &S, CallExpr *Call) { |
1808 | | // OpenCL v2.0 s6.13.16.2 - The built-in read/write |
1809 | | // functions have two forms. |
1810 | 0 | switch (Call->getNumArgs()) { |
1811 | 0 | case 2: |
1812 | 0 | if (checkOpenCLPipeArg(S, Call)) |
1813 | 0 | return true; |
1814 | | // The call with 2 arguments should be |
1815 | | // read/write_pipe(pipe T, T*). |
1816 | | // Check packet type T. |
1817 | 0 | if (checkOpenCLPipePacketType(S, Call, 1)) |
1818 | 0 | return true; |
1819 | 0 | break; |
1820 | | |
1821 | 0 | case 4: { |
1822 | 0 | if (checkOpenCLPipeArg(S, Call)) |
1823 | 0 | return true; |
1824 | | // The call with 4 arguments should be |
1825 | | // read/write_pipe(pipe T, reserve_id_t, uint, T*). |
1826 | | // Check reserve_id_t. |
1827 | 0 | if (!Call->getArg(1)->getType()->isReserveIDT()) { |
1828 | 0 | S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg) |
1829 | 0 | << Call->getDirectCallee() << S.Context.OCLReserveIDTy |
1830 | 0 | << Call->getArg(1)->getType() << Call->getArg(1)->getSourceRange(); |
1831 | 0 | return true; |
1832 | 0 | } |
1833 | | |
1834 | | // Check the index. |
1835 | 0 | const Expr *Arg2 = Call->getArg(2); |
1836 | 0 | if (!Arg2->getType()->isIntegerType() && |
1837 | 0 | !Arg2->getType()->isUnsignedIntegerType()) { |
1838 | 0 | S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg) |
1839 | 0 | << Call->getDirectCallee() << S.Context.UnsignedIntTy |
1840 | 0 | << Arg2->getType() << Arg2->getSourceRange(); |
1841 | 0 | return true; |
1842 | 0 | } |
1843 | | |
1844 | | // Check packet type T. |
1845 | 0 | if (checkOpenCLPipePacketType(S, Call, 3)) |
1846 | 0 | return true; |
1847 | 0 | } break; |
1848 | 0 | default: |
1849 | 0 | S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_arg_num) |
1850 | 0 | << Call->getDirectCallee() << Call->getSourceRange(); |
1851 | 0 | return true; |
1852 | 0 | } |
1853 | | |
1854 | 0 | return false; |
1855 | 0 | } |
1856 | | |
1857 | | // Performs a semantic analysis on the {work_group_/sub_group_ |
1858 | | // /_}reserve_{read/write}_pipe |
1859 | | // \param S Reference to the semantic analyzer. |
1860 | | // \param Call The call to the builtin function to be analyzed. |
1861 | | // \return True if a semantic error was found, false otherwise. |
1862 | 0 | static bool SemaBuiltinReserveRWPipe(Sema &S, CallExpr *Call) { |
1863 | 0 | if (checkArgCount(S, Call, 2)) |
1864 | 0 | return true; |
1865 | | |
1866 | 0 | if (checkOpenCLPipeArg(S, Call)) |
1867 | 0 | return true; |
1868 | | |
1869 | | // Check the reserve size. |
1870 | 0 | if (!Call->getArg(1)->getType()->isIntegerType() && |
1871 | 0 | !Call->getArg(1)->getType()->isUnsignedIntegerType()) { |
1872 | 0 | S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg) |
1873 | 0 | << Call->getDirectCallee() << S.Context.UnsignedIntTy |
1874 | 0 | << Call->getArg(1)->getType() << Call->getArg(1)->getSourceRange(); |
1875 | 0 | return true; |
1876 | 0 | } |
1877 | | |
1878 | | // Since return type of reserve_read/write_pipe built-in function is |
1879 | | // reserve_id_t, which is not defined in the builtin def file , we used int |
1880 | | // as return type and need to override the return type of these functions. |
1881 | 0 | Call->setType(S.Context.OCLReserveIDTy); |
1882 | |
|
1883 | 0 | return false; |
1884 | 0 | } |
1885 | | |
1886 | | // Performs a semantic analysis on {work_group_/sub_group_ |
1887 | | // /_}commit_{read/write}_pipe |
1888 | | // \param S Reference to the semantic analyzer. |
1889 | | // \param Call The call to the builtin function to be analyzed. |
1890 | | // \return True if a semantic error was found, false otherwise. |
1891 | 0 | static bool SemaBuiltinCommitRWPipe(Sema &S, CallExpr *Call) { |
1892 | 0 | if (checkArgCount(S, Call, 2)) |
1893 | 0 | return true; |
1894 | | |
1895 | 0 | if (checkOpenCLPipeArg(S, Call)) |
1896 | 0 | return true; |
1897 | | |
1898 | | // Check reserve_id_t. |
1899 | 0 | if (!Call->getArg(1)->getType()->isReserveIDT()) { |
1900 | 0 | S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg) |
1901 | 0 | << Call->getDirectCallee() << S.Context.OCLReserveIDTy |
1902 | 0 | << Call->getArg(1)->getType() << Call->getArg(1)->getSourceRange(); |
1903 | 0 | return true; |
1904 | 0 | } |
1905 | | |
1906 | 0 | return false; |
1907 | 0 | } |
1908 | | |
1909 | | // Performs a semantic analysis on the call to built-in Pipe |
1910 | | // Query Functions. |
1911 | | // \param S Reference to the semantic analyzer. |
1912 | | // \param Call The call to the builtin function to be analyzed. |
1913 | | // \return True if a semantic error was found, false otherwise. |
1914 | 0 | static bool SemaBuiltinPipePackets(Sema &S, CallExpr *Call) { |
1915 | 0 | if (checkArgCount(S, Call, 1)) |
1916 | 0 | return true; |
1917 | | |
1918 | 0 | if (!Call->getArg(0)->getType()->isPipeType()) { |
1919 | 0 | S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_first_arg) |
1920 | 0 | << Call->getDirectCallee() << Call->getArg(0)->getSourceRange(); |
1921 | 0 | return true; |
1922 | 0 | } |
1923 | | |
1924 | 0 | return false; |
1925 | 0 | } |
1926 | | |
1927 | | // OpenCL v2.0 s6.13.9 - Address space qualifier functions. |
1928 | | // Performs semantic analysis for the to_global/local/private call. |
1929 | | // \param S Reference to the semantic analyzer. |
1930 | | // \param BuiltinID ID of the builtin function. |
1931 | | // \param Call A pointer to the builtin call. |
1932 | | // \return True if a semantic error has been found, false otherwise. |
1933 | | static bool SemaOpenCLBuiltinToAddr(Sema &S, unsigned BuiltinID, |
1934 | 0 | CallExpr *Call) { |
1935 | 0 | if (checkArgCount(S, Call, 1)) |
1936 | 0 | return true; |
1937 | | |
1938 | 0 | auto RT = Call->getArg(0)->getType(); |
1939 | 0 | if (!RT->isPointerType() || RT->getPointeeType() |
1940 | 0 | .getAddressSpace() == LangAS::opencl_constant) { |
1941 | 0 | S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_to_addr_invalid_arg) |
1942 | 0 | << Call->getArg(0) << Call->getDirectCallee() << Call->getSourceRange(); |
1943 | 0 | return true; |
1944 | 0 | } |
1945 | | |
1946 | 0 | if (RT->getPointeeType().getAddressSpace() != LangAS::opencl_generic) { |
1947 | 0 | S.Diag(Call->getArg(0)->getBeginLoc(), |
1948 | 0 | diag::warn_opencl_generic_address_space_arg) |
1949 | 0 | << Call->getDirectCallee()->getNameInfo().getAsString() |
1950 | 0 | << Call->getArg(0)->getSourceRange(); |
1951 | 0 | } |
1952 | |
|
1953 | 0 | RT = RT->getPointeeType(); |
1954 | 0 | auto Qual = RT.getQualifiers(); |
1955 | 0 | switch (BuiltinID) { |
1956 | 0 | case Builtin::BIto_global: |
1957 | 0 | Qual.setAddressSpace(LangAS::opencl_global); |
1958 | 0 | break; |
1959 | 0 | case Builtin::BIto_local: |
1960 | 0 | Qual.setAddressSpace(LangAS::opencl_local); |
1961 | 0 | break; |
1962 | 0 | case Builtin::BIto_private: |
1963 | 0 | Qual.setAddressSpace(LangAS::opencl_private); |
1964 | 0 | break; |
1965 | 0 | default: |
1966 | 0 | llvm_unreachable("Invalid builtin function"); |
1967 | 0 | } |
1968 | 0 | Call->setType(S.Context.getPointerType(S.Context.getQualifiedType( |
1969 | 0 | RT.getUnqualifiedType(), Qual))); |
1970 | |
|
1971 | 0 | return false; |
1972 | 0 | } |
1973 | | |
1974 | 0 | static ExprResult SemaBuiltinLaunder(Sema &S, CallExpr *TheCall) { |
1975 | 0 | if (checkArgCount(S, TheCall, 1)) |
1976 | 0 | return ExprError(); |
1977 | | |
1978 | | // Compute __builtin_launder's parameter type from the argument. |
1979 | | // The parameter type is: |
1980 | | // * The type of the argument if it's not an array or function type, |
1981 | | // Otherwise, |
1982 | | // * The decayed argument type. |
1983 | 0 | QualType ParamTy = [&]() { |
1984 | 0 | QualType ArgTy = TheCall->getArg(0)->getType(); |
1985 | 0 | if (const ArrayType *Ty = ArgTy->getAsArrayTypeUnsafe()) |
1986 | 0 | return S.Context.getPointerType(Ty->getElementType()); |
1987 | 0 | if (ArgTy->isFunctionType()) { |
1988 | 0 | return S.Context.getPointerType(ArgTy); |
1989 | 0 | } |
1990 | 0 | return ArgTy; |
1991 | 0 | }(); |
1992 | |
|
1993 | 0 | TheCall->setType(ParamTy); |
1994 | |
|
1995 | 0 | auto DiagSelect = [&]() -> std::optional<unsigned> { |
1996 | 0 | if (!ParamTy->isPointerType()) |
1997 | 0 | return 0; |
1998 | 0 | if (ParamTy->isFunctionPointerType()) |
1999 | 0 | return 1; |
2000 | 0 | if (ParamTy->isVoidPointerType()) |
2001 | 0 | return 2; |
2002 | 0 | return std::optional<unsigned>{}; |
2003 | 0 | }(); |
2004 | 0 | if (DiagSelect) { |
2005 | 0 | S.Diag(TheCall->getBeginLoc(), diag::err_builtin_launder_invalid_arg) |
2006 | 0 | << *DiagSelect << TheCall->getSourceRange(); |
2007 | 0 | return ExprError(); |
2008 | 0 | } |
2009 | | |
2010 | | // We either have an incomplete class type, or we have a class template |
2011 | | // whose instantiation has not been forced. Example: |
2012 | | // |
2013 | | // template <class T> struct Foo { T value; }; |
2014 | | // Foo<int> *p = nullptr; |
2015 | | // auto *d = __builtin_launder(p); |
2016 | 0 | if (S.RequireCompleteType(TheCall->getBeginLoc(), ParamTy->getPointeeType(), |
2017 | 0 | diag::err_incomplete_type)) |
2018 | 0 | return ExprError(); |
2019 | | |
2020 | 0 | assert(ParamTy->getPointeeType()->isObjectType() && |
2021 | 0 | "Unhandled non-object pointer case"); |
2022 | | |
2023 | 0 | InitializedEntity Entity = |
2024 | 0 | InitializedEntity::InitializeParameter(S.Context, ParamTy, false); |
2025 | 0 | ExprResult Arg = |
2026 | 0 | S.PerformCopyInitialization(Entity, SourceLocation(), TheCall->getArg(0)); |
2027 | 0 | if (Arg.isInvalid()) |
2028 | 0 | return ExprError(); |
2029 | 0 | TheCall->setArg(0, Arg.get()); |
2030 | |
|
2031 | 0 | return TheCall; |
2032 | 0 | } |
2033 | | |
2034 | | // Emit an error and return true if the current object format type is in the |
2035 | | // list of unsupported types. |
2036 | | static bool CheckBuiltinTargetNotInUnsupported( |
2037 | | Sema &S, unsigned BuiltinID, CallExpr *TheCall, |
2038 | 0 | ArrayRef<llvm::Triple::ObjectFormatType> UnsupportedObjectFormatTypes) { |
2039 | 0 | llvm::Triple::ObjectFormatType CurObjFormat = |
2040 | 0 | S.getASTContext().getTargetInfo().getTriple().getObjectFormat(); |
2041 | 0 | if (llvm::is_contained(UnsupportedObjectFormatTypes, CurObjFormat)) { |
2042 | 0 | S.Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported) |
2043 | 0 | << TheCall->getSourceRange(); |
2044 | 0 | return true; |
2045 | 0 | } |
2046 | 0 | return false; |
2047 | 0 | } |
2048 | | |
2049 | | // Emit an error and return true if the current architecture is not in the list |
2050 | | // of supported architectures. |
2051 | | static bool |
2052 | | CheckBuiltinTargetInSupported(Sema &S, unsigned BuiltinID, CallExpr *TheCall, |
2053 | 0 | ArrayRef<llvm::Triple::ArchType> SupportedArchs) { |
2054 | 0 | llvm::Triple::ArchType CurArch = |
2055 | 0 | S.getASTContext().getTargetInfo().getTriple().getArch(); |
2056 | 0 | if (llvm::is_contained(SupportedArchs, CurArch)) |
2057 | 0 | return false; |
2058 | 0 | S.Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported) |
2059 | 0 | << TheCall->getSourceRange(); |
2060 | 0 | return true; |
2061 | 0 | } |
2062 | | |
2063 | | static void CheckNonNullArgument(Sema &S, const Expr *ArgExpr, |
2064 | | SourceLocation CallSiteLoc); |
2065 | | |
2066 | | bool Sema::CheckTSBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID, |
2067 | 0 | CallExpr *TheCall) { |
2068 | 0 | switch (TI.getTriple().getArch()) { |
2069 | 0 | default: |
2070 | | // Some builtins don't require additional checking, so just consider these |
2071 | | // acceptable. |
2072 | 0 | return false; |
2073 | 0 | case llvm::Triple::arm: |
2074 | 0 | case llvm::Triple::armeb: |
2075 | 0 | case llvm::Triple::thumb: |
2076 | 0 | case llvm::Triple::thumbeb: |
2077 | 0 | return CheckARMBuiltinFunctionCall(TI, BuiltinID, TheCall); |
2078 | 0 | case llvm::Triple::aarch64: |
2079 | 0 | case llvm::Triple::aarch64_32: |
2080 | 0 | case llvm::Triple::aarch64_be: |
2081 | 0 | return CheckAArch64BuiltinFunctionCall(TI, BuiltinID, TheCall); |
2082 | 0 | case llvm::Triple::bpfeb: |
2083 | 0 | case llvm::Triple::bpfel: |
2084 | 0 | return CheckBPFBuiltinFunctionCall(BuiltinID, TheCall); |
2085 | 0 | case llvm::Triple::hexagon: |
2086 | 0 | return CheckHexagonBuiltinFunctionCall(BuiltinID, TheCall); |
2087 | 0 | case llvm::Triple::mips: |
2088 | 0 | case llvm::Triple::mipsel: |
2089 | 0 | case llvm::Triple::mips64: |
2090 | 0 | case llvm::Triple::mips64el: |
2091 | 0 | return CheckMipsBuiltinFunctionCall(TI, BuiltinID, TheCall); |
2092 | 0 | case llvm::Triple::systemz: |
2093 | 0 | return CheckSystemZBuiltinFunctionCall(BuiltinID, TheCall); |
2094 | 0 | case llvm::Triple::x86: |
2095 | 0 | case llvm::Triple::x86_64: |
2096 | 0 | return CheckX86BuiltinFunctionCall(TI, BuiltinID, TheCall); |
2097 | 0 | case llvm::Triple::ppc: |
2098 | 0 | case llvm::Triple::ppcle: |
2099 | 0 | case llvm::Triple::ppc64: |
2100 | 0 | case llvm::Triple::ppc64le: |
2101 | 0 | return CheckPPCBuiltinFunctionCall(TI, BuiltinID, TheCall); |
2102 | 0 | case llvm::Triple::amdgcn: |
2103 | 0 | return CheckAMDGCNBuiltinFunctionCall(BuiltinID, TheCall); |
2104 | 0 | case llvm::Triple::riscv32: |
2105 | 0 | case llvm::Triple::riscv64: |
2106 | 0 | return CheckRISCVBuiltinFunctionCall(TI, BuiltinID, TheCall); |
2107 | 0 | case llvm::Triple::loongarch32: |
2108 | 0 | case llvm::Triple::loongarch64: |
2109 | 0 | return CheckLoongArchBuiltinFunctionCall(TI, BuiltinID, TheCall); |
2110 | 0 | case llvm::Triple::wasm32: |
2111 | 0 | case llvm::Triple::wasm64: |
2112 | 0 | return CheckWebAssemblyBuiltinFunctionCall(TI, BuiltinID, TheCall); |
2113 | 0 | case llvm::Triple::nvptx: |
2114 | 0 | case llvm::Triple::nvptx64: |
2115 | 0 | return CheckNVPTXBuiltinFunctionCall(TI, BuiltinID, TheCall); |
2116 | 0 | } |
2117 | 0 | } |
2118 | | |
2119 | | // Check if \p Ty is a valid type for the elementwise math builtins. If it is |
2120 | | // not a valid type, emit an error message and return true. Otherwise return |
2121 | | // false. |
2122 | | static bool checkMathBuiltinElementType(Sema &S, SourceLocation Loc, |
2123 | 0 | QualType Ty) { |
2124 | 0 | if (!Ty->getAs<VectorType>() && !ConstantMatrixType::isValidElementType(Ty)) { |
2125 | 0 | return S.Diag(Loc, diag::err_builtin_invalid_arg_type) |
2126 | 0 | << 1 << /* vector, integer or float ty*/ 0 << Ty; |
2127 | 0 | } |
2128 | | |
2129 | 0 | return false; |
2130 | 0 | } |
2131 | | |
2132 | | static bool checkFPMathBuiltinElementType(Sema &S, SourceLocation Loc, |
2133 | 0 | QualType ArgTy, int ArgIndex) { |
2134 | 0 | QualType EltTy = ArgTy; |
2135 | 0 | if (auto *VecTy = EltTy->getAs<VectorType>()) |
2136 | 0 | EltTy = VecTy->getElementType(); |
2137 | |
|
2138 | 0 | if (!EltTy->isRealFloatingType()) { |
2139 | 0 | return S.Diag(Loc, diag::err_builtin_invalid_arg_type) |
2140 | 0 | << ArgIndex << /* vector or float ty*/ 5 << ArgTy; |
2141 | 0 | } |
2142 | | |
2143 | 0 | return false; |
2144 | 0 | } |
2145 | | |
2146 | | ExprResult |
2147 | | Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID, |
2148 | 0 | CallExpr *TheCall) { |
2149 | 0 | ExprResult TheCallResult(TheCall); |
2150 | | |
2151 | | // Find out if any arguments are required to be integer constant expressions. |
2152 | 0 | unsigned ICEArguments = 0; |
2153 | 0 | ASTContext::GetBuiltinTypeError Error; |
2154 | 0 | Context.GetBuiltinType(BuiltinID, Error, &ICEArguments); |
2155 | 0 | if (Error != ASTContext::GE_None) |
2156 | 0 | ICEArguments = 0; // Don't diagnose previously diagnosed errors. |
2157 | | |
2158 | | // If any arguments are required to be ICE's, check and diagnose. |
2159 | 0 | for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) { |
2160 | | // Skip arguments not required to be ICE's. |
2161 | 0 | if ((ICEArguments & (1 << ArgNo)) == 0) continue; |
2162 | | |
2163 | 0 | llvm::APSInt Result; |
2164 | | // If we don't have enough arguments, continue so we can issue better |
2165 | | // diagnostic in checkArgCount(...) |
2166 | 0 | if (ArgNo < TheCall->getNumArgs() && |
2167 | 0 | SemaBuiltinConstantArg(TheCall, ArgNo, Result)) |
2168 | 0 | return true; |
2169 | 0 | ICEArguments &= ~(1 << ArgNo); |
2170 | 0 | } |
2171 | | |
2172 | 0 | switch (BuiltinID) { |
2173 | 0 | case Builtin::BI__builtin___CFStringMakeConstantString: |
2174 | | // CFStringMakeConstantString is currently not implemented for GOFF (i.e., |
2175 | | // on z/OS) and for XCOFF (i.e., on AIX). Emit unsupported |
2176 | 0 | if (CheckBuiltinTargetNotInUnsupported( |
2177 | 0 | *this, BuiltinID, TheCall, |
2178 | 0 | {llvm::Triple::GOFF, llvm::Triple::XCOFF})) |
2179 | 0 | return ExprError(); |
2180 | 0 | assert(TheCall->getNumArgs() == 1 && |
2181 | 0 | "Wrong # arguments to builtin CFStringMakeConstantString"); |
2182 | 0 | if (CheckObjCString(TheCall->getArg(0))) |
2183 | 0 | return ExprError(); |
2184 | 0 | break; |
2185 | 0 | case Builtin::BI__builtin_ms_va_start: |
2186 | 0 | case Builtin::BI__builtin_stdarg_start: |
2187 | 0 | case Builtin::BI__builtin_va_start: |
2188 | 0 | if (SemaBuiltinVAStart(BuiltinID, TheCall)) |
2189 | 0 | return ExprError(); |
2190 | 0 | break; |
2191 | 0 | case Builtin::BI__va_start: { |
2192 | 0 | switch (Context.getTargetInfo().getTriple().getArch()) { |
2193 | 0 | case llvm::Triple::aarch64: |
2194 | 0 | case llvm::Triple::arm: |
2195 | 0 | case llvm::Triple::thumb: |
2196 | 0 | if (SemaBuiltinVAStartARMMicrosoft(TheCall)) |
2197 | 0 | return ExprError(); |
2198 | 0 | break; |
2199 | 0 | default: |
2200 | 0 | if (SemaBuiltinVAStart(BuiltinID, TheCall)) |
2201 | 0 | return ExprError(); |
2202 | 0 | break; |
2203 | 0 | } |
2204 | 0 | break; |
2205 | 0 | } |
2206 | | |
2207 | | // The acquire, release, and no fence variants are ARM and AArch64 only. |
2208 | 0 | case Builtin::BI_interlockedbittestandset_acq: |
2209 | 0 | case Builtin::BI_interlockedbittestandset_rel: |
2210 | 0 | case Builtin::BI_interlockedbittestandset_nf: |
2211 | 0 | case Builtin::BI_interlockedbittestandreset_acq: |
2212 | 0 | case Builtin::BI_interlockedbittestandreset_rel: |
2213 | 0 | case Builtin::BI_interlockedbittestandreset_nf: |
2214 | 0 | if (CheckBuiltinTargetInSupported( |
2215 | 0 | *this, BuiltinID, TheCall, |
2216 | 0 | {llvm::Triple::arm, llvm::Triple::thumb, llvm::Triple::aarch64})) |
2217 | 0 | return ExprError(); |
2218 | 0 | break; |
2219 | | |
2220 | | // The 64-bit bittest variants are x64, ARM, and AArch64 only. |
2221 | 0 | case Builtin::BI_bittest64: |
2222 | 0 | case Builtin::BI_bittestandcomplement64: |
2223 | 0 | case Builtin::BI_bittestandreset64: |
2224 | 0 | case Builtin::BI_bittestandset64: |
2225 | 0 | case Builtin::BI_interlockedbittestandreset64: |
2226 | 0 | case Builtin::BI_interlockedbittestandset64: |
2227 | 0 | if (CheckBuiltinTargetInSupported(*this, BuiltinID, TheCall, |
2228 | 0 | {llvm::Triple::x86_64, llvm::Triple::arm, |
2229 | 0 | llvm::Triple::thumb, |
2230 | 0 | llvm::Triple::aarch64})) |
2231 | 0 | return ExprError(); |
2232 | 0 | break; |
2233 | | |
2234 | 0 | case Builtin::BI__builtin_set_flt_rounds: |
2235 | 0 | if (CheckBuiltinTargetInSupported(*this, BuiltinID, TheCall, |
2236 | 0 | {llvm::Triple::x86, llvm::Triple::x86_64, |
2237 | 0 | llvm::Triple::arm, llvm::Triple::thumb, |
2238 | 0 | llvm::Triple::aarch64})) |
2239 | 0 | return ExprError(); |
2240 | 0 | break; |
2241 | | |
2242 | 0 | case Builtin::BI__builtin_isgreater: |
2243 | 0 | case Builtin::BI__builtin_isgreaterequal: |
2244 | 0 | case Builtin::BI__builtin_isless: |
2245 | 0 | case Builtin::BI__builtin_islessequal: |
2246 | 0 | case Builtin::BI__builtin_islessgreater: |
2247 | 0 | case Builtin::BI__builtin_isunordered: |
2248 | 0 | if (SemaBuiltinUnorderedCompare(TheCall)) |
2249 | 0 | return ExprError(); |
2250 | 0 | break; |
2251 | 0 | case Builtin::BI__builtin_fpclassify: |
2252 | 0 | if (SemaBuiltinFPClassification(TheCall, 6)) |
2253 | 0 | return ExprError(); |
2254 | 0 | break; |
2255 | 0 | case Builtin::BI__builtin_isfpclass: |
2256 | 0 | if (SemaBuiltinFPClassification(TheCall, 2)) |
2257 | 0 | return ExprError(); |
2258 | 0 | break; |
2259 | 0 | case Builtin::BI__builtin_isfinite: |
2260 | 0 | case Builtin::BI__builtin_isinf: |
2261 | 0 | case Builtin::BI__builtin_isinf_sign: |
2262 | 0 | case Builtin::BI__builtin_isnan: |
2263 | 0 | case Builtin::BI__builtin_issignaling: |
2264 | 0 | case Builtin::BI__builtin_isnormal: |
2265 | 0 | case Builtin::BI__builtin_issubnormal: |
2266 | 0 | case Builtin::BI__builtin_iszero: |
2267 | 0 | case Builtin::BI__builtin_signbit: |
2268 | 0 | case Builtin::BI__builtin_signbitf: |
2269 | 0 | case Builtin::BI__builtin_signbitl: |
2270 | 0 | if (SemaBuiltinFPClassification(TheCall, 1)) |
2271 | 0 | return ExprError(); |
2272 | 0 | break; |
2273 | 0 | case Builtin::BI__builtin_shufflevector: |
2274 | 0 | return SemaBuiltinShuffleVector(TheCall); |
2275 | | // TheCall will be freed by the smart pointer here, but that's fine, since |
2276 | | // SemaBuiltinShuffleVector guts it, but then doesn't release it. |
2277 | 0 | case Builtin::BI__builtin_prefetch: |
2278 | 0 | if (SemaBuiltinPrefetch(TheCall)) |
2279 | 0 | return ExprError(); |
2280 | 0 | break; |
2281 | 0 | case Builtin::BI__builtin_alloca_with_align: |
2282 | 0 | case Builtin::BI__builtin_alloca_with_align_uninitialized: |
2283 | 0 | if (SemaBuiltinAllocaWithAlign(TheCall)) |
2284 | 0 | return ExprError(); |
2285 | 0 | [[fallthrough]]; |
2286 | 0 | case Builtin::BI__builtin_alloca: |
2287 | 0 | case Builtin::BI__builtin_alloca_uninitialized: |
2288 | 0 | Diag(TheCall->getBeginLoc(), diag::warn_alloca) |
2289 | 0 | << TheCall->getDirectCallee(); |
2290 | 0 | break; |
2291 | 0 | case Builtin::BI__arithmetic_fence: |
2292 | 0 | if (SemaBuiltinArithmeticFence(TheCall)) |
2293 | 0 | return ExprError(); |
2294 | 0 | break; |
2295 | 0 | case Builtin::BI__assume: |
2296 | 0 | case Builtin::BI__builtin_assume: |
2297 | 0 | if (SemaBuiltinAssume(TheCall)) |
2298 | 0 | return ExprError(); |
2299 | 0 | break; |
2300 | 0 | case Builtin::BI__builtin_assume_aligned: |
2301 | 0 | if (SemaBuiltinAssumeAligned(TheCall)) |
2302 | 0 | return ExprError(); |
2303 | 0 | break; |
2304 | 0 | case Builtin::BI__builtin_dynamic_object_size: |
2305 | 0 | case Builtin::BI__builtin_object_size: |
2306 | 0 | if (SemaBuiltinConstantArgRange(TheCall, 1, 0, 3)) |
2307 | 0 | return ExprError(); |
2308 | 0 | break; |
2309 | 0 | case Builtin::BI__builtin_longjmp: |
2310 | 0 | if (SemaBuiltinLongjmp(TheCall)) |
2311 | 0 | return ExprError(); |
2312 | 0 | break; |
2313 | 0 | case Builtin::BI__builtin_setjmp: |
2314 | 0 | if (SemaBuiltinSetjmp(TheCall)) |
2315 | 0 | return ExprError(); |
2316 | 0 | break; |
2317 | 0 | case Builtin::BI__builtin_classify_type: |
2318 | 0 | if (checkArgCount(*this, TheCall, 1)) return true; |
2319 | 0 | TheCall->setType(Context.IntTy); |
2320 | 0 | break; |
2321 | 0 | case Builtin::BI__builtin_complex: |
2322 | 0 | if (SemaBuiltinComplex(TheCall)) |
2323 | 0 | return ExprError(); |
2324 | 0 | break; |
2325 | 0 | case Builtin::BI__builtin_constant_p: { |
2326 | 0 | if (checkArgCount(*this, TheCall, 1)) return true; |
2327 | 0 | ExprResult Arg = DefaultFunctionArrayLvalueConversion(TheCall->getArg(0)); |
2328 | 0 | if (Arg.isInvalid()) return true; |
2329 | 0 | TheCall->setArg(0, Arg.get()); |
2330 | 0 | TheCall->setType(Context.IntTy); |
2331 | 0 | break; |
2332 | 0 | } |
2333 | 0 | case Builtin::BI__builtin_launder: |
2334 | 0 | return SemaBuiltinLaunder(*this, TheCall); |
2335 | 0 | case Builtin::BI__sync_fetch_and_add: |
2336 | 0 | case Builtin::BI__sync_fetch_and_add_1: |
2337 | 0 | case Builtin::BI__sync_fetch_and_add_2: |
2338 | 0 | case Builtin::BI__sync_fetch_and_add_4: |
2339 | 0 | case Builtin::BI__sync_fetch_and_add_8: |
2340 | 0 | case Builtin::BI__sync_fetch_and_add_16: |
2341 | 0 | case Builtin::BI__sync_fetch_and_sub: |
2342 | 0 | case Builtin::BI__sync_fetch_and_sub_1: |
2343 | 0 | case Builtin::BI__sync_fetch_and_sub_2: |
2344 | 0 | case Builtin::BI__sync_fetch_and_sub_4: |
2345 | 0 | case Builtin::BI__sync_fetch_and_sub_8: |
2346 | 0 | case Builtin::BI__sync_fetch_and_sub_16: |
2347 | 0 | case Builtin::BI__sync_fetch_and_or: |
2348 | 0 | case Builtin::BI__sync_fetch_and_or_1: |
2349 | 0 | case Builtin::BI__sync_fetch_and_or_2: |
2350 | 0 | case Builtin::BI__sync_fetch_and_or_4: |
2351 | 0 | case Builtin::BI__sync_fetch_and_or_8: |
2352 | 0 | case Builtin::BI__sync_fetch_and_or_16: |
2353 | 0 | case Builtin::BI__sync_fetch_and_and: |
2354 | 0 | case Builtin::BI__sync_fetch_and_and_1: |
2355 | 0 | case Builtin::BI__sync_fetch_and_and_2: |
2356 | 0 | case Builtin::BI__sync_fetch_and_and_4: |
2357 | 0 | case Builtin::BI__sync_fetch_and_and_8: |
2358 | 0 | case Builtin::BI__sync_fetch_and_and_16: |
2359 | 0 | case Builtin::BI__sync_fetch_and_xor: |
2360 | 0 | case Builtin::BI__sync_fetch_and_xor_1: |
2361 | 0 | case Builtin::BI__sync_fetch_and_xor_2: |
2362 | 0 | case Builtin::BI__sync_fetch_and_xor_4: |
2363 | 0 | case Builtin::BI__sync_fetch_and_xor_8: |
2364 | 0 | case Builtin::BI__sync_fetch_and_xor_16: |
2365 | 0 | case Builtin::BI__sync_fetch_and_nand: |
2366 | 0 | case Builtin::BI__sync_fetch_and_nand_1: |
2367 | 0 | case Builtin::BI__sync_fetch_and_nand_2: |
2368 | 0 | case Builtin::BI__sync_fetch_and_nand_4: |
2369 | 0 | case Builtin::BI__sync_fetch_and_nand_8: |
2370 | 0 | case Builtin::BI__sync_fetch_and_nand_16: |
2371 | 0 | case Builtin::BI__sync_add_and_fetch: |
2372 | 0 | case Builtin::BI__sync_add_and_fetch_1: |
2373 | 0 | case Builtin::BI__sync_add_and_fetch_2: |
2374 | 0 | case Builtin::BI__sync_add_and_fetch_4: |
2375 | 0 | case Builtin::BI__sync_add_and_fetch_8: |
2376 | 0 | case Builtin::BI__sync_add_and_fetch_16: |
2377 | 0 | case Builtin::BI__sync_sub_and_fetch: |
2378 | 0 | case Builtin::BI__sync_sub_and_fetch_1: |
2379 | 0 | case Builtin::BI__sync_sub_and_fetch_2: |
2380 | 0 | case Builtin::BI__sync_sub_and_fetch_4: |
2381 | 0 | case Builtin::BI__sync_sub_and_fetch_8: |
2382 | 0 | case Builtin::BI__sync_sub_and_fetch_16: |
2383 | 0 | case Builtin::BI__sync_and_and_fetch: |
2384 | 0 | case Builtin::BI__sync_and_and_fetch_1: |
2385 | 0 | case Builtin::BI__sync_and_and_fetch_2: |
2386 | 0 | case Builtin::BI__sync_and_and_fetch_4: |
2387 | 0 | case Builtin::BI__sync_and_and_fetch_8: |
2388 | 0 | case Builtin::BI__sync_and_and_fetch_16: |
2389 | 0 | case Builtin::BI__sync_or_and_fetch: |
2390 | 0 | case Builtin::BI__sync_or_and_fetch_1: |
2391 | 0 | case Builtin::BI__sync_or_and_fetch_2: |
2392 | 0 | case Builtin::BI__sync_or_and_fetch_4: |
2393 | 0 | case Builtin::BI__sync_or_and_fetch_8: |
2394 | 0 | case Builtin::BI__sync_or_and_fetch_16: |
2395 | 0 | case Builtin::BI__sync_xor_and_fetch: |
2396 | 0 | case Builtin::BI__sync_xor_and_fetch_1: |
2397 | 0 | case Builtin::BI__sync_xor_and_fetch_2: |
2398 | 0 | case Builtin::BI__sync_xor_and_fetch_4: |
2399 | 0 | case Builtin::BI__sync_xor_and_fetch_8: |
2400 | 0 | case Builtin::BI__sync_xor_and_fetch_16: |
2401 | 0 | case Builtin::BI__sync_nand_and_fetch: |
2402 | 0 | case Builtin::BI__sync_nand_and_fetch_1: |
2403 | 0 | case Builtin::BI__sync_nand_and_fetch_2: |
2404 | 0 | case Builtin::BI__sync_nand_and_fetch_4: |
2405 | 0 | case Builtin::BI__sync_nand_and_fetch_8: |
2406 | 0 | case Builtin::BI__sync_nand_and_fetch_16: |
2407 | 0 | case Builtin::BI__sync_val_compare_and_swap: |
2408 | 0 | case Builtin::BI__sync_val_compare_and_swap_1: |
2409 | 0 | case Builtin::BI__sync_val_compare_and_swap_2: |
2410 | 0 | case Builtin::BI__sync_val_compare_and_swap_4: |
2411 | 0 | case Builtin::BI__sync_val_compare_and_swap_8: |
2412 | 0 | case Builtin::BI__sync_val_compare_and_swap_16: |
2413 | 0 | case Builtin::BI__sync_bool_compare_and_swap: |
2414 | 0 | case Builtin::BI__sync_bool_compare_and_swap_1: |
2415 | 0 | case Builtin::BI__sync_bool_compare_and_swap_2: |
2416 | 0 | case Builtin::BI__sync_bool_compare_and_swap_4: |
2417 | 0 | case Builtin::BI__sync_bool_compare_and_swap_8: |
2418 | 0 | case Builtin::BI__sync_bool_compare_and_swap_16: |
2419 | 0 | case Builtin::BI__sync_lock_test_and_set: |
2420 | 0 | case Builtin::BI__sync_lock_test_and_set_1: |
2421 | 0 | case Builtin::BI__sync_lock_test_and_set_2: |
2422 | 0 | case Builtin::BI__sync_lock_test_and_set_4: |
2423 | 0 | case Builtin::BI__sync_lock_test_and_set_8: |
2424 | 0 | case Builtin::BI__sync_lock_test_and_set_16: |
2425 | 0 | case Builtin::BI__sync_lock_release: |
2426 | 0 | case Builtin::BI__sync_lock_release_1: |
2427 | 0 | case Builtin::BI__sync_lock_release_2: |
2428 | 0 | case Builtin::BI__sync_lock_release_4: |
2429 | 0 | case Builtin::BI__sync_lock_release_8: |
2430 | 0 | case Builtin::BI__sync_lock_release_16: |
2431 | 0 | case Builtin::BI__sync_swap: |
2432 | 0 | case Builtin::BI__sync_swap_1: |
2433 | 0 | case Builtin::BI__sync_swap_2: |
2434 | 0 | case Builtin::BI__sync_swap_4: |
2435 | 0 | case Builtin::BI__sync_swap_8: |
2436 | 0 | case Builtin::BI__sync_swap_16: |
2437 | 0 | return SemaBuiltinAtomicOverloaded(TheCallResult); |
2438 | 0 | case Builtin::BI__sync_synchronize: |
2439 | 0 | Diag(TheCall->getBeginLoc(), diag::warn_atomic_implicit_seq_cst) |
2440 | 0 | << TheCall->getCallee()->getSourceRange(); |
2441 | 0 | break; |
2442 | 0 | case Builtin::BI__builtin_nontemporal_load: |
2443 | 0 | case Builtin::BI__builtin_nontemporal_store: |
2444 | 0 | return SemaBuiltinNontemporalOverloaded(TheCallResult); |
2445 | 0 | case Builtin::BI__builtin_memcpy_inline: { |
2446 | 0 | clang::Expr *SizeOp = TheCall->getArg(2); |
2447 | | // We warn about copying to or from `nullptr` pointers when `size` is |
2448 | | // greater than 0. When `size` is value dependent we cannot evaluate its |
2449 | | // value so we bail out. |
2450 | 0 | if (SizeOp->isValueDependent()) |
2451 | 0 | break; |
2452 | 0 | if (!SizeOp->EvaluateKnownConstInt(Context).isZero()) { |
2453 | 0 | CheckNonNullArgument(*this, TheCall->getArg(0), TheCall->getExprLoc()); |
2454 | 0 | CheckNonNullArgument(*this, TheCall->getArg(1), TheCall->getExprLoc()); |
2455 | 0 | } |
2456 | 0 | break; |
2457 | 0 | } |
2458 | 0 | case Builtin::BI__builtin_memset_inline: { |
2459 | 0 | clang::Expr *SizeOp = TheCall->getArg(2); |
2460 | | // We warn about filling to `nullptr` pointers when `size` is greater than |
2461 | | // 0. When `size` is value dependent we cannot evaluate its value so we bail |
2462 | | // out. |
2463 | 0 | if (SizeOp->isValueDependent()) |
2464 | 0 | break; |
2465 | 0 | if (!SizeOp->EvaluateKnownConstInt(Context).isZero()) |
2466 | 0 | CheckNonNullArgument(*this, TheCall->getArg(0), TheCall->getExprLoc()); |
2467 | 0 | break; |
2468 | 0 | } |
2469 | 0 | #define BUILTIN(ID, TYPE, ATTRS) |
2470 | 0 | #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ |
2471 | 0 | case Builtin::BI##ID: \ |
2472 | 0 | return SemaAtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID); |
2473 | 0 | #include "clang/Basic/Builtins.def" |
2474 | 0 | case Builtin::BI__annotation: |
2475 | 0 | if (SemaBuiltinMSVCAnnotation(*this, TheCall)) |
2476 | 0 | return ExprError(); |
2477 | 0 | break; |
2478 | 0 | case Builtin::BI__builtin_annotation: |
2479 | 0 | if (SemaBuiltinAnnotation(*this, TheCall)) |
2480 | 0 | return ExprError(); |
2481 | 0 | break; |
2482 | 0 | case Builtin::BI__builtin_addressof: |
2483 | 0 | if (SemaBuiltinAddressof(*this, TheCall)) |
2484 | 0 | return ExprError(); |
2485 | 0 | break; |
2486 | 0 | case Builtin::BI__builtin_function_start: |
2487 | 0 | if (SemaBuiltinFunctionStart(*this, TheCall)) |
2488 | 0 | return ExprError(); |
2489 | 0 | break; |
2490 | 0 | case Builtin::BI__builtin_is_aligned: |
2491 | 0 | case Builtin::BI__builtin_align_up: |
2492 | 0 | case Builtin::BI__builtin_align_down: |
2493 | 0 | if (SemaBuiltinAlignment(*this, TheCall, BuiltinID)) |
2494 | 0 | return ExprError(); |
2495 | 0 | break; |
2496 | 0 | case Builtin::BI__builtin_add_overflow: |
2497 | 0 | case Builtin::BI__builtin_sub_overflow: |
2498 | 0 | case Builtin::BI__builtin_mul_overflow: |
2499 | 0 | if (SemaBuiltinOverflow(*this, TheCall, BuiltinID)) |
2500 | 0 | return ExprError(); |
2501 | 0 | break; |
2502 | 0 | case Builtin::BI__builtin_operator_new: |
2503 | 0 | case Builtin::BI__builtin_operator_delete: { |
2504 | 0 | bool IsDelete = BuiltinID == Builtin::BI__builtin_operator_delete; |
2505 | 0 | ExprResult Res = |
2506 | 0 | SemaBuiltinOperatorNewDeleteOverloaded(TheCallResult, IsDelete); |
2507 | 0 | if (Res.isInvalid()) |
2508 | 0 | CorrectDelayedTyposInExpr(TheCallResult.get()); |
2509 | 0 | return Res; |
2510 | 0 | } |
2511 | 0 | case Builtin::BI__builtin_dump_struct: |
2512 | 0 | return SemaBuiltinDumpStruct(*this, TheCall); |
2513 | 0 | case Builtin::BI__builtin_expect_with_probability: { |
2514 | | // We first want to ensure we are called with 3 arguments |
2515 | 0 | if (checkArgCount(*this, TheCall, 3)) |
2516 | 0 | return ExprError(); |
2517 | | // then check probability is constant float in range [0.0, 1.0] |
2518 | 0 | const Expr *ProbArg = TheCall->getArg(2); |
2519 | 0 | SmallVector<PartialDiagnosticAt, 8> Notes; |
2520 | 0 | Expr::EvalResult Eval; |
2521 | 0 | Eval.Diag = &Notes; |
2522 | 0 | if ((!ProbArg->EvaluateAsConstantExpr(Eval, Context)) || |
2523 | 0 | !Eval.Val.isFloat()) { |
2524 | 0 | Diag(ProbArg->getBeginLoc(), diag::err_probability_not_constant_float) |
2525 | 0 | << ProbArg->getSourceRange(); |
2526 | 0 | for (const PartialDiagnosticAt &PDiag : Notes) |
2527 | 0 | Diag(PDiag.first, PDiag.second); |
2528 | 0 | return ExprError(); |
2529 | 0 | } |
2530 | 0 | llvm::APFloat Probability = Eval.Val.getFloat(); |
2531 | 0 | bool LoseInfo = false; |
2532 | 0 | Probability.convert(llvm::APFloat::IEEEdouble(), |
2533 | 0 | llvm::RoundingMode::Dynamic, &LoseInfo); |
2534 | 0 | if (!(Probability >= llvm::APFloat(0.0) && |
2535 | 0 | Probability <= llvm::APFloat(1.0))) { |
2536 | 0 | Diag(ProbArg->getBeginLoc(), diag::err_probability_out_of_range) |
2537 | 0 | << ProbArg->getSourceRange(); |
2538 | 0 | return ExprError(); |
2539 | 0 | } |
2540 | 0 | break; |
2541 | 0 | } |
2542 | 0 | case Builtin::BI__builtin_preserve_access_index: |
2543 | 0 | if (SemaBuiltinPreserveAI(*this, TheCall)) |
2544 | 0 | return ExprError(); |
2545 | 0 | break; |
2546 | 0 | case Builtin::BI__builtin_call_with_static_chain: |
2547 | 0 | if (SemaBuiltinCallWithStaticChain(*this, TheCall)) |
2548 | 0 | return ExprError(); |
2549 | 0 | break; |
2550 | 0 | case Builtin::BI__exception_code: |
2551 | 0 | case Builtin::BI_exception_code: |
2552 | 0 | if (SemaBuiltinSEHScopeCheck(*this, TheCall, Scope::SEHExceptScope, |
2553 | 0 | diag::err_seh___except_block)) |
2554 | 0 | return ExprError(); |
2555 | 0 | break; |
2556 | 0 | case Builtin::BI__exception_info: |
2557 | 0 | case Builtin::BI_exception_info: |
2558 | 0 | if (SemaBuiltinSEHScopeCheck(*this, TheCall, Scope::SEHFilterScope, |
2559 | 0 | diag::err_seh___except_filter)) |
2560 | 0 | return ExprError(); |
2561 | 0 | break; |
2562 | 0 | case Builtin::BI__GetExceptionInfo: |
2563 | 0 | if (checkArgCount(*this, TheCall, 1)) |
2564 | 0 | return ExprError(); |
2565 | | |
2566 | 0 | if (CheckCXXThrowOperand( |
2567 | 0 | TheCall->getBeginLoc(), |
2568 | 0 | Context.getExceptionObjectType(FDecl->getParamDecl(0)->getType()), |
2569 | 0 | TheCall)) |
2570 | 0 | return ExprError(); |
2571 | | |
2572 | 0 | TheCall->setType(Context.VoidPtrTy); |
2573 | 0 | break; |
2574 | 0 | case Builtin::BIaddressof: |
2575 | 0 | case Builtin::BI__addressof: |
2576 | 0 | case Builtin::BIforward: |
2577 | 0 | case Builtin::BIforward_like: |
2578 | 0 | case Builtin::BImove: |
2579 | 0 | case Builtin::BImove_if_noexcept: |
2580 | 0 | case Builtin::BIas_const: { |
2581 | | // These are all expected to be of the form |
2582 | | // T &/&&/* f(U &/&&) |
2583 | | // where T and U only differ in qualification. |
2584 | 0 | if (checkArgCount(*this, TheCall, 1)) |
2585 | 0 | return ExprError(); |
2586 | 0 | QualType Param = FDecl->getParamDecl(0)->getType(); |
2587 | 0 | QualType Result = FDecl->getReturnType(); |
2588 | 0 | bool ReturnsPointer = BuiltinID == Builtin::BIaddressof || |
2589 | 0 | BuiltinID == Builtin::BI__addressof; |
2590 | 0 | if (!(Param->isReferenceType() && |
2591 | 0 | (ReturnsPointer ? Result->isAnyPointerType() |
2592 | 0 | : Result->isReferenceType()) && |
2593 | 0 | Context.hasSameUnqualifiedType(Param->getPointeeType(), |
2594 | 0 | Result->getPointeeType()))) { |
2595 | 0 | Diag(TheCall->getBeginLoc(), diag::err_builtin_move_forward_unsupported) |
2596 | 0 | << FDecl; |
2597 | 0 | return ExprError(); |
2598 | 0 | } |
2599 | 0 | break; |
2600 | 0 | } |
2601 | | // OpenCL v2.0, s6.13.16 - Pipe functions |
2602 | 0 | case Builtin::BIread_pipe: |
2603 | 0 | case Builtin::BIwrite_pipe: |
2604 | | // Since those two functions are declared with var args, we need a semantic |
2605 | | // check for the argument. |
2606 | 0 | if (SemaBuiltinRWPipe(*this, TheCall)) |
2607 | 0 | return ExprError(); |
2608 | 0 | break; |
2609 | 0 | case Builtin::BIreserve_read_pipe: |
2610 | 0 | case Builtin::BIreserve_write_pipe: |
2611 | 0 | case Builtin::BIwork_group_reserve_read_pipe: |
2612 | 0 | case Builtin::BIwork_group_reserve_write_pipe: |
2613 | 0 | if (SemaBuiltinReserveRWPipe(*this, TheCall)) |
2614 | 0 | return ExprError(); |
2615 | 0 | break; |
2616 | 0 | case Builtin::BIsub_group_reserve_read_pipe: |
2617 | 0 | case Builtin::BIsub_group_reserve_write_pipe: |
2618 | 0 | if (checkOpenCLSubgroupExt(*this, TheCall) || |
2619 | 0 | SemaBuiltinReserveRWPipe(*this, TheCall)) |
2620 | 0 | return ExprError(); |
2621 | 0 | break; |
2622 | 0 | case Builtin::BIcommit_read_pipe: |
2623 | 0 | case Builtin::BIcommit_write_pipe: |
2624 | 0 | case Builtin::BIwork_group_commit_read_pipe: |
2625 | 0 | case Builtin::BIwork_group_commit_write_pipe: |
2626 | 0 | if (SemaBuiltinCommitRWPipe(*this, TheCall)) |
2627 | 0 | return ExprError(); |
2628 | 0 | break; |
2629 | 0 | case Builtin::BIsub_group_commit_read_pipe: |
2630 | 0 | case Builtin::BIsub_group_commit_write_pipe: |
2631 | 0 | if (checkOpenCLSubgroupExt(*this, TheCall) || |
2632 | 0 | SemaBuiltinCommitRWPipe(*this, TheCall)) |
2633 | 0 | return ExprError(); |
2634 | 0 | break; |
2635 | 0 | case Builtin::BIget_pipe_num_packets: |
2636 | 0 | case Builtin::BIget_pipe_max_packets: |
2637 | 0 | if (SemaBuiltinPipePackets(*this, TheCall)) |
2638 | 0 | return ExprError(); |
2639 | 0 | break; |
2640 | 0 | case Builtin::BIto_global: |
2641 | 0 | case Builtin::BIto_local: |
2642 | 0 | case Builtin::BIto_private: |
2643 | 0 | if (SemaOpenCLBuiltinToAddr(*this, BuiltinID, TheCall)) |
2644 | 0 | return ExprError(); |
2645 | 0 | break; |
2646 | | // OpenCL v2.0, s6.13.17 - Enqueue kernel functions. |
2647 | 0 | case Builtin::BIenqueue_kernel: |
2648 | 0 | if (SemaOpenCLBuiltinEnqueueKernel(*this, TheCall)) |
2649 | 0 | return ExprError(); |
2650 | 0 | break; |
2651 | 0 | case Builtin::BIget_kernel_work_group_size: |
2652 | 0 | case Builtin::BIget_kernel_preferred_work_group_size_multiple: |
2653 | 0 | if (SemaOpenCLBuiltinKernelWorkGroupSize(*this, TheCall)) |
2654 | 0 | return ExprError(); |
2655 | 0 | break; |
2656 | 0 | case Builtin::BIget_kernel_max_sub_group_size_for_ndrange: |
2657 | 0 | case Builtin::BIget_kernel_sub_group_count_for_ndrange: |
2658 | 0 | if (SemaOpenCLBuiltinNDRangeAndBlock(*this, TheCall)) |
2659 | 0 | return ExprError(); |
2660 | 0 | break; |
2661 | 0 | case Builtin::BI__builtin_os_log_format: |
2662 | 0 | Cleanup.setExprNeedsCleanups(true); |
2663 | 0 | [[fallthrough]]; |
2664 | 0 | case Builtin::BI__builtin_os_log_format_buffer_size: |
2665 | 0 | if (SemaBuiltinOSLogFormat(TheCall)) |
2666 | 0 | return ExprError(); |
2667 | 0 | break; |
2668 | 0 | case Builtin::BI__builtin_frame_address: |
2669 | 0 | case Builtin::BI__builtin_return_address: { |
2670 | 0 | if (SemaBuiltinConstantArgRange(TheCall, 0, 0, 0xFFFF)) |
2671 | 0 | return ExprError(); |
2672 | | |
2673 | | // -Wframe-address warning if non-zero passed to builtin |
2674 | | // return/frame address. |
2675 | 0 | Expr::EvalResult Result; |
2676 | 0 | if (!TheCall->getArg(0)->isValueDependent() && |
2677 | 0 | TheCall->getArg(0)->EvaluateAsInt(Result, getASTContext()) && |
2678 | 0 | Result.Val.getInt() != 0) |
2679 | 0 | Diag(TheCall->getBeginLoc(), diag::warn_frame_address) |
2680 | 0 | << ((BuiltinID == Builtin::BI__builtin_return_address) |
2681 | 0 | ? "__builtin_return_address" |
2682 | 0 | : "__builtin_frame_address") |
2683 | 0 | << TheCall->getSourceRange(); |
2684 | 0 | break; |
2685 | 0 | } |
2686 | | |
2687 | 0 | case Builtin::BI__builtin_nondeterministic_value: { |
2688 | 0 | if (SemaBuiltinNonDeterministicValue(TheCall)) |
2689 | 0 | return ExprError(); |
2690 | 0 | break; |
2691 | 0 | } |
2692 | | |
2693 | | // __builtin_elementwise_abs restricts the element type to signed integers or |
2694 | | // floating point types only. |
2695 | 0 | case Builtin::BI__builtin_elementwise_abs: { |
2696 | 0 | if (PrepareBuiltinElementwiseMathOneArgCall(TheCall)) |
2697 | 0 | return ExprError(); |
2698 | | |
2699 | 0 | QualType ArgTy = TheCall->getArg(0)->getType(); |
2700 | 0 | QualType EltTy = ArgTy; |
2701 | |
|
2702 | 0 | if (auto *VecTy = EltTy->getAs<VectorType>()) |
2703 | 0 | EltTy = VecTy->getElementType(); |
2704 | 0 | if (EltTy->isUnsignedIntegerType()) { |
2705 | 0 | Diag(TheCall->getArg(0)->getBeginLoc(), |
2706 | 0 | diag::err_builtin_invalid_arg_type) |
2707 | 0 | << 1 << /* signed integer or float ty*/ 3 << ArgTy; |
2708 | 0 | return ExprError(); |
2709 | 0 | } |
2710 | 0 | break; |
2711 | 0 | } |
2712 | | |
2713 | | // These builtins restrict the element type to floating point |
2714 | | // types only. |
2715 | 0 | case Builtin::BI__builtin_elementwise_ceil: |
2716 | 0 | case Builtin::BI__builtin_elementwise_cos: |
2717 | 0 | case Builtin::BI__builtin_elementwise_exp: |
2718 | 0 | case Builtin::BI__builtin_elementwise_exp2: |
2719 | 0 | case Builtin::BI__builtin_elementwise_floor: |
2720 | 0 | case Builtin::BI__builtin_elementwise_log: |
2721 | 0 | case Builtin::BI__builtin_elementwise_log2: |
2722 | 0 | case Builtin::BI__builtin_elementwise_log10: |
2723 | 0 | case Builtin::BI__builtin_elementwise_roundeven: |
2724 | 0 | case Builtin::BI__builtin_elementwise_round: |
2725 | 0 | case Builtin::BI__builtin_elementwise_rint: |
2726 | 0 | case Builtin::BI__builtin_elementwise_nearbyint: |
2727 | 0 | case Builtin::BI__builtin_elementwise_sin: |
2728 | 0 | case Builtin::BI__builtin_elementwise_sqrt: |
2729 | 0 | case Builtin::BI__builtin_elementwise_trunc: |
2730 | 0 | case Builtin::BI__builtin_elementwise_canonicalize: { |
2731 | 0 | if (PrepareBuiltinElementwiseMathOneArgCall(TheCall)) |
2732 | 0 | return ExprError(); |
2733 | | |
2734 | 0 | QualType ArgTy = TheCall->getArg(0)->getType(); |
2735 | 0 | if (checkFPMathBuiltinElementType(*this, TheCall->getArg(0)->getBeginLoc(), |
2736 | 0 | ArgTy, 1)) |
2737 | 0 | return ExprError(); |
2738 | 0 | break; |
2739 | 0 | } |
2740 | 0 | case Builtin::BI__builtin_elementwise_fma: { |
2741 | 0 | if (SemaBuiltinElementwiseTernaryMath(TheCall)) |
2742 | 0 | return ExprError(); |
2743 | 0 | break; |
2744 | 0 | } |
2745 | | |
2746 | | // These builtins restrict the element type to floating point |
2747 | | // types only, and take in two arguments. |
2748 | 0 | case Builtin::BI__builtin_elementwise_pow: { |
2749 | 0 | if (SemaBuiltinElementwiseMath(TheCall)) |
2750 | 0 | return ExprError(); |
2751 | | |
2752 | 0 | QualType ArgTy = TheCall->getArg(0)->getType(); |
2753 | 0 | if (checkFPMathBuiltinElementType(*this, TheCall->getArg(0)->getBeginLoc(), |
2754 | 0 | ArgTy, 1) || |
2755 | 0 | checkFPMathBuiltinElementType(*this, TheCall->getArg(1)->getBeginLoc(), |
2756 | 0 | ArgTy, 2)) |
2757 | 0 | return ExprError(); |
2758 | 0 | break; |
2759 | 0 | } |
2760 | | |
2761 | | // These builtins restrict the element type to integer |
2762 | | // types only. |
2763 | 0 | case Builtin::BI__builtin_elementwise_add_sat: |
2764 | 0 | case Builtin::BI__builtin_elementwise_sub_sat: { |
2765 | 0 | if (SemaBuiltinElementwiseMath(TheCall)) |
2766 | 0 | return ExprError(); |
2767 | | |
2768 | 0 | const Expr *Arg = TheCall->getArg(0); |
2769 | 0 | QualType ArgTy = Arg->getType(); |
2770 | 0 | QualType EltTy = ArgTy; |
2771 | |
|
2772 | 0 | if (auto *VecTy = EltTy->getAs<VectorType>()) |
2773 | 0 | EltTy = VecTy->getElementType(); |
2774 | |
|
2775 | 0 | if (!EltTy->isIntegerType()) { |
2776 | 0 | Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) |
2777 | 0 | << 1 << /* integer ty */ 6 << ArgTy; |
2778 | 0 | return ExprError(); |
2779 | 0 | } |
2780 | 0 | break; |
2781 | 0 | } |
2782 | | |
2783 | 0 | case Builtin::BI__builtin_elementwise_min: |
2784 | 0 | case Builtin::BI__builtin_elementwise_max: |
2785 | 0 | if (SemaBuiltinElementwiseMath(TheCall)) |
2786 | 0 | return ExprError(); |
2787 | 0 | break; |
2788 | | |
2789 | 0 | case Builtin::BI__builtin_elementwise_bitreverse: { |
2790 | 0 | if (PrepareBuiltinElementwiseMathOneArgCall(TheCall)) |
2791 | 0 | return ExprError(); |
2792 | | |
2793 | 0 | const Expr *Arg = TheCall->getArg(0); |
2794 | 0 | QualType ArgTy = Arg->getType(); |
2795 | 0 | QualType EltTy = ArgTy; |
2796 | |
|
2797 | 0 | if (auto *VecTy = EltTy->getAs<VectorType>()) |
2798 | 0 | EltTy = VecTy->getElementType(); |
2799 | |
|
2800 | 0 | if (!EltTy->isIntegerType()) { |
2801 | 0 | Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) |
2802 | 0 | << 1 << /* integer ty */ 6 << ArgTy; |
2803 | 0 | return ExprError(); |
2804 | 0 | } |
2805 | 0 | break; |
2806 | 0 | } |
2807 | | |
2808 | 0 | case Builtin::BI__builtin_elementwise_copysign: { |
2809 | 0 | if (checkArgCount(*this, TheCall, 2)) |
2810 | 0 | return ExprError(); |
2811 | | |
2812 | 0 | ExprResult Magnitude = UsualUnaryConversions(TheCall->getArg(0)); |
2813 | 0 | ExprResult Sign = UsualUnaryConversions(TheCall->getArg(1)); |
2814 | 0 | if (Magnitude.isInvalid() || Sign.isInvalid()) |
2815 | 0 | return ExprError(); |
2816 | | |
2817 | 0 | QualType MagnitudeTy = Magnitude.get()->getType(); |
2818 | 0 | QualType SignTy = Sign.get()->getType(); |
2819 | 0 | if (checkFPMathBuiltinElementType(*this, TheCall->getArg(0)->getBeginLoc(), |
2820 | 0 | MagnitudeTy, 1) || |
2821 | 0 | checkFPMathBuiltinElementType(*this, TheCall->getArg(1)->getBeginLoc(), |
2822 | 0 | SignTy, 2)) { |
2823 | 0 | return ExprError(); |
2824 | 0 | } |
2825 | | |
2826 | 0 | if (MagnitudeTy.getCanonicalType() != SignTy.getCanonicalType()) { |
2827 | 0 | return Diag(Sign.get()->getBeginLoc(), |
2828 | 0 | diag::err_typecheck_call_different_arg_types) |
2829 | 0 | << MagnitudeTy << SignTy; |
2830 | 0 | } |
2831 | | |
2832 | 0 | TheCall->setArg(0, Magnitude.get()); |
2833 | 0 | TheCall->setArg(1, Sign.get()); |
2834 | 0 | TheCall->setType(Magnitude.get()->getType()); |
2835 | 0 | break; |
2836 | 0 | } |
2837 | 0 | case Builtin::BI__builtin_reduce_max: |
2838 | 0 | case Builtin::BI__builtin_reduce_min: { |
2839 | 0 | if (PrepareBuiltinReduceMathOneArgCall(TheCall)) |
2840 | 0 | return ExprError(); |
2841 | | |
2842 | 0 | const Expr *Arg = TheCall->getArg(0); |
2843 | 0 | const auto *TyA = Arg->getType()->getAs<VectorType>(); |
2844 | 0 | if (!TyA) { |
2845 | 0 | Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) |
2846 | 0 | << 1 << /* vector ty*/ 4 << Arg->getType(); |
2847 | 0 | return ExprError(); |
2848 | 0 | } |
2849 | | |
2850 | 0 | TheCall->setType(TyA->getElementType()); |
2851 | 0 | break; |
2852 | 0 | } |
2853 | | |
2854 | | // These builtins support vectors of integers only. |
2855 | | // TODO: ADD/MUL should support floating-point types. |
2856 | 0 | case Builtin::BI__builtin_reduce_add: |
2857 | 0 | case Builtin::BI__builtin_reduce_mul: |
2858 | 0 | case Builtin::BI__builtin_reduce_xor: |
2859 | 0 | case Builtin::BI__builtin_reduce_or: |
2860 | 0 | case Builtin::BI__builtin_reduce_and: { |
2861 | 0 | if (PrepareBuiltinReduceMathOneArgCall(TheCall)) |
2862 | 0 | return ExprError(); |
2863 | | |
2864 | 0 | const Expr *Arg = TheCall->getArg(0); |
2865 | 0 | const auto *TyA = Arg->getType()->getAs<VectorType>(); |
2866 | 0 | if (!TyA || !TyA->getElementType()->isIntegerType()) { |
2867 | 0 | Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) |
2868 | 0 | << 1 << /* vector of integers */ 6 << Arg->getType(); |
2869 | 0 | return ExprError(); |
2870 | 0 | } |
2871 | 0 | TheCall->setType(TyA->getElementType()); |
2872 | 0 | break; |
2873 | 0 | } |
2874 | | |
2875 | 0 | case Builtin::BI__builtin_matrix_transpose: |
2876 | 0 | return SemaBuiltinMatrixTranspose(TheCall, TheCallResult); |
2877 | | |
2878 | 0 | case Builtin::BI__builtin_matrix_column_major_load: |
2879 | 0 | return SemaBuiltinMatrixColumnMajorLoad(TheCall, TheCallResult); |
2880 | | |
2881 | 0 | case Builtin::BI__builtin_matrix_column_major_store: |
2882 | 0 | return SemaBuiltinMatrixColumnMajorStore(TheCall, TheCallResult); |
2883 | | |
2884 | 0 | case Builtin::BI__builtin_get_device_side_mangled_name: { |
2885 | 0 | auto Check = [](CallExpr *TheCall) { |
2886 | 0 | if (TheCall->getNumArgs() != 1) |
2887 | 0 | return false; |
2888 | 0 | auto *DRE = dyn_cast<DeclRefExpr>(TheCall->getArg(0)->IgnoreImpCasts()); |
2889 | 0 | if (!DRE) |
2890 | 0 | return false; |
2891 | 0 | auto *D = DRE->getDecl(); |
2892 | 0 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) |
2893 | 0 | return false; |
2894 | 0 | return D->hasAttr<CUDAGlobalAttr>() || D->hasAttr<CUDADeviceAttr>() || |
2895 | 0 | D->hasAttr<CUDAConstantAttr>() || D->hasAttr<HIPManagedAttr>(); |
2896 | 0 | }; |
2897 | 0 | if (!Check(TheCall)) { |
2898 | 0 | Diag(TheCall->getBeginLoc(), |
2899 | 0 | diag::err_hip_invalid_args_builtin_mangled_name); |
2900 | 0 | return ExprError(); |
2901 | 0 | } |
2902 | 0 | } |
2903 | 0 | } |
2904 | | |
2905 | | // Since the target specific builtins for each arch overlap, only check those |
2906 | | // of the arch we are compiling for. |
2907 | 0 | if (Context.BuiltinInfo.isTSBuiltin(BuiltinID)) { |
2908 | 0 | if (Context.BuiltinInfo.isAuxBuiltinID(BuiltinID)) { |
2909 | 0 | assert(Context.getAuxTargetInfo() && |
2910 | 0 | "Aux Target Builtin, but not an aux target?"); |
2911 | | |
2912 | 0 | if (CheckTSBuiltinFunctionCall( |
2913 | 0 | *Context.getAuxTargetInfo(), |
2914 | 0 | Context.BuiltinInfo.getAuxBuiltinID(BuiltinID), TheCall)) |
2915 | 0 | return ExprError(); |
2916 | 0 | } else { |
2917 | 0 | if (CheckTSBuiltinFunctionCall(Context.getTargetInfo(), BuiltinID, |
2918 | 0 | TheCall)) |
2919 | 0 | return ExprError(); |
2920 | 0 | } |
2921 | 0 | } |
2922 | | |
2923 | 0 | return TheCallResult; |
2924 | 0 | } |
2925 | | |
2926 | | // Get the valid immediate range for the specified NEON type code. |
2927 | 0 | static unsigned RFT(unsigned t, bool shift = false, bool ForceQuad = false) { |
2928 | 0 | NeonTypeFlags Type(t); |
2929 | 0 | int IsQuad = ForceQuad ? true : Type.isQuad(); |
2930 | 0 | switch (Type.getEltType()) { |
2931 | 0 | case NeonTypeFlags::Int8: |
2932 | 0 | case NeonTypeFlags::Poly8: |
2933 | 0 | return shift ? 7 : (8 << IsQuad) - 1; |
2934 | 0 | case NeonTypeFlags::Int16: |
2935 | 0 | case NeonTypeFlags::Poly16: |
2936 | 0 | return shift ? 15 : (4 << IsQuad) - 1; |
2937 | 0 | case NeonTypeFlags::Int32: |
2938 | 0 | return shift ? 31 : (2 << IsQuad) - 1; |
2939 | 0 | case NeonTypeFlags::Int64: |
2940 | 0 | case NeonTypeFlags::Poly64: |
2941 | 0 | return shift ? 63 : (1 << IsQuad) - 1; |
2942 | 0 | case NeonTypeFlags::Poly128: |
2943 | 0 | return shift ? 127 : (1 << IsQuad) - 1; |
2944 | 0 | case NeonTypeFlags::Float16: |
2945 | 0 | assert(!shift && "cannot shift float types!"); |
2946 | 0 | return (4 << IsQuad) - 1; |
2947 | 0 | case NeonTypeFlags::Float32: |
2948 | 0 | assert(!shift && "cannot shift float types!"); |
2949 | 0 | return (2 << IsQuad) - 1; |
2950 | 0 | case NeonTypeFlags::Float64: |
2951 | 0 | assert(!shift && "cannot shift float types!"); |
2952 | 0 | return (1 << IsQuad) - 1; |
2953 | 0 | case NeonTypeFlags::BFloat16: |
2954 | 0 | assert(!shift && "cannot shift float types!"); |
2955 | 0 | return (4 << IsQuad) - 1; |
2956 | 0 | } |
2957 | 0 | llvm_unreachable("Invalid NeonTypeFlag!"); |
2958 | 0 | } |
2959 | | |
2960 | | /// getNeonEltType - Return the QualType corresponding to the elements of |
2961 | | /// the vector type specified by the NeonTypeFlags. This is used to check |
2962 | | /// the pointer arguments for Neon load/store intrinsics. |
2963 | | static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context, |
2964 | 0 | bool IsPolyUnsigned, bool IsInt64Long) { |
2965 | 0 | switch (Flags.getEltType()) { |
2966 | 0 | case NeonTypeFlags::Int8: |
2967 | 0 | return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy; |
2968 | 0 | case NeonTypeFlags::Int16: |
2969 | 0 | return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy; |
2970 | 0 | case NeonTypeFlags::Int32: |
2971 | 0 | return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy; |
2972 | 0 | case NeonTypeFlags::Int64: |
2973 | 0 | if (IsInt64Long) |
2974 | 0 | return Flags.isUnsigned() ? Context.UnsignedLongTy : Context.LongTy; |
2975 | 0 | else |
2976 | 0 | return Flags.isUnsigned() ? Context.UnsignedLongLongTy |
2977 | 0 | : Context.LongLongTy; |
2978 | 0 | case NeonTypeFlags::Poly8: |
2979 | 0 | return IsPolyUnsigned ? Context.UnsignedCharTy : Context.SignedCharTy; |
2980 | 0 | case NeonTypeFlags::Poly16: |
2981 | 0 | return IsPolyUnsigned ? Context.UnsignedShortTy : Context.ShortTy; |
2982 | 0 | case NeonTypeFlags::Poly64: |
2983 | 0 | if (IsInt64Long) |
2984 | 0 | return Context.UnsignedLongTy; |
2985 | 0 | else |
2986 | 0 | return Context.UnsignedLongLongTy; |
2987 | 0 | case NeonTypeFlags::Poly128: |
2988 | 0 | break; |
2989 | 0 | case NeonTypeFlags::Float16: |
2990 | 0 | return Context.HalfTy; |
2991 | 0 | case NeonTypeFlags::Float32: |
2992 | 0 | return Context.FloatTy; |
2993 | 0 | case NeonTypeFlags::Float64: |
2994 | 0 | return Context.DoubleTy; |
2995 | 0 | case NeonTypeFlags::BFloat16: |
2996 | 0 | return Context.BFloat16Ty; |
2997 | 0 | } |
2998 | 0 | llvm_unreachable("Invalid NeonTypeFlag!"); |
2999 | 0 | } |
3000 | | |
3001 | | enum ArmStreamingType { |
3002 | | ArmNonStreaming, |
3003 | | ArmStreaming, |
3004 | | ArmStreamingCompatible, |
3005 | | ArmStreamingOrSVE2p1 |
3006 | | }; |
3007 | | |
3008 | | bool Sema::ParseSVEImmChecks( |
3009 | 0 | CallExpr *TheCall, SmallVector<std::tuple<int, int, int>, 3> &ImmChecks) { |
3010 | | // Perform all the immediate checks for this builtin call. |
3011 | 0 | bool HasError = false; |
3012 | 0 | for (auto &I : ImmChecks) { |
3013 | 0 | int ArgNum, CheckTy, ElementSizeInBits; |
3014 | 0 | std::tie(ArgNum, CheckTy, ElementSizeInBits) = I; |
3015 | |
|
3016 | 0 | typedef bool (*OptionSetCheckFnTy)(int64_t Value); |
3017 | | |
3018 | | // Function that checks whether the operand (ArgNum) is an immediate |
3019 | | // that is one of the predefined values. |
3020 | 0 | auto CheckImmediateInSet = [&](OptionSetCheckFnTy CheckImm, |
3021 | 0 | int ErrDiag) -> bool { |
3022 | | // We can't check the value of a dependent argument. |
3023 | 0 | Expr *Arg = TheCall->getArg(ArgNum); |
3024 | 0 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
3025 | 0 | return false; |
3026 | | |
3027 | | // Check constant-ness first. |
3028 | 0 | llvm::APSInt Imm; |
3029 | 0 | if (SemaBuiltinConstantArg(TheCall, ArgNum, Imm)) |
3030 | 0 | return true; |
3031 | | |
3032 | 0 | if (!CheckImm(Imm.getSExtValue())) |
3033 | 0 | return Diag(TheCall->getBeginLoc(), ErrDiag) << Arg->getSourceRange(); |
3034 | 0 | return false; |
3035 | 0 | }; |
3036 | |
|
3037 | 0 | switch ((SVETypeFlags::ImmCheckType)CheckTy) { |
3038 | 0 | case SVETypeFlags::ImmCheck0_31: |
3039 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 31)) |
3040 | 0 | HasError = true; |
3041 | 0 | break; |
3042 | 0 | case SVETypeFlags::ImmCheck0_13: |
3043 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 13)) |
3044 | 0 | HasError = true; |
3045 | 0 | break; |
3046 | 0 | case SVETypeFlags::ImmCheck1_16: |
3047 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 1, 16)) |
3048 | 0 | HasError = true; |
3049 | 0 | break; |
3050 | 0 | case SVETypeFlags::ImmCheck0_7: |
3051 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 7)) |
3052 | 0 | HasError = true; |
3053 | 0 | break; |
3054 | 0 | case SVETypeFlags::ImmCheck1_1: |
3055 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 1, 1)) |
3056 | 0 | HasError = true; |
3057 | 0 | break; |
3058 | 0 | case SVETypeFlags::ImmCheck1_3: |
3059 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 1, 3)) |
3060 | 0 | HasError = true; |
3061 | 0 | break; |
3062 | 0 | case SVETypeFlags::ImmCheck1_7: |
3063 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 1, 7)) |
3064 | 0 | HasError = true; |
3065 | 0 | break; |
3066 | 0 | case SVETypeFlags::ImmCheckExtract: |
3067 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, |
3068 | 0 | (2048 / ElementSizeInBits) - 1)) |
3069 | 0 | HasError = true; |
3070 | 0 | break; |
3071 | 0 | case SVETypeFlags::ImmCheckShiftRight: |
3072 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 1, ElementSizeInBits)) |
3073 | 0 | HasError = true; |
3074 | 0 | break; |
3075 | 0 | case SVETypeFlags::ImmCheckShiftRightNarrow: |
3076 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 1, |
3077 | 0 | ElementSizeInBits / 2)) |
3078 | 0 | HasError = true; |
3079 | 0 | break; |
3080 | 0 | case SVETypeFlags::ImmCheckShiftLeft: |
3081 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, |
3082 | 0 | ElementSizeInBits - 1)) |
3083 | 0 | HasError = true; |
3084 | 0 | break; |
3085 | 0 | case SVETypeFlags::ImmCheckLaneIndex: |
3086 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, |
3087 | 0 | (128 / (1 * ElementSizeInBits)) - 1)) |
3088 | 0 | HasError = true; |
3089 | 0 | break; |
3090 | 0 | case SVETypeFlags::ImmCheckLaneIndexCompRotate: |
3091 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, |
3092 | 0 | (128 / (2 * ElementSizeInBits)) - 1)) |
3093 | 0 | HasError = true; |
3094 | 0 | break; |
3095 | 0 | case SVETypeFlags::ImmCheckLaneIndexDot: |
3096 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, |
3097 | 0 | (128 / (4 * ElementSizeInBits)) - 1)) |
3098 | 0 | HasError = true; |
3099 | 0 | break; |
3100 | 0 | case SVETypeFlags::ImmCheckComplexRot90_270: |
3101 | 0 | if (CheckImmediateInSet([](int64_t V) { return V == 90 || V == 270; }, |
3102 | 0 | diag::err_rotation_argument_to_cadd)) |
3103 | 0 | HasError = true; |
3104 | 0 | break; |
3105 | 0 | case SVETypeFlags::ImmCheckComplexRotAll90: |
3106 | 0 | if (CheckImmediateInSet( |
3107 | 0 | [](int64_t V) { |
3108 | 0 | return V == 0 || V == 90 || V == 180 || V == 270; |
3109 | 0 | }, |
3110 | 0 | diag::err_rotation_argument_to_cmla)) |
3111 | 0 | HasError = true; |
3112 | 0 | break; |
3113 | 0 | case SVETypeFlags::ImmCheck0_1: |
3114 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 1)) |
3115 | 0 | HasError = true; |
3116 | 0 | break; |
3117 | 0 | case SVETypeFlags::ImmCheck0_2: |
3118 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 2)) |
3119 | 0 | HasError = true; |
3120 | 0 | break; |
3121 | 0 | case SVETypeFlags::ImmCheck0_3: |
3122 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 3)) |
3123 | 0 | HasError = true; |
3124 | 0 | break; |
3125 | 0 | case SVETypeFlags::ImmCheck0_0: |
3126 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 0)) |
3127 | 0 | HasError = true; |
3128 | 0 | break; |
3129 | 0 | case SVETypeFlags::ImmCheck0_15: |
3130 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 15)) |
3131 | 0 | HasError = true; |
3132 | 0 | break; |
3133 | 0 | case SVETypeFlags::ImmCheck0_255: |
3134 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 255)) |
3135 | 0 | HasError = true; |
3136 | 0 | break; |
3137 | 0 | case SVETypeFlags::ImmCheck2_4_Mul2: |
3138 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 2, 4) || |
3139 | 0 | SemaBuiltinConstantArgMultiple(TheCall, ArgNum, 2)) |
3140 | 0 | HasError = true; |
3141 | 0 | break; |
3142 | 0 | } |
3143 | 0 | } |
3144 | | |
3145 | 0 | return HasError; |
3146 | 0 | } |
3147 | | |
3148 | 0 | static ArmStreamingType getArmStreamingFnType(const FunctionDecl *FD) { |
3149 | 0 | if (FD->hasAttr<ArmLocallyStreamingAttr>()) |
3150 | 0 | return ArmStreaming; |
3151 | 0 | if (const auto *T = FD->getType()->getAs<FunctionProtoType>()) { |
3152 | 0 | if (T->getAArch64SMEAttributes() & FunctionType::SME_PStateSMEnabledMask) |
3153 | 0 | return ArmStreaming; |
3154 | 0 | if (T->getAArch64SMEAttributes() & FunctionType::SME_PStateSMCompatibleMask) |
3155 | 0 | return ArmStreamingCompatible; |
3156 | 0 | } |
3157 | 0 | return ArmNonStreaming; |
3158 | 0 | } |
3159 | | |
3160 | | static void checkArmStreamingBuiltin(Sema &S, CallExpr *TheCall, |
3161 | | const FunctionDecl *FD, |
3162 | 0 | ArmStreamingType BuiltinType) { |
3163 | 0 | ArmStreamingType FnType = getArmStreamingFnType(FD); |
3164 | 0 | if (BuiltinType == ArmStreamingOrSVE2p1) { |
3165 | | // Check intrinsics that are available in [sve2p1 or sme/sme2]. |
3166 | 0 | llvm::StringMap<bool> CallerFeatureMap; |
3167 | 0 | S.Context.getFunctionFeatureMap(CallerFeatureMap, FD); |
3168 | 0 | if (Builtin::evaluateRequiredTargetFeatures("sve2p1", CallerFeatureMap)) |
3169 | 0 | BuiltinType = ArmStreamingCompatible; |
3170 | 0 | else |
3171 | 0 | BuiltinType = ArmStreaming; |
3172 | 0 | } |
3173 | |
|
3174 | 0 | if (FnType == ArmStreaming && BuiltinType == ArmNonStreaming) { |
3175 | 0 | S.Diag(TheCall->getBeginLoc(), diag::warn_attribute_arm_sm_incompat_builtin) |
3176 | 0 | << TheCall->getSourceRange() << "streaming"; |
3177 | 0 | } |
3178 | |
|
3179 | 0 | if (FnType == ArmStreamingCompatible && |
3180 | 0 | BuiltinType != ArmStreamingCompatible) { |
3181 | 0 | S.Diag(TheCall->getBeginLoc(), diag::warn_attribute_arm_sm_incompat_builtin) |
3182 | 0 | << TheCall->getSourceRange() << "streaming compatible"; |
3183 | 0 | return; |
3184 | 0 | } |
3185 | | |
3186 | 0 | if (FnType == ArmNonStreaming && BuiltinType == ArmStreaming) { |
3187 | 0 | S.Diag(TheCall->getBeginLoc(), diag::warn_attribute_arm_sm_incompat_builtin) |
3188 | 0 | << TheCall->getSourceRange() << "non-streaming"; |
3189 | 0 | } |
3190 | 0 | } |
3191 | | |
3192 | 0 | static bool hasSMEZAState(const FunctionDecl *FD) { |
3193 | 0 | if (auto *Attr = FD->getAttr<ArmNewAttr>()) |
3194 | 0 | if (Attr->isNewZA()) |
3195 | 0 | return true; |
3196 | 0 | if (const auto *T = FD->getType()->getAs<FunctionProtoType>()) { |
3197 | 0 | FunctionType::ArmStateValue State = |
3198 | 0 | FunctionType::getArmZAState(T->getAArch64SMEAttributes()); |
3199 | 0 | if (State != FunctionType::ARM_None) |
3200 | 0 | return true; |
3201 | 0 | } |
3202 | 0 | return false; |
3203 | 0 | } |
3204 | | |
3205 | 0 | static bool hasSMEZAState(unsigned BuiltinID) { |
3206 | 0 | switch (BuiltinID) { |
3207 | 0 | default: |
3208 | 0 | return false; |
3209 | 0 | #define GET_SME_BUILTIN_HAS_ZA_STATE |
3210 | 0 | #include "clang/Basic/arm_sme_builtins_za_state.inc" |
3211 | 0 | #undef GET_SME_BUILTIN_HAS_ZA_STATE |
3212 | 0 | } |
3213 | 0 | } |
3214 | | |
3215 | 0 | bool Sema::CheckSMEBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
3216 | 0 | if (const FunctionDecl *FD = getCurFunctionDecl()) { |
3217 | 0 | std::optional<ArmStreamingType> BuiltinType; |
3218 | |
|
3219 | 0 | switch (BuiltinID) { |
3220 | 0 | #define GET_SME_STREAMING_ATTRS |
3221 | 0 | #include "clang/Basic/arm_sme_streaming_attrs.inc" |
3222 | 0 | #undef GET_SME_STREAMING_ATTRS |
3223 | 0 | } |
3224 | | |
3225 | 0 | if (BuiltinType) |
3226 | 0 | checkArmStreamingBuiltin(*this, TheCall, FD, *BuiltinType); |
3227 | |
|
3228 | 0 | if (hasSMEZAState(BuiltinID) && !hasSMEZAState(FD)) |
3229 | 0 | Diag(TheCall->getBeginLoc(), |
3230 | 0 | diag::warn_attribute_arm_za_builtin_no_za_state) |
3231 | 0 | << TheCall->getSourceRange(); |
3232 | 0 | } |
3233 | | |
3234 | | // Range check SME intrinsics that take immediate values. |
3235 | 0 | SmallVector<std::tuple<int, int, int>, 3> ImmChecks; |
3236 | |
|
3237 | 0 | switch (BuiltinID) { |
3238 | 0 | default: |
3239 | 0 | return false; |
3240 | 0 | #define GET_SME_IMMEDIATE_CHECK |
3241 | 0 | #include "clang/Basic/arm_sme_sema_rangechecks.inc" |
3242 | 0 | #undef GET_SME_IMMEDIATE_CHECK |
3243 | 0 | } |
3244 | | |
3245 | 0 | return ParseSVEImmChecks(TheCall, ImmChecks); |
3246 | 0 | } |
3247 | | |
3248 | 0 | bool Sema::CheckSVEBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
3249 | 0 | if (const FunctionDecl *FD = getCurFunctionDecl()) { |
3250 | 0 | std::optional<ArmStreamingType> BuiltinType; |
3251 | |
|
3252 | 0 | switch (BuiltinID) { |
3253 | 0 | #define GET_SVE_STREAMING_ATTRS |
3254 | 0 | #include "clang/Basic/arm_sve_streaming_attrs.inc" |
3255 | 0 | #undef GET_SVE_STREAMING_ATTRS |
3256 | 0 | } |
3257 | 0 | if (BuiltinType) |
3258 | 0 | checkArmStreamingBuiltin(*this, TheCall, FD, *BuiltinType); |
3259 | 0 | } |
3260 | | // Range check SVE intrinsics that take immediate values. |
3261 | 0 | SmallVector<std::tuple<int, int, int>, 3> ImmChecks; |
3262 | |
|
3263 | 0 | switch (BuiltinID) { |
3264 | 0 | default: |
3265 | 0 | return false; |
3266 | 0 | #define GET_SVE_IMMEDIATE_CHECK |
3267 | 0 | #include "clang/Basic/arm_sve_sema_rangechecks.inc" |
3268 | 0 | #undef GET_SVE_IMMEDIATE_CHECK |
3269 | 0 | } |
3270 | | |
3271 | 0 | return ParseSVEImmChecks(TheCall, ImmChecks); |
3272 | 0 | } |
3273 | | |
3274 | | bool Sema::CheckNeonBuiltinFunctionCall(const TargetInfo &TI, |
3275 | 0 | unsigned BuiltinID, CallExpr *TheCall) { |
3276 | 0 | if (const FunctionDecl *FD = getCurFunctionDecl()) { |
3277 | |
|
3278 | 0 | switch (BuiltinID) { |
3279 | 0 | default: |
3280 | 0 | break; |
3281 | 0 | #define GET_NEON_BUILTINS |
3282 | 0 | #define TARGET_BUILTIN(id, ...) case NEON::BI##id: |
3283 | 0 | #define BUILTIN(id, ...) case NEON::BI##id: |
3284 | 0 | #include "clang/Basic/arm_neon.inc" |
3285 | 0 | checkArmStreamingBuiltin(*this, TheCall, FD, ArmNonStreaming); |
3286 | 0 | break; |
3287 | 0 | #undef TARGET_BUILTIN |
3288 | 0 | #undef BUILTIN |
3289 | 0 | #undef GET_NEON_BUILTINS |
3290 | 0 | } |
3291 | 0 | } |
3292 | | |
3293 | 0 | llvm::APSInt Result; |
3294 | 0 | uint64_t mask = 0; |
3295 | 0 | unsigned TV = 0; |
3296 | 0 | int PtrArgNum = -1; |
3297 | 0 | bool HasConstPtr = false; |
3298 | 0 | switch (BuiltinID) { |
3299 | 0 | #define GET_NEON_OVERLOAD_CHECK |
3300 | 0 | #include "clang/Basic/arm_neon.inc" |
3301 | 0 | #include "clang/Basic/arm_fp16.inc" |
3302 | 0 | #undef GET_NEON_OVERLOAD_CHECK |
3303 | 0 | } |
3304 | | |
3305 | | // For NEON intrinsics which are overloaded on vector element type, validate |
3306 | | // the immediate which specifies which variant to emit. |
3307 | 0 | unsigned ImmArg = TheCall->getNumArgs()-1; |
3308 | 0 | if (mask) { |
3309 | 0 | if (SemaBuiltinConstantArg(TheCall, ImmArg, Result)) |
3310 | 0 | return true; |
3311 | | |
3312 | 0 | TV = Result.getLimitedValue(64); |
3313 | 0 | if ((TV > 63) || (mask & (1ULL << TV)) == 0) |
3314 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_invalid_neon_type_code) |
3315 | 0 | << TheCall->getArg(ImmArg)->getSourceRange(); |
3316 | 0 | } |
3317 | | |
3318 | 0 | if (PtrArgNum >= 0) { |
3319 | | // Check that pointer arguments have the specified type. |
3320 | 0 | Expr *Arg = TheCall->getArg(PtrArgNum); |
3321 | 0 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) |
3322 | 0 | Arg = ICE->getSubExpr(); |
3323 | 0 | ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg); |
3324 | 0 | QualType RHSTy = RHS.get()->getType(); |
3325 | |
|
3326 | 0 | llvm::Triple::ArchType Arch = TI.getTriple().getArch(); |
3327 | 0 | bool IsPolyUnsigned = Arch == llvm::Triple::aarch64 || |
3328 | 0 | Arch == llvm::Triple::aarch64_32 || |
3329 | 0 | Arch == llvm::Triple::aarch64_be; |
3330 | 0 | bool IsInt64Long = TI.getInt64Type() == TargetInfo::SignedLong; |
3331 | 0 | QualType EltTy = |
3332 | 0 | getNeonEltType(NeonTypeFlags(TV), Context, IsPolyUnsigned, IsInt64Long); |
3333 | 0 | if (HasConstPtr) |
3334 | 0 | EltTy = EltTy.withConst(); |
3335 | 0 | QualType LHSTy = Context.getPointerType(EltTy); |
3336 | 0 | AssignConvertType ConvTy; |
3337 | 0 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
3338 | 0 | if (RHS.isInvalid()) |
3339 | 0 | return true; |
3340 | 0 | if (DiagnoseAssignmentResult(ConvTy, Arg->getBeginLoc(), LHSTy, RHSTy, |
3341 | 0 | RHS.get(), AA_Assigning)) |
3342 | 0 | return true; |
3343 | 0 | } |
3344 | | |
3345 | | // For NEON intrinsics which take an immediate value as part of the |
3346 | | // instruction, range check them here. |
3347 | 0 | unsigned i = 0, l = 0, u = 0; |
3348 | 0 | switch (BuiltinID) { |
3349 | 0 | default: |
3350 | 0 | return false; |
3351 | 0 | #define GET_NEON_IMMEDIATE_CHECK |
3352 | 0 | #include "clang/Basic/arm_neon.inc" |
3353 | 0 | #include "clang/Basic/arm_fp16.inc" |
3354 | 0 | #undef GET_NEON_IMMEDIATE_CHECK |
3355 | 0 | } |
3356 | | |
3357 | 0 | return SemaBuiltinConstantArgRange(TheCall, i, l, u + l); |
3358 | 0 | } |
3359 | | |
3360 | 0 | bool Sema::CheckMVEBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
3361 | 0 | switch (BuiltinID) { |
3362 | 0 | default: |
3363 | 0 | return false; |
3364 | 0 | #include "clang/Basic/arm_mve_builtin_sema.inc" |
3365 | 0 | } |
3366 | 0 | } |
3367 | | |
3368 | | bool Sema::CheckCDEBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID, |
3369 | 0 | CallExpr *TheCall) { |
3370 | 0 | bool Err = false; |
3371 | 0 | switch (BuiltinID) { |
3372 | 0 | default: |
3373 | 0 | return false; |
3374 | 0 | #include "clang/Basic/arm_cde_builtin_sema.inc" |
3375 | 0 | } |
3376 | | |
3377 | 0 | if (Err) |
3378 | 0 | return true; |
3379 | | |
3380 | 0 | return CheckARMCoprocessorImmediate(TI, TheCall->getArg(0), /*WantCDE*/ true); |
3381 | 0 | } |
3382 | | |
3383 | | bool Sema::CheckARMCoprocessorImmediate(const TargetInfo &TI, |
3384 | 0 | const Expr *CoprocArg, bool WantCDE) { |
3385 | 0 | if (isConstantEvaluatedContext()) |
3386 | 0 | return false; |
3387 | | |
3388 | | // We can't check the value of a dependent argument. |
3389 | 0 | if (CoprocArg->isTypeDependent() || CoprocArg->isValueDependent()) |
3390 | 0 | return false; |
3391 | | |
3392 | 0 | llvm::APSInt CoprocNoAP = *CoprocArg->getIntegerConstantExpr(Context); |
3393 | 0 | int64_t CoprocNo = CoprocNoAP.getExtValue(); |
3394 | 0 | assert(CoprocNo >= 0 && "Coprocessor immediate must be non-negative"); |
3395 | | |
3396 | 0 | uint32_t CDECoprocMask = TI.getARMCDECoprocMask(); |
3397 | 0 | bool IsCDECoproc = CoprocNo <= 7 && (CDECoprocMask & (1 << CoprocNo)); |
3398 | |
|
3399 | 0 | if (IsCDECoproc != WantCDE) |
3400 | 0 | return Diag(CoprocArg->getBeginLoc(), diag::err_arm_invalid_coproc) |
3401 | 0 | << (int)CoprocNo << (int)WantCDE << CoprocArg->getSourceRange(); |
3402 | | |
3403 | 0 | return false; |
3404 | 0 | } |
3405 | | |
3406 | | bool Sema::CheckARMBuiltinExclusiveCall(unsigned BuiltinID, CallExpr *TheCall, |
3407 | 0 | unsigned MaxWidth) { |
3408 | 0 | assert((BuiltinID == ARM::BI__builtin_arm_ldrex || |
3409 | 0 | BuiltinID == ARM::BI__builtin_arm_ldaex || |
3410 | 0 | BuiltinID == ARM::BI__builtin_arm_strex || |
3411 | 0 | BuiltinID == ARM::BI__builtin_arm_stlex || |
3412 | 0 | BuiltinID == AArch64::BI__builtin_arm_ldrex || |
3413 | 0 | BuiltinID == AArch64::BI__builtin_arm_ldaex || |
3414 | 0 | BuiltinID == AArch64::BI__builtin_arm_strex || |
3415 | 0 | BuiltinID == AArch64::BI__builtin_arm_stlex) && |
3416 | 0 | "unexpected ARM builtin"); |
3417 | 0 | bool IsLdrex = BuiltinID == ARM::BI__builtin_arm_ldrex || |
3418 | 0 | BuiltinID == ARM::BI__builtin_arm_ldaex || |
3419 | 0 | BuiltinID == AArch64::BI__builtin_arm_ldrex || |
3420 | 0 | BuiltinID == AArch64::BI__builtin_arm_ldaex; |
3421 | |
|
3422 | 0 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
3423 | | |
3424 | | // Ensure that we have the proper number of arguments. |
3425 | 0 | if (checkArgCount(*this, TheCall, IsLdrex ? 1 : 2)) |
3426 | 0 | return true; |
3427 | | |
3428 | | // Inspect the pointer argument of the atomic builtin. This should always be |
3429 | | // a pointer type, whose element is an integral scalar or pointer type. |
3430 | | // Because it is a pointer type, we don't have to worry about any implicit |
3431 | | // casts here. |
3432 | 0 | Expr *PointerArg = TheCall->getArg(IsLdrex ? 0 : 1); |
3433 | 0 | ExprResult PointerArgRes = DefaultFunctionArrayLvalueConversion(PointerArg); |
3434 | 0 | if (PointerArgRes.isInvalid()) |
3435 | 0 | return true; |
3436 | 0 | PointerArg = PointerArgRes.get(); |
3437 | |
|
3438 | 0 | const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>(); |
3439 | 0 | if (!pointerType) { |
3440 | 0 | Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer) |
3441 | 0 | << PointerArg->getType() << PointerArg->getSourceRange(); |
3442 | 0 | return true; |
3443 | 0 | } |
3444 | | |
3445 | | // ldrex takes a "const volatile T*" and strex takes a "volatile T*". Our next |
3446 | | // task is to insert the appropriate casts into the AST. First work out just |
3447 | | // what the appropriate type is. |
3448 | 0 | QualType ValType = pointerType->getPointeeType(); |
3449 | 0 | QualType AddrType = ValType.getUnqualifiedType().withVolatile(); |
3450 | 0 | if (IsLdrex) |
3451 | 0 | AddrType.addConst(); |
3452 | | |
3453 | | // Issue a warning if the cast is dodgy. |
3454 | 0 | CastKind CastNeeded = CK_NoOp; |
3455 | 0 | if (!AddrType.isAtLeastAsQualifiedAs(ValType)) { |
3456 | 0 | CastNeeded = CK_BitCast; |
3457 | 0 | Diag(DRE->getBeginLoc(), diag::ext_typecheck_convert_discards_qualifiers) |
3458 | 0 | << PointerArg->getType() << Context.getPointerType(AddrType) |
3459 | 0 | << AA_Passing << PointerArg->getSourceRange(); |
3460 | 0 | } |
3461 | | |
3462 | | // Finally, do the cast and replace the argument with the corrected version. |
3463 | 0 | AddrType = Context.getPointerType(AddrType); |
3464 | 0 | PointerArgRes = ImpCastExprToType(PointerArg, AddrType, CastNeeded); |
3465 | 0 | if (PointerArgRes.isInvalid()) |
3466 | 0 | return true; |
3467 | 0 | PointerArg = PointerArgRes.get(); |
3468 | |
|
3469 | 0 | TheCall->setArg(IsLdrex ? 0 : 1, PointerArg); |
3470 | | |
3471 | | // In general, we allow ints, floats and pointers to be loaded and stored. |
3472 | 0 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && |
3473 | 0 | !ValType->isBlockPointerType() && !ValType->isFloatingType()) { |
3474 | 0 | Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer_intfltptr) |
3475 | 0 | << PointerArg->getType() << PointerArg->getSourceRange(); |
3476 | 0 | return true; |
3477 | 0 | } |
3478 | | |
3479 | | // But ARM doesn't have instructions to deal with 128-bit versions. |
3480 | 0 | if (Context.getTypeSize(ValType) > MaxWidth) { |
3481 | 0 | assert(MaxWidth == 64 && "Diagnostic unexpectedly inaccurate"); |
3482 | 0 | Diag(DRE->getBeginLoc(), diag::err_atomic_exclusive_builtin_pointer_size) |
3483 | 0 | << PointerArg->getType() << PointerArg->getSourceRange(); |
3484 | 0 | return true; |
3485 | 0 | } |
3486 | | |
3487 | 0 | switch (ValType.getObjCLifetime()) { |
3488 | 0 | case Qualifiers::OCL_None: |
3489 | 0 | case Qualifiers::OCL_ExplicitNone: |
3490 | | // okay |
3491 | 0 | break; |
3492 | | |
3493 | 0 | case Qualifiers::OCL_Weak: |
3494 | 0 | case Qualifiers::OCL_Strong: |
3495 | 0 | case Qualifiers::OCL_Autoreleasing: |
3496 | 0 | Diag(DRE->getBeginLoc(), diag::err_arc_atomic_ownership) |
3497 | 0 | << ValType << PointerArg->getSourceRange(); |
3498 | 0 | return true; |
3499 | 0 | } |
3500 | | |
3501 | 0 | if (IsLdrex) { |
3502 | 0 | TheCall->setType(ValType); |
3503 | 0 | return false; |
3504 | 0 | } |
3505 | | |
3506 | | // Initialize the argument to be stored. |
3507 | 0 | ExprResult ValArg = TheCall->getArg(0); |
3508 | 0 | InitializedEntity Entity = InitializedEntity::InitializeParameter( |
3509 | 0 | Context, ValType, /*consume*/ false); |
3510 | 0 | ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg); |
3511 | 0 | if (ValArg.isInvalid()) |
3512 | 0 | return true; |
3513 | 0 | TheCall->setArg(0, ValArg.get()); |
3514 | | |
3515 | | // __builtin_arm_strex always returns an int. It's marked as such in the .def, |
3516 | | // but the custom checker bypasses all default analysis. |
3517 | 0 | TheCall->setType(Context.IntTy); |
3518 | 0 | return false; |
3519 | 0 | } |
3520 | | |
3521 | | bool Sema::CheckARMBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID, |
3522 | 0 | CallExpr *TheCall) { |
3523 | 0 | if (BuiltinID == ARM::BI__builtin_arm_ldrex || |
3524 | 0 | BuiltinID == ARM::BI__builtin_arm_ldaex || |
3525 | 0 | BuiltinID == ARM::BI__builtin_arm_strex || |
3526 | 0 | BuiltinID == ARM::BI__builtin_arm_stlex) { |
3527 | 0 | return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 64); |
3528 | 0 | } |
3529 | | |
3530 | 0 | if (BuiltinID == ARM::BI__builtin_arm_prefetch) { |
3531 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) || |
3532 | 0 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 1); |
3533 | 0 | } |
3534 | | |
3535 | 0 | if (BuiltinID == ARM::BI__builtin_arm_rsr64 || |
3536 | 0 | BuiltinID == ARM::BI__builtin_arm_wsr64) |
3537 | 0 | return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 3, false); |
3538 | | |
3539 | 0 | if (BuiltinID == ARM::BI__builtin_arm_rsr || |
3540 | 0 | BuiltinID == ARM::BI__builtin_arm_rsrp || |
3541 | 0 | BuiltinID == ARM::BI__builtin_arm_wsr || |
3542 | 0 | BuiltinID == ARM::BI__builtin_arm_wsrp) |
3543 | 0 | return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true); |
3544 | | |
3545 | 0 | if (CheckNeonBuiltinFunctionCall(TI, BuiltinID, TheCall)) |
3546 | 0 | return true; |
3547 | 0 | if (CheckMVEBuiltinFunctionCall(BuiltinID, TheCall)) |
3548 | 0 | return true; |
3549 | 0 | if (CheckCDEBuiltinFunctionCall(TI, BuiltinID, TheCall)) |
3550 | 0 | return true; |
3551 | | |
3552 | | // For intrinsics which take an immediate value as part of the instruction, |
3553 | | // range check them here. |
3554 | | // FIXME: VFP Intrinsics should error if VFP not present. |
3555 | 0 | switch (BuiltinID) { |
3556 | 0 | default: return false; |
3557 | 0 | case ARM::BI__builtin_arm_ssat: |
3558 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 1, 32); |
3559 | 0 | case ARM::BI__builtin_arm_usat: |
3560 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 31); |
3561 | 0 | case ARM::BI__builtin_arm_ssat16: |
3562 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 1, 16); |
3563 | 0 | case ARM::BI__builtin_arm_usat16: |
3564 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15); |
3565 | 0 | case ARM::BI__builtin_arm_vcvtr_f: |
3566 | 0 | case ARM::BI__builtin_arm_vcvtr_d: |
3567 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1); |
3568 | 0 | case ARM::BI__builtin_arm_dmb: |
3569 | 0 | case ARM::BI__builtin_arm_dsb: |
3570 | 0 | case ARM::BI__builtin_arm_isb: |
3571 | 0 | case ARM::BI__builtin_arm_dbg: |
3572 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 15); |
3573 | 0 | case ARM::BI__builtin_arm_cdp: |
3574 | 0 | case ARM::BI__builtin_arm_cdp2: |
3575 | 0 | case ARM::BI__builtin_arm_mcr: |
3576 | 0 | case ARM::BI__builtin_arm_mcr2: |
3577 | 0 | case ARM::BI__builtin_arm_mrc: |
3578 | 0 | case ARM::BI__builtin_arm_mrc2: |
3579 | 0 | case ARM::BI__builtin_arm_mcrr: |
3580 | 0 | case ARM::BI__builtin_arm_mcrr2: |
3581 | 0 | case ARM::BI__builtin_arm_mrrc: |
3582 | 0 | case ARM::BI__builtin_arm_mrrc2: |
3583 | 0 | case ARM::BI__builtin_arm_ldc: |
3584 | 0 | case ARM::BI__builtin_arm_ldcl: |
3585 | 0 | case ARM::BI__builtin_arm_ldc2: |
3586 | 0 | case ARM::BI__builtin_arm_ldc2l: |
3587 | 0 | case ARM::BI__builtin_arm_stc: |
3588 | 0 | case ARM::BI__builtin_arm_stcl: |
3589 | 0 | case ARM::BI__builtin_arm_stc2: |
3590 | 0 | case ARM::BI__builtin_arm_stc2l: |
3591 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 15) || |
3592 | 0 | CheckARMCoprocessorImmediate(TI, TheCall->getArg(0), |
3593 | 0 | /*WantCDE*/ false); |
3594 | 0 | } |
3595 | 0 | } |
3596 | | |
3597 | | bool Sema::CheckAArch64BuiltinFunctionCall(const TargetInfo &TI, |
3598 | | unsigned BuiltinID, |
3599 | 0 | CallExpr *TheCall) { |
3600 | 0 | if (BuiltinID == AArch64::BI__builtin_arm_ldrex || |
3601 | 0 | BuiltinID == AArch64::BI__builtin_arm_ldaex || |
3602 | 0 | BuiltinID == AArch64::BI__builtin_arm_strex || |
3603 | 0 | BuiltinID == AArch64::BI__builtin_arm_stlex) { |
3604 | 0 | return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 128); |
3605 | 0 | } |
3606 | | |
3607 | 0 | if (BuiltinID == AArch64::BI__builtin_arm_prefetch) { |
3608 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) || |
3609 | 0 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 3) || |
3610 | 0 | SemaBuiltinConstantArgRange(TheCall, 3, 0, 1) || |
3611 | 0 | SemaBuiltinConstantArgRange(TheCall, 4, 0, 1); |
3612 | 0 | } |
3613 | | |
3614 | 0 | if (BuiltinID == AArch64::BI__builtin_arm_rsr64 || |
3615 | 0 | BuiltinID == AArch64::BI__builtin_arm_wsr64 || |
3616 | 0 | BuiltinID == AArch64::BI__builtin_arm_rsr128 || |
3617 | 0 | BuiltinID == AArch64::BI__builtin_arm_wsr128) |
3618 | 0 | return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true); |
3619 | | |
3620 | | // Memory Tagging Extensions (MTE) Intrinsics |
3621 | 0 | if (BuiltinID == AArch64::BI__builtin_arm_irg || |
3622 | 0 | BuiltinID == AArch64::BI__builtin_arm_addg || |
3623 | 0 | BuiltinID == AArch64::BI__builtin_arm_gmi || |
3624 | 0 | BuiltinID == AArch64::BI__builtin_arm_ldg || |
3625 | 0 | BuiltinID == AArch64::BI__builtin_arm_stg || |
3626 | 0 | BuiltinID == AArch64::BI__builtin_arm_subp) { |
3627 | 0 | return SemaBuiltinARMMemoryTaggingCall(BuiltinID, TheCall); |
3628 | 0 | } |
3629 | | |
3630 | 0 | if (BuiltinID == AArch64::BI__builtin_arm_rsr || |
3631 | 0 | BuiltinID == AArch64::BI__builtin_arm_rsrp || |
3632 | 0 | BuiltinID == AArch64::BI__builtin_arm_wsr || |
3633 | 0 | BuiltinID == AArch64::BI__builtin_arm_wsrp) |
3634 | 0 | return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true); |
3635 | | |
3636 | | // Only check the valid encoding range. Any constant in this range would be |
3637 | | // converted to a register of the form S1_2_C3_C4_5. Let the hardware throw |
3638 | | // an exception for incorrect registers. This matches MSVC behavior. |
3639 | 0 | if (BuiltinID == AArch64::BI_ReadStatusReg || |
3640 | 0 | BuiltinID == AArch64::BI_WriteStatusReg) |
3641 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 0x7fff); |
3642 | | |
3643 | 0 | if (BuiltinID == AArch64::BI__getReg) |
3644 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 31); |
3645 | | |
3646 | 0 | if (BuiltinID == AArch64::BI__break) |
3647 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 0xffff); |
3648 | | |
3649 | 0 | if (CheckNeonBuiltinFunctionCall(TI, BuiltinID, TheCall)) |
3650 | 0 | return true; |
3651 | | |
3652 | 0 | if (CheckSVEBuiltinFunctionCall(BuiltinID, TheCall)) |
3653 | 0 | return true; |
3654 | | |
3655 | 0 | if (CheckSMEBuiltinFunctionCall(BuiltinID, TheCall)) |
3656 | 0 | return true; |
3657 | | |
3658 | | // For intrinsics which take an immediate value as part of the instruction, |
3659 | | // range check them here. |
3660 | 0 | unsigned i = 0, l = 0, u = 0; |
3661 | 0 | switch (BuiltinID) { |
3662 | 0 | default: return false; |
3663 | 0 | case AArch64::BI__builtin_arm_dmb: |
3664 | 0 | case AArch64::BI__builtin_arm_dsb: |
3665 | 0 | case AArch64::BI__builtin_arm_isb: l = 0; u = 15; break; |
3666 | 0 | case AArch64::BI__builtin_arm_tcancel: l = 0; u = 65535; break; |
3667 | 0 | } |
3668 | | |
3669 | 0 | return SemaBuiltinConstantArgRange(TheCall, i, l, u + l); |
3670 | 0 | } |
3671 | | |
3672 | 0 | static bool isValidBPFPreserveFieldInfoArg(Expr *Arg) { |
3673 | 0 | if (Arg->getType()->getAsPlaceholderType()) |
3674 | 0 | return false; |
3675 | | |
3676 | | // The first argument needs to be a record field access. |
3677 | | // If it is an array element access, we delay decision |
3678 | | // to BPF backend to check whether the access is a |
3679 | | // field access or not. |
3680 | 0 | return (Arg->IgnoreParens()->getObjectKind() == OK_BitField || |
3681 | 0 | isa<MemberExpr>(Arg->IgnoreParens()) || |
3682 | 0 | isa<ArraySubscriptExpr>(Arg->IgnoreParens())); |
3683 | 0 | } |
3684 | | |
3685 | 0 | static bool isValidBPFPreserveTypeInfoArg(Expr *Arg) { |
3686 | 0 | QualType ArgType = Arg->getType(); |
3687 | 0 | if (ArgType->getAsPlaceholderType()) |
3688 | 0 | return false; |
3689 | | |
3690 | | // for TYPE_EXISTENCE/TYPE_MATCH/TYPE_SIZEOF reloc type |
3691 | | // format: |
3692 | | // 1. __builtin_preserve_type_info(*(<type> *)0, flag); |
3693 | | // 2. <type> var; |
3694 | | // __builtin_preserve_type_info(var, flag); |
3695 | 0 | if (!isa<DeclRefExpr>(Arg->IgnoreParens()) && |
3696 | 0 | !isa<UnaryOperator>(Arg->IgnoreParens())) |
3697 | 0 | return false; |
3698 | | |
3699 | | // Typedef type. |
3700 | 0 | if (ArgType->getAs<TypedefType>()) |
3701 | 0 | return true; |
3702 | | |
3703 | | // Record type or Enum type. |
3704 | 0 | const Type *Ty = ArgType->getUnqualifiedDesugaredType(); |
3705 | 0 | if (const auto *RT = Ty->getAs<RecordType>()) { |
3706 | 0 | if (!RT->getDecl()->getDeclName().isEmpty()) |
3707 | 0 | return true; |
3708 | 0 | } else if (const auto *ET = Ty->getAs<EnumType>()) { |
3709 | 0 | if (!ET->getDecl()->getDeclName().isEmpty()) |
3710 | 0 | return true; |
3711 | 0 | } |
3712 | | |
3713 | 0 | return false; |
3714 | 0 | } |
3715 | | |
3716 | 0 | static bool isValidBPFPreserveEnumValueArg(Expr *Arg) { |
3717 | 0 | QualType ArgType = Arg->getType(); |
3718 | 0 | if (ArgType->getAsPlaceholderType()) |
3719 | 0 | return false; |
3720 | | |
3721 | | // for ENUM_VALUE_EXISTENCE/ENUM_VALUE reloc type |
3722 | | // format: |
3723 | | // __builtin_preserve_enum_value(*(<enum_type> *)<enum_value>, |
3724 | | // flag); |
3725 | 0 | const auto *UO = dyn_cast<UnaryOperator>(Arg->IgnoreParens()); |
3726 | 0 | if (!UO) |
3727 | 0 | return false; |
3728 | | |
3729 | 0 | const auto *CE = dyn_cast<CStyleCastExpr>(UO->getSubExpr()); |
3730 | 0 | if (!CE) |
3731 | 0 | return false; |
3732 | 0 | if (CE->getCastKind() != CK_IntegralToPointer && |
3733 | 0 | CE->getCastKind() != CK_NullToPointer) |
3734 | 0 | return false; |
3735 | | |
3736 | | // The integer must be from an EnumConstantDecl. |
3737 | 0 | const auto *DR = dyn_cast<DeclRefExpr>(CE->getSubExpr()); |
3738 | 0 | if (!DR) |
3739 | 0 | return false; |
3740 | | |
3741 | 0 | const EnumConstantDecl *Enumerator = |
3742 | 0 | dyn_cast<EnumConstantDecl>(DR->getDecl()); |
3743 | 0 | if (!Enumerator) |
3744 | 0 | return false; |
3745 | | |
3746 | | // The type must be EnumType. |
3747 | 0 | const Type *Ty = ArgType->getUnqualifiedDesugaredType(); |
3748 | 0 | const auto *ET = Ty->getAs<EnumType>(); |
3749 | 0 | if (!ET) |
3750 | 0 | return false; |
3751 | | |
3752 | | // The enum value must be supported. |
3753 | 0 | return llvm::is_contained(ET->getDecl()->enumerators(), Enumerator); |
3754 | 0 | } |
3755 | | |
3756 | | bool Sema::CheckBPFBuiltinFunctionCall(unsigned BuiltinID, |
3757 | 0 | CallExpr *TheCall) { |
3758 | 0 | assert((BuiltinID == BPF::BI__builtin_preserve_field_info || |
3759 | 0 | BuiltinID == BPF::BI__builtin_btf_type_id || |
3760 | 0 | BuiltinID == BPF::BI__builtin_preserve_type_info || |
3761 | 0 | BuiltinID == BPF::BI__builtin_preserve_enum_value) && |
3762 | 0 | "unexpected BPF builtin"); |
3763 | | |
3764 | 0 | if (checkArgCount(*this, TheCall, 2)) |
3765 | 0 | return true; |
3766 | | |
3767 | | // The second argument needs to be a constant int |
3768 | 0 | Expr *Arg = TheCall->getArg(1); |
3769 | 0 | std::optional<llvm::APSInt> Value = Arg->getIntegerConstantExpr(Context); |
3770 | 0 | diag::kind kind; |
3771 | 0 | if (!Value) { |
3772 | 0 | if (BuiltinID == BPF::BI__builtin_preserve_field_info) |
3773 | 0 | kind = diag::err_preserve_field_info_not_const; |
3774 | 0 | else if (BuiltinID == BPF::BI__builtin_btf_type_id) |
3775 | 0 | kind = diag::err_btf_type_id_not_const; |
3776 | 0 | else if (BuiltinID == BPF::BI__builtin_preserve_type_info) |
3777 | 0 | kind = diag::err_preserve_type_info_not_const; |
3778 | 0 | else |
3779 | 0 | kind = diag::err_preserve_enum_value_not_const; |
3780 | 0 | Diag(Arg->getBeginLoc(), kind) << 2 << Arg->getSourceRange(); |
3781 | 0 | return true; |
3782 | 0 | } |
3783 | | |
3784 | | // The first argument |
3785 | 0 | Arg = TheCall->getArg(0); |
3786 | 0 | bool InvalidArg = false; |
3787 | 0 | bool ReturnUnsignedInt = true; |
3788 | 0 | if (BuiltinID == BPF::BI__builtin_preserve_field_info) { |
3789 | 0 | if (!isValidBPFPreserveFieldInfoArg(Arg)) { |
3790 | 0 | InvalidArg = true; |
3791 | 0 | kind = diag::err_preserve_field_info_not_field; |
3792 | 0 | } |
3793 | 0 | } else if (BuiltinID == BPF::BI__builtin_preserve_type_info) { |
3794 | 0 | if (!isValidBPFPreserveTypeInfoArg(Arg)) { |
3795 | 0 | InvalidArg = true; |
3796 | 0 | kind = diag::err_preserve_type_info_invalid; |
3797 | 0 | } |
3798 | 0 | } else if (BuiltinID == BPF::BI__builtin_preserve_enum_value) { |
3799 | 0 | if (!isValidBPFPreserveEnumValueArg(Arg)) { |
3800 | 0 | InvalidArg = true; |
3801 | 0 | kind = diag::err_preserve_enum_value_invalid; |
3802 | 0 | } |
3803 | 0 | ReturnUnsignedInt = false; |
3804 | 0 | } else if (BuiltinID == BPF::BI__builtin_btf_type_id) { |
3805 | 0 | ReturnUnsignedInt = false; |
3806 | 0 | } |
3807 | |
|
3808 | 0 | if (InvalidArg) { |
3809 | 0 | Diag(Arg->getBeginLoc(), kind) << 1 << Arg->getSourceRange(); |
3810 | 0 | return true; |
3811 | 0 | } |
3812 | | |
3813 | 0 | if (ReturnUnsignedInt) |
3814 | 0 | TheCall->setType(Context.UnsignedIntTy); |
3815 | 0 | else |
3816 | 0 | TheCall->setType(Context.UnsignedLongTy); |
3817 | 0 | return false; |
3818 | 0 | } |
3819 | | |
3820 | 0 | bool Sema::CheckHexagonBuiltinArgument(unsigned BuiltinID, CallExpr *TheCall) { |
3821 | 0 | struct ArgInfo { |
3822 | 0 | uint8_t OpNum; |
3823 | 0 | bool IsSigned; |
3824 | 0 | uint8_t BitWidth; |
3825 | 0 | uint8_t Align; |
3826 | 0 | }; |
3827 | 0 | struct BuiltinInfo { |
3828 | 0 | unsigned BuiltinID; |
3829 | 0 | ArgInfo Infos[2]; |
3830 | 0 | }; |
3831 | |
|
3832 | 0 | static BuiltinInfo Infos[] = { |
3833 | 0 | { Hexagon::BI__builtin_circ_ldd, {{ 3, true, 4, 3 }} }, |
3834 | 0 | { Hexagon::BI__builtin_circ_ldw, {{ 3, true, 4, 2 }} }, |
3835 | 0 | { Hexagon::BI__builtin_circ_ldh, {{ 3, true, 4, 1 }} }, |
3836 | 0 | { Hexagon::BI__builtin_circ_lduh, {{ 3, true, 4, 1 }} }, |
3837 | 0 | { Hexagon::BI__builtin_circ_ldb, {{ 3, true, 4, 0 }} }, |
3838 | 0 | { Hexagon::BI__builtin_circ_ldub, {{ 3, true, 4, 0 }} }, |
3839 | 0 | { Hexagon::BI__builtin_circ_std, {{ 3, true, 4, 3 }} }, |
3840 | 0 | { Hexagon::BI__builtin_circ_stw, {{ 3, true, 4, 2 }} }, |
3841 | 0 | { Hexagon::BI__builtin_circ_sth, {{ 3, true, 4, 1 }} }, |
3842 | 0 | { Hexagon::BI__builtin_circ_sthhi, {{ 3, true, 4, 1 }} }, |
3843 | 0 | { Hexagon::BI__builtin_circ_stb, {{ 3, true, 4, 0 }} }, |
3844 | |
|
3845 | 0 | { Hexagon::BI__builtin_HEXAGON_L2_loadrub_pci, {{ 1, true, 4, 0 }} }, |
3846 | 0 | { Hexagon::BI__builtin_HEXAGON_L2_loadrb_pci, {{ 1, true, 4, 0 }} }, |
3847 | 0 | { Hexagon::BI__builtin_HEXAGON_L2_loadruh_pci, {{ 1, true, 4, 1 }} }, |
3848 | 0 | { Hexagon::BI__builtin_HEXAGON_L2_loadrh_pci, {{ 1, true, 4, 1 }} }, |
3849 | 0 | { Hexagon::BI__builtin_HEXAGON_L2_loadri_pci, {{ 1, true, 4, 2 }} }, |
3850 | 0 | { Hexagon::BI__builtin_HEXAGON_L2_loadrd_pci, {{ 1, true, 4, 3 }} }, |
3851 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_storerb_pci, {{ 1, true, 4, 0 }} }, |
3852 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_storerh_pci, {{ 1, true, 4, 1 }} }, |
3853 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_storerf_pci, {{ 1, true, 4, 1 }} }, |
3854 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_storeri_pci, {{ 1, true, 4, 2 }} }, |
3855 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_storerd_pci, {{ 1, true, 4, 3 }} }, |
3856 | |
|
3857 | 0 | { Hexagon::BI__builtin_HEXAGON_A2_combineii, {{ 1, true, 8, 0 }} }, |
3858 | 0 | { Hexagon::BI__builtin_HEXAGON_A2_tfrih, {{ 1, false, 16, 0 }} }, |
3859 | 0 | { Hexagon::BI__builtin_HEXAGON_A2_tfril, {{ 1, false, 16, 0 }} }, |
3860 | 0 | { Hexagon::BI__builtin_HEXAGON_A2_tfrpi, {{ 0, true, 8, 0 }} }, |
3861 | 0 | { Hexagon::BI__builtin_HEXAGON_A4_bitspliti, {{ 1, false, 5, 0 }} }, |
3862 | 0 | { Hexagon::BI__builtin_HEXAGON_A4_cmpbeqi, {{ 1, false, 8, 0 }} }, |
3863 | 0 | { Hexagon::BI__builtin_HEXAGON_A4_cmpbgti, {{ 1, true, 8, 0 }} }, |
3864 | 0 | { Hexagon::BI__builtin_HEXAGON_A4_cround_ri, {{ 1, false, 5, 0 }} }, |
3865 | 0 | { Hexagon::BI__builtin_HEXAGON_A4_round_ri, {{ 1, false, 5, 0 }} }, |
3866 | 0 | { Hexagon::BI__builtin_HEXAGON_A4_round_ri_sat, {{ 1, false, 5, 0 }} }, |
3867 | 0 | { Hexagon::BI__builtin_HEXAGON_A4_vcmpbeqi, {{ 1, false, 8, 0 }} }, |
3868 | 0 | { Hexagon::BI__builtin_HEXAGON_A4_vcmpbgti, {{ 1, true, 8, 0 }} }, |
3869 | 0 | { Hexagon::BI__builtin_HEXAGON_A4_vcmpbgtui, {{ 1, false, 7, 0 }} }, |
3870 | 0 | { Hexagon::BI__builtin_HEXAGON_A4_vcmpheqi, {{ 1, true, 8, 0 }} }, |
3871 | 0 | { Hexagon::BI__builtin_HEXAGON_A4_vcmphgti, {{ 1, true, 8, 0 }} }, |
3872 | 0 | { Hexagon::BI__builtin_HEXAGON_A4_vcmphgtui, {{ 1, false, 7, 0 }} }, |
3873 | 0 | { Hexagon::BI__builtin_HEXAGON_A4_vcmpweqi, {{ 1, true, 8, 0 }} }, |
3874 | 0 | { Hexagon::BI__builtin_HEXAGON_A4_vcmpwgti, {{ 1, true, 8, 0 }} }, |
3875 | 0 | { Hexagon::BI__builtin_HEXAGON_A4_vcmpwgtui, {{ 1, false, 7, 0 }} }, |
3876 | 0 | { Hexagon::BI__builtin_HEXAGON_C2_bitsclri, {{ 1, false, 6, 0 }} }, |
3877 | 0 | { Hexagon::BI__builtin_HEXAGON_C2_muxii, {{ 2, true, 8, 0 }} }, |
3878 | 0 | { Hexagon::BI__builtin_HEXAGON_C4_nbitsclri, {{ 1, false, 6, 0 }} }, |
3879 | 0 | { Hexagon::BI__builtin_HEXAGON_F2_dfclass, {{ 1, false, 5, 0 }} }, |
3880 | 0 | { Hexagon::BI__builtin_HEXAGON_F2_dfimm_n, {{ 0, false, 10, 0 }} }, |
3881 | 0 | { Hexagon::BI__builtin_HEXAGON_F2_dfimm_p, {{ 0, false, 10, 0 }} }, |
3882 | 0 | { Hexagon::BI__builtin_HEXAGON_F2_sfclass, {{ 1, false, 5, 0 }} }, |
3883 | 0 | { Hexagon::BI__builtin_HEXAGON_F2_sfimm_n, {{ 0, false, 10, 0 }} }, |
3884 | 0 | { Hexagon::BI__builtin_HEXAGON_F2_sfimm_p, {{ 0, false, 10, 0 }} }, |
3885 | 0 | { Hexagon::BI__builtin_HEXAGON_M4_mpyri_addi, {{ 2, false, 6, 0 }} }, |
3886 | 0 | { Hexagon::BI__builtin_HEXAGON_M4_mpyri_addr_u2, {{ 1, false, 6, 2 }} }, |
3887 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_addasl_rrri, {{ 2, false, 3, 0 }} }, |
3888 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_acc, {{ 2, false, 6, 0 }} }, |
3889 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_and, {{ 2, false, 6, 0 }} }, |
3890 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p, {{ 1, false, 6, 0 }} }, |
3891 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_nac, {{ 2, false, 6, 0 }} }, |
3892 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_or, {{ 2, false, 6, 0 }} }, |
3893 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_xacc, {{ 2, false, 6, 0 }} }, |
3894 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_acc, {{ 2, false, 5, 0 }} }, |
3895 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_and, {{ 2, false, 5, 0 }} }, |
3896 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r, {{ 1, false, 5, 0 }} }, |
3897 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_nac, {{ 2, false, 5, 0 }} }, |
3898 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_or, {{ 2, false, 5, 0 }} }, |
3899 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_sat, {{ 1, false, 5, 0 }} }, |
3900 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_xacc, {{ 2, false, 5, 0 }} }, |
3901 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asl_i_vh, {{ 1, false, 4, 0 }} }, |
3902 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asl_i_vw, {{ 1, false, 5, 0 }} }, |
3903 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_acc, {{ 2, false, 6, 0 }} }, |
3904 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_and, {{ 2, false, 6, 0 }} }, |
3905 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p, {{ 1, false, 6, 0 }} }, |
3906 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_nac, {{ 2, false, 6, 0 }} }, |
3907 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_or, {{ 2, false, 6, 0 }} }, |
3908 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_rnd_goodsyntax, |
3909 | 0 | {{ 1, false, 6, 0 }} }, |
3910 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_rnd, {{ 1, false, 6, 0 }} }, |
3911 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_acc, {{ 2, false, 5, 0 }} }, |
3912 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_and, {{ 2, false, 5, 0 }} }, |
3913 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r, {{ 1, false, 5, 0 }} }, |
3914 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_nac, {{ 2, false, 5, 0 }} }, |
3915 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_or, {{ 2, false, 5, 0 }} }, |
3916 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_rnd_goodsyntax, |
3917 | 0 | {{ 1, false, 5, 0 }} }, |
3918 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_rnd, {{ 1, false, 5, 0 }} }, |
3919 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asr_i_svw_trun, {{ 1, false, 5, 0 }} }, |
3920 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asr_i_vh, {{ 1, false, 4, 0 }} }, |
3921 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_asr_i_vw, {{ 1, false, 5, 0 }} }, |
3922 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_clrbit_i, {{ 1, false, 5, 0 }} }, |
3923 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_extractu, {{ 1, false, 5, 0 }, |
3924 | 0 | { 2, false, 5, 0 }} }, |
3925 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_extractup, {{ 1, false, 6, 0 }, |
3926 | 0 | { 2, false, 6, 0 }} }, |
3927 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_insert, {{ 2, false, 5, 0 }, |
3928 | 0 | { 3, false, 5, 0 }} }, |
3929 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_insertp, {{ 2, false, 6, 0 }, |
3930 | 0 | { 3, false, 6, 0 }} }, |
3931 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_acc, {{ 2, false, 6, 0 }} }, |
3932 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_and, {{ 2, false, 6, 0 }} }, |
3933 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p, {{ 1, false, 6, 0 }} }, |
3934 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_nac, {{ 2, false, 6, 0 }} }, |
3935 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_or, {{ 2, false, 6, 0 }} }, |
3936 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_xacc, {{ 2, false, 6, 0 }} }, |
3937 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_acc, {{ 2, false, 5, 0 }} }, |
3938 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_and, {{ 2, false, 5, 0 }} }, |
3939 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r, {{ 1, false, 5, 0 }} }, |
3940 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_nac, {{ 2, false, 5, 0 }} }, |
3941 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_or, {{ 2, false, 5, 0 }} }, |
3942 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_xacc, {{ 2, false, 5, 0 }} }, |
3943 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_vh, {{ 1, false, 4, 0 }} }, |
3944 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_vw, {{ 1, false, 5, 0 }} }, |
3945 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_setbit_i, {{ 1, false, 5, 0 }} }, |
3946 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_tableidxb_goodsyntax, |
3947 | 0 | {{ 2, false, 4, 0 }, |
3948 | 0 | { 3, false, 5, 0 }} }, |
3949 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_tableidxd_goodsyntax, |
3950 | 0 | {{ 2, false, 4, 0 }, |
3951 | 0 | { 3, false, 5, 0 }} }, |
3952 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_tableidxh_goodsyntax, |
3953 | 0 | {{ 2, false, 4, 0 }, |
3954 | 0 | { 3, false, 5, 0 }} }, |
3955 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_tableidxw_goodsyntax, |
3956 | 0 | {{ 2, false, 4, 0 }, |
3957 | 0 | { 3, false, 5, 0 }} }, |
3958 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_togglebit_i, {{ 1, false, 5, 0 }} }, |
3959 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_tstbit_i, {{ 1, false, 5, 0 }} }, |
3960 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_valignib, {{ 2, false, 3, 0 }} }, |
3961 | 0 | { Hexagon::BI__builtin_HEXAGON_S2_vspliceib, {{ 2, false, 3, 0 }} }, |
3962 | 0 | { Hexagon::BI__builtin_HEXAGON_S4_addi_asl_ri, {{ 2, false, 5, 0 }} }, |
3963 | 0 | { Hexagon::BI__builtin_HEXAGON_S4_addi_lsr_ri, {{ 2, false, 5, 0 }} }, |
3964 | 0 | { Hexagon::BI__builtin_HEXAGON_S4_andi_asl_ri, {{ 2, false, 5, 0 }} }, |
3965 | 0 | { Hexagon::BI__builtin_HEXAGON_S4_andi_lsr_ri, {{ 2, false, 5, 0 }} }, |
3966 | 0 | { Hexagon::BI__builtin_HEXAGON_S4_clbaddi, {{ 1, true , 6, 0 }} }, |
3967 | 0 | { Hexagon::BI__builtin_HEXAGON_S4_clbpaddi, {{ 1, true, 6, 0 }} }, |
3968 | 0 | { Hexagon::BI__builtin_HEXAGON_S4_extract, {{ 1, false, 5, 0 }, |
3969 | 0 | { 2, false, 5, 0 }} }, |
3970 | 0 | { Hexagon::BI__builtin_HEXAGON_S4_extractp, {{ 1, false, 6, 0 }, |
3971 | 0 | { 2, false, 6, 0 }} }, |
3972 | 0 | { Hexagon::BI__builtin_HEXAGON_S4_lsli, {{ 0, true, 6, 0 }} }, |
3973 | 0 | { Hexagon::BI__builtin_HEXAGON_S4_ntstbit_i, {{ 1, false, 5, 0 }} }, |
3974 | 0 | { Hexagon::BI__builtin_HEXAGON_S4_ori_asl_ri, {{ 2, false, 5, 0 }} }, |
3975 | 0 | { Hexagon::BI__builtin_HEXAGON_S4_ori_lsr_ri, {{ 2, false, 5, 0 }} }, |
3976 | 0 | { Hexagon::BI__builtin_HEXAGON_S4_subi_asl_ri, {{ 2, false, 5, 0 }} }, |
3977 | 0 | { Hexagon::BI__builtin_HEXAGON_S4_subi_lsr_ri, {{ 2, false, 5, 0 }} }, |
3978 | 0 | { Hexagon::BI__builtin_HEXAGON_S4_vrcrotate_acc, {{ 3, false, 2, 0 }} }, |
3979 | 0 | { Hexagon::BI__builtin_HEXAGON_S4_vrcrotate, {{ 2, false, 2, 0 }} }, |
3980 | 0 | { Hexagon::BI__builtin_HEXAGON_S5_asrhub_rnd_sat_goodsyntax, |
3981 | 0 | {{ 1, false, 4, 0 }} }, |
3982 | 0 | { Hexagon::BI__builtin_HEXAGON_S5_asrhub_sat, {{ 1, false, 4, 0 }} }, |
3983 | 0 | { Hexagon::BI__builtin_HEXAGON_S5_vasrhrnd_goodsyntax, |
3984 | 0 | {{ 1, false, 4, 0 }} }, |
3985 | 0 | { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p, {{ 1, false, 6, 0 }} }, |
3986 | 0 | { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_acc, {{ 2, false, 6, 0 }} }, |
3987 | 0 | { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_and, {{ 2, false, 6, 0 }} }, |
3988 | 0 | { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_nac, {{ 2, false, 6, 0 }} }, |
3989 | 0 | { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_or, {{ 2, false, 6, 0 }} }, |
3990 | 0 | { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_xacc, {{ 2, false, 6, 0 }} }, |
3991 | 0 | { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r, {{ 1, false, 5, 0 }} }, |
3992 | 0 | { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_acc, {{ 2, false, 5, 0 }} }, |
3993 | 0 | { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_and, {{ 2, false, 5, 0 }} }, |
3994 | 0 | { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_nac, {{ 2, false, 5, 0 }} }, |
3995 | 0 | { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_or, {{ 2, false, 5, 0 }} }, |
3996 | 0 | { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_xacc, {{ 2, false, 5, 0 }} }, |
3997 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_valignbi, {{ 2, false, 3, 0 }} }, |
3998 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_valignbi_128B, {{ 2, false, 3, 0 }} }, |
3999 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vlalignbi, {{ 2, false, 3, 0 }} }, |
4000 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vlalignbi_128B, {{ 2, false, 3, 0 }} }, |
4001 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi, {{ 2, false, 1, 0 }} }, |
4002 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi_128B, {{ 2, false, 1, 0 }} }, |
4003 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi_acc, {{ 3, false, 1, 0 }} }, |
4004 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi_acc_128B, |
4005 | 0 | {{ 3, false, 1, 0 }} }, |
4006 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi, {{ 2, false, 1, 0 }} }, |
4007 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi_128B, {{ 2, false, 1, 0 }} }, |
4008 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi_acc, {{ 3, false, 1, 0 }} }, |
4009 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi_acc_128B, |
4010 | 0 | {{ 3, false, 1, 0 }} }, |
4011 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi, {{ 2, false, 1, 0 }} }, |
4012 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi_128B, {{ 2, false, 1, 0 }} }, |
4013 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi_acc, {{ 3, false, 1, 0 }} }, |
4014 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi_acc_128B, |
4015 | 0 | {{ 3, false, 1, 0 }} }, |
4016 | |
|
4017 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_v6mpyhubs10, {{ 2, false, 2, 0 }} }, |
4018 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_v6mpyhubs10_128B, |
4019 | 0 | {{ 2, false, 2, 0 }} }, |
4020 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_v6mpyhubs10_vxx, |
4021 | 0 | {{ 3, false, 2, 0 }} }, |
4022 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_v6mpyhubs10_vxx_128B, |
4023 | 0 | {{ 3, false, 2, 0 }} }, |
4024 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_v6mpyvubs10, {{ 2, false, 2, 0 }} }, |
4025 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_v6mpyvubs10_128B, |
4026 | 0 | {{ 2, false, 2, 0 }} }, |
4027 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_v6mpyvubs10_vxx, |
4028 | 0 | {{ 3, false, 2, 0 }} }, |
4029 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_v6mpyvubs10_vxx_128B, |
4030 | 0 | {{ 3, false, 2, 0 }} }, |
4031 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vlutvvbi, {{ 2, false, 3, 0 }} }, |
4032 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vlutvvbi_128B, {{ 2, false, 3, 0 }} }, |
4033 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vlutvvb_oracci, {{ 3, false, 3, 0 }} }, |
4034 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vlutvvb_oracci_128B, |
4035 | 0 | {{ 3, false, 3, 0 }} }, |
4036 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vlutvwhi, {{ 2, false, 3, 0 }} }, |
4037 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vlutvwhi_128B, {{ 2, false, 3, 0 }} }, |
4038 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vlutvwh_oracci, {{ 3, false, 3, 0 }} }, |
4039 | 0 | { Hexagon::BI__builtin_HEXAGON_V6_vlutvwh_oracci_128B, |
4040 | 0 | {{ 3, false, 3, 0 }} }, |
4041 | 0 | }; |
4042 | | |
4043 | | // Use a dynamically initialized static to sort the table exactly once on |
4044 | | // first run. |
4045 | 0 | static const bool SortOnce = |
4046 | 0 | (llvm::sort(Infos, |
4047 | 0 | [](const BuiltinInfo &LHS, const BuiltinInfo &RHS) { |
4048 | 0 | return LHS.BuiltinID < RHS.BuiltinID; |
4049 | 0 | }), |
4050 | 0 | true); |
4051 | 0 | (void)SortOnce; |
4052 | |
|
4053 | 0 | const BuiltinInfo *F = llvm::partition_point( |
4054 | 0 | Infos, [=](const BuiltinInfo &BI) { return BI.BuiltinID < BuiltinID; }); |
4055 | 0 | if (F == std::end(Infos) || F->BuiltinID != BuiltinID) |
4056 | 0 | return false; |
4057 | | |
4058 | 0 | bool Error = false; |
4059 | |
|
4060 | 0 | for (const ArgInfo &A : F->Infos) { |
4061 | | // Ignore empty ArgInfo elements. |
4062 | 0 | if (A.BitWidth == 0) |
4063 | 0 | continue; |
4064 | | |
4065 | 0 | int32_t Min = A.IsSigned ? -(1 << (A.BitWidth - 1)) : 0; |
4066 | 0 | int32_t Max = (1 << (A.IsSigned ? A.BitWidth - 1 : A.BitWidth)) - 1; |
4067 | 0 | if (!A.Align) { |
4068 | 0 | Error |= SemaBuiltinConstantArgRange(TheCall, A.OpNum, Min, Max); |
4069 | 0 | } else { |
4070 | 0 | unsigned M = 1 << A.Align; |
4071 | 0 | Min *= M; |
4072 | 0 | Max *= M; |
4073 | 0 | Error |= SemaBuiltinConstantArgRange(TheCall, A.OpNum, Min, Max); |
4074 | 0 | Error |= SemaBuiltinConstantArgMultiple(TheCall, A.OpNum, M); |
4075 | 0 | } |
4076 | 0 | } |
4077 | 0 | return Error; |
4078 | 0 | } |
4079 | | |
4080 | | bool Sema::CheckHexagonBuiltinFunctionCall(unsigned BuiltinID, |
4081 | 0 | CallExpr *TheCall) { |
4082 | 0 | return CheckHexagonBuiltinArgument(BuiltinID, TheCall); |
4083 | 0 | } |
4084 | | |
4085 | | bool Sema::CheckLoongArchBuiltinFunctionCall(const TargetInfo &TI, |
4086 | | unsigned BuiltinID, |
4087 | 0 | CallExpr *TheCall) { |
4088 | 0 | switch (BuiltinID) { |
4089 | 0 | default: |
4090 | 0 | break; |
4091 | | // Basic intrinsics. |
4092 | 0 | case LoongArch::BI__builtin_loongarch_cacop_d: |
4093 | 0 | case LoongArch::BI__builtin_loongarch_cacop_w: { |
4094 | 0 | SemaBuiltinConstantArgRange(TheCall, 0, 0, llvm::maxUIntN(5)); |
4095 | 0 | SemaBuiltinConstantArgRange(TheCall, 2, llvm::minIntN(12), |
4096 | 0 | llvm::maxIntN(12)); |
4097 | 0 | break; |
4098 | 0 | } |
4099 | 0 | case LoongArch::BI__builtin_loongarch_break: |
4100 | 0 | case LoongArch::BI__builtin_loongarch_dbar: |
4101 | 0 | case LoongArch::BI__builtin_loongarch_ibar: |
4102 | 0 | case LoongArch::BI__builtin_loongarch_syscall: |
4103 | | // Check if immediate is in [0, 32767]. |
4104 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 32767); |
4105 | 0 | case LoongArch::BI__builtin_loongarch_csrrd_w: |
4106 | 0 | case LoongArch::BI__builtin_loongarch_csrrd_d: |
4107 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 16383); |
4108 | 0 | case LoongArch::BI__builtin_loongarch_csrwr_w: |
4109 | 0 | case LoongArch::BI__builtin_loongarch_csrwr_d: |
4110 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 16383); |
4111 | 0 | case LoongArch::BI__builtin_loongarch_csrxchg_w: |
4112 | 0 | case LoongArch::BI__builtin_loongarch_csrxchg_d: |
4113 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 16383); |
4114 | 0 | case LoongArch::BI__builtin_loongarch_lddir_d: |
4115 | 0 | case LoongArch::BI__builtin_loongarch_ldpte_d: |
4116 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 31); |
4117 | 0 | case LoongArch::BI__builtin_loongarch_movfcsr2gr: |
4118 | 0 | case LoongArch::BI__builtin_loongarch_movgr2fcsr: |
4119 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, llvm::maxUIntN(2)); |
4120 | | |
4121 | | // LSX intrinsics. |
4122 | 0 | case LoongArch::BI__builtin_lsx_vbitclri_b: |
4123 | 0 | case LoongArch::BI__builtin_lsx_vbitrevi_b: |
4124 | 0 | case LoongArch::BI__builtin_lsx_vbitseti_b: |
4125 | 0 | case LoongArch::BI__builtin_lsx_vsat_b: |
4126 | 0 | case LoongArch::BI__builtin_lsx_vsat_bu: |
4127 | 0 | case LoongArch::BI__builtin_lsx_vslli_b: |
4128 | 0 | case LoongArch::BI__builtin_lsx_vsrai_b: |
4129 | 0 | case LoongArch::BI__builtin_lsx_vsrari_b: |
4130 | 0 | case LoongArch::BI__builtin_lsx_vsrli_b: |
4131 | 0 | case LoongArch::BI__builtin_lsx_vsllwil_h_b: |
4132 | 0 | case LoongArch::BI__builtin_lsx_vsllwil_hu_bu: |
4133 | 0 | case LoongArch::BI__builtin_lsx_vrotri_b: |
4134 | 0 | case LoongArch::BI__builtin_lsx_vsrlri_b: |
4135 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 7); |
4136 | 0 | case LoongArch::BI__builtin_lsx_vbitclri_h: |
4137 | 0 | case LoongArch::BI__builtin_lsx_vbitrevi_h: |
4138 | 0 | case LoongArch::BI__builtin_lsx_vbitseti_h: |
4139 | 0 | case LoongArch::BI__builtin_lsx_vsat_h: |
4140 | 0 | case LoongArch::BI__builtin_lsx_vsat_hu: |
4141 | 0 | case LoongArch::BI__builtin_lsx_vslli_h: |
4142 | 0 | case LoongArch::BI__builtin_lsx_vsrai_h: |
4143 | 0 | case LoongArch::BI__builtin_lsx_vsrari_h: |
4144 | 0 | case LoongArch::BI__builtin_lsx_vsrli_h: |
4145 | 0 | case LoongArch::BI__builtin_lsx_vsllwil_w_h: |
4146 | 0 | case LoongArch::BI__builtin_lsx_vsllwil_wu_hu: |
4147 | 0 | case LoongArch::BI__builtin_lsx_vrotri_h: |
4148 | 0 | case LoongArch::BI__builtin_lsx_vsrlri_h: |
4149 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15); |
4150 | 0 | case LoongArch::BI__builtin_lsx_vssrarni_b_h: |
4151 | 0 | case LoongArch::BI__builtin_lsx_vssrarni_bu_h: |
4152 | 0 | case LoongArch::BI__builtin_lsx_vssrani_b_h: |
4153 | 0 | case LoongArch::BI__builtin_lsx_vssrani_bu_h: |
4154 | 0 | case LoongArch::BI__builtin_lsx_vsrarni_b_h: |
4155 | 0 | case LoongArch::BI__builtin_lsx_vsrlni_b_h: |
4156 | 0 | case LoongArch::BI__builtin_lsx_vsrlrni_b_h: |
4157 | 0 | case LoongArch::BI__builtin_lsx_vssrlni_b_h: |
4158 | 0 | case LoongArch::BI__builtin_lsx_vssrlni_bu_h: |
4159 | 0 | case LoongArch::BI__builtin_lsx_vssrlrni_b_h: |
4160 | 0 | case LoongArch::BI__builtin_lsx_vssrlrni_bu_h: |
4161 | 0 | case LoongArch::BI__builtin_lsx_vsrani_b_h: |
4162 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 15); |
4163 | 0 | case LoongArch::BI__builtin_lsx_vslei_bu: |
4164 | 0 | case LoongArch::BI__builtin_lsx_vslei_hu: |
4165 | 0 | case LoongArch::BI__builtin_lsx_vslei_wu: |
4166 | 0 | case LoongArch::BI__builtin_lsx_vslei_du: |
4167 | 0 | case LoongArch::BI__builtin_lsx_vslti_bu: |
4168 | 0 | case LoongArch::BI__builtin_lsx_vslti_hu: |
4169 | 0 | case LoongArch::BI__builtin_lsx_vslti_wu: |
4170 | 0 | case LoongArch::BI__builtin_lsx_vslti_du: |
4171 | 0 | case LoongArch::BI__builtin_lsx_vmaxi_bu: |
4172 | 0 | case LoongArch::BI__builtin_lsx_vmaxi_hu: |
4173 | 0 | case LoongArch::BI__builtin_lsx_vmaxi_wu: |
4174 | 0 | case LoongArch::BI__builtin_lsx_vmaxi_du: |
4175 | 0 | case LoongArch::BI__builtin_lsx_vmini_bu: |
4176 | 0 | case LoongArch::BI__builtin_lsx_vmini_hu: |
4177 | 0 | case LoongArch::BI__builtin_lsx_vmini_wu: |
4178 | 0 | case LoongArch::BI__builtin_lsx_vmini_du: |
4179 | 0 | case LoongArch::BI__builtin_lsx_vaddi_bu: |
4180 | 0 | case LoongArch::BI__builtin_lsx_vaddi_hu: |
4181 | 0 | case LoongArch::BI__builtin_lsx_vaddi_wu: |
4182 | 0 | case LoongArch::BI__builtin_lsx_vaddi_du: |
4183 | 0 | case LoongArch::BI__builtin_lsx_vbitclri_w: |
4184 | 0 | case LoongArch::BI__builtin_lsx_vbitrevi_w: |
4185 | 0 | case LoongArch::BI__builtin_lsx_vbitseti_w: |
4186 | 0 | case LoongArch::BI__builtin_lsx_vsat_w: |
4187 | 0 | case LoongArch::BI__builtin_lsx_vsat_wu: |
4188 | 0 | case LoongArch::BI__builtin_lsx_vslli_w: |
4189 | 0 | case LoongArch::BI__builtin_lsx_vsrai_w: |
4190 | 0 | case LoongArch::BI__builtin_lsx_vsrari_w: |
4191 | 0 | case LoongArch::BI__builtin_lsx_vsrli_w: |
4192 | 0 | case LoongArch::BI__builtin_lsx_vsllwil_d_w: |
4193 | 0 | case LoongArch::BI__builtin_lsx_vsllwil_du_wu: |
4194 | 0 | case LoongArch::BI__builtin_lsx_vsrlri_w: |
4195 | 0 | case LoongArch::BI__builtin_lsx_vrotri_w: |
4196 | 0 | case LoongArch::BI__builtin_lsx_vsubi_bu: |
4197 | 0 | case LoongArch::BI__builtin_lsx_vsubi_hu: |
4198 | 0 | case LoongArch::BI__builtin_lsx_vbsrl_v: |
4199 | 0 | case LoongArch::BI__builtin_lsx_vbsll_v: |
4200 | 0 | case LoongArch::BI__builtin_lsx_vsubi_wu: |
4201 | 0 | case LoongArch::BI__builtin_lsx_vsubi_du: |
4202 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 31); |
4203 | 0 | case LoongArch::BI__builtin_lsx_vssrarni_h_w: |
4204 | 0 | case LoongArch::BI__builtin_lsx_vssrarni_hu_w: |
4205 | 0 | case LoongArch::BI__builtin_lsx_vssrani_h_w: |
4206 | 0 | case LoongArch::BI__builtin_lsx_vssrani_hu_w: |
4207 | 0 | case LoongArch::BI__builtin_lsx_vsrarni_h_w: |
4208 | 0 | case LoongArch::BI__builtin_lsx_vsrani_h_w: |
4209 | 0 | case LoongArch::BI__builtin_lsx_vfrstpi_b: |
4210 | 0 | case LoongArch::BI__builtin_lsx_vfrstpi_h: |
4211 | 0 | case LoongArch::BI__builtin_lsx_vsrlni_h_w: |
4212 | 0 | case LoongArch::BI__builtin_lsx_vsrlrni_h_w: |
4213 | 0 | case LoongArch::BI__builtin_lsx_vssrlni_h_w: |
4214 | 0 | case LoongArch::BI__builtin_lsx_vssrlni_hu_w: |
4215 | 0 | case LoongArch::BI__builtin_lsx_vssrlrni_h_w: |
4216 | 0 | case LoongArch::BI__builtin_lsx_vssrlrni_hu_w: |
4217 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 31); |
4218 | 0 | case LoongArch::BI__builtin_lsx_vbitclri_d: |
4219 | 0 | case LoongArch::BI__builtin_lsx_vbitrevi_d: |
4220 | 0 | case LoongArch::BI__builtin_lsx_vbitseti_d: |
4221 | 0 | case LoongArch::BI__builtin_lsx_vsat_d: |
4222 | 0 | case LoongArch::BI__builtin_lsx_vsat_du: |
4223 | 0 | case LoongArch::BI__builtin_lsx_vslli_d: |
4224 | 0 | case LoongArch::BI__builtin_lsx_vsrai_d: |
4225 | 0 | case LoongArch::BI__builtin_lsx_vsrli_d: |
4226 | 0 | case LoongArch::BI__builtin_lsx_vsrari_d: |
4227 | 0 | case LoongArch::BI__builtin_lsx_vrotri_d: |
4228 | 0 | case LoongArch::BI__builtin_lsx_vsrlri_d: |
4229 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 63); |
4230 | 0 | case LoongArch::BI__builtin_lsx_vssrarni_w_d: |
4231 | 0 | case LoongArch::BI__builtin_lsx_vssrarni_wu_d: |
4232 | 0 | case LoongArch::BI__builtin_lsx_vssrani_w_d: |
4233 | 0 | case LoongArch::BI__builtin_lsx_vssrani_wu_d: |
4234 | 0 | case LoongArch::BI__builtin_lsx_vsrarni_w_d: |
4235 | 0 | case LoongArch::BI__builtin_lsx_vsrlni_w_d: |
4236 | 0 | case LoongArch::BI__builtin_lsx_vsrlrni_w_d: |
4237 | 0 | case LoongArch::BI__builtin_lsx_vssrlni_w_d: |
4238 | 0 | case LoongArch::BI__builtin_lsx_vssrlni_wu_d: |
4239 | 0 | case LoongArch::BI__builtin_lsx_vssrlrni_w_d: |
4240 | 0 | case LoongArch::BI__builtin_lsx_vssrlrni_wu_d: |
4241 | 0 | case LoongArch::BI__builtin_lsx_vsrani_w_d: |
4242 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 63); |
4243 | 0 | case LoongArch::BI__builtin_lsx_vssrarni_d_q: |
4244 | 0 | case LoongArch::BI__builtin_lsx_vssrarni_du_q: |
4245 | 0 | case LoongArch::BI__builtin_lsx_vssrani_d_q: |
4246 | 0 | case LoongArch::BI__builtin_lsx_vssrani_du_q: |
4247 | 0 | case LoongArch::BI__builtin_lsx_vsrarni_d_q: |
4248 | 0 | case LoongArch::BI__builtin_lsx_vssrlni_d_q: |
4249 | 0 | case LoongArch::BI__builtin_lsx_vssrlni_du_q: |
4250 | 0 | case LoongArch::BI__builtin_lsx_vssrlrni_d_q: |
4251 | 0 | case LoongArch::BI__builtin_lsx_vssrlrni_du_q: |
4252 | 0 | case LoongArch::BI__builtin_lsx_vsrani_d_q: |
4253 | 0 | case LoongArch::BI__builtin_lsx_vsrlrni_d_q: |
4254 | 0 | case LoongArch::BI__builtin_lsx_vsrlni_d_q: |
4255 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 127); |
4256 | 0 | case LoongArch::BI__builtin_lsx_vseqi_b: |
4257 | 0 | case LoongArch::BI__builtin_lsx_vseqi_h: |
4258 | 0 | case LoongArch::BI__builtin_lsx_vseqi_w: |
4259 | 0 | case LoongArch::BI__builtin_lsx_vseqi_d: |
4260 | 0 | case LoongArch::BI__builtin_lsx_vslti_b: |
4261 | 0 | case LoongArch::BI__builtin_lsx_vslti_h: |
4262 | 0 | case LoongArch::BI__builtin_lsx_vslti_w: |
4263 | 0 | case LoongArch::BI__builtin_lsx_vslti_d: |
4264 | 0 | case LoongArch::BI__builtin_lsx_vslei_b: |
4265 | 0 | case LoongArch::BI__builtin_lsx_vslei_h: |
4266 | 0 | case LoongArch::BI__builtin_lsx_vslei_w: |
4267 | 0 | case LoongArch::BI__builtin_lsx_vslei_d: |
4268 | 0 | case LoongArch::BI__builtin_lsx_vmaxi_b: |
4269 | 0 | case LoongArch::BI__builtin_lsx_vmaxi_h: |
4270 | 0 | case LoongArch::BI__builtin_lsx_vmaxi_w: |
4271 | 0 | case LoongArch::BI__builtin_lsx_vmaxi_d: |
4272 | 0 | case LoongArch::BI__builtin_lsx_vmini_b: |
4273 | 0 | case LoongArch::BI__builtin_lsx_vmini_h: |
4274 | 0 | case LoongArch::BI__builtin_lsx_vmini_w: |
4275 | 0 | case LoongArch::BI__builtin_lsx_vmini_d: |
4276 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, -16, 15); |
4277 | 0 | case LoongArch::BI__builtin_lsx_vandi_b: |
4278 | 0 | case LoongArch::BI__builtin_lsx_vnori_b: |
4279 | 0 | case LoongArch::BI__builtin_lsx_vori_b: |
4280 | 0 | case LoongArch::BI__builtin_lsx_vshuf4i_b: |
4281 | 0 | case LoongArch::BI__builtin_lsx_vshuf4i_h: |
4282 | 0 | case LoongArch::BI__builtin_lsx_vshuf4i_w: |
4283 | 0 | case LoongArch::BI__builtin_lsx_vxori_b: |
4284 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 255); |
4285 | 0 | case LoongArch::BI__builtin_lsx_vbitseli_b: |
4286 | 0 | case LoongArch::BI__builtin_lsx_vshuf4i_d: |
4287 | 0 | case LoongArch::BI__builtin_lsx_vextrins_b: |
4288 | 0 | case LoongArch::BI__builtin_lsx_vextrins_h: |
4289 | 0 | case LoongArch::BI__builtin_lsx_vextrins_w: |
4290 | 0 | case LoongArch::BI__builtin_lsx_vextrins_d: |
4291 | 0 | case LoongArch::BI__builtin_lsx_vpermi_w: |
4292 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 255); |
4293 | 0 | case LoongArch::BI__builtin_lsx_vpickve2gr_b: |
4294 | 0 | case LoongArch::BI__builtin_lsx_vpickve2gr_bu: |
4295 | 0 | case LoongArch::BI__builtin_lsx_vreplvei_b: |
4296 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15); |
4297 | 0 | case LoongArch::BI__builtin_lsx_vinsgr2vr_b: |
4298 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 15); |
4299 | 0 | case LoongArch::BI__builtin_lsx_vpickve2gr_h: |
4300 | 0 | case LoongArch::BI__builtin_lsx_vpickve2gr_hu: |
4301 | 0 | case LoongArch::BI__builtin_lsx_vreplvei_h: |
4302 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 7); |
4303 | 0 | case LoongArch::BI__builtin_lsx_vinsgr2vr_h: |
4304 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 7); |
4305 | 0 | case LoongArch::BI__builtin_lsx_vpickve2gr_w: |
4306 | 0 | case LoongArch::BI__builtin_lsx_vpickve2gr_wu: |
4307 | 0 | case LoongArch::BI__builtin_lsx_vreplvei_w: |
4308 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 3); |
4309 | 0 | case LoongArch::BI__builtin_lsx_vinsgr2vr_w: |
4310 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 3); |
4311 | 0 | case LoongArch::BI__builtin_lsx_vpickve2gr_d: |
4312 | 0 | case LoongArch::BI__builtin_lsx_vpickve2gr_du: |
4313 | 0 | case LoongArch::BI__builtin_lsx_vreplvei_d: |
4314 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1); |
4315 | 0 | case LoongArch::BI__builtin_lsx_vinsgr2vr_d: |
4316 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 1); |
4317 | 0 | case LoongArch::BI__builtin_lsx_vstelm_b: |
4318 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, -128, 127) || |
4319 | 0 | SemaBuiltinConstantArgRange(TheCall, 3, 0, 15); |
4320 | 0 | case LoongArch::BI__builtin_lsx_vstelm_h: |
4321 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, -256, 254) || |
4322 | 0 | SemaBuiltinConstantArgRange(TheCall, 3, 0, 7); |
4323 | 0 | case LoongArch::BI__builtin_lsx_vstelm_w: |
4324 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, -512, 508) || |
4325 | 0 | SemaBuiltinConstantArgRange(TheCall, 3, 0, 3); |
4326 | 0 | case LoongArch::BI__builtin_lsx_vstelm_d: |
4327 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, -1024, 1016) || |
4328 | 0 | SemaBuiltinConstantArgRange(TheCall, 3, 0, 1); |
4329 | 0 | case LoongArch::BI__builtin_lsx_vldrepl_b: |
4330 | 0 | case LoongArch::BI__builtin_lsx_vld: |
4331 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, -2048, 2047); |
4332 | 0 | case LoongArch::BI__builtin_lsx_vldrepl_h: |
4333 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, -2048, 2046); |
4334 | 0 | case LoongArch::BI__builtin_lsx_vldrepl_w: |
4335 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, -2048, 2044); |
4336 | 0 | case LoongArch::BI__builtin_lsx_vldrepl_d: |
4337 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, -2048, 2040); |
4338 | 0 | case LoongArch::BI__builtin_lsx_vst: |
4339 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, -2048, 2047); |
4340 | 0 | case LoongArch::BI__builtin_lsx_vldi: |
4341 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, -4096, 4095); |
4342 | 0 | case LoongArch::BI__builtin_lsx_vrepli_b: |
4343 | 0 | case LoongArch::BI__builtin_lsx_vrepli_h: |
4344 | 0 | case LoongArch::BI__builtin_lsx_vrepli_w: |
4345 | 0 | case LoongArch::BI__builtin_lsx_vrepli_d: |
4346 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, -512, 511); |
4347 | | |
4348 | | // LASX intrinsics. |
4349 | 0 | case LoongArch::BI__builtin_lasx_xvbitclri_b: |
4350 | 0 | case LoongArch::BI__builtin_lasx_xvbitrevi_b: |
4351 | 0 | case LoongArch::BI__builtin_lasx_xvbitseti_b: |
4352 | 0 | case LoongArch::BI__builtin_lasx_xvsat_b: |
4353 | 0 | case LoongArch::BI__builtin_lasx_xvsat_bu: |
4354 | 0 | case LoongArch::BI__builtin_lasx_xvslli_b: |
4355 | 0 | case LoongArch::BI__builtin_lasx_xvsrai_b: |
4356 | 0 | case LoongArch::BI__builtin_lasx_xvsrari_b: |
4357 | 0 | case LoongArch::BI__builtin_lasx_xvsrli_b: |
4358 | 0 | case LoongArch::BI__builtin_lasx_xvsllwil_h_b: |
4359 | 0 | case LoongArch::BI__builtin_lasx_xvsllwil_hu_bu: |
4360 | 0 | case LoongArch::BI__builtin_lasx_xvrotri_b: |
4361 | 0 | case LoongArch::BI__builtin_lasx_xvsrlri_b: |
4362 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 7); |
4363 | 0 | case LoongArch::BI__builtin_lasx_xvbitclri_h: |
4364 | 0 | case LoongArch::BI__builtin_lasx_xvbitrevi_h: |
4365 | 0 | case LoongArch::BI__builtin_lasx_xvbitseti_h: |
4366 | 0 | case LoongArch::BI__builtin_lasx_xvsat_h: |
4367 | 0 | case LoongArch::BI__builtin_lasx_xvsat_hu: |
4368 | 0 | case LoongArch::BI__builtin_lasx_xvslli_h: |
4369 | 0 | case LoongArch::BI__builtin_lasx_xvsrai_h: |
4370 | 0 | case LoongArch::BI__builtin_lasx_xvsrari_h: |
4371 | 0 | case LoongArch::BI__builtin_lasx_xvsrli_h: |
4372 | 0 | case LoongArch::BI__builtin_lasx_xvsllwil_w_h: |
4373 | 0 | case LoongArch::BI__builtin_lasx_xvsllwil_wu_hu: |
4374 | 0 | case LoongArch::BI__builtin_lasx_xvrotri_h: |
4375 | 0 | case LoongArch::BI__builtin_lasx_xvsrlri_h: |
4376 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15); |
4377 | 0 | case LoongArch::BI__builtin_lasx_xvssrarni_b_h: |
4378 | 0 | case LoongArch::BI__builtin_lasx_xvssrarni_bu_h: |
4379 | 0 | case LoongArch::BI__builtin_lasx_xvssrani_b_h: |
4380 | 0 | case LoongArch::BI__builtin_lasx_xvssrani_bu_h: |
4381 | 0 | case LoongArch::BI__builtin_lasx_xvsrarni_b_h: |
4382 | 0 | case LoongArch::BI__builtin_lasx_xvsrlni_b_h: |
4383 | 0 | case LoongArch::BI__builtin_lasx_xvsrlrni_b_h: |
4384 | 0 | case LoongArch::BI__builtin_lasx_xvssrlni_b_h: |
4385 | 0 | case LoongArch::BI__builtin_lasx_xvssrlni_bu_h: |
4386 | 0 | case LoongArch::BI__builtin_lasx_xvssrlrni_b_h: |
4387 | 0 | case LoongArch::BI__builtin_lasx_xvssrlrni_bu_h: |
4388 | 0 | case LoongArch::BI__builtin_lasx_xvsrani_b_h: |
4389 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 15); |
4390 | 0 | case LoongArch::BI__builtin_lasx_xvslei_bu: |
4391 | 0 | case LoongArch::BI__builtin_lasx_xvslei_hu: |
4392 | 0 | case LoongArch::BI__builtin_lasx_xvslei_wu: |
4393 | 0 | case LoongArch::BI__builtin_lasx_xvslei_du: |
4394 | 0 | case LoongArch::BI__builtin_lasx_xvslti_bu: |
4395 | 0 | case LoongArch::BI__builtin_lasx_xvslti_hu: |
4396 | 0 | case LoongArch::BI__builtin_lasx_xvslti_wu: |
4397 | 0 | case LoongArch::BI__builtin_lasx_xvslti_du: |
4398 | 0 | case LoongArch::BI__builtin_lasx_xvmaxi_bu: |
4399 | 0 | case LoongArch::BI__builtin_lasx_xvmaxi_hu: |
4400 | 0 | case LoongArch::BI__builtin_lasx_xvmaxi_wu: |
4401 | 0 | case LoongArch::BI__builtin_lasx_xvmaxi_du: |
4402 | 0 | case LoongArch::BI__builtin_lasx_xvmini_bu: |
4403 | 0 | case LoongArch::BI__builtin_lasx_xvmini_hu: |
4404 | 0 | case LoongArch::BI__builtin_lasx_xvmini_wu: |
4405 | 0 | case LoongArch::BI__builtin_lasx_xvmini_du: |
4406 | 0 | case LoongArch::BI__builtin_lasx_xvaddi_bu: |
4407 | 0 | case LoongArch::BI__builtin_lasx_xvaddi_hu: |
4408 | 0 | case LoongArch::BI__builtin_lasx_xvaddi_wu: |
4409 | 0 | case LoongArch::BI__builtin_lasx_xvaddi_du: |
4410 | 0 | case LoongArch::BI__builtin_lasx_xvbitclri_w: |
4411 | 0 | case LoongArch::BI__builtin_lasx_xvbitrevi_w: |
4412 | 0 | case LoongArch::BI__builtin_lasx_xvbitseti_w: |
4413 | 0 | case LoongArch::BI__builtin_lasx_xvsat_w: |
4414 | 0 | case LoongArch::BI__builtin_lasx_xvsat_wu: |
4415 | 0 | case LoongArch::BI__builtin_lasx_xvslli_w: |
4416 | 0 | case LoongArch::BI__builtin_lasx_xvsrai_w: |
4417 | 0 | case LoongArch::BI__builtin_lasx_xvsrari_w: |
4418 | 0 | case LoongArch::BI__builtin_lasx_xvsrli_w: |
4419 | 0 | case LoongArch::BI__builtin_lasx_xvsllwil_d_w: |
4420 | 0 | case LoongArch::BI__builtin_lasx_xvsllwil_du_wu: |
4421 | 0 | case LoongArch::BI__builtin_lasx_xvsrlri_w: |
4422 | 0 | case LoongArch::BI__builtin_lasx_xvrotri_w: |
4423 | 0 | case LoongArch::BI__builtin_lasx_xvsubi_bu: |
4424 | 0 | case LoongArch::BI__builtin_lasx_xvsubi_hu: |
4425 | 0 | case LoongArch::BI__builtin_lasx_xvsubi_wu: |
4426 | 0 | case LoongArch::BI__builtin_lasx_xvsubi_du: |
4427 | 0 | case LoongArch::BI__builtin_lasx_xvbsrl_v: |
4428 | 0 | case LoongArch::BI__builtin_lasx_xvbsll_v: |
4429 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 31); |
4430 | 0 | case LoongArch::BI__builtin_lasx_xvssrarni_h_w: |
4431 | 0 | case LoongArch::BI__builtin_lasx_xvssrarni_hu_w: |
4432 | 0 | case LoongArch::BI__builtin_lasx_xvssrani_h_w: |
4433 | 0 | case LoongArch::BI__builtin_lasx_xvssrani_hu_w: |
4434 | 0 | case LoongArch::BI__builtin_lasx_xvsrarni_h_w: |
4435 | 0 | case LoongArch::BI__builtin_lasx_xvsrani_h_w: |
4436 | 0 | case LoongArch::BI__builtin_lasx_xvfrstpi_b: |
4437 | 0 | case LoongArch::BI__builtin_lasx_xvfrstpi_h: |
4438 | 0 | case LoongArch::BI__builtin_lasx_xvsrlni_h_w: |
4439 | 0 | case LoongArch::BI__builtin_lasx_xvsrlrni_h_w: |
4440 | 0 | case LoongArch::BI__builtin_lasx_xvssrlni_h_w: |
4441 | 0 | case LoongArch::BI__builtin_lasx_xvssrlni_hu_w: |
4442 | 0 | case LoongArch::BI__builtin_lasx_xvssrlrni_h_w: |
4443 | 0 | case LoongArch::BI__builtin_lasx_xvssrlrni_hu_w: |
4444 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 31); |
4445 | 0 | case LoongArch::BI__builtin_lasx_xvbitclri_d: |
4446 | 0 | case LoongArch::BI__builtin_lasx_xvbitrevi_d: |
4447 | 0 | case LoongArch::BI__builtin_lasx_xvbitseti_d: |
4448 | 0 | case LoongArch::BI__builtin_lasx_xvsat_d: |
4449 | 0 | case LoongArch::BI__builtin_lasx_xvsat_du: |
4450 | 0 | case LoongArch::BI__builtin_lasx_xvslli_d: |
4451 | 0 | case LoongArch::BI__builtin_lasx_xvsrai_d: |
4452 | 0 | case LoongArch::BI__builtin_lasx_xvsrli_d: |
4453 | 0 | case LoongArch::BI__builtin_lasx_xvsrari_d: |
4454 | 0 | case LoongArch::BI__builtin_lasx_xvrotri_d: |
4455 | 0 | case LoongArch::BI__builtin_lasx_xvsrlri_d: |
4456 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 63); |
4457 | 0 | case LoongArch::BI__builtin_lasx_xvssrarni_w_d: |
4458 | 0 | case LoongArch::BI__builtin_lasx_xvssrarni_wu_d: |
4459 | 0 | case LoongArch::BI__builtin_lasx_xvssrani_w_d: |
4460 | 0 | case LoongArch::BI__builtin_lasx_xvssrani_wu_d: |
4461 | 0 | case LoongArch::BI__builtin_lasx_xvsrarni_w_d: |
4462 | 0 | case LoongArch::BI__builtin_lasx_xvsrlni_w_d: |
4463 | 0 | case LoongArch::BI__builtin_lasx_xvsrlrni_w_d: |
4464 | 0 | case LoongArch::BI__builtin_lasx_xvssrlni_w_d: |
4465 | 0 | case LoongArch::BI__builtin_lasx_xvssrlni_wu_d: |
4466 | 0 | case LoongArch::BI__builtin_lasx_xvssrlrni_w_d: |
4467 | 0 | case LoongArch::BI__builtin_lasx_xvssrlrni_wu_d: |
4468 | 0 | case LoongArch::BI__builtin_lasx_xvsrani_w_d: |
4469 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 63); |
4470 | 0 | case LoongArch::BI__builtin_lasx_xvssrarni_d_q: |
4471 | 0 | case LoongArch::BI__builtin_lasx_xvssrarni_du_q: |
4472 | 0 | case LoongArch::BI__builtin_lasx_xvssrani_d_q: |
4473 | 0 | case LoongArch::BI__builtin_lasx_xvssrani_du_q: |
4474 | 0 | case LoongArch::BI__builtin_lasx_xvsrarni_d_q: |
4475 | 0 | case LoongArch::BI__builtin_lasx_xvssrlni_d_q: |
4476 | 0 | case LoongArch::BI__builtin_lasx_xvssrlni_du_q: |
4477 | 0 | case LoongArch::BI__builtin_lasx_xvssrlrni_d_q: |
4478 | 0 | case LoongArch::BI__builtin_lasx_xvssrlrni_du_q: |
4479 | 0 | case LoongArch::BI__builtin_lasx_xvsrani_d_q: |
4480 | 0 | case LoongArch::BI__builtin_lasx_xvsrlni_d_q: |
4481 | 0 | case LoongArch::BI__builtin_lasx_xvsrlrni_d_q: |
4482 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 127); |
4483 | 0 | case LoongArch::BI__builtin_lasx_xvseqi_b: |
4484 | 0 | case LoongArch::BI__builtin_lasx_xvseqi_h: |
4485 | 0 | case LoongArch::BI__builtin_lasx_xvseqi_w: |
4486 | 0 | case LoongArch::BI__builtin_lasx_xvseqi_d: |
4487 | 0 | case LoongArch::BI__builtin_lasx_xvslti_b: |
4488 | 0 | case LoongArch::BI__builtin_lasx_xvslti_h: |
4489 | 0 | case LoongArch::BI__builtin_lasx_xvslti_w: |
4490 | 0 | case LoongArch::BI__builtin_lasx_xvslti_d: |
4491 | 0 | case LoongArch::BI__builtin_lasx_xvslei_b: |
4492 | 0 | case LoongArch::BI__builtin_lasx_xvslei_h: |
4493 | 0 | case LoongArch::BI__builtin_lasx_xvslei_w: |
4494 | 0 | case LoongArch::BI__builtin_lasx_xvslei_d: |
4495 | 0 | case LoongArch::BI__builtin_lasx_xvmaxi_b: |
4496 | 0 | case LoongArch::BI__builtin_lasx_xvmaxi_h: |
4497 | 0 | case LoongArch::BI__builtin_lasx_xvmaxi_w: |
4498 | 0 | case LoongArch::BI__builtin_lasx_xvmaxi_d: |
4499 | 0 | case LoongArch::BI__builtin_lasx_xvmini_b: |
4500 | 0 | case LoongArch::BI__builtin_lasx_xvmini_h: |
4501 | 0 | case LoongArch::BI__builtin_lasx_xvmini_w: |
4502 | 0 | case LoongArch::BI__builtin_lasx_xvmini_d: |
4503 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, -16, 15); |
4504 | 0 | case LoongArch::BI__builtin_lasx_xvandi_b: |
4505 | 0 | case LoongArch::BI__builtin_lasx_xvnori_b: |
4506 | 0 | case LoongArch::BI__builtin_lasx_xvori_b: |
4507 | 0 | case LoongArch::BI__builtin_lasx_xvshuf4i_b: |
4508 | 0 | case LoongArch::BI__builtin_lasx_xvshuf4i_h: |
4509 | 0 | case LoongArch::BI__builtin_lasx_xvshuf4i_w: |
4510 | 0 | case LoongArch::BI__builtin_lasx_xvxori_b: |
4511 | 0 | case LoongArch::BI__builtin_lasx_xvpermi_d: |
4512 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 255); |
4513 | 0 | case LoongArch::BI__builtin_lasx_xvbitseli_b: |
4514 | 0 | case LoongArch::BI__builtin_lasx_xvshuf4i_d: |
4515 | 0 | case LoongArch::BI__builtin_lasx_xvextrins_b: |
4516 | 0 | case LoongArch::BI__builtin_lasx_xvextrins_h: |
4517 | 0 | case LoongArch::BI__builtin_lasx_xvextrins_w: |
4518 | 0 | case LoongArch::BI__builtin_lasx_xvextrins_d: |
4519 | 0 | case LoongArch::BI__builtin_lasx_xvpermi_q: |
4520 | 0 | case LoongArch::BI__builtin_lasx_xvpermi_w: |
4521 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 255); |
4522 | 0 | case LoongArch::BI__builtin_lasx_xvrepl128vei_b: |
4523 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15); |
4524 | 0 | case LoongArch::BI__builtin_lasx_xvrepl128vei_h: |
4525 | 0 | case LoongArch::BI__builtin_lasx_xvpickve2gr_w: |
4526 | 0 | case LoongArch::BI__builtin_lasx_xvpickve2gr_wu: |
4527 | 0 | case LoongArch::BI__builtin_lasx_xvpickve_w_f: |
4528 | 0 | case LoongArch::BI__builtin_lasx_xvpickve_w: |
4529 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 7); |
4530 | 0 | case LoongArch::BI__builtin_lasx_xvinsgr2vr_w: |
4531 | 0 | case LoongArch::BI__builtin_lasx_xvinsve0_w: |
4532 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 7); |
4533 | 0 | case LoongArch::BI__builtin_lasx_xvrepl128vei_w: |
4534 | 0 | case LoongArch::BI__builtin_lasx_xvpickve2gr_d: |
4535 | 0 | case LoongArch::BI__builtin_lasx_xvpickve2gr_du: |
4536 | 0 | case LoongArch::BI__builtin_lasx_xvpickve_d_f: |
4537 | 0 | case LoongArch::BI__builtin_lasx_xvpickve_d: |
4538 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 3); |
4539 | 0 | case LoongArch::BI__builtin_lasx_xvinsve0_d: |
4540 | 0 | case LoongArch::BI__builtin_lasx_xvinsgr2vr_d: |
4541 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 3); |
4542 | 0 | case LoongArch::BI__builtin_lasx_xvstelm_b: |
4543 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, -128, 127) || |
4544 | 0 | SemaBuiltinConstantArgRange(TheCall, 3, 0, 31); |
4545 | 0 | case LoongArch::BI__builtin_lasx_xvstelm_h: |
4546 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, -256, 254) || |
4547 | 0 | SemaBuiltinConstantArgRange(TheCall, 3, 0, 15); |
4548 | 0 | case LoongArch::BI__builtin_lasx_xvstelm_w: |
4549 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, -512, 508) || |
4550 | 0 | SemaBuiltinConstantArgRange(TheCall, 3, 0, 7); |
4551 | 0 | case LoongArch::BI__builtin_lasx_xvstelm_d: |
4552 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, -1024, 1016) || |
4553 | 0 | SemaBuiltinConstantArgRange(TheCall, 3, 0, 3); |
4554 | 0 | case LoongArch::BI__builtin_lasx_xvrepl128vei_d: |
4555 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1); |
4556 | 0 | case LoongArch::BI__builtin_lasx_xvldrepl_b: |
4557 | 0 | case LoongArch::BI__builtin_lasx_xvld: |
4558 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, -2048, 2047); |
4559 | 0 | case LoongArch::BI__builtin_lasx_xvldrepl_h: |
4560 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, -2048, 2046); |
4561 | 0 | case LoongArch::BI__builtin_lasx_xvldrepl_w: |
4562 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, -2048, 2044); |
4563 | 0 | case LoongArch::BI__builtin_lasx_xvldrepl_d: |
4564 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, -2048, 2040); |
4565 | 0 | case LoongArch::BI__builtin_lasx_xvst: |
4566 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, -2048, 2047); |
4567 | 0 | case LoongArch::BI__builtin_lasx_xvldi: |
4568 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, -4096, 4095); |
4569 | 0 | case LoongArch::BI__builtin_lasx_xvrepli_b: |
4570 | 0 | case LoongArch::BI__builtin_lasx_xvrepli_h: |
4571 | 0 | case LoongArch::BI__builtin_lasx_xvrepli_w: |
4572 | 0 | case LoongArch::BI__builtin_lasx_xvrepli_d: |
4573 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, -512, 511); |
4574 | 0 | } |
4575 | 0 | return false; |
4576 | 0 | } |
4577 | | |
4578 | | bool Sema::CheckMipsBuiltinFunctionCall(const TargetInfo &TI, |
4579 | 0 | unsigned BuiltinID, CallExpr *TheCall) { |
4580 | 0 | return CheckMipsBuiltinCpu(TI, BuiltinID, TheCall) || |
4581 | 0 | CheckMipsBuiltinArgument(BuiltinID, TheCall); |
4582 | 0 | } |
4583 | | |
4584 | | bool Sema::CheckMipsBuiltinCpu(const TargetInfo &TI, unsigned BuiltinID, |
4585 | 0 | CallExpr *TheCall) { |
4586 | |
|
4587 | 0 | if (Mips::BI__builtin_mips_addu_qb <= BuiltinID && |
4588 | 0 | BuiltinID <= Mips::BI__builtin_mips_lwx) { |
4589 | 0 | if (!TI.hasFeature("dsp")) |
4590 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_mips_builtin_requires_dsp); |
4591 | 0 | } |
4592 | | |
4593 | 0 | if (Mips::BI__builtin_mips_absq_s_qb <= BuiltinID && |
4594 | 0 | BuiltinID <= Mips::BI__builtin_mips_subuh_r_qb) { |
4595 | 0 | if (!TI.hasFeature("dspr2")) |
4596 | 0 | return Diag(TheCall->getBeginLoc(), |
4597 | 0 | diag::err_mips_builtin_requires_dspr2); |
4598 | 0 | } |
4599 | | |
4600 | 0 | if (Mips::BI__builtin_msa_add_a_b <= BuiltinID && |
4601 | 0 | BuiltinID <= Mips::BI__builtin_msa_xori_b) { |
4602 | 0 | if (!TI.hasFeature("msa")) |
4603 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_mips_builtin_requires_msa); |
4604 | 0 | } |
4605 | | |
4606 | 0 | return false; |
4607 | 0 | } |
4608 | | |
4609 | | // CheckMipsBuiltinArgument - Checks the constant value passed to the |
4610 | | // intrinsic is correct. The switch statement is ordered by DSP, MSA. The |
4611 | | // ordering for DSP is unspecified. MSA is ordered by the data format used |
4612 | | // by the underlying instruction i.e., df/m, df/n and then by size. |
4613 | | // |
4614 | | // FIXME: The size tests here should instead be tablegen'd along with the |
4615 | | // definitions from include/clang/Basic/BuiltinsMips.def. |
4616 | | // FIXME: GCC is strict on signedness for some of these intrinsics, we should |
4617 | | // be too. |
4618 | 0 | bool Sema::CheckMipsBuiltinArgument(unsigned BuiltinID, CallExpr *TheCall) { |
4619 | 0 | unsigned i = 0, l = 0, u = 0, m = 0; |
4620 | 0 | switch (BuiltinID) { |
4621 | 0 | default: return false; |
4622 | 0 | case Mips::BI__builtin_mips_wrdsp: i = 1; l = 0; u = 63; break; |
4623 | 0 | case Mips::BI__builtin_mips_rddsp: i = 0; l = 0; u = 63; break; |
4624 | 0 | case Mips::BI__builtin_mips_append: i = 2; l = 0; u = 31; break; |
4625 | 0 | case Mips::BI__builtin_mips_balign: i = 2; l = 0; u = 3; break; |
4626 | 0 | case Mips::BI__builtin_mips_precr_sra_ph_w: i = 2; l = 0; u = 31; break; |
4627 | 0 | case Mips::BI__builtin_mips_precr_sra_r_ph_w: i = 2; l = 0; u = 31; break; |
4628 | 0 | case Mips::BI__builtin_mips_prepend: i = 2; l = 0; u = 31; break; |
4629 | | // MSA intrinsics. Instructions (which the intrinsics maps to) which use the |
4630 | | // df/m field. |
4631 | | // These intrinsics take an unsigned 3 bit immediate. |
4632 | 0 | case Mips::BI__builtin_msa_bclri_b: |
4633 | 0 | case Mips::BI__builtin_msa_bnegi_b: |
4634 | 0 | case Mips::BI__builtin_msa_bseti_b: |
4635 | 0 | case Mips::BI__builtin_msa_sat_s_b: |
4636 | 0 | case Mips::BI__builtin_msa_sat_u_b: |
4637 | 0 | case Mips::BI__builtin_msa_slli_b: |
4638 | 0 | case Mips::BI__builtin_msa_srai_b: |
4639 | 0 | case Mips::BI__builtin_msa_srari_b: |
4640 | 0 | case Mips::BI__builtin_msa_srli_b: |
4641 | 0 | case Mips::BI__builtin_msa_srlri_b: i = 1; l = 0; u = 7; break; |
4642 | 0 | case Mips::BI__builtin_msa_binsli_b: |
4643 | 0 | case Mips::BI__builtin_msa_binsri_b: i = 2; l = 0; u = 7; break; |
4644 | | // These intrinsics take an unsigned 4 bit immediate. |
4645 | 0 | case Mips::BI__builtin_msa_bclri_h: |
4646 | 0 | case Mips::BI__builtin_msa_bnegi_h: |
4647 | 0 | case Mips::BI__builtin_msa_bseti_h: |
4648 | 0 | case Mips::BI__builtin_msa_sat_s_h: |
4649 | 0 | case Mips::BI__builtin_msa_sat_u_h: |
4650 | 0 | case Mips::BI__builtin_msa_slli_h: |
4651 | 0 | case Mips::BI__builtin_msa_srai_h: |
4652 | 0 | case Mips::BI__builtin_msa_srari_h: |
4653 | 0 | case Mips::BI__builtin_msa_srli_h: |
4654 | 0 | case Mips::BI__builtin_msa_srlri_h: i = 1; l = 0; u = 15; break; |
4655 | 0 | case Mips::BI__builtin_msa_binsli_h: |
4656 | 0 | case Mips::BI__builtin_msa_binsri_h: i = 2; l = 0; u = 15; break; |
4657 | | // These intrinsics take an unsigned 5 bit immediate. |
4658 | | // The first block of intrinsics actually have an unsigned 5 bit field, |
4659 | | // not a df/n field. |
4660 | 0 | case Mips::BI__builtin_msa_cfcmsa: |
4661 | 0 | case Mips::BI__builtin_msa_ctcmsa: i = 0; l = 0; u = 31; break; |
4662 | 0 | case Mips::BI__builtin_msa_clei_u_b: |
4663 | 0 | case Mips::BI__builtin_msa_clei_u_h: |
4664 | 0 | case Mips::BI__builtin_msa_clei_u_w: |
4665 | 0 | case Mips::BI__builtin_msa_clei_u_d: |
4666 | 0 | case Mips::BI__builtin_msa_clti_u_b: |
4667 | 0 | case Mips::BI__builtin_msa_clti_u_h: |
4668 | 0 | case Mips::BI__builtin_msa_clti_u_w: |
4669 | 0 | case Mips::BI__builtin_msa_clti_u_d: |
4670 | 0 | case Mips::BI__builtin_msa_maxi_u_b: |
4671 | 0 | case Mips::BI__builtin_msa_maxi_u_h: |
4672 | 0 | case Mips::BI__builtin_msa_maxi_u_w: |
4673 | 0 | case Mips::BI__builtin_msa_maxi_u_d: |
4674 | 0 | case Mips::BI__builtin_msa_mini_u_b: |
4675 | 0 | case Mips::BI__builtin_msa_mini_u_h: |
4676 | 0 | case Mips::BI__builtin_msa_mini_u_w: |
4677 | 0 | case Mips::BI__builtin_msa_mini_u_d: |
4678 | 0 | case Mips::BI__builtin_msa_addvi_b: |
4679 | 0 | case Mips::BI__builtin_msa_addvi_h: |
4680 | 0 | case Mips::BI__builtin_msa_addvi_w: |
4681 | 0 | case Mips::BI__builtin_msa_addvi_d: |
4682 | 0 | case Mips::BI__builtin_msa_bclri_w: |
4683 | 0 | case Mips::BI__builtin_msa_bnegi_w: |
4684 | 0 | case Mips::BI__builtin_msa_bseti_w: |
4685 | 0 | case Mips::BI__builtin_msa_sat_s_w: |
4686 | 0 | case Mips::BI__builtin_msa_sat_u_w: |
4687 | 0 | case Mips::BI__builtin_msa_slli_w: |
4688 | 0 | case Mips::BI__builtin_msa_srai_w: |
4689 | 0 | case Mips::BI__builtin_msa_srari_w: |
4690 | 0 | case Mips::BI__builtin_msa_srli_w: |
4691 | 0 | case Mips::BI__builtin_msa_srlri_w: |
4692 | 0 | case Mips::BI__builtin_msa_subvi_b: |
4693 | 0 | case Mips::BI__builtin_msa_subvi_h: |
4694 | 0 | case Mips::BI__builtin_msa_subvi_w: |
4695 | 0 | case Mips::BI__builtin_msa_subvi_d: i = 1; l = 0; u = 31; break; |
4696 | 0 | case Mips::BI__builtin_msa_binsli_w: |
4697 | 0 | case Mips::BI__builtin_msa_binsri_w: i = 2; l = 0; u = 31; break; |
4698 | | // These intrinsics take an unsigned 6 bit immediate. |
4699 | 0 | case Mips::BI__builtin_msa_bclri_d: |
4700 | 0 | case Mips::BI__builtin_msa_bnegi_d: |
4701 | 0 | case Mips::BI__builtin_msa_bseti_d: |
4702 | 0 | case Mips::BI__builtin_msa_sat_s_d: |
4703 | 0 | case Mips::BI__builtin_msa_sat_u_d: |
4704 | 0 | case Mips::BI__builtin_msa_slli_d: |
4705 | 0 | case Mips::BI__builtin_msa_srai_d: |
4706 | 0 | case Mips::BI__builtin_msa_srari_d: |
4707 | 0 | case Mips::BI__builtin_msa_srli_d: |
4708 | 0 | case Mips::BI__builtin_msa_srlri_d: i = 1; l = 0; u = 63; break; |
4709 | 0 | case Mips::BI__builtin_msa_binsli_d: |
4710 | 0 | case Mips::BI__builtin_msa_binsri_d: i = 2; l = 0; u = 63; break; |
4711 | | // These intrinsics take a signed 5 bit immediate. |
4712 | 0 | case Mips::BI__builtin_msa_ceqi_b: |
4713 | 0 | case Mips::BI__builtin_msa_ceqi_h: |
4714 | 0 | case Mips::BI__builtin_msa_ceqi_w: |
4715 | 0 | case Mips::BI__builtin_msa_ceqi_d: |
4716 | 0 | case Mips::BI__builtin_msa_clti_s_b: |
4717 | 0 | case Mips::BI__builtin_msa_clti_s_h: |
4718 | 0 | case Mips::BI__builtin_msa_clti_s_w: |
4719 | 0 | case Mips::BI__builtin_msa_clti_s_d: |
4720 | 0 | case Mips::BI__builtin_msa_clei_s_b: |
4721 | 0 | case Mips::BI__builtin_msa_clei_s_h: |
4722 | 0 | case Mips::BI__builtin_msa_clei_s_w: |
4723 | 0 | case Mips::BI__builtin_msa_clei_s_d: |
4724 | 0 | case Mips::BI__builtin_msa_maxi_s_b: |
4725 | 0 | case Mips::BI__builtin_msa_maxi_s_h: |
4726 | 0 | case Mips::BI__builtin_msa_maxi_s_w: |
4727 | 0 | case Mips::BI__builtin_msa_maxi_s_d: |
4728 | 0 | case Mips::BI__builtin_msa_mini_s_b: |
4729 | 0 | case Mips::BI__builtin_msa_mini_s_h: |
4730 | 0 | case Mips::BI__builtin_msa_mini_s_w: |
4731 | 0 | case Mips::BI__builtin_msa_mini_s_d: i = 1; l = -16; u = 15; break; |
4732 | | // These intrinsics take an unsigned 8 bit immediate. |
4733 | 0 | case Mips::BI__builtin_msa_andi_b: |
4734 | 0 | case Mips::BI__builtin_msa_nori_b: |
4735 | 0 | case Mips::BI__builtin_msa_ori_b: |
4736 | 0 | case Mips::BI__builtin_msa_shf_b: |
4737 | 0 | case Mips::BI__builtin_msa_shf_h: |
4738 | 0 | case Mips::BI__builtin_msa_shf_w: |
4739 | 0 | case Mips::BI__builtin_msa_xori_b: i = 1; l = 0; u = 255; break; |
4740 | 0 | case Mips::BI__builtin_msa_bseli_b: |
4741 | 0 | case Mips::BI__builtin_msa_bmnzi_b: |
4742 | 0 | case Mips::BI__builtin_msa_bmzi_b: i = 2; l = 0; u = 255; break; |
4743 | | // df/n format |
4744 | | // These intrinsics take an unsigned 4 bit immediate. |
4745 | 0 | case Mips::BI__builtin_msa_copy_s_b: |
4746 | 0 | case Mips::BI__builtin_msa_copy_u_b: |
4747 | 0 | case Mips::BI__builtin_msa_insve_b: |
4748 | 0 | case Mips::BI__builtin_msa_splati_b: i = 1; l = 0; u = 15; break; |
4749 | 0 | case Mips::BI__builtin_msa_sldi_b: i = 2; l = 0; u = 15; break; |
4750 | | // These intrinsics take an unsigned 3 bit immediate. |
4751 | 0 | case Mips::BI__builtin_msa_copy_s_h: |
4752 | 0 | case Mips::BI__builtin_msa_copy_u_h: |
4753 | 0 | case Mips::BI__builtin_msa_insve_h: |
4754 | 0 | case Mips::BI__builtin_msa_splati_h: i = 1; l = 0; u = 7; break; |
4755 | 0 | case Mips::BI__builtin_msa_sldi_h: i = 2; l = 0; u = 7; break; |
4756 | | // These intrinsics take an unsigned 2 bit immediate. |
4757 | 0 | case Mips::BI__builtin_msa_copy_s_w: |
4758 | 0 | case Mips::BI__builtin_msa_copy_u_w: |
4759 | 0 | case Mips::BI__builtin_msa_insve_w: |
4760 | 0 | case Mips::BI__builtin_msa_splati_w: i = 1; l = 0; u = 3; break; |
4761 | 0 | case Mips::BI__builtin_msa_sldi_w: i = 2; l = 0; u = 3; break; |
4762 | | // These intrinsics take an unsigned 1 bit immediate. |
4763 | 0 | case Mips::BI__builtin_msa_copy_s_d: |
4764 | 0 | case Mips::BI__builtin_msa_copy_u_d: |
4765 | 0 | case Mips::BI__builtin_msa_insve_d: |
4766 | 0 | case Mips::BI__builtin_msa_splati_d: i = 1; l = 0; u = 1; break; |
4767 | 0 | case Mips::BI__builtin_msa_sldi_d: i = 2; l = 0; u = 1; break; |
4768 | | // Memory offsets and immediate loads. |
4769 | | // These intrinsics take a signed 10 bit immediate. |
4770 | 0 | case Mips::BI__builtin_msa_ldi_b: i = 0; l = -128; u = 255; break; |
4771 | 0 | case Mips::BI__builtin_msa_ldi_h: |
4772 | 0 | case Mips::BI__builtin_msa_ldi_w: |
4773 | 0 | case Mips::BI__builtin_msa_ldi_d: i = 0; l = -512; u = 511; break; |
4774 | 0 | case Mips::BI__builtin_msa_ld_b: i = 1; l = -512; u = 511; m = 1; break; |
4775 | 0 | case Mips::BI__builtin_msa_ld_h: i = 1; l = -1024; u = 1022; m = 2; break; |
4776 | 0 | case Mips::BI__builtin_msa_ld_w: i = 1; l = -2048; u = 2044; m = 4; break; |
4777 | 0 | case Mips::BI__builtin_msa_ld_d: i = 1; l = -4096; u = 4088; m = 8; break; |
4778 | 0 | case Mips::BI__builtin_msa_ldr_d: i = 1; l = -4096; u = 4088; m = 8; break; |
4779 | 0 | case Mips::BI__builtin_msa_ldr_w: i = 1; l = -2048; u = 2044; m = 4; break; |
4780 | 0 | case Mips::BI__builtin_msa_st_b: i = 2; l = -512; u = 511; m = 1; break; |
4781 | 0 | case Mips::BI__builtin_msa_st_h: i = 2; l = -1024; u = 1022; m = 2; break; |
4782 | 0 | case Mips::BI__builtin_msa_st_w: i = 2; l = -2048; u = 2044; m = 4; break; |
4783 | 0 | case Mips::BI__builtin_msa_st_d: i = 2; l = -4096; u = 4088; m = 8; break; |
4784 | 0 | case Mips::BI__builtin_msa_str_d: i = 2; l = -4096; u = 4088; m = 8; break; |
4785 | 0 | case Mips::BI__builtin_msa_str_w: i = 2; l = -2048; u = 2044; m = 4; break; |
4786 | 0 | } |
4787 | | |
4788 | 0 | if (!m) |
4789 | 0 | return SemaBuiltinConstantArgRange(TheCall, i, l, u); |
4790 | | |
4791 | 0 | return SemaBuiltinConstantArgRange(TheCall, i, l, u) || |
4792 | 0 | SemaBuiltinConstantArgMultiple(TheCall, i, m); |
4793 | 0 | } |
4794 | | |
4795 | | /// DecodePPCMMATypeFromStr - This decodes one PPC MMA type descriptor from Str, |
4796 | | /// advancing the pointer over the consumed characters. The decoded type is |
4797 | | /// returned. If the decoded type represents a constant integer with a |
4798 | | /// constraint on its value then Mask is set to that value. The type descriptors |
4799 | | /// used in Str are specific to PPC MMA builtins and are documented in the file |
4800 | | /// defining the PPC builtins. |
4801 | | static QualType DecodePPCMMATypeFromStr(ASTContext &Context, const char *&Str, |
4802 | 0 | unsigned &Mask) { |
4803 | 0 | bool RequireICE = false; |
4804 | 0 | ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None; |
4805 | 0 | switch (*Str++) { |
4806 | 0 | case 'V': |
4807 | 0 | return Context.getVectorType(Context.UnsignedCharTy, 16, |
4808 | 0 | VectorKind::AltiVecVector); |
4809 | 0 | case 'i': { |
4810 | 0 | char *End; |
4811 | 0 | unsigned size = strtoul(Str, &End, 10); |
4812 | 0 | assert(End != Str && "Missing constant parameter constraint"); |
4813 | 0 | Str = End; |
4814 | 0 | Mask = size; |
4815 | 0 | return Context.IntTy; |
4816 | 0 | } |
4817 | 0 | case 'W': { |
4818 | 0 | char *End; |
4819 | 0 | unsigned size = strtoul(Str, &End, 10); |
4820 | 0 | assert(End != Str && "Missing PowerPC MMA type size"); |
4821 | 0 | Str = End; |
4822 | 0 | QualType Type; |
4823 | 0 | switch (size) { |
4824 | 0 | #define PPC_VECTOR_TYPE(typeName, Id, size) \ |
4825 | 0 | case size: Type = Context.Id##Ty; break; |
4826 | 0 | #include "clang/Basic/PPCTypes.def" |
4827 | 0 | default: llvm_unreachable("Invalid PowerPC MMA vector type"); |
4828 | 0 | } |
4829 | 0 | bool CheckVectorArgs = false; |
4830 | 0 | while (!CheckVectorArgs) { |
4831 | 0 | switch (*Str++) { |
4832 | 0 | case '*': |
4833 | 0 | Type = Context.getPointerType(Type); |
4834 | 0 | break; |
4835 | 0 | case 'C': |
4836 | 0 | Type = Type.withConst(); |
4837 | 0 | break; |
4838 | 0 | default: |
4839 | 0 | CheckVectorArgs = true; |
4840 | 0 | --Str; |
4841 | 0 | break; |
4842 | 0 | } |
4843 | 0 | } |
4844 | 0 | return Type; |
4845 | 0 | } |
4846 | 0 | default: |
4847 | 0 | return Context.DecodeTypeStr(--Str, Context, Error, RequireICE, true); |
4848 | 0 | } |
4849 | 0 | } |
4850 | | |
4851 | 0 | static bool isPPC_64Builtin(unsigned BuiltinID) { |
4852 | | // These builtins only work on PPC 64bit targets. |
4853 | 0 | switch (BuiltinID) { |
4854 | 0 | case PPC::BI__builtin_divde: |
4855 | 0 | case PPC::BI__builtin_divdeu: |
4856 | 0 | case PPC::BI__builtin_bpermd: |
4857 | 0 | case PPC::BI__builtin_pdepd: |
4858 | 0 | case PPC::BI__builtin_pextd: |
4859 | 0 | case PPC::BI__builtin_ppc_ldarx: |
4860 | 0 | case PPC::BI__builtin_ppc_stdcx: |
4861 | 0 | case PPC::BI__builtin_ppc_tdw: |
4862 | 0 | case PPC::BI__builtin_ppc_trapd: |
4863 | 0 | case PPC::BI__builtin_ppc_cmpeqb: |
4864 | 0 | case PPC::BI__builtin_ppc_setb: |
4865 | 0 | case PPC::BI__builtin_ppc_mulhd: |
4866 | 0 | case PPC::BI__builtin_ppc_mulhdu: |
4867 | 0 | case PPC::BI__builtin_ppc_maddhd: |
4868 | 0 | case PPC::BI__builtin_ppc_maddhdu: |
4869 | 0 | case PPC::BI__builtin_ppc_maddld: |
4870 | 0 | case PPC::BI__builtin_ppc_load8r: |
4871 | 0 | case PPC::BI__builtin_ppc_store8r: |
4872 | 0 | case PPC::BI__builtin_ppc_insert_exp: |
4873 | 0 | case PPC::BI__builtin_ppc_extract_sig: |
4874 | 0 | case PPC::BI__builtin_ppc_addex: |
4875 | 0 | case PPC::BI__builtin_darn: |
4876 | 0 | case PPC::BI__builtin_darn_raw: |
4877 | 0 | case PPC::BI__builtin_ppc_compare_and_swaplp: |
4878 | 0 | case PPC::BI__builtin_ppc_fetch_and_addlp: |
4879 | 0 | case PPC::BI__builtin_ppc_fetch_and_andlp: |
4880 | 0 | case PPC::BI__builtin_ppc_fetch_and_orlp: |
4881 | 0 | case PPC::BI__builtin_ppc_fetch_and_swaplp: |
4882 | 0 | return true; |
4883 | 0 | } |
4884 | 0 | return false; |
4885 | 0 | } |
4886 | | |
4887 | | /// Returns true if the argument consists of one contiguous run of 1s with any |
4888 | | /// number of 0s on either side. The 1s are allowed to wrap from LSB to MSB, so |
4889 | | /// 0x000FFF0, 0x0000FFFF, 0xFF0000FF, 0x0 are all runs. 0x0F0F0000 is not, |
4890 | | /// since all 1s are not contiguous. |
4891 | 0 | bool Sema::SemaValueIsRunOfOnes(CallExpr *TheCall, unsigned ArgNum) { |
4892 | 0 | llvm::APSInt Result; |
4893 | | // We can't check the value of a dependent argument. |
4894 | 0 | Expr *Arg = TheCall->getArg(ArgNum); |
4895 | 0 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
4896 | 0 | return false; |
4897 | | |
4898 | | // Check constant-ness first. |
4899 | 0 | if (SemaBuiltinConstantArg(TheCall, ArgNum, Result)) |
4900 | 0 | return true; |
4901 | | |
4902 | | // Check contiguous run of 1s, 0xFF0000FF is also a run of 1s. |
4903 | 0 | if (Result.isShiftedMask() || (~Result).isShiftedMask()) |
4904 | 0 | return false; |
4905 | | |
4906 | 0 | return Diag(TheCall->getBeginLoc(), |
4907 | 0 | diag::err_argument_not_contiguous_bit_field) |
4908 | 0 | << ArgNum << Arg->getSourceRange(); |
4909 | 0 | } |
4910 | | |
4911 | | bool Sema::CheckPPCBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID, |
4912 | 0 | CallExpr *TheCall) { |
4913 | 0 | unsigned i = 0, l = 0, u = 0; |
4914 | 0 | bool IsTarget64Bit = TI.getTypeWidth(TI.getIntPtrType()) == 64; |
4915 | 0 | llvm::APSInt Result; |
4916 | |
|
4917 | 0 | if (isPPC_64Builtin(BuiltinID) && !IsTarget64Bit) |
4918 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_64_bit_builtin_32_bit_tgt) |
4919 | 0 | << TheCall->getSourceRange(); |
4920 | | |
4921 | 0 | switch (BuiltinID) { |
4922 | 0 | default: return false; |
4923 | 0 | case PPC::BI__builtin_altivec_crypto_vshasigmaw: |
4924 | 0 | case PPC::BI__builtin_altivec_crypto_vshasigmad: |
4925 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) || |
4926 | 0 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 15); |
4927 | 0 | case PPC::BI__builtin_altivec_dss: |
4928 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 3); |
4929 | 0 | case PPC::BI__builtin_tbegin: |
4930 | 0 | case PPC::BI__builtin_tend: |
4931 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 1); |
4932 | 0 | case PPC::BI__builtin_tsr: |
4933 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 7); |
4934 | 0 | case PPC::BI__builtin_tabortwc: |
4935 | 0 | case PPC::BI__builtin_tabortdc: |
4936 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 31); |
4937 | 0 | case PPC::BI__builtin_tabortwci: |
4938 | 0 | case PPC::BI__builtin_tabortdci: |
4939 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 31) || |
4940 | 0 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 31); |
4941 | | // According to GCC 'Basic PowerPC Built-in Functions Available on ISA 2.05', |
4942 | | // __builtin_(un)pack_longdouble are available only if long double uses IBM |
4943 | | // extended double representation. |
4944 | 0 | case PPC::BI__builtin_unpack_longdouble: |
4945 | 0 | if (SemaBuiltinConstantArgRange(TheCall, 1, 0, 1)) |
4946 | 0 | return true; |
4947 | 0 | [[fallthrough]]; |
4948 | 0 | case PPC::BI__builtin_pack_longdouble: |
4949 | 0 | if (&TI.getLongDoubleFormat() != &llvm::APFloat::PPCDoubleDouble()) |
4950 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_ppc_builtin_requires_abi) |
4951 | 0 | << "ibmlongdouble"; |
4952 | 0 | return false; |
4953 | 0 | case PPC::BI__builtin_altivec_dst: |
4954 | 0 | case PPC::BI__builtin_altivec_dstt: |
4955 | 0 | case PPC::BI__builtin_altivec_dstst: |
4956 | 0 | case PPC::BI__builtin_altivec_dststt: |
4957 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 3); |
4958 | 0 | case PPC::BI__builtin_vsx_xxpermdi: |
4959 | 0 | case PPC::BI__builtin_vsx_xxsldwi: |
4960 | 0 | return SemaBuiltinVSX(TheCall); |
4961 | 0 | case PPC::BI__builtin_unpack_vector_int128: |
4962 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1); |
4963 | 0 | case PPC::BI__builtin_altivec_vgnb: |
4964 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 2, 7); |
4965 | 0 | case PPC::BI__builtin_vsx_xxeval: |
4966 | 0 | return SemaBuiltinConstantArgRange(TheCall, 3, 0, 255); |
4967 | 0 | case PPC::BI__builtin_altivec_vsldbi: |
4968 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 7); |
4969 | 0 | case PPC::BI__builtin_altivec_vsrdbi: |
4970 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 7); |
4971 | 0 | case PPC::BI__builtin_vsx_xxpermx: |
4972 | 0 | return SemaBuiltinConstantArgRange(TheCall, 3, 0, 7); |
4973 | 0 | case PPC::BI__builtin_ppc_tw: |
4974 | 0 | case PPC::BI__builtin_ppc_tdw: |
4975 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 1, 31); |
4976 | 0 | case PPC::BI__builtin_ppc_cmprb: |
4977 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 1); |
4978 | | // For __rlwnm, __rlwimi and __rldimi, the last parameter mask must |
4979 | | // be a constant that represents a contiguous bit field. |
4980 | 0 | case PPC::BI__builtin_ppc_rlwnm: |
4981 | 0 | return SemaValueIsRunOfOnes(TheCall, 2); |
4982 | 0 | case PPC::BI__builtin_ppc_rlwimi: |
4983 | 0 | case PPC::BI__builtin_ppc_rldimi: |
4984 | 0 | return SemaBuiltinConstantArg(TheCall, 2, Result) || |
4985 | 0 | SemaValueIsRunOfOnes(TheCall, 3); |
4986 | 0 | case PPC::BI__builtin_ppc_addex: { |
4987 | 0 | if (SemaBuiltinConstantArgRange(TheCall, 2, 0, 3)) |
4988 | 0 | return true; |
4989 | | // Output warning for reserved values 1 to 3. |
4990 | 0 | int ArgValue = |
4991 | 0 | TheCall->getArg(2)->getIntegerConstantExpr(Context)->getSExtValue(); |
4992 | 0 | if (ArgValue != 0) |
4993 | 0 | Diag(TheCall->getBeginLoc(), diag::warn_argument_undefined_behaviour) |
4994 | 0 | << ArgValue; |
4995 | 0 | return false; |
4996 | 0 | } |
4997 | 0 | case PPC::BI__builtin_ppc_mtfsb0: |
4998 | 0 | case PPC::BI__builtin_ppc_mtfsb1: |
4999 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 31); |
5000 | 0 | case PPC::BI__builtin_ppc_mtfsf: |
5001 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 255); |
5002 | 0 | case PPC::BI__builtin_ppc_mtfsfi: |
5003 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 7) || |
5004 | 0 | SemaBuiltinConstantArgRange(TheCall, 1, 0, 15); |
5005 | 0 | case PPC::BI__builtin_ppc_alignx: |
5006 | 0 | return SemaBuiltinConstantArgPower2(TheCall, 0); |
5007 | 0 | case PPC::BI__builtin_ppc_rdlam: |
5008 | 0 | return SemaValueIsRunOfOnes(TheCall, 2); |
5009 | 0 | case PPC::BI__builtin_vsx_ldrmb: |
5010 | 0 | case PPC::BI__builtin_vsx_strmb: |
5011 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 1, 16); |
5012 | 0 | case PPC::BI__builtin_altivec_vcntmbb: |
5013 | 0 | case PPC::BI__builtin_altivec_vcntmbh: |
5014 | 0 | case PPC::BI__builtin_altivec_vcntmbw: |
5015 | 0 | case PPC::BI__builtin_altivec_vcntmbd: |
5016 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1); |
5017 | 0 | case PPC::BI__builtin_vsx_xxgenpcvbm: |
5018 | 0 | case PPC::BI__builtin_vsx_xxgenpcvhm: |
5019 | 0 | case PPC::BI__builtin_vsx_xxgenpcvwm: |
5020 | 0 | case PPC::BI__builtin_vsx_xxgenpcvdm: |
5021 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 3); |
5022 | 0 | case PPC::BI__builtin_ppc_test_data_class: { |
5023 | | // Check if the first argument of the __builtin_ppc_test_data_class call is |
5024 | | // valid. The argument must be 'float' or 'double' or '__float128'. |
5025 | 0 | QualType ArgType = TheCall->getArg(0)->getType(); |
5026 | 0 | if (ArgType != QualType(Context.FloatTy) && |
5027 | 0 | ArgType != QualType(Context.DoubleTy) && |
5028 | 0 | ArgType != QualType(Context.Float128Ty)) |
5029 | 0 | return Diag(TheCall->getBeginLoc(), |
5030 | 0 | diag::err_ppc_invalid_test_data_class_type); |
5031 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 127); |
5032 | 0 | } |
5033 | 0 | case PPC::BI__builtin_ppc_maxfe: |
5034 | 0 | case PPC::BI__builtin_ppc_minfe: |
5035 | 0 | case PPC::BI__builtin_ppc_maxfl: |
5036 | 0 | case PPC::BI__builtin_ppc_minfl: |
5037 | 0 | case PPC::BI__builtin_ppc_maxfs: |
5038 | 0 | case PPC::BI__builtin_ppc_minfs: { |
5039 | 0 | if (Context.getTargetInfo().getTriple().isOSAIX() && |
5040 | 0 | (BuiltinID == PPC::BI__builtin_ppc_maxfe || |
5041 | 0 | BuiltinID == PPC::BI__builtin_ppc_minfe)) |
5042 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_target_unsupported_type) |
5043 | 0 | << "builtin" << true << 128 << QualType(Context.LongDoubleTy) |
5044 | 0 | << false << Context.getTargetInfo().getTriple().str(); |
5045 | | // Argument type should be exact. |
5046 | 0 | QualType ArgType = QualType(Context.LongDoubleTy); |
5047 | 0 | if (BuiltinID == PPC::BI__builtin_ppc_maxfl || |
5048 | 0 | BuiltinID == PPC::BI__builtin_ppc_minfl) |
5049 | 0 | ArgType = QualType(Context.DoubleTy); |
5050 | 0 | else if (BuiltinID == PPC::BI__builtin_ppc_maxfs || |
5051 | 0 | BuiltinID == PPC::BI__builtin_ppc_minfs) |
5052 | 0 | ArgType = QualType(Context.FloatTy); |
5053 | 0 | for (unsigned I = 0, E = TheCall->getNumArgs(); I < E; ++I) |
5054 | 0 | if (TheCall->getArg(I)->getType() != ArgType) |
5055 | 0 | return Diag(TheCall->getBeginLoc(), |
5056 | 0 | diag::err_typecheck_convert_incompatible) |
5057 | 0 | << TheCall->getArg(I)->getType() << ArgType << 1 << 0 << 0; |
5058 | 0 | return false; |
5059 | 0 | } |
5060 | 0 | #define CUSTOM_BUILTIN(Name, Intr, Types, Acc, Feature) \ |
5061 | 0 | case PPC::BI__builtin_##Name: \ |
5062 | 0 | return SemaBuiltinPPCMMACall(TheCall, BuiltinID, Types); |
5063 | 0 | #include "clang/Basic/BuiltinsPPC.def" |
5064 | 0 | } |
5065 | 0 | return SemaBuiltinConstantArgRange(TheCall, i, l, u); |
5066 | 0 | } |
5067 | | |
5068 | | // Check if the given type is a non-pointer PPC MMA type. This function is used |
5069 | | // in Sema to prevent invalid uses of restricted PPC MMA types. |
5070 | 0 | bool Sema::CheckPPCMMAType(QualType Type, SourceLocation TypeLoc) { |
5071 | 0 | if (Type->isPointerType() || Type->isArrayType()) |
5072 | 0 | return false; |
5073 | | |
5074 | 0 | QualType CoreType = Type.getCanonicalType().getUnqualifiedType(); |
5075 | 0 | #define PPC_VECTOR_TYPE(Name, Id, Size) || CoreType == Context.Id##Ty |
5076 | 0 | if (false |
5077 | 0 | #include "clang/Basic/PPCTypes.def" |
5078 | 0 | ) { |
5079 | 0 | Diag(TypeLoc, diag::err_ppc_invalid_use_mma_type); |
5080 | 0 | return true; |
5081 | 0 | } |
5082 | 0 | return false; |
5083 | 0 | } |
5084 | | |
5085 | | bool Sema::CheckAMDGCNBuiltinFunctionCall(unsigned BuiltinID, |
5086 | 0 | CallExpr *TheCall) { |
5087 | | // position of memory order and scope arguments in the builtin |
5088 | 0 | unsigned OrderIndex, ScopeIndex; |
5089 | 0 | switch (BuiltinID) { |
5090 | 0 | case AMDGPU::BI__builtin_amdgcn_atomic_inc32: |
5091 | 0 | case AMDGPU::BI__builtin_amdgcn_atomic_inc64: |
5092 | 0 | case AMDGPU::BI__builtin_amdgcn_atomic_dec32: |
5093 | 0 | case AMDGPU::BI__builtin_amdgcn_atomic_dec64: |
5094 | 0 | OrderIndex = 2; |
5095 | 0 | ScopeIndex = 3; |
5096 | 0 | break; |
5097 | 0 | case AMDGPU::BI__builtin_amdgcn_fence: |
5098 | 0 | OrderIndex = 0; |
5099 | 0 | ScopeIndex = 1; |
5100 | 0 | break; |
5101 | 0 | default: |
5102 | 0 | return false; |
5103 | 0 | } |
5104 | | |
5105 | 0 | ExprResult Arg = TheCall->getArg(OrderIndex); |
5106 | 0 | auto ArgExpr = Arg.get(); |
5107 | 0 | Expr::EvalResult ArgResult; |
5108 | |
|
5109 | 0 | if (!ArgExpr->EvaluateAsInt(ArgResult, Context)) |
5110 | 0 | return Diag(ArgExpr->getExprLoc(), diag::err_typecheck_expect_int) |
5111 | 0 | << ArgExpr->getType(); |
5112 | 0 | auto Ord = ArgResult.Val.getInt().getZExtValue(); |
5113 | | |
5114 | | // Check validity of memory ordering as per C11 / C++11's memody model. |
5115 | | // Only fence needs check. Atomic dec/inc allow all memory orders. |
5116 | 0 | if (!llvm::isValidAtomicOrderingCABI(Ord)) |
5117 | 0 | return Diag(ArgExpr->getBeginLoc(), |
5118 | 0 | diag::warn_atomic_op_has_invalid_memory_order) |
5119 | 0 | << 0 << ArgExpr->getSourceRange(); |
5120 | 0 | switch (static_cast<llvm::AtomicOrderingCABI>(Ord)) { |
5121 | 0 | case llvm::AtomicOrderingCABI::relaxed: |
5122 | 0 | case llvm::AtomicOrderingCABI::consume: |
5123 | 0 | if (BuiltinID == AMDGPU::BI__builtin_amdgcn_fence) |
5124 | 0 | return Diag(ArgExpr->getBeginLoc(), |
5125 | 0 | diag::warn_atomic_op_has_invalid_memory_order) |
5126 | 0 | << 0 << ArgExpr->getSourceRange(); |
5127 | 0 | break; |
5128 | 0 | case llvm::AtomicOrderingCABI::acquire: |
5129 | 0 | case llvm::AtomicOrderingCABI::release: |
5130 | 0 | case llvm::AtomicOrderingCABI::acq_rel: |
5131 | 0 | case llvm::AtomicOrderingCABI::seq_cst: |
5132 | 0 | break; |
5133 | 0 | } |
5134 | | |
5135 | 0 | Arg = TheCall->getArg(ScopeIndex); |
5136 | 0 | ArgExpr = Arg.get(); |
5137 | 0 | Expr::EvalResult ArgResult1; |
5138 | | // Check that sync scope is a constant literal |
5139 | 0 | if (!ArgExpr->EvaluateAsConstantExpr(ArgResult1, Context)) |
5140 | 0 | return Diag(ArgExpr->getExprLoc(), diag::err_expr_not_string_literal) |
5141 | 0 | << ArgExpr->getType(); |
5142 | | |
5143 | 0 | return false; |
5144 | 0 | } |
5145 | | |
5146 | 0 | bool Sema::CheckRISCVLMUL(CallExpr *TheCall, unsigned ArgNum) { |
5147 | 0 | llvm::APSInt Result; |
5148 | | |
5149 | | // We can't check the value of a dependent argument. |
5150 | 0 | Expr *Arg = TheCall->getArg(ArgNum); |
5151 | 0 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
5152 | 0 | return false; |
5153 | | |
5154 | | // Check constant-ness first. |
5155 | 0 | if (SemaBuiltinConstantArg(TheCall, ArgNum, Result)) |
5156 | 0 | return true; |
5157 | | |
5158 | 0 | int64_t Val = Result.getSExtValue(); |
5159 | 0 | if ((Val >= 0 && Val <= 3) || (Val >= 5 && Val <= 7)) |
5160 | 0 | return false; |
5161 | | |
5162 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_riscv_builtin_invalid_lmul) |
5163 | 0 | << Arg->getSourceRange(); |
5164 | 0 | } |
5165 | | |
5166 | | static bool CheckInvalidVLENandLMUL(const TargetInfo &TI, CallExpr *TheCall, |
5167 | 0 | Sema &S, QualType Type, int EGW) { |
5168 | 0 | assert((EGW == 128 || EGW == 256) && "EGW can only be 128 or 256 bits"); |
5169 | | |
5170 | | // LMUL * VLEN >= EGW |
5171 | 0 | ASTContext::BuiltinVectorTypeInfo Info = |
5172 | 0 | S.Context.getBuiltinVectorTypeInfo(Type->castAs<BuiltinType>()); |
5173 | 0 | unsigned ElemSize = S.Context.getTypeSize(Info.ElementType); |
5174 | 0 | unsigned MinElemCount = Info.EC.getKnownMinValue(); |
5175 | |
|
5176 | 0 | unsigned EGS = EGW / ElemSize; |
5177 | | // If EGS is less than or equal to the minimum number of elements, then the |
5178 | | // type is valid. |
5179 | 0 | if (EGS <= MinElemCount) |
5180 | 0 | return false; |
5181 | | |
5182 | | // Otherwise, we need vscale to be at least EGS / MinElemCont. |
5183 | 0 | assert(EGS % MinElemCount == 0); |
5184 | 0 | unsigned VScaleFactor = EGS / MinElemCount; |
5185 | | // Vscale is VLEN/RVVBitsPerBlock. |
5186 | 0 | unsigned MinRequiredVLEN = VScaleFactor * llvm::RISCV::RVVBitsPerBlock; |
5187 | 0 | std::string RequiredExt = "zvl" + std::to_string(MinRequiredVLEN) + "b"; |
5188 | 0 | if (!TI.hasFeature(RequiredExt)) |
5189 | 0 | return S.Diag(TheCall->getBeginLoc(), |
5190 | 0 | diag::err_riscv_type_requires_extension) << Type << RequiredExt; |
5191 | | |
5192 | 0 | return false; |
5193 | 0 | } |
5194 | | |
5195 | | bool Sema::CheckRISCVBuiltinFunctionCall(const TargetInfo &TI, |
5196 | | unsigned BuiltinID, |
5197 | 0 | CallExpr *TheCall) { |
5198 | | // CodeGenFunction can also detect this, but this gives a better error |
5199 | | // message. |
5200 | 0 | bool FeatureMissing = false; |
5201 | 0 | SmallVector<StringRef> ReqFeatures; |
5202 | 0 | StringRef Features = Context.BuiltinInfo.getRequiredFeatures(BuiltinID); |
5203 | 0 | Features.split(ReqFeatures, ',', -1, false); |
5204 | | |
5205 | | // Check if each required feature is included |
5206 | 0 | for (StringRef F : ReqFeatures) { |
5207 | 0 | SmallVector<StringRef> ReqOpFeatures; |
5208 | 0 | F.split(ReqOpFeatures, '|'); |
5209 | |
|
5210 | 0 | if (llvm::none_of(ReqOpFeatures, |
5211 | 0 | [&TI](StringRef OF) { return TI.hasFeature(OF); })) { |
5212 | 0 | std::string FeatureStrs; |
5213 | 0 | bool IsExtension = true; |
5214 | 0 | for (StringRef OF : ReqOpFeatures) { |
5215 | | // If the feature is 64bit, alter the string so it will print better in |
5216 | | // the diagnostic. |
5217 | 0 | if (OF == "64bit") { |
5218 | 0 | assert(ReqOpFeatures.size() == 1 && "Expected '64bit' to be alone"); |
5219 | 0 | OF = "RV64"; |
5220 | 0 | IsExtension = false; |
5221 | 0 | } |
5222 | 0 | if (OF == "32bit") { |
5223 | 0 | assert(ReqOpFeatures.size() == 1 && "Expected '32bit' to be alone"); |
5224 | 0 | OF = "RV32"; |
5225 | 0 | IsExtension = false; |
5226 | 0 | } |
5227 | | |
5228 | | // Convert features like "zbr" and "experimental-zbr" to "Zbr". |
5229 | 0 | OF.consume_front("experimental-"); |
5230 | 0 | std::string FeatureStr = OF.str(); |
5231 | 0 | FeatureStr[0] = std::toupper(FeatureStr[0]); |
5232 | | // Combine strings. |
5233 | 0 | FeatureStrs += FeatureStrs.empty() ? "" : ", "; |
5234 | 0 | FeatureStrs += "'"; |
5235 | 0 | FeatureStrs += FeatureStr; |
5236 | 0 | FeatureStrs += "'"; |
5237 | 0 | } |
5238 | | // Error message |
5239 | 0 | FeatureMissing = true; |
5240 | 0 | Diag(TheCall->getBeginLoc(), diag::err_riscv_builtin_requires_extension) |
5241 | 0 | << IsExtension |
5242 | 0 | << TheCall->getSourceRange() << StringRef(FeatureStrs); |
5243 | 0 | } |
5244 | 0 | } |
5245 | |
|
5246 | 0 | if (FeatureMissing) |
5247 | 0 | return true; |
5248 | | |
5249 | | // vmulh.vv, vmulh.vx, vmulhu.vv, vmulhu.vx, vmulhsu.vv, vmulhsu.vx, |
5250 | | // vsmul.vv, vsmul.vx are not included for EEW=64 in Zve64*. |
5251 | 0 | switch (BuiltinID) { |
5252 | 0 | default: |
5253 | 0 | break; |
5254 | 0 | case RISCVVector::BI__builtin_rvv_vmulhsu_vv: |
5255 | 0 | case RISCVVector::BI__builtin_rvv_vmulhsu_vx: |
5256 | 0 | case RISCVVector::BI__builtin_rvv_vmulhsu_vv_tu: |
5257 | 0 | case RISCVVector::BI__builtin_rvv_vmulhsu_vx_tu: |
5258 | 0 | case RISCVVector::BI__builtin_rvv_vmulhsu_vv_m: |
5259 | 0 | case RISCVVector::BI__builtin_rvv_vmulhsu_vx_m: |
5260 | 0 | case RISCVVector::BI__builtin_rvv_vmulhsu_vv_mu: |
5261 | 0 | case RISCVVector::BI__builtin_rvv_vmulhsu_vx_mu: |
5262 | 0 | case RISCVVector::BI__builtin_rvv_vmulhsu_vv_tum: |
5263 | 0 | case RISCVVector::BI__builtin_rvv_vmulhsu_vx_tum: |
5264 | 0 | case RISCVVector::BI__builtin_rvv_vmulhsu_vv_tumu: |
5265 | 0 | case RISCVVector::BI__builtin_rvv_vmulhsu_vx_tumu: |
5266 | 0 | case RISCVVector::BI__builtin_rvv_vmulhu_vv: |
5267 | 0 | case RISCVVector::BI__builtin_rvv_vmulhu_vx: |
5268 | 0 | case RISCVVector::BI__builtin_rvv_vmulhu_vv_tu: |
5269 | 0 | case RISCVVector::BI__builtin_rvv_vmulhu_vx_tu: |
5270 | 0 | case RISCVVector::BI__builtin_rvv_vmulhu_vv_m: |
5271 | 0 | case RISCVVector::BI__builtin_rvv_vmulhu_vx_m: |
5272 | 0 | case RISCVVector::BI__builtin_rvv_vmulhu_vv_mu: |
5273 | 0 | case RISCVVector::BI__builtin_rvv_vmulhu_vx_mu: |
5274 | 0 | case RISCVVector::BI__builtin_rvv_vmulhu_vv_tum: |
5275 | 0 | case RISCVVector::BI__builtin_rvv_vmulhu_vx_tum: |
5276 | 0 | case RISCVVector::BI__builtin_rvv_vmulhu_vv_tumu: |
5277 | 0 | case RISCVVector::BI__builtin_rvv_vmulhu_vx_tumu: |
5278 | 0 | case RISCVVector::BI__builtin_rvv_vmulh_vv: |
5279 | 0 | case RISCVVector::BI__builtin_rvv_vmulh_vx: |
5280 | 0 | case RISCVVector::BI__builtin_rvv_vmulh_vv_tu: |
5281 | 0 | case RISCVVector::BI__builtin_rvv_vmulh_vx_tu: |
5282 | 0 | case RISCVVector::BI__builtin_rvv_vmulh_vv_m: |
5283 | 0 | case RISCVVector::BI__builtin_rvv_vmulh_vx_m: |
5284 | 0 | case RISCVVector::BI__builtin_rvv_vmulh_vv_mu: |
5285 | 0 | case RISCVVector::BI__builtin_rvv_vmulh_vx_mu: |
5286 | 0 | case RISCVVector::BI__builtin_rvv_vmulh_vv_tum: |
5287 | 0 | case RISCVVector::BI__builtin_rvv_vmulh_vx_tum: |
5288 | 0 | case RISCVVector::BI__builtin_rvv_vmulh_vv_tumu: |
5289 | 0 | case RISCVVector::BI__builtin_rvv_vmulh_vx_tumu: |
5290 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vv: |
5291 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vx: |
5292 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vv_tu: |
5293 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vx_tu: |
5294 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vv_m: |
5295 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vx_m: |
5296 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vv_mu: |
5297 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vx_mu: |
5298 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vv_tum: |
5299 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vx_tum: |
5300 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vv_tumu: |
5301 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vx_tumu: { |
5302 | 0 | ASTContext::BuiltinVectorTypeInfo Info = Context.getBuiltinVectorTypeInfo( |
5303 | 0 | TheCall->getType()->castAs<BuiltinType>()); |
5304 | |
|
5305 | 0 | if (Context.getTypeSize(Info.ElementType) == 64 && !TI.hasFeature("v")) |
5306 | 0 | return Diag(TheCall->getBeginLoc(), |
5307 | 0 | diag::err_riscv_builtin_requires_extension) |
5308 | 0 | << /* IsExtension */ true << TheCall->getSourceRange() << "v"; |
5309 | | |
5310 | 0 | break; |
5311 | 0 | } |
5312 | 0 | } |
5313 | | |
5314 | 0 | switch (BuiltinID) { |
5315 | 0 | case RISCVVector::BI__builtin_rvv_vsetvli: |
5316 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 3) || |
5317 | 0 | CheckRISCVLMUL(TheCall, 2); |
5318 | 0 | case RISCVVector::BI__builtin_rvv_vsetvlimax: |
5319 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 3) || |
5320 | 0 | CheckRISCVLMUL(TheCall, 1); |
5321 | 0 | case RISCVVector::BI__builtin_rvv_vget_v: { |
5322 | 0 | ASTContext::BuiltinVectorTypeInfo ResVecInfo = |
5323 | 0 | Context.getBuiltinVectorTypeInfo(cast<BuiltinType>( |
5324 | 0 | TheCall->getType().getCanonicalType().getTypePtr())); |
5325 | 0 | ASTContext::BuiltinVectorTypeInfo VecInfo = |
5326 | 0 | Context.getBuiltinVectorTypeInfo(cast<BuiltinType>( |
5327 | 0 | TheCall->getArg(0)->getType().getCanonicalType().getTypePtr())); |
5328 | 0 | unsigned MaxIndex; |
5329 | 0 | if (VecInfo.NumVectors != 1) // vget for tuple type |
5330 | 0 | MaxIndex = VecInfo.NumVectors; |
5331 | 0 | else // vget for non-tuple type |
5332 | 0 | MaxIndex = (VecInfo.EC.getKnownMinValue() * VecInfo.NumVectors) / |
5333 | 0 | (ResVecInfo.EC.getKnownMinValue() * ResVecInfo.NumVectors); |
5334 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, MaxIndex - 1); |
5335 | 0 | } |
5336 | 0 | case RISCVVector::BI__builtin_rvv_vset_v: { |
5337 | 0 | ASTContext::BuiltinVectorTypeInfo ResVecInfo = |
5338 | 0 | Context.getBuiltinVectorTypeInfo(cast<BuiltinType>( |
5339 | 0 | TheCall->getType().getCanonicalType().getTypePtr())); |
5340 | 0 | ASTContext::BuiltinVectorTypeInfo VecInfo = |
5341 | 0 | Context.getBuiltinVectorTypeInfo(cast<BuiltinType>( |
5342 | 0 | TheCall->getArg(2)->getType().getCanonicalType().getTypePtr())); |
5343 | 0 | unsigned MaxIndex; |
5344 | 0 | if (ResVecInfo.NumVectors != 1) // vset for tuple type |
5345 | 0 | MaxIndex = ResVecInfo.NumVectors; |
5346 | 0 | else // vset fo non-tuple type |
5347 | 0 | MaxIndex = (ResVecInfo.EC.getKnownMinValue() * ResVecInfo.NumVectors) / |
5348 | 0 | (VecInfo.EC.getKnownMinValue() * VecInfo.NumVectors); |
5349 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, MaxIndex - 1); |
5350 | 0 | } |
5351 | | // Vector Crypto |
5352 | 0 | case RISCVVector::BI__builtin_rvv_vaeskf1_vi_tu: |
5353 | 0 | case RISCVVector::BI__builtin_rvv_vaeskf2_vi_tu: |
5354 | 0 | case RISCVVector::BI__builtin_rvv_vaeskf2_vi: |
5355 | 0 | case RISCVVector::BI__builtin_rvv_vsm4k_vi_tu: { |
5356 | 0 | QualType Op1Type = TheCall->getArg(0)->getType(); |
5357 | 0 | QualType Op2Type = TheCall->getArg(1)->getType(); |
5358 | 0 | return CheckInvalidVLENandLMUL(TI, TheCall, *this, Op1Type, 128) || |
5359 | 0 | CheckInvalidVLENandLMUL(TI, TheCall, *this, Op2Type, 128) || |
5360 | 0 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 31); |
5361 | 0 | } |
5362 | 0 | case RISCVVector::BI__builtin_rvv_vsm3c_vi_tu: |
5363 | 0 | case RISCVVector::BI__builtin_rvv_vsm3c_vi: { |
5364 | 0 | QualType Op1Type = TheCall->getArg(0)->getType(); |
5365 | 0 | return CheckInvalidVLENandLMUL(TI, TheCall, *this, Op1Type, 256) || |
5366 | 0 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 31); |
5367 | 0 | } |
5368 | 0 | case RISCVVector::BI__builtin_rvv_vaeskf1_vi: |
5369 | 0 | case RISCVVector::BI__builtin_rvv_vsm4k_vi: { |
5370 | 0 | QualType Op1Type = TheCall->getArg(0)->getType(); |
5371 | 0 | return CheckInvalidVLENandLMUL(TI, TheCall, *this, Op1Type, 128) || |
5372 | 0 | SemaBuiltinConstantArgRange(TheCall, 1, 0, 31); |
5373 | 0 | } |
5374 | 0 | case RISCVVector::BI__builtin_rvv_vaesdf_vv: |
5375 | 0 | case RISCVVector::BI__builtin_rvv_vaesdf_vs: |
5376 | 0 | case RISCVVector::BI__builtin_rvv_vaesdm_vv: |
5377 | 0 | case RISCVVector::BI__builtin_rvv_vaesdm_vs: |
5378 | 0 | case RISCVVector::BI__builtin_rvv_vaesef_vv: |
5379 | 0 | case RISCVVector::BI__builtin_rvv_vaesef_vs: |
5380 | 0 | case RISCVVector::BI__builtin_rvv_vaesem_vv: |
5381 | 0 | case RISCVVector::BI__builtin_rvv_vaesem_vs: |
5382 | 0 | case RISCVVector::BI__builtin_rvv_vaesz_vs: |
5383 | 0 | case RISCVVector::BI__builtin_rvv_vsm4r_vv: |
5384 | 0 | case RISCVVector::BI__builtin_rvv_vsm4r_vs: |
5385 | 0 | case RISCVVector::BI__builtin_rvv_vaesdf_vv_tu: |
5386 | 0 | case RISCVVector::BI__builtin_rvv_vaesdf_vs_tu: |
5387 | 0 | case RISCVVector::BI__builtin_rvv_vaesdm_vv_tu: |
5388 | 0 | case RISCVVector::BI__builtin_rvv_vaesdm_vs_tu: |
5389 | 0 | case RISCVVector::BI__builtin_rvv_vaesef_vv_tu: |
5390 | 0 | case RISCVVector::BI__builtin_rvv_vaesef_vs_tu: |
5391 | 0 | case RISCVVector::BI__builtin_rvv_vaesem_vv_tu: |
5392 | 0 | case RISCVVector::BI__builtin_rvv_vaesem_vs_tu: |
5393 | 0 | case RISCVVector::BI__builtin_rvv_vaesz_vs_tu: |
5394 | 0 | case RISCVVector::BI__builtin_rvv_vsm4r_vv_tu: |
5395 | 0 | case RISCVVector::BI__builtin_rvv_vsm4r_vs_tu: { |
5396 | 0 | QualType Op1Type = TheCall->getArg(0)->getType(); |
5397 | 0 | QualType Op2Type = TheCall->getArg(1)->getType(); |
5398 | 0 | return CheckInvalidVLENandLMUL(TI, TheCall, *this, Op1Type, 128) || |
5399 | 0 | CheckInvalidVLENandLMUL(TI, TheCall, *this, Op2Type, 128); |
5400 | 0 | } |
5401 | 0 | case RISCVVector::BI__builtin_rvv_vsha2ch_vv: |
5402 | 0 | case RISCVVector::BI__builtin_rvv_vsha2cl_vv: |
5403 | 0 | case RISCVVector::BI__builtin_rvv_vsha2ms_vv: |
5404 | 0 | case RISCVVector::BI__builtin_rvv_vsha2ch_vv_tu: |
5405 | 0 | case RISCVVector::BI__builtin_rvv_vsha2cl_vv_tu: |
5406 | 0 | case RISCVVector::BI__builtin_rvv_vsha2ms_vv_tu: { |
5407 | 0 | QualType Op1Type = TheCall->getArg(0)->getType(); |
5408 | 0 | QualType Op2Type = TheCall->getArg(1)->getType(); |
5409 | 0 | QualType Op3Type = TheCall->getArg(2)->getType(); |
5410 | 0 | ASTContext::BuiltinVectorTypeInfo Info = |
5411 | 0 | Context.getBuiltinVectorTypeInfo(Op1Type->castAs<BuiltinType>()); |
5412 | 0 | uint64_t ElemSize = Context.getTypeSize(Info.ElementType); |
5413 | 0 | if (ElemSize == 64 && !TI.hasFeature("zvknhb")) |
5414 | 0 | return Diag(TheCall->getBeginLoc(), |
5415 | 0 | diag::err_riscv_builtin_requires_extension) |
5416 | 0 | << /* IsExtension */ true << TheCall->getSourceRange() << "zvknb"; |
5417 | | |
5418 | 0 | return CheckInvalidVLENandLMUL(TI, TheCall, *this, Op1Type, ElemSize * 4) || |
5419 | 0 | CheckInvalidVLENandLMUL(TI, TheCall, *this, Op2Type, ElemSize * 4) || |
5420 | 0 | CheckInvalidVLENandLMUL(TI, TheCall, *this, Op3Type, ElemSize * 4); |
5421 | 0 | } |
5422 | | |
5423 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u8mf8: |
5424 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u8mf4: |
5425 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u8mf2: |
5426 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u8m1: |
5427 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u8m2: |
5428 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u8m4: |
5429 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u8m8: |
5430 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u16mf4: |
5431 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u16mf2: |
5432 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u16m1: |
5433 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u16m2: |
5434 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u16m4: |
5435 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u16m8: |
5436 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u32mf2: |
5437 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u32m1: |
5438 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u32m2: |
5439 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u32m4: |
5440 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u32m8: |
5441 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u64m1: |
5442 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u64m2: |
5443 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u64m4: |
5444 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_i_se_u64m8: |
5445 | | // bit_27_26, bit_24_20, bit_11_7, simm5 |
5446 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 3) || |
5447 | 0 | SemaBuiltinConstantArgRange(TheCall, 1, 0, 31) || |
5448 | 0 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 31) || |
5449 | 0 | SemaBuiltinConstantArgRange(TheCall, 3, -16, 15); |
5450 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_iv_se: |
5451 | | // bit_27_26, bit_11_7, vs2, simm5 |
5452 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 3) || |
5453 | 0 | SemaBuiltinConstantArgRange(TheCall, 1, 0, 31) || |
5454 | 0 | SemaBuiltinConstantArgRange(TheCall, 3, -16, 15); |
5455 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_i: |
5456 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_i_se: |
5457 | | // bit_27_26, bit_24_20, simm5 |
5458 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 3) || |
5459 | 0 | SemaBuiltinConstantArgRange(TheCall, 1, 0, 31) || |
5460 | 0 | SemaBuiltinConstantArgRange(TheCall, 2, -16, 15); |
5461 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_iv: |
5462 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_iv_se: |
5463 | | // bit_27_26, vs2, simm5 |
5464 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 3) || |
5465 | 0 | SemaBuiltinConstantArgRange(TheCall, 2, -16, 15); |
5466 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_ivv_se: |
5467 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_ivw_se: |
5468 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_ivv: |
5469 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_ivw: |
5470 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_ivv_se: |
5471 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_ivw_se: |
5472 | | // bit_27_26, vd, vs2, simm5 |
5473 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 3) || |
5474 | 0 | SemaBuiltinConstantArgRange(TheCall, 3, -16, 15); |
5475 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u8mf8: |
5476 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u8mf4: |
5477 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u8mf2: |
5478 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u8m1: |
5479 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u8m2: |
5480 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u8m4: |
5481 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u8m8: |
5482 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u16mf4: |
5483 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u16mf2: |
5484 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u16m1: |
5485 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u16m2: |
5486 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u16m4: |
5487 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u16m8: |
5488 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u32mf2: |
5489 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u32m1: |
5490 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u32m2: |
5491 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u32m4: |
5492 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u32m8: |
5493 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u64m1: |
5494 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u64m2: |
5495 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u64m4: |
5496 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_x_se_u64m8: |
5497 | | // bit_27_26, bit_24_20, bit_11_7, xs1 |
5498 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 3) || |
5499 | 0 | SemaBuiltinConstantArgRange(TheCall, 1, 0, 31) || |
5500 | 0 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 31); |
5501 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_xv_se: |
5502 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_vv_se: |
5503 | | // bit_27_26, bit_11_7, vs2, xs1/vs1 |
5504 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_x: |
5505 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_x_se: |
5506 | | // bit_27_26, bit_24-20, xs1 |
5507 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 3) || |
5508 | 0 | SemaBuiltinConstantArgRange(TheCall, 1, 0, 31); |
5509 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_vvv_se: |
5510 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_xvv_se: |
5511 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_vvw_se: |
5512 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_xvw_se: |
5513 | | // bit_27_26, vd, vs2, xs1 |
5514 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_xv: |
5515 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_vv: |
5516 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_xv_se: |
5517 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_vv_se: |
5518 | | // bit_27_26, vs2, xs1/vs1 |
5519 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_xvv: |
5520 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_vvv: |
5521 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_xvw: |
5522 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_vvw: |
5523 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_xvv_se: |
5524 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_vvv_se: |
5525 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_xvw_se: |
5526 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_vvw_se: |
5527 | | // bit_27_26, vd, vs2, xs1/vs1 |
5528 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 3); |
5529 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_fv_se: |
5530 | | // bit_26, bit_11_7, vs2, fs1 |
5531 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 1) || |
5532 | 0 | SemaBuiltinConstantArgRange(TheCall, 1, 0, 31); |
5533 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_fvv_se: |
5534 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_fvw_se: |
5535 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_fvv: |
5536 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_fvw: |
5537 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_fvv_se: |
5538 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_fvw_se: |
5539 | | // bit_26, vd, vs2, fs1 |
5540 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_fv: |
5541 | 0 | case RISCVVector::BI__builtin_rvv_sf_vc_v_fv_se: |
5542 | | // bit_26, vs2, fs1 |
5543 | 0 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 1); |
5544 | | // Check if byteselect is in [0, 3] |
5545 | 0 | case RISCV::BI__builtin_riscv_aes32dsi: |
5546 | 0 | case RISCV::BI__builtin_riscv_aes32dsmi: |
5547 | 0 | case RISCV::BI__builtin_riscv_aes32esi: |
5548 | 0 | case RISCV::BI__builtin_riscv_aes32esmi: |
5549 | 0 | case RISCV::BI__builtin_riscv_sm4ks: |
5550 | 0 | case RISCV::BI__builtin_riscv_sm4ed: |
5551 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 3); |
5552 | | // Check if rnum is in [0, 10] |
5553 | 0 | case RISCV::BI__builtin_riscv_aes64ks1i: |
5554 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 10); |
5555 | | // Check if value range for vxrm is in [0, 3] |
5556 | 0 | case RISCVVector::BI__builtin_rvv_vaaddu_vv: |
5557 | 0 | case RISCVVector::BI__builtin_rvv_vaaddu_vx: |
5558 | 0 | case RISCVVector::BI__builtin_rvv_vaadd_vv: |
5559 | 0 | case RISCVVector::BI__builtin_rvv_vaadd_vx: |
5560 | 0 | case RISCVVector::BI__builtin_rvv_vasubu_vv: |
5561 | 0 | case RISCVVector::BI__builtin_rvv_vasubu_vx: |
5562 | 0 | case RISCVVector::BI__builtin_rvv_vasub_vv: |
5563 | 0 | case RISCVVector::BI__builtin_rvv_vasub_vx: |
5564 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vv: |
5565 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vx: |
5566 | 0 | case RISCVVector::BI__builtin_rvv_vssra_vv: |
5567 | 0 | case RISCVVector::BI__builtin_rvv_vssra_vx: |
5568 | 0 | case RISCVVector::BI__builtin_rvv_vssrl_vv: |
5569 | 0 | case RISCVVector::BI__builtin_rvv_vssrl_vx: |
5570 | 0 | case RISCVVector::BI__builtin_rvv_vnclip_wv: |
5571 | 0 | case RISCVVector::BI__builtin_rvv_vnclip_wx: |
5572 | 0 | case RISCVVector::BI__builtin_rvv_vnclipu_wv: |
5573 | 0 | case RISCVVector::BI__builtin_rvv_vnclipu_wx: |
5574 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 3); |
5575 | 0 | case RISCVVector::BI__builtin_rvv_vaaddu_vv_tu: |
5576 | 0 | case RISCVVector::BI__builtin_rvv_vaaddu_vx_tu: |
5577 | 0 | case RISCVVector::BI__builtin_rvv_vaadd_vv_tu: |
5578 | 0 | case RISCVVector::BI__builtin_rvv_vaadd_vx_tu: |
5579 | 0 | case RISCVVector::BI__builtin_rvv_vasubu_vv_tu: |
5580 | 0 | case RISCVVector::BI__builtin_rvv_vasubu_vx_tu: |
5581 | 0 | case RISCVVector::BI__builtin_rvv_vasub_vv_tu: |
5582 | 0 | case RISCVVector::BI__builtin_rvv_vasub_vx_tu: |
5583 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vv_tu: |
5584 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vx_tu: |
5585 | 0 | case RISCVVector::BI__builtin_rvv_vssra_vv_tu: |
5586 | 0 | case RISCVVector::BI__builtin_rvv_vssra_vx_tu: |
5587 | 0 | case RISCVVector::BI__builtin_rvv_vssrl_vv_tu: |
5588 | 0 | case RISCVVector::BI__builtin_rvv_vssrl_vx_tu: |
5589 | 0 | case RISCVVector::BI__builtin_rvv_vnclip_wv_tu: |
5590 | 0 | case RISCVVector::BI__builtin_rvv_vnclip_wx_tu: |
5591 | 0 | case RISCVVector::BI__builtin_rvv_vnclipu_wv_tu: |
5592 | 0 | case RISCVVector::BI__builtin_rvv_vnclipu_wx_tu: |
5593 | 0 | case RISCVVector::BI__builtin_rvv_vaaddu_vv_m: |
5594 | 0 | case RISCVVector::BI__builtin_rvv_vaaddu_vx_m: |
5595 | 0 | case RISCVVector::BI__builtin_rvv_vaadd_vv_m: |
5596 | 0 | case RISCVVector::BI__builtin_rvv_vaadd_vx_m: |
5597 | 0 | case RISCVVector::BI__builtin_rvv_vasubu_vv_m: |
5598 | 0 | case RISCVVector::BI__builtin_rvv_vasubu_vx_m: |
5599 | 0 | case RISCVVector::BI__builtin_rvv_vasub_vv_m: |
5600 | 0 | case RISCVVector::BI__builtin_rvv_vasub_vx_m: |
5601 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vv_m: |
5602 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vx_m: |
5603 | 0 | case RISCVVector::BI__builtin_rvv_vssra_vv_m: |
5604 | 0 | case RISCVVector::BI__builtin_rvv_vssra_vx_m: |
5605 | 0 | case RISCVVector::BI__builtin_rvv_vssrl_vv_m: |
5606 | 0 | case RISCVVector::BI__builtin_rvv_vssrl_vx_m: |
5607 | 0 | case RISCVVector::BI__builtin_rvv_vnclip_wv_m: |
5608 | 0 | case RISCVVector::BI__builtin_rvv_vnclip_wx_m: |
5609 | 0 | case RISCVVector::BI__builtin_rvv_vnclipu_wv_m: |
5610 | 0 | case RISCVVector::BI__builtin_rvv_vnclipu_wx_m: |
5611 | 0 | return SemaBuiltinConstantArgRange(TheCall, 3, 0, 3); |
5612 | 0 | case RISCVVector::BI__builtin_rvv_vaaddu_vv_tum: |
5613 | 0 | case RISCVVector::BI__builtin_rvv_vaaddu_vv_tumu: |
5614 | 0 | case RISCVVector::BI__builtin_rvv_vaaddu_vv_mu: |
5615 | 0 | case RISCVVector::BI__builtin_rvv_vaaddu_vx_tum: |
5616 | 0 | case RISCVVector::BI__builtin_rvv_vaaddu_vx_tumu: |
5617 | 0 | case RISCVVector::BI__builtin_rvv_vaaddu_vx_mu: |
5618 | 0 | case RISCVVector::BI__builtin_rvv_vaadd_vv_tum: |
5619 | 0 | case RISCVVector::BI__builtin_rvv_vaadd_vv_tumu: |
5620 | 0 | case RISCVVector::BI__builtin_rvv_vaadd_vv_mu: |
5621 | 0 | case RISCVVector::BI__builtin_rvv_vaadd_vx_tum: |
5622 | 0 | case RISCVVector::BI__builtin_rvv_vaadd_vx_tumu: |
5623 | 0 | case RISCVVector::BI__builtin_rvv_vaadd_vx_mu: |
5624 | 0 | case RISCVVector::BI__builtin_rvv_vasubu_vv_tum: |
5625 | 0 | case RISCVVector::BI__builtin_rvv_vasubu_vv_tumu: |
5626 | 0 | case RISCVVector::BI__builtin_rvv_vasubu_vv_mu: |
5627 | 0 | case RISCVVector::BI__builtin_rvv_vasubu_vx_tum: |
5628 | 0 | case RISCVVector::BI__builtin_rvv_vasubu_vx_tumu: |
5629 | 0 | case RISCVVector::BI__builtin_rvv_vasubu_vx_mu: |
5630 | 0 | case RISCVVector::BI__builtin_rvv_vasub_vv_tum: |
5631 | 0 | case RISCVVector::BI__builtin_rvv_vasub_vv_tumu: |
5632 | 0 | case RISCVVector::BI__builtin_rvv_vasub_vv_mu: |
5633 | 0 | case RISCVVector::BI__builtin_rvv_vasub_vx_tum: |
5634 | 0 | case RISCVVector::BI__builtin_rvv_vasub_vx_tumu: |
5635 | 0 | case RISCVVector::BI__builtin_rvv_vasub_vx_mu: |
5636 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vv_mu: |
5637 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vx_mu: |
5638 | 0 | case RISCVVector::BI__builtin_rvv_vssra_vv_mu: |
5639 | 0 | case RISCVVector::BI__builtin_rvv_vssra_vx_mu: |
5640 | 0 | case RISCVVector::BI__builtin_rvv_vssrl_vv_mu: |
5641 | 0 | case RISCVVector::BI__builtin_rvv_vssrl_vx_mu: |
5642 | 0 | case RISCVVector::BI__builtin_rvv_vnclip_wv_mu: |
5643 | 0 | case RISCVVector::BI__builtin_rvv_vnclip_wx_mu: |
5644 | 0 | case RISCVVector::BI__builtin_rvv_vnclipu_wv_mu: |
5645 | 0 | case RISCVVector::BI__builtin_rvv_vnclipu_wx_mu: |
5646 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vv_tum: |
5647 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vx_tum: |
5648 | 0 | case RISCVVector::BI__builtin_rvv_vssra_vv_tum: |
5649 | 0 | case RISCVVector::BI__builtin_rvv_vssra_vx_tum: |
5650 | 0 | case RISCVVector::BI__builtin_rvv_vssrl_vv_tum: |
5651 | 0 | case RISCVVector::BI__builtin_rvv_vssrl_vx_tum: |
5652 | 0 | case RISCVVector::BI__builtin_rvv_vnclip_wv_tum: |
5653 | 0 | case RISCVVector::BI__builtin_rvv_vnclip_wx_tum: |
5654 | 0 | case RISCVVector::BI__builtin_rvv_vnclipu_wv_tum: |
5655 | 0 | case RISCVVector::BI__builtin_rvv_vnclipu_wx_tum: |
5656 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vv_tumu: |
5657 | 0 | case RISCVVector::BI__builtin_rvv_vsmul_vx_tumu: |
5658 | 0 | case RISCVVector::BI__builtin_rvv_vssra_vv_tumu: |
5659 | 0 | case RISCVVector::BI__builtin_rvv_vssra_vx_tumu: |
5660 | 0 | case RISCVVector::BI__builtin_rvv_vssrl_vv_tumu: |
5661 | 0 | case RISCVVector::BI__builtin_rvv_vssrl_vx_tumu: |
5662 | 0 | case RISCVVector::BI__builtin_rvv_vnclip_wv_tumu: |
5663 | 0 | case RISCVVector::BI__builtin_rvv_vnclip_wx_tumu: |
5664 | 0 | case RISCVVector::BI__builtin_rvv_vnclipu_wv_tumu: |
5665 | 0 | case RISCVVector::BI__builtin_rvv_vnclipu_wx_tumu: |
5666 | 0 | return SemaBuiltinConstantArgRange(TheCall, 4, 0, 3); |
5667 | 0 | case RISCVVector::BI__builtin_rvv_vfsqrt_v_rm: |
5668 | 0 | case RISCVVector::BI__builtin_rvv_vfrec7_v_rm: |
5669 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_x_f_v_rm: |
5670 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_xu_f_v_rm: |
5671 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_f_x_v_rm: |
5672 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_f_xu_v_rm: |
5673 | 0 | case RISCVVector::BI__builtin_rvv_vfwcvt_x_f_v_rm: |
5674 | 0 | case RISCVVector::BI__builtin_rvv_vfwcvt_xu_f_v_rm: |
5675 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_x_f_w_rm: |
5676 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_xu_f_w_rm: |
5677 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_f_x_w_rm: |
5678 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_f_xu_w_rm: |
5679 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_f_f_w_rm: |
5680 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 4); |
5681 | 0 | case RISCVVector::BI__builtin_rvv_vfadd_vv_rm: |
5682 | 0 | case RISCVVector::BI__builtin_rvv_vfadd_vf_rm: |
5683 | 0 | case RISCVVector::BI__builtin_rvv_vfsub_vv_rm: |
5684 | 0 | case RISCVVector::BI__builtin_rvv_vfsub_vf_rm: |
5685 | 0 | case RISCVVector::BI__builtin_rvv_vfrsub_vf_rm: |
5686 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_vv_rm: |
5687 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_vf_rm: |
5688 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_vv_rm: |
5689 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_vf_rm: |
5690 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_wv_rm: |
5691 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_wf_rm: |
5692 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_wv_rm: |
5693 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_wf_rm: |
5694 | 0 | case RISCVVector::BI__builtin_rvv_vfmul_vv_rm: |
5695 | 0 | case RISCVVector::BI__builtin_rvv_vfmul_vf_rm: |
5696 | 0 | case RISCVVector::BI__builtin_rvv_vfdiv_vv_rm: |
5697 | 0 | case RISCVVector::BI__builtin_rvv_vfdiv_vf_rm: |
5698 | 0 | case RISCVVector::BI__builtin_rvv_vfrdiv_vf_rm: |
5699 | 0 | case RISCVVector::BI__builtin_rvv_vfwmul_vv_rm: |
5700 | 0 | case RISCVVector::BI__builtin_rvv_vfwmul_vf_rm: |
5701 | 0 | case RISCVVector::BI__builtin_rvv_vfredosum_vs_rm: |
5702 | 0 | case RISCVVector::BI__builtin_rvv_vfredusum_vs_rm: |
5703 | 0 | case RISCVVector::BI__builtin_rvv_vfwredosum_vs_rm: |
5704 | 0 | case RISCVVector::BI__builtin_rvv_vfwredusum_vs_rm: |
5705 | 0 | case RISCVVector::BI__builtin_rvv_vfsqrt_v_rm_tu: |
5706 | 0 | case RISCVVector::BI__builtin_rvv_vfrec7_v_rm_tu: |
5707 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_x_f_v_rm_tu: |
5708 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_xu_f_v_rm_tu: |
5709 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_f_x_v_rm_tu: |
5710 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_f_xu_v_rm_tu: |
5711 | 0 | case RISCVVector::BI__builtin_rvv_vfwcvt_x_f_v_rm_tu: |
5712 | 0 | case RISCVVector::BI__builtin_rvv_vfwcvt_xu_f_v_rm_tu: |
5713 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_x_f_w_rm_tu: |
5714 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_xu_f_w_rm_tu: |
5715 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_f_x_w_rm_tu: |
5716 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_f_xu_w_rm_tu: |
5717 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_f_f_w_rm_tu: |
5718 | 0 | case RISCVVector::BI__builtin_rvv_vfsqrt_v_rm_m: |
5719 | 0 | case RISCVVector::BI__builtin_rvv_vfrec7_v_rm_m: |
5720 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_x_f_v_rm_m: |
5721 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_xu_f_v_rm_m: |
5722 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_f_x_v_rm_m: |
5723 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_f_xu_v_rm_m: |
5724 | 0 | case RISCVVector::BI__builtin_rvv_vfwcvt_x_f_v_rm_m: |
5725 | 0 | case RISCVVector::BI__builtin_rvv_vfwcvt_xu_f_v_rm_m: |
5726 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_x_f_w_rm_m: |
5727 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_xu_f_w_rm_m: |
5728 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_f_x_w_rm_m: |
5729 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_f_xu_w_rm_m: |
5730 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_f_f_w_rm_m: |
5731 | 0 | return SemaBuiltinConstantArgRange(TheCall, 2, 0, 4); |
5732 | 0 | case RISCVVector::BI__builtin_rvv_vfadd_vv_rm_tu: |
5733 | 0 | case RISCVVector::BI__builtin_rvv_vfadd_vf_rm_tu: |
5734 | 0 | case RISCVVector::BI__builtin_rvv_vfsub_vv_rm_tu: |
5735 | 0 | case RISCVVector::BI__builtin_rvv_vfsub_vf_rm_tu: |
5736 | 0 | case RISCVVector::BI__builtin_rvv_vfrsub_vf_rm_tu: |
5737 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_vv_rm_tu: |
5738 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_vf_rm_tu: |
5739 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_vv_rm_tu: |
5740 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_vf_rm_tu: |
5741 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_wv_rm_tu: |
5742 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_wf_rm_tu: |
5743 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_wv_rm_tu: |
5744 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_wf_rm_tu: |
5745 | 0 | case RISCVVector::BI__builtin_rvv_vfmul_vv_rm_tu: |
5746 | 0 | case RISCVVector::BI__builtin_rvv_vfmul_vf_rm_tu: |
5747 | 0 | case RISCVVector::BI__builtin_rvv_vfdiv_vv_rm_tu: |
5748 | 0 | case RISCVVector::BI__builtin_rvv_vfdiv_vf_rm_tu: |
5749 | 0 | case RISCVVector::BI__builtin_rvv_vfrdiv_vf_rm_tu: |
5750 | 0 | case RISCVVector::BI__builtin_rvv_vfwmul_vv_rm_tu: |
5751 | 0 | case RISCVVector::BI__builtin_rvv_vfwmul_vf_rm_tu: |
5752 | 0 | case RISCVVector::BI__builtin_rvv_vfredosum_vs_rm_tu: |
5753 | 0 | case RISCVVector::BI__builtin_rvv_vfredusum_vs_rm_tu: |
5754 | 0 | case RISCVVector::BI__builtin_rvv_vfwredosum_vs_rm_tu: |
5755 | 0 | case RISCVVector::BI__builtin_rvv_vfwredusum_vs_rm_tu: |
5756 | 0 | case RISCVVector::BI__builtin_rvv_vfmacc_vv_rm: |
5757 | 0 | case RISCVVector::BI__builtin_rvv_vfmacc_vf_rm: |
5758 | 0 | case RISCVVector::BI__builtin_rvv_vfnmacc_vv_rm: |
5759 | 0 | case RISCVVector::BI__builtin_rvv_vfnmacc_vf_rm: |
5760 | 0 | case RISCVVector::BI__builtin_rvv_vfmsac_vv_rm: |
5761 | 0 | case RISCVVector::BI__builtin_rvv_vfmsac_vf_rm: |
5762 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsac_vv_rm: |
5763 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsac_vf_rm: |
5764 | 0 | case RISCVVector::BI__builtin_rvv_vfmadd_vv_rm: |
5765 | 0 | case RISCVVector::BI__builtin_rvv_vfmadd_vf_rm: |
5766 | 0 | case RISCVVector::BI__builtin_rvv_vfnmadd_vv_rm: |
5767 | 0 | case RISCVVector::BI__builtin_rvv_vfnmadd_vf_rm: |
5768 | 0 | case RISCVVector::BI__builtin_rvv_vfmsub_vv_rm: |
5769 | 0 | case RISCVVector::BI__builtin_rvv_vfmsub_vf_rm: |
5770 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsub_vv_rm: |
5771 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsub_vf_rm: |
5772 | 0 | case RISCVVector::BI__builtin_rvv_vfwmacc_vv_rm: |
5773 | 0 | case RISCVVector::BI__builtin_rvv_vfwmacc_vf_rm: |
5774 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmacc_vv_rm: |
5775 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmacc_vf_rm: |
5776 | 0 | case RISCVVector::BI__builtin_rvv_vfwmsac_vv_rm: |
5777 | 0 | case RISCVVector::BI__builtin_rvv_vfwmsac_vf_rm: |
5778 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmsac_vv_rm: |
5779 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmsac_vf_rm: |
5780 | 0 | case RISCVVector::BI__builtin_rvv_vfmacc_vv_rm_tu: |
5781 | 0 | case RISCVVector::BI__builtin_rvv_vfmacc_vf_rm_tu: |
5782 | 0 | case RISCVVector::BI__builtin_rvv_vfnmacc_vv_rm_tu: |
5783 | 0 | case RISCVVector::BI__builtin_rvv_vfnmacc_vf_rm_tu: |
5784 | 0 | case RISCVVector::BI__builtin_rvv_vfmsac_vv_rm_tu: |
5785 | 0 | case RISCVVector::BI__builtin_rvv_vfmsac_vf_rm_tu: |
5786 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsac_vv_rm_tu: |
5787 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsac_vf_rm_tu: |
5788 | 0 | case RISCVVector::BI__builtin_rvv_vfmadd_vv_rm_tu: |
5789 | 0 | case RISCVVector::BI__builtin_rvv_vfmadd_vf_rm_tu: |
5790 | 0 | case RISCVVector::BI__builtin_rvv_vfnmadd_vv_rm_tu: |
5791 | 0 | case RISCVVector::BI__builtin_rvv_vfnmadd_vf_rm_tu: |
5792 | 0 | case RISCVVector::BI__builtin_rvv_vfmsub_vv_rm_tu: |
5793 | 0 | case RISCVVector::BI__builtin_rvv_vfmsub_vf_rm_tu: |
5794 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsub_vv_rm_tu: |
5795 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsub_vf_rm_tu: |
5796 | 0 | case RISCVVector::BI__builtin_rvv_vfwmacc_vv_rm_tu: |
5797 | 0 | case RISCVVector::BI__builtin_rvv_vfwmacc_vf_rm_tu: |
5798 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmacc_vv_rm_tu: |
5799 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmacc_vf_rm_tu: |
5800 | 0 | case RISCVVector::BI__builtin_rvv_vfwmsac_vv_rm_tu: |
5801 | 0 | case RISCVVector::BI__builtin_rvv_vfwmsac_vf_rm_tu: |
5802 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmsac_vv_rm_tu: |
5803 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmsac_vf_rm_tu: |
5804 | 0 | case RISCVVector::BI__builtin_rvv_vfadd_vv_rm_m: |
5805 | 0 | case RISCVVector::BI__builtin_rvv_vfadd_vf_rm_m: |
5806 | 0 | case RISCVVector::BI__builtin_rvv_vfsub_vv_rm_m: |
5807 | 0 | case RISCVVector::BI__builtin_rvv_vfsub_vf_rm_m: |
5808 | 0 | case RISCVVector::BI__builtin_rvv_vfrsub_vf_rm_m: |
5809 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_vv_rm_m: |
5810 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_vf_rm_m: |
5811 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_vv_rm_m: |
5812 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_vf_rm_m: |
5813 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_wv_rm_m: |
5814 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_wf_rm_m: |
5815 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_wv_rm_m: |
5816 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_wf_rm_m: |
5817 | 0 | case RISCVVector::BI__builtin_rvv_vfmul_vv_rm_m: |
5818 | 0 | case RISCVVector::BI__builtin_rvv_vfmul_vf_rm_m: |
5819 | 0 | case RISCVVector::BI__builtin_rvv_vfdiv_vv_rm_m: |
5820 | 0 | case RISCVVector::BI__builtin_rvv_vfdiv_vf_rm_m: |
5821 | 0 | case RISCVVector::BI__builtin_rvv_vfrdiv_vf_rm_m: |
5822 | 0 | case RISCVVector::BI__builtin_rvv_vfwmul_vv_rm_m: |
5823 | 0 | case RISCVVector::BI__builtin_rvv_vfwmul_vf_rm_m: |
5824 | 0 | case RISCVVector::BI__builtin_rvv_vfredosum_vs_rm_m: |
5825 | 0 | case RISCVVector::BI__builtin_rvv_vfredusum_vs_rm_m: |
5826 | 0 | case RISCVVector::BI__builtin_rvv_vfwredosum_vs_rm_m: |
5827 | 0 | case RISCVVector::BI__builtin_rvv_vfwredusum_vs_rm_m: |
5828 | 0 | case RISCVVector::BI__builtin_rvv_vfsqrt_v_rm_tum: |
5829 | 0 | case RISCVVector::BI__builtin_rvv_vfrec7_v_rm_tum: |
5830 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_x_f_v_rm_tum: |
5831 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_xu_f_v_rm_tum: |
5832 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_f_x_v_rm_tum: |
5833 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_f_xu_v_rm_tum: |
5834 | 0 | case RISCVVector::BI__builtin_rvv_vfwcvt_x_f_v_rm_tum: |
5835 | 0 | case RISCVVector::BI__builtin_rvv_vfwcvt_xu_f_v_rm_tum: |
5836 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_x_f_w_rm_tum: |
5837 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_xu_f_w_rm_tum: |
5838 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_f_x_w_rm_tum: |
5839 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_f_xu_w_rm_tum: |
5840 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_f_f_w_rm_tum: |
5841 | 0 | case RISCVVector::BI__builtin_rvv_vfsqrt_v_rm_tumu: |
5842 | 0 | case RISCVVector::BI__builtin_rvv_vfrec7_v_rm_tumu: |
5843 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_x_f_v_rm_tumu: |
5844 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_xu_f_v_rm_tumu: |
5845 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_f_x_v_rm_tumu: |
5846 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_f_xu_v_rm_tumu: |
5847 | 0 | case RISCVVector::BI__builtin_rvv_vfwcvt_x_f_v_rm_tumu: |
5848 | 0 | case RISCVVector::BI__builtin_rvv_vfwcvt_xu_f_v_rm_tumu: |
5849 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_x_f_w_rm_tumu: |
5850 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_xu_f_w_rm_tumu: |
5851 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_f_x_w_rm_tumu: |
5852 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_f_xu_w_rm_tumu: |
5853 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_f_f_w_rm_tumu: |
5854 | 0 | case RISCVVector::BI__builtin_rvv_vfsqrt_v_rm_mu: |
5855 | 0 | case RISCVVector::BI__builtin_rvv_vfrec7_v_rm_mu: |
5856 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_x_f_v_rm_mu: |
5857 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_xu_f_v_rm_mu: |
5858 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_f_x_v_rm_mu: |
5859 | 0 | case RISCVVector::BI__builtin_rvv_vfcvt_f_xu_v_rm_mu: |
5860 | 0 | case RISCVVector::BI__builtin_rvv_vfwcvt_x_f_v_rm_mu: |
5861 | 0 | case RISCVVector::BI__builtin_rvv_vfwcvt_xu_f_v_rm_mu: |
5862 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_x_f_w_rm_mu: |
5863 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_xu_f_w_rm_mu: |
5864 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_f_x_w_rm_mu: |
5865 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_f_xu_w_rm_mu: |
5866 | 0 | case RISCVVector::BI__builtin_rvv_vfncvt_f_f_w_rm_mu: |
5867 | 0 | return SemaBuiltinConstantArgRange(TheCall, 3, 0, 4); |
5868 | 0 | case RISCVVector::BI__builtin_rvv_vfmacc_vv_rm_m: |
5869 | 0 | case RISCVVector::BI__builtin_rvv_vfmacc_vf_rm_m: |
5870 | 0 | case RISCVVector::BI__builtin_rvv_vfnmacc_vv_rm_m: |
5871 | 0 | case RISCVVector::BI__builtin_rvv_vfnmacc_vf_rm_m: |
5872 | 0 | case RISCVVector::BI__builtin_rvv_vfmsac_vv_rm_m: |
5873 | 0 | case RISCVVector::BI__builtin_rvv_vfmsac_vf_rm_m: |
5874 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsac_vv_rm_m: |
5875 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsac_vf_rm_m: |
5876 | 0 | case RISCVVector::BI__builtin_rvv_vfmadd_vv_rm_m: |
5877 | 0 | case RISCVVector::BI__builtin_rvv_vfmadd_vf_rm_m: |
5878 | 0 | case RISCVVector::BI__builtin_rvv_vfnmadd_vv_rm_m: |
5879 | 0 | case RISCVVector::BI__builtin_rvv_vfnmadd_vf_rm_m: |
5880 | 0 | case RISCVVector::BI__builtin_rvv_vfmsub_vv_rm_m: |
5881 | 0 | case RISCVVector::BI__builtin_rvv_vfmsub_vf_rm_m: |
5882 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsub_vv_rm_m: |
5883 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsub_vf_rm_m: |
5884 | 0 | case RISCVVector::BI__builtin_rvv_vfwmacc_vv_rm_m: |
5885 | 0 | case RISCVVector::BI__builtin_rvv_vfwmacc_vf_rm_m: |
5886 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmacc_vv_rm_m: |
5887 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmacc_vf_rm_m: |
5888 | 0 | case RISCVVector::BI__builtin_rvv_vfwmsac_vv_rm_m: |
5889 | 0 | case RISCVVector::BI__builtin_rvv_vfwmsac_vf_rm_m: |
5890 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmsac_vv_rm_m: |
5891 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmsac_vf_rm_m: |
5892 | 0 | case RISCVVector::BI__builtin_rvv_vfadd_vv_rm_tum: |
5893 | 0 | case RISCVVector::BI__builtin_rvv_vfadd_vf_rm_tum: |
5894 | 0 | case RISCVVector::BI__builtin_rvv_vfsub_vv_rm_tum: |
5895 | 0 | case RISCVVector::BI__builtin_rvv_vfsub_vf_rm_tum: |
5896 | 0 | case RISCVVector::BI__builtin_rvv_vfrsub_vf_rm_tum: |
5897 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_vv_rm_tum: |
5898 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_vf_rm_tum: |
5899 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_vv_rm_tum: |
5900 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_vf_rm_tum: |
5901 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_wv_rm_tum: |
5902 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_wf_rm_tum: |
5903 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_wv_rm_tum: |
5904 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_wf_rm_tum: |
5905 | 0 | case RISCVVector::BI__builtin_rvv_vfmul_vv_rm_tum: |
5906 | 0 | case RISCVVector::BI__builtin_rvv_vfmul_vf_rm_tum: |
5907 | 0 | case RISCVVector::BI__builtin_rvv_vfdiv_vv_rm_tum: |
5908 | 0 | case RISCVVector::BI__builtin_rvv_vfdiv_vf_rm_tum: |
5909 | 0 | case RISCVVector::BI__builtin_rvv_vfrdiv_vf_rm_tum: |
5910 | 0 | case RISCVVector::BI__builtin_rvv_vfwmul_vv_rm_tum: |
5911 | 0 | case RISCVVector::BI__builtin_rvv_vfwmul_vf_rm_tum: |
5912 | 0 | case RISCVVector::BI__builtin_rvv_vfmacc_vv_rm_tum: |
5913 | 0 | case RISCVVector::BI__builtin_rvv_vfmacc_vf_rm_tum: |
5914 | 0 | case RISCVVector::BI__builtin_rvv_vfnmacc_vv_rm_tum: |
5915 | 0 | case RISCVVector::BI__builtin_rvv_vfnmacc_vf_rm_tum: |
5916 | 0 | case RISCVVector::BI__builtin_rvv_vfmsac_vv_rm_tum: |
5917 | 0 | case RISCVVector::BI__builtin_rvv_vfmsac_vf_rm_tum: |
5918 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsac_vv_rm_tum: |
5919 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsac_vf_rm_tum: |
5920 | 0 | case RISCVVector::BI__builtin_rvv_vfmadd_vv_rm_tum: |
5921 | 0 | case RISCVVector::BI__builtin_rvv_vfmadd_vf_rm_tum: |
5922 | 0 | case RISCVVector::BI__builtin_rvv_vfnmadd_vv_rm_tum: |
5923 | 0 | case RISCVVector::BI__builtin_rvv_vfnmadd_vf_rm_tum: |
5924 | 0 | case RISCVVector::BI__builtin_rvv_vfmsub_vv_rm_tum: |
5925 | 0 | case RISCVVector::BI__builtin_rvv_vfmsub_vf_rm_tum: |
5926 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsub_vv_rm_tum: |
5927 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsub_vf_rm_tum: |
5928 | 0 | case RISCVVector::BI__builtin_rvv_vfwmacc_vv_rm_tum: |
5929 | 0 | case RISCVVector::BI__builtin_rvv_vfwmacc_vf_rm_tum: |
5930 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmacc_vv_rm_tum: |
5931 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmacc_vf_rm_tum: |
5932 | 0 | case RISCVVector::BI__builtin_rvv_vfwmsac_vv_rm_tum: |
5933 | 0 | case RISCVVector::BI__builtin_rvv_vfwmsac_vf_rm_tum: |
5934 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmsac_vv_rm_tum: |
5935 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmsac_vf_rm_tum: |
5936 | 0 | case RISCVVector::BI__builtin_rvv_vfredosum_vs_rm_tum: |
5937 | 0 | case RISCVVector::BI__builtin_rvv_vfredusum_vs_rm_tum: |
5938 | 0 | case RISCVVector::BI__builtin_rvv_vfwredosum_vs_rm_tum: |
5939 | 0 | case RISCVVector::BI__builtin_rvv_vfwredusum_vs_rm_tum: |
5940 | 0 | case RISCVVector::BI__builtin_rvv_vfadd_vv_rm_tumu: |
5941 | 0 | case RISCVVector::BI__builtin_rvv_vfadd_vf_rm_tumu: |
5942 | 0 | case RISCVVector::BI__builtin_rvv_vfsub_vv_rm_tumu: |
5943 | 0 | case RISCVVector::BI__builtin_rvv_vfsub_vf_rm_tumu: |
5944 | 0 | case RISCVVector::BI__builtin_rvv_vfrsub_vf_rm_tumu: |
5945 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_vv_rm_tumu: |
5946 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_vf_rm_tumu: |
5947 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_vv_rm_tumu: |
5948 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_vf_rm_tumu: |
5949 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_wv_rm_tumu: |
5950 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_wf_rm_tumu: |
5951 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_wv_rm_tumu: |
5952 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_wf_rm_tumu: |
5953 | 0 | case RISCVVector::BI__builtin_rvv_vfmul_vv_rm_tumu: |
5954 | 0 | case RISCVVector::BI__builtin_rvv_vfmul_vf_rm_tumu: |
5955 | 0 | case RISCVVector::BI__builtin_rvv_vfdiv_vv_rm_tumu: |
5956 | 0 | case RISCVVector::BI__builtin_rvv_vfdiv_vf_rm_tumu: |
5957 | 0 | case RISCVVector::BI__builtin_rvv_vfrdiv_vf_rm_tumu: |
5958 | 0 | case RISCVVector::BI__builtin_rvv_vfwmul_vv_rm_tumu: |
5959 | 0 | case RISCVVector::BI__builtin_rvv_vfwmul_vf_rm_tumu: |
5960 | 0 | case RISCVVector::BI__builtin_rvv_vfmacc_vv_rm_tumu: |
5961 | 0 | case RISCVVector::BI__builtin_rvv_vfmacc_vf_rm_tumu: |
5962 | 0 | case RISCVVector::BI__builtin_rvv_vfnmacc_vv_rm_tumu: |
5963 | 0 | case RISCVVector::BI__builtin_rvv_vfnmacc_vf_rm_tumu: |
5964 | 0 | case RISCVVector::BI__builtin_rvv_vfmsac_vv_rm_tumu: |
5965 | 0 | case RISCVVector::BI__builtin_rvv_vfmsac_vf_rm_tumu: |
5966 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsac_vv_rm_tumu: |
5967 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsac_vf_rm_tumu: |
5968 | 0 | case RISCVVector::BI__builtin_rvv_vfmadd_vv_rm_tumu: |
5969 | 0 | case RISCVVector::BI__builtin_rvv_vfmadd_vf_rm_tumu: |
5970 | 0 | case RISCVVector::BI__builtin_rvv_vfnmadd_vv_rm_tumu: |
5971 | 0 | case RISCVVector::BI__builtin_rvv_vfnmadd_vf_rm_tumu: |
5972 | 0 | case RISCVVector::BI__builtin_rvv_vfmsub_vv_rm_tumu: |
5973 | 0 | case RISCVVector::BI__builtin_rvv_vfmsub_vf_rm_tumu: |
5974 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsub_vv_rm_tumu: |
5975 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsub_vf_rm_tumu: |
5976 | 0 | case RISCVVector::BI__builtin_rvv_vfwmacc_vv_rm_tumu: |
5977 | 0 | case RISCVVector::BI__builtin_rvv_vfwmacc_vf_rm_tumu: |
5978 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmacc_vv_rm_tumu: |
5979 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmacc_vf_rm_tumu: |
5980 | 0 | case RISCVVector::BI__builtin_rvv_vfwmsac_vv_rm_tumu: |
5981 | 0 | case RISCVVector::BI__builtin_rvv_vfwmsac_vf_rm_tumu: |
5982 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmsac_vv_rm_tumu: |
5983 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmsac_vf_rm_tumu: |
5984 | 0 | case RISCVVector::BI__builtin_rvv_vfadd_vv_rm_mu: |
5985 | 0 | case RISCVVector::BI__builtin_rvv_vfadd_vf_rm_mu: |
5986 | 0 | case RISCVVector::BI__builtin_rvv_vfsub_vv_rm_mu: |
5987 | 0 | case RISCVVector::BI__builtin_rvv_vfsub_vf_rm_mu: |
5988 | 0 | case RISCVVector::BI__builtin_rvv_vfrsub_vf_rm_mu: |
5989 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_vv_rm_mu: |
5990 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_vf_rm_mu: |
5991 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_vv_rm_mu: |
5992 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_vf_rm_mu: |
5993 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_wv_rm_mu: |
5994 | 0 | case RISCVVector::BI__builtin_rvv_vfwadd_wf_rm_mu: |
5995 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_wv_rm_mu: |
5996 | 0 | case RISCVVector::BI__builtin_rvv_vfwsub_wf_rm_mu: |
5997 | 0 | case RISCVVector::BI__builtin_rvv_vfmul_vv_rm_mu: |
5998 | 0 | case RISCVVector::BI__builtin_rvv_vfmul_vf_rm_mu: |
5999 | 0 | case RISCVVector::BI__builtin_rvv_vfdiv_vv_rm_mu: |
6000 | 0 | case RISCVVector::BI__builtin_rvv_vfdiv_vf_rm_mu: |
6001 | 0 | case RISCVVector::BI__builtin_rvv_vfrdiv_vf_rm_mu: |
6002 | 0 | case RISCVVector::BI__builtin_rvv_vfwmul_vv_rm_mu: |
6003 | 0 | case RISCVVector::BI__builtin_rvv_vfwmul_vf_rm_mu: |
6004 | 0 | case RISCVVector::BI__builtin_rvv_vfmacc_vv_rm_mu: |
6005 | 0 | case RISCVVector::BI__builtin_rvv_vfmacc_vf_rm_mu: |
6006 | 0 | case RISCVVector::BI__builtin_rvv_vfnmacc_vv_rm_mu: |
6007 | 0 | case RISCVVector::BI__builtin_rvv_vfnmacc_vf_rm_mu: |
6008 | 0 | case RISCVVector::BI__builtin_rvv_vfmsac_vv_rm_mu: |
6009 | 0 | case RISCVVector::BI__builtin_rvv_vfmsac_vf_rm_mu: |
6010 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsac_vv_rm_mu: |
6011 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsac_vf_rm_mu: |
6012 | 0 | case RISCVVector::BI__builtin_rvv_vfmadd_vv_rm_mu: |
6013 | 0 | case RISCVVector::BI__builtin_rvv_vfmadd_vf_rm_mu: |
6014 | 0 | case RISCVVector::BI__builtin_rvv_vfnmadd_vv_rm_mu: |
6015 | 0 | case RISCVVector::BI__builtin_rvv_vfnmadd_vf_rm_mu: |
6016 | 0 | case RISCVVector::BI__builtin_rvv_vfmsub_vv_rm_mu: |
6017 | 0 | case RISCVVector::BI__builtin_rvv_vfmsub_vf_rm_mu: |
6018 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsub_vv_rm_mu: |
6019 | 0 | case RISCVVector::BI__builtin_rvv_vfnmsub_vf_rm_mu: |
6020 | 0 | case RISCVVector::BI__builtin_rvv_vfwmacc_vv_rm_mu: |
6021 | 0 | case RISCVVector::BI__builtin_rvv_vfwmacc_vf_rm_mu: |
6022 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmacc_vv_rm_mu: |
6023 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmacc_vf_rm_mu: |
6024 | 0 | case RISCVVector::BI__builtin_rvv_vfwmsac_vv_rm_mu: |
6025 | 0 | case RISCVVector::BI__builtin_rvv_vfwmsac_vf_rm_mu: |
6026 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmsac_vv_rm_mu: |
6027 | 0 | case RISCVVector::BI__builtin_rvv_vfwnmsac_vf_rm_mu: |
6028 | 0 | return SemaBuiltinConstantArgRange(TheCall, 4, 0, 4); |
6029 | 0 | case RISCV::BI__builtin_riscv_ntl_load: |
6030 | 0 | case RISCV::BI__builtin_riscv_ntl_store: |
6031 | 0 | DeclRefExpr *DRE = |
6032 | 0 | cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
6033 | 0 | assert((BuiltinID == RISCV::BI__builtin_riscv_ntl_store || |
6034 | 0 | BuiltinID == RISCV::BI__builtin_riscv_ntl_load) && |
6035 | 0 | "Unexpected RISC-V nontemporal load/store builtin!"); |
6036 | 0 | bool IsStore = BuiltinID == RISCV::BI__builtin_riscv_ntl_store; |
6037 | 0 | unsigned NumArgs = IsStore ? 3 : 2; |
6038 | |
|
6039 | 0 | if (checkArgCountAtLeast(*this, TheCall, NumArgs - 1)) |
6040 | 0 | return true; |
6041 | | |
6042 | 0 | if (checkArgCountAtMost(*this, TheCall, NumArgs)) |
6043 | 0 | return true; |
6044 | | |
6045 | | // Domain value should be compile-time constant. |
6046 | | // 2 <= domain <= 5 |
6047 | 0 | if (TheCall->getNumArgs() == NumArgs && |
6048 | 0 | SemaBuiltinConstantArgRange(TheCall, NumArgs - 1, 2, 5)) |
6049 | 0 | return true; |
6050 | | |
6051 | 0 | Expr *PointerArg = TheCall->getArg(0); |
6052 | 0 | ExprResult PointerArgResult = |
6053 | 0 | DefaultFunctionArrayLvalueConversion(PointerArg); |
6054 | |
|
6055 | 0 | if (PointerArgResult.isInvalid()) |
6056 | 0 | return true; |
6057 | 0 | PointerArg = PointerArgResult.get(); |
6058 | |
|
6059 | 0 | const PointerType *PtrType = PointerArg->getType()->getAs<PointerType>(); |
6060 | 0 | if (!PtrType) { |
6061 | 0 | Diag(DRE->getBeginLoc(), diag::err_nontemporal_builtin_must_be_pointer) |
6062 | 0 | << PointerArg->getType() << PointerArg->getSourceRange(); |
6063 | 0 | return true; |
6064 | 0 | } |
6065 | | |
6066 | 0 | QualType ValType = PtrType->getPointeeType(); |
6067 | 0 | ValType = ValType.getUnqualifiedType(); |
6068 | 0 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && |
6069 | 0 | !ValType->isBlockPointerType() && !ValType->isFloatingType() && |
6070 | 0 | !ValType->isVectorType() && !ValType->isRVVSizelessBuiltinType()) { |
6071 | 0 | Diag(DRE->getBeginLoc(), |
6072 | 0 | diag::err_nontemporal_builtin_must_be_pointer_intfltptr_or_vector) |
6073 | 0 | << PointerArg->getType() << PointerArg->getSourceRange(); |
6074 | 0 | return true; |
6075 | 0 | } |
6076 | | |
6077 | 0 | if (!IsStore) { |
6078 | 0 | TheCall->setType(ValType); |
6079 | 0 | return false; |
6080 | 0 | } |
6081 | | |
6082 | 0 | ExprResult ValArg = TheCall->getArg(1); |
6083 | 0 | InitializedEntity Entity = InitializedEntity::InitializeParameter( |
6084 | 0 | Context, ValType, /*consume*/ false); |
6085 | 0 | ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg); |
6086 | 0 | if (ValArg.isInvalid()) |
6087 | 0 | return true; |
6088 | | |
6089 | 0 | TheCall->setArg(1, ValArg.get()); |
6090 | 0 | TheCall->setType(Context.VoidTy); |
6091 | 0 | return false; |
6092 | 0 | } |
6093 | | |
6094 | 0 | return false; |
6095 | 0 | } |
6096 | | |
6097 | | bool Sema::CheckSystemZBuiltinFunctionCall(unsigned BuiltinID, |
6098 | 0 | CallExpr *TheCall) { |
6099 | 0 | if (BuiltinID == SystemZ::BI__builtin_tabort) { |
6100 | 0 | Expr *Arg = TheCall->getArg(0); |
6101 | 0 | if (std::optional<llvm::APSInt> AbortCode = |
6102 | 0 | Arg->getIntegerConstantExpr(Context)) |
6103 | 0 | if (AbortCode->getSExtValue() >= 0 && AbortCode->getSExtValue() < 256) |
6104 | 0 | return Diag(Arg->getBeginLoc(), diag::err_systemz_invalid_tabort_code) |
6105 | 0 | << Arg->getSourceRange(); |
6106 | 0 | } |
6107 | | |
6108 | | // For intrinsics which take an immediate value as part of the instruction, |
6109 | | // range check them here. |
6110 | 0 | unsigned i = 0, l = 0, u = 0; |
6111 | 0 | switch (BuiltinID) { |
6112 | 0 | default: return false; |
6113 | 0 | case SystemZ::BI__builtin_s390_lcbb: i = 1; l = 0; u = 15; break; |
6114 | 0 | case SystemZ::BI__builtin_s390_verimb: |
6115 | 0 | case SystemZ::BI__builtin_s390_verimh: |
6116 | 0 | case SystemZ::BI__builtin_s390_verimf: |
6117 | 0 | case SystemZ::BI__builtin_s390_verimg: i = 3; l = 0; u = 255; break; |
6118 | 0 | case SystemZ::BI__builtin_s390_vfaeb: |
6119 | 0 | case SystemZ::BI__builtin_s390_vfaeh: |
6120 | 0 | case SystemZ::BI__builtin_s390_vfaef: |
6121 | 0 | case SystemZ::BI__builtin_s390_vfaebs: |
6122 | 0 | case SystemZ::BI__builtin_s390_vfaehs: |
6123 | 0 | case SystemZ::BI__builtin_s390_vfaefs: |
6124 | 0 | case SystemZ::BI__builtin_s390_vfaezb: |
6125 | 0 | case SystemZ::BI__builtin_s390_vfaezh: |
6126 | 0 | case SystemZ::BI__builtin_s390_vfaezf: |
6127 | 0 | case SystemZ::BI__builtin_s390_vfaezbs: |
6128 | 0 | case SystemZ::BI__builtin_s390_vfaezhs: |
6129 | 0 | case SystemZ::BI__builtin_s390_vfaezfs: i = 2; l = 0; u = 15; break; |
6130 | 0 | case SystemZ::BI__builtin_s390_vfisb: |
6131 | 0 | case SystemZ::BI__builtin_s390_vfidb: |
6132 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15) || |
6133 | 0 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 15); |
6134 | 0 | case SystemZ::BI__builtin_s390_vftcisb: |
6135 | 0 | case SystemZ::BI__builtin_s390_vftcidb: i = 1; l = 0; u = 4095; break; |
6136 | 0 | case SystemZ::BI__builtin_s390_vlbb: i = 1; l = 0; u = 15; break; |
6137 | 0 | case SystemZ::BI__builtin_s390_vpdi: i = 2; l = 0; u = 15; break; |
6138 | 0 | case SystemZ::BI__builtin_s390_vsldb: i = 2; l = 0; u = 15; break; |
6139 | 0 | case SystemZ::BI__builtin_s390_vstrcb: |
6140 | 0 | case SystemZ::BI__builtin_s390_vstrch: |
6141 | 0 | case SystemZ::BI__builtin_s390_vstrcf: |
6142 | 0 | case SystemZ::BI__builtin_s390_vstrczb: |
6143 | 0 | case SystemZ::BI__builtin_s390_vstrczh: |
6144 | 0 | case SystemZ::BI__builtin_s390_vstrczf: |
6145 | 0 | case SystemZ::BI__builtin_s390_vstrcbs: |
6146 | 0 | case SystemZ::BI__builtin_s390_vstrchs: |
6147 | 0 | case SystemZ::BI__builtin_s390_vstrcfs: |
6148 | 0 | case SystemZ::BI__builtin_s390_vstrczbs: |
6149 | 0 | case SystemZ::BI__builtin_s390_vstrczhs: |
6150 | 0 | case SystemZ::BI__builtin_s390_vstrczfs: i = 3; l = 0; u = 15; break; |
6151 | 0 | case SystemZ::BI__builtin_s390_vmslg: i = 3; l = 0; u = 15; break; |
6152 | 0 | case SystemZ::BI__builtin_s390_vfminsb: |
6153 | 0 | case SystemZ::BI__builtin_s390_vfmaxsb: |
6154 | 0 | case SystemZ::BI__builtin_s390_vfmindb: |
6155 | 0 | case SystemZ::BI__builtin_s390_vfmaxdb: i = 2; l = 0; u = 15; break; |
6156 | 0 | case SystemZ::BI__builtin_s390_vsld: i = 2; l = 0; u = 7; break; |
6157 | 0 | case SystemZ::BI__builtin_s390_vsrd: i = 2; l = 0; u = 7; break; |
6158 | 0 | case SystemZ::BI__builtin_s390_vclfnhs: |
6159 | 0 | case SystemZ::BI__builtin_s390_vclfnls: |
6160 | 0 | case SystemZ::BI__builtin_s390_vcfn: |
6161 | 0 | case SystemZ::BI__builtin_s390_vcnf: i = 1; l = 0; u = 15; break; |
6162 | 0 | case SystemZ::BI__builtin_s390_vcrnfs: i = 2; l = 0; u = 15; break; |
6163 | 0 | } |
6164 | 0 | return SemaBuiltinConstantArgRange(TheCall, i, l, u); |
6165 | 0 | } |
6166 | | |
6167 | | bool Sema::CheckWebAssemblyBuiltinFunctionCall(const TargetInfo &TI, |
6168 | | unsigned BuiltinID, |
6169 | 0 | CallExpr *TheCall) { |
6170 | 0 | switch (BuiltinID) { |
6171 | 0 | case WebAssembly::BI__builtin_wasm_ref_null_extern: |
6172 | 0 | return BuiltinWasmRefNullExtern(TheCall); |
6173 | 0 | case WebAssembly::BI__builtin_wasm_ref_null_func: |
6174 | 0 | return BuiltinWasmRefNullFunc(TheCall); |
6175 | 0 | case WebAssembly::BI__builtin_wasm_table_get: |
6176 | 0 | return BuiltinWasmTableGet(TheCall); |
6177 | 0 | case WebAssembly::BI__builtin_wasm_table_set: |
6178 | 0 | return BuiltinWasmTableSet(TheCall); |
6179 | 0 | case WebAssembly::BI__builtin_wasm_table_size: |
6180 | 0 | return BuiltinWasmTableSize(TheCall); |
6181 | 0 | case WebAssembly::BI__builtin_wasm_table_grow: |
6182 | 0 | return BuiltinWasmTableGrow(TheCall); |
6183 | 0 | case WebAssembly::BI__builtin_wasm_table_fill: |
6184 | 0 | return BuiltinWasmTableFill(TheCall); |
6185 | 0 | case WebAssembly::BI__builtin_wasm_table_copy: |
6186 | 0 | return BuiltinWasmTableCopy(TheCall); |
6187 | 0 | } |
6188 | | |
6189 | 0 | return false; |
6190 | 0 | } |
6191 | | |
6192 | 0 | void Sema::checkRVVTypeSupport(QualType Ty, SourceLocation Loc, Decl *D) { |
6193 | 0 | const TargetInfo &TI = Context.getTargetInfo(); |
6194 | |
|
6195 | 0 | ASTContext::BuiltinVectorTypeInfo Info = |
6196 | 0 | Context.getBuiltinVectorTypeInfo(Ty->castAs<BuiltinType>()); |
6197 | 0 | unsigned EltSize = Context.getTypeSize(Info.ElementType); |
6198 | 0 | unsigned MinElts = Info.EC.getKnownMinValue(); |
6199 | | |
6200 | | // (ELEN, LMUL) pairs of (8, mf8), (16, mf4), (32, mf2), (64, m1) requires at |
6201 | | // least zve64x |
6202 | 0 | if (((EltSize == 64 && Info.ElementType->isIntegerType()) || MinElts == 1) && |
6203 | 0 | !TI.hasFeature("zve64x")) |
6204 | 0 | Diag(Loc, diag::err_riscv_type_requires_extension, D) << Ty << "zve64x"; |
6205 | 0 | else if (Info.ElementType->isFloat16Type() && !TI.hasFeature("zvfh") && |
6206 | 0 | !TI.hasFeature("zvfhmin")) |
6207 | 0 | Diag(Loc, diag::err_riscv_type_requires_extension, D) |
6208 | 0 | << Ty << "zvfh or zvfhmin"; |
6209 | 0 | else if (Info.ElementType->isBFloat16Type() && |
6210 | 0 | !TI.hasFeature("experimental-zvfbfmin")) |
6211 | 0 | Diag(Loc, diag::err_riscv_type_requires_extension, D) << Ty << "zvfbfmin"; |
6212 | 0 | else if (Info.ElementType->isSpecificBuiltinType(BuiltinType::Float) && |
6213 | 0 | !TI.hasFeature("zve32f")) |
6214 | 0 | Diag(Loc, diag::err_riscv_type_requires_extension, D) << Ty << "zve32f"; |
6215 | 0 | else if (Info.ElementType->isSpecificBuiltinType(BuiltinType::Double) && |
6216 | 0 | !TI.hasFeature("zve64d")) |
6217 | 0 | Diag(Loc, diag::err_riscv_type_requires_extension, D) << Ty << "zve64d"; |
6218 | | // Given that caller already checked isRVVType() before calling this function, |
6219 | | // if we don't have at least zve32x supported, then we need to emit error. |
6220 | 0 | else if (!TI.hasFeature("zve32x")) |
6221 | 0 | Diag(Loc, diag::err_riscv_type_requires_extension, D) << Ty << "zve32x"; |
6222 | 0 | } |
6223 | | |
6224 | | bool Sema::CheckNVPTXBuiltinFunctionCall(const TargetInfo &TI, |
6225 | | unsigned BuiltinID, |
6226 | 0 | CallExpr *TheCall) { |
6227 | 0 | switch (BuiltinID) { |
6228 | 0 | case NVPTX::BI__nvvm_cp_async_ca_shared_global_4: |
6229 | 0 | case NVPTX::BI__nvvm_cp_async_ca_shared_global_8: |
6230 | 0 | case NVPTX::BI__nvvm_cp_async_ca_shared_global_16: |
6231 | 0 | case NVPTX::BI__nvvm_cp_async_cg_shared_global_16: |
6232 | 0 | return checkArgCountAtMost(*this, TheCall, 3); |
6233 | 0 | } |
6234 | | |
6235 | 0 | return false; |
6236 | 0 | } |
6237 | | |
6238 | | /// SemaBuiltinCpuSupports - Handle __builtin_cpu_supports(char *). |
6239 | | /// This checks that the target supports __builtin_cpu_supports and |
6240 | | /// that the string argument is constant and valid. |
6241 | | static bool SemaBuiltinCpuSupports(Sema &S, const TargetInfo &TI, |
6242 | 0 | CallExpr *TheCall) { |
6243 | 0 | Expr *Arg = TheCall->getArg(0); |
6244 | | |
6245 | | // Check if the argument is a string literal. |
6246 | 0 | if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts())) |
6247 | 0 | return S.Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal) |
6248 | 0 | << Arg->getSourceRange(); |
6249 | | |
6250 | | // Check the contents of the string. |
6251 | 0 | StringRef Feature = |
6252 | 0 | cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString(); |
6253 | 0 | if (!TI.validateCpuSupports(Feature)) |
6254 | 0 | return S.Diag(TheCall->getBeginLoc(), diag::err_invalid_cpu_supports) |
6255 | 0 | << Arg->getSourceRange(); |
6256 | 0 | return false; |
6257 | 0 | } |
6258 | | |
6259 | | /// SemaBuiltinCpuIs - Handle __builtin_cpu_is(char *). |
6260 | | /// This checks that the target supports __builtin_cpu_is and |
6261 | | /// that the string argument is constant and valid. |
6262 | 0 | static bool SemaBuiltinCpuIs(Sema &S, const TargetInfo &TI, CallExpr *TheCall) { |
6263 | 0 | Expr *Arg = TheCall->getArg(0); |
6264 | | |
6265 | | // Check if the argument is a string literal. |
6266 | 0 | if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts())) |
6267 | 0 | return S.Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal) |
6268 | 0 | << Arg->getSourceRange(); |
6269 | | |
6270 | | // Check the contents of the string. |
6271 | 0 | StringRef Feature = |
6272 | 0 | cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString(); |
6273 | 0 | if (!TI.validateCpuIs(Feature)) |
6274 | 0 | return S.Diag(TheCall->getBeginLoc(), diag::err_invalid_cpu_is) |
6275 | 0 | << Arg->getSourceRange(); |
6276 | 0 | return false; |
6277 | 0 | } |
6278 | | |
6279 | | // Check if the rounding mode is legal. |
6280 | 0 | bool Sema::CheckX86BuiltinRoundingOrSAE(unsigned BuiltinID, CallExpr *TheCall) { |
6281 | | // Indicates if this instruction has rounding control or just SAE. |
6282 | 0 | bool HasRC = false; |
6283 | |
|
6284 | 0 | unsigned ArgNum = 0; |
6285 | 0 | switch (BuiltinID) { |
6286 | 0 | default: |
6287 | 0 | return false; |
6288 | 0 | case X86::BI__builtin_ia32_vcvttsd2si32: |
6289 | 0 | case X86::BI__builtin_ia32_vcvttsd2si64: |
6290 | 0 | case X86::BI__builtin_ia32_vcvttsd2usi32: |
6291 | 0 | case X86::BI__builtin_ia32_vcvttsd2usi64: |
6292 | 0 | case X86::BI__builtin_ia32_vcvttss2si32: |
6293 | 0 | case X86::BI__builtin_ia32_vcvttss2si64: |
6294 | 0 | case X86::BI__builtin_ia32_vcvttss2usi32: |
6295 | 0 | case X86::BI__builtin_ia32_vcvttss2usi64: |
6296 | 0 | case X86::BI__builtin_ia32_vcvttsh2si32: |
6297 | 0 | case X86::BI__builtin_ia32_vcvttsh2si64: |
6298 | 0 | case X86::BI__builtin_ia32_vcvttsh2usi32: |
6299 | 0 | case X86::BI__builtin_ia32_vcvttsh2usi64: |
6300 | 0 | ArgNum = 1; |
6301 | 0 | break; |
6302 | 0 | case X86::BI__builtin_ia32_maxpd512: |
6303 | 0 | case X86::BI__builtin_ia32_maxps512: |
6304 | 0 | case X86::BI__builtin_ia32_minpd512: |
6305 | 0 | case X86::BI__builtin_ia32_minps512: |
6306 | 0 | case X86::BI__builtin_ia32_maxph512: |
6307 | 0 | case X86::BI__builtin_ia32_minph512: |
6308 | 0 | ArgNum = 2; |
6309 | 0 | break; |
6310 | 0 | case X86::BI__builtin_ia32_vcvtph2pd512_mask: |
6311 | 0 | case X86::BI__builtin_ia32_vcvtph2psx512_mask: |
6312 | 0 | case X86::BI__builtin_ia32_cvtps2pd512_mask: |
6313 | 0 | case X86::BI__builtin_ia32_cvttpd2dq512_mask: |
6314 | 0 | case X86::BI__builtin_ia32_cvttpd2qq512_mask: |
6315 | 0 | case X86::BI__builtin_ia32_cvttpd2udq512_mask: |
6316 | 0 | case X86::BI__builtin_ia32_cvttpd2uqq512_mask: |
6317 | 0 | case X86::BI__builtin_ia32_cvttps2dq512_mask: |
6318 | 0 | case X86::BI__builtin_ia32_cvttps2qq512_mask: |
6319 | 0 | case X86::BI__builtin_ia32_cvttps2udq512_mask: |
6320 | 0 | case X86::BI__builtin_ia32_cvttps2uqq512_mask: |
6321 | 0 | case X86::BI__builtin_ia32_vcvttph2w512_mask: |
6322 | 0 | case X86::BI__builtin_ia32_vcvttph2uw512_mask: |
6323 | 0 | case X86::BI__builtin_ia32_vcvttph2dq512_mask: |
6324 | 0 | case X86::BI__builtin_ia32_vcvttph2udq512_mask: |
6325 | 0 | case X86::BI__builtin_ia32_vcvttph2qq512_mask: |
6326 | 0 | case X86::BI__builtin_ia32_vcvttph2uqq512_mask: |
6327 | 0 | case X86::BI__builtin_ia32_exp2pd_mask: |
6328 | 0 | case X86::BI__builtin_ia32_exp2ps_mask: |
6329 | 0 | case X86::BI__builtin_ia32_getexppd512_mask: |
6330 | 0 | case X86::BI__builtin_ia32_getexpps512_mask: |
6331 | 0 | case X86::BI__builtin_ia32_getexpph512_mask: |
6332 | 0 | case X86::BI__builtin_ia32_rcp28pd_mask: |
6333 | 0 | case X86::BI__builtin_ia32_rcp28ps_mask: |
6334 | 0 | case X86::BI__builtin_ia32_rsqrt28pd_mask: |
6335 | 0 | case X86::BI__builtin_ia32_rsqrt28ps_mask: |
6336 | 0 | case X86::BI__builtin_ia32_vcomisd: |
6337 | 0 | case X86::BI__builtin_ia32_vcomiss: |
6338 | 0 | case X86::BI__builtin_ia32_vcomish: |
6339 | 0 | case X86::BI__builtin_ia32_vcvtph2ps512_mask: |
6340 | 0 | ArgNum = 3; |
6341 | 0 | break; |
6342 | 0 | case X86::BI__builtin_ia32_cmppd512_mask: |
6343 | 0 | case X86::BI__builtin_ia32_cmpps512_mask: |
6344 | 0 | case X86::BI__builtin_ia32_cmpsd_mask: |
6345 | 0 | case X86::BI__builtin_ia32_cmpss_mask: |
6346 | 0 | case X86::BI__builtin_ia32_cmpsh_mask: |
6347 | 0 | case X86::BI__builtin_ia32_vcvtsh2sd_round_mask: |
6348 | 0 | case X86::BI__builtin_ia32_vcvtsh2ss_round_mask: |
6349 | 0 | case X86::BI__builtin_ia32_cvtss2sd_round_mask: |
6350 | 0 | case X86::BI__builtin_ia32_getexpsd128_round_mask: |
6351 | 0 | case X86::BI__builtin_ia32_getexpss128_round_mask: |
6352 | 0 | case X86::BI__builtin_ia32_getexpsh128_round_mask: |
6353 | 0 | case X86::BI__builtin_ia32_getmantpd512_mask: |
6354 | 0 | case X86::BI__builtin_ia32_getmantps512_mask: |
6355 | 0 | case X86::BI__builtin_ia32_getmantph512_mask: |
6356 | 0 | case X86::BI__builtin_ia32_maxsd_round_mask: |
6357 | 0 | case X86::BI__builtin_ia32_maxss_round_mask: |
6358 | 0 | case X86::BI__builtin_ia32_maxsh_round_mask: |
6359 | 0 | case X86::BI__builtin_ia32_minsd_round_mask: |
6360 | 0 | case X86::BI__builtin_ia32_minss_round_mask: |
6361 | 0 | case X86::BI__builtin_ia32_minsh_round_mask: |
6362 | 0 | case X86::BI__builtin_ia32_rcp28sd_round_mask: |
6363 | 0 | case X86::BI__builtin_ia32_rcp28ss_round_mask: |
6364 | 0 | case X86::BI__builtin_ia32_reducepd512_mask: |
6365 | 0 | case X86::BI__builtin_ia32_reduceps512_mask: |
6366 | 0 | case X86::BI__builtin_ia32_reduceph512_mask: |
6367 | 0 | case X86::BI__builtin_ia32_rndscalepd_mask: |
6368 | 0 | case X86::BI__builtin_ia32_rndscaleps_mask: |
6369 | 0 | case X86::BI__builtin_ia32_rndscaleph_mask: |
6370 | 0 | case X86::BI__builtin_ia32_rsqrt28sd_round_mask: |
6371 | 0 | case X86::BI__builtin_ia32_rsqrt28ss_round_mask: |
6372 | 0 | ArgNum = 4; |
6373 | 0 | break; |
6374 | 0 | case X86::BI__builtin_ia32_fixupimmpd512_mask: |
6375 | 0 | case X86::BI__builtin_ia32_fixupimmpd512_maskz: |
6376 | 0 | case X86::BI__builtin_ia32_fixupimmps512_mask: |
6377 | 0 | case X86::BI__builtin_ia32_fixupimmps512_maskz: |
6378 | 0 | case X86::BI__builtin_ia32_fixupimmsd_mask: |
6379 | 0 | case X86::BI__builtin_ia32_fixupimmsd_maskz: |
6380 | 0 | case X86::BI__builtin_ia32_fixupimmss_mask: |
6381 | 0 | case X86::BI__builtin_ia32_fixupimmss_maskz: |
6382 | 0 | case X86::BI__builtin_ia32_getmantsd_round_mask: |
6383 | 0 | case X86::BI__builtin_ia32_getmantss_round_mask: |
6384 | 0 | case X86::BI__builtin_ia32_getmantsh_round_mask: |
6385 | 0 | case X86::BI__builtin_ia32_rangepd512_mask: |
6386 | 0 | case X86::BI__builtin_ia32_rangeps512_mask: |
6387 | 0 | case X86::BI__builtin_ia32_rangesd128_round_mask: |
6388 | 0 | case X86::BI__builtin_ia32_rangess128_round_mask: |
6389 | 0 | case X86::BI__builtin_ia32_reducesd_mask: |
6390 | 0 | case X86::BI__builtin_ia32_reducess_mask: |
6391 | 0 | case X86::BI__builtin_ia32_reducesh_mask: |
6392 | 0 | case X86::BI__builtin_ia32_rndscalesd_round_mask: |
6393 | 0 | case X86::BI__builtin_ia32_rndscaless_round_mask: |
6394 | 0 | case X86::BI__builtin_ia32_rndscalesh_round_mask: |
6395 | 0 | ArgNum = 5; |
6396 | 0 | break; |
6397 | 0 | case X86::BI__builtin_ia32_vcvtsd2si64: |
6398 | 0 | case X86::BI__builtin_ia32_vcvtsd2si32: |
6399 | 0 | case X86::BI__builtin_ia32_vcvtsd2usi32: |
6400 | 0 | case X86::BI__builtin_ia32_vcvtsd2usi64: |
6401 | 0 | case X86::BI__builtin_ia32_vcvtss2si32: |
6402 | 0 | case X86::BI__builtin_ia32_vcvtss2si64: |
6403 | 0 | case X86::BI__builtin_ia32_vcvtss2usi32: |
6404 | 0 | case X86::BI__builtin_ia32_vcvtss2usi64: |
6405 | 0 | case X86::BI__builtin_ia32_vcvtsh2si32: |
6406 | 0 | case X86::BI__builtin_ia32_vcvtsh2si64: |
6407 | 0 | case X86::BI__builtin_ia32_vcvtsh2usi32: |
6408 | 0 | case X86::BI__builtin_ia32_vcvtsh2usi64: |
6409 | 0 | case X86::BI__builtin_ia32_sqrtpd512: |
6410 | 0 | case X86::BI__builtin_ia32_sqrtps512: |
6411 | 0 | case X86::BI__builtin_ia32_sqrtph512: |
6412 | 0 | ArgNum = 1; |
6413 | 0 | HasRC = true; |
6414 | 0 | break; |
6415 | 0 | case X86::BI__builtin_ia32_addph512: |
6416 | 0 | case X86::BI__builtin_ia32_divph512: |
6417 | 0 | case X86::BI__builtin_ia32_mulph512: |
6418 | 0 | case X86::BI__builtin_ia32_subph512: |
6419 | 0 | case X86::BI__builtin_ia32_addpd512: |
6420 | 0 | case X86::BI__builtin_ia32_addps512: |
6421 | 0 | case X86::BI__builtin_ia32_divpd512: |
6422 | 0 | case X86::BI__builtin_ia32_divps512: |
6423 | 0 | case X86::BI__builtin_ia32_mulpd512: |
6424 | 0 | case X86::BI__builtin_ia32_mulps512: |
6425 | 0 | case X86::BI__builtin_ia32_subpd512: |
6426 | 0 | case X86::BI__builtin_ia32_subps512: |
6427 | 0 | case X86::BI__builtin_ia32_cvtsi2sd64: |
6428 | 0 | case X86::BI__builtin_ia32_cvtsi2ss32: |
6429 | 0 | case X86::BI__builtin_ia32_cvtsi2ss64: |
6430 | 0 | case X86::BI__builtin_ia32_cvtusi2sd64: |
6431 | 0 | case X86::BI__builtin_ia32_cvtusi2ss32: |
6432 | 0 | case X86::BI__builtin_ia32_cvtusi2ss64: |
6433 | 0 | case X86::BI__builtin_ia32_vcvtusi2sh: |
6434 | 0 | case X86::BI__builtin_ia32_vcvtusi642sh: |
6435 | 0 | case X86::BI__builtin_ia32_vcvtsi2sh: |
6436 | 0 | case X86::BI__builtin_ia32_vcvtsi642sh: |
6437 | 0 | ArgNum = 2; |
6438 | 0 | HasRC = true; |
6439 | 0 | break; |
6440 | 0 | case X86::BI__builtin_ia32_cvtdq2ps512_mask: |
6441 | 0 | case X86::BI__builtin_ia32_cvtudq2ps512_mask: |
6442 | 0 | case X86::BI__builtin_ia32_vcvtpd2ph512_mask: |
6443 | 0 | case X86::BI__builtin_ia32_vcvtps2phx512_mask: |
6444 | 0 | case X86::BI__builtin_ia32_cvtpd2ps512_mask: |
6445 | 0 | case X86::BI__builtin_ia32_cvtpd2dq512_mask: |
6446 | 0 | case X86::BI__builtin_ia32_cvtpd2qq512_mask: |
6447 | 0 | case X86::BI__builtin_ia32_cvtpd2udq512_mask: |
6448 | 0 | case X86::BI__builtin_ia32_cvtpd2uqq512_mask: |
6449 | 0 | case X86::BI__builtin_ia32_cvtps2dq512_mask: |
6450 | 0 | case X86::BI__builtin_ia32_cvtps2qq512_mask: |
6451 | 0 | case X86::BI__builtin_ia32_cvtps2udq512_mask: |
6452 | 0 | case X86::BI__builtin_ia32_cvtps2uqq512_mask: |
6453 | 0 | case X86::BI__builtin_ia32_cvtqq2pd512_mask: |
6454 | 0 | case X86::BI__builtin_ia32_cvtqq2ps512_mask: |
6455 | 0 | case X86::BI__builtin_ia32_cvtuqq2pd512_mask: |
6456 | 0 | case X86::BI__builtin_ia32_cvtuqq2ps512_mask: |
6457 | 0 | case X86::BI__builtin_ia32_vcvtdq2ph512_mask: |
6458 | 0 | case X86::BI__builtin_ia32_vcvtudq2ph512_mask: |
6459 | 0 | case X86::BI__builtin_ia32_vcvtw2ph512_mask: |
6460 | 0 | case X86::BI__builtin_ia32_vcvtuw2ph512_mask: |
6461 | 0 | case X86::BI__builtin_ia32_vcvtph2w512_mask: |
6462 | 0 | case X86::BI__builtin_ia32_vcvtph2uw512_mask: |
6463 | 0 | case X86::BI__builtin_ia32_vcvtph2dq512_mask: |
6464 | 0 | case X86::BI__builtin_ia32_vcvtph2udq512_mask: |
6465 | 0 | case X86::BI__builtin_ia32_vcvtph2qq512_mask: |
6466 | 0 | case X86::BI__builtin_ia32_vcvtph2uqq512_mask: |
6467 | 0 | case X86::BI__builtin_ia32_vcvtqq2ph512_mask: |
6468 | 0 | case X86::BI__builtin_ia32_vcvtuqq2ph512_mask: |
6469 | 0 | ArgNum = 3; |
6470 | 0 | HasRC = true; |
6471 | 0 | break; |
6472 | 0 | case X86::BI__builtin_ia32_addsh_round_mask: |
6473 | 0 | case X86::BI__builtin_ia32_addss_round_mask: |
6474 | 0 | case X86::BI__builtin_ia32_addsd_round_mask: |
6475 | 0 | case X86::BI__builtin_ia32_divsh_round_mask: |
6476 | 0 | case X86::BI__builtin_ia32_divss_round_mask: |
6477 | 0 | case X86::BI__builtin_ia32_divsd_round_mask: |
6478 | 0 | case X86::BI__builtin_ia32_mulsh_round_mask: |
6479 | 0 | case X86::BI__builtin_ia32_mulss_round_mask: |
6480 | 0 | case X86::BI__builtin_ia32_mulsd_round_mask: |
6481 | 0 | case X86::BI__builtin_ia32_subsh_round_mask: |
6482 | 0 | case X86::BI__builtin_ia32_subss_round_mask: |
6483 | 0 | case X86::BI__builtin_ia32_subsd_round_mask: |
6484 | 0 | case X86::BI__builtin_ia32_scalefph512_mask: |
6485 | 0 | case X86::BI__builtin_ia32_scalefpd512_mask: |
6486 | 0 | case X86::BI__builtin_ia32_scalefps512_mask: |
6487 | 0 | case X86::BI__builtin_ia32_scalefsd_round_mask: |
6488 | 0 | case X86::BI__builtin_ia32_scalefss_round_mask: |
6489 | 0 | case X86::BI__builtin_ia32_scalefsh_round_mask: |
6490 | 0 | case X86::BI__builtin_ia32_cvtsd2ss_round_mask: |
6491 | 0 | case X86::BI__builtin_ia32_vcvtss2sh_round_mask: |
6492 | 0 | case X86::BI__builtin_ia32_vcvtsd2sh_round_mask: |
6493 | 0 | case X86::BI__builtin_ia32_sqrtsd_round_mask: |
6494 | 0 | case X86::BI__builtin_ia32_sqrtss_round_mask: |
6495 | 0 | case X86::BI__builtin_ia32_sqrtsh_round_mask: |
6496 | 0 | case X86::BI__builtin_ia32_vfmaddsd3_mask: |
6497 | 0 | case X86::BI__builtin_ia32_vfmaddsd3_maskz: |
6498 | 0 | case X86::BI__builtin_ia32_vfmaddsd3_mask3: |
6499 | 0 | case X86::BI__builtin_ia32_vfmaddss3_mask: |
6500 | 0 | case X86::BI__builtin_ia32_vfmaddss3_maskz: |
6501 | 0 | case X86::BI__builtin_ia32_vfmaddss3_mask3: |
6502 | 0 | case X86::BI__builtin_ia32_vfmaddsh3_mask: |
6503 | 0 | case X86::BI__builtin_ia32_vfmaddsh3_maskz: |
6504 | 0 | case X86::BI__builtin_ia32_vfmaddsh3_mask3: |
6505 | 0 | case X86::BI__builtin_ia32_vfmaddpd512_mask: |
6506 | 0 | case X86::BI__builtin_ia32_vfmaddpd512_maskz: |
6507 | 0 | case X86::BI__builtin_ia32_vfmaddpd512_mask3: |
6508 | 0 | case X86::BI__builtin_ia32_vfmsubpd512_mask3: |
6509 | 0 | case X86::BI__builtin_ia32_vfmaddps512_mask: |
6510 | 0 | case X86::BI__builtin_ia32_vfmaddps512_maskz: |
6511 | 0 | case X86::BI__builtin_ia32_vfmaddps512_mask3: |
6512 | 0 | case X86::BI__builtin_ia32_vfmsubps512_mask3: |
6513 | 0 | case X86::BI__builtin_ia32_vfmaddph512_mask: |
6514 | 0 | case X86::BI__builtin_ia32_vfmaddph512_maskz: |
6515 | 0 | case X86::BI__builtin_ia32_vfmaddph512_mask3: |
6516 | 0 | case X86::BI__builtin_ia32_vfmsubph512_mask3: |
6517 | 0 | case X86::BI__builtin_ia32_vfmaddsubpd512_mask: |
6518 | 0 | case X86::BI__builtin_ia32_vfmaddsubpd512_maskz: |
6519 | 0 | case X86::BI__builtin_ia32_vfmaddsubpd512_mask3: |
6520 | 0 | case X86::BI__builtin_ia32_vfmsubaddpd512_mask3: |
6521 | 0 | case X86::BI__builtin_ia32_vfmaddsubps512_mask: |
6522 | 0 | case X86::BI__builtin_ia32_vfmaddsubps512_maskz: |
6523 | 0 | case X86::BI__builtin_ia32_vfmaddsubps512_mask3: |
6524 | 0 | case X86::BI__builtin_ia32_vfmsubaddps512_mask3: |
6525 | 0 | case X86::BI__builtin_ia32_vfmaddsubph512_mask: |
6526 | 0 | case X86::BI__builtin_ia32_vfmaddsubph512_maskz: |
6527 | 0 | case X86::BI__builtin_ia32_vfmaddsubph512_mask3: |
6528 | 0 | case X86::BI__builtin_ia32_vfmsubaddph512_mask3: |
6529 | 0 | case X86::BI__builtin_ia32_vfmaddcsh_mask: |
6530 | 0 | case X86::BI__builtin_ia32_vfmaddcsh_round_mask: |
6531 | 0 | case X86::BI__builtin_ia32_vfmaddcsh_round_mask3: |
6532 | 0 | case X86::BI__builtin_ia32_vfmaddcph512_mask: |
6533 | 0 | case X86::BI__builtin_ia32_vfmaddcph512_maskz: |
6534 | 0 | case X86::BI__builtin_ia32_vfmaddcph512_mask3: |
6535 | 0 | case X86::BI__builtin_ia32_vfcmaddcsh_mask: |
6536 | 0 | case X86::BI__builtin_ia32_vfcmaddcsh_round_mask: |
6537 | 0 | case X86::BI__builtin_ia32_vfcmaddcsh_round_mask3: |
6538 | 0 | case X86::BI__builtin_ia32_vfcmaddcph512_mask: |
6539 | 0 | case X86::BI__builtin_ia32_vfcmaddcph512_maskz: |
6540 | 0 | case X86::BI__builtin_ia32_vfcmaddcph512_mask3: |
6541 | 0 | case X86::BI__builtin_ia32_vfmulcsh_mask: |
6542 | 0 | case X86::BI__builtin_ia32_vfmulcph512_mask: |
6543 | 0 | case X86::BI__builtin_ia32_vfcmulcsh_mask: |
6544 | 0 | case X86::BI__builtin_ia32_vfcmulcph512_mask: |
6545 | 0 | ArgNum = 4; |
6546 | 0 | HasRC = true; |
6547 | 0 | break; |
6548 | 0 | } |
6549 | | |
6550 | 0 | llvm::APSInt Result; |
6551 | | |
6552 | | // We can't check the value of a dependent argument. |
6553 | 0 | Expr *Arg = TheCall->getArg(ArgNum); |
6554 | 0 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
6555 | 0 | return false; |
6556 | | |
6557 | | // Check constant-ness first. |
6558 | 0 | if (SemaBuiltinConstantArg(TheCall, ArgNum, Result)) |
6559 | 0 | return true; |
6560 | | |
6561 | | // Make sure rounding mode is either ROUND_CUR_DIRECTION or ROUND_NO_EXC bit |
6562 | | // is set. If the intrinsic has rounding control(bits 1:0), make sure its only |
6563 | | // combined with ROUND_NO_EXC. If the intrinsic does not have rounding |
6564 | | // control, allow ROUND_NO_EXC and ROUND_CUR_DIRECTION together. |
6565 | 0 | if (Result == 4/*ROUND_CUR_DIRECTION*/ || |
6566 | 0 | Result == 8/*ROUND_NO_EXC*/ || |
6567 | 0 | (!HasRC && Result == 12/*ROUND_CUR_DIRECTION|ROUND_NO_EXC*/) || |
6568 | 0 | (HasRC && Result.getZExtValue() >= 8 && Result.getZExtValue() <= 11)) |
6569 | 0 | return false; |
6570 | | |
6571 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_x86_builtin_invalid_rounding) |
6572 | 0 | << Arg->getSourceRange(); |
6573 | 0 | } |
6574 | | |
6575 | | // Check if the gather/scatter scale is legal. |
6576 | | bool Sema::CheckX86BuiltinGatherScatterScale(unsigned BuiltinID, |
6577 | 0 | CallExpr *TheCall) { |
6578 | 0 | unsigned ArgNum = 0; |
6579 | 0 | switch (BuiltinID) { |
6580 | 0 | default: |
6581 | 0 | return false; |
6582 | 0 | case X86::BI__builtin_ia32_gatherpfdpd: |
6583 | 0 | case X86::BI__builtin_ia32_gatherpfdps: |
6584 | 0 | case X86::BI__builtin_ia32_gatherpfqpd: |
6585 | 0 | case X86::BI__builtin_ia32_gatherpfqps: |
6586 | 0 | case X86::BI__builtin_ia32_scatterpfdpd: |
6587 | 0 | case X86::BI__builtin_ia32_scatterpfdps: |
6588 | 0 | case X86::BI__builtin_ia32_scatterpfqpd: |
6589 | 0 | case X86::BI__builtin_ia32_scatterpfqps: |
6590 | 0 | ArgNum = 3; |
6591 | 0 | break; |
6592 | 0 | case X86::BI__builtin_ia32_gatherd_pd: |
6593 | 0 | case X86::BI__builtin_ia32_gatherd_pd256: |
6594 | 0 | case X86::BI__builtin_ia32_gatherq_pd: |
6595 | 0 | case X86::BI__builtin_ia32_gatherq_pd256: |
6596 | 0 | case X86::BI__builtin_ia32_gatherd_ps: |
6597 | 0 | case X86::BI__builtin_ia32_gatherd_ps256: |
6598 | 0 | case X86::BI__builtin_ia32_gatherq_ps: |
6599 | 0 | case X86::BI__builtin_ia32_gatherq_ps256: |
6600 | 0 | case X86::BI__builtin_ia32_gatherd_q: |
6601 | 0 | case X86::BI__builtin_ia32_gatherd_q256: |
6602 | 0 | case X86::BI__builtin_ia32_gatherq_q: |
6603 | 0 | case X86::BI__builtin_ia32_gatherq_q256: |
6604 | 0 | case X86::BI__builtin_ia32_gatherd_d: |
6605 | 0 | case X86::BI__builtin_ia32_gatherd_d256: |
6606 | 0 | case X86::BI__builtin_ia32_gatherq_d: |
6607 | 0 | case X86::BI__builtin_ia32_gatherq_d256: |
6608 | 0 | case X86::BI__builtin_ia32_gather3div2df: |
6609 | 0 | case X86::BI__builtin_ia32_gather3div2di: |
6610 | 0 | case X86::BI__builtin_ia32_gather3div4df: |
6611 | 0 | case X86::BI__builtin_ia32_gather3div4di: |
6612 | 0 | case X86::BI__builtin_ia32_gather3div4sf: |
6613 | 0 | case X86::BI__builtin_ia32_gather3div4si: |
6614 | 0 | case X86::BI__builtin_ia32_gather3div8sf: |
6615 | 0 | case X86::BI__builtin_ia32_gather3div8si: |
6616 | 0 | case X86::BI__builtin_ia32_gather3siv2df: |
6617 | 0 | case X86::BI__builtin_ia32_gather3siv2di: |
6618 | 0 | case X86::BI__builtin_ia32_gather3siv4df: |
6619 | 0 | case X86::BI__builtin_ia32_gather3siv4di: |
6620 | 0 | case X86::BI__builtin_ia32_gather3siv4sf: |
6621 | 0 | case X86::BI__builtin_ia32_gather3siv4si: |
6622 | 0 | case X86::BI__builtin_ia32_gather3siv8sf: |
6623 | 0 | case X86::BI__builtin_ia32_gather3siv8si: |
6624 | 0 | case X86::BI__builtin_ia32_gathersiv8df: |
6625 | 0 | case X86::BI__builtin_ia32_gathersiv16sf: |
6626 | 0 | case X86::BI__builtin_ia32_gatherdiv8df: |
6627 | 0 | case X86::BI__builtin_ia32_gatherdiv16sf: |
6628 | 0 | case X86::BI__builtin_ia32_gathersiv8di: |
6629 | 0 | case X86::BI__builtin_ia32_gathersiv16si: |
6630 | 0 | case X86::BI__builtin_ia32_gatherdiv8di: |
6631 | 0 | case X86::BI__builtin_ia32_gatherdiv16si: |
6632 | 0 | case X86::BI__builtin_ia32_scatterdiv2df: |
6633 | 0 | case X86::BI__builtin_ia32_scatterdiv2di: |
6634 | 0 | case X86::BI__builtin_ia32_scatterdiv4df: |
6635 | 0 | case X86::BI__builtin_ia32_scatterdiv4di: |
6636 | 0 | case X86::BI__builtin_ia32_scatterdiv4sf: |
6637 | 0 | case X86::BI__builtin_ia32_scatterdiv4si: |
6638 | 0 | case X86::BI__builtin_ia32_scatterdiv8sf: |
6639 | 0 | case X86::BI__builtin_ia32_scatterdiv8si: |
6640 | 0 | case X86::BI__builtin_ia32_scattersiv2df: |
6641 | 0 | case X86::BI__builtin_ia32_scattersiv2di: |
6642 | 0 | case X86::BI__builtin_ia32_scattersiv4df: |
6643 | 0 | case X86::BI__builtin_ia32_scattersiv4di: |
6644 | 0 | case X86::BI__builtin_ia32_scattersiv4sf: |
6645 | 0 | case X86::BI__builtin_ia32_scattersiv4si: |
6646 | 0 | case X86::BI__builtin_ia32_scattersiv8sf: |
6647 | 0 | case X86::BI__builtin_ia32_scattersiv8si: |
6648 | 0 | case X86::BI__builtin_ia32_scattersiv8df: |
6649 | 0 | case X86::BI__builtin_ia32_scattersiv16sf: |
6650 | 0 | case X86::BI__builtin_ia32_scatterdiv8df: |
6651 | 0 | case X86::BI__builtin_ia32_scatterdiv16sf: |
6652 | 0 | case X86::BI__builtin_ia32_scattersiv8di: |
6653 | 0 | case X86::BI__builtin_ia32_scattersiv16si: |
6654 | 0 | case X86::BI__builtin_ia32_scatterdiv8di: |
6655 | 0 | case X86::BI__builtin_ia32_scatterdiv16si: |
6656 | 0 | ArgNum = 4; |
6657 | 0 | break; |
6658 | 0 | } |
6659 | | |
6660 | 0 | llvm::APSInt Result; |
6661 | | |
6662 | | // We can't check the value of a dependent argument. |
6663 | 0 | Expr *Arg = TheCall->getArg(ArgNum); |
6664 | 0 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
6665 | 0 | return false; |
6666 | | |
6667 | | // Check constant-ness first. |
6668 | 0 | if (SemaBuiltinConstantArg(TheCall, ArgNum, Result)) |
6669 | 0 | return true; |
6670 | | |
6671 | 0 | if (Result == 1 || Result == 2 || Result == 4 || Result == 8) |
6672 | 0 | return false; |
6673 | | |
6674 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_x86_builtin_invalid_scale) |
6675 | 0 | << Arg->getSourceRange(); |
6676 | 0 | } |
6677 | | |
6678 | | enum { TileRegLow = 0, TileRegHigh = 7 }; |
6679 | | |
6680 | | bool Sema::CheckX86BuiltinTileArgumentsRange(CallExpr *TheCall, |
6681 | 0 | ArrayRef<int> ArgNums) { |
6682 | 0 | for (int ArgNum : ArgNums) { |
6683 | 0 | if (SemaBuiltinConstantArgRange(TheCall, ArgNum, TileRegLow, TileRegHigh)) |
6684 | 0 | return true; |
6685 | 0 | } |
6686 | 0 | return false; |
6687 | 0 | } |
6688 | | |
6689 | | bool Sema::CheckX86BuiltinTileDuplicate(CallExpr *TheCall, |
6690 | 0 | ArrayRef<int> ArgNums) { |
6691 | | // Because the max number of tile register is TileRegHigh + 1, so here we use |
6692 | | // each bit to represent the usage of them in bitset. |
6693 | 0 | std::bitset<TileRegHigh + 1> ArgValues; |
6694 | 0 | for (int ArgNum : ArgNums) { |
6695 | 0 | Expr *Arg = TheCall->getArg(ArgNum); |
6696 | 0 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
6697 | 0 | continue; |
6698 | | |
6699 | 0 | llvm::APSInt Result; |
6700 | 0 | if (SemaBuiltinConstantArg(TheCall, ArgNum, Result)) |
6701 | 0 | return true; |
6702 | 0 | int ArgExtValue = Result.getExtValue(); |
6703 | 0 | assert((ArgExtValue >= TileRegLow && ArgExtValue <= TileRegHigh) && |
6704 | 0 | "Incorrect tile register num."); |
6705 | 0 | if (ArgValues.test(ArgExtValue)) |
6706 | 0 | return Diag(TheCall->getBeginLoc(), |
6707 | 0 | diag::err_x86_builtin_tile_arg_duplicate) |
6708 | 0 | << TheCall->getArg(ArgNum)->getSourceRange(); |
6709 | 0 | ArgValues.set(ArgExtValue); |
6710 | 0 | } |
6711 | 0 | return false; |
6712 | 0 | } |
6713 | | |
6714 | | bool Sema::CheckX86BuiltinTileRangeAndDuplicate(CallExpr *TheCall, |
6715 | 0 | ArrayRef<int> ArgNums) { |
6716 | 0 | return CheckX86BuiltinTileArgumentsRange(TheCall, ArgNums) || |
6717 | 0 | CheckX86BuiltinTileDuplicate(TheCall, ArgNums); |
6718 | 0 | } |
6719 | | |
6720 | 0 | bool Sema::CheckX86BuiltinTileArguments(unsigned BuiltinID, CallExpr *TheCall) { |
6721 | 0 | switch (BuiltinID) { |
6722 | 0 | default: |
6723 | 0 | return false; |
6724 | 0 | case X86::BI__builtin_ia32_tileloadd64: |
6725 | 0 | case X86::BI__builtin_ia32_tileloaddt164: |
6726 | 0 | case X86::BI__builtin_ia32_tilestored64: |
6727 | 0 | case X86::BI__builtin_ia32_tilezero: |
6728 | 0 | return CheckX86BuiltinTileArgumentsRange(TheCall, 0); |
6729 | 0 | case X86::BI__builtin_ia32_tdpbssd: |
6730 | 0 | case X86::BI__builtin_ia32_tdpbsud: |
6731 | 0 | case X86::BI__builtin_ia32_tdpbusd: |
6732 | 0 | case X86::BI__builtin_ia32_tdpbuud: |
6733 | 0 | case X86::BI__builtin_ia32_tdpbf16ps: |
6734 | 0 | case X86::BI__builtin_ia32_tdpfp16ps: |
6735 | 0 | case X86::BI__builtin_ia32_tcmmimfp16ps: |
6736 | 0 | case X86::BI__builtin_ia32_tcmmrlfp16ps: |
6737 | 0 | return CheckX86BuiltinTileRangeAndDuplicate(TheCall, {0, 1, 2}); |
6738 | 0 | } |
6739 | 0 | } |
6740 | 0 | static bool isX86_32Builtin(unsigned BuiltinID) { |
6741 | | // These builtins only work on x86-32 targets. |
6742 | 0 | switch (BuiltinID) { |
6743 | 0 | case X86::BI__builtin_ia32_readeflags_u32: |
6744 | 0 | case X86::BI__builtin_ia32_writeeflags_u32: |
6745 | 0 | return true; |
6746 | 0 | } |
6747 | | |
6748 | 0 | return false; |
6749 | 0 | } |
6750 | | |
6751 | | bool Sema::CheckX86BuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID, |
6752 | 0 | CallExpr *TheCall) { |
6753 | 0 | if (BuiltinID == X86::BI__builtin_cpu_supports) |
6754 | 0 | return SemaBuiltinCpuSupports(*this, TI, TheCall); |
6755 | | |
6756 | 0 | if (BuiltinID == X86::BI__builtin_cpu_is) |
6757 | 0 | return SemaBuiltinCpuIs(*this, TI, TheCall); |
6758 | | |
6759 | | // Check for 32-bit only builtins on a 64-bit target. |
6760 | 0 | const llvm::Triple &TT = TI.getTriple(); |
6761 | 0 | if (TT.getArch() != llvm::Triple::x86 && isX86_32Builtin(BuiltinID)) |
6762 | 0 | return Diag(TheCall->getCallee()->getBeginLoc(), |
6763 | 0 | diag::err_32_bit_builtin_64_bit_tgt); |
6764 | | |
6765 | | // If the intrinsic has rounding or SAE make sure its valid. |
6766 | 0 | if (CheckX86BuiltinRoundingOrSAE(BuiltinID, TheCall)) |
6767 | 0 | return true; |
6768 | | |
6769 | | // If the intrinsic has a gather/scatter scale immediate make sure its valid. |
6770 | 0 | if (CheckX86BuiltinGatherScatterScale(BuiltinID, TheCall)) |
6771 | 0 | return true; |
6772 | | |
6773 | | // If the intrinsic has a tile arguments, make sure they are valid. |
6774 | 0 | if (CheckX86BuiltinTileArguments(BuiltinID, TheCall)) |
6775 | 0 | return true; |
6776 | | |
6777 | | // For intrinsics which take an immediate value as part of the instruction, |
6778 | | // range check them here. |
6779 | 0 | int i = 0, l = 0, u = 0; |
6780 | 0 | switch (BuiltinID) { |
6781 | 0 | default: |
6782 | 0 | return false; |
6783 | 0 | case X86::BI__builtin_ia32_vec_ext_v2si: |
6784 | 0 | case X86::BI__builtin_ia32_vec_ext_v2di: |
6785 | 0 | case X86::BI__builtin_ia32_vextractf128_pd256: |
6786 | 0 | case X86::BI__builtin_ia32_vextractf128_ps256: |
6787 | 0 | case X86::BI__builtin_ia32_vextractf128_si256: |
6788 | 0 | case X86::BI__builtin_ia32_extract128i256: |
6789 | 0 | case X86::BI__builtin_ia32_extractf64x4_mask: |
6790 | 0 | case X86::BI__builtin_ia32_extracti64x4_mask: |
6791 | 0 | case X86::BI__builtin_ia32_extractf32x8_mask: |
6792 | 0 | case X86::BI__builtin_ia32_extracti32x8_mask: |
6793 | 0 | case X86::BI__builtin_ia32_extractf64x2_256_mask: |
6794 | 0 | case X86::BI__builtin_ia32_extracti64x2_256_mask: |
6795 | 0 | case X86::BI__builtin_ia32_extractf32x4_256_mask: |
6796 | 0 | case X86::BI__builtin_ia32_extracti32x4_256_mask: |
6797 | 0 | i = 1; l = 0; u = 1; |
6798 | 0 | break; |
6799 | 0 | case X86::BI__builtin_ia32_vec_set_v2di: |
6800 | 0 | case X86::BI__builtin_ia32_vinsertf128_pd256: |
6801 | 0 | case X86::BI__builtin_ia32_vinsertf128_ps256: |
6802 | 0 | case X86::BI__builtin_ia32_vinsertf128_si256: |
6803 | 0 | case X86::BI__builtin_ia32_insert128i256: |
6804 | 0 | case X86::BI__builtin_ia32_insertf32x8: |
6805 | 0 | case X86::BI__builtin_ia32_inserti32x8: |
6806 | 0 | case X86::BI__builtin_ia32_insertf64x4: |
6807 | 0 | case X86::BI__builtin_ia32_inserti64x4: |
6808 | 0 | case X86::BI__builtin_ia32_insertf64x2_256: |
6809 | 0 | case X86::BI__builtin_ia32_inserti64x2_256: |
6810 | 0 | case X86::BI__builtin_ia32_insertf32x4_256: |
6811 | 0 | case X86::BI__builtin_ia32_inserti32x4_256: |
6812 | 0 | i = 2; l = 0; u = 1; |
6813 | 0 | break; |
6814 | 0 | case X86::BI__builtin_ia32_vpermilpd: |
6815 | 0 | case X86::BI__builtin_ia32_vec_ext_v4hi: |
6816 | 0 | case X86::BI__builtin_ia32_vec_ext_v4si: |
6817 | 0 | case X86::BI__builtin_ia32_vec_ext_v4sf: |
6818 | 0 | case X86::BI__builtin_ia32_vec_ext_v4di: |
6819 | 0 | case X86::BI__builtin_ia32_extractf32x4_mask: |
6820 | 0 | case X86::BI__builtin_ia32_extracti32x4_mask: |
6821 | 0 | case X86::BI__builtin_ia32_extractf64x2_512_mask: |
6822 | 0 | case X86::BI__builtin_ia32_extracti64x2_512_mask: |
6823 | 0 | i = 1; l = 0; u = 3; |
6824 | 0 | break; |
6825 | 0 | case X86::BI_mm_prefetch: |
6826 | 0 | case X86::BI__builtin_ia32_vec_ext_v8hi: |
6827 | 0 | case X86::BI__builtin_ia32_vec_ext_v8si: |
6828 | 0 | i = 1; l = 0; u = 7; |
6829 | 0 | break; |
6830 | 0 | case X86::BI__builtin_ia32_sha1rnds4: |
6831 | 0 | case X86::BI__builtin_ia32_blendpd: |
6832 | 0 | case X86::BI__builtin_ia32_shufpd: |
6833 | 0 | case X86::BI__builtin_ia32_vec_set_v4hi: |
6834 | 0 | case X86::BI__builtin_ia32_vec_set_v4si: |
6835 | 0 | case X86::BI__builtin_ia32_vec_set_v4di: |
6836 | 0 | case X86::BI__builtin_ia32_shuf_f32x4_256: |
6837 | 0 | case X86::BI__builtin_ia32_shuf_f64x2_256: |
6838 | 0 | case X86::BI__builtin_ia32_shuf_i32x4_256: |
6839 | 0 | case X86::BI__builtin_ia32_shuf_i64x2_256: |
6840 | 0 | case X86::BI__builtin_ia32_insertf64x2_512: |
6841 | 0 | case X86::BI__builtin_ia32_inserti64x2_512: |
6842 | 0 | case X86::BI__builtin_ia32_insertf32x4: |
6843 | 0 | case X86::BI__builtin_ia32_inserti32x4: |
6844 | 0 | i = 2; l = 0; u = 3; |
6845 | 0 | break; |
6846 | 0 | case X86::BI__builtin_ia32_vpermil2pd: |
6847 | 0 | case X86::BI__builtin_ia32_vpermil2pd256: |
6848 | 0 | case X86::BI__builtin_ia32_vpermil2ps: |
6849 | 0 | case X86::BI__builtin_ia32_vpermil2ps256: |
6850 | 0 | i = 3; l = 0; u = 3; |
6851 | 0 | break; |
6852 | 0 | case X86::BI__builtin_ia32_cmpb128_mask: |
6853 | 0 | case X86::BI__builtin_ia32_cmpw128_mask: |
6854 | 0 | case X86::BI__builtin_ia32_cmpd128_mask: |
6855 | 0 | case X86::BI__builtin_ia32_cmpq128_mask: |
6856 | 0 | case X86::BI__builtin_ia32_cmpb256_mask: |
6857 | 0 | case X86::BI__builtin_ia32_cmpw256_mask: |
6858 | 0 | case X86::BI__builtin_ia32_cmpd256_mask: |
6859 | 0 | case X86::BI__builtin_ia32_cmpq256_mask: |
6860 | 0 | case X86::BI__builtin_ia32_cmpb512_mask: |
6861 | 0 | case X86::BI__builtin_ia32_cmpw512_mask: |
6862 | 0 | case X86::BI__builtin_ia32_cmpd512_mask: |
6863 | 0 | case X86::BI__builtin_ia32_cmpq512_mask: |
6864 | 0 | case X86::BI__builtin_ia32_ucmpb128_mask: |
6865 | 0 | case X86::BI__builtin_ia32_ucmpw128_mask: |
6866 | 0 | case X86::BI__builtin_ia32_ucmpd128_mask: |
6867 | 0 | case X86::BI__builtin_ia32_ucmpq128_mask: |
6868 | 0 | case X86::BI__builtin_ia32_ucmpb256_mask: |
6869 | 0 | case X86::BI__builtin_ia32_ucmpw256_mask: |
6870 | 0 | case X86::BI__builtin_ia32_ucmpd256_mask: |
6871 | 0 | case X86::BI__builtin_ia32_ucmpq256_mask: |
6872 | 0 | case X86::BI__builtin_ia32_ucmpb512_mask: |
6873 | 0 | case X86::BI__builtin_ia32_ucmpw512_mask: |
6874 | 0 | case X86::BI__builtin_ia32_ucmpd512_mask: |
6875 | 0 | case X86::BI__builtin_ia32_ucmpq512_mask: |
6876 | 0 | case X86::BI__builtin_ia32_vpcomub: |
6877 | 0 | case X86::BI__builtin_ia32_vpcomuw: |
6878 | 0 | case X86::BI__builtin_ia32_vpcomud: |
6879 | 0 | case X86::BI__builtin_ia32_vpcomuq: |
6880 | 0 | case X86::BI__builtin_ia32_vpcomb: |
6881 | 0 | case X86::BI__builtin_ia32_vpcomw: |
6882 | 0 | case X86::BI__builtin_ia32_vpcomd: |
6883 | 0 | case X86::BI__builtin_ia32_vpcomq: |
6884 | 0 | case X86::BI__builtin_ia32_vec_set_v8hi: |
6885 | 0 | case X86::BI__builtin_ia32_vec_set_v8si: |
6886 | 0 | i = 2; l = 0; u = 7; |
6887 | 0 | break; |
6888 | 0 | case X86::BI__builtin_ia32_vpermilpd256: |
6889 | 0 | case X86::BI__builtin_ia32_roundps: |
6890 | 0 | case X86::BI__builtin_ia32_roundpd: |
6891 | 0 | case X86::BI__builtin_ia32_roundps256: |
6892 | 0 | case X86::BI__builtin_ia32_roundpd256: |
6893 | 0 | case X86::BI__builtin_ia32_getmantpd128_mask: |
6894 | 0 | case X86::BI__builtin_ia32_getmantpd256_mask: |
6895 | 0 | case X86::BI__builtin_ia32_getmantps128_mask: |
6896 | 0 | case X86::BI__builtin_ia32_getmantps256_mask: |
6897 | 0 | case X86::BI__builtin_ia32_getmantpd512_mask: |
6898 | 0 | case X86::BI__builtin_ia32_getmantps512_mask: |
6899 | 0 | case X86::BI__builtin_ia32_getmantph128_mask: |
6900 | 0 | case X86::BI__builtin_ia32_getmantph256_mask: |
6901 | 0 | case X86::BI__builtin_ia32_getmantph512_mask: |
6902 | 0 | case X86::BI__builtin_ia32_vec_ext_v16qi: |
6903 | 0 | case X86::BI__builtin_ia32_vec_ext_v16hi: |
6904 | 0 | i = 1; l = 0; u = 15; |
6905 | 0 | break; |
6906 | 0 | case X86::BI__builtin_ia32_pblendd128: |
6907 | 0 | case X86::BI__builtin_ia32_blendps: |
6908 | 0 | case X86::BI__builtin_ia32_blendpd256: |
6909 | 0 | case X86::BI__builtin_ia32_shufpd256: |
6910 | 0 | case X86::BI__builtin_ia32_roundss: |
6911 | 0 | case X86::BI__builtin_ia32_roundsd: |
6912 | 0 | case X86::BI__builtin_ia32_rangepd128_mask: |
6913 | 0 | case X86::BI__builtin_ia32_rangepd256_mask: |
6914 | 0 | case X86::BI__builtin_ia32_rangepd512_mask: |
6915 | 0 | case X86::BI__builtin_ia32_rangeps128_mask: |
6916 | 0 | case X86::BI__builtin_ia32_rangeps256_mask: |
6917 | 0 | case X86::BI__builtin_ia32_rangeps512_mask: |
6918 | 0 | case X86::BI__builtin_ia32_getmantsd_round_mask: |
6919 | 0 | case X86::BI__builtin_ia32_getmantss_round_mask: |
6920 | 0 | case X86::BI__builtin_ia32_getmantsh_round_mask: |
6921 | 0 | case X86::BI__builtin_ia32_vec_set_v16qi: |
6922 | 0 | case X86::BI__builtin_ia32_vec_set_v16hi: |
6923 | 0 | i = 2; l = 0; u = 15; |
6924 | 0 | break; |
6925 | 0 | case X86::BI__builtin_ia32_vec_ext_v32qi: |
6926 | 0 | i = 1; l = 0; u = 31; |
6927 | 0 | break; |
6928 | 0 | case X86::BI__builtin_ia32_cmpps: |
6929 | 0 | case X86::BI__builtin_ia32_cmpss: |
6930 | 0 | case X86::BI__builtin_ia32_cmppd: |
6931 | 0 | case X86::BI__builtin_ia32_cmpsd: |
6932 | 0 | case X86::BI__builtin_ia32_cmpps256: |
6933 | 0 | case X86::BI__builtin_ia32_cmppd256: |
6934 | 0 | case X86::BI__builtin_ia32_cmpps128_mask: |
6935 | 0 | case X86::BI__builtin_ia32_cmppd128_mask: |
6936 | 0 | case X86::BI__builtin_ia32_cmpps256_mask: |
6937 | 0 | case X86::BI__builtin_ia32_cmppd256_mask: |
6938 | 0 | case X86::BI__builtin_ia32_cmpps512_mask: |
6939 | 0 | case X86::BI__builtin_ia32_cmppd512_mask: |
6940 | 0 | case X86::BI__builtin_ia32_cmpsd_mask: |
6941 | 0 | case X86::BI__builtin_ia32_cmpss_mask: |
6942 | 0 | case X86::BI__builtin_ia32_vec_set_v32qi: |
6943 | 0 | i = 2; l = 0; u = 31; |
6944 | 0 | break; |
6945 | 0 | case X86::BI__builtin_ia32_permdf256: |
6946 | 0 | case X86::BI__builtin_ia32_permdi256: |
6947 | 0 | case X86::BI__builtin_ia32_permdf512: |
6948 | 0 | case X86::BI__builtin_ia32_permdi512: |
6949 | 0 | case X86::BI__builtin_ia32_vpermilps: |
6950 | 0 | case X86::BI__builtin_ia32_vpermilps256: |
6951 | 0 | case X86::BI__builtin_ia32_vpermilpd512: |
6952 | 0 | case X86::BI__builtin_ia32_vpermilps512: |
6953 | 0 | case X86::BI__builtin_ia32_pshufd: |
6954 | 0 | case X86::BI__builtin_ia32_pshufd256: |
6955 | 0 | case X86::BI__builtin_ia32_pshufd512: |
6956 | 0 | case X86::BI__builtin_ia32_pshufhw: |
6957 | 0 | case X86::BI__builtin_ia32_pshufhw256: |
6958 | 0 | case X86::BI__builtin_ia32_pshufhw512: |
6959 | 0 | case X86::BI__builtin_ia32_pshuflw: |
6960 | 0 | case X86::BI__builtin_ia32_pshuflw256: |
6961 | 0 | case X86::BI__builtin_ia32_pshuflw512: |
6962 | 0 | case X86::BI__builtin_ia32_vcvtps2ph: |
6963 | 0 | case X86::BI__builtin_ia32_vcvtps2ph_mask: |
6964 | 0 | case X86::BI__builtin_ia32_vcvtps2ph256: |
6965 | 0 | case X86::BI__builtin_ia32_vcvtps2ph256_mask: |
6966 | 0 | case X86::BI__builtin_ia32_vcvtps2ph512_mask: |
6967 | 0 | case X86::BI__builtin_ia32_rndscaleps_128_mask: |
6968 | 0 | case X86::BI__builtin_ia32_rndscalepd_128_mask: |
6969 | 0 | case X86::BI__builtin_ia32_rndscaleps_256_mask: |
6970 | 0 | case X86::BI__builtin_ia32_rndscalepd_256_mask: |
6971 | 0 | case X86::BI__builtin_ia32_rndscaleps_mask: |
6972 | 0 | case X86::BI__builtin_ia32_rndscalepd_mask: |
6973 | 0 | case X86::BI__builtin_ia32_rndscaleph_mask: |
6974 | 0 | case X86::BI__builtin_ia32_reducepd128_mask: |
6975 | 0 | case X86::BI__builtin_ia32_reducepd256_mask: |
6976 | 0 | case X86::BI__builtin_ia32_reducepd512_mask: |
6977 | 0 | case X86::BI__builtin_ia32_reduceps128_mask: |
6978 | 0 | case X86::BI__builtin_ia32_reduceps256_mask: |
6979 | 0 | case X86::BI__builtin_ia32_reduceps512_mask: |
6980 | 0 | case X86::BI__builtin_ia32_reduceph128_mask: |
6981 | 0 | case X86::BI__builtin_ia32_reduceph256_mask: |
6982 | 0 | case X86::BI__builtin_ia32_reduceph512_mask: |
6983 | 0 | case X86::BI__builtin_ia32_prold512: |
6984 | 0 | case X86::BI__builtin_ia32_prolq512: |
6985 | 0 | case X86::BI__builtin_ia32_prold128: |
6986 | 0 | case X86::BI__builtin_ia32_prold256: |
6987 | 0 | case X86::BI__builtin_ia32_prolq128: |
6988 | 0 | case X86::BI__builtin_ia32_prolq256: |
6989 | 0 | case X86::BI__builtin_ia32_prord512: |
6990 | 0 | case X86::BI__builtin_ia32_prorq512: |
6991 | 0 | case X86::BI__builtin_ia32_prord128: |
6992 | 0 | case X86::BI__builtin_ia32_prord256: |
6993 | 0 | case X86::BI__builtin_ia32_prorq128: |
6994 | 0 | case X86::BI__builtin_ia32_prorq256: |
6995 | 0 | case X86::BI__builtin_ia32_fpclasspd128_mask: |
6996 | 0 | case X86::BI__builtin_ia32_fpclasspd256_mask: |
6997 | 0 | case X86::BI__builtin_ia32_fpclassps128_mask: |
6998 | 0 | case X86::BI__builtin_ia32_fpclassps256_mask: |
6999 | 0 | case X86::BI__builtin_ia32_fpclassps512_mask: |
7000 | 0 | case X86::BI__builtin_ia32_fpclasspd512_mask: |
7001 | 0 | case X86::BI__builtin_ia32_fpclassph128_mask: |
7002 | 0 | case X86::BI__builtin_ia32_fpclassph256_mask: |
7003 | 0 | case X86::BI__builtin_ia32_fpclassph512_mask: |
7004 | 0 | case X86::BI__builtin_ia32_fpclasssd_mask: |
7005 | 0 | case X86::BI__builtin_ia32_fpclassss_mask: |
7006 | 0 | case X86::BI__builtin_ia32_fpclasssh_mask: |
7007 | 0 | case X86::BI__builtin_ia32_pslldqi128_byteshift: |
7008 | 0 | case X86::BI__builtin_ia32_pslldqi256_byteshift: |
7009 | 0 | case X86::BI__builtin_ia32_pslldqi512_byteshift: |
7010 | 0 | case X86::BI__builtin_ia32_psrldqi128_byteshift: |
7011 | 0 | case X86::BI__builtin_ia32_psrldqi256_byteshift: |
7012 | 0 | case X86::BI__builtin_ia32_psrldqi512_byteshift: |
7013 | 0 | case X86::BI__builtin_ia32_kshiftliqi: |
7014 | 0 | case X86::BI__builtin_ia32_kshiftlihi: |
7015 | 0 | case X86::BI__builtin_ia32_kshiftlisi: |
7016 | 0 | case X86::BI__builtin_ia32_kshiftlidi: |
7017 | 0 | case X86::BI__builtin_ia32_kshiftriqi: |
7018 | 0 | case X86::BI__builtin_ia32_kshiftrihi: |
7019 | 0 | case X86::BI__builtin_ia32_kshiftrisi: |
7020 | 0 | case X86::BI__builtin_ia32_kshiftridi: |
7021 | 0 | i = 1; l = 0; u = 255; |
7022 | 0 | break; |
7023 | 0 | case X86::BI__builtin_ia32_vperm2f128_pd256: |
7024 | 0 | case X86::BI__builtin_ia32_vperm2f128_ps256: |
7025 | 0 | case X86::BI__builtin_ia32_vperm2f128_si256: |
7026 | 0 | case X86::BI__builtin_ia32_permti256: |
7027 | 0 | case X86::BI__builtin_ia32_pblendw128: |
7028 | 0 | case X86::BI__builtin_ia32_pblendw256: |
7029 | 0 | case X86::BI__builtin_ia32_blendps256: |
7030 | 0 | case X86::BI__builtin_ia32_pblendd256: |
7031 | 0 | case X86::BI__builtin_ia32_palignr128: |
7032 | 0 | case X86::BI__builtin_ia32_palignr256: |
7033 | 0 | case X86::BI__builtin_ia32_palignr512: |
7034 | 0 | case X86::BI__builtin_ia32_alignq512: |
7035 | 0 | case X86::BI__builtin_ia32_alignd512: |
7036 | 0 | case X86::BI__builtin_ia32_alignd128: |
7037 | 0 | case X86::BI__builtin_ia32_alignd256: |
7038 | 0 | case X86::BI__builtin_ia32_alignq128: |
7039 | 0 | case X86::BI__builtin_ia32_alignq256: |
7040 | 0 | case X86::BI__builtin_ia32_vcomisd: |
7041 | 0 | case X86::BI__builtin_ia32_vcomiss: |
7042 | 0 | case X86::BI__builtin_ia32_shuf_f32x4: |
7043 | 0 | case X86::BI__builtin_ia32_shuf_f64x2: |
7044 | 0 | case X86::BI__builtin_ia32_shuf_i32x4: |
7045 | 0 | case X86::BI__builtin_ia32_shuf_i64x2: |
7046 | 0 | case X86::BI__builtin_ia32_shufpd512: |
7047 | 0 | case X86::BI__builtin_ia32_shufps: |
7048 | 0 | case X86::BI__builtin_ia32_shufps256: |
7049 | 0 | case X86::BI__builtin_ia32_shufps512: |
7050 | 0 | case X86::BI__builtin_ia32_dbpsadbw128: |
7051 | 0 | case X86::BI__builtin_ia32_dbpsadbw256: |
7052 | 0 | case X86::BI__builtin_ia32_dbpsadbw512: |
7053 | 0 | case X86::BI__builtin_ia32_vpshldd128: |
7054 | 0 | case X86::BI__builtin_ia32_vpshldd256: |
7055 | 0 | case X86::BI__builtin_ia32_vpshldd512: |
7056 | 0 | case X86::BI__builtin_ia32_vpshldq128: |
7057 | 0 | case X86::BI__builtin_ia32_vpshldq256: |
7058 | 0 | case X86::BI__builtin_ia32_vpshldq512: |
7059 | 0 | case X86::BI__builtin_ia32_vpshldw128: |
7060 | 0 | case X86::BI__builtin_ia32_vpshldw256: |
7061 | 0 | case X86::BI__builtin_ia32_vpshldw512: |
7062 | 0 | case X86::BI__builtin_ia32_vpshrdd128: |
7063 | 0 | case X86::BI__builtin_ia32_vpshrdd256: |
7064 | 0 | case X86::BI__builtin_ia32_vpshrdd512: |
7065 | 0 | case X86::BI__builtin_ia32_vpshrdq128: |
7066 | 0 | case X86::BI__builtin_ia32_vpshrdq256: |
7067 | 0 | case X86::BI__builtin_ia32_vpshrdq512: |
7068 | 0 | case X86::BI__builtin_ia32_vpshrdw128: |
7069 | 0 | case X86::BI__builtin_ia32_vpshrdw256: |
7070 | 0 | case X86::BI__builtin_ia32_vpshrdw512: |
7071 | 0 | i = 2; l = 0; u = 255; |
7072 | 0 | break; |
7073 | 0 | case X86::BI__builtin_ia32_fixupimmpd512_mask: |
7074 | 0 | case X86::BI__builtin_ia32_fixupimmpd512_maskz: |
7075 | 0 | case X86::BI__builtin_ia32_fixupimmps512_mask: |
7076 | 0 | case X86::BI__builtin_ia32_fixupimmps512_maskz: |
7077 | 0 | case X86::BI__builtin_ia32_fixupimmsd_mask: |
7078 | 0 | case X86::BI__builtin_ia32_fixupimmsd_maskz: |
7079 | 0 | case X86::BI__builtin_ia32_fixupimmss_mask: |
7080 | 0 | case X86::BI__builtin_ia32_fixupimmss_maskz: |
7081 | 0 | case X86::BI__builtin_ia32_fixupimmpd128_mask: |
7082 | 0 | case X86::BI__builtin_ia32_fixupimmpd128_maskz: |
7083 | 0 | case X86::BI__builtin_ia32_fixupimmpd256_mask: |
7084 | 0 | case X86::BI__builtin_ia32_fixupimmpd256_maskz: |
7085 | 0 | case X86::BI__builtin_ia32_fixupimmps128_mask: |
7086 | 0 | case X86::BI__builtin_ia32_fixupimmps128_maskz: |
7087 | 0 | case X86::BI__builtin_ia32_fixupimmps256_mask: |
7088 | 0 | case X86::BI__builtin_ia32_fixupimmps256_maskz: |
7089 | 0 | case X86::BI__builtin_ia32_pternlogd512_mask: |
7090 | 0 | case X86::BI__builtin_ia32_pternlogd512_maskz: |
7091 | 0 | case X86::BI__builtin_ia32_pternlogq512_mask: |
7092 | 0 | case X86::BI__builtin_ia32_pternlogq512_maskz: |
7093 | 0 | case X86::BI__builtin_ia32_pternlogd128_mask: |
7094 | 0 | case X86::BI__builtin_ia32_pternlogd128_maskz: |
7095 | 0 | case X86::BI__builtin_ia32_pternlogd256_mask: |
7096 | 0 | case X86::BI__builtin_ia32_pternlogd256_maskz: |
7097 | 0 | case X86::BI__builtin_ia32_pternlogq128_mask: |
7098 | 0 | case X86::BI__builtin_ia32_pternlogq128_maskz: |
7099 | 0 | case X86::BI__builtin_ia32_pternlogq256_mask: |
7100 | 0 | case X86::BI__builtin_ia32_pternlogq256_maskz: |
7101 | 0 | case X86::BI__builtin_ia32_vsm3rnds2: |
7102 | 0 | i = 3; l = 0; u = 255; |
7103 | 0 | break; |
7104 | 0 | case X86::BI__builtin_ia32_gatherpfdpd: |
7105 | 0 | case X86::BI__builtin_ia32_gatherpfdps: |
7106 | 0 | case X86::BI__builtin_ia32_gatherpfqpd: |
7107 | 0 | case X86::BI__builtin_ia32_gatherpfqps: |
7108 | 0 | case X86::BI__builtin_ia32_scatterpfdpd: |
7109 | 0 | case X86::BI__builtin_ia32_scatterpfdps: |
7110 | 0 | case X86::BI__builtin_ia32_scatterpfqpd: |
7111 | 0 | case X86::BI__builtin_ia32_scatterpfqps: |
7112 | 0 | i = 4; l = 2; u = 3; |
7113 | 0 | break; |
7114 | 0 | case X86::BI__builtin_ia32_reducesd_mask: |
7115 | 0 | case X86::BI__builtin_ia32_reducess_mask: |
7116 | 0 | case X86::BI__builtin_ia32_rndscalesd_round_mask: |
7117 | 0 | case X86::BI__builtin_ia32_rndscaless_round_mask: |
7118 | 0 | case X86::BI__builtin_ia32_rndscalesh_round_mask: |
7119 | 0 | case X86::BI__builtin_ia32_reducesh_mask: |
7120 | 0 | i = 4; l = 0; u = 255; |
7121 | 0 | break; |
7122 | 0 | case X86::BI__builtin_ia32_cmpccxadd32: |
7123 | 0 | case X86::BI__builtin_ia32_cmpccxadd64: |
7124 | 0 | i = 3; l = 0; u = 15; |
7125 | 0 | break; |
7126 | 0 | } |
7127 | | |
7128 | | // Note that we don't force a hard error on the range check here, allowing |
7129 | | // template-generated or macro-generated dead code to potentially have out-of- |
7130 | | // range values. These need to code generate, but don't need to necessarily |
7131 | | // make any sense. We use a warning that defaults to an error. |
7132 | 0 | return SemaBuiltinConstantArgRange(TheCall, i, l, u, /*RangeIsError*/ false); |
7133 | 0 | } |
7134 | | |
7135 | | /// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo |
7136 | | /// parameter with the FormatAttr's correct format_idx and firstDataArg. |
7137 | | /// Returns true when the format fits the function and the FormatStringInfo has |
7138 | | /// been populated. |
7139 | | bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember, |
7140 | 0 | bool IsVariadic, FormatStringInfo *FSI) { |
7141 | 0 | if (Format->getFirstArg() == 0) |
7142 | 0 | FSI->ArgPassingKind = FAPK_VAList; |
7143 | 0 | else if (IsVariadic) |
7144 | 0 | FSI->ArgPassingKind = FAPK_Variadic; |
7145 | 0 | else |
7146 | 0 | FSI->ArgPassingKind = FAPK_Fixed; |
7147 | 0 | FSI->FormatIdx = Format->getFormatIdx() - 1; |
7148 | 0 | FSI->FirstDataArg = |
7149 | 0 | FSI->ArgPassingKind == FAPK_VAList ? 0 : Format->getFirstArg() - 1; |
7150 | | |
7151 | | // The way the format attribute works in GCC, the implicit this argument |
7152 | | // of member functions is counted. However, it doesn't appear in our own |
7153 | | // lists, so decrement format_idx in that case. |
7154 | 0 | if (IsCXXMember) { |
7155 | 0 | if(FSI->FormatIdx == 0) |
7156 | 0 | return false; |
7157 | 0 | --FSI->FormatIdx; |
7158 | 0 | if (FSI->FirstDataArg != 0) |
7159 | 0 | --FSI->FirstDataArg; |
7160 | 0 | } |
7161 | 0 | return true; |
7162 | 0 | } |
7163 | | |
7164 | | /// Checks if a the given expression evaluates to null. |
7165 | | /// |
7166 | | /// Returns true if the value evaluates to null. |
7167 | 0 | static bool CheckNonNullExpr(Sema &S, const Expr *Expr) { |
7168 | | // If the expression has non-null type, it doesn't evaluate to null. |
7169 | 0 | if (auto nullability = Expr->IgnoreImplicit()->getType()->getNullability()) { |
7170 | 0 | if (*nullability == NullabilityKind::NonNull) |
7171 | 0 | return false; |
7172 | 0 | } |
7173 | | |
7174 | | // As a special case, transparent unions initialized with zero are |
7175 | | // considered null for the purposes of the nonnull attribute. |
7176 | 0 | if (const RecordType *UT = Expr->getType()->getAsUnionType()) { |
7177 | 0 | if (UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
7178 | 0 | if (const CompoundLiteralExpr *CLE = |
7179 | 0 | dyn_cast<CompoundLiteralExpr>(Expr)) |
7180 | 0 | if (const InitListExpr *ILE = |
7181 | 0 | dyn_cast<InitListExpr>(CLE->getInitializer())) |
7182 | 0 | Expr = ILE->getInit(0); |
7183 | 0 | } |
7184 | |
|
7185 | 0 | bool Result; |
7186 | 0 | return (!Expr->isValueDependent() && |
7187 | 0 | Expr->EvaluateAsBooleanCondition(Result, S.Context) && |
7188 | 0 | !Result); |
7189 | 0 | } |
7190 | | |
7191 | | static void CheckNonNullArgument(Sema &S, |
7192 | | const Expr *ArgExpr, |
7193 | 0 | SourceLocation CallSiteLoc) { |
7194 | 0 | if (CheckNonNullExpr(S, ArgExpr)) |
7195 | 0 | S.DiagRuntimeBehavior(CallSiteLoc, ArgExpr, |
7196 | 0 | S.PDiag(diag::warn_null_arg) |
7197 | 0 | << ArgExpr->getSourceRange()); |
7198 | 0 | } |
7199 | | |
7200 | 0 | bool Sema::GetFormatNSStringIdx(const FormatAttr *Format, unsigned &Idx) { |
7201 | 0 | FormatStringInfo FSI; |
7202 | 0 | if ((GetFormatStringType(Format) == FST_NSString) && |
7203 | 0 | getFormatStringInfo(Format, false, true, &FSI)) { |
7204 | 0 | Idx = FSI.FormatIdx; |
7205 | 0 | return true; |
7206 | 0 | } |
7207 | 0 | return false; |
7208 | 0 | } |
7209 | | |
7210 | | /// Diagnose use of %s directive in an NSString which is being passed |
7211 | | /// as formatting string to formatting method. |
7212 | | static void |
7213 | | DiagnoseCStringFormatDirectiveInCFAPI(Sema &S, |
7214 | | const NamedDecl *FDecl, |
7215 | | Expr **Args, |
7216 | 0 | unsigned NumArgs) { |
7217 | 0 | unsigned Idx = 0; |
7218 | 0 | bool Format = false; |
7219 | 0 | ObjCStringFormatFamily SFFamily = FDecl->getObjCFStringFormattingFamily(); |
7220 | 0 | if (SFFamily == ObjCStringFormatFamily::SFF_CFString) { |
7221 | 0 | Idx = 2; |
7222 | 0 | Format = true; |
7223 | 0 | } |
7224 | 0 | else |
7225 | 0 | for (const auto *I : FDecl->specific_attrs<FormatAttr>()) { |
7226 | 0 | if (S.GetFormatNSStringIdx(I, Idx)) { |
7227 | 0 | Format = true; |
7228 | 0 | break; |
7229 | 0 | } |
7230 | 0 | } |
7231 | 0 | if (!Format || NumArgs <= Idx) |
7232 | 0 | return; |
7233 | 0 | const Expr *FormatExpr = Args[Idx]; |
7234 | 0 | if (const CStyleCastExpr *CSCE = dyn_cast<CStyleCastExpr>(FormatExpr)) |
7235 | 0 | FormatExpr = CSCE->getSubExpr(); |
7236 | 0 | const StringLiteral *FormatString; |
7237 | 0 | if (const ObjCStringLiteral *OSL = |
7238 | 0 | dyn_cast<ObjCStringLiteral>(FormatExpr->IgnoreParenImpCasts())) |
7239 | 0 | FormatString = OSL->getString(); |
7240 | 0 | else |
7241 | 0 | FormatString = dyn_cast<StringLiteral>(FormatExpr->IgnoreParenImpCasts()); |
7242 | 0 | if (!FormatString) |
7243 | 0 | return; |
7244 | 0 | if (S.FormatStringHasSArg(FormatString)) { |
7245 | 0 | S.Diag(FormatExpr->getExprLoc(), diag::warn_objc_cdirective_format_string) |
7246 | 0 | << "%s" << 1 << 1; |
7247 | 0 | S.Diag(FDecl->getLocation(), diag::note_entity_declared_at) |
7248 | 0 | << FDecl->getDeclName(); |
7249 | 0 | } |
7250 | 0 | } |
7251 | | |
7252 | | /// Determine whether the given type has a non-null nullability annotation. |
7253 | 0 | static bool isNonNullType(QualType type) { |
7254 | 0 | if (auto nullability = type->getNullability()) |
7255 | 0 | return *nullability == NullabilityKind::NonNull; |
7256 | | |
7257 | 0 | return false; |
7258 | 0 | } |
7259 | | |
7260 | | static void CheckNonNullArguments(Sema &S, |
7261 | | const NamedDecl *FDecl, |
7262 | | const FunctionProtoType *Proto, |
7263 | | ArrayRef<const Expr *> Args, |
7264 | 0 | SourceLocation CallSiteLoc) { |
7265 | 0 | assert((FDecl || Proto) && "Need a function declaration or prototype"); |
7266 | | |
7267 | | // Already checked by constant evaluator. |
7268 | 0 | if (S.isConstantEvaluatedContext()) |
7269 | 0 | return; |
7270 | | // Check the attributes attached to the method/function itself. |
7271 | 0 | llvm::SmallBitVector NonNullArgs; |
7272 | 0 | if (FDecl) { |
7273 | | // Handle the nonnull attribute on the function/method declaration itself. |
7274 | 0 | for (const auto *NonNull : FDecl->specific_attrs<NonNullAttr>()) { |
7275 | 0 | if (!NonNull->args_size()) { |
7276 | | // Easy case: all pointer arguments are nonnull. |
7277 | 0 | for (const auto *Arg : Args) |
7278 | 0 | if (S.isValidPointerAttrType(Arg->getType())) |
7279 | 0 | CheckNonNullArgument(S, Arg, CallSiteLoc); |
7280 | 0 | return; |
7281 | 0 | } |
7282 | | |
7283 | 0 | for (const ParamIdx &Idx : NonNull->args()) { |
7284 | 0 | unsigned IdxAST = Idx.getASTIndex(); |
7285 | 0 | if (IdxAST >= Args.size()) |
7286 | 0 | continue; |
7287 | 0 | if (NonNullArgs.empty()) |
7288 | 0 | NonNullArgs.resize(Args.size()); |
7289 | 0 | NonNullArgs.set(IdxAST); |
7290 | 0 | } |
7291 | 0 | } |
7292 | 0 | } |
7293 | | |
7294 | 0 | if (FDecl && (isa<FunctionDecl>(FDecl) || isa<ObjCMethodDecl>(FDecl))) { |
7295 | | // Handle the nonnull attribute on the parameters of the |
7296 | | // function/method. |
7297 | 0 | ArrayRef<ParmVarDecl*> parms; |
7298 | 0 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FDecl)) |
7299 | 0 | parms = FD->parameters(); |
7300 | 0 | else |
7301 | 0 | parms = cast<ObjCMethodDecl>(FDecl)->parameters(); |
7302 | |
|
7303 | 0 | unsigned ParamIndex = 0; |
7304 | 0 | for (ArrayRef<ParmVarDecl*>::iterator I = parms.begin(), E = parms.end(); |
7305 | 0 | I != E; ++I, ++ParamIndex) { |
7306 | 0 | const ParmVarDecl *PVD = *I; |
7307 | 0 | if (PVD->hasAttr<NonNullAttr>() || isNonNullType(PVD->getType())) { |
7308 | 0 | if (NonNullArgs.empty()) |
7309 | 0 | NonNullArgs.resize(Args.size()); |
7310 | |
|
7311 | 0 | NonNullArgs.set(ParamIndex); |
7312 | 0 | } |
7313 | 0 | } |
7314 | 0 | } else { |
7315 | | // If we have a non-function, non-method declaration but no |
7316 | | // function prototype, try to dig out the function prototype. |
7317 | 0 | if (!Proto) { |
7318 | 0 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(FDecl)) { |
7319 | 0 | QualType type = VD->getType().getNonReferenceType(); |
7320 | 0 | if (auto pointerType = type->getAs<PointerType>()) |
7321 | 0 | type = pointerType->getPointeeType(); |
7322 | 0 | else if (auto blockType = type->getAs<BlockPointerType>()) |
7323 | 0 | type = blockType->getPointeeType(); |
7324 | | // FIXME: data member pointers? |
7325 | | |
7326 | | // Dig out the function prototype, if there is one. |
7327 | 0 | Proto = type->getAs<FunctionProtoType>(); |
7328 | 0 | } |
7329 | 0 | } |
7330 | | |
7331 | | // Fill in non-null argument information from the nullability |
7332 | | // information on the parameter types (if we have them). |
7333 | 0 | if (Proto) { |
7334 | 0 | unsigned Index = 0; |
7335 | 0 | for (auto paramType : Proto->getParamTypes()) { |
7336 | 0 | if (isNonNullType(paramType)) { |
7337 | 0 | if (NonNullArgs.empty()) |
7338 | 0 | NonNullArgs.resize(Args.size()); |
7339 | |
|
7340 | 0 | NonNullArgs.set(Index); |
7341 | 0 | } |
7342 | |
|
7343 | 0 | ++Index; |
7344 | 0 | } |
7345 | 0 | } |
7346 | 0 | } |
7347 | | |
7348 | | // Check for non-null arguments. |
7349 | 0 | for (unsigned ArgIndex = 0, ArgIndexEnd = NonNullArgs.size(); |
7350 | 0 | ArgIndex != ArgIndexEnd; ++ArgIndex) { |
7351 | 0 | if (NonNullArgs[ArgIndex]) |
7352 | 0 | CheckNonNullArgument(S, Args[ArgIndex], Args[ArgIndex]->getExprLoc()); |
7353 | 0 | } |
7354 | 0 | } |
7355 | | |
7356 | | // 16 byte ByVal alignment not due to a vector member is not honoured by XL |
7357 | | // on AIX. Emit a warning here that users are generating binary incompatible |
7358 | | // code to be safe. |
7359 | | // Here we try to get information about the alignment of the struct member |
7360 | | // from the struct passed to the caller function. We only warn when the struct |
7361 | | // is passed byval, hence the series of checks and early returns if we are a not |
7362 | | // passing a struct byval. |
7363 | 0 | void Sema::checkAIXMemberAlignment(SourceLocation Loc, const Expr *Arg) { |
7364 | 0 | const auto *ICE = dyn_cast<ImplicitCastExpr>(Arg->IgnoreParens()); |
7365 | 0 | if (!ICE) |
7366 | 0 | return; |
7367 | | |
7368 | 0 | const auto *DR = dyn_cast<DeclRefExpr>(ICE->getSubExpr()); |
7369 | 0 | if (!DR) |
7370 | 0 | return; |
7371 | | |
7372 | 0 | const auto *PD = dyn_cast<ParmVarDecl>(DR->getDecl()); |
7373 | 0 | if (!PD || !PD->getType()->isRecordType()) |
7374 | 0 | return; |
7375 | | |
7376 | 0 | QualType ArgType = Arg->getType(); |
7377 | 0 | for (const FieldDecl *FD : |
7378 | 0 | ArgType->castAs<RecordType>()->getDecl()->fields()) { |
7379 | 0 | if (const auto *AA = FD->getAttr<AlignedAttr>()) { |
7380 | 0 | CharUnits Alignment = |
7381 | 0 | Context.toCharUnitsFromBits(AA->getAlignment(Context)); |
7382 | 0 | if (Alignment.getQuantity() == 16) { |
7383 | 0 | Diag(FD->getLocation(), diag::warn_not_xl_compatible) << FD; |
7384 | 0 | Diag(Loc, diag::note_misaligned_member_used_here) << PD; |
7385 | 0 | } |
7386 | 0 | } |
7387 | 0 | } |
7388 | 0 | } |
7389 | | |
7390 | | /// Warn if a pointer or reference argument passed to a function points to an |
7391 | | /// object that is less aligned than the parameter. This can happen when |
7392 | | /// creating a typedef with a lower alignment than the original type and then |
7393 | | /// calling functions defined in terms of the original type. |
7394 | | void Sema::CheckArgAlignment(SourceLocation Loc, NamedDecl *FDecl, |
7395 | | StringRef ParamName, QualType ArgTy, |
7396 | 0 | QualType ParamTy) { |
7397 | | |
7398 | | // If a function accepts a pointer or reference type |
7399 | 0 | if (!ParamTy->isPointerType() && !ParamTy->isReferenceType()) |
7400 | 0 | return; |
7401 | | |
7402 | | // If the parameter is a pointer type, get the pointee type for the |
7403 | | // argument too. If the parameter is a reference type, don't try to get |
7404 | | // the pointee type for the argument. |
7405 | 0 | if (ParamTy->isPointerType()) |
7406 | 0 | ArgTy = ArgTy->getPointeeType(); |
7407 | | |
7408 | | // Remove reference or pointer |
7409 | 0 | ParamTy = ParamTy->getPointeeType(); |
7410 | | |
7411 | | // Find expected alignment, and the actual alignment of the passed object. |
7412 | | // getTypeAlignInChars requires complete types |
7413 | 0 | if (ArgTy.isNull() || ParamTy->isDependentType() || |
7414 | 0 | ParamTy->isIncompleteType() || ArgTy->isIncompleteType() || |
7415 | 0 | ParamTy->isUndeducedType() || ArgTy->isUndeducedType()) |
7416 | 0 | return; |
7417 | | |
7418 | 0 | CharUnits ParamAlign = Context.getTypeAlignInChars(ParamTy); |
7419 | 0 | CharUnits ArgAlign = Context.getTypeAlignInChars(ArgTy); |
7420 | | |
7421 | | // If the argument is less aligned than the parameter, there is a |
7422 | | // potential alignment issue. |
7423 | 0 | if (ArgAlign < ParamAlign) |
7424 | 0 | Diag(Loc, diag::warn_param_mismatched_alignment) |
7425 | 0 | << (int)ArgAlign.getQuantity() << (int)ParamAlign.getQuantity() |
7426 | 0 | << ParamName << (FDecl != nullptr) << FDecl; |
7427 | 0 | } |
7428 | | |
7429 | | /// Handles the checks for format strings, non-POD arguments to vararg |
7430 | | /// functions, NULL arguments passed to non-NULL parameters, diagnose_if |
7431 | | /// attributes and AArch64 SME attributes. |
7432 | | void Sema::checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto, |
7433 | | const Expr *ThisArg, ArrayRef<const Expr *> Args, |
7434 | | bool IsMemberFunction, SourceLocation Loc, |
7435 | 0 | SourceRange Range, VariadicCallType CallType) { |
7436 | | // FIXME: We should check as much as we can in the template definition. |
7437 | 0 | if (CurContext->isDependentContext()) |
7438 | 0 | return; |
7439 | | |
7440 | | // Printf and scanf checking. |
7441 | 0 | llvm::SmallBitVector CheckedVarArgs; |
7442 | 0 | if (FDecl) { |
7443 | 0 | for (const auto *I : FDecl->specific_attrs<FormatAttr>()) { |
7444 | | // Only create vector if there are format attributes. |
7445 | 0 | CheckedVarArgs.resize(Args.size()); |
7446 | |
|
7447 | 0 | CheckFormatArguments(I, Args, IsMemberFunction, CallType, Loc, Range, |
7448 | 0 | CheckedVarArgs); |
7449 | 0 | } |
7450 | 0 | } |
7451 | | |
7452 | | // Refuse POD arguments that weren't caught by the format string |
7453 | | // checks above. |
7454 | 0 | auto *FD = dyn_cast_or_null<FunctionDecl>(FDecl); |
7455 | 0 | if (CallType != VariadicDoesNotApply && |
7456 | 0 | (!FD || FD->getBuiltinID() != Builtin::BI__noop)) { |
7457 | 0 | unsigned NumParams = Proto ? Proto->getNumParams() |
7458 | 0 | : FDecl && isa<FunctionDecl>(FDecl) |
7459 | 0 | ? cast<FunctionDecl>(FDecl)->getNumParams() |
7460 | 0 | : FDecl && isa<ObjCMethodDecl>(FDecl) |
7461 | 0 | ? cast<ObjCMethodDecl>(FDecl)->param_size() |
7462 | 0 | : 0; |
7463 | |
|
7464 | 0 | for (unsigned ArgIdx = NumParams; ArgIdx < Args.size(); ++ArgIdx) { |
7465 | | // Args[ArgIdx] can be null in malformed code. |
7466 | 0 | if (const Expr *Arg = Args[ArgIdx]) { |
7467 | 0 | if (CheckedVarArgs.empty() || !CheckedVarArgs[ArgIdx]) |
7468 | 0 | checkVariadicArgument(Arg, CallType); |
7469 | 0 | } |
7470 | 0 | } |
7471 | 0 | } |
7472 | |
|
7473 | 0 | if (FDecl || Proto) { |
7474 | 0 | CheckNonNullArguments(*this, FDecl, Proto, Args, Loc); |
7475 | | |
7476 | | // Type safety checking. |
7477 | 0 | if (FDecl) { |
7478 | 0 | for (const auto *I : FDecl->specific_attrs<ArgumentWithTypeTagAttr>()) |
7479 | 0 | CheckArgumentWithTypeTag(I, Args, Loc); |
7480 | 0 | } |
7481 | 0 | } |
7482 | | |
7483 | | // Check that passed arguments match the alignment of original arguments. |
7484 | | // Try to get the missing prototype from the declaration. |
7485 | 0 | if (!Proto && FDecl) { |
7486 | 0 | const auto *FT = FDecl->getFunctionType(); |
7487 | 0 | if (isa_and_nonnull<FunctionProtoType>(FT)) |
7488 | 0 | Proto = cast<FunctionProtoType>(FDecl->getFunctionType()); |
7489 | 0 | } |
7490 | 0 | if (Proto) { |
7491 | | // For variadic functions, we may have more args than parameters. |
7492 | | // For some K&R functions, we may have less args than parameters. |
7493 | 0 | const auto N = std::min<unsigned>(Proto->getNumParams(), Args.size()); |
7494 | 0 | for (unsigned ArgIdx = 0; ArgIdx < N; ++ArgIdx) { |
7495 | | // Args[ArgIdx] can be null in malformed code. |
7496 | 0 | if (const Expr *Arg = Args[ArgIdx]) { |
7497 | 0 | if (Arg->containsErrors()) |
7498 | 0 | continue; |
7499 | | |
7500 | 0 | if (Context.getTargetInfo().getTriple().isOSAIX() && FDecl && Arg && |
7501 | 0 | FDecl->hasLinkage() && |
7502 | 0 | FDecl->getFormalLinkage() != Linkage::Internal && |
7503 | 0 | CallType == VariadicDoesNotApply) |
7504 | 0 | checkAIXMemberAlignment((Arg->getExprLoc()), Arg); |
7505 | |
|
7506 | 0 | QualType ParamTy = Proto->getParamType(ArgIdx); |
7507 | 0 | QualType ArgTy = Arg->getType(); |
7508 | 0 | CheckArgAlignment(Arg->getExprLoc(), FDecl, std::to_string(ArgIdx + 1), |
7509 | 0 | ArgTy, ParamTy); |
7510 | 0 | } |
7511 | 0 | } |
7512 | | |
7513 | | // If the callee has an AArch64 SME attribute to indicate that it is an |
7514 | | // __arm_streaming function, then the caller requires SME to be available. |
7515 | 0 | FunctionProtoType::ExtProtoInfo ExtInfo = Proto->getExtProtoInfo(); |
7516 | 0 | if (ExtInfo.AArch64SMEAttributes & FunctionType::SME_PStateSMEnabledMask) { |
7517 | 0 | if (auto *CallerFD = dyn_cast<FunctionDecl>(CurContext)) { |
7518 | 0 | llvm::StringMap<bool> CallerFeatureMap; |
7519 | 0 | Context.getFunctionFeatureMap(CallerFeatureMap, CallerFD); |
7520 | 0 | if (!CallerFeatureMap.contains("sme")) |
7521 | 0 | Diag(Loc, diag::err_sme_call_in_non_sme_target); |
7522 | 0 | } else if (!Context.getTargetInfo().hasFeature("sme")) { |
7523 | 0 | Diag(Loc, diag::err_sme_call_in_non_sme_target); |
7524 | 0 | } |
7525 | 0 | } |
7526 | | |
7527 | | // If the callee uses AArch64 SME ZA state but the caller doesn't define |
7528 | | // any, then this is an error. |
7529 | 0 | FunctionType::ArmStateValue ArmZAState = |
7530 | 0 | FunctionType::getArmZAState(ExtInfo.AArch64SMEAttributes); |
7531 | 0 | if (ArmZAState != FunctionType::ARM_None) { |
7532 | 0 | bool CallerHasZAState = false; |
7533 | 0 | if (const auto *CallerFD = dyn_cast<FunctionDecl>(CurContext)) { |
7534 | 0 | auto *Attr = CallerFD->getAttr<ArmNewAttr>(); |
7535 | 0 | if (Attr && Attr->isNewZA()) |
7536 | 0 | CallerHasZAState = true; |
7537 | 0 | else if (const auto *FPT = |
7538 | 0 | CallerFD->getType()->getAs<FunctionProtoType>()) |
7539 | 0 | CallerHasZAState = FunctionType::getArmZAState( |
7540 | 0 | FPT->getExtProtoInfo().AArch64SMEAttributes) != |
7541 | 0 | FunctionType::ARM_None; |
7542 | 0 | } |
7543 | |
|
7544 | 0 | if (!CallerHasZAState) |
7545 | 0 | Diag(Loc, diag::err_sme_za_call_no_za_state); |
7546 | 0 | } |
7547 | 0 | } |
7548 | |
|
7549 | 0 | if (FDecl && FDecl->hasAttr<AllocAlignAttr>()) { |
7550 | 0 | auto *AA = FDecl->getAttr<AllocAlignAttr>(); |
7551 | 0 | const Expr *Arg = Args[AA->getParamIndex().getASTIndex()]; |
7552 | 0 | if (!Arg->isValueDependent()) { |
7553 | 0 | Expr::EvalResult Align; |
7554 | 0 | if (Arg->EvaluateAsInt(Align, Context)) { |
7555 | 0 | const llvm::APSInt &I = Align.Val.getInt(); |
7556 | 0 | if (!I.isPowerOf2()) |
7557 | 0 | Diag(Arg->getExprLoc(), diag::warn_alignment_not_power_of_two) |
7558 | 0 | << Arg->getSourceRange(); |
7559 | |
|
7560 | 0 | if (I > Sema::MaximumAlignment) |
7561 | 0 | Diag(Arg->getExprLoc(), diag::warn_assume_aligned_too_great) |
7562 | 0 | << Arg->getSourceRange() << Sema::MaximumAlignment; |
7563 | 0 | } |
7564 | 0 | } |
7565 | 0 | } |
7566 | |
|
7567 | 0 | if (FD) |
7568 | 0 | diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc); |
7569 | 0 | } |
7570 | | |
7571 | | /// CheckConstructorCall - Check a constructor call for correctness and safety |
7572 | | /// properties not enforced by the C type system. |
7573 | | void Sema::CheckConstructorCall(FunctionDecl *FDecl, QualType ThisType, |
7574 | | ArrayRef<const Expr *> Args, |
7575 | | const FunctionProtoType *Proto, |
7576 | 0 | SourceLocation Loc) { |
7577 | 0 | VariadicCallType CallType = |
7578 | 0 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
7579 | |
|
7580 | 0 | auto *Ctor = cast<CXXConstructorDecl>(FDecl); |
7581 | 0 | CheckArgAlignment( |
7582 | 0 | Loc, FDecl, "'this'", Context.getPointerType(ThisType), |
7583 | 0 | Context.getPointerType(Ctor->getFunctionObjectParameterType())); |
7584 | |
|
7585 | 0 | checkCall(FDecl, Proto, /*ThisArg=*/nullptr, Args, /*IsMemberFunction=*/true, |
7586 | 0 | Loc, SourceRange(), CallType); |
7587 | 0 | } |
7588 | | |
7589 | | /// CheckFunctionCall - Check a direct function call for various correctness |
7590 | | /// and safety properties not strictly enforced by the C type system. |
7591 | | bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall, |
7592 | 0 | const FunctionProtoType *Proto) { |
7593 | 0 | bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) && |
7594 | 0 | isa<CXXMethodDecl>(FDecl); |
7595 | 0 | bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) || |
7596 | 0 | IsMemberOperatorCall; |
7597 | 0 | VariadicCallType CallType = getVariadicCallType(FDecl, Proto, |
7598 | 0 | TheCall->getCallee()); |
7599 | 0 | Expr** Args = TheCall->getArgs(); |
7600 | 0 | unsigned NumArgs = TheCall->getNumArgs(); |
7601 | |
|
7602 | 0 | Expr *ImplicitThis = nullptr; |
7603 | 0 | if (IsMemberOperatorCall && !FDecl->isStatic() && |
7604 | 0 | !FDecl->hasCXXExplicitFunctionObjectParameter()) { |
7605 | | // If this is a call to a non-static member operator, hide the first |
7606 | | // argument from checkCall. |
7607 | | // FIXME: Our choice of AST representation here is less than ideal. |
7608 | 0 | ImplicitThis = Args[0]; |
7609 | 0 | ++Args; |
7610 | 0 | --NumArgs; |
7611 | 0 | } else if (IsMemberFunction && !FDecl->isStatic() && |
7612 | 0 | !FDecl->hasCXXExplicitFunctionObjectParameter()) |
7613 | 0 | ImplicitThis = |
7614 | 0 | cast<CXXMemberCallExpr>(TheCall)->getImplicitObjectArgument(); |
7615 | |
|
7616 | 0 | if (ImplicitThis) { |
7617 | | // ImplicitThis may or may not be a pointer, depending on whether . or -> is |
7618 | | // used. |
7619 | 0 | QualType ThisType = ImplicitThis->getType(); |
7620 | 0 | if (!ThisType->isPointerType()) { |
7621 | 0 | assert(!ThisType->isReferenceType()); |
7622 | 0 | ThisType = Context.getPointerType(ThisType); |
7623 | 0 | } |
7624 | | |
7625 | 0 | QualType ThisTypeFromDecl = Context.getPointerType( |
7626 | 0 | cast<CXXMethodDecl>(FDecl)->getFunctionObjectParameterType()); |
7627 | |
|
7628 | 0 | CheckArgAlignment(TheCall->getRParenLoc(), FDecl, "'this'", ThisType, |
7629 | 0 | ThisTypeFromDecl); |
7630 | 0 | } |
7631 | | |
7632 | 0 | checkCall(FDecl, Proto, ImplicitThis, llvm::ArrayRef(Args, NumArgs), |
7633 | 0 | IsMemberFunction, TheCall->getRParenLoc(), |
7634 | 0 | TheCall->getCallee()->getSourceRange(), CallType); |
7635 | |
|
7636 | 0 | IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
7637 | | // None of the checks below are needed for functions that don't have |
7638 | | // simple names (e.g., C++ conversion functions). |
7639 | 0 | if (!FnInfo) |
7640 | 0 | return false; |
7641 | | |
7642 | | // Enforce TCB except for builtin calls, which are always allowed. |
7643 | 0 | if (FDecl->getBuiltinID() == 0) |
7644 | 0 | CheckTCBEnforcement(TheCall->getExprLoc(), FDecl); |
7645 | |
|
7646 | 0 | CheckAbsoluteValueFunction(TheCall, FDecl); |
7647 | 0 | CheckMaxUnsignedZero(TheCall, FDecl); |
7648 | |
|
7649 | 0 | if (getLangOpts().ObjC) |
7650 | 0 | DiagnoseCStringFormatDirectiveInCFAPI(*this, FDecl, Args, NumArgs); |
7651 | |
|
7652 | 0 | unsigned CMId = FDecl->getMemoryFunctionKind(); |
7653 | | |
7654 | | // Handle memory setting and copying functions. |
7655 | 0 | switch (CMId) { |
7656 | 0 | case 0: |
7657 | 0 | return false; |
7658 | 0 | case Builtin::BIstrlcpy: // fallthrough |
7659 | 0 | case Builtin::BIstrlcat: |
7660 | 0 | CheckStrlcpycatArguments(TheCall, FnInfo); |
7661 | 0 | break; |
7662 | 0 | case Builtin::BIstrncat: |
7663 | 0 | CheckStrncatArguments(TheCall, FnInfo); |
7664 | 0 | break; |
7665 | 0 | case Builtin::BIfree: |
7666 | 0 | CheckFreeArguments(TheCall); |
7667 | 0 | break; |
7668 | 0 | default: |
7669 | 0 | CheckMemaccessArguments(TheCall, CMId, FnInfo); |
7670 | 0 | } |
7671 | | |
7672 | 0 | return false; |
7673 | 0 | } |
7674 | | |
7675 | | bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac, |
7676 | 0 | ArrayRef<const Expr *> Args) { |
7677 | 0 | VariadicCallType CallType = |
7678 | 0 | Method->isVariadic() ? VariadicMethod : VariadicDoesNotApply; |
7679 | |
|
7680 | 0 | checkCall(Method, nullptr, /*ThisArg=*/nullptr, Args, |
7681 | 0 | /*IsMemberFunction=*/false, lbrac, Method->getSourceRange(), |
7682 | 0 | CallType); |
7683 | |
|
7684 | 0 | CheckTCBEnforcement(lbrac, Method); |
7685 | |
|
7686 | 0 | return false; |
7687 | 0 | } |
7688 | | |
7689 | | bool Sema::CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall, |
7690 | 0 | const FunctionProtoType *Proto) { |
7691 | 0 | QualType Ty; |
7692 | 0 | if (const auto *V = dyn_cast<VarDecl>(NDecl)) |
7693 | 0 | Ty = V->getType().getNonReferenceType(); |
7694 | 0 | else if (const auto *F = dyn_cast<FieldDecl>(NDecl)) |
7695 | 0 | Ty = F->getType().getNonReferenceType(); |
7696 | 0 | else |
7697 | 0 | return false; |
7698 | | |
7699 | 0 | if (!Ty->isBlockPointerType() && !Ty->isFunctionPointerType() && |
7700 | 0 | !Ty->isFunctionProtoType()) |
7701 | 0 | return false; |
7702 | | |
7703 | 0 | VariadicCallType CallType; |
7704 | 0 | if (!Proto || !Proto->isVariadic()) { |
7705 | 0 | CallType = VariadicDoesNotApply; |
7706 | 0 | } else if (Ty->isBlockPointerType()) { |
7707 | 0 | CallType = VariadicBlock; |
7708 | 0 | } else { // Ty->isFunctionPointerType() |
7709 | 0 | CallType = VariadicFunction; |
7710 | 0 | } |
7711 | |
|
7712 | 0 | checkCall(NDecl, Proto, /*ThisArg=*/nullptr, |
7713 | 0 | llvm::ArrayRef(TheCall->getArgs(), TheCall->getNumArgs()), |
7714 | 0 | /*IsMemberFunction=*/false, TheCall->getRParenLoc(), |
7715 | 0 | TheCall->getCallee()->getSourceRange(), CallType); |
7716 | |
|
7717 | 0 | return false; |
7718 | 0 | } |
7719 | | |
7720 | | /// Checks function calls when a FunctionDecl or a NamedDecl is not available, |
7721 | | /// such as function pointers returned from functions. |
7722 | 0 | bool Sema::CheckOtherCall(CallExpr *TheCall, const FunctionProtoType *Proto) { |
7723 | 0 | VariadicCallType CallType = getVariadicCallType(/*FDecl=*/nullptr, Proto, |
7724 | 0 | TheCall->getCallee()); |
7725 | 0 | checkCall(/*FDecl=*/nullptr, Proto, /*ThisArg=*/nullptr, |
7726 | 0 | llvm::ArrayRef(TheCall->getArgs(), TheCall->getNumArgs()), |
7727 | 0 | /*IsMemberFunction=*/false, TheCall->getRParenLoc(), |
7728 | 0 | TheCall->getCallee()->getSourceRange(), CallType); |
7729 | |
|
7730 | 0 | return false; |
7731 | 0 | } |
7732 | | |
7733 | 0 | static bool isValidOrderingForOp(int64_t Ordering, AtomicExpr::AtomicOp Op) { |
7734 | 0 | if (!llvm::isValidAtomicOrderingCABI(Ordering)) |
7735 | 0 | return false; |
7736 | | |
7737 | 0 | auto OrderingCABI = (llvm::AtomicOrderingCABI)Ordering; |
7738 | 0 | switch (Op) { |
7739 | 0 | case AtomicExpr::AO__c11_atomic_init: |
7740 | 0 | case AtomicExpr::AO__opencl_atomic_init: |
7741 | 0 | llvm_unreachable("There is no ordering argument for an init"); |
7742 | |
|
7743 | 0 | case AtomicExpr::AO__c11_atomic_load: |
7744 | 0 | case AtomicExpr::AO__opencl_atomic_load: |
7745 | 0 | case AtomicExpr::AO__hip_atomic_load: |
7746 | 0 | case AtomicExpr::AO__atomic_load_n: |
7747 | 0 | case AtomicExpr::AO__atomic_load: |
7748 | 0 | case AtomicExpr::AO__scoped_atomic_load_n: |
7749 | 0 | case AtomicExpr::AO__scoped_atomic_load: |
7750 | 0 | return OrderingCABI != llvm::AtomicOrderingCABI::release && |
7751 | 0 | OrderingCABI != llvm::AtomicOrderingCABI::acq_rel; |
7752 | | |
7753 | 0 | case AtomicExpr::AO__c11_atomic_store: |
7754 | 0 | case AtomicExpr::AO__opencl_atomic_store: |
7755 | 0 | case AtomicExpr::AO__hip_atomic_store: |
7756 | 0 | case AtomicExpr::AO__atomic_store: |
7757 | 0 | case AtomicExpr::AO__atomic_store_n: |
7758 | 0 | case AtomicExpr::AO__scoped_atomic_store: |
7759 | 0 | case AtomicExpr::AO__scoped_atomic_store_n: |
7760 | 0 | return OrderingCABI != llvm::AtomicOrderingCABI::consume && |
7761 | 0 | OrderingCABI != llvm::AtomicOrderingCABI::acquire && |
7762 | 0 | OrderingCABI != llvm::AtomicOrderingCABI::acq_rel; |
7763 | | |
7764 | 0 | default: |
7765 | 0 | return true; |
7766 | 0 | } |
7767 | 0 | } |
7768 | | |
7769 | | ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult, |
7770 | 0 | AtomicExpr::AtomicOp Op) { |
7771 | 0 | CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); |
7772 | 0 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
7773 | 0 | MultiExprArg Args{TheCall->getArgs(), TheCall->getNumArgs()}; |
7774 | 0 | return BuildAtomicExpr({TheCall->getBeginLoc(), TheCall->getEndLoc()}, |
7775 | 0 | DRE->getSourceRange(), TheCall->getRParenLoc(), Args, |
7776 | 0 | Op); |
7777 | 0 | } |
7778 | | |
7779 | | ExprResult Sema::BuildAtomicExpr(SourceRange CallRange, SourceRange ExprRange, |
7780 | | SourceLocation RParenLoc, MultiExprArg Args, |
7781 | | AtomicExpr::AtomicOp Op, |
7782 | 0 | AtomicArgumentOrder ArgOrder) { |
7783 | | // All the non-OpenCL operations take one of the following forms. |
7784 | | // The OpenCL operations take the __c11 forms with one extra argument for |
7785 | | // synchronization scope. |
7786 | 0 | enum { |
7787 | | // C __c11_atomic_init(A *, C) |
7788 | 0 | Init, |
7789 | | |
7790 | | // C __c11_atomic_load(A *, int) |
7791 | 0 | Load, |
7792 | | |
7793 | | // void __atomic_load(A *, CP, int) |
7794 | 0 | LoadCopy, |
7795 | | |
7796 | | // void __atomic_store(A *, CP, int) |
7797 | 0 | Copy, |
7798 | | |
7799 | | // C __c11_atomic_add(A *, M, int) |
7800 | 0 | Arithmetic, |
7801 | | |
7802 | | // C __atomic_exchange_n(A *, CP, int) |
7803 | 0 | Xchg, |
7804 | | |
7805 | | // void __atomic_exchange(A *, C *, CP, int) |
7806 | 0 | GNUXchg, |
7807 | | |
7808 | | // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int) |
7809 | 0 | C11CmpXchg, |
7810 | | |
7811 | | // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int) |
7812 | 0 | GNUCmpXchg |
7813 | 0 | } Form = Init; |
7814 | |
|
7815 | 0 | const unsigned NumForm = GNUCmpXchg + 1; |
7816 | 0 | const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 3, 4, 5, 6 }; |
7817 | 0 | const unsigned NumVals[] = { 1, 0, 1, 1, 1, 1, 2, 2, 3 }; |
7818 | | // where: |
7819 | | // C is an appropriate type, |
7820 | | // A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins, |
7821 | | // CP is C for __c11 builtins and GNU _n builtins and is C * otherwise, |
7822 | | // M is C if C is an integer, and ptrdiff_t if C is a pointer, and |
7823 | | // the int parameters are for orderings. |
7824 | |
|
7825 | 0 | static_assert(sizeof(NumArgs)/sizeof(NumArgs[0]) == NumForm |
7826 | 0 | && sizeof(NumVals)/sizeof(NumVals[0]) == NumForm, |
7827 | 0 | "need to update code for modified forms"); |
7828 | 0 | static_assert(AtomicExpr::AO__c11_atomic_init == 0 && |
7829 | 0 | AtomicExpr::AO__c11_atomic_fetch_min + 1 == |
7830 | 0 | AtomicExpr::AO__atomic_load, |
7831 | 0 | "need to update code for modified C11 atomics"); |
7832 | 0 | bool IsOpenCL = Op >= AtomicExpr::AO__opencl_atomic_init && |
7833 | 0 | Op <= AtomicExpr::AO__opencl_atomic_fetch_max; |
7834 | 0 | bool IsHIP = Op >= AtomicExpr::AO__hip_atomic_load && |
7835 | 0 | Op <= AtomicExpr::AO__hip_atomic_fetch_max; |
7836 | 0 | bool IsScoped = Op >= AtomicExpr::AO__scoped_atomic_load && |
7837 | 0 | Op <= AtomicExpr::AO__scoped_atomic_fetch_max; |
7838 | 0 | bool IsC11 = (Op >= AtomicExpr::AO__c11_atomic_init && |
7839 | 0 | Op <= AtomicExpr::AO__c11_atomic_fetch_min) || |
7840 | 0 | IsOpenCL; |
7841 | 0 | bool IsN = Op == AtomicExpr::AO__atomic_load_n || |
7842 | 0 | Op == AtomicExpr::AO__atomic_store_n || |
7843 | 0 | Op == AtomicExpr::AO__atomic_exchange_n || |
7844 | 0 | Op == AtomicExpr::AO__atomic_compare_exchange_n || |
7845 | 0 | Op == AtomicExpr::AO__scoped_atomic_load_n || |
7846 | 0 | Op == AtomicExpr::AO__scoped_atomic_store_n || |
7847 | 0 | Op == AtomicExpr::AO__scoped_atomic_exchange_n || |
7848 | 0 | Op == AtomicExpr::AO__scoped_atomic_compare_exchange_n; |
7849 | | // Bit mask for extra allowed value types other than integers for atomic |
7850 | | // arithmetic operations. Add/sub allow pointer and floating point. Min/max |
7851 | | // allow floating point. |
7852 | 0 | enum ArithOpExtraValueType { |
7853 | 0 | AOEVT_None = 0, |
7854 | 0 | AOEVT_Pointer = 1, |
7855 | 0 | AOEVT_FP = 2, |
7856 | 0 | }; |
7857 | 0 | unsigned ArithAllows = AOEVT_None; |
7858 | |
|
7859 | 0 | switch (Op) { |
7860 | 0 | case AtomicExpr::AO__c11_atomic_init: |
7861 | 0 | case AtomicExpr::AO__opencl_atomic_init: |
7862 | 0 | Form = Init; |
7863 | 0 | break; |
7864 | | |
7865 | 0 | case AtomicExpr::AO__c11_atomic_load: |
7866 | 0 | case AtomicExpr::AO__opencl_atomic_load: |
7867 | 0 | case AtomicExpr::AO__hip_atomic_load: |
7868 | 0 | case AtomicExpr::AO__atomic_load_n: |
7869 | 0 | case AtomicExpr::AO__scoped_atomic_load_n: |
7870 | 0 | Form = Load; |
7871 | 0 | break; |
7872 | | |
7873 | 0 | case AtomicExpr::AO__atomic_load: |
7874 | 0 | case AtomicExpr::AO__scoped_atomic_load: |
7875 | 0 | Form = LoadCopy; |
7876 | 0 | break; |
7877 | | |
7878 | 0 | case AtomicExpr::AO__c11_atomic_store: |
7879 | 0 | case AtomicExpr::AO__opencl_atomic_store: |
7880 | 0 | case AtomicExpr::AO__hip_atomic_store: |
7881 | 0 | case AtomicExpr::AO__atomic_store: |
7882 | 0 | case AtomicExpr::AO__atomic_store_n: |
7883 | 0 | case AtomicExpr::AO__scoped_atomic_store: |
7884 | 0 | case AtomicExpr::AO__scoped_atomic_store_n: |
7885 | 0 | Form = Copy; |
7886 | 0 | break; |
7887 | 0 | case AtomicExpr::AO__atomic_fetch_add: |
7888 | 0 | case AtomicExpr::AO__atomic_fetch_sub: |
7889 | 0 | case AtomicExpr::AO__atomic_add_fetch: |
7890 | 0 | case AtomicExpr::AO__atomic_sub_fetch: |
7891 | 0 | case AtomicExpr::AO__scoped_atomic_fetch_add: |
7892 | 0 | case AtomicExpr::AO__scoped_atomic_fetch_sub: |
7893 | 0 | case AtomicExpr::AO__scoped_atomic_add_fetch: |
7894 | 0 | case AtomicExpr::AO__scoped_atomic_sub_fetch: |
7895 | 0 | case AtomicExpr::AO__c11_atomic_fetch_add: |
7896 | 0 | case AtomicExpr::AO__c11_atomic_fetch_sub: |
7897 | 0 | case AtomicExpr::AO__opencl_atomic_fetch_add: |
7898 | 0 | case AtomicExpr::AO__opencl_atomic_fetch_sub: |
7899 | 0 | case AtomicExpr::AO__hip_atomic_fetch_add: |
7900 | 0 | case AtomicExpr::AO__hip_atomic_fetch_sub: |
7901 | 0 | ArithAllows = AOEVT_Pointer | AOEVT_FP; |
7902 | 0 | Form = Arithmetic; |
7903 | 0 | break; |
7904 | 0 | case AtomicExpr::AO__atomic_fetch_max: |
7905 | 0 | case AtomicExpr::AO__atomic_fetch_min: |
7906 | 0 | case AtomicExpr::AO__atomic_max_fetch: |
7907 | 0 | case AtomicExpr::AO__atomic_min_fetch: |
7908 | 0 | case AtomicExpr::AO__scoped_atomic_fetch_max: |
7909 | 0 | case AtomicExpr::AO__scoped_atomic_fetch_min: |
7910 | 0 | case AtomicExpr::AO__scoped_atomic_max_fetch: |
7911 | 0 | case AtomicExpr::AO__scoped_atomic_min_fetch: |
7912 | 0 | case AtomicExpr::AO__c11_atomic_fetch_max: |
7913 | 0 | case AtomicExpr::AO__c11_atomic_fetch_min: |
7914 | 0 | case AtomicExpr::AO__opencl_atomic_fetch_max: |
7915 | 0 | case AtomicExpr::AO__opencl_atomic_fetch_min: |
7916 | 0 | case AtomicExpr::AO__hip_atomic_fetch_max: |
7917 | 0 | case AtomicExpr::AO__hip_atomic_fetch_min: |
7918 | 0 | ArithAllows = AOEVT_FP; |
7919 | 0 | Form = Arithmetic; |
7920 | 0 | break; |
7921 | 0 | case AtomicExpr::AO__c11_atomic_fetch_and: |
7922 | 0 | case AtomicExpr::AO__c11_atomic_fetch_or: |
7923 | 0 | case AtomicExpr::AO__c11_atomic_fetch_xor: |
7924 | 0 | case AtomicExpr::AO__hip_atomic_fetch_and: |
7925 | 0 | case AtomicExpr::AO__hip_atomic_fetch_or: |
7926 | 0 | case AtomicExpr::AO__hip_atomic_fetch_xor: |
7927 | 0 | case AtomicExpr::AO__c11_atomic_fetch_nand: |
7928 | 0 | case AtomicExpr::AO__opencl_atomic_fetch_and: |
7929 | 0 | case AtomicExpr::AO__opencl_atomic_fetch_or: |
7930 | 0 | case AtomicExpr::AO__opencl_atomic_fetch_xor: |
7931 | 0 | case AtomicExpr::AO__atomic_fetch_and: |
7932 | 0 | case AtomicExpr::AO__atomic_fetch_or: |
7933 | 0 | case AtomicExpr::AO__atomic_fetch_xor: |
7934 | 0 | case AtomicExpr::AO__atomic_fetch_nand: |
7935 | 0 | case AtomicExpr::AO__atomic_and_fetch: |
7936 | 0 | case AtomicExpr::AO__atomic_or_fetch: |
7937 | 0 | case AtomicExpr::AO__atomic_xor_fetch: |
7938 | 0 | case AtomicExpr::AO__atomic_nand_fetch: |
7939 | 0 | case AtomicExpr::AO__scoped_atomic_fetch_and: |
7940 | 0 | case AtomicExpr::AO__scoped_atomic_fetch_or: |
7941 | 0 | case AtomicExpr::AO__scoped_atomic_fetch_xor: |
7942 | 0 | case AtomicExpr::AO__scoped_atomic_fetch_nand: |
7943 | 0 | case AtomicExpr::AO__scoped_atomic_and_fetch: |
7944 | 0 | case AtomicExpr::AO__scoped_atomic_or_fetch: |
7945 | 0 | case AtomicExpr::AO__scoped_atomic_xor_fetch: |
7946 | 0 | case AtomicExpr::AO__scoped_atomic_nand_fetch: |
7947 | 0 | Form = Arithmetic; |
7948 | 0 | break; |
7949 | | |
7950 | 0 | case AtomicExpr::AO__c11_atomic_exchange: |
7951 | 0 | case AtomicExpr::AO__hip_atomic_exchange: |
7952 | 0 | case AtomicExpr::AO__opencl_atomic_exchange: |
7953 | 0 | case AtomicExpr::AO__atomic_exchange_n: |
7954 | 0 | case AtomicExpr::AO__scoped_atomic_exchange_n: |
7955 | 0 | Form = Xchg; |
7956 | 0 | break; |
7957 | | |
7958 | 0 | case AtomicExpr::AO__atomic_exchange: |
7959 | 0 | case AtomicExpr::AO__scoped_atomic_exchange: |
7960 | 0 | Form = GNUXchg; |
7961 | 0 | break; |
7962 | | |
7963 | 0 | case AtomicExpr::AO__c11_atomic_compare_exchange_strong: |
7964 | 0 | case AtomicExpr::AO__c11_atomic_compare_exchange_weak: |
7965 | 0 | case AtomicExpr::AO__hip_atomic_compare_exchange_strong: |
7966 | 0 | case AtomicExpr::AO__opencl_atomic_compare_exchange_strong: |
7967 | 0 | case AtomicExpr::AO__opencl_atomic_compare_exchange_weak: |
7968 | 0 | case AtomicExpr::AO__hip_atomic_compare_exchange_weak: |
7969 | 0 | Form = C11CmpXchg; |
7970 | 0 | break; |
7971 | | |
7972 | 0 | case AtomicExpr::AO__atomic_compare_exchange: |
7973 | 0 | case AtomicExpr::AO__atomic_compare_exchange_n: |
7974 | 0 | case AtomicExpr::AO__scoped_atomic_compare_exchange: |
7975 | 0 | case AtomicExpr::AO__scoped_atomic_compare_exchange_n: |
7976 | 0 | Form = GNUCmpXchg; |
7977 | 0 | break; |
7978 | 0 | } |
7979 | | |
7980 | 0 | unsigned AdjustedNumArgs = NumArgs[Form]; |
7981 | 0 | if ((IsOpenCL || IsHIP || IsScoped) && |
7982 | 0 | Op != AtomicExpr::AO__opencl_atomic_init) |
7983 | 0 | ++AdjustedNumArgs; |
7984 | | // Check we have the right number of arguments. |
7985 | 0 | if (Args.size() < AdjustedNumArgs) { |
7986 | 0 | Diag(CallRange.getEnd(), diag::err_typecheck_call_too_few_args) |
7987 | 0 | << 0 << AdjustedNumArgs << static_cast<unsigned>(Args.size()) |
7988 | 0 | << /*is non object*/ 0 << ExprRange; |
7989 | 0 | return ExprError(); |
7990 | 0 | } else if (Args.size() > AdjustedNumArgs) { |
7991 | 0 | Diag(Args[AdjustedNumArgs]->getBeginLoc(), |
7992 | 0 | diag::err_typecheck_call_too_many_args) |
7993 | 0 | << 0 << AdjustedNumArgs << static_cast<unsigned>(Args.size()) |
7994 | 0 | << /*is non object*/ 0 << ExprRange; |
7995 | 0 | return ExprError(); |
7996 | 0 | } |
7997 | | |
7998 | | // Inspect the first argument of the atomic operation. |
7999 | 0 | Expr *Ptr = Args[0]; |
8000 | 0 | ExprResult ConvertedPtr = DefaultFunctionArrayLvalueConversion(Ptr); |
8001 | 0 | if (ConvertedPtr.isInvalid()) |
8002 | 0 | return ExprError(); |
8003 | | |
8004 | 0 | Ptr = ConvertedPtr.get(); |
8005 | 0 | const PointerType *pointerType = Ptr->getType()->getAs<PointerType>(); |
8006 | 0 | if (!pointerType) { |
8007 | 0 | Diag(ExprRange.getBegin(), diag::err_atomic_builtin_must_be_pointer) |
8008 | 0 | << Ptr->getType() << Ptr->getSourceRange(); |
8009 | 0 | return ExprError(); |
8010 | 0 | } |
8011 | | |
8012 | | // For a __c11 builtin, this should be a pointer to an _Atomic type. |
8013 | 0 | QualType AtomTy = pointerType->getPointeeType(); // 'A' |
8014 | 0 | QualType ValType = AtomTy; // 'C' |
8015 | 0 | if (IsC11) { |
8016 | 0 | if (!AtomTy->isAtomicType()) { |
8017 | 0 | Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_atomic) |
8018 | 0 | << Ptr->getType() << Ptr->getSourceRange(); |
8019 | 0 | return ExprError(); |
8020 | 0 | } |
8021 | 0 | if ((Form != Load && Form != LoadCopy && AtomTy.isConstQualified()) || |
8022 | 0 | AtomTy.getAddressSpace() == LangAS::opencl_constant) { |
8023 | 0 | Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_const_atomic) |
8024 | 0 | << (AtomTy.isConstQualified() ? 0 : 1) << Ptr->getType() |
8025 | 0 | << Ptr->getSourceRange(); |
8026 | 0 | return ExprError(); |
8027 | 0 | } |
8028 | 0 | ValType = AtomTy->castAs<AtomicType>()->getValueType(); |
8029 | 0 | } else if (Form != Load && Form != LoadCopy) { |
8030 | 0 | if (ValType.isConstQualified()) { |
8031 | 0 | Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_const_pointer) |
8032 | 0 | << Ptr->getType() << Ptr->getSourceRange(); |
8033 | 0 | return ExprError(); |
8034 | 0 | } |
8035 | 0 | } |
8036 | | |
8037 | | // For an arithmetic operation, the implied arithmetic must be well-formed. |
8038 | 0 | if (Form == Arithmetic) { |
8039 | | // GCC does not enforce these rules for GNU atomics, but we do to help catch |
8040 | | // trivial type errors. |
8041 | 0 | auto IsAllowedValueType = [&](QualType ValType, |
8042 | 0 | unsigned AllowedType) -> bool { |
8043 | 0 | if (ValType->isIntegerType()) |
8044 | 0 | return true; |
8045 | 0 | if (ValType->isPointerType()) |
8046 | 0 | return AllowedType & AOEVT_Pointer; |
8047 | 0 | if (!(ValType->isFloatingType() && (AllowedType & AOEVT_FP))) |
8048 | 0 | return false; |
8049 | | // LLVM Parser does not allow atomicrmw with x86_fp80 type. |
8050 | 0 | if (ValType->isSpecificBuiltinType(BuiltinType::LongDouble) && |
8051 | 0 | &Context.getTargetInfo().getLongDoubleFormat() == |
8052 | 0 | &llvm::APFloat::x87DoubleExtended()) |
8053 | 0 | return false; |
8054 | 0 | return true; |
8055 | 0 | }; |
8056 | 0 | if (!IsAllowedValueType(ValType, ArithAllows)) { |
8057 | 0 | auto DID = ArithAllows & AOEVT_FP |
8058 | 0 | ? (ArithAllows & AOEVT_Pointer |
8059 | 0 | ? diag::err_atomic_op_needs_atomic_int_ptr_or_fp |
8060 | 0 | : diag::err_atomic_op_needs_atomic_int_or_fp) |
8061 | 0 | : diag::err_atomic_op_needs_atomic_int; |
8062 | 0 | Diag(ExprRange.getBegin(), DID) |
8063 | 0 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
8064 | 0 | return ExprError(); |
8065 | 0 | } |
8066 | 0 | if (IsC11 && ValType->isPointerType() && |
8067 | 0 | RequireCompleteType(Ptr->getBeginLoc(), ValType->getPointeeType(), |
8068 | 0 | diag::err_incomplete_type)) { |
8069 | 0 | return ExprError(); |
8070 | 0 | } |
8071 | 0 | } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) { |
8072 | | // For __atomic_*_n operations, the value type must be a scalar integral or |
8073 | | // pointer type which is 1, 2, 4, 8 or 16 bytes in length. |
8074 | 0 | Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_atomic_int_or_ptr) |
8075 | 0 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
8076 | 0 | return ExprError(); |
8077 | 0 | } |
8078 | | |
8079 | 0 | if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context) && |
8080 | 0 | !AtomTy->isScalarType()) { |
8081 | | // For GNU atomics, require a trivially-copyable type. This is not part of |
8082 | | // the GNU atomics specification but we enforce it for consistency with |
8083 | | // other atomics which generally all require a trivially-copyable type. This |
8084 | | // is because atomics just copy bits. |
8085 | 0 | Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_trivial_copy) |
8086 | 0 | << Ptr->getType() << Ptr->getSourceRange(); |
8087 | 0 | return ExprError(); |
8088 | 0 | } |
8089 | | |
8090 | 0 | switch (ValType.getObjCLifetime()) { |
8091 | 0 | case Qualifiers::OCL_None: |
8092 | 0 | case Qualifiers::OCL_ExplicitNone: |
8093 | | // okay |
8094 | 0 | break; |
8095 | | |
8096 | 0 | case Qualifiers::OCL_Weak: |
8097 | 0 | case Qualifiers::OCL_Strong: |
8098 | 0 | case Qualifiers::OCL_Autoreleasing: |
8099 | | // FIXME: Can this happen? By this point, ValType should be known |
8100 | | // to be trivially copyable. |
8101 | 0 | Diag(ExprRange.getBegin(), diag::err_arc_atomic_ownership) |
8102 | 0 | << ValType << Ptr->getSourceRange(); |
8103 | 0 | return ExprError(); |
8104 | 0 | } |
8105 | | |
8106 | | // All atomic operations have an overload which takes a pointer to a volatile |
8107 | | // 'A'. We shouldn't let the volatile-ness of the pointee-type inject itself |
8108 | | // into the result or the other operands. Similarly atomic_load takes a |
8109 | | // pointer to a const 'A'. |
8110 | 0 | ValType.removeLocalVolatile(); |
8111 | 0 | ValType.removeLocalConst(); |
8112 | 0 | QualType ResultType = ValType; |
8113 | 0 | if (Form == Copy || Form == LoadCopy || Form == GNUXchg || |
8114 | 0 | Form == Init) |
8115 | 0 | ResultType = Context.VoidTy; |
8116 | 0 | else if (Form == C11CmpXchg || Form == GNUCmpXchg) |
8117 | 0 | ResultType = Context.BoolTy; |
8118 | | |
8119 | | // The type of a parameter passed 'by value'. In the GNU atomics, such |
8120 | | // arguments are actually passed as pointers. |
8121 | 0 | QualType ByValType = ValType; // 'CP' |
8122 | 0 | bool IsPassedByAddress = false; |
8123 | 0 | if (!IsC11 && !IsHIP && !IsN) { |
8124 | 0 | ByValType = Ptr->getType(); |
8125 | 0 | IsPassedByAddress = true; |
8126 | 0 | } |
8127 | |
|
8128 | 0 | SmallVector<Expr *, 5> APIOrderedArgs; |
8129 | 0 | if (ArgOrder == Sema::AtomicArgumentOrder::AST) { |
8130 | 0 | APIOrderedArgs.push_back(Args[0]); |
8131 | 0 | switch (Form) { |
8132 | 0 | case Init: |
8133 | 0 | case Load: |
8134 | 0 | APIOrderedArgs.push_back(Args[1]); // Val1/Order |
8135 | 0 | break; |
8136 | 0 | case LoadCopy: |
8137 | 0 | case Copy: |
8138 | 0 | case Arithmetic: |
8139 | 0 | case Xchg: |
8140 | 0 | APIOrderedArgs.push_back(Args[2]); // Val1 |
8141 | 0 | APIOrderedArgs.push_back(Args[1]); // Order |
8142 | 0 | break; |
8143 | 0 | case GNUXchg: |
8144 | 0 | APIOrderedArgs.push_back(Args[2]); // Val1 |
8145 | 0 | APIOrderedArgs.push_back(Args[3]); // Val2 |
8146 | 0 | APIOrderedArgs.push_back(Args[1]); // Order |
8147 | 0 | break; |
8148 | 0 | case C11CmpXchg: |
8149 | 0 | APIOrderedArgs.push_back(Args[2]); // Val1 |
8150 | 0 | APIOrderedArgs.push_back(Args[4]); // Val2 |
8151 | 0 | APIOrderedArgs.push_back(Args[1]); // Order |
8152 | 0 | APIOrderedArgs.push_back(Args[3]); // OrderFail |
8153 | 0 | break; |
8154 | 0 | case GNUCmpXchg: |
8155 | 0 | APIOrderedArgs.push_back(Args[2]); // Val1 |
8156 | 0 | APIOrderedArgs.push_back(Args[4]); // Val2 |
8157 | 0 | APIOrderedArgs.push_back(Args[5]); // Weak |
8158 | 0 | APIOrderedArgs.push_back(Args[1]); // Order |
8159 | 0 | APIOrderedArgs.push_back(Args[3]); // OrderFail |
8160 | 0 | break; |
8161 | 0 | } |
8162 | 0 | } else |
8163 | 0 | APIOrderedArgs.append(Args.begin(), Args.end()); |
8164 | | |
8165 | | // The first argument's non-CV pointer type is used to deduce the type of |
8166 | | // subsequent arguments, except for: |
8167 | | // - weak flag (always converted to bool) |
8168 | | // - memory order (always converted to int) |
8169 | | // - scope (always converted to int) |
8170 | 0 | for (unsigned i = 0; i != APIOrderedArgs.size(); ++i) { |
8171 | 0 | QualType Ty; |
8172 | 0 | if (i < NumVals[Form] + 1) { |
8173 | 0 | switch (i) { |
8174 | 0 | case 0: |
8175 | | // The first argument is always a pointer. It has a fixed type. |
8176 | | // It is always dereferenced, a nullptr is undefined. |
8177 | 0 | CheckNonNullArgument(*this, APIOrderedArgs[i], ExprRange.getBegin()); |
8178 | | // Nothing else to do: we already know all we want about this pointer. |
8179 | 0 | continue; |
8180 | 0 | case 1: |
8181 | | // The second argument is the non-atomic operand. For arithmetic, this |
8182 | | // is always passed by value, and for a compare_exchange it is always |
8183 | | // passed by address. For the rest, GNU uses by-address and C11 uses |
8184 | | // by-value. |
8185 | 0 | assert(Form != Load); |
8186 | 0 | if (Form == Arithmetic && ValType->isPointerType()) |
8187 | 0 | Ty = Context.getPointerDiffType(); |
8188 | 0 | else if (Form == Init || Form == Arithmetic) |
8189 | 0 | Ty = ValType; |
8190 | 0 | else if (Form == Copy || Form == Xchg) { |
8191 | 0 | if (IsPassedByAddress) { |
8192 | | // The value pointer is always dereferenced, a nullptr is undefined. |
8193 | 0 | CheckNonNullArgument(*this, APIOrderedArgs[i], |
8194 | 0 | ExprRange.getBegin()); |
8195 | 0 | } |
8196 | 0 | Ty = ByValType; |
8197 | 0 | } else { |
8198 | 0 | Expr *ValArg = APIOrderedArgs[i]; |
8199 | | // The value pointer is always dereferenced, a nullptr is undefined. |
8200 | 0 | CheckNonNullArgument(*this, ValArg, ExprRange.getBegin()); |
8201 | 0 | LangAS AS = LangAS::Default; |
8202 | | // Keep address space of non-atomic pointer type. |
8203 | 0 | if (const PointerType *PtrTy = |
8204 | 0 | ValArg->getType()->getAs<PointerType>()) { |
8205 | 0 | AS = PtrTy->getPointeeType().getAddressSpace(); |
8206 | 0 | } |
8207 | 0 | Ty = Context.getPointerType( |
8208 | 0 | Context.getAddrSpaceQualType(ValType.getUnqualifiedType(), AS)); |
8209 | 0 | } |
8210 | 0 | break; |
8211 | 0 | case 2: |
8212 | | // The third argument to compare_exchange / GNU exchange is the desired |
8213 | | // value, either by-value (for the C11 and *_n variant) or as a pointer. |
8214 | 0 | if (IsPassedByAddress) |
8215 | 0 | CheckNonNullArgument(*this, APIOrderedArgs[i], ExprRange.getBegin()); |
8216 | 0 | Ty = ByValType; |
8217 | 0 | break; |
8218 | 0 | case 3: |
8219 | | // The fourth argument to GNU compare_exchange is a 'weak' flag. |
8220 | 0 | Ty = Context.BoolTy; |
8221 | 0 | break; |
8222 | 0 | } |
8223 | 0 | } else { |
8224 | | // The order(s) and scope are always converted to int. |
8225 | 0 | Ty = Context.IntTy; |
8226 | 0 | } |
8227 | | |
8228 | 0 | InitializedEntity Entity = |
8229 | 0 | InitializedEntity::InitializeParameter(Context, Ty, false); |
8230 | 0 | ExprResult Arg = APIOrderedArgs[i]; |
8231 | 0 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
8232 | 0 | if (Arg.isInvalid()) |
8233 | 0 | return true; |
8234 | 0 | APIOrderedArgs[i] = Arg.get(); |
8235 | 0 | } |
8236 | | |
8237 | | // Permute the arguments into a 'consistent' order. |
8238 | 0 | SmallVector<Expr*, 5> SubExprs; |
8239 | 0 | SubExprs.push_back(Ptr); |
8240 | 0 | switch (Form) { |
8241 | 0 | case Init: |
8242 | | // Note, AtomicExpr::getVal1() has a special case for this atomic. |
8243 | 0 | SubExprs.push_back(APIOrderedArgs[1]); // Val1 |
8244 | 0 | break; |
8245 | 0 | case Load: |
8246 | 0 | SubExprs.push_back(APIOrderedArgs[1]); // Order |
8247 | 0 | break; |
8248 | 0 | case LoadCopy: |
8249 | 0 | case Copy: |
8250 | 0 | case Arithmetic: |
8251 | 0 | case Xchg: |
8252 | 0 | SubExprs.push_back(APIOrderedArgs[2]); // Order |
8253 | 0 | SubExprs.push_back(APIOrderedArgs[1]); // Val1 |
8254 | 0 | break; |
8255 | 0 | case GNUXchg: |
8256 | | // Note, AtomicExpr::getVal2() has a special case for this atomic. |
8257 | 0 | SubExprs.push_back(APIOrderedArgs[3]); // Order |
8258 | 0 | SubExprs.push_back(APIOrderedArgs[1]); // Val1 |
8259 | 0 | SubExprs.push_back(APIOrderedArgs[2]); // Val2 |
8260 | 0 | break; |
8261 | 0 | case C11CmpXchg: |
8262 | 0 | SubExprs.push_back(APIOrderedArgs[3]); // Order |
8263 | 0 | SubExprs.push_back(APIOrderedArgs[1]); // Val1 |
8264 | 0 | SubExprs.push_back(APIOrderedArgs[4]); // OrderFail |
8265 | 0 | SubExprs.push_back(APIOrderedArgs[2]); // Val2 |
8266 | 0 | break; |
8267 | 0 | case GNUCmpXchg: |
8268 | 0 | SubExprs.push_back(APIOrderedArgs[4]); // Order |
8269 | 0 | SubExprs.push_back(APIOrderedArgs[1]); // Val1 |
8270 | 0 | SubExprs.push_back(APIOrderedArgs[5]); // OrderFail |
8271 | 0 | SubExprs.push_back(APIOrderedArgs[2]); // Val2 |
8272 | 0 | SubExprs.push_back(APIOrderedArgs[3]); // Weak |
8273 | 0 | break; |
8274 | 0 | } |
8275 | | |
8276 | | // If the memory orders are constants, check they are valid. |
8277 | 0 | if (SubExprs.size() >= 2 && Form != Init) { |
8278 | 0 | std::optional<llvm::APSInt> Success = |
8279 | 0 | SubExprs[1]->getIntegerConstantExpr(Context); |
8280 | 0 | if (Success && !isValidOrderingForOp(Success->getSExtValue(), Op)) { |
8281 | 0 | Diag(SubExprs[1]->getBeginLoc(), |
8282 | 0 | diag::warn_atomic_op_has_invalid_memory_order) |
8283 | 0 | << /*success=*/(Form == C11CmpXchg || Form == GNUCmpXchg) |
8284 | 0 | << SubExprs[1]->getSourceRange(); |
8285 | 0 | } |
8286 | 0 | if (SubExprs.size() >= 5) { |
8287 | 0 | if (std::optional<llvm::APSInt> Failure = |
8288 | 0 | SubExprs[3]->getIntegerConstantExpr(Context)) { |
8289 | 0 | if (!llvm::is_contained( |
8290 | 0 | {llvm::AtomicOrderingCABI::relaxed, |
8291 | 0 | llvm::AtomicOrderingCABI::consume, |
8292 | 0 | llvm::AtomicOrderingCABI::acquire, |
8293 | 0 | llvm::AtomicOrderingCABI::seq_cst}, |
8294 | 0 | (llvm::AtomicOrderingCABI)Failure->getSExtValue())) { |
8295 | 0 | Diag(SubExprs[3]->getBeginLoc(), |
8296 | 0 | diag::warn_atomic_op_has_invalid_memory_order) |
8297 | 0 | << /*failure=*/2 << SubExprs[3]->getSourceRange(); |
8298 | 0 | } |
8299 | 0 | } |
8300 | 0 | } |
8301 | 0 | } |
8302 | |
|
8303 | 0 | if (auto ScopeModel = AtomicExpr::getScopeModel(Op)) { |
8304 | 0 | auto *Scope = Args[Args.size() - 1]; |
8305 | 0 | if (std::optional<llvm::APSInt> Result = |
8306 | 0 | Scope->getIntegerConstantExpr(Context)) { |
8307 | 0 | if (!ScopeModel->isValid(Result->getZExtValue())) |
8308 | 0 | Diag(Scope->getBeginLoc(), diag::err_atomic_op_has_invalid_synch_scope) |
8309 | 0 | << Scope->getSourceRange(); |
8310 | 0 | } |
8311 | 0 | SubExprs.push_back(Scope); |
8312 | 0 | } |
8313 | |
|
8314 | 0 | AtomicExpr *AE = new (Context) |
8315 | 0 | AtomicExpr(ExprRange.getBegin(), SubExprs, ResultType, Op, RParenLoc); |
8316 | |
|
8317 | 0 | if ((Op == AtomicExpr::AO__c11_atomic_load || |
8318 | 0 | Op == AtomicExpr::AO__c11_atomic_store || |
8319 | 0 | Op == AtomicExpr::AO__opencl_atomic_load || |
8320 | 0 | Op == AtomicExpr::AO__hip_atomic_load || |
8321 | 0 | Op == AtomicExpr::AO__opencl_atomic_store || |
8322 | 0 | Op == AtomicExpr::AO__hip_atomic_store) && |
8323 | 0 | Context.AtomicUsesUnsupportedLibcall(AE)) |
8324 | 0 | Diag(AE->getBeginLoc(), diag::err_atomic_load_store_uses_lib) |
8325 | 0 | << ((Op == AtomicExpr::AO__c11_atomic_load || |
8326 | 0 | Op == AtomicExpr::AO__opencl_atomic_load || |
8327 | 0 | Op == AtomicExpr::AO__hip_atomic_load) |
8328 | 0 | ? 0 |
8329 | 0 | : 1); |
8330 | |
|
8331 | 0 | if (ValType->isBitIntType()) { |
8332 | 0 | Diag(Ptr->getExprLoc(), diag::err_atomic_builtin_bit_int_prohibit); |
8333 | 0 | return ExprError(); |
8334 | 0 | } |
8335 | | |
8336 | 0 | return AE; |
8337 | 0 | } |
8338 | | |
8339 | | /// checkBuiltinArgument - Given a call to a builtin function, perform |
8340 | | /// normal type-checking on the given argument, updating the call in |
8341 | | /// place. This is useful when a builtin function requires custom |
8342 | | /// type-checking for some of its arguments but not necessarily all of |
8343 | | /// them. |
8344 | | /// |
8345 | | /// Returns true on error. |
8346 | 0 | static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) { |
8347 | 0 | FunctionDecl *Fn = E->getDirectCallee(); |
8348 | 0 | assert(Fn && "builtin call without direct callee!"); |
8349 | | |
8350 | 0 | ParmVarDecl *Param = Fn->getParamDecl(ArgIndex); |
8351 | 0 | InitializedEntity Entity = |
8352 | 0 | InitializedEntity::InitializeParameter(S.Context, Param); |
8353 | |
|
8354 | 0 | ExprResult Arg = E->getArg(ArgIndex); |
8355 | 0 | Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); |
8356 | 0 | if (Arg.isInvalid()) |
8357 | 0 | return true; |
8358 | | |
8359 | 0 | E->setArg(ArgIndex, Arg.get()); |
8360 | 0 | return false; |
8361 | 0 | } |
8362 | | |
8363 | 0 | bool Sema::BuiltinWasmRefNullExtern(CallExpr *TheCall) { |
8364 | 0 | if (TheCall->getNumArgs() != 0) |
8365 | 0 | return true; |
8366 | | |
8367 | 0 | TheCall->setType(Context.getWebAssemblyExternrefType()); |
8368 | |
|
8369 | 0 | return false; |
8370 | 0 | } |
8371 | | |
8372 | 0 | bool Sema::BuiltinWasmRefNullFunc(CallExpr *TheCall) { |
8373 | 0 | if (TheCall->getNumArgs() != 0) { |
8374 | 0 | Diag(TheCall->getBeginLoc(), diag::err_typecheck_call_too_many_args) |
8375 | 0 | << 0 /*function call*/ << /*expected*/ 0 << TheCall->getNumArgs() |
8376 | 0 | << /*is non object*/ 0; |
8377 | 0 | return true; |
8378 | 0 | } |
8379 | | |
8380 | | // This custom type checking code ensures that the nodes are as expected |
8381 | | // in order to later on generate the necessary builtin. |
8382 | 0 | QualType Pointee = Context.getFunctionType(Context.VoidTy, {}, {}); |
8383 | 0 | QualType Type = Context.getPointerType(Pointee); |
8384 | 0 | Pointee = Context.getAddrSpaceQualType(Pointee, LangAS::wasm_funcref); |
8385 | 0 | Type = Context.getAttributedType(attr::WebAssemblyFuncref, Type, |
8386 | 0 | Context.getPointerType(Pointee)); |
8387 | 0 | TheCall->setType(Type); |
8388 | |
|
8389 | 0 | return false; |
8390 | 0 | } |
8391 | | |
8392 | | /// We have a call to a function like __sync_fetch_and_add, which is an |
8393 | | /// overloaded function based on the pointer type of its first argument. |
8394 | | /// The main BuildCallExpr routines have already promoted the types of |
8395 | | /// arguments because all of these calls are prototyped as void(...). |
8396 | | /// |
8397 | | /// This function goes through and does final semantic checking for these |
8398 | | /// builtins, as well as generating any warnings. |
8399 | | ExprResult |
8400 | 0 | Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) { |
8401 | 0 | CallExpr *TheCall = static_cast<CallExpr *>(TheCallResult.get()); |
8402 | 0 | Expr *Callee = TheCall->getCallee(); |
8403 | 0 | DeclRefExpr *DRE = cast<DeclRefExpr>(Callee->IgnoreParenCasts()); |
8404 | 0 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
8405 | | |
8406 | | // Ensure that we have at least one argument to do type inference from. |
8407 | 0 | if (TheCall->getNumArgs() < 1) { |
8408 | 0 | Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least) |
8409 | 0 | << 0 << 1 << TheCall->getNumArgs() << /*is non object*/ 0 |
8410 | 0 | << Callee->getSourceRange(); |
8411 | 0 | return ExprError(); |
8412 | 0 | } |
8413 | | |
8414 | | // Inspect the first argument of the atomic builtin. This should always be |
8415 | | // a pointer type, whose element is an integral scalar or pointer type. |
8416 | | // Because it is a pointer type, we don't have to worry about any implicit |
8417 | | // casts here. |
8418 | | // FIXME: We don't allow floating point scalars as input. |
8419 | 0 | Expr *FirstArg = TheCall->getArg(0); |
8420 | 0 | ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg); |
8421 | 0 | if (FirstArgResult.isInvalid()) |
8422 | 0 | return ExprError(); |
8423 | 0 | FirstArg = FirstArgResult.get(); |
8424 | 0 | TheCall->setArg(0, FirstArg); |
8425 | |
|
8426 | 0 | const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>(); |
8427 | 0 | if (!pointerType) { |
8428 | 0 | Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer) |
8429 | 0 | << FirstArg->getType() << FirstArg->getSourceRange(); |
8430 | 0 | return ExprError(); |
8431 | 0 | } |
8432 | | |
8433 | 0 | QualType ValType = pointerType->getPointeeType(); |
8434 | 0 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && |
8435 | 0 | !ValType->isBlockPointerType()) { |
8436 | 0 | Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer_intptr) |
8437 | 0 | << FirstArg->getType() << FirstArg->getSourceRange(); |
8438 | 0 | return ExprError(); |
8439 | 0 | } |
8440 | | |
8441 | 0 | if (ValType.isConstQualified()) { |
8442 | 0 | Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_cannot_be_const) |
8443 | 0 | << FirstArg->getType() << FirstArg->getSourceRange(); |
8444 | 0 | return ExprError(); |
8445 | 0 | } |
8446 | | |
8447 | 0 | switch (ValType.getObjCLifetime()) { |
8448 | 0 | case Qualifiers::OCL_None: |
8449 | 0 | case Qualifiers::OCL_ExplicitNone: |
8450 | | // okay |
8451 | 0 | break; |
8452 | | |
8453 | 0 | case Qualifiers::OCL_Weak: |
8454 | 0 | case Qualifiers::OCL_Strong: |
8455 | 0 | case Qualifiers::OCL_Autoreleasing: |
8456 | 0 | Diag(DRE->getBeginLoc(), diag::err_arc_atomic_ownership) |
8457 | 0 | << ValType << FirstArg->getSourceRange(); |
8458 | 0 | return ExprError(); |
8459 | 0 | } |
8460 | | |
8461 | | // Strip any qualifiers off ValType. |
8462 | 0 | ValType = ValType.getUnqualifiedType(); |
8463 | | |
8464 | | // The majority of builtins return a value, but a few have special return |
8465 | | // types, so allow them to override appropriately below. |
8466 | 0 | QualType ResultType = ValType; |
8467 | | |
8468 | | // We need to figure out which concrete builtin this maps onto. For example, |
8469 | | // __sync_fetch_and_add with a 2 byte object turns into |
8470 | | // __sync_fetch_and_add_2. |
8471 | 0 | #define BUILTIN_ROW(x) \ |
8472 | 0 | { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ |
8473 | 0 | Builtin::BI##x##_8, Builtin::BI##x##_16 } |
8474 | |
|
8475 | 0 | static const unsigned BuiltinIndices[][5] = { |
8476 | 0 | BUILTIN_ROW(__sync_fetch_and_add), |
8477 | 0 | BUILTIN_ROW(__sync_fetch_and_sub), |
8478 | 0 | BUILTIN_ROW(__sync_fetch_and_or), |
8479 | 0 | BUILTIN_ROW(__sync_fetch_and_and), |
8480 | 0 | BUILTIN_ROW(__sync_fetch_and_xor), |
8481 | 0 | BUILTIN_ROW(__sync_fetch_and_nand), |
8482 | |
|
8483 | 0 | BUILTIN_ROW(__sync_add_and_fetch), |
8484 | 0 | BUILTIN_ROW(__sync_sub_and_fetch), |
8485 | 0 | BUILTIN_ROW(__sync_and_and_fetch), |
8486 | 0 | BUILTIN_ROW(__sync_or_and_fetch), |
8487 | 0 | BUILTIN_ROW(__sync_xor_and_fetch), |
8488 | 0 | BUILTIN_ROW(__sync_nand_and_fetch), |
8489 | |
|
8490 | 0 | BUILTIN_ROW(__sync_val_compare_and_swap), |
8491 | 0 | BUILTIN_ROW(__sync_bool_compare_and_swap), |
8492 | 0 | BUILTIN_ROW(__sync_lock_test_and_set), |
8493 | 0 | BUILTIN_ROW(__sync_lock_release), |
8494 | 0 | BUILTIN_ROW(__sync_swap) |
8495 | 0 | }; |
8496 | 0 | #undef BUILTIN_ROW |
8497 | | |
8498 | | // Determine the index of the size. |
8499 | 0 | unsigned SizeIndex; |
8500 | 0 | switch (Context.getTypeSizeInChars(ValType).getQuantity()) { |
8501 | 0 | case 1: SizeIndex = 0; break; |
8502 | 0 | case 2: SizeIndex = 1; break; |
8503 | 0 | case 4: SizeIndex = 2; break; |
8504 | 0 | case 8: SizeIndex = 3; break; |
8505 | 0 | case 16: SizeIndex = 4; break; |
8506 | 0 | default: |
8507 | 0 | Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_pointer_size) |
8508 | 0 | << FirstArg->getType() << FirstArg->getSourceRange(); |
8509 | 0 | return ExprError(); |
8510 | 0 | } |
8511 | | |
8512 | | // Each of these builtins has one pointer argument, followed by some number of |
8513 | | // values (0, 1 or 2) followed by a potentially empty varags list of stuff |
8514 | | // that we ignore. Find out which row of BuiltinIndices to read from as well |
8515 | | // as the number of fixed args. |
8516 | 0 | unsigned BuiltinID = FDecl->getBuiltinID(); |
8517 | 0 | unsigned BuiltinIndex, NumFixed = 1; |
8518 | 0 | bool WarnAboutSemanticsChange = false; |
8519 | 0 | switch (BuiltinID) { |
8520 | 0 | default: llvm_unreachable("Unknown overloaded atomic builtin!"); |
8521 | 0 | case Builtin::BI__sync_fetch_and_add: |
8522 | 0 | case Builtin::BI__sync_fetch_and_add_1: |
8523 | 0 | case Builtin::BI__sync_fetch_and_add_2: |
8524 | 0 | case Builtin::BI__sync_fetch_and_add_4: |
8525 | 0 | case Builtin::BI__sync_fetch_and_add_8: |
8526 | 0 | case Builtin::BI__sync_fetch_and_add_16: |
8527 | 0 | BuiltinIndex = 0; |
8528 | 0 | break; |
8529 | | |
8530 | 0 | case Builtin::BI__sync_fetch_and_sub: |
8531 | 0 | case Builtin::BI__sync_fetch_and_sub_1: |
8532 | 0 | case Builtin::BI__sync_fetch_and_sub_2: |
8533 | 0 | case Builtin::BI__sync_fetch_and_sub_4: |
8534 | 0 | case Builtin::BI__sync_fetch_and_sub_8: |
8535 | 0 | case Builtin::BI__sync_fetch_and_sub_16: |
8536 | 0 | BuiltinIndex = 1; |
8537 | 0 | break; |
8538 | | |
8539 | 0 | case Builtin::BI__sync_fetch_and_or: |
8540 | 0 | case Builtin::BI__sync_fetch_and_or_1: |
8541 | 0 | case Builtin::BI__sync_fetch_and_or_2: |
8542 | 0 | case Builtin::BI__sync_fetch_and_or_4: |
8543 | 0 | case Builtin::BI__sync_fetch_and_or_8: |
8544 | 0 | case Builtin::BI__sync_fetch_and_or_16: |
8545 | 0 | BuiltinIndex = 2; |
8546 | 0 | break; |
8547 | | |
8548 | 0 | case Builtin::BI__sync_fetch_and_and: |
8549 | 0 | case Builtin::BI__sync_fetch_and_and_1: |
8550 | 0 | case Builtin::BI__sync_fetch_and_and_2: |
8551 | 0 | case Builtin::BI__sync_fetch_and_and_4: |
8552 | 0 | case Builtin::BI__sync_fetch_and_and_8: |
8553 | 0 | case Builtin::BI__sync_fetch_and_and_16: |
8554 | 0 | BuiltinIndex = 3; |
8555 | 0 | break; |
8556 | | |
8557 | 0 | case Builtin::BI__sync_fetch_and_xor: |
8558 | 0 | case Builtin::BI__sync_fetch_and_xor_1: |
8559 | 0 | case Builtin::BI__sync_fetch_and_xor_2: |
8560 | 0 | case Builtin::BI__sync_fetch_and_xor_4: |
8561 | 0 | case Builtin::BI__sync_fetch_and_xor_8: |
8562 | 0 | case Builtin::BI__sync_fetch_and_xor_16: |
8563 | 0 | BuiltinIndex = 4; |
8564 | 0 | break; |
8565 | | |
8566 | 0 | case Builtin::BI__sync_fetch_and_nand: |
8567 | 0 | case Builtin::BI__sync_fetch_and_nand_1: |
8568 | 0 | case Builtin::BI__sync_fetch_and_nand_2: |
8569 | 0 | case Builtin::BI__sync_fetch_and_nand_4: |
8570 | 0 | case Builtin::BI__sync_fetch_and_nand_8: |
8571 | 0 | case Builtin::BI__sync_fetch_and_nand_16: |
8572 | 0 | BuiltinIndex = 5; |
8573 | 0 | WarnAboutSemanticsChange = true; |
8574 | 0 | break; |
8575 | | |
8576 | 0 | case Builtin::BI__sync_add_and_fetch: |
8577 | 0 | case Builtin::BI__sync_add_and_fetch_1: |
8578 | 0 | case Builtin::BI__sync_add_and_fetch_2: |
8579 | 0 | case Builtin::BI__sync_add_and_fetch_4: |
8580 | 0 | case Builtin::BI__sync_add_and_fetch_8: |
8581 | 0 | case Builtin::BI__sync_add_and_fetch_16: |
8582 | 0 | BuiltinIndex = 6; |
8583 | 0 | break; |
8584 | | |
8585 | 0 | case Builtin::BI__sync_sub_and_fetch: |
8586 | 0 | case Builtin::BI__sync_sub_and_fetch_1: |
8587 | 0 | case Builtin::BI__sync_sub_and_fetch_2: |
8588 | 0 | case Builtin::BI__sync_sub_and_fetch_4: |
8589 | 0 | case Builtin::BI__sync_sub_and_fetch_8: |
8590 | 0 | case Builtin::BI__sync_sub_and_fetch_16: |
8591 | 0 | BuiltinIndex = 7; |
8592 | 0 | break; |
8593 | | |
8594 | 0 | case Builtin::BI__sync_and_and_fetch: |
8595 | 0 | case Builtin::BI__sync_and_and_fetch_1: |
8596 | 0 | case Builtin::BI__sync_and_and_fetch_2: |
8597 | 0 | case Builtin::BI__sync_and_and_fetch_4: |
8598 | 0 | case Builtin::BI__sync_and_and_fetch_8: |
8599 | 0 | case Builtin::BI__sync_and_and_fetch_16: |
8600 | 0 | BuiltinIndex = 8; |
8601 | 0 | break; |
8602 | | |
8603 | 0 | case Builtin::BI__sync_or_and_fetch: |
8604 | 0 | case Builtin::BI__sync_or_and_fetch_1: |
8605 | 0 | case Builtin::BI__sync_or_and_fetch_2: |
8606 | 0 | case Builtin::BI__sync_or_and_fetch_4: |
8607 | 0 | case Builtin::BI__sync_or_and_fetch_8: |
8608 | 0 | case Builtin::BI__sync_or_and_fetch_16: |
8609 | 0 | BuiltinIndex = 9; |
8610 | 0 | break; |
8611 | | |
8612 | 0 | case Builtin::BI__sync_xor_and_fetch: |
8613 | 0 | case Builtin::BI__sync_xor_and_fetch_1: |
8614 | 0 | case Builtin::BI__sync_xor_and_fetch_2: |
8615 | 0 | case Builtin::BI__sync_xor_and_fetch_4: |
8616 | 0 | case Builtin::BI__sync_xor_and_fetch_8: |
8617 | 0 | case Builtin::BI__sync_xor_and_fetch_16: |
8618 | 0 | BuiltinIndex = 10; |
8619 | 0 | break; |
8620 | | |
8621 | 0 | case Builtin::BI__sync_nand_and_fetch: |
8622 | 0 | case Builtin::BI__sync_nand_and_fetch_1: |
8623 | 0 | case Builtin::BI__sync_nand_and_fetch_2: |
8624 | 0 | case Builtin::BI__sync_nand_and_fetch_4: |
8625 | 0 | case Builtin::BI__sync_nand_and_fetch_8: |
8626 | 0 | case Builtin::BI__sync_nand_and_fetch_16: |
8627 | 0 | BuiltinIndex = 11; |
8628 | 0 | WarnAboutSemanticsChange = true; |
8629 | 0 | break; |
8630 | | |
8631 | 0 | case Builtin::BI__sync_val_compare_and_swap: |
8632 | 0 | case Builtin::BI__sync_val_compare_and_swap_1: |
8633 | 0 | case Builtin::BI__sync_val_compare_and_swap_2: |
8634 | 0 | case Builtin::BI__sync_val_compare_and_swap_4: |
8635 | 0 | case Builtin::BI__sync_val_compare_and_swap_8: |
8636 | 0 | case Builtin::BI__sync_val_compare_and_swap_16: |
8637 | 0 | BuiltinIndex = 12; |
8638 | 0 | NumFixed = 2; |
8639 | 0 | break; |
8640 | | |
8641 | 0 | case Builtin::BI__sync_bool_compare_and_swap: |
8642 | 0 | case Builtin::BI__sync_bool_compare_and_swap_1: |
8643 | 0 | case Builtin::BI__sync_bool_compare_and_swap_2: |
8644 | 0 | case Builtin::BI__sync_bool_compare_and_swap_4: |
8645 | 0 | case Builtin::BI__sync_bool_compare_and_swap_8: |
8646 | 0 | case Builtin::BI__sync_bool_compare_and_swap_16: |
8647 | 0 | BuiltinIndex = 13; |
8648 | 0 | NumFixed = 2; |
8649 | 0 | ResultType = Context.BoolTy; |
8650 | 0 | break; |
8651 | | |
8652 | 0 | case Builtin::BI__sync_lock_test_and_set: |
8653 | 0 | case Builtin::BI__sync_lock_test_and_set_1: |
8654 | 0 | case Builtin::BI__sync_lock_test_and_set_2: |
8655 | 0 | case Builtin::BI__sync_lock_test_and_set_4: |
8656 | 0 | case Builtin::BI__sync_lock_test_and_set_8: |
8657 | 0 | case Builtin::BI__sync_lock_test_and_set_16: |
8658 | 0 | BuiltinIndex = 14; |
8659 | 0 | break; |
8660 | | |
8661 | 0 | case Builtin::BI__sync_lock_release: |
8662 | 0 | case Builtin::BI__sync_lock_release_1: |
8663 | 0 | case Builtin::BI__sync_lock_release_2: |
8664 | 0 | case Builtin::BI__sync_lock_release_4: |
8665 | 0 | case Builtin::BI__sync_lock_release_8: |
8666 | 0 | case Builtin::BI__sync_lock_release_16: |
8667 | 0 | BuiltinIndex = 15; |
8668 | 0 | NumFixed = 0; |
8669 | 0 | ResultType = Context.VoidTy; |
8670 | 0 | break; |
8671 | | |
8672 | 0 | case Builtin::BI__sync_swap: |
8673 | 0 | case Builtin::BI__sync_swap_1: |
8674 | 0 | case Builtin::BI__sync_swap_2: |
8675 | 0 | case Builtin::BI__sync_swap_4: |
8676 | 0 | case Builtin::BI__sync_swap_8: |
8677 | 0 | case Builtin::BI__sync_swap_16: |
8678 | 0 | BuiltinIndex = 16; |
8679 | 0 | break; |
8680 | 0 | } |
8681 | | |
8682 | | // Now that we know how many fixed arguments we expect, first check that we |
8683 | | // have at least that many. |
8684 | 0 | if (TheCall->getNumArgs() < 1+NumFixed) { |
8685 | 0 | Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least) |
8686 | 0 | << 0 << 1 + NumFixed << TheCall->getNumArgs() << /*is non object*/ 0 |
8687 | 0 | << Callee->getSourceRange(); |
8688 | 0 | return ExprError(); |
8689 | 0 | } |
8690 | | |
8691 | 0 | Diag(TheCall->getEndLoc(), diag::warn_atomic_implicit_seq_cst) |
8692 | 0 | << Callee->getSourceRange(); |
8693 | |
|
8694 | 0 | if (WarnAboutSemanticsChange) { |
8695 | 0 | Diag(TheCall->getEndLoc(), diag::warn_sync_fetch_and_nand_semantics_change) |
8696 | 0 | << Callee->getSourceRange(); |
8697 | 0 | } |
8698 | | |
8699 | | // Get the decl for the concrete builtin from this, we can tell what the |
8700 | | // concrete integer type we should convert to is. |
8701 | 0 | unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; |
8702 | 0 | StringRef NewBuiltinName = Context.BuiltinInfo.getName(NewBuiltinID); |
8703 | 0 | FunctionDecl *NewBuiltinDecl; |
8704 | 0 | if (NewBuiltinID == BuiltinID) |
8705 | 0 | NewBuiltinDecl = FDecl; |
8706 | 0 | else { |
8707 | | // Perform builtin lookup to avoid redeclaring it. |
8708 | 0 | DeclarationName DN(&Context.Idents.get(NewBuiltinName)); |
8709 | 0 | LookupResult Res(*this, DN, DRE->getBeginLoc(), LookupOrdinaryName); |
8710 | 0 | LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true); |
8711 | 0 | assert(Res.getFoundDecl()); |
8712 | 0 | NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl()); |
8713 | 0 | if (!NewBuiltinDecl) |
8714 | 0 | return ExprError(); |
8715 | 0 | } |
8716 | | |
8717 | | // The first argument --- the pointer --- has a fixed type; we |
8718 | | // deduce the types of the rest of the arguments accordingly. Walk |
8719 | | // the remaining arguments, converting them to the deduced value type. |
8720 | 0 | for (unsigned i = 0; i != NumFixed; ++i) { |
8721 | 0 | ExprResult Arg = TheCall->getArg(i+1); |
8722 | | |
8723 | | // GCC does an implicit conversion to the pointer or integer ValType. This |
8724 | | // can fail in some cases (1i -> int**), check for this error case now. |
8725 | | // Initialize the argument. |
8726 | 0 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
8727 | 0 | ValType, /*consume*/ false); |
8728 | 0 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
8729 | 0 | if (Arg.isInvalid()) |
8730 | 0 | return ExprError(); |
8731 | | |
8732 | | // Okay, we have something that *can* be converted to the right type. Check |
8733 | | // to see if there is a potentially weird extension going on here. This can |
8734 | | // happen when you do an atomic operation on something like an char* and |
8735 | | // pass in 42. The 42 gets converted to char. This is even more strange |
8736 | | // for things like 45.123 -> char, etc. |
8737 | | // FIXME: Do this check. |
8738 | 0 | TheCall->setArg(i+1, Arg.get()); |
8739 | 0 | } |
8740 | | |
8741 | | // Create a new DeclRefExpr to refer to the new decl. |
8742 | 0 | DeclRefExpr *NewDRE = DeclRefExpr::Create( |
8743 | 0 | Context, DRE->getQualifierLoc(), SourceLocation(), NewBuiltinDecl, |
8744 | 0 | /*enclosing*/ false, DRE->getLocation(), Context.BuiltinFnTy, |
8745 | 0 | DRE->getValueKind(), nullptr, nullptr, DRE->isNonOdrUse()); |
8746 | | |
8747 | | // Set the callee in the CallExpr. |
8748 | | // FIXME: This loses syntactic information. |
8749 | 0 | QualType CalleePtrTy = Context.getPointerType(NewBuiltinDecl->getType()); |
8750 | 0 | ExprResult PromotedCall = ImpCastExprToType(NewDRE, CalleePtrTy, |
8751 | 0 | CK_BuiltinFnToFnPtr); |
8752 | 0 | TheCall->setCallee(PromotedCall.get()); |
8753 | | |
8754 | | // Change the result type of the call to match the original value type. This |
8755 | | // is arbitrary, but the codegen for these builtins ins design to handle it |
8756 | | // gracefully. |
8757 | 0 | TheCall->setType(ResultType); |
8758 | | |
8759 | | // Prohibit problematic uses of bit-precise integer types with atomic |
8760 | | // builtins. The arguments would have already been converted to the first |
8761 | | // argument's type, so only need to check the first argument. |
8762 | 0 | const auto *BitIntValType = ValType->getAs<BitIntType>(); |
8763 | 0 | if (BitIntValType && !llvm::isPowerOf2_64(BitIntValType->getNumBits())) { |
8764 | 0 | Diag(FirstArg->getExprLoc(), diag::err_atomic_builtin_ext_int_size); |
8765 | 0 | return ExprError(); |
8766 | 0 | } |
8767 | | |
8768 | 0 | return TheCallResult; |
8769 | 0 | } |
8770 | | |
8771 | | /// SemaBuiltinNontemporalOverloaded - We have a call to |
8772 | | /// __builtin_nontemporal_store or __builtin_nontemporal_load, which is an |
8773 | | /// overloaded function based on the pointer type of its last argument. |
8774 | | /// |
8775 | | /// This function goes through and does final semantic checking for these |
8776 | | /// builtins. |
8777 | 0 | ExprResult Sema::SemaBuiltinNontemporalOverloaded(ExprResult TheCallResult) { |
8778 | 0 | CallExpr *TheCall = (CallExpr *)TheCallResult.get(); |
8779 | 0 | DeclRefExpr *DRE = |
8780 | 0 | cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
8781 | 0 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
8782 | 0 | unsigned BuiltinID = FDecl->getBuiltinID(); |
8783 | 0 | assert((BuiltinID == Builtin::BI__builtin_nontemporal_store || |
8784 | 0 | BuiltinID == Builtin::BI__builtin_nontemporal_load) && |
8785 | 0 | "Unexpected nontemporal load/store builtin!"); |
8786 | 0 | bool isStore = BuiltinID == Builtin::BI__builtin_nontemporal_store; |
8787 | 0 | unsigned numArgs = isStore ? 2 : 1; |
8788 | | |
8789 | | // Ensure that we have the proper number of arguments. |
8790 | 0 | if (checkArgCount(*this, TheCall, numArgs)) |
8791 | 0 | return ExprError(); |
8792 | | |
8793 | | // Inspect the last argument of the nontemporal builtin. This should always |
8794 | | // be a pointer type, from which we imply the type of the memory access. |
8795 | | // Because it is a pointer type, we don't have to worry about any implicit |
8796 | | // casts here. |
8797 | 0 | Expr *PointerArg = TheCall->getArg(numArgs - 1); |
8798 | 0 | ExprResult PointerArgResult = |
8799 | 0 | DefaultFunctionArrayLvalueConversion(PointerArg); |
8800 | |
|
8801 | 0 | if (PointerArgResult.isInvalid()) |
8802 | 0 | return ExprError(); |
8803 | 0 | PointerArg = PointerArgResult.get(); |
8804 | 0 | TheCall->setArg(numArgs - 1, PointerArg); |
8805 | |
|
8806 | 0 | const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>(); |
8807 | 0 | if (!pointerType) { |
8808 | 0 | Diag(DRE->getBeginLoc(), diag::err_nontemporal_builtin_must_be_pointer) |
8809 | 0 | << PointerArg->getType() << PointerArg->getSourceRange(); |
8810 | 0 | return ExprError(); |
8811 | 0 | } |
8812 | | |
8813 | 0 | QualType ValType = pointerType->getPointeeType(); |
8814 | | |
8815 | | // Strip any qualifiers off ValType. |
8816 | 0 | ValType = ValType.getUnqualifiedType(); |
8817 | 0 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && |
8818 | 0 | !ValType->isBlockPointerType() && !ValType->isFloatingType() && |
8819 | 0 | !ValType->isVectorType()) { |
8820 | 0 | Diag(DRE->getBeginLoc(), |
8821 | 0 | diag::err_nontemporal_builtin_must_be_pointer_intfltptr_or_vector) |
8822 | 0 | << PointerArg->getType() << PointerArg->getSourceRange(); |
8823 | 0 | return ExprError(); |
8824 | 0 | } |
8825 | | |
8826 | 0 | if (!isStore) { |
8827 | 0 | TheCall->setType(ValType); |
8828 | 0 | return TheCallResult; |
8829 | 0 | } |
8830 | | |
8831 | 0 | ExprResult ValArg = TheCall->getArg(0); |
8832 | 0 | InitializedEntity Entity = InitializedEntity::InitializeParameter( |
8833 | 0 | Context, ValType, /*consume*/ false); |
8834 | 0 | ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg); |
8835 | 0 | if (ValArg.isInvalid()) |
8836 | 0 | return ExprError(); |
8837 | | |
8838 | 0 | TheCall->setArg(0, ValArg.get()); |
8839 | 0 | TheCall->setType(Context.VoidTy); |
8840 | 0 | return TheCallResult; |
8841 | 0 | } |
8842 | | |
8843 | | /// CheckObjCString - Checks that the argument to the builtin |
8844 | | /// CFString constructor is correct |
8845 | | /// Note: It might also make sense to do the UTF-16 conversion here (would |
8846 | | /// simplify the backend). |
8847 | 0 | bool Sema::CheckObjCString(Expr *Arg) { |
8848 | 0 | Arg = Arg->IgnoreParenCasts(); |
8849 | 0 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); |
8850 | |
|
8851 | 0 | if (!Literal || !Literal->isOrdinary()) { |
8852 | 0 | Diag(Arg->getBeginLoc(), diag::err_cfstring_literal_not_string_constant) |
8853 | 0 | << Arg->getSourceRange(); |
8854 | 0 | return true; |
8855 | 0 | } |
8856 | | |
8857 | 0 | if (Literal->containsNonAsciiOrNull()) { |
8858 | 0 | StringRef String = Literal->getString(); |
8859 | 0 | unsigned NumBytes = String.size(); |
8860 | 0 | SmallVector<llvm::UTF16, 128> ToBuf(NumBytes); |
8861 | 0 | const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)String.data(); |
8862 | 0 | llvm::UTF16 *ToPtr = &ToBuf[0]; |
8863 | |
|
8864 | 0 | llvm::ConversionResult Result = |
8865 | 0 | llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, &ToPtr, |
8866 | 0 | ToPtr + NumBytes, llvm::strictConversion); |
8867 | | // Check for conversion failure. |
8868 | 0 | if (Result != llvm::conversionOK) |
8869 | 0 | Diag(Arg->getBeginLoc(), diag::warn_cfstring_truncated) |
8870 | 0 | << Arg->getSourceRange(); |
8871 | 0 | } |
8872 | 0 | return false; |
8873 | 0 | } |
8874 | | |
8875 | | /// CheckObjCString - Checks that the format string argument to the os_log() |
8876 | | /// and os_trace() functions is correct, and converts it to const char *. |
8877 | 0 | ExprResult Sema::CheckOSLogFormatStringArg(Expr *Arg) { |
8878 | 0 | Arg = Arg->IgnoreParenCasts(); |
8879 | 0 | auto *Literal = dyn_cast<StringLiteral>(Arg); |
8880 | 0 | if (!Literal) { |
8881 | 0 | if (auto *ObjcLiteral = dyn_cast<ObjCStringLiteral>(Arg)) { |
8882 | 0 | Literal = ObjcLiteral->getString(); |
8883 | 0 | } |
8884 | 0 | } |
8885 | |
|
8886 | 0 | if (!Literal || (!Literal->isOrdinary() && !Literal->isUTF8())) { |
8887 | 0 | return ExprError( |
8888 | 0 | Diag(Arg->getBeginLoc(), diag::err_os_log_format_not_string_constant) |
8889 | 0 | << Arg->getSourceRange()); |
8890 | 0 | } |
8891 | | |
8892 | 0 | ExprResult Result(Literal); |
8893 | 0 | QualType ResultTy = Context.getPointerType(Context.CharTy.withConst()); |
8894 | 0 | InitializedEntity Entity = |
8895 | 0 | InitializedEntity::InitializeParameter(Context, ResultTy, false); |
8896 | 0 | Result = PerformCopyInitialization(Entity, SourceLocation(), Result); |
8897 | 0 | return Result; |
8898 | 0 | } |
8899 | | |
8900 | | /// Check that the user is calling the appropriate va_start builtin for the |
8901 | | /// target and calling convention. |
8902 | 0 | static bool checkVAStartABI(Sema &S, unsigned BuiltinID, Expr *Fn) { |
8903 | 0 | const llvm::Triple &TT = S.Context.getTargetInfo().getTriple(); |
8904 | 0 | bool IsX64 = TT.getArch() == llvm::Triple::x86_64; |
8905 | 0 | bool IsAArch64 = (TT.getArch() == llvm::Triple::aarch64 || |
8906 | 0 | TT.getArch() == llvm::Triple::aarch64_32); |
8907 | 0 | bool IsWindows = TT.isOSWindows(); |
8908 | 0 | bool IsMSVAStart = BuiltinID == Builtin::BI__builtin_ms_va_start; |
8909 | 0 | if (IsX64 || IsAArch64) { |
8910 | 0 | CallingConv CC = CC_C; |
8911 | 0 | if (const FunctionDecl *FD = S.getCurFunctionDecl()) |
8912 | 0 | CC = FD->getType()->castAs<FunctionType>()->getCallConv(); |
8913 | 0 | if (IsMSVAStart) { |
8914 | | // Don't allow this in System V ABI functions. |
8915 | 0 | if (CC == CC_X86_64SysV || (!IsWindows && CC != CC_Win64)) |
8916 | 0 | return S.Diag(Fn->getBeginLoc(), |
8917 | 0 | diag::err_ms_va_start_used_in_sysv_function); |
8918 | 0 | } else { |
8919 | | // On x86-64/AArch64 Unix, don't allow this in Win64 ABI functions. |
8920 | | // On x64 Windows, don't allow this in System V ABI functions. |
8921 | | // (Yes, that means there's no corresponding way to support variadic |
8922 | | // System V ABI functions on Windows.) |
8923 | 0 | if ((IsWindows && CC == CC_X86_64SysV) || |
8924 | 0 | (!IsWindows && CC == CC_Win64)) |
8925 | 0 | return S.Diag(Fn->getBeginLoc(), |
8926 | 0 | diag::err_va_start_used_in_wrong_abi_function) |
8927 | 0 | << !IsWindows; |
8928 | 0 | } |
8929 | 0 | return false; |
8930 | 0 | } |
8931 | | |
8932 | 0 | if (IsMSVAStart) |
8933 | 0 | return S.Diag(Fn->getBeginLoc(), diag::err_builtin_x64_aarch64_only); |
8934 | 0 | return false; |
8935 | 0 | } |
8936 | | |
8937 | | static bool checkVAStartIsInVariadicFunction(Sema &S, Expr *Fn, |
8938 | 0 | ParmVarDecl **LastParam = nullptr) { |
8939 | | // Determine whether the current function, block, or obj-c method is variadic |
8940 | | // and get its parameter list. |
8941 | 0 | bool IsVariadic = false; |
8942 | 0 | ArrayRef<ParmVarDecl *> Params; |
8943 | 0 | DeclContext *Caller = S.CurContext; |
8944 | 0 | if (auto *Block = dyn_cast<BlockDecl>(Caller)) { |
8945 | 0 | IsVariadic = Block->isVariadic(); |
8946 | 0 | Params = Block->parameters(); |
8947 | 0 | } else if (auto *FD = dyn_cast<FunctionDecl>(Caller)) { |
8948 | 0 | IsVariadic = FD->isVariadic(); |
8949 | 0 | Params = FD->parameters(); |
8950 | 0 | } else if (auto *MD = dyn_cast<ObjCMethodDecl>(Caller)) { |
8951 | 0 | IsVariadic = MD->isVariadic(); |
8952 | | // FIXME: This isn't correct for methods (results in bogus warning). |
8953 | 0 | Params = MD->parameters(); |
8954 | 0 | } else if (isa<CapturedDecl>(Caller)) { |
8955 | | // We don't support va_start in a CapturedDecl. |
8956 | 0 | S.Diag(Fn->getBeginLoc(), diag::err_va_start_captured_stmt); |
8957 | 0 | return true; |
8958 | 0 | } else { |
8959 | | // This must be some other declcontext that parses exprs. |
8960 | 0 | S.Diag(Fn->getBeginLoc(), diag::err_va_start_outside_function); |
8961 | 0 | return true; |
8962 | 0 | } |
8963 | | |
8964 | 0 | if (!IsVariadic) { |
8965 | 0 | S.Diag(Fn->getBeginLoc(), diag::err_va_start_fixed_function); |
8966 | 0 | return true; |
8967 | 0 | } |
8968 | | |
8969 | 0 | if (LastParam) |
8970 | 0 | *LastParam = Params.empty() ? nullptr : Params.back(); |
8971 | |
|
8972 | 0 | return false; |
8973 | 0 | } |
8974 | | |
8975 | | /// Check the arguments to '__builtin_va_start' or '__builtin_ms_va_start' |
8976 | | /// for validity. Emit an error and return true on failure; return false |
8977 | | /// on success. |
8978 | 0 | bool Sema::SemaBuiltinVAStart(unsigned BuiltinID, CallExpr *TheCall) { |
8979 | 0 | Expr *Fn = TheCall->getCallee(); |
8980 | |
|
8981 | 0 | if (checkVAStartABI(*this, BuiltinID, Fn)) |
8982 | 0 | return true; |
8983 | | |
8984 | | // In C23 mode, va_start only needs one argument. However, the builtin still |
8985 | | // requires two arguments (which matches the behavior of the GCC builtin), |
8986 | | // <stdarg.h> passes `0` as the second argument in C23 mode. |
8987 | 0 | if (checkArgCount(*this, TheCall, 2)) |
8988 | 0 | return true; |
8989 | | |
8990 | | // Type-check the first argument normally. |
8991 | 0 | if (checkBuiltinArgument(*this, TheCall, 0)) |
8992 | 0 | return true; |
8993 | | |
8994 | | // Check that the current function is variadic, and get its last parameter. |
8995 | 0 | ParmVarDecl *LastParam; |
8996 | 0 | if (checkVAStartIsInVariadicFunction(*this, Fn, &LastParam)) |
8997 | 0 | return true; |
8998 | | |
8999 | | // Verify that the second argument to the builtin is the last argument of the |
9000 | | // current function or method. In C23 mode, if the second argument is an |
9001 | | // integer constant expression with value 0, then we don't bother with this |
9002 | | // check. |
9003 | 0 | bool SecondArgIsLastNamedArgument = false; |
9004 | 0 | const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); |
9005 | 0 | if (std::optional<llvm::APSInt> Val = |
9006 | 0 | TheCall->getArg(1)->getIntegerConstantExpr(Context); |
9007 | 0 | Val && LangOpts.C23 && *Val == 0) |
9008 | 0 | return false; |
9009 | | |
9010 | | // These are valid if SecondArgIsLastNamedArgument is false after the next |
9011 | | // block. |
9012 | 0 | QualType Type; |
9013 | 0 | SourceLocation ParamLoc; |
9014 | 0 | bool IsCRegister = false; |
9015 | |
|
9016 | 0 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { |
9017 | 0 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { |
9018 | 0 | SecondArgIsLastNamedArgument = PV == LastParam; |
9019 | |
|
9020 | 0 | Type = PV->getType(); |
9021 | 0 | ParamLoc = PV->getLocation(); |
9022 | 0 | IsCRegister = |
9023 | 0 | PV->getStorageClass() == SC_Register && !getLangOpts().CPlusPlus; |
9024 | 0 | } |
9025 | 0 | } |
9026 | |
|
9027 | 0 | if (!SecondArgIsLastNamedArgument) |
9028 | 0 | Diag(TheCall->getArg(1)->getBeginLoc(), |
9029 | 0 | diag::warn_second_arg_of_va_start_not_last_named_param); |
9030 | 0 | else if (IsCRegister || Type->isReferenceType() || |
9031 | 0 | Type->isSpecificBuiltinType(BuiltinType::Float) || [=] { |
9032 | | // Promotable integers are UB, but enumerations need a bit of |
9033 | | // extra checking to see what their promotable type actually is. |
9034 | 0 | if (!Context.isPromotableIntegerType(Type)) |
9035 | 0 | return false; |
9036 | 0 | if (!Type->isEnumeralType()) |
9037 | 0 | return true; |
9038 | 0 | const EnumDecl *ED = Type->castAs<EnumType>()->getDecl(); |
9039 | 0 | return !(ED && |
9040 | 0 | Context.typesAreCompatible(ED->getPromotionType(), Type)); |
9041 | 0 | }()) { |
9042 | 0 | unsigned Reason = 0; |
9043 | 0 | if (Type->isReferenceType()) Reason = 1; |
9044 | 0 | else if (IsCRegister) Reason = 2; |
9045 | 0 | Diag(Arg->getBeginLoc(), diag::warn_va_start_type_is_undefined) << Reason; |
9046 | 0 | Diag(ParamLoc, diag::note_parameter_type) << Type; |
9047 | 0 | } |
9048 | |
|
9049 | 0 | return false; |
9050 | 0 | } |
9051 | | |
9052 | 0 | bool Sema::SemaBuiltinVAStartARMMicrosoft(CallExpr *Call) { |
9053 | 0 | auto IsSuitablyTypedFormatArgument = [this](const Expr *Arg) -> bool { |
9054 | 0 | const LangOptions &LO = getLangOpts(); |
9055 | |
|
9056 | 0 | if (LO.CPlusPlus) |
9057 | 0 | return Arg->getType() |
9058 | 0 | .getCanonicalType() |
9059 | 0 | .getTypePtr() |
9060 | 0 | ->getPointeeType() |
9061 | 0 | .withoutLocalFastQualifiers() == Context.CharTy; |
9062 | | |
9063 | | // In C, allow aliasing through `char *`, this is required for AArch64 at |
9064 | | // least. |
9065 | 0 | return true; |
9066 | 0 | }; |
9067 | | |
9068 | | // void __va_start(va_list *ap, const char *named_addr, size_t slot_size, |
9069 | | // const char *named_addr); |
9070 | |
|
9071 | 0 | Expr *Func = Call->getCallee(); |
9072 | |
|
9073 | 0 | if (Call->getNumArgs() < 3) |
9074 | 0 | return Diag(Call->getEndLoc(), |
9075 | 0 | diag::err_typecheck_call_too_few_args_at_least) |
9076 | 0 | << 0 /*function call*/ << 3 << Call->getNumArgs() |
9077 | 0 | << /*is non object*/ 0; |
9078 | | |
9079 | | // Type-check the first argument normally. |
9080 | 0 | if (checkBuiltinArgument(*this, Call, 0)) |
9081 | 0 | return true; |
9082 | | |
9083 | | // Check that the current function is variadic. |
9084 | 0 | if (checkVAStartIsInVariadicFunction(*this, Func)) |
9085 | 0 | return true; |
9086 | | |
9087 | | // __va_start on Windows does not validate the parameter qualifiers |
9088 | | |
9089 | 0 | const Expr *Arg1 = Call->getArg(1)->IgnoreParens(); |
9090 | 0 | const Type *Arg1Ty = Arg1->getType().getCanonicalType().getTypePtr(); |
9091 | |
|
9092 | 0 | const Expr *Arg2 = Call->getArg(2)->IgnoreParens(); |
9093 | 0 | const Type *Arg2Ty = Arg2->getType().getCanonicalType().getTypePtr(); |
9094 | |
|
9095 | 0 | const QualType &ConstCharPtrTy = |
9096 | 0 | Context.getPointerType(Context.CharTy.withConst()); |
9097 | 0 | if (!Arg1Ty->isPointerType() || !IsSuitablyTypedFormatArgument(Arg1)) |
9098 | 0 | Diag(Arg1->getBeginLoc(), diag::err_typecheck_convert_incompatible) |
9099 | 0 | << Arg1->getType() << ConstCharPtrTy << 1 /* different class */ |
9100 | 0 | << 0 /* qualifier difference */ |
9101 | 0 | << 3 /* parameter mismatch */ |
9102 | 0 | << 2 << Arg1->getType() << ConstCharPtrTy; |
9103 | |
|
9104 | 0 | const QualType SizeTy = Context.getSizeType(); |
9105 | 0 | if (Arg2Ty->getCanonicalTypeInternal().withoutLocalFastQualifiers() != SizeTy) |
9106 | 0 | Diag(Arg2->getBeginLoc(), diag::err_typecheck_convert_incompatible) |
9107 | 0 | << Arg2->getType() << SizeTy << 1 /* different class */ |
9108 | 0 | << 0 /* qualifier difference */ |
9109 | 0 | << 3 /* parameter mismatch */ |
9110 | 0 | << 3 << Arg2->getType() << SizeTy; |
9111 | |
|
9112 | 0 | return false; |
9113 | 0 | } |
9114 | | |
9115 | | /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and |
9116 | | /// friends. This is declared to take (...), so we have to check everything. |
9117 | 0 | bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { |
9118 | 0 | if (checkArgCount(*this, TheCall, 2)) |
9119 | 0 | return true; |
9120 | | |
9121 | 0 | ExprResult OrigArg0 = TheCall->getArg(0); |
9122 | 0 | ExprResult OrigArg1 = TheCall->getArg(1); |
9123 | | |
9124 | | // Do standard promotions between the two arguments, returning their common |
9125 | | // type. |
9126 | 0 | QualType Res = UsualArithmeticConversions( |
9127 | 0 | OrigArg0, OrigArg1, TheCall->getExprLoc(), ACK_Comparison); |
9128 | 0 | if (OrigArg0.isInvalid() || OrigArg1.isInvalid()) |
9129 | 0 | return true; |
9130 | | |
9131 | | // Make sure any conversions are pushed back into the call; this is |
9132 | | // type safe since unordered compare builtins are declared as "_Bool |
9133 | | // foo(...)". |
9134 | 0 | TheCall->setArg(0, OrigArg0.get()); |
9135 | 0 | TheCall->setArg(1, OrigArg1.get()); |
9136 | |
|
9137 | 0 | if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent()) |
9138 | 0 | return false; |
9139 | | |
9140 | | // If the common type isn't a real floating type, then the arguments were |
9141 | | // invalid for this operation. |
9142 | 0 | if (Res.isNull() || !Res->isRealFloatingType()) |
9143 | 0 | return Diag(OrigArg0.get()->getBeginLoc(), |
9144 | 0 | diag::err_typecheck_call_invalid_ordered_compare) |
9145 | 0 | << OrigArg0.get()->getType() << OrigArg1.get()->getType() |
9146 | 0 | << SourceRange(OrigArg0.get()->getBeginLoc(), |
9147 | 0 | OrigArg1.get()->getEndLoc()); |
9148 | | |
9149 | 0 | return false; |
9150 | 0 | } |
9151 | | |
9152 | | /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like |
9153 | | /// __builtin_isnan and friends. This is declared to take (...), so we have |
9154 | | /// to check everything. |
9155 | 0 | bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) { |
9156 | 0 | if (checkArgCount(*this, TheCall, NumArgs)) |
9157 | 0 | return true; |
9158 | | |
9159 | 0 | bool IsFPClass = NumArgs == 2; |
9160 | | |
9161 | | // Find out position of floating-point argument. |
9162 | 0 | unsigned FPArgNo = IsFPClass ? 0 : NumArgs - 1; |
9163 | | |
9164 | | // We can count on all parameters preceding the floating-point just being int. |
9165 | | // Try all of those. |
9166 | 0 | for (unsigned i = 0; i < FPArgNo; ++i) { |
9167 | 0 | Expr *Arg = TheCall->getArg(i); |
9168 | |
|
9169 | 0 | if (Arg->isTypeDependent()) |
9170 | 0 | return false; |
9171 | | |
9172 | 0 | ExprResult Res = PerformImplicitConversion(Arg, Context.IntTy, AA_Passing); |
9173 | |
|
9174 | 0 | if (Res.isInvalid()) |
9175 | 0 | return true; |
9176 | 0 | TheCall->setArg(i, Res.get()); |
9177 | 0 | } |
9178 | | |
9179 | 0 | Expr *OrigArg = TheCall->getArg(FPArgNo); |
9180 | |
|
9181 | 0 | if (OrigArg->isTypeDependent()) |
9182 | 0 | return false; |
9183 | | |
9184 | | // Usual Unary Conversions will convert half to float, which we want for |
9185 | | // machines that use fp16 conversion intrinsics. Else, we wnat to leave the |
9186 | | // type how it is, but do normal L->Rvalue conversions. |
9187 | 0 | if (Context.getTargetInfo().useFP16ConversionIntrinsics()) |
9188 | 0 | OrigArg = UsualUnaryConversions(OrigArg).get(); |
9189 | 0 | else |
9190 | 0 | OrigArg = DefaultFunctionArrayLvalueConversion(OrigArg).get(); |
9191 | 0 | TheCall->setArg(FPArgNo, OrigArg); |
9192 | |
|
9193 | 0 | QualType VectorResultTy; |
9194 | 0 | QualType ElementTy = OrigArg->getType(); |
9195 | | // TODO: When all classification function are implemented with is_fpclass, |
9196 | | // vector argument can be supported in all of them. |
9197 | 0 | if (ElementTy->isVectorType() && IsFPClass) { |
9198 | 0 | VectorResultTy = GetSignedVectorType(ElementTy); |
9199 | 0 | ElementTy = ElementTy->getAs<VectorType>()->getElementType(); |
9200 | 0 | } |
9201 | | |
9202 | | // This operation requires a non-_Complex floating-point number. |
9203 | 0 | if (!ElementTy->isRealFloatingType()) |
9204 | 0 | return Diag(OrigArg->getBeginLoc(), |
9205 | 0 | diag::err_typecheck_call_invalid_unary_fp) |
9206 | 0 | << OrigArg->getType() << OrigArg->getSourceRange(); |
9207 | | |
9208 | | // __builtin_isfpclass has integer parameter that specify test mask. It is |
9209 | | // passed in (...), so it should be analyzed completely here. |
9210 | 0 | if (IsFPClass) |
9211 | 0 | if (SemaBuiltinConstantArgRange(TheCall, 1, 0, llvm::fcAllFlags)) |
9212 | 0 | return true; |
9213 | | |
9214 | | // TODO: enable this code to all classification functions. |
9215 | 0 | if (IsFPClass) { |
9216 | 0 | QualType ResultTy; |
9217 | 0 | if (!VectorResultTy.isNull()) |
9218 | 0 | ResultTy = VectorResultTy; |
9219 | 0 | else |
9220 | 0 | ResultTy = Context.IntTy; |
9221 | 0 | TheCall->setType(ResultTy); |
9222 | 0 | } |
9223 | |
|
9224 | 0 | return false; |
9225 | 0 | } |
9226 | | |
9227 | | /// Perform semantic analysis for a call to __builtin_complex. |
9228 | 0 | bool Sema::SemaBuiltinComplex(CallExpr *TheCall) { |
9229 | 0 | if (checkArgCount(*this, TheCall, 2)) |
9230 | 0 | return true; |
9231 | | |
9232 | 0 | bool Dependent = false; |
9233 | 0 | for (unsigned I = 0; I != 2; ++I) { |
9234 | 0 | Expr *Arg = TheCall->getArg(I); |
9235 | 0 | QualType T = Arg->getType(); |
9236 | 0 | if (T->isDependentType()) { |
9237 | 0 | Dependent = true; |
9238 | 0 | continue; |
9239 | 0 | } |
9240 | | |
9241 | | // Despite supporting _Complex int, GCC requires a real floating point type |
9242 | | // for the operands of __builtin_complex. |
9243 | 0 | if (!T->isRealFloatingType()) { |
9244 | 0 | return Diag(Arg->getBeginLoc(), diag::err_typecheck_call_requires_real_fp) |
9245 | 0 | << Arg->getType() << Arg->getSourceRange(); |
9246 | 0 | } |
9247 | | |
9248 | 0 | ExprResult Converted = DefaultLvalueConversion(Arg); |
9249 | 0 | if (Converted.isInvalid()) |
9250 | 0 | return true; |
9251 | 0 | TheCall->setArg(I, Converted.get()); |
9252 | 0 | } |
9253 | | |
9254 | 0 | if (Dependent) { |
9255 | 0 | TheCall->setType(Context.DependentTy); |
9256 | 0 | return false; |
9257 | 0 | } |
9258 | | |
9259 | 0 | Expr *Real = TheCall->getArg(0); |
9260 | 0 | Expr *Imag = TheCall->getArg(1); |
9261 | 0 | if (!Context.hasSameType(Real->getType(), Imag->getType())) { |
9262 | 0 | return Diag(Real->getBeginLoc(), |
9263 | 0 | diag::err_typecheck_call_different_arg_types) |
9264 | 0 | << Real->getType() << Imag->getType() |
9265 | 0 | << Real->getSourceRange() << Imag->getSourceRange(); |
9266 | 0 | } |
9267 | | |
9268 | | // We don't allow _Complex _Float16 nor _Complex __fp16 as type specifiers; |
9269 | | // don't allow this builtin to form those types either. |
9270 | | // FIXME: Should we allow these types? |
9271 | 0 | if (Real->getType()->isFloat16Type()) |
9272 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_invalid_complex_spec) |
9273 | 0 | << "_Float16"; |
9274 | 0 | if (Real->getType()->isHalfType()) |
9275 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_invalid_complex_spec) |
9276 | 0 | << "half"; |
9277 | | |
9278 | 0 | TheCall->setType(Context.getComplexType(Real->getType())); |
9279 | 0 | return false; |
9280 | 0 | } |
9281 | | |
9282 | | // Customized Sema Checking for VSX builtins that have the following signature: |
9283 | | // vector [...] builtinName(vector [...], vector [...], const int); |
9284 | | // Which takes the same type of vectors (any legal vector type) for the first |
9285 | | // two arguments and takes compile time constant for the third argument. |
9286 | | // Example builtins are : |
9287 | | // vector double vec_xxpermdi(vector double, vector double, int); |
9288 | | // vector short vec_xxsldwi(vector short, vector short, int); |
9289 | 0 | bool Sema::SemaBuiltinVSX(CallExpr *TheCall) { |
9290 | 0 | unsigned ExpectedNumArgs = 3; |
9291 | 0 | if (checkArgCount(*this, TheCall, ExpectedNumArgs)) |
9292 | 0 | return true; |
9293 | | |
9294 | | // Check the third argument is a compile time constant |
9295 | 0 | if (!TheCall->getArg(2)->isIntegerConstantExpr(Context)) |
9296 | 0 | return Diag(TheCall->getBeginLoc(), |
9297 | 0 | diag::err_vsx_builtin_nonconstant_argument) |
9298 | 0 | << 3 /* argument index */ << TheCall->getDirectCallee() |
9299 | 0 | << SourceRange(TheCall->getArg(2)->getBeginLoc(), |
9300 | 0 | TheCall->getArg(2)->getEndLoc()); |
9301 | | |
9302 | 0 | QualType Arg1Ty = TheCall->getArg(0)->getType(); |
9303 | 0 | QualType Arg2Ty = TheCall->getArg(1)->getType(); |
9304 | | |
9305 | | // Check the type of argument 1 and argument 2 are vectors. |
9306 | 0 | SourceLocation BuiltinLoc = TheCall->getBeginLoc(); |
9307 | 0 | if ((!Arg1Ty->isVectorType() && !Arg1Ty->isDependentType()) || |
9308 | 0 | (!Arg2Ty->isVectorType() && !Arg2Ty->isDependentType())) { |
9309 | 0 | return Diag(BuiltinLoc, diag::err_vec_builtin_non_vector) |
9310 | 0 | << TheCall->getDirectCallee() |
9311 | 0 | << SourceRange(TheCall->getArg(0)->getBeginLoc(), |
9312 | 0 | TheCall->getArg(1)->getEndLoc()); |
9313 | 0 | } |
9314 | | |
9315 | | // Check the first two arguments are the same type. |
9316 | 0 | if (!Context.hasSameUnqualifiedType(Arg1Ty, Arg2Ty)) { |
9317 | 0 | return Diag(BuiltinLoc, diag::err_vec_builtin_incompatible_vector) |
9318 | 0 | << TheCall->getDirectCallee() |
9319 | 0 | << SourceRange(TheCall->getArg(0)->getBeginLoc(), |
9320 | 0 | TheCall->getArg(1)->getEndLoc()); |
9321 | 0 | } |
9322 | | |
9323 | | // When default clang type checking is turned off and the customized type |
9324 | | // checking is used, the returning type of the function must be explicitly |
9325 | | // set. Otherwise it is _Bool by default. |
9326 | 0 | TheCall->setType(Arg1Ty); |
9327 | |
|
9328 | 0 | return false; |
9329 | 0 | } |
9330 | | |
9331 | | /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. |
9332 | | // This is declared to take (...), so we have to check everything. |
9333 | 0 | ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { |
9334 | 0 | if (TheCall->getNumArgs() < 2) |
9335 | 0 | return ExprError(Diag(TheCall->getEndLoc(), |
9336 | 0 | diag::err_typecheck_call_too_few_args_at_least) |
9337 | 0 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
9338 | 0 | << /*is non object*/ 0 << TheCall->getSourceRange()); |
9339 | | |
9340 | | // Determine which of the following types of shufflevector we're checking: |
9341 | | // 1) unary, vector mask: (lhs, mask) |
9342 | | // 2) binary, scalar mask: (lhs, rhs, index, ..., index) |
9343 | 0 | QualType resType = TheCall->getArg(0)->getType(); |
9344 | 0 | unsigned numElements = 0; |
9345 | |
|
9346 | 0 | if (!TheCall->getArg(0)->isTypeDependent() && |
9347 | 0 | !TheCall->getArg(1)->isTypeDependent()) { |
9348 | 0 | QualType LHSType = TheCall->getArg(0)->getType(); |
9349 | 0 | QualType RHSType = TheCall->getArg(1)->getType(); |
9350 | |
|
9351 | 0 | if (!LHSType->isVectorType() || !RHSType->isVectorType()) |
9352 | 0 | return ExprError( |
9353 | 0 | Diag(TheCall->getBeginLoc(), diag::err_vec_builtin_non_vector) |
9354 | 0 | << TheCall->getDirectCallee() |
9355 | 0 | << SourceRange(TheCall->getArg(0)->getBeginLoc(), |
9356 | 0 | TheCall->getArg(1)->getEndLoc())); |
9357 | | |
9358 | 0 | numElements = LHSType->castAs<VectorType>()->getNumElements(); |
9359 | 0 | unsigned numResElements = TheCall->getNumArgs() - 2; |
9360 | | |
9361 | | // Check to see if we have a call with 2 vector arguments, the unary shuffle |
9362 | | // with mask. If so, verify that RHS is an integer vector type with the |
9363 | | // same number of elts as lhs. |
9364 | 0 | if (TheCall->getNumArgs() == 2) { |
9365 | 0 | if (!RHSType->hasIntegerRepresentation() || |
9366 | 0 | RHSType->castAs<VectorType>()->getNumElements() != numElements) |
9367 | 0 | return ExprError(Diag(TheCall->getBeginLoc(), |
9368 | 0 | diag::err_vec_builtin_incompatible_vector) |
9369 | 0 | << TheCall->getDirectCallee() |
9370 | 0 | << SourceRange(TheCall->getArg(1)->getBeginLoc(), |
9371 | 0 | TheCall->getArg(1)->getEndLoc())); |
9372 | 0 | } else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { |
9373 | 0 | return ExprError(Diag(TheCall->getBeginLoc(), |
9374 | 0 | diag::err_vec_builtin_incompatible_vector) |
9375 | 0 | << TheCall->getDirectCallee() |
9376 | 0 | << SourceRange(TheCall->getArg(0)->getBeginLoc(), |
9377 | 0 | TheCall->getArg(1)->getEndLoc())); |
9378 | 0 | } else if (numElements != numResElements) { |
9379 | 0 | QualType eltType = LHSType->castAs<VectorType>()->getElementType(); |
9380 | 0 | resType = |
9381 | 0 | Context.getVectorType(eltType, numResElements, VectorKind::Generic); |
9382 | 0 | } |
9383 | 0 | } |
9384 | | |
9385 | 0 | for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { |
9386 | 0 | if (TheCall->getArg(i)->isTypeDependent() || |
9387 | 0 | TheCall->getArg(i)->isValueDependent()) |
9388 | 0 | continue; |
9389 | | |
9390 | 0 | std::optional<llvm::APSInt> Result; |
9391 | 0 | if (!(Result = TheCall->getArg(i)->getIntegerConstantExpr(Context))) |
9392 | 0 | return ExprError(Diag(TheCall->getBeginLoc(), |
9393 | 0 | diag::err_shufflevector_nonconstant_argument) |
9394 | 0 | << TheCall->getArg(i)->getSourceRange()); |
9395 | | |
9396 | | // Allow -1 which will be translated to undef in the IR. |
9397 | 0 | if (Result->isSigned() && Result->isAllOnes()) |
9398 | 0 | continue; |
9399 | | |
9400 | 0 | if (Result->getActiveBits() > 64 || |
9401 | 0 | Result->getZExtValue() >= numElements * 2) |
9402 | 0 | return ExprError(Diag(TheCall->getBeginLoc(), |
9403 | 0 | diag::err_shufflevector_argument_too_large) |
9404 | 0 | << TheCall->getArg(i)->getSourceRange()); |
9405 | 0 | } |
9406 | | |
9407 | 0 | SmallVector<Expr*, 32> exprs; |
9408 | |
|
9409 | 0 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { |
9410 | 0 | exprs.push_back(TheCall->getArg(i)); |
9411 | 0 | TheCall->setArg(i, nullptr); |
9412 | 0 | } |
9413 | |
|
9414 | 0 | return new (Context) ShuffleVectorExpr(Context, exprs, resType, |
9415 | 0 | TheCall->getCallee()->getBeginLoc(), |
9416 | 0 | TheCall->getRParenLoc()); |
9417 | 0 | } |
9418 | | |
9419 | | /// SemaConvertVectorExpr - Handle __builtin_convertvector |
9420 | | ExprResult Sema::SemaConvertVectorExpr(Expr *E, TypeSourceInfo *TInfo, |
9421 | | SourceLocation BuiltinLoc, |
9422 | 0 | SourceLocation RParenLoc) { |
9423 | 0 | ExprValueKind VK = VK_PRValue; |
9424 | 0 | ExprObjectKind OK = OK_Ordinary; |
9425 | 0 | QualType DstTy = TInfo->getType(); |
9426 | 0 | QualType SrcTy = E->getType(); |
9427 | |
|
9428 | 0 | if (!SrcTy->isVectorType() && !SrcTy->isDependentType()) |
9429 | 0 | return ExprError(Diag(BuiltinLoc, |
9430 | 0 | diag::err_convertvector_non_vector) |
9431 | 0 | << E->getSourceRange()); |
9432 | 0 | if (!DstTy->isVectorType() && !DstTy->isDependentType()) |
9433 | 0 | return ExprError(Diag(BuiltinLoc, diag::err_builtin_non_vector_type) |
9434 | 0 | << "second" |
9435 | 0 | << "__builtin_convertvector"); |
9436 | | |
9437 | 0 | if (!SrcTy->isDependentType() && !DstTy->isDependentType()) { |
9438 | 0 | unsigned SrcElts = SrcTy->castAs<VectorType>()->getNumElements(); |
9439 | 0 | unsigned DstElts = DstTy->castAs<VectorType>()->getNumElements(); |
9440 | 0 | if (SrcElts != DstElts) |
9441 | 0 | return ExprError(Diag(BuiltinLoc, |
9442 | 0 | diag::err_convertvector_incompatible_vector) |
9443 | 0 | << E->getSourceRange()); |
9444 | 0 | } |
9445 | | |
9446 | 0 | return new (Context) |
9447 | 0 | ConvertVectorExpr(E, TInfo, DstTy, VK, OK, BuiltinLoc, RParenLoc); |
9448 | 0 | } |
9449 | | |
9450 | | /// SemaBuiltinPrefetch - Handle __builtin_prefetch. |
9451 | | // This is declared to take (const void*, ...) and can take two |
9452 | | // optional constant int args. |
9453 | 0 | bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { |
9454 | 0 | unsigned NumArgs = TheCall->getNumArgs(); |
9455 | |
|
9456 | 0 | if (NumArgs > 3) |
9457 | 0 | return Diag(TheCall->getEndLoc(), |
9458 | 0 | diag::err_typecheck_call_too_many_args_at_most) |
9459 | 0 | << 0 /*function call*/ << 3 << NumArgs << /*is non object*/ 0 |
9460 | 0 | << TheCall->getSourceRange(); |
9461 | | |
9462 | | // Argument 0 is checked for us and the remaining arguments must be |
9463 | | // constant integers. |
9464 | 0 | for (unsigned i = 1; i != NumArgs; ++i) |
9465 | 0 | if (SemaBuiltinConstantArgRange(TheCall, i, 0, i == 1 ? 1 : 3)) |
9466 | 0 | return true; |
9467 | | |
9468 | 0 | return false; |
9469 | 0 | } |
9470 | | |
9471 | | /// SemaBuiltinArithmeticFence - Handle __arithmetic_fence. |
9472 | 0 | bool Sema::SemaBuiltinArithmeticFence(CallExpr *TheCall) { |
9473 | 0 | if (!Context.getTargetInfo().checkArithmeticFenceSupported()) |
9474 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported) |
9475 | 0 | << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); |
9476 | 0 | if (checkArgCount(*this, TheCall, 1)) |
9477 | 0 | return true; |
9478 | 0 | Expr *Arg = TheCall->getArg(0); |
9479 | 0 | if (Arg->isInstantiationDependent()) |
9480 | 0 | return false; |
9481 | | |
9482 | 0 | QualType ArgTy = Arg->getType(); |
9483 | 0 | if (!ArgTy->hasFloatingRepresentation()) |
9484 | 0 | return Diag(TheCall->getEndLoc(), diag::err_typecheck_expect_flt_or_vector) |
9485 | 0 | << ArgTy; |
9486 | 0 | if (Arg->isLValue()) { |
9487 | 0 | ExprResult FirstArg = DefaultLvalueConversion(Arg); |
9488 | 0 | TheCall->setArg(0, FirstArg.get()); |
9489 | 0 | } |
9490 | 0 | TheCall->setType(TheCall->getArg(0)->getType()); |
9491 | 0 | return false; |
9492 | 0 | } |
9493 | | |
9494 | | /// SemaBuiltinAssume - Handle __assume (MS Extension). |
9495 | | // __assume does not evaluate its arguments, and should warn if its argument |
9496 | | // has side effects. |
9497 | 0 | bool Sema::SemaBuiltinAssume(CallExpr *TheCall) { |
9498 | 0 | Expr *Arg = TheCall->getArg(0); |
9499 | 0 | if (Arg->isInstantiationDependent()) return false; |
9500 | | |
9501 | 0 | if (Arg->HasSideEffects(Context)) |
9502 | 0 | Diag(Arg->getBeginLoc(), diag::warn_assume_side_effects) |
9503 | 0 | << Arg->getSourceRange() |
9504 | 0 | << cast<FunctionDecl>(TheCall->getCalleeDecl())->getIdentifier(); |
9505 | |
|
9506 | 0 | return false; |
9507 | 0 | } |
9508 | | |
9509 | | /// Handle __builtin_alloca_with_align. This is declared |
9510 | | /// as (size_t, size_t) where the second size_t must be a power of 2 greater |
9511 | | /// than 8. |
9512 | 0 | bool Sema::SemaBuiltinAllocaWithAlign(CallExpr *TheCall) { |
9513 | | // The alignment must be a constant integer. |
9514 | 0 | Expr *Arg = TheCall->getArg(1); |
9515 | | |
9516 | | // We can't check the value of a dependent argument. |
9517 | 0 | if (!Arg->isTypeDependent() && !Arg->isValueDependent()) { |
9518 | 0 | if (const auto *UE = |
9519 | 0 | dyn_cast<UnaryExprOrTypeTraitExpr>(Arg->IgnoreParenImpCasts())) |
9520 | 0 | if (UE->getKind() == UETT_AlignOf || |
9521 | 0 | UE->getKind() == UETT_PreferredAlignOf) |
9522 | 0 | Diag(TheCall->getBeginLoc(), diag::warn_alloca_align_alignof) |
9523 | 0 | << Arg->getSourceRange(); |
9524 | |
|
9525 | 0 | llvm::APSInt Result = Arg->EvaluateKnownConstInt(Context); |
9526 | |
|
9527 | 0 | if (!Result.isPowerOf2()) |
9528 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_alignment_not_power_of_two) |
9529 | 0 | << Arg->getSourceRange(); |
9530 | | |
9531 | 0 | if (Result < Context.getCharWidth()) |
9532 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_alignment_too_small) |
9533 | 0 | << (unsigned)Context.getCharWidth() << Arg->getSourceRange(); |
9534 | | |
9535 | 0 | if (Result > std::numeric_limits<int32_t>::max()) |
9536 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_alignment_too_big) |
9537 | 0 | << std::numeric_limits<int32_t>::max() << Arg->getSourceRange(); |
9538 | 0 | } |
9539 | | |
9540 | 0 | return false; |
9541 | 0 | } |
9542 | | |
9543 | | /// Handle __builtin_assume_aligned. This is declared |
9544 | | /// as (const void*, size_t, ...) and can take one optional constant int arg. |
9545 | 0 | bool Sema::SemaBuiltinAssumeAligned(CallExpr *TheCall) { |
9546 | 0 | if (checkArgCountRange(*this, TheCall, 2, 3)) |
9547 | 0 | return true; |
9548 | | |
9549 | 0 | unsigned NumArgs = TheCall->getNumArgs(); |
9550 | 0 | Expr *FirstArg = TheCall->getArg(0); |
9551 | |
|
9552 | 0 | { |
9553 | 0 | ExprResult FirstArgResult = |
9554 | 0 | DefaultFunctionArrayLvalueConversion(FirstArg); |
9555 | 0 | if (checkBuiltinArgument(*this, TheCall, 0)) |
9556 | 0 | return true; |
9557 | | /// In-place updation of FirstArg by checkBuiltinArgument is ignored. |
9558 | 0 | TheCall->setArg(0, FirstArgResult.get()); |
9559 | 0 | } |
9560 | | |
9561 | | // The alignment must be a constant integer. |
9562 | 0 | Expr *SecondArg = TheCall->getArg(1); |
9563 | | |
9564 | | // We can't check the value of a dependent argument. |
9565 | 0 | if (!SecondArg->isValueDependent()) { |
9566 | 0 | llvm::APSInt Result; |
9567 | 0 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
9568 | 0 | return true; |
9569 | | |
9570 | 0 | if (!Result.isPowerOf2()) |
9571 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_alignment_not_power_of_two) |
9572 | 0 | << SecondArg->getSourceRange(); |
9573 | | |
9574 | 0 | if (Result > Sema::MaximumAlignment) |
9575 | 0 | Diag(TheCall->getBeginLoc(), diag::warn_assume_aligned_too_great) |
9576 | 0 | << SecondArg->getSourceRange() << Sema::MaximumAlignment; |
9577 | 0 | } |
9578 | | |
9579 | 0 | if (NumArgs > 2) { |
9580 | 0 | Expr *ThirdArg = TheCall->getArg(2); |
9581 | 0 | if (convertArgumentToType(*this, ThirdArg, Context.getSizeType())) |
9582 | 0 | return true; |
9583 | 0 | TheCall->setArg(2, ThirdArg); |
9584 | 0 | } |
9585 | | |
9586 | 0 | return false; |
9587 | 0 | } |
9588 | | |
9589 | 0 | bool Sema::SemaBuiltinOSLogFormat(CallExpr *TheCall) { |
9590 | 0 | unsigned BuiltinID = |
9591 | 0 | cast<FunctionDecl>(TheCall->getCalleeDecl())->getBuiltinID(); |
9592 | 0 | bool IsSizeCall = BuiltinID == Builtin::BI__builtin_os_log_format_buffer_size; |
9593 | |
|
9594 | 0 | unsigned NumArgs = TheCall->getNumArgs(); |
9595 | 0 | unsigned NumRequiredArgs = IsSizeCall ? 1 : 2; |
9596 | 0 | if (NumArgs < NumRequiredArgs) { |
9597 | 0 | return Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args) |
9598 | 0 | << 0 /* function call */ << NumRequiredArgs << NumArgs |
9599 | 0 | << /*is non object*/ 0 << TheCall->getSourceRange(); |
9600 | 0 | } |
9601 | 0 | if (NumArgs >= NumRequiredArgs + 0x100) { |
9602 | 0 | return Diag(TheCall->getEndLoc(), |
9603 | 0 | diag::err_typecheck_call_too_many_args_at_most) |
9604 | 0 | << 0 /* function call */ << (NumRequiredArgs + 0xff) << NumArgs |
9605 | 0 | << /*is non object*/ 0 << TheCall->getSourceRange(); |
9606 | 0 | } |
9607 | 0 | unsigned i = 0; |
9608 | | |
9609 | | // For formatting call, check buffer arg. |
9610 | 0 | if (!IsSizeCall) { |
9611 | 0 | ExprResult Arg(TheCall->getArg(i)); |
9612 | 0 | InitializedEntity Entity = InitializedEntity::InitializeParameter( |
9613 | 0 | Context, Context.VoidPtrTy, false); |
9614 | 0 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
9615 | 0 | if (Arg.isInvalid()) |
9616 | 0 | return true; |
9617 | 0 | TheCall->setArg(i, Arg.get()); |
9618 | 0 | i++; |
9619 | 0 | } |
9620 | | |
9621 | | // Check string literal arg. |
9622 | 0 | unsigned FormatIdx = i; |
9623 | 0 | { |
9624 | 0 | ExprResult Arg = CheckOSLogFormatStringArg(TheCall->getArg(i)); |
9625 | 0 | if (Arg.isInvalid()) |
9626 | 0 | return true; |
9627 | 0 | TheCall->setArg(i, Arg.get()); |
9628 | 0 | i++; |
9629 | 0 | } |
9630 | | |
9631 | | // Make sure variadic args are scalar. |
9632 | 0 | unsigned FirstDataArg = i; |
9633 | 0 | while (i < NumArgs) { |
9634 | 0 | ExprResult Arg = DefaultVariadicArgumentPromotion( |
9635 | 0 | TheCall->getArg(i), VariadicFunction, nullptr); |
9636 | 0 | if (Arg.isInvalid()) |
9637 | 0 | return true; |
9638 | 0 | CharUnits ArgSize = Context.getTypeSizeInChars(Arg.get()->getType()); |
9639 | 0 | if (ArgSize.getQuantity() >= 0x100) { |
9640 | 0 | return Diag(Arg.get()->getEndLoc(), diag::err_os_log_argument_too_big) |
9641 | 0 | << i << (int)ArgSize.getQuantity() << 0xff |
9642 | 0 | << TheCall->getSourceRange(); |
9643 | 0 | } |
9644 | 0 | TheCall->setArg(i, Arg.get()); |
9645 | 0 | i++; |
9646 | 0 | } |
9647 | | |
9648 | | // Check formatting specifiers. NOTE: We're only doing this for the non-size |
9649 | | // call to avoid duplicate diagnostics. |
9650 | 0 | if (!IsSizeCall) { |
9651 | 0 | llvm::SmallBitVector CheckedVarArgs(NumArgs, false); |
9652 | 0 | ArrayRef<const Expr *> Args(TheCall->getArgs(), TheCall->getNumArgs()); |
9653 | 0 | bool Success = CheckFormatArguments( |
9654 | 0 | Args, FAPK_Variadic, FormatIdx, FirstDataArg, FST_OSLog, |
9655 | 0 | VariadicFunction, TheCall->getBeginLoc(), SourceRange(), |
9656 | 0 | CheckedVarArgs); |
9657 | 0 | if (!Success) |
9658 | 0 | return true; |
9659 | 0 | } |
9660 | | |
9661 | 0 | if (IsSizeCall) { |
9662 | 0 | TheCall->setType(Context.getSizeType()); |
9663 | 0 | } else { |
9664 | 0 | TheCall->setType(Context.VoidPtrTy); |
9665 | 0 | } |
9666 | 0 | return false; |
9667 | 0 | } |
9668 | | |
9669 | | /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr |
9670 | | /// TheCall is a constant expression. |
9671 | | bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum, |
9672 | 0 | llvm::APSInt &Result) { |
9673 | 0 | Expr *Arg = TheCall->getArg(ArgNum); |
9674 | 0 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
9675 | 0 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
9676 | |
|
9677 | 0 | if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; |
9678 | | |
9679 | 0 | std::optional<llvm::APSInt> R; |
9680 | 0 | if (!(R = Arg->getIntegerConstantExpr(Context))) |
9681 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_constant_integer_arg_type) |
9682 | 0 | << FDecl->getDeclName() << Arg->getSourceRange(); |
9683 | 0 | Result = *R; |
9684 | 0 | return false; |
9685 | 0 | } |
9686 | | |
9687 | | /// SemaBuiltinConstantArgRange - Handle a check if argument ArgNum of CallExpr |
9688 | | /// TheCall is a constant expression in the range [Low, High]. |
9689 | | bool Sema::SemaBuiltinConstantArgRange(CallExpr *TheCall, int ArgNum, |
9690 | 0 | int Low, int High, bool RangeIsError) { |
9691 | 0 | if (isConstantEvaluatedContext()) |
9692 | 0 | return false; |
9693 | 0 | llvm::APSInt Result; |
9694 | | |
9695 | | // We can't check the value of a dependent argument. |
9696 | 0 | Expr *Arg = TheCall->getArg(ArgNum); |
9697 | 0 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
9698 | 0 | return false; |
9699 | | |
9700 | | // Check constant-ness first. |
9701 | 0 | if (SemaBuiltinConstantArg(TheCall, ArgNum, Result)) |
9702 | 0 | return true; |
9703 | | |
9704 | 0 | if (Result.getSExtValue() < Low || Result.getSExtValue() > High) { |
9705 | 0 | if (RangeIsError) |
9706 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_argument_invalid_range) |
9707 | 0 | << toString(Result, 10) << Low << High << Arg->getSourceRange(); |
9708 | 0 | else |
9709 | | // Defer the warning until we know if the code will be emitted so that |
9710 | | // dead code can ignore this. |
9711 | 0 | DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, |
9712 | 0 | PDiag(diag::warn_argument_invalid_range) |
9713 | 0 | << toString(Result, 10) << Low << High |
9714 | 0 | << Arg->getSourceRange()); |
9715 | 0 | } |
9716 | | |
9717 | 0 | return false; |
9718 | 0 | } |
9719 | | |
9720 | | /// SemaBuiltinConstantArgMultiple - Handle a check if argument ArgNum of CallExpr |
9721 | | /// TheCall is a constant expression is a multiple of Num.. |
9722 | | bool Sema::SemaBuiltinConstantArgMultiple(CallExpr *TheCall, int ArgNum, |
9723 | 0 | unsigned Num) { |
9724 | 0 | llvm::APSInt Result; |
9725 | | |
9726 | | // We can't check the value of a dependent argument. |
9727 | 0 | Expr *Arg = TheCall->getArg(ArgNum); |
9728 | 0 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
9729 | 0 | return false; |
9730 | | |
9731 | | // Check constant-ness first. |
9732 | 0 | if (SemaBuiltinConstantArg(TheCall, ArgNum, Result)) |
9733 | 0 | return true; |
9734 | | |
9735 | 0 | if (Result.getSExtValue() % Num != 0) |
9736 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_argument_not_multiple) |
9737 | 0 | << Num << Arg->getSourceRange(); |
9738 | | |
9739 | 0 | return false; |
9740 | 0 | } |
9741 | | |
9742 | | /// SemaBuiltinConstantArgPower2 - Check if argument ArgNum of TheCall is a |
9743 | | /// constant expression representing a power of 2. |
9744 | 0 | bool Sema::SemaBuiltinConstantArgPower2(CallExpr *TheCall, int ArgNum) { |
9745 | 0 | llvm::APSInt Result; |
9746 | | |
9747 | | // We can't check the value of a dependent argument. |
9748 | 0 | Expr *Arg = TheCall->getArg(ArgNum); |
9749 | 0 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
9750 | 0 | return false; |
9751 | | |
9752 | | // Check constant-ness first. |
9753 | 0 | if (SemaBuiltinConstantArg(TheCall, ArgNum, Result)) |
9754 | 0 | return true; |
9755 | | |
9756 | | // Bit-twiddling to test for a power of 2: for x > 0, x & (x-1) is zero if |
9757 | | // and only if x is a power of 2. |
9758 | 0 | if (Result.isStrictlyPositive() && (Result & (Result - 1)) == 0) |
9759 | 0 | return false; |
9760 | | |
9761 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_argument_not_power_of_2) |
9762 | 0 | << Arg->getSourceRange(); |
9763 | 0 | } |
9764 | | |
9765 | 0 | static bool IsShiftedByte(llvm::APSInt Value) { |
9766 | 0 | if (Value.isNegative()) |
9767 | 0 | return false; |
9768 | | |
9769 | | // Check if it's a shifted byte, by shifting it down |
9770 | 0 | while (true) { |
9771 | | // If the value fits in the bottom byte, the check passes. |
9772 | 0 | if (Value < 0x100) |
9773 | 0 | return true; |
9774 | | |
9775 | | // Otherwise, if the value has _any_ bits in the bottom byte, the check |
9776 | | // fails. |
9777 | 0 | if ((Value & 0xFF) != 0) |
9778 | 0 | return false; |
9779 | | |
9780 | | // If the bottom 8 bits are all 0, but something above that is nonzero, |
9781 | | // then shifting the value right by 8 bits won't affect whether it's a |
9782 | | // shifted byte or not. So do that, and go round again. |
9783 | 0 | Value >>= 8; |
9784 | 0 | } |
9785 | 0 | } |
9786 | | |
9787 | | /// SemaBuiltinConstantArgShiftedByte - Check if argument ArgNum of TheCall is |
9788 | | /// a constant expression representing an arbitrary byte value shifted left by |
9789 | | /// a multiple of 8 bits. |
9790 | | bool Sema::SemaBuiltinConstantArgShiftedByte(CallExpr *TheCall, int ArgNum, |
9791 | 0 | unsigned ArgBits) { |
9792 | 0 | llvm::APSInt Result; |
9793 | | |
9794 | | // We can't check the value of a dependent argument. |
9795 | 0 | Expr *Arg = TheCall->getArg(ArgNum); |
9796 | 0 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
9797 | 0 | return false; |
9798 | | |
9799 | | // Check constant-ness first. |
9800 | 0 | if (SemaBuiltinConstantArg(TheCall, ArgNum, Result)) |
9801 | 0 | return true; |
9802 | | |
9803 | | // Truncate to the given size. |
9804 | 0 | Result = Result.getLoBits(ArgBits); |
9805 | 0 | Result.setIsUnsigned(true); |
9806 | |
|
9807 | 0 | if (IsShiftedByte(Result)) |
9808 | 0 | return false; |
9809 | | |
9810 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_argument_not_shifted_byte) |
9811 | 0 | << Arg->getSourceRange(); |
9812 | 0 | } |
9813 | | |
9814 | | /// SemaBuiltinConstantArgShiftedByteOr0xFF - Check if argument ArgNum of |
9815 | | /// TheCall is a constant expression representing either a shifted byte value, |
9816 | | /// or a value of the form 0x??FF (i.e. a member of the arithmetic progression |
9817 | | /// 0x00FF, 0x01FF, ..., 0xFFFF). This strange range check is needed for some |
9818 | | /// Arm MVE intrinsics. |
9819 | | bool Sema::SemaBuiltinConstantArgShiftedByteOrXXFF(CallExpr *TheCall, |
9820 | | int ArgNum, |
9821 | 0 | unsigned ArgBits) { |
9822 | 0 | llvm::APSInt Result; |
9823 | | |
9824 | | // We can't check the value of a dependent argument. |
9825 | 0 | Expr *Arg = TheCall->getArg(ArgNum); |
9826 | 0 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
9827 | 0 | return false; |
9828 | | |
9829 | | // Check constant-ness first. |
9830 | 0 | if (SemaBuiltinConstantArg(TheCall, ArgNum, Result)) |
9831 | 0 | return true; |
9832 | | |
9833 | | // Truncate to the given size. |
9834 | 0 | Result = Result.getLoBits(ArgBits); |
9835 | 0 | Result.setIsUnsigned(true); |
9836 | | |
9837 | | // Check to see if it's in either of the required forms. |
9838 | 0 | if (IsShiftedByte(Result) || |
9839 | 0 | (Result > 0 && Result < 0x10000 && (Result & 0xFF) == 0xFF)) |
9840 | 0 | return false; |
9841 | | |
9842 | 0 | return Diag(TheCall->getBeginLoc(), |
9843 | 0 | diag::err_argument_not_shifted_byte_or_xxff) |
9844 | 0 | << Arg->getSourceRange(); |
9845 | 0 | } |
9846 | | |
9847 | | /// SemaBuiltinARMMemoryTaggingCall - Handle calls of memory tagging extensions |
9848 | 0 | bool Sema::SemaBuiltinARMMemoryTaggingCall(unsigned BuiltinID, CallExpr *TheCall) { |
9849 | 0 | if (BuiltinID == AArch64::BI__builtin_arm_irg) { |
9850 | 0 | if (checkArgCount(*this, TheCall, 2)) |
9851 | 0 | return true; |
9852 | 0 | Expr *Arg0 = TheCall->getArg(0); |
9853 | 0 | Expr *Arg1 = TheCall->getArg(1); |
9854 | |
|
9855 | 0 | ExprResult FirstArg = DefaultFunctionArrayLvalueConversion(Arg0); |
9856 | 0 | if (FirstArg.isInvalid()) |
9857 | 0 | return true; |
9858 | 0 | QualType FirstArgType = FirstArg.get()->getType(); |
9859 | 0 | if (!FirstArgType->isAnyPointerType()) |
9860 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_memtag_arg_must_be_pointer) |
9861 | 0 | << "first" << FirstArgType << Arg0->getSourceRange(); |
9862 | 0 | TheCall->setArg(0, FirstArg.get()); |
9863 | |
|
9864 | 0 | ExprResult SecArg = DefaultLvalueConversion(Arg1); |
9865 | 0 | if (SecArg.isInvalid()) |
9866 | 0 | return true; |
9867 | 0 | QualType SecArgType = SecArg.get()->getType(); |
9868 | 0 | if (!SecArgType->isIntegerType()) |
9869 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_memtag_arg_must_be_integer) |
9870 | 0 | << "second" << SecArgType << Arg1->getSourceRange(); |
9871 | | |
9872 | | // Derive the return type from the pointer argument. |
9873 | 0 | TheCall->setType(FirstArgType); |
9874 | 0 | return false; |
9875 | 0 | } |
9876 | | |
9877 | 0 | if (BuiltinID == AArch64::BI__builtin_arm_addg) { |
9878 | 0 | if (checkArgCount(*this, TheCall, 2)) |
9879 | 0 | return true; |
9880 | | |
9881 | 0 | Expr *Arg0 = TheCall->getArg(0); |
9882 | 0 | ExprResult FirstArg = DefaultFunctionArrayLvalueConversion(Arg0); |
9883 | 0 | if (FirstArg.isInvalid()) |
9884 | 0 | return true; |
9885 | 0 | QualType FirstArgType = FirstArg.get()->getType(); |
9886 | 0 | if (!FirstArgType->isAnyPointerType()) |
9887 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_memtag_arg_must_be_pointer) |
9888 | 0 | << "first" << FirstArgType << Arg0->getSourceRange(); |
9889 | 0 | TheCall->setArg(0, FirstArg.get()); |
9890 | | |
9891 | | // Derive the return type from the pointer argument. |
9892 | 0 | TheCall->setType(FirstArgType); |
9893 | | |
9894 | | // Second arg must be an constant in range [0,15] |
9895 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15); |
9896 | 0 | } |
9897 | | |
9898 | 0 | if (BuiltinID == AArch64::BI__builtin_arm_gmi) { |
9899 | 0 | if (checkArgCount(*this, TheCall, 2)) |
9900 | 0 | return true; |
9901 | 0 | Expr *Arg0 = TheCall->getArg(0); |
9902 | 0 | Expr *Arg1 = TheCall->getArg(1); |
9903 | |
|
9904 | 0 | ExprResult FirstArg = DefaultFunctionArrayLvalueConversion(Arg0); |
9905 | 0 | if (FirstArg.isInvalid()) |
9906 | 0 | return true; |
9907 | 0 | QualType FirstArgType = FirstArg.get()->getType(); |
9908 | 0 | if (!FirstArgType->isAnyPointerType()) |
9909 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_memtag_arg_must_be_pointer) |
9910 | 0 | << "first" << FirstArgType << Arg0->getSourceRange(); |
9911 | | |
9912 | 0 | QualType SecArgType = Arg1->getType(); |
9913 | 0 | if (!SecArgType->isIntegerType()) |
9914 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_memtag_arg_must_be_integer) |
9915 | 0 | << "second" << SecArgType << Arg1->getSourceRange(); |
9916 | 0 | TheCall->setType(Context.IntTy); |
9917 | 0 | return false; |
9918 | 0 | } |
9919 | | |
9920 | 0 | if (BuiltinID == AArch64::BI__builtin_arm_ldg || |
9921 | 0 | BuiltinID == AArch64::BI__builtin_arm_stg) { |
9922 | 0 | if (checkArgCount(*this, TheCall, 1)) |
9923 | 0 | return true; |
9924 | 0 | Expr *Arg0 = TheCall->getArg(0); |
9925 | 0 | ExprResult FirstArg = DefaultFunctionArrayLvalueConversion(Arg0); |
9926 | 0 | if (FirstArg.isInvalid()) |
9927 | 0 | return true; |
9928 | | |
9929 | 0 | QualType FirstArgType = FirstArg.get()->getType(); |
9930 | 0 | if (!FirstArgType->isAnyPointerType()) |
9931 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_memtag_arg_must_be_pointer) |
9932 | 0 | << "first" << FirstArgType << Arg0->getSourceRange(); |
9933 | 0 | TheCall->setArg(0, FirstArg.get()); |
9934 | | |
9935 | | // Derive the return type from the pointer argument. |
9936 | 0 | if (BuiltinID == AArch64::BI__builtin_arm_ldg) |
9937 | 0 | TheCall->setType(FirstArgType); |
9938 | 0 | return false; |
9939 | 0 | } |
9940 | | |
9941 | 0 | if (BuiltinID == AArch64::BI__builtin_arm_subp) { |
9942 | 0 | Expr *ArgA = TheCall->getArg(0); |
9943 | 0 | Expr *ArgB = TheCall->getArg(1); |
9944 | |
|
9945 | 0 | ExprResult ArgExprA = DefaultFunctionArrayLvalueConversion(ArgA); |
9946 | 0 | ExprResult ArgExprB = DefaultFunctionArrayLvalueConversion(ArgB); |
9947 | |
|
9948 | 0 | if (ArgExprA.isInvalid() || ArgExprB.isInvalid()) |
9949 | 0 | return true; |
9950 | | |
9951 | 0 | QualType ArgTypeA = ArgExprA.get()->getType(); |
9952 | 0 | QualType ArgTypeB = ArgExprB.get()->getType(); |
9953 | |
|
9954 | 0 | auto isNull = [&] (Expr *E) -> bool { |
9955 | 0 | return E->isNullPointerConstant( |
9956 | 0 | Context, Expr::NPC_ValueDependentIsNotNull); }; |
9957 | | |
9958 | | // argument should be either a pointer or null |
9959 | 0 | if (!ArgTypeA->isAnyPointerType() && !isNull(ArgA)) |
9960 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_memtag_arg_null_or_pointer) |
9961 | 0 | << "first" << ArgTypeA << ArgA->getSourceRange(); |
9962 | | |
9963 | 0 | if (!ArgTypeB->isAnyPointerType() && !isNull(ArgB)) |
9964 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_memtag_arg_null_or_pointer) |
9965 | 0 | << "second" << ArgTypeB << ArgB->getSourceRange(); |
9966 | | |
9967 | | // Ensure Pointee types are compatible |
9968 | 0 | if (ArgTypeA->isAnyPointerType() && !isNull(ArgA) && |
9969 | 0 | ArgTypeB->isAnyPointerType() && !isNull(ArgB)) { |
9970 | 0 | QualType pointeeA = ArgTypeA->getPointeeType(); |
9971 | 0 | QualType pointeeB = ArgTypeB->getPointeeType(); |
9972 | 0 | if (!Context.typesAreCompatible( |
9973 | 0 | Context.getCanonicalType(pointeeA).getUnqualifiedType(), |
9974 | 0 | Context.getCanonicalType(pointeeB).getUnqualifiedType())) { |
9975 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_typecheck_sub_ptr_compatible) |
9976 | 0 | << ArgTypeA << ArgTypeB << ArgA->getSourceRange() |
9977 | 0 | << ArgB->getSourceRange(); |
9978 | 0 | } |
9979 | 0 | } |
9980 | | |
9981 | | // at least one argument should be pointer type |
9982 | 0 | if (!ArgTypeA->isAnyPointerType() && !ArgTypeB->isAnyPointerType()) |
9983 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_memtag_any2arg_pointer) |
9984 | 0 | << ArgTypeA << ArgTypeB << ArgA->getSourceRange(); |
9985 | | |
9986 | 0 | if (isNull(ArgA)) // adopt type of the other pointer |
9987 | 0 | ArgExprA = ImpCastExprToType(ArgExprA.get(), ArgTypeB, CK_NullToPointer); |
9988 | |
|
9989 | 0 | if (isNull(ArgB)) |
9990 | 0 | ArgExprB = ImpCastExprToType(ArgExprB.get(), ArgTypeA, CK_NullToPointer); |
9991 | |
|
9992 | 0 | TheCall->setArg(0, ArgExprA.get()); |
9993 | 0 | TheCall->setArg(1, ArgExprB.get()); |
9994 | 0 | TheCall->setType(Context.LongLongTy); |
9995 | 0 | return false; |
9996 | 0 | } |
9997 | 0 | assert(false && "Unhandled ARM MTE intrinsic"); |
9998 | 0 | return true; |
9999 | 0 | } |
10000 | | |
10001 | | /// SemaBuiltinARMSpecialReg - Handle a check if argument ArgNum of CallExpr |
10002 | | /// TheCall is an ARM/AArch64 special register string literal. |
10003 | | bool Sema::SemaBuiltinARMSpecialReg(unsigned BuiltinID, CallExpr *TheCall, |
10004 | | int ArgNum, unsigned ExpectedFieldNum, |
10005 | 0 | bool AllowName) { |
10006 | 0 | bool IsARMBuiltin = BuiltinID == ARM::BI__builtin_arm_rsr64 || |
10007 | 0 | BuiltinID == ARM::BI__builtin_arm_wsr64 || |
10008 | 0 | BuiltinID == ARM::BI__builtin_arm_rsr || |
10009 | 0 | BuiltinID == ARM::BI__builtin_arm_rsrp || |
10010 | 0 | BuiltinID == ARM::BI__builtin_arm_wsr || |
10011 | 0 | BuiltinID == ARM::BI__builtin_arm_wsrp; |
10012 | 0 | bool IsAArch64Builtin = BuiltinID == AArch64::BI__builtin_arm_rsr64 || |
10013 | 0 | BuiltinID == AArch64::BI__builtin_arm_wsr64 || |
10014 | 0 | BuiltinID == AArch64::BI__builtin_arm_rsr128 || |
10015 | 0 | BuiltinID == AArch64::BI__builtin_arm_wsr128 || |
10016 | 0 | BuiltinID == AArch64::BI__builtin_arm_rsr || |
10017 | 0 | BuiltinID == AArch64::BI__builtin_arm_rsrp || |
10018 | 0 | BuiltinID == AArch64::BI__builtin_arm_wsr || |
10019 | 0 | BuiltinID == AArch64::BI__builtin_arm_wsrp; |
10020 | 0 | assert((IsARMBuiltin || IsAArch64Builtin) && "Unexpected ARM builtin."); |
10021 | | |
10022 | | // We can't check the value of a dependent argument. |
10023 | 0 | Expr *Arg = TheCall->getArg(ArgNum); |
10024 | 0 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
10025 | 0 | return false; |
10026 | | |
10027 | | // Check if the argument is a string literal. |
10028 | 0 | if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts())) |
10029 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal) |
10030 | 0 | << Arg->getSourceRange(); |
10031 | | |
10032 | | // Check the type of special register given. |
10033 | 0 | StringRef Reg = cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString(); |
10034 | 0 | SmallVector<StringRef, 6> Fields; |
10035 | 0 | Reg.split(Fields, ":"); |
10036 | |
|
10037 | 0 | if (Fields.size() != ExpectedFieldNum && !(AllowName && Fields.size() == 1)) |
10038 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_arm_invalid_specialreg) |
10039 | 0 | << Arg->getSourceRange(); |
10040 | | |
10041 | | // If the string is the name of a register then we cannot check that it is |
10042 | | // valid here but if the string is of one the forms described in ACLE then we |
10043 | | // can check that the supplied fields are integers and within the valid |
10044 | | // ranges. |
10045 | 0 | if (Fields.size() > 1) { |
10046 | 0 | bool FiveFields = Fields.size() == 5; |
10047 | |
|
10048 | 0 | bool ValidString = true; |
10049 | 0 | if (IsARMBuiltin) { |
10050 | 0 | ValidString &= Fields[0].starts_with_insensitive("cp") || |
10051 | 0 | Fields[0].starts_with_insensitive("p"); |
10052 | 0 | if (ValidString) |
10053 | 0 | Fields[0] = Fields[0].drop_front( |
10054 | 0 | Fields[0].starts_with_insensitive("cp") ? 2 : 1); |
10055 | |
|
10056 | 0 | ValidString &= Fields[2].starts_with_insensitive("c"); |
10057 | 0 | if (ValidString) |
10058 | 0 | Fields[2] = Fields[2].drop_front(1); |
10059 | |
|
10060 | 0 | if (FiveFields) { |
10061 | 0 | ValidString &= Fields[3].starts_with_insensitive("c"); |
10062 | 0 | if (ValidString) |
10063 | 0 | Fields[3] = Fields[3].drop_front(1); |
10064 | 0 | } |
10065 | 0 | } |
10066 | |
|
10067 | 0 | SmallVector<int, 5> Ranges; |
10068 | 0 | if (FiveFields) |
10069 | 0 | Ranges.append({IsAArch64Builtin ? 1 : 15, 7, 15, 15, 7}); |
10070 | 0 | else |
10071 | 0 | Ranges.append({15, 7, 15}); |
10072 | |
|
10073 | 0 | for (unsigned i=0; i<Fields.size(); ++i) { |
10074 | 0 | int IntField; |
10075 | 0 | ValidString &= !Fields[i].getAsInteger(10, IntField); |
10076 | 0 | ValidString &= (IntField >= 0 && IntField <= Ranges[i]); |
10077 | 0 | } |
10078 | |
|
10079 | 0 | if (!ValidString) |
10080 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_arm_invalid_specialreg) |
10081 | 0 | << Arg->getSourceRange(); |
10082 | 0 | } else if (IsAArch64Builtin && Fields.size() == 1) { |
10083 | | // This code validates writes to PSTATE registers. |
10084 | | |
10085 | | // Not a write. |
10086 | 0 | if (TheCall->getNumArgs() != 2) |
10087 | 0 | return false; |
10088 | | |
10089 | | // The 128-bit system register accesses do not touch PSTATE. |
10090 | 0 | if (BuiltinID == AArch64::BI__builtin_arm_rsr128 || |
10091 | 0 | BuiltinID == AArch64::BI__builtin_arm_wsr128) |
10092 | 0 | return false; |
10093 | | |
10094 | | // These are the named PSTATE accesses using "MSR (immediate)" instructions, |
10095 | | // along with the upper limit on the immediates allowed. |
10096 | 0 | auto MaxLimit = llvm::StringSwitch<std::optional<unsigned>>(Reg) |
10097 | 0 | .CaseLower("spsel", 15) |
10098 | 0 | .CaseLower("daifclr", 15) |
10099 | 0 | .CaseLower("daifset", 15) |
10100 | 0 | .CaseLower("pan", 15) |
10101 | 0 | .CaseLower("uao", 15) |
10102 | 0 | .CaseLower("dit", 15) |
10103 | 0 | .CaseLower("ssbs", 15) |
10104 | 0 | .CaseLower("tco", 15) |
10105 | 0 | .CaseLower("allint", 1) |
10106 | 0 | .CaseLower("pm", 1) |
10107 | 0 | .Default(std::nullopt); |
10108 | | |
10109 | | // If this is not a named PSTATE, just continue without validating, as this |
10110 | | // will be lowered to an "MSR (register)" instruction directly |
10111 | 0 | if (!MaxLimit) |
10112 | 0 | return false; |
10113 | | |
10114 | | // Here we only allow constants in the range for that pstate, as required by |
10115 | | // the ACLE. |
10116 | | // |
10117 | | // While clang also accepts the names of system registers in its ACLE |
10118 | | // intrinsics, we prevent this with the PSTATE names used in MSR (immediate) |
10119 | | // as the value written via a register is different to the value used as an |
10120 | | // immediate to have the same effect. e.g., for the instruction `msr tco, |
10121 | | // x0`, it is bit 25 of register x0 that is written into PSTATE.TCO, but |
10122 | | // with `msr tco, #imm`, it is bit 0 of xN that is written into PSTATE.TCO. |
10123 | | // |
10124 | | // If a programmer wants to codegen the MSR (register) form of `msr tco, |
10125 | | // xN`, they can still do so by specifying the register using five |
10126 | | // colon-separated numbers in a string. |
10127 | 0 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, *MaxLimit); |
10128 | 0 | } |
10129 | | |
10130 | 0 | return false; |
10131 | 0 | } |
10132 | | |
10133 | | /// SemaBuiltinPPCMMACall - Check the call to a PPC MMA builtin for validity. |
10134 | | /// Emit an error and return true on failure; return false on success. |
10135 | | /// TypeStr is a string containing the type descriptor of the value returned by |
10136 | | /// the builtin and the descriptors of the expected type of the arguments. |
10137 | | bool Sema::SemaBuiltinPPCMMACall(CallExpr *TheCall, unsigned BuiltinID, |
10138 | 0 | const char *TypeStr) { |
10139 | |
|
10140 | 0 | assert((TypeStr[0] != '\0') && |
10141 | 0 | "Invalid types in PPC MMA builtin declaration"); |
10142 | | |
10143 | 0 | unsigned Mask = 0; |
10144 | 0 | unsigned ArgNum = 0; |
10145 | | |
10146 | | // The first type in TypeStr is the type of the value returned by the |
10147 | | // builtin. So we first read that type and change the type of TheCall. |
10148 | 0 | QualType type = DecodePPCMMATypeFromStr(Context, TypeStr, Mask); |
10149 | 0 | TheCall->setType(type); |
10150 | |
|
10151 | 0 | while (*TypeStr != '\0') { |
10152 | 0 | Mask = 0; |
10153 | 0 | QualType ExpectedType = DecodePPCMMATypeFromStr(Context, TypeStr, Mask); |
10154 | 0 | if (ArgNum >= TheCall->getNumArgs()) { |
10155 | 0 | ArgNum++; |
10156 | 0 | break; |
10157 | 0 | } |
10158 | | |
10159 | 0 | Expr *Arg = TheCall->getArg(ArgNum); |
10160 | 0 | QualType PassedType = Arg->getType(); |
10161 | 0 | QualType StrippedRVType = PassedType.getCanonicalType(); |
10162 | | |
10163 | | // Strip Restrict/Volatile qualifiers. |
10164 | 0 | if (StrippedRVType.isRestrictQualified() || |
10165 | 0 | StrippedRVType.isVolatileQualified()) |
10166 | 0 | StrippedRVType = StrippedRVType.getCanonicalType().getUnqualifiedType(); |
10167 | | |
10168 | | // The only case where the argument type and expected type are allowed to |
10169 | | // mismatch is if the argument type is a non-void pointer (or array) and |
10170 | | // expected type is a void pointer. |
10171 | 0 | if (StrippedRVType != ExpectedType) |
10172 | 0 | if (!(ExpectedType->isVoidPointerType() && |
10173 | 0 | (StrippedRVType->isPointerType() || StrippedRVType->isArrayType()))) |
10174 | 0 | return Diag(Arg->getBeginLoc(), |
10175 | 0 | diag::err_typecheck_convert_incompatible) |
10176 | 0 | << PassedType << ExpectedType << 1 << 0 << 0; |
10177 | | |
10178 | | // If the value of the Mask is not 0, we have a constraint in the size of |
10179 | | // the integer argument so here we ensure the argument is a constant that |
10180 | | // is in the valid range. |
10181 | 0 | if (Mask != 0 && |
10182 | 0 | SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, Mask, true)) |
10183 | 0 | return true; |
10184 | | |
10185 | 0 | ArgNum++; |
10186 | 0 | } |
10187 | | |
10188 | | // In case we exited early from the previous loop, there are other types to |
10189 | | // read from TypeStr. So we need to read them all to ensure we have the right |
10190 | | // number of arguments in TheCall and if it is not the case, to display a |
10191 | | // better error message. |
10192 | 0 | while (*TypeStr != '\0') { |
10193 | 0 | (void) DecodePPCMMATypeFromStr(Context, TypeStr, Mask); |
10194 | 0 | ArgNum++; |
10195 | 0 | } |
10196 | 0 | if (checkArgCount(*this, TheCall, ArgNum)) |
10197 | 0 | return true; |
10198 | | |
10199 | 0 | return false; |
10200 | 0 | } |
10201 | | |
10202 | | /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). |
10203 | | /// This checks that the target supports __builtin_longjmp and |
10204 | | /// that val is a constant 1. |
10205 | 0 | bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { |
10206 | 0 | if (!Context.getTargetInfo().hasSjLjLowering()) |
10207 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_builtin_longjmp_unsupported) |
10208 | 0 | << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); |
10209 | | |
10210 | 0 | Expr *Arg = TheCall->getArg(1); |
10211 | 0 | llvm::APSInt Result; |
10212 | | |
10213 | | // TODO: This is less than ideal. Overload this to take a value. |
10214 | 0 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
10215 | 0 | return true; |
10216 | | |
10217 | 0 | if (Result != 1) |
10218 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_builtin_longjmp_invalid_val) |
10219 | 0 | << SourceRange(Arg->getBeginLoc(), Arg->getEndLoc()); |
10220 | | |
10221 | 0 | return false; |
10222 | 0 | } |
10223 | | |
10224 | | /// SemaBuiltinSetjmp - Handle __builtin_setjmp(void *env[5]). |
10225 | | /// This checks that the target supports __builtin_setjmp. |
10226 | 0 | bool Sema::SemaBuiltinSetjmp(CallExpr *TheCall) { |
10227 | 0 | if (!Context.getTargetInfo().hasSjLjLowering()) |
10228 | 0 | return Diag(TheCall->getBeginLoc(), diag::err_builtin_setjmp_unsupported) |
10229 | 0 | << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); |
10230 | 0 | return false; |
10231 | 0 | } |
10232 | | |
10233 | | namespace { |
10234 | | |
10235 | | class UncoveredArgHandler { |
10236 | | enum { Unknown = -1, AllCovered = -2 }; |
10237 | | |
10238 | | signed FirstUncoveredArg = Unknown; |
10239 | | SmallVector<const Expr *, 4> DiagnosticExprs; |
10240 | | |
10241 | | public: |
10242 | 0 | UncoveredArgHandler() = default; |
10243 | | |
10244 | 0 | bool hasUncoveredArg() const { |
10245 | 0 | return (FirstUncoveredArg >= 0); |
10246 | 0 | } |
10247 | | |
10248 | 0 | unsigned getUncoveredArg() const { |
10249 | 0 | assert(hasUncoveredArg() && "no uncovered argument"); |
10250 | 0 | return FirstUncoveredArg; |
10251 | 0 | } |
10252 | | |
10253 | 0 | void setAllCovered() { |
10254 | | // A string has been found with all arguments covered, so clear out |
10255 | | // the diagnostics. |
10256 | 0 | DiagnosticExprs.clear(); |
10257 | 0 | FirstUncoveredArg = AllCovered; |
10258 | 0 | } |
10259 | | |
10260 | 0 | void Update(signed NewFirstUncoveredArg, const Expr *StrExpr) { |
10261 | 0 | assert(NewFirstUncoveredArg >= 0 && "Outside range"); |
10262 | | |
10263 | | // Don't update if a previous string covers all arguments. |
10264 | 0 | if (FirstUncoveredArg == AllCovered) |
10265 | 0 | return; |
10266 | | |
10267 | | // UncoveredArgHandler tracks the highest uncovered argument index |
10268 | | // and with it all the strings that match this index. |
10269 | 0 | if (NewFirstUncoveredArg == FirstUncoveredArg) |
10270 | 0 | DiagnosticExprs.push_back(StrExpr); |
10271 | 0 | else if (NewFirstUncoveredArg > FirstUncoveredArg) { |
10272 | 0 | DiagnosticExprs.clear(); |
10273 | 0 | DiagnosticExprs.push_back(StrExpr); |
10274 | 0 | FirstUncoveredArg = NewFirstUncoveredArg; |
10275 | 0 | } |
10276 | 0 | } |
10277 | | |
10278 | | void Diagnose(Sema &S, bool IsFunctionCall, const Expr *ArgExpr); |
10279 | | }; |
10280 | | |
10281 | | enum StringLiteralCheckType { |
10282 | | SLCT_NotALiteral, |
10283 | | SLCT_UncheckedLiteral, |
10284 | | SLCT_CheckedLiteral |
10285 | | }; |
10286 | | |
10287 | | } // namespace |
10288 | | |
10289 | | static void sumOffsets(llvm::APSInt &Offset, llvm::APSInt Addend, |
10290 | | BinaryOperatorKind BinOpKind, |
10291 | 0 | bool AddendIsRight) { |
10292 | 0 | unsigned BitWidth = Offset.getBitWidth(); |
10293 | 0 | unsigned AddendBitWidth = Addend.getBitWidth(); |
10294 | | // There might be negative interim results. |
10295 | 0 | if (Addend.isUnsigned()) { |
10296 | 0 | Addend = Addend.zext(++AddendBitWidth); |
10297 | 0 | Addend.setIsSigned(true); |
10298 | 0 | } |
10299 | | // Adjust the bit width of the APSInts. |
10300 | 0 | if (AddendBitWidth > BitWidth) { |
10301 | 0 | Offset = Offset.sext(AddendBitWidth); |
10302 | 0 | BitWidth = AddendBitWidth; |
10303 | 0 | } else if (BitWidth > AddendBitWidth) { |
10304 | 0 | Addend = Addend.sext(BitWidth); |
10305 | 0 | } |
10306 | |
|
10307 | 0 | bool Ov = false; |
10308 | 0 | llvm::APSInt ResOffset = Offset; |
10309 | 0 | if (BinOpKind == BO_Add) |
10310 | 0 | ResOffset = Offset.sadd_ov(Addend, Ov); |
10311 | 0 | else { |
10312 | 0 | assert(AddendIsRight && BinOpKind == BO_Sub && |
10313 | 0 | "operator must be add or sub with addend on the right"); |
10314 | 0 | ResOffset = Offset.ssub_ov(Addend, Ov); |
10315 | 0 | } |
10316 | | |
10317 | | // We add an offset to a pointer here so we should support an offset as big as |
10318 | | // possible. |
10319 | 0 | if (Ov) { |
10320 | 0 | assert(BitWidth <= std::numeric_limits<unsigned>::max() / 2 && |
10321 | 0 | "index (intermediate) result too big"); |
10322 | 0 | Offset = Offset.sext(2 * BitWidth); |
10323 | 0 | sumOffsets(Offset, Addend, BinOpKind, AddendIsRight); |
10324 | 0 | return; |
10325 | 0 | } |
10326 | | |
10327 | 0 | Offset = ResOffset; |
10328 | 0 | } |
10329 | | |
10330 | | namespace { |
10331 | | |
10332 | | // This is a wrapper class around StringLiteral to support offsetted string |
10333 | | // literals as format strings. It takes the offset into account when returning |
10334 | | // the string and its length or the source locations to display notes correctly. |
10335 | | class FormatStringLiteral { |
10336 | | const StringLiteral *FExpr; |
10337 | | int64_t Offset; |
10338 | | |
10339 | | public: |
10340 | | FormatStringLiteral(const StringLiteral *fexpr, int64_t Offset = 0) |
10341 | 0 | : FExpr(fexpr), Offset(Offset) {} |
10342 | | |
10343 | 0 | StringRef getString() const { |
10344 | 0 | return FExpr->getString().drop_front(Offset); |
10345 | 0 | } |
10346 | | |
10347 | 0 | unsigned getByteLength() const { |
10348 | 0 | return FExpr->getByteLength() - getCharByteWidth() * Offset; |
10349 | 0 | } |
10350 | | |
10351 | 0 | unsigned getLength() const { return FExpr->getLength() - Offset; } |
10352 | 0 | unsigned getCharByteWidth() const { return FExpr->getCharByteWidth(); } |
10353 | | |
10354 | 0 | StringLiteralKind getKind() const { return FExpr->getKind(); } |
10355 | | |
10356 | 0 | QualType getType() const { return FExpr->getType(); } |
10357 | | |
10358 | 0 | bool isAscii() const { return FExpr->isOrdinary(); } |
10359 | 0 | bool isWide() const { return FExpr->isWide(); } |
10360 | 0 | bool isUTF8() const { return FExpr->isUTF8(); } |
10361 | 0 | bool isUTF16() const { return FExpr->isUTF16(); } |
10362 | 0 | bool isUTF32() const { return FExpr->isUTF32(); } |
10363 | 0 | bool isPascal() const { return FExpr->isPascal(); } |
10364 | | |
10365 | | SourceLocation getLocationOfByte( |
10366 | | unsigned ByteNo, const SourceManager &SM, const LangOptions &Features, |
10367 | | const TargetInfo &Target, unsigned *StartToken = nullptr, |
10368 | 0 | unsigned *StartTokenByteOffset = nullptr) const { |
10369 | 0 | return FExpr->getLocationOfByte(ByteNo + Offset, SM, Features, Target, |
10370 | 0 | StartToken, StartTokenByteOffset); |
10371 | 0 | } |
10372 | | |
10373 | 0 | SourceLocation getBeginLoc() const LLVM_READONLY { |
10374 | 0 | return FExpr->getBeginLoc().getLocWithOffset(Offset); |
10375 | 0 | } |
10376 | | |
10377 | 0 | SourceLocation getEndLoc() const LLVM_READONLY { return FExpr->getEndLoc(); } |
10378 | | }; |
10379 | | |
10380 | | } // namespace |
10381 | | |
10382 | | static void CheckFormatString( |
10383 | | Sema &S, const FormatStringLiteral *FExpr, const Expr *OrigFormatExpr, |
10384 | | ArrayRef<const Expr *> Args, Sema::FormatArgumentPassingKind APK, |
10385 | | unsigned format_idx, unsigned firstDataArg, Sema::FormatStringType Type, |
10386 | | bool inFunctionCall, Sema::VariadicCallType CallType, |
10387 | | llvm::SmallBitVector &CheckedVarArgs, UncoveredArgHandler &UncoveredArg, |
10388 | | bool IgnoreStringsWithoutSpecifiers); |
10389 | | |
10390 | | static const Expr *maybeConstEvalStringLiteral(ASTContext &Context, |
10391 | | const Expr *E); |
10392 | | |
10393 | | // Determine if an expression is a string literal or constant string. |
10394 | | // If this function returns false on the arguments to a function expecting a |
10395 | | // format string, we will usually need to emit a warning. |
10396 | | // True string literals are then checked by CheckFormatString. |
10397 | | static StringLiteralCheckType |
10398 | | checkFormatStringExpr(Sema &S, const Expr *E, ArrayRef<const Expr *> Args, |
10399 | | Sema::FormatArgumentPassingKind APK, unsigned format_idx, |
10400 | | unsigned firstDataArg, Sema::FormatStringType Type, |
10401 | | Sema::VariadicCallType CallType, bool InFunctionCall, |
10402 | | llvm::SmallBitVector &CheckedVarArgs, |
10403 | | UncoveredArgHandler &UncoveredArg, llvm::APSInt Offset, |
10404 | 0 | bool IgnoreStringsWithoutSpecifiers = false) { |
10405 | 0 | if (S.isConstantEvaluatedContext()) |
10406 | 0 | return SLCT_NotALiteral; |
10407 | 0 | tryAgain: |
10408 | 0 | assert(Offset.isSigned() && "invalid offset"); |
10409 | | |
10410 | 0 | if (E->isTypeDependent() || E->isValueDependent()) |
10411 | 0 | return SLCT_NotALiteral; |
10412 | | |
10413 | 0 | E = E->IgnoreParenCasts(); |
10414 | |
|
10415 | 0 | if (E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)) |
10416 | | // Technically -Wformat-nonliteral does not warn about this case. |
10417 | | // The behavior of printf and friends in this case is implementation |
10418 | | // dependent. Ideally if the format string cannot be null then |
10419 | | // it should have a 'nonnull' attribute in the function prototype. |
10420 | 0 | return SLCT_UncheckedLiteral; |
10421 | | |
10422 | 0 | switch (E->getStmtClass()) { |
10423 | 0 | case Stmt::InitListExprClass: |
10424 | | // Handle expressions like {"foobar"}. |
10425 | 0 | if (const clang::Expr *SLE = maybeConstEvalStringLiteral(S.Context, E)) { |
10426 | 0 | return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg, |
10427 | 0 | Type, CallType, /*InFunctionCall*/ false, |
10428 | 0 | CheckedVarArgs, UncoveredArg, Offset, |
10429 | 0 | IgnoreStringsWithoutSpecifiers); |
10430 | 0 | } |
10431 | 0 | return SLCT_NotALiteral; |
10432 | 0 | case Stmt::BinaryConditionalOperatorClass: |
10433 | 0 | case Stmt::ConditionalOperatorClass: { |
10434 | | // The expression is a literal if both sub-expressions were, and it was |
10435 | | // completely checked only if both sub-expressions were checked. |
10436 | 0 | const AbstractConditionalOperator *C = |
10437 | 0 | cast<AbstractConditionalOperator>(E); |
10438 | | |
10439 | | // Determine whether it is necessary to check both sub-expressions, for |
10440 | | // example, because the condition expression is a constant that can be |
10441 | | // evaluated at compile time. |
10442 | 0 | bool CheckLeft = true, CheckRight = true; |
10443 | |
|
10444 | 0 | bool Cond; |
10445 | 0 | if (C->getCond()->EvaluateAsBooleanCondition( |
10446 | 0 | Cond, S.getASTContext(), S.isConstantEvaluatedContext())) { |
10447 | 0 | if (Cond) |
10448 | 0 | CheckRight = false; |
10449 | 0 | else |
10450 | 0 | CheckLeft = false; |
10451 | 0 | } |
10452 | | |
10453 | | // We need to maintain the offsets for the right and the left hand side |
10454 | | // separately to check if every possible indexed expression is a valid |
10455 | | // string literal. They might have different offsets for different string |
10456 | | // literals in the end. |
10457 | 0 | StringLiteralCheckType Left; |
10458 | 0 | if (!CheckLeft) |
10459 | 0 | Left = SLCT_UncheckedLiteral; |
10460 | 0 | else { |
10461 | 0 | Left = checkFormatStringExpr(S, C->getTrueExpr(), Args, APK, format_idx, |
10462 | 0 | firstDataArg, Type, CallType, InFunctionCall, |
10463 | 0 | CheckedVarArgs, UncoveredArg, Offset, |
10464 | 0 | IgnoreStringsWithoutSpecifiers); |
10465 | 0 | if (Left == SLCT_NotALiteral || !CheckRight) { |
10466 | 0 | return Left; |
10467 | 0 | } |
10468 | 0 | } |
10469 | | |
10470 | 0 | StringLiteralCheckType Right = checkFormatStringExpr( |
10471 | 0 | S, C->getFalseExpr(), Args, APK, format_idx, firstDataArg, Type, |
10472 | 0 | CallType, InFunctionCall, CheckedVarArgs, UncoveredArg, Offset, |
10473 | 0 | IgnoreStringsWithoutSpecifiers); |
10474 | |
|
10475 | 0 | return (CheckLeft && Left < Right) ? Left : Right; |
10476 | 0 | } |
10477 | | |
10478 | 0 | case Stmt::ImplicitCastExprClass: |
10479 | 0 | E = cast<ImplicitCastExpr>(E)->getSubExpr(); |
10480 | 0 | goto tryAgain; |
10481 | | |
10482 | 0 | case Stmt::OpaqueValueExprClass: |
10483 | 0 | if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) { |
10484 | 0 | E = src; |
10485 | 0 | goto tryAgain; |
10486 | 0 | } |
10487 | 0 | return SLCT_NotALiteral; |
10488 | | |
10489 | 0 | case Stmt::PredefinedExprClass: |
10490 | | // While __func__, etc., are technically not string literals, they |
10491 | | // cannot contain format specifiers and thus are not a security |
10492 | | // liability. |
10493 | 0 | return SLCT_UncheckedLiteral; |
10494 | | |
10495 | 0 | case Stmt::DeclRefExprClass: { |
10496 | 0 | const DeclRefExpr *DR = cast<DeclRefExpr>(E); |
10497 | | |
10498 | | // As an exception, do not flag errors for variables binding to |
10499 | | // const string literals. |
10500 | 0 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
10501 | 0 | bool isConstant = false; |
10502 | 0 | QualType T = DR->getType(); |
10503 | |
|
10504 | 0 | if (const ArrayType *AT = S.Context.getAsArrayType(T)) { |
10505 | 0 | isConstant = AT->getElementType().isConstant(S.Context); |
10506 | 0 | } else if (const PointerType *PT = T->getAs<PointerType>()) { |
10507 | 0 | isConstant = T.isConstant(S.Context) && |
10508 | 0 | PT->getPointeeType().isConstant(S.Context); |
10509 | 0 | } else if (T->isObjCObjectPointerType()) { |
10510 | | // In ObjC, there is usually no "const ObjectPointer" type, |
10511 | | // so don't check if the pointee type is constant. |
10512 | 0 | isConstant = T.isConstant(S.Context); |
10513 | 0 | } |
10514 | |
|
10515 | 0 | if (isConstant) { |
10516 | 0 | if (const Expr *Init = VD->getAnyInitializer()) { |
10517 | | // Look through initializers like const char c[] = { "foo" } |
10518 | 0 | if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { |
10519 | 0 | if (InitList->isStringLiteralInit()) |
10520 | 0 | Init = InitList->getInit(0)->IgnoreParenImpCasts(); |
10521 | 0 | } |
10522 | 0 | return checkFormatStringExpr( |
10523 | 0 | S, Init, Args, APK, format_idx, firstDataArg, Type, CallType, |
10524 | 0 | /*InFunctionCall*/ false, CheckedVarArgs, UncoveredArg, Offset); |
10525 | 0 | } |
10526 | 0 | } |
10527 | | |
10528 | | // When the format argument is an argument of this function, and this |
10529 | | // function also has the format attribute, there are several interactions |
10530 | | // for which there shouldn't be a warning. For instance, when calling |
10531 | | // v*printf from a function that has the printf format attribute, we |
10532 | | // should not emit a warning about using `fmt`, even though it's not |
10533 | | // constant, because the arguments have already been checked for the |
10534 | | // caller of `logmessage`: |
10535 | | // |
10536 | | // __attribute__((format(printf, 1, 2))) |
10537 | | // void logmessage(char const *fmt, ...) { |
10538 | | // va_list ap; |
10539 | | // va_start(ap, fmt); |
10540 | | // vprintf(fmt, ap); /* do not emit a warning about "fmt" */ |
10541 | | // ... |
10542 | | // } |
10543 | | // |
10544 | | // Another interaction that we need to support is calling a variadic |
10545 | | // format function from a format function that has fixed arguments. For |
10546 | | // instance: |
10547 | | // |
10548 | | // __attribute__((format(printf, 1, 2))) |
10549 | | // void logstring(char const *fmt, char const *str) { |
10550 | | // printf(fmt, str); /* do not emit a warning about "fmt" */ |
10551 | | // } |
10552 | | // |
10553 | | // Same (and perhaps more relatably) for the variadic template case: |
10554 | | // |
10555 | | // template<typename... Args> |
10556 | | // __attribute__((format(printf, 1, 2))) |
10557 | | // void log(const char *fmt, Args&&... args) { |
10558 | | // printf(fmt, forward<Args>(args)...); |
10559 | | // /* do not emit a warning about "fmt" */ |
10560 | | // } |
10561 | | // |
10562 | | // Due to implementation difficulty, we only check the format, not the |
10563 | | // format arguments, in all cases. |
10564 | | // |
10565 | 0 | if (const auto *PV = dyn_cast<ParmVarDecl>(VD)) { |
10566 | 0 | if (const auto *D = dyn_cast<Decl>(PV->getDeclContext())) { |
10567 | 0 | for (const auto *PVFormat : D->specific_attrs<FormatAttr>()) { |
10568 | 0 | bool IsCXXMember = false; |
10569 | 0 | if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) |
10570 | 0 | IsCXXMember = MD->isInstance(); |
10571 | |
|
10572 | 0 | bool IsVariadic = false; |
10573 | 0 | if (const FunctionType *FnTy = D->getFunctionType()) |
10574 | 0 | IsVariadic = cast<FunctionProtoType>(FnTy)->isVariadic(); |
10575 | 0 | else if (const auto *BD = dyn_cast<BlockDecl>(D)) |
10576 | 0 | IsVariadic = BD->isVariadic(); |
10577 | 0 | else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) |
10578 | 0 | IsVariadic = OMD->isVariadic(); |
10579 | |
|
10580 | 0 | Sema::FormatStringInfo CallerFSI; |
10581 | 0 | if (Sema::getFormatStringInfo(PVFormat, IsCXXMember, IsVariadic, |
10582 | 0 | &CallerFSI)) { |
10583 | | // We also check if the formats are compatible. |
10584 | | // We can't pass a 'scanf' string to a 'printf' function. |
10585 | 0 | if (PV->getFunctionScopeIndex() == CallerFSI.FormatIdx && |
10586 | 0 | Type == S.GetFormatStringType(PVFormat)) { |
10587 | | // Lastly, check that argument passing kinds transition in a |
10588 | | // way that makes sense: |
10589 | | // from a caller with FAPK_VAList, allow FAPK_VAList |
10590 | | // from a caller with FAPK_Fixed, allow FAPK_Fixed |
10591 | | // from a caller with FAPK_Fixed, allow FAPK_Variadic |
10592 | | // from a caller with FAPK_Variadic, allow FAPK_VAList |
10593 | 0 | switch (combineFAPK(CallerFSI.ArgPassingKind, APK)) { |
10594 | 0 | case combineFAPK(Sema::FAPK_VAList, Sema::FAPK_VAList): |
10595 | 0 | case combineFAPK(Sema::FAPK_Fixed, Sema::FAPK_Fixed): |
10596 | 0 | case combineFAPK(Sema::FAPK_Fixed, Sema::FAPK_Variadic): |
10597 | 0 | case combineFAPK(Sema::FAPK_Variadic, Sema::FAPK_VAList): |
10598 | 0 | return SLCT_UncheckedLiteral; |
10599 | 0 | } |
10600 | 0 | } |
10601 | 0 | } |
10602 | 0 | } |
10603 | 0 | } |
10604 | 0 | } |
10605 | 0 | } |
10606 | | |
10607 | 0 | return SLCT_NotALiteral; |
10608 | 0 | } |
10609 | | |
10610 | 0 | case Stmt::CallExprClass: |
10611 | 0 | case Stmt::CXXMemberCallExprClass: { |
10612 | 0 | const CallExpr *CE = cast<CallExpr>(E); |
10613 | 0 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) { |
10614 | 0 | bool IsFirst = true; |
10615 | 0 | StringLiteralCheckType CommonResult; |
10616 | 0 | for (const auto *FA : ND->specific_attrs<FormatArgAttr>()) { |
10617 | 0 | const Expr *Arg = CE->getArg(FA->getFormatIdx().getASTIndex()); |
10618 | 0 | StringLiteralCheckType Result = checkFormatStringExpr( |
10619 | 0 | S, Arg, Args, APK, format_idx, firstDataArg, Type, CallType, |
10620 | 0 | InFunctionCall, CheckedVarArgs, UncoveredArg, Offset, |
10621 | 0 | IgnoreStringsWithoutSpecifiers); |
10622 | 0 | if (IsFirst) { |
10623 | 0 | CommonResult = Result; |
10624 | 0 | IsFirst = false; |
10625 | 0 | } |
10626 | 0 | } |
10627 | 0 | if (!IsFirst) |
10628 | 0 | return CommonResult; |
10629 | | |
10630 | 0 | if (const auto *FD = dyn_cast<FunctionDecl>(ND)) { |
10631 | 0 | unsigned BuiltinID = FD->getBuiltinID(); |
10632 | 0 | if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString || |
10633 | 0 | BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) { |
10634 | 0 | const Expr *Arg = CE->getArg(0); |
10635 | 0 | return checkFormatStringExpr( |
10636 | 0 | S, Arg, Args, APK, format_idx, firstDataArg, Type, CallType, |
10637 | 0 | InFunctionCall, CheckedVarArgs, UncoveredArg, Offset, |
10638 | 0 | IgnoreStringsWithoutSpecifiers); |
10639 | 0 | } |
10640 | 0 | } |
10641 | 0 | } |
10642 | 0 | if (const Expr *SLE = maybeConstEvalStringLiteral(S.Context, E)) |
10643 | 0 | return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg, |
10644 | 0 | Type, CallType, /*InFunctionCall*/ false, |
10645 | 0 | CheckedVarArgs, UncoveredArg, Offset, |
10646 | 0 | IgnoreStringsWithoutSpecifiers); |
10647 | 0 | return SLCT_NotALiteral; |
10648 | 0 | } |
10649 | 0 | case Stmt::ObjCMessageExprClass: { |
10650 | 0 | const auto *ME = cast<ObjCMessageExpr>(E); |
10651 | 0 | if (const auto *MD = ME->getMethodDecl()) { |
10652 | 0 | if (const auto *FA = MD->getAttr<FormatArgAttr>()) { |
10653 | | // As a special case heuristic, if we're using the method -[NSBundle |
10654 | | // localizedStringForKey:value:table:], ignore any key strings that lack |
10655 | | // format specifiers. The idea is that if the key doesn't have any |
10656 | | // format specifiers then its probably just a key to map to the |
10657 | | // localized strings. If it does have format specifiers though, then its |
10658 | | // likely that the text of the key is the format string in the |
10659 | | // programmer's language, and should be checked. |
10660 | 0 | const ObjCInterfaceDecl *IFace; |
10661 | 0 | if (MD->isInstanceMethod() && (IFace = MD->getClassInterface()) && |
10662 | 0 | IFace->getIdentifier()->isStr("NSBundle") && |
10663 | 0 | MD->getSelector().isKeywordSelector( |
10664 | 0 | {"localizedStringForKey", "value", "table"})) { |
10665 | 0 | IgnoreStringsWithoutSpecifiers = true; |
10666 | 0 | } |
10667 | |
|
10668 | 0 | const Expr *Arg = ME->getArg(FA->getFormatIdx().getASTIndex()); |
10669 | 0 | return checkFormatStringExpr( |
10670 | 0 | S, Arg, Args, APK, format_idx, firstDataArg, Type, CallType, |
10671 | 0 | InFunctionCall, CheckedVarArgs, UncoveredArg, Offset, |
10672 | 0 | IgnoreStringsWithoutSpecifiers); |
10673 | 0 | } |
10674 | 0 | } |
10675 | | |
10676 | 0 | return SLCT_NotALiteral; |
10677 | 0 | } |
10678 | 0 | case Stmt::ObjCStringLiteralClass: |
10679 | 0 | case Stmt::StringLiteralClass: { |
10680 | 0 | const StringLiteral *StrE = nullptr; |
10681 | |
|
10682 | 0 | if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) |
10683 | 0 | StrE = ObjCFExpr->getString(); |
10684 | 0 | else |
10685 | 0 | StrE = cast<StringLiteral>(E); |
10686 | |
|
10687 | 0 | if (StrE) { |
10688 | 0 | if (Offset.isNegative() || Offset > StrE->getLength()) { |
10689 | | // TODO: It would be better to have an explicit warning for out of |
10690 | | // bounds literals. |
10691 | 0 | return SLCT_NotALiteral; |
10692 | 0 | } |
10693 | 0 | FormatStringLiteral FStr(StrE, Offset.sextOrTrunc(64).getSExtValue()); |
10694 | 0 | CheckFormatString(S, &FStr, E, Args, APK, format_idx, firstDataArg, Type, |
10695 | 0 | InFunctionCall, CallType, CheckedVarArgs, UncoveredArg, |
10696 | 0 | IgnoreStringsWithoutSpecifiers); |
10697 | 0 | return SLCT_CheckedLiteral; |
10698 | 0 | } |
10699 | | |
10700 | 0 | return SLCT_NotALiteral; |
10701 | 0 | } |
10702 | 0 | case Stmt::BinaryOperatorClass: { |
10703 | 0 | const BinaryOperator *BinOp = cast<BinaryOperator>(E); |
10704 | | |
10705 | | // A string literal + an int offset is still a string literal. |
10706 | 0 | if (BinOp->isAdditiveOp()) { |
10707 | 0 | Expr::EvalResult LResult, RResult; |
10708 | |
|
10709 | 0 | bool LIsInt = BinOp->getLHS()->EvaluateAsInt( |
10710 | 0 | LResult, S.Context, Expr::SE_NoSideEffects, |
10711 | 0 | S.isConstantEvaluatedContext()); |
10712 | 0 | bool RIsInt = BinOp->getRHS()->EvaluateAsInt( |
10713 | 0 | RResult, S.Context, Expr::SE_NoSideEffects, |
10714 | 0 | S.isConstantEvaluatedContext()); |
10715 | |
|
10716 | 0 | if (LIsInt != RIsInt) { |
10717 | 0 | BinaryOperatorKind BinOpKind = BinOp->getOpcode(); |
10718 | |
|
10719 | 0 | if (LIsInt) { |
10720 | 0 | if (BinOpKind == BO_Add) { |
10721 | 0 | sumOffsets(Offset, LResult.Val.getInt(), BinOpKind, RIsInt); |
10722 | 0 | E = BinOp->getRHS(); |
10723 | 0 | goto tryAgain; |
10724 | 0 | } |
10725 | 0 | } else { |
10726 | 0 | sumOffsets(Offset, RResult.Val.getInt(), BinOpKind, RIsInt); |
10727 | 0 | E = BinOp->getLHS(); |
10728 | 0 | goto tryAgain; |
10729 | 0 | } |
10730 | 0 | } |
10731 | 0 | } |
10732 | | |
10733 | 0 | return SLCT_NotALiteral; |
10734 | 0 | } |
10735 | 0 | case Stmt::UnaryOperatorClass: { |
10736 | 0 | const UnaryOperator *UnaOp = cast<UnaryOperator>(E); |
10737 | 0 | auto ASE = dyn_cast<ArraySubscriptExpr>(UnaOp->getSubExpr()); |
10738 | 0 | if (UnaOp->getOpcode() == UO_AddrOf && ASE) { |
10739 | 0 | Expr::EvalResult IndexResult; |
10740 | 0 | if (ASE->getRHS()->EvaluateAsInt(IndexResult, S.Context, |
10741 | 0 | Expr::SE_NoSideEffects, |
10742 | 0 | S.isConstantEvaluatedContext())) { |
10743 | 0 | sumOffsets(Offset, IndexResult.Val.getInt(), BO_Add, |
10744 | 0 | /*RHS is int*/ true); |
10745 | 0 | E = ASE->getBase(); |
10746 | 0 | goto tryAgain; |
10747 | 0 | } |
10748 | 0 | } |
10749 | | |
10750 | 0 | return SLCT_NotALiteral; |
10751 | 0 | } |
10752 | | |
10753 | 0 | default: |
10754 | 0 | return SLCT_NotALiteral; |
10755 | 0 | } |
10756 | 0 | } |
10757 | | |
10758 | | // If this expression can be evaluated at compile-time, |
10759 | | // check if the result is a StringLiteral and return it |
10760 | | // otherwise return nullptr |
10761 | | static const Expr *maybeConstEvalStringLiteral(ASTContext &Context, |
10762 | 0 | const Expr *E) { |
10763 | 0 | Expr::EvalResult Result; |
10764 | 0 | if (E->EvaluateAsRValue(Result, Context) && Result.Val.isLValue()) { |
10765 | 0 | const auto *LVE = Result.Val.getLValueBase().dyn_cast<const Expr *>(); |
10766 | 0 | if (isa_and_nonnull<StringLiteral>(LVE)) |
10767 | 0 | return LVE; |
10768 | 0 | } |
10769 | 0 | return nullptr; |
10770 | 0 | } |
10771 | | |
10772 | 0 | Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) { |
10773 | 0 | return llvm::StringSwitch<FormatStringType>(Format->getType()->getName()) |
10774 | 0 | .Case("scanf", FST_Scanf) |
10775 | 0 | .Cases("printf", "printf0", FST_Printf) |
10776 | 0 | .Cases("NSString", "CFString", FST_NSString) |
10777 | 0 | .Case("strftime", FST_Strftime) |
10778 | 0 | .Case("strfmon", FST_Strfmon) |
10779 | 0 | .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf) |
10780 | 0 | .Case("freebsd_kprintf", FST_FreeBSDKPrintf) |
10781 | 0 | .Case("os_trace", FST_OSLog) |
10782 | 0 | .Case("os_log", FST_OSLog) |
10783 | 0 | .Default(FST_Unknown); |
10784 | 0 | } |
10785 | | |
10786 | | /// CheckFormatArguments - Check calls to printf and scanf (and similar |
10787 | | /// functions) for correct use of format strings. |
10788 | | /// Returns true if a format string has been fully checked. |
10789 | | bool Sema::CheckFormatArguments(const FormatAttr *Format, |
10790 | | ArrayRef<const Expr *> Args, bool IsCXXMember, |
10791 | | VariadicCallType CallType, SourceLocation Loc, |
10792 | | SourceRange Range, |
10793 | 0 | llvm::SmallBitVector &CheckedVarArgs) { |
10794 | 0 | FormatStringInfo FSI; |
10795 | 0 | if (getFormatStringInfo(Format, IsCXXMember, CallType != VariadicDoesNotApply, |
10796 | 0 | &FSI)) |
10797 | 0 | return CheckFormatArguments(Args, FSI.ArgPassingKind, FSI.FormatIdx, |
10798 | 0 | FSI.FirstDataArg, GetFormatStringType(Format), |
10799 | 0 | CallType, Loc, Range, CheckedVarArgs); |
10800 | 0 | return false; |
10801 | 0 | } |
10802 | | |
10803 | | bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args, |
10804 | | Sema::FormatArgumentPassingKind APK, |
10805 | | unsigned format_idx, unsigned firstDataArg, |
10806 | | FormatStringType Type, |
10807 | | VariadicCallType CallType, SourceLocation Loc, |
10808 | | SourceRange Range, |
10809 | 0 | llvm::SmallBitVector &CheckedVarArgs) { |
10810 | | // CHECK: printf/scanf-like function is called with no format string. |
10811 | 0 | if (format_idx >= Args.size()) { |
10812 | 0 | Diag(Loc, diag::warn_missing_format_string) << Range; |
10813 | 0 | return false; |
10814 | 0 | } |
10815 | | |
10816 | 0 | const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts(); |
10817 | | |
10818 | | // CHECK: format string is not a string literal. |
10819 | | // |
10820 | | // Dynamically generated format strings are difficult to |
10821 | | // automatically vet at compile time. Requiring that format strings |
10822 | | // are string literals: (1) permits the checking of format strings by |
10823 | | // the compiler and thereby (2) can practically remove the source of |
10824 | | // many format string exploits. |
10825 | | |
10826 | | // Format string can be either ObjC string (e.g. @"%d") or |
10827 | | // C string (e.g. "%d") |
10828 | | // ObjC string uses the same format specifiers as C string, so we can use |
10829 | | // the same format string checking logic for both ObjC and C strings. |
10830 | 0 | UncoveredArgHandler UncoveredArg; |
10831 | 0 | StringLiteralCheckType CT = checkFormatStringExpr( |
10832 | 0 | *this, OrigFormatExpr, Args, APK, format_idx, firstDataArg, Type, |
10833 | 0 | CallType, |
10834 | 0 | /*IsFunctionCall*/ true, CheckedVarArgs, UncoveredArg, |
10835 | 0 | /*no string offset*/ llvm::APSInt(64, false) = 0); |
10836 | | |
10837 | | // Generate a diagnostic where an uncovered argument is detected. |
10838 | 0 | if (UncoveredArg.hasUncoveredArg()) { |
10839 | 0 | unsigned ArgIdx = UncoveredArg.getUncoveredArg() + firstDataArg; |
10840 | 0 | assert(ArgIdx < Args.size() && "ArgIdx outside bounds"); |
10841 | 0 | UncoveredArg.Diagnose(*this, /*IsFunctionCall*/true, Args[ArgIdx]); |
10842 | 0 | } |
10843 | | |
10844 | 0 | if (CT != SLCT_NotALiteral) |
10845 | | // Literal format string found, check done! |
10846 | 0 | return CT == SLCT_CheckedLiteral; |
10847 | | |
10848 | | // Strftime is particular as it always uses a single 'time' argument, |
10849 | | // so it is safe to pass a non-literal string. |
10850 | 0 | if (Type == FST_Strftime) |
10851 | 0 | return false; |
10852 | | |
10853 | | // Do not emit diag when the string param is a macro expansion and the |
10854 | | // format is either NSString or CFString. This is a hack to prevent |
10855 | | // diag when using the NSLocalizedString and CFCopyLocalizedString macros |
10856 | | // which are usually used in place of NS and CF string literals. |
10857 | 0 | SourceLocation FormatLoc = Args[format_idx]->getBeginLoc(); |
10858 | 0 | if (Type == FST_NSString && SourceMgr.isInSystemMacro(FormatLoc)) |
10859 | 0 | return false; |
10860 | | |
10861 | | // If there are no arguments specified, warn with -Wformat-security, otherwise |
10862 | | // warn only with -Wformat-nonliteral. |
10863 | 0 | if (Args.size() == firstDataArg) { |
10864 | 0 | Diag(FormatLoc, diag::warn_format_nonliteral_noargs) |
10865 | 0 | << OrigFormatExpr->getSourceRange(); |
10866 | 0 | switch (Type) { |
10867 | 0 | default: |
10868 | 0 | break; |
10869 | 0 | case FST_Kprintf: |
10870 | 0 | case FST_FreeBSDKPrintf: |
10871 | 0 | case FST_Printf: |
10872 | 0 | Diag(FormatLoc, diag::note_format_security_fixit) |
10873 | 0 | << FixItHint::CreateInsertion(FormatLoc, "\"%s\", "); |
10874 | 0 | break; |
10875 | 0 | case FST_NSString: |
10876 | 0 | Diag(FormatLoc, diag::note_format_security_fixit) |
10877 | 0 | << FixItHint::CreateInsertion(FormatLoc, "@\"%@\", "); |
10878 | 0 | break; |
10879 | 0 | } |
10880 | 0 | } else { |
10881 | 0 | Diag(FormatLoc, diag::warn_format_nonliteral) |
10882 | 0 | << OrigFormatExpr->getSourceRange(); |
10883 | 0 | } |
10884 | 0 | return false; |
10885 | 0 | } |
10886 | | |
10887 | | namespace { |
10888 | | |
10889 | | class CheckFormatHandler : public analyze_format_string::FormatStringHandler { |
10890 | | protected: |
10891 | | Sema &S; |
10892 | | const FormatStringLiteral *FExpr; |
10893 | | const Expr *OrigFormatExpr; |
10894 | | const Sema::FormatStringType FSType; |
10895 | | const unsigned FirstDataArg; |
10896 | | const unsigned NumDataArgs; |
10897 | | const char *Beg; // Start of format string. |
10898 | | const Sema::FormatArgumentPassingKind ArgPassingKind; |
10899 | | ArrayRef<const Expr *> Args; |
10900 | | unsigned FormatIdx; |
10901 | | llvm::SmallBitVector CoveredArgs; |
10902 | | bool usesPositionalArgs = false; |
10903 | | bool atFirstArg = true; |
10904 | | bool inFunctionCall; |
10905 | | Sema::VariadicCallType CallType; |
10906 | | llvm::SmallBitVector &CheckedVarArgs; |
10907 | | UncoveredArgHandler &UncoveredArg; |
10908 | | |
10909 | | public: |
10910 | | CheckFormatHandler(Sema &s, const FormatStringLiteral *fexpr, |
10911 | | const Expr *origFormatExpr, |
10912 | | const Sema::FormatStringType type, unsigned firstDataArg, |
10913 | | unsigned numDataArgs, const char *beg, |
10914 | | Sema::FormatArgumentPassingKind APK, |
10915 | | ArrayRef<const Expr *> Args, unsigned formatIdx, |
10916 | | bool inFunctionCall, Sema::VariadicCallType callType, |
10917 | | llvm::SmallBitVector &CheckedVarArgs, |
10918 | | UncoveredArgHandler &UncoveredArg) |
10919 | | : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), FSType(type), |
10920 | | FirstDataArg(firstDataArg), NumDataArgs(numDataArgs), Beg(beg), |
10921 | | ArgPassingKind(APK), Args(Args), FormatIdx(formatIdx), |
10922 | | inFunctionCall(inFunctionCall), CallType(callType), |
10923 | 0 | CheckedVarArgs(CheckedVarArgs), UncoveredArg(UncoveredArg) { |
10924 | 0 | CoveredArgs.resize(numDataArgs); |
10925 | 0 | CoveredArgs.reset(); |
10926 | 0 | } |
10927 | | |
10928 | | void DoneProcessing(); |
10929 | | |
10930 | | void HandleIncompleteSpecifier(const char *startSpecifier, |
10931 | | unsigned specifierLen) override; |
10932 | | |
10933 | | void HandleInvalidLengthModifier( |
10934 | | const analyze_format_string::FormatSpecifier &FS, |
10935 | | const analyze_format_string::ConversionSpecifier &CS, |
10936 | | const char *startSpecifier, unsigned specifierLen, |
10937 | | unsigned DiagID); |
10938 | | |
10939 | | void HandleNonStandardLengthModifier( |
10940 | | const analyze_format_string::FormatSpecifier &FS, |
10941 | | const char *startSpecifier, unsigned specifierLen); |
10942 | | |
10943 | | void HandleNonStandardConversionSpecifier( |
10944 | | const analyze_format_string::ConversionSpecifier &CS, |
10945 | | const char *startSpecifier, unsigned specifierLen); |
10946 | | |
10947 | | void HandlePosition(const char *startPos, unsigned posLen) override; |
10948 | | |
10949 | | void HandleInvalidPosition(const char *startSpecifier, |
10950 | | unsigned specifierLen, |
10951 | | analyze_format_string::PositionContext p) override; |
10952 | | |
10953 | | void HandleZeroPosition(const char *startPos, unsigned posLen) override; |
10954 | | |
10955 | | void HandleNullChar(const char *nullCharacter) override; |
10956 | | |
10957 | | template <typename Range> |
10958 | | static void |
10959 | | EmitFormatDiagnostic(Sema &S, bool inFunctionCall, const Expr *ArgumentExpr, |
10960 | | const PartialDiagnostic &PDiag, SourceLocation StringLoc, |
10961 | | bool IsStringLocation, Range StringRange, |
10962 | | ArrayRef<FixItHint> Fixit = std::nullopt); |
10963 | | |
10964 | | protected: |
10965 | | bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, |
10966 | | const char *startSpec, |
10967 | | unsigned specifierLen, |
10968 | | const char *csStart, unsigned csLen); |
10969 | | |
10970 | | void HandlePositionalNonpositionalArgs(SourceLocation Loc, |
10971 | | const char *startSpec, |
10972 | | unsigned specifierLen); |
10973 | | |
10974 | | SourceRange getFormatStringRange(); |
10975 | | CharSourceRange getSpecifierRange(const char *startSpecifier, |
10976 | | unsigned specifierLen); |
10977 | | SourceLocation getLocationOfByte(const char *x); |
10978 | | |
10979 | | const Expr *getDataArg(unsigned i) const; |
10980 | | |
10981 | | bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, |
10982 | | const analyze_format_string::ConversionSpecifier &CS, |
10983 | | const char *startSpecifier, unsigned specifierLen, |
10984 | | unsigned argIndex); |
10985 | | |
10986 | | template <typename Range> |
10987 | | void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc, |
10988 | | bool IsStringLocation, Range StringRange, |
10989 | | ArrayRef<FixItHint> Fixit = std::nullopt); |
10990 | | }; |
10991 | | |
10992 | | } // namespace |
10993 | | |
10994 | 0 | SourceRange CheckFormatHandler::getFormatStringRange() { |
10995 | 0 | return OrigFormatExpr->getSourceRange(); |
10996 | 0 | } |
10997 | | |
10998 | | CharSourceRange CheckFormatHandler:: |
10999 | 0 | getSpecifierRange(const char *startSpecifier, unsigned specifierLen) { |
11000 | 0 | SourceLocation Start = getLocationOfByte(startSpecifier); |
11001 | 0 | SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1); |
11002 | | |
11003 | | // Advance the end SourceLocation by one due to half-open ranges. |
11004 | 0 | End = End.getLocWithOffset(1); |
11005 | |
|
11006 | 0 | return CharSourceRange::getCharRange(Start, End); |
11007 | 0 | } |
11008 | | |
11009 | 0 | SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { |
11010 | 0 | return FExpr->getLocationOfByte(x - Beg, S.getSourceManager(), |
11011 | 0 | S.getLangOpts(), S.Context.getTargetInfo()); |
11012 | 0 | } |
11013 | | |
11014 | | void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, |
11015 | 0 | unsigned specifierLen){ |
11016 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier), |
11017 | 0 | getLocationOfByte(startSpecifier), |
11018 | 0 | /*IsStringLocation*/true, |
11019 | 0 | getSpecifierRange(startSpecifier, specifierLen)); |
11020 | 0 | } |
11021 | | |
11022 | | void CheckFormatHandler::HandleInvalidLengthModifier( |
11023 | | const analyze_format_string::FormatSpecifier &FS, |
11024 | | const analyze_format_string::ConversionSpecifier &CS, |
11025 | 0 | const char *startSpecifier, unsigned specifierLen, unsigned DiagID) { |
11026 | 0 | using namespace analyze_format_string; |
11027 | |
|
11028 | 0 | const LengthModifier &LM = FS.getLengthModifier(); |
11029 | 0 | CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); |
11030 | | |
11031 | | // See if we know how to fix this length modifier. |
11032 | 0 | std::optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); |
11033 | 0 | if (FixedLM) { |
11034 | 0 | EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), |
11035 | 0 | getLocationOfByte(LM.getStart()), |
11036 | 0 | /*IsStringLocation*/true, |
11037 | 0 | getSpecifierRange(startSpecifier, specifierLen)); |
11038 | |
|
11039 | 0 | S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) |
11040 | 0 | << FixedLM->toString() |
11041 | 0 | << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); |
11042 | |
|
11043 | 0 | } else { |
11044 | 0 | FixItHint Hint; |
11045 | 0 | if (DiagID == diag::warn_format_nonsensical_length) |
11046 | 0 | Hint = FixItHint::CreateRemoval(LMRange); |
11047 | |
|
11048 | 0 | EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), |
11049 | 0 | getLocationOfByte(LM.getStart()), |
11050 | 0 | /*IsStringLocation*/true, |
11051 | 0 | getSpecifierRange(startSpecifier, specifierLen), |
11052 | 0 | Hint); |
11053 | 0 | } |
11054 | 0 | } |
11055 | | |
11056 | | void CheckFormatHandler::HandleNonStandardLengthModifier( |
11057 | | const analyze_format_string::FormatSpecifier &FS, |
11058 | 0 | const char *startSpecifier, unsigned specifierLen) { |
11059 | 0 | using namespace analyze_format_string; |
11060 | |
|
11061 | 0 | const LengthModifier &LM = FS.getLengthModifier(); |
11062 | 0 | CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); |
11063 | | |
11064 | | // See if we know how to fix this length modifier. |
11065 | 0 | std::optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); |
11066 | 0 | if (FixedLM) { |
11067 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
11068 | 0 | << LM.toString() << 0, |
11069 | 0 | getLocationOfByte(LM.getStart()), |
11070 | 0 | /*IsStringLocation*/true, |
11071 | 0 | getSpecifierRange(startSpecifier, specifierLen)); |
11072 | |
|
11073 | 0 | S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) |
11074 | 0 | << FixedLM->toString() |
11075 | 0 | << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); |
11076 | |
|
11077 | 0 | } else { |
11078 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
11079 | 0 | << LM.toString() << 0, |
11080 | 0 | getLocationOfByte(LM.getStart()), |
11081 | 0 | /*IsStringLocation*/true, |
11082 | 0 | getSpecifierRange(startSpecifier, specifierLen)); |
11083 | 0 | } |
11084 | 0 | } |
11085 | | |
11086 | | void CheckFormatHandler::HandleNonStandardConversionSpecifier( |
11087 | | const analyze_format_string::ConversionSpecifier &CS, |
11088 | 0 | const char *startSpecifier, unsigned specifierLen) { |
11089 | 0 | using namespace analyze_format_string; |
11090 | | |
11091 | | // See if we know how to fix this conversion specifier. |
11092 | 0 | std::optional<ConversionSpecifier> FixedCS = CS.getStandardSpecifier(); |
11093 | 0 | if (FixedCS) { |
11094 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
11095 | 0 | << CS.toString() << /*conversion specifier*/1, |
11096 | 0 | getLocationOfByte(CS.getStart()), |
11097 | 0 | /*IsStringLocation*/true, |
11098 | 0 | getSpecifierRange(startSpecifier, specifierLen)); |
11099 | |
|
11100 | 0 | CharSourceRange CSRange = getSpecifierRange(CS.getStart(), CS.getLength()); |
11101 | 0 | S.Diag(getLocationOfByte(CS.getStart()), diag::note_format_fix_specifier) |
11102 | 0 | << FixedCS->toString() |
11103 | 0 | << FixItHint::CreateReplacement(CSRange, FixedCS->toString()); |
11104 | 0 | } else { |
11105 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
11106 | 0 | << CS.toString() << /*conversion specifier*/1, |
11107 | 0 | getLocationOfByte(CS.getStart()), |
11108 | 0 | /*IsStringLocation*/true, |
11109 | 0 | getSpecifierRange(startSpecifier, specifierLen)); |
11110 | 0 | } |
11111 | 0 | } |
11112 | | |
11113 | | void CheckFormatHandler::HandlePosition(const char *startPos, |
11114 | 0 | unsigned posLen) { |
11115 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg), |
11116 | 0 | getLocationOfByte(startPos), |
11117 | 0 | /*IsStringLocation*/true, |
11118 | 0 | getSpecifierRange(startPos, posLen)); |
11119 | 0 | } |
11120 | | |
11121 | | void CheckFormatHandler::HandleInvalidPosition( |
11122 | | const char *startSpecifier, unsigned specifierLen, |
11123 | 0 | analyze_format_string::PositionContext p) { |
11124 | 0 | EmitFormatDiagnostic( |
11125 | 0 | S.PDiag(diag::warn_format_invalid_positional_specifier) << (unsigned)p, |
11126 | 0 | getLocationOfByte(startSpecifier), /*IsStringLocation*/ true, |
11127 | 0 | getSpecifierRange(startSpecifier, specifierLen)); |
11128 | 0 | } |
11129 | | |
11130 | | void CheckFormatHandler::HandleZeroPosition(const char *startPos, |
11131 | 0 | unsigned posLen) { |
11132 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier), |
11133 | 0 | getLocationOfByte(startPos), |
11134 | 0 | /*IsStringLocation*/true, |
11135 | 0 | getSpecifierRange(startPos, posLen)); |
11136 | 0 | } |
11137 | | |
11138 | 0 | void CheckFormatHandler::HandleNullChar(const char *nullCharacter) { |
11139 | 0 | if (!isa<ObjCStringLiteral>(OrigFormatExpr)) { |
11140 | | // The presence of a null character is likely an error. |
11141 | 0 | EmitFormatDiagnostic( |
11142 | 0 | S.PDiag(diag::warn_printf_format_string_contains_null_char), |
11143 | 0 | getLocationOfByte(nullCharacter), /*IsStringLocation*/true, |
11144 | 0 | getFormatStringRange()); |
11145 | 0 | } |
11146 | 0 | } |
11147 | | |
11148 | | // Note that this may return NULL if there was an error parsing or building |
11149 | | // one of the argument expressions. |
11150 | 0 | const Expr *CheckFormatHandler::getDataArg(unsigned i) const { |
11151 | 0 | return Args[FirstDataArg + i]; |
11152 | 0 | } |
11153 | | |
11154 | 0 | void CheckFormatHandler::DoneProcessing() { |
11155 | | // Does the number of data arguments exceed the number of |
11156 | | // format conversions in the format string? |
11157 | 0 | if (ArgPassingKind != Sema::FAPK_VAList) { |
11158 | | // Find any arguments that weren't covered. |
11159 | 0 | CoveredArgs.flip(); |
11160 | 0 | signed notCoveredArg = CoveredArgs.find_first(); |
11161 | 0 | if (notCoveredArg >= 0) { |
11162 | 0 | assert((unsigned)notCoveredArg < NumDataArgs); |
11163 | 0 | UncoveredArg.Update(notCoveredArg, OrigFormatExpr); |
11164 | 0 | } else { |
11165 | 0 | UncoveredArg.setAllCovered(); |
11166 | 0 | } |
11167 | 0 | } |
11168 | 0 | } |
11169 | | |
11170 | | void UncoveredArgHandler::Diagnose(Sema &S, bool IsFunctionCall, |
11171 | 0 | const Expr *ArgExpr) { |
11172 | 0 | assert(hasUncoveredArg() && !DiagnosticExprs.empty() && |
11173 | 0 | "Invalid state"); |
11174 | | |
11175 | 0 | if (!ArgExpr) |
11176 | 0 | return; |
11177 | | |
11178 | 0 | SourceLocation Loc = ArgExpr->getBeginLoc(); |
11179 | |
|
11180 | 0 | if (S.getSourceManager().isInSystemMacro(Loc)) |
11181 | 0 | return; |
11182 | | |
11183 | 0 | PartialDiagnostic PDiag = S.PDiag(diag::warn_printf_data_arg_not_used); |
11184 | 0 | for (auto E : DiagnosticExprs) |
11185 | 0 | PDiag << E->getSourceRange(); |
11186 | |
|
11187 | 0 | CheckFormatHandler::EmitFormatDiagnostic( |
11188 | 0 | S, IsFunctionCall, DiagnosticExprs[0], |
11189 | 0 | PDiag, Loc, /*IsStringLocation*/false, |
11190 | 0 | DiagnosticExprs[0]->getSourceRange()); |
11191 | 0 | } |
11192 | | |
11193 | | bool |
11194 | | CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, |
11195 | | SourceLocation Loc, |
11196 | | const char *startSpec, |
11197 | | unsigned specifierLen, |
11198 | | const char *csStart, |
11199 | 0 | unsigned csLen) { |
11200 | 0 | bool keepGoing = true; |
11201 | 0 | if (argIndex < NumDataArgs) { |
11202 | | // Consider the argument coverered, even though the specifier doesn't |
11203 | | // make sense. |
11204 | 0 | CoveredArgs.set(argIndex); |
11205 | 0 | } |
11206 | 0 | else { |
11207 | | // If argIndex exceeds the number of data arguments we |
11208 | | // don't issue a warning because that is just a cascade of warnings (and |
11209 | | // they may have intended '%%' anyway). We don't want to continue processing |
11210 | | // the format string after this point, however, as we will like just get |
11211 | | // gibberish when trying to match arguments. |
11212 | 0 | keepGoing = false; |
11213 | 0 | } |
11214 | |
|
11215 | 0 | StringRef Specifier(csStart, csLen); |
11216 | | |
11217 | | // If the specifier in non-printable, it could be the first byte of a UTF-8 |
11218 | | // sequence. In that case, print the UTF-8 code point. If not, print the byte |
11219 | | // hex value. |
11220 | 0 | std::string CodePointStr; |
11221 | 0 | if (!llvm::sys::locale::isPrint(*csStart)) { |
11222 | 0 | llvm::UTF32 CodePoint; |
11223 | 0 | const llvm::UTF8 **B = reinterpret_cast<const llvm::UTF8 **>(&csStart); |
11224 | 0 | const llvm::UTF8 *E = |
11225 | 0 | reinterpret_cast<const llvm::UTF8 *>(csStart + csLen); |
11226 | 0 | llvm::ConversionResult Result = |
11227 | 0 | llvm::convertUTF8Sequence(B, E, &CodePoint, llvm::strictConversion); |
11228 | |
|
11229 | 0 | if (Result != llvm::conversionOK) { |
11230 | 0 | unsigned char FirstChar = *csStart; |
11231 | 0 | CodePoint = (llvm::UTF32)FirstChar; |
11232 | 0 | } |
11233 | |
|
11234 | 0 | llvm::raw_string_ostream OS(CodePointStr); |
11235 | 0 | if (CodePoint < 256) |
11236 | 0 | OS << "\\x" << llvm::format("%02x", CodePoint); |
11237 | 0 | else if (CodePoint <= 0xFFFF) |
11238 | 0 | OS << "\\u" << llvm::format("%04x", CodePoint); |
11239 | 0 | else |
11240 | 0 | OS << "\\U" << llvm::format("%08x", CodePoint); |
11241 | 0 | OS.flush(); |
11242 | 0 | Specifier = CodePointStr; |
11243 | 0 | } |
11244 | |
|
11245 | 0 | EmitFormatDiagnostic( |
11246 | 0 | S.PDiag(diag::warn_format_invalid_conversion) << Specifier, Loc, |
11247 | 0 | /*IsStringLocation*/ true, getSpecifierRange(startSpec, specifierLen)); |
11248 | |
|
11249 | 0 | return keepGoing; |
11250 | 0 | } |
11251 | | |
11252 | | void |
11253 | | CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc, |
11254 | | const char *startSpec, |
11255 | 0 | unsigned specifierLen) { |
11256 | 0 | EmitFormatDiagnostic( |
11257 | 0 | S.PDiag(diag::warn_format_mix_positional_nonpositional_args), |
11258 | 0 | Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen)); |
11259 | 0 | } |
11260 | | |
11261 | | bool |
11262 | | CheckFormatHandler::CheckNumArgs( |
11263 | | const analyze_format_string::FormatSpecifier &FS, |
11264 | | const analyze_format_string::ConversionSpecifier &CS, |
11265 | 0 | const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { |
11266 | |
|
11267 | 0 | if (argIndex >= NumDataArgs) { |
11268 | 0 | PartialDiagnostic PDiag = FS.usesPositionalArg() |
11269 | 0 | ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args) |
11270 | 0 | << (argIndex+1) << NumDataArgs) |
11271 | 0 | : S.PDiag(diag::warn_printf_insufficient_data_args); |
11272 | 0 | EmitFormatDiagnostic( |
11273 | 0 | PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true, |
11274 | 0 | getSpecifierRange(startSpecifier, specifierLen)); |
11275 | | |
11276 | | // Since more arguments than conversion tokens are given, by extension |
11277 | | // all arguments are covered, so mark this as so. |
11278 | 0 | UncoveredArg.setAllCovered(); |
11279 | 0 | return false; |
11280 | 0 | } |
11281 | 0 | return true; |
11282 | 0 | } |
11283 | | |
11284 | | template<typename Range> |
11285 | | void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag, |
11286 | | SourceLocation Loc, |
11287 | | bool IsStringLocation, |
11288 | | Range StringRange, |
11289 | 0 | ArrayRef<FixItHint> FixIt) { |
11290 | 0 | EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag, |
11291 | 0 | Loc, IsStringLocation, StringRange, FixIt); |
11292 | 0 | } Unexecuted instantiation: SemaChecking.cpp:void (anonymous namespace)::CheckFormatHandler::EmitFormatDiagnostic<clang::SourceRange>(clang::PartialDiagnostic, clang::SourceLocation, bool, clang::SourceRange, llvm::ArrayRef<clang::FixItHint>) Unexecuted instantiation: SemaChecking.cpp:void (anonymous namespace)::CheckFormatHandler::EmitFormatDiagnostic<clang::CharSourceRange>(clang::PartialDiagnostic, clang::SourceLocation, bool, clang::CharSourceRange, llvm::ArrayRef<clang::FixItHint>) |
11293 | | |
11294 | | /// If the format string is not within the function call, emit a note |
11295 | | /// so that the function call and string are in diagnostic messages. |
11296 | | /// |
11297 | | /// \param InFunctionCall if true, the format string is within the function |
11298 | | /// call and only one diagnostic message will be produced. Otherwise, an |
11299 | | /// extra note will be emitted pointing to location of the format string. |
11300 | | /// |
11301 | | /// \param ArgumentExpr the expression that is passed as the format string |
11302 | | /// argument in the function call. Used for getting locations when two |
11303 | | /// diagnostics are emitted. |
11304 | | /// |
11305 | | /// \param PDiag the callee should already have provided any strings for the |
11306 | | /// diagnostic message. This function only adds locations and fixits |
11307 | | /// to diagnostics. |
11308 | | /// |
11309 | | /// \param Loc primary location for diagnostic. If two diagnostics are |
11310 | | /// required, one will be at Loc and a new SourceLocation will be created for |
11311 | | /// the other one. |
11312 | | /// |
11313 | | /// \param IsStringLocation if true, Loc points to the format string should be |
11314 | | /// used for the note. Otherwise, Loc points to the argument list and will |
11315 | | /// be used with PDiag. |
11316 | | /// |
11317 | | /// \param StringRange some or all of the string to highlight. This is |
11318 | | /// templated so it can accept either a CharSourceRange or a SourceRange. |
11319 | | /// |
11320 | | /// \param FixIt optional fix it hint for the format string. |
11321 | | template <typename Range> |
11322 | | void CheckFormatHandler::EmitFormatDiagnostic( |
11323 | | Sema &S, bool InFunctionCall, const Expr *ArgumentExpr, |
11324 | | const PartialDiagnostic &PDiag, SourceLocation Loc, bool IsStringLocation, |
11325 | 0 | Range StringRange, ArrayRef<FixItHint> FixIt) { |
11326 | 0 | if (InFunctionCall) { |
11327 | 0 | const Sema::SemaDiagnosticBuilder &D = S.Diag(Loc, PDiag); |
11328 | 0 | D << StringRange; |
11329 | 0 | D << FixIt; |
11330 | 0 | } else { |
11331 | 0 | S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag) |
11332 | 0 | << ArgumentExpr->getSourceRange(); |
11333 | |
|
11334 | 0 | const Sema::SemaDiagnosticBuilder &Note = |
11335 | 0 | S.Diag(IsStringLocation ? Loc : StringRange.getBegin(), |
11336 | 0 | diag::note_format_string_defined); |
11337 | |
|
11338 | 0 | Note << StringRange; |
11339 | 0 | Note << FixIt; |
11340 | 0 | } |
11341 | 0 | } Unexecuted instantiation: SemaChecking.cpp:void (anonymous namespace)::CheckFormatHandler::EmitFormatDiagnostic<clang::SourceRange>(clang::Sema&, bool, clang::Expr const*, clang::PartialDiagnostic const&, clang::SourceLocation, bool, clang::SourceRange, llvm::ArrayRef<clang::FixItHint>) Unexecuted instantiation: SemaChecking.cpp:void (anonymous namespace)::CheckFormatHandler::EmitFormatDiagnostic<clang::CharSourceRange>(clang::Sema&, bool, clang::Expr const*, clang::PartialDiagnostic const&, clang::SourceLocation, bool, clang::CharSourceRange, llvm::ArrayRef<clang::FixItHint>) |
11342 | | |
11343 | | //===--- CHECK: Printf format string checking ------------------------------===// |
11344 | | |
11345 | | namespace { |
11346 | | |
11347 | | class CheckPrintfHandler : public CheckFormatHandler { |
11348 | | public: |
11349 | | CheckPrintfHandler(Sema &s, const FormatStringLiteral *fexpr, |
11350 | | const Expr *origFormatExpr, |
11351 | | const Sema::FormatStringType type, unsigned firstDataArg, |
11352 | | unsigned numDataArgs, bool isObjC, const char *beg, |
11353 | | Sema::FormatArgumentPassingKind APK, |
11354 | | ArrayRef<const Expr *> Args, unsigned formatIdx, |
11355 | | bool inFunctionCall, Sema::VariadicCallType CallType, |
11356 | | llvm::SmallBitVector &CheckedVarArgs, |
11357 | | UncoveredArgHandler &UncoveredArg) |
11358 | | : CheckFormatHandler(s, fexpr, origFormatExpr, type, firstDataArg, |
11359 | | numDataArgs, beg, APK, Args, formatIdx, |
11360 | | inFunctionCall, CallType, CheckedVarArgs, |
11361 | 0 | UncoveredArg) {} |
11362 | | |
11363 | 0 | bool isObjCContext() const { return FSType == Sema::FST_NSString; } |
11364 | | |
11365 | | /// Returns true if '%@' specifiers are allowed in the format string. |
11366 | 0 | bool allowsObjCArg() const { |
11367 | 0 | return FSType == Sema::FST_NSString || FSType == Sema::FST_OSLog || |
11368 | 0 | FSType == Sema::FST_OSTrace; |
11369 | 0 | } |
11370 | | |
11371 | | bool HandleInvalidPrintfConversionSpecifier( |
11372 | | const analyze_printf::PrintfSpecifier &FS, |
11373 | | const char *startSpecifier, |
11374 | | unsigned specifierLen) override; |
11375 | | |
11376 | | void handleInvalidMaskType(StringRef MaskType) override; |
11377 | | |
11378 | | bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, |
11379 | | const char *startSpecifier, unsigned specifierLen, |
11380 | | const TargetInfo &Target) override; |
11381 | | bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, |
11382 | | const char *StartSpecifier, |
11383 | | unsigned SpecifierLen, |
11384 | | const Expr *E); |
11385 | | |
11386 | | bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, |
11387 | | const char *startSpecifier, unsigned specifierLen); |
11388 | | void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, |
11389 | | const analyze_printf::OptionalAmount &Amt, |
11390 | | unsigned type, |
11391 | | const char *startSpecifier, unsigned specifierLen); |
11392 | | void HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
11393 | | const analyze_printf::OptionalFlag &flag, |
11394 | | const char *startSpecifier, unsigned specifierLen); |
11395 | | void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, |
11396 | | const analyze_printf::OptionalFlag &ignoredFlag, |
11397 | | const analyze_printf::OptionalFlag &flag, |
11398 | | const char *startSpecifier, unsigned specifierLen); |
11399 | | bool checkForCStrMembers(const analyze_printf::ArgType &AT, |
11400 | | const Expr *E); |
11401 | | |
11402 | | void HandleEmptyObjCModifierFlag(const char *startFlag, |
11403 | | unsigned flagLen) override; |
11404 | | |
11405 | | void HandleInvalidObjCModifierFlag(const char *startFlag, |
11406 | | unsigned flagLen) override; |
11407 | | |
11408 | | void HandleObjCFlagsWithNonObjCConversion(const char *flagsStart, |
11409 | | const char *flagsEnd, |
11410 | | const char *conversionPosition) |
11411 | | override; |
11412 | | }; |
11413 | | |
11414 | | } // namespace |
11415 | | |
11416 | | bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( |
11417 | | const analyze_printf::PrintfSpecifier &FS, |
11418 | | const char *startSpecifier, |
11419 | 0 | unsigned specifierLen) { |
11420 | 0 | const analyze_printf::PrintfConversionSpecifier &CS = |
11421 | 0 | FS.getConversionSpecifier(); |
11422 | |
|
11423 | 0 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
11424 | 0 | getLocationOfByte(CS.getStart()), |
11425 | 0 | startSpecifier, specifierLen, |
11426 | 0 | CS.getStart(), CS.getLength()); |
11427 | 0 | } |
11428 | | |
11429 | 0 | void CheckPrintfHandler::handleInvalidMaskType(StringRef MaskType) { |
11430 | 0 | S.Diag(getLocationOfByte(MaskType.data()), diag::err_invalid_mask_type_size); |
11431 | 0 | } |
11432 | | |
11433 | | bool CheckPrintfHandler::HandleAmount( |
11434 | | const analyze_format_string::OptionalAmount &Amt, unsigned k, |
11435 | 0 | const char *startSpecifier, unsigned specifierLen) { |
11436 | 0 | if (Amt.hasDataArgument()) { |
11437 | 0 | if (ArgPassingKind != Sema::FAPK_VAList) { |
11438 | 0 | unsigned argIndex = Amt.getArgIndex(); |
11439 | 0 | if (argIndex >= NumDataArgs) { |
11440 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg) |
11441 | 0 | << k, |
11442 | 0 | getLocationOfByte(Amt.getStart()), |
11443 | 0 | /*IsStringLocation*/ true, |
11444 | 0 | getSpecifierRange(startSpecifier, specifierLen)); |
11445 | | // Don't do any more checking. We will just emit |
11446 | | // spurious errors. |
11447 | 0 | return false; |
11448 | 0 | } |
11449 | | |
11450 | | // Type check the data argument. It should be an 'int'. |
11451 | | // Although not in conformance with C99, we also allow the argument to be |
11452 | | // an 'unsigned int' as that is a reasonably safe case. GCC also |
11453 | | // doesn't emit a warning for that case. |
11454 | 0 | CoveredArgs.set(argIndex); |
11455 | 0 | const Expr *Arg = getDataArg(argIndex); |
11456 | 0 | if (!Arg) |
11457 | 0 | return false; |
11458 | | |
11459 | 0 | QualType T = Arg->getType(); |
11460 | |
|
11461 | 0 | const analyze_printf::ArgType &AT = Amt.getArgType(S.Context); |
11462 | 0 | assert(AT.isValid()); |
11463 | | |
11464 | 0 | if (!AT.matchesType(S.Context, T)) { |
11465 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type) |
11466 | 0 | << k << AT.getRepresentativeTypeName(S.Context) |
11467 | 0 | << T << Arg->getSourceRange(), |
11468 | 0 | getLocationOfByte(Amt.getStart()), |
11469 | 0 | /*IsStringLocation*/true, |
11470 | 0 | getSpecifierRange(startSpecifier, specifierLen)); |
11471 | | // Don't do any more checking. We will just emit |
11472 | | // spurious errors. |
11473 | 0 | return false; |
11474 | 0 | } |
11475 | 0 | } |
11476 | 0 | } |
11477 | 0 | return true; |
11478 | 0 | } |
11479 | | |
11480 | | void CheckPrintfHandler::HandleInvalidAmount( |
11481 | | const analyze_printf::PrintfSpecifier &FS, |
11482 | | const analyze_printf::OptionalAmount &Amt, |
11483 | | unsigned type, |
11484 | | const char *startSpecifier, |
11485 | 0 | unsigned specifierLen) { |
11486 | 0 | const analyze_printf::PrintfConversionSpecifier &CS = |
11487 | 0 | FS.getConversionSpecifier(); |
11488 | |
|
11489 | 0 | FixItHint fixit = |
11490 | 0 | Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant |
11491 | 0 | ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), |
11492 | 0 | Amt.getConstantLength())) |
11493 | 0 | : FixItHint(); |
11494 | |
|
11495 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount) |
11496 | 0 | << type << CS.toString(), |
11497 | 0 | getLocationOfByte(Amt.getStart()), |
11498 | 0 | /*IsStringLocation*/true, |
11499 | 0 | getSpecifierRange(startSpecifier, specifierLen), |
11500 | 0 | fixit); |
11501 | 0 | } |
11502 | | |
11503 | | void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
11504 | | const analyze_printf::OptionalFlag &flag, |
11505 | | const char *startSpecifier, |
11506 | 0 | unsigned specifierLen) { |
11507 | | // Warn about pointless flag with a fixit removal. |
11508 | 0 | const analyze_printf::PrintfConversionSpecifier &CS = |
11509 | 0 | FS.getConversionSpecifier(); |
11510 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag) |
11511 | 0 | << flag.toString() << CS.toString(), |
11512 | 0 | getLocationOfByte(flag.getPosition()), |
11513 | 0 | /*IsStringLocation*/true, |
11514 | 0 | getSpecifierRange(startSpecifier, specifierLen), |
11515 | 0 | FixItHint::CreateRemoval( |
11516 | 0 | getSpecifierRange(flag.getPosition(), 1))); |
11517 | 0 | } |
11518 | | |
11519 | | void CheckPrintfHandler::HandleIgnoredFlag( |
11520 | | const analyze_printf::PrintfSpecifier &FS, |
11521 | | const analyze_printf::OptionalFlag &ignoredFlag, |
11522 | | const analyze_printf::OptionalFlag &flag, |
11523 | | const char *startSpecifier, |
11524 | 0 | unsigned specifierLen) { |
11525 | | // Warn about ignored flag with a fixit removal. |
11526 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag) |
11527 | 0 | << ignoredFlag.toString() << flag.toString(), |
11528 | 0 | getLocationOfByte(ignoredFlag.getPosition()), |
11529 | 0 | /*IsStringLocation*/true, |
11530 | 0 | getSpecifierRange(startSpecifier, specifierLen), |
11531 | 0 | FixItHint::CreateRemoval( |
11532 | 0 | getSpecifierRange(ignoredFlag.getPosition(), 1))); |
11533 | 0 | } |
11534 | | |
11535 | | void CheckPrintfHandler::HandleEmptyObjCModifierFlag(const char *startFlag, |
11536 | 0 | unsigned flagLen) { |
11537 | | // Warn about an empty flag. |
11538 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_empty_objc_flag), |
11539 | 0 | getLocationOfByte(startFlag), |
11540 | 0 | /*IsStringLocation*/true, |
11541 | 0 | getSpecifierRange(startFlag, flagLen)); |
11542 | 0 | } |
11543 | | |
11544 | | void CheckPrintfHandler::HandleInvalidObjCModifierFlag(const char *startFlag, |
11545 | 0 | unsigned flagLen) { |
11546 | | // Warn about an invalid flag. |
11547 | 0 | auto Range = getSpecifierRange(startFlag, flagLen); |
11548 | 0 | StringRef flag(startFlag, flagLen); |
11549 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_invalid_objc_flag) << flag, |
11550 | 0 | getLocationOfByte(startFlag), |
11551 | 0 | /*IsStringLocation*/true, |
11552 | 0 | Range, FixItHint::CreateRemoval(Range)); |
11553 | 0 | } |
11554 | | |
11555 | | void CheckPrintfHandler::HandleObjCFlagsWithNonObjCConversion( |
11556 | 0 | const char *flagsStart, const char *flagsEnd, const char *conversionPosition) { |
11557 | | // Warn about using '[...]' without a '@' conversion. |
11558 | 0 | auto Range = getSpecifierRange(flagsStart, flagsEnd - flagsStart + 1); |
11559 | 0 | auto diag = diag::warn_printf_ObjCflags_without_ObjCConversion; |
11560 | 0 | EmitFormatDiagnostic(S.PDiag(diag) << StringRef(conversionPosition, 1), |
11561 | 0 | getLocationOfByte(conversionPosition), |
11562 | 0 | /*IsStringLocation*/true, |
11563 | 0 | Range, FixItHint::CreateRemoval(Range)); |
11564 | 0 | } |
11565 | | |
11566 | | // Determines if the specified is a C++ class or struct containing |
11567 | | // a member with the specified name and kind (e.g. a CXXMethodDecl named |
11568 | | // "c_str()"). |
11569 | | template<typename MemberKind> |
11570 | | static llvm::SmallPtrSet<MemberKind*, 1> |
11571 | 0 | CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) { |
11572 | 0 | const RecordType *RT = Ty->getAs<RecordType>(); |
11573 | 0 | llvm::SmallPtrSet<MemberKind*, 1> Results; |
11574 | |
|
11575 | 0 | if (!RT) |
11576 | 0 | return Results; |
11577 | 0 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
11578 | 0 | if (!RD || !RD->getDefinition()) |
11579 | 0 | return Results; |
11580 | | |
11581 | 0 | LookupResult R(S, &S.Context.Idents.get(Name), SourceLocation(), |
11582 | 0 | Sema::LookupMemberName); |
11583 | 0 | R.suppressDiagnostics(); |
11584 | | |
11585 | | // We just need to include all members of the right kind turned up by the |
11586 | | // filter, at this point. |
11587 | 0 | if (S.LookupQualifiedName(R, RT->getDecl())) |
11588 | 0 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
11589 | 0 | NamedDecl *decl = (*I)->getUnderlyingDecl(); |
11590 | 0 | if (MemberKind *FK = dyn_cast<MemberKind>(decl)) |
11591 | 0 | Results.insert(FK); |
11592 | 0 | } |
11593 | 0 | return Results; |
11594 | 0 | } |
11595 | | |
11596 | | /// Check if we could call '.c_str()' on an object. |
11597 | | /// |
11598 | | /// FIXME: This returns the wrong results in some cases (if cv-qualifiers don't |
11599 | | /// allow the call, or if it would be ambiguous). |
11600 | 0 | bool Sema::hasCStrMethod(const Expr *E) { |
11601 | 0 | using MethodSet = llvm::SmallPtrSet<CXXMethodDecl *, 1>; |
11602 | |
|
11603 | 0 | MethodSet Results = |
11604 | 0 | CXXRecordMembersNamed<CXXMethodDecl>("c_str", *this, E->getType()); |
11605 | 0 | for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); |
11606 | 0 | MI != ME; ++MI) |
11607 | 0 | if ((*MI)->getMinRequiredArguments() == 0) |
11608 | 0 | return true; |
11609 | 0 | return false; |
11610 | 0 | } |
11611 | | |
11612 | | // Check if a (w)string was passed when a (w)char* was needed, and offer a |
11613 | | // better diagnostic if so. AT is assumed to be valid. |
11614 | | // Returns true when a c_str() conversion method is found. |
11615 | | bool CheckPrintfHandler::checkForCStrMembers( |
11616 | 0 | const analyze_printf::ArgType &AT, const Expr *E) { |
11617 | 0 | using MethodSet = llvm::SmallPtrSet<CXXMethodDecl *, 1>; |
11618 | |
|
11619 | 0 | MethodSet Results = |
11620 | 0 | CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType()); |
11621 | |
|
11622 | 0 | for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); |
11623 | 0 | MI != ME; ++MI) { |
11624 | 0 | const CXXMethodDecl *Method = *MI; |
11625 | 0 | if (Method->getMinRequiredArguments() == 0 && |
11626 | 0 | AT.matchesType(S.Context, Method->getReturnType())) { |
11627 | | // FIXME: Suggest parens if the expression needs them. |
11628 | 0 | SourceLocation EndLoc = S.getLocForEndOfToken(E->getEndLoc()); |
11629 | 0 | S.Diag(E->getBeginLoc(), diag::note_printf_c_str) |
11630 | 0 | << "c_str()" << FixItHint::CreateInsertion(EndLoc, ".c_str()"); |
11631 | 0 | return true; |
11632 | 0 | } |
11633 | 0 | } |
11634 | | |
11635 | 0 | return false; |
11636 | 0 | } |
11637 | | |
11638 | | bool CheckPrintfHandler::HandlePrintfSpecifier( |
11639 | | const analyze_printf::PrintfSpecifier &FS, const char *startSpecifier, |
11640 | 0 | unsigned specifierLen, const TargetInfo &Target) { |
11641 | 0 | using namespace analyze_format_string; |
11642 | 0 | using namespace analyze_printf; |
11643 | |
|
11644 | 0 | const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); |
11645 | |
|
11646 | 0 | if (FS.consumesDataArgument()) { |
11647 | 0 | if (atFirstArg) { |
11648 | 0 | atFirstArg = false; |
11649 | 0 | usesPositionalArgs = FS.usesPositionalArg(); |
11650 | 0 | } |
11651 | 0 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
11652 | 0 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), |
11653 | 0 | startSpecifier, specifierLen); |
11654 | 0 | return false; |
11655 | 0 | } |
11656 | 0 | } |
11657 | | |
11658 | | // First check if the field width, precision, and conversion specifier |
11659 | | // have matching data arguments. |
11660 | 0 | if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, |
11661 | 0 | startSpecifier, specifierLen)) { |
11662 | 0 | return false; |
11663 | 0 | } |
11664 | | |
11665 | 0 | if (!HandleAmount(FS.getPrecision(), /* precision */ 1, |
11666 | 0 | startSpecifier, specifierLen)) { |
11667 | 0 | return false; |
11668 | 0 | } |
11669 | | |
11670 | 0 | if (!CS.consumesDataArgument()) { |
11671 | | // FIXME: Technically specifying a precision or field width here |
11672 | | // makes no sense. Worth issuing a warning at some point. |
11673 | 0 | return true; |
11674 | 0 | } |
11675 | | |
11676 | | // Consume the argument. |
11677 | 0 | unsigned argIndex = FS.getArgIndex(); |
11678 | 0 | if (argIndex < NumDataArgs) { |
11679 | | // The check to see if the argIndex is valid will come later. |
11680 | | // We set the bit here because we may exit early from this |
11681 | | // function if we encounter some other error. |
11682 | 0 | CoveredArgs.set(argIndex); |
11683 | 0 | } |
11684 | | |
11685 | | // FreeBSD kernel extensions. |
11686 | 0 | if (CS.getKind() == ConversionSpecifier::FreeBSDbArg || |
11687 | 0 | CS.getKind() == ConversionSpecifier::FreeBSDDArg) { |
11688 | | // We need at least two arguments. |
11689 | 0 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex + 1)) |
11690 | 0 | return false; |
11691 | | |
11692 | | // Claim the second argument. |
11693 | 0 | CoveredArgs.set(argIndex + 1); |
11694 | | |
11695 | | // Type check the first argument (int for %b, pointer for %D) |
11696 | 0 | const Expr *Ex = getDataArg(argIndex); |
11697 | 0 | const analyze_printf::ArgType &AT = |
11698 | 0 | (CS.getKind() == ConversionSpecifier::FreeBSDbArg) ? |
11699 | 0 | ArgType(S.Context.IntTy) : ArgType::CPointerTy; |
11700 | 0 | if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType())) |
11701 | 0 | EmitFormatDiagnostic( |
11702 | 0 | S.PDiag(diag::warn_format_conversion_argument_type_mismatch) |
11703 | 0 | << AT.getRepresentativeTypeName(S.Context) << Ex->getType() |
11704 | 0 | << false << Ex->getSourceRange(), |
11705 | 0 | Ex->getBeginLoc(), /*IsStringLocation*/ false, |
11706 | 0 | getSpecifierRange(startSpecifier, specifierLen)); |
11707 | | |
11708 | | // Type check the second argument (char * for both %b and %D) |
11709 | 0 | Ex = getDataArg(argIndex + 1); |
11710 | 0 | const analyze_printf::ArgType &AT2 = ArgType::CStrTy; |
11711 | 0 | if (AT2.isValid() && !AT2.matchesType(S.Context, Ex->getType())) |
11712 | 0 | EmitFormatDiagnostic( |
11713 | 0 | S.PDiag(diag::warn_format_conversion_argument_type_mismatch) |
11714 | 0 | << AT2.getRepresentativeTypeName(S.Context) << Ex->getType() |
11715 | 0 | << false << Ex->getSourceRange(), |
11716 | 0 | Ex->getBeginLoc(), /*IsStringLocation*/ false, |
11717 | 0 | getSpecifierRange(startSpecifier, specifierLen)); |
11718 | |
|
11719 | 0 | return true; |
11720 | 0 | } |
11721 | | |
11722 | | // Check for using an Objective-C specific conversion specifier |
11723 | | // in a non-ObjC literal. |
11724 | 0 | if (!allowsObjCArg() && CS.isObjCArg()) { |
11725 | 0 | return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, |
11726 | 0 | specifierLen); |
11727 | 0 | } |
11728 | | |
11729 | | // %P can only be used with os_log. |
11730 | 0 | if (FSType != Sema::FST_OSLog && CS.getKind() == ConversionSpecifier::PArg) { |
11731 | 0 | return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, |
11732 | 0 | specifierLen); |
11733 | 0 | } |
11734 | | |
11735 | | // %n is not allowed with os_log. |
11736 | 0 | if (FSType == Sema::FST_OSLog && CS.getKind() == ConversionSpecifier::nArg) { |
11737 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_os_log_format_narg), |
11738 | 0 | getLocationOfByte(CS.getStart()), |
11739 | 0 | /*IsStringLocation*/ false, |
11740 | 0 | getSpecifierRange(startSpecifier, specifierLen)); |
11741 | |
|
11742 | 0 | return true; |
11743 | 0 | } |
11744 | | |
11745 | | // Only scalars are allowed for os_trace. |
11746 | 0 | if (FSType == Sema::FST_OSTrace && |
11747 | 0 | (CS.getKind() == ConversionSpecifier::PArg || |
11748 | 0 | CS.getKind() == ConversionSpecifier::sArg || |
11749 | 0 | CS.getKind() == ConversionSpecifier::ObjCObjArg)) { |
11750 | 0 | return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, |
11751 | 0 | specifierLen); |
11752 | 0 | } |
11753 | | |
11754 | | // Check for use of public/private annotation outside of os_log(). |
11755 | 0 | if (FSType != Sema::FST_OSLog) { |
11756 | 0 | if (FS.isPublic().isSet()) { |
11757 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_annotation) |
11758 | 0 | << "public", |
11759 | 0 | getLocationOfByte(FS.isPublic().getPosition()), |
11760 | 0 | /*IsStringLocation*/ false, |
11761 | 0 | getSpecifierRange(startSpecifier, specifierLen)); |
11762 | 0 | } |
11763 | 0 | if (FS.isPrivate().isSet()) { |
11764 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_annotation) |
11765 | 0 | << "private", |
11766 | 0 | getLocationOfByte(FS.isPrivate().getPosition()), |
11767 | 0 | /*IsStringLocation*/ false, |
11768 | 0 | getSpecifierRange(startSpecifier, specifierLen)); |
11769 | 0 | } |
11770 | 0 | } |
11771 | |
|
11772 | 0 | const llvm::Triple &Triple = Target.getTriple(); |
11773 | 0 | if (CS.getKind() == ConversionSpecifier::nArg && |
11774 | 0 | (Triple.isAndroid() || Triple.isOSFuchsia())) { |
11775 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_narg_not_supported), |
11776 | 0 | getLocationOfByte(CS.getStart()), |
11777 | 0 | /*IsStringLocation*/ false, |
11778 | 0 | getSpecifierRange(startSpecifier, specifierLen)); |
11779 | 0 | } |
11780 | | |
11781 | | // Check for invalid use of field width |
11782 | 0 | if (!FS.hasValidFieldWidth()) { |
11783 | 0 | HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, |
11784 | 0 | startSpecifier, specifierLen); |
11785 | 0 | } |
11786 | | |
11787 | | // Check for invalid use of precision |
11788 | 0 | if (!FS.hasValidPrecision()) { |
11789 | 0 | HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, |
11790 | 0 | startSpecifier, specifierLen); |
11791 | 0 | } |
11792 | | |
11793 | | // Precision is mandatory for %P specifier. |
11794 | 0 | if (CS.getKind() == ConversionSpecifier::PArg && |
11795 | 0 | FS.getPrecision().getHowSpecified() == OptionalAmount::NotSpecified) { |
11796 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_P_no_precision), |
11797 | 0 | getLocationOfByte(startSpecifier), |
11798 | 0 | /*IsStringLocation*/ false, |
11799 | 0 | getSpecifierRange(startSpecifier, specifierLen)); |
11800 | 0 | } |
11801 | | |
11802 | | // Check each flag does not conflict with any other component. |
11803 | 0 | if (!FS.hasValidThousandsGroupingPrefix()) |
11804 | 0 | HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen); |
11805 | 0 | if (!FS.hasValidLeadingZeros()) |
11806 | 0 | HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); |
11807 | 0 | if (!FS.hasValidPlusPrefix()) |
11808 | 0 | HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); |
11809 | 0 | if (!FS.hasValidSpacePrefix()) |
11810 | 0 | HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); |
11811 | 0 | if (!FS.hasValidAlternativeForm()) |
11812 | 0 | HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); |
11813 | 0 | if (!FS.hasValidLeftJustified()) |
11814 | 0 | HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); |
11815 | | |
11816 | | // Check that flags are not ignored by another flag |
11817 | 0 | if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' |
11818 | 0 | HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), |
11819 | 0 | startSpecifier, specifierLen); |
11820 | 0 | if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' |
11821 | 0 | HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), |
11822 | 0 | startSpecifier, specifierLen); |
11823 | | |
11824 | | // Check the length modifier is valid with the given conversion specifier. |
11825 | 0 | if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo(), |
11826 | 0 | S.getLangOpts())) |
11827 | 0 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
11828 | 0 | diag::warn_format_nonsensical_length); |
11829 | 0 | else if (!FS.hasStandardLengthModifier()) |
11830 | 0 | HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); |
11831 | 0 | else if (!FS.hasStandardLengthConversionCombination()) |
11832 | 0 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
11833 | 0 | diag::warn_format_non_standard_conversion_spec); |
11834 | |
|
11835 | 0 | if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) |
11836 | 0 | HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); |
11837 | | |
11838 | | // The remaining checks depend on the data arguments. |
11839 | 0 | if (ArgPassingKind == Sema::FAPK_VAList) |
11840 | 0 | return true; |
11841 | | |
11842 | 0 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
11843 | 0 | return false; |
11844 | | |
11845 | 0 | const Expr *Arg = getDataArg(argIndex); |
11846 | 0 | if (!Arg) |
11847 | 0 | return true; |
11848 | | |
11849 | 0 | return checkFormatExpr(FS, startSpecifier, specifierLen, Arg); |
11850 | 0 | } |
11851 | | |
11852 | 0 | static bool requiresParensToAddCast(const Expr *E) { |
11853 | | // FIXME: We should have a general way to reason about operator |
11854 | | // precedence and whether parens are actually needed here. |
11855 | | // Take care of a few common cases where they aren't. |
11856 | 0 | const Expr *Inside = E->IgnoreImpCasts(); |
11857 | 0 | if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Inside)) |
11858 | 0 | Inside = POE->getSyntacticForm()->IgnoreImpCasts(); |
11859 | |
|
11860 | 0 | switch (Inside->getStmtClass()) { |
11861 | 0 | case Stmt::ArraySubscriptExprClass: |
11862 | 0 | case Stmt::CallExprClass: |
11863 | 0 | case Stmt::CharacterLiteralClass: |
11864 | 0 | case Stmt::CXXBoolLiteralExprClass: |
11865 | 0 | case Stmt::DeclRefExprClass: |
11866 | 0 | case Stmt::FloatingLiteralClass: |
11867 | 0 | case Stmt::IntegerLiteralClass: |
11868 | 0 | case Stmt::MemberExprClass: |
11869 | 0 | case Stmt::ObjCArrayLiteralClass: |
11870 | 0 | case Stmt::ObjCBoolLiteralExprClass: |
11871 | 0 | case Stmt::ObjCBoxedExprClass: |
11872 | 0 | case Stmt::ObjCDictionaryLiteralClass: |
11873 | 0 | case Stmt::ObjCEncodeExprClass: |
11874 | 0 | case Stmt::ObjCIvarRefExprClass: |
11875 | 0 | case Stmt::ObjCMessageExprClass: |
11876 | 0 | case Stmt::ObjCPropertyRefExprClass: |
11877 | 0 | case Stmt::ObjCStringLiteralClass: |
11878 | 0 | case Stmt::ObjCSubscriptRefExprClass: |
11879 | 0 | case Stmt::ParenExprClass: |
11880 | 0 | case Stmt::StringLiteralClass: |
11881 | 0 | case Stmt::UnaryOperatorClass: |
11882 | 0 | return false; |
11883 | 0 | default: |
11884 | 0 | return true; |
11885 | 0 | } |
11886 | 0 | } |
11887 | | |
11888 | | static std::pair<QualType, StringRef> |
11889 | | shouldNotPrintDirectly(const ASTContext &Context, |
11890 | | QualType IntendedTy, |
11891 | 0 | const Expr *E) { |
11892 | | // Use a 'while' to peel off layers of typedefs. |
11893 | 0 | QualType TyTy = IntendedTy; |
11894 | 0 | while (const TypedefType *UserTy = TyTy->getAs<TypedefType>()) { |
11895 | 0 | StringRef Name = UserTy->getDecl()->getName(); |
11896 | 0 | QualType CastTy = llvm::StringSwitch<QualType>(Name) |
11897 | 0 | .Case("CFIndex", Context.getNSIntegerType()) |
11898 | 0 | .Case("NSInteger", Context.getNSIntegerType()) |
11899 | 0 | .Case("NSUInteger", Context.getNSUIntegerType()) |
11900 | 0 | .Case("SInt32", Context.IntTy) |
11901 | 0 | .Case("UInt32", Context.UnsignedIntTy) |
11902 | 0 | .Default(QualType()); |
11903 | |
|
11904 | 0 | if (!CastTy.isNull()) |
11905 | 0 | return std::make_pair(CastTy, Name); |
11906 | | |
11907 | 0 | TyTy = UserTy->desugar(); |
11908 | 0 | } |
11909 | | |
11910 | | // Strip parens if necessary. |
11911 | 0 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
11912 | 0 | return shouldNotPrintDirectly(Context, |
11913 | 0 | PE->getSubExpr()->getType(), |
11914 | 0 | PE->getSubExpr()); |
11915 | | |
11916 | | // If this is a conditional expression, then its result type is constructed |
11917 | | // via usual arithmetic conversions and thus there might be no necessary |
11918 | | // typedef sugar there. Recurse to operands to check for NSInteger & |
11919 | | // Co. usage condition. |
11920 | 0 | if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
11921 | 0 | QualType TrueTy, FalseTy; |
11922 | 0 | StringRef TrueName, FalseName; |
11923 | |
|
11924 | 0 | std::tie(TrueTy, TrueName) = |
11925 | 0 | shouldNotPrintDirectly(Context, |
11926 | 0 | CO->getTrueExpr()->getType(), |
11927 | 0 | CO->getTrueExpr()); |
11928 | 0 | std::tie(FalseTy, FalseName) = |
11929 | 0 | shouldNotPrintDirectly(Context, |
11930 | 0 | CO->getFalseExpr()->getType(), |
11931 | 0 | CO->getFalseExpr()); |
11932 | |
|
11933 | 0 | if (TrueTy == FalseTy) |
11934 | 0 | return std::make_pair(TrueTy, TrueName); |
11935 | 0 | else if (TrueTy.isNull()) |
11936 | 0 | return std::make_pair(FalseTy, FalseName); |
11937 | 0 | else if (FalseTy.isNull()) |
11938 | 0 | return std::make_pair(TrueTy, TrueName); |
11939 | 0 | } |
11940 | | |
11941 | 0 | return std::make_pair(QualType(), StringRef()); |
11942 | 0 | } |
11943 | | |
11944 | | /// Return true if \p ICE is an implicit argument promotion of an arithmetic |
11945 | | /// type. Bit-field 'promotions' from a higher ranked type to a lower ranked |
11946 | | /// type do not count. |
11947 | | static bool |
11948 | 0 | isArithmeticArgumentPromotion(Sema &S, const ImplicitCastExpr *ICE) { |
11949 | 0 | QualType From = ICE->getSubExpr()->getType(); |
11950 | 0 | QualType To = ICE->getType(); |
11951 | | // It's an integer promotion if the destination type is the promoted |
11952 | | // source type. |
11953 | 0 | if (ICE->getCastKind() == CK_IntegralCast && |
11954 | 0 | S.Context.isPromotableIntegerType(From) && |
11955 | 0 | S.Context.getPromotedIntegerType(From) == To) |
11956 | 0 | return true; |
11957 | | // Look through vector types, since we do default argument promotion for |
11958 | | // those in OpenCL. |
11959 | 0 | if (const auto *VecTy = From->getAs<ExtVectorType>()) |
11960 | 0 | From = VecTy->getElementType(); |
11961 | 0 | if (const auto *VecTy = To->getAs<ExtVectorType>()) |
11962 | 0 | To = VecTy->getElementType(); |
11963 | | // It's a floating promotion if the source type is a lower rank. |
11964 | 0 | return ICE->getCastKind() == CK_FloatingCast && |
11965 | 0 | S.Context.getFloatingTypeOrder(From, To) < 0; |
11966 | 0 | } |
11967 | | |
11968 | | bool |
11969 | | CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, |
11970 | | const char *StartSpecifier, |
11971 | | unsigned SpecifierLen, |
11972 | 0 | const Expr *E) { |
11973 | 0 | using namespace analyze_format_string; |
11974 | 0 | using namespace analyze_printf; |
11975 | | |
11976 | | // Now type check the data expression that matches the |
11977 | | // format specifier. |
11978 | 0 | const analyze_printf::ArgType &AT = FS.getArgType(S.Context, isObjCContext()); |
11979 | 0 | if (!AT.isValid()) |
11980 | 0 | return true; |
11981 | | |
11982 | 0 | QualType ExprTy = E->getType(); |
11983 | 0 | while (const TypeOfExprType *TET = dyn_cast<TypeOfExprType>(ExprTy)) { |
11984 | 0 | ExprTy = TET->getUnderlyingExpr()->getType(); |
11985 | 0 | } |
11986 | | |
11987 | | // When using the format attribute in C++, you can receive a function or an |
11988 | | // array that will necessarily decay to a pointer when passed to the final |
11989 | | // format consumer. Apply decay before type comparison. |
11990 | 0 | if (ExprTy->canDecayToPointerType()) |
11991 | 0 | ExprTy = S.Context.getDecayedType(ExprTy); |
11992 | | |
11993 | | // Diagnose attempts to print a boolean value as a character. Unlike other |
11994 | | // -Wformat diagnostics, this is fine from a type perspective, but it still |
11995 | | // doesn't make sense. |
11996 | 0 | if (FS.getConversionSpecifier().getKind() == ConversionSpecifier::cArg && |
11997 | 0 | E->isKnownToHaveBooleanValue()) { |
11998 | 0 | const CharSourceRange &CSR = |
11999 | 0 | getSpecifierRange(StartSpecifier, SpecifierLen); |
12000 | 0 | SmallString<4> FSString; |
12001 | 0 | llvm::raw_svector_ostream os(FSString); |
12002 | 0 | FS.toString(os); |
12003 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_bool_as_character) |
12004 | 0 | << FSString, |
12005 | 0 | E->getExprLoc(), false, CSR); |
12006 | 0 | return true; |
12007 | 0 | } |
12008 | | |
12009 | 0 | ArgType::MatchKind ImplicitMatch = ArgType::NoMatch; |
12010 | 0 | ArgType::MatchKind Match = AT.matchesType(S.Context, ExprTy); |
12011 | 0 | if (Match == ArgType::Match) |
12012 | 0 | return true; |
12013 | | |
12014 | | // NoMatchPromotionTypeConfusion should be only returned in ImplictCastExpr |
12015 | 0 | assert(Match != ArgType::NoMatchPromotionTypeConfusion); |
12016 | | |
12017 | | // Look through argument promotions for our error message's reported type. |
12018 | | // This includes the integral and floating promotions, but excludes array |
12019 | | // and function pointer decay (seeing that an argument intended to be a |
12020 | | // string has type 'char [6]' is probably more confusing than 'char *') and |
12021 | | // certain bitfield promotions (bitfields can be 'demoted' to a lesser type). |
12022 | 0 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
12023 | 0 | if (isArithmeticArgumentPromotion(S, ICE)) { |
12024 | 0 | E = ICE->getSubExpr(); |
12025 | 0 | ExprTy = E->getType(); |
12026 | | |
12027 | | // Check if we didn't match because of an implicit cast from a 'char' |
12028 | | // or 'short' to an 'int'. This is done because printf is a varargs |
12029 | | // function. |
12030 | 0 | if (ICE->getType() == S.Context.IntTy || |
12031 | 0 | ICE->getType() == S.Context.UnsignedIntTy) { |
12032 | | // All further checking is done on the subexpression |
12033 | 0 | ImplicitMatch = AT.matchesType(S.Context, ExprTy); |
12034 | 0 | if (ImplicitMatch == ArgType::Match) |
12035 | 0 | return true; |
12036 | 0 | } |
12037 | 0 | } |
12038 | 0 | } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) { |
12039 | | // Special case for 'a', which has type 'int' in C. |
12040 | | // Note, however, that we do /not/ want to treat multibyte constants like |
12041 | | // 'MooV' as characters! This form is deprecated but still exists. In |
12042 | | // addition, don't treat expressions as of type 'char' if one byte length |
12043 | | // modifier is provided. |
12044 | 0 | if (ExprTy == S.Context.IntTy && |
12045 | 0 | FS.getLengthModifier().getKind() != LengthModifier::AsChar) |
12046 | 0 | if (llvm::isUIntN(S.Context.getCharWidth(), CL->getValue())) { |
12047 | 0 | ExprTy = S.Context.CharTy; |
12048 | | // To improve check results, we consider a character literal in C |
12049 | | // to be a 'char' rather than an 'int'. 'printf("%hd", 'a');' is |
12050 | | // more likely a type confusion situation, so we will suggest to |
12051 | | // use '%hhd' instead by discarding the MatchPromotion. |
12052 | 0 | if (Match == ArgType::MatchPromotion) |
12053 | 0 | Match = ArgType::NoMatch; |
12054 | 0 | } |
12055 | 0 | } |
12056 | 0 | if (Match == ArgType::MatchPromotion) { |
12057 | | // WG14 N2562 only clarified promotions in *printf |
12058 | | // For NSLog in ObjC, just preserve -Wformat behavior |
12059 | 0 | if (!S.getLangOpts().ObjC && |
12060 | 0 | ImplicitMatch != ArgType::NoMatchPromotionTypeConfusion && |
12061 | 0 | ImplicitMatch != ArgType::NoMatchTypeConfusion) |
12062 | 0 | return true; |
12063 | 0 | Match = ArgType::NoMatch; |
12064 | 0 | } |
12065 | 0 | if (ImplicitMatch == ArgType::NoMatchPedantic || |
12066 | 0 | ImplicitMatch == ArgType::NoMatchTypeConfusion) |
12067 | 0 | Match = ImplicitMatch; |
12068 | 0 | assert(Match != ArgType::MatchPromotion); |
12069 | | |
12070 | | // Look through unscoped enums to their underlying type. |
12071 | 0 | bool IsEnum = false; |
12072 | 0 | bool IsScopedEnum = false; |
12073 | 0 | QualType IntendedTy = ExprTy; |
12074 | 0 | if (auto EnumTy = ExprTy->getAs<EnumType>()) { |
12075 | 0 | IntendedTy = EnumTy->getDecl()->getIntegerType(); |
12076 | 0 | if (EnumTy->isUnscopedEnumerationType()) { |
12077 | 0 | ExprTy = IntendedTy; |
12078 | | // This controls whether we're talking about the underlying type or not, |
12079 | | // which we only want to do when it's an unscoped enum. |
12080 | 0 | IsEnum = true; |
12081 | 0 | } else { |
12082 | 0 | IsScopedEnum = true; |
12083 | 0 | } |
12084 | 0 | } |
12085 | | |
12086 | | // %C in an Objective-C context prints a unichar, not a wchar_t. |
12087 | | // If the argument is an integer of some kind, believe the %C and suggest |
12088 | | // a cast instead of changing the conversion specifier. |
12089 | 0 | if (isObjCContext() && |
12090 | 0 | FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) { |
12091 | 0 | if (ExprTy->isIntegralOrUnscopedEnumerationType() && |
12092 | 0 | !ExprTy->isCharType()) { |
12093 | | // 'unichar' is defined as a typedef of unsigned short, but we should |
12094 | | // prefer using the typedef if it is visible. |
12095 | 0 | IntendedTy = S.Context.UnsignedShortTy; |
12096 | | |
12097 | | // While we are here, check if the value is an IntegerLiteral that happens |
12098 | | // to be within the valid range. |
12099 | 0 | if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) { |
12100 | 0 | const llvm::APInt &V = IL->getValue(); |
12101 | 0 | if (V.getActiveBits() <= S.Context.getTypeSize(IntendedTy)) |
12102 | 0 | return true; |
12103 | 0 | } |
12104 | | |
12105 | 0 | LookupResult Result(S, &S.Context.Idents.get("unichar"), E->getBeginLoc(), |
12106 | 0 | Sema::LookupOrdinaryName); |
12107 | 0 | if (S.LookupName(Result, S.getCurScope())) { |
12108 | 0 | NamedDecl *ND = Result.getFoundDecl(); |
12109 | 0 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND)) |
12110 | 0 | if (TD->getUnderlyingType() == IntendedTy) |
12111 | 0 | IntendedTy = S.Context.getTypedefType(TD); |
12112 | 0 | } |
12113 | 0 | } |
12114 | 0 | } |
12115 | | |
12116 | | // Special-case some of Darwin's platform-independence types by suggesting |
12117 | | // casts to primitive types that are known to be large enough. |
12118 | 0 | bool ShouldNotPrintDirectly = false; StringRef CastTyName; |
12119 | 0 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
12120 | 0 | QualType CastTy; |
12121 | 0 | std::tie(CastTy, CastTyName) = shouldNotPrintDirectly(S.Context, IntendedTy, E); |
12122 | 0 | if (!CastTy.isNull()) { |
12123 | | // %zi/%zu and %td/%tu are OK to use for NSInteger/NSUInteger of type int |
12124 | | // (long in ASTContext). Only complain to pedants or when they're the |
12125 | | // underlying type of a scoped enum (which always needs a cast). |
12126 | 0 | if (!IsScopedEnum && |
12127 | 0 | (CastTyName == "NSInteger" || CastTyName == "NSUInteger") && |
12128 | 0 | (AT.isSizeT() || AT.isPtrdiffT()) && |
12129 | 0 | AT.matchesType(S.Context, CastTy)) |
12130 | 0 | Match = ArgType::NoMatchPedantic; |
12131 | 0 | IntendedTy = CastTy; |
12132 | 0 | ShouldNotPrintDirectly = true; |
12133 | 0 | } |
12134 | 0 | } |
12135 | | |
12136 | | // We may be able to offer a FixItHint if it is a supported type. |
12137 | 0 | PrintfSpecifier fixedFS = FS; |
12138 | 0 | bool Success = |
12139 | 0 | fixedFS.fixType(IntendedTy, S.getLangOpts(), S.Context, isObjCContext()); |
12140 | |
|
12141 | 0 | if (Success) { |
12142 | | // Get the fix string from the fixed format specifier |
12143 | 0 | SmallString<16> buf; |
12144 | 0 | llvm::raw_svector_ostream os(buf); |
12145 | 0 | fixedFS.toString(os); |
12146 | |
|
12147 | 0 | CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen); |
12148 | |
|
12149 | 0 | if (IntendedTy == ExprTy && !ShouldNotPrintDirectly && !IsScopedEnum) { |
12150 | 0 | unsigned Diag; |
12151 | 0 | switch (Match) { |
12152 | 0 | case ArgType::Match: |
12153 | 0 | case ArgType::MatchPromotion: |
12154 | 0 | case ArgType::NoMatchPromotionTypeConfusion: |
12155 | 0 | llvm_unreachable("expected non-matching"); |
12156 | 0 | case ArgType::NoMatchPedantic: |
12157 | 0 | Diag = diag::warn_format_conversion_argument_type_mismatch_pedantic; |
12158 | 0 | break; |
12159 | 0 | case ArgType::NoMatchTypeConfusion: |
12160 | 0 | Diag = diag::warn_format_conversion_argument_type_mismatch_confusion; |
12161 | 0 | break; |
12162 | 0 | case ArgType::NoMatch: |
12163 | 0 | Diag = diag::warn_format_conversion_argument_type_mismatch; |
12164 | 0 | break; |
12165 | 0 | } |
12166 | | |
12167 | | // In this case, the specifier is wrong and should be changed to match |
12168 | | // the argument. |
12169 | 0 | EmitFormatDiagnostic(S.PDiag(Diag) |
12170 | 0 | << AT.getRepresentativeTypeName(S.Context) |
12171 | 0 | << IntendedTy << IsEnum << E->getSourceRange(), |
12172 | 0 | E->getBeginLoc(), |
12173 | 0 | /*IsStringLocation*/ false, SpecRange, |
12174 | 0 | FixItHint::CreateReplacement(SpecRange, os.str())); |
12175 | 0 | } else { |
12176 | | // The canonical type for formatting this value is different from the |
12177 | | // actual type of the expression. (This occurs, for example, with Darwin's |
12178 | | // NSInteger on 32-bit platforms, where it is typedef'd as 'int', but |
12179 | | // should be printed as 'long' for 64-bit compatibility.) |
12180 | | // Rather than emitting a normal format/argument mismatch, we want to |
12181 | | // add a cast to the recommended type (and correct the format string |
12182 | | // if necessary). We should also do so for scoped enumerations. |
12183 | 0 | SmallString<16> CastBuf; |
12184 | 0 | llvm::raw_svector_ostream CastFix(CastBuf); |
12185 | 0 | CastFix << (S.LangOpts.CPlusPlus ? "static_cast<" : "("); |
12186 | 0 | IntendedTy.print(CastFix, S.Context.getPrintingPolicy()); |
12187 | 0 | CastFix << (S.LangOpts.CPlusPlus ? ">" : ")"); |
12188 | |
|
12189 | 0 | SmallVector<FixItHint,4> Hints; |
12190 | 0 | if (AT.matchesType(S.Context, IntendedTy) != ArgType::Match || |
12191 | 0 | ShouldNotPrintDirectly) |
12192 | 0 | Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str())); |
12193 | |
|
12194 | 0 | if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) { |
12195 | | // If there's already a cast present, just replace it. |
12196 | 0 | SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc()); |
12197 | 0 | Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str())); |
12198 | |
|
12199 | 0 | } else if (!requiresParensToAddCast(E) && !S.LangOpts.CPlusPlus) { |
12200 | | // If the expression has high enough precedence, |
12201 | | // just write the C-style cast. |
12202 | 0 | Hints.push_back( |
12203 | 0 | FixItHint::CreateInsertion(E->getBeginLoc(), CastFix.str())); |
12204 | 0 | } else { |
12205 | | // Otherwise, add parens around the expression as well as the cast. |
12206 | 0 | CastFix << "("; |
12207 | 0 | Hints.push_back( |
12208 | 0 | FixItHint::CreateInsertion(E->getBeginLoc(), CastFix.str())); |
12209 | | |
12210 | | // We don't use getLocForEndOfToken because it returns invalid source |
12211 | | // locations for macro expansions (by design). |
12212 | 0 | SourceLocation EndLoc = S.SourceMgr.getSpellingLoc(E->getEndLoc()); |
12213 | 0 | SourceLocation After = EndLoc.getLocWithOffset( |
12214 | 0 | Lexer::MeasureTokenLength(EndLoc, S.SourceMgr, S.LangOpts)); |
12215 | 0 | Hints.push_back(FixItHint::CreateInsertion(After, ")")); |
12216 | 0 | } |
12217 | |
|
12218 | 0 | if (ShouldNotPrintDirectly && !IsScopedEnum) { |
12219 | | // The expression has a type that should not be printed directly. |
12220 | | // We extract the name from the typedef because we don't want to show |
12221 | | // the underlying type in the diagnostic. |
12222 | 0 | StringRef Name; |
12223 | 0 | if (const auto *TypedefTy = ExprTy->getAs<TypedefType>()) |
12224 | 0 | Name = TypedefTy->getDecl()->getName(); |
12225 | 0 | else |
12226 | 0 | Name = CastTyName; |
12227 | 0 | unsigned Diag = Match == ArgType::NoMatchPedantic |
12228 | 0 | ? diag::warn_format_argument_needs_cast_pedantic |
12229 | 0 | : diag::warn_format_argument_needs_cast; |
12230 | 0 | EmitFormatDiagnostic(S.PDiag(Diag) << Name << IntendedTy << IsEnum |
12231 | 0 | << E->getSourceRange(), |
12232 | 0 | E->getBeginLoc(), /*IsStringLocation=*/false, |
12233 | 0 | SpecRange, Hints); |
12234 | 0 | } else { |
12235 | | // In this case, the expression could be printed using a different |
12236 | | // specifier, but we've decided that the specifier is probably correct |
12237 | | // and we should cast instead. Just use the normal warning message. |
12238 | 0 | EmitFormatDiagnostic( |
12239 | 0 | S.PDiag(diag::warn_format_conversion_argument_type_mismatch) |
12240 | 0 | << AT.getRepresentativeTypeName(S.Context) << ExprTy << IsEnum |
12241 | 0 | << E->getSourceRange(), |
12242 | 0 | E->getBeginLoc(), /*IsStringLocation*/ false, SpecRange, Hints); |
12243 | 0 | } |
12244 | 0 | } |
12245 | 0 | } else { |
12246 | 0 | const CharSourceRange &CSR = getSpecifierRange(StartSpecifier, |
12247 | 0 | SpecifierLen); |
12248 | | // Since the warning for passing non-POD types to variadic functions |
12249 | | // was deferred until now, we emit a warning for non-POD |
12250 | | // arguments here. |
12251 | 0 | bool EmitTypeMismatch = false; |
12252 | 0 | switch (S.isValidVarArgType(ExprTy)) { |
12253 | 0 | case Sema::VAK_Valid: |
12254 | 0 | case Sema::VAK_ValidInCXX11: { |
12255 | 0 | unsigned Diag; |
12256 | 0 | switch (Match) { |
12257 | 0 | case ArgType::Match: |
12258 | 0 | case ArgType::MatchPromotion: |
12259 | 0 | case ArgType::NoMatchPromotionTypeConfusion: |
12260 | 0 | llvm_unreachable("expected non-matching"); |
12261 | 0 | case ArgType::NoMatchPedantic: |
12262 | 0 | Diag = diag::warn_format_conversion_argument_type_mismatch_pedantic; |
12263 | 0 | break; |
12264 | 0 | case ArgType::NoMatchTypeConfusion: |
12265 | 0 | Diag = diag::warn_format_conversion_argument_type_mismatch_confusion; |
12266 | 0 | break; |
12267 | 0 | case ArgType::NoMatch: |
12268 | 0 | Diag = diag::warn_format_conversion_argument_type_mismatch; |
12269 | 0 | break; |
12270 | 0 | } |
12271 | | |
12272 | 0 | EmitFormatDiagnostic( |
12273 | 0 | S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context) << ExprTy |
12274 | 0 | << IsEnum << CSR << E->getSourceRange(), |
12275 | 0 | E->getBeginLoc(), /*IsStringLocation*/ false, CSR); |
12276 | 0 | break; |
12277 | 0 | } |
12278 | 0 | case Sema::VAK_Undefined: |
12279 | 0 | case Sema::VAK_MSVCUndefined: |
12280 | 0 | if (CallType == Sema::VariadicDoesNotApply) { |
12281 | 0 | EmitTypeMismatch = true; |
12282 | 0 | } else { |
12283 | 0 | EmitFormatDiagnostic( |
12284 | 0 | S.PDiag(diag::warn_non_pod_vararg_with_format_string) |
12285 | 0 | << S.getLangOpts().CPlusPlus11 << ExprTy << CallType |
12286 | 0 | << AT.getRepresentativeTypeName(S.Context) << CSR |
12287 | 0 | << E->getSourceRange(), |
12288 | 0 | E->getBeginLoc(), /*IsStringLocation*/ false, CSR); |
12289 | 0 | checkForCStrMembers(AT, E); |
12290 | 0 | } |
12291 | 0 | break; |
12292 | | |
12293 | 0 | case Sema::VAK_Invalid: |
12294 | 0 | if (CallType == Sema::VariadicDoesNotApply) |
12295 | 0 | EmitTypeMismatch = true; |
12296 | 0 | else if (ExprTy->isObjCObjectType()) |
12297 | 0 | EmitFormatDiagnostic( |
12298 | 0 | S.PDiag(diag::err_cannot_pass_objc_interface_to_vararg_format) |
12299 | 0 | << S.getLangOpts().CPlusPlus11 << ExprTy << CallType |
12300 | 0 | << AT.getRepresentativeTypeName(S.Context) << CSR |
12301 | 0 | << E->getSourceRange(), |
12302 | 0 | E->getBeginLoc(), /*IsStringLocation*/ false, CSR); |
12303 | 0 | else |
12304 | | // FIXME: If this is an initializer list, suggest removing the braces |
12305 | | // or inserting a cast to the target type. |
12306 | 0 | S.Diag(E->getBeginLoc(), diag::err_cannot_pass_to_vararg_format) |
12307 | 0 | << isa<InitListExpr>(E) << ExprTy << CallType |
12308 | 0 | << AT.getRepresentativeTypeName(S.Context) << E->getSourceRange(); |
12309 | 0 | break; |
12310 | 0 | } |
12311 | | |
12312 | 0 | if (EmitTypeMismatch) { |
12313 | | // The function is not variadic, so we do not generate warnings about |
12314 | | // being allowed to pass that object as a variadic argument. Instead, |
12315 | | // since there are inherently no printf specifiers for types which cannot |
12316 | | // be passed as variadic arguments, emit a plain old specifier mismatch |
12317 | | // argument. |
12318 | 0 | EmitFormatDiagnostic( |
12319 | 0 | S.PDiag(diag::warn_format_conversion_argument_type_mismatch) |
12320 | 0 | << AT.getRepresentativeTypeName(S.Context) << ExprTy << false |
12321 | 0 | << E->getSourceRange(), |
12322 | 0 | E->getBeginLoc(), false, CSR); |
12323 | 0 | } |
12324 | |
|
12325 | 0 | assert(FirstDataArg + FS.getArgIndex() < CheckedVarArgs.size() && |
12326 | 0 | "format string specifier index out of range"); |
12327 | 0 | CheckedVarArgs[FirstDataArg + FS.getArgIndex()] = true; |
12328 | 0 | } |
12329 | | |
12330 | 0 | return true; |
12331 | 0 | } |
12332 | | |
12333 | | //===--- CHECK: Scanf format string checking ------------------------------===// |
12334 | | |
12335 | | namespace { |
12336 | | |
12337 | | class CheckScanfHandler : public CheckFormatHandler { |
12338 | | public: |
12339 | | CheckScanfHandler(Sema &s, const FormatStringLiteral *fexpr, |
12340 | | const Expr *origFormatExpr, Sema::FormatStringType type, |
12341 | | unsigned firstDataArg, unsigned numDataArgs, |
12342 | | const char *beg, Sema::FormatArgumentPassingKind APK, |
12343 | | ArrayRef<const Expr *> Args, unsigned formatIdx, |
12344 | | bool inFunctionCall, Sema::VariadicCallType CallType, |
12345 | | llvm::SmallBitVector &CheckedVarArgs, |
12346 | | UncoveredArgHandler &UncoveredArg) |
12347 | | : CheckFormatHandler(s, fexpr, origFormatExpr, type, firstDataArg, |
12348 | | numDataArgs, beg, APK, Args, formatIdx, |
12349 | | inFunctionCall, CallType, CheckedVarArgs, |
12350 | 0 | UncoveredArg) {} |
12351 | | |
12352 | | bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, |
12353 | | const char *startSpecifier, |
12354 | | unsigned specifierLen) override; |
12355 | | |
12356 | | bool HandleInvalidScanfConversionSpecifier( |
12357 | | const analyze_scanf::ScanfSpecifier &FS, |
12358 | | const char *startSpecifier, |
12359 | | unsigned specifierLen) override; |
12360 | | |
12361 | | void HandleIncompleteScanList(const char *start, const char *end) override; |
12362 | | }; |
12363 | | |
12364 | | } // namespace |
12365 | | |
12366 | | void CheckScanfHandler::HandleIncompleteScanList(const char *start, |
12367 | 0 | const char *end) { |
12368 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete), |
12369 | 0 | getLocationOfByte(end), /*IsStringLocation*/true, |
12370 | 0 | getSpecifierRange(start, end - start)); |
12371 | 0 | } |
12372 | | |
12373 | | bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( |
12374 | | const analyze_scanf::ScanfSpecifier &FS, |
12375 | | const char *startSpecifier, |
12376 | 0 | unsigned specifierLen) { |
12377 | 0 | const analyze_scanf::ScanfConversionSpecifier &CS = |
12378 | 0 | FS.getConversionSpecifier(); |
12379 | |
|
12380 | 0 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
12381 | 0 | getLocationOfByte(CS.getStart()), |
12382 | 0 | startSpecifier, specifierLen, |
12383 | 0 | CS.getStart(), CS.getLength()); |
12384 | 0 | } |
12385 | | |
12386 | | bool CheckScanfHandler::HandleScanfSpecifier( |
12387 | | const analyze_scanf::ScanfSpecifier &FS, |
12388 | | const char *startSpecifier, |
12389 | 0 | unsigned specifierLen) { |
12390 | 0 | using namespace analyze_scanf; |
12391 | 0 | using namespace analyze_format_string; |
12392 | |
|
12393 | 0 | const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); |
12394 | | |
12395 | | // Handle case where '%' and '*' don't consume an argument. These shouldn't |
12396 | | // be used to decide if we are using positional arguments consistently. |
12397 | 0 | if (FS.consumesDataArgument()) { |
12398 | 0 | if (atFirstArg) { |
12399 | 0 | atFirstArg = false; |
12400 | 0 | usesPositionalArgs = FS.usesPositionalArg(); |
12401 | 0 | } |
12402 | 0 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
12403 | 0 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), |
12404 | 0 | startSpecifier, specifierLen); |
12405 | 0 | return false; |
12406 | 0 | } |
12407 | 0 | } |
12408 | | |
12409 | | // Check if the field with is non-zero. |
12410 | 0 | const OptionalAmount &Amt = FS.getFieldWidth(); |
12411 | 0 | if (Amt.getHowSpecified() == OptionalAmount::Constant) { |
12412 | 0 | if (Amt.getConstantAmount() == 0) { |
12413 | 0 | const CharSourceRange &R = getSpecifierRange(Amt.getStart(), |
12414 | 0 | Amt.getConstantLength()); |
12415 | 0 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width), |
12416 | 0 | getLocationOfByte(Amt.getStart()), |
12417 | 0 | /*IsStringLocation*/true, R, |
12418 | 0 | FixItHint::CreateRemoval(R)); |
12419 | 0 | } |
12420 | 0 | } |
12421 | |
|
12422 | 0 | if (!FS.consumesDataArgument()) { |
12423 | | // FIXME: Technically specifying a precision or field width here |
12424 | | // makes no sense. Worth issuing a warning at some point. |
12425 | 0 | return true; |
12426 | 0 | } |
12427 | | |
12428 | | // Consume the argument. |
12429 | 0 | unsigned argIndex = FS.getArgIndex(); |
12430 | 0 | if (argIndex < NumDataArgs) { |
12431 | | // The check to see if the argIndex is valid will come later. |
12432 | | // We set the bit here because we may exit early from this |
12433 | | // function if we encounter some other error. |
12434 | 0 | CoveredArgs.set(argIndex); |
12435 | 0 | } |
12436 | | |
12437 | | // Check the length modifier is valid with the given conversion specifier. |
12438 | 0 | if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo(), |
12439 | 0 | S.getLangOpts())) |
12440 | 0 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
12441 | 0 | diag::warn_format_nonsensical_length); |
12442 | 0 | else if (!FS.hasStandardLengthModifier()) |
12443 | 0 | HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); |
12444 | 0 | else if (!FS.hasStandardLengthConversionCombination()) |
12445 | 0 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
12446 | 0 | diag::warn_format_non_standard_conversion_spec); |
12447 | |
|
12448 | 0 | if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) |
12449 | 0 | HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); |
12450 | | |
12451 | | // The remaining checks depend on the data arguments. |
12452 | 0 | if (ArgPassingKind == Sema::FAPK_VAList) |
12453 | 0 | return true; |
12454 | | |
12455 | 0 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
12456 | 0 | return false; |
12457 | | |
12458 | | // Check that the argument type matches the format specifier. |
12459 | 0 | const Expr *Ex = getDataArg(argIndex); |
12460 | 0 | if (!Ex) |
12461 | 0 | return true; |
12462 | | |
12463 | 0 | const analyze_format_string::ArgType &AT = FS.getArgType(S.Context); |
12464 | |
|
12465 | 0 | if (!AT.isValid()) { |
12466 | 0 | return true; |
12467 | 0 | } |
12468 | | |
12469 | 0 | analyze_format_string::ArgType::MatchKind Match = |
12470 | 0 | AT.matchesType(S.Context, Ex->getType()); |
12471 | 0 | bool Pedantic = Match == analyze_format_string::ArgType::NoMatchPedantic; |
12472 | 0 | if (Match == analyze_format_string::ArgType::Match) |
12473 | 0 | return true; |
12474 | | |
12475 | 0 | ScanfSpecifier fixedFS = FS; |
12476 | 0 | bool Success = fixedFS.fixType(Ex->getType(), Ex->IgnoreImpCasts()->getType(), |
12477 | 0 | S.getLangOpts(), S.Context); |
12478 | |
|
12479 | 0 | unsigned Diag = |
12480 | 0 | Pedantic ? diag::warn_format_conversion_argument_type_mismatch_pedantic |
12481 | 0 | : diag::warn_format_conversion_argument_type_mismatch; |
12482 | |
|
12483 | 0 | if (Success) { |
12484 | | // Get the fix string from the fixed format specifier. |
12485 | 0 | SmallString<128> buf; |
12486 | 0 | llvm::raw_svector_ostream os(buf); |
12487 | 0 | fixedFS.toString(os); |
12488 | |
|
12489 | 0 | EmitFormatDiagnostic( |
12490 | 0 | S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context) |
12491 | 0 | << Ex->getType() << false << Ex->getSourceRange(), |
12492 | 0 | Ex->getBeginLoc(), |
12493 | 0 | /*IsStringLocation*/ false, |
12494 | 0 | getSpecifierRange(startSpecifier, specifierLen), |
12495 | 0 | FixItHint::CreateReplacement( |
12496 | 0 | getSpecifierRange(startSpecifier, specifierLen), os.str())); |
12497 | 0 | } else { |
12498 | 0 | EmitFormatDiagnostic(S.PDiag(Diag) |
12499 | 0 | << AT.getRepresentativeTypeName(S.Context) |
12500 | 0 | << Ex->getType() << false << Ex->getSourceRange(), |
12501 | 0 | Ex->getBeginLoc(), |
12502 | 0 | /*IsStringLocation*/ false, |
12503 | 0 | getSpecifierRange(startSpecifier, specifierLen)); |
12504 | 0 | } |
12505 | |
|
12506 | 0 | return true; |
12507 | 0 | } |
12508 | | |
12509 | | static void CheckFormatString( |
12510 | | Sema &S, const FormatStringLiteral *FExpr, const Expr *OrigFormatExpr, |
12511 | | ArrayRef<const Expr *> Args, Sema::FormatArgumentPassingKind APK, |
12512 | | unsigned format_idx, unsigned firstDataArg, Sema::FormatStringType Type, |
12513 | | bool inFunctionCall, Sema::VariadicCallType CallType, |
12514 | | llvm::SmallBitVector &CheckedVarArgs, UncoveredArgHandler &UncoveredArg, |
12515 | 0 | bool IgnoreStringsWithoutSpecifiers) { |
12516 | | // CHECK: is the format string a wide literal? |
12517 | 0 | if (!FExpr->isAscii() && !FExpr->isUTF8()) { |
12518 | 0 | CheckFormatHandler::EmitFormatDiagnostic( |
12519 | 0 | S, inFunctionCall, Args[format_idx], |
12520 | 0 | S.PDiag(diag::warn_format_string_is_wide_literal), FExpr->getBeginLoc(), |
12521 | 0 | /*IsStringLocation*/ true, OrigFormatExpr->getSourceRange()); |
12522 | 0 | return; |
12523 | 0 | } |
12524 | | |
12525 | | // Str - The format string. NOTE: this is NOT null-terminated! |
12526 | 0 | StringRef StrRef = FExpr->getString(); |
12527 | 0 | const char *Str = StrRef.data(); |
12528 | | // Account for cases where the string literal is truncated in a declaration. |
12529 | 0 | const ConstantArrayType *T = |
12530 | 0 | S.Context.getAsConstantArrayType(FExpr->getType()); |
12531 | 0 | assert(T && "String literal not of constant array type!"); |
12532 | 0 | size_t TypeSize = T->getSize().getZExtValue(); |
12533 | 0 | size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size()); |
12534 | 0 | const unsigned numDataArgs = Args.size() - firstDataArg; |
12535 | |
|
12536 | 0 | if (IgnoreStringsWithoutSpecifiers && |
12537 | 0 | !analyze_format_string::parseFormatStringHasFormattingSpecifiers( |
12538 | 0 | Str, Str + StrLen, S.getLangOpts(), S.Context.getTargetInfo())) |
12539 | 0 | return; |
12540 | | |
12541 | | // Emit a warning if the string literal is truncated and does not contain an |
12542 | | // embedded null character. |
12543 | 0 | if (TypeSize <= StrRef.size() && !StrRef.substr(0, TypeSize).contains('\0')) { |
12544 | 0 | CheckFormatHandler::EmitFormatDiagnostic( |
12545 | 0 | S, inFunctionCall, Args[format_idx], |
12546 | 0 | S.PDiag(diag::warn_printf_format_string_not_null_terminated), |
12547 | 0 | FExpr->getBeginLoc(), |
12548 | 0 | /*IsStringLocation=*/true, OrigFormatExpr->getSourceRange()); |
12549 | 0 | return; |
12550 | 0 | } |
12551 | | |
12552 | | // CHECK: empty format string? |
12553 | 0 | if (StrLen == 0 && numDataArgs > 0) { |
12554 | 0 | CheckFormatHandler::EmitFormatDiagnostic( |
12555 | 0 | S, inFunctionCall, Args[format_idx], |
12556 | 0 | S.PDiag(diag::warn_empty_format_string), FExpr->getBeginLoc(), |
12557 | 0 | /*IsStringLocation*/ true, OrigFormatExpr->getSourceRange()); |
12558 | 0 | return; |
12559 | 0 | } |
12560 | | |
12561 | 0 | if (Type == Sema::FST_Printf || Type == Sema::FST_NSString || |
12562 | 0 | Type == Sema::FST_FreeBSDKPrintf || Type == Sema::FST_OSLog || |
12563 | 0 | Type == Sema::FST_OSTrace) { |
12564 | 0 | CheckPrintfHandler H( |
12565 | 0 | S, FExpr, OrigFormatExpr, Type, firstDataArg, numDataArgs, |
12566 | 0 | (Type == Sema::FST_NSString || Type == Sema::FST_OSTrace), Str, APK, |
12567 | 0 | Args, format_idx, inFunctionCall, CallType, CheckedVarArgs, |
12568 | 0 | UncoveredArg); |
12569 | |
|
12570 | 0 | if (!analyze_format_string::ParsePrintfString( |
12571 | 0 | H, Str, Str + StrLen, S.getLangOpts(), S.Context.getTargetInfo(), |
12572 | 0 | Type == Sema::FST_FreeBSDKPrintf)) |
12573 | 0 | H.DoneProcessing(); |
12574 | 0 | } else if (Type == Sema::FST_Scanf) { |
12575 | 0 | CheckScanfHandler H(S, FExpr, OrigFormatExpr, Type, firstDataArg, |
12576 | 0 | numDataArgs, Str, APK, Args, format_idx, inFunctionCall, |
12577 | 0 | CallType, CheckedVarArgs, UncoveredArg); |
12578 | |
|
12579 | 0 | if (!analyze_format_string::ParseScanfString( |
12580 | 0 | H, Str, Str + StrLen, S.getLangOpts(), S.Context.getTargetInfo())) |
12581 | 0 | H.DoneProcessing(); |
12582 | 0 | } // TODO: handle other formats |
12583 | 0 | } |
12584 | | |
12585 | 0 | bool Sema::FormatStringHasSArg(const StringLiteral *FExpr) { |
12586 | | // Str - The format string. NOTE: this is NOT null-terminated! |
12587 | 0 | StringRef StrRef = FExpr->getString(); |
12588 | 0 | const char *Str = StrRef.data(); |
12589 | | // Account for cases where the string literal is truncated in a declaration. |
12590 | 0 | const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType()); |
12591 | 0 | assert(T && "String literal not of constant array type!"); |
12592 | 0 | size_t TypeSize = T->getSize().getZExtValue(); |
12593 | 0 | size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size()); |
12594 | 0 | return analyze_format_string::ParseFormatStringHasSArg(Str, Str + StrLen, |
12595 | 0 | getLangOpts(), |
12596 | 0 | Context.getTargetInfo()); |
12597 | 0 | } |
12598 | | |
12599 | | //===--- CHECK: Warn on use of wrong absolute value function. -------------===// |
12600 | | |
12601 | | // Returns the related absolute value function that is larger, of 0 if one |
12602 | | // does not exist. |
12603 | 0 | static unsigned getLargerAbsoluteValueFunction(unsigned AbsFunction) { |
12604 | 0 | switch (AbsFunction) { |
12605 | 0 | default: |
12606 | 0 | return 0; |
12607 | | |
12608 | 0 | case Builtin::BI__builtin_abs: |
12609 | 0 | return Builtin::BI__builtin_labs; |
12610 | 0 | case Builtin::BI__builtin_labs: |
12611 | 0 | return Builtin::BI__builtin_llabs; |
12612 | 0 | case Builtin::BI__builtin_llabs: |
12613 | 0 | return 0; |
12614 | | |
12615 | 0 | case Builtin::BI__builtin_fabsf: |
12616 | 0 | return Builtin::BI__builtin_fabs; |
12617 | 0 | case Builtin::BI__builtin_fabs: |
12618 | 0 | return Builtin::BI__builtin_fabsl; |
12619 | 0 | case Builtin::BI__builtin_fabsl: |
12620 | 0 | return 0; |
12621 | | |
12622 | 0 | case Builtin::BI__builtin_cabsf: |
12623 | 0 | return Builtin::BI__builtin_cabs; |
12624 | 0 | case Builtin::BI__builtin_cabs: |
12625 | 0 | return Builtin::BI__builtin_cabsl; |
12626 | 0 | case Builtin::BI__builtin_cabsl: |
12627 | 0 | return 0; |
12628 | | |
12629 | 0 | case Builtin::BIabs: |
12630 | 0 | return Builtin::BIlabs; |
12631 | 0 | case Builtin::BIlabs: |
12632 | 0 | return Builtin::BIllabs; |
12633 | 0 | case Builtin::BIllabs: |
12634 | 0 | return 0; |
12635 | | |
12636 | 0 | case Builtin::BIfabsf: |
12637 | 0 | return Builtin::BIfabs; |
12638 | 0 | case Builtin::BIfabs: |
12639 | 0 | return Builtin::BIfabsl; |
12640 | 0 | case Builtin::BIfabsl: |
12641 | 0 | return 0; |
12642 | | |
12643 | 0 | case Builtin::BIcabsf: |
12644 | 0 | return Builtin::BIcabs; |
12645 | 0 | case Builtin::BIcabs: |
12646 | 0 | return Builtin::BIcabsl; |
12647 | 0 | case Builtin::BIcabsl: |
12648 | 0 | return 0; |
12649 | 0 | } |
12650 | 0 | } |
12651 | | |
12652 | | // Returns the argument type of the absolute value function. |
12653 | | static QualType getAbsoluteValueArgumentType(ASTContext &Context, |
12654 | 0 | unsigned AbsType) { |
12655 | 0 | if (AbsType == 0) |
12656 | 0 | return QualType(); |
12657 | | |
12658 | 0 | ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None; |
12659 | 0 | QualType BuiltinType = Context.GetBuiltinType(AbsType, Error); |
12660 | 0 | if (Error != ASTContext::GE_None) |
12661 | 0 | return QualType(); |
12662 | | |
12663 | 0 | const FunctionProtoType *FT = BuiltinType->getAs<FunctionProtoType>(); |
12664 | 0 | if (!FT) |
12665 | 0 | return QualType(); |
12666 | | |
12667 | 0 | if (FT->getNumParams() != 1) |
12668 | 0 | return QualType(); |
12669 | | |
12670 | 0 | return FT->getParamType(0); |
12671 | 0 | } |
12672 | | |
12673 | | // Returns the best absolute value function, or zero, based on type and |
12674 | | // current absolute value function. |
12675 | | static unsigned getBestAbsFunction(ASTContext &Context, QualType ArgType, |
12676 | 0 | unsigned AbsFunctionKind) { |
12677 | 0 | unsigned BestKind = 0; |
12678 | 0 | uint64_t ArgSize = Context.getTypeSize(ArgType); |
12679 | 0 | for (unsigned Kind = AbsFunctionKind; Kind != 0; |
12680 | 0 | Kind = getLargerAbsoluteValueFunction(Kind)) { |
12681 | 0 | QualType ParamType = getAbsoluteValueArgumentType(Context, Kind); |
12682 | 0 | if (Context.getTypeSize(ParamType) >= ArgSize) { |
12683 | 0 | if (BestKind == 0) |
12684 | 0 | BestKind = Kind; |
12685 | 0 | else if (Context.hasSameType(ParamType, ArgType)) { |
12686 | 0 | BestKind = Kind; |
12687 | 0 | break; |
12688 | 0 | } |
12689 | 0 | } |
12690 | 0 | } |
12691 | 0 | return BestKind; |
12692 | 0 | } |
12693 | | |
12694 | | enum AbsoluteValueKind { |
12695 | | AVK_Integer, |
12696 | | AVK_Floating, |
12697 | | AVK_Complex |
12698 | | }; |
12699 | | |
12700 | 0 | static AbsoluteValueKind getAbsoluteValueKind(QualType T) { |
12701 | 0 | if (T->isIntegralOrEnumerationType()) |
12702 | 0 | return AVK_Integer; |
12703 | 0 | if (T->isRealFloatingType()) |
12704 | 0 | return AVK_Floating; |
12705 | 0 | if (T->isAnyComplexType()) |
12706 | 0 | return AVK_Complex; |
12707 | | |
12708 | 0 | llvm_unreachable("Type not integer, floating, or complex"); |
12709 | 0 | } |
12710 | | |
12711 | | // Changes the absolute value function to a different type. Preserves whether |
12712 | | // the function is a builtin. |
12713 | | static unsigned changeAbsFunction(unsigned AbsKind, |
12714 | 0 | AbsoluteValueKind ValueKind) { |
12715 | 0 | switch (ValueKind) { |
12716 | 0 | case AVK_Integer: |
12717 | 0 | switch (AbsKind) { |
12718 | 0 | default: |
12719 | 0 | return 0; |
12720 | 0 | case Builtin::BI__builtin_fabsf: |
12721 | 0 | case Builtin::BI__builtin_fabs: |
12722 | 0 | case Builtin::BI__builtin_fabsl: |
12723 | 0 | case Builtin::BI__builtin_cabsf: |
12724 | 0 | case Builtin::BI__builtin_cabs: |
12725 | 0 | case Builtin::BI__builtin_cabsl: |
12726 | 0 | return Builtin::BI__builtin_abs; |
12727 | 0 | case Builtin::BIfabsf: |
12728 | 0 | case Builtin::BIfabs: |
12729 | 0 | case Builtin::BIfabsl: |
12730 | 0 | case Builtin::BIcabsf: |
12731 | 0 | case Builtin::BIcabs: |
12732 | 0 | case Builtin::BIcabsl: |
12733 | 0 | return Builtin::BIabs; |
12734 | 0 | } |
12735 | 0 | case AVK_Floating: |
12736 | 0 | switch (AbsKind) { |
12737 | 0 | default: |
12738 | 0 | return 0; |
12739 | 0 | case Builtin::BI__builtin_abs: |
12740 | 0 | case Builtin::BI__builtin_labs: |
12741 | 0 | case Builtin::BI__builtin_llabs: |
12742 | 0 | case Builtin::BI__builtin_cabsf: |
12743 | 0 | case Builtin::BI__builtin_cabs: |
12744 | 0 | case Builtin::BI__builtin_cabsl: |
12745 | 0 | return Builtin::BI__builtin_fabsf; |
12746 | 0 | case Builtin::BIabs: |
12747 | 0 | case Builtin::BIlabs: |
12748 | 0 | case Builtin::BIllabs: |
12749 | 0 | case Builtin::BIcabsf: |
12750 | 0 | case Builtin::BIcabs: |
12751 | 0 | case Builtin::BIcabsl: |
12752 | 0 | return Builtin::BIfabsf; |
12753 | 0 | } |
12754 | 0 | case AVK_Complex: |
12755 | 0 | switch (AbsKind) { |
12756 | 0 | default: |
12757 | 0 | return 0; |
12758 | 0 | case Builtin::BI__builtin_abs: |
12759 | 0 | case Builtin::BI__builtin_labs: |
12760 | 0 | case Builtin::BI__builtin_llabs: |
12761 | 0 | case Builtin::BI__builtin_fabsf: |
12762 | 0 | case Builtin::BI__builtin_fabs: |
12763 | 0 | case Builtin::BI__builtin_fabsl: |
12764 | 0 | return Builtin::BI__builtin_cabsf; |
12765 | 0 | case Builtin::BIabs: |
12766 | 0 | case Builtin::BIlabs: |
12767 | 0 | case Builtin::BIllabs: |
12768 | 0 | case Builtin::BIfabsf: |
12769 | 0 | case Builtin::BIfabs: |
12770 | 0 | case Builtin::BIfabsl: |
12771 | 0 | return Builtin::BIcabsf; |
12772 | 0 | } |
12773 | 0 | } |
12774 | 0 | llvm_unreachable("Unable to convert function"); |
12775 | 0 | } |
12776 | | |
12777 | 0 | static unsigned getAbsoluteValueFunctionKind(const FunctionDecl *FDecl) { |
12778 | 0 | const IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
12779 | 0 | if (!FnInfo) |
12780 | 0 | return 0; |
12781 | | |
12782 | 0 | switch (FDecl->getBuiltinID()) { |
12783 | 0 | default: |
12784 | 0 | return 0; |
12785 | 0 | case Builtin::BI__builtin_abs: |
12786 | 0 | case Builtin::BI__builtin_fabs: |
12787 | 0 | case Builtin::BI__builtin_fabsf: |
12788 | 0 | case Builtin::BI__builtin_fabsl: |
12789 | 0 | case Builtin::BI__builtin_labs: |
12790 | 0 | case Builtin::BI__builtin_llabs: |
12791 | 0 | case Builtin::BI__builtin_cabs: |
12792 | 0 | case Builtin::BI__builtin_cabsf: |
12793 | 0 | case Builtin::BI__builtin_cabsl: |
12794 | 0 | case Builtin::BIabs: |
12795 | 0 | case Builtin::BIlabs: |
12796 | 0 | case Builtin::BIllabs: |
12797 | 0 | case Builtin::BIfabs: |
12798 | 0 | case Builtin::BIfabsf: |
12799 | 0 | case Builtin::BIfabsl: |
12800 | 0 | case Builtin::BIcabs: |
12801 | 0 | case Builtin::BIcabsf: |
12802 | 0 | case Builtin::BIcabsl: |
12803 | 0 | return FDecl->getBuiltinID(); |
12804 | 0 | } |
12805 | 0 | llvm_unreachable("Unknown Builtin type"); |
12806 | 0 | } |
12807 | | |
12808 | | // If the replacement is valid, emit a note with replacement function. |
12809 | | // Additionally, suggest including the proper header if not already included. |
12810 | | static void emitReplacement(Sema &S, SourceLocation Loc, SourceRange Range, |
12811 | 0 | unsigned AbsKind, QualType ArgType) { |
12812 | 0 | bool EmitHeaderHint = true; |
12813 | 0 | const char *HeaderName = nullptr; |
12814 | 0 | StringRef FunctionName; |
12815 | 0 | if (S.getLangOpts().CPlusPlus && !ArgType->isAnyComplexType()) { |
12816 | 0 | FunctionName = "std::abs"; |
12817 | 0 | if (ArgType->isIntegralOrEnumerationType()) { |
12818 | 0 | HeaderName = "cstdlib"; |
12819 | 0 | } else if (ArgType->isRealFloatingType()) { |
12820 | 0 | HeaderName = "cmath"; |
12821 | 0 | } else { |
12822 | 0 | llvm_unreachable("Invalid Type"); |
12823 | 0 | } |
12824 | | |
12825 | | // Lookup all std::abs |
12826 | 0 | if (NamespaceDecl *Std = S.getStdNamespace()) { |
12827 | 0 | LookupResult R(S, &S.Context.Idents.get("abs"), Loc, Sema::LookupAnyName); |
12828 | 0 | R.suppressDiagnostics(); |
12829 | 0 | S.LookupQualifiedName(R, Std); |
12830 | |
|
12831 | 0 | for (const auto *I : R) { |
12832 | 0 | const FunctionDecl *FDecl = nullptr; |
12833 | 0 | if (const UsingShadowDecl *UsingD = dyn_cast<UsingShadowDecl>(I)) { |
12834 | 0 | FDecl = dyn_cast<FunctionDecl>(UsingD->getTargetDecl()); |
12835 | 0 | } else { |
12836 | 0 | FDecl = dyn_cast<FunctionDecl>(I); |
12837 | 0 | } |
12838 | 0 | if (!FDecl) |
12839 | 0 | continue; |
12840 | | |
12841 | | // Found std::abs(), check that they are the right ones. |
12842 | 0 | if (FDecl->getNumParams() != 1) |
12843 | 0 | continue; |
12844 | | |
12845 | | // Check that the parameter type can handle the argument. |
12846 | 0 | QualType ParamType = FDecl->getParamDecl(0)->getType(); |
12847 | 0 | if (getAbsoluteValueKind(ArgType) == getAbsoluteValueKind(ParamType) && |
12848 | 0 | S.Context.getTypeSize(ArgType) <= |
12849 | 0 | S.Context.getTypeSize(ParamType)) { |
12850 | | // Found a function, don't need the header hint. |
12851 | 0 | EmitHeaderHint = false; |
12852 | 0 | break; |
12853 | 0 | } |
12854 | 0 | } |
12855 | 0 | } |
12856 | 0 | } else { |
12857 | 0 | FunctionName = S.Context.BuiltinInfo.getName(AbsKind); |
12858 | 0 | HeaderName = S.Context.BuiltinInfo.getHeaderName(AbsKind); |
12859 | |
|
12860 | 0 | if (HeaderName) { |
12861 | 0 | DeclarationName DN(&S.Context.Idents.get(FunctionName)); |
12862 | 0 | LookupResult R(S, DN, Loc, Sema::LookupAnyName); |
12863 | 0 | R.suppressDiagnostics(); |
12864 | 0 | S.LookupName(R, S.getCurScope()); |
12865 | |
|
12866 | 0 | if (R.isSingleResult()) { |
12867 | 0 | FunctionDecl *FD = dyn_cast<FunctionDecl>(R.getFoundDecl()); |
12868 | 0 | if (FD && FD->getBuiltinID() == AbsKind) { |
12869 | 0 | EmitHeaderHint = false; |
12870 | 0 | } else { |
12871 | 0 | return; |
12872 | 0 | } |
12873 | 0 | } else if (!R.empty()) { |
12874 | 0 | return; |
12875 | 0 | } |
12876 | 0 | } |
12877 | 0 | } |
12878 | | |
12879 | 0 | S.Diag(Loc, diag::note_replace_abs_function) |
12880 | 0 | << FunctionName << FixItHint::CreateReplacement(Range, FunctionName); |
12881 | |
|
12882 | 0 | if (!HeaderName) |
12883 | 0 | return; |
12884 | | |
12885 | 0 | if (!EmitHeaderHint) |
12886 | 0 | return; |
12887 | | |
12888 | 0 | S.Diag(Loc, diag::note_include_header_or_declare) << HeaderName |
12889 | 0 | << FunctionName; |
12890 | 0 | } |
12891 | | |
12892 | | template <std::size_t StrLen> |
12893 | | static bool IsStdFunction(const FunctionDecl *FDecl, |
12894 | 0 | const char (&Str)[StrLen]) { |
12895 | 0 | if (!FDecl) |
12896 | 0 | return false; |
12897 | 0 | if (!FDecl->getIdentifier() || !FDecl->getIdentifier()->isStr(Str)) |
12898 | 0 | return false; |
12899 | 0 | if (!FDecl->isInStdNamespace()) |
12900 | 0 | return false; |
12901 | | |
12902 | 0 | return true; |
12903 | 0 | } |
12904 | | |
12905 | | // Warn when using the wrong abs() function. |
12906 | | void Sema::CheckAbsoluteValueFunction(const CallExpr *Call, |
12907 | 0 | const FunctionDecl *FDecl) { |
12908 | 0 | if (Call->getNumArgs() != 1) |
12909 | 0 | return; |
12910 | | |
12911 | 0 | unsigned AbsKind = getAbsoluteValueFunctionKind(FDecl); |
12912 | 0 | bool IsStdAbs = IsStdFunction(FDecl, "abs"); |
12913 | 0 | if (AbsKind == 0 && !IsStdAbs) |
12914 | 0 | return; |
12915 | | |
12916 | 0 | QualType ArgType = Call->getArg(0)->IgnoreParenImpCasts()->getType(); |
12917 | 0 | QualType ParamType = Call->getArg(0)->getType(); |
12918 | | |
12919 | | // Unsigned types cannot be negative. Suggest removing the absolute value |
12920 | | // function call. |
12921 | 0 | if (ArgType->isUnsignedIntegerType()) { |
12922 | 0 | StringRef FunctionName = |
12923 | 0 | IsStdAbs ? "std::abs" : Context.BuiltinInfo.getName(AbsKind); |
12924 | 0 | Diag(Call->getExprLoc(), diag::warn_unsigned_abs) << ArgType << ParamType; |
12925 | 0 | Diag(Call->getExprLoc(), diag::note_remove_abs) |
12926 | 0 | << FunctionName |
12927 | 0 | << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange()); |
12928 | 0 | return; |
12929 | 0 | } |
12930 | | |
12931 | | // Taking the absolute value of a pointer is very suspicious, they probably |
12932 | | // wanted to index into an array, dereference a pointer, call a function, etc. |
12933 | 0 | if (ArgType->isPointerType() || ArgType->canDecayToPointerType()) { |
12934 | 0 | unsigned DiagType = 0; |
12935 | 0 | if (ArgType->isFunctionType()) |
12936 | 0 | DiagType = 1; |
12937 | 0 | else if (ArgType->isArrayType()) |
12938 | 0 | DiagType = 2; |
12939 | |
|
12940 | 0 | Diag(Call->getExprLoc(), diag::warn_pointer_abs) << DiagType << ArgType; |
12941 | 0 | return; |
12942 | 0 | } |
12943 | | |
12944 | | // std::abs has overloads which prevent most of the absolute value problems |
12945 | | // from occurring. |
12946 | 0 | if (IsStdAbs) |
12947 | 0 | return; |
12948 | | |
12949 | 0 | AbsoluteValueKind ArgValueKind = getAbsoluteValueKind(ArgType); |
12950 | 0 | AbsoluteValueKind ParamValueKind = getAbsoluteValueKind(ParamType); |
12951 | | |
12952 | | // The argument and parameter are the same kind. Check if they are the right |
12953 | | // size. |
12954 | 0 | if (ArgValueKind == ParamValueKind) { |
12955 | 0 | if (Context.getTypeSize(ArgType) <= Context.getTypeSize(ParamType)) |
12956 | 0 | return; |
12957 | | |
12958 | 0 | unsigned NewAbsKind = getBestAbsFunction(Context, ArgType, AbsKind); |
12959 | 0 | Diag(Call->getExprLoc(), diag::warn_abs_too_small) |
12960 | 0 | << FDecl << ArgType << ParamType; |
12961 | |
|
12962 | 0 | if (NewAbsKind == 0) |
12963 | 0 | return; |
12964 | | |
12965 | 0 | emitReplacement(*this, Call->getExprLoc(), |
12966 | 0 | Call->getCallee()->getSourceRange(), NewAbsKind, ArgType); |
12967 | 0 | return; |
12968 | 0 | } |
12969 | | |
12970 | | // ArgValueKind != ParamValueKind |
12971 | | // The wrong type of absolute value function was used. Attempt to find the |
12972 | | // proper one. |
12973 | 0 | unsigned NewAbsKind = changeAbsFunction(AbsKind, ArgValueKind); |
12974 | 0 | NewAbsKind = getBestAbsFunction(Context, ArgType, NewAbsKind); |
12975 | 0 | if (NewAbsKind == 0) |
12976 | 0 | return; |
12977 | | |
12978 | 0 | Diag(Call->getExprLoc(), diag::warn_wrong_absolute_value_type) |
12979 | 0 | << FDecl << ParamValueKind << ArgValueKind; |
12980 | |
|
12981 | 0 | emitReplacement(*this, Call->getExprLoc(), |
12982 | 0 | Call->getCallee()->getSourceRange(), NewAbsKind, ArgType); |
12983 | 0 | } |
12984 | | |
12985 | | //===--- CHECK: Warn on use of std::max and unsigned zero. r---------------===// |
12986 | | void Sema::CheckMaxUnsignedZero(const CallExpr *Call, |
12987 | 0 | const FunctionDecl *FDecl) { |
12988 | 0 | if (!Call || !FDecl) return; |
12989 | | |
12990 | | // Ignore template specializations and macros. |
12991 | 0 | if (inTemplateInstantiation()) return; |
12992 | 0 | if (Call->getExprLoc().isMacroID()) return; |
12993 | | |
12994 | | // Only care about the one template argument, two function parameter std::max |
12995 | 0 | if (Call->getNumArgs() != 2) return; |
12996 | 0 | if (!IsStdFunction(FDecl, "max")) return; |
12997 | 0 | const auto * ArgList = FDecl->getTemplateSpecializationArgs(); |
12998 | 0 | if (!ArgList) return; |
12999 | 0 | if (ArgList->size() != 1) return; |
13000 | | |
13001 | | // Check that template type argument is unsigned integer. |
13002 | 0 | const auto& TA = ArgList->get(0); |
13003 | 0 | if (TA.getKind() != TemplateArgument::Type) return; |
13004 | 0 | QualType ArgType = TA.getAsType(); |
13005 | 0 | if (!ArgType->isUnsignedIntegerType()) return; |
13006 | | |
13007 | | // See if either argument is a literal zero. |
13008 | 0 | auto IsLiteralZeroArg = [](const Expr* E) -> bool { |
13009 | 0 | const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E); |
13010 | 0 | if (!MTE) return false; |
13011 | 0 | const auto *Num = dyn_cast<IntegerLiteral>(MTE->getSubExpr()); |
13012 | 0 | if (!Num) return false; |
13013 | 0 | if (Num->getValue() != 0) return false; |
13014 | 0 | return true; |
13015 | 0 | }; |
13016 | |
|
13017 | 0 | const Expr *FirstArg = Call->getArg(0); |
13018 | 0 | const Expr *SecondArg = Call->getArg(1); |
13019 | 0 | const bool IsFirstArgZero = IsLiteralZeroArg(FirstArg); |
13020 | 0 | const bool IsSecondArgZero = IsLiteralZeroArg(SecondArg); |
13021 | | |
13022 | | // Only warn when exactly one argument is zero. |
13023 | 0 | if (IsFirstArgZero == IsSecondArgZero) return; |
13024 | | |
13025 | 0 | SourceRange FirstRange = FirstArg->getSourceRange(); |
13026 | 0 | SourceRange SecondRange = SecondArg->getSourceRange(); |
13027 | |
|
13028 | 0 | SourceRange ZeroRange = IsFirstArgZero ? FirstRange : SecondRange; |
13029 | |
|
13030 | 0 | Diag(Call->getExprLoc(), diag::warn_max_unsigned_zero) |
13031 | 0 | << IsFirstArgZero << Call->getCallee()->getSourceRange() << ZeroRange; |
13032 | | |
13033 | | // Deduce what parts to remove so that "std::max(0u, foo)" becomes "(foo)". |
13034 | 0 | SourceRange RemovalRange; |
13035 | 0 | if (IsFirstArgZero) { |
13036 | 0 | RemovalRange = SourceRange(FirstRange.getBegin(), |
13037 | 0 | SecondRange.getBegin().getLocWithOffset(-1)); |
13038 | 0 | } else { |
13039 | 0 | RemovalRange = SourceRange(getLocForEndOfToken(FirstRange.getEnd()), |
13040 | 0 | SecondRange.getEnd()); |
13041 | 0 | } |
13042 | |
|
13043 | 0 | Diag(Call->getExprLoc(), diag::note_remove_max_call) |
13044 | 0 | << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange()) |
13045 | 0 | << FixItHint::CreateRemoval(RemovalRange); |
13046 | 0 | } |
13047 | | |
13048 | | //===--- CHECK: Standard memory functions ---------------------------------===// |
13049 | | |
13050 | | /// Takes the expression passed to the size_t parameter of functions |
13051 | | /// such as memcmp, strncat, etc and warns if it's a comparison. |
13052 | | /// |
13053 | | /// This is to catch typos like `if (memcmp(&a, &b, sizeof(a) > 0))`. |
13054 | | static bool CheckMemorySizeofForComparison(Sema &S, const Expr *E, |
13055 | | IdentifierInfo *FnName, |
13056 | | SourceLocation FnLoc, |
13057 | 0 | SourceLocation RParenLoc) { |
13058 | 0 | const BinaryOperator *Size = dyn_cast<BinaryOperator>(E); |
13059 | 0 | if (!Size) |
13060 | 0 | return false; |
13061 | | |
13062 | | // if E is binop and op is <=>, >, <, >=, <=, ==, &&, ||: |
13063 | 0 | if (!Size->isComparisonOp() && !Size->isLogicalOp()) |
13064 | 0 | return false; |
13065 | | |
13066 | 0 | SourceRange SizeRange = Size->getSourceRange(); |
13067 | 0 | S.Diag(Size->getOperatorLoc(), diag::warn_memsize_comparison) |
13068 | 0 | << SizeRange << FnName; |
13069 | 0 | S.Diag(FnLoc, diag::note_memsize_comparison_paren) |
13070 | 0 | << FnName |
13071 | 0 | << FixItHint::CreateInsertion( |
13072 | 0 | S.getLocForEndOfToken(Size->getLHS()->getEndLoc()), ")") |
13073 | 0 | << FixItHint::CreateRemoval(RParenLoc); |
13074 | 0 | S.Diag(SizeRange.getBegin(), diag::note_memsize_comparison_cast_silence) |
13075 | 0 | << FixItHint::CreateInsertion(SizeRange.getBegin(), "(size_t)(") |
13076 | 0 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(SizeRange.getEnd()), |
13077 | 0 | ")"); |
13078 | |
|
13079 | 0 | return true; |
13080 | 0 | } |
13081 | | |
13082 | | /// Determine whether the given type is or contains a dynamic class type |
13083 | | /// (e.g., whether it has a vtable). |
13084 | | static const CXXRecordDecl *getContainedDynamicClass(QualType T, |
13085 | 0 | bool &IsContained) { |
13086 | | // Look through array types while ignoring qualifiers. |
13087 | 0 | const Type *Ty = T->getBaseElementTypeUnsafe(); |
13088 | 0 | IsContained = false; |
13089 | |
|
13090 | 0 | const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
13091 | 0 | RD = RD ? RD->getDefinition() : nullptr; |
13092 | 0 | if (!RD || RD->isInvalidDecl()) |
13093 | 0 | return nullptr; |
13094 | | |
13095 | 0 | if (RD->isDynamicClass()) |
13096 | 0 | return RD; |
13097 | | |
13098 | | // Check all the fields. If any bases were dynamic, the class is dynamic. |
13099 | | // It's impossible for a class to transitively contain itself by value, so |
13100 | | // infinite recursion is impossible. |
13101 | 0 | for (auto *FD : RD->fields()) { |
13102 | 0 | bool SubContained; |
13103 | 0 | if (const CXXRecordDecl *ContainedRD = |
13104 | 0 | getContainedDynamicClass(FD->getType(), SubContained)) { |
13105 | 0 | IsContained = true; |
13106 | 0 | return ContainedRD; |
13107 | 0 | } |
13108 | 0 | } |
13109 | | |
13110 | 0 | return nullptr; |
13111 | 0 | } |
13112 | | |
13113 | 0 | static const UnaryExprOrTypeTraitExpr *getAsSizeOfExpr(const Expr *E) { |
13114 | 0 | if (const auto *Unary = dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
13115 | 0 | if (Unary->getKind() == UETT_SizeOf) |
13116 | 0 | return Unary; |
13117 | 0 | return nullptr; |
13118 | 0 | } |
13119 | | |
13120 | | /// If E is a sizeof expression, returns its argument expression, |
13121 | | /// otherwise returns NULL. |
13122 | 0 | static const Expr *getSizeOfExprArg(const Expr *E) { |
13123 | 0 | if (const UnaryExprOrTypeTraitExpr *SizeOf = getAsSizeOfExpr(E)) |
13124 | 0 | if (!SizeOf->isArgumentType()) |
13125 | 0 | return SizeOf->getArgumentExpr()->IgnoreParenImpCasts(); |
13126 | 0 | return nullptr; |
13127 | 0 | } |
13128 | | |
13129 | | /// If E is a sizeof expression, returns its argument type. |
13130 | 0 | static QualType getSizeOfArgType(const Expr *E) { |
13131 | 0 | if (const UnaryExprOrTypeTraitExpr *SizeOf = getAsSizeOfExpr(E)) |
13132 | 0 | return SizeOf->getTypeOfArgument(); |
13133 | 0 | return QualType(); |
13134 | 0 | } |
13135 | | |
13136 | | namespace { |
13137 | | |
13138 | | struct SearchNonTrivialToInitializeField |
13139 | | : DefaultInitializedTypeVisitor<SearchNonTrivialToInitializeField> { |
13140 | | using Super = |
13141 | | DefaultInitializedTypeVisitor<SearchNonTrivialToInitializeField>; |
13142 | | |
13143 | 0 | SearchNonTrivialToInitializeField(const Expr *E, Sema &S) : E(E), S(S) {} |
13144 | | |
13145 | | void visitWithKind(QualType::PrimitiveDefaultInitializeKind PDIK, QualType FT, |
13146 | 0 | SourceLocation SL) { |
13147 | 0 | if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) { |
13148 | 0 | asDerived().visitArray(PDIK, AT, SL); |
13149 | 0 | return; |
13150 | 0 | } |
13151 | | |
13152 | 0 | Super::visitWithKind(PDIK, FT, SL); |
13153 | 0 | } |
13154 | | |
13155 | 0 | void visitARCStrong(QualType FT, SourceLocation SL) { |
13156 | 0 | S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 1); |
13157 | 0 | } |
13158 | 0 | void visitARCWeak(QualType FT, SourceLocation SL) { |
13159 | 0 | S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 1); |
13160 | 0 | } |
13161 | 0 | void visitStruct(QualType FT, SourceLocation SL) { |
13162 | 0 | for (const FieldDecl *FD : FT->castAs<RecordType>()->getDecl()->fields()) |
13163 | 0 | visit(FD->getType(), FD->getLocation()); |
13164 | 0 | } |
13165 | | void visitArray(QualType::PrimitiveDefaultInitializeKind PDIK, |
13166 | 0 | const ArrayType *AT, SourceLocation SL) { |
13167 | 0 | visit(getContext().getBaseElementType(AT), SL); |
13168 | 0 | } |
13169 | 0 | void visitTrivial(QualType FT, SourceLocation SL) {} |
13170 | | |
13171 | 0 | static void diag(QualType RT, const Expr *E, Sema &S) { |
13172 | 0 | SearchNonTrivialToInitializeField(E, S).visitStruct(RT, SourceLocation()); |
13173 | 0 | } |
13174 | | |
13175 | 0 | ASTContext &getContext() { return S.getASTContext(); } |
13176 | | |
13177 | | const Expr *E; |
13178 | | Sema &S; |
13179 | | }; |
13180 | | |
13181 | | struct SearchNonTrivialToCopyField |
13182 | | : CopiedTypeVisitor<SearchNonTrivialToCopyField, false> { |
13183 | | using Super = CopiedTypeVisitor<SearchNonTrivialToCopyField, false>; |
13184 | | |
13185 | 0 | SearchNonTrivialToCopyField(const Expr *E, Sema &S) : E(E), S(S) {} |
13186 | | |
13187 | | void visitWithKind(QualType::PrimitiveCopyKind PCK, QualType FT, |
13188 | 0 | SourceLocation SL) { |
13189 | 0 | if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) { |
13190 | 0 | asDerived().visitArray(PCK, AT, SL); |
13191 | 0 | return; |
13192 | 0 | } |
13193 | | |
13194 | 0 | Super::visitWithKind(PCK, FT, SL); |
13195 | 0 | } |
13196 | | |
13197 | 0 | void visitARCStrong(QualType FT, SourceLocation SL) { |
13198 | 0 | S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 0); |
13199 | 0 | } |
13200 | 0 | void visitARCWeak(QualType FT, SourceLocation SL) { |
13201 | 0 | S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 0); |
13202 | 0 | } |
13203 | 0 | void visitStruct(QualType FT, SourceLocation SL) { |
13204 | 0 | for (const FieldDecl *FD : FT->castAs<RecordType>()->getDecl()->fields()) |
13205 | 0 | visit(FD->getType(), FD->getLocation()); |
13206 | 0 | } |
13207 | | void visitArray(QualType::PrimitiveCopyKind PCK, const ArrayType *AT, |
13208 | 0 | SourceLocation SL) { |
13209 | 0 | visit(getContext().getBaseElementType(AT), SL); |
13210 | 0 | } |
13211 | | void preVisit(QualType::PrimitiveCopyKind PCK, QualType FT, |
13212 | 0 | SourceLocation SL) {} |
13213 | 0 | void visitTrivial(QualType FT, SourceLocation SL) {} |
13214 | 0 | void visitVolatileTrivial(QualType FT, SourceLocation SL) {} |
13215 | | |
13216 | 0 | static void diag(QualType RT, const Expr *E, Sema &S) { |
13217 | 0 | SearchNonTrivialToCopyField(E, S).visitStruct(RT, SourceLocation()); |
13218 | 0 | } |
13219 | | |
13220 | 0 | ASTContext &getContext() { return S.getASTContext(); } |
13221 | | |
13222 | | const Expr *E; |
13223 | | Sema &S; |
13224 | | }; |
13225 | | |
13226 | | } |
13227 | | |
13228 | | /// Detect if \c SizeofExpr is likely to calculate the sizeof an object. |
13229 | 0 | static bool doesExprLikelyComputeSize(const Expr *SizeofExpr) { |
13230 | 0 | SizeofExpr = SizeofExpr->IgnoreParenImpCasts(); |
13231 | |
|
13232 | 0 | if (const auto *BO = dyn_cast<BinaryOperator>(SizeofExpr)) { |
13233 | 0 | if (BO->getOpcode() != BO_Mul && BO->getOpcode() != BO_Add) |
13234 | 0 | return false; |
13235 | | |
13236 | 0 | return doesExprLikelyComputeSize(BO->getLHS()) || |
13237 | 0 | doesExprLikelyComputeSize(BO->getRHS()); |
13238 | 0 | } |
13239 | | |
13240 | 0 | return getAsSizeOfExpr(SizeofExpr) != nullptr; |
13241 | 0 | } |
13242 | | |
13243 | | /// Check if the ArgLoc originated from a macro passed to the call at CallLoc. |
13244 | | /// |
13245 | | /// \code |
13246 | | /// #define MACRO 0 |
13247 | | /// foo(MACRO); |
13248 | | /// foo(0); |
13249 | | /// \endcode |
13250 | | /// |
13251 | | /// This should return true for the first call to foo, but not for the second |
13252 | | /// (regardless of whether foo is a macro or function). |
13253 | | static bool isArgumentExpandedFromMacro(SourceManager &SM, |
13254 | | SourceLocation CallLoc, |
13255 | 0 | SourceLocation ArgLoc) { |
13256 | 0 | if (!CallLoc.isMacroID()) |
13257 | 0 | return SM.getFileID(CallLoc) != SM.getFileID(ArgLoc); |
13258 | | |
13259 | 0 | return SM.getFileID(SM.getImmediateMacroCallerLoc(CallLoc)) != |
13260 | 0 | SM.getFileID(SM.getImmediateMacroCallerLoc(ArgLoc)); |
13261 | 0 | } |
13262 | | |
13263 | | /// Diagnose cases like 'memset(buf, sizeof(buf), 0)', which should have the |
13264 | | /// last two arguments transposed. |
13265 | 0 | static void CheckMemaccessSize(Sema &S, unsigned BId, const CallExpr *Call) { |
13266 | 0 | if (BId != Builtin::BImemset && BId != Builtin::BIbzero) |
13267 | 0 | return; |
13268 | | |
13269 | 0 | const Expr *SizeArg = |
13270 | 0 | Call->getArg(BId == Builtin::BImemset ? 2 : 1)->IgnoreImpCasts(); |
13271 | |
|
13272 | 0 | auto isLiteralZero = [](const Expr *E) { |
13273 | 0 | return (isa<IntegerLiteral>(E) && |
13274 | 0 | cast<IntegerLiteral>(E)->getValue() == 0) || |
13275 | 0 | (isa<CharacterLiteral>(E) && |
13276 | 0 | cast<CharacterLiteral>(E)->getValue() == 0); |
13277 | 0 | }; |
13278 | | |
13279 | | // If we're memsetting or bzeroing 0 bytes, then this is likely an error. |
13280 | 0 | SourceLocation CallLoc = Call->getRParenLoc(); |
13281 | 0 | SourceManager &SM = S.getSourceManager(); |
13282 | 0 | if (isLiteralZero(SizeArg) && |
13283 | 0 | !isArgumentExpandedFromMacro(SM, CallLoc, SizeArg->getExprLoc())) { |
13284 | |
|
13285 | 0 | SourceLocation DiagLoc = SizeArg->getExprLoc(); |
13286 | | |
13287 | | // Some platforms #define bzero to __builtin_memset. See if this is the |
13288 | | // case, and if so, emit a better diagnostic. |
13289 | 0 | if (BId == Builtin::BIbzero || |
13290 | 0 | (CallLoc.isMacroID() && Lexer::getImmediateMacroName( |
13291 | 0 | CallLoc, SM, S.getLangOpts()) == "bzero")) { |
13292 | 0 | S.Diag(DiagLoc, diag::warn_suspicious_bzero_size); |
13293 | 0 | S.Diag(DiagLoc, diag::note_suspicious_bzero_size_silence); |
13294 | 0 | } else if (!isLiteralZero(Call->getArg(1)->IgnoreImpCasts())) { |
13295 | 0 | S.Diag(DiagLoc, diag::warn_suspicious_sizeof_memset) << 0; |
13296 | 0 | S.Diag(DiagLoc, diag::note_suspicious_sizeof_memset_silence) << 0; |
13297 | 0 | } |
13298 | 0 | return; |
13299 | 0 | } |
13300 | | |
13301 | | // If the second argument to a memset is a sizeof expression and the third |
13302 | | // isn't, this is also likely an error. This should catch |
13303 | | // 'memset(buf, sizeof(buf), 0xff)'. |
13304 | 0 | if (BId == Builtin::BImemset && |
13305 | 0 | doesExprLikelyComputeSize(Call->getArg(1)) && |
13306 | 0 | !doesExprLikelyComputeSize(Call->getArg(2))) { |
13307 | 0 | SourceLocation DiagLoc = Call->getArg(1)->getExprLoc(); |
13308 | 0 | S.Diag(DiagLoc, diag::warn_suspicious_sizeof_memset) << 1; |
13309 | 0 | S.Diag(DiagLoc, diag::note_suspicious_sizeof_memset_silence) << 1; |
13310 | 0 | return; |
13311 | 0 | } |
13312 | 0 | } |
13313 | | |
13314 | | /// Check for dangerous or invalid arguments to memset(). |
13315 | | /// |
13316 | | /// This issues warnings on known problematic, dangerous or unspecified |
13317 | | /// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp' |
13318 | | /// function calls. |
13319 | | /// |
13320 | | /// \param Call The call expression to diagnose. |
13321 | | void Sema::CheckMemaccessArguments(const CallExpr *Call, |
13322 | | unsigned BId, |
13323 | 0 | IdentifierInfo *FnName) { |
13324 | 0 | assert(BId != 0); |
13325 | | |
13326 | | // It is possible to have a non-standard definition of memset. Validate |
13327 | | // we have enough arguments, and if not, abort further checking. |
13328 | 0 | unsigned ExpectedNumArgs = |
13329 | 0 | (BId == Builtin::BIstrndup || BId == Builtin::BIbzero ? 2 : 3); |
13330 | 0 | if (Call->getNumArgs() < ExpectedNumArgs) |
13331 | 0 | return; |
13332 | | |
13333 | 0 | unsigned LastArg = (BId == Builtin::BImemset || BId == Builtin::BIbzero || |
13334 | 0 | BId == Builtin::BIstrndup ? 1 : 2); |
13335 | 0 | unsigned LenArg = |
13336 | 0 | (BId == Builtin::BIbzero || BId == Builtin::BIstrndup ? 1 : 2); |
13337 | 0 | const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts(); |
13338 | |
|
13339 | 0 | if (CheckMemorySizeofForComparison(*this, LenExpr, FnName, |
13340 | 0 | Call->getBeginLoc(), Call->getRParenLoc())) |
13341 | 0 | return; |
13342 | | |
13343 | | // Catch cases like 'memset(buf, sizeof(buf), 0)'. |
13344 | 0 | CheckMemaccessSize(*this, BId, Call); |
13345 | | |
13346 | | // We have special checking when the length is a sizeof expression. |
13347 | 0 | QualType SizeOfArgTy = getSizeOfArgType(LenExpr); |
13348 | 0 | const Expr *SizeOfArg = getSizeOfExprArg(LenExpr); |
13349 | 0 | llvm::FoldingSetNodeID SizeOfArgID; |
13350 | | |
13351 | | // Although widely used, 'bzero' is not a standard function. Be more strict |
13352 | | // with the argument types before allowing diagnostics and only allow the |
13353 | | // form bzero(ptr, sizeof(...)). |
13354 | 0 | QualType FirstArgTy = Call->getArg(0)->IgnoreParenImpCasts()->getType(); |
13355 | 0 | if (BId == Builtin::BIbzero && !FirstArgTy->getAs<PointerType>()) |
13356 | 0 | return; |
13357 | | |
13358 | 0 | for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) { |
13359 | 0 | const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts(); |
13360 | 0 | SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange(); |
13361 | |
|
13362 | 0 | QualType DestTy = Dest->getType(); |
13363 | 0 | QualType PointeeTy; |
13364 | 0 | if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) { |
13365 | 0 | PointeeTy = DestPtrTy->getPointeeType(); |
13366 | | |
13367 | | // Never warn about void type pointers. This can be used to suppress |
13368 | | // false positives. |
13369 | 0 | if (PointeeTy->isVoidType()) |
13370 | 0 | continue; |
13371 | | |
13372 | | // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by |
13373 | | // actually comparing the expressions for equality. Because computing the |
13374 | | // expression IDs can be expensive, we only do this if the diagnostic is |
13375 | | // enabled. |
13376 | 0 | if (SizeOfArg && |
13377 | 0 | !Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, |
13378 | 0 | SizeOfArg->getExprLoc())) { |
13379 | | // We only compute IDs for expressions if the warning is enabled, and |
13380 | | // cache the sizeof arg's ID. |
13381 | 0 | if (SizeOfArgID == llvm::FoldingSetNodeID()) |
13382 | 0 | SizeOfArg->Profile(SizeOfArgID, Context, true); |
13383 | 0 | llvm::FoldingSetNodeID DestID; |
13384 | 0 | Dest->Profile(DestID, Context, true); |
13385 | 0 | if (DestID == SizeOfArgID) { |
13386 | | // TODO: For strncpy() and friends, this could suggest sizeof(dst) |
13387 | | // over sizeof(src) as well. |
13388 | 0 | unsigned ActionIdx = 0; // Default is to suggest dereferencing. |
13389 | 0 | StringRef ReadableName = FnName->getName(); |
13390 | |
|
13391 | 0 | if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest)) |
13392 | 0 | if (UnaryOp->getOpcode() == UO_AddrOf) |
13393 | 0 | ActionIdx = 1; // If its an address-of operator, just remove it. |
13394 | 0 | if (!PointeeTy->isIncompleteType() && |
13395 | 0 | (Context.getTypeSize(PointeeTy) == Context.getCharWidth())) |
13396 | 0 | ActionIdx = 2; // If the pointee's size is sizeof(char), |
13397 | | // suggest an explicit length. |
13398 | | |
13399 | | // If the function is defined as a builtin macro, do not show macro |
13400 | | // expansion. |
13401 | 0 | SourceLocation SL = SizeOfArg->getExprLoc(); |
13402 | 0 | SourceRange DSR = Dest->getSourceRange(); |
13403 | 0 | SourceRange SSR = SizeOfArg->getSourceRange(); |
13404 | 0 | SourceManager &SM = getSourceManager(); |
13405 | |
|
13406 | 0 | if (SM.isMacroArgExpansion(SL)) { |
13407 | 0 | ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts); |
13408 | 0 | SL = SM.getSpellingLoc(SL); |
13409 | 0 | DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()), |
13410 | 0 | SM.getSpellingLoc(DSR.getEnd())); |
13411 | 0 | SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()), |
13412 | 0 | SM.getSpellingLoc(SSR.getEnd())); |
13413 | 0 | } |
13414 | |
|
13415 | 0 | DiagRuntimeBehavior(SL, SizeOfArg, |
13416 | 0 | PDiag(diag::warn_sizeof_pointer_expr_memaccess) |
13417 | 0 | << ReadableName |
13418 | 0 | << PointeeTy |
13419 | 0 | << DestTy |
13420 | 0 | << DSR |
13421 | 0 | << SSR); |
13422 | 0 | DiagRuntimeBehavior(SL, SizeOfArg, |
13423 | 0 | PDiag(diag::warn_sizeof_pointer_expr_memaccess_note) |
13424 | 0 | << ActionIdx |
13425 | 0 | << SSR); |
13426 | |
|
13427 | 0 | break; |
13428 | 0 | } |
13429 | 0 | } |
13430 | | |
13431 | | // Also check for cases where the sizeof argument is the exact same |
13432 | | // type as the memory argument, and where it points to a user-defined |
13433 | | // record type. |
13434 | 0 | if (SizeOfArgTy != QualType()) { |
13435 | 0 | if (PointeeTy->isRecordType() && |
13436 | 0 | Context.typesAreCompatible(SizeOfArgTy, DestTy)) { |
13437 | 0 | DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest, |
13438 | 0 | PDiag(diag::warn_sizeof_pointer_type_memaccess) |
13439 | 0 | << FnName << SizeOfArgTy << ArgIdx |
13440 | 0 | << PointeeTy << Dest->getSourceRange() |
13441 | 0 | << LenExpr->getSourceRange()); |
13442 | 0 | break; |
13443 | 0 | } |
13444 | 0 | } |
13445 | 0 | } else if (DestTy->isArrayType()) { |
13446 | 0 | PointeeTy = DestTy; |
13447 | 0 | } |
13448 | | |
13449 | 0 | if (PointeeTy == QualType()) |
13450 | 0 | continue; |
13451 | | |
13452 | | // Always complain about dynamic classes. |
13453 | 0 | bool IsContained; |
13454 | 0 | if (const CXXRecordDecl *ContainedRD = |
13455 | 0 | getContainedDynamicClass(PointeeTy, IsContained)) { |
13456 | |
|
13457 | 0 | unsigned OperationType = 0; |
13458 | 0 | const bool IsCmp = BId == Builtin::BImemcmp || BId == Builtin::BIbcmp; |
13459 | | // "overwritten" if we're warning about the destination for any call |
13460 | | // but memcmp; otherwise a verb appropriate to the call. |
13461 | 0 | if (ArgIdx != 0 || IsCmp) { |
13462 | 0 | if (BId == Builtin::BImemcpy) |
13463 | 0 | OperationType = 1; |
13464 | 0 | else if(BId == Builtin::BImemmove) |
13465 | 0 | OperationType = 2; |
13466 | 0 | else if (IsCmp) |
13467 | 0 | OperationType = 3; |
13468 | 0 | } |
13469 | |
|
13470 | 0 | DiagRuntimeBehavior(Dest->getExprLoc(), Dest, |
13471 | 0 | PDiag(diag::warn_dyn_class_memaccess) |
13472 | 0 | << (IsCmp ? ArgIdx + 2 : ArgIdx) << FnName |
13473 | 0 | << IsContained << ContainedRD << OperationType |
13474 | 0 | << Call->getCallee()->getSourceRange()); |
13475 | 0 | } else if (PointeeTy.hasNonTrivialObjCLifetime() && |
13476 | 0 | BId != Builtin::BImemset) |
13477 | 0 | DiagRuntimeBehavior( |
13478 | 0 | Dest->getExprLoc(), Dest, |
13479 | 0 | PDiag(diag::warn_arc_object_memaccess) |
13480 | 0 | << ArgIdx << FnName << PointeeTy |
13481 | 0 | << Call->getCallee()->getSourceRange()); |
13482 | 0 | else if (const auto *RT = PointeeTy->getAs<RecordType>()) { |
13483 | 0 | if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) && |
13484 | 0 | RT->getDecl()->isNonTrivialToPrimitiveDefaultInitialize()) { |
13485 | 0 | DiagRuntimeBehavior(Dest->getExprLoc(), Dest, |
13486 | 0 | PDiag(diag::warn_cstruct_memaccess) |
13487 | 0 | << ArgIdx << FnName << PointeeTy << 0); |
13488 | 0 | SearchNonTrivialToInitializeField::diag(PointeeTy, Dest, *this); |
13489 | 0 | } else if ((BId == Builtin::BImemcpy || BId == Builtin::BImemmove) && |
13490 | 0 | RT->getDecl()->isNonTrivialToPrimitiveCopy()) { |
13491 | 0 | DiagRuntimeBehavior(Dest->getExprLoc(), Dest, |
13492 | 0 | PDiag(diag::warn_cstruct_memaccess) |
13493 | 0 | << ArgIdx << FnName << PointeeTy << 1); |
13494 | 0 | SearchNonTrivialToCopyField::diag(PointeeTy, Dest, *this); |
13495 | 0 | } else { |
13496 | 0 | continue; |
13497 | 0 | } |
13498 | 0 | } else |
13499 | 0 | continue; |
13500 | | |
13501 | 0 | DiagRuntimeBehavior( |
13502 | 0 | Dest->getExprLoc(), Dest, |
13503 | 0 | PDiag(diag::note_bad_memaccess_silence) |
13504 | 0 | << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)")); |
13505 | 0 | break; |
13506 | 0 | } |
13507 | 0 | } |
13508 | | |
13509 | | // A little helper routine: ignore addition and subtraction of integer literals. |
13510 | | // This intentionally does not ignore all integer constant expressions because |
13511 | | // we don't want to remove sizeof(). |
13512 | 0 | static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) { |
13513 | 0 | Ex = Ex->IgnoreParenCasts(); |
13514 | |
|
13515 | 0 | while (true) { |
13516 | 0 | const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex); |
13517 | 0 | if (!BO || !BO->isAdditiveOp()) |
13518 | 0 | break; |
13519 | | |
13520 | 0 | const Expr *RHS = BO->getRHS()->IgnoreParenCasts(); |
13521 | 0 | const Expr *LHS = BO->getLHS()->IgnoreParenCasts(); |
13522 | |
|
13523 | 0 | if (isa<IntegerLiteral>(RHS)) |
13524 | 0 | Ex = LHS; |
13525 | 0 | else if (isa<IntegerLiteral>(LHS)) |
13526 | 0 | Ex = RHS; |
13527 | 0 | else |
13528 | 0 | break; |
13529 | 0 | } |
13530 | |
|
13531 | 0 | return Ex; |
13532 | 0 | } |
13533 | | |
13534 | | static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty, |
13535 | 0 | ASTContext &Context) { |
13536 | | // Only handle constant-sized or VLAs, but not flexible members. |
13537 | 0 | if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) { |
13538 | | // Only issue the FIXIT for arrays of size > 1. |
13539 | 0 | if (CAT->getSize().getSExtValue() <= 1) |
13540 | 0 | return false; |
13541 | 0 | } else if (!Ty->isVariableArrayType()) { |
13542 | 0 | return false; |
13543 | 0 | } |
13544 | 0 | return true; |
13545 | 0 | } |
13546 | | |
13547 | | // Warn if the user has made the 'size' argument to strlcpy or strlcat |
13548 | | // be the size of the source, instead of the destination. |
13549 | | void Sema::CheckStrlcpycatArguments(const CallExpr *Call, |
13550 | 0 | IdentifierInfo *FnName) { |
13551 | | |
13552 | | // Don't crash if the user has the wrong number of arguments |
13553 | 0 | unsigned NumArgs = Call->getNumArgs(); |
13554 | 0 | if ((NumArgs != 3) && (NumArgs != 4)) |
13555 | 0 | return; |
13556 | | |
13557 | 0 | const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context); |
13558 | 0 | const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context); |
13559 | 0 | const Expr *CompareWithSrc = nullptr; |
13560 | |
|
13561 | 0 | if (CheckMemorySizeofForComparison(*this, SizeArg, FnName, |
13562 | 0 | Call->getBeginLoc(), Call->getRParenLoc())) |
13563 | 0 | return; |
13564 | | |
13565 | | // Look for 'strlcpy(dst, x, sizeof(x))' |
13566 | 0 | if (const Expr *Ex = getSizeOfExprArg(SizeArg)) |
13567 | 0 | CompareWithSrc = Ex; |
13568 | 0 | else { |
13569 | | // Look for 'strlcpy(dst, x, strlen(x))' |
13570 | 0 | if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) { |
13571 | 0 | if (SizeCall->getBuiltinCallee() == Builtin::BIstrlen && |
13572 | 0 | SizeCall->getNumArgs() == 1) |
13573 | 0 | CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context); |
13574 | 0 | } |
13575 | 0 | } |
13576 | |
|
13577 | 0 | if (!CompareWithSrc) |
13578 | 0 | return; |
13579 | | |
13580 | | // Determine if the argument to sizeof/strlen is equal to the source |
13581 | | // argument. In principle there's all kinds of things you could do |
13582 | | // here, for instance creating an == expression and evaluating it with |
13583 | | // EvaluateAsBooleanCondition, but this uses a more direct technique: |
13584 | 0 | const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg); |
13585 | 0 | if (!SrcArgDRE) |
13586 | 0 | return; |
13587 | | |
13588 | 0 | const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc); |
13589 | 0 | if (!CompareWithSrcDRE || |
13590 | 0 | SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl()) |
13591 | 0 | return; |
13592 | | |
13593 | 0 | const Expr *OriginalSizeArg = Call->getArg(2); |
13594 | 0 | Diag(CompareWithSrcDRE->getBeginLoc(), diag::warn_strlcpycat_wrong_size) |
13595 | 0 | << OriginalSizeArg->getSourceRange() << FnName; |
13596 | | |
13597 | | // Output a FIXIT hint if the destination is an array (rather than a |
13598 | | // pointer to an array). This could be enhanced to handle some |
13599 | | // pointers if we know the actual size, like if DstArg is 'array+2' |
13600 | | // we could say 'sizeof(array)-2'. |
13601 | 0 | const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts(); |
13602 | 0 | if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context)) |
13603 | 0 | return; |
13604 | | |
13605 | 0 | SmallString<128> sizeString; |
13606 | 0 | llvm::raw_svector_ostream OS(sizeString); |
13607 | 0 | OS << "sizeof("; |
13608 | 0 | DstArg->printPretty(OS, nullptr, getPrintingPolicy()); |
13609 | 0 | OS << ")"; |
13610 | |
|
13611 | 0 | Diag(OriginalSizeArg->getBeginLoc(), diag::note_strlcpycat_wrong_size) |
13612 | 0 | << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(), |
13613 | 0 | OS.str()); |
13614 | 0 | } |
13615 | | |
13616 | | /// Check if two expressions refer to the same declaration. |
13617 | 0 | static bool referToTheSameDecl(const Expr *E1, const Expr *E2) { |
13618 | 0 | if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1)) |
13619 | 0 | if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2)) |
13620 | 0 | return D1->getDecl() == D2->getDecl(); |
13621 | 0 | return false; |
13622 | 0 | } |
13623 | | |
13624 | 0 | static const Expr *getStrlenExprArg(const Expr *E) { |
13625 | 0 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { |
13626 | 0 | const FunctionDecl *FD = CE->getDirectCallee(); |
13627 | 0 | if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen) |
13628 | 0 | return nullptr; |
13629 | 0 | return CE->getArg(0)->IgnoreParenCasts(); |
13630 | 0 | } |
13631 | 0 | return nullptr; |
13632 | 0 | } |
13633 | | |
13634 | | // Warn on anti-patterns as the 'size' argument to strncat. |
13635 | | // The correct size argument should look like following: |
13636 | | // strncat(dst, src, sizeof(dst) - strlen(dest) - 1); |
13637 | | void Sema::CheckStrncatArguments(const CallExpr *CE, |
13638 | 0 | IdentifierInfo *FnName) { |
13639 | | // Don't crash if the user has the wrong number of arguments. |
13640 | 0 | if (CE->getNumArgs() < 3) |
13641 | 0 | return; |
13642 | 0 | const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts(); |
13643 | 0 | const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts(); |
13644 | 0 | const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts(); |
13645 | |
|
13646 | 0 | if (CheckMemorySizeofForComparison(*this, LenArg, FnName, CE->getBeginLoc(), |
13647 | 0 | CE->getRParenLoc())) |
13648 | 0 | return; |
13649 | | |
13650 | | // Identify common expressions, which are wrongly used as the size argument |
13651 | | // to strncat and may lead to buffer overflows. |
13652 | 0 | unsigned PatternType = 0; |
13653 | 0 | if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) { |
13654 | | // - sizeof(dst) |
13655 | 0 | if (referToTheSameDecl(SizeOfArg, DstArg)) |
13656 | 0 | PatternType = 1; |
13657 | | // - sizeof(src) |
13658 | 0 | else if (referToTheSameDecl(SizeOfArg, SrcArg)) |
13659 | 0 | PatternType = 2; |
13660 | 0 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) { |
13661 | 0 | if (BE->getOpcode() == BO_Sub) { |
13662 | 0 | const Expr *L = BE->getLHS()->IgnoreParenCasts(); |
13663 | 0 | const Expr *R = BE->getRHS()->IgnoreParenCasts(); |
13664 | | // - sizeof(dst) - strlen(dst) |
13665 | 0 | if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) && |
13666 | 0 | referToTheSameDecl(DstArg, getStrlenExprArg(R))) |
13667 | 0 | PatternType = 1; |
13668 | | // - sizeof(src) - (anything) |
13669 | 0 | else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L))) |
13670 | 0 | PatternType = 2; |
13671 | 0 | } |
13672 | 0 | } |
13673 | |
|
13674 | 0 | if (PatternType == 0) |
13675 | 0 | return; |
13676 | | |
13677 | | // Generate the diagnostic. |
13678 | 0 | SourceLocation SL = LenArg->getBeginLoc(); |
13679 | 0 | SourceRange SR = LenArg->getSourceRange(); |
13680 | 0 | SourceManager &SM = getSourceManager(); |
13681 | | |
13682 | | // If the function is defined as a builtin macro, do not show macro expansion. |
13683 | 0 | if (SM.isMacroArgExpansion(SL)) { |
13684 | 0 | SL = SM.getSpellingLoc(SL); |
13685 | 0 | SR = SourceRange(SM.getSpellingLoc(SR.getBegin()), |
13686 | 0 | SM.getSpellingLoc(SR.getEnd())); |
13687 | 0 | } |
13688 | | |
13689 | | // Check if the destination is an array (rather than a pointer to an array). |
13690 | 0 | QualType DstTy = DstArg->getType(); |
13691 | 0 | bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy, |
13692 | 0 | Context); |
13693 | 0 | if (!isKnownSizeArray) { |
13694 | 0 | if (PatternType == 1) |
13695 | 0 | Diag(SL, diag::warn_strncat_wrong_size) << SR; |
13696 | 0 | else |
13697 | 0 | Diag(SL, diag::warn_strncat_src_size) << SR; |
13698 | 0 | return; |
13699 | 0 | } |
13700 | | |
13701 | 0 | if (PatternType == 1) |
13702 | 0 | Diag(SL, diag::warn_strncat_large_size) << SR; |
13703 | 0 | else |
13704 | 0 | Diag(SL, diag::warn_strncat_src_size) << SR; |
13705 | |
|
13706 | 0 | SmallString<128> sizeString; |
13707 | 0 | llvm::raw_svector_ostream OS(sizeString); |
13708 | 0 | OS << "sizeof("; |
13709 | 0 | DstArg->printPretty(OS, nullptr, getPrintingPolicy()); |
13710 | 0 | OS << ") - "; |
13711 | 0 | OS << "strlen("; |
13712 | 0 | DstArg->printPretty(OS, nullptr, getPrintingPolicy()); |
13713 | 0 | OS << ") - 1"; |
13714 | |
|
13715 | 0 | Diag(SL, diag::note_strncat_wrong_size) |
13716 | 0 | << FixItHint::CreateReplacement(SR, OS.str()); |
13717 | 0 | } |
13718 | | |
13719 | | namespace { |
13720 | | void CheckFreeArgumentsOnLvalue(Sema &S, const std::string &CalleeName, |
13721 | 0 | const UnaryOperator *UnaryExpr, const Decl *D) { |
13722 | 0 | if (isa<FieldDecl, FunctionDecl, VarDecl>(D)) { |
13723 | 0 | S.Diag(UnaryExpr->getBeginLoc(), diag::warn_free_nonheap_object) |
13724 | 0 | << CalleeName << 0 /*object: */ << cast<NamedDecl>(D); |
13725 | 0 | return; |
13726 | 0 | } |
13727 | 0 | } |
13728 | | |
13729 | | void CheckFreeArgumentsAddressof(Sema &S, const std::string &CalleeName, |
13730 | 0 | const UnaryOperator *UnaryExpr) { |
13731 | 0 | if (const auto *Lvalue = dyn_cast<DeclRefExpr>(UnaryExpr->getSubExpr())) { |
13732 | 0 | const Decl *D = Lvalue->getDecl(); |
13733 | 0 | if (isa<DeclaratorDecl>(D)) |
13734 | 0 | if (!dyn_cast<DeclaratorDecl>(D)->getType()->isReferenceType()) |
13735 | 0 | return CheckFreeArgumentsOnLvalue(S, CalleeName, UnaryExpr, D); |
13736 | 0 | } |
13737 | | |
13738 | 0 | if (const auto *Lvalue = dyn_cast<MemberExpr>(UnaryExpr->getSubExpr())) |
13739 | 0 | return CheckFreeArgumentsOnLvalue(S, CalleeName, UnaryExpr, |
13740 | 0 | Lvalue->getMemberDecl()); |
13741 | 0 | } |
13742 | | |
13743 | | void CheckFreeArgumentsPlus(Sema &S, const std::string &CalleeName, |
13744 | 0 | const UnaryOperator *UnaryExpr) { |
13745 | 0 | const auto *Lambda = dyn_cast<LambdaExpr>( |
13746 | 0 | UnaryExpr->getSubExpr()->IgnoreImplicitAsWritten()->IgnoreParens()); |
13747 | 0 | if (!Lambda) |
13748 | 0 | return; |
13749 | | |
13750 | 0 | S.Diag(Lambda->getBeginLoc(), diag::warn_free_nonheap_object) |
13751 | 0 | << CalleeName << 2 /*object: lambda expression*/; |
13752 | 0 | } |
13753 | | |
13754 | | void CheckFreeArgumentsStackArray(Sema &S, const std::string &CalleeName, |
13755 | 0 | const DeclRefExpr *Lvalue) { |
13756 | 0 | const auto *Var = dyn_cast<VarDecl>(Lvalue->getDecl()); |
13757 | 0 | if (Var == nullptr) |
13758 | 0 | return; |
13759 | | |
13760 | 0 | S.Diag(Lvalue->getBeginLoc(), diag::warn_free_nonheap_object) |
13761 | 0 | << CalleeName << 0 /*object: */ << Var; |
13762 | 0 | } |
13763 | | |
13764 | | void CheckFreeArgumentsCast(Sema &S, const std::string &CalleeName, |
13765 | 0 | const CastExpr *Cast) { |
13766 | 0 | SmallString<128> SizeString; |
13767 | 0 | llvm::raw_svector_ostream OS(SizeString); |
13768 | |
|
13769 | 0 | clang::CastKind Kind = Cast->getCastKind(); |
13770 | 0 | if (Kind == clang::CK_BitCast && |
13771 | 0 | !Cast->getSubExpr()->getType()->isFunctionPointerType()) |
13772 | 0 | return; |
13773 | 0 | if (Kind == clang::CK_IntegralToPointer && |
13774 | 0 | !isa<IntegerLiteral>( |
13775 | 0 | Cast->getSubExpr()->IgnoreParenImpCasts()->IgnoreParens())) |
13776 | 0 | return; |
13777 | | |
13778 | 0 | switch (Cast->getCastKind()) { |
13779 | 0 | case clang::CK_BitCast: |
13780 | 0 | case clang::CK_IntegralToPointer: |
13781 | 0 | case clang::CK_FunctionToPointerDecay: |
13782 | 0 | OS << '\''; |
13783 | 0 | Cast->printPretty(OS, nullptr, S.getPrintingPolicy()); |
13784 | 0 | OS << '\''; |
13785 | 0 | break; |
13786 | 0 | default: |
13787 | 0 | return; |
13788 | 0 | } |
13789 | | |
13790 | 0 | S.Diag(Cast->getBeginLoc(), diag::warn_free_nonheap_object) |
13791 | 0 | << CalleeName << 0 /*object: */ << OS.str(); |
13792 | 0 | } |
13793 | | } // namespace |
13794 | | |
13795 | | /// Alerts the user that they are attempting to free a non-malloc'd object. |
13796 | 0 | void Sema::CheckFreeArguments(const CallExpr *E) { |
13797 | 0 | const std::string CalleeName = |
13798 | 0 | cast<FunctionDecl>(E->getCalleeDecl())->getQualifiedNameAsString(); |
13799 | |
|
13800 | 0 | { // Prefer something that doesn't involve a cast to make things simpler. |
13801 | 0 | const Expr *Arg = E->getArg(0)->IgnoreParenCasts(); |
13802 | 0 | if (const auto *UnaryExpr = dyn_cast<UnaryOperator>(Arg)) |
13803 | 0 | switch (UnaryExpr->getOpcode()) { |
13804 | 0 | case UnaryOperator::Opcode::UO_AddrOf: |
13805 | 0 | return CheckFreeArgumentsAddressof(*this, CalleeName, UnaryExpr); |
13806 | 0 | case UnaryOperator::Opcode::UO_Plus: |
13807 | 0 | return CheckFreeArgumentsPlus(*this, CalleeName, UnaryExpr); |
13808 | 0 | default: |
13809 | 0 | break; |
13810 | 0 | } |
13811 | | |
13812 | 0 | if (const auto *Lvalue = dyn_cast<DeclRefExpr>(Arg)) |
13813 | 0 | if (Lvalue->getType()->isArrayType()) |
13814 | 0 | return CheckFreeArgumentsStackArray(*this, CalleeName, Lvalue); |
13815 | | |
13816 | 0 | if (const auto *Label = dyn_cast<AddrLabelExpr>(Arg)) { |
13817 | 0 | Diag(Label->getBeginLoc(), diag::warn_free_nonheap_object) |
13818 | 0 | << CalleeName << 0 /*object: */ << Label->getLabel()->getIdentifier(); |
13819 | 0 | return; |
13820 | 0 | } |
13821 | | |
13822 | 0 | if (isa<BlockExpr>(Arg)) { |
13823 | 0 | Diag(Arg->getBeginLoc(), diag::warn_free_nonheap_object) |
13824 | 0 | << CalleeName << 1 /*object: block*/; |
13825 | 0 | return; |
13826 | 0 | } |
13827 | 0 | } |
13828 | | // Maybe the cast was important, check after the other cases. |
13829 | 0 | if (const auto *Cast = dyn_cast<CastExpr>(E->getArg(0))) |
13830 | 0 | return CheckFreeArgumentsCast(*this, CalleeName, Cast); |
13831 | 0 | } |
13832 | | |
13833 | | void |
13834 | | Sema::CheckReturnValExpr(Expr *RetValExp, QualType lhsType, |
13835 | | SourceLocation ReturnLoc, |
13836 | | bool isObjCMethod, |
13837 | | const AttrVec *Attrs, |
13838 | 0 | const FunctionDecl *FD) { |
13839 | | // Check if the return value is null but should not be. |
13840 | 0 | if (((Attrs && hasSpecificAttr<ReturnsNonNullAttr>(*Attrs)) || |
13841 | 0 | (!isObjCMethod && isNonNullType(lhsType))) && |
13842 | 0 | CheckNonNullExpr(*this, RetValExp)) |
13843 | 0 | Diag(ReturnLoc, diag::warn_null_ret) |
13844 | 0 | << (isObjCMethod ? 1 : 0) << RetValExp->getSourceRange(); |
13845 | | |
13846 | | // C++11 [basic.stc.dynamic.allocation]p4: |
13847 | | // If an allocation function declared with a non-throwing |
13848 | | // exception-specification fails to allocate storage, it shall return |
13849 | | // a null pointer. Any other allocation function that fails to allocate |
13850 | | // storage shall indicate failure only by throwing an exception [...] |
13851 | 0 | if (FD) { |
13852 | 0 | OverloadedOperatorKind Op = FD->getOverloadedOperator(); |
13853 | 0 | if (Op == OO_New || Op == OO_Array_New) { |
13854 | 0 | const FunctionProtoType *Proto |
13855 | 0 | = FD->getType()->castAs<FunctionProtoType>(); |
13856 | 0 | if (!Proto->isNothrow(/*ResultIfDependent*/true) && |
13857 | 0 | CheckNonNullExpr(*this, RetValExp)) |
13858 | 0 | Diag(ReturnLoc, diag::warn_operator_new_returns_null) |
13859 | 0 | << FD << getLangOpts().CPlusPlus11; |
13860 | 0 | } |
13861 | 0 | } |
13862 | |
|
13863 | 0 | if (RetValExp && RetValExp->getType()->isWebAssemblyTableType()) { |
13864 | 0 | Diag(ReturnLoc, diag::err_wasm_table_art) << 1; |
13865 | 0 | } |
13866 | | |
13867 | | // PPC MMA non-pointer types are not allowed as return type. Checking the type |
13868 | | // here prevent the user from using a PPC MMA type as trailing return type. |
13869 | 0 | if (Context.getTargetInfo().getTriple().isPPC64()) |
13870 | 0 | CheckPPCMMAType(RetValExp->getType(), ReturnLoc); |
13871 | 0 | } |
13872 | | |
13873 | | /// Check for comparisons of floating-point values using == and !=. Issue a |
13874 | | /// warning if the comparison is not likely to do what the programmer intended. |
13875 | | void Sema::CheckFloatComparison(SourceLocation Loc, Expr *LHS, Expr *RHS, |
13876 | 0 | BinaryOperatorKind Opcode) { |
13877 | 0 | if (!BinaryOperator::isEqualityOp(Opcode)) |
13878 | 0 | return; |
13879 | | |
13880 | | // Match and capture subexpressions such as "(float) X == 0.1". |
13881 | 0 | FloatingLiteral *FPLiteral; |
13882 | 0 | CastExpr *FPCast; |
13883 | 0 | auto getCastAndLiteral = [&FPLiteral, &FPCast](Expr *L, Expr *R) { |
13884 | 0 | FPLiteral = dyn_cast<FloatingLiteral>(L->IgnoreParens()); |
13885 | 0 | FPCast = dyn_cast<CastExpr>(R->IgnoreParens()); |
13886 | 0 | return FPLiteral && FPCast; |
13887 | 0 | }; |
13888 | |
|
13889 | 0 | if (getCastAndLiteral(LHS, RHS) || getCastAndLiteral(RHS, LHS)) { |
13890 | 0 | auto *SourceTy = FPCast->getSubExpr()->getType()->getAs<BuiltinType>(); |
13891 | 0 | auto *TargetTy = FPLiteral->getType()->getAs<BuiltinType>(); |
13892 | 0 | if (SourceTy && TargetTy && SourceTy->isFloatingPoint() && |
13893 | 0 | TargetTy->isFloatingPoint()) { |
13894 | 0 | bool Lossy; |
13895 | 0 | llvm::APFloat TargetC = FPLiteral->getValue(); |
13896 | 0 | TargetC.convert(Context.getFloatTypeSemantics(QualType(SourceTy, 0)), |
13897 | 0 | llvm::APFloat::rmNearestTiesToEven, &Lossy); |
13898 | 0 | if (Lossy) { |
13899 | | // If the literal cannot be represented in the source type, then a |
13900 | | // check for == is always false and check for != is always true. |
13901 | 0 | Diag(Loc, diag::warn_float_compare_literal) |
13902 | 0 | << (Opcode == BO_EQ) << QualType(SourceTy, 0) |
13903 | 0 | << LHS->getSourceRange() << RHS->getSourceRange(); |
13904 | 0 | return; |
13905 | 0 | } |
13906 | 0 | } |
13907 | 0 | } |
13908 | | |
13909 | | // Match a more general floating-point equality comparison (-Wfloat-equal). |
13910 | 0 | Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts(); |
13911 | 0 | Expr* RightExprSansParen = RHS->IgnoreParenImpCasts(); |
13912 | | |
13913 | | // Special case: check for x == x (which is OK). |
13914 | | // Do not emit warnings for such cases. |
13915 | 0 | if (auto *DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) |
13916 | 0 | if (auto *DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) |
13917 | 0 | if (DRL->getDecl() == DRR->getDecl()) |
13918 | 0 | return; |
13919 | | |
13920 | | // Special case: check for comparisons against literals that can be exactly |
13921 | | // represented by APFloat. In such cases, do not emit a warning. This |
13922 | | // is a heuristic: often comparison against such literals are used to |
13923 | | // detect if a value in a variable has not changed. This clearly can |
13924 | | // lead to false negatives. |
13925 | 0 | if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { |
13926 | 0 | if (FLL->isExact()) |
13927 | 0 | return; |
13928 | 0 | } else |
13929 | 0 | if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)) |
13930 | 0 | if (FLR->isExact()) |
13931 | 0 | return; |
13932 | | |
13933 | | // Check for comparisons with builtin types. |
13934 | 0 | if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) |
13935 | 0 | if (CL->getBuiltinCallee()) |
13936 | 0 | return; |
13937 | | |
13938 | 0 | if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) |
13939 | 0 | if (CR->getBuiltinCallee()) |
13940 | 0 | return; |
13941 | | |
13942 | | // Emit the diagnostic. |
13943 | 0 | Diag(Loc, diag::warn_floatingpoint_eq) |
13944 | 0 | << LHS->getSourceRange() << RHS->getSourceRange(); |
13945 | 0 | } |
13946 | | |
13947 | | //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// |
13948 | | //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// |
13949 | | |
13950 | | namespace { |
13951 | | |
13952 | | /// Structure recording the 'active' range of an integer-valued |
13953 | | /// expression. |
13954 | | struct IntRange { |
13955 | | /// The number of bits active in the int. Note that this includes exactly one |
13956 | | /// sign bit if !NonNegative. |
13957 | | unsigned Width; |
13958 | | |
13959 | | /// True if the int is known not to have negative values. If so, all leading |
13960 | | /// bits before Width are known zero, otherwise they are known to be the |
13961 | | /// same as the MSB within Width. |
13962 | | bool NonNegative; |
13963 | | |
13964 | | IntRange(unsigned Width, bool NonNegative) |
13965 | 0 | : Width(Width), NonNegative(NonNegative) {} |
13966 | | |
13967 | | /// Number of bits excluding the sign bit. |
13968 | 0 | unsigned valueBits() const { |
13969 | 0 | return NonNegative ? Width : Width - 1; |
13970 | 0 | } |
13971 | | |
13972 | | /// Returns the range of the bool type. |
13973 | 0 | static IntRange forBoolType() { |
13974 | 0 | return IntRange(1, true); |
13975 | 0 | } |
13976 | | |
13977 | | /// Returns the range of an opaque value of the given integral type. |
13978 | 0 | static IntRange forValueOfType(ASTContext &C, QualType T) { |
13979 | 0 | return forValueOfCanonicalType(C, |
13980 | 0 | T->getCanonicalTypeInternal().getTypePtr()); |
13981 | 0 | } |
13982 | | |
13983 | | /// Returns the range of an opaque value of a canonical integral type. |
13984 | 0 | static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) { |
13985 | 0 | assert(T->isCanonicalUnqualified()); |
13986 | | |
13987 | 0 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
13988 | 0 | T = VT->getElementType().getTypePtr(); |
13989 | 0 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
13990 | 0 | T = CT->getElementType().getTypePtr(); |
13991 | 0 | if (const AtomicType *AT = dyn_cast<AtomicType>(T)) |
13992 | 0 | T = AT->getValueType().getTypePtr(); |
13993 | |
|
13994 | 0 | if (!C.getLangOpts().CPlusPlus) { |
13995 | | // For enum types in C code, use the underlying datatype. |
13996 | 0 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
13997 | 0 | T = ET->getDecl()->getIntegerType().getDesugaredType(C).getTypePtr(); |
13998 | 0 | } else if (const EnumType *ET = dyn_cast<EnumType>(T)) { |
13999 | | // For enum types in C++, use the known bit width of the enumerators. |
14000 | 0 | EnumDecl *Enum = ET->getDecl(); |
14001 | | // In C++11, enums can have a fixed underlying type. Use this type to |
14002 | | // compute the range. |
14003 | 0 | if (Enum->isFixed()) { |
14004 | 0 | return IntRange(C.getIntWidth(QualType(T, 0)), |
14005 | 0 | !ET->isSignedIntegerOrEnumerationType()); |
14006 | 0 | } |
14007 | | |
14008 | 0 | unsigned NumPositive = Enum->getNumPositiveBits(); |
14009 | 0 | unsigned NumNegative = Enum->getNumNegativeBits(); |
14010 | |
|
14011 | 0 | if (NumNegative == 0) |
14012 | 0 | return IntRange(NumPositive, true/*NonNegative*/); |
14013 | 0 | else |
14014 | 0 | return IntRange(std::max(NumPositive + 1, NumNegative), |
14015 | 0 | false/*NonNegative*/); |
14016 | 0 | } |
14017 | | |
14018 | 0 | if (const auto *EIT = dyn_cast<BitIntType>(T)) |
14019 | 0 | return IntRange(EIT->getNumBits(), EIT->isUnsigned()); |
14020 | | |
14021 | 0 | const BuiltinType *BT = cast<BuiltinType>(T); |
14022 | 0 | assert(BT->isInteger()); |
14023 | | |
14024 | 0 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
14025 | 0 | } |
14026 | | |
14027 | | /// Returns the "target" range of a canonical integral type, i.e. |
14028 | | /// the range of values expressible in the type. |
14029 | | /// |
14030 | | /// This matches forValueOfCanonicalType except that enums have the |
14031 | | /// full range of their type, not the range of their enumerators. |
14032 | 0 | static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) { |
14033 | 0 | assert(T->isCanonicalUnqualified()); |
14034 | | |
14035 | 0 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
14036 | 0 | T = VT->getElementType().getTypePtr(); |
14037 | 0 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
14038 | 0 | T = CT->getElementType().getTypePtr(); |
14039 | 0 | if (const AtomicType *AT = dyn_cast<AtomicType>(T)) |
14040 | 0 | T = AT->getValueType().getTypePtr(); |
14041 | 0 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
14042 | 0 | T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr(); |
14043 | |
|
14044 | 0 | if (const auto *EIT = dyn_cast<BitIntType>(T)) |
14045 | 0 | return IntRange(EIT->getNumBits(), EIT->isUnsigned()); |
14046 | | |
14047 | 0 | const BuiltinType *BT = cast<BuiltinType>(T); |
14048 | 0 | assert(BT->isInteger()); |
14049 | | |
14050 | 0 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
14051 | 0 | } |
14052 | | |
14053 | | /// Returns the supremum of two ranges: i.e. their conservative merge. |
14054 | 0 | static IntRange join(IntRange L, IntRange R) { |
14055 | 0 | bool Unsigned = L.NonNegative && R.NonNegative; |
14056 | 0 | return IntRange(std::max(L.valueBits(), R.valueBits()) + !Unsigned, |
14057 | 0 | L.NonNegative && R.NonNegative); |
14058 | 0 | } |
14059 | | |
14060 | | /// Return the range of a bitwise-AND of the two ranges. |
14061 | 0 | static IntRange bit_and(IntRange L, IntRange R) { |
14062 | 0 | unsigned Bits = std::max(L.Width, R.Width); |
14063 | 0 | bool NonNegative = false; |
14064 | 0 | if (L.NonNegative) { |
14065 | 0 | Bits = std::min(Bits, L.Width); |
14066 | 0 | NonNegative = true; |
14067 | 0 | } |
14068 | 0 | if (R.NonNegative) { |
14069 | 0 | Bits = std::min(Bits, R.Width); |
14070 | 0 | NonNegative = true; |
14071 | 0 | } |
14072 | 0 | return IntRange(Bits, NonNegative); |
14073 | 0 | } |
14074 | | |
14075 | | /// Return the range of a sum of the two ranges. |
14076 | 0 | static IntRange sum(IntRange L, IntRange R) { |
14077 | 0 | bool Unsigned = L.NonNegative && R.NonNegative; |
14078 | 0 | return IntRange(std::max(L.valueBits(), R.valueBits()) + 1 + !Unsigned, |
14079 | 0 | Unsigned); |
14080 | 0 | } |
14081 | | |
14082 | | /// Return the range of a difference of the two ranges. |
14083 | 0 | static IntRange difference(IntRange L, IntRange R) { |
14084 | | // We need a 1-bit-wider range if: |
14085 | | // 1) LHS can be negative: least value can be reduced. |
14086 | | // 2) RHS can be negative: greatest value can be increased. |
14087 | 0 | bool CanWiden = !L.NonNegative || !R.NonNegative; |
14088 | 0 | bool Unsigned = L.NonNegative && R.Width == 0; |
14089 | 0 | return IntRange(std::max(L.valueBits(), R.valueBits()) + CanWiden + |
14090 | 0 | !Unsigned, |
14091 | 0 | Unsigned); |
14092 | 0 | } |
14093 | | |
14094 | | /// Return the range of a product of the two ranges. |
14095 | 0 | static IntRange product(IntRange L, IntRange R) { |
14096 | | // If both LHS and RHS can be negative, we can form |
14097 | | // -2^L * -2^R = 2^(L + R) |
14098 | | // which requires L + R + 1 value bits to represent. |
14099 | 0 | bool CanWiden = !L.NonNegative && !R.NonNegative; |
14100 | 0 | bool Unsigned = L.NonNegative && R.NonNegative; |
14101 | 0 | return IntRange(L.valueBits() + R.valueBits() + CanWiden + !Unsigned, |
14102 | 0 | Unsigned); |
14103 | 0 | } |
14104 | | |
14105 | | /// Return the range of a remainder operation between the two ranges. |
14106 | 0 | static IntRange rem(IntRange L, IntRange R) { |
14107 | | // The result of a remainder can't be larger than the result of |
14108 | | // either side. The sign of the result is the sign of the LHS. |
14109 | 0 | bool Unsigned = L.NonNegative; |
14110 | 0 | return IntRange(std::min(L.valueBits(), R.valueBits()) + !Unsigned, |
14111 | 0 | Unsigned); |
14112 | 0 | } |
14113 | | }; |
14114 | | |
14115 | | } // namespace |
14116 | | |
14117 | | static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, |
14118 | 0 | unsigned MaxWidth) { |
14119 | 0 | if (value.isSigned() && value.isNegative()) |
14120 | 0 | return IntRange(value.getSignificantBits(), false); |
14121 | | |
14122 | 0 | if (value.getBitWidth() > MaxWidth) |
14123 | 0 | value = value.trunc(MaxWidth); |
14124 | | |
14125 | | // isNonNegative() just checks the sign bit without considering |
14126 | | // signedness. |
14127 | 0 | return IntRange(value.getActiveBits(), true); |
14128 | 0 | } |
14129 | | |
14130 | | static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, |
14131 | 0 | unsigned MaxWidth) { |
14132 | 0 | if (result.isInt()) |
14133 | 0 | return GetValueRange(C, result.getInt(), MaxWidth); |
14134 | | |
14135 | 0 | if (result.isVector()) { |
14136 | 0 | IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); |
14137 | 0 | for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { |
14138 | 0 | IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); |
14139 | 0 | R = IntRange::join(R, El); |
14140 | 0 | } |
14141 | 0 | return R; |
14142 | 0 | } |
14143 | | |
14144 | 0 | if (result.isComplexInt()) { |
14145 | 0 | IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); |
14146 | 0 | IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); |
14147 | 0 | return IntRange::join(R, I); |
14148 | 0 | } |
14149 | | |
14150 | | // This can happen with lossless casts to intptr_t of "based" lvalues. |
14151 | | // Assume it might use arbitrary bits. |
14152 | | // FIXME: The only reason we need to pass the type in here is to get |
14153 | | // the sign right on this one case. It would be nice if APValue |
14154 | | // preserved this. |
14155 | 0 | assert(result.isLValue() || result.isAddrLabelDiff()); |
14156 | 0 | return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType()); |
14157 | 0 | } |
14158 | | |
14159 | 0 | static QualType GetExprType(const Expr *E) { |
14160 | 0 | QualType Ty = E->getType(); |
14161 | 0 | if (const AtomicType *AtomicRHS = Ty->getAs<AtomicType>()) |
14162 | 0 | Ty = AtomicRHS->getValueType(); |
14163 | 0 | return Ty; |
14164 | 0 | } |
14165 | | |
14166 | | /// Pseudo-evaluate the given integer expression, estimating the |
14167 | | /// range of values it might take. |
14168 | | /// |
14169 | | /// \param MaxWidth The width to which the value will be truncated. |
14170 | | /// \param Approximate If \c true, return a likely range for the result: in |
14171 | | /// particular, assume that arithmetic on narrower types doesn't leave |
14172 | | /// those types. If \c false, return a range including all possible |
14173 | | /// result values. |
14174 | | static IntRange GetExprRange(ASTContext &C, const Expr *E, unsigned MaxWidth, |
14175 | 0 | bool InConstantContext, bool Approximate) { |
14176 | 0 | E = E->IgnoreParens(); |
14177 | | |
14178 | | // Try a full evaluation first. |
14179 | 0 | Expr::EvalResult result; |
14180 | 0 | if (E->EvaluateAsRValue(result, C, InConstantContext)) |
14181 | 0 | return GetValueRange(C, result.Val, GetExprType(E), MaxWidth); |
14182 | | |
14183 | | // I think we only want to look through implicit casts here; if the |
14184 | | // user has an explicit widening cast, we should treat the value as |
14185 | | // being of the new, wider type. |
14186 | 0 | if (const auto *CE = dyn_cast<ImplicitCastExpr>(E)) { |
14187 | 0 | if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue) |
14188 | 0 | return GetExprRange(C, CE->getSubExpr(), MaxWidth, InConstantContext, |
14189 | 0 | Approximate); |
14190 | | |
14191 | 0 | IntRange OutputTypeRange = IntRange::forValueOfType(C, GetExprType(CE)); |
14192 | |
|
14193 | 0 | bool isIntegerCast = CE->getCastKind() == CK_IntegralCast || |
14194 | 0 | CE->getCastKind() == CK_BooleanToSignedIntegral; |
14195 | | |
14196 | | // Assume that non-integer casts can span the full range of the type. |
14197 | 0 | if (!isIntegerCast) |
14198 | 0 | return OutputTypeRange; |
14199 | | |
14200 | 0 | IntRange SubRange = GetExprRange(C, CE->getSubExpr(), |
14201 | 0 | std::min(MaxWidth, OutputTypeRange.Width), |
14202 | 0 | InConstantContext, Approximate); |
14203 | | |
14204 | | // Bail out if the subexpr's range is as wide as the cast type. |
14205 | 0 | if (SubRange.Width >= OutputTypeRange.Width) |
14206 | 0 | return OutputTypeRange; |
14207 | | |
14208 | | // Otherwise, we take the smaller width, and we're non-negative if |
14209 | | // either the output type or the subexpr is. |
14210 | 0 | return IntRange(SubRange.Width, |
14211 | 0 | SubRange.NonNegative || OutputTypeRange.NonNegative); |
14212 | 0 | } |
14213 | | |
14214 | 0 | if (const auto *CO = dyn_cast<ConditionalOperator>(E)) { |
14215 | | // If we can fold the condition, just take that operand. |
14216 | 0 | bool CondResult; |
14217 | 0 | if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) |
14218 | 0 | return GetExprRange(C, |
14219 | 0 | CondResult ? CO->getTrueExpr() : CO->getFalseExpr(), |
14220 | 0 | MaxWidth, InConstantContext, Approximate); |
14221 | | |
14222 | | // Otherwise, conservatively merge. |
14223 | | // GetExprRange requires an integer expression, but a throw expression |
14224 | | // results in a void type. |
14225 | 0 | Expr *E = CO->getTrueExpr(); |
14226 | 0 | IntRange L = E->getType()->isVoidType() |
14227 | 0 | ? IntRange{0, true} |
14228 | 0 | : GetExprRange(C, E, MaxWidth, InConstantContext, Approximate); |
14229 | 0 | E = CO->getFalseExpr(); |
14230 | 0 | IntRange R = E->getType()->isVoidType() |
14231 | 0 | ? IntRange{0, true} |
14232 | 0 | : GetExprRange(C, E, MaxWidth, InConstantContext, Approximate); |
14233 | 0 | return IntRange::join(L, R); |
14234 | 0 | } |
14235 | | |
14236 | 0 | if (const auto *BO = dyn_cast<BinaryOperator>(E)) { |
14237 | 0 | IntRange (*Combine)(IntRange, IntRange) = IntRange::join; |
14238 | |
|
14239 | 0 | switch (BO->getOpcode()) { |
14240 | 0 | case BO_Cmp: |
14241 | 0 | llvm_unreachable("builtin <=> should have class type"); |
14242 | | |
14243 | | // Boolean-valued operations are single-bit and positive. |
14244 | 0 | case BO_LAnd: |
14245 | 0 | case BO_LOr: |
14246 | 0 | case BO_LT: |
14247 | 0 | case BO_GT: |
14248 | 0 | case BO_LE: |
14249 | 0 | case BO_GE: |
14250 | 0 | case BO_EQ: |
14251 | 0 | case BO_NE: |
14252 | 0 | return IntRange::forBoolType(); |
14253 | | |
14254 | | // The type of the assignments is the type of the LHS, so the RHS |
14255 | | // is not necessarily the same type. |
14256 | 0 | case BO_MulAssign: |
14257 | 0 | case BO_DivAssign: |
14258 | 0 | case BO_RemAssign: |
14259 | 0 | case BO_AddAssign: |
14260 | 0 | case BO_SubAssign: |
14261 | 0 | case BO_XorAssign: |
14262 | 0 | case BO_OrAssign: |
14263 | | // TODO: bitfields? |
14264 | 0 | return IntRange::forValueOfType(C, GetExprType(E)); |
14265 | | |
14266 | | // Simple assignments just pass through the RHS, which will have |
14267 | | // been coerced to the LHS type. |
14268 | 0 | case BO_Assign: |
14269 | | // TODO: bitfields? |
14270 | 0 | return GetExprRange(C, BO->getRHS(), MaxWidth, InConstantContext, |
14271 | 0 | Approximate); |
14272 | | |
14273 | | // Operations with opaque sources are black-listed. |
14274 | 0 | case BO_PtrMemD: |
14275 | 0 | case BO_PtrMemI: |
14276 | 0 | return IntRange::forValueOfType(C, GetExprType(E)); |
14277 | | |
14278 | | // Bitwise-and uses the *infinum* of the two source ranges. |
14279 | 0 | case BO_And: |
14280 | 0 | case BO_AndAssign: |
14281 | 0 | Combine = IntRange::bit_and; |
14282 | 0 | break; |
14283 | | |
14284 | | // Left shift gets black-listed based on a judgement call. |
14285 | 0 | case BO_Shl: |
14286 | | // ...except that we want to treat '1 << (blah)' as logically |
14287 | | // positive. It's an important idiom. |
14288 | 0 | if (IntegerLiteral *I |
14289 | 0 | = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { |
14290 | 0 | if (I->getValue() == 1) { |
14291 | 0 | IntRange R = IntRange::forValueOfType(C, GetExprType(E)); |
14292 | 0 | return IntRange(R.Width, /*NonNegative*/ true); |
14293 | 0 | } |
14294 | 0 | } |
14295 | 0 | [[fallthrough]]; |
14296 | | |
14297 | 0 | case BO_ShlAssign: |
14298 | 0 | return IntRange::forValueOfType(C, GetExprType(E)); |
14299 | | |
14300 | | // Right shift by a constant can narrow its left argument. |
14301 | 0 | case BO_Shr: |
14302 | 0 | case BO_ShrAssign: { |
14303 | 0 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth, InConstantContext, |
14304 | 0 | Approximate); |
14305 | | |
14306 | | // If the shift amount is a positive constant, drop the width by |
14307 | | // that much. |
14308 | 0 | if (std::optional<llvm::APSInt> shift = |
14309 | 0 | BO->getRHS()->getIntegerConstantExpr(C)) { |
14310 | 0 | if (shift->isNonNegative()) { |
14311 | 0 | if (shift->uge(L.Width)) |
14312 | 0 | L.Width = (L.NonNegative ? 0 : 1); |
14313 | 0 | else |
14314 | 0 | L.Width -= shift->getZExtValue(); |
14315 | 0 | } |
14316 | 0 | } |
14317 | |
|
14318 | 0 | return L; |
14319 | 0 | } |
14320 | | |
14321 | | // Comma acts as its right operand. |
14322 | 0 | case BO_Comma: |
14323 | 0 | return GetExprRange(C, BO->getRHS(), MaxWidth, InConstantContext, |
14324 | 0 | Approximate); |
14325 | | |
14326 | 0 | case BO_Add: |
14327 | 0 | if (!Approximate) |
14328 | 0 | Combine = IntRange::sum; |
14329 | 0 | break; |
14330 | | |
14331 | 0 | case BO_Sub: |
14332 | 0 | if (BO->getLHS()->getType()->isPointerType()) |
14333 | 0 | return IntRange::forValueOfType(C, GetExprType(E)); |
14334 | 0 | if (!Approximate) |
14335 | 0 | Combine = IntRange::difference; |
14336 | 0 | break; |
14337 | | |
14338 | 0 | case BO_Mul: |
14339 | 0 | if (!Approximate) |
14340 | 0 | Combine = IntRange::product; |
14341 | 0 | break; |
14342 | | |
14343 | | // The width of a division result is mostly determined by the size |
14344 | | // of the LHS. |
14345 | 0 | case BO_Div: { |
14346 | | // Don't 'pre-truncate' the operands. |
14347 | 0 | unsigned opWidth = C.getIntWidth(GetExprType(E)); |
14348 | 0 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth, InConstantContext, |
14349 | 0 | Approximate); |
14350 | | |
14351 | | // If the divisor is constant, use that. |
14352 | 0 | if (std::optional<llvm::APSInt> divisor = |
14353 | 0 | BO->getRHS()->getIntegerConstantExpr(C)) { |
14354 | 0 | unsigned log2 = divisor->logBase2(); // floor(log_2(divisor)) |
14355 | 0 | if (log2 >= L.Width) |
14356 | 0 | L.Width = (L.NonNegative ? 0 : 1); |
14357 | 0 | else |
14358 | 0 | L.Width = std::min(L.Width - log2, MaxWidth); |
14359 | 0 | return L; |
14360 | 0 | } |
14361 | | |
14362 | | // Otherwise, just use the LHS's width. |
14363 | | // FIXME: This is wrong if the LHS could be its minimal value and the RHS |
14364 | | // could be -1. |
14365 | 0 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth, InConstantContext, |
14366 | 0 | Approximate); |
14367 | 0 | return IntRange(L.Width, L.NonNegative && R.NonNegative); |
14368 | 0 | } |
14369 | | |
14370 | 0 | case BO_Rem: |
14371 | 0 | Combine = IntRange::rem; |
14372 | 0 | break; |
14373 | | |
14374 | | // The default behavior is okay for these. |
14375 | 0 | case BO_Xor: |
14376 | 0 | case BO_Or: |
14377 | 0 | break; |
14378 | 0 | } |
14379 | | |
14380 | | // Combine the two ranges, but limit the result to the type in which we |
14381 | | // performed the computation. |
14382 | 0 | QualType T = GetExprType(E); |
14383 | 0 | unsigned opWidth = C.getIntWidth(T); |
14384 | 0 | IntRange L = |
14385 | 0 | GetExprRange(C, BO->getLHS(), opWidth, InConstantContext, Approximate); |
14386 | 0 | IntRange R = |
14387 | 0 | GetExprRange(C, BO->getRHS(), opWidth, InConstantContext, Approximate); |
14388 | 0 | IntRange C = Combine(L, R); |
14389 | 0 | C.NonNegative |= T->isUnsignedIntegerOrEnumerationType(); |
14390 | 0 | C.Width = std::min(C.Width, MaxWidth); |
14391 | 0 | return C; |
14392 | 0 | } |
14393 | | |
14394 | 0 | if (const auto *UO = dyn_cast<UnaryOperator>(E)) { |
14395 | 0 | switch (UO->getOpcode()) { |
14396 | | // Boolean-valued operations are white-listed. |
14397 | 0 | case UO_LNot: |
14398 | 0 | return IntRange::forBoolType(); |
14399 | | |
14400 | | // Operations with opaque sources are black-listed. |
14401 | 0 | case UO_Deref: |
14402 | 0 | case UO_AddrOf: // should be impossible |
14403 | 0 | return IntRange::forValueOfType(C, GetExprType(E)); |
14404 | | |
14405 | 0 | default: |
14406 | 0 | return GetExprRange(C, UO->getSubExpr(), MaxWidth, InConstantContext, |
14407 | 0 | Approximate); |
14408 | 0 | } |
14409 | 0 | } |
14410 | | |
14411 | 0 | if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E)) |
14412 | 0 | return GetExprRange(C, OVE->getSourceExpr(), MaxWidth, InConstantContext, |
14413 | 0 | Approximate); |
14414 | | |
14415 | 0 | if (const auto *BitField = E->getSourceBitField()) |
14416 | 0 | return IntRange(BitField->getBitWidthValue(C), |
14417 | 0 | BitField->getType()->isUnsignedIntegerOrEnumerationType()); |
14418 | | |
14419 | 0 | return IntRange::forValueOfType(C, GetExprType(E)); |
14420 | 0 | } |
14421 | | |
14422 | | static IntRange GetExprRange(ASTContext &C, const Expr *E, |
14423 | 0 | bool InConstantContext, bool Approximate) { |
14424 | 0 | return GetExprRange(C, E, C.getIntWidth(GetExprType(E)), InConstantContext, |
14425 | 0 | Approximate); |
14426 | 0 | } |
14427 | | |
14428 | | /// Checks whether the given value, which currently has the given |
14429 | | /// source semantics, has the same value when coerced through the |
14430 | | /// target semantics. |
14431 | | static bool IsSameFloatAfterCast(const llvm::APFloat &value, |
14432 | | const llvm::fltSemantics &Src, |
14433 | 0 | const llvm::fltSemantics &Tgt) { |
14434 | 0 | llvm::APFloat truncated = value; |
14435 | |
|
14436 | 0 | bool ignored; |
14437 | 0 | truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); |
14438 | 0 | truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); |
14439 | |
|
14440 | 0 | return truncated.bitwiseIsEqual(value); |
14441 | 0 | } |
14442 | | |
14443 | | /// Checks whether the given value, which currently has the given |
14444 | | /// source semantics, has the same value when coerced through the |
14445 | | /// target semantics. |
14446 | | /// |
14447 | | /// The value might be a vector of floats (or a complex number). |
14448 | | static bool IsSameFloatAfterCast(const APValue &value, |
14449 | | const llvm::fltSemantics &Src, |
14450 | 0 | const llvm::fltSemantics &Tgt) { |
14451 | 0 | if (value.isFloat()) |
14452 | 0 | return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); |
14453 | | |
14454 | 0 | if (value.isVector()) { |
14455 | 0 | for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) |
14456 | 0 | if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) |
14457 | 0 | return false; |
14458 | 0 | return true; |
14459 | 0 | } |
14460 | | |
14461 | 0 | assert(value.isComplexFloat()); |
14462 | 0 | return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && |
14463 | 0 | IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); |
14464 | 0 | } |
14465 | | |
14466 | | static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC, |
14467 | | bool IsListInit = false); |
14468 | | |
14469 | 0 | static bool IsEnumConstOrFromMacro(Sema &S, Expr *E) { |
14470 | | // Suppress cases where we are comparing against an enum constant. |
14471 | 0 | if (const DeclRefExpr *DR = |
14472 | 0 | dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) |
14473 | 0 | if (isa<EnumConstantDecl>(DR->getDecl())) |
14474 | 0 | return true; |
14475 | | |
14476 | | // Suppress cases where the value is expanded from a macro, unless that macro |
14477 | | // is how a language represents a boolean literal. This is the case in both C |
14478 | | // and Objective-C. |
14479 | 0 | SourceLocation BeginLoc = E->getBeginLoc(); |
14480 | 0 | if (BeginLoc.isMacroID()) { |
14481 | 0 | StringRef MacroName = Lexer::getImmediateMacroName( |
14482 | 0 | BeginLoc, S.getSourceManager(), S.getLangOpts()); |
14483 | 0 | return MacroName != "YES" && MacroName != "NO" && |
14484 | 0 | MacroName != "true" && MacroName != "false"; |
14485 | 0 | } |
14486 | | |
14487 | 0 | return false; |
14488 | 0 | } |
14489 | | |
14490 | 0 | static bool isKnownToHaveUnsignedValue(Expr *E) { |
14491 | 0 | return E->getType()->isIntegerType() && |
14492 | 0 | (!E->getType()->isSignedIntegerType() || |
14493 | 0 | !E->IgnoreParenImpCasts()->getType()->isSignedIntegerType()); |
14494 | 0 | } |
14495 | | |
14496 | | namespace { |
14497 | | /// The promoted range of values of a type. In general this has the |
14498 | | /// following structure: |
14499 | | /// |
14500 | | /// |-----------| . . . |-----------| |
14501 | | /// ^ ^ ^ ^ |
14502 | | /// Min HoleMin HoleMax Max |
14503 | | /// |
14504 | | /// ... where there is only a hole if a signed type is promoted to unsigned |
14505 | | /// (in which case Min and Max are the smallest and largest representable |
14506 | | /// values). |
14507 | | struct PromotedRange { |
14508 | | // Min, or HoleMax if there is a hole. |
14509 | | llvm::APSInt PromotedMin; |
14510 | | // Max, or HoleMin if there is a hole. |
14511 | | llvm::APSInt PromotedMax; |
14512 | | |
14513 | 0 | PromotedRange(IntRange R, unsigned BitWidth, bool Unsigned) { |
14514 | 0 | if (R.Width == 0) |
14515 | 0 | PromotedMin = PromotedMax = llvm::APSInt(BitWidth, Unsigned); |
14516 | 0 | else if (R.Width >= BitWidth && !Unsigned) { |
14517 | | // Promotion made the type *narrower*. This happens when promoting |
14518 | | // a < 32-bit unsigned / <= 32-bit signed bit-field to 'signed int'. |
14519 | | // Treat all values of 'signed int' as being in range for now. |
14520 | 0 | PromotedMin = llvm::APSInt::getMinValue(BitWidth, Unsigned); |
14521 | 0 | PromotedMax = llvm::APSInt::getMaxValue(BitWidth, Unsigned); |
14522 | 0 | } else { |
14523 | 0 | PromotedMin = llvm::APSInt::getMinValue(R.Width, R.NonNegative) |
14524 | 0 | .extOrTrunc(BitWidth); |
14525 | 0 | PromotedMin.setIsUnsigned(Unsigned); |
14526 | |
|
14527 | 0 | PromotedMax = llvm::APSInt::getMaxValue(R.Width, R.NonNegative) |
14528 | 0 | .extOrTrunc(BitWidth); |
14529 | 0 | PromotedMax.setIsUnsigned(Unsigned); |
14530 | 0 | } |
14531 | 0 | } |
14532 | | |
14533 | | // Determine whether this range is contiguous (has no hole). |
14534 | 0 | bool isContiguous() const { return PromotedMin <= PromotedMax; } |
14535 | | |
14536 | | // Where a constant value is within the range. |
14537 | | enum ComparisonResult { |
14538 | | LT = 0x1, |
14539 | | LE = 0x2, |
14540 | | GT = 0x4, |
14541 | | GE = 0x8, |
14542 | | EQ = 0x10, |
14543 | | NE = 0x20, |
14544 | | InRangeFlag = 0x40, |
14545 | | |
14546 | | Less = LE | LT | NE, |
14547 | | Min = LE | InRangeFlag, |
14548 | | InRange = InRangeFlag, |
14549 | | Max = GE | InRangeFlag, |
14550 | | Greater = GE | GT | NE, |
14551 | | |
14552 | | OnlyValue = LE | GE | EQ | InRangeFlag, |
14553 | | InHole = NE |
14554 | | }; |
14555 | | |
14556 | 0 | ComparisonResult compare(const llvm::APSInt &Value) const { |
14557 | 0 | assert(Value.getBitWidth() == PromotedMin.getBitWidth() && |
14558 | 0 | Value.isUnsigned() == PromotedMin.isUnsigned()); |
14559 | 0 | if (!isContiguous()) { |
14560 | 0 | assert(Value.isUnsigned() && "discontiguous range for signed compare"); |
14561 | 0 | if (Value.isMinValue()) return Min; |
14562 | 0 | if (Value.isMaxValue()) return Max; |
14563 | 0 | if (Value >= PromotedMin) return InRange; |
14564 | 0 | if (Value <= PromotedMax) return InRange; |
14565 | 0 | return InHole; |
14566 | 0 | } |
14567 | | |
14568 | 0 | switch (llvm::APSInt::compareValues(Value, PromotedMin)) { |
14569 | 0 | case -1: return Less; |
14570 | 0 | case 0: return PromotedMin == PromotedMax ? OnlyValue : Min; |
14571 | 0 | case 1: |
14572 | 0 | switch (llvm::APSInt::compareValues(Value, PromotedMax)) { |
14573 | 0 | case -1: return InRange; |
14574 | 0 | case 0: return Max; |
14575 | 0 | case 1: return Greater; |
14576 | 0 | } |
14577 | 0 | } |
14578 | | |
14579 | 0 | llvm_unreachable("impossible compare result"); |
14580 | 0 | } |
14581 | | |
14582 | | static std::optional<StringRef> |
14583 | 0 | constantValue(BinaryOperatorKind Op, ComparisonResult R, bool ConstantOnRHS) { |
14584 | 0 | if (Op == BO_Cmp) { |
14585 | 0 | ComparisonResult LTFlag = LT, GTFlag = GT; |
14586 | 0 | if (ConstantOnRHS) std::swap(LTFlag, GTFlag); |
14587 | |
|
14588 | 0 | if (R & EQ) return StringRef("'std::strong_ordering::equal'"); |
14589 | 0 | if (R & LTFlag) return StringRef("'std::strong_ordering::less'"); |
14590 | 0 | if (R & GTFlag) return StringRef("'std::strong_ordering::greater'"); |
14591 | 0 | return std::nullopt; |
14592 | 0 | } |
14593 | | |
14594 | 0 | ComparisonResult TrueFlag, FalseFlag; |
14595 | 0 | if (Op == BO_EQ) { |
14596 | 0 | TrueFlag = EQ; |
14597 | 0 | FalseFlag = NE; |
14598 | 0 | } else if (Op == BO_NE) { |
14599 | 0 | TrueFlag = NE; |
14600 | 0 | FalseFlag = EQ; |
14601 | 0 | } else { |
14602 | 0 | if ((Op == BO_LT || Op == BO_GE) ^ ConstantOnRHS) { |
14603 | 0 | TrueFlag = LT; |
14604 | 0 | FalseFlag = GE; |
14605 | 0 | } else { |
14606 | 0 | TrueFlag = GT; |
14607 | 0 | FalseFlag = LE; |
14608 | 0 | } |
14609 | 0 | if (Op == BO_GE || Op == BO_LE) |
14610 | 0 | std::swap(TrueFlag, FalseFlag); |
14611 | 0 | } |
14612 | 0 | if (R & TrueFlag) |
14613 | 0 | return StringRef("true"); |
14614 | 0 | if (R & FalseFlag) |
14615 | 0 | return StringRef("false"); |
14616 | 0 | return std::nullopt; |
14617 | 0 | } |
14618 | | }; |
14619 | | } |
14620 | | |
14621 | 0 | static bool HasEnumType(Expr *E) { |
14622 | | // Strip off implicit integral promotions. |
14623 | 0 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
14624 | 0 | if (ICE->getCastKind() != CK_IntegralCast && |
14625 | 0 | ICE->getCastKind() != CK_NoOp) |
14626 | 0 | break; |
14627 | 0 | E = ICE->getSubExpr(); |
14628 | 0 | } |
14629 | |
|
14630 | 0 | return E->getType()->isEnumeralType(); |
14631 | 0 | } |
14632 | | |
14633 | 0 | static int classifyConstantValue(Expr *Constant) { |
14634 | | // The values of this enumeration are used in the diagnostics |
14635 | | // diag::warn_out_of_range_compare and diag::warn_tautological_bool_compare. |
14636 | 0 | enum ConstantValueKind { |
14637 | 0 | Miscellaneous = 0, |
14638 | 0 | LiteralTrue, |
14639 | 0 | LiteralFalse |
14640 | 0 | }; |
14641 | 0 | if (auto *BL = dyn_cast<CXXBoolLiteralExpr>(Constant)) |
14642 | 0 | return BL->getValue() ? ConstantValueKind::LiteralTrue |
14643 | 0 | : ConstantValueKind::LiteralFalse; |
14644 | 0 | return ConstantValueKind::Miscellaneous; |
14645 | 0 | } |
14646 | | |
14647 | | static bool CheckTautologicalComparison(Sema &S, BinaryOperator *E, |
14648 | | Expr *Constant, Expr *Other, |
14649 | | const llvm::APSInt &Value, |
14650 | 0 | bool RhsConstant) { |
14651 | 0 | if (S.inTemplateInstantiation()) |
14652 | 0 | return false; |
14653 | | |
14654 | 0 | Expr *OriginalOther = Other; |
14655 | |
|
14656 | 0 | Constant = Constant->IgnoreParenImpCasts(); |
14657 | 0 | Other = Other->IgnoreParenImpCasts(); |
14658 | | |
14659 | | // Suppress warnings on tautological comparisons between values of the same |
14660 | | // enumeration type. There are only two ways we could warn on this: |
14661 | | // - If the constant is outside the range of representable values of |
14662 | | // the enumeration. In such a case, we should warn about the cast |
14663 | | // to enumeration type, not about the comparison. |
14664 | | // - If the constant is the maximum / minimum in-range value. For an |
14665 | | // enumeratin type, such comparisons can be meaningful and useful. |
14666 | 0 | if (Constant->getType()->isEnumeralType() && |
14667 | 0 | S.Context.hasSameUnqualifiedType(Constant->getType(), Other->getType())) |
14668 | 0 | return false; |
14669 | | |
14670 | 0 | IntRange OtherValueRange = GetExprRange( |
14671 | 0 | S.Context, Other, S.isConstantEvaluatedContext(), /*Approximate=*/false); |
14672 | |
|
14673 | 0 | QualType OtherT = Other->getType(); |
14674 | 0 | if (const auto *AT = OtherT->getAs<AtomicType>()) |
14675 | 0 | OtherT = AT->getValueType(); |
14676 | 0 | IntRange OtherTypeRange = IntRange::forValueOfType(S.Context, OtherT); |
14677 | | |
14678 | | // Special case for ObjC BOOL on targets where its a typedef for a signed char |
14679 | | // (Namely, macOS). FIXME: IntRange::forValueOfType should do this. |
14680 | 0 | bool IsObjCSignedCharBool = S.getLangOpts().ObjC && |
14681 | 0 | S.NSAPIObj->isObjCBOOLType(OtherT) && |
14682 | 0 | OtherT->isSpecificBuiltinType(BuiltinType::SChar); |
14683 | | |
14684 | | // Whether we're treating Other as being a bool because of the form of |
14685 | | // expression despite it having another type (typically 'int' in C). |
14686 | 0 | bool OtherIsBooleanDespiteType = |
14687 | 0 | !OtherT->isBooleanType() && Other->isKnownToHaveBooleanValue(); |
14688 | 0 | if (OtherIsBooleanDespiteType || IsObjCSignedCharBool) |
14689 | 0 | OtherTypeRange = OtherValueRange = IntRange::forBoolType(); |
14690 | | |
14691 | | // Check if all values in the range of possible values of this expression |
14692 | | // lead to the same comparison outcome. |
14693 | 0 | PromotedRange OtherPromotedValueRange(OtherValueRange, Value.getBitWidth(), |
14694 | 0 | Value.isUnsigned()); |
14695 | 0 | auto Cmp = OtherPromotedValueRange.compare(Value); |
14696 | 0 | auto Result = PromotedRange::constantValue(E->getOpcode(), Cmp, RhsConstant); |
14697 | 0 | if (!Result) |
14698 | 0 | return false; |
14699 | | |
14700 | | // Also consider the range determined by the type alone. This allows us to |
14701 | | // classify the warning under the proper diagnostic group. |
14702 | 0 | bool TautologicalTypeCompare = false; |
14703 | 0 | { |
14704 | 0 | PromotedRange OtherPromotedTypeRange(OtherTypeRange, Value.getBitWidth(), |
14705 | 0 | Value.isUnsigned()); |
14706 | 0 | auto TypeCmp = OtherPromotedTypeRange.compare(Value); |
14707 | 0 | if (auto TypeResult = PromotedRange::constantValue(E->getOpcode(), TypeCmp, |
14708 | 0 | RhsConstant)) { |
14709 | 0 | TautologicalTypeCompare = true; |
14710 | 0 | Cmp = TypeCmp; |
14711 | 0 | Result = TypeResult; |
14712 | 0 | } |
14713 | 0 | } |
14714 | | |
14715 | | // Don't warn if the non-constant operand actually always evaluates to the |
14716 | | // same value. |
14717 | 0 | if (!TautologicalTypeCompare && OtherValueRange.Width == 0) |
14718 | 0 | return false; |
14719 | | |
14720 | | // Suppress the diagnostic for an in-range comparison if the constant comes |
14721 | | // from a macro or enumerator. We don't want to diagnose |
14722 | | // |
14723 | | // some_long_value <= INT_MAX |
14724 | | // |
14725 | | // when sizeof(int) == sizeof(long). |
14726 | 0 | bool InRange = Cmp & PromotedRange::InRangeFlag; |
14727 | 0 | if (InRange && IsEnumConstOrFromMacro(S, Constant)) |
14728 | 0 | return false; |
14729 | | |
14730 | | // A comparison of an unsigned bit-field against 0 is really a type problem, |
14731 | | // even though at the type level the bit-field might promote to 'signed int'. |
14732 | 0 | if (Other->refersToBitField() && InRange && Value == 0 && |
14733 | 0 | Other->getType()->isUnsignedIntegerOrEnumerationType()) |
14734 | 0 | TautologicalTypeCompare = true; |
14735 | | |
14736 | | // If this is a comparison to an enum constant, include that |
14737 | | // constant in the diagnostic. |
14738 | 0 | const EnumConstantDecl *ED = nullptr; |
14739 | 0 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Constant)) |
14740 | 0 | ED = dyn_cast<EnumConstantDecl>(DR->getDecl()); |
14741 | | |
14742 | | // Should be enough for uint128 (39 decimal digits) |
14743 | 0 | SmallString<64> PrettySourceValue; |
14744 | 0 | llvm::raw_svector_ostream OS(PrettySourceValue); |
14745 | 0 | if (ED) { |
14746 | 0 | OS << '\'' << *ED << "' (" << Value << ")"; |
14747 | 0 | } else if (auto *BL = dyn_cast<ObjCBoolLiteralExpr>( |
14748 | 0 | Constant->IgnoreParenImpCasts())) { |
14749 | 0 | OS << (BL->getValue() ? "YES" : "NO"); |
14750 | 0 | } else { |
14751 | 0 | OS << Value; |
14752 | 0 | } |
14753 | |
|
14754 | 0 | if (!TautologicalTypeCompare) { |
14755 | 0 | S.Diag(E->getOperatorLoc(), diag::warn_tautological_compare_value_range) |
14756 | 0 | << RhsConstant << OtherValueRange.Width << OtherValueRange.NonNegative |
14757 | 0 | << E->getOpcodeStr() << OS.str() << *Result |
14758 | 0 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
14759 | 0 | return true; |
14760 | 0 | } |
14761 | | |
14762 | 0 | if (IsObjCSignedCharBool) { |
14763 | 0 | S.DiagRuntimeBehavior(E->getOperatorLoc(), E, |
14764 | 0 | S.PDiag(diag::warn_tautological_compare_objc_bool) |
14765 | 0 | << OS.str() << *Result); |
14766 | 0 | return true; |
14767 | 0 | } |
14768 | | |
14769 | | // FIXME: We use a somewhat different formatting for the in-range cases and |
14770 | | // cases involving boolean values for historical reasons. We should pick a |
14771 | | // consistent way of presenting these diagnostics. |
14772 | 0 | if (!InRange || Other->isKnownToHaveBooleanValue()) { |
14773 | |
|
14774 | 0 | S.DiagRuntimeBehavior( |
14775 | 0 | E->getOperatorLoc(), E, |
14776 | 0 | S.PDiag(!InRange ? diag::warn_out_of_range_compare |
14777 | 0 | : diag::warn_tautological_bool_compare) |
14778 | 0 | << OS.str() << classifyConstantValue(Constant) << OtherT |
14779 | 0 | << OtherIsBooleanDespiteType << *Result |
14780 | 0 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange()); |
14781 | 0 | } else { |
14782 | 0 | bool IsCharTy = OtherT.withoutLocalFastQualifiers() == S.Context.CharTy; |
14783 | 0 | unsigned Diag = |
14784 | 0 | (isKnownToHaveUnsignedValue(OriginalOther) && Value == 0) |
14785 | 0 | ? (HasEnumType(OriginalOther) |
14786 | 0 | ? diag::warn_unsigned_enum_always_true_comparison |
14787 | 0 | : IsCharTy ? diag::warn_unsigned_char_always_true_comparison |
14788 | 0 | : diag::warn_unsigned_always_true_comparison) |
14789 | 0 | : diag::warn_tautological_constant_compare; |
14790 | |
|
14791 | 0 | S.Diag(E->getOperatorLoc(), Diag) |
14792 | 0 | << RhsConstant << OtherT << E->getOpcodeStr() << OS.str() << *Result |
14793 | 0 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
14794 | 0 | } |
14795 | |
|
14796 | 0 | return true; |
14797 | 0 | } |
14798 | | |
14799 | | /// Analyze the operands of the given comparison. Implements the |
14800 | | /// fallback case from AnalyzeComparison. |
14801 | 0 | static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { |
14802 | 0 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
14803 | 0 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
14804 | 0 | } |
14805 | | |
14806 | | /// Implements -Wsign-compare. |
14807 | | /// |
14808 | | /// \param E the binary operator to check for warnings |
14809 | 0 | static void AnalyzeComparison(Sema &S, BinaryOperator *E) { |
14810 | | // The type the comparison is being performed in. |
14811 | 0 | QualType T = E->getLHS()->getType(); |
14812 | | |
14813 | | // Only analyze comparison operators where both sides have been converted to |
14814 | | // the same type. |
14815 | 0 | if (!S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType())) |
14816 | 0 | return AnalyzeImpConvsInComparison(S, E); |
14817 | | |
14818 | | // Don't analyze value-dependent comparisons directly. |
14819 | 0 | if (E->isValueDependent()) |
14820 | 0 | return AnalyzeImpConvsInComparison(S, E); |
14821 | | |
14822 | 0 | Expr *LHS = E->getLHS(); |
14823 | 0 | Expr *RHS = E->getRHS(); |
14824 | |
|
14825 | 0 | if (T->isIntegralType(S.Context)) { |
14826 | 0 | std::optional<llvm::APSInt> RHSValue = |
14827 | 0 | RHS->getIntegerConstantExpr(S.Context); |
14828 | 0 | std::optional<llvm::APSInt> LHSValue = |
14829 | 0 | LHS->getIntegerConstantExpr(S.Context); |
14830 | | |
14831 | | // We don't care about expressions whose result is a constant. |
14832 | 0 | if (RHSValue && LHSValue) |
14833 | 0 | return AnalyzeImpConvsInComparison(S, E); |
14834 | | |
14835 | | // We only care about expressions where just one side is literal |
14836 | 0 | if ((bool)RHSValue ^ (bool)LHSValue) { |
14837 | | // Is the constant on the RHS or LHS? |
14838 | 0 | const bool RhsConstant = (bool)RHSValue; |
14839 | 0 | Expr *Const = RhsConstant ? RHS : LHS; |
14840 | 0 | Expr *Other = RhsConstant ? LHS : RHS; |
14841 | 0 | const llvm::APSInt &Value = RhsConstant ? *RHSValue : *LHSValue; |
14842 | | |
14843 | | // Check whether an integer constant comparison results in a value |
14844 | | // of 'true' or 'false'. |
14845 | 0 | if (CheckTautologicalComparison(S, E, Const, Other, Value, RhsConstant)) |
14846 | 0 | return AnalyzeImpConvsInComparison(S, E); |
14847 | 0 | } |
14848 | 0 | } |
14849 | | |
14850 | 0 | if (!T->hasUnsignedIntegerRepresentation()) { |
14851 | | // We don't do anything special if this isn't an unsigned integral |
14852 | | // comparison: we're only interested in integral comparisons, and |
14853 | | // signed comparisons only happen in cases we don't care to warn about. |
14854 | 0 | return AnalyzeImpConvsInComparison(S, E); |
14855 | 0 | } |
14856 | | |
14857 | 0 | LHS = LHS->IgnoreParenImpCasts(); |
14858 | 0 | RHS = RHS->IgnoreParenImpCasts(); |
14859 | |
|
14860 | 0 | if (!S.getLangOpts().CPlusPlus) { |
14861 | | // Avoid warning about comparison of integers with different signs when |
14862 | | // RHS/LHS has a `typeof(E)` type whose sign is different from the sign of |
14863 | | // the type of `E`. |
14864 | 0 | if (const auto *TET = dyn_cast<TypeOfExprType>(LHS->getType())) |
14865 | 0 | LHS = TET->getUnderlyingExpr()->IgnoreParenImpCasts(); |
14866 | 0 | if (const auto *TET = dyn_cast<TypeOfExprType>(RHS->getType())) |
14867 | 0 | RHS = TET->getUnderlyingExpr()->IgnoreParenImpCasts(); |
14868 | 0 | } |
14869 | | |
14870 | | // Check to see if one of the (unmodified) operands is of different |
14871 | | // signedness. |
14872 | 0 | Expr *signedOperand, *unsignedOperand; |
14873 | 0 | if (LHS->getType()->hasSignedIntegerRepresentation()) { |
14874 | 0 | assert(!RHS->getType()->hasSignedIntegerRepresentation() && |
14875 | 0 | "unsigned comparison between two signed integer expressions?"); |
14876 | 0 | signedOperand = LHS; |
14877 | 0 | unsignedOperand = RHS; |
14878 | 0 | } else if (RHS->getType()->hasSignedIntegerRepresentation()) { |
14879 | 0 | signedOperand = RHS; |
14880 | 0 | unsignedOperand = LHS; |
14881 | 0 | } else { |
14882 | 0 | return AnalyzeImpConvsInComparison(S, E); |
14883 | 0 | } |
14884 | | |
14885 | | // Otherwise, calculate the effective range of the signed operand. |
14886 | 0 | IntRange signedRange = |
14887 | 0 | GetExprRange(S.Context, signedOperand, S.isConstantEvaluatedContext(), |
14888 | 0 | /*Approximate=*/true); |
14889 | | |
14890 | | // Go ahead and analyze implicit conversions in the operands. Note |
14891 | | // that we skip the implicit conversions on both sides. |
14892 | 0 | AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc()); |
14893 | 0 | AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc()); |
14894 | | |
14895 | | // If the signed range is non-negative, -Wsign-compare won't fire. |
14896 | 0 | if (signedRange.NonNegative) |
14897 | 0 | return; |
14898 | | |
14899 | | // For (in)equality comparisons, if the unsigned operand is a |
14900 | | // constant which cannot collide with a overflowed signed operand, |
14901 | | // then reinterpreting the signed operand as unsigned will not |
14902 | | // change the result of the comparison. |
14903 | 0 | if (E->isEqualityOp()) { |
14904 | 0 | unsigned comparisonWidth = S.Context.getIntWidth(T); |
14905 | 0 | IntRange unsignedRange = |
14906 | 0 | GetExprRange(S.Context, unsignedOperand, S.isConstantEvaluatedContext(), |
14907 | 0 | /*Approximate=*/true); |
14908 | | |
14909 | | // We should never be unable to prove that the unsigned operand is |
14910 | | // non-negative. |
14911 | 0 | assert(unsignedRange.NonNegative && "unsigned range includes negative?"); |
14912 | | |
14913 | 0 | if (unsignedRange.Width < comparisonWidth) |
14914 | 0 | return; |
14915 | 0 | } |
14916 | | |
14917 | 0 | S.DiagRuntimeBehavior(E->getOperatorLoc(), E, |
14918 | 0 | S.PDiag(diag::warn_mixed_sign_comparison) |
14919 | 0 | << LHS->getType() << RHS->getType() |
14920 | 0 | << LHS->getSourceRange() << RHS->getSourceRange()); |
14921 | 0 | } |
14922 | | |
14923 | | /// Analyzes an attempt to assign the given value to a bitfield. |
14924 | | /// |
14925 | | /// Returns true if there was something fishy about the attempt. |
14926 | | static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, |
14927 | 0 | SourceLocation InitLoc) { |
14928 | 0 | assert(Bitfield->isBitField()); |
14929 | 0 | if (Bitfield->isInvalidDecl()) |
14930 | 0 | return false; |
14931 | | |
14932 | | // White-list bool bitfields. |
14933 | 0 | QualType BitfieldType = Bitfield->getType(); |
14934 | 0 | if (BitfieldType->isBooleanType()) |
14935 | 0 | return false; |
14936 | | |
14937 | 0 | if (BitfieldType->isEnumeralType()) { |
14938 | 0 | EnumDecl *BitfieldEnumDecl = BitfieldType->castAs<EnumType>()->getDecl(); |
14939 | | // If the underlying enum type was not explicitly specified as an unsigned |
14940 | | // type and the enum contain only positive values, MSVC++ will cause an |
14941 | | // inconsistency by storing this as a signed type. |
14942 | 0 | if (S.getLangOpts().CPlusPlus11 && |
14943 | 0 | !BitfieldEnumDecl->getIntegerTypeSourceInfo() && |
14944 | 0 | BitfieldEnumDecl->getNumPositiveBits() > 0 && |
14945 | 0 | BitfieldEnumDecl->getNumNegativeBits() == 0) { |
14946 | 0 | S.Diag(InitLoc, diag::warn_no_underlying_type_specified_for_enum_bitfield) |
14947 | 0 | << BitfieldEnumDecl; |
14948 | 0 | } |
14949 | 0 | } |
14950 | | |
14951 | | // Ignore value- or type-dependent expressions. |
14952 | 0 | if (Bitfield->getBitWidth()->isValueDependent() || |
14953 | 0 | Bitfield->getBitWidth()->isTypeDependent() || |
14954 | 0 | Init->isValueDependent() || |
14955 | 0 | Init->isTypeDependent()) |
14956 | 0 | return false; |
14957 | | |
14958 | 0 | Expr *OriginalInit = Init->IgnoreParenImpCasts(); |
14959 | 0 | unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context); |
14960 | |
|
14961 | 0 | Expr::EvalResult Result; |
14962 | 0 | if (!OriginalInit->EvaluateAsInt(Result, S.Context, |
14963 | 0 | Expr::SE_AllowSideEffects)) { |
14964 | | // The RHS is not constant. If the RHS has an enum type, make sure the |
14965 | | // bitfield is wide enough to hold all the values of the enum without |
14966 | | // truncation. |
14967 | 0 | if (const auto *EnumTy = OriginalInit->getType()->getAs<EnumType>()) { |
14968 | 0 | EnumDecl *ED = EnumTy->getDecl(); |
14969 | 0 | bool SignedBitfield = BitfieldType->isSignedIntegerType(); |
14970 | | |
14971 | | // Enum types are implicitly signed on Windows, so check if there are any |
14972 | | // negative enumerators to see if the enum was intended to be signed or |
14973 | | // not. |
14974 | 0 | bool SignedEnum = ED->getNumNegativeBits() > 0; |
14975 | | |
14976 | | // Check for surprising sign changes when assigning enum values to a |
14977 | | // bitfield of different signedness. If the bitfield is signed and we |
14978 | | // have exactly the right number of bits to store this unsigned enum, |
14979 | | // suggest changing the enum to an unsigned type. This typically happens |
14980 | | // on Windows where unfixed enums always use an underlying type of 'int'. |
14981 | 0 | unsigned DiagID = 0; |
14982 | 0 | if (SignedEnum && !SignedBitfield) { |
14983 | 0 | DiagID = diag::warn_unsigned_bitfield_assigned_signed_enum; |
14984 | 0 | } else if (SignedBitfield && !SignedEnum && |
14985 | 0 | ED->getNumPositiveBits() == FieldWidth) { |
14986 | 0 | DiagID = diag::warn_signed_bitfield_enum_conversion; |
14987 | 0 | } |
14988 | |
|
14989 | 0 | if (DiagID) { |
14990 | 0 | S.Diag(InitLoc, DiagID) << Bitfield << ED; |
14991 | 0 | TypeSourceInfo *TSI = Bitfield->getTypeSourceInfo(); |
14992 | 0 | SourceRange TypeRange = |
14993 | 0 | TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange(); |
14994 | 0 | S.Diag(Bitfield->getTypeSpecStartLoc(), diag::note_change_bitfield_sign) |
14995 | 0 | << SignedEnum << TypeRange; |
14996 | 0 | } |
14997 | | |
14998 | | // Compute the required bitwidth. If the enum has negative values, we need |
14999 | | // one more bit than the normal number of positive bits to represent the |
15000 | | // sign bit. |
15001 | 0 | unsigned BitsNeeded = SignedEnum ? std::max(ED->getNumPositiveBits() + 1, |
15002 | 0 | ED->getNumNegativeBits()) |
15003 | 0 | : ED->getNumPositiveBits(); |
15004 | | |
15005 | | // Check the bitwidth. |
15006 | 0 | if (BitsNeeded > FieldWidth) { |
15007 | 0 | Expr *WidthExpr = Bitfield->getBitWidth(); |
15008 | 0 | S.Diag(InitLoc, diag::warn_bitfield_too_small_for_enum) |
15009 | 0 | << Bitfield << ED; |
15010 | 0 | S.Diag(WidthExpr->getExprLoc(), diag::note_widen_bitfield) |
15011 | 0 | << BitsNeeded << ED << WidthExpr->getSourceRange(); |
15012 | 0 | } |
15013 | 0 | } |
15014 | |
|
15015 | 0 | return false; |
15016 | 0 | } |
15017 | | |
15018 | 0 | llvm::APSInt Value = Result.Val.getInt(); |
15019 | |
|
15020 | 0 | unsigned OriginalWidth = Value.getBitWidth(); |
15021 | | |
15022 | | // In C, the macro 'true' from stdbool.h will evaluate to '1'; To reduce |
15023 | | // false positives where the user is demonstrating they intend to use the |
15024 | | // bit-field as a Boolean, check to see if the value is 1 and we're assigning |
15025 | | // to a one-bit bit-field to see if the value came from a macro named 'true'. |
15026 | 0 | bool OneAssignedToOneBitBitfield = FieldWidth == 1 && Value == 1; |
15027 | 0 | if (OneAssignedToOneBitBitfield && !S.LangOpts.CPlusPlus) { |
15028 | 0 | SourceLocation MaybeMacroLoc = OriginalInit->getBeginLoc(); |
15029 | 0 | if (S.SourceMgr.isInSystemMacro(MaybeMacroLoc) && |
15030 | 0 | S.findMacroSpelling(MaybeMacroLoc, "true")) |
15031 | 0 | return false; |
15032 | 0 | } |
15033 | | |
15034 | 0 | if (!Value.isSigned() || Value.isNegative()) |
15035 | 0 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(OriginalInit)) |
15036 | 0 | if (UO->getOpcode() == UO_Minus || UO->getOpcode() == UO_Not) |
15037 | 0 | OriginalWidth = Value.getSignificantBits(); |
15038 | |
|
15039 | 0 | if (OriginalWidth <= FieldWidth) |
15040 | 0 | return false; |
15041 | | |
15042 | | // Compute the value which the bitfield will contain. |
15043 | 0 | llvm::APSInt TruncatedValue = Value.trunc(FieldWidth); |
15044 | 0 | TruncatedValue.setIsSigned(BitfieldType->isSignedIntegerType()); |
15045 | | |
15046 | | // Check whether the stored value is equal to the original value. |
15047 | 0 | TruncatedValue = TruncatedValue.extend(OriginalWidth); |
15048 | 0 | if (llvm::APSInt::isSameValue(Value, TruncatedValue)) |
15049 | 0 | return false; |
15050 | | |
15051 | 0 | std::string PrettyValue = toString(Value, 10); |
15052 | 0 | std::string PrettyTrunc = toString(TruncatedValue, 10); |
15053 | |
|
15054 | 0 | S.Diag(InitLoc, OneAssignedToOneBitBitfield |
15055 | 0 | ? diag::warn_impcast_single_bit_bitield_precision_constant |
15056 | 0 | : diag::warn_impcast_bitfield_precision_constant) |
15057 | 0 | << PrettyValue << PrettyTrunc << OriginalInit->getType() |
15058 | 0 | << Init->getSourceRange(); |
15059 | |
|
15060 | 0 | return true; |
15061 | 0 | } |
15062 | | |
15063 | | /// Analyze the given simple or compound assignment for warning-worthy |
15064 | | /// operations. |
15065 | 0 | static void AnalyzeAssignment(Sema &S, BinaryOperator *E) { |
15066 | | // Just recurse on the LHS. |
15067 | 0 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
15068 | | |
15069 | | // We want to recurse on the RHS as normal unless we're assigning to |
15070 | | // a bitfield. |
15071 | 0 | if (FieldDecl *Bitfield = E->getLHS()->getSourceBitField()) { |
15072 | 0 | if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(), |
15073 | 0 | E->getOperatorLoc())) { |
15074 | | // Recurse, ignoring any implicit conversions on the RHS. |
15075 | 0 | return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(), |
15076 | 0 | E->getOperatorLoc()); |
15077 | 0 | } |
15078 | 0 | } |
15079 | | |
15080 | 0 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
15081 | | |
15082 | | // Diagnose implicitly sequentially-consistent atomic assignment. |
15083 | 0 | if (E->getLHS()->getType()->isAtomicType()) |
15084 | 0 | S.Diag(E->getRHS()->getBeginLoc(), diag::warn_atomic_implicit_seq_cst); |
15085 | 0 | } |
15086 | | |
15087 | | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
15088 | | static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, |
15089 | | SourceLocation CContext, unsigned diag, |
15090 | 0 | bool pruneControlFlow = false) { |
15091 | 0 | if (pruneControlFlow) { |
15092 | 0 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
15093 | 0 | S.PDiag(diag) |
15094 | 0 | << SourceType << T << E->getSourceRange() |
15095 | 0 | << SourceRange(CContext)); |
15096 | 0 | return; |
15097 | 0 | } |
15098 | 0 | S.Diag(E->getExprLoc(), diag) |
15099 | 0 | << SourceType << T << E->getSourceRange() << SourceRange(CContext); |
15100 | 0 | } |
15101 | | |
15102 | | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
15103 | | static void DiagnoseImpCast(Sema &S, Expr *E, QualType T, |
15104 | | SourceLocation CContext, |
15105 | 0 | unsigned diag, bool pruneControlFlow = false) { |
15106 | 0 | DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow); |
15107 | 0 | } |
15108 | | |
15109 | 1 | static bool isObjCSignedCharBool(Sema &S, QualType Ty) { |
15110 | 1 | return Ty->isSpecificBuiltinType(BuiltinType::SChar) && |
15111 | 1 | S.getLangOpts().ObjC && S.NSAPIObj->isObjCBOOLType(Ty); |
15112 | 1 | } |
15113 | | |
15114 | | static void adornObjCBoolConversionDiagWithTernaryFixit( |
15115 | 0 | Sema &S, Expr *SourceExpr, const Sema::SemaDiagnosticBuilder &Builder) { |
15116 | 0 | Expr *Ignored = SourceExpr->IgnoreImplicit(); |
15117 | 0 | if (const auto *OVE = dyn_cast<OpaqueValueExpr>(Ignored)) |
15118 | 0 | Ignored = OVE->getSourceExpr(); |
15119 | 0 | bool NeedsParens = isa<AbstractConditionalOperator>(Ignored) || |
15120 | 0 | isa<BinaryOperator>(Ignored) || |
15121 | 0 | isa<CXXOperatorCallExpr>(Ignored); |
15122 | 0 | SourceLocation EndLoc = S.getLocForEndOfToken(SourceExpr->getEndLoc()); |
15123 | 0 | if (NeedsParens) |
15124 | 0 | Builder << FixItHint::CreateInsertion(SourceExpr->getBeginLoc(), "(") |
15125 | 0 | << FixItHint::CreateInsertion(EndLoc, ")"); |
15126 | 0 | Builder << FixItHint::CreateInsertion(EndLoc, " ? YES : NO"); |
15127 | 0 | } |
15128 | | |
15129 | | /// Diagnose an implicit cast from a floating point value to an integer value. |
15130 | | static void DiagnoseFloatingImpCast(Sema &S, Expr *E, QualType T, |
15131 | 0 | SourceLocation CContext) { |
15132 | 0 | const bool IsBool = T->isSpecificBuiltinType(BuiltinType::Bool); |
15133 | 0 | const bool PruneWarnings = S.inTemplateInstantiation(); |
15134 | |
|
15135 | 0 | Expr *InnerE = E->IgnoreParenImpCasts(); |
15136 | | // We also want to warn on, e.g., "int i = -1.234" |
15137 | 0 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE)) |
15138 | 0 | if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus) |
15139 | 0 | InnerE = UOp->getSubExpr()->IgnoreParenImpCasts(); |
15140 | |
|
15141 | 0 | const bool IsLiteral = |
15142 | 0 | isa<FloatingLiteral>(E) || isa<FloatingLiteral>(InnerE); |
15143 | |
|
15144 | 0 | llvm::APFloat Value(0.0); |
15145 | 0 | bool IsConstant = |
15146 | 0 | E->EvaluateAsFloat(Value, S.Context, Expr::SE_AllowSideEffects); |
15147 | 0 | if (!IsConstant) { |
15148 | 0 | if (isObjCSignedCharBool(S, T)) { |
15149 | 0 | return adornObjCBoolConversionDiagWithTernaryFixit( |
15150 | 0 | S, E, |
15151 | 0 | S.Diag(CContext, diag::warn_impcast_float_to_objc_signed_char_bool) |
15152 | 0 | << E->getType()); |
15153 | 0 | } |
15154 | | |
15155 | 0 | return DiagnoseImpCast(S, E, T, CContext, |
15156 | 0 | diag::warn_impcast_float_integer, PruneWarnings); |
15157 | 0 | } |
15158 | | |
15159 | 0 | bool isExact = false; |
15160 | |
|
15161 | 0 | llvm::APSInt IntegerValue(S.Context.getIntWidth(T), |
15162 | 0 | T->hasUnsignedIntegerRepresentation()); |
15163 | 0 | llvm::APFloat::opStatus Result = Value.convertToInteger( |
15164 | 0 | IntegerValue, llvm::APFloat::rmTowardZero, &isExact); |
15165 | | |
15166 | | // FIXME: Force the precision of the source value down so we don't print |
15167 | | // digits which are usually useless (we don't really care here if we |
15168 | | // truncate a digit by accident in edge cases). Ideally, APFloat::toString |
15169 | | // would automatically print the shortest representation, but it's a bit |
15170 | | // tricky to implement. |
15171 | 0 | SmallString<16> PrettySourceValue; |
15172 | 0 | unsigned precision = llvm::APFloat::semanticsPrecision(Value.getSemantics()); |
15173 | 0 | precision = (precision * 59 + 195) / 196; |
15174 | 0 | Value.toString(PrettySourceValue, precision); |
15175 | |
|
15176 | 0 | if (isObjCSignedCharBool(S, T) && IntegerValue != 0 && IntegerValue != 1) { |
15177 | 0 | return adornObjCBoolConversionDiagWithTernaryFixit( |
15178 | 0 | S, E, |
15179 | 0 | S.Diag(CContext, diag::warn_impcast_constant_value_to_objc_bool) |
15180 | 0 | << PrettySourceValue); |
15181 | 0 | } |
15182 | | |
15183 | 0 | if (Result == llvm::APFloat::opOK && isExact) { |
15184 | 0 | if (IsLiteral) return; |
15185 | 0 | return DiagnoseImpCast(S, E, T, CContext, diag::warn_impcast_float_integer, |
15186 | 0 | PruneWarnings); |
15187 | 0 | } |
15188 | | |
15189 | | // Conversion of a floating-point value to a non-bool integer where the |
15190 | | // integral part cannot be represented by the integer type is undefined. |
15191 | 0 | if (!IsBool && Result == llvm::APFloat::opInvalidOp) |
15192 | 0 | return DiagnoseImpCast( |
15193 | 0 | S, E, T, CContext, |
15194 | 0 | IsLiteral ? diag::warn_impcast_literal_float_to_integer_out_of_range |
15195 | 0 | : diag::warn_impcast_float_to_integer_out_of_range, |
15196 | 0 | PruneWarnings); |
15197 | | |
15198 | 0 | unsigned DiagID = 0; |
15199 | 0 | if (IsLiteral) { |
15200 | | // Warn on floating point literal to integer. |
15201 | 0 | DiagID = diag::warn_impcast_literal_float_to_integer; |
15202 | 0 | } else if (IntegerValue == 0) { |
15203 | 0 | if (Value.isZero()) { // Skip -0.0 to 0 conversion. |
15204 | 0 | return DiagnoseImpCast(S, E, T, CContext, |
15205 | 0 | diag::warn_impcast_float_integer, PruneWarnings); |
15206 | 0 | } |
15207 | | // Warn on non-zero to zero conversion. |
15208 | 0 | DiagID = diag::warn_impcast_float_to_integer_zero; |
15209 | 0 | } else { |
15210 | 0 | if (IntegerValue.isUnsigned()) { |
15211 | 0 | if (!IntegerValue.isMaxValue()) { |
15212 | 0 | return DiagnoseImpCast(S, E, T, CContext, |
15213 | 0 | diag::warn_impcast_float_integer, PruneWarnings); |
15214 | 0 | } |
15215 | 0 | } else { // IntegerValue.isSigned() |
15216 | 0 | if (!IntegerValue.isMaxSignedValue() && |
15217 | 0 | !IntegerValue.isMinSignedValue()) { |
15218 | 0 | return DiagnoseImpCast(S, E, T, CContext, |
15219 | 0 | diag::warn_impcast_float_integer, PruneWarnings); |
15220 | 0 | } |
15221 | 0 | } |
15222 | | // Warn on evaluatable floating point expression to integer conversion. |
15223 | 0 | DiagID = diag::warn_impcast_float_to_integer; |
15224 | 0 | } |
15225 | | |
15226 | 0 | SmallString<16> PrettyTargetValue; |
15227 | 0 | if (IsBool) |
15228 | 0 | PrettyTargetValue = Value.isZero() ? "false" : "true"; |
15229 | 0 | else |
15230 | 0 | IntegerValue.toString(PrettyTargetValue); |
15231 | |
|
15232 | 0 | if (PruneWarnings) { |
15233 | 0 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
15234 | 0 | S.PDiag(DiagID) |
15235 | 0 | << E->getType() << T.getUnqualifiedType() |
15236 | 0 | << PrettySourceValue << PrettyTargetValue |
15237 | 0 | << E->getSourceRange() << SourceRange(CContext)); |
15238 | 0 | } else { |
15239 | 0 | S.Diag(E->getExprLoc(), DiagID) |
15240 | 0 | << E->getType() << T.getUnqualifiedType() << PrettySourceValue |
15241 | 0 | << PrettyTargetValue << E->getSourceRange() << SourceRange(CContext); |
15242 | 0 | } |
15243 | 0 | } |
15244 | | |
15245 | | /// Analyze the given compound assignment for the possible losing of |
15246 | | /// floating-point precision. |
15247 | 0 | static void AnalyzeCompoundAssignment(Sema &S, BinaryOperator *E) { |
15248 | 0 | assert(isa<CompoundAssignOperator>(E) && |
15249 | 0 | "Must be compound assignment operation"); |
15250 | | // Recurse on the LHS and RHS in here |
15251 | 0 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
15252 | 0 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
15253 | |
|
15254 | 0 | if (E->getLHS()->getType()->isAtomicType()) |
15255 | 0 | S.Diag(E->getOperatorLoc(), diag::warn_atomic_implicit_seq_cst); |
15256 | | |
15257 | | // Now check the outermost expression |
15258 | 0 | const auto *ResultBT = E->getLHS()->getType()->getAs<BuiltinType>(); |
15259 | 0 | const auto *RBT = cast<CompoundAssignOperator>(E) |
15260 | 0 | ->getComputationResultType() |
15261 | 0 | ->getAs<BuiltinType>(); |
15262 | | |
15263 | | // The below checks assume source is floating point. |
15264 | 0 | if (!ResultBT || !RBT || !RBT->isFloatingPoint()) return; |
15265 | | |
15266 | | // If source is floating point but target is an integer. |
15267 | 0 | if (ResultBT->isInteger()) |
15268 | 0 | return DiagnoseImpCast(S, E, E->getRHS()->getType(), E->getLHS()->getType(), |
15269 | 0 | E->getExprLoc(), diag::warn_impcast_float_integer); |
15270 | | |
15271 | 0 | if (!ResultBT->isFloatingPoint()) |
15272 | 0 | return; |
15273 | | |
15274 | | // If both source and target are floating points, warn about losing precision. |
15275 | 0 | int Order = S.getASTContext().getFloatingTypeSemanticOrder( |
15276 | 0 | QualType(ResultBT, 0), QualType(RBT, 0)); |
15277 | 0 | if (Order < 0 && !S.SourceMgr.isInSystemMacro(E->getOperatorLoc())) |
15278 | | // warn about dropping FP rank. |
15279 | 0 | DiagnoseImpCast(S, E->getRHS(), E->getLHS()->getType(), E->getOperatorLoc(), |
15280 | 0 | diag::warn_impcast_float_result_precision); |
15281 | 0 | } |
15282 | | |
15283 | | static std::string PrettyPrintInRange(const llvm::APSInt &Value, |
15284 | 0 | IntRange Range) { |
15285 | 0 | if (!Range.Width) return "0"; |
15286 | | |
15287 | 0 | llvm::APSInt ValueInRange = Value; |
15288 | 0 | ValueInRange.setIsSigned(!Range.NonNegative); |
15289 | 0 | ValueInRange = ValueInRange.trunc(Range.Width); |
15290 | 0 | return toString(ValueInRange, 10); |
15291 | 0 | } |
15292 | | |
15293 | 0 | static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) { |
15294 | 0 | if (!isa<ImplicitCastExpr>(Ex)) |
15295 | 0 | return false; |
15296 | | |
15297 | 0 | Expr *InnerE = Ex->IgnoreParenImpCasts(); |
15298 | 0 | const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr(); |
15299 | 0 | const Type *Source = |
15300 | 0 | S.Context.getCanonicalType(InnerE->getType()).getTypePtr(); |
15301 | 0 | if (Target->isDependentType()) |
15302 | 0 | return false; |
15303 | | |
15304 | 0 | const BuiltinType *FloatCandidateBT = |
15305 | 0 | dyn_cast<BuiltinType>(ToBool ? Source : Target); |
15306 | 0 | const Type *BoolCandidateType = ToBool ? Target : Source; |
15307 | |
|
15308 | 0 | return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) && |
15309 | 0 | FloatCandidateBT && (FloatCandidateBT->isFloatingPoint())); |
15310 | 0 | } |
15311 | | |
15312 | | static void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall, |
15313 | 0 | SourceLocation CC) { |
15314 | 0 | unsigned NumArgs = TheCall->getNumArgs(); |
15315 | 0 | for (unsigned i = 0; i < NumArgs; ++i) { |
15316 | 0 | Expr *CurrA = TheCall->getArg(i); |
15317 | 0 | if (!IsImplicitBoolFloatConversion(S, CurrA, true)) |
15318 | 0 | continue; |
15319 | | |
15320 | 0 | bool IsSwapped = ((i > 0) && |
15321 | 0 | IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false)); |
15322 | 0 | IsSwapped |= ((i < (NumArgs - 1)) && |
15323 | 0 | IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false)); |
15324 | 0 | if (IsSwapped) { |
15325 | | // Warn on this floating-point to bool conversion. |
15326 | 0 | DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(), |
15327 | 0 | CurrA->getType(), CC, |
15328 | 0 | diag::warn_impcast_floating_point_to_bool); |
15329 | 0 | } |
15330 | 0 | } |
15331 | 0 | } |
15332 | | |
15333 | | static void DiagnoseNullConversion(Sema &S, Expr *E, QualType T, |
15334 | 1 | SourceLocation CC) { |
15335 | 1 | if (S.Diags.isIgnored(diag::warn_impcast_null_pointer_to_integer, |
15336 | 1 | E->getExprLoc())) |
15337 | 0 | return; |
15338 | | |
15339 | | // Don't warn on functions which have return type nullptr_t. |
15340 | 1 | if (isa<CallExpr>(E)) |
15341 | 0 | return; |
15342 | | |
15343 | | // Check for NULL (GNUNull) or nullptr (CXX11_nullptr). |
15344 | 1 | const Expr *NewE = E->IgnoreParenImpCasts(); |
15345 | 1 | bool IsGNUNullExpr = isa<GNUNullExpr>(NewE); |
15346 | 1 | bool HasNullPtrType = NewE->getType()->isNullPtrType(); |
15347 | 1 | if (!IsGNUNullExpr && !HasNullPtrType) |
15348 | 1 | return; |
15349 | | |
15350 | | // Return if target type is a safe conversion. |
15351 | 0 | if (T->isAnyPointerType() || T->isBlockPointerType() || |
15352 | 0 | T->isMemberPointerType() || !T->isScalarType() || T->isNullPtrType()) |
15353 | 0 | return; |
15354 | | |
15355 | 0 | SourceLocation Loc = E->getSourceRange().getBegin(); |
15356 | | |
15357 | | // Venture through the macro stacks to get to the source of macro arguments. |
15358 | | // The new location is a better location than the complete location that was |
15359 | | // passed in. |
15360 | 0 | Loc = S.SourceMgr.getTopMacroCallerLoc(Loc); |
15361 | 0 | CC = S.SourceMgr.getTopMacroCallerLoc(CC); |
15362 | | |
15363 | | // __null is usually wrapped in a macro. Go up a macro if that is the case. |
15364 | 0 | if (IsGNUNullExpr && Loc.isMacroID()) { |
15365 | 0 | StringRef MacroName = Lexer::getImmediateMacroNameForDiagnostics( |
15366 | 0 | Loc, S.SourceMgr, S.getLangOpts()); |
15367 | 0 | if (MacroName == "NULL") |
15368 | 0 | Loc = S.SourceMgr.getImmediateExpansionRange(Loc).getBegin(); |
15369 | 0 | } |
15370 | | |
15371 | | // Only warn if the null and context location are in the same macro expansion. |
15372 | 0 | if (S.SourceMgr.getFileID(Loc) != S.SourceMgr.getFileID(CC)) |
15373 | 0 | return; |
15374 | | |
15375 | 0 | S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer) |
15376 | 0 | << HasNullPtrType << T << SourceRange(CC) |
15377 | 0 | << FixItHint::CreateReplacement(Loc, |
15378 | 0 | S.getFixItZeroLiteralForType(T, Loc)); |
15379 | 0 | } |
15380 | | |
15381 | | static void checkObjCArrayLiteral(Sema &S, QualType TargetType, |
15382 | | ObjCArrayLiteral *ArrayLiteral); |
15383 | | |
15384 | | static void |
15385 | | checkObjCDictionaryLiteral(Sema &S, QualType TargetType, |
15386 | | ObjCDictionaryLiteral *DictionaryLiteral); |
15387 | | |
15388 | | /// Check a single element within a collection literal against the |
15389 | | /// target element type. |
15390 | | static void checkObjCCollectionLiteralElement(Sema &S, |
15391 | | QualType TargetElementType, |
15392 | | Expr *Element, |
15393 | 0 | unsigned ElementKind) { |
15394 | | // Skip a bitcast to 'id' or qualified 'id'. |
15395 | 0 | if (auto ICE = dyn_cast<ImplicitCastExpr>(Element)) { |
15396 | 0 | if (ICE->getCastKind() == CK_BitCast && |
15397 | 0 | ICE->getSubExpr()->getType()->getAs<ObjCObjectPointerType>()) |
15398 | 0 | Element = ICE->getSubExpr(); |
15399 | 0 | } |
15400 | |
|
15401 | 0 | QualType ElementType = Element->getType(); |
15402 | 0 | ExprResult ElementResult(Element); |
15403 | 0 | if (ElementType->getAs<ObjCObjectPointerType>() && |
15404 | 0 | S.CheckSingleAssignmentConstraints(TargetElementType, |
15405 | 0 | ElementResult, |
15406 | 0 | false, false) |
15407 | 0 | != Sema::Compatible) { |
15408 | 0 | S.Diag(Element->getBeginLoc(), diag::warn_objc_collection_literal_element) |
15409 | 0 | << ElementType << ElementKind << TargetElementType |
15410 | 0 | << Element->getSourceRange(); |
15411 | 0 | } |
15412 | |
|
15413 | 0 | if (auto ArrayLiteral = dyn_cast<ObjCArrayLiteral>(Element)) |
15414 | 0 | checkObjCArrayLiteral(S, TargetElementType, ArrayLiteral); |
15415 | 0 | else if (auto DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(Element)) |
15416 | 0 | checkObjCDictionaryLiteral(S, TargetElementType, DictionaryLiteral); |
15417 | 0 | } |
15418 | | |
15419 | | /// Check an Objective-C array literal being converted to the given |
15420 | | /// target type. |
15421 | | static void checkObjCArrayLiteral(Sema &S, QualType TargetType, |
15422 | 0 | ObjCArrayLiteral *ArrayLiteral) { |
15423 | 0 | if (!S.NSArrayDecl) |
15424 | 0 | return; |
15425 | | |
15426 | 0 | const auto *TargetObjCPtr = TargetType->getAs<ObjCObjectPointerType>(); |
15427 | 0 | if (!TargetObjCPtr) |
15428 | 0 | return; |
15429 | | |
15430 | 0 | if (TargetObjCPtr->isUnspecialized() || |
15431 | 0 | TargetObjCPtr->getInterfaceDecl()->getCanonicalDecl() |
15432 | 0 | != S.NSArrayDecl->getCanonicalDecl()) |
15433 | 0 | return; |
15434 | | |
15435 | 0 | auto TypeArgs = TargetObjCPtr->getTypeArgs(); |
15436 | 0 | if (TypeArgs.size() != 1) |
15437 | 0 | return; |
15438 | | |
15439 | 0 | QualType TargetElementType = TypeArgs[0]; |
15440 | 0 | for (unsigned I = 0, N = ArrayLiteral->getNumElements(); I != N; ++I) { |
15441 | 0 | checkObjCCollectionLiteralElement(S, TargetElementType, |
15442 | 0 | ArrayLiteral->getElement(I), |
15443 | 0 | 0); |
15444 | 0 | } |
15445 | 0 | } |
15446 | | |
15447 | | /// Check an Objective-C dictionary literal being converted to the given |
15448 | | /// target type. |
15449 | | static void |
15450 | | checkObjCDictionaryLiteral(Sema &S, QualType TargetType, |
15451 | 0 | ObjCDictionaryLiteral *DictionaryLiteral) { |
15452 | 0 | if (!S.NSDictionaryDecl) |
15453 | 0 | return; |
15454 | | |
15455 | 0 | const auto *TargetObjCPtr = TargetType->getAs<ObjCObjectPointerType>(); |
15456 | 0 | if (!TargetObjCPtr) |
15457 | 0 | return; |
15458 | | |
15459 | 0 | if (TargetObjCPtr->isUnspecialized() || |
15460 | 0 | TargetObjCPtr->getInterfaceDecl()->getCanonicalDecl() |
15461 | 0 | != S.NSDictionaryDecl->getCanonicalDecl()) |
15462 | 0 | return; |
15463 | | |
15464 | 0 | auto TypeArgs = TargetObjCPtr->getTypeArgs(); |
15465 | 0 | if (TypeArgs.size() != 2) |
15466 | 0 | return; |
15467 | | |
15468 | 0 | QualType TargetKeyType = TypeArgs[0]; |
15469 | 0 | QualType TargetObjectType = TypeArgs[1]; |
15470 | 0 | for (unsigned I = 0, N = DictionaryLiteral->getNumElements(); I != N; ++I) { |
15471 | 0 | auto Element = DictionaryLiteral->getKeyValueElement(I); |
15472 | 0 | checkObjCCollectionLiteralElement(S, TargetKeyType, Element.Key, 1); |
15473 | 0 | checkObjCCollectionLiteralElement(S, TargetObjectType, Element.Value, 2); |
15474 | 0 | } |
15475 | 0 | } |
15476 | | |
15477 | | // Helper function to filter out cases for constant width constant conversion. |
15478 | | // Don't warn on char array initialization or for non-decimal values. |
15479 | | static bool isSameWidthConstantConversion(Sema &S, Expr *E, QualType T, |
15480 | 0 | SourceLocation CC) { |
15481 | | // If initializing from a constant, and the constant starts with '0', |
15482 | | // then it is a binary, octal, or hexadecimal. Allow these constants |
15483 | | // to fill all the bits, even if there is a sign change. |
15484 | 0 | if (auto *IntLit = dyn_cast<IntegerLiteral>(E->IgnoreParenImpCasts())) { |
15485 | 0 | const char FirstLiteralCharacter = |
15486 | 0 | S.getSourceManager().getCharacterData(IntLit->getBeginLoc())[0]; |
15487 | 0 | if (FirstLiteralCharacter == '0') |
15488 | 0 | return false; |
15489 | 0 | } |
15490 | | |
15491 | | // If the CC location points to a '{', and the type is char, then assume |
15492 | | // assume it is an array initialization. |
15493 | 0 | if (CC.isValid() && T->isCharType()) { |
15494 | 0 | const char FirstContextCharacter = |
15495 | 0 | S.getSourceManager().getCharacterData(CC)[0]; |
15496 | 0 | if (FirstContextCharacter == '{') |
15497 | 0 | return false; |
15498 | 0 | } |
15499 | | |
15500 | 0 | return true; |
15501 | 0 | } |
15502 | | |
15503 | 0 | static const IntegerLiteral *getIntegerLiteral(Expr *E) { |
15504 | 0 | const auto *IL = dyn_cast<IntegerLiteral>(E); |
15505 | 0 | if (!IL) { |
15506 | 0 | if (auto *UO = dyn_cast<UnaryOperator>(E)) { |
15507 | 0 | if (UO->getOpcode() == UO_Minus) |
15508 | 0 | return dyn_cast<IntegerLiteral>(UO->getSubExpr()); |
15509 | 0 | } |
15510 | 0 | } |
15511 | | |
15512 | 0 | return IL; |
15513 | 0 | } |
15514 | | |
15515 | 0 | static void DiagnoseIntInBoolContext(Sema &S, Expr *E) { |
15516 | 0 | E = E->IgnoreParenImpCasts(); |
15517 | 0 | SourceLocation ExprLoc = E->getExprLoc(); |
15518 | |
|
15519 | 0 | if (const auto *BO = dyn_cast<BinaryOperator>(E)) { |
15520 | 0 | BinaryOperator::Opcode Opc = BO->getOpcode(); |
15521 | 0 | Expr::EvalResult Result; |
15522 | | // Do not diagnose unsigned shifts. |
15523 | 0 | if (Opc == BO_Shl) { |
15524 | 0 | const auto *LHS = getIntegerLiteral(BO->getLHS()); |
15525 | 0 | const auto *RHS = getIntegerLiteral(BO->getRHS()); |
15526 | 0 | if (LHS && LHS->getValue() == 0) |
15527 | 0 | S.Diag(ExprLoc, diag::warn_left_shift_always) << 0; |
15528 | 0 | else if (!E->isValueDependent() && LHS && RHS && |
15529 | 0 | RHS->getValue().isNonNegative() && |
15530 | 0 | E->EvaluateAsInt(Result, S.Context, Expr::SE_AllowSideEffects)) |
15531 | 0 | S.Diag(ExprLoc, diag::warn_left_shift_always) |
15532 | 0 | << (Result.Val.getInt() != 0); |
15533 | 0 | else if (E->getType()->isSignedIntegerType()) |
15534 | 0 | S.Diag(ExprLoc, diag::warn_left_shift_in_bool_context) << E; |
15535 | 0 | } |
15536 | 0 | } |
15537 | |
|
15538 | 0 | if (const auto *CO = dyn_cast<ConditionalOperator>(E)) { |
15539 | 0 | const auto *LHS = getIntegerLiteral(CO->getTrueExpr()); |
15540 | 0 | const auto *RHS = getIntegerLiteral(CO->getFalseExpr()); |
15541 | 0 | if (!LHS || !RHS) |
15542 | 0 | return; |
15543 | 0 | if ((LHS->getValue() == 0 || LHS->getValue() == 1) && |
15544 | 0 | (RHS->getValue() == 0 || RHS->getValue() == 1)) |
15545 | | // Do not diagnose common idioms. |
15546 | 0 | return; |
15547 | 0 | if (LHS->getValue() != 0 && RHS->getValue() != 0) |
15548 | 0 | S.Diag(ExprLoc, diag::warn_integer_constants_in_conditional_always_true); |
15549 | 0 | } |
15550 | 0 | } |
15551 | | |
15552 | | static void CheckImplicitConversion(Sema &S, Expr *E, QualType T, |
15553 | | SourceLocation CC, |
15554 | | bool *ICContext = nullptr, |
15555 | 1 | bool IsListInit = false) { |
15556 | 1 | if (E->isTypeDependent() || E->isValueDependent()) return; |
15557 | | |
15558 | 1 | const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr(); |
15559 | 1 | const Type *Target = S.Context.getCanonicalType(T).getTypePtr(); |
15560 | 1 | if (Source == Target) return; |
15561 | 1 | if (Target->isDependentType()) return; |
15562 | | |
15563 | | // If the conversion context location is invalid don't complain. We also |
15564 | | // don't want to emit a warning if the issue occurs from the expansion of |
15565 | | // a system macro. The problem is that 'getSpellingLoc()' is slow, so we |
15566 | | // delay this check as long as possible. Once we detect we are in that |
15567 | | // scenario, we just return. |
15568 | 1 | if (CC.isInvalid()) |
15569 | 0 | return; |
15570 | | |
15571 | 1 | if (Source->isAtomicType()) |
15572 | 0 | S.Diag(E->getExprLoc(), diag::warn_atomic_implicit_seq_cst); |
15573 | | |
15574 | | // Diagnose implicit casts to bool. |
15575 | 1 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) { |
15576 | 0 | if (isa<StringLiteral>(E)) |
15577 | | // Warn on string literal to bool. Checks for string literals in logical |
15578 | | // and expressions, for instance, assert(0 && "error here"), are |
15579 | | // prevented by a check in AnalyzeImplicitConversions(). |
15580 | 0 | return DiagnoseImpCast(S, E, T, CC, |
15581 | 0 | diag::warn_impcast_string_literal_to_bool); |
15582 | 0 | if (isa<ObjCStringLiteral>(E) || isa<ObjCArrayLiteral>(E) || |
15583 | 0 | isa<ObjCDictionaryLiteral>(E) || isa<ObjCBoxedExpr>(E)) { |
15584 | | // This covers the literal expressions that evaluate to Objective-C |
15585 | | // objects. |
15586 | 0 | return DiagnoseImpCast(S, E, T, CC, |
15587 | 0 | diag::warn_impcast_objective_c_literal_to_bool); |
15588 | 0 | } |
15589 | 0 | if (Source->isPointerType() || Source->canDecayToPointerType()) { |
15590 | | // Warn on pointer to bool conversion that is always true. |
15591 | 0 | S.DiagnoseAlwaysNonNullPointer(E, Expr::NPCK_NotNull, /*IsEqual*/ false, |
15592 | 0 | SourceRange(CC)); |
15593 | 0 | } |
15594 | 0 | } |
15595 | | |
15596 | | // If the we're converting a constant to an ObjC BOOL on a platform where BOOL |
15597 | | // is a typedef for signed char (macOS), then that constant value has to be 1 |
15598 | | // or 0. |
15599 | 1 | if (isObjCSignedCharBool(S, T) && Source->isIntegralType(S.Context)) { |
15600 | 0 | Expr::EvalResult Result; |
15601 | 0 | if (E->EvaluateAsInt(Result, S.getASTContext(), |
15602 | 0 | Expr::SE_AllowSideEffects)) { |
15603 | 0 | if (Result.Val.getInt() != 1 && Result.Val.getInt() != 0) { |
15604 | 0 | adornObjCBoolConversionDiagWithTernaryFixit( |
15605 | 0 | S, E, |
15606 | 0 | S.Diag(CC, diag::warn_impcast_constant_value_to_objc_bool) |
15607 | 0 | << toString(Result.Val.getInt(), 10)); |
15608 | 0 | } |
15609 | 0 | return; |
15610 | 0 | } |
15611 | 0 | } |
15612 | | |
15613 | | // Check implicit casts from Objective-C collection literals to specialized |
15614 | | // collection types, e.g., NSArray<NSString *> *. |
15615 | 1 | if (auto *ArrayLiteral = dyn_cast<ObjCArrayLiteral>(E)) |
15616 | 0 | checkObjCArrayLiteral(S, QualType(Target, 0), ArrayLiteral); |
15617 | 1 | else if (auto *DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(E)) |
15618 | 0 | checkObjCDictionaryLiteral(S, QualType(Target, 0), DictionaryLiteral); |
15619 | | |
15620 | | // Strip vector types. |
15621 | 1 | if (isa<VectorType>(Source)) { |
15622 | 0 | if (Target->isSveVLSBuiltinType() && |
15623 | 0 | (S.Context.areCompatibleSveTypes(QualType(Target, 0), |
15624 | 0 | QualType(Source, 0)) || |
15625 | 0 | S.Context.areLaxCompatibleSveTypes(QualType(Target, 0), |
15626 | 0 | QualType(Source, 0)))) |
15627 | 0 | return; |
15628 | | |
15629 | 0 | if (Target->isRVVVLSBuiltinType() && |
15630 | 0 | (S.Context.areCompatibleRVVTypes(QualType(Target, 0), |
15631 | 0 | QualType(Source, 0)) || |
15632 | 0 | S.Context.areLaxCompatibleRVVTypes(QualType(Target, 0), |
15633 | 0 | QualType(Source, 0)))) |
15634 | 0 | return; |
15635 | | |
15636 | 0 | if (!isa<VectorType>(Target)) { |
15637 | 0 | if (S.SourceMgr.isInSystemMacro(CC)) |
15638 | 0 | return; |
15639 | 0 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar); |
15640 | 0 | } |
15641 | | |
15642 | | // If the vector cast is cast between two vectors of the same size, it is |
15643 | | // a bitcast, not a conversion. |
15644 | 0 | if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target)) |
15645 | 0 | return; |
15646 | | |
15647 | 0 | Source = cast<VectorType>(Source)->getElementType().getTypePtr(); |
15648 | 0 | Target = cast<VectorType>(Target)->getElementType().getTypePtr(); |
15649 | 0 | } |
15650 | 1 | if (auto VecTy = dyn_cast<VectorType>(Target)) |
15651 | 0 | Target = VecTy->getElementType().getTypePtr(); |
15652 | | |
15653 | | // Strip complex types. |
15654 | 1 | if (isa<ComplexType>(Source)) { |
15655 | 0 | if (!isa<ComplexType>(Target)) { |
15656 | 0 | if (S.SourceMgr.isInSystemMacro(CC) || Target->isBooleanType()) |
15657 | 0 | return; |
15658 | | |
15659 | 0 | return DiagnoseImpCast(S, E, T, CC, |
15660 | 0 | S.getLangOpts().CPlusPlus |
15661 | 0 | ? diag::err_impcast_complex_scalar |
15662 | 0 | : diag::warn_impcast_complex_scalar); |
15663 | 0 | } |
15664 | | |
15665 | 0 | Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); |
15666 | 0 | Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); |
15667 | 0 | } |
15668 | | |
15669 | 1 | const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); |
15670 | 1 | const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); |
15671 | | |
15672 | | // Strip SVE vector types |
15673 | 1 | if (SourceBT && SourceBT->isSveVLSBuiltinType()) { |
15674 | | // Need the original target type for vector type checks |
15675 | 0 | const Type *OriginalTarget = S.Context.getCanonicalType(T).getTypePtr(); |
15676 | | // Handle conversion from scalable to fixed when msve-vector-bits is |
15677 | | // specified |
15678 | 0 | if (S.Context.areCompatibleSveTypes(QualType(OriginalTarget, 0), |
15679 | 0 | QualType(Source, 0)) || |
15680 | 0 | S.Context.areLaxCompatibleSveTypes(QualType(OriginalTarget, 0), |
15681 | 0 | QualType(Source, 0))) |
15682 | 0 | return; |
15683 | | |
15684 | | // If the vector cast is cast between two vectors of the same size, it is |
15685 | | // a bitcast, not a conversion. |
15686 | 0 | if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target)) |
15687 | 0 | return; |
15688 | | |
15689 | 0 | Source = SourceBT->getSveEltType(S.Context).getTypePtr(); |
15690 | 0 | } |
15691 | | |
15692 | 1 | if (TargetBT && TargetBT->isSveVLSBuiltinType()) |
15693 | 0 | Target = TargetBT->getSveEltType(S.Context).getTypePtr(); |
15694 | | |
15695 | | // If the source is floating point... |
15696 | 1 | if (SourceBT && SourceBT->isFloatingPoint()) { |
15697 | | // ...and the target is floating point... |
15698 | 0 | if (TargetBT && TargetBT->isFloatingPoint()) { |
15699 | | // ...then warn if we're dropping FP rank. |
15700 | |
|
15701 | 0 | int Order = S.getASTContext().getFloatingTypeSemanticOrder( |
15702 | 0 | QualType(SourceBT, 0), QualType(TargetBT, 0)); |
15703 | 0 | if (Order > 0) { |
15704 | | // Don't warn about float constants that are precisely |
15705 | | // representable in the target type. |
15706 | 0 | Expr::EvalResult result; |
15707 | 0 | if (E->EvaluateAsRValue(result, S.Context)) { |
15708 | | // Value might be a float, a float vector, or a float complex. |
15709 | 0 | if (IsSameFloatAfterCast(result.Val, |
15710 | 0 | S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)), |
15711 | 0 | S.Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) |
15712 | 0 | return; |
15713 | 0 | } |
15714 | | |
15715 | 0 | if (S.SourceMgr.isInSystemMacro(CC)) |
15716 | 0 | return; |
15717 | | |
15718 | 0 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision); |
15719 | 0 | } |
15720 | | // ... or possibly if we're increasing rank, too |
15721 | 0 | else if (Order < 0) { |
15722 | 0 | if (S.SourceMgr.isInSystemMacro(CC)) |
15723 | 0 | return; |
15724 | | |
15725 | 0 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_double_promotion); |
15726 | 0 | } |
15727 | 0 | return; |
15728 | 0 | } |
15729 | | |
15730 | | // If the target is integral, always warn. |
15731 | 0 | if (TargetBT && TargetBT->isInteger()) { |
15732 | 0 | if (S.SourceMgr.isInSystemMacro(CC)) |
15733 | 0 | return; |
15734 | | |
15735 | 0 | DiagnoseFloatingImpCast(S, E, T, CC); |
15736 | 0 | } |
15737 | | |
15738 | | // Detect the case where a call result is converted from floating-point to |
15739 | | // to bool, and the final argument to the call is converted from bool, to |
15740 | | // discover this typo: |
15741 | | // |
15742 | | // bool b = fabs(x < 1.0); // should be "bool b = fabs(x) < 1.0;" |
15743 | | // |
15744 | | // FIXME: This is an incredibly special case; is there some more general |
15745 | | // way to detect this class of misplaced-parentheses bug? |
15746 | 0 | if (Target->isBooleanType() && isa<CallExpr>(E)) { |
15747 | | // Check last argument of function call to see if it is an |
15748 | | // implicit cast from a type matching the type the result |
15749 | | // is being cast to. |
15750 | 0 | CallExpr *CEx = cast<CallExpr>(E); |
15751 | 0 | if (unsigned NumArgs = CEx->getNumArgs()) { |
15752 | 0 | Expr *LastA = CEx->getArg(NumArgs - 1); |
15753 | 0 | Expr *InnerE = LastA->IgnoreParenImpCasts(); |
15754 | 0 | if (isa<ImplicitCastExpr>(LastA) && |
15755 | 0 | InnerE->getType()->isBooleanType()) { |
15756 | | // Warn on this floating-point to bool conversion |
15757 | 0 | DiagnoseImpCast(S, E, T, CC, |
15758 | 0 | diag::warn_impcast_floating_point_to_bool); |
15759 | 0 | } |
15760 | 0 | } |
15761 | 0 | } |
15762 | 0 | return; |
15763 | 0 | } |
15764 | | |
15765 | | // Valid casts involving fixed point types should be accounted for here. |
15766 | 1 | if (Source->isFixedPointType()) { |
15767 | 0 | if (Target->isUnsaturatedFixedPointType()) { |
15768 | 0 | Expr::EvalResult Result; |
15769 | 0 | if (E->EvaluateAsFixedPoint(Result, S.Context, Expr::SE_AllowSideEffects, |
15770 | 0 | S.isConstantEvaluatedContext())) { |
15771 | 0 | llvm::APFixedPoint Value = Result.Val.getFixedPoint(); |
15772 | 0 | llvm::APFixedPoint MaxVal = S.Context.getFixedPointMax(T); |
15773 | 0 | llvm::APFixedPoint MinVal = S.Context.getFixedPointMin(T); |
15774 | 0 | if (Value > MaxVal || Value < MinVal) { |
15775 | 0 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
15776 | 0 | S.PDiag(diag::warn_impcast_fixed_point_range) |
15777 | 0 | << Value.toString() << T |
15778 | 0 | << E->getSourceRange() |
15779 | 0 | << clang::SourceRange(CC)); |
15780 | 0 | return; |
15781 | 0 | } |
15782 | 0 | } |
15783 | 0 | } else if (Target->isIntegerType()) { |
15784 | 0 | Expr::EvalResult Result; |
15785 | 0 | if (!S.isConstantEvaluatedContext() && |
15786 | 0 | E->EvaluateAsFixedPoint(Result, S.Context, |
15787 | 0 | Expr::SE_AllowSideEffects)) { |
15788 | 0 | llvm::APFixedPoint FXResult = Result.Val.getFixedPoint(); |
15789 | |
|
15790 | 0 | bool Overflowed; |
15791 | 0 | llvm::APSInt IntResult = FXResult.convertToInt( |
15792 | 0 | S.Context.getIntWidth(T), |
15793 | 0 | Target->isSignedIntegerOrEnumerationType(), &Overflowed); |
15794 | |
|
15795 | 0 | if (Overflowed) { |
15796 | 0 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
15797 | 0 | S.PDiag(diag::warn_impcast_fixed_point_range) |
15798 | 0 | << FXResult.toString() << T |
15799 | 0 | << E->getSourceRange() |
15800 | 0 | << clang::SourceRange(CC)); |
15801 | 0 | return; |
15802 | 0 | } |
15803 | 0 | } |
15804 | 0 | } |
15805 | 1 | } else if (Target->isUnsaturatedFixedPointType()) { |
15806 | 0 | if (Source->isIntegerType()) { |
15807 | 0 | Expr::EvalResult Result; |
15808 | 0 | if (!S.isConstantEvaluatedContext() && |
15809 | 0 | E->EvaluateAsInt(Result, S.Context, Expr::SE_AllowSideEffects)) { |
15810 | 0 | llvm::APSInt Value = Result.Val.getInt(); |
15811 | |
|
15812 | 0 | bool Overflowed; |
15813 | 0 | llvm::APFixedPoint IntResult = llvm::APFixedPoint::getFromIntValue( |
15814 | 0 | Value, S.Context.getFixedPointSemantics(T), &Overflowed); |
15815 | |
|
15816 | 0 | if (Overflowed) { |
15817 | 0 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
15818 | 0 | S.PDiag(diag::warn_impcast_fixed_point_range) |
15819 | 0 | << toString(Value, /*Radix=*/10) << T |
15820 | 0 | << E->getSourceRange() |
15821 | 0 | << clang::SourceRange(CC)); |
15822 | 0 | return; |
15823 | 0 | } |
15824 | 0 | } |
15825 | 0 | } |
15826 | 0 | } |
15827 | | |
15828 | | // If we are casting an integer type to a floating point type without |
15829 | | // initialization-list syntax, we might lose accuracy if the floating |
15830 | | // point type has a narrower significand than the integer type. |
15831 | 1 | if (SourceBT && TargetBT && SourceBT->isIntegerType() && |
15832 | 1 | TargetBT->isFloatingType() && !IsListInit) { |
15833 | | // Determine the number of precision bits in the source integer type. |
15834 | 0 | IntRange SourceRange = |
15835 | 0 | GetExprRange(S.Context, E, S.isConstantEvaluatedContext(), |
15836 | 0 | /*Approximate=*/true); |
15837 | 0 | unsigned int SourcePrecision = SourceRange.Width; |
15838 | | |
15839 | | // Determine the number of precision bits in the |
15840 | | // target floating point type. |
15841 | 0 | unsigned int TargetPrecision = llvm::APFloatBase::semanticsPrecision( |
15842 | 0 | S.Context.getFloatTypeSemantics(QualType(TargetBT, 0))); |
15843 | |
|
15844 | 0 | if (SourcePrecision > 0 && TargetPrecision > 0 && |
15845 | 0 | SourcePrecision > TargetPrecision) { |
15846 | |
|
15847 | 0 | if (std::optional<llvm::APSInt> SourceInt = |
15848 | 0 | E->getIntegerConstantExpr(S.Context)) { |
15849 | | // If the source integer is a constant, convert it to the target |
15850 | | // floating point type. Issue a warning if the value changes |
15851 | | // during the whole conversion. |
15852 | 0 | llvm::APFloat TargetFloatValue( |
15853 | 0 | S.Context.getFloatTypeSemantics(QualType(TargetBT, 0))); |
15854 | 0 | llvm::APFloat::opStatus ConversionStatus = |
15855 | 0 | TargetFloatValue.convertFromAPInt( |
15856 | 0 | *SourceInt, SourceBT->isSignedInteger(), |
15857 | 0 | llvm::APFloat::rmNearestTiesToEven); |
15858 | |
|
15859 | 0 | if (ConversionStatus != llvm::APFloat::opOK) { |
15860 | 0 | SmallString<32> PrettySourceValue; |
15861 | 0 | SourceInt->toString(PrettySourceValue, 10); |
15862 | 0 | SmallString<32> PrettyTargetValue; |
15863 | 0 | TargetFloatValue.toString(PrettyTargetValue, TargetPrecision); |
15864 | |
|
15865 | 0 | S.DiagRuntimeBehavior( |
15866 | 0 | E->getExprLoc(), E, |
15867 | 0 | S.PDiag(diag::warn_impcast_integer_float_precision_constant) |
15868 | 0 | << PrettySourceValue << PrettyTargetValue << E->getType() << T |
15869 | 0 | << E->getSourceRange() << clang::SourceRange(CC)); |
15870 | 0 | } |
15871 | 0 | } else { |
15872 | | // Otherwise, the implicit conversion may lose precision. |
15873 | 0 | DiagnoseImpCast(S, E, T, CC, |
15874 | 0 | diag::warn_impcast_integer_float_precision); |
15875 | 0 | } |
15876 | 0 | } |
15877 | 0 | } |
15878 | | |
15879 | 1 | DiagnoseNullConversion(S, E, T, CC); |
15880 | | |
15881 | 1 | S.DiscardMisalignedMemberAddress(Target, E); |
15882 | | |
15883 | 1 | if (Target->isBooleanType()) |
15884 | 0 | DiagnoseIntInBoolContext(S, E); |
15885 | | |
15886 | 1 | if (!Source->isIntegerType() || !Target->isIntegerType()) |
15887 | 1 | return; |
15888 | | |
15889 | | // TODO: remove this early return once the false positives for constant->bool |
15890 | | // in templates, macros, etc, are reduced or removed. |
15891 | 0 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) |
15892 | 0 | return; |
15893 | | |
15894 | 0 | if (isObjCSignedCharBool(S, T) && !Source->isCharType() && |
15895 | 0 | !E->isKnownToHaveBooleanValue(/*Semantic=*/false)) { |
15896 | 0 | return adornObjCBoolConversionDiagWithTernaryFixit( |
15897 | 0 | S, E, |
15898 | 0 | S.Diag(CC, diag::warn_impcast_int_to_objc_signed_char_bool) |
15899 | 0 | << E->getType()); |
15900 | 0 | } |
15901 | | |
15902 | 0 | IntRange SourceTypeRange = |
15903 | 0 | IntRange::forTargetOfCanonicalType(S.Context, Source); |
15904 | 0 | IntRange LikelySourceRange = GetExprRange( |
15905 | 0 | S.Context, E, S.isConstantEvaluatedContext(), /*Approximate=*/true); |
15906 | 0 | IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target); |
15907 | |
|
15908 | 0 | if (LikelySourceRange.Width > TargetRange.Width) { |
15909 | | // If the source is a constant, use a default-on diagnostic. |
15910 | | // TODO: this should happen for bitfield stores, too. |
15911 | 0 | Expr::EvalResult Result; |
15912 | 0 | if (E->EvaluateAsInt(Result, S.Context, Expr::SE_AllowSideEffects, |
15913 | 0 | S.isConstantEvaluatedContext())) { |
15914 | 0 | llvm::APSInt Value(32); |
15915 | 0 | Value = Result.Val.getInt(); |
15916 | |
|
15917 | 0 | if (S.SourceMgr.isInSystemMacro(CC)) |
15918 | 0 | return; |
15919 | | |
15920 | 0 | std::string PrettySourceValue = toString(Value, 10); |
15921 | 0 | std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); |
15922 | |
|
15923 | 0 | S.DiagRuntimeBehavior( |
15924 | 0 | E->getExprLoc(), E, |
15925 | 0 | S.PDiag(diag::warn_impcast_integer_precision_constant) |
15926 | 0 | << PrettySourceValue << PrettyTargetValue << E->getType() << T |
15927 | 0 | << E->getSourceRange() << SourceRange(CC)); |
15928 | 0 | return; |
15929 | 0 | } |
15930 | | |
15931 | | // People want to build with -Wshorten-64-to-32 and not -Wconversion. |
15932 | 0 | if (S.SourceMgr.isInSystemMacro(CC)) |
15933 | 0 | return; |
15934 | | |
15935 | 0 | if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64) |
15936 | 0 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32, |
15937 | 0 | /* pruneControlFlow */ true); |
15938 | 0 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision); |
15939 | 0 | } |
15940 | | |
15941 | 0 | if (TargetRange.Width > SourceTypeRange.Width) { |
15942 | 0 | if (auto *UO = dyn_cast<UnaryOperator>(E)) |
15943 | 0 | if (UO->getOpcode() == UO_Minus) |
15944 | 0 | if (Source->isUnsignedIntegerType()) { |
15945 | 0 | if (Target->isUnsignedIntegerType()) |
15946 | 0 | return DiagnoseImpCast(S, E, T, CC, |
15947 | 0 | diag::warn_impcast_high_order_zero_bits); |
15948 | 0 | if (Target->isSignedIntegerType()) |
15949 | 0 | return DiagnoseImpCast(S, E, T, CC, |
15950 | 0 | diag::warn_impcast_nonnegative_result); |
15951 | 0 | } |
15952 | 0 | } |
15953 | | |
15954 | 0 | if (TargetRange.Width == LikelySourceRange.Width && |
15955 | 0 | !TargetRange.NonNegative && LikelySourceRange.NonNegative && |
15956 | 0 | Source->isSignedIntegerType()) { |
15957 | | // Warn when doing a signed to signed conversion, warn if the positive |
15958 | | // source value is exactly the width of the target type, which will |
15959 | | // cause a negative value to be stored. |
15960 | |
|
15961 | 0 | Expr::EvalResult Result; |
15962 | 0 | if (E->EvaluateAsInt(Result, S.Context, Expr::SE_AllowSideEffects) && |
15963 | 0 | !S.SourceMgr.isInSystemMacro(CC)) { |
15964 | 0 | llvm::APSInt Value = Result.Val.getInt(); |
15965 | 0 | if (isSameWidthConstantConversion(S, E, T, CC)) { |
15966 | 0 | std::string PrettySourceValue = toString(Value, 10); |
15967 | 0 | std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); |
15968 | |
|
15969 | 0 | S.DiagRuntimeBehavior( |
15970 | 0 | E->getExprLoc(), E, |
15971 | 0 | S.PDiag(diag::warn_impcast_integer_precision_constant) |
15972 | 0 | << PrettySourceValue << PrettyTargetValue << E->getType() << T |
15973 | 0 | << E->getSourceRange() << SourceRange(CC)); |
15974 | 0 | return; |
15975 | 0 | } |
15976 | 0 | } |
15977 | | |
15978 | | // Fall through for non-constants to give a sign conversion warning. |
15979 | 0 | } |
15980 | | |
15981 | 0 | if ((!isa<EnumType>(Target) || !isa<EnumType>(Source)) && |
15982 | 0 | ((TargetRange.NonNegative && !LikelySourceRange.NonNegative) || |
15983 | 0 | (!TargetRange.NonNegative && LikelySourceRange.NonNegative && |
15984 | 0 | LikelySourceRange.Width == TargetRange.Width))) { |
15985 | 0 | if (S.SourceMgr.isInSystemMacro(CC)) |
15986 | 0 | return; |
15987 | | |
15988 | 0 | if (SourceBT && SourceBT->isInteger() && TargetBT && |
15989 | 0 | TargetBT->isInteger() && |
15990 | 0 | Source->isSignedIntegerType() == Target->isSignedIntegerType()) { |
15991 | 0 | return; |
15992 | 0 | } |
15993 | | |
15994 | 0 | unsigned DiagID = diag::warn_impcast_integer_sign; |
15995 | | |
15996 | | // Traditionally, gcc has warned about this under -Wsign-compare. |
15997 | | // We also want to warn about it in -Wconversion. |
15998 | | // So if -Wconversion is off, use a completely identical diagnostic |
15999 | | // in the sign-compare group. |
16000 | | // The conditional-checking code will |
16001 | 0 | if (ICContext) { |
16002 | 0 | DiagID = diag::warn_impcast_integer_sign_conditional; |
16003 | 0 | *ICContext = true; |
16004 | 0 | } |
16005 | |
|
16006 | 0 | return DiagnoseImpCast(S, E, T, CC, DiagID); |
16007 | 0 | } |
16008 | | |
16009 | | // Diagnose conversions between different enumeration types. |
16010 | | // In C, we pretend that the type of an EnumConstantDecl is its enumeration |
16011 | | // type, to give us better diagnostics. |
16012 | 0 | QualType SourceType = E->getType(); |
16013 | 0 | if (!S.getLangOpts().CPlusPlus) { |
16014 | 0 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
16015 | 0 | if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
16016 | 0 | EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext()); |
16017 | 0 | SourceType = S.Context.getTypeDeclType(Enum); |
16018 | 0 | Source = S.Context.getCanonicalType(SourceType).getTypePtr(); |
16019 | 0 | } |
16020 | 0 | } |
16021 | |
|
16022 | 0 | if (const EnumType *SourceEnum = Source->getAs<EnumType>()) |
16023 | 0 | if (const EnumType *TargetEnum = Target->getAs<EnumType>()) |
16024 | 0 | if (SourceEnum->getDecl()->hasNameForLinkage() && |
16025 | 0 | TargetEnum->getDecl()->hasNameForLinkage() && |
16026 | 0 | SourceEnum != TargetEnum) { |
16027 | 0 | if (S.SourceMgr.isInSystemMacro(CC)) |
16028 | 0 | return; |
16029 | | |
16030 | 0 | return DiagnoseImpCast(S, E, SourceType, T, CC, |
16031 | 0 | diag::warn_impcast_different_enum_types); |
16032 | 0 | } |
16033 | 0 | } |
16034 | | |
16035 | | static void CheckConditionalOperator(Sema &S, AbstractConditionalOperator *E, |
16036 | | SourceLocation CC, QualType T); |
16037 | | |
16038 | | static void CheckConditionalOperand(Sema &S, Expr *E, QualType T, |
16039 | 0 | SourceLocation CC, bool &ICContext) { |
16040 | 0 | E = E->IgnoreParenImpCasts(); |
16041 | | // Diagnose incomplete type for second or third operand in C. |
16042 | 0 | if (!S.getLangOpts().CPlusPlus && E->getType()->isRecordType()) |
16043 | 0 | S.RequireCompleteExprType(E, diag::err_incomplete_type); |
16044 | |
|
16045 | 0 | if (auto *CO = dyn_cast<AbstractConditionalOperator>(E)) |
16046 | 0 | return CheckConditionalOperator(S, CO, CC, T); |
16047 | | |
16048 | 0 | AnalyzeImplicitConversions(S, E, CC); |
16049 | 0 | if (E->getType() != T) |
16050 | 0 | return CheckImplicitConversion(S, E, T, CC, &ICContext); |
16051 | 0 | } |
16052 | | |
16053 | | static void CheckConditionalOperator(Sema &S, AbstractConditionalOperator *E, |
16054 | 0 | SourceLocation CC, QualType T) { |
16055 | 0 | AnalyzeImplicitConversions(S, E->getCond(), E->getQuestionLoc()); |
16056 | |
|
16057 | 0 | Expr *TrueExpr = E->getTrueExpr(); |
16058 | 0 | if (auto *BCO = dyn_cast<BinaryConditionalOperator>(E)) |
16059 | 0 | TrueExpr = BCO->getCommon(); |
16060 | |
|
16061 | 0 | bool Suspicious = false; |
16062 | 0 | CheckConditionalOperand(S, TrueExpr, T, CC, Suspicious); |
16063 | 0 | CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious); |
16064 | |
|
16065 | 0 | if (T->isBooleanType()) |
16066 | 0 | DiagnoseIntInBoolContext(S, E); |
16067 | | |
16068 | | // If -Wconversion would have warned about either of the candidates |
16069 | | // for a signedness conversion to the context type... |
16070 | 0 | if (!Suspicious) return; |
16071 | | |
16072 | | // ...but it's currently ignored... |
16073 | 0 | if (!S.Diags.isIgnored(diag::warn_impcast_integer_sign_conditional, CC)) |
16074 | 0 | return; |
16075 | | |
16076 | | // ...then check whether it would have warned about either of the |
16077 | | // candidates for a signedness conversion to the condition type. |
16078 | 0 | if (E->getType() == T) return; |
16079 | | |
16080 | 0 | Suspicious = false; |
16081 | 0 | CheckImplicitConversion(S, TrueExpr->IgnoreParenImpCasts(), |
16082 | 0 | E->getType(), CC, &Suspicious); |
16083 | 0 | if (!Suspicious) |
16084 | 0 | CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(), |
16085 | 0 | E->getType(), CC, &Suspicious); |
16086 | 0 | } |
16087 | | |
16088 | | /// Check conversion of given expression to boolean. |
16089 | | /// Input argument E is a logical expression. |
16090 | 2 | static void CheckBoolLikeConversion(Sema &S, Expr *E, SourceLocation CC) { |
16091 | 2 | if (S.getLangOpts().Bool) |
16092 | 2 | return; |
16093 | 0 | if (E->IgnoreParenImpCasts()->getType()->isAtomicType()) |
16094 | 0 | return; |
16095 | 0 | CheckImplicitConversion(S, E->IgnoreParenImpCasts(), S.Context.BoolTy, CC); |
16096 | 0 | } |
16097 | | |
16098 | | namespace { |
16099 | | struct AnalyzeImplicitConversionsWorkItem { |
16100 | | Expr *E; |
16101 | | SourceLocation CC; |
16102 | | bool IsListInit; |
16103 | | }; |
16104 | | } |
16105 | | |
16106 | | /// Data recursive variant of AnalyzeImplicitConversions. Subexpressions |
16107 | | /// that should be visited are added to WorkList. |
16108 | | static void AnalyzeImplicitConversions( |
16109 | | Sema &S, AnalyzeImplicitConversionsWorkItem Item, |
16110 | 8 | llvm::SmallVectorImpl<AnalyzeImplicitConversionsWorkItem> &WorkList) { |
16111 | 8 | Expr *OrigE = Item.E; |
16112 | 8 | SourceLocation CC = Item.CC; |
16113 | | |
16114 | 8 | QualType T = OrigE->getType(); |
16115 | 8 | Expr *E = OrigE->IgnoreParenImpCasts(); |
16116 | | |
16117 | | // Propagate whether we are in a C++ list initialization expression. |
16118 | | // If so, we do not issue warnings for implicit int-float conversion |
16119 | | // precision loss, because C++11 narrowing already handles it. |
16120 | 8 | bool IsListInit = Item.IsListInit || |
16121 | 8 | (isa<InitListExpr>(OrigE) && S.getLangOpts().CPlusPlus); |
16122 | | |
16123 | 8 | if (E->isTypeDependent() || E->isValueDependent()) |
16124 | 0 | return; |
16125 | | |
16126 | 8 | Expr *SourceExpr = E; |
16127 | | // Examine, but don't traverse into the source expression of an |
16128 | | // OpaqueValueExpr, since it may have multiple parents and we don't want to |
16129 | | // emit duplicate diagnostics. Its fine to examine the form or attempt to |
16130 | | // evaluate it in the context of checking the specific conversion to T though. |
16131 | 8 | if (auto *OVE = dyn_cast<OpaqueValueExpr>(E)) |
16132 | 0 | if (auto *Src = OVE->getSourceExpr()) |
16133 | 0 | SourceExpr = Src; |
16134 | | |
16135 | 8 | if (const auto *UO = dyn_cast<UnaryOperator>(SourceExpr)) |
16136 | 0 | if (UO->getOpcode() == UO_Not && |
16137 | 0 | UO->getSubExpr()->isKnownToHaveBooleanValue()) |
16138 | 0 | S.Diag(UO->getBeginLoc(), diag::warn_bitwise_negation_bool) |
16139 | 0 | << OrigE->getSourceRange() << T->isBooleanType() |
16140 | 0 | << FixItHint::CreateReplacement(UO->getBeginLoc(), "!"); |
16141 | | |
16142 | 8 | if (const auto *BO = dyn_cast<BinaryOperator>(SourceExpr)) |
16143 | 0 | if ((BO->getOpcode() == BO_And || BO->getOpcode() == BO_Or) && |
16144 | 0 | BO->getLHS()->isKnownToHaveBooleanValue() && |
16145 | 0 | BO->getRHS()->isKnownToHaveBooleanValue() && |
16146 | 0 | BO->getLHS()->HasSideEffects(S.Context) && |
16147 | 0 | BO->getRHS()->HasSideEffects(S.Context)) { |
16148 | 0 | S.Diag(BO->getBeginLoc(), diag::warn_bitwise_instead_of_logical) |
16149 | 0 | << (BO->getOpcode() == BO_And ? "&" : "|") << OrigE->getSourceRange() |
16150 | 0 | << FixItHint::CreateReplacement( |
16151 | 0 | BO->getOperatorLoc(), |
16152 | 0 | (BO->getOpcode() == BO_And ? "&&" : "||")); |
16153 | 0 | S.Diag(BO->getBeginLoc(), diag::note_cast_operand_to_int); |
16154 | 0 | } |
16155 | | |
16156 | | // For conditional operators, we analyze the arguments as if they |
16157 | | // were being fed directly into the output. |
16158 | 8 | if (auto *CO = dyn_cast<AbstractConditionalOperator>(SourceExpr)) { |
16159 | 0 | CheckConditionalOperator(S, CO, CC, T); |
16160 | 0 | return; |
16161 | 0 | } |
16162 | | |
16163 | | // Check implicit argument conversions for function calls. |
16164 | 8 | if (CallExpr *Call = dyn_cast<CallExpr>(SourceExpr)) |
16165 | 0 | CheckImplicitArgumentConversions(S, Call, CC); |
16166 | | |
16167 | | // Go ahead and check any implicit conversions we might have skipped. |
16168 | | // The non-canonical typecheck is just an optimization; |
16169 | | // CheckImplicitConversion will filter out dead implicit conversions. |
16170 | 8 | if (SourceExpr->getType() != T) |
16171 | 1 | CheckImplicitConversion(S, SourceExpr, T, CC, nullptr, IsListInit); |
16172 | | |
16173 | | // Now continue drilling into this expression. |
16174 | | |
16175 | 8 | if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) { |
16176 | | // The bound subexpressions in a PseudoObjectExpr are not reachable |
16177 | | // as transitive children. |
16178 | | // FIXME: Use a more uniform representation for this. |
16179 | 0 | for (auto *SE : POE->semantics()) |
16180 | 0 | if (auto *OVE = dyn_cast<OpaqueValueExpr>(SE)) |
16181 | 0 | WorkList.push_back({OVE->getSourceExpr(), CC, IsListInit}); |
16182 | 0 | } |
16183 | | |
16184 | | // Skip past explicit casts. |
16185 | 8 | if (auto *CE = dyn_cast<ExplicitCastExpr>(E)) { |
16186 | 0 | E = CE->getSubExpr()->IgnoreParenImpCasts(); |
16187 | 0 | if (!CE->getType()->isVoidType() && E->getType()->isAtomicType()) |
16188 | 0 | S.Diag(E->getBeginLoc(), diag::warn_atomic_implicit_seq_cst); |
16189 | 0 | WorkList.push_back({E, CC, IsListInit}); |
16190 | 0 | return; |
16191 | 0 | } |
16192 | | |
16193 | 8 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
16194 | | // Do a somewhat different check with comparison operators. |
16195 | 0 | if (BO->isComparisonOp()) |
16196 | 0 | return AnalyzeComparison(S, BO); |
16197 | | |
16198 | | // And with simple assignments. |
16199 | 0 | if (BO->getOpcode() == BO_Assign) |
16200 | 0 | return AnalyzeAssignment(S, BO); |
16201 | | // And with compound assignments. |
16202 | 0 | if (BO->isAssignmentOp()) |
16203 | 0 | return AnalyzeCompoundAssignment(S, BO); |
16204 | 0 | } |
16205 | | |
16206 | | // These break the otherwise-useful invariant below. Fortunately, |
16207 | | // we don't really need to recurse into them, because any internal |
16208 | | // expressions should have been analyzed already when they were |
16209 | | // built into statements. |
16210 | 8 | if (isa<StmtExpr>(E)) return; |
16211 | | |
16212 | | // Don't descend into unevaluated contexts. |
16213 | 8 | if (isa<UnaryExprOrTypeTraitExpr>(E)) return; |
16214 | | |
16215 | | // Now just recurse over the expression's children. |
16216 | 8 | CC = E->getExprLoc(); |
16217 | 8 | BinaryOperator *BO = dyn_cast<BinaryOperator>(E); |
16218 | 8 | bool IsLogicalAndOperator = BO && BO->getOpcode() == BO_LAnd; |
16219 | 8 | for (Stmt *SubStmt : E->children()) { |
16220 | 0 | Expr *ChildExpr = dyn_cast_or_null<Expr>(SubStmt); |
16221 | 0 | if (!ChildExpr) |
16222 | 0 | continue; |
16223 | | |
16224 | 0 | if (auto *CSE = dyn_cast<CoroutineSuspendExpr>(E)) |
16225 | 0 | if (ChildExpr == CSE->getOperand()) |
16226 | | // Do not recurse over a CoroutineSuspendExpr's operand. |
16227 | | // The operand is also a subexpression of getCommonExpr(), and |
16228 | | // recursing into it directly would produce duplicate diagnostics. |
16229 | 0 | continue; |
16230 | | |
16231 | 0 | if (IsLogicalAndOperator && |
16232 | 0 | isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts())) |
16233 | | // Ignore checking string literals that are in logical and operators. |
16234 | | // This is a common pattern for asserts. |
16235 | 0 | continue; |
16236 | 0 | WorkList.push_back({ChildExpr, CC, IsListInit}); |
16237 | 0 | } |
16238 | | |
16239 | 8 | if (BO && BO->isLogicalOp()) { |
16240 | 0 | Expr *SubExpr = BO->getLHS()->IgnoreParenImpCasts(); |
16241 | 0 | if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr)) |
16242 | 0 | ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc()); |
16243 | |
|
16244 | 0 | SubExpr = BO->getRHS()->IgnoreParenImpCasts(); |
16245 | 0 | if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr)) |
16246 | 0 | ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc()); |
16247 | 0 | } |
16248 | | |
16249 | 8 | if (const UnaryOperator *U = dyn_cast<UnaryOperator>(E)) { |
16250 | 0 | if (U->getOpcode() == UO_LNot) { |
16251 | 0 | ::CheckBoolLikeConversion(S, U->getSubExpr(), CC); |
16252 | 0 | } else if (U->getOpcode() != UO_AddrOf) { |
16253 | 0 | if (U->getSubExpr()->getType()->isAtomicType()) |
16254 | 0 | S.Diag(U->getSubExpr()->getBeginLoc(), |
16255 | 0 | diag::warn_atomic_implicit_seq_cst); |
16256 | 0 | } |
16257 | 0 | } |
16258 | 8 | } |
16259 | | |
16260 | | /// AnalyzeImplicitConversions - Find and report any interesting |
16261 | | /// implicit conversions in the given expression. There are a couple |
16262 | | /// of competing diagnostics here, -Wconversion and -Wsign-compare. |
16263 | | static void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC, |
16264 | 8 | bool IsListInit/*= false*/) { |
16265 | 8 | llvm::SmallVector<AnalyzeImplicitConversionsWorkItem, 16> WorkList; |
16266 | 8 | WorkList.push_back({OrigE, CC, IsListInit}); |
16267 | 16 | while (!WorkList.empty()) |
16268 | 8 | AnalyzeImplicitConversions(S, WorkList.pop_back_val(), WorkList); |
16269 | 8 | } |
16270 | | |
16271 | | /// Diagnose integer type and any valid implicit conversion to it. |
16272 | 0 | static bool checkOpenCLEnqueueIntType(Sema &S, Expr *E, const QualType &IntT) { |
16273 | | // Taking into account implicit conversions, |
16274 | | // allow any integer. |
16275 | 0 | if (!E->getType()->isIntegerType()) { |
16276 | 0 | S.Diag(E->getBeginLoc(), |
16277 | 0 | diag::err_opencl_enqueue_kernel_invalid_local_size_type); |
16278 | 0 | return true; |
16279 | 0 | } |
16280 | | // Potentially emit standard warnings for implicit conversions if enabled |
16281 | | // using -Wconversion. |
16282 | 0 | CheckImplicitConversion(S, E, IntT, E->getBeginLoc()); |
16283 | 0 | return false; |
16284 | 0 | } |
16285 | | |
16286 | | // Helper function for Sema::DiagnoseAlwaysNonNullPointer. |
16287 | | // Returns true when emitting a warning about taking the address of a reference. |
16288 | | static bool CheckForReference(Sema &SemaRef, const Expr *E, |
16289 | 0 | const PartialDiagnostic &PD) { |
16290 | 0 | E = E->IgnoreParenImpCasts(); |
16291 | |
|
16292 | 0 | const FunctionDecl *FD = nullptr; |
16293 | |
|
16294 | 0 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
16295 | 0 | if (!DRE->getDecl()->getType()->isReferenceType()) |
16296 | 0 | return false; |
16297 | 0 | } else if (const MemberExpr *M = dyn_cast<MemberExpr>(E)) { |
16298 | 0 | if (!M->getMemberDecl()->getType()->isReferenceType()) |
16299 | 0 | return false; |
16300 | 0 | } else if (const CallExpr *Call = dyn_cast<CallExpr>(E)) { |
16301 | 0 | if (!Call->getCallReturnType(SemaRef.Context)->isReferenceType()) |
16302 | 0 | return false; |
16303 | 0 | FD = Call->getDirectCallee(); |
16304 | 0 | } else { |
16305 | 0 | return false; |
16306 | 0 | } |
16307 | | |
16308 | 0 | SemaRef.Diag(E->getExprLoc(), PD); |
16309 | | |
16310 | | // If possible, point to location of function. |
16311 | 0 | if (FD) { |
16312 | 0 | SemaRef.Diag(FD->getLocation(), diag::note_reference_is_return_value) << FD; |
16313 | 0 | } |
16314 | |
|
16315 | 0 | return true; |
16316 | 0 | } |
16317 | | |
16318 | | // Returns true if the SourceLocation is expanded from any macro body. |
16319 | | // Returns false if the SourceLocation is invalid, is from not in a macro |
16320 | | // expansion, or is from expanded from a top-level macro argument. |
16321 | 0 | static bool IsInAnyMacroBody(const SourceManager &SM, SourceLocation Loc) { |
16322 | 0 | if (Loc.isInvalid()) |
16323 | 0 | return false; |
16324 | | |
16325 | 0 | while (Loc.isMacroID()) { |
16326 | 0 | if (SM.isMacroBodyExpansion(Loc)) |
16327 | 0 | return true; |
16328 | 0 | Loc = SM.getImmediateMacroCallerLoc(Loc); |
16329 | 0 | } |
16330 | | |
16331 | 0 | return false; |
16332 | 0 | } |
16333 | | |
16334 | | /// Diagnose pointers that are always non-null. |
16335 | | /// \param E the expression containing the pointer |
16336 | | /// \param NullKind NPCK_NotNull if E is a cast to bool, otherwise, E is |
16337 | | /// compared to a null pointer |
16338 | | /// \param IsEqual True when the comparison is equal to a null pointer |
16339 | | /// \param Range Extra SourceRange to highlight in the diagnostic |
16340 | | void Sema::DiagnoseAlwaysNonNullPointer(Expr *E, |
16341 | | Expr::NullPointerConstantKind NullKind, |
16342 | 0 | bool IsEqual, SourceRange Range) { |
16343 | 0 | if (!E) |
16344 | 0 | return; |
16345 | | |
16346 | | // Don't warn inside macros. |
16347 | 0 | if (E->getExprLoc().isMacroID()) { |
16348 | 0 | const SourceManager &SM = getSourceManager(); |
16349 | 0 | if (IsInAnyMacroBody(SM, E->getExprLoc()) || |
16350 | 0 | IsInAnyMacroBody(SM, Range.getBegin())) |
16351 | 0 | return; |
16352 | 0 | } |
16353 | 0 | E = E->IgnoreImpCasts(); |
16354 | |
|
16355 | 0 | const bool IsCompare = NullKind != Expr::NPCK_NotNull; |
16356 | |
|
16357 | 0 | if (isa<CXXThisExpr>(E)) { |
16358 | 0 | unsigned DiagID = IsCompare ? diag::warn_this_null_compare |
16359 | 0 | : diag::warn_this_bool_conversion; |
16360 | 0 | Diag(E->getExprLoc(), DiagID) << E->getSourceRange() << Range << IsEqual; |
16361 | 0 | return; |
16362 | 0 | } |
16363 | | |
16364 | 0 | bool IsAddressOf = false; |
16365 | |
|
16366 | 0 | if (auto *UO = dyn_cast<UnaryOperator>(E->IgnoreParens())) { |
16367 | 0 | if (UO->getOpcode() != UO_AddrOf) |
16368 | 0 | return; |
16369 | 0 | IsAddressOf = true; |
16370 | 0 | E = UO->getSubExpr(); |
16371 | 0 | } |
16372 | | |
16373 | 0 | if (IsAddressOf) { |
16374 | 0 | unsigned DiagID = IsCompare |
16375 | 0 | ? diag::warn_address_of_reference_null_compare |
16376 | 0 | : diag::warn_address_of_reference_bool_conversion; |
16377 | 0 | PartialDiagnostic PD = PDiag(DiagID) << E->getSourceRange() << Range |
16378 | 0 | << IsEqual; |
16379 | 0 | if (CheckForReference(*this, E, PD)) { |
16380 | 0 | return; |
16381 | 0 | } |
16382 | 0 | } |
16383 | | |
16384 | 0 | auto ComplainAboutNonnullParamOrCall = [&](const Attr *NonnullAttr) { |
16385 | 0 | bool IsParam = isa<NonNullAttr>(NonnullAttr); |
16386 | 0 | std::string Str; |
16387 | 0 | llvm::raw_string_ostream S(Str); |
16388 | 0 | E->printPretty(S, nullptr, getPrintingPolicy()); |
16389 | 0 | unsigned DiagID = IsCompare ? diag::warn_nonnull_expr_compare |
16390 | 0 | : diag::warn_cast_nonnull_to_bool; |
16391 | 0 | Diag(E->getExprLoc(), DiagID) << IsParam << S.str() |
16392 | 0 | << E->getSourceRange() << Range << IsEqual; |
16393 | 0 | Diag(NonnullAttr->getLocation(), diag::note_declared_nonnull) << IsParam; |
16394 | 0 | }; |
16395 | | |
16396 | | // If we have a CallExpr that is tagged with returns_nonnull, we can complain. |
16397 | 0 | if (auto *Call = dyn_cast<CallExpr>(E->IgnoreParenImpCasts())) { |
16398 | 0 | if (auto *Callee = Call->getDirectCallee()) { |
16399 | 0 | if (const Attr *A = Callee->getAttr<ReturnsNonNullAttr>()) { |
16400 | 0 | ComplainAboutNonnullParamOrCall(A); |
16401 | 0 | return; |
16402 | 0 | } |
16403 | 0 | } |
16404 | 0 | } |
16405 | | |
16406 | | // Expect to find a single Decl. Skip anything more complicated. |
16407 | 0 | ValueDecl *D = nullptr; |
16408 | 0 | if (DeclRefExpr *R = dyn_cast<DeclRefExpr>(E)) { |
16409 | 0 | D = R->getDecl(); |
16410 | 0 | } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) { |
16411 | 0 | D = M->getMemberDecl(); |
16412 | 0 | } |
16413 | | |
16414 | | // Weak Decls can be null. |
16415 | 0 | if (!D || D->isWeak()) |
16416 | 0 | return; |
16417 | | |
16418 | | // Check for parameter decl with nonnull attribute |
16419 | 0 | if (const auto* PV = dyn_cast<ParmVarDecl>(D)) { |
16420 | 0 | if (getCurFunction() && |
16421 | 0 | !getCurFunction()->ModifiedNonNullParams.count(PV)) { |
16422 | 0 | if (const Attr *A = PV->getAttr<NonNullAttr>()) { |
16423 | 0 | ComplainAboutNonnullParamOrCall(A); |
16424 | 0 | return; |
16425 | 0 | } |
16426 | | |
16427 | 0 | if (const auto *FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) { |
16428 | | // Skip function template not specialized yet. |
16429 | 0 | if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) |
16430 | 0 | return; |
16431 | 0 | auto ParamIter = llvm::find(FD->parameters(), PV); |
16432 | 0 | assert(ParamIter != FD->param_end()); |
16433 | 0 | unsigned ParamNo = std::distance(FD->param_begin(), ParamIter); |
16434 | |
|
16435 | 0 | for (const auto *NonNull : FD->specific_attrs<NonNullAttr>()) { |
16436 | 0 | if (!NonNull->args_size()) { |
16437 | 0 | ComplainAboutNonnullParamOrCall(NonNull); |
16438 | 0 | return; |
16439 | 0 | } |
16440 | | |
16441 | 0 | for (const ParamIdx &ArgNo : NonNull->args()) { |
16442 | 0 | if (ArgNo.getASTIndex() == ParamNo) { |
16443 | 0 | ComplainAboutNonnullParamOrCall(NonNull); |
16444 | 0 | return; |
16445 | 0 | } |
16446 | 0 | } |
16447 | 0 | } |
16448 | 0 | } |
16449 | 0 | } |
16450 | 0 | } |
16451 | | |
16452 | 0 | QualType T = D->getType(); |
16453 | 0 | const bool IsArray = T->isArrayType(); |
16454 | 0 | const bool IsFunction = T->isFunctionType(); |
16455 | | |
16456 | | // Address of function is used to silence the function warning. |
16457 | 0 | if (IsAddressOf && IsFunction) { |
16458 | 0 | return; |
16459 | 0 | } |
16460 | | |
16461 | | // Found nothing. |
16462 | 0 | if (!IsAddressOf && !IsFunction && !IsArray) |
16463 | 0 | return; |
16464 | | |
16465 | | // Pretty print the expression for the diagnostic. |
16466 | 0 | std::string Str; |
16467 | 0 | llvm::raw_string_ostream S(Str); |
16468 | 0 | E->printPretty(S, nullptr, getPrintingPolicy()); |
16469 | |
|
16470 | 0 | unsigned DiagID = IsCompare ? diag::warn_null_pointer_compare |
16471 | 0 | : diag::warn_impcast_pointer_to_bool; |
16472 | 0 | enum { |
16473 | 0 | AddressOf, |
16474 | 0 | FunctionPointer, |
16475 | 0 | ArrayPointer |
16476 | 0 | } DiagType; |
16477 | 0 | if (IsAddressOf) |
16478 | 0 | DiagType = AddressOf; |
16479 | 0 | else if (IsFunction) |
16480 | 0 | DiagType = FunctionPointer; |
16481 | 0 | else if (IsArray) |
16482 | 0 | DiagType = ArrayPointer; |
16483 | 0 | else |
16484 | 0 | llvm_unreachable("Could not determine diagnostic."); |
16485 | 0 | Diag(E->getExprLoc(), DiagID) << DiagType << S.str() << E->getSourceRange() |
16486 | 0 | << Range << IsEqual; |
16487 | |
|
16488 | 0 | if (!IsFunction) |
16489 | 0 | return; |
16490 | | |
16491 | | // Suggest '&' to silence the function warning. |
16492 | 0 | Diag(E->getExprLoc(), diag::note_function_warning_silence) |
16493 | 0 | << FixItHint::CreateInsertion(E->getBeginLoc(), "&"); |
16494 | | |
16495 | | // Check to see if '()' fixit should be emitted. |
16496 | 0 | QualType ReturnType; |
16497 | 0 | UnresolvedSet<4> NonTemplateOverloads; |
16498 | 0 | tryExprAsCall(*E, ReturnType, NonTemplateOverloads); |
16499 | 0 | if (ReturnType.isNull()) |
16500 | 0 | return; |
16501 | | |
16502 | 0 | if (IsCompare) { |
16503 | | // There are two cases here. If there is null constant, the only suggest |
16504 | | // for a pointer return type. If the null is 0, then suggest if the return |
16505 | | // type is a pointer or an integer type. |
16506 | 0 | if (!ReturnType->isPointerType()) { |
16507 | 0 | if (NullKind == Expr::NPCK_ZeroExpression || |
16508 | 0 | NullKind == Expr::NPCK_ZeroLiteral) { |
16509 | 0 | if (!ReturnType->isIntegerType()) |
16510 | 0 | return; |
16511 | 0 | } else { |
16512 | 0 | return; |
16513 | 0 | } |
16514 | 0 | } |
16515 | 0 | } else { // !IsCompare |
16516 | | // For function to bool, only suggest if the function pointer has bool |
16517 | | // return type. |
16518 | 0 | if (!ReturnType->isSpecificBuiltinType(BuiltinType::Bool)) |
16519 | 0 | return; |
16520 | 0 | } |
16521 | 0 | Diag(E->getExprLoc(), diag::note_function_to_function_call) |
16522 | 0 | << FixItHint::CreateInsertion(getLocForEndOfToken(E->getEndLoc()), "()"); |
16523 | 0 | } |
16524 | | |
16525 | | /// Diagnoses "dangerous" implicit conversions within the given |
16526 | | /// expression (which is a full expression). Implements -Wconversion |
16527 | | /// and -Wsign-compare. |
16528 | | /// |
16529 | | /// \param CC the "context" location of the implicit conversion, i.e. |
16530 | | /// the most location of the syntactic entity requiring the implicit |
16531 | | /// conversion |
16532 | 20 | void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { |
16533 | | // Don't diagnose in unevaluated contexts. |
16534 | 20 | if (isUnevaluatedContext()) |
16535 | 0 | return; |
16536 | | |
16537 | | // Don't diagnose for value- or type-dependent expressions. |
16538 | 20 | if (E->isTypeDependent() || E->isValueDependent()) |
16539 | 12 | return; |
16540 | | |
16541 | | // Check for array bounds violations in cases where the check isn't triggered |
16542 | | // elsewhere for other Expr types (like BinaryOperators), e.g. when an |
16543 | | // ArraySubscriptExpr is on the RHS of a variable initialization. |
16544 | 8 | CheckArrayAccess(E); |
16545 | | |
16546 | | // This is not the right CC for (e.g.) a variable initialization. |
16547 | 8 | AnalyzeImplicitConversions(*this, E, CC); |
16548 | 8 | } |
16549 | | |
16550 | | /// CheckBoolLikeConversion - Check conversion of given expression to boolean. |
16551 | | /// Input argument E is a logical expression. |
16552 | 2 | void Sema::CheckBoolLikeConversion(Expr *E, SourceLocation CC) { |
16553 | 2 | ::CheckBoolLikeConversion(*this, E, CC); |
16554 | 2 | } |
16555 | | |
16556 | | /// Diagnose when expression is an integer constant expression and its evaluation |
16557 | | /// results in integer overflow |
16558 | 8 | void Sema::CheckForIntOverflow (const Expr *E) { |
16559 | | // Use a work list to deal with nested struct initializers. |
16560 | 8 | SmallVector<const Expr *, 2> Exprs(1, E); |
16561 | | |
16562 | 8 | do { |
16563 | 8 | const Expr *OriginalE = Exprs.pop_back_val(); |
16564 | 8 | const Expr *E = OriginalE->IgnoreParenCasts(); |
16565 | | |
16566 | 8 | if (isa<BinaryOperator, UnaryOperator>(E)) { |
16567 | 0 | E->EvaluateForOverflow(Context); |
16568 | 0 | continue; |
16569 | 0 | } |
16570 | | |
16571 | 8 | if (const auto *InitList = dyn_cast<InitListExpr>(OriginalE)) |
16572 | 0 | Exprs.append(InitList->inits().begin(), InitList->inits().end()); |
16573 | 8 | else if (isa<ObjCBoxedExpr>(OriginalE)) |
16574 | 0 | E->EvaluateForOverflow(Context); |
16575 | 8 | else if (const auto *Call = dyn_cast<CallExpr>(E)) |
16576 | 0 | Exprs.append(Call->arg_begin(), Call->arg_end()); |
16577 | 8 | else if (const auto *Message = dyn_cast<ObjCMessageExpr>(E)) |
16578 | 0 | Exprs.append(Message->arg_begin(), Message->arg_end()); |
16579 | 8 | else if (const auto *Construct = dyn_cast<CXXConstructExpr>(E)) |
16580 | 0 | Exprs.append(Construct->arg_begin(), Construct->arg_end()); |
16581 | 8 | else if (const auto *Temporary = dyn_cast<CXXBindTemporaryExpr>(E)) |
16582 | 0 | Exprs.push_back(Temporary->getSubExpr()); |
16583 | 8 | else if (const auto *Array = dyn_cast<ArraySubscriptExpr>(E)) |
16584 | 0 | Exprs.push_back(Array->getIdx()); |
16585 | 8 | else if (const auto *Compound = dyn_cast<CompoundLiteralExpr>(E)) |
16586 | 0 | Exprs.push_back(Compound->getInitializer()); |
16587 | 8 | else if (const auto *New = dyn_cast<CXXNewExpr>(E); |
16588 | 8 | New && New->isArray()) { |
16589 | 0 | if (auto ArraySize = New->getArraySize()) |
16590 | 0 | Exprs.push_back(*ArraySize); |
16591 | 0 | } |
16592 | 8 | } while (!Exprs.empty()); |
16593 | 8 | } |
16594 | | |
16595 | | namespace { |
16596 | | |
16597 | | /// Visitor for expressions which looks for unsequenced operations on the |
16598 | | /// same object. |
16599 | | class SequenceChecker : public ConstEvaluatedExprVisitor<SequenceChecker> { |
16600 | | using Base = ConstEvaluatedExprVisitor<SequenceChecker>; |
16601 | | |
16602 | | /// A tree of sequenced regions within an expression. Two regions are |
16603 | | /// unsequenced if one is an ancestor or a descendent of the other. When we |
16604 | | /// finish processing an expression with sequencing, such as a comma |
16605 | | /// expression, we fold its tree nodes into its parent, since they are |
16606 | | /// unsequenced with respect to nodes we will visit later. |
16607 | | class SequenceTree { |
16608 | | struct Value { |
16609 | 8 | explicit Value(unsigned Parent) : Parent(Parent), Merged(false) {} |
16610 | | unsigned Parent : 31; |
16611 | | unsigned Merged : 1; |
16612 | | }; |
16613 | | SmallVector<Value, 8> Values; |
16614 | | |
16615 | | public: |
16616 | | /// A region within an expression which may be sequenced with respect |
16617 | | /// to some other region. |
16618 | | class Seq { |
16619 | | friend class SequenceTree; |
16620 | | |
16621 | | unsigned Index; |
16622 | | |
16623 | 8 | explicit Seq(unsigned N) : Index(N) {} |
16624 | | |
16625 | | public: |
16626 | 12 | Seq() : Index(0) {} |
16627 | | }; |
16628 | | |
16629 | 8 | SequenceTree() { Values.push_back(Value(0)); } |
16630 | 8 | Seq root() const { return Seq(0); } |
16631 | | |
16632 | | /// Create a new sequence of operations, which is an unsequenced |
16633 | | /// subset of \p Parent. This sequence of operations is sequenced with |
16634 | | /// respect to other children of \p Parent. |
16635 | 0 | Seq allocate(Seq Parent) { |
16636 | 0 | Values.push_back(Value(Parent.Index)); |
16637 | 0 | return Seq(Values.size() - 1); |
16638 | 0 | } |
16639 | | |
16640 | | /// Merge a sequence of operations into its parent. |
16641 | 0 | void merge(Seq S) { |
16642 | 0 | Values[S.Index].Merged = true; |
16643 | 0 | } |
16644 | | |
16645 | | /// Determine whether two operations are unsequenced. This operation |
16646 | | /// is asymmetric: \p Cur should be the more recent sequence, and \p Old |
16647 | | /// should have been merged into its parent as appropriate. |
16648 | 0 | bool isUnsequenced(Seq Cur, Seq Old) { |
16649 | 0 | unsigned C = representative(Cur.Index); |
16650 | 0 | unsigned Target = representative(Old.Index); |
16651 | 0 | while (C >= Target) { |
16652 | 0 | if (C == Target) |
16653 | 0 | return true; |
16654 | 0 | C = Values[C].Parent; |
16655 | 0 | } |
16656 | 0 | return false; |
16657 | 0 | } |
16658 | | |
16659 | | private: |
16660 | | /// Pick a representative for a sequence. |
16661 | 0 | unsigned representative(unsigned K) { |
16662 | 0 | if (Values[K].Merged) |
16663 | | // Perform path compression as we go. |
16664 | 0 | return Values[K].Parent = representative(Values[K].Parent); |
16665 | 0 | return K; |
16666 | 0 | } |
16667 | | }; |
16668 | | |
16669 | | /// An object for which we can track unsequenced uses. |
16670 | | using Object = const NamedDecl *; |
16671 | | |
16672 | | /// Different flavors of object usage which we track. We only track the |
16673 | | /// least-sequenced usage of each kind. |
16674 | | enum UsageKind { |
16675 | | /// A read of an object. Multiple unsequenced reads are OK. |
16676 | | UK_Use, |
16677 | | |
16678 | | /// A modification of an object which is sequenced before the value |
16679 | | /// computation of the expression, such as ++n in C++. |
16680 | | UK_ModAsValue, |
16681 | | |
16682 | | /// A modification of an object which is not sequenced before the value |
16683 | | /// computation of the expression, such as n++. |
16684 | | UK_ModAsSideEffect, |
16685 | | |
16686 | | UK_Count = UK_ModAsSideEffect + 1 |
16687 | | }; |
16688 | | |
16689 | | /// Bundle together a sequencing region and the expression corresponding |
16690 | | /// to a specific usage. One Usage is stored for each usage kind in UsageInfo. |
16691 | | struct Usage { |
16692 | | const Expr *UsageExpr = nullptr; |
16693 | | SequenceTree::Seq Seq; |
16694 | | |
16695 | 12 | Usage() = default; |
16696 | | }; |
16697 | | |
16698 | | struct UsageInfo { |
16699 | | Usage Uses[UK_Count]; |
16700 | | |
16701 | | /// Have we issued a diagnostic for this object already? |
16702 | | bool Diagnosed = false; |
16703 | | |
16704 | | UsageInfo(); |
16705 | | }; |
16706 | | using UsageInfoMap = llvm::SmallDenseMap<Object, UsageInfo, 16>; |
16707 | | |
16708 | | Sema &SemaRef; |
16709 | | |
16710 | | /// Sequenced regions within the expression. |
16711 | | SequenceTree Tree; |
16712 | | |
16713 | | /// Declaration modifications and references which we have seen. |
16714 | | UsageInfoMap UsageMap; |
16715 | | |
16716 | | /// The region we are currently within. |
16717 | | SequenceTree::Seq Region; |
16718 | | |
16719 | | /// Filled in with declarations which were modified as a side-effect |
16720 | | /// (that is, post-increment operations). |
16721 | | SmallVectorImpl<std::pair<Object, Usage>> *ModAsSideEffect = nullptr; |
16722 | | |
16723 | | /// Expressions to check later. We defer checking these to reduce |
16724 | | /// stack usage. |
16725 | | SmallVectorImpl<const Expr *> &WorkList; |
16726 | | |
16727 | | /// RAII object wrapping the visitation of a sequenced subexpression of an |
16728 | | /// expression. At the end of this process, the side-effects of the evaluation |
16729 | | /// become sequenced with respect to the value computation of the result, so |
16730 | | /// we downgrade any UK_ModAsSideEffect within the evaluation to |
16731 | | /// UK_ModAsValue. |
16732 | | struct SequencedSubexpression { |
16733 | | SequencedSubexpression(SequenceChecker &Self) |
16734 | 0 | : Self(Self), OldModAsSideEffect(Self.ModAsSideEffect) { |
16735 | 0 | Self.ModAsSideEffect = &ModAsSideEffect; |
16736 | 0 | } |
16737 | | |
16738 | 0 | ~SequencedSubexpression() { |
16739 | 0 | for (const std::pair<Object, Usage> &M : llvm::reverse(ModAsSideEffect)) { |
16740 | | // Add a new usage with usage kind UK_ModAsValue, and then restore |
16741 | | // the previous usage with UK_ModAsSideEffect (thus clearing it if |
16742 | | // the previous one was empty). |
16743 | 0 | UsageInfo &UI = Self.UsageMap[M.first]; |
16744 | 0 | auto &SideEffectUsage = UI.Uses[UK_ModAsSideEffect]; |
16745 | 0 | Self.addUsage(M.first, UI, SideEffectUsage.UsageExpr, UK_ModAsValue); |
16746 | 0 | SideEffectUsage = M.second; |
16747 | 0 | } |
16748 | 0 | Self.ModAsSideEffect = OldModAsSideEffect; |
16749 | 0 | } |
16750 | | |
16751 | | SequenceChecker &Self; |
16752 | | SmallVector<std::pair<Object, Usage>, 4> ModAsSideEffect; |
16753 | | SmallVectorImpl<std::pair<Object, Usage>> *OldModAsSideEffect; |
16754 | | }; |
16755 | | |
16756 | | /// RAII object wrapping the visitation of a subexpression which we might |
16757 | | /// choose to evaluate as a constant. If any subexpression is evaluated and |
16758 | | /// found to be non-constant, this allows us to suppress the evaluation of |
16759 | | /// the outer expression. |
16760 | | class EvaluationTracker { |
16761 | | public: |
16762 | | EvaluationTracker(SequenceChecker &Self) |
16763 | 0 | : Self(Self), Prev(Self.EvalTracker) { |
16764 | 0 | Self.EvalTracker = this; |
16765 | 0 | } |
16766 | | |
16767 | 0 | ~EvaluationTracker() { |
16768 | 0 | Self.EvalTracker = Prev; |
16769 | 0 | if (Prev) |
16770 | 0 | Prev->EvalOK &= EvalOK; |
16771 | 0 | } |
16772 | | |
16773 | 0 | bool evaluate(const Expr *E, bool &Result) { |
16774 | 0 | if (!EvalOK || E->isValueDependent()) |
16775 | 0 | return false; |
16776 | 0 | EvalOK = E->EvaluateAsBooleanCondition( |
16777 | 0 | Result, Self.SemaRef.Context, |
16778 | 0 | Self.SemaRef.isConstantEvaluatedContext()); |
16779 | 0 | return EvalOK; |
16780 | 0 | } |
16781 | | |
16782 | | private: |
16783 | | SequenceChecker &Self; |
16784 | | EvaluationTracker *Prev; |
16785 | | bool EvalOK = true; |
16786 | | } *EvalTracker = nullptr; |
16787 | | |
16788 | | /// Find the object which is produced by the specified expression, |
16789 | | /// if any. |
16790 | 4 | Object getObject(const Expr *E, bool Mod) const { |
16791 | 4 | E = E->IgnoreParenCasts(); |
16792 | 4 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
16793 | 0 | if (Mod && (UO->getOpcode() == UO_PreInc || UO->getOpcode() == UO_PreDec)) |
16794 | 0 | return getObject(UO->getSubExpr(), Mod); |
16795 | 4 | } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
16796 | 0 | if (BO->getOpcode() == BO_Comma) |
16797 | 0 | return getObject(BO->getRHS(), Mod); |
16798 | 0 | if (Mod && BO->isAssignmentOp()) |
16799 | 0 | return getObject(BO->getLHS(), Mod); |
16800 | 4 | } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
16801 | | // FIXME: Check for more interesting cases, like "x.n = ++x.n". |
16802 | 0 | if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenCasts())) |
16803 | 0 | return ME->getMemberDecl(); |
16804 | 4 | } else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
16805 | | // FIXME: If this is a reference, map through to its value. |
16806 | 4 | return DRE->getDecl(); |
16807 | 0 | return nullptr; |
16808 | 4 | } |
16809 | | |
16810 | | /// Note that an object \p O was modified or used by an expression |
16811 | | /// \p UsageExpr with usage kind \p UK. \p UI is the \p UsageInfo for |
16812 | | /// the object \p O as obtained via the \p UsageMap. |
16813 | 4 | void addUsage(Object O, UsageInfo &UI, const Expr *UsageExpr, UsageKind UK) { |
16814 | | // Get the old usage for the given object and usage kind. |
16815 | 4 | Usage &U = UI.Uses[UK]; |
16816 | 4 | if (!U.UsageExpr || !Tree.isUnsequenced(Region, U.Seq)) { |
16817 | | // If we have a modification as side effect and are in a sequenced |
16818 | | // subexpression, save the old Usage so that we can restore it later |
16819 | | // in SequencedSubexpression::~SequencedSubexpression. |
16820 | 4 | if (UK == UK_ModAsSideEffect && ModAsSideEffect) |
16821 | 0 | ModAsSideEffect->push_back(std::make_pair(O, U)); |
16822 | | // Then record the new usage with the current sequencing region. |
16823 | 4 | U.UsageExpr = UsageExpr; |
16824 | 4 | U.Seq = Region; |
16825 | 4 | } |
16826 | 4 | } |
16827 | | |
16828 | | /// Check whether a modification or use of an object \p O in an expression |
16829 | | /// \p UsageExpr conflicts with a prior usage of kind \p OtherKind. \p UI is |
16830 | | /// the \p UsageInfo for the object \p O as obtained via the \p UsageMap. |
16831 | | /// \p IsModMod is true when we are checking for a mod-mod unsequenced |
16832 | | /// usage and false we are checking for a mod-use unsequenced usage. |
16833 | | void checkUsage(Object O, UsageInfo &UI, const Expr *UsageExpr, |
16834 | 8 | UsageKind OtherKind, bool IsModMod) { |
16835 | 8 | if (UI.Diagnosed) |
16836 | 0 | return; |
16837 | | |
16838 | 8 | const Usage &U = UI.Uses[OtherKind]; |
16839 | 8 | if (!U.UsageExpr || !Tree.isUnsequenced(Region, U.Seq)) |
16840 | 8 | return; |
16841 | | |
16842 | 0 | const Expr *Mod = U.UsageExpr; |
16843 | 0 | const Expr *ModOrUse = UsageExpr; |
16844 | 0 | if (OtherKind == UK_Use) |
16845 | 0 | std::swap(Mod, ModOrUse); |
16846 | |
|
16847 | 0 | SemaRef.DiagRuntimeBehavior( |
16848 | 0 | Mod->getExprLoc(), {Mod, ModOrUse}, |
16849 | 0 | SemaRef.PDiag(IsModMod ? diag::warn_unsequenced_mod_mod |
16850 | 0 | : diag::warn_unsequenced_mod_use) |
16851 | 0 | << O << SourceRange(ModOrUse->getExprLoc())); |
16852 | 0 | UI.Diagnosed = true; |
16853 | 0 | } |
16854 | | |
16855 | | // A note on note{Pre, Post}{Use, Mod}: |
16856 | | // |
16857 | | // (It helps to follow the algorithm with an expression such as |
16858 | | // "((++k)++, k) = k" or "k = (k++, k++)". Both contain unsequenced |
16859 | | // operations before C++17 and both are well-defined in C++17). |
16860 | | // |
16861 | | // When visiting a node which uses/modify an object we first call notePreUse |
16862 | | // or notePreMod before visiting its sub-expression(s). At this point the |
16863 | | // children of the current node have not yet been visited and so the eventual |
16864 | | // uses/modifications resulting from the children of the current node have not |
16865 | | // been recorded yet. |
16866 | | // |
16867 | | // We then visit the children of the current node. After that notePostUse or |
16868 | | // notePostMod is called. These will 1) detect an unsequenced modification |
16869 | | // as side effect (as in "k++ + k") and 2) add a new usage with the |
16870 | | // appropriate usage kind. |
16871 | | // |
16872 | | // We also have to be careful that some operation sequences modification as |
16873 | | // side effect as well (for example: || or ,). To account for this we wrap |
16874 | | // the visitation of such a sub-expression (for example: the LHS of || or ,) |
16875 | | // with SequencedSubexpression. SequencedSubexpression is an RAII object |
16876 | | // which record usages which are modifications as side effect, and then |
16877 | | // downgrade them (or more accurately restore the previous usage which was a |
16878 | | // modification as side effect) when exiting the scope of the sequenced |
16879 | | // subexpression. |
16880 | | |
16881 | 4 | void notePreUse(Object O, const Expr *UseExpr) { |
16882 | 4 | UsageInfo &UI = UsageMap[O]; |
16883 | | // Uses conflict with other modifications. |
16884 | 4 | checkUsage(O, UI, UseExpr, /*OtherKind=*/UK_ModAsValue, /*IsModMod=*/false); |
16885 | 4 | } |
16886 | | |
16887 | 4 | void notePostUse(Object O, const Expr *UseExpr) { |
16888 | 4 | UsageInfo &UI = UsageMap[O]; |
16889 | 4 | checkUsage(O, UI, UseExpr, /*OtherKind=*/UK_ModAsSideEffect, |
16890 | 4 | /*IsModMod=*/false); |
16891 | 4 | addUsage(O, UI, UseExpr, /*UsageKind=*/UK_Use); |
16892 | 4 | } |
16893 | | |
16894 | 0 | void notePreMod(Object O, const Expr *ModExpr) { |
16895 | 0 | UsageInfo &UI = UsageMap[O]; |
16896 | | // Modifications conflict with other modifications and with uses. |
16897 | 0 | checkUsage(O, UI, ModExpr, /*OtherKind=*/UK_ModAsValue, /*IsModMod=*/true); |
16898 | 0 | checkUsage(O, UI, ModExpr, /*OtherKind=*/UK_Use, /*IsModMod=*/false); |
16899 | 0 | } |
16900 | | |
16901 | 0 | void notePostMod(Object O, const Expr *ModExpr, UsageKind UK) { |
16902 | 0 | UsageInfo &UI = UsageMap[O]; |
16903 | 0 | checkUsage(O, UI, ModExpr, /*OtherKind=*/UK_ModAsSideEffect, |
16904 | 0 | /*IsModMod=*/true); |
16905 | 0 | addUsage(O, UI, ModExpr, /*UsageKind=*/UK); |
16906 | 0 | } |
16907 | | |
16908 | | public: |
16909 | | SequenceChecker(Sema &S, const Expr *E, |
16910 | | SmallVectorImpl<const Expr *> &WorkList) |
16911 | 8 | : Base(S.Context), SemaRef(S), Region(Tree.root()), WorkList(WorkList) { |
16912 | 8 | Visit(E); |
16913 | | // Silence a -Wunused-private-field since WorkList is now unused. |
16914 | | // TODO: Evaluate if it can be used, and if not remove it. |
16915 | 8 | (void)this->WorkList; |
16916 | 8 | } |
16917 | | |
16918 | 0 | void VisitStmt(const Stmt *S) { |
16919 | | // Skip all statements which aren't expressions for now. |
16920 | 0 | } |
16921 | | |
16922 | 8 | void VisitExpr(const Expr *E) { |
16923 | | // By default, just recurse to evaluated subexpressions. |
16924 | 8 | Base::VisitStmt(E); |
16925 | 8 | } |
16926 | | |
16927 | 0 | void VisitCoroutineSuspendExpr(const CoroutineSuspendExpr *CSE) { |
16928 | 0 | for (auto *Sub : CSE->children()) { |
16929 | 0 | const Expr *ChildExpr = dyn_cast_or_null<Expr>(Sub); |
16930 | 0 | if (!ChildExpr) |
16931 | 0 | continue; |
16932 | | |
16933 | 0 | if (ChildExpr == CSE->getOperand()) |
16934 | | // Do not recurse over a CoroutineSuspendExpr's operand. |
16935 | | // The operand is also a subexpression of getCommonExpr(), and |
16936 | | // recursing into it directly could confuse object management |
16937 | | // for the sake of sequence tracking. |
16938 | 0 | continue; |
16939 | | |
16940 | 0 | Visit(Sub); |
16941 | 0 | } |
16942 | 0 | } |
16943 | | |
16944 | 5 | void VisitCastExpr(const CastExpr *E) { |
16945 | 5 | Object O = Object(); |
16946 | 5 | if (E->getCastKind() == CK_LValueToRValue) |
16947 | 4 | O = getObject(E->getSubExpr(), false); |
16948 | | |
16949 | 5 | if (O) |
16950 | 4 | notePreUse(O, E); |
16951 | 5 | VisitExpr(E); |
16952 | 5 | if (O) |
16953 | 4 | notePostUse(O, E); |
16954 | 5 | } |
16955 | | |
16956 | | void VisitSequencedExpressions(const Expr *SequencedBefore, |
16957 | 0 | const Expr *SequencedAfter) { |
16958 | 0 | SequenceTree::Seq BeforeRegion = Tree.allocate(Region); |
16959 | 0 | SequenceTree::Seq AfterRegion = Tree.allocate(Region); |
16960 | 0 | SequenceTree::Seq OldRegion = Region; |
16961 | |
|
16962 | 0 | { |
16963 | 0 | SequencedSubexpression SeqBefore(*this); |
16964 | 0 | Region = BeforeRegion; |
16965 | 0 | Visit(SequencedBefore); |
16966 | 0 | } |
16967 | |
|
16968 | 0 | Region = AfterRegion; |
16969 | 0 | Visit(SequencedAfter); |
16970 | |
|
16971 | 0 | Region = OldRegion; |
16972 | |
|
16973 | 0 | Tree.merge(BeforeRegion); |
16974 | 0 | Tree.merge(AfterRegion); |
16975 | 0 | } |
16976 | | |
16977 | 0 | void VisitArraySubscriptExpr(const ArraySubscriptExpr *ASE) { |
16978 | | // C++17 [expr.sub]p1: |
16979 | | // The expression E1[E2] is identical (by definition) to *((E1)+(E2)). The |
16980 | | // expression E1 is sequenced before the expression E2. |
16981 | 0 | if (SemaRef.getLangOpts().CPlusPlus17) |
16982 | 0 | VisitSequencedExpressions(ASE->getLHS(), ASE->getRHS()); |
16983 | 0 | else { |
16984 | 0 | Visit(ASE->getLHS()); |
16985 | 0 | Visit(ASE->getRHS()); |
16986 | 0 | } |
16987 | 0 | } |
16988 | | |
16989 | 0 | void VisitBinPtrMemD(const BinaryOperator *BO) { VisitBinPtrMem(BO); } |
16990 | 0 | void VisitBinPtrMemI(const BinaryOperator *BO) { VisitBinPtrMem(BO); } |
16991 | 0 | void VisitBinPtrMem(const BinaryOperator *BO) { |
16992 | | // C++17 [expr.mptr.oper]p4: |
16993 | | // Abbreviating pm-expression.*cast-expression as E1.*E2, [...] |
16994 | | // the expression E1 is sequenced before the expression E2. |
16995 | 0 | if (SemaRef.getLangOpts().CPlusPlus17) |
16996 | 0 | VisitSequencedExpressions(BO->getLHS(), BO->getRHS()); |
16997 | 0 | else { |
16998 | 0 | Visit(BO->getLHS()); |
16999 | 0 | Visit(BO->getRHS()); |
17000 | 0 | } |
17001 | 0 | } |
17002 | | |
17003 | 0 | void VisitBinShl(const BinaryOperator *BO) { VisitBinShlShr(BO); } |
17004 | 0 | void VisitBinShr(const BinaryOperator *BO) { VisitBinShlShr(BO); } |
17005 | 0 | void VisitBinShlShr(const BinaryOperator *BO) { |
17006 | | // C++17 [expr.shift]p4: |
17007 | | // The expression E1 is sequenced before the expression E2. |
17008 | 0 | if (SemaRef.getLangOpts().CPlusPlus17) |
17009 | 0 | VisitSequencedExpressions(BO->getLHS(), BO->getRHS()); |
17010 | 0 | else { |
17011 | 0 | Visit(BO->getLHS()); |
17012 | 0 | Visit(BO->getRHS()); |
17013 | 0 | } |
17014 | 0 | } |
17015 | | |
17016 | 0 | void VisitBinComma(const BinaryOperator *BO) { |
17017 | | // C++11 [expr.comma]p1: |
17018 | | // Every value computation and side effect associated with the left |
17019 | | // expression is sequenced before every value computation and side |
17020 | | // effect associated with the right expression. |
17021 | 0 | VisitSequencedExpressions(BO->getLHS(), BO->getRHS()); |
17022 | 0 | } |
17023 | | |
17024 | 0 | void VisitBinAssign(const BinaryOperator *BO) { |
17025 | 0 | SequenceTree::Seq RHSRegion; |
17026 | 0 | SequenceTree::Seq LHSRegion; |
17027 | 0 | if (SemaRef.getLangOpts().CPlusPlus17) { |
17028 | 0 | RHSRegion = Tree.allocate(Region); |
17029 | 0 | LHSRegion = Tree.allocate(Region); |
17030 | 0 | } else { |
17031 | 0 | RHSRegion = Region; |
17032 | 0 | LHSRegion = Region; |
17033 | 0 | } |
17034 | 0 | SequenceTree::Seq OldRegion = Region; |
17035 | | |
17036 | | // C++11 [expr.ass]p1: |
17037 | | // [...] the assignment is sequenced after the value computation |
17038 | | // of the right and left operands, [...] |
17039 | | // |
17040 | | // so check it before inspecting the operands and update the |
17041 | | // map afterwards. |
17042 | 0 | Object O = getObject(BO->getLHS(), /*Mod=*/true); |
17043 | 0 | if (O) |
17044 | 0 | notePreMod(O, BO); |
17045 | |
|
17046 | 0 | if (SemaRef.getLangOpts().CPlusPlus17) { |
17047 | | // C++17 [expr.ass]p1: |
17048 | | // [...] The right operand is sequenced before the left operand. [...] |
17049 | 0 | { |
17050 | 0 | SequencedSubexpression SeqBefore(*this); |
17051 | 0 | Region = RHSRegion; |
17052 | 0 | Visit(BO->getRHS()); |
17053 | 0 | } |
17054 | |
|
17055 | 0 | Region = LHSRegion; |
17056 | 0 | Visit(BO->getLHS()); |
17057 | |
|
17058 | 0 | if (O && isa<CompoundAssignOperator>(BO)) |
17059 | 0 | notePostUse(O, BO); |
17060 | |
|
17061 | 0 | } else { |
17062 | | // C++11 does not specify any sequencing between the LHS and RHS. |
17063 | 0 | Region = LHSRegion; |
17064 | 0 | Visit(BO->getLHS()); |
17065 | |
|
17066 | 0 | if (O && isa<CompoundAssignOperator>(BO)) |
17067 | 0 | notePostUse(O, BO); |
17068 | |
|
17069 | 0 | Region = RHSRegion; |
17070 | 0 | Visit(BO->getRHS()); |
17071 | 0 | } |
17072 | | |
17073 | | // C++11 [expr.ass]p1: |
17074 | | // the assignment is sequenced [...] before the value computation of the |
17075 | | // assignment expression. |
17076 | | // C11 6.5.16/3 has no such rule. |
17077 | 0 | Region = OldRegion; |
17078 | 0 | if (O) |
17079 | 0 | notePostMod(O, BO, |
17080 | 0 | SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue |
17081 | 0 | : UK_ModAsSideEffect); |
17082 | 0 | if (SemaRef.getLangOpts().CPlusPlus17) { |
17083 | 0 | Tree.merge(RHSRegion); |
17084 | 0 | Tree.merge(LHSRegion); |
17085 | 0 | } |
17086 | 0 | } |
17087 | | |
17088 | 0 | void VisitCompoundAssignOperator(const CompoundAssignOperator *CAO) { |
17089 | 0 | VisitBinAssign(CAO); |
17090 | 0 | } |
17091 | | |
17092 | 0 | void VisitUnaryPreInc(const UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } |
17093 | 0 | void VisitUnaryPreDec(const UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } |
17094 | 0 | void VisitUnaryPreIncDec(const UnaryOperator *UO) { |
17095 | 0 | Object O = getObject(UO->getSubExpr(), true); |
17096 | 0 | if (!O) |
17097 | 0 | return VisitExpr(UO); |
17098 | | |
17099 | 0 | notePreMod(O, UO); |
17100 | 0 | Visit(UO->getSubExpr()); |
17101 | | // C++11 [expr.pre.incr]p1: |
17102 | | // the expression ++x is equivalent to x+=1 |
17103 | 0 | notePostMod(O, UO, |
17104 | 0 | SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue |
17105 | 0 | : UK_ModAsSideEffect); |
17106 | 0 | } |
17107 | | |
17108 | 0 | void VisitUnaryPostInc(const UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } |
17109 | 0 | void VisitUnaryPostDec(const UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } |
17110 | 0 | void VisitUnaryPostIncDec(const UnaryOperator *UO) { |
17111 | 0 | Object O = getObject(UO->getSubExpr(), true); |
17112 | 0 | if (!O) |
17113 | 0 | return VisitExpr(UO); |
17114 | | |
17115 | 0 | notePreMod(O, UO); |
17116 | 0 | Visit(UO->getSubExpr()); |
17117 | 0 | notePostMod(O, UO, UK_ModAsSideEffect); |
17118 | 0 | } |
17119 | | |
17120 | 0 | void VisitBinLOr(const BinaryOperator *BO) { |
17121 | | // C++11 [expr.log.or]p2: |
17122 | | // If the second expression is evaluated, every value computation and |
17123 | | // side effect associated with the first expression is sequenced before |
17124 | | // every value computation and side effect associated with the |
17125 | | // second expression. |
17126 | 0 | SequenceTree::Seq LHSRegion = Tree.allocate(Region); |
17127 | 0 | SequenceTree::Seq RHSRegion = Tree.allocate(Region); |
17128 | 0 | SequenceTree::Seq OldRegion = Region; |
17129 | |
|
17130 | 0 | EvaluationTracker Eval(*this); |
17131 | 0 | { |
17132 | 0 | SequencedSubexpression Sequenced(*this); |
17133 | 0 | Region = LHSRegion; |
17134 | 0 | Visit(BO->getLHS()); |
17135 | 0 | } |
17136 | | |
17137 | | // C++11 [expr.log.or]p1: |
17138 | | // [...] the second operand is not evaluated if the first operand |
17139 | | // evaluates to true. |
17140 | 0 | bool EvalResult = false; |
17141 | 0 | bool EvalOK = Eval.evaluate(BO->getLHS(), EvalResult); |
17142 | 0 | bool ShouldVisitRHS = !EvalOK || (EvalOK && !EvalResult); |
17143 | 0 | if (ShouldVisitRHS) { |
17144 | 0 | Region = RHSRegion; |
17145 | 0 | Visit(BO->getRHS()); |
17146 | 0 | } |
17147 | |
|
17148 | 0 | Region = OldRegion; |
17149 | 0 | Tree.merge(LHSRegion); |
17150 | 0 | Tree.merge(RHSRegion); |
17151 | 0 | } |
17152 | | |
17153 | 0 | void VisitBinLAnd(const BinaryOperator *BO) { |
17154 | | // C++11 [expr.log.and]p2: |
17155 | | // If the second expression is evaluated, every value computation and |
17156 | | // side effect associated with the first expression is sequenced before |
17157 | | // every value computation and side effect associated with the |
17158 | | // second expression. |
17159 | 0 | SequenceTree::Seq LHSRegion = Tree.allocate(Region); |
17160 | 0 | SequenceTree::Seq RHSRegion = Tree.allocate(Region); |
17161 | 0 | SequenceTree::Seq OldRegion = Region; |
17162 | |
|
17163 | 0 | EvaluationTracker Eval(*this); |
17164 | 0 | { |
17165 | 0 | SequencedSubexpression Sequenced(*this); |
17166 | 0 | Region = LHSRegion; |
17167 | 0 | Visit(BO->getLHS()); |
17168 | 0 | } |
17169 | | |
17170 | | // C++11 [expr.log.and]p1: |
17171 | | // [...] the second operand is not evaluated if the first operand is false. |
17172 | 0 | bool EvalResult = false; |
17173 | 0 | bool EvalOK = Eval.evaluate(BO->getLHS(), EvalResult); |
17174 | 0 | bool ShouldVisitRHS = !EvalOK || (EvalOK && EvalResult); |
17175 | 0 | if (ShouldVisitRHS) { |
17176 | 0 | Region = RHSRegion; |
17177 | 0 | Visit(BO->getRHS()); |
17178 | 0 | } |
17179 | |
|
17180 | 0 | Region = OldRegion; |
17181 | 0 | Tree.merge(LHSRegion); |
17182 | 0 | Tree.merge(RHSRegion); |
17183 | 0 | } |
17184 | | |
17185 | 0 | void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO) { |
17186 | | // C++11 [expr.cond]p1: |
17187 | | // [...] Every value computation and side effect associated with the first |
17188 | | // expression is sequenced before every value computation and side effect |
17189 | | // associated with the second or third expression. |
17190 | 0 | SequenceTree::Seq ConditionRegion = Tree.allocate(Region); |
17191 | | |
17192 | | // No sequencing is specified between the true and false expression. |
17193 | | // However since exactly one of both is going to be evaluated we can |
17194 | | // consider them to be sequenced. This is needed to avoid warning on |
17195 | | // something like "x ? y+= 1 : y += 2;" in the case where we will visit |
17196 | | // both the true and false expressions because we can't evaluate x. |
17197 | | // This will still allow us to detect an expression like (pre C++17) |
17198 | | // "(x ? y += 1 : y += 2) = y". |
17199 | | // |
17200 | | // We don't wrap the visitation of the true and false expression with |
17201 | | // SequencedSubexpression because we don't want to downgrade modifications |
17202 | | // as side effect in the true and false expressions after the visition |
17203 | | // is done. (for example in the expression "(x ? y++ : y++) + y" we should |
17204 | | // not warn between the two "y++", but we should warn between the "y++" |
17205 | | // and the "y". |
17206 | 0 | SequenceTree::Seq TrueRegion = Tree.allocate(Region); |
17207 | 0 | SequenceTree::Seq FalseRegion = Tree.allocate(Region); |
17208 | 0 | SequenceTree::Seq OldRegion = Region; |
17209 | |
|
17210 | 0 | EvaluationTracker Eval(*this); |
17211 | 0 | { |
17212 | 0 | SequencedSubexpression Sequenced(*this); |
17213 | 0 | Region = ConditionRegion; |
17214 | 0 | Visit(CO->getCond()); |
17215 | 0 | } |
17216 | | |
17217 | | // C++11 [expr.cond]p1: |
17218 | | // [...] The first expression is contextually converted to bool (Clause 4). |
17219 | | // It is evaluated and if it is true, the result of the conditional |
17220 | | // expression is the value of the second expression, otherwise that of the |
17221 | | // third expression. Only one of the second and third expressions is |
17222 | | // evaluated. [...] |
17223 | 0 | bool EvalResult = false; |
17224 | 0 | bool EvalOK = Eval.evaluate(CO->getCond(), EvalResult); |
17225 | 0 | bool ShouldVisitTrueExpr = !EvalOK || (EvalOK && EvalResult); |
17226 | 0 | bool ShouldVisitFalseExpr = !EvalOK || (EvalOK && !EvalResult); |
17227 | 0 | if (ShouldVisitTrueExpr) { |
17228 | 0 | Region = TrueRegion; |
17229 | 0 | Visit(CO->getTrueExpr()); |
17230 | 0 | } |
17231 | 0 | if (ShouldVisitFalseExpr) { |
17232 | 0 | Region = FalseRegion; |
17233 | 0 | Visit(CO->getFalseExpr()); |
17234 | 0 | } |
17235 | |
|
17236 | 0 | Region = OldRegion; |
17237 | 0 | Tree.merge(ConditionRegion); |
17238 | 0 | Tree.merge(TrueRegion); |
17239 | 0 | Tree.merge(FalseRegion); |
17240 | 0 | } |
17241 | | |
17242 | 0 | void VisitCallExpr(const CallExpr *CE) { |
17243 | | // FIXME: CXXNewExpr and CXXDeleteExpr implicitly call functions. |
17244 | |
|
17245 | 0 | if (CE->isUnevaluatedBuiltinCall(Context)) |
17246 | 0 | return; |
17247 | | |
17248 | | // C++11 [intro.execution]p15: |
17249 | | // When calling a function [...], every value computation and side effect |
17250 | | // associated with any argument expression, or with the postfix expression |
17251 | | // designating the called function, is sequenced before execution of every |
17252 | | // expression or statement in the body of the function [and thus before |
17253 | | // the value computation of its result]. |
17254 | 0 | SequencedSubexpression Sequenced(*this); |
17255 | 0 | SemaRef.runWithSufficientStackSpace(CE->getExprLoc(), [&] { |
17256 | | // C++17 [expr.call]p5 |
17257 | | // The postfix-expression is sequenced before each expression in the |
17258 | | // expression-list and any default argument. [...] |
17259 | 0 | SequenceTree::Seq CalleeRegion; |
17260 | 0 | SequenceTree::Seq OtherRegion; |
17261 | 0 | if (SemaRef.getLangOpts().CPlusPlus17) { |
17262 | 0 | CalleeRegion = Tree.allocate(Region); |
17263 | 0 | OtherRegion = Tree.allocate(Region); |
17264 | 0 | } else { |
17265 | 0 | CalleeRegion = Region; |
17266 | 0 | OtherRegion = Region; |
17267 | 0 | } |
17268 | 0 | SequenceTree::Seq OldRegion = Region; |
17269 | | |
17270 | | // Visit the callee expression first. |
17271 | 0 | Region = CalleeRegion; |
17272 | 0 | if (SemaRef.getLangOpts().CPlusPlus17) { |
17273 | 0 | SequencedSubexpression Sequenced(*this); |
17274 | 0 | Visit(CE->getCallee()); |
17275 | 0 | } else { |
17276 | 0 | Visit(CE->getCallee()); |
17277 | 0 | } |
17278 | | |
17279 | | // Then visit the argument expressions. |
17280 | 0 | Region = OtherRegion; |
17281 | 0 | for (const Expr *Argument : CE->arguments()) |
17282 | 0 | Visit(Argument); |
17283 | |
|
17284 | 0 | Region = OldRegion; |
17285 | 0 | if (SemaRef.getLangOpts().CPlusPlus17) { |
17286 | 0 | Tree.merge(CalleeRegion); |
17287 | 0 | Tree.merge(OtherRegion); |
17288 | 0 | } |
17289 | 0 | }); |
17290 | 0 | } |
17291 | | |
17292 | 0 | void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CXXOCE) { |
17293 | | // C++17 [over.match.oper]p2: |
17294 | | // [...] the operator notation is first transformed to the equivalent |
17295 | | // function-call notation as summarized in Table 12 (where @ denotes one |
17296 | | // of the operators covered in the specified subclause). However, the |
17297 | | // operands are sequenced in the order prescribed for the built-in |
17298 | | // operator (Clause 8). |
17299 | | // |
17300 | | // From the above only overloaded binary operators and overloaded call |
17301 | | // operators have sequencing rules in C++17 that we need to handle |
17302 | | // separately. |
17303 | 0 | if (!SemaRef.getLangOpts().CPlusPlus17 || |
17304 | 0 | (CXXOCE->getNumArgs() != 2 && CXXOCE->getOperator() != OO_Call)) |
17305 | 0 | return VisitCallExpr(CXXOCE); |
17306 | | |
17307 | 0 | enum { |
17308 | 0 | NoSequencing, |
17309 | 0 | LHSBeforeRHS, |
17310 | 0 | RHSBeforeLHS, |
17311 | 0 | LHSBeforeRest |
17312 | 0 | } SequencingKind; |
17313 | 0 | switch (CXXOCE->getOperator()) { |
17314 | 0 | case OO_Equal: |
17315 | 0 | case OO_PlusEqual: |
17316 | 0 | case OO_MinusEqual: |
17317 | 0 | case OO_StarEqual: |
17318 | 0 | case OO_SlashEqual: |
17319 | 0 | case OO_PercentEqual: |
17320 | 0 | case OO_CaretEqual: |
17321 | 0 | case OO_AmpEqual: |
17322 | 0 | case OO_PipeEqual: |
17323 | 0 | case OO_LessLessEqual: |
17324 | 0 | case OO_GreaterGreaterEqual: |
17325 | 0 | SequencingKind = RHSBeforeLHS; |
17326 | 0 | break; |
17327 | | |
17328 | 0 | case OO_LessLess: |
17329 | 0 | case OO_GreaterGreater: |
17330 | 0 | case OO_AmpAmp: |
17331 | 0 | case OO_PipePipe: |
17332 | 0 | case OO_Comma: |
17333 | 0 | case OO_ArrowStar: |
17334 | 0 | case OO_Subscript: |
17335 | 0 | SequencingKind = LHSBeforeRHS; |
17336 | 0 | break; |
17337 | | |
17338 | 0 | case OO_Call: |
17339 | 0 | SequencingKind = LHSBeforeRest; |
17340 | 0 | break; |
17341 | | |
17342 | 0 | default: |
17343 | 0 | SequencingKind = NoSequencing; |
17344 | 0 | break; |
17345 | 0 | } |
17346 | | |
17347 | 0 | if (SequencingKind == NoSequencing) |
17348 | 0 | return VisitCallExpr(CXXOCE); |
17349 | | |
17350 | | // This is a call, so all subexpressions are sequenced before the result. |
17351 | 0 | SequencedSubexpression Sequenced(*this); |
17352 | |
|
17353 | 0 | SemaRef.runWithSufficientStackSpace(CXXOCE->getExprLoc(), [&] { |
17354 | 0 | assert(SemaRef.getLangOpts().CPlusPlus17 && |
17355 | 0 | "Should only get there with C++17 and above!"); |
17356 | 0 | assert((CXXOCE->getNumArgs() == 2 || CXXOCE->getOperator() == OO_Call) && |
17357 | 0 | "Should only get there with an overloaded binary operator" |
17358 | 0 | " or an overloaded call operator!"); |
17359 | | |
17360 | 0 | if (SequencingKind == LHSBeforeRest) { |
17361 | 0 | assert(CXXOCE->getOperator() == OO_Call && |
17362 | 0 | "We should only have an overloaded call operator here!"); |
17363 | | |
17364 | | // This is very similar to VisitCallExpr, except that we only have the |
17365 | | // C++17 case. The postfix-expression is the first argument of the |
17366 | | // CXXOperatorCallExpr. The expressions in the expression-list, if any, |
17367 | | // are in the following arguments. |
17368 | | // |
17369 | | // Note that we intentionally do not visit the callee expression since |
17370 | | // it is just a decayed reference to a function. |
17371 | 0 | SequenceTree::Seq PostfixExprRegion = Tree.allocate(Region); |
17372 | 0 | SequenceTree::Seq ArgsRegion = Tree.allocate(Region); |
17373 | 0 | SequenceTree::Seq OldRegion = Region; |
17374 | |
|
17375 | 0 | assert(CXXOCE->getNumArgs() >= 1 && |
17376 | 0 | "An overloaded call operator must have at least one argument" |
17377 | 0 | " for the postfix-expression!"); |
17378 | 0 | const Expr *PostfixExpr = CXXOCE->getArgs()[0]; |
17379 | 0 | llvm::ArrayRef<const Expr *> Args(CXXOCE->getArgs() + 1, |
17380 | 0 | CXXOCE->getNumArgs() - 1); |
17381 | | |
17382 | | // Visit the postfix-expression first. |
17383 | 0 | { |
17384 | 0 | Region = PostfixExprRegion; |
17385 | 0 | SequencedSubexpression Sequenced(*this); |
17386 | 0 | Visit(PostfixExpr); |
17387 | 0 | } |
17388 | | |
17389 | | // Then visit the argument expressions. |
17390 | 0 | Region = ArgsRegion; |
17391 | 0 | for (const Expr *Arg : Args) |
17392 | 0 | Visit(Arg); |
17393 | |
|
17394 | 0 | Region = OldRegion; |
17395 | 0 | Tree.merge(PostfixExprRegion); |
17396 | 0 | Tree.merge(ArgsRegion); |
17397 | 0 | } else { |
17398 | 0 | assert(CXXOCE->getNumArgs() == 2 && |
17399 | 0 | "Should only have two arguments here!"); |
17400 | 0 | assert((SequencingKind == LHSBeforeRHS || |
17401 | 0 | SequencingKind == RHSBeforeLHS) && |
17402 | 0 | "Unexpected sequencing kind!"); |
17403 | | |
17404 | | // We do not visit the callee expression since it is just a decayed |
17405 | | // reference to a function. |
17406 | 0 | const Expr *E1 = CXXOCE->getArg(0); |
17407 | 0 | const Expr *E2 = CXXOCE->getArg(1); |
17408 | 0 | if (SequencingKind == RHSBeforeLHS) |
17409 | 0 | std::swap(E1, E2); |
17410 | |
|
17411 | 0 | return VisitSequencedExpressions(E1, E2); |
17412 | 0 | } |
17413 | 0 | }); |
17414 | 0 | } |
17415 | | |
17416 | 0 | void VisitCXXConstructExpr(const CXXConstructExpr *CCE) { |
17417 | | // This is a call, so all subexpressions are sequenced before the result. |
17418 | 0 | SequencedSubexpression Sequenced(*this); |
17419 | |
|
17420 | 0 | if (!CCE->isListInitialization()) |
17421 | 0 | return VisitExpr(CCE); |
17422 | | |
17423 | | // In C++11, list initializations are sequenced. |
17424 | 0 | SmallVector<SequenceTree::Seq, 32> Elts; |
17425 | 0 | SequenceTree::Seq Parent = Region; |
17426 | 0 | for (CXXConstructExpr::const_arg_iterator I = CCE->arg_begin(), |
17427 | 0 | E = CCE->arg_end(); |
17428 | 0 | I != E; ++I) { |
17429 | 0 | Region = Tree.allocate(Parent); |
17430 | 0 | Elts.push_back(Region); |
17431 | 0 | Visit(*I); |
17432 | 0 | } |
17433 | | |
17434 | | // Forget that the initializers are sequenced. |
17435 | 0 | Region = Parent; |
17436 | 0 | for (unsigned I = 0; I < Elts.size(); ++I) |
17437 | 0 | Tree.merge(Elts[I]); |
17438 | 0 | } |
17439 | | |
17440 | 0 | void VisitInitListExpr(const InitListExpr *ILE) { |
17441 | 0 | if (!SemaRef.getLangOpts().CPlusPlus11) |
17442 | 0 | return VisitExpr(ILE); |
17443 | | |
17444 | | // In C++11, list initializations are sequenced. |
17445 | 0 | SmallVector<SequenceTree::Seq, 32> Elts; |
17446 | 0 | SequenceTree::Seq Parent = Region; |
17447 | 0 | for (unsigned I = 0; I < ILE->getNumInits(); ++I) { |
17448 | 0 | const Expr *E = ILE->getInit(I); |
17449 | 0 | if (!E) |
17450 | 0 | continue; |
17451 | 0 | Region = Tree.allocate(Parent); |
17452 | 0 | Elts.push_back(Region); |
17453 | 0 | Visit(E); |
17454 | 0 | } |
17455 | | |
17456 | | // Forget that the initializers are sequenced. |
17457 | 0 | Region = Parent; |
17458 | 0 | for (unsigned I = 0; I < Elts.size(); ++I) |
17459 | 0 | Tree.merge(Elts[I]); |
17460 | 0 | } |
17461 | | }; |
17462 | | |
17463 | 4 | SequenceChecker::UsageInfo::UsageInfo() = default; |
17464 | | |
17465 | | } // namespace |
17466 | | |
17467 | 8 | void Sema::CheckUnsequencedOperations(const Expr *E) { |
17468 | 8 | SmallVector<const Expr *, 8> WorkList; |
17469 | 8 | WorkList.push_back(E); |
17470 | 16 | while (!WorkList.empty()) { |
17471 | 8 | const Expr *Item = WorkList.pop_back_val(); |
17472 | 8 | SequenceChecker(*this, Item, WorkList); |
17473 | 8 | } |
17474 | 8 | } |
17475 | | |
17476 | | void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc, |
17477 | 20 | bool IsConstexpr) { |
17478 | 20 | llvm::SaveAndRestore ConstantContext(isConstantEvaluatedOverride, |
17479 | 20 | IsConstexpr || isa<ConstantExpr>(E)); |
17480 | 20 | CheckImplicitConversions(E, CheckLoc); |
17481 | 20 | if (!E->isInstantiationDependent()) |
17482 | 8 | CheckUnsequencedOperations(E); |
17483 | 20 | if (!IsConstexpr && !E->isValueDependent()) |
17484 | 8 | CheckForIntOverflow(E); |
17485 | 20 | DiagnoseMisalignedMembers(); |
17486 | 20 | } |
17487 | | |
17488 | | void Sema::CheckBitFieldInitialization(SourceLocation InitLoc, |
17489 | | FieldDecl *BitField, |
17490 | 0 | Expr *Init) { |
17491 | 0 | (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc); |
17492 | 0 | } |
17493 | | |
17494 | | static void diagnoseArrayStarInParamType(Sema &S, QualType PType, |
17495 | 0 | SourceLocation Loc) { |
17496 | 0 | if (!PType->isVariablyModifiedType()) |
17497 | 0 | return; |
17498 | 0 | if (const auto *PointerTy = dyn_cast<PointerType>(PType)) { |
17499 | 0 | diagnoseArrayStarInParamType(S, PointerTy->getPointeeType(), Loc); |
17500 | 0 | return; |
17501 | 0 | } |
17502 | 0 | if (const auto *ReferenceTy = dyn_cast<ReferenceType>(PType)) { |
17503 | 0 | diagnoseArrayStarInParamType(S, ReferenceTy->getPointeeType(), Loc); |
17504 | 0 | return; |
17505 | 0 | } |
17506 | 0 | if (const auto *ParenTy = dyn_cast<ParenType>(PType)) { |
17507 | 0 | diagnoseArrayStarInParamType(S, ParenTy->getInnerType(), Loc); |
17508 | 0 | return; |
17509 | 0 | } |
17510 | | |
17511 | 0 | const ArrayType *AT = S.Context.getAsArrayType(PType); |
17512 | 0 | if (!AT) |
17513 | 0 | return; |
17514 | | |
17515 | 0 | if (AT->getSizeModifier() != ArraySizeModifier::Star) { |
17516 | 0 | diagnoseArrayStarInParamType(S, AT->getElementType(), Loc); |
17517 | 0 | return; |
17518 | 0 | } |
17519 | | |
17520 | 0 | S.Diag(Loc, diag::err_array_star_in_function_definition); |
17521 | 0 | } |
17522 | | |
17523 | | /// CheckParmsForFunctionDef - Check that the parameters of the given |
17524 | | /// function are appropriate for the definition of a function. This |
17525 | | /// takes care of any checks that cannot be performed on the |
17526 | | /// declaration itself, e.g., that the types of each of the function |
17527 | | /// parameters are complete. |
17528 | | bool Sema::CheckParmsForFunctionDef(ArrayRef<ParmVarDecl *> Parameters, |
17529 | 0 | bool CheckParameterNames) { |
17530 | 0 | bool HasInvalidParm = false; |
17531 | 0 | for (ParmVarDecl *Param : Parameters) { |
17532 | 0 | assert(Param && "null in a parameter list"); |
17533 | | // C99 6.7.5.3p4: the parameters in a parameter type list in a |
17534 | | // function declarator that is part of a function definition of |
17535 | | // that function shall not have incomplete type. |
17536 | | // |
17537 | | // C++23 [dcl.fct.def.general]/p2 |
17538 | | // The type of a parameter [...] for a function definition |
17539 | | // shall not be a (possibly cv-qualified) class type that is incomplete |
17540 | | // or abstract within the function body unless the function is deleted. |
17541 | 0 | if (!Param->isInvalidDecl() && |
17542 | 0 | (RequireCompleteType(Param->getLocation(), Param->getType(), |
17543 | 0 | diag::err_typecheck_decl_incomplete_type) || |
17544 | 0 | RequireNonAbstractType(Param->getBeginLoc(), Param->getOriginalType(), |
17545 | 0 | diag::err_abstract_type_in_decl, |
17546 | 0 | AbstractParamType))) { |
17547 | 0 | Param->setInvalidDecl(); |
17548 | 0 | HasInvalidParm = true; |
17549 | 0 | } |
17550 | | |
17551 | | // C99 6.9.1p5: If the declarator includes a parameter type list, the |
17552 | | // declaration of each parameter shall include an identifier. |
17553 | 0 | if (CheckParameterNames && Param->getIdentifier() == nullptr && |
17554 | 0 | !Param->isImplicit() && !getLangOpts().CPlusPlus) { |
17555 | | // Diagnose this as an extension in C17 and earlier. |
17556 | 0 | if (!getLangOpts().C23) |
17557 | 0 | Diag(Param->getLocation(), diag::ext_parameter_name_omitted_c23); |
17558 | 0 | } |
17559 | | |
17560 | | // C99 6.7.5.3p12: |
17561 | | // If the function declarator is not part of a definition of that |
17562 | | // function, parameters may have incomplete type and may use the [*] |
17563 | | // notation in their sequences of declarator specifiers to specify |
17564 | | // variable length array types. |
17565 | 0 | QualType PType = Param->getOriginalType(); |
17566 | | // FIXME: This diagnostic should point the '[*]' if source-location |
17567 | | // information is added for it. |
17568 | 0 | diagnoseArrayStarInParamType(*this, PType, Param->getLocation()); |
17569 | | |
17570 | | // If the parameter is a c++ class type and it has to be destructed in the |
17571 | | // callee function, declare the destructor so that it can be called by the |
17572 | | // callee function. Do not perform any direct access check on the dtor here. |
17573 | 0 | if (!Param->isInvalidDecl()) { |
17574 | 0 | if (CXXRecordDecl *ClassDecl = Param->getType()->getAsCXXRecordDecl()) { |
17575 | 0 | if (!ClassDecl->isInvalidDecl() && |
17576 | 0 | !ClassDecl->hasIrrelevantDestructor() && |
17577 | 0 | !ClassDecl->isDependentContext() && |
17578 | 0 | ClassDecl->isParamDestroyedInCallee()) { |
17579 | 0 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
17580 | 0 | MarkFunctionReferenced(Param->getLocation(), Destructor); |
17581 | 0 | DiagnoseUseOfDecl(Destructor, Param->getLocation()); |
17582 | 0 | } |
17583 | 0 | } |
17584 | 0 | } |
17585 | | |
17586 | | // Parameters with the pass_object_size attribute only need to be marked |
17587 | | // constant at function definitions. Because we lack information about |
17588 | | // whether we're on a declaration or definition when we're instantiating the |
17589 | | // attribute, we need to check for constness here. |
17590 | 0 | if (const auto *Attr = Param->getAttr<PassObjectSizeAttr>()) |
17591 | 0 | if (!Param->getType().isConstQualified()) |
17592 | 0 | Diag(Param->getLocation(), diag::err_attribute_pointers_only) |
17593 | 0 | << Attr->getSpelling() << 1; |
17594 | | |
17595 | | // Check for parameter names shadowing fields from the class. |
17596 | 0 | if (LangOpts.CPlusPlus && !Param->isInvalidDecl()) { |
17597 | | // The owning context for the parameter should be the function, but we |
17598 | | // want to see if this function's declaration context is a record. |
17599 | 0 | DeclContext *DC = Param->getDeclContext(); |
17600 | 0 | if (DC && DC->isFunctionOrMethod()) { |
17601 | 0 | if (auto *RD = dyn_cast<CXXRecordDecl>(DC->getParent())) |
17602 | 0 | CheckShadowInheritedFields(Param->getLocation(), Param->getDeclName(), |
17603 | 0 | RD, /*DeclIsField*/ false); |
17604 | 0 | } |
17605 | 0 | } |
17606 | |
|
17607 | 0 | if (!Param->isInvalidDecl() && |
17608 | 0 | Param->getOriginalType()->isWebAssemblyTableType()) { |
17609 | 0 | Param->setInvalidDecl(); |
17610 | 0 | HasInvalidParm = true; |
17611 | 0 | Diag(Param->getLocation(), diag::err_wasm_table_as_function_parameter); |
17612 | 0 | } |
17613 | 0 | } |
17614 | |
|
17615 | 0 | return HasInvalidParm; |
17616 | 0 | } |
17617 | | |
17618 | | std::optional<std::pair< |
17619 | | CharUnits, CharUnits>> static getBaseAlignmentAndOffsetFromPtr(const Expr |
17620 | | *E, |
17621 | | ASTContext |
17622 | | &Ctx); |
17623 | | |
17624 | | /// Compute the alignment and offset of the base class object given the |
17625 | | /// derived-to-base cast expression and the alignment and offset of the derived |
17626 | | /// class object. |
17627 | | static std::pair<CharUnits, CharUnits> |
17628 | | getDerivedToBaseAlignmentAndOffset(const CastExpr *CE, QualType DerivedType, |
17629 | | CharUnits BaseAlignment, CharUnits Offset, |
17630 | 0 | ASTContext &Ctx) { |
17631 | 0 | for (auto PathI = CE->path_begin(), PathE = CE->path_end(); PathI != PathE; |
17632 | 0 | ++PathI) { |
17633 | 0 | const CXXBaseSpecifier *Base = *PathI; |
17634 | 0 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
17635 | 0 | if (Base->isVirtual()) { |
17636 | | // The complete object may have a lower alignment than the non-virtual |
17637 | | // alignment of the base, in which case the base may be misaligned. Choose |
17638 | | // the smaller of the non-virtual alignment and BaseAlignment, which is a |
17639 | | // conservative lower bound of the complete object alignment. |
17640 | 0 | CharUnits NonVirtualAlignment = |
17641 | 0 | Ctx.getASTRecordLayout(BaseDecl).getNonVirtualAlignment(); |
17642 | 0 | BaseAlignment = std::min(BaseAlignment, NonVirtualAlignment); |
17643 | 0 | Offset = CharUnits::Zero(); |
17644 | 0 | } else { |
17645 | 0 | const ASTRecordLayout &RL = |
17646 | 0 | Ctx.getASTRecordLayout(DerivedType->getAsCXXRecordDecl()); |
17647 | 0 | Offset += RL.getBaseClassOffset(BaseDecl); |
17648 | 0 | } |
17649 | 0 | DerivedType = Base->getType(); |
17650 | 0 | } |
17651 | |
|
17652 | 0 | return std::make_pair(BaseAlignment, Offset); |
17653 | 0 | } |
17654 | | |
17655 | | /// Compute the alignment and offset of a binary additive operator. |
17656 | | static std::optional<std::pair<CharUnits, CharUnits>> |
17657 | | getAlignmentAndOffsetFromBinAddOrSub(const Expr *PtrE, const Expr *IntE, |
17658 | 0 | bool IsSub, ASTContext &Ctx) { |
17659 | 0 | QualType PointeeType = PtrE->getType()->getPointeeType(); |
17660 | |
|
17661 | 0 | if (!PointeeType->isConstantSizeType()) |
17662 | 0 | return std::nullopt; |
17663 | | |
17664 | 0 | auto P = getBaseAlignmentAndOffsetFromPtr(PtrE, Ctx); |
17665 | |
|
17666 | 0 | if (!P) |
17667 | 0 | return std::nullopt; |
17668 | | |
17669 | 0 | CharUnits EltSize = Ctx.getTypeSizeInChars(PointeeType); |
17670 | 0 | if (std::optional<llvm::APSInt> IdxRes = IntE->getIntegerConstantExpr(Ctx)) { |
17671 | 0 | CharUnits Offset = EltSize * IdxRes->getExtValue(); |
17672 | 0 | if (IsSub) |
17673 | 0 | Offset = -Offset; |
17674 | 0 | return std::make_pair(P->first, P->second + Offset); |
17675 | 0 | } |
17676 | | |
17677 | | // If the integer expression isn't a constant expression, compute the lower |
17678 | | // bound of the alignment using the alignment and offset of the pointer |
17679 | | // expression and the element size. |
17680 | 0 | return std::make_pair( |
17681 | 0 | P->first.alignmentAtOffset(P->second).alignmentAtOffset(EltSize), |
17682 | 0 | CharUnits::Zero()); |
17683 | 0 | } |
17684 | | |
17685 | | /// This helper function takes an lvalue expression and returns the alignment of |
17686 | | /// a VarDecl and a constant offset from the VarDecl. |
17687 | | std::optional<std::pair< |
17688 | | CharUnits, |
17689 | | CharUnits>> static getBaseAlignmentAndOffsetFromLValue(const Expr *E, |
17690 | 0 | ASTContext &Ctx) { |
17691 | 0 | E = E->IgnoreParens(); |
17692 | 0 | switch (E->getStmtClass()) { |
17693 | 0 | default: |
17694 | 0 | break; |
17695 | 0 | case Stmt::CStyleCastExprClass: |
17696 | 0 | case Stmt::CXXStaticCastExprClass: |
17697 | 0 | case Stmt::ImplicitCastExprClass: { |
17698 | 0 | auto *CE = cast<CastExpr>(E); |
17699 | 0 | const Expr *From = CE->getSubExpr(); |
17700 | 0 | switch (CE->getCastKind()) { |
17701 | 0 | default: |
17702 | 0 | break; |
17703 | 0 | case CK_NoOp: |
17704 | 0 | return getBaseAlignmentAndOffsetFromLValue(From, Ctx); |
17705 | 0 | case CK_UncheckedDerivedToBase: |
17706 | 0 | case CK_DerivedToBase: { |
17707 | 0 | auto P = getBaseAlignmentAndOffsetFromLValue(From, Ctx); |
17708 | 0 | if (!P) |
17709 | 0 | break; |
17710 | 0 | return getDerivedToBaseAlignmentAndOffset(CE, From->getType(), P->first, |
17711 | 0 | P->second, Ctx); |
17712 | 0 | } |
17713 | 0 | } |
17714 | 0 | break; |
17715 | 0 | } |
17716 | 0 | case Stmt::ArraySubscriptExprClass: { |
17717 | 0 | auto *ASE = cast<ArraySubscriptExpr>(E); |
17718 | 0 | return getAlignmentAndOffsetFromBinAddOrSub(ASE->getBase(), ASE->getIdx(), |
17719 | 0 | false, Ctx); |
17720 | 0 | } |
17721 | 0 | case Stmt::DeclRefExprClass: { |
17722 | 0 | if (auto *VD = dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) { |
17723 | | // FIXME: If VD is captured by copy or is an escaping __block variable, |
17724 | | // use the alignment of VD's type. |
17725 | 0 | if (!VD->getType()->isReferenceType()) { |
17726 | | // Dependent alignment cannot be resolved -> bail out. |
17727 | 0 | if (VD->hasDependentAlignment()) |
17728 | 0 | break; |
17729 | 0 | return std::make_pair(Ctx.getDeclAlign(VD), CharUnits::Zero()); |
17730 | 0 | } |
17731 | 0 | if (VD->hasInit()) |
17732 | 0 | return getBaseAlignmentAndOffsetFromLValue(VD->getInit(), Ctx); |
17733 | 0 | } |
17734 | 0 | break; |
17735 | 0 | } |
17736 | 0 | case Stmt::MemberExprClass: { |
17737 | 0 | auto *ME = cast<MemberExpr>(E); |
17738 | 0 | auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()); |
17739 | 0 | if (!FD || FD->getType()->isReferenceType() || |
17740 | 0 | FD->getParent()->isInvalidDecl()) |
17741 | 0 | break; |
17742 | 0 | std::optional<std::pair<CharUnits, CharUnits>> P; |
17743 | 0 | if (ME->isArrow()) |
17744 | 0 | P = getBaseAlignmentAndOffsetFromPtr(ME->getBase(), Ctx); |
17745 | 0 | else |
17746 | 0 | P = getBaseAlignmentAndOffsetFromLValue(ME->getBase(), Ctx); |
17747 | 0 | if (!P) |
17748 | 0 | break; |
17749 | 0 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(FD->getParent()); |
17750 | 0 | uint64_t Offset = Layout.getFieldOffset(FD->getFieldIndex()); |
17751 | 0 | return std::make_pair(P->first, |
17752 | 0 | P->second + CharUnits::fromQuantity(Offset)); |
17753 | 0 | } |
17754 | 0 | case Stmt::UnaryOperatorClass: { |
17755 | 0 | auto *UO = cast<UnaryOperator>(E); |
17756 | 0 | switch (UO->getOpcode()) { |
17757 | 0 | default: |
17758 | 0 | break; |
17759 | 0 | case UO_Deref: |
17760 | 0 | return getBaseAlignmentAndOffsetFromPtr(UO->getSubExpr(), Ctx); |
17761 | 0 | } |
17762 | 0 | break; |
17763 | 0 | } |
17764 | 0 | case Stmt::BinaryOperatorClass: { |
17765 | 0 | auto *BO = cast<BinaryOperator>(E); |
17766 | 0 | auto Opcode = BO->getOpcode(); |
17767 | 0 | switch (Opcode) { |
17768 | 0 | default: |
17769 | 0 | break; |
17770 | 0 | case BO_Comma: |
17771 | 0 | return getBaseAlignmentAndOffsetFromLValue(BO->getRHS(), Ctx); |
17772 | 0 | } |
17773 | 0 | break; |
17774 | 0 | } |
17775 | 0 | } |
17776 | 0 | return std::nullopt; |
17777 | 0 | } |
17778 | | |
17779 | | /// This helper function takes a pointer expression and returns the alignment of |
17780 | | /// a VarDecl and a constant offset from the VarDecl. |
17781 | | std::optional<std::pair< |
17782 | | CharUnits, CharUnits>> static getBaseAlignmentAndOffsetFromPtr(const Expr |
17783 | | *E, |
17784 | | ASTContext |
17785 | 0 | &Ctx) { |
17786 | 0 | E = E->IgnoreParens(); |
17787 | 0 | switch (E->getStmtClass()) { |
17788 | 0 | default: |
17789 | 0 | break; |
17790 | 0 | case Stmt::CStyleCastExprClass: |
17791 | 0 | case Stmt::CXXStaticCastExprClass: |
17792 | 0 | case Stmt::ImplicitCastExprClass: { |
17793 | 0 | auto *CE = cast<CastExpr>(E); |
17794 | 0 | const Expr *From = CE->getSubExpr(); |
17795 | 0 | switch (CE->getCastKind()) { |
17796 | 0 | default: |
17797 | 0 | break; |
17798 | 0 | case CK_NoOp: |
17799 | 0 | return getBaseAlignmentAndOffsetFromPtr(From, Ctx); |
17800 | 0 | case CK_ArrayToPointerDecay: |
17801 | 0 | return getBaseAlignmentAndOffsetFromLValue(From, Ctx); |
17802 | 0 | case CK_UncheckedDerivedToBase: |
17803 | 0 | case CK_DerivedToBase: { |
17804 | 0 | auto P = getBaseAlignmentAndOffsetFromPtr(From, Ctx); |
17805 | 0 | if (!P) |
17806 | 0 | break; |
17807 | 0 | return getDerivedToBaseAlignmentAndOffset( |
17808 | 0 | CE, From->getType()->getPointeeType(), P->first, P->second, Ctx); |
17809 | 0 | } |
17810 | 0 | } |
17811 | 0 | break; |
17812 | 0 | } |
17813 | 0 | case Stmt::CXXThisExprClass: { |
17814 | 0 | auto *RD = E->getType()->getPointeeType()->getAsCXXRecordDecl(); |
17815 | 0 | CharUnits Alignment = Ctx.getASTRecordLayout(RD).getNonVirtualAlignment(); |
17816 | 0 | return std::make_pair(Alignment, CharUnits::Zero()); |
17817 | 0 | } |
17818 | 0 | case Stmt::UnaryOperatorClass: { |
17819 | 0 | auto *UO = cast<UnaryOperator>(E); |
17820 | 0 | if (UO->getOpcode() == UO_AddrOf) |
17821 | 0 | return getBaseAlignmentAndOffsetFromLValue(UO->getSubExpr(), Ctx); |
17822 | 0 | break; |
17823 | 0 | } |
17824 | 0 | case Stmt::BinaryOperatorClass: { |
17825 | 0 | auto *BO = cast<BinaryOperator>(E); |
17826 | 0 | auto Opcode = BO->getOpcode(); |
17827 | 0 | switch (Opcode) { |
17828 | 0 | default: |
17829 | 0 | break; |
17830 | 0 | case BO_Add: |
17831 | 0 | case BO_Sub: { |
17832 | 0 | const Expr *LHS = BO->getLHS(), *RHS = BO->getRHS(); |
17833 | 0 | if (Opcode == BO_Add && !RHS->getType()->isIntegralOrEnumerationType()) |
17834 | 0 | std::swap(LHS, RHS); |
17835 | 0 | return getAlignmentAndOffsetFromBinAddOrSub(LHS, RHS, Opcode == BO_Sub, |
17836 | 0 | Ctx); |
17837 | 0 | } |
17838 | 0 | case BO_Comma: |
17839 | 0 | return getBaseAlignmentAndOffsetFromPtr(BO->getRHS(), Ctx); |
17840 | 0 | } |
17841 | 0 | break; |
17842 | 0 | } |
17843 | 0 | } |
17844 | 0 | return std::nullopt; |
17845 | 0 | } |
17846 | | |
17847 | 0 | static CharUnits getPresumedAlignmentOfPointer(const Expr *E, Sema &S) { |
17848 | | // See if we can compute the alignment of a VarDecl and an offset from it. |
17849 | 0 | std::optional<std::pair<CharUnits, CharUnits>> P = |
17850 | 0 | getBaseAlignmentAndOffsetFromPtr(E, S.Context); |
17851 | |
|
17852 | 0 | if (P) |
17853 | 0 | return P->first.alignmentAtOffset(P->second); |
17854 | | |
17855 | | // If that failed, return the type's alignment. |
17856 | 0 | return S.Context.getTypeAlignInChars(E->getType()->getPointeeType()); |
17857 | 0 | } |
17858 | | |
17859 | | /// CheckCastAlign - Implements -Wcast-align, which warns when a |
17860 | | /// pointer cast increases the alignment requirements. |
17861 | 0 | void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { |
17862 | | // This is actually a lot of work to potentially be doing on every |
17863 | | // cast; don't do it if we're ignoring -Wcast_align (as is the default). |
17864 | 0 | if (getDiagnostics().isIgnored(diag::warn_cast_align, TRange.getBegin())) |
17865 | 0 | return; |
17866 | | |
17867 | | // Ignore dependent types. |
17868 | 0 | if (T->isDependentType() || Op->getType()->isDependentType()) |
17869 | 0 | return; |
17870 | | |
17871 | | // Require that the destination be a pointer type. |
17872 | 0 | const PointerType *DestPtr = T->getAs<PointerType>(); |
17873 | 0 | if (!DestPtr) return; |
17874 | | |
17875 | | // If the destination has alignment 1, we're done. |
17876 | 0 | QualType DestPointee = DestPtr->getPointeeType(); |
17877 | 0 | if (DestPointee->isIncompleteType()) return; |
17878 | 0 | CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); |
17879 | 0 | if (DestAlign.isOne()) return; |
17880 | | |
17881 | | // Require that the source be a pointer type. |
17882 | 0 | const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); |
17883 | 0 | if (!SrcPtr) return; |
17884 | 0 | QualType SrcPointee = SrcPtr->getPointeeType(); |
17885 | | |
17886 | | // Explicitly allow casts from cv void*. We already implicitly |
17887 | | // allowed casts to cv void*, since they have alignment 1. |
17888 | | // Also allow casts involving incomplete types, which implicitly |
17889 | | // includes 'void'. |
17890 | 0 | if (SrcPointee->isIncompleteType()) return; |
17891 | | |
17892 | 0 | CharUnits SrcAlign = getPresumedAlignmentOfPointer(Op, *this); |
17893 | |
|
17894 | 0 | if (SrcAlign >= DestAlign) return; |
17895 | | |
17896 | 0 | Diag(TRange.getBegin(), diag::warn_cast_align) |
17897 | 0 | << Op->getType() << T |
17898 | 0 | << static_cast<unsigned>(SrcAlign.getQuantity()) |
17899 | 0 | << static_cast<unsigned>(DestAlign.getQuantity()) |
17900 | 0 | << TRange << Op->getSourceRange(); |
17901 | 0 | } |
17902 | | |
17903 | | void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, |
17904 | | const ArraySubscriptExpr *ASE, |
17905 | 0 | bool AllowOnePastEnd, bool IndexNegated) { |
17906 | | // Already diagnosed by the constant evaluator. |
17907 | 0 | if (isConstantEvaluatedContext()) |
17908 | 0 | return; |
17909 | | |
17910 | 0 | IndexExpr = IndexExpr->IgnoreParenImpCasts(); |
17911 | 0 | if (IndexExpr->isValueDependent()) |
17912 | 0 | return; |
17913 | | |
17914 | 0 | const Type *EffectiveType = |
17915 | 0 | BaseExpr->getType()->getPointeeOrArrayElementType(); |
17916 | 0 | BaseExpr = BaseExpr->IgnoreParenCasts(); |
17917 | 0 | const ConstantArrayType *ArrayTy = |
17918 | 0 | Context.getAsConstantArrayType(BaseExpr->getType()); |
17919 | |
|
17920 | 0 | LangOptions::StrictFlexArraysLevelKind |
17921 | 0 | StrictFlexArraysLevel = getLangOpts().getStrictFlexArraysLevel(); |
17922 | |
|
17923 | 0 | const Type *BaseType = |
17924 | 0 | ArrayTy == nullptr ? nullptr : ArrayTy->getElementType().getTypePtr(); |
17925 | 0 | bool IsUnboundedArray = |
17926 | 0 | BaseType == nullptr || BaseExpr->isFlexibleArrayMemberLike( |
17927 | 0 | Context, StrictFlexArraysLevel, |
17928 | 0 | /*IgnoreTemplateOrMacroSubstitution=*/true); |
17929 | 0 | if (EffectiveType->isDependentType() || |
17930 | 0 | (!IsUnboundedArray && BaseType->isDependentType())) |
17931 | 0 | return; |
17932 | | |
17933 | 0 | Expr::EvalResult Result; |
17934 | 0 | if (!IndexExpr->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects)) |
17935 | 0 | return; |
17936 | | |
17937 | 0 | llvm::APSInt index = Result.Val.getInt(); |
17938 | 0 | if (IndexNegated) { |
17939 | 0 | index.setIsUnsigned(false); |
17940 | 0 | index = -index; |
17941 | 0 | } |
17942 | |
|
17943 | 0 | if (IsUnboundedArray) { |
17944 | 0 | if (EffectiveType->isFunctionType()) |
17945 | 0 | return; |
17946 | 0 | if (index.isUnsigned() || !index.isNegative()) { |
17947 | 0 | const auto &ASTC = getASTContext(); |
17948 | 0 | unsigned AddrBits = ASTC.getTargetInfo().getPointerWidth( |
17949 | 0 | EffectiveType->getCanonicalTypeInternal().getAddressSpace()); |
17950 | 0 | if (index.getBitWidth() < AddrBits) |
17951 | 0 | index = index.zext(AddrBits); |
17952 | 0 | std::optional<CharUnits> ElemCharUnits = |
17953 | 0 | ASTC.getTypeSizeInCharsIfKnown(EffectiveType); |
17954 | | // PR50741 - If EffectiveType has unknown size (e.g., if it's a void |
17955 | | // pointer) bounds-checking isn't meaningful. |
17956 | 0 | if (!ElemCharUnits || ElemCharUnits->isZero()) |
17957 | 0 | return; |
17958 | 0 | llvm::APInt ElemBytes(index.getBitWidth(), ElemCharUnits->getQuantity()); |
17959 | | // If index has more active bits than address space, we already know |
17960 | | // we have a bounds violation to warn about. Otherwise, compute |
17961 | | // address of (index + 1)th element, and warn about bounds violation |
17962 | | // only if that address exceeds address space. |
17963 | 0 | if (index.getActiveBits() <= AddrBits) { |
17964 | 0 | bool Overflow; |
17965 | 0 | llvm::APInt Product(index); |
17966 | 0 | Product += 1; |
17967 | 0 | Product = Product.umul_ov(ElemBytes, Overflow); |
17968 | 0 | if (!Overflow && Product.getActiveBits() <= AddrBits) |
17969 | 0 | return; |
17970 | 0 | } |
17971 | | |
17972 | | // Need to compute max possible elements in address space, since that |
17973 | | // is included in diag message. |
17974 | 0 | llvm::APInt MaxElems = llvm::APInt::getMaxValue(AddrBits); |
17975 | 0 | MaxElems = MaxElems.zext(std::max(AddrBits + 1, ElemBytes.getBitWidth())); |
17976 | 0 | MaxElems += 1; |
17977 | 0 | ElemBytes = ElemBytes.zextOrTrunc(MaxElems.getBitWidth()); |
17978 | 0 | MaxElems = MaxElems.udiv(ElemBytes); |
17979 | |
|
17980 | 0 | unsigned DiagID = |
17981 | 0 | ASE ? diag::warn_array_index_exceeds_max_addressable_bounds |
17982 | 0 | : diag::warn_ptr_arith_exceeds_max_addressable_bounds; |
17983 | | |
17984 | | // Diag message shows element size in bits and in "bytes" (platform- |
17985 | | // dependent CharUnits) |
17986 | 0 | DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr, |
17987 | 0 | PDiag(DiagID) |
17988 | 0 | << toString(index, 10, true) << AddrBits |
17989 | 0 | << (unsigned)ASTC.toBits(*ElemCharUnits) |
17990 | 0 | << toString(ElemBytes, 10, false) |
17991 | 0 | << toString(MaxElems, 10, false) |
17992 | 0 | << (unsigned)MaxElems.getLimitedValue(~0U) |
17993 | 0 | << IndexExpr->getSourceRange()); |
17994 | |
|
17995 | 0 | const NamedDecl *ND = nullptr; |
17996 | | // Try harder to find a NamedDecl to point at in the note. |
17997 | 0 | while (const auto *ASE = dyn_cast<ArraySubscriptExpr>(BaseExpr)) |
17998 | 0 | BaseExpr = ASE->getBase()->IgnoreParenCasts(); |
17999 | 0 | if (const auto *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
18000 | 0 | ND = DRE->getDecl(); |
18001 | 0 | if (const auto *ME = dyn_cast<MemberExpr>(BaseExpr)) |
18002 | 0 | ND = ME->getMemberDecl(); |
18003 | |
|
18004 | 0 | if (ND) |
18005 | 0 | DiagRuntimeBehavior(ND->getBeginLoc(), BaseExpr, |
18006 | 0 | PDiag(diag::note_array_declared_here) << ND); |
18007 | 0 | } |
18008 | 0 | return; |
18009 | 0 | } |
18010 | | |
18011 | 0 | if (index.isUnsigned() || !index.isNegative()) { |
18012 | | // It is possible that the type of the base expression after |
18013 | | // IgnoreParenCasts is incomplete, even though the type of the base |
18014 | | // expression before IgnoreParenCasts is complete (see PR39746 for an |
18015 | | // example). In this case we have no information about whether the array |
18016 | | // access exceeds the array bounds. However we can still diagnose an array |
18017 | | // access which precedes the array bounds. |
18018 | 0 | if (BaseType->isIncompleteType()) |
18019 | 0 | return; |
18020 | | |
18021 | 0 | llvm::APInt size = ArrayTy->getSize(); |
18022 | |
|
18023 | 0 | if (BaseType != EffectiveType) { |
18024 | | // Make sure we're comparing apples to apples when comparing index to |
18025 | | // size. |
18026 | 0 | uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); |
18027 | 0 | uint64_t array_typesize = Context.getTypeSize(BaseType); |
18028 | | |
18029 | | // Handle ptrarith_typesize being zero, such as when casting to void*. |
18030 | | // Use the size in bits (what "getTypeSize()" returns) rather than bytes. |
18031 | 0 | if (!ptrarith_typesize) |
18032 | 0 | ptrarith_typesize = Context.getCharWidth(); |
18033 | |
|
18034 | 0 | if (ptrarith_typesize != array_typesize) { |
18035 | | // There's a cast to a different size type involved. |
18036 | 0 | uint64_t ratio = array_typesize / ptrarith_typesize; |
18037 | | |
18038 | | // TODO: Be smarter about handling cases where array_typesize is not a |
18039 | | // multiple of ptrarith_typesize. |
18040 | 0 | if (ptrarith_typesize * ratio == array_typesize) |
18041 | 0 | size *= llvm::APInt(size.getBitWidth(), ratio); |
18042 | 0 | } |
18043 | 0 | } |
18044 | |
|
18045 | 0 | if (size.getBitWidth() > index.getBitWidth()) |
18046 | 0 | index = index.zext(size.getBitWidth()); |
18047 | 0 | else if (size.getBitWidth() < index.getBitWidth()) |
18048 | 0 | size = size.zext(index.getBitWidth()); |
18049 | | |
18050 | | // For array subscripting the index must be less than size, but for pointer |
18051 | | // arithmetic also allow the index (offset) to be equal to size since |
18052 | | // computing the next address after the end of the array is legal and |
18053 | | // commonly done e.g. in C++ iterators and range-based for loops. |
18054 | 0 | if (AllowOnePastEnd ? index.ule(size) : index.ult(size)) |
18055 | 0 | return; |
18056 | | |
18057 | | // Suppress the warning if the subscript expression (as identified by the |
18058 | | // ']' location) and the index expression are both from macro expansions |
18059 | | // within a system header. |
18060 | 0 | if (ASE) { |
18061 | 0 | SourceLocation RBracketLoc = SourceMgr.getSpellingLoc( |
18062 | 0 | ASE->getRBracketLoc()); |
18063 | 0 | if (SourceMgr.isInSystemHeader(RBracketLoc)) { |
18064 | 0 | SourceLocation IndexLoc = |
18065 | 0 | SourceMgr.getSpellingLoc(IndexExpr->getBeginLoc()); |
18066 | 0 | if (SourceMgr.isWrittenInSameFile(RBracketLoc, IndexLoc)) |
18067 | 0 | return; |
18068 | 0 | } |
18069 | 0 | } |
18070 | | |
18071 | 0 | unsigned DiagID = ASE ? diag::warn_array_index_exceeds_bounds |
18072 | 0 | : diag::warn_ptr_arith_exceeds_bounds; |
18073 | 0 | unsigned CastMsg = (!ASE || BaseType == EffectiveType) ? 0 : 1; |
18074 | 0 | QualType CastMsgTy = ASE ? ASE->getLHS()->getType() : QualType(); |
18075 | |
|
18076 | 0 | DiagRuntimeBehavior( |
18077 | 0 | BaseExpr->getBeginLoc(), BaseExpr, |
18078 | 0 | PDiag(DiagID) << toString(index, 10, true) << ArrayTy->desugar() |
18079 | 0 | << CastMsg << CastMsgTy << IndexExpr->getSourceRange()); |
18080 | 0 | } else { |
18081 | 0 | unsigned DiagID = diag::warn_array_index_precedes_bounds; |
18082 | 0 | if (!ASE) { |
18083 | 0 | DiagID = diag::warn_ptr_arith_precedes_bounds; |
18084 | 0 | if (index.isNegative()) index = -index; |
18085 | 0 | } |
18086 | |
|
18087 | 0 | DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr, |
18088 | 0 | PDiag(DiagID) << toString(index, 10, true) |
18089 | 0 | << IndexExpr->getSourceRange()); |
18090 | 0 | } |
18091 | | |
18092 | 0 | const NamedDecl *ND = nullptr; |
18093 | | // Try harder to find a NamedDecl to point at in the note. |
18094 | 0 | while (const auto *ASE = dyn_cast<ArraySubscriptExpr>(BaseExpr)) |
18095 | 0 | BaseExpr = ASE->getBase()->IgnoreParenCasts(); |
18096 | 0 | if (const auto *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
18097 | 0 | ND = DRE->getDecl(); |
18098 | 0 | if (const auto *ME = dyn_cast<MemberExpr>(BaseExpr)) |
18099 | 0 | ND = ME->getMemberDecl(); |
18100 | |
|
18101 | 0 | if (ND) |
18102 | 0 | DiagRuntimeBehavior(ND->getBeginLoc(), BaseExpr, |
18103 | 0 | PDiag(diag::note_array_declared_here) << ND); |
18104 | 0 | } |
18105 | | |
18106 | 12 | void Sema::CheckArrayAccess(const Expr *expr) { |
18107 | 12 | int AllowOnePastEnd = 0; |
18108 | 12 | while (expr) { |
18109 | 12 | expr = expr->IgnoreParenImpCasts(); |
18110 | 12 | switch (expr->getStmtClass()) { |
18111 | 0 | case Stmt::ArraySubscriptExprClass: { |
18112 | 0 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr); |
18113 | 0 | CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE, |
18114 | 0 | AllowOnePastEnd > 0); |
18115 | 0 | expr = ASE->getBase(); |
18116 | 0 | break; |
18117 | 0 | } |
18118 | 0 | case Stmt::MemberExprClass: { |
18119 | 0 | expr = cast<MemberExpr>(expr)->getBase(); |
18120 | 0 | break; |
18121 | 0 | } |
18122 | 0 | case Stmt::OMPArraySectionExprClass: { |
18123 | 0 | const OMPArraySectionExpr *ASE = cast<OMPArraySectionExpr>(expr); |
18124 | 0 | if (ASE->getLowerBound()) |
18125 | 0 | CheckArrayAccess(ASE->getBase(), ASE->getLowerBound(), |
18126 | 0 | /*ASE=*/nullptr, AllowOnePastEnd > 0); |
18127 | 0 | return; |
18128 | 0 | } |
18129 | 0 | case Stmt::UnaryOperatorClass: { |
18130 | | // Only unwrap the * and & unary operators |
18131 | 0 | const UnaryOperator *UO = cast<UnaryOperator>(expr); |
18132 | 0 | expr = UO->getSubExpr(); |
18133 | 0 | switch (UO->getOpcode()) { |
18134 | 0 | case UO_AddrOf: |
18135 | 0 | AllowOnePastEnd++; |
18136 | 0 | break; |
18137 | 0 | case UO_Deref: |
18138 | 0 | AllowOnePastEnd--; |
18139 | 0 | break; |
18140 | 0 | default: |
18141 | 0 | return; |
18142 | 0 | } |
18143 | 0 | break; |
18144 | 0 | } |
18145 | 0 | case Stmt::ConditionalOperatorClass: { |
18146 | 0 | const ConditionalOperator *cond = cast<ConditionalOperator>(expr); |
18147 | 0 | if (const Expr *lhs = cond->getLHS()) |
18148 | 0 | CheckArrayAccess(lhs); |
18149 | 0 | if (const Expr *rhs = cond->getRHS()) |
18150 | 0 | CheckArrayAccess(rhs); |
18151 | 0 | return; |
18152 | 0 | } |
18153 | 0 | case Stmt::CXXOperatorCallExprClass: { |
18154 | 0 | const auto *OCE = cast<CXXOperatorCallExpr>(expr); |
18155 | 0 | for (const auto *Arg : OCE->arguments()) |
18156 | 0 | CheckArrayAccess(Arg); |
18157 | 0 | return; |
18158 | 0 | } |
18159 | 12 | default: |
18160 | 12 | return; |
18161 | 12 | } |
18162 | 12 | } |
18163 | 12 | } |
18164 | | |
18165 | | //===--- CHECK: Objective-C retain cycles ----------------------------------// |
18166 | | |
18167 | | namespace { |
18168 | | |
18169 | | struct RetainCycleOwner { |
18170 | | VarDecl *Variable = nullptr; |
18171 | | SourceRange Range; |
18172 | | SourceLocation Loc; |
18173 | | bool Indirect = false; |
18174 | | |
18175 | 0 | RetainCycleOwner() = default; |
18176 | | |
18177 | 0 | void setLocsFrom(Expr *e) { |
18178 | 0 | Loc = e->getExprLoc(); |
18179 | 0 | Range = e->getSourceRange(); |
18180 | 0 | } |
18181 | | }; |
18182 | | |
18183 | | } // namespace |
18184 | | |
18185 | | /// Consider whether capturing the given variable can possibly lead to |
18186 | | /// a retain cycle. |
18187 | 0 | static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) { |
18188 | | // In ARC, it's captured strongly iff the variable has __strong |
18189 | | // lifetime. In MRR, it's captured strongly if the variable is |
18190 | | // __block and has an appropriate type. |
18191 | 0 | if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
18192 | 0 | return false; |
18193 | | |
18194 | 0 | owner.Variable = var; |
18195 | 0 | if (ref) |
18196 | 0 | owner.setLocsFrom(ref); |
18197 | 0 | return true; |
18198 | 0 | } |
18199 | | |
18200 | 0 | static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) { |
18201 | 0 | while (true) { |
18202 | 0 | e = e->IgnoreParens(); |
18203 | 0 | if (CastExpr *cast = dyn_cast<CastExpr>(e)) { |
18204 | 0 | switch (cast->getCastKind()) { |
18205 | 0 | case CK_BitCast: |
18206 | 0 | case CK_LValueBitCast: |
18207 | 0 | case CK_LValueToRValue: |
18208 | 0 | case CK_ARCReclaimReturnedObject: |
18209 | 0 | e = cast->getSubExpr(); |
18210 | 0 | continue; |
18211 | | |
18212 | 0 | default: |
18213 | 0 | return false; |
18214 | 0 | } |
18215 | 0 | } |
18216 | | |
18217 | 0 | if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) { |
18218 | 0 | ObjCIvarDecl *ivar = ref->getDecl(); |
18219 | 0 | if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
18220 | 0 | return false; |
18221 | | |
18222 | | // Try to find a retain cycle in the base. |
18223 | 0 | if (!findRetainCycleOwner(S, ref->getBase(), owner)) |
18224 | 0 | return false; |
18225 | | |
18226 | 0 | if (ref->isFreeIvar()) owner.setLocsFrom(ref); |
18227 | 0 | owner.Indirect = true; |
18228 | 0 | return true; |
18229 | 0 | } |
18230 | | |
18231 | 0 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
18232 | 0 | VarDecl *var = dyn_cast<VarDecl>(ref->getDecl()); |
18233 | 0 | if (!var) return false; |
18234 | 0 | return considerVariable(var, ref, owner); |
18235 | 0 | } |
18236 | | |
18237 | 0 | if (MemberExpr *member = dyn_cast<MemberExpr>(e)) { |
18238 | 0 | if (member->isArrow()) return false; |
18239 | | |
18240 | | // Don't count this as an indirect ownership. |
18241 | 0 | e = member->getBase(); |
18242 | 0 | continue; |
18243 | 0 | } |
18244 | | |
18245 | 0 | if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { |
18246 | | // Only pay attention to pseudo-objects on property references. |
18247 | 0 | ObjCPropertyRefExpr *pre |
18248 | 0 | = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm() |
18249 | 0 | ->IgnoreParens()); |
18250 | 0 | if (!pre) return false; |
18251 | 0 | if (pre->isImplicitProperty()) return false; |
18252 | 0 | ObjCPropertyDecl *property = pre->getExplicitProperty(); |
18253 | 0 | if (!property->isRetaining() && |
18254 | 0 | !(property->getPropertyIvarDecl() && |
18255 | 0 | property->getPropertyIvarDecl()->getType() |
18256 | 0 | .getObjCLifetime() == Qualifiers::OCL_Strong)) |
18257 | 0 | return false; |
18258 | | |
18259 | 0 | owner.Indirect = true; |
18260 | 0 | if (pre->isSuperReceiver()) { |
18261 | 0 | owner.Variable = S.getCurMethodDecl()->getSelfDecl(); |
18262 | 0 | if (!owner.Variable) |
18263 | 0 | return false; |
18264 | 0 | owner.Loc = pre->getLocation(); |
18265 | 0 | owner.Range = pre->getSourceRange(); |
18266 | 0 | return true; |
18267 | 0 | } |
18268 | 0 | e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase()) |
18269 | 0 | ->getSourceExpr()); |
18270 | 0 | continue; |
18271 | 0 | } |
18272 | | |
18273 | | // Array ivars? |
18274 | | |
18275 | 0 | return false; |
18276 | 0 | } |
18277 | 0 | } |
18278 | | |
18279 | | namespace { |
18280 | | |
18281 | | struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> { |
18282 | | VarDecl *Variable; |
18283 | | Expr *Capturer = nullptr; |
18284 | | bool VarWillBeReased = false; |
18285 | | |
18286 | | FindCaptureVisitor(ASTContext &Context, VarDecl *variable) |
18287 | | : EvaluatedExprVisitor<FindCaptureVisitor>(Context), |
18288 | 0 | Variable(variable) {} |
18289 | | |
18290 | 0 | void VisitDeclRefExpr(DeclRefExpr *ref) { |
18291 | 0 | if (ref->getDecl() == Variable && !Capturer) |
18292 | 0 | Capturer = ref; |
18293 | 0 | } |
18294 | | |
18295 | 0 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) { |
18296 | 0 | if (Capturer) return; |
18297 | 0 | Visit(ref->getBase()); |
18298 | 0 | if (Capturer && ref->isFreeIvar()) |
18299 | 0 | Capturer = ref; |
18300 | 0 | } |
18301 | | |
18302 | 0 | void VisitBlockExpr(BlockExpr *block) { |
18303 | | // Look inside nested blocks |
18304 | 0 | if (block->getBlockDecl()->capturesVariable(Variable)) |
18305 | 0 | Visit(block->getBlockDecl()->getBody()); |
18306 | 0 | } |
18307 | | |
18308 | 0 | void VisitOpaqueValueExpr(OpaqueValueExpr *OVE) { |
18309 | 0 | if (Capturer) return; |
18310 | 0 | if (OVE->getSourceExpr()) |
18311 | 0 | Visit(OVE->getSourceExpr()); |
18312 | 0 | } |
18313 | | |
18314 | 0 | void VisitBinaryOperator(BinaryOperator *BinOp) { |
18315 | 0 | if (!Variable || VarWillBeReased || BinOp->getOpcode() != BO_Assign) |
18316 | 0 | return; |
18317 | 0 | Expr *LHS = BinOp->getLHS(); |
18318 | 0 | if (const DeclRefExpr *DRE = dyn_cast_or_null<DeclRefExpr>(LHS)) { |
18319 | 0 | if (DRE->getDecl() != Variable) |
18320 | 0 | return; |
18321 | 0 | if (Expr *RHS = BinOp->getRHS()) { |
18322 | 0 | RHS = RHS->IgnoreParenCasts(); |
18323 | 0 | std::optional<llvm::APSInt> Value; |
18324 | 0 | VarWillBeReased = |
18325 | 0 | (RHS && (Value = RHS->getIntegerConstantExpr(Context)) && |
18326 | 0 | *Value == 0); |
18327 | 0 | } |
18328 | 0 | } |
18329 | 0 | } |
18330 | | }; |
18331 | | |
18332 | | } // namespace |
18333 | | |
18334 | | /// Check whether the given argument is a block which captures a |
18335 | | /// variable. |
18336 | 0 | static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) { |
18337 | 0 | assert(owner.Variable && owner.Loc.isValid()); |
18338 | | |
18339 | 0 | e = e->IgnoreParenCasts(); |
18340 | | |
18341 | | // Look through [^{...} copy] and Block_copy(^{...}). |
18342 | 0 | if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(e)) { |
18343 | 0 | Selector Cmd = ME->getSelector(); |
18344 | 0 | if (Cmd.isUnarySelector() && Cmd.getNameForSlot(0) == "copy") { |
18345 | 0 | e = ME->getInstanceReceiver(); |
18346 | 0 | if (!e) |
18347 | 0 | return nullptr; |
18348 | 0 | e = e->IgnoreParenCasts(); |
18349 | 0 | } |
18350 | 0 | } else if (CallExpr *CE = dyn_cast<CallExpr>(e)) { |
18351 | 0 | if (CE->getNumArgs() == 1) { |
18352 | 0 | FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(CE->getCalleeDecl()); |
18353 | 0 | if (Fn) { |
18354 | 0 | const IdentifierInfo *FnI = Fn->getIdentifier(); |
18355 | 0 | if (FnI && FnI->isStr("_Block_copy")) { |
18356 | 0 | e = CE->getArg(0)->IgnoreParenCasts(); |
18357 | 0 | } |
18358 | 0 | } |
18359 | 0 | } |
18360 | 0 | } |
18361 | | |
18362 | 0 | BlockExpr *block = dyn_cast<BlockExpr>(e); |
18363 | 0 | if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable)) |
18364 | 0 | return nullptr; |
18365 | | |
18366 | 0 | FindCaptureVisitor visitor(S.Context, owner.Variable); |
18367 | 0 | visitor.Visit(block->getBlockDecl()->getBody()); |
18368 | 0 | return visitor.VarWillBeReased ? nullptr : visitor.Capturer; |
18369 | 0 | } |
18370 | | |
18371 | | static void diagnoseRetainCycle(Sema &S, Expr *capturer, |
18372 | 0 | RetainCycleOwner &owner) { |
18373 | 0 | assert(capturer); |
18374 | 0 | assert(owner.Variable && owner.Loc.isValid()); |
18375 | | |
18376 | 0 | S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle) |
18377 | 0 | << owner.Variable << capturer->getSourceRange(); |
18378 | 0 | S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner) |
18379 | 0 | << owner.Indirect << owner.Range; |
18380 | 0 | } |
18381 | | |
18382 | | /// Check for a keyword selector that starts with the word 'add' or |
18383 | | /// 'set'. |
18384 | 0 | static bool isSetterLikeSelector(Selector sel) { |
18385 | 0 | if (sel.isUnarySelector()) return false; |
18386 | | |
18387 | 0 | StringRef str = sel.getNameForSlot(0); |
18388 | 0 | str = str.ltrim('_'); |
18389 | 0 | if (str.starts_with("set")) |
18390 | 0 | str = str.substr(3); |
18391 | 0 | else if (str.starts_with("add")) { |
18392 | | // Specially allow 'addOperationWithBlock:'. |
18393 | 0 | if (sel.getNumArgs() == 1 && str.starts_with("addOperationWithBlock")) |
18394 | 0 | return false; |
18395 | 0 | str = str.substr(3); |
18396 | 0 | } else |
18397 | 0 | return false; |
18398 | | |
18399 | 0 | if (str.empty()) return true; |
18400 | 0 | return !isLowercase(str.front()); |
18401 | 0 | } |
18402 | | |
18403 | | static std::optional<int> |
18404 | 0 | GetNSMutableArrayArgumentIndex(Sema &S, ObjCMessageExpr *Message) { |
18405 | 0 | bool IsMutableArray = S.NSAPIObj->isSubclassOfNSClass( |
18406 | 0 | Message->getReceiverInterface(), |
18407 | 0 | NSAPI::ClassId_NSMutableArray); |
18408 | 0 | if (!IsMutableArray) { |
18409 | 0 | return std::nullopt; |
18410 | 0 | } |
18411 | | |
18412 | 0 | Selector Sel = Message->getSelector(); |
18413 | |
|
18414 | 0 | std::optional<NSAPI::NSArrayMethodKind> MKOpt = |
18415 | 0 | S.NSAPIObj->getNSArrayMethodKind(Sel); |
18416 | 0 | if (!MKOpt) { |
18417 | 0 | return std::nullopt; |
18418 | 0 | } |
18419 | | |
18420 | 0 | NSAPI::NSArrayMethodKind MK = *MKOpt; |
18421 | |
|
18422 | 0 | switch (MK) { |
18423 | 0 | case NSAPI::NSMutableArr_addObject: |
18424 | 0 | case NSAPI::NSMutableArr_insertObjectAtIndex: |
18425 | 0 | case NSAPI::NSMutableArr_setObjectAtIndexedSubscript: |
18426 | 0 | return 0; |
18427 | 0 | case NSAPI::NSMutableArr_replaceObjectAtIndex: |
18428 | 0 | return 1; |
18429 | | |
18430 | 0 | default: |
18431 | 0 | return std::nullopt; |
18432 | 0 | } |
18433 | | |
18434 | 0 | return std::nullopt; |
18435 | 0 | } |
18436 | | |
18437 | | static std::optional<int> |
18438 | 0 | GetNSMutableDictionaryArgumentIndex(Sema &S, ObjCMessageExpr *Message) { |
18439 | 0 | bool IsMutableDictionary = S.NSAPIObj->isSubclassOfNSClass( |
18440 | 0 | Message->getReceiverInterface(), |
18441 | 0 | NSAPI::ClassId_NSMutableDictionary); |
18442 | 0 | if (!IsMutableDictionary) { |
18443 | 0 | return std::nullopt; |
18444 | 0 | } |
18445 | | |
18446 | 0 | Selector Sel = Message->getSelector(); |
18447 | |
|
18448 | 0 | std::optional<NSAPI::NSDictionaryMethodKind> MKOpt = |
18449 | 0 | S.NSAPIObj->getNSDictionaryMethodKind(Sel); |
18450 | 0 | if (!MKOpt) { |
18451 | 0 | return std::nullopt; |
18452 | 0 | } |
18453 | | |
18454 | 0 | NSAPI::NSDictionaryMethodKind MK = *MKOpt; |
18455 | |
|
18456 | 0 | switch (MK) { |
18457 | 0 | case NSAPI::NSMutableDict_setObjectForKey: |
18458 | 0 | case NSAPI::NSMutableDict_setValueForKey: |
18459 | 0 | case NSAPI::NSMutableDict_setObjectForKeyedSubscript: |
18460 | 0 | return 0; |
18461 | | |
18462 | 0 | default: |
18463 | 0 | return std::nullopt; |
18464 | 0 | } |
18465 | | |
18466 | 0 | return std::nullopt; |
18467 | 0 | } |
18468 | | |
18469 | | static std::optional<int> GetNSSetArgumentIndex(Sema &S, |
18470 | 0 | ObjCMessageExpr *Message) { |
18471 | 0 | bool IsMutableSet = S.NSAPIObj->isSubclassOfNSClass( |
18472 | 0 | Message->getReceiverInterface(), |
18473 | 0 | NSAPI::ClassId_NSMutableSet); |
18474 | |
|
18475 | 0 | bool IsMutableOrderedSet = S.NSAPIObj->isSubclassOfNSClass( |
18476 | 0 | Message->getReceiverInterface(), |
18477 | 0 | NSAPI::ClassId_NSMutableOrderedSet); |
18478 | 0 | if (!IsMutableSet && !IsMutableOrderedSet) { |
18479 | 0 | return std::nullopt; |
18480 | 0 | } |
18481 | | |
18482 | 0 | Selector Sel = Message->getSelector(); |
18483 | |
|
18484 | 0 | std::optional<NSAPI::NSSetMethodKind> MKOpt = |
18485 | 0 | S.NSAPIObj->getNSSetMethodKind(Sel); |
18486 | 0 | if (!MKOpt) { |
18487 | 0 | return std::nullopt; |
18488 | 0 | } |
18489 | | |
18490 | 0 | NSAPI::NSSetMethodKind MK = *MKOpt; |
18491 | |
|
18492 | 0 | switch (MK) { |
18493 | 0 | case NSAPI::NSMutableSet_addObject: |
18494 | 0 | case NSAPI::NSOrderedSet_setObjectAtIndex: |
18495 | 0 | case NSAPI::NSOrderedSet_setObjectAtIndexedSubscript: |
18496 | 0 | case NSAPI::NSOrderedSet_insertObjectAtIndex: |
18497 | 0 | return 0; |
18498 | 0 | case NSAPI::NSOrderedSet_replaceObjectAtIndexWithObject: |
18499 | 0 | return 1; |
18500 | 0 | } |
18501 | | |
18502 | 0 | return std::nullopt; |
18503 | 0 | } |
18504 | | |
18505 | 0 | void Sema::CheckObjCCircularContainer(ObjCMessageExpr *Message) { |
18506 | 0 | if (!Message->isInstanceMessage()) { |
18507 | 0 | return; |
18508 | 0 | } |
18509 | | |
18510 | 0 | std::optional<int> ArgOpt; |
18511 | |
|
18512 | 0 | if (!(ArgOpt = GetNSMutableArrayArgumentIndex(*this, Message)) && |
18513 | 0 | !(ArgOpt = GetNSMutableDictionaryArgumentIndex(*this, Message)) && |
18514 | 0 | !(ArgOpt = GetNSSetArgumentIndex(*this, Message))) { |
18515 | 0 | return; |
18516 | 0 | } |
18517 | | |
18518 | 0 | int ArgIndex = *ArgOpt; |
18519 | |
|
18520 | 0 | Expr *Arg = Message->getArg(ArgIndex)->IgnoreImpCasts(); |
18521 | 0 | if (OpaqueValueExpr *OE = dyn_cast<OpaqueValueExpr>(Arg)) { |
18522 | 0 | Arg = OE->getSourceExpr()->IgnoreImpCasts(); |
18523 | 0 | } |
18524 | |
|
18525 | 0 | if (Message->getReceiverKind() == ObjCMessageExpr::SuperInstance) { |
18526 | 0 | if (DeclRefExpr *ArgRE = dyn_cast<DeclRefExpr>(Arg)) { |
18527 | 0 | if (ArgRE->isObjCSelfExpr()) { |
18528 | 0 | Diag(Message->getSourceRange().getBegin(), |
18529 | 0 | diag::warn_objc_circular_container) |
18530 | 0 | << ArgRE->getDecl() << StringRef("'super'"); |
18531 | 0 | } |
18532 | 0 | } |
18533 | 0 | } else { |
18534 | 0 | Expr *Receiver = Message->getInstanceReceiver()->IgnoreImpCasts(); |
18535 | |
|
18536 | 0 | if (OpaqueValueExpr *OE = dyn_cast<OpaqueValueExpr>(Receiver)) { |
18537 | 0 | Receiver = OE->getSourceExpr()->IgnoreImpCasts(); |
18538 | 0 | } |
18539 | |
|
18540 | 0 | if (DeclRefExpr *ReceiverRE = dyn_cast<DeclRefExpr>(Receiver)) { |
18541 | 0 | if (DeclRefExpr *ArgRE = dyn_cast<DeclRefExpr>(Arg)) { |
18542 | 0 | if (ReceiverRE->getDecl() == ArgRE->getDecl()) { |
18543 | 0 | ValueDecl *Decl = ReceiverRE->getDecl(); |
18544 | 0 | Diag(Message->getSourceRange().getBegin(), |
18545 | 0 | diag::warn_objc_circular_container) |
18546 | 0 | << Decl << Decl; |
18547 | 0 | if (!ArgRE->isObjCSelfExpr()) { |
18548 | 0 | Diag(Decl->getLocation(), |
18549 | 0 | diag::note_objc_circular_container_declared_here) |
18550 | 0 | << Decl; |
18551 | 0 | } |
18552 | 0 | } |
18553 | 0 | } |
18554 | 0 | } else if (ObjCIvarRefExpr *IvarRE = dyn_cast<ObjCIvarRefExpr>(Receiver)) { |
18555 | 0 | if (ObjCIvarRefExpr *IvarArgRE = dyn_cast<ObjCIvarRefExpr>(Arg)) { |
18556 | 0 | if (IvarRE->getDecl() == IvarArgRE->getDecl()) { |
18557 | 0 | ObjCIvarDecl *Decl = IvarRE->getDecl(); |
18558 | 0 | Diag(Message->getSourceRange().getBegin(), |
18559 | 0 | diag::warn_objc_circular_container) |
18560 | 0 | << Decl << Decl; |
18561 | 0 | Diag(Decl->getLocation(), |
18562 | 0 | diag::note_objc_circular_container_declared_here) |
18563 | 0 | << Decl; |
18564 | 0 | } |
18565 | 0 | } |
18566 | 0 | } |
18567 | 0 | } |
18568 | 0 | } |
18569 | | |
18570 | | /// Check a message send to see if it's likely to cause a retain cycle. |
18571 | 0 | void Sema::checkRetainCycles(ObjCMessageExpr *msg) { |
18572 | | // Only check instance methods whose selector looks like a setter. |
18573 | 0 | if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector())) |
18574 | 0 | return; |
18575 | | |
18576 | | // Try to find a variable that the receiver is strongly owned by. |
18577 | 0 | RetainCycleOwner owner; |
18578 | 0 | if (msg->getReceiverKind() == ObjCMessageExpr::Instance) { |
18579 | 0 | if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner)) |
18580 | 0 | return; |
18581 | 0 | } else { |
18582 | 0 | assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance); |
18583 | 0 | owner.Variable = getCurMethodDecl()->getSelfDecl(); |
18584 | 0 | owner.Loc = msg->getSuperLoc(); |
18585 | 0 | owner.Range = msg->getSuperLoc(); |
18586 | 0 | } |
18587 | | |
18588 | | // Check whether the receiver is captured by any of the arguments. |
18589 | 0 | const ObjCMethodDecl *MD = msg->getMethodDecl(); |
18590 | 0 | for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) { |
18591 | 0 | if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) { |
18592 | | // noescape blocks should not be retained by the method. |
18593 | 0 | if (MD && MD->parameters()[i]->hasAttr<NoEscapeAttr>()) |
18594 | 0 | continue; |
18595 | 0 | return diagnoseRetainCycle(*this, capturer, owner); |
18596 | 0 | } |
18597 | 0 | } |
18598 | 0 | } |
18599 | | |
18600 | | /// Check a property assign to see if it's likely to cause a retain cycle. |
18601 | 0 | void Sema::checkRetainCycles(Expr *receiver, Expr *argument) { |
18602 | 0 | RetainCycleOwner owner; |
18603 | 0 | if (!findRetainCycleOwner(*this, receiver, owner)) |
18604 | 0 | return; |
18605 | | |
18606 | 0 | if (Expr *capturer = findCapturingExpr(*this, argument, owner)) |
18607 | 0 | diagnoseRetainCycle(*this, capturer, owner); |
18608 | 0 | } |
18609 | | |
18610 | 0 | void Sema::checkRetainCycles(VarDecl *Var, Expr *Init) { |
18611 | 0 | RetainCycleOwner Owner; |
18612 | 0 | if (!considerVariable(Var, /*DeclRefExpr=*/nullptr, Owner)) |
18613 | 0 | return; |
18614 | | |
18615 | | // Because we don't have an expression for the variable, we have to set the |
18616 | | // location explicitly here. |
18617 | 0 | Owner.Loc = Var->getLocation(); |
18618 | 0 | Owner.Range = Var->getSourceRange(); |
18619 | |
|
18620 | 0 | if (Expr *Capturer = findCapturingExpr(*this, Init, Owner)) |
18621 | 0 | diagnoseRetainCycle(*this, Capturer, Owner); |
18622 | 0 | } |
18623 | | |
18624 | | static bool checkUnsafeAssignLiteral(Sema &S, SourceLocation Loc, |
18625 | 0 | Expr *RHS, bool isProperty) { |
18626 | | // Check if RHS is an Objective-C object literal, which also can get |
18627 | | // immediately zapped in a weak reference. Note that we explicitly |
18628 | | // allow ObjCStringLiterals, since those are designed to never really die. |
18629 | 0 | RHS = RHS->IgnoreParenImpCasts(); |
18630 | | |
18631 | | // This enum needs to match with the 'select' in |
18632 | | // warn_objc_arc_literal_assign (off-by-1). |
18633 | 0 | Sema::ObjCLiteralKind Kind = S.CheckLiteralKind(RHS); |
18634 | 0 | if (Kind == Sema::LK_String || Kind == Sema::LK_None) |
18635 | 0 | return false; |
18636 | | |
18637 | 0 | S.Diag(Loc, diag::warn_arc_literal_assign) |
18638 | 0 | << (unsigned) Kind |
18639 | 0 | << (isProperty ? 0 : 1) |
18640 | 0 | << RHS->getSourceRange(); |
18641 | |
|
18642 | 0 | return true; |
18643 | 0 | } |
18644 | | |
18645 | | static bool checkUnsafeAssignObject(Sema &S, SourceLocation Loc, |
18646 | | Qualifiers::ObjCLifetime LT, |
18647 | 0 | Expr *RHS, bool isProperty) { |
18648 | | // Strip off any implicit cast added to get to the one ARC-specific. |
18649 | 0 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
18650 | 0 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
18651 | 0 | S.Diag(Loc, diag::warn_arc_retained_assign) |
18652 | 0 | << (LT == Qualifiers::OCL_ExplicitNone) |
18653 | 0 | << (isProperty ? 0 : 1) |
18654 | 0 | << RHS->getSourceRange(); |
18655 | 0 | return true; |
18656 | 0 | } |
18657 | 0 | RHS = cast->getSubExpr(); |
18658 | 0 | } |
18659 | | |
18660 | 0 | if (LT == Qualifiers::OCL_Weak && |
18661 | 0 | checkUnsafeAssignLiteral(S, Loc, RHS, isProperty)) |
18662 | 0 | return true; |
18663 | | |
18664 | 0 | return false; |
18665 | 0 | } |
18666 | | |
18667 | | bool Sema::checkUnsafeAssigns(SourceLocation Loc, |
18668 | 20 | QualType LHS, Expr *RHS) { |
18669 | 20 | Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime(); |
18670 | | |
18671 | 20 | if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone) |
18672 | 20 | return false; |
18673 | | |
18674 | 0 | if (checkUnsafeAssignObject(*this, Loc, LT, RHS, false)) |
18675 | 0 | return true; |
18676 | | |
18677 | 0 | return false; |
18678 | 0 | } |
18679 | | |
18680 | | void Sema::checkUnsafeExprAssigns(SourceLocation Loc, |
18681 | 0 | Expr *LHS, Expr *RHS) { |
18682 | 0 | QualType LHSType; |
18683 | | // PropertyRef on LHS type need be directly obtained from |
18684 | | // its declaration as it has a PseudoType. |
18685 | 0 | ObjCPropertyRefExpr *PRE |
18686 | 0 | = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens()); |
18687 | 0 | if (PRE && !PRE->isImplicitProperty()) { |
18688 | 0 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
18689 | 0 | if (PD) |
18690 | 0 | LHSType = PD->getType(); |
18691 | 0 | } |
18692 | |
|
18693 | 0 | if (LHSType.isNull()) |
18694 | 0 | LHSType = LHS->getType(); |
18695 | |
|
18696 | 0 | Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime(); |
18697 | |
|
18698 | 0 | if (LT == Qualifiers::OCL_Weak) { |
18699 | 0 | if (!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, Loc)) |
18700 | 0 | getCurFunction()->markSafeWeakUse(LHS); |
18701 | 0 | } |
18702 | |
|
18703 | 0 | if (checkUnsafeAssigns(Loc, LHSType, RHS)) |
18704 | 0 | return; |
18705 | | |
18706 | | // FIXME. Check for other life times. |
18707 | 0 | if (LT != Qualifiers::OCL_None) |
18708 | 0 | return; |
18709 | | |
18710 | 0 | if (PRE) { |
18711 | 0 | if (PRE->isImplicitProperty()) |
18712 | 0 | return; |
18713 | 0 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
18714 | 0 | if (!PD) |
18715 | 0 | return; |
18716 | | |
18717 | 0 | unsigned Attributes = PD->getPropertyAttributes(); |
18718 | 0 | if (Attributes & ObjCPropertyAttribute::kind_assign) { |
18719 | | // when 'assign' attribute was not explicitly specified |
18720 | | // by user, ignore it and rely on property type itself |
18721 | | // for lifetime info. |
18722 | 0 | unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten(); |
18723 | 0 | if (!(AsWrittenAttr & ObjCPropertyAttribute::kind_assign) && |
18724 | 0 | LHSType->isObjCRetainableType()) |
18725 | 0 | return; |
18726 | | |
18727 | 0 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
18728 | 0 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
18729 | 0 | Diag(Loc, diag::warn_arc_retained_property_assign) |
18730 | 0 | << RHS->getSourceRange(); |
18731 | 0 | return; |
18732 | 0 | } |
18733 | 0 | RHS = cast->getSubExpr(); |
18734 | 0 | } |
18735 | 0 | } else if (Attributes & ObjCPropertyAttribute::kind_weak) { |
18736 | 0 | if (checkUnsafeAssignObject(*this, Loc, Qualifiers::OCL_Weak, RHS, true)) |
18737 | 0 | return; |
18738 | 0 | } |
18739 | 0 | } |
18740 | 0 | } |
18741 | | |
18742 | | //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===// |
18743 | | |
18744 | | static bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr, |
18745 | | SourceLocation StmtLoc, |
18746 | 0 | const NullStmt *Body) { |
18747 | | // Do not warn if the body is a macro that expands to nothing, e.g: |
18748 | | // |
18749 | | // #define CALL(x) |
18750 | | // if (condition) |
18751 | | // CALL(0); |
18752 | 0 | if (Body->hasLeadingEmptyMacro()) |
18753 | 0 | return false; |
18754 | | |
18755 | | // Get line numbers of statement and body. |
18756 | 0 | bool StmtLineInvalid; |
18757 | 0 | unsigned StmtLine = SourceMgr.getPresumedLineNumber(StmtLoc, |
18758 | 0 | &StmtLineInvalid); |
18759 | 0 | if (StmtLineInvalid) |
18760 | 0 | return false; |
18761 | | |
18762 | 0 | bool BodyLineInvalid; |
18763 | 0 | unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(), |
18764 | 0 | &BodyLineInvalid); |
18765 | 0 | if (BodyLineInvalid) |
18766 | 0 | return false; |
18767 | | |
18768 | | // Warn if null statement and body are on the same line. |
18769 | 0 | if (StmtLine != BodyLine) |
18770 | 0 | return false; |
18771 | | |
18772 | 0 | return true; |
18773 | 0 | } |
18774 | | |
18775 | | void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc, |
18776 | | const Stmt *Body, |
18777 | 0 | unsigned DiagID) { |
18778 | | // Since this is a syntactic check, don't emit diagnostic for template |
18779 | | // instantiations, this just adds noise. |
18780 | 0 | if (CurrentInstantiationScope) |
18781 | 0 | return; |
18782 | | |
18783 | | // The body should be a null statement. |
18784 | 0 | const NullStmt *NBody = dyn_cast<NullStmt>(Body); |
18785 | 0 | if (!NBody) |
18786 | 0 | return; |
18787 | | |
18788 | | // Do the usual checks. |
18789 | 0 | if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) |
18790 | 0 | return; |
18791 | | |
18792 | 0 | Diag(NBody->getSemiLoc(), DiagID); |
18793 | 0 | Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); |
18794 | 0 | } |
18795 | | |
18796 | | void Sema::DiagnoseEmptyLoopBody(const Stmt *S, |
18797 | 0 | const Stmt *PossibleBody) { |
18798 | 0 | assert(!CurrentInstantiationScope); // Ensured by caller |
18799 | | |
18800 | 0 | SourceLocation StmtLoc; |
18801 | 0 | const Stmt *Body; |
18802 | 0 | unsigned DiagID; |
18803 | 0 | if (const ForStmt *FS = dyn_cast<ForStmt>(S)) { |
18804 | 0 | StmtLoc = FS->getRParenLoc(); |
18805 | 0 | Body = FS->getBody(); |
18806 | 0 | DiagID = diag::warn_empty_for_body; |
18807 | 0 | } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) { |
18808 | 0 | StmtLoc = WS->getRParenLoc(); |
18809 | 0 | Body = WS->getBody(); |
18810 | 0 | DiagID = diag::warn_empty_while_body; |
18811 | 0 | } else |
18812 | 0 | return; // Neither `for' nor `while'. |
18813 | | |
18814 | | // The body should be a null statement. |
18815 | 0 | const NullStmt *NBody = dyn_cast<NullStmt>(Body); |
18816 | 0 | if (!NBody) |
18817 | 0 | return; |
18818 | | |
18819 | | // Skip expensive checks if diagnostic is disabled. |
18820 | 0 | if (Diags.isIgnored(DiagID, NBody->getSemiLoc())) |
18821 | 0 | return; |
18822 | | |
18823 | | // Do the usual checks. |
18824 | 0 | if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) |
18825 | 0 | return; |
18826 | | |
18827 | | // `for(...);' and `while(...);' are popular idioms, so in order to keep |
18828 | | // noise level low, emit diagnostics only if for/while is followed by a |
18829 | | // CompoundStmt, e.g.: |
18830 | | // for (int i = 0; i < n; i++); |
18831 | | // { |
18832 | | // a(i); |
18833 | | // } |
18834 | | // or if for/while is followed by a statement with more indentation |
18835 | | // than for/while itself: |
18836 | | // for (int i = 0; i < n; i++); |
18837 | | // a(i); |
18838 | 0 | bool ProbableTypo = isa<CompoundStmt>(PossibleBody); |
18839 | 0 | if (!ProbableTypo) { |
18840 | 0 | bool BodyColInvalid; |
18841 | 0 | unsigned BodyCol = SourceMgr.getPresumedColumnNumber( |
18842 | 0 | PossibleBody->getBeginLoc(), &BodyColInvalid); |
18843 | 0 | if (BodyColInvalid) |
18844 | 0 | return; |
18845 | | |
18846 | 0 | bool StmtColInvalid; |
18847 | 0 | unsigned StmtCol = |
18848 | 0 | SourceMgr.getPresumedColumnNumber(S->getBeginLoc(), &StmtColInvalid); |
18849 | 0 | if (StmtColInvalid) |
18850 | 0 | return; |
18851 | | |
18852 | 0 | if (BodyCol > StmtCol) |
18853 | 0 | ProbableTypo = true; |
18854 | 0 | } |
18855 | | |
18856 | 0 | if (ProbableTypo) { |
18857 | 0 | Diag(NBody->getSemiLoc(), DiagID); |
18858 | 0 | Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); |
18859 | 0 | } |
18860 | 0 | } |
18861 | | |
18862 | | //===--- CHECK: Warn on self move with std::move. -------------------------===// |
18863 | | |
18864 | | /// DiagnoseSelfMove - Emits a warning if a value is moved to itself. |
18865 | | void Sema::DiagnoseSelfMove(const Expr *LHSExpr, const Expr *RHSExpr, |
18866 | 0 | SourceLocation OpLoc) { |
18867 | 0 | if (Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, OpLoc)) |
18868 | 0 | return; |
18869 | | |
18870 | 0 | if (inTemplateInstantiation()) |
18871 | 0 | return; |
18872 | | |
18873 | | // Strip parens and casts away. |
18874 | 0 | LHSExpr = LHSExpr->IgnoreParenImpCasts(); |
18875 | 0 | RHSExpr = RHSExpr->IgnoreParenImpCasts(); |
18876 | | |
18877 | | // Check for a call expression |
18878 | 0 | const CallExpr *CE = dyn_cast<CallExpr>(RHSExpr); |
18879 | 0 | if (!CE || CE->getNumArgs() != 1) |
18880 | 0 | return; |
18881 | | |
18882 | | // Check for a call to std::move |
18883 | 0 | if (!CE->isCallToStdMove()) |
18884 | 0 | return; |
18885 | | |
18886 | | // Get argument from std::move |
18887 | 0 | RHSExpr = CE->getArg(0); |
18888 | |
|
18889 | 0 | const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr); |
18890 | 0 | const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr); |
18891 | | |
18892 | | // Two DeclRefExpr's, check that the decls are the same. |
18893 | 0 | if (LHSDeclRef && RHSDeclRef) { |
18894 | 0 | if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl()) |
18895 | 0 | return; |
18896 | 0 | if (LHSDeclRef->getDecl()->getCanonicalDecl() != |
18897 | 0 | RHSDeclRef->getDecl()->getCanonicalDecl()) |
18898 | 0 | return; |
18899 | | |
18900 | 0 | auto D = Diag(OpLoc, diag::warn_self_move) |
18901 | 0 | << LHSExpr->getType() << LHSExpr->getSourceRange() |
18902 | 0 | << RHSExpr->getSourceRange(); |
18903 | 0 | if (const FieldDecl *F = |
18904 | 0 | getSelfAssignmentClassMemberCandidate(RHSDeclRef->getDecl())) |
18905 | 0 | D << 1 << F |
18906 | 0 | << FixItHint::CreateInsertion(LHSDeclRef->getBeginLoc(), "this->"); |
18907 | 0 | else |
18908 | 0 | D << 0; |
18909 | 0 | return; |
18910 | 0 | } |
18911 | | |
18912 | | // Member variables require a different approach to check for self moves. |
18913 | | // MemberExpr's are the same if every nested MemberExpr refers to the same |
18914 | | // Decl and that the base Expr's are DeclRefExpr's with the same Decl or |
18915 | | // the base Expr's are CXXThisExpr's. |
18916 | 0 | const Expr *LHSBase = LHSExpr; |
18917 | 0 | const Expr *RHSBase = RHSExpr; |
18918 | 0 | const MemberExpr *LHSME = dyn_cast<MemberExpr>(LHSExpr); |
18919 | 0 | const MemberExpr *RHSME = dyn_cast<MemberExpr>(RHSExpr); |
18920 | 0 | if (!LHSME || !RHSME) |
18921 | 0 | return; |
18922 | | |
18923 | 0 | while (LHSME && RHSME) { |
18924 | 0 | if (LHSME->getMemberDecl()->getCanonicalDecl() != |
18925 | 0 | RHSME->getMemberDecl()->getCanonicalDecl()) |
18926 | 0 | return; |
18927 | | |
18928 | 0 | LHSBase = LHSME->getBase(); |
18929 | 0 | RHSBase = RHSME->getBase(); |
18930 | 0 | LHSME = dyn_cast<MemberExpr>(LHSBase); |
18931 | 0 | RHSME = dyn_cast<MemberExpr>(RHSBase); |
18932 | 0 | } |
18933 | | |
18934 | 0 | LHSDeclRef = dyn_cast<DeclRefExpr>(LHSBase); |
18935 | 0 | RHSDeclRef = dyn_cast<DeclRefExpr>(RHSBase); |
18936 | 0 | if (LHSDeclRef && RHSDeclRef) { |
18937 | 0 | if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl()) |
18938 | 0 | return; |
18939 | 0 | if (LHSDeclRef->getDecl()->getCanonicalDecl() != |
18940 | 0 | RHSDeclRef->getDecl()->getCanonicalDecl()) |
18941 | 0 | return; |
18942 | | |
18943 | 0 | Diag(OpLoc, diag::warn_self_move) |
18944 | 0 | << LHSExpr->getType() << 0 << LHSExpr->getSourceRange() |
18945 | 0 | << RHSExpr->getSourceRange(); |
18946 | 0 | return; |
18947 | 0 | } |
18948 | | |
18949 | 0 | if (isa<CXXThisExpr>(LHSBase) && isa<CXXThisExpr>(RHSBase)) |
18950 | 0 | Diag(OpLoc, diag::warn_self_move) |
18951 | 0 | << LHSExpr->getType() << 0 << LHSExpr->getSourceRange() |
18952 | 0 | << RHSExpr->getSourceRange(); |
18953 | 0 | } |
18954 | | |
18955 | | //===--- Layout compatibility ----------------------------------------------// |
18956 | | |
18957 | | static bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2); |
18958 | | |
18959 | | /// Check if two enumeration types are layout-compatible. |
18960 | 0 | static bool isLayoutCompatible(ASTContext &C, EnumDecl *ED1, EnumDecl *ED2) { |
18961 | | // C++11 [dcl.enum] p8: |
18962 | | // Two enumeration types are layout-compatible if they have the same |
18963 | | // underlying type. |
18964 | 0 | return ED1->isComplete() && ED2->isComplete() && |
18965 | 0 | C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType()); |
18966 | 0 | } |
18967 | | |
18968 | | /// Check if two fields are layout-compatible. |
18969 | | static bool isLayoutCompatible(ASTContext &C, FieldDecl *Field1, |
18970 | 0 | FieldDecl *Field2) { |
18971 | 0 | if (!isLayoutCompatible(C, Field1->getType(), Field2->getType())) |
18972 | 0 | return false; |
18973 | | |
18974 | 0 | if (Field1->isBitField() != Field2->isBitField()) |
18975 | 0 | return false; |
18976 | | |
18977 | 0 | if (Field1->isBitField()) { |
18978 | | // Make sure that the bit-fields are the same length. |
18979 | 0 | unsigned Bits1 = Field1->getBitWidthValue(C); |
18980 | 0 | unsigned Bits2 = Field2->getBitWidthValue(C); |
18981 | |
|
18982 | 0 | if (Bits1 != Bits2) |
18983 | 0 | return false; |
18984 | 0 | } |
18985 | | |
18986 | 0 | return true; |
18987 | 0 | } |
18988 | | |
18989 | | /// Check if two standard-layout structs are layout-compatible. |
18990 | | /// (C++11 [class.mem] p17) |
18991 | | static bool isLayoutCompatibleStruct(ASTContext &C, RecordDecl *RD1, |
18992 | 0 | RecordDecl *RD2) { |
18993 | | // If both records are C++ classes, check that base classes match. |
18994 | 0 | if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1)) { |
18995 | | // If one of records is a CXXRecordDecl we are in C++ mode, |
18996 | | // thus the other one is a CXXRecordDecl, too. |
18997 | 0 | const CXXRecordDecl *D2CXX = cast<CXXRecordDecl>(RD2); |
18998 | | // Check number of base classes. |
18999 | 0 | if (D1CXX->getNumBases() != D2CXX->getNumBases()) |
19000 | 0 | return false; |
19001 | | |
19002 | | // Check the base classes. |
19003 | 0 | for (CXXRecordDecl::base_class_const_iterator |
19004 | 0 | Base1 = D1CXX->bases_begin(), |
19005 | 0 | BaseEnd1 = D1CXX->bases_end(), |
19006 | 0 | Base2 = D2CXX->bases_begin(); |
19007 | 0 | Base1 != BaseEnd1; |
19008 | 0 | ++Base1, ++Base2) { |
19009 | 0 | if (!isLayoutCompatible(C, Base1->getType(), Base2->getType())) |
19010 | 0 | return false; |
19011 | 0 | } |
19012 | 0 | } else if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2)) { |
19013 | | // If only RD2 is a C++ class, it should have zero base classes. |
19014 | 0 | if (D2CXX->getNumBases() > 0) |
19015 | 0 | return false; |
19016 | 0 | } |
19017 | | |
19018 | | // Check the fields. |
19019 | 0 | RecordDecl::field_iterator Field2 = RD2->field_begin(), |
19020 | 0 | Field2End = RD2->field_end(), |
19021 | 0 | Field1 = RD1->field_begin(), |
19022 | 0 | Field1End = RD1->field_end(); |
19023 | 0 | for ( ; Field1 != Field1End && Field2 != Field2End; ++Field1, ++Field2) { |
19024 | 0 | if (!isLayoutCompatible(C, *Field1, *Field2)) |
19025 | 0 | return false; |
19026 | 0 | } |
19027 | 0 | if (Field1 != Field1End || Field2 != Field2End) |
19028 | 0 | return false; |
19029 | | |
19030 | 0 | return true; |
19031 | 0 | } |
19032 | | |
19033 | | /// Check if two standard-layout unions are layout-compatible. |
19034 | | /// (C++11 [class.mem] p18) |
19035 | | static bool isLayoutCompatibleUnion(ASTContext &C, RecordDecl *RD1, |
19036 | 0 | RecordDecl *RD2) { |
19037 | 0 | llvm::SmallPtrSet<FieldDecl *, 8> UnmatchedFields; |
19038 | 0 | for (auto *Field2 : RD2->fields()) |
19039 | 0 | UnmatchedFields.insert(Field2); |
19040 | |
|
19041 | 0 | for (auto *Field1 : RD1->fields()) { |
19042 | 0 | llvm::SmallPtrSet<FieldDecl *, 8>::iterator |
19043 | 0 | I = UnmatchedFields.begin(), |
19044 | 0 | E = UnmatchedFields.end(); |
19045 | |
|
19046 | 0 | for ( ; I != E; ++I) { |
19047 | 0 | if (isLayoutCompatible(C, Field1, *I)) { |
19048 | 0 | bool Result = UnmatchedFields.erase(*I); |
19049 | 0 | (void) Result; |
19050 | 0 | assert(Result); |
19051 | 0 | break; |
19052 | 0 | } |
19053 | 0 | } |
19054 | 0 | if (I == E) |
19055 | 0 | return false; |
19056 | 0 | } |
19057 | | |
19058 | 0 | return UnmatchedFields.empty(); |
19059 | 0 | } |
19060 | | |
19061 | | static bool isLayoutCompatible(ASTContext &C, RecordDecl *RD1, |
19062 | 0 | RecordDecl *RD2) { |
19063 | 0 | if (RD1->isUnion() != RD2->isUnion()) |
19064 | 0 | return false; |
19065 | | |
19066 | 0 | if (RD1->isUnion()) |
19067 | 0 | return isLayoutCompatibleUnion(C, RD1, RD2); |
19068 | 0 | else |
19069 | 0 | return isLayoutCompatibleStruct(C, RD1, RD2); |
19070 | 0 | } |
19071 | | |
19072 | | /// Check if two types are layout-compatible in C++11 sense. |
19073 | 0 | static bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2) { |
19074 | 0 | if (T1.isNull() || T2.isNull()) |
19075 | 0 | return false; |
19076 | | |
19077 | | // C++11 [basic.types] p11: |
19078 | | // If two types T1 and T2 are the same type, then T1 and T2 are |
19079 | | // layout-compatible types. |
19080 | 0 | if (C.hasSameType(T1, T2)) |
19081 | 0 | return true; |
19082 | | |
19083 | 0 | T1 = T1.getCanonicalType().getUnqualifiedType(); |
19084 | 0 | T2 = T2.getCanonicalType().getUnqualifiedType(); |
19085 | |
|
19086 | 0 | const Type::TypeClass TC1 = T1->getTypeClass(); |
19087 | 0 | const Type::TypeClass TC2 = T2->getTypeClass(); |
19088 | |
|
19089 | 0 | if (TC1 != TC2) |
19090 | 0 | return false; |
19091 | | |
19092 | 0 | if (TC1 == Type::Enum) { |
19093 | 0 | return isLayoutCompatible(C, |
19094 | 0 | cast<EnumType>(T1)->getDecl(), |
19095 | 0 | cast<EnumType>(T2)->getDecl()); |
19096 | 0 | } else if (TC1 == Type::Record) { |
19097 | 0 | if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType()) |
19098 | 0 | return false; |
19099 | | |
19100 | 0 | return isLayoutCompatible(C, |
19101 | 0 | cast<RecordType>(T1)->getDecl(), |
19102 | 0 | cast<RecordType>(T2)->getDecl()); |
19103 | 0 | } |
19104 | | |
19105 | 0 | return false; |
19106 | 0 | } |
19107 | | |
19108 | | //===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----// |
19109 | | |
19110 | | /// Given a type tag expression find the type tag itself. |
19111 | | /// |
19112 | | /// \param TypeExpr Type tag expression, as it appears in user's code. |
19113 | | /// |
19114 | | /// \param VD Declaration of an identifier that appears in a type tag. |
19115 | | /// |
19116 | | /// \param MagicValue Type tag magic value. |
19117 | | /// |
19118 | | /// \param isConstantEvaluated whether the evalaution should be performed in |
19119 | | |
19120 | | /// constant context. |
19121 | | static bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx, |
19122 | | const ValueDecl **VD, uint64_t *MagicValue, |
19123 | 0 | bool isConstantEvaluated) { |
19124 | 0 | while(true) { |
19125 | 0 | if (!TypeExpr) |
19126 | 0 | return false; |
19127 | | |
19128 | 0 | TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts(); |
19129 | |
|
19130 | 0 | switch (TypeExpr->getStmtClass()) { |
19131 | 0 | case Stmt::UnaryOperatorClass: { |
19132 | 0 | const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr); |
19133 | 0 | if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) { |
19134 | 0 | TypeExpr = UO->getSubExpr(); |
19135 | 0 | continue; |
19136 | 0 | } |
19137 | 0 | return false; |
19138 | 0 | } |
19139 | | |
19140 | 0 | case Stmt::DeclRefExprClass: { |
19141 | 0 | const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr); |
19142 | 0 | *VD = DRE->getDecl(); |
19143 | 0 | return true; |
19144 | 0 | } |
19145 | | |
19146 | 0 | case Stmt::IntegerLiteralClass: { |
19147 | 0 | const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr); |
19148 | 0 | llvm::APInt MagicValueAPInt = IL->getValue(); |
19149 | 0 | if (MagicValueAPInt.getActiveBits() <= 64) { |
19150 | 0 | *MagicValue = MagicValueAPInt.getZExtValue(); |
19151 | 0 | return true; |
19152 | 0 | } else |
19153 | 0 | return false; |
19154 | 0 | } |
19155 | | |
19156 | 0 | case Stmt::BinaryConditionalOperatorClass: |
19157 | 0 | case Stmt::ConditionalOperatorClass: { |
19158 | 0 | const AbstractConditionalOperator *ACO = |
19159 | 0 | cast<AbstractConditionalOperator>(TypeExpr); |
19160 | 0 | bool Result; |
19161 | 0 | if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx, |
19162 | 0 | isConstantEvaluated)) { |
19163 | 0 | if (Result) |
19164 | 0 | TypeExpr = ACO->getTrueExpr(); |
19165 | 0 | else |
19166 | 0 | TypeExpr = ACO->getFalseExpr(); |
19167 | 0 | continue; |
19168 | 0 | } |
19169 | 0 | return false; |
19170 | 0 | } |
19171 | | |
19172 | 0 | case Stmt::BinaryOperatorClass: { |
19173 | 0 | const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr); |
19174 | 0 | if (BO->getOpcode() == BO_Comma) { |
19175 | 0 | TypeExpr = BO->getRHS(); |
19176 | 0 | continue; |
19177 | 0 | } |
19178 | 0 | return false; |
19179 | 0 | } |
19180 | | |
19181 | 0 | default: |
19182 | 0 | return false; |
19183 | 0 | } |
19184 | 0 | } |
19185 | 0 | } |
19186 | | |
19187 | | /// Retrieve the C type corresponding to type tag TypeExpr. |
19188 | | /// |
19189 | | /// \param TypeExpr Expression that specifies a type tag. |
19190 | | /// |
19191 | | /// \param MagicValues Registered magic values. |
19192 | | /// |
19193 | | /// \param FoundWrongKind Set to true if a type tag was found, but of a wrong |
19194 | | /// kind. |
19195 | | /// |
19196 | | /// \param TypeInfo Information about the corresponding C type. |
19197 | | /// |
19198 | | /// \param isConstantEvaluated whether the evalaution should be performed in |
19199 | | /// constant context. |
19200 | | /// |
19201 | | /// \returns true if the corresponding C type was found. |
19202 | | static bool GetMatchingCType( |
19203 | | const IdentifierInfo *ArgumentKind, const Expr *TypeExpr, |
19204 | | const ASTContext &Ctx, |
19205 | | const llvm::DenseMap<Sema::TypeTagMagicValue, Sema::TypeTagData> |
19206 | | *MagicValues, |
19207 | | bool &FoundWrongKind, Sema::TypeTagData &TypeInfo, |
19208 | 0 | bool isConstantEvaluated) { |
19209 | 0 | FoundWrongKind = false; |
19210 | | |
19211 | | // Variable declaration that has type_tag_for_datatype attribute. |
19212 | 0 | const ValueDecl *VD = nullptr; |
19213 | |
|
19214 | 0 | uint64_t MagicValue; |
19215 | |
|
19216 | 0 | if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue, isConstantEvaluated)) |
19217 | 0 | return false; |
19218 | | |
19219 | 0 | if (VD) { |
19220 | 0 | if (TypeTagForDatatypeAttr *I = VD->getAttr<TypeTagForDatatypeAttr>()) { |
19221 | 0 | if (I->getArgumentKind() != ArgumentKind) { |
19222 | 0 | FoundWrongKind = true; |
19223 | 0 | return false; |
19224 | 0 | } |
19225 | 0 | TypeInfo.Type = I->getMatchingCType(); |
19226 | 0 | TypeInfo.LayoutCompatible = I->getLayoutCompatible(); |
19227 | 0 | TypeInfo.MustBeNull = I->getMustBeNull(); |
19228 | 0 | return true; |
19229 | 0 | } |
19230 | 0 | return false; |
19231 | 0 | } |
19232 | | |
19233 | 0 | if (!MagicValues) |
19234 | 0 | return false; |
19235 | | |
19236 | 0 | llvm::DenseMap<Sema::TypeTagMagicValue, |
19237 | 0 | Sema::TypeTagData>::const_iterator I = |
19238 | 0 | MagicValues->find(std::make_pair(ArgumentKind, MagicValue)); |
19239 | 0 | if (I == MagicValues->end()) |
19240 | 0 | return false; |
19241 | | |
19242 | 0 | TypeInfo = I->second; |
19243 | 0 | return true; |
19244 | 0 | } |
19245 | | |
19246 | | void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind, |
19247 | | uint64_t MagicValue, QualType Type, |
19248 | | bool LayoutCompatible, |
19249 | 0 | bool MustBeNull) { |
19250 | 0 | if (!TypeTagForDatatypeMagicValues) |
19251 | 0 | TypeTagForDatatypeMagicValues.reset( |
19252 | 0 | new llvm::DenseMap<TypeTagMagicValue, TypeTagData>); |
19253 | |
|
19254 | 0 | TypeTagMagicValue Magic(ArgumentKind, MagicValue); |
19255 | 0 | (*TypeTagForDatatypeMagicValues)[Magic] = |
19256 | 0 | TypeTagData(Type, LayoutCompatible, MustBeNull); |
19257 | 0 | } |
19258 | | |
19259 | 0 | static bool IsSameCharType(QualType T1, QualType T2) { |
19260 | 0 | const BuiltinType *BT1 = T1->getAs<BuiltinType>(); |
19261 | 0 | if (!BT1) |
19262 | 0 | return false; |
19263 | | |
19264 | 0 | const BuiltinType *BT2 = T2->getAs<BuiltinType>(); |
19265 | 0 | if (!BT2) |
19266 | 0 | return false; |
19267 | | |
19268 | 0 | BuiltinType::Kind T1Kind = BT1->getKind(); |
19269 | 0 | BuiltinType::Kind T2Kind = BT2->getKind(); |
19270 | |
|
19271 | 0 | return (T1Kind == BuiltinType::SChar && T2Kind == BuiltinType::Char_S) || |
19272 | 0 | (T1Kind == BuiltinType::UChar && T2Kind == BuiltinType::Char_U) || |
19273 | 0 | (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) || |
19274 | 0 | (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar); |
19275 | 0 | } |
19276 | | |
19277 | | void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr, |
19278 | | const ArrayRef<const Expr *> ExprArgs, |
19279 | 0 | SourceLocation CallSiteLoc) { |
19280 | 0 | const IdentifierInfo *ArgumentKind = Attr->getArgumentKind(); |
19281 | 0 | bool IsPointerAttr = Attr->getIsPointer(); |
19282 | | |
19283 | | // Retrieve the argument representing the 'type_tag'. |
19284 | 0 | unsigned TypeTagIdxAST = Attr->getTypeTagIdx().getASTIndex(); |
19285 | 0 | if (TypeTagIdxAST >= ExprArgs.size()) { |
19286 | 0 | Diag(CallSiteLoc, diag::err_tag_index_out_of_range) |
19287 | 0 | << 0 << Attr->getTypeTagIdx().getSourceIndex(); |
19288 | 0 | return; |
19289 | 0 | } |
19290 | 0 | const Expr *TypeTagExpr = ExprArgs[TypeTagIdxAST]; |
19291 | 0 | bool FoundWrongKind; |
19292 | 0 | TypeTagData TypeInfo; |
19293 | 0 | if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context, |
19294 | 0 | TypeTagForDatatypeMagicValues.get(), FoundWrongKind, |
19295 | 0 | TypeInfo, isConstantEvaluatedContext())) { |
19296 | 0 | if (FoundWrongKind) |
19297 | 0 | Diag(TypeTagExpr->getExprLoc(), |
19298 | 0 | diag::warn_type_tag_for_datatype_wrong_kind) |
19299 | 0 | << TypeTagExpr->getSourceRange(); |
19300 | 0 | return; |
19301 | 0 | } |
19302 | | |
19303 | | // Retrieve the argument representing the 'arg_idx'. |
19304 | 0 | unsigned ArgumentIdxAST = Attr->getArgumentIdx().getASTIndex(); |
19305 | 0 | if (ArgumentIdxAST >= ExprArgs.size()) { |
19306 | 0 | Diag(CallSiteLoc, diag::err_tag_index_out_of_range) |
19307 | 0 | << 1 << Attr->getArgumentIdx().getSourceIndex(); |
19308 | 0 | return; |
19309 | 0 | } |
19310 | 0 | const Expr *ArgumentExpr = ExprArgs[ArgumentIdxAST]; |
19311 | 0 | if (IsPointerAttr) { |
19312 | | // Skip implicit cast of pointer to `void *' (as a function argument). |
19313 | 0 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr)) |
19314 | 0 | if (ICE->getType()->isVoidPointerType() && |
19315 | 0 | ICE->getCastKind() == CK_BitCast) |
19316 | 0 | ArgumentExpr = ICE->getSubExpr(); |
19317 | 0 | } |
19318 | 0 | QualType ArgumentType = ArgumentExpr->getType(); |
19319 | | |
19320 | | // Passing a `void*' pointer shouldn't trigger a warning. |
19321 | 0 | if (IsPointerAttr && ArgumentType->isVoidPointerType()) |
19322 | 0 | return; |
19323 | | |
19324 | 0 | if (TypeInfo.MustBeNull) { |
19325 | | // Type tag with matching void type requires a null pointer. |
19326 | 0 | if (!ArgumentExpr->isNullPointerConstant(Context, |
19327 | 0 | Expr::NPC_ValueDependentIsNotNull)) { |
19328 | 0 | Diag(ArgumentExpr->getExprLoc(), |
19329 | 0 | diag::warn_type_safety_null_pointer_required) |
19330 | 0 | << ArgumentKind->getName() |
19331 | 0 | << ArgumentExpr->getSourceRange() |
19332 | 0 | << TypeTagExpr->getSourceRange(); |
19333 | 0 | } |
19334 | 0 | return; |
19335 | 0 | } |
19336 | | |
19337 | 0 | QualType RequiredType = TypeInfo.Type; |
19338 | 0 | if (IsPointerAttr) |
19339 | 0 | RequiredType = Context.getPointerType(RequiredType); |
19340 | |
|
19341 | 0 | bool mismatch = false; |
19342 | 0 | if (!TypeInfo.LayoutCompatible) { |
19343 | 0 | mismatch = !Context.hasSameType(ArgumentType, RequiredType); |
19344 | | |
19345 | | // C++11 [basic.fundamental] p1: |
19346 | | // Plain char, signed char, and unsigned char are three distinct types. |
19347 | | // |
19348 | | // But we treat plain `char' as equivalent to `signed char' or `unsigned |
19349 | | // char' depending on the current char signedness mode. |
19350 | 0 | if (mismatch) |
19351 | 0 | if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(), |
19352 | 0 | RequiredType->getPointeeType())) || |
19353 | 0 | (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType))) |
19354 | 0 | mismatch = false; |
19355 | 0 | } else |
19356 | 0 | if (IsPointerAttr) |
19357 | 0 | mismatch = !isLayoutCompatible(Context, |
19358 | 0 | ArgumentType->getPointeeType(), |
19359 | 0 | RequiredType->getPointeeType()); |
19360 | 0 | else |
19361 | 0 | mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType); |
19362 | |
|
19363 | 0 | if (mismatch) |
19364 | 0 | Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch) |
19365 | 0 | << ArgumentType << ArgumentKind |
19366 | 0 | << TypeInfo.LayoutCompatible << RequiredType |
19367 | 0 | << ArgumentExpr->getSourceRange() |
19368 | 0 | << TypeTagExpr->getSourceRange(); |
19369 | 0 | } |
19370 | | |
19371 | | void Sema::AddPotentialMisalignedMembers(Expr *E, RecordDecl *RD, ValueDecl *MD, |
19372 | 0 | CharUnits Alignment) { |
19373 | 0 | MisalignedMembers.emplace_back(E, RD, MD, Alignment); |
19374 | 0 | } |
19375 | | |
19376 | 20 | void Sema::DiagnoseMisalignedMembers() { |
19377 | 20 | for (MisalignedMember &m : MisalignedMembers) { |
19378 | 0 | const NamedDecl *ND = m.RD; |
19379 | 0 | if (ND->getName().empty()) { |
19380 | 0 | if (const TypedefNameDecl *TD = m.RD->getTypedefNameForAnonDecl()) |
19381 | 0 | ND = TD; |
19382 | 0 | } |
19383 | 0 | Diag(m.E->getBeginLoc(), diag::warn_taking_address_of_packed_member) |
19384 | 0 | << m.MD << ND << m.E->getSourceRange(); |
19385 | 0 | } |
19386 | 20 | MisalignedMembers.clear(); |
19387 | 20 | } |
19388 | | |
19389 | 1 | void Sema::DiscardMisalignedMemberAddress(const Type *T, Expr *E) { |
19390 | 1 | E = E->IgnoreParens(); |
19391 | 1 | if (!T->isPointerType() && !T->isIntegerType() && !T->isDependentType()) |
19392 | 1 | return; |
19393 | 0 | if (isa<UnaryOperator>(E) && |
19394 | 0 | cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf) { |
19395 | 0 | auto *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
19396 | 0 | if (isa<MemberExpr>(Op)) { |
19397 | 0 | auto *MA = llvm::find(MisalignedMembers, MisalignedMember(Op)); |
19398 | 0 | if (MA != MisalignedMembers.end() && |
19399 | 0 | (T->isDependentType() || T->isIntegerType() || |
19400 | 0 | (T->isPointerType() && (T->getPointeeType()->isIncompleteType() || |
19401 | 0 | Context.getTypeAlignInChars( |
19402 | 0 | T->getPointeeType()) <= MA->Alignment)))) |
19403 | 0 | MisalignedMembers.erase(MA); |
19404 | 0 | } |
19405 | 0 | } |
19406 | 0 | } |
19407 | | |
19408 | | void Sema::RefersToMemberWithReducedAlignment( |
19409 | | Expr *E, |
19410 | | llvm::function_ref<void(Expr *, RecordDecl *, FieldDecl *, CharUnits)> |
19411 | 0 | Action) { |
19412 | 0 | const auto *ME = dyn_cast<MemberExpr>(E); |
19413 | 0 | if (!ME) |
19414 | 0 | return; |
19415 | | |
19416 | | // No need to check expressions with an __unaligned-qualified type. |
19417 | 0 | if (E->getType().getQualifiers().hasUnaligned()) |
19418 | 0 | return; |
19419 | | |
19420 | | // For a chain of MemberExpr like "a.b.c.d" this list |
19421 | | // will keep FieldDecl's like [d, c, b]. |
19422 | 0 | SmallVector<FieldDecl *, 4> ReverseMemberChain; |
19423 | 0 | const MemberExpr *TopME = nullptr; |
19424 | 0 | bool AnyIsPacked = false; |
19425 | 0 | do { |
19426 | 0 | QualType BaseType = ME->getBase()->getType(); |
19427 | 0 | if (BaseType->isDependentType()) |
19428 | 0 | return; |
19429 | 0 | if (ME->isArrow()) |
19430 | 0 | BaseType = BaseType->getPointeeType(); |
19431 | 0 | RecordDecl *RD = BaseType->castAs<RecordType>()->getDecl(); |
19432 | 0 | if (RD->isInvalidDecl()) |
19433 | 0 | return; |
19434 | | |
19435 | 0 | ValueDecl *MD = ME->getMemberDecl(); |
19436 | 0 | auto *FD = dyn_cast<FieldDecl>(MD); |
19437 | | // We do not care about non-data members. |
19438 | 0 | if (!FD || FD->isInvalidDecl()) |
19439 | 0 | return; |
19440 | | |
19441 | 0 | AnyIsPacked = |
19442 | 0 | AnyIsPacked || (RD->hasAttr<PackedAttr>() || MD->hasAttr<PackedAttr>()); |
19443 | 0 | ReverseMemberChain.push_back(FD); |
19444 | |
|
19445 | 0 | TopME = ME; |
19446 | 0 | ME = dyn_cast<MemberExpr>(ME->getBase()->IgnoreParens()); |
19447 | 0 | } while (ME); |
19448 | 0 | assert(TopME && "We did not compute a topmost MemberExpr!"); |
19449 | | |
19450 | | // Not the scope of this diagnostic. |
19451 | 0 | if (!AnyIsPacked) |
19452 | 0 | return; |
19453 | | |
19454 | 0 | const Expr *TopBase = TopME->getBase()->IgnoreParenImpCasts(); |
19455 | 0 | const auto *DRE = dyn_cast<DeclRefExpr>(TopBase); |
19456 | | // TODO: The innermost base of the member expression may be too complicated. |
19457 | | // For now, just disregard these cases. This is left for future |
19458 | | // improvement. |
19459 | 0 | if (!DRE && !isa<CXXThisExpr>(TopBase)) |
19460 | 0 | return; |
19461 | | |
19462 | | // Alignment expected by the whole expression. |
19463 | 0 | CharUnits ExpectedAlignment = Context.getTypeAlignInChars(E->getType()); |
19464 | | |
19465 | | // No need to do anything else with this case. |
19466 | 0 | if (ExpectedAlignment.isOne()) |
19467 | 0 | return; |
19468 | | |
19469 | | // Synthesize offset of the whole access. |
19470 | 0 | CharUnits Offset; |
19471 | 0 | for (const FieldDecl *FD : llvm::reverse(ReverseMemberChain)) |
19472 | 0 | Offset += Context.toCharUnitsFromBits(Context.getFieldOffset(FD)); |
19473 | | |
19474 | | // Compute the CompleteObjectAlignment as the alignment of the whole chain. |
19475 | 0 | CharUnits CompleteObjectAlignment = Context.getTypeAlignInChars( |
19476 | 0 | ReverseMemberChain.back()->getParent()->getTypeForDecl()); |
19477 | | |
19478 | | // The base expression of the innermost MemberExpr may give |
19479 | | // stronger guarantees than the class containing the member. |
19480 | 0 | if (DRE && !TopME->isArrow()) { |
19481 | 0 | const ValueDecl *VD = DRE->getDecl(); |
19482 | 0 | if (!VD->getType()->isReferenceType()) |
19483 | 0 | CompleteObjectAlignment = |
19484 | 0 | std::max(CompleteObjectAlignment, Context.getDeclAlign(VD)); |
19485 | 0 | } |
19486 | | |
19487 | | // Check if the synthesized offset fulfills the alignment. |
19488 | 0 | if (Offset % ExpectedAlignment != 0 || |
19489 | | // It may fulfill the offset it but the effective alignment may still be |
19490 | | // lower than the expected expression alignment. |
19491 | 0 | CompleteObjectAlignment < ExpectedAlignment) { |
19492 | | // If this happens, we want to determine a sensible culprit of this. |
19493 | | // Intuitively, watching the chain of member expressions from right to |
19494 | | // left, we start with the required alignment (as required by the field |
19495 | | // type) but some packed attribute in that chain has reduced the alignment. |
19496 | | // It may happen that another packed structure increases it again. But if |
19497 | | // we are here such increase has not been enough. So pointing the first |
19498 | | // FieldDecl that either is packed or else its RecordDecl is, |
19499 | | // seems reasonable. |
19500 | 0 | FieldDecl *FD = nullptr; |
19501 | 0 | CharUnits Alignment; |
19502 | 0 | for (FieldDecl *FDI : ReverseMemberChain) { |
19503 | 0 | if (FDI->hasAttr<PackedAttr>() || |
19504 | 0 | FDI->getParent()->hasAttr<PackedAttr>()) { |
19505 | 0 | FD = FDI; |
19506 | 0 | Alignment = std::min( |
19507 | 0 | Context.getTypeAlignInChars(FD->getType()), |
19508 | 0 | Context.getTypeAlignInChars(FD->getParent()->getTypeForDecl())); |
19509 | 0 | break; |
19510 | 0 | } |
19511 | 0 | } |
19512 | 0 | assert(FD && "We did not find a packed FieldDecl!"); |
19513 | 0 | Action(E, FD->getParent(), FD, Alignment); |
19514 | 0 | } |
19515 | 0 | } |
19516 | | |
19517 | 0 | void Sema::CheckAddressOfPackedMember(Expr *rhs) { |
19518 | 0 | using namespace std::placeholders; |
19519 | |
|
19520 | 0 | RefersToMemberWithReducedAlignment( |
19521 | 0 | rhs, std::bind(&Sema::AddPotentialMisalignedMembers, std::ref(*this), _1, |
19522 | 0 | _2, _3, _4)); |
19523 | 0 | } |
19524 | | |
19525 | 0 | bool Sema::PrepareBuiltinElementwiseMathOneArgCall(CallExpr *TheCall) { |
19526 | 0 | if (checkArgCount(*this, TheCall, 1)) |
19527 | 0 | return true; |
19528 | | |
19529 | 0 | ExprResult A = UsualUnaryConversions(TheCall->getArg(0)); |
19530 | 0 | if (A.isInvalid()) |
19531 | 0 | return true; |
19532 | | |
19533 | 0 | TheCall->setArg(0, A.get()); |
19534 | 0 | QualType TyA = A.get()->getType(); |
19535 | |
|
19536 | 0 | if (checkMathBuiltinElementType(*this, A.get()->getBeginLoc(), TyA)) |
19537 | 0 | return true; |
19538 | | |
19539 | 0 | TheCall->setType(TyA); |
19540 | 0 | return false; |
19541 | 0 | } |
19542 | | |
19543 | 0 | bool Sema::SemaBuiltinElementwiseMath(CallExpr *TheCall) { |
19544 | 0 | if (checkArgCount(*this, TheCall, 2)) |
19545 | 0 | return true; |
19546 | | |
19547 | 0 | ExprResult A = TheCall->getArg(0); |
19548 | 0 | ExprResult B = TheCall->getArg(1); |
19549 | | // Do standard promotions between the two arguments, returning their common |
19550 | | // type. |
19551 | 0 | QualType Res = |
19552 | 0 | UsualArithmeticConversions(A, B, TheCall->getExprLoc(), ACK_Comparison); |
19553 | 0 | if (A.isInvalid() || B.isInvalid()) |
19554 | 0 | return true; |
19555 | | |
19556 | 0 | QualType TyA = A.get()->getType(); |
19557 | 0 | QualType TyB = B.get()->getType(); |
19558 | |
|
19559 | 0 | if (Res.isNull() || TyA.getCanonicalType() != TyB.getCanonicalType()) |
19560 | 0 | return Diag(A.get()->getBeginLoc(), |
19561 | 0 | diag::err_typecheck_call_different_arg_types) |
19562 | 0 | << TyA << TyB; |
19563 | | |
19564 | 0 | if (checkMathBuiltinElementType(*this, A.get()->getBeginLoc(), TyA)) |
19565 | 0 | return true; |
19566 | | |
19567 | 0 | TheCall->setArg(0, A.get()); |
19568 | 0 | TheCall->setArg(1, B.get()); |
19569 | 0 | TheCall->setType(Res); |
19570 | 0 | return false; |
19571 | 0 | } |
19572 | | |
19573 | 0 | bool Sema::SemaBuiltinElementwiseTernaryMath(CallExpr *TheCall) { |
19574 | 0 | if (checkArgCount(*this, TheCall, 3)) |
19575 | 0 | return true; |
19576 | | |
19577 | 0 | Expr *Args[3]; |
19578 | 0 | for (int I = 0; I < 3; ++I) { |
19579 | 0 | ExprResult Converted = UsualUnaryConversions(TheCall->getArg(I)); |
19580 | 0 | if (Converted.isInvalid()) |
19581 | 0 | return true; |
19582 | 0 | Args[I] = Converted.get(); |
19583 | 0 | } |
19584 | | |
19585 | 0 | int ArgOrdinal = 1; |
19586 | 0 | for (Expr *Arg : Args) { |
19587 | 0 | if (checkFPMathBuiltinElementType(*this, Arg->getBeginLoc(), Arg->getType(), |
19588 | 0 | ArgOrdinal++)) |
19589 | 0 | return true; |
19590 | 0 | } |
19591 | | |
19592 | 0 | for (int I = 1; I < 3; ++I) { |
19593 | 0 | if (Args[0]->getType().getCanonicalType() != |
19594 | 0 | Args[I]->getType().getCanonicalType()) { |
19595 | 0 | return Diag(Args[0]->getBeginLoc(), |
19596 | 0 | diag::err_typecheck_call_different_arg_types) |
19597 | 0 | << Args[0]->getType() << Args[I]->getType(); |
19598 | 0 | } |
19599 | | |
19600 | 0 | TheCall->setArg(I, Args[I]); |
19601 | 0 | } |
19602 | | |
19603 | 0 | TheCall->setType(Args[0]->getType()); |
19604 | 0 | return false; |
19605 | 0 | } |
19606 | | |
19607 | 0 | bool Sema::PrepareBuiltinReduceMathOneArgCall(CallExpr *TheCall) { |
19608 | 0 | if (checkArgCount(*this, TheCall, 1)) |
19609 | 0 | return true; |
19610 | | |
19611 | 0 | ExprResult A = UsualUnaryConversions(TheCall->getArg(0)); |
19612 | 0 | if (A.isInvalid()) |
19613 | 0 | return true; |
19614 | | |
19615 | 0 | TheCall->setArg(0, A.get()); |
19616 | 0 | return false; |
19617 | 0 | } |
19618 | | |
19619 | 0 | bool Sema::SemaBuiltinNonDeterministicValue(CallExpr *TheCall) { |
19620 | 0 | if (checkArgCount(*this, TheCall, 1)) |
19621 | 0 | return true; |
19622 | | |
19623 | 0 | ExprResult Arg = TheCall->getArg(0); |
19624 | 0 | QualType TyArg = Arg.get()->getType(); |
19625 | |
|
19626 | 0 | if (!TyArg->isBuiltinType() && !TyArg->isVectorType()) |
19627 | 0 | return Diag(TheCall->getArg(0)->getBeginLoc(), diag::err_builtin_invalid_arg_type) |
19628 | 0 | << 1 << /*vector, integer or floating point ty*/ 0 << TyArg; |
19629 | | |
19630 | 0 | TheCall->setType(TyArg); |
19631 | 0 | return false; |
19632 | 0 | } |
19633 | | |
19634 | | ExprResult Sema::SemaBuiltinMatrixTranspose(CallExpr *TheCall, |
19635 | 0 | ExprResult CallResult) { |
19636 | 0 | if (checkArgCount(*this, TheCall, 1)) |
19637 | 0 | return ExprError(); |
19638 | | |
19639 | 0 | ExprResult MatrixArg = DefaultLvalueConversion(TheCall->getArg(0)); |
19640 | 0 | if (MatrixArg.isInvalid()) |
19641 | 0 | return MatrixArg; |
19642 | 0 | Expr *Matrix = MatrixArg.get(); |
19643 | |
|
19644 | 0 | auto *MType = Matrix->getType()->getAs<ConstantMatrixType>(); |
19645 | 0 | if (!MType) { |
19646 | 0 | Diag(Matrix->getBeginLoc(), diag::err_builtin_invalid_arg_type) |
19647 | 0 | << 1 << /* matrix ty*/ 1 << Matrix->getType(); |
19648 | 0 | return ExprError(); |
19649 | 0 | } |
19650 | | |
19651 | | // Create returned matrix type by swapping rows and columns of the argument |
19652 | | // matrix type. |
19653 | 0 | QualType ResultType = Context.getConstantMatrixType( |
19654 | 0 | MType->getElementType(), MType->getNumColumns(), MType->getNumRows()); |
19655 | | |
19656 | | // Change the return type to the type of the returned matrix. |
19657 | 0 | TheCall->setType(ResultType); |
19658 | | |
19659 | | // Update call argument to use the possibly converted matrix argument. |
19660 | 0 | TheCall->setArg(0, Matrix); |
19661 | 0 | return CallResult; |
19662 | 0 | } |
19663 | | |
19664 | | // Get and verify the matrix dimensions. |
19665 | | static std::optional<unsigned> |
19666 | 0 | getAndVerifyMatrixDimension(Expr *Expr, StringRef Name, Sema &S) { |
19667 | 0 | SourceLocation ErrorPos; |
19668 | 0 | std::optional<llvm::APSInt> Value = |
19669 | 0 | Expr->getIntegerConstantExpr(S.Context, &ErrorPos); |
19670 | 0 | if (!Value) { |
19671 | 0 | S.Diag(Expr->getBeginLoc(), diag::err_builtin_matrix_scalar_unsigned_arg) |
19672 | 0 | << Name; |
19673 | 0 | return {}; |
19674 | 0 | } |
19675 | 0 | uint64_t Dim = Value->getZExtValue(); |
19676 | 0 | if (!ConstantMatrixType::isDimensionValid(Dim)) { |
19677 | 0 | S.Diag(Expr->getBeginLoc(), diag::err_builtin_matrix_invalid_dimension) |
19678 | 0 | << Name << ConstantMatrixType::getMaxElementsPerDimension(); |
19679 | 0 | return {}; |
19680 | 0 | } |
19681 | 0 | return Dim; |
19682 | 0 | } |
19683 | | |
19684 | | ExprResult Sema::SemaBuiltinMatrixColumnMajorLoad(CallExpr *TheCall, |
19685 | 0 | ExprResult CallResult) { |
19686 | 0 | if (!getLangOpts().MatrixTypes) { |
19687 | 0 | Diag(TheCall->getBeginLoc(), diag::err_builtin_matrix_disabled); |
19688 | 0 | return ExprError(); |
19689 | 0 | } |
19690 | | |
19691 | 0 | if (checkArgCount(*this, TheCall, 4)) |
19692 | 0 | return ExprError(); |
19693 | | |
19694 | 0 | unsigned PtrArgIdx = 0; |
19695 | 0 | Expr *PtrExpr = TheCall->getArg(PtrArgIdx); |
19696 | 0 | Expr *RowsExpr = TheCall->getArg(1); |
19697 | 0 | Expr *ColumnsExpr = TheCall->getArg(2); |
19698 | 0 | Expr *StrideExpr = TheCall->getArg(3); |
19699 | |
|
19700 | 0 | bool ArgError = false; |
19701 | | |
19702 | | // Check pointer argument. |
19703 | 0 | { |
19704 | 0 | ExprResult PtrConv = DefaultFunctionArrayLvalueConversion(PtrExpr); |
19705 | 0 | if (PtrConv.isInvalid()) |
19706 | 0 | return PtrConv; |
19707 | 0 | PtrExpr = PtrConv.get(); |
19708 | 0 | TheCall->setArg(0, PtrExpr); |
19709 | 0 | if (PtrExpr->isTypeDependent()) { |
19710 | 0 | TheCall->setType(Context.DependentTy); |
19711 | 0 | return TheCall; |
19712 | 0 | } |
19713 | 0 | } |
19714 | | |
19715 | 0 | auto *PtrTy = PtrExpr->getType()->getAs<PointerType>(); |
19716 | 0 | QualType ElementTy; |
19717 | 0 | if (!PtrTy) { |
19718 | 0 | Diag(PtrExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type) |
19719 | 0 | << PtrArgIdx + 1 << /*pointer to element ty*/ 2 << PtrExpr->getType(); |
19720 | 0 | ArgError = true; |
19721 | 0 | } else { |
19722 | 0 | ElementTy = PtrTy->getPointeeType().getUnqualifiedType(); |
19723 | |
|
19724 | 0 | if (!ConstantMatrixType::isValidElementType(ElementTy)) { |
19725 | 0 | Diag(PtrExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type) |
19726 | 0 | << PtrArgIdx + 1 << /* pointer to element ty*/ 2 |
19727 | 0 | << PtrExpr->getType(); |
19728 | 0 | ArgError = true; |
19729 | 0 | } |
19730 | 0 | } |
19731 | | |
19732 | | // Apply default Lvalue conversions and convert the expression to size_t. |
19733 | 0 | auto ApplyArgumentConversions = [this](Expr *E) { |
19734 | 0 | ExprResult Conv = DefaultLvalueConversion(E); |
19735 | 0 | if (Conv.isInvalid()) |
19736 | 0 | return Conv; |
19737 | | |
19738 | 0 | return tryConvertExprToType(Conv.get(), Context.getSizeType()); |
19739 | 0 | }; |
19740 | | |
19741 | | // Apply conversion to row and column expressions. |
19742 | 0 | ExprResult RowsConv = ApplyArgumentConversions(RowsExpr); |
19743 | 0 | if (!RowsConv.isInvalid()) { |
19744 | 0 | RowsExpr = RowsConv.get(); |
19745 | 0 | TheCall->setArg(1, RowsExpr); |
19746 | 0 | } else |
19747 | 0 | RowsExpr = nullptr; |
19748 | |
|
19749 | 0 | ExprResult ColumnsConv = ApplyArgumentConversions(ColumnsExpr); |
19750 | 0 | if (!ColumnsConv.isInvalid()) { |
19751 | 0 | ColumnsExpr = ColumnsConv.get(); |
19752 | 0 | TheCall->setArg(2, ColumnsExpr); |
19753 | 0 | } else |
19754 | 0 | ColumnsExpr = nullptr; |
19755 | | |
19756 | | // If any part of the result matrix type is still pending, just use |
19757 | | // Context.DependentTy, until all parts are resolved. |
19758 | 0 | if ((RowsExpr && RowsExpr->isTypeDependent()) || |
19759 | 0 | (ColumnsExpr && ColumnsExpr->isTypeDependent())) { |
19760 | 0 | TheCall->setType(Context.DependentTy); |
19761 | 0 | return CallResult; |
19762 | 0 | } |
19763 | | |
19764 | | // Check row and column dimensions. |
19765 | 0 | std::optional<unsigned> MaybeRows; |
19766 | 0 | if (RowsExpr) |
19767 | 0 | MaybeRows = getAndVerifyMatrixDimension(RowsExpr, "row", *this); |
19768 | |
|
19769 | 0 | std::optional<unsigned> MaybeColumns; |
19770 | 0 | if (ColumnsExpr) |
19771 | 0 | MaybeColumns = getAndVerifyMatrixDimension(ColumnsExpr, "column", *this); |
19772 | | |
19773 | | // Check stride argument. |
19774 | 0 | ExprResult StrideConv = ApplyArgumentConversions(StrideExpr); |
19775 | 0 | if (StrideConv.isInvalid()) |
19776 | 0 | return ExprError(); |
19777 | 0 | StrideExpr = StrideConv.get(); |
19778 | 0 | TheCall->setArg(3, StrideExpr); |
19779 | |
|
19780 | 0 | if (MaybeRows) { |
19781 | 0 | if (std::optional<llvm::APSInt> Value = |
19782 | 0 | StrideExpr->getIntegerConstantExpr(Context)) { |
19783 | 0 | uint64_t Stride = Value->getZExtValue(); |
19784 | 0 | if (Stride < *MaybeRows) { |
19785 | 0 | Diag(StrideExpr->getBeginLoc(), |
19786 | 0 | diag::err_builtin_matrix_stride_too_small); |
19787 | 0 | ArgError = true; |
19788 | 0 | } |
19789 | 0 | } |
19790 | 0 | } |
19791 | |
|
19792 | 0 | if (ArgError || !MaybeRows || !MaybeColumns) |
19793 | 0 | return ExprError(); |
19794 | | |
19795 | 0 | TheCall->setType( |
19796 | 0 | Context.getConstantMatrixType(ElementTy, *MaybeRows, *MaybeColumns)); |
19797 | 0 | return CallResult; |
19798 | 0 | } |
19799 | | |
19800 | | ExprResult Sema::SemaBuiltinMatrixColumnMajorStore(CallExpr *TheCall, |
19801 | 0 | ExprResult CallResult) { |
19802 | 0 | if (checkArgCount(*this, TheCall, 3)) |
19803 | 0 | return ExprError(); |
19804 | | |
19805 | 0 | unsigned PtrArgIdx = 1; |
19806 | 0 | Expr *MatrixExpr = TheCall->getArg(0); |
19807 | 0 | Expr *PtrExpr = TheCall->getArg(PtrArgIdx); |
19808 | 0 | Expr *StrideExpr = TheCall->getArg(2); |
19809 | |
|
19810 | 0 | bool ArgError = false; |
19811 | |
|
19812 | 0 | { |
19813 | 0 | ExprResult MatrixConv = DefaultLvalueConversion(MatrixExpr); |
19814 | 0 | if (MatrixConv.isInvalid()) |
19815 | 0 | return MatrixConv; |
19816 | 0 | MatrixExpr = MatrixConv.get(); |
19817 | 0 | TheCall->setArg(0, MatrixExpr); |
19818 | 0 | } |
19819 | 0 | if (MatrixExpr->isTypeDependent()) { |
19820 | 0 | TheCall->setType(Context.DependentTy); |
19821 | 0 | return TheCall; |
19822 | 0 | } |
19823 | | |
19824 | 0 | auto *MatrixTy = MatrixExpr->getType()->getAs<ConstantMatrixType>(); |
19825 | 0 | if (!MatrixTy) { |
19826 | 0 | Diag(MatrixExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type) |
19827 | 0 | << 1 << /*matrix ty */ 1 << MatrixExpr->getType(); |
19828 | 0 | ArgError = true; |
19829 | 0 | } |
19830 | |
|
19831 | 0 | { |
19832 | 0 | ExprResult PtrConv = DefaultFunctionArrayLvalueConversion(PtrExpr); |
19833 | 0 | if (PtrConv.isInvalid()) |
19834 | 0 | return PtrConv; |
19835 | 0 | PtrExpr = PtrConv.get(); |
19836 | 0 | TheCall->setArg(1, PtrExpr); |
19837 | 0 | if (PtrExpr->isTypeDependent()) { |
19838 | 0 | TheCall->setType(Context.DependentTy); |
19839 | 0 | return TheCall; |
19840 | 0 | } |
19841 | 0 | } |
19842 | | |
19843 | | // Check pointer argument. |
19844 | 0 | auto *PtrTy = PtrExpr->getType()->getAs<PointerType>(); |
19845 | 0 | if (!PtrTy) { |
19846 | 0 | Diag(PtrExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type) |
19847 | 0 | << PtrArgIdx + 1 << /*pointer to element ty*/ 2 << PtrExpr->getType(); |
19848 | 0 | ArgError = true; |
19849 | 0 | } else { |
19850 | 0 | QualType ElementTy = PtrTy->getPointeeType(); |
19851 | 0 | if (ElementTy.isConstQualified()) { |
19852 | 0 | Diag(PtrExpr->getBeginLoc(), diag::err_builtin_matrix_store_to_const); |
19853 | 0 | ArgError = true; |
19854 | 0 | } |
19855 | 0 | ElementTy = ElementTy.getUnqualifiedType().getCanonicalType(); |
19856 | 0 | if (MatrixTy && |
19857 | 0 | !Context.hasSameType(ElementTy, MatrixTy->getElementType())) { |
19858 | 0 | Diag(PtrExpr->getBeginLoc(), |
19859 | 0 | diag::err_builtin_matrix_pointer_arg_mismatch) |
19860 | 0 | << ElementTy << MatrixTy->getElementType(); |
19861 | 0 | ArgError = true; |
19862 | 0 | } |
19863 | 0 | } |
19864 | | |
19865 | | // Apply default Lvalue conversions and convert the stride expression to |
19866 | | // size_t. |
19867 | 0 | { |
19868 | 0 | ExprResult StrideConv = DefaultLvalueConversion(StrideExpr); |
19869 | 0 | if (StrideConv.isInvalid()) |
19870 | 0 | return StrideConv; |
19871 | | |
19872 | 0 | StrideConv = tryConvertExprToType(StrideConv.get(), Context.getSizeType()); |
19873 | 0 | if (StrideConv.isInvalid()) |
19874 | 0 | return StrideConv; |
19875 | 0 | StrideExpr = StrideConv.get(); |
19876 | 0 | TheCall->setArg(2, StrideExpr); |
19877 | 0 | } |
19878 | | |
19879 | | // Check stride argument. |
19880 | 0 | if (MatrixTy) { |
19881 | 0 | if (std::optional<llvm::APSInt> Value = |
19882 | 0 | StrideExpr->getIntegerConstantExpr(Context)) { |
19883 | 0 | uint64_t Stride = Value->getZExtValue(); |
19884 | 0 | if (Stride < MatrixTy->getNumRows()) { |
19885 | 0 | Diag(StrideExpr->getBeginLoc(), |
19886 | 0 | diag::err_builtin_matrix_stride_too_small); |
19887 | 0 | ArgError = true; |
19888 | 0 | } |
19889 | 0 | } |
19890 | 0 | } |
19891 | |
|
19892 | 0 | if (ArgError) |
19893 | 0 | return ExprError(); |
19894 | | |
19895 | 0 | return CallResult; |
19896 | 0 | } |
19897 | | |
19898 | | /// Checks the argument at the given index is a WebAssembly table and if it |
19899 | | /// is, sets ElTy to the element type. |
19900 | | static bool CheckWasmBuiltinArgIsTable(Sema &S, CallExpr *E, unsigned ArgIndex, |
19901 | 0 | QualType &ElTy) { |
19902 | 0 | Expr *ArgExpr = E->getArg(ArgIndex); |
19903 | 0 | const auto *ATy = dyn_cast<ArrayType>(ArgExpr->getType()); |
19904 | 0 | if (!ATy || !ATy->getElementType().isWebAssemblyReferenceType()) { |
19905 | 0 | return S.Diag(ArgExpr->getBeginLoc(), |
19906 | 0 | diag::err_wasm_builtin_arg_must_be_table_type) |
19907 | 0 | << ArgIndex + 1 << ArgExpr->getSourceRange(); |
19908 | 0 | } |
19909 | 0 | ElTy = ATy->getElementType(); |
19910 | 0 | return false; |
19911 | 0 | } |
19912 | | |
19913 | | /// Checks the argument at the given index is an integer. |
19914 | | static bool CheckWasmBuiltinArgIsInteger(Sema &S, CallExpr *E, |
19915 | 0 | unsigned ArgIndex) { |
19916 | 0 | Expr *ArgExpr = E->getArg(ArgIndex); |
19917 | 0 | if (!ArgExpr->getType()->isIntegerType()) { |
19918 | 0 | return S.Diag(ArgExpr->getBeginLoc(), |
19919 | 0 | diag::err_wasm_builtin_arg_must_be_integer_type) |
19920 | 0 | << ArgIndex + 1 << ArgExpr->getSourceRange(); |
19921 | 0 | } |
19922 | 0 | return false; |
19923 | 0 | } |
19924 | | |
19925 | | /// Check that the first argument is a WebAssembly table, and the second |
19926 | | /// is an index to use as index into the table. |
19927 | 0 | bool Sema::BuiltinWasmTableGet(CallExpr *TheCall) { |
19928 | 0 | if (checkArgCount(*this, TheCall, 2)) |
19929 | 0 | return true; |
19930 | | |
19931 | 0 | QualType ElTy; |
19932 | 0 | if (CheckWasmBuiltinArgIsTable(*this, TheCall, 0, ElTy)) |
19933 | 0 | return true; |
19934 | | |
19935 | 0 | if (CheckWasmBuiltinArgIsInteger(*this, TheCall, 1)) |
19936 | 0 | return true; |
19937 | | |
19938 | | // If all is well, we set the type of TheCall to be the type of the |
19939 | | // element of the table. |
19940 | | // i.e. a table.get on an externref table has type externref, |
19941 | | // or whatever the type of the table element is. |
19942 | 0 | TheCall->setType(ElTy); |
19943 | |
|
19944 | 0 | return false; |
19945 | 0 | } |
19946 | | |
19947 | | /// Check that the first argumnet is a WebAssembly table, the second is |
19948 | | /// an index to use as index into the table and the third is the reference |
19949 | | /// type to set into the table. |
19950 | 0 | bool Sema::BuiltinWasmTableSet(CallExpr *TheCall) { |
19951 | 0 | if (checkArgCount(*this, TheCall, 3)) |
19952 | 0 | return true; |
19953 | | |
19954 | 0 | QualType ElTy; |
19955 | 0 | if (CheckWasmBuiltinArgIsTable(*this, TheCall, 0, ElTy)) |
19956 | 0 | return true; |
19957 | | |
19958 | 0 | if (CheckWasmBuiltinArgIsInteger(*this, TheCall, 1)) |
19959 | 0 | return true; |
19960 | | |
19961 | 0 | if (!Context.hasSameType(ElTy, TheCall->getArg(2)->getType())) |
19962 | 0 | return true; |
19963 | | |
19964 | 0 | return false; |
19965 | 0 | } |
19966 | | |
19967 | | /// Check that the argument is a WebAssembly table. |
19968 | 0 | bool Sema::BuiltinWasmTableSize(CallExpr *TheCall) { |
19969 | 0 | if (checkArgCount(*this, TheCall, 1)) |
19970 | 0 | return true; |
19971 | | |
19972 | 0 | QualType ElTy; |
19973 | 0 | if (CheckWasmBuiltinArgIsTable(*this, TheCall, 0, ElTy)) |
19974 | 0 | return true; |
19975 | | |
19976 | 0 | return false; |
19977 | 0 | } |
19978 | | |
19979 | | /// Check that the first argument is a WebAssembly table, the second is the |
19980 | | /// value to use for new elements (of a type matching the table type), the |
19981 | | /// third value is an integer. |
19982 | 0 | bool Sema::BuiltinWasmTableGrow(CallExpr *TheCall) { |
19983 | 0 | if (checkArgCount(*this, TheCall, 3)) |
19984 | 0 | return true; |
19985 | | |
19986 | 0 | QualType ElTy; |
19987 | 0 | if (CheckWasmBuiltinArgIsTable(*this, TheCall, 0, ElTy)) |
19988 | 0 | return true; |
19989 | | |
19990 | 0 | Expr *NewElemArg = TheCall->getArg(1); |
19991 | 0 | if (!Context.hasSameType(ElTy, NewElemArg->getType())) { |
19992 | 0 | return Diag(NewElemArg->getBeginLoc(), |
19993 | 0 | diag::err_wasm_builtin_arg_must_match_table_element_type) |
19994 | 0 | << 2 << 1 << NewElemArg->getSourceRange(); |
19995 | 0 | } |
19996 | | |
19997 | 0 | if (CheckWasmBuiltinArgIsInteger(*this, TheCall, 2)) |
19998 | 0 | return true; |
19999 | | |
20000 | 0 | return false; |
20001 | 0 | } |
20002 | | |
20003 | | /// Check that the first argument is a WebAssembly table, the second is an |
20004 | | /// integer, the third is the value to use to fill the table (of a type |
20005 | | /// matching the table type), and the fourth is an integer. |
20006 | 0 | bool Sema::BuiltinWasmTableFill(CallExpr *TheCall) { |
20007 | 0 | if (checkArgCount(*this, TheCall, 4)) |
20008 | 0 | return true; |
20009 | | |
20010 | 0 | QualType ElTy; |
20011 | 0 | if (CheckWasmBuiltinArgIsTable(*this, TheCall, 0, ElTy)) |
20012 | 0 | return true; |
20013 | | |
20014 | 0 | if (CheckWasmBuiltinArgIsInteger(*this, TheCall, 1)) |
20015 | 0 | return true; |
20016 | | |
20017 | 0 | Expr *NewElemArg = TheCall->getArg(2); |
20018 | 0 | if (!Context.hasSameType(ElTy, NewElemArg->getType())) { |
20019 | 0 | return Diag(NewElemArg->getBeginLoc(), |
20020 | 0 | diag::err_wasm_builtin_arg_must_match_table_element_type) |
20021 | 0 | << 3 << 1 << NewElemArg->getSourceRange(); |
20022 | 0 | } |
20023 | | |
20024 | 0 | if (CheckWasmBuiltinArgIsInteger(*this, TheCall, 3)) |
20025 | 0 | return true; |
20026 | | |
20027 | 0 | return false; |
20028 | 0 | } |
20029 | | |
20030 | | /// Check that the first argument is a WebAssembly table, the second is also a |
20031 | | /// WebAssembly table (of the same element type), and the third to fifth |
20032 | | /// arguments are integers. |
20033 | 0 | bool Sema::BuiltinWasmTableCopy(CallExpr *TheCall) { |
20034 | 0 | if (checkArgCount(*this, TheCall, 5)) |
20035 | 0 | return true; |
20036 | | |
20037 | 0 | QualType XElTy; |
20038 | 0 | if (CheckWasmBuiltinArgIsTable(*this, TheCall, 0, XElTy)) |
20039 | 0 | return true; |
20040 | | |
20041 | 0 | QualType YElTy; |
20042 | 0 | if (CheckWasmBuiltinArgIsTable(*this, TheCall, 1, YElTy)) |
20043 | 0 | return true; |
20044 | | |
20045 | 0 | Expr *TableYArg = TheCall->getArg(1); |
20046 | 0 | if (!Context.hasSameType(XElTy, YElTy)) { |
20047 | 0 | return Diag(TableYArg->getBeginLoc(), |
20048 | 0 | diag::err_wasm_builtin_arg_must_match_table_element_type) |
20049 | 0 | << 2 << 1 << TableYArg->getSourceRange(); |
20050 | 0 | } |
20051 | | |
20052 | 0 | for (int I = 2; I <= 4; I++) { |
20053 | 0 | if (CheckWasmBuiltinArgIsInteger(*this, TheCall, I)) |
20054 | 0 | return true; |
20055 | 0 | } |
20056 | | |
20057 | 0 | return false; |
20058 | 0 | } |
20059 | | |
20060 | | /// \brief Enforce the bounds of a TCB |
20061 | | /// CheckTCBEnforcement - Enforces that every function in a named TCB only |
20062 | | /// directly calls other functions in the same TCB as marked by the enforce_tcb |
20063 | | /// and enforce_tcb_leaf attributes. |
20064 | | void Sema::CheckTCBEnforcement(const SourceLocation CallExprLoc, |
20065 | 0 | const NamedDecl *Callee) { |
20066 | | // This warning does not make sense in code that has no runtime behavior. |
20067 | 0 | if (isUnevaluatedContext()) |
20068 | 0 | return; |
20069 | | |
20070 | 0 | const NamedDecl *Caller = getCurFunctionOrMethodDecl(); |
20071 | |
|
20072 | 0 | if (!Caller || !Caller->hasAttr<EnforceTCBAttr>()) |
20073 | 0 | return; |
20074 | | |
20075 | | // Search through the enforce_tcb and enforce_tcb_leaf attributes to find |
20076 | | // all TCBs the callee is a part of. |
20077 | 0 | llvm::StringSet<> CalleeTCBs; |
20078 | 0 | for (const auto *A : Callee->specific_attrs<EnforceTCBAttr>()) |
20079 | 0 | CalleeTCBs.insert(A->getTCBName()); |
20080 | 0 | for (const auto *A : Callee->specific_attrs<EnforceTCBLeafAttr>()) |
20081 | 0 | CalleeTCBs.insert(A->getTCBName()); |
20082 | | |
20083 | | // Go through the TCBs the caller is a part of and emit warnings if Caller |
20084 | | // is in a TCB that the Callee is not. |
20085 | 0 | for (const auto *A : Caller->specific_attrs<EnforceTCBAttr>()) { |
20086 | 0 | StringRef CallerTCB = A->getTCBName(); |
20087 | 0 | if (CalleeTCBs.count(CallerTCB) == 0) { |
20088 | 0 | this->Diag(CallExprLoc, diag::warn_tcb_enforcement_violation) |
20089 | 0 | << Callee << CallerTCB; |
20090 | 0 | } |
20091 | 0 | } |
20092 | 0 | } |