xref: /llvm-project/clang/lib/Sema/SemaChecking.cpp (revision e5992b686bb06dd53a4ff1e9586fa350d3ff43b5)
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 "CheckExprLifetime.h"
15 #include "clang/AST/APValue.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/AttrIterator.h"
19 #include "clang/AST/CharUnits.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclBase.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclObjC.h"
24 #include "clang/AST/DeclarationName.h"
25 #include "clang/AST/EvaluatedExprVisitor.h"
26 #include "clang/AST/Expr.h"
27 #include "clang/AST/ExprCXX.h"
28 #include "clang/AST/ExprObjC.h"
29 #include "clang/AST/FormatString.h"
30 #include "clang/AST/IgnoreExpr.h"
31 #include "clang/AST/NSAPI.h"
32 #include "clang/AST/NonTrivialTypeVisitor.h"
33 #include "clang/AST/OperationKinds.h"
34 #include "clang/AST/RecordLayout.h"
35 #include "clang/AST/Stmt.h"
36 #include "clang/AST/TemplateBase.h"
37 #include "clang/AST/Type.h"
38 #include "clang/AST/TypeLoc.h"
39 #include "clang/AST/UnresolvedSet.h"
40 #include "clang/Basic/AddressSpaces.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/TargetInfo.h"
53 #include "clang/Basic/TypeTraits.h"
54 #include "clang/Lex/Lexer.h" // TODO: Extract static functions to fix layering.
55 #include "clang/Sema/Initialization.h"
56 #include "clang/Sema/Lookup.h"
57 #include "clang/Sema/Ownership.h"
58 #include "clang/Sema/Scope.h"
59 #include "clang/Sema/ScopeInfo.h"
60 #include "clang/Sema/Sema.h"
61 #include "clang/Sema/SemaAMDGPU.h"
62 #include "clang/Sema/SemaARM.h"
63 #include "clang/Sema/SemaBPF.h"
64 #include "clang/Sema/SemaHLSL.h"
65 #include "clang/Sema/SemaHexagon.h"
66 #include "clang/Sema/SemaLoongArch.h"
67 #include "clang/Sema/SemaMIPS.h"
68 #include "clang/Sema/SemaNVPTX.h"
69 #include "clang/Sema/SemaObjC.h"
70 #include "clang/Sema/SemaOpenCL.h"
71 #include "clang/Sema/SemaPPC.h"
72 #include "clang/Sema/SemaRISCV.h"
73 #include "clang/Sema/SemaSPIRV.h"
74 #include "clang/Sema/SemaSystemZ.h"
75 #include "clang/Sema/SemaWasm.h"
76 #include "clang/Sema/SemaX86.h"
77 #include "llvm/ADT/APFloat.h"
78 #include "llvm/ADT/APInt.h"
79 #include "llvm/ADT/APSInt.h"
80 #include "llvm/ADT/ArrayRef.h"
81 #include "llvm/ADT/DenseMap.h"
82 #include "llvm/ADT/FoldingSet.h"
83 #include "llvm/ADT/STLExtras.h"
84 #include "llvm/ADT/SmallBitVector.h"
85 #include "llvm/ADT/SmallPtrSet.h"
86 #include "llvm/ADT/SmallString.h"
87 #include "llvm/ADT/SmallVector.h"
88 #include "llvm/ADT/StringExtras.h"
89 #include "llvm/ADT/StringRef.h"
90 #include "llvm/ADT/StringSet.h"
91 #include "llvm/ADT/StringSwitch.h"
92 #include "llvm/Support/AtomicOrdering.h"
93 #include "llvm/Support/Compiler.h"
94 #include "llvm/Support/ConvertUTF.h"
95 #include "llvm/Support/ErrorHandling.h"
96 #include "llvm/Support/Format.h"
97 #include "llvm/Support/Locale.h"
98 #include "llvm/Support/MathExtras.h"
99 #include "llvm/Support/SaveAndRestore.h"
100 #include "llvm/Support/raw_ostream.h"
101 #include "llvm/TargetParser/RISCVTargetParser.h"
102 #include "llvm/TargetParser/Triple.h"
103 #include <algorithm>
104 #include <cassert>
105 #include <cctype>
106 #include <cstddef>
107 #include <cstdint>
108 #include <functional>
109 #include <limits>
110 #include <optional>
111 #include <string>
112 #include <tuple>
113 #include <utility>
114 
115 using namespace clang;
116 using namespace sema;
117 
118 SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL,
119                                                     unsigned ByteNo) const {
120   return SL->getLocationOfByte(ByteNo, getSourceManager(), LangOpts,
121                                Context.getTargetInfo());
122 }
123 
124 static constexpr unsigned short combineFAPK(Sema::FormatArgumentPassingKind A,
125                                             Sema::FormatArgumentPassingKind B) {
126   return (A << 8) | B;
127 }
128 
129 bool Sema::checkArgCountAtLeast(CallExpr *Call, unsigned MinArgCount) {
130   unsigned ArgCount = Call->getNumArgs();
131   if (ArgCount >= MinArgCount)
132     return false;
133 
134   return Diag(Call->getEndLoc(), diag::err_typecheck_call_too_few_args)
135          << 0 /*function call*/ << MinArgCount << ArgCount
136          << /*is non object*/ 0 << Call->getSourceRange();
137 }
138 
139 bool Sema::checkArgCountAtMost(CallExpr *Call, unsigned MaxArgCount) {
140   unsigned ArgCount = Call->getNumArgs();
141   if (ArgCount <= MaxArgCount)
142     return false;
143   return Diag(Call->getEndLoc(), diag::err_typecheck_call_too_many_args_at_most)
144          << 0 /*function call*/ << MaxArgCount << ArgCount
145          << /*is non object*/ 0 << Call->getSourceRange();
146 }
147 
148 bool Sema::checkArgCountRange(CallExpr *Call, unsigned MinArgCount,
149                               unsigned MaxArgCount) {
150   return checkArgCountAtLeast(Call, MinArgCount) ||
151          checkArgCountAtMost(Call, MaxArgCount);
152 }
153 
154 bool Sema::checkArgCount(CallExpr *Call, unsigned DesiredArgCount) {
155   unsigned ArgCount = Call->getNumArgs();
156   if (ArgCount == DesiredArgCount)
157     return false;
158 
159   if (checkArgCountAtLeast(Call, DesiredArgCount))
160     return true;
161   assert(ArgCount > DesiredArgCount && "should have diagnosed this");
162 
163   // Highlight all the excess arguments.
164   SourceRange Range(Call->getArg(DesiredArgCount)->getBeginLoc(),
165                     Call->getArg(ArgCount - 1)->getEndLoc());
166 
167   return Diag(Range.getBegin(), diag::err_typecheck_call_too_many_args)
168          << 0 /*function call*/ << DesiredArgCount << ArgCount
169          << /*is non object*/ 0 << Call->getArg(1)->getSourceRange();
170 }
171 
172 static bool checkBuiltinVerboseTrap(CallExpr *Call, Sema &S) {
173   bool HasError = false;
174 
175   for (unsigned I = 0; I < Call->getNumArgs(); ++I) {
176     Expr *Arg = Call->getArg(I);
177 
178     if (Arg->isValueDependent())
179       continue;
180 
181     std::optional<std::string> ArgString = Arg->tryEvaluateString(S.Context);
182     int DiagMsgKind = -1;
183     // Arguments must be pointers to constant strings and cannot use '$'.
184     if (!ArgString.has_value())
185       DiagMsgKind = 0;
186     else if (ArgString->find('$') != std::string::npos)
187       DiagMsgKind = 1;
188 
189     if (DiagMsgKind >= 0) {
190       S.Diag(Arg->getBeginLoc(), diag::err_builtin_verbose_trap_arg)
191           << DiagMsgKind << Arg->getSourceRange();
192       HasError = true;
193     }
194   }
195 
196   return !HasError;
197 }
198 
199 static bool convertArgumentToType(Sema &S, Expr *&Value, QualType Ty) {
200   if (Value->isTypeDependent())
201     return false;
202 
203   InitializedEntity Entity =
204       InitializedEntity::InitializeParameter(S.Context, Ty, false);
205   ExprResult Result =
206       S.PerformCopyInitialization(Entity, SourceLocation(), Value);
207   if (Result.isInvalid())
208     return true;
209   Value = Result.get();
210   return false;
211 }
212 
213 /// Check that the first argument to __builtin_annotation is an integer
214 /// and the second argument is a non-wide string literal.
215 static bool BuiltinAnnotation(Sema &S, CallExpr *TheCall) {
216   if (S.checkArgCount(TheCall, 2))
217     return true;
218 
219   // First argument should be an integer.
220   Expr *ValArg = TheCall->getArg(0);
221   QualType Ty = ValArg->getType();
222   if (!Ty->isIntegerType()) {
223     S.Diag(ValArg->getBeginLoc(), diag::err_builtin_annotation_first_arg)
224         << ValArg->getSourceRange();
225     return true;
226   }
227 
228   // Second argument should be a constant string.
229   Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts();
230   StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg);
231   if (!Literal || !Literal->isOrdinary()) {
232     S.Diag(StrArg->getBeginLoc(), diag::err_builtin_annotation_second_arg)
233         << StrArg->getSourceRange();
234     return true;
235   }
236 
237   TheCall->setType(Ty);
238   return false;
239 }
240 
241 static bool BuiltinMSVCAnnotation(Sema &S, CallExpr *TheCall) {
242   // We need at least one argument.
243   if (TheCall->getNumArgs() < 1) {
244     S.Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least)
245         << 0 << 1 << TheCall->getNumArgs() << /*is non object*/ 0
246         << TheCall->getCallee()->getSourceRange();
247     return true;
248   }
249 
250   // All arguments should be wide string literals.
251   for (Expr *Arg : TheCall->arguments()) {
252     auto *Literal = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
253     if (!Literal || !Literal->isWide()) {
254       S.Diag(Arg->getBeginLoc(), diag::err_msvc_annotation_wide_str)
255           << Arg->getSourceRange();
256       return true;
257     }
258   }
259 
260   return false;
261 }
262 
263 /// Check that the argument to __builtin_addressof is a glvalue, and set the
264 /// result type to the corresponding pointer type.
265 static bool BuiltinAddressof(Sema &S, CallExpr *TheCall) {
266   if (S.checkArgCount(TheCall, 1))
267     return true;
268 
269   ExprResult Arg(TheCall->getArg(0));
270   QualType ResultType = S.CheckAddressOfOperand(Arg, TheCall->getBeginLoc());
271   if (ResultType.isNull())
272     return true;
273 
274   TheCall->setArg(0, Arg.get());
275   TheCall->setType(ResultType);
276   return false;
277 }
278 
279 /// Check that the argument to __builtin_function_start is a function.
280 static bool BuiltinFunctionStart(Sema &S, CallExpr *TheCall) {
281   if (S.checkArgCount(TheCall, 1))
282     return true;
283 
284   ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(0));
285   if (Arg.isInvalid())
286     return true;
287 
288   TheCall->setArg(0, Arg.get());
289   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(
290       Arg.get()->getAsBuiltinConstantDeclRef(S.getASTContext()));
291 
292   if (!FD) {
293     S.Diag(TheCall->getBeginLoc(), diag::err_function_start_invalid_type)
294         << TheCall->getSourceRange();
295     return true;
296   }
297 
298   return !S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true,
299                                               TheCall->getBeginLoc());
300 }
301 
302 /// Check the number of arguments and set the result type to
303 /// the argument type.
304 static bool BuiltinPreserveAI(Sema &S, CallExpr *TheCall) {
305   if (S.checkArgCount(TheCall, 1))
306     return true;
307 
308   TheCall->setType(TheCall->getArg(0)->getType());
309   return false;
310 }
311 
312 /// Check that the value argument for __builtin_is_aligned(value, alignment) and
313 /// __builtin_aligned_{up,down}(value, alignment) is an integer or a pointer
314 /// type (but not a function pointer) and that the alignment is a power-of-two.
315 static bool BuiltinAlignment(Sema &S, CallExpr *TheCall, unsigned ID) {
316   if (S.checkArgCount(TheCall, 2))
317     return true;
318 
319   clang::Expr *Source = TheCall->getArg(0);
320   bool IsBooleanAlignBuiltin = ID == Builtin::BI__builtin_is_aligned;
321 
322   auto IsValidIntegerType = [](QualType Ty) {
323     return Ty->isIntegerType() && !Ty->isEnumeralType() && !Ty->isBooleanType();
324   };
325   QualType SrcTy = Source->getType();
326   // We should also be able to use it with arrays (but not functions!).
327   if (SrcTy->canDecayToPointerType() && SrcTy->isArrayType()) {
328     SrcTy = S.Context.getDecayedType(SrcTy);
329   }
330   if ((!SrcTy->isPointerType() && !IsValidIntegerType(SrcTy)) ||
331       SrcTy->isFunctionPointerType()) {
332     // FIXME: this is not quite the right error message since we don't allow
333     // floating point types, or member pointers.
334     S.Diag(Source->getExprLoc(), diag::err_typecheck_expect_scalar_operand)
335         << SrcTy;
336     return true;
337   }
338 
339   clang::Expr *AlignOp = TheCall->getArg(1);
340   if (!IsValidIntegerType(AlignOp->getType())) {
341     S.Diag(AlignOp->getExprLoc(), diag::err_typecheck_expect_int)
342         << AlignOp->getType();
343     return true;
344   }
345   Expr::EvalResult AlignResult;
346   unsigned MaxAlignmentBits = S.Context.getIntWidth(SrcTy) - 1;
347   // We can't check validity of alignment if it is value dependent.
348   if (!AlignOp->isValueDependent() &&
349       AlignOp->EvaluateAsInt(AlignResult, S.Context,
350                              Expr::SE_AllowSideEffects)) {
351     llvm::APSInt AlignValue = AlignResult.Val.getInt();
352     llvm::APSInt MaxValue(
353         llvm::APInt::getOneBitSet(MaxAlignmentBits + 1, MaxAlignmentBits));
354     if (AlignValue < 1) {
355       S.Diag(AlignOp->getExprLoc(), diag::err_alignment_too_small) << 1;
356       return true;
357     }
358     if (llvm::APSInt::compareValues(AlignValue, MaxValue) > 0) {
359       S.Diag(AlignOp->getExprLoc(), diag::err_alignment_too_big)
360           << toString(MaxValue, 10);
361       return true;
362     }
363     if (!AlignValue.isPowerOf2()) {
364       S.Diag(AlignOp->getExprLoc(), diag::err_alignment_not_power_of_two);
365       return true;
366     }
367     if (AlignValue == 1) {
368       S.Diag(AlignOp->getExprLoc(), diag::warn_alignment_builtin_useless)
369           << IsBooleanAlignBuiltin;
370     }
371   }
372 
373   ExprResult SrcArg = S.PerformCopyInitialization(
374       InitializedEntity::InitializeParameter(S.Context, SrcTy, false),
375       SourceLocation(), Source);
376   if (SrcArg.isInvalid())
377     return true;
378   TheCall->setArg(0, SrcArg.get());
379   ExprResult AlignArg =
380       S.PerformCopyInitialization(InitializedEntity::InitializeParameter(
381                                       S.Context, AlignOp->getType(), false),
382                                   SourceLocation(), AlignOp);
383   if (AlignArg.isInvalid())
384     return true;
385   TheCall->setArg(1, AlignArg.get());
386   // For align_up/align_down, the return type is the same as the (potentially
387   // decayed) argument type including qualifiers. For is_aligned(), the result
388   // is always bool.
389   TheCall->setType(IsBooleanAlignBuiltin ? S.Context.BoolTy : SrcTy);
390   return false;
391 }
392 
393 static bool BuiltinOverflow(Sema &S, CallExpr *TheCall, unsigned BuiltinID) {
394   if (S.checkArgCount(TheCall, 3))
395     return true;
396 
397   std::pair<unsigned, const char *> Builtins[] = {
398     { Builtin::BI__builtin_add_overflow, "ckd_add" },
399     { Builtin::BI__builtin_sub_overflow, "ckd_sub" },
400     { Builtin::BI__builtin_mul_overflow, "ckd_mul" },
401   };
402 
403   bool CkdOperation = llvm::any_of(Builtins, [&](const std::pair<unsigned,
404     const char *> &P) {
405     return BuiltinID == P.first && TheCall->getExprLoc().isMacroID() &&
406          Lexer::getImmediateMacroName(TheCall->getExprLoc(),
407          S.getSourceManager(), S.getLangOpts()) == P.second;
408   });
409 
410   auto ValidCkdIntType = [](QualType QT) {
411     // A valid checked integer type is an integer type other than a plain char,
412     // bool, a bit-precise type, or an enumeration type.
413     if (const auto *BT = QT.getCanonicalType()->getAs<BuiltinType>())
414       return (BT->getKind() >= BuiltinType::Short &&
415            BT->getKind() <= BuiltinType::Int128) || (
416            BT->getKind() >= BuiltinType::UShort &&
417            BT->getKind() <= BuiltinType::UInt128) ||
418            BT->getKind() == BuiltinType::UChar ||
419            BT->getKind() == BuiltinType::SChar;
420     return false;
421   };
422 
423   // First two arguments should be integers.
424   for (unsigned I = 0; I < 2; ++I) {
425     ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(I));
426     if (Arg.isInvalid()) return true;
427     TheCall->setArg(I, Arg.get());
428 
429     QualType Ty = Arg.get()->getType();
430     bool IsValid = CkdOperation ? ValidCkdIntType(Ty) : Ty->isIntegerType();
431     if (!IsValid) {
432       S.Diag(Arg.get()->getBeginLoc(), diag::err_overflow_builtin_must_be_int)
433           << CkdOperation << Ty << Arg.get()->getSourceRange();
434       return true;
435     }
436   }
437 
438   // Third argument should be a pointer to a non-const integer.
439   // IRGen correctly handles volatile, restrict, and address spaces, and
440   // the other qualifiers aren't possible.
441   {
442     ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(2));
443     if (Arg.isInvalid()) return true;
444     TheCall->setArg(2, Arg.get());
445 
446     QualType Ty = Arg.get()->getType();
447     const auto *PtrTy = Ty->getAs<PointerType>();
448     if (!PtrTy ||
449         !PtrTy->getPointeeType()->isIntegerType() ||
450         (!ValidCkdIntType(PtrTy->getPointeeType()) && CkdOperation) ||
451         PtrTy->getPointeeType().isConstQualified()) {
452       S.Diag(Arg.get()->getBeginLoc(),
453              diag::err_overflow_builtin_must_be_ptr_int)
454         << CkdOperation << Ty << Arg.get()->getSourceRange();
455       return true;
456     }
457   }
458 
459   // Disallow signed bit-precise integer args larger than 128 bits to mul
460   // function until we improve backend support.
461   if (BuiltinID == Builtin::BI__builtin_mul_overflow) {
462     for (unsigned I = 0; I < 3; ++I) {
463       const auto Arg = TheCall->getArg(I);
464       // Third argument will be a pointer.
465       auto Ty = I < 2 ? Arg->getType() : Arg->getType()->getPointeeType();
466       if (Ty->isBitIntType() && Ty->isSignedIntegerType() &&
467           S.getASTContext().getIntWidth(Ty) > 128)
468         return S.Diag(Arg->getBeginLoc(),
469                       diag::err_overflow_builtin_bit_int_max_size)
470                << 128;
471     }
472   }
473 
474   return false;
475 }
476 
477 namespace {
478 struct BuiltinDumpStructGenerator {
479   Sema &S;
480   CallExpr *TheCall;
481   SourceLocation Loc = TheCall->getBeginLoc();
482   SmallVector<Expr *, 32> Actions;
483   DiagnosticErrorTrap ErrorTracker;
484   PrintingPolicy Policy;
485 
486   BuiltinDumpStructGenerator(Sema &S, CallExpr *TheCall)
487       : S(S), TheCall(TheCall), ErrorTracker(S.getDiagnostics()),
488         Policy(S.Context.getPrintingPolicy()) {
489     Policy.AnonymousTagLocations = false;
490   }
491 
492   Expr *makeOpaqueValueExpr(Expr *Inner) {
493     auto *OVE = new (S.Context)
494         OpaqueValueExpr(Loc, Inner->getType(), Inner->getValueKind(),
495                         Inner->getObjectKind(), Inner);
496     Actions.push_back(OVE);
497     return OVE;
498   }
499 
500   Expr *getStringLiteral(llvm::StringRef Str) {
501     Expr *Lit = S.Context.getPredefinedStringLiteralFromCache(Str);
502     // Wrap the literal in parentheses to attach a source location.
503     return new (S.Context) ParenExpr(Loc, Loc, Lit);
504   }
505 
506   bool callPrintFunction(llvm::StringRef Format,
507                          llvm::ArrayRef<Expr *> Exprs = {}) {
508     SmallVector<Expr *, 8> Args;
509     assert(TheCall->getNumArgs() >= 2);
510     Args.reserve((TheCall->getNumArgs() - 2) + /*Format*/ 1 + Exprs.size());
511     Args.assign(TheCall->arg_begin() + 2, TheCall->arg_end());
512     Args.push_back(getStringLiteral(Format));
513     Args.insert(Args.end(), Exprs.begin(), Exprs.end());
514 
515     // Register a note to explain why we're performing the call.
516     Sema::CodeSynthesisContext Ctx;
517     Ctx.Kind = Sema::CodeSynthesisContext::BuildingBuiltinDumpStructCall;
518     Ctx.PointOfInstantiation = Loc;
519     Ctx.CallArgs = Args.data();
520     Ctx.NumCallArgs = Args.size();
521     S.pushCodeSynthesisContext(Ctx);
522 
523     ExprResult RealCall =
524         S.BuildCallExpr(/*Scope=*/nullptr, TheCall->getArg(1),
525                         TheCall->getBeginLoc(), Args, TheCall->getRParenLoc());
526 
527     S.popCodeSynthesisContext();
528     if (!RealCall.isInvalid())
529       Actions.push_back(RealCall.get());
530     // Bail out if we've hit any errors, even if we managed to build the
531     // call. We don't want to produce more than one error.
532     return RealCall.isInvalid() || ErrorTracker.hasErrorOccurred();
533   }
534 
535   Expr *getIndentString(unsigned Depth) {
536     if (!Depth)
537       return nullptr;
538 
539     llvm::SmallString<32> Indent;
540     Indent.resize(Depth * Policy.Indentation, ' ');
541     return getStringLiteral(Indent);
542   }
543 
544   Expr *getTypeString(QualType T) {
545     return getStringLiteral(T.getAsString(Policy));
546   }
547 
548   bool appendFormatSpecifier(QualType T, llvm::SmallVectorImpl<char> &Str) {
549     llvm::raw_svector_ostream OS(Str);
550 
551     // Format 'bool', 'char', 'signed char', 'unsigned char' as numbers, rather
552     // than trying to print a single character.
553     if (auto *BT = T->getAs<BuiltinType>()) {
554       switch (BT->getKind()) {
555       case BuiltinType::Bool:
556         OS << "%d";
557         return true;
558       case BuiltinType::Char_U:
559       case BuiltinType::UChar:
560         OS << "%hhu";
561         return true;
562       case BuiltinType::Char_S:
563       case BuiltinType::SChar:
564         OS << "%hhd";
565         return true;
566       default:
567         break;
568       }
569     }
570 
571     analyze_printf::PrintfSpecifier Specifier;
572     if (Specifier.fixType(T, S.getLangOpts(), S.Context, /*IsObjCLiteral=*/false)) {
573       // We were able to guess how to format this.
574       if (Specifier.getConversionSpecifier().getKind() ==
575           analyze_printf::PrintfConversionSpecifier::sArg) {
576         // Wrap double-quotes around a '%s' specifier and limit its maximum
577         // length. Ideally we'd also somehow escape special characters in the
578         // contents but printf doesn't support that.
579         // FIXME: '%s' formatting is not safe in general.
580         OS << '"';
581         Specifier.setPrecision(analyze_printf::OptionalAmount(32u));
582         Specifier.toString(OS);
583         OS << '"';
584         // FIXME: It would be nice to include a '...' if the string doesn't fit
585         // in the length limit.
586       } else {
587         Specifier.toString(OS);
588       }
589       return true;
590     }
591 
592     if (T->isPointerType()) {
593       // Format all pointers with '%p'.
594       OS << "%p";
595       return true;
596     }
597 
598     return false;
599   }
600 
601   bool dumpUnnamedRecord(const RecordDecl *RD, Expr *E, unsigned Depth) {
602     Expr *IndentLit = getIndentString(Depth);
603     Expr *TypeLit = getTypeString(S.Context.getRecordType(RD));
604     if (IndentLit ? callPrintFunction("%s%s", {IndentLit, TypeLit})
605                   : callPrintFunction("%s", {TypeLit}))
606       return true;
607 
608     return dumpRecordValue(RD, E, IndentLit, Depth);
609   }
610 
611   // Dump a record value. E should be a pointer or lvalue referring to an RD.
612   bool dumpRecordValue(const RecordDecl *RD, Expr *E, Expr *RecordIndent,
613                        unsigned Depth) {
614     // FIXME: Decide what to do if RD is a union. At least we should probably
615     // turn off printing `const char*` members with `%s`, because that is very
616     // likely to crash if that's not the active member. Whatever we decide, we
617     // should document it.
618 
619     // Build an OpaqueValueExpr so we can refer to E more than once without
620     // triggering re-evaluation.
621     Expr *RecordArg = makeOpaqueValueExpr(E);
622     bool RecordArgIsPtr = RecordArg->getType()->isPointerType();
623 
624     if (callPrintFunction(" {\n"))
625       return true;
626 
627     // Dump each base class, regardless of whether they're aggregates.
628     if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
629       for (const auto &Base : CXXRD->bases()) {
630         QualType BaseType =
631             RecordArgIsPtr ? S.Context.getPointerType(Base.getType())
632                            : S.Context.getLValueReferenceType(Base.getType());
633         ExprResult BasePtr = S.BuildCStyleCastExpr(
634             Loc, S.Context.getTrivialTypeSourceInfo(BaseType, Loc), Loc,
635             RecordArg);
636         if (BasePtr.isInvalid() ||
637             dumpUnnamedRecord(Base.getType()->getAsRecordDecl(), BasePtr.get(),
638                               Depth + 1))
639           return true;
640       }
641     }
642 
643     Expr *FieldIndentArg = getIndentString(Depth + 1);
644 
645     // Dump each field.
646     for (auto *D : RD->decls()) {
647       auto *IFD = dyn_cast<IndirectFieldDecl>(D);
648       auto *FD = IFD ? IFD->getAnonField() : dyn_cast<FieldDecl>(D);
649       if (!FD || FD->isUnnamedBitField() || FD->isAnonymousStructOrUnion())
650         continue;
651 
652       llvm::SmallString<20> Format = llvm::StringRef("%s%s %s ");
653       llvm::SmallVector<Expr *, 5> Args = {FieldIndentArg,
654                                            getTypeString(FD->getType()),
655                                            getStringLiteral(FD->getName())};
656 
657       if (FD->isBitField()) {
658         Format += ": %zu ";
659         QualType SizeT = S.Context.getSizeType();
660         llvm::APInt BitWidth(S.Context.getIntWidth(SizeT),
661                              FD->getBitWidthValue());
662         Args.push_back(IntegerLiteral::Create(S.Context, BitWidth, SizeT, Loc));
663       }
664 
665       Format += "=";
666 
667       ExprResult Field =
668           IFD ? S.BuildAnonymousStructUnionMemberReference(
669                     CXXScopeSpec(), Loc, IFD,
670                     DeclAccessPair::make(IFD, AS_public), RecordArg, Loc)
671               : S.BuildFieldReferenceExpr(
672                     RecordArg, RecordArgIsPtr, Loc, CXXScopeSpec(), FD,
673                     DeclAccessPair::make(FD, AS_public),
674                     DeclarationNameInfo(FD->getDeclName(), Loc));
675       if (Field.isInvalid())
676         return true;
677 
678       auto *InnerRD = FD->getType()->getAsRecordDecl();
679       auto *InnerCXXRD = dyn_cast_or_null<CXXRecordDecl>(InnerRD);
680       if (InnerRD && (!InnerCXXRD || InnerCXXRD->isAggregate())) {
681         // Recursively print the values of members of aggregate record type.
682         if (callPrintFunction(Format, Args) ||
683             dumpRecordValue(InnerRD, Field.get(), FieldIndentArg, Depth + 1))
684           return true;
685       } else {
686         Format += " ";
687         if (appendFormatSpecifier(FD->getType(), Format)) {
688           // We know how to print this field.
689           Args.push_back(Field.get());
690         } else {
691           // We don't know how to print this field. Print out its address
692           // with a format specifier that a smart tool will be able to
693           // recognize and treat specially.
694           Format += "*%p";
695           ExprResult FieldAddr =
696               S.BuildUnaryOp(nullptr, Loc, UO_AddrOf, Field.get());
697           if (FieldAddr.isInvalid())
698             return true;
699           Args.push_back(FieldAddr.get());
700         }
701         Format += "\n";
702         if (callPrintFunction(Format, Args))
703           return true;
704       }
705     }
706 
707     return RecordIndent ? callPrintFunction("%s}\n", RecordIndent)
708                         : callPrintFunction("}\n");
709   }
710 
711   Expr *buildWrapper() {
712     auto *Wrapper = PseudoObjectExpr::Create(S.Context, TheCall, Actions,
713                                              PseudoObjectExpr::NoResult);
714     TheCall->setType(Wrapper->getType());
715     TheCall->setValueKind(Wrapper->getValueKind());
716     return Wrapper;
717   }
718 };
719 } // namespace
720 
721 static ExprResult BuiltinDumpStruct(Sema &S, CallExpr *TheCall) {
722   if (S.checkArgCountAtLeast(TheCall, 2))
723     return ExprError();
724 
725   ExprResult PtrArgResult = S.DefaultLvalueConversion(TheCall->getArg(0));
726   if (PtrArgResult.isInvalid())
727     return ExprError();
728   TheCall->setArg(0, PtrArgResult.get());
729 
730   // First argument should be a pointer to a struct.
731   QualType PtrArgType = PtrArgResult.get()->getType();
732   if (!PtrArgType->isPointerType() ||
733       !PtrArgType->getPointeeType()->isRecordType()) {
734     S.Diag(PtrArgResult.get()->getBeginLoc(),
735            diag::err_expected_struct_pointer_argument)
736         << 1 << TheCall->getDirectCallee() << PtrArgType;
737     return ExprError();
738   }
739   QualType Pointee = PtrArgType->getPointeeType();
740   const RecordDecl *RD = Pointee->getAsRecordDecl();
741   // Try to instantiate the class template as appropriate; otherwise, access to
742   // its data() may lead to a crash.
743   if (S.RequireCompleteType(PtrArgResult.get()->getBeginLoc(), Pointee,
744                             diag::err_incomplete_type))
745     return ExprError();
746   // Second argument is a callable, but we can't fully validate it until we try
747   // calling it.
748   QualType FnArgType = TheCall->getArg(1)->getType();
749   if (!FnArgType->isFunctionType() && !FnArgType->isFunctionPointerType() &&
750       !FnArgType->isBlockPointerType() &&
751       !(S.getLangOpts().CPlusPlus && FnArgType->isRecordType())) {
752     auto *BT = FnArgType->getAs<BuiltinType>();
753     switch (BT ? BT->getKind() : BuiltinType::Void) {
754     case BuiltinType::Dependent:
755     case BuiltinType::Overload:
756     case BuiltinType::BoundMember:
757     case BuiltinType::PseudoObject:
758     case BuiltinType::UnknownAny:
759     case BuiltinType::BuiltinFn:
760       // This might be a callable.
761       break;
762 
763     default:
764       S.Diag(TheCall->getArg(1)->getBeginLoc(),
765              diag::err_expected_callable_argument)
766           << 2 << TheCall->getDirectCallee() << FnArgType;
767       return ExprError();
768     }
769   }
770 
771   BuiltinDumpStructGenerator Generator(S, TheCall);
772 
773   // Wrap parentheses around the given pointer. This is not necessary for
774   // correct code generation, but it means that when we pretty-print the call
775   // arguments in our diagnostics we will produce '(&s)->n' instead of the
776   // incorrect '&s->n'.
777   Expr *PtrArg = PtrArgResult.get();
778   PtrArg = new (S.Context)
779       ParenExpr(PtrArg->getBeginLoc(),
780                 S.getLocForEndOfToken(PtrArg->getEndLoc()), PtrArg);
781   if (Generator.dumpUnnamedRecord(RD, PtrArg, 0))
782     return ExprError();
783 
784   return Generator.buildWrapper();
785 }
786 
787 static bool BuiltinCallWithStaticChain(Sema &S, CallExpr *BuiltinCall) {
788   if (S.checkArgCount(BuiltinCall, 2))
789     return true;
790 
791   SourceLocation BuiltinLoc = BuiltinCall->getBeginLoc();
792   Expr *Builtin = BuiltinCall->getCallee()->IgnoreImpCasts();
793   Expr *Call = BuiltinCall->getArg(0);
794   Expr *Chain = BuiltinCall->getArg(1);
795 
796   if (Call->getStmtClass() != Stmt::CallExprClass) {
797     S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_not_call)
798         << Call->getSourceRange();
799     return true;
800   }
801 
802   auto CE = cast<CallExpr>(Call);
803   if (CE->getCallee()->getType()->isBlockPointerType()) {
804     S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_block_call)
805         << Call->getSourceRange();
806     return true;
807   }
808 
809   const Decl *TargetDecl = CE->getCalleeDecl();
810   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl))
811     if (FD->getBuiltinID()) {
812       S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_builtin_call)
813           << Call->getSourceRange();
814       return true;
815     }
816 
817   if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens())) {
818     S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_pdtor_call)
819         << Call->getSourceRange();
820     return true;
821   }
822 
823   ExprResult ChainResult = S.UsualUnaryConversions(Chain);
824   if (ChainResult.isInvalid())
825     return true;
826   if (!ChainResult.get()->getType()->isPointerType()) {
827     S.Diag(BuiltinLoc, diag::err_second_argument_to_cwsc_not_pointer)
828         << Chain->getSourceRange();
829     return true;
830   }
831 
832   QualType ReturnTy = CE->getCallReturnType(S.Context);
833   QualType ArgTys[2] = { ReturnTy, ChainResult.get()->getType() };
834   QualType BuiltinTy = S.Context.getFunctionType(
835       ReturnTy, ArgTys, FunctionProtoType::ExtProtoInfo());
836   QualType BuiltinPtrTy = S.Context.getPointerType(BuiltinTy);
837 
838   Builtin =
839       S.ImpCastExprToType(Builtin, BuiltinPtrTy, CK_BuiltinFnToFnPtr).get();
840 
841   BuiltinCall->setType(CE->getType());
842   BuiltinCall->setValueKind(CE->getValueKind());
843   BuiltinCall->setObjectKind(CE->getObjectKind());
844   BuiltinCall->setCallee(Builtin);
845   BuiltinCall->setArg(1, ChainResult.get());
846 
847   return false;
848 }
849 
850 namespace {
851 
852 class ScanfDiagnosticFormatHandler
853     : public analyze_format_string::FormatStringHandler {
854   // Accepts the argument index (relative to the first destination index) of the
855   // argument whose size we want.
856   using ComputeSizeFunction =
857       llvm::function_ref<std::optional<llvm::APSInt>(unsigned)>;
858 
859   // Accepts the argument index (relative to the first destination index), the
860   // destination size, and the source size).
861   using DiagnoseFunction =
862       llvm::function_ref<void(unsigned, unsigned, unsigned)>;
863 
864   ComputeSizeFunction ComputeSizeArgument;
865   DiagnoseFunction Diagnose;
866 
867 public:
868   ScanfDiagnosticFormatHandler(ComputeSizeFunction ComputeSizeArgument,
869                                DiagnoseFunction Diagnose)
870       : ComputeSizeArgument(ComputeSizeArgument), Diagnose(Diagnose) {}
871 
872   bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS,
873                             const char *StartSpecifier,
874                             unsigned specifierLen) override {
875     if (!FS.consumesDataArgument())
876       return true;
877 
878     unsigned NulByte = 0;
879     switch ((FS.getConversionSpecifier().getKind())) {
880     default:
881       return true;
882     case analyze_format_string::ConversionSpecifier::sArg:
883     case analyze_format_string::ConversionSpecifier::ScanListArg:
884       NulByte = 1;
885       break;
886     case analyze_format_string::ConversionSpecifier::cArg:
887       break;
888     }
889 
890     analyze_format_string::OptionalAmount FW = FS.getFieldWidth();
891     if (FW.getHowSpecified() !=
892         analyze_format_string::OptionalAmount::HowSpecified::Constant)
893       return true;
894 
895     unsigned SourceSize = FW.getConstantAmount() + NulByte;
896 
897     std::optional<llvm::APSInt> DestSizeAPS =
898         ComputeSizeArgument(FS.getArgIndex());
899     if (!DestSizeAPS)
900       return true;
901 
902     unsigned DestSize = DestSizeAPS->getZExtValue();
903 
904     if (DestSize < SourceSize)
905       Diagnose(FS.getArgIndex(), DestSize, SourceSize);
906 
907     return true;
908   }
909 };
910 
911 class EstimateSizeFormatHandler
912     : public analyze_format_string::FormatStringHandler {
913   size_t Size;
914   /// Whether the format string contains Linux kernel's format specifier
915   /// extension.
916   bool IsKernelCompatible = true;
917 
918 public:
919   EstimateSizeFormatHandler(StringRef Format)
920       : Size(std::min(Format.find(0), Format.size()) +
921              1 /* null byte always written by sprintf */) {}
922 
923   bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS,
924                              const char *, unsigned SpecifierLen,
925                              const TargetInfo &) override {
926 
927     const size_t FieldWidth = computeFieldWidth(FS);
928     const size_t Precision = computePrecision(FS);
929 
930     // The actual format.
931     switch (FS.getConversionSpecifier().getKind()) {
932     // Just a char.
933     case analyze_format_string::ConversionSpecifier::cArg:
934     case analyze_format_string::ConversionSpecifier::CArg:
935       Size += std::max(FieldWidth, (size_t)1);
936       break;
937     // Just an integer.
938     case analyze_format_string::ConversionSpecifier::dArg:
939     case analyze_format_string::ConversionSpecifier::DArg:
940     case analyze_format_string::ConversionSpecifier::iArg:
941     case analyze_format_string::ConversionSpecifier::oArg:
942     case analyze_format_string::ConversionSpecifier::OArg:
943     case analyze_format_string::ConversionSpecifier::uArg:
944     case analyze_format_string::ConversionSpecifier::UArg:
945     case analyze_format_string::ConversionSpecifier::xArg:
946     case analyze_format_string::ConversionSpecifier::XArg:
947       Size += std::max(FieldWidth, Precision);
948       break;
949 
950     // %g style conversion switches between %f or %e style dynamically.
951     // %g removes trailing zeros, and does not print decimal point if there are
952     // no digits that follow it. Thus %g can print a single digit.
953     // FIXME: If it is alternative form:
954     // For g and G conversions, trailing zeros are not removed from the result.
955     case analyze_format_string::ConversionSpecifier::gArg:
956     case analyze_format_string::ConversionSpecifier::GArg:
957       Size += 1;
958       break;
959 
960     // Floating point number in the form '[+]ddd.ddd'.
961     case analyze_format_string::ConversionSpecifier::fArg:
962     case analyze_format_string::ConversionSpecifier::FArg:
963       Size += std::max(FieldWidth, 1 /* integer part */ +
964                                        (Precision ? 1 + Precision
965                                                   : 0) /* period + decimal */);
966       break;
967 
968     // Floating point number in the form '[-]d.ddde[+-]dd'.
969     case analyze_format_string::ConversionSpecifier::eArg:
970     case analyze_format_string::ConversionSpecifier::EArg:
971       Size +=
972           std::max(FieldWidth,
973                    1 /* integer part */ +
974                        (Precision ? 1 + Precision : 0) /* period + decimal */ +
975                        1 /* e or E letter */ + 2 /* exponent */);
976       break;
977 
978     // Floating point number in the form '[-]0xh.hhhhp±dd'.
979     case analyze_format_string::ConversionSpecifier::aArg:
980     case analyze_format_string::ConversionSpecifier::AArg:
981       Size +=
982           std::max(FieldWidth,
983                    2 /* 0x */ + 1 /* integer part */ +
984                        (Precision ? 1 + Precision : 0) /* period + decimal */ +
985                        1 /* p or P letter */ + 1 /* + or - */ + 1 /* value */);
986       break;
987 
988     // Just a string.
989     case analyze_format_string::ConversionSpecifier::sArg:
990     case analyze_format_string::ConversionSpecifier::SArg:
991       Size += FieldWidth;
992       break;
993 
994     // Just a pointer in the form '0xddd'.
995     case analyze_format_string::ConversionSpecifier::pArg:
996       // Linux kernel has its own extesion for `%p` specifier.
997       // Kernel Document:
998       // https://docs.kernel.org/core-api/printk-formats.html#pointer-types
999       IsKernelCompatible = false;
1000       Size += std::max(FieldWidth, 2 /* leading 0x */ + Precision);
1001       break;
1002 
1003     // A plain percent.
1004     case analyze_format_string::ConversionSpecifier::PercentArg:
1005       Size += 1;
1006       break;
1007 
1008     default:
1009       break;
1010     }
1011 
1012     Size += FS.hasPlusPrefix() || FS.hasSpacePrefix();
1013 
1014     if (FS.hasAlternativeForm()) {
1015       switch (FS.getConversionSpecifier().getKind()) {
1016       // For o conversion, it increases the precision, if and only if necessary,
1017       // to force the first digit of the result to be a zero
1018       // (if the value and precision are both 0, a single 0 is printed)
1019       case analyze_format_string::ConversionSpecifier::oArg:
1020       // For b conversion, a nonzero result has 0b prefixed to it.
1021       case analyze_format_string::ConversionSpecifier::bArg:
1022       // For x (or X) conversion, a nonzero result has 0x (or 0X) prefixed to
1023       // it.
1024       case analyze_format_string::ConversionSpecifier::xArg:
1025       case analyze_format_string::ConversionSpecifier::XArg:
1026         // Note: even when the prefix is added, if
1027         // (prefix_width <= FieldWidth - formatted_length) holds,
1028         // the prefix does not increase the format
1029         // size. e.g.(("%#3x", 0xf) is "0xf")
1030 
1031         // If the result is zero, o, b, x, X adds nothing.
1032         break;
1033       // For a, A, e, E, f, F, g, and G conversions,
1034       // the result of converting a floating-point number always contains a
1035       // decimal-point
1036       case analyze_format_string::ConversionSpecifier::aArg:
1037       case analyze_format_string::ConversionSpecifier::AArg:
1038       case analyze_format_string::ConversionSpecifier::eArg:
1039       case analyze_format_string::ConversionSpecifier::EArg:
1040       case analyze_format_string::ConversionSpecifier::fArg:
1041       case analyze_format_string::ConversionSpecifier::FArg:
1042       case analyze_format_string::ConversionSpecifier::gArg:
1043       case analyze_format_string::ConversionSpecifier::GArg:
1044         Size += (Precision ? 0 : 1);
1045         break;
1046       // For other conversions, the behavior is undefined.
1047       default:
1048         break;
1049       }
1050     }
1051     assert(SpecifierLen <= Size && "no underflow");
1052     Size -= SpecifierLen;
1053     return true;
1054   }
1055 
1056   size_t getSizeLowerBound() const { return Size; }
1057   bool isKernelCompatible() const { return IsKernelCompatible; }
1058 
1059 private:
1060   static size_t computeFieldWidth(const analyze_printf::PrintfSpecifier &FS) {
1061     const analyze_format_string::OptionalAmount &FW = FS.getFieldWidth();
1062     size_t FieldWidth = 0;
1063     if (FW.getHowSpecified() == analyze_format_string::OptionalAmount::Constant)
1064       FieldWidth = FW.getConstantAmount();
1065     return FieldWidth;
1066   }
1067 
1068   static size_t computePrecision(const analyze_printf::PrintfSpecifier &FS) {
1069     const analyze_format_string::OptionalAmount &FW = FS.getPrecision();
1070     size_t Precision = 0;
1071 
1072     // See man 3 printf for default precision value based on the specifier.
1073     switch (FW.getHowSpecified()) {
1074     case analyze_format_string::OptionalAmount::NotSpecified:
1075       switch (FS.getConversionSpecifier().getKind()) {
1076       default:
1077         break;
1078       case analyze_format_string::ConversionSpecifier::dArg: // %d
1079       case analyze_format_string::ConversionSpecifier::DArg: // %D
1080       case analyze_format_string::ConversionSpecifier::iArg: // %i
1081         Precision = 1;
1082         break;
1083       case analyze_format_string::ConversionSpecifier::oArg: // %d
1084       case analyze_format_string::ConversionSpecifier::OArg: // %D
1085       case analyze_format_string::ConversionSpecifier::uArg: // %d
1086       case analyze_format_string::ConversionSpecifier::UArg: // %D
1087       case analyze_format_string::ConversionSpecifier::xArg: // %d
1088       case analyze_format_string::ConversionSpecifier::XArg: // %D
1089         Precision = 1;
1090         break;
1091       case analyze_format_string::ConversionSpecifier::fArg: // %f
1092       case analyze_format_string::ConversionSpecifier::FArg: // %F
1093       case analyze_format_string::ConversionSpecifier::eArg: // %e
1094       case analyze_format_string::ConversionSpecifier::EArg: // %E
1095       case analyze_format_string::ConversionSpecifier::gArg: // %g
1096       case analyze_format_string::ConversionSpecifier::GArg: // %G
1097         Precision = 6;
1098         break;
1099       case analyze_format_string::ConversionSpecifier::pArg: // %d
1100         Precision = 1;
1101         break;
1102       }
1103       break;
1104     case analyze_format_string::OptionalAmount::Constant:
1105       Precision = FW.getConstantAmount();
1106       break;
1107     default:
1108       break;
1109     }
1110     return Precision;
1111   }
1112 };
1113 
1114 } // namespace
1115 
1116 static bool ProcessFormatStringLiteral(const Expr *FormatExpr,
1117                                        StringRef &FormatStrRef, size_t &StrLen,
1118                                        ASTContext &Context) {
1119   if (const auto *Format = dyn_cast<StringLiteral>(FormatExpr);
1120       Format && (Format->isOrdinary() || Format->isUTF8())) {
1121     FormatStrRef = Format->getString();
1122     const ConstantArrayType *T =
1123         Context.getAsConstantArrayType(Format->getType());
1124     assert(T && "String literal not of constant array type!");
1125     size_t TypeSize = T->getZExtSize();
1126     // In case there's a null byte somewhere.
1127     StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, FormatStrRef.find(0));
1128     return true;
1129   }
1130   return false;
1131 }
1132 
1133 void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD,
1134                                                CallExpr *TheCall) {
1135   if (TheCall->isValueDependent() || TheCall->isTypeDependent() ||
1136       isConstantEvaluatedContext())
1137     return;
1138 
1139   bool UseDABAttr = false;
1140   const FunctionDecl *UseDecl = FD;
1141 
1142   const auto *DABAttr = FD->getAttr<DiagnoseAsBuiltinAttr>();
1143   if (DABAttr) {
1144     UseDecl = DABAttr->getFunction();
1145     assert(UseDecl && "Missing FunctionDecl in DiagnoseAsBuiltin attribute!");
1146     UseDABAttr = true;
1147   }
1148 
1149   unsigned BuiltinID = UseDecl->getBuiltinID(/*ConsiderWrappers=*/true);
1150 
1151   if (!BuiltinID)
1152     return;
1153 
1154   const TargetInfo &TI = getASTContext().getTargetInfo();
1155   unsigned SizeTypeWidth = TI.getTypeWidth(TI.getSizeType());
1156 
1157   auto TranslateIndex = [&](unsigned Index) -> std::optional<unsigned> {
1158     // If we refer to a diagnose_as_builtin attribute, we need to change the
1159     // argument index to refer to the arguments of the called function. Unless
1160     // the index is out of bounds, which presumably means it's a variadic
1161     // function.
1162     if (!UseDABAttr)
1163       return Index;
1164     unsigned DABIndices = DABAttr->argIndices_size();
1165     unsigned NewIndex = Index < DABIndices
1166                             ? DABAttr->argIndices_begin()[Index]
1167                             : Index - DABIndices + FD->getNumParams();
1168     if (NewIndex >= TheCall->getNumArgs())
1169       return std::nullopt;
1170     return NewIndex;
1171   };
1172 
1173   auto ComputeExplicitObjectSizeArgument =
1174       [&](unsigned Index) -> std::optional<llvm::APSInt> {
1175     std::optional<unsigned> IndexOptional = TranslateIndex(Index);
1176     if (!IndexOptional)
1177       return std::nullopt;
1178     unsigned NewIndex = *IndexOptional;
1179     Expr::EvalResult Result;
1180     Expr *SizeArg = TheCall->getArg(NewIndex);
1181     if (!SizeArg->EvaluateAsInt(Result, getASTContext()))
1182       return std::nullopt;
1183     llvm::APSInt Integer = Result.Val.getInt();
1184     Integer.setIsUnsigned(true);
1185     return Integer;
1186   };
1187 
1188   auto ComputeSizeArgument =
1189       [&](unsigned Index) -> std::optional<llvm::APSInt> {
1190     // If the parameter has a pass_object_size attribute, then we should use its
1191     // (potentially) more strict checking mode. Otherwise, conservatively assume
1192     // type 0.
1193     int BOSType = 0;
1194     // This check can fail for variadic functions.
1195     if (Index < FD->getNumParams()) {
1196       if (const auto *POS =
1197               FD->getParamDecl(Index)->getAttr<PassObjectSizeAttr>())
1198         BOSType = POS->getType();
1199     }
1200 
1201     std::optional<unsigned> IndexOptional = TranslateIndex(Index);
1202     if (!IndexOptional)
1203       return std::nullopt;
1204     unsigned NewIndex = *IndexOptional;
1205 
1206     if (NewIndex >= TheCall->getNumArgs())
1207       return std::nullopt;
1208 
1209     const Expr *ObjArg = TheCall->getArg(NewIndex);
1210     uint64_t Result;
1211     if (!ObjArg->tryEvaluateObjectSize(Result, getASTContext(), BOSType))
1212       return std::nullopt;
1213 
1214     // Get the object size in the target's size_t width.
1215     return llvm::APSInt::getUnsigned(Result).extOrTrunc(SizeTypeWidth);
1216   };
1217 
1218   auto ComputeStrLenArgument =
1219       [&](unsigned Index) -> std::optional<llvm::APSInt> {
1220     std::optional<unsigned> IndexOptional = TranslateIndex(Index);
1221     if (!IndexOptional)
1222       return std::nullopt;
1223     unsigned NewIndex = *IndexOptional;
1224 
1225     const Expr *ObjArg = TheCall->getArg(NewIndex);
1226     uint64_t Result;
1227     if (!ObjArg->tryEvaluateStrLen(Result, getASTContext()))
1228       return std::nullopt;
1229     // Add 1 for null byte.
1230     return llvm::APSInt::getUnsigned(Result + 1).extOrTrunc(SizeTypeWidth);
1231   };
1232 
1233   std::optional<llvm::APSInt> SourceSize;
1234   std::optional<llvm::APSInt> DestinationSize;
1235   unsigned DiagID = 0;
1236   bool IsChkVariant = false;
1237 
1238   auto GetFunctionName = [&]() {
1239     StringRef FunctionName = getASTContext().BuiltinInfo.getName(BuiltinID);
1240     // Skim off the details of whichever builtin was called to produce a better
1241     // diagnostic, as it's unlikely that the user wrote the __builtin
1242     // explicitly.
1243     if (IsChkVariant) {
1244       FunctionName = FunctionName.drop_front(std::strlen("__builtin___"));
1245       FunctionName = FunctionName.drop_back(std::strlen("_chk"));
1246     } else {
1247       FunctionName.consume_front("__builtin_");
1248     }
1249     return FunctionName;
1250   };
1251 
1252   switch (BuiltinID) {
1253   default:
1254     return;
1255   case Builtin::BI__builtin_strcpy:
1256   case Builtin::BIstrcpy: {
1257     DiagID = diag::warn_fortify_strlen_overflow;
1258     SourceSize = ComputeStrLenArgument(1);
1259     DestinationSize = ComputeSizeArgument(0);
1260     break;
1261   }
1262 
1263   case Builtin::BI__builtin___strcpy_chk: {
1264     DiagID = diag::warn_fortify_strlen_overflow;
1265     SourceSize = ComputeStrLenArgument(1);
1266     DestinationSize = ComputeExplicitObjectSizeArgument(2);
1267     IsChkVariant = true;
1268     break;
1269   }
1270 
1271   case Builtin::BIscanf:
1272   case Builtin::BIfscanf:
1273   case Builtin::BIsscanf: {
1274     unsigned FormatIndex = 1;
1275     unsigned DataIndex = 2;
1276     if (BuiltinID == Builtin::BIscanf) {
1277       FormatIndex = 0;
1278       DataIndex = 1;
1279     }
1280 
1281     const auto *FormatExpr =
1282         TheCall->getArg(FormatIndex)->IgnoreParenImpCasts();
1283 
1284     StringRef FormatStrRef;
1285     size_t StrLen;
1286     if (!ProcessFormatStringLiteral(FormatExpr, FormatStrRef, StrLen, Context))
1287       return;
1288 
1289     auto Diagnose = [&](unsigned ArgIndex, unsigned DestSize,
1290                         unsigned SourceSize) {
1291       DiagID = diag::warn_fortify_scanf_overflow;
1292       unsigned Index = ArgIndex + DataIndex;
1293       StringRef FunctionName = GetFunctionName();
1294       DiagRuntimeBehavior(TheCall->getArg(Index)->getBeginLoc(), TheCall,
1295                           PDiag(DiagID) << FunctionName << (Index + 1)
1296                                         << DestSize << SourceSize);
1297     };
1298 
1299     auto ShiftedComputeSizeArgument = [&](unsigned Index) {
1300       return ComputeSizeArgument(Index + DataIndex);
1301     };
1302     ScanfDiagnosticFormatHandler H(ShiftedComputeSizeArgument, Diagnose);
1303     const char *FormatBytes = FormatStrRef.data();
1304     analyze_format_string::ParseScanfString(H, FormatBytes,
1305                                             FormatBytes + StrLen, getLangOpts(),
1306                                             Context.getTargetInfo());
1307 
1308     // Unlike the other cases, in this one we have already issued the diagnostic
1309     // here, so no need to continue (because unlike the other cases, here the
1310     // diagnostic refers to the argument number).
1311     return;
1312   }
1313 
1314   case Builtin::BIsprintf:
1315   case Builtin::BI__builtin___sprintf_chk: {
1316     size_t FormatIndex = BuiltinID == Builtin::BIsprintf ? 1 : 3;
1317     auto *FormatExpr = TheCall->getArg(FormatIndex)->IgnoreParenImpCasts();
1318 
1319     StringRef FormatStrRef;
1320     size_t StrLen;
1321     if (ProcessFormatStringLiteral(FormatExpr, FormatStrRef, StrLen, Context)) {
1322       EstimateSizeFormatHandler H(FormatStrRef);
1323       const char *FormatBytes = FormatStrRef.data();
1324       if (!analyze_format_string::ParsePrintfString(
1325               H, FormatBytes, FormatBytes + StrLen, getLangOpts(),
1326               Context.getTargetInfo(), false)) {
1327         DiagID = H.isKernelCompatible()
1328                      ? diag::warn_format_overflow
1329                      : diag::warn_format_overflow_non_kprintf;
1330         SourceSize = llvm::APSInt::getUnsigned(H.getSizeLowerBound())
1331                          .extOrTrunc(SizeTypeWidth);
1332         if (BuiltinID == Builtin::BI__builtin___sprintf_chk) {
1333           DestinationSize = ComputeExplicitObjectSizeArgument(2);
1334           IsChkVariant = true;
1335         } else {
1336           DestinationSize = ComputeSizeArgument(0);
1337         }
1338         break;
1339       }
1340     }
1341     return;
1342   }
1343   case Builtin::BI__builtin___memcpy_chk:
1344   case Builtin::BI__builtin___memmove_chk:
1345   case Builtin::BI__builtin___memset_chk:
1346   case Builtin::BI__builtin___strlcat_chk:
1347   case Builtin::BI__builtin___strlcpy_chk:
1348   case Builtin::BI__builtin___strncat_chk:
1349   case Builtin::BI__builtin___strncpy_chk:
1350   case Builtin::BI__builtin___stpncpy_chk:
1351   case Builtin::BI__builtin___memccpy_chk:
1352   case Builtin::BI__builtin___mempcpy_chk: {
1353     DiagID = diag::warn_builtin_chk_overflow;
1354     SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 2);
1355     DestinationSize =
1356         ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1);
1357     IsChkVariant = true;
1358     break;
1359   }
1360 
1361   case Builtin::BI__builtin___snprintf_chk:
1362   case Builtin::BI__builtin___vsnprintf_chk: {
1363     DiagID = diag::warn_builtin_chk_overflow;
1364     SourceSize = ComputeExplicitObjectSizeArgument(1);
1365     DestinationSize = ComputeExplicitObjectSizeArgument(3);
1366     IsChkVariant = true;
1367     break;
1368   }
1369 
1370   case Builtin::BIstrncat:
1371   case Builtin::BI__builtin_strncat:
1372   case Builtin::BIstrncpy:
1373   case Builtin::BI__builtin_strncpy:
1374   case Builtin::BIstpncpy:
1375   case Builtin::BI__builtin_stpncpy: {
1376     // Whether these functions overflow depends on the runtime strlen of the
1377     // string, not just the buffer size, so emitting the "always overflow"
1378     // diagnostic isn't quite right. We should still diagnose passing a buffer
1379     // size larger than the destination buffer though; this is a runtime abort
1380     // in _FORTIFY_SOURCE mode, and is quite suspicious otherwise.
1381     DiagID = diag::warn_fortify_source_size_mismatch;
1382     SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1);
1383     DestinationSize = ComputeSizeArgument(0);
1384     break;
1385   }
1386 
1387   case Builtin::BImemcpy:
1388   case Builtin::BI__builtin_memcpy:
1389   case Builtin::BImemmove:
1390   case Builtin::BI__builtin_memmove:
1391   case Builtin::BImemset:
1392   case Builtin::BI__builtin_memset:
1393   case Builtin::BImempcpy:
1394   case Builtin::BI__builtin_mempcpy: {
1395     DiagID = diag::warn_fortify_source_overflow;
1396     SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1);
1397     DestinationSize = ComputeSizeArgument(0);
1398     break;
1399   }
1400   case Builtin::BIsnprintf:
1401   case Builtin::BI__builtin_snprintf:
1402   case Builtin::BIvsnprintf:
1403   case Builtin::BI__builtin_vsnprintf: {
1404     DiagID = diag::warn_fortify_source_size_mismatch;
1405     SourceSize = ComputeExplicitObjectSizeArgument(1);
1406     const auto *FormatExpr = TheCall->getArg(2)->IgnoreParenImpCasts();
1407     StringRef FormatStrRef;
1408     size_t StrLen;
1409     if (SourceSize &&
1410         ProcessFormatStringLiteral(FormatExpr, FormatStrRef, StrLen, Context)) {
1411       EstimateSizeFormatHandler H(FormatStrRef);
1412       const char *FormatBytes = FormatStrRef.data();
1413       if (!analyze_format_string::ParsePrintfString(
1414               H, FormatBytes, FormatBytes + StrLen, getLangOpts(),
1415               Context.getTargetInfo(), /*isFreeBSDKPrintf=*/false)) {
1416         llvm::APSInt FormatSize =
1417             llvm::APSInt::getUnsigned(H.getSizeLowerBound())
1418                 .extOrTrunc(SizeTypeWidth);
1419         if (FormatSize > *SourceSize && *SourceSize != 0) {
1420           unsigned TruncationDiagID =
1421               H.isKernelCompatible() ? diag::warn_format_truncation
1422                                      : diag::warn_format_truncation_non_kprintf;
1423           SmallString<16> SpecifiedSizeStr;
1424           SmallString<16> FormatSizeStr;
1425           SourceSize->toString(SpecifiedSizeStr, /*Radix=*/10);
1426           FormatSize.toString(FormatSizeStr, /*Radix=*/10);
1427           DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall,
1428                               PDiag(TruncationDiagID)
1429                                   << GetFunctionName() << SpecifiedSizeStr
1430                                   << FormatSizeStr);
1431         }
1432       }
1433     }
1434     DestinationSize = ComputeSizeArgument(0);
1435   }
1436   }
1437 
1438   if (!SourceSize || !DestinationSize ||
1439       llvm::APSInt::compareValues(*SourceSize, *DestinationSize) <= 0)
1440     return;
1441 
1442   StringRef FunctionName = GetFunctionName();
1443 
1444   SmallString<16> DestinationStr;
1445   SmallString<16> SourceStr;
1446   DestinationSize->toString(DestinationStr, /*Radix=*/10);
1447   SourceSize->toString(SourceStr, /*Radix=*/10);
1448   DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall,
1449                       PDiag(DiagID)
1450                           << FunctionName << DestinationStr << SourceStr);
1451 }
1452 
1453 static bool BuiltinSEHScopeCheck(Sema &SemaRef, CallExpr *TheCall,
1454                                  Scope::ScopeFlags NeededScopeFlags,
1455                                  unsigned DiagID) {
1456   // Scopes aren't available during instantiation. Fortunately, builtin
1457   // functions cannot be template args so they cannot be formed through template
1458   // instantiation. Therefore checking once during the parse is sufficient.
1459   if (SemaRef.inTemplateInstantiation())
1460     return false;
1461 
1462   Scope *S = SemaRef.getCurScope();
1463   while (S && !S->isSEHExceptScope())
1464     S = S->getParent();
1465   if (!S || !(S->getFlags() & NeededScopeFlags)) {
1466     auto *DRE = cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
1467     SemaRef.Diag(TheCall->getExprLoc(), DiagID)
1468         << DRE->getDecl()->getIdentifier();
1469     return true;
1470   }
1471 
1472   return false;
1473 }
1474 
1475 // In OpenCL, __builtin_alloca_* should return a pointer to address space
1476 // that corresponds to the stack address space i.e private address space.
1477 static void builtinAllocaAddrSpace(Sema &S, CallExpr *TheCall) {
1478   QualType RT = TheCall->getType();
1479   assert((RT->isPointerType() && !(RT->getPointeeType().hasAddressSpace())) &&
1480          "__builtin_alloca has invalid address space");
1481 
1482   RT = RT->getPointeeType();
1483   RT = S.Context.getAddrSpaceQualType(RT, LangAS::opencl_private);
1484   TheCall->setType(S.Context.getPointerType(RT));
1485 }
1486 
1487 namespace {
1488 enum PointerAuthOpKind {
1489   PAO_Strip,
1490   PAO_Sign,
1491   PAO_Auth,
1492   PAO_SignGeneric,
1493   PAO_Discriminator,
1494   PAO_BlendPointer,
1495   PAO_BlendInteger
1496 };
1497 }
1498 
1499 bool Sema::checkPointerAuthEnabled(SourceLocation Loc, SourceRange Range) {
1500   if (getLangOpts().PointerAuthIntrinsics)
1501     return false;
1502 
1503   Diag(Loc, diag::err_ptrauth_disabled) << Range;
1504   return true;
1505 }
1506 
1507 static bool checkPointerAuthEnabled(Sema &S, Expr *E) {
1508   return S.checkPointerAuthEnabled(E->getExprLoc(), E->getSourceRange());
1509 }
1510 
1511 static bool checkPointerAuthKey(Sema &S, Expr *&Arg) {
1512   // Convert it to type 'int'.
1513   if (convertArgumentToType(S, Arg, S.Context.IntTy))
1514     return true;
1515 
1516   // Value-dependent expressions are okay; wait for template instantiation.
1517   if (Arg->isValueDependent())
1518     return false;
1519 
1520   unsigned KeyValue;
1521   return S.checkConstantPointerAuthKey(Arg, KeyValue);
1522 }
1523 
1524 bool Sema::checkConstantPointerAuthKey(Expr *Arg, unsigned &Result) {
1525   // Attempt to constant-evaluate the expression.
1526   std::optional<llvm::APSInt> KeyValue = Arg->getIntegerConstantExpr(Context);
1527   if (!KeyValue) {
1528     Diag(Arg->getExprLoc(), diag::err_expr_not_ice)
1529         << 0 << Arg->getSourceRange();
1530     return true;
1531   }
1532 
1533   // Ask the target to validate the key parameter.
1534   if (!Context.getTargetInfo().validatePointerAuthKey(*KeyValue)) {
1535     llvm::SmallString<32> Value;
1536     {
1537       llvm::raw_svector_ostream Str(Value);
1538       Str << *KeyValue;
1539     }
1540 
1541     Diag(Arg->getExprLoc(), diag::err_ptrauth_invalid_key)
1542         << Value << Arg->getSourceRange();
1543     return true;
1544   }
1545 
1546   Result = KeyValue->getZExtValue();
1547   return false;
1548 }
1549 
1550 static std::pair<const ValueDecl *, CharUnits>
1551 findConstantBaseAndOffset(Sema &S, Expr *E) {
1552   // Must evaluate as a pointer.
1553   Expr::EvalResult Result;
1554   if (!E->EvaluateAsRValue(Result, S.Context) || !Result.Val.isLValue())
1555     return {nullptr, CharUnits()};
1556 
1557   const auto *BaseDecl =
1558       Result.Val.getLValueBase().dyn_cast<const ValueDecl *>();
1559   if (!BaseDecl)
1560     return {nullptr, CharUnits()};
1561 
1562   return {BaseDecl, Result.Val.getLValueOffset()};
1563 }
1564 
1565 static bool checkPointerAuthValue(Sema &S, Expr *&Arg, PointerAuthOpKind OpKind,
1566                                   bool RequireConstant = false) {
1567   if (Arg->hasPlaceholderType()) {
1568     ExprResult R = S.CheckPlaceholderExpr(Arg);
1569     if (R.isInvalid())
1570       return true;
1571     Arg = R.get();
1572   }
1573 
1574   auto AllowsPointer = [](PointerAuthOpKind OpKind) {
1575     return OpKind != PAO_BlendInteger;
1576   };
1577   auto AllowsInteger = [](PointerAuthOpKind OpKind) {
1578     return OpKind == PAO_Discriminator || OpKind == PAO_BlendInteger ||
1579            OpKind == PAO_SignGeneric;
1580   };
1581 
1582   // Require the value to have the right range of type.
1583   QualType ExpectedTy;
1584   if (AllowsPointer(OpKind) && Arg->getType()->isPointerType()) {
1585     ExpectedTy = Arg->getType().getUnqualifiedType();
1586   } else if (AllowsPointer(OpKind) && Arg->getType()->isNullPtrType()) {
1587     ExpectedTy = S.Context.VoidPtrTy;
1588   } else if (AllowsInteger(OpKind) &&
1589              Arg->getType()->isIntegralOrUnscopedEnumerationType()) {
1590     ExpectedTy = S.Context.getUIntPtrType();
1591 
1592   } else {
1593     // Diagnose the failures.
1594     S.Diag(Arg->getExprLoc(), diag::err_ptrauth_value_bad_type)
1595         << unsigned(OpKind == PAO_Discriminator  ? 1
1596                     : OpKind == PAO_BlendPointer ? 2
1597                     : OpKind == PAO_BlendInteger ? 3
1598                                                  : 0)
1599         << unsigned(AllowsInteger(OpKind) ? (AllowsPointer(OpKind) ? 2 : 1) : 0)
1600         << Arg->getType() << Arg->getSourceRange();
1601     return true;
1602   }
1603 
1604   // Convert to that type.  This should just be an lvalue-to-rvalue
1605   // conversion.
1606   if (convertArgumentToType(S, Arg, ExpectedTy))
1607     return true;
1608 
1609   if (!RequireConstant) {
1610     // Warn about null pointers for non-generic sign and auth operations.
1611     if ((OpKind == PAO_Sign || OpKind == PAO_Auth) &&
1612         Arg->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNull)) {
1613       S.Diag(Arg->getExprLoc(), OpKind == PAO_Sign
1614                                     ? diag::warn_ptrauth_sign_null_pointer
1615                                     : diag::warn_ptrauth_auth_null_pointer)
1616           << Arg->getSourceRange();
1617     }
1618 
1619     return false;
1620   }
1621 
1622   // Perform special checking on the arguments to ptrauth_sign_constant.
1623 
1624   // The main argument.
1625   if (OpKind == PAO_Sign) {
1626     // Require the value we're signing to have a special form.
1627     auto [BaseDecl, Offset] = findConstantBaseAndOffset(S, Arg);
1628     bool Invalid;
1629 
1630     // Must be rooted in a declaration reference.
1631     if (!BaseDecl)
1632       Invalid = true;
1633 
1634     // If it's a function declaration, we can't have an offset.
1635     else if (isa<FunctionDecl>(BaseDecl))
1636       Invalid = !Offset.isZero();
1637 
1638     // Otherwise we're fine.
1639     else
1640       Invalid = false;
1641 
1642     if (Invalid)
1643       S.Diag(Arg->getExprLoc(), diag::err_ptrauth_bad_constant_pointer);
1644     return Invalid;
1645   }
1646 
1647   // The discriminator argument.
1648   assert(OpKind == PAO_Discriminator);
1649 
1650   // Must be a pointer or integer or blend thereof.
1651   Expr *Pointer = nullptr;
1652   Expr *Integer = nullptr;
1653   if (auto *Call = dyn_cast<CallExpr>(Arg->IgnoreParens())) {
1654     if (Call->getBuiltinCallee() ==
1655         Builtin::BI__builtin_ptrauth_blend_discriminator) {
1656       Pointer = Call->getArg(0);
1657       Integer = Call->getArg(1);
1658     }
1659   }
1660   if (!Pointer && !Integer) {
1661     if (Arg->getType()->isPointerType())
1662       Pointer = Arg;
1663     else
1664       Integer = Arg;
1665   }
1666 
1667   // Check the pointer.
1668   bool Invalid = false;
1669   if (Pointer) {
1670     assert(Pointer->getType()->isPointerType());
1671 
1672     // TODO: if we're initializing a global, check that the address is
1673     // somehow related to what we're initializing.  This probably will
1674     // never really be feasible and we'll have to catch it at link-time.
1675     auto [BaseDecl, Offset] = findConstantBaseAndOffset(S, Pointer);
1676     if (!BaseDecl || !isa<VarDecl>(BaseDecl))
1677       Invalid = true;
1678   }
1679 
1680   // Check the integer.
1681   if (Integer) {
1682     assert(Integer->getType()->isIntegerType());
1683     if (!Integer->isEvaluatable(S.Context))
1684       Invalid = true;
1685   }
1686 
1687   if (Invalid)
1688     S.Diag(Arg->getExprLoc(), diag::err_ptrauth_bad_constant_discriminator);
1689   return Invalid;
1690 }
1691 
1692 static ExprResult PointerAuthStrip(Sema &S, CallExpr *Call) {
1693   if (S.checkArgCount(Call, 2))
1694     return ExprError();
1695   if (checkPointerAuthEnabled(S, Call))
1696     return ExprError();
1697   if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_Strip) ||
1698       checkPointerAuthKey(S, Call->getArgs()[1]))
1699     return ExprError();
1700 
1701   Call->setType(Call->getArgs()[0]->getType());
1702   return Call;
1703 }
1704 
1705 static ExprResult PointerAuthBlendDiscriminator(Sema &S, CallExpr *Call) {
1706   if (S.checkArgCount(Call, 2))
1707     return ExprError();
1708   if (checkPointerAuthEnabled(S, Call))
1709     return ExprError();
1710   if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_BlendPointer) ||
1711       checkPointerAuthValue(S, Call->getArgs()[1], PAO_BlendInteger))
1712     return ExprError();
1713 
1714   Call->setType(S.Context.getUIntPtrType());
1715   return Call;
1716 }
1717 
1718 static ExprResult PointerAuthSignGenericData(Sema &S, CallExpr *Call) {
1719   if (S.checkArgCount(Call, 2))
1720     return ExprError();
1721   if (checkPointerAuthEnabled(S, Call))
1722     return ExprError();
1723   if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_SignGeneric) ||
1724       checkPointerAuthValue(S, Call->getArgs()[1], PAO_Discriminator))
1725     return ExprError();
1726 
1727   Call->setType(S.Context.getUIntPtrType());
1728   return Call;
1729 }
1730 
1731 static ExprResult PointerAuthSignOrAuth(Sema &S, CallExpr *Call,
1732                                         PointerAuthOpKind OpKind,
1733                                         bool RequireConstant) {
1734   if (S.checkArgCount(Call, 3))
1735     return ExprError();
1736   if (checkPointerAuthEnabled(S, Call))
1737     return ExprError();
1738   if (checkPointerAuthValue(S, Call->getArgs()[0], OpKind, RequireConstant) ||
1739       checkPointerAuthKey(S, Call->getArgs()[1]) ||
1740       checkPointerAuthValue(S, Call->getArgs()[2], PAO_Discriminator,
1741                             RequireConstant))
1742     return ExprError();
1743 
1744   Call->setType(Call->getArgs()[0]->getType());
1745   return Call;
1746 }
1747 
1748 static ExprResult PointerAuthAuthAndResign(Sema &S, CallExpr *Call) {
1749   if (S.checkArgCount(Call, 5))
1750     return ExprError();
1751   if (checkPointerAuthEnabled(S, Call))
1752     return ExprError();
1753   if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_Auth) ||
1754       checkPointerAuthKey(S, Call->getArgs()[1]) ||
1755       checkPointerAuthValue(S, Call->getArgs()[2], PAO_Discriminator) ||
1756       checkPointerAuthKey(S, Call->getArgs()[3]) ||
1757       checkPointerAuthValue(S, Call->getArgs()[4], PAO_Discriminator))
1758     return ExprError();
1759 
1760   Call->setType(Call->getArgs()[0]->getType());
1761   return Call;
1762 }
1763 
1764 static ExprResult PointerAuthStringDiscriminator(Sema &S, CallExpr *Call) {
1765   if (checkPointerAuthEnabled(S, Call))
1766     return ExprError();
1767 
1768   // We've already performed normal call type-checking.
1769   const Expr *Arg = Call->getArg(0)->IgnoreParenImpCasts();
1770 
1771   // Operand must be an ordinary or UTF-8 string literal.
1772   const auto *Literal = dyn_cast<StringLiteral>(Arg);
1773   if (!Literal || Literal->getCharByteWidth() != 1) {
1774     S.Diag(Arg->getExprLoc(), diag::err_ptrauth_string_not_literal)
1775         << (Literal ? 1 : 0) << Arg->getSourceRange();
1776     return ExprError();
1777   }
1778 
1779   return Call;
1780 }
1781 
1782 static ExprResult BuiltinLaunder(Sema &S, CallExpr *TheCall) {
1783   if (S.checkArgCount(TheCall, 1))
1784     return ExprError();
1785 
1786   // Compute __builtin_launder's parameter type from the argument.
1787   // The parameter type is:
1788   //  * The type of the argument if it's not an array or function type,
1789   //  Otherwise,
1790   //  * The decayed argument type.
1791   QualType ParamTy = [&]() {
1792     QualType ArgTy = TheCall->getArg(0)->getType();
1793     if (const ArrayType *Ty = ArgTy->getAsArrayTypeUnsafe())
1794       return S.Context.getPointerType(Ty->getElementType());
1795     if (ArgTy->isFunctionType()) {
1796       return S.Context.getPointerType(ArgTy);
1797     }
1798     return ArgTy;
1799   }();
1800 
1801   TheCall->setType(ParamTy);
1802 
1803   auto DiagSelect = [&]() -> std::optional<unsigned> {
1804     if (!ParamTy->isPointerType())
1805       return 0;
1806     if (ParamTy->isFunctionPointerType())
1807       return 1;
1808     if (ParamTy->isVoidPointerType())
1809       return 2;
1810     return std::optional<unsigned>{};
1811   }();
1812   if (DiagSelect) {
1813     S.Diag(TheCall->getBeginLoc(), diag::err_builtin_launder_invalid_arg)
1814         << *DiagSelect << TheCall->getSourceRange();
1815     return ExprError();
1816   }
1817 
1818   // We either have an incomplete class type, or we have a class template
1819   // whose instantiation has not been forced. Example:
1820   //
1821   //   template <class T> struct Foo { T value; };
1822   //   Foo<int> *p = nullptr;
1823   //   auto *d = __builtin_launder(p);
1824   if (S.RequireCompleteType(TheCall->getBeginLoc(), ParamTy->getPointeeType(),
1825                             diag::err_incomplete_type))
1826     return ExprError();
1827 
1828   assert(ParamTy->getPointeeType()->isObjectType() &&
1829          "Unhandled non-object pointer case");
1830 
1831   InitializedEntity Entity =
1832       InitializedEntity::InitializeParameter(S.Context, ParamTy, false);
1833   ExprResult Arg =
1834       S.PerformCopyInitialization(Entity, SourceLocation(), TheCall->getArg(0));
1835   if (Arg.isInvalid())
1836     return ExprError();
1837   TheCall->setArg(0, Arg.get());
1838 
1839   return TheCall;
1840 }
1841 
1842 static ExprResult BuiltinIsWithinLifetime(Sema &S, CallExpr *TheCall) {
1843   if (S.checkArgCount(TheCall, 1))
1844     return ExprError();
1845 
1846   ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(0));
1847   if (Arg.isInvalid())
1848     return ExprError();
1849   QualType ParamTy = Arg.get()->getType();
1850   TheCall->setArg(0, Arg.get());
1851   TheCall->setType(S.Context.BoolTy);
1852 
1853   // Only accept pointers to objects as arguments, which should have object
1854   // pointer or void pointer types.
1855   if (const auto *PT = ParamTy->getAs<PointerType>()) {
1856     // LWG4138: Function pointer types not allowed
1857     if (PT->getPointeeType()->isFunctionType()) {
1858       S.Diag(TheCall->getArg(0)->getExprLoc(),
1859              diag::err_builtin_is_within_lifetime_invalid_arg)
1860           << 1;
1861       return ExprError();
1862     }
1863     // Disallow VLAs too since those shouldn't be able to
1864     // be a template parameter for `std::is_within_lifetime`
1865     if (PT->getPointeeType()->isVariableArrayType()) {
1866       S.Diag(TheCall->getArg(0)->getExprLoc(), diag::err_vla_unsupported)
1867           << 1 << "__builtin_is_within_lifetime";
1868       return ExprError();
1869     }
1870   } else {
1871     S.Diag(TheCall->getArg(0)->getExprLoc(),
1872            diag::err_builtin_is_within_lifetime_invalid_arg)
1873         << 0;
1874     return ExprError();
1875   }
1876 
1877   return TheCall;
1878 }
1879 
1880 // Emit an error and return true if the current object format type is in the
1881 // list of unsupported types.
1882 static bool CheckBuiltinTargetNotInUnsupported(
1883     Sema &S, unsigned BuiltinID, CallExpr *TheCall,
1884     ArrayRef<llvm::Triple::ObjectFormatType> UnsupportedObjectFormatTypes) {
1885   llvm::Triple::ObjectFormatType CurObjFormat =
1886       S.getASTContext().getTargetInfo().getTriple().getObjectFormat();
1887   if (llvm::is_contained(UnsupportedObjectFormatTypes, CurObjFormat)) {
1888     S.Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported)
1889         << TheCall->getSourceRange();
1890     return true;
1891   }
1892   return false;
1893 }
1894 
1895 // Emit an error and return true if the current architecture is not in the list
1896 // of supported architectures.
1897 static bool
1898 CheckBuiltinTargetInSupported(Sema &S, CallExpr *TheCall,
1899                               ArrayRef<llvm::Triple::ArchType> SupportedArchs) {
1900   llvm::Triple::ArchType CurArch =
1901       S.getASTContext().getTargetInfo().getTriple().getArch();
1902   if (llvm::is_contained(SupportedArchs, CurArch))
1903     return false;
1904   S.Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported)
1905       << TheCall->getSourceRange();
1906   return true;
1907 }
1908 
1909 static void CheckNonNullArgument(Sema &S, const Expr *ArgExpr,
1910                                  SourceLocation CallSiteLoc);
1911 
1912 bool Sema::CheckTSBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
1913                                       CallExpr *TheCall) {
1914   switch (TI.getTriple().getArch()) {
1915   default:
1916     // Some builtins don't require additional checking, so just consider these
1917     // acceptable.
1918     return false;
1919   case llvm::Triple::arm:
1920   case llvm::Triple::armeb:
1921   case llvm::Triple::thumb:
1922   case llvm::Triple::thumbeb:
1923     return ARM().CheckARMBuiltinFunctionCall(TI, BuiltinID, TheCall);
1924   case llvm::Triple::aarch64:
1925   case llvm::Triple::aarch64_32:
1926   case llvm::Triple::aarch64_be:
1927     return ARM().CheckAArch64BuiltinFunctionCall(TI, BuiltinID, TheCall);
1928   case llvm::Triple::bpfeb:
1929   case llvm::Triple::bpfel:
1930     return BPF().CheckBPFBuiltinFunctionCall(BuiltinID, TheCall);
1931   case llvm::Triple::hexagon:
1932     return Hexagon().CheckHexagonBuiltinFunctionCall(BuiltinID, TheCall);
1933   case llvm::Triple::mips:
1934   case llvm::Triple::mipsel:
1935   case llvm::Triple::mips64:
1936   case llvm::Triple::mips64el:
1937     return MIPS().CheckMipsBuiltinFunctionCall(TI, BuiltinID, TheCall);
1938   case llvm::Triple::spirv:
1939     return SPIRV().CheckSPIRVBuiltinFunctionCall(BuiltinID, TheCall);
1940   case llvm::Triple::systemz:
1941     return SystemZ().CheckSystemZBuiltinFunctionCall(BuiltinID, TheCall);
1942   case llvm::Triple::x86:
1943   case llvm::Triple::x86_64:
1944     return X86().CheckBuiltinFunctionCall(TI, BuiltinID, TheCall);
1945   case llvm::Triple::ppc:
1946   case llvm::Triple::ppcle:
1947   case llvm::Triple::ppc64:
1948   case llvm::Triple::ppc64le:
1949     return PPC().CheckPPCBuiltinFunctionCall(TI, BuiltinID, TheCall);
1950   case llvm::Triple::amdgcn:
1951     return AMDGPU().CheckAMDGCNBuiltinFunctionCall(BuiltinID, TheCall);
1952   case llvm::Triple::riscv32:
1953   case llvm::Triple::riscv64:
1954     return RISCV().CheckBuiltinFunctionCall(TI, BuiltinID, TheCall);
1955   case llvm::Triple::loongarch32:
1956   case llvm::Triple::loongarch64:
1957     return LoongArch().CheckLoongArchBuiltinFunctionCall(TI, BuiltinID,
1958                                                          TheCall);
1959   case llvm::Triple::wasm32:
1960   case llvm::Triple::wasm64:
1961     return Wasm().CheckWebAssemblyBuiltinFunctionCall(TI, BuiltinID, TheCall);
1962   case llvm::Triple::nvptx:
1963   case llvm::Triple::nvptx64:
1964     return NVPTX().CheckNVPTXBuiltinFunctionCall(TI, BuiltinID, TheCall);
1965   }
1966 }
1967 
1968 // Check if \p Ty is a valid type for the elementwise math builtins. If it is
1969 // not a valid type, emit an error message and return true. Otherwise return
1970 // false.
1971 static bool checkMathBuiltinElementType(Sema &S, SourceLocation Loc,
1972                                         QualType ArgTy, int ArgIndex) {
1973   if (!ArgTy->getAs<VectorType>() &&
1974       !ConstantMatrixType::isValidElementType(ArgTy)) {
1975     return S.Diag(Loc, diag::err_builtin_invalid_arg_type)
1976            << ArgIndex << /* vector, integer or float ty*/ 0 << ArgTy;
1977   }
1978 
1979   return false;
1980 }
1981 
1982 static bool checkFPMathBuiltinElementType(Sema &S, SourceLocation Loc,
1983                                           QualType ArgTy, int ArgIndex) {
1984   QualType EltTy = ArgTy;
1985   if (auto *VecTy = EltTy->getAs<VectorType>())
1986     EltTy = VecTy->getElementType();
1987 
1988   if (!EltTy->isRealFloatingType()) {
1989     return S.Diag(Loc, diag::err_builtin_invalid_arg_type)
1990            << ArgIndex << /* vector or float ty*/ 5 << ArgTy;
1991   }
1992 
1993   return false;
1994 }
1995 
1996 /// BuiltinCpu{Supports|Is} - Handle __builtin_cpu_{supports|is}(char *).
1997 /// This checks that the target supports the builtin and that the string
1998 /// argument is constant and valid.
1999 static bool BuiltinCpu(Sema &S, const TargetInfo &TI, CallExpr *TheCall,
2000                        const TargetInfo *AuxTI, unsigned BuiltinID) {
2001   assert((BuiltinID == Builtin::BI__builtin_cpu_supports ||
2002           BuiltinID == Builtin::BI__builtin_cpu_is) &&
2003          "Expecting __builtin_cpu_...");
2004 
2005   bool IsCPUSupports = BuiltinID == Builtin::BI__builtin_cpu_supports;
2006   const TargetInfo *TheTI = &TI;
2007   auto SupportsBI = [=](const TargetInfo *TInfo) {
2008     return TInfo && ((IsCPUSupports && TInfo->supportsCpuSupports()) ||
2009                      (!IsCPUSupports && TInfo->supportsCpuIs()));
2010   };
2011   if (!SupportsBI(&TI) && SupportsBI(AuxTI))
2012     TheTI = AuxTI;
2013 
2014   if ((!IsCPUSupports && !TheTI->supportsCpuIs()) ||
2015       (IsCPUSupports && !TheTI->supportsCpuSupports()))
2016     return S.Diag(TheCall->getBeginLoc(),
2017                   TI.getTriple().isOSAIX()
2018                       ? diag::err_builtin_aix_os_unsupported
2019                       : diag::err_builtin_target_unsupported)
2020            << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc());
2021 
2022   Expr *Arg = TheCall->getArg(0)->IgnoreParenImpCasts();
2023   // Check if the argument is a string literal.
2024   if (!isa<StringLiteral>(Arg))
2025     return S.Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal)
2026            << Arg->getSourceRange();
2027 
2028   // Check the contents of the string.
2029   StringRef Feature = cast<StringLiteral>(Arg)->getString();
2030   if (IsCPUSupports && !TheTI->validateCpuSupports(Feature)) {
2031     S.Diag(TheCall->getBeginLoc(), diag::warn_invalid_cpu_supports)
2032         << Arg->getSourceRange();
2033     return false;
2034   }
2035   if (!IsCPUSupports && !TheTI->validateCpuIs(Feature))
2036     return S.Diag(TheCall->getBeginLoc(), diag::err_invalid_cpu_is)
2037            << Arg->getSourceRange();
2038   return false;
2039 }
2040 
2041 /// Checks that __builtin_popcountg was called with a single argument, which is
2042 /// an unsigned integer.
2043 static bool BuiltinPopcountg(Sema &S, CallExpr *TheCall) {
2044   if (S.checkArgCount(TheCall, 1))
2045     return true;
2046 
2047   ExprResult ArgRes = S.DefaultLvalueConversion(TheCall->getArg(0));
2048   if (ArgRes.isInvalid())
2049     return true;
2050 
2051   Expr *Arg = ArgRes.get();
2052   TheCall->setArg(0, Arg);
2053 
2054   QualType ArgTy = Arg->getType();
2055 
2056   if (!ArgTy->isUnsignedIntegerType()) {
2057     S.Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
2058         << 1 << /*unsigned integer ty*/ 7 << ArgTy;
2059     return true;
2060   }
2061   return false;
2062 }
2063 
2064 /// Checks that __builtin_{clzg,ctzg} was called with a first argument, which is
2065 /// an unsigned integer, and an optional second argument, which is promoted to
2066 /// an 'int'.
2067 static bool BuiltinCountZeroBitsGeneric(Sema &S, CallExpr *TheCall) {
2068   if (S.checkArgCountRange(TheCall, 1, 2))
2069     return true;
2070 
2071   ExprResult Arg0Res = S.DefaultLvalueConversion(TheCall->getArg(0));
2072   if (Arg0Res.isInvalid())
2073     return true;
2074 
2075   Expr *Arg0 = Arg0Res.get();
2076   TheCall->setArg(0, Arg0);
2077 
2078   QualType Arg0Ty = Arg0->getType();
2079 
2080   if (!Arg0Ty->isUnsignedIntegerType()) {
2081     S.Diag(Arg0->getBeginLoc(), diag::err_builtin_invalid_arg_type)
2082         << 1 << /*unsigned integer ty*/ 7 << Arg0Ty;
2083     return true;
2084   }
2085 
2086   if (TheCall->getNumArgs() > 1) {
2087     ExprResult Arg1Res = S.UsualUnaryConversions(TheCall->getArg(1));
2088     if (Arg1Res.isInvalid())
2089       return true;
2090 
2091     Expr *Arg1 = Arg1Res.get();
2092     TheCall->setArg(1, Arg1);
2093 
2094     QualType Arg1Ty = Arg1->getType();
2095 
2096     if (!Arg1Ty->isSpecificBuiltinType(BuiltinType::Int)) {
2097       S.Diag(Arg1->getBeginLoc(), diag::err_builtin_invalid_arg_type)
2098           << 2 << /*'int' ty*/ 8 << Arg1Ty;
2099       return true;
2100     }
2101   }
2102 
2103   return false;
2104 }
2105 
2106 ExprResult
2107 Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
2108                                CallExpr *TheCall) {
2109   ExprResult TheCallResult(TheCall);
2110 
2111   // Find out if any arguments are required to be integer constant expressions.
2112   unsigned ICEArguments = 0;
2113   ASTContext::GetBuiltinTypeError Error;
2114   Context.GetBuiltinType(BuiltinID, Error, &ICEArguments);
2115   if (Error != ASTContext::GE_None)
2116     ICEArguments = 0;  // Don't diagnose previously diagnosed errors.
2117 
2118   // If any arguments are required to be ICE's, check and diagnose.
2119   for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) {
2120     // Skip arguments not required to be ICE's.
2121     if ((ICEArguments & (1 << ArgNo)) == 0) continue;
2122 
2123     llvm::APSInt Result;
2124     // If we don't have enough arguments, continue so we can issue better
2125     // diagnostic in checkArgCount(...)
2126     if (ArgNo < TheCall->getNumArgs() &&
2127         BuiltinConstantArg(TheCall, ArgNo, Result))
2128       return true;
2129     ICEArguments &= ~(1 << ArgNo);
2130   }
2131 
2132   FPOptions FPO;
2133   switch (BuiltinID) {
2134   case Builtin::BI__builtin_cpu_supports:
2135   case Builtin::BI__builtin_cpu_is:
2136     if (BuiltinCpu(*this, Context.getTargetInfo(), TheCall,
2137                    Context.getAuxTargetInfo(), BuiltinID))
2138       return ExprError();
2139     break;
2140   case Builtin::BI__builtin_cpu_init:
2141     if (!Context.getTargetInfo().supportsCpuInit()) {
2142       Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported)
2143           << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc());
2144       return ExprError();
2145     }
2146     break;
2147   case Builtin::BI__builtin___CFStringMakeConstantString:
2148     // CFStringMakeConstantString is currently not implemented for GOFF (i.e.,
2149     // on z/OS) and for XCOFF (i.e., on AIX). Emit unsupported
2150     if (CheckBuiltinTargetNotInUnsupported(
2151             *this, BuiltinID, TheCall,
2152             {llvm::Triple::GOFF, llvm::Triple::XCOFF}))
2153       return ExprError();
2154     assert(TheCall->getNumArgs() == 1 &&
2155            "Wrong # arguments to builtin CFStringMakeConstantString");
2156     if (ObjC().CheckObjCString(TheCall->getArg(0)))
2157       return ExprError();
2158     break;
2159   case Builtin::BI__builtin_ms_va_start:
2160   case Builtin::BI__builtin_stdarg_start:
2161   case Builtin::BI__builtin_va_start:
2162     if (BuiltinVAStart(BuiltinID, TheCall))
2163       return ExprError();
2164     break;
2165   case Builtin::BI__va_start: {
2166     switch (Context.getTargetInfo().getTriple().getArch()) {
2167     case llvm::Triple::aarch64:
2168     case llvm::Triple::arm:
2169     case llvm::Triple::thumb:
2170       if (BuiltinVAStartARMMicrosoft(TheCall))
2171         return ExprError();
2172       break;
2173     default:
2174       if (BuiltinVAStart(BuiltinID, TheCall))
2175         return ExprError();
2176       break;
2177     }
2178     break;
2179   }
2180 
2181   // The acquire, release, and no fence variants are ARM and AArch64 only.
2182   case Builtin::BI_interlockedbittestandset_acq:
2183   case Builtin::BI_interlockedbittestandset_rel:
2184   case Builtin::BI_interlockedbittestandset_nf:
2185   case Builtin::BI_interlockedbittestandreset_acq:
2186   case Builtin::BI_interlockedbittestandreset_rel:
2187   case Builtin::BI_interlockedbittestandreset_nf:
2188     if (CheckBuiltinTargetInSupported(
2189             *this, TheCall,
2190             {llvm::Triple::arm, llvm::Triple::thumb, llvm::Triple::aarch64}))
2191       return ExprError();
2192     break;
2193 
2194   // The 64-bit bittest variants are x64, ARM, and AArch64 only.
2195   case Builtin::BI_bittest64:
2196   case Builtin::BI_bittestandcomplement64:
2197   case Builtin::BI_bittestandreset64:
2198   case Builtin::BI_bittestandset64:
2199   case Builtin::BI_interlockedbittestandreset64:
2200   case Builtin::BI_interlockedbittestandset64:
2201     if (CheckBuiltinTargetInSupported(
2202             *this, TheCall,
2203             {llvm::Triple::x86_64, llvm::Triple::arm, llvm::Triple::thumb,
2204              llvm::Triple::aarch64, llvm::Triple::amdgcn}))
2205       return ExprError();
2206     break;
2207 
2208   case Builtin::BI__builtin_set_flt_rounds:
2209     if (CheckBuiltinTargetInSupported(
2210             *this, TheCall,
2211             {llvm::Triple::x86, llvm::Triple::x86_64, llvm::Triple::arm,
2212              llvm::Triple::thumb, llvm::Triple::aarch64, llvm::Triple::amdgcn,
2213              llvm::Triple::ppc, llvm::Triple::ppc64, llvm::Triple::ppcle,
2214              llvm::Triple::ppc64le}))
2215       return ExprError();
2216     break;
2217 
2218   case Builtin::BI__builtin_isgreater:
2219   case Builtin::BI__builtin_isgreaterequal:
2220   case Builtin::BI__builtin_isless:
2221   case Builtin::BI__builtin_islessequal:
2222   case Builtin::BI__builtin_islessgreater:
2223   case Builtin::BI__builtin_isunordered:
2224     if (BuiltinUnorderedCompare(TheCall, BuiltinID))
2225       return ExprError();
2226     break;
2227   case Builtin::BI__builtin_fpclassify:
2228     if (BuiltinFPClassification(TheCall, 6, BuiltinID))
2229       return ExprError();
2230     break;
2231   case Builtin::BI__builtin_isfpclass:
2232     if (BuiltinFPClassification(TheCall, 2, BuiltinID))
2233       return ExprError();
2234     break;
2235   case Builtin::BI__builtin_isfinite:
2236   case Builtin::BI__builtin_isinf:
2237   case Builtin::BI__builtin_isinf_sign:
2238   case Builtin::BI__builtin_isnan:
2239   case Builtin::BI__builtin_issignaling:
2240   case Builtin::BI__builtin_isnormal:
2241   case Builtin::BI__builtin_issubnormal:
2242   case Builtin::BI__builtin_iszero:
2243   case Builtin::BI__builtin_signbit:
2244   case Builtin::BI__builtin_signbitf:
2245   case Builtin::BI__builtin_signbitl:
2246     if (BuiltinFPClassification(TheCall, 1, BuiltinID))
2247       return ExprError();
2248     break;
2249   case Builtin::BI__builtin_shufflevector:
2250     return BuiltinShuffleVector(TheCall);
2251     // TheCall will be freed by the smart pointer here, but that's fine, since
2252     // BuiltinShuffleVector guts it, but then doesn't release it.
2253   case Builtin::BI__builtin_prefetch:
2254     if (BuiltinPrefetch(TheCall))
2255       return ExprError();
2256     break;
2257   case Builtin::BI__builtin_alloca_with_align:
2258   case Builtin::BI__builtin_alloca_with_align_uninitialized:
2259     if (BuiltinAllocaWithAlign(TheCall))
2260       return ExprError();
2261     [[fallthrough]];
2262   case Builtin::BI__builtin_alloca:
2263   case Builtin::BI__builtin_alloca_uninitialized:
2264     Diag(TheCall->getBeginLoc(), diag::warn_alloca)
2265         << TheCall->getDirectCallee();
2266     if (getLangOpts().OpenCL) {
2267       builtinAllocaAddrSpace(*this, TheCall);
2268     }
2269     break;
2270   case Builtin::BI__arithmetic_fence:
2271     if (BuiltinArithmeticFence(TheCall))
2272       return ExprError();
2273     break;
2274   case Builtin::BI__assume:
2275   case Builtin::BI__builtin_assume:
2276     if (BuiltinAssume(TheCall))
2277       return ExprError();
2278     break;
2279   case Builtin::BI__builtin_assume_aligned:
2280     if (BuiltinAssumeAligned(TheCall))
2281       return ExprError();
2282     break;
2283   case Builtin::BI__builtin_dynamic_object_size:
2284   case Builtin::BI__builtin_object_size:
2285     if (BuiltinConstantArgRange(TheCall, 1, 0, 3))
2286       return ExprError();
2287     break;
2288   case Builtin::BI__builtin_longjmp:
2289     if (BuiltinLongjmp(TheCall))
2290       return ExprError();
2291     break;
2292   case Builtin::BI__builtin_setjmp:
2293     if (BuiltinSetjmp(TheCall))
2294       return ExprError();
2295     break;
2296   case Builtin::BI__builtin_classify_type:
2297     if (checkArgCount(TheCall, 1))
2298       return true;
2299     TheCall->setType(Context.IntTy);
2300     break;
2301   case Builtin::BI__builtin_complex:
2302     if (BuiltinComplex(TheCall))
2303       return ExprError();
2304     break;
2305   case Builtin::BI__builtin_constant_p: {
2306     if (checkArgCount(TheCall, 1))
2307       return true;
2308     ExprResult Arg = DefaultFunctionArrayLvalueConversion(TheCall->getArg(0));
2309     if (Arg.isInvalid()) return true;
2310     TheCall->setArg(0, Arg.get());
2311     TheCall->setType(Context.IntTy);
2312     break;
2313   }
2314   case Builtin::BI__builtin_launder:
2315     return BuiltinLaunder(*this, TheCall);
2316   case Builtin::BI__builtin_is_within_lifetime:
2317     return BuiltinIsWithinLifetime(*this, TheCall);
2318   case Builtin::BI__sync_fetch_and_add:
2319   case Builtin::BI__sync_fetch_and_add_1:
2320   case Builtin::BI__sync_fetch_and_add_2:
2321   case Builtin::BI__sync_fetch_and_add_4:
2322   case Builtin::BI__sync_fetch_and_add_8:
2323   case Builtin::BI__sync_fetch_and_add_16:
2324   case Builtin::BI__sync_fetch_and_sub:
2325   case Builtin::BI__sync_fetch_and_sub_1:
2326   case Builtin::BI__sync_fetch_and_sub_2:
2327   case Builtin::BI__sync_fetch_and_sub_4:
2328   case Builtin::BI__sync_fetch_and_sub_8:
2329   case Builtin::BI__sync_fetch_and_sub_16:
2330   case Builtin::BI__sync_fetch_and_or:
2331   case Builtin::BI__sync_fetch_and_or_1:
2332   case Builtin::BI__sync_fetch_and_or_2:
2333   case Builtin::BI__sync_fetch_and_or_4:
2334   case Builtin::BI__sync_fetch_and_or_8:
2335   case Builtin::BI__sync_fetch_and_or_16:
2336   case Builtin::BI__sync_fetch_and_and:
2337   case Builtin::BI__sync_fetch_and_and_1:
2338   case Builtin::BI__sync_fetch_and_and_2:
2339   case Builtin::BI__sync_fetch_and_and_4:
2340   case Builtin::BI__sync_fetch_and_and_8:
2341   case Builtin::BI__sync_fetch_and_and_16:
2342   case Builtin::BI__sync_fetch_and_xor:
2343   case Builtin::BI__sync_fetch_and_xor_1:
2344   case Builtin::BI__sync_fetch_and_xor_2:
2345   case Builtin::BI__sync_fetch_and_xor_4:
2346   case Builtin::BI__sync_fetch_and_xor_8:
2347   case Builtin::BI__sync_fetch_and_xor_16:
2348   case Builtin::BI__sync_fetch_and_nand:
2349   case Builtin::BI__sync_fetch_and_nand_1:
2350   case Builtin::BI__sync_fetch_and_nand_2:
2351   case Builtin::BI__sync_fetch_and_nand_4:
2352   case Builtin::BI__sync_fetch_and_nand_8:
2353   case Builtin::BI__sync_fetch_and_nand_16:
2354   case Builtin::BI__sync_add_and_fetch:
2355   case Builtin::BI__sync_add_and_fetch_1:
2356   case Builtin::BI__sync_add_and_fetch_2:
2357   case Builtin::BI__sync_add_and_fetch_4:
2358   case Builtin::BI__sync_add_and_fetch_8:
2359   case Builtin::BI__sync_add_and_fetch_16:
2360   case Builtin::BI__sync_sub_and_fetch:
2361   case Builtin::BI__sync_sub_and_fetch_1:
2362   case Builtin::BI__sync_sub_and_fetch_2:
2363   case Builtin::BI__sync_sub_and_fetch_4:
2364   case Builtin::BI__sync_sub_and_fetch_8:
2365   case Builtin::BI__sync_sub_and_fetch_16:
2366   case Builtin::BI__sync_and_and_fetch:
2367   case Builtin::BI__sync_and_and_fetch_1:
2368   case Builtin::BI__sync_and_and_fetch_2:
2369   case Builtin::BI__sync_and_and_fetch_4:
2370   case Builtin::BI__sync_and_and_fetch_8:
2371   case Builtin::BI__sync_and_and_fetch_16:
2372   case Builtin::BI__sync_or_and_fetch:
2373   case Builtin::BI__sync_or_and_fetch_1:
2374   case Builtin::BI__sync_or_and_fetch_2:
2375   case Builtin::BI__sync_or_and_fetch_4:
2376   case Builtin::BI__sync_or_and_fetch_8:
2377   case Builtin::BI__sync_or_and_fetch_16:
2378   case Builtin::BI__sync_xor_and_fetch:
2379   case Builtin::BI__sync_xor_and_fetch_1:
2380   case Builtin::BI__sync_xor_and_fetch_2:
2381   case Builtin::BI__sync_xor_and_fetch_4:
2382   case Builtin::BI__sync_xor_and_fetch_8:
2383   case Builtin::BI__sync_xor_and_fetch_16:
2384   case Builtin::BI__sync_nand_and_fetch:
2385   case Builtin::BI__sync_nand_and_fetch_1:
2386   case Builtin::BI__sync_nand_and_fetch_2:
2387   case Builtin::BI__sync_nand_and_fetch_4:
2388   case Builtin::BI__sync_nand_and_fetch_8:
2389   case Builtin::BI__sync_nand_and_fetch_16:
2390   case Builtin::BI__sync_val_compare_and_swap:
2391   case Builtin::BI__sync_val_compare_and_swap_1:
2392   case Builtin::BI__sync_val_compare_and_swap_2:
2393   case Builtin::BI__sync_val_compare_and_swap_4:
2394   case Builtin::BI__sync_val_compare_and_swap_8:
2395   case Builtin::BI__sync_val_compare_and_swap_16:
2396   case Builtin::BI__sync_bool_compare_and_swap:
2397   case Builtin::BI__sync_bool_compare_and_swap_1:
2398   case Builtin::BI__sync_bool_compare_and_swap_2:
2399   case Builtin::BI__sync_bool_compare_and_swap_4:
2400   case Builtin::BI__sync_bool_compare_and_swap_8:
2401   case Builtin::BI__sync_bool_compare_and_swap_16:
2402   case Builtin::BI__sync_lock_test_and_set:
2403   case Builtin::BI__sync_lock_test_and_set_1:
2404   case Builtin::BI__sync_lock_test_and_set_2:
2405   case Builtin::BI__sync_lock_test_and_set_4:
2406   case Builtin::BI__sync_lock_test_and_set_8:
2407   case Builtin::BI__sync_lock_test_and_set_16:
2408   case Builtin::BI__sync_lock_release:
2409   case Builtin::BI__sync_lock_release_1:
2410   case Builtin::BI__sync_lock_release_2:
2411   case Builtin::BI__sync_lock_release_4:
2412   case Builtin::BI__sync_lock_release_8:
2413   case Builtin::BI__sync_lock_release_16:
2414   case Builtin::BI__sync_swap:
2415   case Builtin::BI__sync_swap_1:
2416   case Builtin::BI__sync_swap_2:
2417   case Builtin::BI__sync_swap_4:
2418   case Builtin::BI__sync_swap_8:
2419   case Builtin::BI__sync_swap_16:
2420     return BuiltinAtomicOverloaded(TheCallResult);
2421   case Builtin::BI__sync_synchronize:
2422     Diag(TheCall->getBeginLoc(), diag::warn_atomic_implicit_seq_cst)
2423         << TheCall->getCallee()->getSourceRange();
2424     break;
2425   case Builtin::BI__builtin_nontemporal_load:
2426   case Builtin::BI__builtin_nontemporal_store:
2427     return BuiltinNontemporalOverloaded(TheCallResult);
2428   case Builtin::BI__builtin_memcpy_inline: {
2429     clang::Expr *SizeOp = TheCall->getArg(2);
2430     // We warn about copying to or from `nullptr` pointers when `size` is
2431     // greater than 0. When `size` is value dependent we cannot evaluate its
2432     // value so we bail out.
2433     if (SizeOp->isValueDependent())
2434       break;
2435     if (!SizeOp->EvaluateKnownConstInt(Context).isZero()) {
2436       CheckNonNullArgument(*this, TheCall->getArg(0), TheCall->getExprLoc());
2437       CheckNonNullArgument(*this, TheCall->getArg(1), TheCall->getExprLoc());
2438     }
2439     break;
2440   }
2441   case Builtin::BI__builtin_memset_inline: {
2442     clang::Expr *SizeOp = TheCall->getArg(2);
2443     // We warn about filling to `nullptr` pointers when `size` is greater than
2444     // 0. When `size` is value dependent we cannot evaluate its value so we bail
2445     // out.
2446     if (SizeOp->isValueDependent())
2447       break;
2448     if (!SizeOp->EvaluateKnownConstInt(Context).isZero())
2449       CheckNonNullArgument(*this, TheCall->getArg(0), TheCall->getExprLoc());
2450     break;
2451   }
2452 #define BUILTIN(ID, TYPE, ATTRS)
2453 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS)                                        \
2454   case Builtin::BI##ID:                                                        \
2455     return AtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID);
2456 #include "clang/Basic/Builtins.inc"
2457   case Builtin::BI__annotation:
2458     if (BuiltinMSVCAnnotation(*this, TheCall))
2459       return ExprError();
2460     break;
2461   case Builtin::BI__builtin_annotation:
2462     if (BuiltinAnnotation(*this, TheCall))
2463       return ExprError();
2464     break;
2465   case Builtin::BI__builtin_addressof:
2466     if (BuiltinAddressof(*this, TheCall))
2467       return ExprError();
2468     break;
2469   case Builtin::BI__builtin_function_start:
2470     if (BuiltinFunctionStart(*this, TheCall))
2471       return ExprError();
2472     break;
2473   case Builtin::BI__builtin_is_aligned:
2474   case Builtin::BI__builtin_align_up:
2475   case Builtin::BI__builtin_align_down:
2476     if (BuiltinAlignment(*this, TheCall, BuiltinID))
2477       return ExprError();
2478     break;
2479   case Builtin::BI__builtin_add_overflow:
2480   case Builtin::BI__builtin_sub_overflow:
2481   case Builtin::BI__builtin_mul_overflow:
2482     if (BuiltinOverflow(*this, TheCall, BuiltinID))
2483       return ExprError();
2484     break;
2485   case Builtin::BI__builtin_operator_new:
2486   case Builtin::BI__builtin_operator_delete: {
2487     bool IsDelete = BuiltinID == Builtin::BI__builtin_operator_delete;
2488     ExprResult Res =
2489         BuiltinOperatorNewDeleteOverloaded(TheCallResult, IsDelete);
2490     if (Res.isInvalid())
2491       CorrectDelayedTyposInExpr(TheCallResult.get());
2492     return Res;
2493   }
2494   case Builtin::BI__builtin_dump_struct:
2495     return BuiltinDumpStruct(*this, TheCall);
2496   case Builtin::BI__builtin_expect_with_probability: {
2497     // We first want to ensure we are called with 3 arguments
2498     if (checkArgCount(TheCall, 3))
2499       return ExprError();
2500     // then check probability is constant float in range [0.0, 1.0]
2501     const Expr *ProbArg = TheCall->getArg(2);
2502     SmallVector<PartialDiagnosticAt, 8> Notes;
2503     Expr::EvalResult Eval;
2504     Eval.Diag = &Notes;
2505     if ((!ProbArg->EvaluateAsConstantExpr(Eval, Context)) ||
2506         !Eval.Val.isFloat()) {
2507       Diag(ProbArg->getBeginLoc(), diag::err_probability_not_constant_float)
2508           << ProbArg->getSourceRange();
2509       for (const PartialDiagnosticAt &PDiag : Notes)
2510         Diag(PDiag.first, PDiag.second);
2511       return ExprError();
2512     }
2513     llvm::APFloat Probability = Eval.Val.getFloat();
2514     bool LoseInfo = false;
2515     Probability.convert(llvm::APFloat::IEEEdouble(),
2516                         llvm::RoundingMode::Dynamic, &LoseInfo);
2517     if (!(Probability >= llvm::APFloat(0.0) &&
2518           Probability <= llvm::APFloat(1.0))) {
2519       Diag(ProbArg->getBeginLoc(), diag::err_probability_out_of_range)
2520           << ProbArg->getSourceRange();
2521       return ExprError();
2522     }
2523     break;
2524   }
2525   case Builtin::BI__builtin_preserve_access_index:
2526     if (BuiltinPreserveAI(*this, TheCall))
2527       return ExprError();
2528     break;
2529   case Builtin::BI__builtin_call_with_static_chain:
2530     if (BuiltinCallWithStaticChain(*this, TheCall))
2531       return ExprError();
2532     break;
2533   case Builtin::BI__exception_code:
2534   case Builtin::BI_exception_code:
2535     if (BuiltinSEHScopeCheck(*this, TheCall, Scope::SEHExceptScope,
2536                              diag::err_seh___except_block))
2537       return ExprError();
2538     break;
2539   case Builtin::BI__exception_info:
2540   case Builtin::BI_exception_info:
2541     if (BuiltinSEHScopeCheck(*this, TheCall, Scope::SEHFilterScope,
2542                              diag::err_seh___except_filter))
2543       return ExprError();
2544     break;
2545   case Builtin::BI__GetExceptionInfo:
2546     if (checkArgCount(TheCall, 1))
2547       return ExprError();
2548 
2549     if (CheckCXXThrowOperand(
2550             TheCall->getBeginLoc(),
2551             Context.getExceptionObjectType(FDecl->getParamDecl(0)->getType()),
2552             TheCall))
2553       return ExprError();
2554 
2555     TheCall->setType(Context.VoidPtrTy);
2556     break;
2557   case Builtin::BIaddressof:
2558   case Builtin::BI__addressof:
2559   case Builtin::BIforward:
2560   case Builtin::BIforward_like:
2561   case Builtin::BImove:
2562   case Builtin::BImove_if_noexcept:
2563   case Builtin::BIas_const: {
2564     // These are all expected to be of the form
2565     //   T &/&&/* f(U &/&&)
2566     // where T and U only differ in qualification.
2567     if (checkArgCount(TheCall, 1))
2568       return ExprError();
2569     QualType Param = FDecl->getParamDecl(0)->getType();
2570     QualType Result = FDecl->getReturnType();
2571     bool ReturnsPointer = BuiltinID == Builtin::BIaddressof ||
2572                           BuiltinID == Builtin::BI__addressof;
2573     if (!(Param->isReferenceType() &&
2574           (ReturnsPointer ? Result->isAnyPointerType()
2575                           : Result->isReferenceType()) &&
2576           Context.hasSameUnqualifiedType(Param->getPointeeType(),
2577                                          Result->getPointeeType()))) {
2578       Diag(TheCall->getBeginLoc(), diag::err_builtin_move_forward_unsupported)
2579           << FDecl;
2580       return ExprError();
2581     }
2582     break;
2583   }
2584   case Builtin::BI__builtin_ptrauth_strip:
2585     return PointerAuthStrip(*this, TheCall);
2586   case Builtin::BI__builtin_ptrauth_blend_discriminator:
2587     return PointerAuthBlendDiscriminator(*this, TheCall);
2588   case Builtin::BI__builtin_ptrauth_sign_constant:
2589     return PointerAuthSignOrAuth(*this, TheCall, PAO_Sign,
2590                                  /*RequireConstant=*/true);
2591   case Builtin::BI__builtin_ptrauth_sign_unauthenticated:
2592     return PointerAuthSignOrAuth(*this, TheCall, PAO_Sign,
2593                                  /*RequireConstant=*/false);
2594   case Builtin::BI__builtin_ptrauth_auth:
2595     return PointerAuthSignOrAuth(*this, TheCall, PAO_Auth,
2596                                  /*RequireConstant=*/false);
2597   case Builtin::BI__builtin_ptrauth_sign_generic_data:
2598     return PointerAuthSignGenericData(*this, TheCall);
2599   case Builtin::BI__builtin_ptrauth_auth_and_resign:
2600     return PointerAuthAuthAndResign(*this, TheCall);
2601   case Builtin::BI__builtin_ptrauth_string_discriminator:
2602     return PointerAuthStringDiscriminator(*this, TheCall);
2603   // OpenCL v2.0, s6.13.16 - Pipe functions
2604   case Builtin::BIread_pipe:
2605   case Builtin::BIwrite_pipe:
2606     // Since those two functions are declared with var args, we need a semantic
2607     // check for the argument.
2608     if (OpenCL().checkBuiltinRWPipe(TheCall))
2609       return ExprError();
2610     break;
2611   case Builtin::BIreserve_read_pipe:
2612   case Builtin::BIreserve_write_pipe:
2613   case Builtin::BIwork_group_reserve_read_pipe:
2614   case Builtin::BIwork_group_reserve_write_pipe:
2615     if (OpenCL().checkBuiltinReserveRWPipe(TheCall))
2616       return ExprError();
2617     break;
2618   case Builtin::BIsub_group_reserve_read_pipe:
2619   case Builtin::BIsub_group_reserve_write_pipe:
2620     if (OpenCL().checkSubgroupExt(TheCall) ||
2621         OpenCL().checkBuiltinReserveRWPipe(TheCall))
2622       return ExprError();
2623     break;
2624   case Builtin::BIcommit_read_pipe:
2625   case Builtin::BIcommit_write_pipe:
2626   case Builtin::BIwork_group_commit_read_pipe:
2627   case Builtin::BIwork_group_commit_write_pipe:
2628     if (OpenCL().checkBuiltinCommitRWPipe(TheCall))
2629       return ExprError();
2630     break;
2631   case Builtin::BIsub_group_commit_read_pipe:
2632   case Builtin::BIsub_group_commit_write_pipe:
2633     if (OpenCL().checkSubgroupExt(TheCall) ||
2634         OpenCL().checkBuiltinCommitRWPipe(TheCall))
2635       return ExprError();
2636     break;
2637   case Builtin::BIget_pipe_num_packets:
2638   case Builtin::BIget_pipe_max_packets:
2639     if (OpenCL().checkBuiltinPipePackets(TheCall))
2640       return ExprError();
2641     break;
2642   case Builtin::BIto_global:
2643   case Builtin::BIto_local:
2644   case Builtin::BIto_private:
2645     if (OpenCL().checkBuiltinToAddr(BuiltinID, TheCall))
2646       return ExprError();
2647     break;
2648   // OpenCL v2.0, s6.13.17 - Enqueue kernel functions.
2649   case Builtin::BIenqueue_kernel:
2650     if (OpenCL().checkBuiltinEnqueueKernel(TheCall))
2651       return ExprError();
2652     break;
2653   case Builtin::BIget_kernel_work_group_size:
2654   case Builtin::BIget_kernel_preferred_work_group_size_multiple:
2655     if (OpenCL().checkBuiltinKernelWorkGroupSize(TheCall))
2656       return ExprError();
2657     break;
2658   case Builtin::BIget_kernel_max_sub_group_size_for_ndrange:
2659   case Builtin::BIget_kernel_sub_group_count_for_ndrange:
2660     if (OpenCL().checkBuiltinNDRangeAndBlock(TheCall))
2661       return ExprError();
2662     break;
2663   case Builtin::BI__builtin_os_log_format:
2664     Cleanup.setExprNeedsCleanups(true);
2665     [[fallthrough]];
2666   case Builtin::BI__builtin_os_log_format_buffer_size:
2667     if (BuiltinOSLogFormat(TheCall))
2668       return ExprError();
2669     break;
2670   case Builtin::BI__builtin_frame_address:
2671   case Builtin::BI__builtin_return_address: {
2672     if (BuiltinConstantArgRange(TheCall, 0, 0, 0xFFFF))
2673       return ExprError();
2674 
2675     // -Wframe-address warning if non-zero passed to builtin
2676     // return/frame address.
2677     Expr::EvalResult Result;
2678     if (!TheCall->getArg(0)->isValueDependent() &&
2679         TheCall->getArg(0)->EvaluateAsInt(Result, getASTContext()) &&
2680         Result.Val.getInt() != 0)
2681       Diag(TheCall->getBeginLoc(), diag::warn_frame_address)
2682           << ((BuiltinID == Builtin::BI__builtin_return_address)
2683                   ? "__builtin_return_address"
2684                   : "__builtin_frame_address")
2685           << TheCall->getSourceRange();
2686     break;
2687   }
2688 
2689   case Builtin::BI__builtin_nondeterministic_value: {
2690     if (BuiltinNonDeterministicValue(TheCall))
2691       return ExprError();
2692     break;
2693   }
2694 
2695   // __builtin_elementwise_abs restricts the element type to signed integers or
2696   // floating point types only.
2697   case Builtin::BI__builtin_elementwise_abs: {
2698     if (PrepareBuiltinElementwiseMathOneArgCall(TheCall))
2699       return ExprError();
2700 
2701     QualType ArgTy = TheCall->getArg(0)->getType();
2702     QualType EltTy = ArgTy;
2703 
2704     if (auto *VecTy = EltTy->getAs<VectorType>())
2705       EltTy = VecTy->getElementType();
2706     if (EltTy->isUnsignedIntegerType()) {
2707       Diag(TheCall->getArg(0)->getBeginLoc(),
2708            diag::err_builtin_invalid_arg_type)
2709           << 1 << /* signed integer or float ty*/ 3 << ArgTy;
2710       return ExprError();
2711     }
2712     break;
2713   }
2714 
2715   // These builtins restrict the element type to floating point
2716   // types only.
2717   case Builtin::BI__builtin_elementwise_acos:
2718   case Builtin::BI__builtin_elementwise_asin:
2719   case Builtin::BI__builtin_elementwise_atan:
2720   case Builtin::BI__builtin_elementwise_ceil:
2721   case Builtin::BI__builtin_elementwise_cos:
2722   case Builtin::BI__builtin_elementwise_cosh:
2723   case Builtin::BI__builtin_elementwise_exp:
2724   case Builtin::BI__builtin_elementwise_exp2:
2725   case Builtin::BI__builtin_elementwise_floor:
2726   case Builtin::BI__builtin_elementwise_log:
2727   case Builtin::BI__builtin_elementwise_log2:
2728   case Builtin::BI__builtin_elementwise_log10:
2729   case Builtin::BI__builtin_elementwise_roundeven:
2730   case Builtin::BI__builtin_elementwise_round:
2731   case Builtin::BI__builtin_elementwise_rint:
2732   case Builtin::BI__builtin_elementwise_nearbyint:
2733   case Builtin::BI__builtin_elementwise_sin:
2734   case Builtin::BI__builtin_elementwise_sinh:
2735   case Builtin::BI__builtin_elementwise_sqrt:
2736   case Builtin::BI__builtin_elementwise_tan:
2737   case Builtin::BI__builtin_elementwise_tanh:
2738   case Builtin::BI__builtin_elementwise_trunc:
2739   case Builtin::BI__builtin_elementwise_canonicalize: {
2740     if (PrepareBuiltinElementwiseMathOneArgCall(TheCall))
2741       return ExprError();
2742 
2743     QualType ArgTy = TheCall->getArg(0)->getType();
2744     if (checkFPMathBuiltinElementType(*this, TheCall->getArg(0)->getBeginLoc(),
2745                                       ArgTy, 1))
2746       return ExprError();
2747     break;
2748   }
2749   case Builtin::BI__builtin_elementwise_fma: {
2750     if (BuiltinElementwiseTernaryMath(TheCall))
2751       return ExprError();
2752     break;
2753   }
2754 
2755   // These builtins restrict the element type to floating point
2756   // types only, and take in two arguments.
2757   case Builtin::BI__builtin_elementwise_minimum:
2758   case Builtin::BI__builtin_elementwise_maximum:
2759   case Builtin::BI__builtin_elementwise_atan2:
2760   case Builtin::BI__builtin_elementwise_fmod:
2761   case Builtin::BI__builtin_elementwise_pow: {
2762     if (BuiltinElementwiseMath(TheCall, /*FPOnly=*/true))
2763       return ExprError();
2764     break;
2765   }
2766 
2767   // These builtins restrict the element type to integer
2768   // types only.
2769   case Builtin::BI__builtin_elementwise_add_sat:
2770   case Builtin::BI__builtin_elementwise_sub_sat: {
2771     if (BuiltinElementwiseMath(TheCall))
2772       return ExprError();
2773 
2774     const Expr *Arg = TheCall->getArg(0);
2775     QualType ArgTy = Arg->getType();
2776     QualType EltTy = ArgTy;
2777 
2778     if (auto *VecTy = EltTy->getAs<VectorType>())
2779       EltTy = VecTy->getElementType();
2780 
2781     if (!EltTy->isIntegerType()) {
2782       Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
2783           << 1 << /* integer ty */ 6 << ArgTy;
2784       return ExprError();
2785     }
2786     break;
2787   }
2788 
2789   case Builtin::BI__builtin_elementwise_min:
2790   case Builtin::BI__builtin_elementwise_max:
2791     if (BuiltinElementwiseMath(TheCall))
2792       return ExprError();
2793     break;
2794   case Builtin::BI__builtin_elementwise_popcount:
2795   case Builtin::BI__builtin_elementwise_bitreverse: {
2796     if (PrepareBuiltinElementwiseMathOneArgCall(TheCall))
2797       return ExprError();
2798 
2799     const Expr *Arg = TheCall->getArg(0);
2800     QualType ArgTy = Arg->getType();
2801     QualType EltTy = ArgTy;
2802 
2803     if (auto *VecTy = EltTy->getAs<VectorType>())
2804       EltTy = VecTy->getElementType();
2805 
2806     if (!EltTy->isIntegerType()) {
2807       Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
2808           << 1 << /* integer ty */ 6 << ArgTy;
2809       return ExprError();
2810     }
2811     break;
2812   }
2813 
2814   case Builtin::BI__builtin_elementwise_copysign: {
2815     if (checkArgCount(TheCall, 2))
2816       return ExprError();
2817 
2818     ExprResult Magnitude = UsualUnaryConversions(TheCall->getArg(0));
2819     ExprResult Sign = UsualUnaryConversions(TheCall->getArg(1));
2820     if (Magnitude.isInvalid() || Sign.isInvalid())
2821       return ExprError();
2822 
2823     QualType MagnitudeTy = Magnitude.get()->getType();
2824     QualType SignTy = Sign.get()->getType();
2825     if (checkFPMathBuiltinElementType(*this, TheCall->getArg(0)->getBeginLoc(),
2826                                       MagnitudeTy, 1) ||
2827         checkFPMathBuiltinElementType(*this, TheCall->getArg(1)->getBeginLoc(),
2828                                       SignTy, 2)) {
2829       return ExprError();
2830     }
2831 
2832     if (MagnitudeTy.getCanonicalType() != SignTy.getCanonicalType()) {
2833       return Diag(Sign.get()->getBeginLoc(),
2834                   diag::err_typecheck_call_different_arg_types)
2835              << MagnitudeTy << SignTy;
2836     }
2837 
2838     TheCall->setArg(0, Magnitude.get());
2839     TheCall->setArg(1, Sign.get());
2840     TheCall->setType(Magnitude.get()->getType());
2841     break;
2842   }
2843   case Builtin::BI__builtin_reduce_max:
2844   case Builtin::BI__builtin_reduce_min: {
2845     if (PrepareBuiltinReduceMathOneArgCall(TheCall))
2846       return ExprError();
2847 
2848     const Expr *Arg = TheCall->getArg(0);
2849     const auto *TyA = Arg->getType()->getAs<VectorType>();
2850 
2851     QualType ElTy;
2852     if (TyA)
2853       ElTy = TyA->getElementType();
2854     else if (Arg->getType()->isSizelessVectorType())
2855       ElTy = Arg->getType()->getSizelessVectorEltType(Context);
2856 
2857     if (ElTy.isNull()) {
2858       Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
2859           << 1 << /* vector ty*/ 4 << Arg->getType();
2860       return ExprError();
2861     }
2862 
2863     TheCall->setType(ElTy);
2864     break;
2865   }
2866   case Builtin::BI__builtin_reduce_maximum:
2867   case Builtin::BI__builtin_reduce_minimum: {
2868     if (PrepareBuiltinReduceMathOneArgCall(TheCall))
2869       return ExprError();
2870 
2871     const Expr *Arg = TheCall->getArg(0);
2872     const auto *TyA = Arg->getType()->getAs<VectorType>();
2873 
2874     QualType ElTy;
2875     if (TyA)
2876       ElTy = TyA->getElementType();
2877     else if (Arg->getType()->isSizelessVectorType())
2878       ElTy = Arg->getType()->getSizelessVectorEltType(Context);
2879 
2880     if (ElTy.isNull() || !ElTy->isFloatingType()) {
2881       Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
2882           << 1 << /* vector of floating points */ 9 << Arg->getType();
2883       return ExprError();
2884     }
2885 
2886     TheCall->setType(ElTy);
2887     break;
2888   }
2889 
2890   // These builtins support vectors of integers only.
2891   // TODO: ADD/MUL should support floating-point types.
2892   case Builtin::BI__builtin_reduce_add:
2893   case Builtin::BI__builtin_reduce_mul:
2894   case Builtin::BI__builtin_reduce_xor:
2895   case Builtin::BI__builtin_reduce_or:
2896   case Builtin::BI__builtin_reduce_and: {
2897     if (PrepareBuiltinReduceMathOneArgCall(TheCall))
2898       return ExprError();
2899 
2900     const Expr *Arg = TheCall->getArg(0);
2901     const auto *TyA = Arg->getType()->getAs<VectorType>();
2902 
2903     QualType ElTy;
2904     if (TyA)
2905       ElTy = TyA->getElementType();
2906     else if (Arg->getType()->isSizelessVectorType())
2907       ElTy = Arg->getType()->getSizelessVectorEltType(Context);
2908 
2909     if (ElTy.isNull() || !ElTy->isIntegerType()) {
2910       Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
2911           << 1  << /* vector of integers */ 6 << Arg->getType();
2912       return ExprError();
2913     }
2914 
2915     TheCall->setType(ElTy);
2916     break;
2917   }
2918 
2919   case Builtin::BI__builtin_matrix_transpose:
2920     return BuiltinMatrixTranspose(TheCall, TheCallResult);
2921 
2922   case Builtin::BI__builtin_matrix_column_major_load:
2923     return BuiltinMatrixColumnMajorLoad(TheCall, TheCallResult);
2924 
2925   case Builtin::BI__builtin_matrix_column_major_store:
2926     return BuiltinMatrixColumnMajorStore(TheCall, TheCallResult);
2927 
2928   case Builtin::BI__builtin_verbose_trap:
2929     if (!checkBuiltinVerboseTrap(TheCall, *this))
2930       return ExprError();
2931     break;
2932 
2933   case Builtin::BI__builtin_get_device_side_mangled_name: {
2934     auto Check = [](CallExpr *TheCall) {
2935       if (TheCall->getNumArgs() != 1)
2936         return false;
2937       auto *DRE = dyn_cast<DeclRefExpr>(TheCall->getArg(0)->IgnoreImpCasts());
2938       if (!DRE)
2939         return false;
2940       auto *D = DRE->getDecl();
2941       if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D))
2942         return false;
2943       return D->hasAttr<CUDAGlobalAttr>() || D->hasAttr<CUDADeviceAttr>() ||
2944              D->hasAttr<CUDAConstantAttr>() || D->hasAttr<HIPManagedAttr>();
2945     };
2946     if (!Check(TheCall)) {
2947       Diag(TheCall->getBeginLoc(),
2948            diag::err_hip_invalid_args_builtin_mangled_name);
2949       return ExprError();
2950     }
2951     break;
2952   }
2953   case Builtin::BI__builtin_popcountg:
2954     if (BuiltinPopcountg(*this, TheCall))
2955       return ExprError();
2956     break;
2957   case Builtin::BI__builtin_clzg:
2958   case Builtin::BI__builtin_ctzg:
2959     if (BuiltinCountZeroBitsGeneric(*this, TheCall))
2960       return ExprError();
2961     break;
2962 
2963   case Builtin::BI__builtin_allow_runtime_check: {
2964     Expr *Arg = TheCall->getArg(0);
2965     // Check if the argument is a string literal.
2966     if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts())) {
2967       Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal)
2968           << Arg->getSourceRange();
2969       return ExprError();
2970     }
2971     break;
2972   }
2973   case Builtin::BI__builtin_counted_by_ref:
2974     if (BuiltinCountedByRef(TheCall))
2975       return ExprError();
2976     break;
2977   }
2978 
2979   if (getLangOpts().HLSL && HLSL().CheckBuiltinFunctionCall(BuiltinID, TheCall))
2980     return ExprError();
2981 
2982   // Since the target specific builtins for each arch overlap, only check those
2983   // of the arch we are compiling for.
2984   if (Context.BuiltinInfo.isTSBuiltin(BuiltinID)) {
2985     if (Context.BuiltinInfo.isAuxBuiltinID(BuiltinID)) {
2986       assert(Context.getAuxTargetInfo() &&
2987              "Aux Target Builtin, but not an aux target?");
2988 
2989       if (CheckTSBuiltinFunctionCall(
2990               *Context.getAuxTargetInfo(),
2991               Context.BuiltinInfo.getAuxBuiltinID(BuiltinID), TheCall))
2992         return ExprError();
2993     } else {
2994       if (CheckTSBuiltinFunctionCall(Context.getTargetInfo(), BuiltinID,
2995                                      TheCall))
2996         return ExprError();
2997     }
2998   }
2999 
3000   return TheCallResult;
3001 }
3002 
3003 bool Sema::ValueIsRunOfOnes(CallExpr *TheCall, unsigned ArgNum) {
3004   llvm::APSInt Result;
3005   // We can't check the value of a dependent argument.
3006   Expr *Arg = TheCall->getArg(ArgNum);
3007   if (Arg->isTypeDependent() || Arg->isValueDependent())
3008     return false;
3009 
3010   // Check constant-ness first.
3011   if (BuiltinConstantArg(TheCall, ArgNum, Result))
3012     return true;
3013 
3014   // Check contiguous run of 1s, 0xFF0000FF is also a run of 1s.
3015   if (Result.isShiftedMask() || (~Result).isShiftedMask())
3016     return false;
3017 
3018   return Diag(TheCall->getBeginLoc(),
3019               diag::err_argument_not_contiguous_bit_field)
3020          << ArgNum << Arg->getSourceRange();
3021 }
3022 
3023 bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember,
3024                                bool IsVariadic, FormatStringInfo *FSI) {
3025   if (Format->getFirstArg() == 0)
3026     FSI->ArgPassingKind = FAPK_VAList;
3027   else if (IsVariadic)
3028     FSI->ArgPassingKind = FAPK_Variadic;
3029   else
3030     FSI->ArgPassingKind = FAPK_Fixed;
3031   FSI->FormatIdx = Format->getFormatIdx() - 1;
3032   FSI->FirstDataArg =
3033       FSI->ArgPassingKind == FAPK_VAList ? 0 : Format->getFirstArg() - 1;
3034 
3035   // The way the format attribute works in GCC, the implicit this argument
3036   // of member functions is counted. However, it doesn't appear in our own
3037   // lists, so decrement format_idx in that case.
3038   if (IsCXXMember) {
3039     if(FSI->FormatIdx == 0)
3040       return false;
3041     --FSI->FormatIdx;
3042     if (FSI->FirstDataArg != 0)
3043       --FSI->FirstDataArg;
3044   }
3045   return true;
3046 }
3047 
3048 /// Checks if a the given expression evaluates to null.
3049 ///
3050 /// Returns true if the value evaluates to null.
3051 static bool CheckNonNullExpr(Sema &S, const Expr *Expr) {
3052   // Treat (smart) pointers constructed from nullptr as null, whether we can
3053   // const-evaluate them or not.
3054   // This must happen first: the smart pointer expr might have _Nonnull type!
3055   if (isa<CXXNullPtrLiteralExpr>(
3056           IgnoreExprNodes(Expr, IgnoreImplicitAsWrittenSingleStep,
3057                           IgnoreElidableImplicitConstructorSingleStep)))
3058     return true;
3059 
3060   // If the expression has non-null type, it doesn't evaluate to null.
3061   if (auto nullability = Expr->IgnoreImplicit()->getType()->getNullability()) {
3062     if (*nullability == NullabilityKind::NonNull)
3063       return false;
3064   }
3065 
3066   // As a special case, transparent unions initialized with zero are
3067   // considered null for the purposes of the nonnull attribute.
3068   if (const RecordType *UT = Expr->getType()->getAsUnionType();
3069       UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
3070     if (const auto *CLE = dyn_cast<CompoundLiteralExpr>(Expr))
3071       if (const auto *ILE = dyn_cast<InitListExpr>(CLE->getInitializer()))
3072         Expr = ILE->getInit(0);
3073   }
3074 
3075   bool Result;
3076   return (!Expr->isValueDependent() &&
3077           Expr->EvaluateAsBooleanCondition(Result, S.Context) &&
3078           !Result);
3079 }
3080 
3081 static void CheckNonNullArgument(Sema &S,
3082                                  const Expr *ArgExpr,
3083                                  SourceLocation CallSiteLoc) {
3084   if (CheckNonNullExpr(S, ArgExpr))
3085     S.DiagRuntimeBehavior(CallSiteLoc, ArgExpr,
3086                           S.PDiag(diag::warn_null_arg)
3087                               << ArgExpr->getSourceRange());
3088 }
3089 
3090 /// Determine whether the given type has a non-null nullability annotation.
3091 static bool isNonNullType(QualType type) {
3092   if (auto nullability = type->getNullability())
3093     return *nullability == NullabilityKind::NonNull;
3094 
3095   return false;
3096 }
3097 
3098 static void CheckNonNullArguments(Sema &S,
3099                                   const NamedDecl *FDecl,
3100                                   const FunctionProtoType *Proto,
3101                                   ArrayRef<const Expr *> Args,
3102                                   SourceLocation CallSiteLoc) {
3103   assert((FDecl || Proto) && "Need a function declaration or prototype");
3104 
3105   // Already checked by constant evaluator.
3106   if (S.isConstantEvaluatedContext())
3107     return;
3108   // Check the attributes attached to the method/function itself.
3109   llvm::SmallBitVector NonNullArgs;
3110   if (FDecl) {
3111     // Handle the nonnull attribute on the function/method declaration itself.
3112     for (const auto *NonNull : FDecl->specific_attrs<NonNullAttr>()) {
3113       if (!NonNull->args_size()) {
3114         // Easy case: all pointer arguments are nonnull.
3115         for (const auto *Arg : Args)
3116           if (S.isValidPointerAttrType(Arg->getType()))
3117             CheckNonNullArgument(S, Arg, CallSiteLoc);
3118         return;
3119       }
3120 
3121       for (const ParamIdx &Idx : NonNull->args()) {
3122         unsigned IdxAST = Idx.getASTIndex();
3123         if (IdxAST >= Args.size())
3124           continue;
3125         if (NonNullArgs.empty())
3126           NonNullArgs.resize(Args.size());
3127         NonNullArgs.set(IdxAST);
3128       }
3129     }
3130   }
3131 
3132   if (FDecl && (isa<FunctionDecl>(FDecl) || isa<ObjCMethodDecl>(FDecl))) {
3133     // Handle the nonnull attribute on the parameters of the
3134     // function/method.
3135     ArrayRef<ParmVarDecl*> parms;
3136     if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FDecl))
3137       parms = FD->parameters();
3138     else
3139       parms = cast<ObjCMethodDecl>(FDecl)->parameters();
3140 
3141     unsigned ParamIndex = 0;
3142     for (ArrayRef<ParmVarDecl*>::iterator I = parms.begin(), E = parms.end();
3143          I != E; ++I, ++ParamIndex) {
3144       const ParmVarDecl *PVD = *I;
3145       if (PVD->hasAttr<NonNullAttr>() || isNonNullType(PVD->getType())) {
3146         if (NonNullArgs.empty())
3147           NonNullArgs.resize(Args.size());
3148 
3149         NonNullArgs.set(ParamIndex);
3150       }
3151     }
3152   } else {
3153     // If we have a non-function, non-method declaration but no
3154     // function prototype, try to dig out the function prototype.
3155     if (!Proto) {
3156       if (const ValueDecl *VD = dyn_cast<ValueDecl>(FDecl)) {
3157         QualType type = VD->getType().getNonReferenceType();
3158         if (auto pointerType = type->getAs<PointerType>())
3159           type = pointerType->getPointeeType();
3160         else if (auto blockType = type->getAs<BlockPointerType>())
3161           type = blockType->getPointeeType();
3162         // FIXME: data member pointers?
3163 
3164         // Dig out the function prototype, if there is one.
3165         Proto = type->getAs<FunctionProtoType>();
3166       }
3167     }
3168 
3169     // Fill in non-null argument information from the nullability
3170     // information on the parameter types (if we have them).
3171     if (Proto) {
3172       unsigned Index = 0;
3173       for (auto paramType : Proto->getParamTypes()) {
3174         if (isNonNullType(paramType)) {
3175           if (NonNullArgs.empty())
3176             NonNullArgs.resize(Args.size());
3177 
3178           NonNullArgs.set(Index);
3179         }
3180 
3181         ++Index;
3182       }
3183     }
3184   }
3185 
3186   // Check for non-null arguments.
3187   for (unsigned ArgIndex = 0, ArgIndexEnd = NonNullArgs.size();
3188        ArgIndex != ArgIndexEnd; ++ArgIndex) {
3189     if (NonNullArgs[ArgIndex])
3190       CheckNonNullArgument(S, Args[ArgIndex], Args[ArgIndex]->getExprLoc());
3191   }
3192 }
3193 
3194 void Sema::CheckArgAlignment(SourceLocation Loc, NamedDecl *FDecl,
3195                              StringRef ParamName, QualType ArgTy,
3196                              QualType ParamTy) {
3197 
3198   // If a function accepts a pointer or reference type
3199   if (!ParamTy->isPointerType() && !ParamTy->isReferenceType())
3200     return;
3201 
3202   // If the parameter is a pointer type, get the pointee type for the
3203   // argument too. If the parameter is a reference type, don't try to get
3204   // the pointee type for the argument.
3205   if (ParamTy->isPointerType())
3206     ArgTy = ArgTy->getPointeeType();
3207 
3208   // Remove reference or pointer
3209   ParamTy = ParamTy->getPointeeType();
3210 
3211   // Find expected alignment, and the actual alignment of the passed object.
3212   // getTypeAlignInChars requires complete types
3213   if (ArgTy.isNull() || ParamTy->isDependentType() ||
3214       ParamTy->isIncompleteType() || ArgTy->isIncompleteType() ||
3215       ParamTy->isUndeducedType() || ArgTy->isUndeducedType())
3216     return;
3217 
3218   CharUnits ParamAlign = Context.getTypeAlignInChars(ParamTy);
3219   CharUnits ArgAlign = Context.getTypeAlignInChars(ArgTy);
3220 
3221   // If the argument is less aligned than the parameter, there is a
3222   // potential alignment issue.
3223   if (ArgAlign < ParamAlign)
3224     Diag(Loc, diag::warn_param_mismatched_alignment)
3225         << (int)ArgAlign.getQuantity() << (int)ParamAlign.getQuantity()
3226         << ParamName << (FDecl != nullptr) << FDecl;
3227 }
3228 
3229 void Sema::checkLifetimeCaptureBy(FunctionDecl *FD, bool IsMemberFunction,
3230                                   const Expr *ThisArg,
3231                                   ArrayRef<const Expr *> Args) {
3232   if (!FD || Args.empty())
3233     return;
3234   auto GetArgAt = [&](int Idx) -> const Expr * {
3235     if (Idx == LifetimeCaptureByAttr::GLOBAL ||
3236         Idx == LifetimeCaptureByAttr::UNKNOWN)
3237       return nullptr;
3238     if (IsMemberFunction && Idx == 0)
3239       return ThisArg;
3240     return Args[Idx - IsMemberFunction];
3241   };
3242   auto HandleCaptureByAttr = [&](const LifetimeCaptureByAttr *Attr,
3243                                  unsigned ArgIdx) {
3244     if (!Attr)
3245       return;
3246 
3247     Expr *Captured = const_cast<Expr *>(GetArgAt(ArgIdx));
3248     for (int CapturingParamIdx : Attr->params()) {
3249       // lifetime_capture_by(this) case is handled in the lifetimebound expr
3250       // initialization codepath.
3251       if (CapturingParamIdx == LifetimeCaptureByAttr::THIS &&
3252           isa<CXXConstructorDecl>(FD))
3253         continue;
3254       Expr *Capturing = const_cast<Expr *>(GetArgAt(CapturingParamIdx));
3255       CapturingEntity CE{Capturing};
3256       // Ensure that 'Captured' outlives the 'Capturing' entity.
3257       checkCaptureByLifetime(*this, CE, Captured);
3258     }
3259   };
3260   for (unsigned I = 0; I < FD->getNumParams(); ++I)
3261     HandleCaptureByAttr(FD->getParamDecl(I)->getAttr<LifetimeCaptureByAttr>(),
3262                         I + IsMemberFunction);
3263   // Check when the implicit object param is captured.
3264   if (IsMemberFunction) {
3265     TypeSourceInfo *TSI = FD->getTypeSourceInfo();
3266     if (!TSI)
3267       return;
3268     AttributedTypeLoc ATL;
3269     for (TypeLoc TL = TSI->getTypeLoc();
3270          (ATL = TL.getAsAdjusted<AttributedTypeLoc>());
3271          TL = ATL.getModifiedLoc())
3272       HandleCaptureByAttr(ATL.getAttrAs<LifetimeCaptureByAttr>(), 0);
3273   }
3274 }
3275 
3276 void Sema::checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto,
3277                      const Expr *ThisArg, ArrayRef<const Expr *> Args,
3278                      bool IsMemberFunction, SourceLocation Loc,
3279                      SourceRange Range, VariadicCallType CallType) {
3280   // FIXME: We should check as much as we can in the template definition.
3281   if (CurContext->isDependentContext())
3282     return;
3283 
3284   // Printf and scanf checking.
3285   llvm::SmallBitVector CheckedVarArgs;
3286   if (FDecl) {
3287     for (const auto *I : FDecl->specific_attrs<FormatAttr>()) {
3288       // Only create vector if there are format attributes.
3289       CheckedVarArgs.resize(Args.size());
3290 
3291       CheckFormatArguments(I, Args, IsMemberFunction, CallType, Loc, Range,
3292                            CheckedVarArgs);
3293     }
3294   }
3295 
3296   // Refuse POD arguments that weren't caught by the format string
3297   // checks above.
3298   auto *FD = dyn_cast_or_null<FunctionDecl>(FDecl);
3299   if (CallType != VariadicDoesNotApply &&
3300       (!FD || FD->getBuiltinID() != Builtin::BI__noop)) {
3301     unsigned NumParams = Proto ? Proto->getNumParams()
3302                          : isa_and_nonnull<FunctionDecl>(FDecl)
3303                              ? cast<FunctionDecl>(FDecl)->getNumParams()
3304                          : isa_and_nonnull<ObjCMethodDecl>(FDecl)
3305                              ? cast<ObjCMethodDecl>(FDecl)->param_size()
3306                              : 0;
3307 
3308     for (unsigned ArgIdx = NumParams; ArgIdx < Args.size(); ++ArgIdx) {
3309       // Args[ArgIdx] can be null in malformed code.
3310       if (const Expr *Arg = Args[ArgIdx]) {
3311         if (CheckedVarArgs.empty() || !CheckedVarArgs[ArgIdx])
3312           checkVariadicArgument(Arg, CallType);
3313       }
3314     }
3315   }
3316   if (FD)
3317     checkLifetimeCaptureBy(FD, IsMemberFunction, ThisArg, Args);
3318   if (FDecl || Proto) {
3319     CheckNonNullArguments(*this, FDecl, Proto, Args, Loc);
3320 
3321     // Type safety checking.
3322     if (FDecl) {
3323       for (const auto *I : FDecl->specific_attrs<ArgumentWithTypeTagAttr>())
3324         CheckArgumentWithTypeTag(I, Args, Loc);
3325     }
3326   }
3327 
3328   // Check that passed arguments match the alignment of original arguments.
3329   // Try to get the missing prototype from the declaration.
3330   if (!Proto && FDecl) {
3331     const auto *FT = FDecl->getFunctionType();
3332     if (isa_and_nonnull<FunctionProtoType>(FT))
3333       Proto = cast<FunctionProtoType>(FDecl->getFunctionType());
3334   }
3335   if (Proto) {
3336     // For variadic functions, we may have more args than parameters.
3337     // For some K&R functions, we may have less args than parameters.
3338     const auto N = std::min<unsigned>(Proto->getNumParams(), Args.size());
3339     bool IsScalableRet = Proto->getReturnType()->isSizelessVectorType();
3340     bool IsScalableArg = false;
3341     for (unsigned ArgIdx = 0; ArgIdx < N; ++ArgIdx) {
3342       // Args[ArgIdx] can be null in malformed code.
3343       if (const Expr *Arg = Args[ArgIdx]) {
3344         if (Arg->containsErrors())
3345           continue;
3346 
3347         if (Context.getTargetInfo().getTriple().isOSAIX() && FDecl && Arg &&
3348             FDecl->hasLinkage() &&
3349             FDecl->getFormalLinkage() != Linkage::Internal &&
3350             CallType == VariadicDoesNotApply)
3351           PPC().checkAIXMemberAlignment((Arg->getExprLoc()), Arg);
3352 
3353         QualType ParamTy = Proto->getParamType(ArgIdx);
3354         if (ParamTy->isSizelessVectorType())
3355           IsScalableArg = true;
3356         QualType ArgTy = Arg->getType();
3357         CheckArgAlignment(Arg->getExprLoc(), FDecl, std::to_string(ArgIdx + 1),
3358                           ArgTy, ParamTy);
3359       }
3360     }
3361 
3362     // If the callee has an AArch64 SME attribute to indicate that it is an
3363     // __arm_streaming function, then the caller requires SME to be available.
3364     FunctionProtoType::ExtProtoInfo ExtInfo = Proto->getExtProtoInfo();
3365     if (ExtInfo.AArch64SMEAttributes & FunctionType::SME_PStateSMEnabledMask) {
3366       if (auto *CallerFD = dyn_cast<FunctionDecl>(CurContext)) {
3367         llvm::StringMap<bool> CallerFeatureMap;
3368         Context.getFunctionFeatureMap(CallerFeatureMap, CallerFD);
3369         if (!CallerFeatureMap.contains("sme"))
3370           Diag(Loc, diag::err_sme_call_in_non_sme_target);
3371       } else if (!Context.getTargetInfo().hasFeature("sme")) {
3372         Diag(Loc, diag::err_sme_call_in_non_sme_target);
3373       }
3374     }
3375 
3376     // If the call requires a streaming-mode change and has scalable vector
3377     // arguments or return values, then warn the user that the streaming and
3378     // non-streaming vector lengths may be different.
3379     const auto *CallerFD = dyn_cast<FunctionDecl>(CurContext);
3380     if (CallerFD && (!FD || !FD->getBuiltinID()) &&
3381         (IsScalableArg || IsScalableRet)) {
3382       bool IsCalleeStreaming =
3383           ExtInfo.AArch64SMEAttributes & FunctionType::SME_PStateSMEnabledMask;
3384       bool IsCalleeStreamingCompatible =
3385           ExtInfo.AArch64SMEAttributes &
3386           FunctionType::SME_PStateSMCompatibleMask;
3387       SemaARM::ArmStreamingType CallerFnType = getArmStreamingFnType(CallerFD);
3388       if (!IsCalleeStreamingCompatible &&
3389           (CallerFnType == SemaARM::ArmStreamingCompatible ||
3390            ((CallerFnType == SemaARM::ArmStreaming) ^ IsCalleeStreaming))) {
3391         if (IsScalableArg)
3392           Diag(Loc, diag::warn_sme_streaming_pass_return_vl_to_non_streaming)
3393               << /*IsArg=*/true;
3394         if (IsScalableRet)
3395           Diag(Loc, diag::warn_sme_streaming_pass_return_vl_to_non_streaming)
3396               << /*IsArg=*/false;
3397       }
3398     }
3399 
3400     FunctionType::ArmStateValue CalleeArmZAState =
3401         FunctionType::getArmZAState(ExtInfo.AArch64SMEAttributes);
3402     FunctionType::ArmStateValue CalleeArmZT0State =
3403         FunctionType::getArmZT0State(ExtInfo.AArch64SMEAttributes);
3404     if (CalleeArmZAState != FunctionType::ARM_None ||
3405         CalleeArmZT0State != FunctionType::ARM_None) {
3406       bool CallerHasZAState = false;
3407       bool CallerHasZT0State = false;
3408       if (CallerFD) {
3409         auto *Attr = CallerFD->getAttr<ArmNewAttr>();
3410         if (Attr && Attr->isNewZA())
3411           CallerHasZAState = true;
3412         if (Attr && Attr->isNewZT0())
3413           CallerHasZT0State = true;
3414         if (const auto *FPT = CallerFD->getType()->getAs<FunctionProtoType>()) {
3415           CallerHasZAState |=
3416               FunctionType::getArmZAState(
3417                   FPT->getExtProtoInfo().AArch64SMEAttributes) !=
3418               FunctionType::ARM_None;
3419           CallerHasZT0State |=
3420               FunctionType::getArmZT0State(
3421                   FPT->getExtProtoInfo().AArch64SMEAttributes) !=
3422               FunctionType::ARM_None;
3423         }
3424       }
3425 
3426       if (CalleeArmZAState != FunctionType::ARM_None && !CallerHasZAState)
3427         Diag(Loc, diag::err_sme_za_call_no_za_state);
3428 
3429       if (CalleeArmZT0State != FunctionType::ARM_None && !CallerHasZT0State)
3430         Diag(Loc, diag::err_sme_zt0_call_no_zt0_state);
3431 
3432       if (CallerHasZAState && CalleeArmZAState == FunctionType::ARM_None &&
3433           CalleeArmZT0State != FunctionType::ARM_None) {
3434         Diag(Loc, diag::err_sme_unimplemented_za_save_restore);
3435         Diag(Loc, diag::note_sme_use_preserves_za);
3436       }
3437     }
3438   }
3439 
3440   if (FDecl && FDecl->hasAttr<AllocAlignAttr>()) {
3441     auto *AA = FDecl->getAttr<AllocAlignAttr>();
3442     const Expr *Arg = Args[AA->getParamIndex().getASTIndex()];
3443     if (!Arg->isValueDependent()) {
3444       Expr::EvalResult Align;
3445       if (Arg->EvaluateAsInt(Align, Context)) {
3446         const llvm::APSInt &I = Align.Val.getInt();
3447         if (!I.isPowerOf2())
3448           Diag(Arg->getExprLoc(), diag::warn_alignment_not_power_of_two)
3449               << Arg->getSourceRange();
3450 
3451         if (I > Sema::MaximumAlignment)
3452           Diag(Arg->getExprLoc(), diag::warn_assume_aligned_too_great)
3453               << Arg->getSourceRange() << Sema::MaximumAlignment;
3454       }
3455     }
3456   }
3457 
3458   if (FD)
3459     diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
3460 }
3461 
3462 void Sema::CheckConstrainedAuto(const AutoType *AutoT, SourceLocation Loc) {
3463   if (ConceptDecl *Decl = AutoT->getTypeConstraintConcept()) {
3464     DiagnoseUseOfDecl(Decl, Loc);
3465   }
3466 }
3467 
3468 void Sema::CheckConstructorCall(FunctionDecl *FDecl, QualType ThisType,
3469                                 ArrayRef<const Expr *> Args,
3470                                 const FunctionProtoType *Proto,
3471                                 SourceLocation Loc) {
3472   VariadicCallType CallType =
3473       Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply;
3474 
3475   auto *Ctor = cast<CXXConstructorDecl>(FDecl);
3476   CheckArgAlignment(
3477       Loc, FDecl, "'this'", Context.getPointerType(ThisType),
3478       Context.getPointerType(Ctor->getFunctionObjectParameterType()));
3479 
3480   checkCall(FDecl, Proto, /*ThisArg=*/nullptr, Args, /*IsMemberFunction=*/true,
3481             Loc, SourceRange(), CallType);
3482 }
3483 
3484 bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall,
3485                              const FunctionProtoType *Proto) {
3486   bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) &&
3487                               isa<CXXMethodDecl>(FDecl);
3488   bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) ||
3489                           IsMemberOperatorCall;
3490   VariadicCallType CallType = getVariadicCallType(FDecl, Proto,
3491                                                   TheCall->getCallee());
3492   Expr** Args = TheCall->getArgs();
3493   unsigned NumArgs = TheCall->getNumArgs();
3494 
3495   Expr *ImplicitThis = nullptr;
3496   if (IsMemberOperatorCall && !FDecl->hasCXXExplicitFunctionObjectParameter()) {
3497     // If this is a call to a member operator, hide the first
3498     // argument from checkCall.
3499     // FIXME: Our choice of AST representation here is less than ideal.
3500     ImplicitThis = Args[0];
3501     ++Args;
3502     --NumArgs;
3503   } else if (IsMemberFunction && !FDecl->isStatic() &&
3504              !FDecl->hasCXXExplicitFunctionObjectParameter())
3505     ImplicitThis =
3506         cast<CXXMemberCallExpr>(TheCall)->getImplicitObjectArgument();
3507 
3508   if (ImplicitThis) {
3509     // ImplicitThis may or may not be a pointer, depending on whether . or -> is
3510     // used.
3511     QualType ThisType = ImplicitThis->getType();
3512     if (!ThisType->isPointerType()) {
3513       assert(!ThisType->isReferenceType());
3514       ThisType = Context.getPointerType(ThisType);
3515     }
3516 
3517     QualType ThisTypeFromDecl = Context.getPointerType(
3518         cast<CXXMethodDecl>(FDecl)->getFunctionObjectParameterType());
3519 
3520     CheckArgAlignment(TheCall->getRParenLoc(), FDecl, "'this'", ThisType,
3521                       ThisTypeFromDecl);
3522   }
3523 
3524   checkCall(FDecl, Proto, ImplicitThis, llvm::ArrayRef(Args, NumArgs),
3525             IsMemberFunction, TheCall->getRParenLoc(),
3526             TheCall->getCallee()->getSourceRange(), CallType);
3527 
3528   IdentifierInfo *FnInfo = FDecl->getIdentifier();
3529   // None of the checks below are needed for functions that don't have
3530   // simple names (e.g., C++ conversion functions).
3531   if (!FnInfo)
3532     return false;
3533 
3534   // Enforce TCB except for builtin calls, which are always allowed.
3535   if (FDecl->getBuiltinID() == 0)
3536     CheckTCBEnforcement(TheCall->getExprLoc(), FDecl);
3537 
3538   CheckAbsoluteValueFunction(TheCall, FDecl);
3539   CheckMaxUnsignedZero(TheCall, FDecl);
3540   CheckInfNaNFunction(TheCall, FDecl);
3541 
3542   if (getLangOpts().ObjC)
3543     ObjC().DiagnoseCStringFormatDirectiveInCFAPI(FDecl, Args, NumArgs);
3544 
3545   unsigned CMId = FDecl->getMemoryFunctionKind();
3546 
3547   // Handle memory setting and copying functions.
3548   switch (CMId) {
3549   case 0:
3550     return false;
3551   case Builtin::BIstrlcpy: // fallthrough
3552   case Builtin::BIstrlcat:
3553     CheckStrlcpycatArguments(TheCall, FnInfo);
3554     break;
3555   case Builtin::BIstrncat:
3556     CheckStrncatArguments(TheCall, FnInfo);
3557     break;
3558   case Builtin::BIfree:
3559     CheckFreeArguments(TheCall);
3560     break;
3561   default:
3562     CheckMemaccessArguments(TheCall, CMId, FnInfo);
3563   }
3564 
3565   return false;
3566 }
3567 
3568 bool Sema::CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall,
3569                             const FunctionProtoType *Proto) {
3570   QualType Ty;
3571   if (const auto *V = dyn_cast<VarDecl>(NDecl))
3572     Ty = V->getType().getNonReferenceType();
3573   else if (const auto *F = dyn_cast<FieldDecl>(NDecl))
3574     Ty = F->getType().getNonReferenceType();
3575   else
3576     return false;
3577 
3578   if (!Ty->isBlockPointerType() && !Ty->isFunctionPointerType() &&
3579       !Ty->isFunctionProtoType())
3580     return false;
3581 
3582   VariadicCallType CallType;
3583   if (!Proto || !Proto->isVariadic()) {
3584     CallType = VariadicDoesNotApply;
3585   } else if (Ty->isBlockPointerType()) {
3586     CallType = VariadicBlock;
3587   } else { // Ty->isFunctionPointerType()
3588     CallType = VariadicFunction;
3589   }
3590 
3591   checkCall(NDecl, Proto, /*ThisArg=*/nullptr,
3592             llvm::ArrayRef(TheCall->getArgs(), TheCall->getNumArgs()),
3593             /*IsMemberFunction=*/false, TheCall->getRParenLoc(),
3594             TheCall->getCallee()->getSourceRange(), CallType);
3595 
3596   return false;
3597 }
3598 
3599 bool Sema::CheckOtherCall(CallExpr *TheCall, const FunctionProtoType *Proto) {
3600   VariadicCallType CallType = getVariadicCallType(/*FDecl=*/nullptr, Proto,
3601                                                   TheCall->getCallee());
3602   checkCall(/*FDecl=*/nullptr, Proto, /*ThisArg=*/nullptr,
3603             llvm::ArrayRef(TheCall->getArgs(), TheCall->getNumArgs()),
3604             /*IsMemberFunction=*/false, TheCall->getRParenLoc(),
3605             TheCall->getCallee()->getSourceRange(), CallType);
3606 
3607   return false;
3608 }
3609 
3610 static bool isValidOrderingForOp(int64_t Ordering, AtomicExpr::AtomicOp Op) {
3611   if (!llvm::isValidAtomicOrderingCABI(Ordering))
3612     return false;
3613 
3614   auto OrderingCABI = (llvm::AtomicOrderingCABI)Ordering;
3615   switch (Op) {
3616   case AtomicExpr::AO__c11_atomic_init:
3617   case AtomicExpr::AO__opencl_atomic_init:
3618     llvm_unreachable("There is no ordering argument for an init");
3619 
3620   case AtomicExpr::AO__c11_atomic_load:
3621   case AtomicExpr::AO__opencl_atomic_load:
3622   case AtomicExpr::AO__hip_atomic_load:
3623   case AtomicExpr::AO__atomic_load_n:
3624   case AtomicExpr::AO__atomic_load:
3625   case AtomicExpr::AO__scoped_atomic_load_n:
3626   case AtomicExpr::AO__scoped_atomic_load:
3627     return OrderingCABI != llvm::AtomicOrderingCABI::release &&
3628            OrderingCABI != llvm::AtomicOrderingCABI::acq_rel;
3629 
3630   case AtomicExpr::AO__c11_atomic_store:
3631   case AtomicExpr::AO__opencl_atomic_store:
3632   case AtomicExpr::AO__hip_atomic_store:
3633   case AtomicExpr::AO__atomic_store:
3634   case AtomicExpr::AO__atomic_store_n:
3635   case AtomicExpr::AO__scoped_atomic_store:
3636   case AtomicExpr::AO__scoped_atomic_store_n:
3637     return OrderingCABI != llvm::AtomicOrderingCABI::consume &&
3638            OrderingCABI != llvm::AtomicOrderingCABI::acquire &&
3639            OrderingCABI != llvm::AtomicOrderingCABI::acq_rel;
3640 
3641   default:
3642     return true;
3643   }
3644 }
3645 
3646 ExprResult Sema::AtomicOpsOverloaded(ExprResult TheCallResult,
3647                                      AtomicExpr::AtomicOp Op) {
3648   CallExpr *TheCall = cast<CallExpr>(TheCallResult.get());
3649   DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
3650   MultiExprArg Args{TheCall->getArgs(), TheCall->getNumArgs()};
3651   return BuildAtomicExpr({TheCall->getBeginLoc(), TheCall->getEndLoc()},
3652                          DRE->getSourceRange(), TheCall->getRParenLoc(), Args,
3653                          Op);
3654 }
3655 
3656 ExprResult Sema::BuildAtomicExpr(SourceRange CallRange, SourceRange ExprRange,
3657                                  SourceLocation RParenLoc, MultiExprArg Args,
3658                                  AtomicExpr::AtomicOp Op,
3659                                  AtomicArgumentOrder ArgOrder) {
3660   // All the non-OpenCL operations take one of the following forms.
3661   // The OpenCL operations take the __c11 forms with one extra argument for
3662   // synchronization scope.
3663   enum {
3664     // C    __c11_atomic_init(A *, C)
3665     Init,
3666 
3667     // C    __c11_atomic_load(A *, int)
3668     Load,
3669 
3670     // void __atomic_load(A *, CP, int)
3671     LoadCopy,
3672 
3673     // void __atomic_store(A *, CP, int)
3674     Copy,
3675 
3676     // C    __c11_atomic_add(A *, M, int)
3677     Arithmetic,
3678 
3679     // C    __atomic_exchange_n(A *, CP, int)
3680     Xchg,
3681 
3682     // void __atomic_exchange(A *, C *, CP, int)
3683     GNUXchg,
3684 
3685     // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int)
3686     C11CmpXchg,
3687 
3688     // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int)
3689     GNUCmpXchg
3690   } Form = Init;
3691 
3692   const unsigned NumForm = GNUCmpXchg + 1;
3693   const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 3, 4, 5, 6 };
3694   const unsigned NumVals[] = { 1, 0, 1, 1, 1, 1, 2, 2, 3 };
3695   // where:
3696   //   C is an appropriate type,
3697   //   A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins,
3698   //   CP is C for __c11 builtins and GNU _n builtins and is C * otherwise,
3699   //   M is C if C is an integer, and ptrdiff_t if C is a pointer, and
3700   //   the int parameters are for orderings.
3701 
3702   static_assert(sizeof(NumArgs)/sizeof(NumArgs[0]) == NumForm
3703       && sizeof(NumVals)/sizeof(NumVals[0]) == NumForm,
3704       "need to update code for modified forms");
3705   static_assert(AtomicExpr::AO__atomic_add_fetch == 0 &&
3706                     AtomicExpr::AO__atomic_xor_fetch + 1 ==
3707                         AtomicExpr::AO__c11_atomic_compare_exchange_strong,
3708                 "need to update code for modified C11 atomics");
3709   bool IsOpenCL = Op >= AtomicExpr::AO__opencl_atomic_compare_exchange_strong &&
3710                   Op <= AtomicExpr::AO__opencl_atomic_store;
3711   bool IsHIP = Op >= AtomicExpr::AO__hip_atomic_compare_exchange_strong &&
3712                Op <= AtomicExpr::AO__hip_atomic_store;
3713   bool IsScoped = Op >= AtomicExpr::AO__scoped_atomic_add_fetch &&
3714                   Op <= AtomicExpr::AO__scoped_atomic_xor_fetch;
3715   bool IsC11 = (Op >= AtomicExpr::AO__c11_atomic_compare_exchange_strong &&
3716                 Op <= AtomicExpr::AO__c11_atomic_store) ||
3717                IsOpenCL;
3718   bool IsN = Op == AtomicExpr::AO__atomic_load_n ||
3719              Op == AtomicExpr::AO__atomic_store_n ||
3720              Op == AtomicExpr::AO__atomic_exchange_n ||
3721              Op == AtomicExpr::AO__atomic_compare_exchange_n ||
3722              Op == AtomicExpr::AO__scoped_atomic_load_n ||
3723              Op == AtomicExpr::AO__scoped_atomic_store_n ||
3724              Op == AtomicExpr::AO__scoped_atomic_exchange_n ||
3725              Op == AtomicExpr::AO__scoped_atomic_compare_exchange_n;
3726   // Bit mask for extra allowed value types other than integers for atomic
3727   // arithmetic operations. Add/sub allow pointer and floating point. Min/max
3728   // allow floating point.
3729   enum ArithOpExtraValueType {
3730     AOEVT_None = 0,
3731     AOEVT_Pointer = 1,
3732     AOEVT_FP = 2,
3733   };
3734   unsigned ArithAllows = AOEVT_None;
3735 
3736   switch (Op) {
3737   case AtomicExpr::AO__c11_atomic_init:
3738   case AtomicExpr::AO__opencl_atomic_init:
3739     Form = Init;
3740     break;
3741 
3742   case AtomicExpr::AO__c11_atomic_load:
3743   case AtomicExpr::AO__opencl_atomic_load:
3744   case AtomicExpr::AO__hip_atomic_load:
3745   case AtomicExpr::AO__atomic_load_n:
3746   case AtomicExpr::AO__scoped_atomic_load_n:
3747     Form = Load;
3748     break;
3749 
3750   case AtomicExpr::AO__atomic_load:
3751   case AtomicExpr::AO__scoped_atomic_load:
3752     Form = LoadCopy;
3753     break;
3754 
3755   case AtomicExpr::AO__c11_atomic_store:
3756   case AtomicExpr::AO__opencl_atomic_store:
3757   case AtomicExpr::AO__hip_atomic_store:
3758   case AtomicExpr::AO__atomic_store:
3759   case AtomicExpr::AO__atomic_store_n:
3760   case AtomicExpr::AO__scoped_atomic_store:
3761   case AtomicExpr::AO__scoped_atomic_store_n:
3762     Form = Copy;
3763     break;
3764   case AtomicExpr::AO__atomic_fetch_add:
3765   case AtomicExpr::AO__atomic_fetch_sub:
3766   case AtomicExpr::AO__atomic_add_fetch:
3767   case AtomicExpr::AO__atomic_sub_fetch:
3768   case AtomicExpr::AO__scoped_atomic_fetch_add:
3769   case AtomicExpr::AO__scoped_atomic_fetch_sub:
3770   case AtomicExpr::AO__scoped_atomic_add_fetch:
3771   case AtomicExpr::AO__scoped_atomic_sub_fetch:
3772   case AtomicExpr::AO__c11_atomic_fetch_add:
3773   case AtomicExpr::AO__c11_atomic_fetch_sub:
3774   case AtomicExpr::AO__opencl_atomic_fetch_add:
3775   case AtomicExpr::AO__opencl_atomic_fetch_sub:
3776   case AtomicExpr::AO__hip_atomic_fetch_add:
3777   case AtomicExpr::AO__hip_atomic_fetch_sub:
3778     ArithAllows = AOEVT_Pointer | AOEVT_FP;
3779     Form = Arithmetic;
3780     break;
3781   case AtomicExpr::AO__atomic_fetch_max:
3782   case AtomicExpr::AO__atomic_fetch_min:
3783   case AtomicExpr::AO__atomic_max_fetch:
3784   case AtomicExpr::AO__atomic_min_fetch:
3785   case AtomicExpr::AO__scoped_atomic_fetch_max:
3786   case AtomicExpr::AO__scoped_atomic_fetch_min:
3787   case AtomicExpr::AO__scoped_atomic_max_fetch:
3788   case AtomicExpr::AO__scoped_atomic_min_fetch:
3789   case AtomicExpr::AO__c11_atomic_fetch_max:
3790   case AtomicExpr::AO__c11_atomic_fetch_min:
3791   case AtomicExpr::AO__opencl_atomic_fetch_max:
3792   case AtomicExpr::AO__opencl_atomic_fetch_min:
3793   case AtomicExpr::AO__hip_atomic_fetch_max:
3794   case AtomicExpr::AO__hip_atomic_fetch_min:
3795     ArithAllows = AOEVT_FP;
3796     Form = Arithmetic;
3797     break;
3798   case AtomicExpr::AO__c11_atomic_fetch_and:
3799   case AtomicExpr::AO__c11_atomic_fetch_or:
3800   case AtomicExpr::AO__c11_atomic_fetch_xor:
3801   case AtomicExpr::AO__hip_atomic_fetch_and:
3802   case AtomicExpr::AO__hip_atomic_fetch_or:
3803   case AtomicExpr::AO__hip_atomic_fetch_xor:
3804   case AtomicExpr::AO__c11_atomic_fetch_nand:
3805   case AtomicExpr::AO__opencl_atomic_fetch_and:
3806   case AtomicExpr::AO__opencl_atomic_fetch_or:
3807   case AtomicExpr::AO__opencl_atomic_fetch_xor:
3808   case AtomicExpr::AO__atomic_fetch_and:
3809   case AtomicExpr::AO__atomic_fetch_or:
3810   case AtomicExpr::AO__atomic_fetch_xor:
3811   case AtomicExpr::AO__atomic_fetch_nand:
3812   case AtomicExpr::AO__atomic_and_fetch:
3813   case AtomicExpr::AO__atomic_or_fetch:
3814   case AtomicExpr::AO__atomic_xor_fetch:
3815   case AtomicExpr::AO__atomic_nand_fetch:
3816   case AtomicExpr::AO__scoped_atomic_fetch_and:
3817   case AtomicExpr::AO__scoped_atomic_fetch_or:
3818   case AtomicExpr::AO__scoped_atomic_fetch_xor:
3819   case AtomicExpr::AO__scoped_atomic_fetch_nand:
3820   case AtomicExpr::AO__scoped_atomic_and_fetch:
3821   case AtomicExpr::AO__scoped_atomic_or_fetch:
3822   case AtomicExpr::AO__scoped_atomic_xor_fetch:
3823   case AtomicExpr::AO__scoped_atomic_nand_fetch:
3824     Form = Arithmetic;
3825     break;
3826 
3827   case AtomicExpr::AO__c11_atomic_exchange:
3828   case AtomicExpr::AO__hip_atomic_exchange:
3829   case AtomicExpr::AO__opencl_atomic_exchange:
3830   case AtomicExpr::AO__atomic_exchange_n:
3831   case AtomicExpr::AO__scoped_atomic_exchange_n:
3832     Form = Xchg;
3833     break;
3834 
3835   case AtomicExpr::AO__atomic_exchange:
3836   case AtomicExpr::AO__scoped_atomic_exchange:
3837     Form = GNUXchg;
3838     break;
3839 
3840   case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
3841   case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
3842   case AtomicExpr::AO__hip_atomic_compare_exchange_strong:
3843   case AtomicExpr::AO__opencl_atomic_compare_exchange_strong:
3844   case AtomicExpr::AO__opencl_atomic_compare_exchange_weak:
3845   case AtomicExpr::AO__hip_atomic_compare_exchange_weak:
3846     Form = C11CmpXchg;
3847     break;
3848 
3849   case AtomicExpr::AO__atomic_compare_exchange:
3850   case AtomicExpr::AO__atomic_compare_exchange_n:
3851   case AtomicExpr::AO__scoped_atomic_compare_exchange:
3852   case AtomicExpr::AO__scoped_atomic_compare_exchange_n:
3853     Form = GNUCmpXchg;
3854     break;
3855   }
3856 
3857   unsigned AdjustedNumArgs = NumArgs[Form];
3858   if ((IsOpenCL || IsHIP || IsScoped) &&
3859       Op != AtomicExpr::AO__opencl_atomic_init)
3860     ++AdjustedNumArgs;
3861   // Check we have the right number of arguments.
3862   if (Args.size() < AdjustedNumArgs) {
3863     Diag(CallRange.getEnd(), diag::err_typecheck_call_too_few_args)
3864         << 0 << AdjustedNumArgs << static_cast<unsigned>(Args.size())
3865         << /*is non object*/ 0 << ExprRange;
3866     return ExprError();
3867   } else if (Args.size() > AdjustedNumArgs) {
3868     Diag(Args[AdjustedNumArgs]->getBeginLoc(),
3869          diag::err_typecheck_call_too_many_args)
3870         << 0 << AdjustedNumArgs << static_cast<unsigned>(Args.size())
3871         << /*is non object*/ 0 << ExprRange;
3872     return ExprError();
3873   }
3874 
3875   // Inspect the first argument of the atomic operation.
3876   Expr *Ptr = Args[0];
3877   ExprResult ConvertedPtr = DefaultFunctionArrayLvalueConversion(Ptr);
3878   if (ConvertedPtr.isInvalid())
3879     return ExprError();
3880 
3881   Ptr = ConvertedPtr.get();
3882   const PointerType *pointerType = Ptr->getType()->getAs<PointerType>();
3883   if (!pointerType) {
3884     Diag(ExprRange.getBegin(), diag::err_atomic_builtin_must_be_pointer)
3885         << Ptr->getType() << 0 << Ptr->getSourceRange();
3886     return ExprError();
3887   }
3888 
3889   // For a __c11 builtin, this should be a pointer to an _Atomic type.
3890   QualType AtomTy = pointerType->getPointeeType(); // 'A'
3891   QualType ValType = AtomTy; // 'C'
3892   if (IsC11) {
3893     if (!AtomTy->isAtomicType()) {
3894       Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_atomic)
3895           << Ptr->getType() << Ptr->getSourceRange();
3896       return ExprError();
3897     }
3898     if ((Form != Load && Form != LoadCopy && AtomTy.isConstQualified()) ||
3899         AtomTy.getAddressSpace() == LangAS::opencl_constant) {
3900       Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_const_atomic)
3901           << (AtomTy.isConstQualified() ? 0 : 1) << Ptr->getType()
3902           << Ptr->getSourceRange();
3903       return ExprError();
3904     }
3905     ValType = AtomTy->castAs<AtomicType>()->getValueType();
3906   } else if (Form != Load && Form != LoadCopy) {
3907     if (ValType.isConstQualified()) {
3908       Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_const_pointer)
3909           << Ptr->getType() << Ptr->getSourceRange();
3910       return ExprError();
3911     }
3912   }
3913 
3914   // Pointer to object of size zero is not allowed.
3915   if (RequireCompleteType(Ptr->getBeginLoc(), AtomTy,
3916                           diag::err_incomplete_type))
3917     return ExprError();
3918   if (Context.getTypeInfoInChars(AtomTy).Width.isZero()) {
3919     Diag(ExprRange.getBegin(), diag::err_atomic_builtin_must_be_pointer)
3920         << Ptr->getType() << 1 << Ptr->getSourceRange();
3921     return ExprError();
3922   }
3923 
3924   // For an arithmetic operation, the implied arithmetic must be well-formed.
3925   if (Form == Arithmetic) {
3926     // GCC does not enforce these rules for GNU atomics, but we do to help catch
3927     // trivial type errors.
3928     auto IsAllowedValueType = [&](QualType ValType,
3929                                   unsigned AllowedType) -> bool {
3930       if (ValType->isIntegerType())
3931         return true;
3932       if (ValType->isPointerType())
3933         return AllowedType & AOEVT_Pointer;
3934       if (!(ValType->isFloatingType() && (AllowedType & AOEVT_FP)))
3935         return false;
3936       // LLVM Parser does not allow atomicrmw with x86_fp80 type.
3937       if (ValType->isSpecificBuiltinType(BuiltinType::LongDouble) &&
3938           &Context.getTargetInfo().getLongDoubleFormat() ==
3939               &llvm::APFloat::x87DoubleExtended())
3940         return false;
3941       return true;
3942     };
3943     if (!IsAllowedValueType(ValType, ArithAllows)) {
3944       auto DID = ArithAllows & AOEVT_FP
3945                      ? (ArithAllows & AOEVT_Pointer
3946                             ? diag::err_atomic_op_needs_atomic_int_ptr_or_fp
3947                             : diag::err_atomic_op_needs_atomic_int_or_fp)
3948                      : diag::err_atomic_op_needs_atomic_int;
3949       Diag(ExprRange.getBegin(), DID)
3950           << IsC11 << Ptr->getType() << Ptr->getSourceRange();
3951       return ExprError();
3952     }
3953     if (IsC11 && ValType->isPointerType() &&
3954         RequireCompleteType(Ptr->getBeginLoc(), ValType->getPointeeType(),
3955                             diag::err_incomplete_type)) {
3956       return ExprError();
3957     }
3958   } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) {
3959     // For __atomic_*_n operations, the value type must be a scalar integral or
3960     // pointer type which is 1, 2, 4, 8 or 16 bytes in length.
3961     Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_atomic_int_or_ptr)
3962         << IsC11 << Ptr->getType() << Ptr->getSourceRange();
3963     return ExprError();
3964   }
3965 
3966   if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context) &&
3967       !AtomTy->isScalarType()) {
3968     // For GNU atomics, require a trivially-copyable type. This is not part of
3969     // the GNU atomics specification but we enforce it for consistency with
3970     // other atomics which generally all require a trivially-copyable type. This
3971     // is because atomics just copy bits.
3972     Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_trivial_copy)
3973         << Ptr->getType() << Ptr->getSourceRange();
3974     return ExprError();
3975   }
3976 
3977   switch (ValType.getObjCLifetime()) {
3978   case Qualifiers::OCL_None:
3979   case Qualifiers::OCL_ExplicitNone:
3980     // okay
3981     break;
3982 
3983   case Qualifiers::OCL_Weak:
3984   case Qualifiers::OCL_Strong:
3985   case Qualifiers::OCL_Autoreleasing:
3986     // FIXME: Can this happen? By this point, ValType should be known
3987     // to be trivially copyable.
3988     Diag(ExprRange.getBegin(), diag::err_arc_atomic_ownership)
3989         << ValType << Ptr->getSourceRange();
3990     return ExprError();
3991   }
3992 
3993   // All atomic operations have an overload which takes a pointer to a volatile
3994   // 'A'.  We shouldn't let the volatile-ness of the pointee-type inject itself
3995   // into the result or the other operands. Similarly atomic_load takes a
3996   // pointer to a const 'A'.
3997   ValType.removeLocalVolatile();
3998   ValType.removeLocalConst();
3999   QualType ResultType = ValType;
4000   if (Form == Copy || Form == LoadCopy || Form == GNUXchg ||
4001       Form == Init)
4002     ResultType = Context.VoidTy;
4003   else if (Form == C11CmpXchg || Form == GNUCmpXchg)
4004     ResultType = Context.BoolTy;
4005 
4006   // The type of a parameter passed 'by value'. In the GNU atomics, such
4007   // arguments are actually passed as pointers.
4008   QualType ByValType = ValType; // 'CP'
4009   bool IsPassedByAddress = false;
4010   if (!IsC11 && !IsHIP && !IsN) {
4011     ByValType = Ptr->getType();
4012     IsPassedByAddress = true;
4013   }
4014 
4015   SmallVector<Expr *, 5> APIOrderedArgs;
4016   if (ArgOrder == Sema::AtomicArgumentOrder::AST) {
4017     APIOrderedArgs.push_back(Args[0]);
4018     switch (Form) {
4019     case Init:
4020     case Load:
4021       APIOrderedArgs.push_back(Args[1]); // Val1/Order
4022       break;
4023     case LoadCopy:
4024     case Copy:
4025     case Arithmetic:
4026     case Xchg:
4027       APIOrderedArgs.push_back(Args[2]); // Val1
4028       APIOrderedArgs.push_back(Args[1]); // Order
4029       break;
4030     case GNUXchg:
4031       APIOrderedArgs.push_back(Args[2]); // Val1
4032       APIOrderedArgs.push_back(Args[3]); // Val2
4033       APIOrderedArgs.push_back(Args[1]); // Order
4034       break;
4035     case C11CmpXchg:
4036       APIOrderedArgs.push_back(Args[2]); // Val1
4037       APIOrderedArgs.push_back(Args[4]); // Val2
4038       APIOrderedArgs.push_back(Args[1]); // Order
4039       APIOrderedArgs.push_back(Args[3]); // OrderFail
4040       break;
4041     case GNUCmpXchg:
4042       APIOrderedArgs.push_back(Args[2]); // Val1
4043       APIOrderedArgs.push_back(Args[4]); // Val2
4044       APIOrderedArgs.push_back(Args[5]); // Weak
4045       APIOrderedArgs.push_back(Args[1]); // Order
4046       APIOrderedArgs.push_back(Args[3]); // OrderFail
4047       break;
4048     }
4049   } else
4050     APIOrderedArgs.append(Args.begin(), Args.end());
4051 
4052   // The first argument's non-CV pointer type is used to deduce the type of
4053   // subsequent arguments, except for:
4054   //  - weak flag (always converted to bool)
4055   //  - memory order (always converted to int)
4056   //  - scope  (always converted to int)
4057   for (unsigned i = 0; i != APIOrderedArgs.size(); ++i) {
4058     QualType Ty;
4059     if (i < NumVals[Form] + 1) {
4060       switch (i) {
4061       case 0:
4062         // The first argument is always a pointer. It has a fixed type.
4063         // It is always dereferenced, a nullptr is undefined.
4064         CheckNonNullArgument(*this, APIOrderedArgs[i], ExprRange.getBegin());
4065         // Nothing else to do: we already know all we want about this pointer.
4066         continue;
4067       case 1:
4068         // The second argument is the non-atomic operand. For arithmetic, this
4069         // is always passed by value, and for a compare_exchange it is always
4070         // passed by address. For the rest, GNU uses by-address and C11 uses
4071         // by-value.
4072         assert(Form != Load);
4073         if (Form == Arithmetic && ValType->isPointerType())
4074           Ty = Context.getPointerDiffType();
4075         else if (Form == Init || Form == Arithmetic)
4076           Ty = ValType;
4077         else if (Form == Copy || Form == Xchg) {
4078           if (IsPassedByAddress) {
4079             // The value pointer is always dereferenced, a nullptr is undefined.
4080             CheckNonNullArgument(*this, APIOrderedArgs[i],
4081                                  ExprRange.getBegin());
4082           }
4083           Ty = ByValType;
4084         } else {
4085           Expr *ValArg = APIOrderedArgs[i];
4086           // The value pointer is always dereferenced, a nullptr is undefined.
4087           CheckNonNullArgument(*this, ValArg, ExprRange.getBegin());
4088           LangAS AS = LangAS::Default;
4089           // Keep address space of non-atomic pointer type.
4090           if (const PointerType *PtrTy =
4091                   ValArg->getType()->getAs<PointerType>()) {
4092             AS = PtrTy->getPointeeType().getAddressSpace();
4093           }
4094           Ty = Context.getPointerType(
4095               Context.getAddrSpaceQualType(ValType.getUnqualifiedType(), AS));
4096         }
4097         break;
4098       case 2:
4099         // The third argument to compare_exchange / GNU exchange is the desired
4100         // value, either by-value (for the C11 and *_n variant) or as a pointer.
4101         if (IsPassedByAddress)
4102           CheckNonNullArgument(*this, APIOrderedArgs[i], ExprRange.getBegin());
4103         Ty = ByValType;
4104         break;
4105       case 3:
4106         // The fourth argument to GNU compare_exchange is a 'weak' flag.
4107         Ty = Context.BoolTy;
4108         break;
4109       }
4110     } else {
4111       // The order(s) and scope are always converted to int.
4112       Ty = Context.IntTy;
4113     }
4114 
4115     InitializedEntity Entity =
4116         InitializedEntity::InitializeParameter(Context, Ty, false);
4117     ExprResult Arg = APIOrderedArgs[i];
4118     Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
4119     if (Arg.isInvalid())
4120       return true;
4121     APIOrderedArgs[i] = Arg.get();
4122   }
4123 
4124   // Permute the arguments into a 'consistent' order.
4125   SmallVector<Expr*, 5> SubExprs;
4126   SubExprs.push_back(Ptr);
4127   switch (Form) {
4128   case Init:
4129     // Note, AtomicExpr::getVal1() has a special case for this atomic.
4130     SubExprs.push_back(APIOrderedArgs[1]); // Val1
4131     break;
4132   case Load:
4133     SubExprs.push_back(APIOrderedArgs[1]); // Order
4134     break;
4135   case LoadCopy:
4136   case Copy:
4137   case Arithmetic:
4138   case Xchg:
4139     SubExprs.push_back(APIOrderedArgs[2]); // Order
4140     SubExprs.push_back(APIOrderedArgs[1]); // Val1
4141     break;
4142   case GNUXchg:
4143     // Note, AtomicExpr::getVal2() has a special case for this atomic.
4144     SubExprs.push_back(APIOrderedArgs[3]); // Order
4145     SubExprs.push_back(APIOrderedArgs[1]); // Val1
4146     SubExprs.push_back(APIOrderedArgs[2]); // Val2
4147     break;
4148   case C11CmpXchg:
4149     SubExprs.push_back(APIOrderedArgs[3]); // Order
4150     SubExprs.push_back(APIOrderedArgs[1]); // Val1
4151     SubExprs.push_back(APIOrderedArgs[4]); // OrderFail
4152     SubExprs.push_back(APIOrderedArgs[2]); // Val2
4153     break;
4154   case GNUCmpXchg:
4155     SubExprs.push_back(APIOrderedArgs[4]); // Order
4156     SubExprs.push_back(APIOrderedArgs[1]); // Val1
4157     SubExprs.push_back(APIOrderedArgs[5]); // OrderFail
4158     SubExprs.push_back(APIOrderedArgs[2]); // Val2
4159     SubExprs.push_back(APIOrderedArgs[3]); // Weak
4160     break;
4161   }
4162 
4163   // If the memory orders are constants, check they are valid.
4164   if (SubExprs.size() >= 2 && Form != Init) {
4165     std::optional<llvm::APSInt> Success =
4166         SubExprs[1]->getIntegerConstantExpr(Context);
4167     if (Success && !isValidOrderingForOp(Success->getSExtValue(), Op)) {
4168       Diag(SubExprs[1]->getBeginLoc(),
4169            diag::warn_atomic_op_has_invalid_memory_order)
4170           << /*success=*/(Form == C11CmpXchg || Form == GNUCmpXchg)
4171           << SubExprs[1]->getSourceRange();
4172     }
4173     if (SubExprs.size() >= 5) {
4174       if (std::optional<llvm::APSInt> Failure =
4175               SubExprs[3]->getIntegerConstantExpr(Context)) {
4176         if (!llvm::is_contained(
4177                 {llvm::AtomicOrderingCABI::relaxed,
4178                  llvm::AtomicOrderingCABI::consume,
4179                  llvm::AtomicOrderingCABI::acquire,
4180                  llvm::AtomicOrderingCABI::seq_cst},
4181                 (llvm::AtomicOrderingCABI)Failure->getSExtValue())) {
4182           Diag(SubExprs[3]->getBeginLoc(),
4183                diag::warn_atomic_op_has_invalid_memory_order)
4184               << /*failure=*/2 << SubExprs[3]->getSourceRange();
4185         }
4186       }
4187     }
4188   }
4189 
4190   if (auto ScopeModel = AtomicExpr::getScopeModel(Op)) {
4191     auto *Scope = Args[Args.size() - 1];
4192     if (std::optional<llvm::APSInt> Result =
4193             Scope->getIntegerConstantExpr(Context)) {
4194       if (!ScopeModel->isValid(Result->getZExtValue()))
4195         Diag(Scope->getBeginLoc(), diag::err_atomic_op_has_invalid_synch_scope)
4196             << Scope->getSourceRange();
4197     }
4198     SubExprs.push_back(Scope);
4199   }
4200 
4201   AtomicExpr *AE = new (Context)
4202       AtomicExpr(ExprRange.getBegin(), SubExprs, ResultType, Op, RParenLoc);
4203 
4204   if ((Op == AtomicExpr::AO__c11_atomic_load ||
4205        Op == AtomicExpr::AO__c11_atomic_store ||
4206        Op == AtomicExpr::AO__opencl_atomic_load ||
4207        Op == AtomicExpr::AO__hip_atomic_load ||
4208        Op == AtomicExpr::AO__opencl_atomic_store ||
4209        Op == AtomicExpr::AO__hip_atomic_store) &&
4210       Context.AtomicUsesUnsupportedLibcall(AE))
4211     Diag(AE->getBeginLoc(), diag::err_atomic_load_store_uses_lib)
4212         << ((Op == AtomicExpr::AO__c11_atomic_load ||
4213              Op == AtomicExpr::AO__opencl_atomic_load ||
4214              Op == AtomicExpr::AO__hip_atomic_load)
4215                 ? 0
4216                 : 1);
4217 
4218   if (ValType->isBitIntType()) {
4219     Diag(Ptr->getExprLoc(), diag::err_atomic_builtin_bit_int_prohibit);
4220     return ExprError();
4221   }
4222 
4223   return AE;
4224 }
4225 
4226 /// checkBuiltinArgument - Given a call to a builtin function, perform
4227 /// normal type-checking on the given argument, updating the call in
4228 /// place.  This is useful when a builtin function requires custom
4229 /// type-checking for some of its arguments but not necessarily all of
4230 /// them.
4231 ///
4232 /// Returns true on error.
4233 static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) {
4234   FunctionDecl *Fn = E->getDirectCallee();
4235   assert(Fn && "builtin call without direct callee!");
4236 
4237   ParmVarDecl *Param = Fn->getParamDecl(ArgIndex);
4238   InitializedEntity Entity =
4239     InitializedEntity::InitializeParameter(S.Context, Param);
4240 
4241   ExprResult Arg = E->getArg(ArgIndex);
4242   Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg);
4243   if (Arg.isInvalid())
4244     return true;
4245 
4246   E->setArg(ArgIndex, Arg.get());
4247   return false;
4248 }
4249 
4250 ExprResult Sema::BuiltinAtomicOverloaded(ExprResult TheCallResult) {
4251   CallExpr *TheCall = static_cast<CallExpr *>(TheCallResult.get());
4252   Expr *Callee = TheCall->getCallee();
4253   DeclRefExpr *DRE = cast<DeclRefExpr>(Callee->IgnoreParenCasts());
4254   FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
4255 
4256   // Ensure that we have at least one argument to do type inference from.
4257   if (TheCall->getNumArgs() < 1) {
4258     Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least)
4259         << 0 << 1 << TheCall->getNumArgs() << /*is non object*/ 0
4260         << Callee->getSourceRange();
4261     return ExprError();
4262   }
4263 
4264   // Inspect the first argument of the atomic builtin.  This should always be
4265   // a pointer type, whose element is an integral scalar or pointer type.
4266   // Because it is a pointer type, we don't have to worry about any implicit
4267   // casts here.
4268   // FIXME: We don't allow floating point scalars as input.
4269   Expr *FirstArg = TheCall->getArg(0);
4270   ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg);
4271   if (FirstArgResult.isInvalid())
4272     return ExprError();
4273   FirstArg = FirstArgResult.get();
4274   TheCall->setArg(0, FirstArg);
4275 
4276   const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>();
4277   if (!pointerType) {
4278     Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer)
4279         << FirstArg->getType() << 0 << FirstArg->getSourceRange();
4280     return ExprError();
4281   }
4282 
4283   QualType ValType = pointerType->getPointeeType();
4284   if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
4285       !ValType->isBlockPointerType()) {
4286     Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer_intptr)
4287         << FirstArg->getType() << 0 << FirstArg->getSourceRange();
4288     return ExprError();
4289   }
4290 
4291   if (ValType.isConstQualified()) {
4292     Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_cannot_be_const)
4293         << FirstArg->getType() << FirstArg->getSourceRange();
4294     return ExprError();
4295   }
4296 
4297   switch (ValType.getObjCLifetime()) {
4298   case Qualifiers::OCL_None:
4299   case Qualifiers::OCL_ExplicitNone:
4300     // okay
4301     break;
4302 
4303   case Qualifiers::OCL_Weak:
4304   case Qualifiers::OCL_Strong:
4305   case Qualifiers::OCL_Autoreleasing:
4306     Diag(DRE->getBeginLoc(), diag::err_arc_atomic_ownership)
4307         << ValType << FirstArg->getSourceRange();
4308     return ExprError();
4309   }
4310 
4311   // Strip any qualifiers off ValType.
4312   ValType = ValType.getUnqualifiedType();
4313 
4314   // The majority of builtins return a value, but a few have special return
4315   // types, so allow them to override appropriately below.
4316   QualType ResultType = ValType;
4317 
4318   // We need to figure out which concrete builtin this maps onto.  For example,
4319   // __sync_fetch_and_add with a 2 byte object turns into
4320   // __sync_fetch_and_add_2.
4321 #define BUILTIN_ROW(x) \
4322   { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \
4323     Builtin::BI##x##_8, Builtin::BI##x##_16 }
4324 
4325   static const unsigned BuiltinIndices[][5] = {
4326     BUILTIN_ROW(__sync_fetch_and_add),
4327     BUILTIN_ROW(__sync_fetch_and_sub),
4328     BUILTIN_ROW(__sync_fetch_and_or),
4329     BUILTIN_ROW(__sync_fetch_and_and),
4330     BUILTIN_ROW(__sync_fetch_and_xor),
4331     BUILTIN_ROW(__sync_fetch_and_nand),
4332 
4333     BUILTIN_ROW(__sync_add_and_fetch),
4334     BUILTIN_ROW(__sync_sub_and_fetch),
4335     BUILTIN_ROW(__sync_and_and_fetch),
4336     BUILTIN_ROW(__sync_or_and_fetch),
4337     BUILTIN_ROW(__sync_xor_and_fetch),
4338     BUILTIN_ROW(__sync_nand_and_fetch),
4339 
4340     BUILTIN_ROW(__sync_val_compare_and_swap),
4341     BUILTIN_ROW(__sync_bool_compare_and_swap),
4342     BUILTIN_ROW(__sync_lock_test_and_set),
4343     BUILTIN_ROW(__sync_lock_release),
4344     BUILTIN_ROW(__sync_swap)
4345   };
4346 #undef BUILTIN_ROW
4347 
4348   // Determine the index of the size.
4349   unsigned SizeIndex;
4350   switch (Context.getTypeSizeInChars(ValType).getQuantity()) {
4351   case 1: SizeIndex = 0; break;
4352   case 2: SizeIndex = 1; break;
4353   case 4: SizeIndex = 2; break;
4354   case 8: SizeIndex = 3; break;
4355   case 16: SizeIndex = 4; break;
4356   default:
4357     Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_pointer_size)
4358         << FirstArg->getType() << FirstArg->getSourceRange();
4359     return ExprError();
4360   }
4361 
4362   // Each of these builtins has one pointer argument, followed by some number of
4363   // values (0, 1 or 2) followed by a potentially empty varags list of stuff
4364   // that we ignore.  Find out which row of BuiltinIndices to read from as well
4365   // as the number of fixed args.
4366   unsigned BuiltinID = FDecl->getBuiltinID();
4367   unsigned BuiltinIndex, NumFixed = 1;
4368   bool WarnAboutSemanticsChange = false;
4369   switch (BuiltinID) {
4370   default: llvm_unreachable("Unknown overloaded atomic builtin!");
4371   case Builtin::BI__sync_fetch_and_add:
4372   case Builtin::BI__sync_fetch_and_add_1:
4373   case Builtin::BI__sync_fetch_and_add_2:
4374   case Builtin::BI__sync_fetch_and_add_4:
4375   case Builtin::BI__sync_fetch_and_add_8:
4376   case Builtin::BI__sync_fetch_and_add_16:
4377     BuiltinIndex = 0;
4378     break;
4379 
4380   case Builtin::BI__sync_fetch_and_sub:
4381   case Builtin::BI__sync_fetch_and_sub_1:
4382   case Builtin::BI__sync_fetch_and_sub_2:
4383   case Builtin::BI__sync_fetch_and_sub_4:
4384   case Builtin::BI__sync_fetch_and_sub_8:
4385   case Builtin::BI__sync_fetch_and_sub_16:
4386     BuiltinIndex = 1;
4387     break;
4388 
4389   case Builtin::BI__sync_fetch_and_or:
4390   case Builtin::BI__sync_fetch_and_or_1:
4391   case Builtin::BI__sync_fetch_and_or_2:
4392   case Builtin::BI__sync_fetch_and_or_4:
4393   case Builtin::BI__sync_fetch_and_or_8:
4394   case Builtin::BI__sync_fetch_and_or_16:
4395     BuiltinIndex = 2;
4396     break;
4397 
4398   case Builtin::BI__sync_fetch_and_and:
4399   case Builtin::BI__sync_fetch_and_and_1:
4400   case Builtin::BI__sync_fetch_and_and_2:
4401   case Builtin::BI__sync_fetch_and_and_4:
4402   case Builtin::BI__sync_fetch_and_and_8:
4403   case Builtin::BI__sync_fetch_and_and_16:
4404     BuiltinIndex = 3;
4405     break;
4406 
4407   case Builtin::BI__sync_fetch_and_xor:
4408   case Builtin::BI__sync_fetch_and_xor_1:
4409   case Builtin::BI__sync_fetch_and_xor_2:
4410   case Builtin::BI__sync_fetch_and_xor_4:
4411   case Builtin::BI__sync_fetch_and_xor_8:
4412   case Builtin::BI__sync_fetch_and_xor_16:
4413     BuiltinIndex = 4;
4414     break;
4415 
4416   case Builtin::BI__sync_fetch_and_nand:
4417   case Builtin::BI__sync_fetch_and_nand_1:
4418   case Builtin::BI__sync_fetch_and_nand_2:
4419   case Builtin::BI__sync_fetch_and_nand_4:
4420   case Builtin::BI__sync_fetch_and_nand_8:
4421   case Builtin::BI__sync_fetch_and_nand_16:
4422     BuiltinIndex = 5;
4423     WarnAboutSemanticsChange = true;
4424     break;
4425 
4426   case Builtin::BI__sync_add_and_fetch:
4427   case Builtin::BI__sync_add_and_fetch_1:
4428   case Builtin::BI__sync_add_and_fetch_2:
4429   case Builtin::BI__sync_add_and_fetch_4:
4430   case Builtin::BI__sync_add_and_fetch_8:
4431   case Builtin::BI__sync_add_and_fetch_16:
4432     BuiltinIndex = 6;
4433     break;
4434 
4435   case Builtin::BI__sync_sub_and_fetch:
4436   case Builtin::BI__sync_sub_and_fetch_1:
4437   case Builtin::BI__sync_sub_and_fetch_2:
4438   case Builtin::BI__sync_sub_and_fetch_4:
4439   case Builtin::BI__sync_sub_and_fetch_8:
4440   case Builtin::BI__sync_sub_and_fetch_16:
4441     BuiltinIndex = 7;
4442     break;
4443 
4444   case Builtin::BI__sync_and_and_fetch:
4445   case Builtin::BI__sync_and_and_fetch_1:
4446   case Builtin::BI__sync_and_and_fetch_2:
4447   case Builtin::BI__sync_and_and_fetch_4:
4448   case Builtin::BI__sync_and_and_fetch_8:
4449   case Builtin::BI__sync_and_and_fetch_16:
4450     BuiltinIndex = 8;
4451     break;
4452 
4453   case Builtin::BI__sync_or_and_fetch:
4454   case Builtin::BI__sync_or_and_fetch_1:
4455   case Builtin::BI__sync_or_and_fetch_2:
4456   case Builtin::BI__sync_or_and_fetch_4:
4457   case Builtin::BI__sync_or_and_fetch_8:
4458   case Builtin::BI__sync_or_and_fetch_16:
4459     BuiltinIndex = 9;
4460     break;
4461 
4462   case Builtin::BI__sync_xor_and_fetch:
4463   case Builtin::BI__sync_xor_and_fetch_1:
4464   case Builtin::BI__sync_xor_and_fetch_2:
4465   case Builtin::BI__sync_xor_and_fetch_4:
4466   case Builtin::BI__sync_xor_and_fetch_8:
4467   case Builtin::BI__sync_xor_and_fetch_16:
4468     BuiltinIndex = 10;
4469     break;
4470 
4471   case Builtin::BI__sync_nand_and_fetch:
4472   case Builtin::BI__sync_nand_and_fetch_1:
4473   case Builtin::BI__sync_nand_and_fetch_2:
4474   case Builtin::BI__sync_nand_and_fetch_4:
4475   case Builtin::BI__sync_nand_and_fetch_8:
4476   case Builtin::BI__sync_nand_and_fetch_16:
4477     BuiltinIndex = 11;
4478     WarnAboutSemanticsChange = true;
4479     break;
4480 
4481   case Builtin::BI__sync_val_compare_and_swap:
4482   case Builtin::BI__sync_val_compare_and_swap_1:
4483   case Builtin::BI__sync_val_compare_and_swap_2:
4484   case Builtin::BI__sync_val_compare_and_swap_4:
4485   case Builtin::BI__sync_val_compare_and_swap_8:
4486   case Builtin::BI__sync_val_compare_and_swap_16:
4487     BuiltinIndex = 12;
4488     NumFixed = 2;
4489     break;
4490 
4491   case Builtin::BI__sync_bool_compare_and_swap:
4492   case Builtin::BI__sync_bool_compare_and_swap_1:
4493   case Builtin::BI__sync_bool_compare_and_swap_2:
4494   case Builtin::BI__sync_bool_compare_and_swap_4:
4495   case Builtin::BI__sync_bool_compare_and_swap_8:
4496   case Builtin::BI__sync_bool_compare_and_swap_16:
4497     BuiltinIndex = 13;
4498     NumFixed = 2;
4499     ResultType = Context.BoolTy;
4500     break;
4501 
4502   case Builtin::BI__sync_lock_test_and_set:
4503   case Builtin::BI__sync_lock_test_and_set_1:
4504   case Builtin::BI__sync_lock_test_and_set_2:
4505   case Builtin::BI__sync_lock_test_and_set_4:
4506   case Builtin::BI__sync_lock_test_and_set_8:
4507   case Builtin::BI__sync_lock_test_and_set_16:
4508     BuiltinIndex = 14;
4509     break;
4510 
4511   case Builtin::BI__sync_lock_release:
4512   case Builtin::BI__sync_lock_release_1:
4513   case Builtin::BI__sync_lock_release_2:
4514   case Builtin::BI__sync_lock_release_4:
4515   case Builtin::BI__sync_lock_release_8:
4516   case Builtin::BI__sync_lock_release_16:
4517     BuiltinIndex = 15;
4518     NumFixed = 0;
4519     ResultType = Context.VoidTy;
4520     break;
4521 
4522   case Builtin::BI__sync_swap:
4523   case Builtin::BI__sync_swap_1:
4524   case Builtin::BI__sync_swap_2:
4525   case Builtin::BI__sync_swap_4:
4526   case Builtin::BI__sync_swap_8:
4527   case Builtin::BI__sync_swap_16:
4528     BuiltinIndex = 16;
4529     break;
4530   }
4531 
4532   // Now that we know how many fixed arguments we expect, first check that we
4533   // have at least that many.
4534   if (TheCall->getNumArgs() < 1+NumFixed) {
4535     Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least)
4536         << 0 << 1 + NumFixed << TheCall->getNumArgs() << /*is non object*/ 0
4537         << Callee->getSourceRange();
4538     return ExprError();
4539   }
4540 
4541   Diag(TheCall->getEndLoc(), diag::warn_atomic_implicit_seq_cst)
4542       << Callee->getSourceRange();
4543 
4544   if (WarnAboutSemanticsChange) {
4545     Diag(TheCall->getEndLoc(), diag::warn_sync_fetch_and_nand_semantics_change)
4546         << Callee->getSourceRange();
4547   }
4548 
4549   // Get the decl for the concrete builtin from this, we can tell what the
4550   // concrete integer type we should convert to is.
4551   unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex];
4552   StringRef NewBuiltinName = Context.BuiltinInfo.getName(NewBuiltinID);
4553   FunctionDecl *NewBuiltinDecl;
4554   if (NewBuiltinID == BuiltinID)
4555     NewBuiltinDecl = FDecl;
4556   else {
4557     // Perform builtin lookup to avoid redeclaring it.
4558     DeclarationName DN(&Context.Idents.get(NewBuiltinName));
4559     LookupResult Res(*this, DN, DRE->getBeginLoc(), LookupOrdinaryName);
4560     LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true);
4561     assert(Res.getFoundDecl());
4562     NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl());
4563     if (!NewBuiltinDecl)
4564       return ExprError();
4565   }
4566 
4567   // The first argument --- the pointer --- has a fixed type; we
4568   // deduce the types of the rest of the arguments accordingly.  Walk
4569   // the remaining arguments, converting them to the deduced value type.
4570   for (unsigned i = 0; i != NumFixed; ++i) {
4571     ExprResult Arg = TheCall->getArg(i+1);
4572 
4573     // GCC does an implicit conversion to the pointer or integer ValType.  This
4574     // can fail in some cases (1i -> int**), check for this error case now.
4575     // Initialize the argument.
4576     InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
4577                                                    ValType, /*consume*/ false);
4578     Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
4579     if (Arg.isInvalid())
4580       return ExprError();
4581 
4582     // Okay, we have something that *can* be converted to the right type.  Check
4583     // to see if there is a potentially weird extension going on here.  This can
4584     // happen when you do an atomic operation on something like an char* and
4585     // pass in 42.  The 42 gets converted to char.  This is even more strange
4586     // for things like 45.123 -> char, etc.
4587     // FIXME: Do this check.
4588     TheCall->setArg(i+1, Arg.get());
4589   }
4590 
4591   // Create a new DeclRefExpr to refer to the new decl.
4592   DeclRefExpr *NewDRE = DeclRefExpr::Create(
4593       Context, DRE->getQualifierLoc(), SourceLocation(), NewBuiltinDecl,
4594       /*enclosing*/ false, DRE->getLocation(), Context.BuiltinFnTy,
4595       DRE->getValueKind(), nullptr, nullptr, DRE->isNonOdrUse());
4596 
4597   // Set the callee in the CallExpr.
4598   // FIXME: This loses syntactic information.
4599   QualType CalleePtrTy = Context.getPointerType(NewBuiltinDecl->getType());
4600   ExprResult PromotedCall = ImpCastExprToType(NewDRE, CalleePtrTy,
4601                                               CK_BuiltinFnToFnPtr);
4602   TheCall->setCallee(PromotedCall.get());
4603 
4604   // Change the result type of the call to match the original value type. This
4605   // is arbitrary, but the codegen for these builtins ins design to handle it
4606   // gracefully.
4607   TheCall->setType(ResultType);
4608 
4609   // Prohibit problematic uses of bit-precise integer types with atomic
4610   // builtins. The arguments would have already been converted to the first
4611   // argument's type, so only need to check the first argument.
4612   const auto *BitIntValType = ValType->getAs<BitIntType>();
4613   if (BitIntValType && !llvm::isPowerOf2_64(BitIntValType->getNumBits())) {
4614     Diag(FirstArg->getExprLoc(), diag::err_atomic_builtin_ext_int_size);
4615     return ExprError();
4616   }
4617 
4618   return TheCallResult;
4619 }
4620 
4621 ExprResult Sema::BuiltinNontemporalOverloaded(ExprResult TheCallResult) {
4622   CallExpr *TheCall = (CallExpr *)TheCallResult.get();
4623   DeclRefExpr *DRE =
4624       cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
4625   FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
4626   unsigned BuiltinID = FDecl->getBuiltinID();
4627   assert((BuiltinID == Builtin::BI__builtin_nontemporal_store ||
4628           BuiltinID == Builtin::BI__builtin_nontemporal_load) &&
4629          "Unexpected nontemporal load/store builtin!");
4630   bool isStore = BuiltinID == Builtin::BI__builtin_nontemporal_store;
4631   unsigned numArgs = isStore ? 2 : 1;
4632 
4633   // Ensure that we have the proper number of arguments.
4634   if (checkArgCount(TheCall, numArgs))
4635     return ExprError();
4636 
4637   // Inspect the last argument of the nontemporal builtin.  This should always
4638   // be a pointer type, from which we imply the type of the memory access.
4639   // Because it is a pointer type, we don't have to worry about any implicit
4640   // casts here.
4641   Expr *PointerArg = TheCall->getArg(numArgs - 1);
4642   ExprResult PointerArgResult =
4643       DefaultFunctionArrayLvalueConversion(PointerArg);
4644 
4645   if (PointerArgResult.isInvalid())
4646     return ExprError();
4647   PointerArg = PointerArgResult.get();
4648   TheCall->setArg(numArgs - 1, PointerArg);
4649 
4650   const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>();
4651   if (!pointerType) {
4652     Diag(DRE->getBeginLoc(), diag::err_nontemporal_builtin_must_be_pointer)
4653         << PointerArg->getType() << PointerArg->getSourceRange();
4654     return ExprError();
4655   }
4656 
4657   QualType ValType = pointerType->getPointeeType();
4658 
4659   // Strip any qualifiers off ValType.
4660   ValType = ValType.getUnqualifiedType();
4661   if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
4662       !ValType->isBlockPointerType() && !ValType->isFloatingType() &&
4663       !ValType->isVectorType()) {
4664     Diag(DRE->getBeginLoc(),
4665          diag::err_nontemporal_builtin_must_be_pointer_intfltptr_or_vector)
4666         << PointerArg->getType() << PointerArg->getSourceRange();
4667     return ExprError();
4668   }
4669 
4670   if (!isStore) {
4671     TheCall->setType(ValType);
4672     return TheCallResult;
4673   }
4674 
4675   ExprResult ValArg = TheCall->getArg(0);
4676   InitializedEntity Entity = InitializedEntity::InitializeParameter(
4677       Context, ValType, /*consume*/ false);
4678   ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg);
4679   if (ValArg.isInvalid())
4680     return ExprError();
4681 
4682   TheCall->setArg(0, ValArg.get());
4683   TheCall->setType(Context.VoidTy);
4684   return TheCallResult;
4685 }
4686 
4687 /// CheckObjCString - Checks that the format string argument to the os_log()
4688 /// and os_trace() functions is correct, and converts it to const char *.
4689 ExprResult Sema::CheckOSLogFormatStringArg(Expr *Arg) {
4690   Arg = Arg->IgnoreParenCasts();
4691   auto *Literal = dyn_cast<StringLiteral>(Arg);
4692   if (!Literal) {
4693     if (auto *ObjcLiteral = dyn_cast<ObjCStringLiteral>(Arg)) {
4694       Literal = ObjcLiteral->getString();
4695     }
4696   }
4697 
4698   if (!Literal || (!Literal->isOrdinary() && !Literal->isUTF8())) {
4699     return ExprError(
4700         Diag(Arg->getBeginLoc(), diag::err_os_log_format_not_string_constant)
4701         << Arg->getSourceRange());
4702   }
4703 
4704   ExprResult Result(Literal);
4705   QualType ResultTy = Context.getPointerType(Context.CharTy.withConst());
4706   InitializedEntity Entity =
4707       InitializedEntity::InitializeParameter(Context, ResultTy, false);
4708   Result = PerformCopyInitialization(Entity, SourceLocation(), Result);
4709   return Result;
4710 }
4711 
4712 /// Check that the user is calling the appropriate va_start builtin for the
4713 /// target and calling convention.
4714 static bool checkVAStartABI(Sema &S, unsigned BuiltinID, Expr *Fn) {
4715   const llvm::Triple &TT = S.Context.getTargetInfo().getTriple();
4716   bool IsX64 = TT.getArch() == llvm::Triple::x86_64;
4717   bool IsAArch64 = (TT.getArch() == llvm::Triple::aarch64 ||
4718                     TT.getArch() == llvm::Triple::aarch64_32);
4719   bool IsWindows = TT.isOSWindows();
4720   bool IsMSVAStart = BuiltinID == Builtin::BI__builtin_ms_va_start;
4721   if (IsX64 || IsAArch64) {
4722     CallingConv CC = CC_C;
4723     if (const FunctionDecl *FD = S.getCurFunctionDecl())
4724       CC = FD->getType()->castAs<FunctionType>()->getCallConv();
4725     if (IsMSVAStart) {
4726       // Don't allow this in System V ABI functions.
4727       if (CC == CC_X86_64SysV || (!IsWindows && CC != CC_Win64))
4728         return S.Diag(Fn->getBeginLoc(),
4729                       diag::err_ms_va_start_used_in_sysv_function);
4730     } else {
4731       // On x86-64/AArch64 Unix, don't allow this in Win64 ABI functions.
4732       // On x64 Windows, don't allow this in System V ABI functions.
4733       // (Yes, that means there's no corresponding way to support variadic
4734       // System V ABI functions on Windows.)
4735       if ((IsWindows && CC == CC_X86_64SysV) ||
4736           (!IsWindows && CC == CC_Win64))
4737         return S.Diag(Fn->getBeginLoc(),
4738                       diag::err_va_start_used_in_wrong_abi_function)
4739                << !IsWindows;
4740     }
4741     return false;
4742   }
4743 
4744   if (IsMSVAStart)
4745     return S.Diag(Fn->getBeginLoc(), diag::err_builtin_x64_aarch64_only);
4746   return false;
4747 }
4748 
4749 static bool checkVAStartIsInVariadicFunction(Sema &S, Expr *Fn,
4750                                              ParmVarDecl **LastParam = nullptr) {
4751   // Determine whether the current function, block, or obj-c method is variadic
4752   // and get its parameter list.
4753   bool IsVariadic = false;
4754   ArrayRef<ParmVarDecl *> Params;
4755   DeclContext *Caller = S.CurContext;
4756   if (auto *Block = dyn_cast<BlockDecl>(Caller)) {
4757     IsVariadic = Block->isVariadic();
4758     Params = Block->parameters();
4759   } else if (auto *FD = dyn_cast<FunctionDecl>(Caller)) {
4760     IsVariadic = FD->isVariadic();
4761     Params = FD->parameters();
4762   } else if (auto *MD = dyn_cast<ObjCMethodDecl>(Caller)) {
4763     IsVariadic = MD->isVariadic();
4764     // FIXME: This isn't correct for methods (results in bogus warning).
4765     Params = MD->parameters();
4766   } else if (isa<CapturedDecl>(Caller)) {
4767     // We don't support va_start in a CapturedDecl.
4768     S.Diag(Fn->getBeginLoc(), diag::err_va_start_captured_stmt);
4769     return true;
4770   } else {
4771     // This must be some other declcontext that parses exprs.
4772     S.Diag(Fn->getBeginLoc(), diag::err_va_start_outside_function);
4773     return true;
4774   }
4775 
4776   if (!IsVariadic) {
4777     S.Diag(Fn->getBeginLoc(), diag::err_va_start_fixed_function);
4778     return true;
4779   }
4780 
4781   if (LastParam)
4782     *LastParam = Params.empty() ? nullptr : Params.back();
4783 
4784   return false;
4785 }
4786 
4787 bool Sema::BuiltinVAStart(unsigned BuiltinID, CallExpr *TheCall) {
4788   Expr *Fn = TheCall->getCallee();
4789 
4790   if (checkVAStartABI(*this, BuiltinID, Fn))
4791     return true;
4792 
4793   // In C23 mode, va_start only needs one argument. However, the builtin still
4794   // requires two arguments (which matches the behavior of the GCC builtin),
4795   // <stdarg.h> passes `0` as the second argument in C23 mode.
4796   if (checkArgCount(TheCall, 2))
4797     return true;
4798 
4799   // Type-check the first argument normally.
4800   if (checkBuiltinArgument(*this, TheCall, 0))
4801     return true;
4802 
4803   // Check that the current function is variadic, and get its last parameter.
4804   ParmVarDecl *LastParam;
4805   if (checkVAStartIsInVariadicFunction(*this, Fn, &LastParam))
4806     return true;
4807 
4808   // Verify that the second argument to the builtin is the last argument of the
4809   // current function or method. In C23 mode, if the second argument is an
4810   // integer constant expression with value 0, then we don't bother with this
4811   // check.
4812   bool SecondArgIsLastNamedArgument = false;
4813   const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts();
4814   if (std::optional<llvm::APSInt> Val =
4815           TheCall->getArg(1)->getIntegerConstantExpr(Context);
4816       Val && LangOpts.C23 && *Val == 0)
4817     return false;
4818 
4819   // These are valid if SecondArgIsLastNamedArgument is false after the next
4820   // block.
4821   QualType Type;
4822   SourceLocation ParamLoc;
4823   bool IsCRegister = false;
4824 
4825   if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) {
4826     if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
4827       SecondArgIsLastNamedArgument = PV == LastParam;
4828 
4829       Type = PV->getType();
4830       ParamLoc = PV->getLocation();
4831       IsCRegister =
4832           PV->getStorageClass() == SC_Register && !getLangOpts().CPlusPlus;
4833     }
4834   }
4835 
4836   if (!SecondArgIsLastNamedArgument)
4837     Diag(TheCall->getArg(1)->getBeginLoc(),
4838          diag::warn_second_arg_of_va_start_not_last_named_param);
4839   else if (IsCRegister || Type->isReferenceType() ||
4840            Type->isSpecificBuiltinType(BuiltinType::Float) || [=] {
4841              // Promotable integers are UB, but enumerations need a bit of
4842              // extra checking to see what their promotable type actually is.
4843              if (!Context.isPromotableIntegerType(Type))
4844                return false;
4845              if (!Type->isEnumeralType())
4846                return true;
4847              const EnumDecl *ED = Type->castAs<EnumType>()->getDecl();
4848              return !(ED &&
4849                       Context.typesAreCompatible(ED->getPromotionType(), Type));
4850            }()) {
4851     unsigned Reason = 0;
4852     if (Type->isReferenceType())  Reason = 1;
4853     else if (IsCRegister)         Reason = 2;
4854     Diag(Arg->getBeginLoc(), diag::warn_va_start_type_is_undefined) << Reason;
4855     Diag(ParamLoc, diag::note_parameter_type) << Type;
4856   }
4857 
4858   return false;
4859 }
4860 
4861 bool Sema::BuiltinVAStartARMMicrosoft(CallExpr *Call) {
4862   auto IsSuitablyTypedFormatArgument = [this](const Expr *Arg) -> bool {
4863     const LangOptions &LO = getLangOpts();
4864 
4865     if (LO.CPlusPlus)
4866       return Arg->getType()
4867                  .getCanonicalType()
4868                  .getTypePtr()
4869                  ->getPointeeType()
4870                  .withoutLocalFastQualifiers() == Context.CharTy;
4871 
4872     // In C, allow aliasing through `char *`, this is required for AArch64 at
4873     // least.
4874     return true;
4875   };
4876 
4877   // void __va_start(va_list *ap, const char *named_addr, size_t slot_size,
4878   //                 const char *named_addr);
4879 
4880   Expr *Func = Call->getCallee();
4881 
4882   if (Call->getNumArgs() < 3)
4883     return Diag(Call->getEndLoc(),
4884                 diag::err_typecheck_call_too_few_args_at_least)
4885            << 0 /*function call*/ << 3 << Call->getNumArgs()
4886            << /*is non object*/ 0;
4887 
4888   // Type-check the first argument normally.
4889   if (checkBuiltinArgument(*this, Call, 0))
4890     return true;
4891 
4892   // Check that the current function is variadic.
4893   if (checkVAStartIsInVariadicFunction(*this, Func))
4894     return true;
4895 
4896   // __va_start on Windows does not validate the parameter qualifiers
4897 
4898   const Expr *Arg1 = Call->getArg(1)->IgnoreParens();
4899   const Type *Arg1Ty = Arg1->getType().getCanonicalType().getTypePtr();
4900 
4901   const Expr *Arg2 = Call->getArg(2)->IgnoreParens();
4902   const Type *Arg2Ty = Arg2->getType().getCanonicalType().getTypePtr();
4903 
4904   const QualType &ConstCharPtrTy =
4905       Context.getPointerType(Context.CharTy.withConst());
4906   if (!Arg1Ty->isPointerType() || !IsSuitablyTypedFormatArgument(Arg1))
4907     Diag(Arg1->getBeginLoc(), diag::err_typecheck_convert_incompatible)
4908         << Arg1->getType() << ConstCharPtrTy << 1 /* different class */
4909         << 0                                      /* qualifier difference */
4910         << 3                                      /* parameter mismatch */
4911         << 2 << Arg1->getType() << ConstCharPtrTy;
4912 
4913   const QualType SizeTy = Context.getSizeType();
4914   if (Arg2Ty->getCanonicalTypeInternal().withoutLocalFastQualifiers() != SizeTy)
4915     Diag(Arg2->getBeginLoc(), diag::err_typecheck_convert_incompatible)
4916         << Arg2->getType() << SizeTy << 1 /* different class */
4917         << 0                              /* qualifier difference */
4918         << 3                              /* parameter mismatch */
4919         << 3 << Arg2->getType() << SizeTy;
4920 
4921   return false;
4922 }
4923 
4924 bool Sema::BuiltinUnorderedCompare(CallExpr *TheCall, unsigned BuiltinID) {
4925   if (checkArgCount(TheCall, 2))
4926     return true;
4927 
4928   if (BuiltinID == Builtin::BI__builtin_isunordered &&
4929       TheCall->getFPFeaturesInEffect(getLangOpts()).getNoHonorNaNs())
4930     Diag(TheCall->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled)
4931         << 1 << 0 << TheCall->getSourceRange();
4932 
4933   ExprResult OrigArg0 = TheCall->getArg(0);
4934   ExprResult OrigArg1 = TheCall->getArg(1);
4935 
4936   // Do standard promotions between the two arguments, returning their common
4937   // type.
4938   QualType Res = UsualArithmeticConversions(
4939       OrigArg0, OrigArg1, TheCall->getExprLoc(), ACK_Comparison);
4940   if (OrigArg0.isInvalid() || OrigArg1.isInvalid())
4941     return true;
4942 
4943   // Make sure any conversions are pushed back into the call; this is
4944   // type safe since unordered compare builtins are declared as "_Bool
4945   // foo(...)".
4946   TheCall->setArg(0, OrigArg0.get());
4947   TheCall->setArg(1, OrigArg1.get());
4948 
4949   if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent())
4950     return false;
4951 
4952   // If the common type isn't a real floating type, then the arguments were
4953   // invalid for this operation.
4954   if (Res.isNull() || !Res->isRealFloatingType())
4955     return Diag(OrigArg0.get()->getBeginLoc(),
4956                 diag::err_typecheck_call_invalid_ordered_compare)
4957            << OrigArg0.get()->getType() << OrigArg1.get()->getType()
4958            << SourceRange(OrigArg0.get()->getBeginLoc(),
4959                           OrigArg1.get()->getEndLoc());
4960 
4961   return false;
4962 }
4963 
4964 bool Sema::BuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs,
4965                                    unsigned BuiltinID) {
4966   if (checkArgCount(TheCall, NumArgs))
4967     return true;
4968 
4969   FPOptions FPO = TheCall->getFPFeaturesInEffect(getLangOpts());
4970   if (FPO.getNoHonorInfs() && (BuiltinID == Builtin::BI__builtin_isfinite ||
4971                                BuiltinID == Builtin::BI__builtin_isinf ||
4972                                BuiltinID == Builtin::BI__builtin_isinf_sign))
4973     Diag(TheCall->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled)
4974         << 0 << 0 << TheCall->getSourceRange();
4975 
4976   if (FPO.getNoHonorNaNs() && (BuiltinID == Builtin::BI__builtin_isnan ||
4977                                BuiltinID == Builtin::BI__builtin_isunordered))
4978     Diag(TheCall->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled)
4979         << 1 << 0 << TheCall->getSourceRange();
4980 
4981   bool IsFPClass = NumArgs == 2;
4982 
4983   // Find out position of floating-point argument.
4984   unsigned FPArgNo = IsFPClass ? 0 : NumArgs - 1;
4985 
4986   // We can count on all parameters preceding the floating-point just being int.
4987   // Try all of those.
4988   for (unsigned i = 0; i < FPArgNo; ++i) {
4989     Expr *Arg = TheCall->getArg(i);
4990 
4991     if (Arg->isTypeDependent())
4992       return false;
4993 
4994     ExprResult Res = PerformImplicitConversion(Arg, Context.IntTy,
4995                                                AssignmentAction::Passing);
4996 
4997     if (Res.isInvalid())
4998       return true;
4999     TheCall->setArg(i, Res.get());
5000   }
5001 
5002   Expr *OrigArg = TheCall->getArg(FPArgNo);
5003 
5004   if (OrigArg->isTypeDependent())
5005     return false;
5006 
5007   // Usual Unary Conversions will convert half to float, which we want for
5008   // machines that use fp16 conversion intrinsics. Else, we wnat to leave the
5009   // type how it is, but do normal L->Rvalue conversions.
5010   if (Context.getTargetInfo().useFP16ConversionIntrinsics()) {
5011     ExprResult Res = UsualUnaryConversions(OrigArg);
5012 
5013     if (!Res.isUsable())
5014       return true;
5015     OrigArg = Res.get();
5016   } else {
5017     ExprResult Res = DefaultFunctionArrayLvalueConversion(OrigArg);
5018 
5019     if (!Res.isUsable())
5020       return true;
5021     OrigArg = Res.get();
5022   }
5023   TheCall->setArg(FPArgNo, OrigArg);
5024 
5025   QualType VectorResultTy;
5026   QualType ElementTy = OrigArg->getType();
5027   // TODO: When all classification function are implemented with is_fpclass,
5028   // vector argument can be supported in all of them.
5029   if (ElementTy->isVectorType() && IsFPClass) {
5030     VectorResultTy = GetSignedVectorType(ElementTy);
5031     ElementTy = ElementTy->castAs<VectorType>()->getElementType();
5032   }
5033 
5034   // This operation requires a non-_Complex floating-point number.
5035   if (!ElementTy->isRealFloatingType())
5036     return Diag(OrigArg->getBeginLoc(),
5037                 diag::err_typecheck_call_invalid_unary_fp)
5038            << OrigArg->getType() << OrigArg->getSourceRange();
5039 
5040   // __builtin_isfpclass has integer parameter that specify test mask. It is
5041   // passed in (...), so it should be analyzed completely here.
5042   if (IsFPClass)
5043     if (BuiltinConstantArgRange(TheCall, 1, 0, llvm::fcAllFlags))
5044       return true;
5045 
5046   // TODO: enable this code to all classification functions.
5047   if (IsFPClass) {
5048     QualType ResultTy;
5049     if (!VectorResultTy.isNull())
5050       ResultTy = VectorResultTy;
5051     else
5052       ResultTy = Context.IntTy;
5053     TheCall->setType(ResultTy);
5054   }
5055 
5056   return false;
5057 }
5058 
5059 bool Sema::BuiltinComplex(CallExpr *TheCall) {
5060   if (checkArgCount(TheCall, 2))
5061     return true;
5062 
5063   bool Dependent = false;
5064   for (unsigned I = 0; I != 2; ++I) {
5065     Expr *Arg = TheCall->getArg(I);
5066     QualType T = Arg->getType();
5067     if (T->isDependentType()) {
5068       Dependent = true;
5069       continue;
5070     }
5071 
5072     // Despite supporting _Complex int, GCC requires a real floating point type
5073     // for the operands of __builtin_complex.
5074     if (!T->isRealFloatingType()) {
5075       return Diag(Arg->getBeginLoc(), diag::err_typecheck_call_requires_real_fp)
5076              << Arg->getType() << Arg->getSourceRange();
5077     }
5078 
5079     ExprResult Converted = DefaultLvalueConversion(Arg);
5080     if (Converted.isInvalid())
5081       return true;
5082     TheCall->setArg(I, Converted.get());
5083   }
5084 
5085   if (Dependent) {
5086     TheCall->setType(Context.DependentTy);
5087     return false;
5088   }
5089 
5090   Expr *Real = TheCall->getArg(0);
5091   Expr *Imag = TheCall->getArg(1);
5092   if (!Context.hasSameType(Real->getType(), Imag->getType())) {
5093     return Diag(Real->getBeginLoc(),
5094                 diag::err_typecheck_call_different_arg_types)
5095            << Real->getType() << Imag->getType()
5096            << Real->getSourceRange() << Imag->getSourceRange();
5097   }
5098 
5099   // We don't allow _Complex _Float16 nor _Complex __fp16 as type specifiers;
5100   // don't allow this builtin to form those types either.
5101   // FIXME: Should we allow these types?
5102   if (Real->getType()->isFloat16Type())
5103     return Diag(TheCall->getBeginLoc(), diag::err_invalid_complex_spec)
5104            << "_Float16";
5105   if (Real->getType()->isHalfType())
5106     return Diag(TheCall->getBeginLoc(), diag::err_invalid_complex_spec)
5107            << "half";
5108 
5109   TheCall->setType(Context.getComplexType(Real->getType()));
5110   return false;
5111 }
5112 
5113 /// BuiltinShuffleVector - Handle __builtin_shufflevector.
5114 // This is declared to take (...), so we have to check everything.
5115 ExprResult Sema::BuiltinShuffleVector(CallExpr *TheCall) {
5116   if (TheCall->getNumArgs() < 2)
5117     return ExprError(Diag(TheCall->getEndLoc(),
5118                           diag::err_typecheck_call_too_few_args_at_least)
5119                      << 0 /*function call*/ << 2 << TheCall->getNumArgs()
5120                      << /*is non object*/ 0 << TheCall->getSourceRange());
5121 
5122   // Determine which of the following types of shufflevector we're checking:
5123   // 1) unary, vector mask: (lhs, mask)
5124   // 2) binary, scalar mask: (lhs, rhs, index, ..., index)
5125   QualType resType = TheCall->getArg(0)->getType();
5126   unsigned numElements = 0;
5127 
5128   if (!TheCall->getArg(0)->isTypeDependent() &&
5129       !TheCall->getArg(1)->isTypeDependent()) {
5130     QualType LHSType = TheCall->getArg(0)->getType();
5131     QualType RHSType = TheCall->getArg(1)->getType();
5132 
5133     if (!LHSType->isVectorType() || !RHSType->isVectorType())
5134       return ExprError(
5135           Diag(TheCall->getBeginLoc(), diag::err_vec_builtin_non_vector)
5136           << TheCall->getDirectCallee() << /*isMorethantwoArgs*/ false
5137           << SourceRange(TheCall->getArg(0)->getBeginLoc(),
5138                          TheCall->getArg(1)->getEndLoc()));
5139 
5140     numElements = LHSType->castAs<VectorType>()->getNumElements();
5141     unsigned numResElements = TheCall->getNumArgs() - 2;
5142 
5143     // Check to see if we have a call with 2 vector arguments, the unary shuffle
5144     // with mask.  If so, verify that RHS is an integer vector type with the
5145     // same number of elts as lhs.
5146     if (TheCall->getNumArgs() == 2) {
5147       if (!RHSType->hasIntegerRepresentation() ||
5148           RHSType->castAs<VectorType>()->getNumElements() != numElements)
5149         return ExprError(Diag(TheCall->getBeginLoc(),
5150                               diag::err_vec_builtin_incompatible_vector)
5151                          << TheCall->getDirectCallee()
5152                          << /*isMorethantwoArgs*/ false
5153                          << SourceRange(TheCall->getArg(1)->getBeginLoc(),
5154                                         TheCall->getArg(1)->getEndLoc()));
5155     } else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) {
5156       return ExprError(Diag(TheCall->getBeginLoc(),
5157                             diag::err_vec_builtin_incompatible_vector)
5158                        << TheCall->getDirectCallee()
5159                        << /*isMorethantwoArgs*/ false
5160                        << SourceRange(TheCall->getArg(0)->getBeginLoc(),
5161                                       TheCall->getArg(1)->getEndLoc()));
5162     } else if (numElements != numResElements) {
5163       QualType eltType = LHSType->castAs<VectorType>()->getElementType();
5164       resType =
5165           Context.getVectorType(eltType, numResElements, VectorKind::Generic);
5166     }
5167   }
5168 
5169   for (unsigned i = 2; i < TheCall->getNumArgs(); i++) {
5170     if (TheCall->getArg(i)->isTypeDependent() ||
5171         TheCall->getArg(i)->isValueDependent())
5172       continue;
5173 
5174     std::optional<llvm::APSInt> Result;
5175     if (!(Result = TheCall->getArg(i)->getIntegerConstantExpr(Context)))
5176       return ExprError(Diag(TheCall->getBeginLoc(),
5177                             diag::err_shufflevector_nonconstant_argument)
5178                        << TheCall->getArg(i)->getSourceRange());
5179 
5180     // Allow -1 which will be translated to undef in the IR.
5181     if (Result->isSigned() && Result->isAllOnes())
5182       continue;
5183 
5184     if (Result->getActiveBits() > 64 ||
5185         Result->getZExtValue() >= numElements * 2)
5186       return ExprError(Diag(TheCall->getBeginLoc(),
5187                             diag::err_shufflevector_argument_too_large)
5188                        << TheCall->getArg(i)->getSourceRange());
5189   }
5190 
5191   SmallVector<Expr*, 32> exprs;
5192 
5193   for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) {
5194     exprs.push_back(TheCall->getArg(i));
5195     TheCall->setArg(i, nullptr);
5196   }
5197 
5198   return new (Context) ShuffleVectorExpr(Context, exprs, resType,
5199                                          TheCall->getCallee()->getBeginLoc(),
5200                                          TheCall->getRParenLoc());
5201 }
5202 
5203 ExprResult Sema::ConvertVectorExpr(Expr *E, TypeSourceInfo *TInfo,
5204                                    SourceLocation BuiltinLoc,
5205                                    SourceLocation RParenLoc) {
5206   ExprValueKind VK = VK_PRValue;
5207   ExprObjectKind OK = OK_Ordinary;
5208   QualType DstTy = TInfo->getType();
5209   QualType SrcTy = E->getType();
5210 
5211   if (!SrcTy->isVectorType() && !SrcTy->isDependentType())
5212     return ExprError(Diag(BuiltinLoc,
5213                           diag::err_convertvector_non_vector)
5214                      << E->getSourceRange());
5215   if (!DstTy->isVectorType() && !DstTy->isDependentType())
5216     return ExprError(Diag(BuiltinLoc, diag::err_builtin_non_vector_type)
5217                      << "second"
5218                      << "__builtin_convertvector");
5219 
5220   if (!SrcTy->isDependentType() && !DstTy->isDependentType()) {
5221     unsigned SrcElts = SrcTy->castAs<VectorType>()->getNumElements();
5222     unsigned DstElts = DstTy->castAs<VectorType>()->getNumElements();
5223     if (SrcElts != DstElts)
5224       return ExprError(Diag(BuiltinLoc,
5225                             diag::err_convertvector_incompatible_vector)
5226                        << E->getSourceRange());
5227   }
5228 
5229   return new (Context) class ConvertVectorExpr(E, TInfo, DstTy, VK, OK,
5230                                                BuiltinLoc, RParenLoc);
5231 }
5232 
5233 bool Sema::BuiltinPrefetch(CallExpr *TheCall) {
5234   unsigned NumArgs = TheCall->getNumArgs();
5235 
5236   if (NumArgs > 3)
5237     return Diag(TheCall->getEndLoc(),
5238                 diag::err_typecheck_call_too_many_args_at_most)
5239            << 0 /*function call*/ << 3 << NumArgs << /*is non object*/ 0
5240            << TheCall->getSourceRange();
5241 
5242   // Argument 0 is checked for us and the remaining arguments must be
5243   // constant integers.
5244   for (unsigned i = 1; i != NumArgs; ++i)
5245     if (BuiltinConstantArgRange(TheCall, i, 0, i == 1 ? 1 : 3))
5246       return true;
5247 
5248   return false;
5249 }
5250 
5251 bool Sema::BuiltinArithmeticFence(CallExpr *TheCall) {
5252   if (!Context.getTargetInfo().checkArithmeticFenceSupported())
5253     return Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported)
5254            << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc());
5255   if (checkArgCount(TheCall, 1))
5256     return true;
5257   Expr *Arg = TheCall->getArg(0);
5258   if (Arg->isInstantiationDependent())
5259     return false;
5260 
5261   QualType ArgTy = Arg->getType();
5262   if (!ArgTy->hasFloatingRepresentation())
5263     return Diag(TheCall->getEndLoc(), diag::err_typecheck_expect_flt_or_vector)
5264            << ArgTy;
5265   if (Arg->isLValue()) {
5266     ExprResult FirstArg = DefaultLvalueConversion(Arg);
5267     TheCall->setArg(0, FirstArg.get());
5268   }
5269   TheCall->setType(TheCall->getArg(0)->getType());
5270   return false;
5271 }
5272 
5273 bool Sema::BuiltinAssume(CallExpr *TheCall) {
5274   Expr *Arg = TheCall->getArg(0);
5275   if (Arg->isInstantiationDependent()) return false;
5276 
5277   if (Arg->HasSideEffects(Context))
5278     Diag(Arg->getBeginLoc(), diag::warn_assume_side_effects)
5279         << Arg->getSourceRange()
5280         << cast<FunctionDecl>(TheCall->getCalleeDecl())->getIdentifier();
5281 
5282   return false;
5283 }
5284 
5285 bool Sema::BuiltinAllocaWithAlign(CallExpr *TheCall) {
5286   // The alignment must be a constant integer.
5287   Expr *Arg = TheCall->getArg(1);
5288 
5289   // We can't check the value of a dependent argument.
5290   if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
5291     if (const auto *UE =
5292             dyn_cast<UnaryExprOrTypeTraitExpr>(Arg->IgnoreParenImpCasts()))
5293       if (UE->getKind() == UETT_AlignOf ||
5294           UE->getKind() == UETT_PreferredAlignOf)
5295         Diag(TheCall->getBeginLoc(), diag::warn_alloca_align_alignof)
5296             << Arg->getSourceRange();
5297 
5298     llvm::APSInt Result = Arg->EvaluateKnownConstInt(Context);
5299 
5300     if (!Result.isPowerOf2())
5301       return Diag(TheCall->getBeginLoc(), diag::err_alignment_not_power_of_two)
5302              << Arg->getSourceRange();
5303 
5304     if (Result < Context.getCharWidth())
5305       return Diag(TheCall->getBeginLoc(), diag::err_alignment_too_small)
5306              << (unsigned)Context.getCharWidth() << Arg->getSourceRange();
5307 
5308     if (Result > std::numeric_limits<int32_t>::max())
5309       return Diag(TheCall->getBeginLoc(), diag::err_alignment_too_big)
5310              << std::numeric_limits<int32_t>::max() << Arg->getSourceRange();
5311   }
5312 
5313   return false;
5314 }
5315 
5316 bool Sema::BuiltinAssumeAligned(CallExpr *TheCall) {
5317   if (checkArgCountRange(TheCall, 2, 3))
5318     return true;
5319 
5320   unsigned NumArgs = TheCall->getNumArgs();
5321   Expr *FirstArg = TheCall->getArg(0);
5322 
5323   {
5324     ExprResult FirstArgResult =
5325         DefaultFunctionArrayLvalueConversion(FirstArg);
5326     if (!FirstArgResult.get()->getType()->isPointerType()) {
5327       Diag(TheCall->getBeginLoc(), diag::err_builtin_assume_aligned_invalid_arg)
5328           << TheCall->getSourceRange();
5329       return true;
5330     }
5331     TheCall->setArg(0, FirstArgResult.get());
5332   }
5333 
5334   // The alignment must be a constant integer.
5335   Expr *SecondArg = TheCall->getArg(1);
5336 
5337   // We can't check the value of a dependent argument.
5338   if (!SecondArg->isValueDependent()) {
5339     llvm::APSInt Result;
5340     if (BuiltinConstantArg(TheCall, 1, Result))
5341       return true;
5342 
5343     if (!Result.isPowerOf2())
5344       return Diag(TheCall->getBeginLoc(), diag::err_alignment_not_power_of_two)
5345              << SecondArg->getSourceRange();
5346 
5347     if (Result > Sema::MaximumAlignment)
5348       Diag(TheCall->getBeginLoc(), diag::warn_assume_aligned_too_great)
5349           << SecondArg->getSourceRange() << Sema::MaximumAlignment;
5350   }
5351 
5352   if (NumArgs > 2) {
5353     Expr *ThirdArg = TheCall->getArg(2);
5354     if (convertArgumentToType(*this, ThirdArg, Context.getSizeType()))
5355       return true;
5356     TheCall->setArg(2, ThirdArg);
5357   }
5358 
5359   return false;
5360 }
5361 
5362 bool Sema::BuiltinOSLogFormat(CallExpr *TheCall) {
5363   unsigned BuiltinID =
5364       cast<FunctionDecl>(TheCall->getCalleeDecl())->getBuiltinID();
5365   bool IsSizeCall = BuiltinID == Builtin::BI__builtin_os_log_format_buffer_size;
5366 
5367   unsigned NumArgs = TheCall->getNumArgs();
5368   unsigned NumRequiredArgs = IsSizeCall ? 1 : 2;
5369   if (NumArgs < NumRequiredArgs) {
5370     return Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args)
5371            << 0 /* function call */ << NumRequiredArgs << NumArgs
5372            << /*is non object*/ 0 << TheCall->getSourceRange();
5373   }
5374   if (NumArgs >= NumRequiredArgs + 0x100) {
5375     return Diag(TheCall->getEndLoc(),
5376                 diag::err_typecheck_call_too_many_args_at_most)
5377            << 0 /* function call */ << (NumRequiredArgs + 0xff) << NumArgs
5378            << /*is non object*/ 0 << TheCall->getSourceRange();
5379   }
5380   unsigned i = 0;
5381 
5382   // For formatting call, check buffer arg.
5383   if (!IsSizeCall) {
5384     ExprResult Arg(TheCall->getArg(i));
5385     InitializedEntity Entity = InitializedEntity::InitializeParameter(
5386         Context, Context.VoidPtrTy, false);
5387     Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
5388     if (Arg.isInvalid())
5389       return true;
5390     TheCall->setArg(i, Arg.get());
5391     i++;
5392   }
5393 
5394   // Check string literal arg.
5395   unsigned FormatIdx = i;
5396   {
5397     ExprResult Arg = CheckOSLogFormatStringArg(TheCall->getArg(i));
5398     if (Arg.isInvalid())
5399       return true;
5400     TheCall->setArg(i, Arg.get());
5401     i++;
5402   }
5403 
5404   // Make sure variadic args are scalar.
5405   unsigned FirstDataArg = i;
5406   while (i < NumArgs) {
5407     ExprResult Arg = DefaultVariadicArgumentPromotion(
5408         TheCall->getArg(i), VariadicFunction, nullptr);
5409     if (Arg.isInvalid())
5410       return true;
5411     CharUnits ArgSize = Context.getTypeSizeInChars(Arg.get()->getType());
5412     if (ArgSize.getQuantity() >= 0x100) {
5413       return Diag(Arg.get()->getEndLoc(), diag::err_os_log_argument_too_big)
5414              << i << (int)ArgSize.getQuantity() << 0xff
5415              << TheCall->getSourceRange();
5416     }
5417     TheCall->setArg(i, Arg.get());
5418     i++;
5419   }
5420 
5421   // Check formatting specifiers. NOTE: We're only doing this for the non-size
5422   // call to avoid duplicate diagnostics.
5423   if (!IsSizeCall) {
5424     llvm::SmallBitVector CheckedVarArgs(NumArgs, false);
5425     ArrayRef<const Expr *> Args(TheCall->getArgs(), TheCall->getNumArgs());
5426     bool Success = CheckFormatArguments(
5427         Args, FAPK_Variadic, FormatIdx, FirstDataArg, FST_OSLog,
5428         VariadicFunction, TheCall->getBeginLoc(), SourceRange(),
5429         CheckedVarArgs);
5430     if (!Success)
5431       return true;
5432   }
5433 
5434   if (IsSizeCall) {
5435     TheCall->setType(Context.getSizeType());
5436   } else {
5437     TheCall->setType(Context.VoidPtrTy);
5438   }
5439   return false;
5440 }
5441 
5442 bool Sema::BuiltinConstantArg(CallExpr *TheCall, int ArgNum,
5443                               llvm::APSInt &Result) {
5444   Expr *Arg = TheCall->getArg(ArgNum);
5445   DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
5446   FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
5447 
5448   if (Arg->isTypeDependent() || Arg->isValueDependent()) return false;
5449 
5450   std::optional<llvm::APSInt> R;
5451   if (!(R = Arg->getIntegerConstantExpr(Context)))
5452     return Diag(TheCall->getBeginLoc(), diag::err_constant_integer_arg_type)
5453            << FDecl->getDeclName() << Arg->getSourceRange();
5454   Result = *R;
5455   return false;
5456 }
5457 
5458 bool Sema::BuiltinConstantArgRange(CallExpr *TheCall, int ArgNum, int Low,
5459                                    int High, bool RangeIsError) {
5460   if (isConstantEvaluatedContext())
5461     return false;
5462   llvm::APSInt Result;
5463 
5464   // We can't check the value of a dependent argument.
5465   Expr *Arg = TheCall->getArg(ArgNum);
5466   if (Arg->isTypeDependent() || Arg->isValueDependent())
5467     return false;
5468 
5469   // Check constant-ness first.
5470   if (BuiltinConstantArg(TheCall, ArgNum, Result))
5471     return true;
5472 
5473   if (Result.getSExtValue() < Low || Result.getSExtValue() > High) {
5474     if (RangeIsError)
5475       return Diag(TheCall->getBeginLoc(), diag::err_argument_invalid_range)
5476              << toString(Result, 10) << Low << High << Arg->getSourceRange();
5477     else
5478       // Defer the warning until we know if the code will be emitted so that
5479       // dead code can ignore this.
5480       DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall,
5481                           PDiag(diag::warn_argument_invalid_range)
5482                               << toString(Result, 10) << Low << High
5483                               << Arg->getSourceRange());
5484   }
5485 
5486   return false;
5487 }
5488 
5489 bool Sema::BuiltinConstantArgMultiple(CallExpr *TheCall, int ArgNum,
5490                                       unsigned Num) {
5491   llvm::APSInt Result;
5492 
5493   // We can't check the value of a dependent argument.
5494   Expr *Arg = TheCall->getArg(ArgNum);
5495   if (Arg->isTypeDependent() || Arg->isValueDependent())
5496     return false;
5497 
5498   // Check constant-ness first.
5499   if (BuiltinConstantArg(TheCall, ArgNum, Result))
5500     return true;
5501 
5502   if (Result.getSExtValue() % Num != 0)
5503     return Diag(TheCall->getBeginLoc(), diag::err_argument_not_multiple)
5504            << Num << Arg->getSourceRange();
5505 
5506   return false;
5507 }
5508 
5509 bool Sema::BuiltinConstantArgPower2(CallExpr *TheCall, int ArgNum) {
5510   llvm::APSInt Result;
5511 
5512   // We can't check the value of a dependent argument.
5513   Expr *Arg = TheCall->getArg(ArgNum);
5514   if (Arg->isTypeDependent() || Arg->isValueDependent())
5515     return false;
5516 
5517   // Check constant-ness first.
5518   if (BuiltinConstantArg(TheCall, ArgNum, Result))
5519     return true;
5520 
5521   // Bit-twiddling to test for a power of 2: for x > 0, x & (x-1) is zero if
5522   // and only if x is a power of 2.
5523   if (Result.isStrictlyPositive() && (Result & (Result - 1)) == 0)
5524     return false;
5525 
5526   return Diag(TheCall->getBeginLoc(), diag::err_argument_not_power_of_2)
5527          << Arg->getSourceRange();
5528 }
5529 
5530 static bool IsShiftedByte(llvm::APSInt Value) {
5531   if (Value.isNegative())
5532     return false;
5533 
5534   // Check if it's a shifted byte, by shifting it down
5535   while (true) {
5536     // If the value fits in the bottom byte, the check passes.
5537     if (Value < 0x100)
5538       return true;
5539 
5540     // Otherwise, if the value has _any_ bits in the bottom byte, the check
5541     // fails.
5542     if ((Value & 0xFF) != 0)
5543       return false;
5544 
5545     // If the bottom 8 bits are all 0, but something above that is nonzero,
5546     // then shifting the value right by 8 bits won't affect whether it's a
5547     // shifted byte or not. So do that, and go round again.
5548     Value >>= 8;
5549   }
5550 }
5551 
5552 bool Sema::BuiltinConstantArgShiftedByte(CallExpr *TheCall, int ArgNum,
5553                                          unsigned ArgBits) {
5554   llvm::APSInt Result;
5555 
5556   // We can't check the value of a dependent argument.
5557   Expr *Arg = TheCall->getArg(ArgNum);
5558   if (Arg->isTypeDependent() || Arg->isValueDependent())
5559     return false;
5560 
5561   // Check constant-ness first.
5562   if (BuiltinConstantArg(TheCall, ArgNum, Result))
5563     return true;
5564 
5565   // Truncate to the given size.
5566   Result = Result.getLoBits(ArgBits);
5567   Result.setIsUnsigned(true);
5568 
5569   if (IsShiftedByte(Result))
5570     return false;
5571 
5572   return Diag(TheCall->getBeginLoc(), diag::err_argument_not_shifted_byte)
5573          << Arg->getSourceRange();
5574 }
5575 
5576 bool Sema::BuiltinConstantArgShiftedByteOrXXFF(CallExpr *TheCall, int ArgNum,
5577                                                unsigned ArgBits) {
5578   llvm::APSInt Result;
5579 
5580   // We can't check the value of a dependent argument.
5581   Expr *Arg = TheCall->getArg(ArgNum);
5582   if (Arg->isTypeDependent() || Arg->isValueDependent())
5583     return false;
5584 
5585   // Check constant-ness first.
5586   if (BuiltinConstantArg(TheCall, ArgNum, Result))
5587     return true;
5588 
5589   // Truncate to the given size.
5590   Result = Result.getLoBits(ArgBits);
5591   Result.setIsUnsigned(true);
5592 
5593   // Check to see if it's in either of the required forms.
5594   if (IsShiftedByte(Result) ||
5595       (Result > 0 && Result < 0x10000 && (Result & 0xFF) == 0xFF))
5596     return false;
5597 
5598   return Diag(TheCall->getBeginLoc(),
5599               diag::err_argument_not_shifted_byte_or_xxff)
5600          << Arg->getSourceRange();
5601 }
5602 
5603 bool Sema::BuiltinLongjmp(CallExpr *TheCall) {
5604   if (!Context.getTargetInfo().hasSjLjLowering())
5605     return Diag(TheCall->getBeginLoc(), diag::err_builtin_longjmp_unsupported)
5606            << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc());
5607 
5608   Expr *Arg = TheCall->getArg(1);
5609   llvm::APSInt Result;
5610 
5611   // TODO: This is less than ideal. Overload this to take a value.
5612   if (BuiltinConstantArg(TheCall, 1, Result))
5613     return true;
5614 
5615   if (Result != 1)
5616     return Diag(TheCall->getBeginLoc(), diag::err_builtin_longjmp_invalid_val)
5617            << SourceRange(Arg->getBeginLoc(), Arg->getEndLoc());
5618 
5619   return false;
5620 }
5621 
5622 bool Sema::BuiltinSetjmp(CallExpr *TheCall) {
5623   if (!Context.getTargetInfo().hasSjLjLowering())
5624     return Diag(TheCall->getBeginLoc(), diag::err_builtin_setjmp_unsupported)
5625            << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc());
5626   return false;
5627 }
5628 
5629 bool Sema::BuiltinCountedByRef(CallExpr *TheCall) {
5630   if (checkArgCount(TheCall, 1))
5631     return true;
5632 
5633   ExprResult ArgRes = UsualUnaryConversions(TheCall->getArg(0));
5634   if (ArgRes.isInvalid())
5635     return true;
5636 
5637   // For simplicity, we support only limited expressions for the argument.
5638   // Specifically a pointer to a flexible array member:'ptr->array'. This
5639   // allows us to reject arguments with complex casting, which really shouldn't
5640   // be a huge problem.
5641   const Expr *Arg = ArgRes.get()->IgnoreParenImpCasts();
5642   if (!isa<PointerType>(Arg->getType()) && !Arg->getType()->isArrayType())
5643     return Diag(Arg->getBeginLoc(),
5644                 diag::err_builtin_counted_by_ref_must_be_flex_array_member)
5645            << Arg->getSourceRange();
5646 
5647   if (Arg->HasSideEffects(Context))
5648     return Diag(Arg->getBeginLoc(),
5649                 diag::err_builtin_counted_by_ref_has_side_effects)
5650            << Arg->getSourceRange();
5651 
5652   if (const auto *ME = dyn_cast<MemberExpr>(Arg)) {
5653     if (!ME->isFlexibleArrayMemberLike(
5654             Context, getLangOpts().getStrictFlexArraysLevel()))
5655       return Diag(Arg->getBeginLoc(),
5656                   diag::err_builtin_counted_by_ref_must_be_flex_array_member)
5657              << Arg->getSourceRange();
5658 
5659     if (auto *CATy =
5660             ME->getMemberDecl()->getType()->getAs<CountAttributedType>();
5661         CATy && CATy->getKind() == CountAttributedType::CountedBy) {
5662       const auto *FAMDecl = cast<FieldDecl>(ME->getMemberDecl());
5663       if (const FieldDecl *CountFD = FAMDecl->findCountedByField()) {
5664         TheCall->setType(Context.getPointerType(CountFD->getType()));
5665         return false;
5666       }
5667     }
5668   } else {
5669     return Diag(Arg->getBeginLoc(),
5670                 diag::err_builtin_counted_by_ref_must_be_flex_array_member)
5671            << Arg->getSourceRange();
5672   }
5673 
5674   TheCall->setType(Context.getPointerType(Context.VoidTy));
5675   return false;
5676 }
5677 
5678 /// The result of __builtin_counted_by_ref cannot be assigned to a variable.
5679 /// It allows leaking and modification of bounds safety information.
5680 bool Sema::CheckInvalidBuiltinCountedByRef(const Expr *E,
5681                                            BuiltinCountedByRefKind K) {
5682   const CallExpr *CE =
5683       E ? dyn_cast<CallExpr>(E->IgnoreParenImpCasts()) : nullptr;
5684   if (!CE || CE->getBuiltinCallee() != Builtin::BI__builtin_counted_by_ref)
5685     return false;
5686 
5687   switch (K) {
5688   case AssignmentKind:
5689   case InitializerKind:
5690     Diag(E->getExprLoc(),
5691          diag::err_builtin_counted_by_ref_cannot_leak_reference)
5692         << 0 << E->getSourceRange();
5693     break;
5694   case FunctionArgKind:
5695     Diag(E->getExprLoc(),
5696          diag::err_builtin_counted_by_ref_cannot_leak_reference)
5697         << 1 << E->getSourceRange();
5698     break;
5699   case ReturnArgKind:
5700     Diag(E->getExprLoc(),
5701          diag::err_builtin_counted_by_ref_cannot_leak_reference)
5702         << 2 << E->getSourceRange();
5703     break;
5704   case ArraySubscriptKind:
5705     Diag(E->getExprLoc(), diag::err_builtin_counted_by_ref_invalid_use)
5706         << 0 << E->getSourceRange();
5707     break;
5708   case BinaryExprKind:
5709     Diag(E->getExprLoc(), diag::err_builtin_counted_by_ref_invalid_use)
5710         << 1 << E->getSourceRange();
5711     break;
5712   }
5713 
5714   return true;
5715 }
5716 
5717 namespace {
5718 
5719 class UncoveredArgHandler {
5720   enum { Unknown = -1, AllCovered = -2 };
5721 
5722   signed FirstUncoveredArg = Unknown;
5723   SmallVector<const Expr *, 4> DiagnosticExprs;
5724 
5725 public:
5726   UncoveredArgHandler() = default;
5727 
5728   bool hasUncoveredArg() const {
5729     return (FirstUncoveredArg >= 0);
5730   }
5731 
5732   unsigned getUncoveredArg() const {
5733     assert(hasUncoveredArg() && "no uncovered argument");
5734     return FirstUncoveredArg;
5735   }
5736 
5737   void setAllCovered() {
5738     // A string has been found with all arguments covered, so clear out
5739     // the diagnostics.
5740     DiagnosticExprs.clear();
5741     FirstUncoveredArg = AllCovered;
5742   }
5743 
5744   void Update(signed NewFirstUncoveredArg, const Expr *StrExpr) {
5745     assert(NewFirstUncoveredArg >= 0 && "Outside range");
5746 
5747     // Don't update if a previous string covers all arguments.
5748     if (FirstUncoveredArg == AllCovered)
5749       return;
5750 
5751     // UncoveredArgHandler tracks the highest uncovered argument index
5752     // and with it all the strings that match this index.
5753     if (NewFirstUncoveredArg == FirstUncoveredArg)
5754       DiagnosticExprs.push_back(StrExpr);
5755     else if (NewFirstUncoveredArg > FirstUncoveredArg) {
5756       DiagnosticExprs.clear();
5757       DiagnosticExprs.push_back(StrExpr);
5758       FirstUncoveredArg = NewFirstUncoveredArg;
5759     }
5760   }
5761 
5762   void Diagnose(Sema &S, bool IsFunctionCall, const Expr *ArgExpr);
5763 };
5764 
5765 enum StringLiteralCheckType {
5766   SLCT_NotALiteral,
5767   SLCT_UncheckedLiteral,
5768   SLCT_CheckedLiteral
5769 };
5770 
5771 } // namespace
5772 
5773 static void sumOffsets(llvm::APSInt &Offset, llvm::APSInt Addend,
5774                                      BinaryOperatorKind BinOpKind,
5775                                      bool AddendIsRight) {
5776   unsigned BitWidth = Offset.getBitWidth();
5777   unsigned AddendBitWidth = Addend.getBitWidth();
5778   // There might be negative interim results.
5779   if (Addend.isUnsigned()) {
5780     Addend = Addend.zext(++AddendBitWidth);
5781     Addend.setIsSigned(true);
5782   }
5783   // Adjust the bit width of the APSInts.
5784   if (AddendBitWidth > BitWidth) {
5785     Offset = Offset.sext(AddendBitWidth);
5786     BitWidth = AddendBitWidth;
5787   } else if (BitWidth > AddendBitWidth) {
5788     Addend = Addend.sext(BitWidth);
5789   }
5790 
5791   bool Ov = false;
5792   llvm::APSInt ResOffset = Offset;
5793   if (BinOpKind == BO_Add)
5794     ResOffset = Offset.sadd_ov(Addend, Ov);
5795   else {
5796     assert(AddendIsRight && BinOpKind == BO_Sub &&
5797            "operator must be add or sub with addend on the right");
5798     ResOffset = Offset.ssub_ov(Addend, Ov);
5799   }
5800 
5801   // We add an offset to a pointer here so we should support an offset as big as
5802   // possible.
5803   if (Ov) {
5804     assert(BitWidth <= std::numeric_limits<unsigned>::max() / 2 &&
5805            "index (intermediate) result too big");
5806     Offset = Offset.sext(2 * BitWidth);
5807     sumOffsets(Offset, Addend, BinOpKind, AddendIsRight);
5808     return;
5809   }
5810 
5811   Offset = ResOffset;
5812 }
5813 
5814 namespace {
5815 
5816 // This is a wrapper class around StringLiteral to support offsetted string
5817 // literals as format strings. It takes the offset into account when returning
5818 // the string and its length or the source locations to display notes correctly.
5819 class FormatStringLiteral {
5820   const StringLiteral *FExpr;
5821   int64_t Offset;
5822 
5823  public:
5824   FormatStringLiteral(const StringLiteral *fexpr, int64_t Offset = 0)
5825       : FExpr(fexpr), Offset(Offset) {}
5826 
5827   StringRef getString() const {
5828     return FExpr->getString().drop_front(Offset);
5829   }
5830 
5831   unsigned getByteLength() const {
5832     return FExpr->getByteLength() - getCharByteWidth() * Offset;
5833   }
5834 
5835   unsigned getLength() const { return FExpr->getLength() - Offset; }
5836   unsigned getCharByteWidth() const { return FExpr->getCharByteWidth(); }
5837 
5838   StringLiteralKind getKind() const { return FExpr->getKind(); }
5839 
5840   QualType getType() const { return FExpr->getType(); }
5841 
5842   bool isAscii() const { return FExpr->isOrdinary(); }
5843   bool isWide() const { return FExpr->isWide(); }
5844   bool isUTF8() const { return FExpr->isUTF8(); }
5845   bool isUTF16() const { return FExpr->isUTF16(); }
5846   bool isUTF32() const { return FExpr->isUTF32(); }
5847   bool isPascal() const { return FExpr->isPascal(); }
5848 
5849   SourceLocation getLocationOfByte(
5850       unsigned ByteNo, const SourceManager &SM, const LangOptions &Features,
5851       const TargetInfo &Target, unsigned *StartToken = nullptr,
5852       unsigned *StartTokenByteOffset = nullptr) const {
5853     return FExpr->getLocationOfByte(ByteNo + Offset, SM, Features, Target,
5854                                     StartToken, StartTokenByteOffset);
5855   }
5856 
5857   SourceLocation getBeginLoc() const LLVM_READONLY {
5858     return FExpr->getBeginLoc().getLocWithOffset(Offset);
5859   }
5860 
5861   SourceLocation getEndLoc() const LLVM_READONLY { return FExpr->getEndLoc(); }
5862 };
5863 
5864 } // namespace
5865 
5866 static void CheckFormatString(
5867     Sema &S, const FormatStringLiteral *FExpr, const Expr *OrigFormatExpr,
5868     ArrayRef<const Expr *> Args, Sema::FormatArgumentPassingKind APK,
5869     unsigned format_idx, unsigned firstDataArg, Sema::FormatStringType Type,
5870     bool inFunctionCall, Sema::VariadicCallType CallType,
5871     llvm::SmallBitVector &CheckedVarArgs, UncoveredArgHandler &UncoveredArg,
5872     bool IgnoreStringsWithoutSpecifiers);
5873 
5874 static const Expr *maybeConstEvalStringLiteral(ASTContext &Context,
5875                                                const Expr *E);
5876 
5877 // Determine if an expression is a string literal or constant string.
5878 // If this function returns false on the arguments to a function expecting a
5879 // format string, we will usually need to emit a warning.
5880 // True string literals are then checked by CheckFormatString.
5881 static StringLiteralCheckType
5882 checkFormatStringExpr(Sema &S, const Expr *E, ArrayRef<const Expr *> Args,
5883                       Sema::FormatArgumentPassingKind APK, unsigned format_idx,
5884                       unsigned firstDataArg, Sema::FormatStringType Type,
5885                       Sema::VariadicCallType CallType, bool InFunctionCall,
5886                       llvm::SmallBitVector &CheckedVarArgs,
5887                       UncoveredArgHandler &UncoveredArg, llvm::APSInt Offset,
5888                       bool IgnoreStringsWithoutSpecifiers = false) {
5889   if (S.isConstantEvaluatedContext())
5890     return SLCT_NotALiteral;
5891 tryAgain:
5892   assert(Offset.isSigned() && "invalid offset");
5893 
5894   if (E->isTypeDependent() || E->isValueDependent())
5895     return SLCT_NotALiteral;
5896 
5897   E = E->IgnoreParenCasts();
5898 
5899   if (E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull))
5900     // Technically -Wformat-nonliteral does not warn about this case.
5901     // The behavior of printf and friends in this case is implementation
5902     // dependent.  Ideally if the format string cannot be null then
5903     // it should have a 'nonnull' attribute in the function prototype.
5904     return SLCT_UncheckedLiteral;
5905 
5906   switch (E->getStmtClass()) {
5907   case Stmt::InitListExprClass:
5908     // Handle expressions like {"foobar"}.
5909     if (const clang::Expr *SLE = maybeConstEvalStringLiteral(S.Context, E)) {
5910       return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg,
5911                                    Type, CallType, /*InFunctionCall*/ false,
5912                                    CheckedVarArgs, UncoveredArg, Offset,
5913                                    IgnoreStringsWithoutSpecifiers);
5914     }
5915     return SLCT_NotALiteral;
5916   case Stmt::BinaryConditionalOperatorClass:
5917   case Stmt::ConditionalOperatorClass: {
5918     // The expression is a literal if both sub-expressions were, and it was
5919     // completely checked only if both sub-expressions were checked.
5920     const AbstractConditionalOperator *C =
5921         cast<AbstractConditionalOperator>(E);
5922 
5923     // Determine whether it is necessary to check both sub-expressions, for
5924     // example, because the condition expression is a constant that can be
5925     // evaluated at compile time.
5926     bool CheckLeft = true, CheckRight = true;
5927 
5928     bool Cond;
5929     if (C->getCond()->EvaluateAsBooleanCondition(
5930             Cond, S.getASTContext(), S.isConstantEvaluatedContext())) {
5931       if (Cond)
5932         CheckRight = false;
5933       else
5934         CheckLeft = false;
5935     }
5936 
5937     // We need to maintain the offsets for the right and the left hand side
5938     // separately to check if every possible indexed expression is a valid
5939     // string literal. They might have different offsets for different string
5940     // literals in the end.
5941     StringLiteralCheckType Left;
5942     if (!CheckLeft)
5943       Left = SLCT_UncheckedLiteral;
5944     else {
5945       Left = checkFormatStringExpr(S, C->getTrueExpr(), Args, APK, format_idx,
5946                                    firstDataArg, Type, CallType, InFunctionCall,
5947                                    CheckedVarArgs, UncoveredArg, Offset,
5948                                    IgnoreStringsWithoutSpecifiers);
5949       if (Left == SLCT_NotALiteral || !CheckRight) {
5950         return Left;
5951       }
5952     }
5953 
5954     StringLiteralCheckType Right = checkFormatStringExpr(
5955         S, C->getFalseExpr(), Args, APK, format_idx, firstDataArg, Type,
5956         CallType, InFunctionCall, CheckedVarArgs, UncoveredArg, Offset,
5957         IgnoreStringsWithoutSpecifiers);
5958 
5959     return (CheckLeft && Left < Right) ? Left : Right;
5960   }
5961 
5962   case Stmt::ImplicitCastExprClass:
5963     E = cast<ImplicitCastExpr>(E)->getSubExpr();
5964     goto tryAgain;
5965 
5966   case Stmt::OpaqueValueExprClass:
5967     if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) {
5968       E = src;
5969       goto tryAgain;
5970     }
5971     return SLCT_NotALiteral;
5972 
5973   case Stmt::PredefinedExprClass:
5974     // While __func__, etc., are technically not string literals, they
5975     // cannot contain format specifiers and thus are not a security
5976     // liability.
5977     return SLCT_UncheckedLiteral;
5978 
5979   case Stmt::DeclRefExprClass: {
5980     const DeclRefExpr *DR = cast<DeclRefExpr>(E);
5981 
5982     // As an exception, do not flag errors for variables binding to
5983     // const string literals.
5984     if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
5985       bool isConstant = false;
5986       QualType T = DR->getType();
5987 
5988       if (const ArrayType *AT = S.Context.getAsArrayType(T)) {
5989         isConstant = AT->getElementType().isConstant(S.Context);
5990       } else if (const PointerType *PT = T->getAs<PointerType>()) {
5991         isConstant = T.isConstant(S.Context) &&
5992                      PT->getPointeeType().isConstant(S.Context);
5993       } else if (T->isObjCObjectPointerType()) {
5994         // In ObjC, there is usually no "const ObjectPointer" type,
5995         // so don't check if the pointee type is constant.
5996         isConstant = T.isConstant(S.Context);
5997       }
5998 
5999       if (isConstant) {
6000         if (const Expr *Init = VD->getAnyInitializer()) {
6001           // Look through initializers like const char c[] = { "foo" }
6002           if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) {
6003             if (InitList->isStringLiteralInit())
6004               Init = InitList->getInit(0)->IgnoreParenImpCasts();
6005           }
6006           return checkFormatStringExpr(
6007               S, Init, Args, APK, format_idx, firstDataArg, Type, CallType,
6008               /*InFunctionCall*/ false, CheckedVarArgs, UncoveredArg, Offset);
6009         }
6010       }
6011 
6012       // When the format argument is an argument of this function, and this
6013       // function also has the format attribute, there are several interactions
6014       // for which there shouldn't be a warning. For instance, when calling
6015       // v*printf from a function that has the printf format attribute, we
6016       // should not emit a warning about using `fmt`, even though it's not
6017       // constant, because the arguments have already been checked for the
6018       // caller of `logmessage`:
6019       //
6020       //  __attribute__((format(printf, 1, 2)))
6021       //  void logmessage(char const *fmt, ...) {
6022       //    va_list ap;
6023       //    va_start(ap, fmt);
6024       //    vprintf(fmt, ap);  /* do not emit a warning about "fmt" */
6025       //    ...
6026       // }
6027       //
6028       // Another interaction that we need to support is calling a variadic
6029       // format function from a format function that has fixed arguments. For
6030       // instance:
6031       //
6032       //  __attribute__((format(printf, 1, 2)))
6033       //  void logstring(char const *fmt, char const *str) {
6034       //    printf(fmt, str);  /* do not emit a warning about "fmt" */
6035       //  }
6036       //
6037       // Same (and perhaps more relatably) for the variadic template case:
6038       //
6039       //  template<typename... Args>
6040       //  __attribute__((format(printf, 1, 2)))
6041       //  void log(const char *fmt, Args&&... args) {
6042       //    printf(fmt, forward<Args>(args)...);
6043       //           /* do not emit a warning about "fmt" */
6044       //  }
6045       //
6046       // Due to implementation difficulty, we only check the format, not the
6047       // format arguments, in all cases.
6048       //
6049       if (const auto *PV = dyn_cast<ParmVarDecl>(VD)) {
6050         if (const auto *D = dyn_cast<Decl>(PV->getDeclContext())) {
6051           for (const auto *PVFormat : D->specific_attrs<FormatAttr>()) {
6052             bool IsCXXMember = false;
6053             if (const auto *MD = dyn_cast<CXXMethodDecl>(D))
6054               IsCXXMember = MD->isInstance();
6055 
6056             bool IsVariadic = false;
6057             if (const FunctionType *FnTy = D->getFunctionType())
6058               IsVariadic = cast<FunctionProtoType>(FnTy)->isVariadic();
6059             else if (const auto *BD = dyn_cast<BlockDecl>(D))
6060               IsVariadic = BD->isVariadic();
6061             else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D))
6062               IsVariadic = OMD->isVariadic();
6063 
6064             Sema::FormatStringInfo CallerFSI;
6065             if (Sema::getFormatStringInfo(PVFormat, IsCXXMember, IsVariadic,
6066                                           &CallerFSI)) {
6067               // We also check if the formats are compatible.
6068               // We can't pass a 'scanf' string to a 'printf' function.
6069               if (PV->getFunctionScopeIndex() == CallerFSI.FormatIdx &&
6070                   Type == S.GetFormatStringType(PVFormat)) {
6071                 // Lastly, check that argument passing kinds transition in a
6072                 // way that makes sense:
6073                 // from a caller with FAPK_VAList, allow FAPK_VAList
6074                 // from a caller with FAPK_Fixed, allow FAPK_Fixed
6075                 // from a caller with FAPK_Fixed, allow FAPK_Variadic
6076                 // from a caller with FAPK_Variadic, allow FAPK_VAList
6077                 switch (combineFAPK(CallerFSI.ArgPassingKind, APK)) {
6078                 case combineFAPK(Sema::FAPK_VAList, Sema::FAPK_VAList):
6079                 case combineFAPK(Sema::FAPK_Fixed, Sema::FAPK_Fixed):
6080                 case combineFAPK(Sema::FAPK_Fixed, Sema::FAPK_Variadic):
6081                 case combineFAPK(Sema::FAPK_Variadic, Sema::FAPK_VAList):
6082                   return SLCT_UncheckedLiteral;
6083                 }
6084               }
6085             }
6086           }
6087         }
6088       }
6089     }
6090 
6091     return SLCT_NotALiteral;
6092   }
6093 
6094   case Stmt::CallExprClass:
6095   case Stmt::CXXMemberCallExprClass: {
6096     const CallExpr *CE = cast<CallExpr>(E);
6097     if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {
6098       bool IsFirst = true;
6099       StringLiteralCheckType CommonResult;
6100       for (const auto *FA : ND->specific_attrs<FormatArgAttr>()) {
6101         const Expr *Arg = CE->getArg(FA->getFormatIdx().getASTIndex());
6102         StringLiteralCheckType Result = checkFormatStringExpr(
6103             S, Arg, Args, APK, format_idx, firstDataArg, Type, CallType,
6104             InFunctionCall, CheckedVarArgs, UncoveredArg, Offset,
6105             IgnoreStringsWithoutSpecifiers);
6106         if (IsFirst) {
6107           CommonResult = Result;
6108           IsFirst = false;
6109         }
6110       }
6111       if (!IsFirst)
6112         return CommonResult;
6113 
6114       if (const auto *FD = dyn_cast<FunctionDecl>(ND)) {
6115         unsigned BuiltinID = FD->getBuiltinID();
6116         if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString ||
6117             BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) {
6118           const Expr *Arg = CE->getArg(0);
6119           return checkFormatStringExpr(
6120               S, Arg, Args, APK, format_idx, firstDataArg, Type, CallType,
6121               InFunctionCall, CheckedVarArgs, UncoveredArg, Offset,
6122               IgnoreStringsWithoutSpecifiers);
6123         }
6124       }
6125     }
6126     if (const Expr *SLE = maybeConstEvalStringLiteral(S.Context, E))
6127       return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg,
6128                                    Type, CallType, /*InFunctionCall*/ false,
6129                                    CheckedVarArgs, UncoveredArg, Offset,
6130                                    IgnoreStringsWithoutSpecifiers);
6131     return SLCT_NotALiteral;
6132   }
6133   case Stmt::ObjCMessageExprClass: {
6134     const auto *ME = cast<ObjCMessageExpr>(E);
6135     if (const auto *MD = ME->getMethodDecl()) {
6136       if (const auto *FA = MD->getAttr<FormatArgAttr>()) {
6137         // As a special case heuristic, if we're using the method -[NSBundle
6138         // localizedStringForKey:value:table:], ignore any key strings that lack
6139         // format specifiers. The idea is that if the key doesn't have any
6140         // format specifiers then its probably just a key to map to the
6141         // localized strings. If it does have format specifiers though, then its
6142         // likely that the text of the key is the format string in the
6143         // programmer's language, and should be checked.
6144         const ObjCInterfaceDecl *IFace;
6145         if (MD->isInstanceMethod() && (IFace = MD->getClassInterface()) &&
6146             IFace->getIdentifier()->isStr("NSBundle") &&
6147             MD->getSelector().isKeywordSelector(
6148                 {"localizedStringForKey", "value", "table"})) {
6149           IgnoreStringsWithoutSpecifiers = true;
6150         }
6151 
6152         const Expr *Arg = ME->getArg(FA->getFormatIdx().getASTIndex());
6153         return checkFormatStringExpr(
6154             S, Arg, Args, APK, format_idx, firstDataArg, Type, CallType,
6155             InFunctionCall, CheckedVarArgs, UncoveredArg, Offset,
6156             IgnoreStringsWithoutSpecifiers);
6157       }
6158     }
6159 
6160     return SLCT_NotALiteral;
6161   }
6162   case Stmt::ObjCStringLiteralClass:
6163   case Stmt::StringLiteralClass: {
6164     const StringLiteral *StrE = nullptr;
6165 
6166     if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E))
6167       StrE = ObjCFExpr->getString();
6168     else
6169       StrE = cast<StringLiteral>(E);
6170 
6171     if (StrE) {
6172       if (Offset.isNegative() || Offset > StrE->getLength()) {
6173         // TODO: It would be better to have an explicit warning for out of
6174         // bounds literals.
6175         return SLCT_NotALiteral;
6176       }
6177       FormatStringLiteral FStr(StrE, Offset.sextOrTrunc(64).getSExtValue());
6178       CheckFormatString(S, &FStr, E, Args, APK, format_idx, firstDataArg, Type,
6179                         InFunctionCall, CallType, CheckedVarArgs, UncoveredArg,
6180                         IgnoreStringsWithoutSpecifiers);
6181       return SLCT_CheckedLiteral;
6182     }
6183 
6184     return SLCT_NotALiteral;
6185   }
6186   case Stmt::BinaryOperatorClass: {
6187     const BinaryOperator *BinOp = cast<BinaryOperator>(E);
6188 
6189     // A string literal + an int offset is still a string literal.
6190     if (BinOp->isAdditiveOp()) {
6191       Expr::EvalResult LResult, RResult;
6192 
6193       bool LIsInt = BinOp->getLHS()->EvaluateAsInt(
6194           LResult, S.Context, Expr::SE_NoSideEffects,
6195           S.isConstantEvaluatedContext());
6196       bool RIsInt = BinOp->getRHS()->EvaluateAsInt(
6197           RResult, S.Context, Expr::SE_NoSideEffects,
6198           S.isConstantEvaluatedContext());
6199 
6200       if (LIsInt != RIsInt) {
6201         BinaryOperatorKind BinOpKind = BinOp->getOpcode();
6202 
6203         if (LIsInt) {
6204           if (BinOpKind == BO_Add) {
6205             sumOffsets(Offset, LResult.Val.getInt(), BinOpKind, RIsInt);
6206             E = BinOp->getRHS();
6207             goto tryAgain;
6208           }
6209         } else {
6210           sumOffsets(Offset, RResult.Val.getInt(), BinOpKind, RIsInt);
6211           E = BinOp->getLHS();
6212           goto tryAgain;
6213         }
6214       }
6215     }
6216 
6217     return SLCT_NotALiteral;
6218   }
6219   case Stmt::UnaryOperatorClass: {
6220     const UnaryOperator *UnaOp = cast<UnaryOperator>(E);
6221     auto ASE = dyn_cast<ArraySubscriptExpr>(UnaOp->getSubExpr());
6222     if (UnaOp->getOpcode() == UO_AddrOf && ASE) {
6223       Expr::EvalResult IndexResult;
6224       if (ASE->getRHS()->EvaluateAsInt(IndexResult, S.Context,
6225                                        Expr::SE_NoSideEffects,
6226                                        S.isConstantEvaluatedContext())) {
6227         sumOffsets(Offset, IndexResult.Val.getInt(), BO_Add,
6228                    /*RHS is int*/ true);
6229         E = ASE->getBase();
6230         goto tryAgain;
6231       }
6232     }
6233 
6234     return SLCT_NotALiteral;
6235   }
6236 
6237   default:
6238     return SLCT_NotALiteral;
6239   }
6240 }
6241 
6242 // If this expression can be evaluated at compile-time,
6243 // check if the result is a StringLiteral and return it
6244 // otherwise return nullptr
6245 static const Expr *maybeConstEvalStringLiteral(ASTContext &Context,
6246                                                const Expr *E) {
6247   Expr::EvalResult Result;
6248   if (E->EvaluateAsRValue(Result, Context) && Result.Val.isLValue()) {
6249     const auto *LVE = Result.Val.getLValueBase().dyn_cast<const Expr *>();
6250     if (isa_and_nonnull<StringLiteral>(LVE))
6251       return LVE;
6252   }
6253   return nullptr;
6254 }
6255 
6256 Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) {
6257   return llvm::StringSwitch<FormatStringType>(Format->getType()->getName())
6258       .Case("scanf", FST_Scanf)
6259       .Cases("printf", "printf0", "syslog", FST_Printf)
6260       .Cases("NSString", "CFString", FST_NSString)
6261       .Case("strftime", FST_Strftime)
6262       .Case("strfmon", FST_Strfmon)
6263       .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf)
6264       .Case("freebsd_kprintf", FST_FreeBSDKPrintf)
6265       .Case("os_trace", FST_OSLog)
6266       .Case("os_log", FST_OSLog)
6267       .Default(FST_Unknown);
6268 }
6269 
6270 bool Sema::CheckFormatArguments(const FormatAttr *Format,
6271                                 ArrayRef<const Expr *> Args, bool IsCXXMember,
6272                                 VariadicCallType CallType, SourceLocation Loc,
6273                                 SourceRange Range,
6274                                 llvm::SmallBitVector &CheckedVarArgs) {
6275   FormatStringInfo FSI;
6276   if (getFormatStringInfo(Format, IsCXXMember, CallType != VariadicDoesNotApply,
6277                           &FSI))
6278     return CheckFormatArguments(Args, FSI.ArgPassingKind, FSI.FormatIdx,
6279                                 FSI.FirstDataArg, GetFormatStringType(Format),
6280                                 CallType, Loc, Range, CheckedVarArgs);
6281   return false;
6282 }
6283 
6284 bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args,
6285                                 Sema::FormatArgumentPassingKind APK,
6286                                 unsigned format_idx, unsigned firstDataArg,
6287                                 FormatStringType Type,
6288                                 VariadicCallType CallType, SourceLocation Loc,
6289                                 SourceRange Range,
6290                                 llvm::SmallBitVector &CheckedVarArgs) {
6291   // CHECK: printf/scanf-like function is called with no format string.
6292   if (format_idx >= Args.size()) {
6293     Diag(Loc, diag::warn_missing_format_string) << Range;
6294     return false;
6295   }
6296 
6297   const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts();
6298 
6299   // CHECK: format string is not a string literal.
6300   //
6301   // Dynamically generated format strings are difficult to
6302   // automatically vet at compile time.  Requiring that format strings
6303   // are string literals: (1) permits the checking of format strings by
6304   // the compiler and thereby (2) can practically remove the source of
6305   // many format string exploits.
6306 
6307   // Format string can be either ObjC string (e.g. @"%d") or
6308   // C string (e.g. "%d")
6309   // ObjC string uses the same format specifiers as C string, so we can use
6310   // the same format string checking logic for both ObjC and C strings.
6311   UncoveredArgHandler UncoveredArg;
6312   StringLiteralCheckType CT = checkFormatStringExpr(
6313       *this, OrigFormatExpr, Args, APK, format_idx, firstDataArg, Type,
6314       CallType,
6315       /*IsFunctionCall*/ true, CheckedVarArgs, UncoveredArg,
6316       /*no string offset*/ llvm::APSInt(64, false) = 0);
6317 
6318   // Generate a diagnostic where an uncovered argument is detected.
6319   if (UncoveredArg.hasUncoveredArg()) {
6320     unsigned ArgIdx = UncoveredArg.getUncoveredArg() + firstDataArg;
6321     assert(ArgIdx < Args.size() && "ArgIdx outside bounds");
6322     UncoveredArg.Diagnose(*this, /*IsFunctionCall*/true, Args[ArgIdx]);
6323   }
6324 
6325   if (CT != SLCT_NotALiteral)
6326     // Literal format string found, check done!
6327     return CT == SLCT_CheckedLiteral;
6328 
6329   // Strftime is particular as it always uses a single 'time' argument,
6330   // so it is safe to pass a non-literal string.
6331   if (Type == FST_Strftime)
6332     return false;
6333 
6334   // Do not emit diag when the string param is a macro expansion and the
6335   // format is either NSString or CFString. This is a hack to prevent
6336   // diag when using the NSLocalizedString and CFCopyLocalizedString macros
6337   // which are usually used in place of NS and CF string literals.
6338   SourceLocation FormatLoc = Args[format_idx]->getBeginLoc();
6339   if (Type == FST_NSString && SourceMgr.isInSystemMacro(FormatLoc))
6340     return false;
6341 
6342   // If there are no arguments specified, warn with -Wformat-security, otherwise
6343   // warn only with -Wformat-nonliteral.
6344   if (Args.size() == firstDataArg) {
6345     Diag(FormatLoc, diag::warn_format_nonliteral_noargs)
6346       << OrigFormatExpr->getSourceRange();
6347     switch (Type) {
6348     default:
6349       break;
6350     case FST_Kprintf:
6351     case FST_FreeBSDKPrintf:
6352     case FST_Printf:
6353     case FST_Syslog:
6354       Diag(FormatLoc, diag::note_format_security_fixit)
6355         << FixItHint::CreateInsertion(FormatLoc, "\"%s\", ");
6356       break;
6357     case FST_NSString:
6358       Diag(FormatLoc, diag::note_format_security_fixit)
6359         << FixItHint::CreateInsertion(FormatLoc, "@\"%@\", ");
6360       break;
6361     }
6362   } else {
6363     Diag(FormatLoc, diag::warn_format_nonliteral)
6364       << OrigFormatExpr->getSourceRange();
6365   }
6366   return false;
6367 }
6368 
6369 namespace {
6370 
6371 class CheckFormatHandler : public analyze_format_string::FormatStringHandler {
6372 protected:
6373   Sema &S;
6374   const FormatStringLiteral *FExpr;
6375   const Expr *OrigFormatExpr;
6376   const Sema::FormatStringType FSType;
6377   const unsigned FirstDataArg;
6378   const unsigned NumDataArgs;
6379   const char *Beg; // Start of format string.
6380   const Sema::FormatArgumentPassingKind ArgPassingKind;
6381   ArrayRef<const Expr *> Args;
6382   unsigned FormatIdx;
6383   llvm::SmallBitVector CoveredArgs;
6384   bool usesPositionalArgs = false;
6385   bool atFirstArg = true;
6386   bool inFunctionCall;
6387   Sema::VariadicCallType CallType;
6388   llvm::SmallBitVector &CheckedVarArgs;
6389   UncoveredArgHandler &UncoveredArg;
6390 
6391 public:
6392   CheckFormatHandler(Sema &s, const FormatStringLiteral *fexpr,
6393                      const Expr *origFormatExpr,
6394                      const Sema::FormatStringType type, unsigned firstDataArg,
6395                      unsigned numDataArgs, const char *beg,
6396                      Sema::FormatArgumentPassingKind APK,
6397                      ArrayRef<const Expr *> Args, unsigned formatIdx,
6398                      bool inFunctionCall, Sema::VariadicCallType callType,
6399                      llvm::SmallBitVector &CheckedVarArgs,
6400                      UncoveredArgHandler &UncoveredArg)
6401       : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), FSType(type),
6402         FirstDataArg(firstDataArg), NumDataArgs(numDataArgs), Beg(beg),
6403         ArgPassingKind(APK), Args(Args), FormatIdx(formatIdx),
6404         inFunctionCall(inFunctionCall), CallType(callType),
6405         CheckedVarArgs(CheckedVarArgs), UncoveredArg(UncoveredArg) {
6406     CoveredArgs.resize(numDataArgs);
6407     CoveredArgs.reset();
6408   }
6409 
6410   void DoneProcessing();
6411 
6412   void HandleIncompleteSpecifier(const char *startSpecifier,
6413                                  unsigned specifierLen) override;
6414 
6415   void HandleInvalidLengthModifier(
6416                            const analyze_format_string::FormatSpecifier &FS,
6417                            const analyze_format_string::ConversionSpecifier &CS,
6418                            const char *startSpecifier, unsigned specifierLen,
6419                            unsigned DiagID);
6420 
6421   void HandleNonStandardLengthModifier(
6422                     const analyze_format_string::FormatSpecifier &FS,
6423                     const char *startSpecifier, unsigned specifierLen);
6424 
6425   void HandleNonStandardConversionSpecifier(
6426                     const analyze_format_string::ConversionSpecifier &CS,
6427                     const char *startSpecifier, unsigned specifierLen);
6428 
6429   void HandlePosition(const char *startPos, unsigned posLen) override;
6430 
6431   void HandleInvalidPosition(const char *startSpecifier,
6432                              unsigned specifierLen,
6433                              analyze_format_string::PositionContext p) override;
6434 
6435   void HandleZeroPosition(const char *startPos, unsigned posLen) override;
6436 
6437   void HandleNullChar(const char *nullCharacter) override;
6438 
6439   template <typename Range>
6440   static void
6441   EmitFormatDiagnostic(Sema &S, bool inFunctionCall, const Expr *ArgumentExpr,
6442                        const PartialDiagnostic &PDiag, SourceLocation StringLoc,
6443                        bool IsStringLocation, Range StringRange,
6444                        ArrayRef<FixItHint> Fixit = {});
6445 
6446 protected:
6447   bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc,
6448                                         const char *startSpec,
6449                                         unsigned specifierLen,
6450                                         const char *csStart, unsigned csLen);
6451 
6452   void HandlePositionalNonpositionalArgs(SourceLocation Loc,
6453                                          const char *startSpec,
6454                                          unsigned specifierLen);
6455 
6456   SourceRange getFormatStringRange();
6457   CharSourceRange getSpecifierRange(const char *startSpecifier,
6458                                     unsigned specifierLen);
6459   SourceLocation getLocationOfByte(const char *x);
6460 
6461   const Expr *getDataArg(unsigned i) const;
6462 
6463   bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS,
6464                     const analyze_format_string::ConversionSpecifier &CS,
6465                     const char *startSpecifier, unsigned specifierLen,
6466                     unsigned argIndex);
6467 
6468   template <typename Range>
6469   void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc,
6470                             bool IsStringLocation, Range StringRange,
6471                             ArrayRef<FixItHint> Fixit = {});
6472 };
6473 
6474 } // namespace
6475 
6476 SourceRange CheckFormatHandler::getFormatStringRange() {
6477   return OrigFormatExpr->getSourceRange();
6478 }
6479 
6480 CharSourceRange CheckFormatHandler::
6481 getSpecifierRange(const char *startSpecifier, unsigned specifierLen) {
6482   SourceLocation Start = getLocationOfByte(startSpecifier);
6483   SourceLocation End   = getLocationOfByte(startSpecifier + specifierLen - 1);
6484 
6485   // Advance the end SourceLocation by one due to half-open ranges.
6486   End = End.getLocWithOffset(1);
6487 
6488   return CharSourceRange::getCharRange(Start, End);
6489 }
6490 
6491 SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) {
6492   return FExpr->getLocationOfByte(x - Beg, S.getSourceManager(),
6493                                   S.getLangOpts(), S.Context.getTargetInfo());
6494 }
6495 
6496 void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier,
6497                                                    unsigned specifierLen){
6498   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier),
6499                        getLocationOfByte(startSpecifier),
6500                        /*IsStringLocation*/true,
6501                        getSpecifierRange(startSpecifier, specifierLen));
6502 }
6503 
6504 void CheckFormatHandler::HandleInvalidLengthModifier(
6505     const analyze_format_string::FormatSpecifier &FS,
6506     const analyze_format_string::ConversionSpecifier &CS,
6507     const char *startSpecifier, unsigned specifierLen, unsigned DiagID) {
6508   using namespace analyze_format_string;
6509 
6510   const LengthModifier &LM = FS.getLengthModifier();
6511   CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength());
6512 
6513   // See if we know how to fix this length modifier.
6514   std::optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier();
6515   if (FixedLM) {
6516     EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(),
6517                          getLocationOfByte(LM.getStart()),
6518                          /*IsStringLocation*/true,
6519                          getSpecifierRange(startSpecifier, specifierLen));
6520 
6521     S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier)
6522       << FixedLM->toString()
6523       << FixItHint::CreateReplacement(LMRange, FixedLM->toString());
6524 
6525   } else {
6526     FixItHint Hint;
6527     if (DiagID == diag::warn_format_nonsensical_length)
6528       Hint = FixItHint::CreateRemoval(LMRange);
6529 
6530     EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(),
6531                          getLocationOfByte(LM.getStart()),
6532                          /*IsStringLocation*/true,
6533                          getSpecifierRange(startSpecifier, specifierLen),
6534                          Hint);
6535   }
6536 }
6537 
6538 void CheckFormatHandler::HandleNonStandardLengthModifier(
6539     const analyze_format_string::FormatSpecifier &FS,
6540     const char *startSpecifier, unsigned specifierLen) {
6541   using namespace analyze_format_string;
6542 
6543   const LengthModifier &LM = FS.getLengthModifier();
6544   CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength());
6545 
6546   // See if we know how to fix this length modifier.
6547   std::optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier();
6548   if (FixedLM) {
6549     EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
6550                            << LM.toString() << 0,
6551                          getLocationOfByte(LM.getStart()),
6552                          /*IsStringLocation*/true,
6553                          getSpecifierRange(startSpecifier, specifierLen));
6554 
6555     S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier)
6556       << FixedLM->toString()
6557       << FixItHint::CreateReplacement(LMRange, FixedLM->toString());
6558 
6559   } else {
6560     EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
6561                            << LM.toString() << 0,
6562                          getLocationOfByte(LM.getStart()),
6563                          /*IsStringLocation*/true,
6564                          getSpecifierRange(startSpecifier, specifierLen));
6565   }
6566 }
6567 
6568 void CheckFormatHandler::HandleNonStandardConversionSpecifier(
6569     const analyze_format_string::ConversionSpecifier &CS,
6570     const char *startSpecifier, unsigned specifierLen) {
6571   using namespace analyze_format_string;
6572 
6573   // See if we know how to fix this conversion specifier.
6574   std::optional<ConversionSpecifier> FixedCS = CS.getStandardSpecifier();
6575   if (FixedCS) {
6576     EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
6577                           << CS.toString() << /*conversion specifier*/1,
6578                          getLocationOfByte(CS.getStart()),
6579                          /*IsStringLocation*/true,
6580                          getSpecifierRange(startSpecifier, specifierLen));
6581 
6582     CharSourceRange CSRange = getSpecifierRange(CS.getStart(), CS.getLength());
6583     S.Diag(getLocationOfByte(CS.getStart()), diag::note_format_fix_specifier)
6584       << FixedCS->toString()
6585       << FixItHint::CreateReplacement(CSRange, FixedCS->toString());
6586   } else {
6587     EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
6588                           << CS.toString() << /*conversion specifier*/1,
6589                          getLocationOfByte(CS.getStart()),
6590                          /*IsStringLocation*/true,
6591                          getSpecifierRange(startSpecifier, specifierLen));
6592   }
6593 }
6594 
6595 void CheckFormatHandler::HandlePosition(const char *startPos,
6596                                         unsigned posLen) {
6597   if (!S.getDiagnostics().isIgnored(
6598           diag::warn_format_non_standard_positional_arg, SourceLocation()))
6599     EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg),
6600                          getLocationOfByte(startPos),
6601                          /*IsStringLocation*/ true,
6602                          getSpecifierRange(startPos, posLen));
6603 }
6604 
6605 void CheckFormatHandler::HandleInvalidPosition(
6606     const char *startSpecifier, unsigned specifierLen,
6607     analyze_format_string::PositionContext p) {
6608   if (!S.getDiagnostics().isIgnored(
6609           diag::warn_format_invalid_positional_specifier, SourceLocation()))
6610     EmitFormatDiagnostic(
6611         S.PDiag(diag::warn_format_invalid_positional_specifier) << (unsigned)p,
6612         getLocationOfByte(startSpecifier), /*IsStringLocation*/ true,
6613         getSpecifierRange(startSpecifier, specifierLen));
6614 }
6615 
6616 void CheckFormatHandler::HandleZeroPosition(const char *startPos,
6617                                             unsigned posLen) {
6618   if (!S.getDiagnostics().isIgnored(diag::warn_format_zero_positional_specifier,
6619                                     SourceLocation()))
6620     EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier),
6621                          getLocationOfByte(startPos),
6622                          /*IsStringLocation*/ true,
6623                          getSpecifierRange(startPos, posLen));
6624 }
6625 
6626 void CheckFormatHandler::HandleNullChar(const char *nullCharacter) {
6627   if (!isa<ObjCStringLiteral>(OrigFormatExpr)) {
6628     // The presence of a null character is likely an error.
6629     EmitFormatDiagnostic(
6630       S.PDiag(diag::warn_printf_format_string_contains_null_char),
6631       getLocationOfByte(nullCharacter), /*IsStringLocation*/true,
6632       getFormatStringRange());
6633   }
6634 }
6635 
6636 // Note that this may return NULL if there was an error parsing or building
6637 // one of the argument expressions.
6638 const Expr *CheckFormatHandler::getDataArg(unsigned i) const {
6639   return Args[FirstDataArg + i];
6640 }
6641 
6642 void CheckFormatHandler::DoneProcessing() {
6643   // Does the number of data arguments exceed the number of
6644   // format conversions in the format string?
6645   if (ArgPassingKind != Sema::FAPK_VAList) {
6646     // Find any arguments that weren't covered.
6647     CoveredArgs.flip();
6648     signed notCoveredArg = CoveredArgs.find_first();
6649     if (notCoveredArg >= 0) {
6650       assert((unsigned)notCoveredArg < NumDataArgs);
6651       UncoveredArg.Update(notCoveredArg, OrigFormatExpr);
6652     } else {
6653       UncoveredArg.setAllCovered();
6654     }
6655   }
6656 }
6657 
6658 void UncoveredArgHandler::Diagnose(Sema &S, bool IsFunctionCall,
6659                                    const Expr *ArgExpr) {
6660   assert(hasUncoveredArg() && !DiagnosticExprs.empty() &&
6661          "Invalid state");
6662 
6663   if (!ArgExpr)
6664     return;
6665 
6666   SourceLocation Loc = ArgExpr->getBeginLoc();
6667 
6668   if (S.getSourceManager().isInSystemMacro(Loc))
6669     return;
6670 
6671   PartialDiagnostic PDiag = S.PDiag(diag::warn_printf_data_arg_not_used);
6672   for (auto E : DiagnosticExprs)
6673     PDiag << E->getSourceRange();
6674 
6675   CheckFormatHandler::EmitFormatDiagnostic(
6676                                   S, IsFunctionCall, DiagnosticExprs[0],
6677                                   PDiag, Loc, /*IsStringLocation*/false,
6678                                   DiagnosticExprs[0]->getSourceRange());
6679 }
6680 
6681 bool
6682 CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex,
6683                                                      SourceLocation Loc,
6684                                                      const char *startSpec,
6685                                                      unsigned specifierLen,
6686                                                      const char *csStart,
6687                                                      unsigned csLen) {
6688   bool keepGoing = true;
6689   if (argIndex < NumDataArgs) {
6690     // Consider the argument coverered, even though the specifier doesn't
6691     // make sense.
6692     CoveredArgs.set(argIndex);
6693   }
6694   else {
6695     // If argIndex exceeds the number of data arguments we
6696     // don't issue a warning because that is just a cascade of warnings (and
6697     // they may have intended '%%' anyway). We don't want to continue processing
6698     // the format string after this point, however, as we will like just get
6699     // gibberish when trying to match arguments.
6700     keepGoing = false;
6701   }
6702 
6703   StringRef Specifier(csStart, csLen);
6704 
6705   // If the specifier in non-printable, it could be the first byte of a UTF-8
6706   // sequence. In that case, print the UTF-8 code point. If not, print the byte
6707   // hex value.
6708   std::string CodePointStr;
6709   if (!llvm::sys::locale::isPrint(*csStart)) {
6710     llvm::UTF32 CodePoint;
6711     const llvm::UTF8 **B = reinterpret_cast<const llvm::UTF8 **>(&csStart);
6712     const llvm::UTF8 *E =
6713         reinterpret_cast<const llvm::UTF8 *>(csStart + csLen);
6714     llvm::ConversionResult Result =
6715         llvm::convertUTF8Sequence(B, E, &CodePoint, llvm::strictConversion);
6716 
6717     if (Result != llvm::conversionOK) {
6718       unsigned char FirstChar = *csStart;
6719       CodePoint = (llvm::UTF32)FirstChar;
6720     }
6721 
6722     llvm::raw_string_ostream OS(CodePointStr);
6723     if (CodePoint < 256)
6724       OS << "\\x" << llvm::format("%02x", CodePoint);
6725     else if (CodePoint <= 0xFFFF)
6726       OS << "\\u" << llvm::format("%04x", CodePoint);
6727     else
6728       OS << "\\U" << llvm::format("%08x", CodePoint);
6729     Specifier = CodePointStr;
6730   }
6731 
6732   EmitFormatDiagnostic(
6733       S.PDiag(diag::warn_format_invalid_conversion) << Specifier, Loc,
6734       /*IsStringLocation*/ true, getSpecifierRange(startSpec, specifierLen));
6735 
6736   return keepGoing;
6737 }
6738 
6739 void
6740 CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc,
6741                                                       const char *startSpec,
6742                                                       unsigned specifierLen) {
6743   EmitFormatDiagnostic(
6744     S.PDiag(diag::warn_format_mix_positional_nonpositional_args),
6745     Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen));
6746 }
6747 
6748 bool
6749 CheckFormatHandler::CheckNumArgs(
6750   const analyze_format_string::FormatSpecifier &FS,
6751   const analyze_format_string::ConversionSpecifier &CS,
6752   const char *startSpecifier, unsigned specifierLen, unsigned argIndex) {
6753 
6754   if (argIndex >= NumDataArgs) {
6755     PartialDiagnostic PDiag = FS.usesPositionalArg()
6756       ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args)
6757            << (argIndex+1) << NumDataArgs)
6758       : S.PDiag(diag::warn_printf_insufficient_data_args);
6759     EmitFormatDiagnostic(
6760       PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true,
6761       getSpecifierRange(startSpecifier, specifierLen));
6762 
6763     // Since more arguments than conversion tokens are given, by extension
6764     // all arguments are covered, so mark this as so.
6765     UncoveredArg.setAllCovered();
6766     return false;
6767   }
6768   return true;
6769 }
6770 
6771 template<typename Range>
6772 void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag,
6773                                               SourceLocation Loc,
6774                                               bool IsStringLocation,
6775                                               Range StringRange,
6776                                               ArrayRef<FixItHint> FixIt) {
6777   EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag,
6778                        Loc, IsStringLocation, StringRange, FixIt);
6779 }
6780 
6781 /// If the format string is not within the function call, emit a note
6782 /// so that the function call and string are in diagnostic messages.
6783 ///
6784 /// \param InFunctionCall if true, the format string is within the function
6785 /// call and only one diagnostic message will be produced.  Otherwise, an
6786 /// extra note will be emitted pointing to location of the format string.
6787 ///
6788 /// \param ArgumentExpr the expression that is passed as the format string
6789 /// argument in the function call.  Used for getting locations when two
6790 /// diagnostics are emitted.
6791 ///
6792 /// \param PDiag the callee should already have provided any strings for the
6793 /// diagnostic message.  This function only adds locations and fixits
6794 /// to diagnostics.
6795 ///
6796 /// \param Loc primary location for diagnostic.  If two diagnostics are
6797 /// required, one will be at Loc and a new SourceLocation will be created for
6798 /// the other one.
6799 ///
6800 /// \param IsStringLocation if true, Loc points to the format string should be
6801 /// used for the note.  Otherwise, Loc points to the argument list and will
6802 /// be used with PDiag.
6803 ///
6804 /// \param StringRange some or all of the string to highlight.  This is
6805 /// templated so it can accept either a CharSourceRange or a SourceRange.
6806 ///
6807 /// \param FixIt optional fix it hint for the format string.
6808 template <typename Range>
6809 void CheckFormatHandler::EmitFormatDiagnostic(
6810     Sema &S, bool InFunctionCall, const Expr *ArgumentExpr,
6811     const PartialDiagnostic &PDiag, SourceLocation Loc, bool IsStringLocation,
6812     Range StringRange, ArrayRef<FixItHint> FixIt) {
6813   if (InFunctionCall) {
6814     const Sema::SemaDiagnosticBuilder &D = S.Diag(Loc, PDiag);
6815     D << StringRange;
6816     D << FixIt;
6817   } else {
6818     S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag)
6819       << ArgumentExpr->getSourceRange();
6820 
6821     const Sema::SemaDiagnosticBuilder &Note =
6822       S.Diag(IsStringLocation ? Loc : StringRange.getBegin(),
6823              diag::note_format_string_defined);
6824 
6825     Note << StringRange;
6826     Note << FixIt;
6827   }
6828 }
6829 
6830 //===--- CHECK: Printf format string checking -----------------------------===//
6831 
6832 namespace {
6833 
6834 class CheckPrintfHandler : public CheckFormatHandler {
6835 public:
6836   CheckPrintfHandler(Sema &s, const FormatStringLiteral *fexpr,
6837                      const Expr *origFormatExpr,
6838                      const Sema::FormatStringType type, unsigned firstDataArg,
6839                      unsigned numDataArgs, bool isObjC, const char *beg,
6840                      Sema::FormatArgumentPassingKind APK,
6841                      ArrayRef<const Expr *> Args, unsigned formatIdx,
6842                      bool inFunctionCall, Sema::VariadicCallType CallType,
6843                      llvm::SmallBitVector &CheckedVarArgs,
6844                      UncoveredArgHandler &UncoveredArg)
6845       : CheckFormatHandler(s, fexpr, origFormatExpr, type, firstDataArg,
6846                            numDataArgs, beg, APK, Args, formatIdx,
6847                            inFunctionCall, CallType, CheckedVarArgs,
6848                            UncoveredArg) {}
6849 
6850   bool isObjCContext() const { return FSType == Sema::FST_NSString; }
6851 
6852   /// Returns true if '%@' specifiers are allowed in the format string.
6853   bool allowsObjCArg() const {
6854     return FSType == Sema::FST_NSString || FSType == Sema::FST_OSLog ||
6855            FSType == Sema::FST_OSTrace;
6856   }
6857 
6858   bool HandleInvalidPrintfConversionSpecifier(
6859                                       const analyze_printf::PrintfSpecifier &FS,
6860                                       const char *startSpecifier,
6861                                       unsigned specifierLen) override;
6862 
6863   void handleInvalidMaskType(StringRef MaskType) override;
6864 
6865   bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS,
6866                              const char *startSpecifier, unsigned specifierLen,
6867                              const TargetInfo &Target) override;
6868   bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
6869                        const char *StartSpecifier,
6870                        unsigned SpecifierLen,
6871                        const Expr *E);
6872 
6873   bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k,
6874                     const char *startSpecifier, unsigned specifierLen);
6875   void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS,
6876                            const analyze_printf::OptionalAmount &Amt,
6877                            unsigned type,
6878                            const char *startSpecifier, unsigned specifierLen);
6879   void HandleFlag(const analyze_printf::PrintfSpecifier &FS,
6880                   const analyze_printf::OptionalFlag &flag,
6881                   const char *startSpecifier, unsigned specifierLen);
6882   void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS,
6883                          const analyze_printf::OptionalFlag &ignoredFlag,
6884                          const analyze_printf::OptionalFlag &flag,
6885                          const char *startSpecifier, unsigned specifierLen);
6886   bool checkForCStrMembers(const analyze_printf::ArgType &AT,
6887                            const Expr *E);
6888 
6889   void HandleEmptyObjCModifierFlag(const char *startFlag,
6890                                    unsigned flagLen) override;
6891 
6892   void HandleInvalidObjCModifierFlag(const char *startFlag,
6893                                             unsigned flagLen) override;
6894 
6895   void HandleObjCFlagsWithNonObjCConversion(const char *flagsStart,
6896                                            const char *flagsEnd,
6897                                            const char *conversionPosition)
6898                                              override;
6899 };
6900 
6901 } // namespace
6902 
6903 bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier(
6904                                       const analyze_printf::PrintfSpecifier &FS,
6905                                       const char *startSpecifier,
6906                                       unsigned specifierLen) {
6907   const analyze_printf::PrintfConversionSpecifier &CS =
6908     FS.getConversionSpecifier();
6909 
6910   return HandleInvalidConversionSpecifier(FS.getArgIndex(),
6911                                           getLocationOfByte(CS.getStart()),
6912                                           startSpecifier, specifierLen,
6913                                           CS.getStart(), CS.getLength());
6914 }
6915 
6916 void CheckPrintfHandler::handleInvalidMaskType(StringRef MaskType) {
6917   S.Diag(getLocationOfByte(MaskType.data()), diag::err_invalid_mask_type_size);
6918 }
6919 
6920 bool CheckPrintfHandler::HandleAmount(
6921     const analyze_format_string::OptionalAmount &Amt, unsigned k,
6922     const char *startSpecifier, unsigned specifierLen) {
6923   if (Amt.hasDataArgument()) {
6924     if (ArgPassingKind != Sema::FAPK_VAList) {
6925       unsigned argIndex = Amt.getArgIndex();
6926       if (argIndex >= NumDataArgs) {
6927         EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg)
6928                                  << k,
6929                              getLocationOfByte(Amt.getStart()),
6930                              /*IsStringLocation*/ true,
6931                              getSpecifierRange(startSpecifier, specifierLen));
6932         // Don't do any more checking.  We will just emit
6933         // spurious errors.
6934         return false;
6935       }
6936 
6937       // Type check the data argument.  It should be an 'int'.
6938       // Although not in conformance with C99, we also allow the argument to be
6939       // an 'unsigned int' as that is a reasonably safe case.  GCC also
6940       // doesn't emit a warning for that case.
6941       CoveredArgs.set(argIndex);
6942       const Expr *Arg = getDataArg(argIndex);
6943       if (!Arg)
6944         return false;
6945 
6946       QualType T = Arg->getType();
6947 
6948       const analyze_printf::ArgType &AT = Amt.getArgType(S.Context);
6949       assert(AT.isValid());
6950 
6951       if (!AT.matchesType(S.Context, T)) {
6952         EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type)
6953                                << k << AT.getRepresentativeTypeName(S.Context)
6954                                << T << Arg->getSourceRange(),
6955                              getLocationOfByte(Amt.getStart()),
6956                              /*IsStringLocation*/true,
6957                              getSpecifierRange(startSpecifier, specifierLen));
6958         // Don't do any more checking.  We will just emit
6959         // spurious errors.
6960         return false;
6961       }
6962     }
6963   }
6964   return true;
6965 }
6966 
6967 void CheckPrintfHandler::HandleInvalidAmount(
6968                                       const analyze_printf::PrintfSpecifier &FS,
6969                                       const analyze_printf::OptionalAmount &Amt,
6970                                       unsigned type,
6971                                       const char *startSpecifier,
6972                                       unsigned specifierLen) {
6973   const analyze_printf::PrintfConversionSpecifier &CS =
6974     FS.getConversionSpecifier();
6975 
6976   FixItHint fixit =
6977     Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant
6978       ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(),
6979                                  Amt.getConstantLength()))
6980       : FixItHint();
6981 
6982   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount)
6983                          << type << CS.toString(),
6984                        getLocationOfByte(Amt.getStart()),
6985                        /*IsStringLocation*/true,
6986                        getSpecifierRange(startSpecifier, specifierLen),
6987                        fixit);
6988 }
6989 
6990 void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS,
6991                                     const analyze_printf::OptionalFlag &flag,
6992                                     const char *startSpecifier,
6993                                     unsigned specifierLen) {
6994   // Warn about pointless flag with a fixit removal.
6995   const analyze_printf::PrintfConversionSpecifier &CS =
6996     FS.getConversionSpecifier();
6997   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag)
6998                          << flag.toString() << CS.toString(),
6999                        getLocationOfByte(flag.getPosition()),
7000                        /*IsStringLocation*/true,
7001                        getSpecifierRange(startSpecifier, specifierLen),
7002                        FixItHint::CreateRemoval(
7003                          getSpecifierRange(flag.getPosition(), 1)));
7004 }
7005 
7006 void CheckPrintfHandler::HandleIgnoredFlag(
7007                                 const analyze_printf::PrintfSpecifier &FS,
7008                                 const analyze_printf::OptionalFlag &ignoredFlag,
7009                                 const analyze_printf::OptionalFlag &flag,
7010                                 const char *startSpecifier,
7011                                 unsigned specifierLen) {
7012   // Warn about ignored flag with a fixit removal.
7013   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag)
7014                          << ignoredFlag.toString() << flag.toString(),
7015                        getLocationOfByte(ignoredFlag.getPosition()),
7016                        /*IsStringLocation*/true,
7017                        getSpecifierRange(startSpecifier, specifierLen),
7018                        FixItHint::CreateRemoval(
7019                          getSpecifierRange(ignoredFlag.getPosition(), 1)));
7020 }
7021 
7022 void CheckPrintfHandler::HandleEmptyObjCModifierFlag(const char *startFlag,
7023                                                      unsigned flagLen) {
7024   // Warn about an empty flag.
7025   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_empty_objc_flag),
7026                        getLocationOfByte(startFlag),
7027                        /*IsStringLocation*/true,
7028                        getSpecifierRange(startFlag, flagLen));
7029 }
7030 
7031 void CheckPrintfHandler::HandleInvalidObjCModifierFlag(const char *startFlag,
7032                                                        unsigned flagLen) {
7033   // Warn about an invalid flag.
7034   auto Range = getSpecifierRange(startFlag, flagLen);
7035   StringRef flag(startFlag, flagLen);
7036   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_invalid_objc_flag) << flag,
7037                       getLocationOfByte(startFlag),
7038                       /*IsStringLocation*/true,
7039                       Range, FixItHint::CreateRemoval(Range));
7040 }
7041 
7042 void CheckPrintfHandler::HandleObjCFlagsWithNonObjCConversion(
7043     const char *flagsStart, const char *flagsEnd, const char *conversionPosition) {
7044     // Warn about using '[...]' without a '@' conversion.
7045     auto Range = getSpecifierRange(flagsStart, flagsEnd - flagsStart + 1);
7046     auto diag = diag::warn_printf_ObjCflags_without_ObjCConversion;
7047     EmitFormatDiagnostic(S.PDiag(diag) << StringRef(conversionPosition, 1),
7048                          getLocationOfByte(conversionPosition),
7049                          /*IsStringLocation*/true,
7050                          Range, FixItHint::CreateRemoval(Range));
7051 }
7052 
7053 // Determines if the specified is a C++ class or struct containing
7054 // a member with the specified name and kind (e.g. a CXXMethodDecl named
7055 // "c_str()").
7056 template<typename MemberKind>
7057 static llvm::SmallPtrSet<MemberKind*, 1>
7058 CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) {
7059   const RecordType *RT = Ty->getAs<RecordType>();
7060   llvm::SmallPtrSet<MemberKind*, 1> Results;
7061 
7062   if (!RT)
7063     return Results;
7064   const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
7065   if (!RD || !RD->getDefinition())
7066     return Results;
7067 
7068   LookupResult R(S, &S.Context.Idents.get(Name), SourceLocation(),
7069                  Sema::LookupMemberName);
7070   R.suppressDiagnostics();
7071 
7072   // We just need to include all members of the right kind turned up by the
7073   // filter, at this point.
7074   if (S.LookupQualifiedName(R, RT->getDecl()))
7075     for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
7076       NamedDecl *decl = (*I)->getUnderlyingDecl();
7077       if (MemberKind *FK = dyn_cast<MemberKind>(decl))
7078         Results.insert(FK);
7079     }
7080   return Results;
7081 }
7082 
7083 /// Check if we could call '.c_str()' on an object.
7084 ///
7085 /// FIXME: This returns the wrong results in some cases (if cv-qualifiers don't
7086 /// allow the call, or if it would be ambiguous).
7087 bool Sema::hasCStrMethod(const Expr *E) {
7088   using MethodSet = llvm::SmallPtrSet<CXXMethodDecl *, 1>;
7089 
7090   MethodSet Results =
7091       CXXRecordMembersNamed<CXXMethodDecl>("c_str", *this, E->getType());
7092   for (MethodSet::iterator MI = Results.begin(), ME = Results.end();
7093        MI != ME; ++MI)
7094     if ((*MI)->getMinRequiredArguments() == 0)
7095       return true;
7096   return false;
7097 }
7098 
7099 // Check if a (w)string was passed when a (w)char* was needed, and offer a
7100 // better diagnostic if so. AT is assumed to be valid.
7101 // Returns true when a c_str() conversion method is found.
7102 bool CheckPrintfHandler::checkForCStrMembers(
7103     const analyze_printf::ArgType &AT, const Expr *E) {
7104   using MethodSet = llvm::SmallPtrSet<CXXMethodDecl *, 1>;
7105 
7106   MethodSet Results =
7107       CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType());
7108 
7109   for (MethodSet::iterator MI = Results.begin(), ME = Results.end();
7110        MI != ME; ++MI) {
7111     const CXXMethodDecl *Method = *MI;
7112     if (Method->getMinRequiredArguments() == 0 &&
7113         AT.matchesType(S.Context, Method->getReturnType())) {
7114       // FIXME: Suggest parens if the expression needs them.
7115       SourceLocation EndLoc = S.getLocForEndOfToken(E->getEndLoc());
7116       S.Diag(E->getBeginLoc(), diag::note_printf_c_str)
7117           << "c_str()" << FixItHint::CreateInsertion(EndLoc, ".c_str()");
7118       return true;
7119     }
7120   }
7121 
7122   return false;
7123 }
7124 
7125 bool CheckPrintfHandler::HandlePrintfSpecifier(
7126     const analyze_printf::PrintfSpecifier &FS, const char *startSpecifier,
7127     unsigned specifierLen, const TargetInfo &Target) {
7128   using namespace analyze_format_string;
7129   using namespace analyze_printf;
7130 
7131   const PrintfConversionSpecifier &CS = FS.getConversionSpecifier();
7132 
7133   if (FS.consumesDataArgument()) {
7134     if (atFirstArg) {
7135         atFirstArg = false;
7136         usesPositionalArgs = FS.usesPositionalArg();
7137     }
7138     else if (usesPositionalArgs != FS.usesPositionalArg()) {
7139       HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()),
7140                                         startSpecifier, specifierLen);
7141       return false;
7142     }
7143   }
7144 
7145   // First check if the field width, precision, and conversion specifier
7146   // have matching data arguments.
7147   if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0,
7148                     startSpecifier, specifierLen)) {
7149     return false;
7150   }
7151 
7152   if (!HandleAmount(FS.getPrecision(), /* precision */ 1,
7153                     startSpecifier, specifierLen)) {
7154     return false;
7155   }
7156 
7157   if (!CS.consumesDataArgument()) {
7158     // FIXME: Technically specifying a precision or field width here
7159     // makes no sense.  Worth issuing a warning at some point.
7160     return true;
7161   }
7162 
7163   // Consume the argument.
7164   unsigned argIndex = FS.getArgIndex();
7165   if (argIndex < NumDataArgs) {
7166     // The check to see if the argIndex is valid will come later.
7167     // We set the bit here because we may exit early from this
7168     // function if we encounter some other error.
7169     CoveredArgs.set(argIndex);
7170   }
7171 
7172   // FreeBSD kernel extensions.
7173   if (CS.getKind() == ConversionSpecifier::FreeBSDbArg ||
7174       CS.getKind() == ConversionSpecifier::FreeBSDDArg) {
7175     // We need at least two arguments.
7176     if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex + 1))
7177       return false;
7178 
7179     // Claim the second argument.
7180     CoveredArgs.set(argIndex + 1);
7181 
7182     // Type check the first argument (int for %b, pointer for %D)
7183     const Expr *Ex = getDataArg(argIndex);
7184     const analyze_printf::ArgType &AT =
7185       (CS.getKind() == ConversionSpecifier::FreeBSDbArg) ?
7186         ArgType(S.Context.IntTy) : ArgType::CPointerTy;
7187     if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType()))
7188       EmitFormatDiagnostic(
7189           S.PDiag(diag::warn_format_conversion_argument_type_mismatch)
7190               << AT.getRepresentativeTypeName(S.Context) << Ex->getType()
7191               << false << Ex->getSourceRange(),
7192           Ex->getBeginLoc(), /*IsStringLocation*/ false,
7193           getSpecifierRange(startSpecifier, specifierLen));
7194 
7195     // Type check the second argument (char * for both %b and %D)
7196     Ex = getDataArg(argIndex + 1);
7197     const analyze_printf::ArgType &AT2 = ArgType::CStrTy;
7198     if (AT2.isValid() && !AT2.matchesType(S.Context, Ex->getType()))
7199       EmitFormatDiagnostic(
7200           S.PDiag(diag::warn_format_conversion_argument_type_mismatch)
7201               << AT2.getRepresentativeTypeName(S.Context) << Ex->getType()
7202               << false << Ex->getSourceRange(),
7203           Ex->getBeginLoc(), /*IsStringLocation*/ false,
7204           getSpecifierRange(startSpecifier, specifierLen));
7205 
7206      return true;
7207   }
7208 
7209   // Check for using an Objective-C specific conversion specifier
7210   // in a non-ObjC literal.
7211   if (!allowsObjCArg() && CS.isObjCArg()) {
7212     return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier,
7213                                                   specifierLen);
7214   }
7215 
7216   // %P can only be used with os_log.
7217   if (FSType != Sema::FST_OSLog && CS.getKind() == ConversionSpecifier::PArg) {
7218     return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier,
7219                                                   specifierLen);
7220   }
7221 
7222   // %n is not allowed with os_log.
7223   if (FSType == Sema::FST_OSLog && CS.getKind() == ConversionSpecifier::nArg) {
7224     EmitFormatDiagnostic(S.PDiag(diag::warn_os_log_format_narg),
7225                          getLocationOfByte(CS.getStart()),
7226                          /*IsStringLocation*/ false,
7227                          getSpecifierRange(startSpecifier, specifierLen));
7228 
7229     return true;
7230   }
7231 
7232   // Only scalars are allowed for os_trace.
7233   if (FSType == Sema::FST_OSTrace &&
7234       (CS.getKind() == ConversionSpecifier::PArg ||
7235        CS.getKind() == ConversionSpecifier::sArg ||
7236        CS.getKind() == ConversionSpecifier::ObjCObjArg)) {
7237     return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier,
7238                                                   specifierLen);
7239   }
7240 
7241   // Check for use of public/private annotation outside of os_log().
7242   if (FSType != Sema::FST_OSLog) {
7243     if (FS.isPublic().isSet()) {
7244       EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_annotation)
7245                                << "public",
7246                            getLocationOfByte(FS.isPublic().getPosition()),
7247                            /*IsStringLocation*/ false,
7248                            getSpecifierRange(startSpecifier, specifierLen));
7249     }
7250     if (FS.isPrivate().isSet()) {
7251       EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_annotation)
7252                                << "private",
7253                            getLocationOfByte(FS.isPrivate().getPosition()),
7254                            /*IsStringLocation*/ false,
7255                            getSpecifierRange(startSpecifier, specifierLen));
7256     }
7257   }
7258 
7259   const llvm::Triple &Triple = Target.getTriple();
7260   if (CS.getKind() == ConversionSpecifier::nArg &&
7261       (Triple.isAndroid() || Triple.isOSFuchsia())) {
7262     EmitFormatDiagnostic(S.PDiag(diag::warn_printf_narg_not_supported),
7263                          getLocationOfByte(CS.getStart()),
7264                          /*IsStringLocation*/ false,
7265                          getSpecifierRange(startSpecifier, specifierLen));
7266   }
7267 
7268   // Check for invalid use of field width
7269   if (!FS.hasValidFieldWidth()) {
7270     HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0,
7271         startSpecifier, specifierLen);
7272   }
7273 
7274   // Check for invalid use of precision
7275   if (!FS.hasValidPrecision()) {
7276     HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1,
7277         startSpecifier, specifierLen);
7278   }
7279 
7280   // Precision is mandatory for %P specifier.
7281   if (CS.getKind() == ConversionSpecifier::PArg &&
7282       FS.getPrecision().getHowSpecified() == OptionalAmount::NotSpecified) {
7283     EmitFormatDiagnostic(S.PDiag(diag::warn_format_P_no_precision),
7284                          getLocationOfByte(startSpecifier),
7285                          /*IsStringLocation*/ false,
7286                          getSpecifierRange(startSpecifier, specifierLen));
7287   }
7288 
7289   // Check each flag does not conflict with any other component.
7290   if (!FS.hasValidThousandsGroupingPrefix())
7291     HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen);
7292   if (!FS.hasValidLeadingZeros())
7293     HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen);
7294   if (!FS.hasValidPlusPrefix())
7295     HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen);
7296   if (!FS.hasValidSpacePrefix())
7297     HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen);
7298   if (!FS.hasValidAlternativeForm())
7299     HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen);
7300   if (!FS.hasValidLeftJustified())
7301     HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen);
7302 
7303   // Check that flags are not ignored by another flag
7304   if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+'
7305     HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(),
7306         startSpecifier, specifierLen);
7307   if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-'
7308     HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(),
7309             startSpecifier, specifierLen);
7310 
7311   // Check the length modifier is valid with the given conversion specifier.
7312   if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo(),
7313                                  S.getLangOpts()))
7314     HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
7315                                 diag::warn_format_nonsensical_length);
7316   else if (!FS.hasStandardLengthModifier())
7317     HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen);
7318   else if (!FS.hasStandardLengthConversionCombination())
7319     HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
7320                                 diag::warn_format_non_standard_conversion_spec);
7321 
7322   if (!FS.hasStandardConversionSpecifier(S.getLangOpts()))
7323     HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen);
7324 
7325   // The remaining checks depend on the data arguments.
7326   if (ArgPassingKind == Sema::FAPK_VAList)
7327     return true;
7328 
7329   if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
7330     return false;
7331 
7332   const Expr *Arg = getDataArg(argIndex);
7333   if (!Arg)
7334     return true;
7335 
7336   return checkFormatExpr(FS, startSpecifier, specifierLen, Arg);
7337 }
7338 
7339 static bool requiresParensToAddCast(const Expr *E) {
7340   // FIXME: We should have a general way to reason about operator
7341   // precedence and whether parens are actually needed here.
7342   // Take care of a few common cases where they aren't.
7343   const Expr *Inside = E->IgnoreImpCasts();
7344   if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Inside))
7345     Inside = POE->getSyntacticForm()->IgnoreImpCasts();
7346 
7347   switch (Inside->getStmtClass()) {
7348   case Stmt::ArraySubscriptExprClass:
7349   case Stmt::CallExprClass:
7350   case Stmt::CharacterLiteralClass:
7351   case Stmt::CXXBoolLiteralExprClass:
7352   case Stmt::DeclRefExprClass:
7353   case Stmt::FloatingLiteralClass:
7354   case Stmt::IntegerLiteralClass:
7355   case Stmt::MemberExprClass:
7356   case Stmt::ObjCArrayLiteralClass:
7357   case Stmt::ObjCBoolLiteralExprClass:
7358   case Stmt::ObjCBoxedExprClass:
7359   case Stmt::ObjCDictionaryLiteralClass:
7360   case Stmt::ObjCEncodeExprClass:
7361   case Stmt::ObjCIvarRefExprClass:
7362   case Stmt::ObjCMessageExprClass:
7363   case Stmt::ObjCPropertyRefExprClass:
7364   case Stmt::ObjCStringLiteralClass:
7365   case Stmt::ObjCSubscriptRefExprClass:
7366   case Stmt::ParenExprClass:
7367   case Stmt::StringLiteralClass:
7368   case Stmt::UnaryOperatorClass:
7369     return false;
7370   default:
7371     return true;
7372   }
7373 }
7374 
7375 static std::pair<QualType, StringRef>
7376 shouldNotPrintDirectly(const ASTContext &Context,
7377                        QualType IntendedTy,
7378                        const Expr *E) {
7379   // Use a 'while' to peel off layers of typedefs.
7380   QualType TyTy = IntendedTy;
7381   while (const TypedefType *UserTy = TyTy->getAs<TypedefType>()) {
7382     StringRef Name = UserTy->getDecl()->getName();
7383     QualType CastTy = llvm::StringSwitch<QualType>(Name)
7384       .Case("CFIndex", Context.getNSIntegerType())
7385       .Case("NSInteger", Context.getNSIntegerType())
7386       .Case("NSUInteger", Context.getNSUIntegerType())
7387       .Case("SInt32", Context.IntTy)
7388       .Case("UInt32", Context.UnsignedIntTy)
7389       .Default(QualType());
7390 
7391     if (!CastTy.isNull())
7392       return std::make_pair(CastTy, Name);
7393 
7394     TyTy = UserTy->desugar();
7395   }
7396 
7397   // Strip parens if necessary.
7398   if (const ParenExpr *PE = dyn_cast<ParenExpr>(E))
7399     return shouldNotPrintDirectly(Context,
7400                                   PE->getSubExpr()->getType(),
7401                                   PE->getSubExpr());
7402 
7403   // If this is a conditional expression, then its result type is constructed
7404   // via usual arithmetic conversions and thus there might be no necessary
7405   // typedef sugar there.  Recurse to operands to check for NSInteger &
7406   // Co. usage condition.
7407   if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
7408     QualType TrueTy, FalseTy;
7409     StringRef TrueName, FalseName;
7410 
7411     std::tie(TrueTy, TrueName) =
7412       shouldNotPrintDirectly(Context,
7413                              CO->getTrueExpr()->getType(),
7414                              CO->getTrueExpr());
7415     std::tie(FalseTy, FalseName) =
7416       shouldNotPrintDirectly(Context,
7417                              CO->getFalseExpr()->getType(),
7418                              CO->getFalseExpr());
7419 
7420     if (TrueTy == FalseTy)
7421       return std::make_pair(TrueTy, TrueName);
7422     else if (TrueTy.isNull())
7423       return std::make_pair(FalseTy, FalseName);
7424     else if (FalseTy.isNull())
7425       return std::make_pair(TrueTy, TrueName);
7426   }
7427 
7428   return std::make_pair(QualType(), StringRef());
7429 }
7430 
7431 /// Return true if \p ICE is an implicit argument promotion of an arithmetic
7432 /// type. Bit-field 'promotions' from a higher ranked type to a lower ranked
7433 /// type do not count.
7434 static bool
7435 isArithmeticArgumentPromotion(Sema &S, const ImplicitCastExpr *ICE) {
7436   QualType From = ICE->getSubExpr()->getType();
7437   QualType To = ICE->getType();
7438   // It's an integer promotion if the destination type is the promoted
7439   // source type.
7440   if (ICE->getCastKind() == CK_IntegralCast &&
7441       S.Context.isPromotableIntegerType(From) &&
7442       S.Context.getPromotedIntegerType(From) == To)
7443     return true;
7444   // Look through vector types, since we do default argument promotion for
7445   // those in OpenCL.
7446   if (const auto *VecTy = From->getAs<ExtVectorType>())
7447     From = VecTy->getElementType();
7448   if (const auto *VecTy = To->getAs<ExtVectorType>())
7449     To = VecTy->getElementType();
7450   // It's a floating promotion if the source type is a lower rank.
7451   return ICE->getCastKind() == CK_FloatingCast &&
7452          S.Context.getFloatingTypeOrder(From, To) < 0;
7453 }
7454 
7455 static analyze_format_string::ArgType::MatchKind
7456 handleFormatSignedness(analyze_format_string::ArgType::MatchKind Match,
7457                        DiagnosticsEngine &Diags, SourceLocation Loc) {
7458   if (Match == analyze_format_string::ArgType::NoMatchSignedness) {
7459     Match =
7460         Diags.isIgnored(
7461             diag::warn_format_conversion_argument_type_mismatch_signedness, Loc)
7462             ? analyze_format_string::ArgType::Match
7463             : analyze_format_string::ArgType::NoMatch;
7464   }
7465   return Match;
7466 }
7467 
7468 bool
7469 CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
7470                                     const char *StartSpecifier,
7471                                     unsigned SpecifierLen,
7472                                     const Expr *E) {
7473   using namespace analyze_format_string;
7474   using namespace analyze_printf;
7475 
7476   // Now type check the data expression that matches the
7477   // format specifier.
7478   const analyze_printf::ArgType &AT = FS.getArgType(S.Context, isObjCContext());
7479   if (!AT.isValid())
7480     return true;
7481 
7482   QualType ExprTy = E->getType();
7483   while (const TypeOfExprType *TET = dyn_cast<TypeOfExprType>(ExprTy)) {
7484     ExprTy = TET->getUnderlyingExpr()->getType();
7485   }
7486 
7487   // When using the format attribute in C++, you can receive a function or an
7488   // array that will necessarily decay to a pointer when passed to the final
7489   // format consumer. Apply decay before type comparison.
7490   if (ExprTy->canDecayToPointerType())
7491     ExprTy = S.Context.getDecayedType(ExprTy);
7492 
7493   // Diagnose attempts to print a boolean value as a character. Unlike other
7494   // -Wformat diagnostics, this is fine from a type perspective, but it still
7495   // doesn't make sense.
7496   if (FS.getConversionSpecifier().getKind() == ConversionSpecifier::cArg &&
7497       E->isKnownToHaveBooleanValue()) {
7498     const CharSourceRange &CSR =
7499         getSpecifierRange(StartSpecifier, SpecifierLen);
7500     SmallString<4> FSString;
7501     llvm::raw_svector_ostream os(FSString);
7502     FS.toString(os);
7503     EmitFormatDiagnostic(S.PDiag(diag::warn_format_bool_as_character)
7504                              << FSString,
7505                          E->getExprLoc(), false, CSR);
7506     return true;
7507   }
7508 
7509   // Diagnose attempts to use '%P' with ObjC object types, which will result in
7510   // dumping raw class data (like is-a pointer), not actual data.
7511   if (FS.getConversionSpecifier().getKind() == ConversionSpecifier::PArg &&
7512       ExprTy->isObjCObjectPointerType()) {
7513     const CharSourceRange &CSR =
7514         getSpecifierRange(StartSpecifier, SpecifierLen);
7515     EmitFormatDiagnostic(S.PDiag(diag::warn_format_P_with_objc_pointer),
7516                          E->getExprLoc(), false, CSR);
7517     return true;
7518   }
7519 
7520   ArgType::MatchKind ImplicitMatch = ArgType::NoMatch;
7521   ArgType::MatchKind Match = AT.matchesType(S.Context, ExprTy);
7522   ArgType::MatchKind OrigMatch = Match;
7523 
7524   Match = handleFormatSignedness(Match, S.getDiagnostics(), E->getExprLoc());
7525   if (Match == ArgType::Match)
7526     return true;
7527 
7528   // NoMatchPromotionTypeConfusion should be only returned in ImplictCastExpr
7529   assert(Match != ArgType::NoMatchPromotionTypeConfusion);
7530 
7531   // Look through argument promotions for our error message's reported type.
7532   // This includes the integral and floating promotions, but excludes array
7533   // and function pointer decay (seeing that an argument intended to be a
7534   // string has type 'char [6]' is probably more confusing than 'char *') and
7535   // certain bitfield promotions (bitfields can be 'demoted' to a lesser type).
7536   if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
7537     if (isArithmeticArgumentPromotion(S, ICE)) {
7538       E = ICE->getSubExpr();
7539       ExprTy = E->getType();
7540 
7541       // Check if we didn't match because of an implicit cast from a 'char'
7542       // or 'short' to an 'int'.  This is done because printf is a varargs
7543       // function.
7544       if (ICE->getType() == S.Context.IntTy ||
7545           ICE->getType() == S.Context.UnsignedIntTy) {
7546         // All further checking is done on the subexpression
7547         ImplicitMatch = AT.matchesType(S.Context, ExprTy);
7548         if (OrigMatch == ArgType::NoMatchSignedness &&
7549             ImplicitMatch != ArgType::NoMatchSignedness)
7550           // If the original match was a signedness match this match on the
7551           // implicit cast type also need to be signedness match otherwise we
7552           // might introduce new unexpected warnings from -Wformat-signedness.
7553           return true;
7554         ImplicitMatch = handleFormatSignedness(
7555             ImplicitMatch, S.getDiagnostics(), E->getExprLoc());
7556         if (ImplicitMatch == ArgType::Match)
7557           return true;
7558       }
7559     }
7560   } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) {
7561     // Special case for 'a', which has type 'int' in C.
7562     // Note, however, that we do /not/ want to treat multibyte constants like
7563     // 'MooV' as characters! This form is deprecated but still exists. In
7564     // addition, don't treat expressions as of type 'char' if one byte length
7565     // modifier is provided.
7566     if (ExprTy == S.Context.IntTy &&
7567         FS.getLengthModifier().getKind() != LengthModifier::AsChar)
7568       if (llvm::isUIntN(S.Context.getCharWidth(), CL->getValue())) {
7569         ExprTy = S.Context.CharTy;
7570         // To improve check results, we consider a character literal in C
7571         // to be a 'char' rather than an 'int'. 'printf("%hd", 'a');' is
7572         // more likely a type confusion situation, so we will suggest to
7573         // use '%hhd' instead by discarding the MatchPromotion.
7574         if (Match == ArgType::MatchPromotion)
7575           Match = ArgType::NoMatch;
7576       }
7577   }
7578   if (Match == ArgType::MatchPromotion) {
7579     // WG14 N2562 only clarified promotions in *printf
7580     // For NSLog in ObjC, just preserve -Wformat behavior
7581     if (!S.getLangOpts().ObjC &&
7582         ImplicitMatch != ArgType::NoMatchPromotionTypeConfusion &&
7583         ImplicitMatch != ArgType::NoMatchTypeConfusion)
7584       return true;
7585     Match = ArgType::NoMatch;
7586   }
7587   if (ImplicitMatch == ArgType::NoMatchPedantic ||
7588       ImplicitMatch == ArgType::NoMatchTypeConfusion)
7589     Match = ImplicitMatch;
7590   assert(Match != ArgType::MatchPromotion);
7591 
7592   // Look through unscoped enums to their underlying type.
7593   bool IsEnum = false;
7594   bool IsScopedEnum = false;
7595   QualType IntendedTy = ExprTy;
7596   if (auto EnumTy = ExprTy->getAs<EnumType>()) {
7597     IntendedTy = EnumTy->getDecl()->getIntegerType();
7598     if (EnumTy->isUnscopedEnumerationType()) {
7599       ExprTy = IntendedTy;
7600       // This controls whether we're talking about the underlying type or not,
7601       // which we only want to do when it's an unscoped enum.
7602       IsEnum = true;
7603     } else {
7604       IsScopedEnum = true;
7605     }
7606   }
7607 
7608   // %C in an Objective-C context prints a unichar, not a wchar_t.
7609   // If the argument is an integer of some kind, believe the %C and suggest
7610   // a cast instead of changing the conversion specifier.
7611   if (isObjCContext() &&
7612       FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) {
7613     if (ExprTy->isIntegralOrUnscopedEnumerationType() &&
7614         !ExprTy->isCharType()) {
7615       // 'unichar' is defined as a typedef of unsigned short, but we should
7616       // prefer using the typedef if it is visible.
7617       IntendedTy = S.Context.UnsignedShortTy;
7618 
7619       // While we are here, check if the value is an IntegerLiteral that happens
7620       // to be within the valid range.
7621       if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) {
7622         const llvm::APInt &V = IL->getValue();
7623         if (V.getActiveBits() <= S.Context.getTypeSize(IntendedTy))
7624           return true;
7625       }
7626 
7627       LookupResult Result(S, &S.Context.Idents.get("unichar"), E->getBeginLoc(),
7628                           Sema::LookupOrdinaryName);
7629       if (S.LookupName(Result, S.getCurScope())) {
7630         NamedDecl *ND = Result.getFoundDecl();
7631         if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
7632           if (TD->getUnderlyingType() == IntendedTy)
7633             IntendedTy = S.Context.getTypedefType(TD);
7634       }
7635     }
7636   }
7637 
7638   // Special-case some of Darwin's platform-independence types by suggesting
7639   // casts to primitive types that are known to be large enough.
7640   bool ShouldNotPrintDirectly = false; StringRef CastTyName;
7641   if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
7642     QualType CastTy;
7643     std::tie(CastTy, CastTyName) = shouldNotPrintDirectly(S.Context, IntendedTy, E);
7644     if (!CastTy.isNull()) {
7645       // %zi/%zu and %td/%tu are OK to use for NSInteger/NSUInteger of type int
7646       // (long in ASTContext). Only complain to pedants or when they're the
7647       // underlying type of a scoped enum (which always needs a cast).
7648       if (!IsScopedEnum &&
7649           (CastTyName == "NSInteger" || CastTyName == "NSUInteger") &&
7650           (AT.isSizeT() || AT.isPtrdiffT()) &&
7651           AT.matchesType(S.Context, CastTy))
7652         Match = ArgType::NoMatchPedantic;
7653       IntendedTy = CastTy;
7654       ShouldNotPrintDirectly = true;
7655     }
7656   }
7657 
7658   // We may be able to offer a FixItHint if it is a supported type.
7659   PrintfSpecifier fixedFS = FS;
7660   bool Success =
7661       fixedFS.fixType(IntendedTy, S.getLangOpts(), S.Context, isObjCContext());
7662 
7663   if (Success) {
7664     // Get the fix string from the fixed format specifier
7665     SmallString<16> buf;
7666     llvm::raw_svector_ostream os(buf);
7667     fixedFS.toString(os);
7668 
7669     CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen);
7670 
7671     if (IntendedTy == ExprTy && !ShouldNotPrintDirectly && !IsScopedEnum) {
7672       unsigned Diag;
7673       switch (Match) {
7674       case ArgType::Match:
7675       case ArgType::MatchPromotion:
7676       case ArgType::NoMatchPromotionTypeConfusion:
7677       case ArgType::NoMatchSignedness:
7678         llvm_unreachable("expected non-matching");
7679       case ArgType::NoMatchPedantic:
7680         Diag = diag::warn_format_conversion_argument_type_mismatch_pedantic;
7681         break;
7682       case ArgType::NoMatchTypeConfusion:
7683         Diag = diag::warn_format_conversion_argument_type_mismatch_confusion;
7684         break;
7685       case ArgType::NoMatch:
7686         Diag = diag::warn_format_conversion_argument_type_mismatch;
7687         break;
7688       }
7689 
7690       // In this case, the specifier is wrong and should be changed to match
7691       // the argument.
7692       EmitFormatDiagnostic(S.PDiag(Diag)
7693                                << AT.getRepresentativeTypeName(S.Context)
7694                                << IntendedTy << IsEnum << E->getSourceRange(),
7695                            E->getBeginLoc(),
7696                            /*IsStringLocation*/ false, SpecRange,
7697                            FixItHint::CreateReplacement(SpecRange, os.str()));
7698     } else {
7699       // The canonical type for formatting this value is different from the
7700       // actual type of the expression. (This occurs, for example, with Darwin's
7701       // NSInteger on 32-bit platforms, where it is typedef'd as 'int', but
7702       // should be printed as 'long' for 64-bit compatibility.)
7703       // Rather than emitting a normal format/argument mismatch, we want to
7704       // add a cast to the recommended type (and correct the format string
7705       // if necessary). We should also do so for scoped enumerations.
7706       SmallString<16> CastBuf;
7707       llvm::raw_svector_ostream CastFix(CastBuf);
7708       CastFix << (S.LangOpts.CPlusPlus ? "static_cast<" : "(");
7709       IntendedTy.print(CastFix, S.Context.getPrintingPolicy());
7710       CastFix << (S.LangOpts.CPlusPlus ? ">" : ")");
7711 
7712       SmallVector<FixItHint,4> Hints;
7713       ArgType::MatchKind IntendedMatch = AT.matchesType(S.Context, IntendedTy);
7714       IntendedMatch = handleFormatSignedness(IntendedMatch, S.getDiagnostics(),
7715                                              E->getExprLoc());
7716       if ((IntendedMatch != ArgType::Match) || ShouldNotPrintDirectly)
7717         Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str()));
7718 
7719       if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) {
7720         // If there's already a cast present, just replace it.
7721         SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc());
7722         Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str()));
7723 
7724       } else if (!requiresParensToAddCast(E) && !S.LangOpts.CPlusPlus) {
7725         // If the expression has high enough precedence,
7726         // just write the C-style cast.
7727         Hints.push_back(
7728             FixItHint::CreateInsertion(E->getBeginLoc(), CastFix.str()));
7729       } else {
7730         // Otherwise, add parens around the expression as well as the cast.
7731         CastFix << "(";
7732         Hints.push_back(
7733             FixItHint::CreateInsertion(E->getBeginLoc(), CastFix.str()));
7734 
7735         // We don't use getLocForEndOfToken because it returns invalid source
7736         // locations for macro expansions (by design).
7737         SourceLocation EndLoc = S.SourceMgr.getSpellingLoc(E->getEndLoc());
7738         SourceLocation After = EndLoc.getLocWithOffset(
7739             Lexer::MeasureTokenLength(EndLoc, S.SourceMgr, S.LangOpts));
7740         Hints.push_back(FixItHint::CreateInsertion(After, ")"));
7741       }
7742 
7743       if (ShouldNotPrintDirectly && !IsScopedEnum) {
7744         // The expression has a type that should not be printed directly.
7745         // We extract the name from the typedef because we don't want to show
7746         // the underlying type in the diagnostic.
7747         StringRef Name;
7748         if (const auto *TypedefTy = ExprTy->getAs<TypedefType>())
7749           Name = TypedefTy->getDecl()->getName();
7750         else
7751           Name = CastTyName;
7752         unsigned Diag = Match == ArgType::NoMatchPedantic
7753                             ? diag::warn_format_argument_needs_cast_pedantic
7754                             : diag::warn_format_argument_needs_cast;
7755         EmitFormatDiagnostic(S.PDiag(Diag) << Name << IntendedTy << IsEnum
7756                                            << E->getSourceRange(),
7757                              E->getBeginLoc(), /*IsStringLocation=*/false,
7758                              SpecRange, Hints);
7759       } else {
7760         // In this case, the expression could be printed using a different
7761         // specifier, but we've decided that the specifier is probably correct
7762         // and we should cast instead. Just use the normal warning message.
7763 
7764         unsigned Diag =
7765             IsScopedEnum
7766                 ? diag::warn_format_conversion_argument_type_mismatch_pedantic
7767                 : diag::warn_format_conversion_argument_type_mismatch;
7768 
7769         EmitFormatDiagnostic(
7770             S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context) << ExprTy
7771                           << IsEnum << E->getSourceRange(),
7772             E->getBeginLoc(), /*IsStringLocation*/ false, SpecRange, Hints);
7773       }
7774     }
7775   } else {
7776     const CharSourceRange &CSR = getSpecifierRange(StartSpecifier,
7777                                                    SpecifierLen);
7778     // Since the warning for passing non-POD types to variadic functions
7779     // was deferred until now, we emit a warning for non-POD
7780     // arguments here.
7781     bool EmitTypeMismatch = false;
7782     switch (S.isValidVarArgType(ExprTy)) {
7783     case Sema::VAK_Valid:
7784     case Sema::VAK_ValidInCXX11: {
7785       unsigned Diag;
7786       switch (Match) {
7787       case ArgType::Match:
7788       case ArgType::MatchPromotion:
7789       case ArgType::NoMatchPromotionTypeConfusion:
7790       case ArgType::NoMatchSignedness:
7791         llvm_unreachable("expected non-matching");
7792       case ArgType::NoMatchPedantic:
7793         Diag = diag::warn_format_conversion_argument_type_mismatch_pedantic;
7794         break;
7795       case ArgType::NoMatchTypeConfusion:
7796         Diag = diag::warn_format_conversion_argument_type_mismatch_confusion;
7797         break;
7798       case ArgType::NoMatch:
7799         Diag = diag::warn_format_conversion_argument_type_mismatch;
7800         break;
7801       }
7802 
7803       EmitFormatDiagnostic(
7804           S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context) << ExprTy
7805                         << IsEnum << CSR << E->getSourceRange(),
7806           E->getBeginLoc(), /*IsStringLocation*/ false, CSR);
7807       break;
7808     }
7809     case Sema::VAK_Undefined:
7810     case Sema::VAK_MSVCUndefined:
7811       if (CallType == Sema::VariadicDoesNotApply) {
7812         EmitTypeMismatch = true;
7813       } else {
7814         EmitFormatDiagnostic(
7815             S.PDiag(diag::warn_non_pod_vararg_with_format_string)
7816                 << S.getLangOpts().CPlusPlus11 << ExprTy << CallType
7817                 << AT.getRepresentativeTypeName(S.Context) << CSR
7818                 << E->getSourceRange(),
7819             E->getBeginLoc(), /*IsStringLocation*/ false, CSR);
7820         checkForCStrMembers(AT, E);
7821       }
7822       break;
7823 
7824     case Sema::VAK_Invalid:
7825       if (CallType == Sema::VariadicDoesNotApply)
7826         EmitTypeMismatch = true;
7827       else if (ExprTy->isObjCObjectType())
7828         EmitFormatDiagnostic(
7829             S.PDiag(diag::err_cannot_pass_objc_interface_to_vararg_format)
7830                 << S.getLangOpts().CPlusPlus11 << ExprTy << CallType
7831                 << AT.getRepresentativeTypeName(S.Context) << CSR
7832                 << E->getSourceRange(),
7833             E->getBeginLoc(), /*IsStringLocation*/ false, CSR);
7834       else
7835         // FIXME: If this is an initializer list, suggest removing the braces
7836         // or inserting a cast to the target type.
7837         S.Diag(E->getBeginLoc(), diag::err_cannot_pass_to_vararg_format)
7838             << isa<InitListExpr>(E) << ExprTy << CallType
7839             << AT.getRepresentativeTypeName(S.Context) << E->getSourceRange();
7840       break;
7841     }
7842 
7843     if (EmitTypeMismatch) {
7844       // The function is not variadic, so we do not generate warnings about
7845       // being allowed to pass that object as a variadic argument. Instead,
7846       // since there are inherently no printf specifiers for types which cannot
7847       // be passed as variadic arguments, emit a plain old specifier mismatch
7848       // argument.
7849       EmitFormatDiagnostic(
7850           S.PDiag(diag::warn_format_conversion_argument_type_mismatch)
7851               << AT.getRepresentativeTypeName(S.Context) << ExprTy << false
7852               << E->getSourceRange(),
7853           E->getBeginLoc(), false, CSR);
7854     }
7855 
7856     assert(FirstDataArg + FS.getArgIndex() < CheckedVarArgs.size() &&
7857            "format string specifier index out of range");
7858     CheckedVarArgs[FirstDataArg + FS.getArgIndex()] = true;
7859   }
7860 
7861   return true;
7862 }
7863 
7864 //===--- CHECK: Scanf format string checking ------------------------------===//
7865 
7866 namespace {
7867 
7868 class CheckScanfHandler : public CheckFormatHandler {
7869 public:
7870   CheckScanfHandler(Sema &s, const FormatStringLiteral *fexpr,
7871                     const Expr *origFormatExpr, Sema::FormatStringType type,
7872                     unsigned firstDataArg, unsigned numDataArgs,
7873                     const char *beg, Sema::FormatArgumentPassingKind APK,
7874                     ArrayRef<const Expr *> Args, unsigned formatIdx,
7875                     bool inFunctionCall, Sema::VariadicCallType CallType,
7876                     llvm::SmallBitVector &CheckedVarArgs,
7877                     UncoveredArgHandler &UncoveredArg)
7878       : CheckFormatHandler(s, fexpr, origFormatExpr, type, firstDataArg,
7879                            numDataArgs, beg, APK, Args, formatIdx,
7880                            inFunctionCall, CallType, CheckedVarArgs,
7881                            UncoveredArg) {}
7882 
7883   bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS,
7884                             const char *startSpecifier,
7885                             unsigned specifierLen) override;
7886 
7887   bool HandleInvalidScanfConversionSpecifier(
7888           const analyze_scanf::ScanfSpecifier &FS,
7889           const char *startSpecifier,
7890           unsigned specifierLen) override;
7891 
7892   void HandleIncompleteScanList(const char *start, const char *end) override;
7893 };
7894 
7895 } // namespace
7896 
7897 void CheckScanfHandler::HandleIncompleteScanList(const char *start,
7898                                                  const char *end) {
7899   EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete),
7900                        getLocationOfByte(end), /*IsStringLocation*/true,
7901                        getSpecifierRange(start, end - start));
7902 }
7903 
7904 bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier(
7905                                         const analyze_scanf::ScanfSpecifier &FS,
7906                                         const char *startSpecifier,
7907                                         unsigned specifierLen) {
7908   const analyze_scanf::ScanfConversionSpecifier &CS =
7909     FS.getConversionSpecifier();
7910 
7911   return HandleInvalidConversionSpecifier(FS.getArgIndex(),
7912                                           getLocationOfByte(CS.getStart()),
7913                                           startSpecifier, specifierLen,
7914                                           CS.getStart(), CS.getLength());
7915 }
7916 
7917 bool CheckScanfHandler::HandleScanfSpecifier(
7918                                        const analyze_scanf::ScanfSpecifier &FS,
7919                                        const char *startSpecifier,
7920                                        unsigned specifierLen) {
7921   using namespace analyze_scanf;
7922   using namespace analyze_format_string;
7923 
7924   const ScanfConversionSpecifier &CS = FS.getConversionSpecifier();
7925 
7926   // Handle case where '%' and '*' don't consume an argument.  These shouldn't
7927   // be used to decide if we are using positional arguments consistently.
7928   if (FS.consumesDataArgument()) {
7929     if (atFirstArg) {
7930       atFirstArg = false;
7931       usesPositionalArgs = FS.usesPositionalArg();
7932     }
7933     else if (usesPositionalArgs != FS.usesPositionalArg()) {
7934       HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()),
7935                                         startSpecifier, specifierLen);
7936       return false;
7937     }
7938   }
7939 
7940   // Check if the field with is non-zero.
7941   const OptionalAmount &Amt = FS.getFieldWidth();
7942   if (Amt.getHowSpecified() == OptionalAmount::Constant) {
7943     if (Amt.getConstantAmount() == 0) {
7944       const CharSourceRange &R = getSpecifierRange(Amt.getStart(),
7945                                                    Amt.getConstantLength());
7946       EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width),
7947                            getLocationOfByte(Amt.getStart()),
7948                            /*IsStringLocation*/true, R,
7949                            FixItHint::CreateRemoval(R));
7950     }
7951   }
7952 
7953   if (!FS.consumesDataArgument()) {
7954     // FIXME: Technically specifying a precision or field width here
7955     // makes no sense.  Worth issuing a warning at some point.
7956     return true;
7957   }
7958 
7959   // Consume the argument.
7960   unsigned argIndex = FS.getArgIndex();
7961   if (argIndex < NumDataArgs) {
7962       // The check to see if the argIndex is valid will come later.
7963       // We set the bit here because we may exit early from this
7964       // function if we encounter some other error.
7965     CoveredArgs.set(argIndex);
7966   }
7967 
7968   // Check the length modifier is valid with the given conversion specifier.
7969   if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo(),
7970                                  S.getLangOpts()))
7971     HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
7972                                 diag::warn_format_nonsensical_length);
7973   else if (!FS.hasStandardLengthModifier())
7974     HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen);
7975   else if (!FS.hasStandardLengthConversionCombination())
7976     HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
7977                                 diag::warn_format_non_standard_conversion_spec);
7978 
7979   if (!FS.hasStandardConversionSpecifier(S.getLangOpts()))
7980     HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen);
7981 
7982   // The remaining checks depend on the data arguments.
7983   if (ArgPassingKind == Sema::FAPK_VAList)
7984     return true;
7985 
7986   if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
7987     return false;
7988 
7989   // Check that the argument type matches the format specifier.
7990   const Expr *Ex = getDataArg(argIndex);
7991   if (!Ex)
7992     return true;
7993 
7994   const analyze_format_string::ArgType &AT = FS.getArgType(S.Context);
7995 
7996   if (!AT.isValid()) {
7997     return true;
7998   }
7999 
8000   analyze_format_string::ArgType::MatchKind Match =
8001       AT.matchesType(S.Context, Ex->getType());
8002   Match = handleFormatSignedness(Match, S.getDiagnostics(), Ex->getExprLoc());
8003   bool Pedantic = Match == analyze_format_string::ArgType::NoMatchPedantic;
8004   if (Match == analyze_format_string::ArgType::Match)
8005     return true;
8006 
8007   ScanfSpecifier fixedFS = FS;
8008   bool Success = fixedFS.fixType(Ex->getType(), Ex->IgnoreImpCasts()->getType(),
8009                                  S.getLangOpts(), S.Context);
8010 
8011   unsigned Diag =
8012       Pedantic ? diag::warn_format_conversion_argument_type_mismatch_pedantic
8013                : diag::warn_format_conversion_argument_type_mismatch;
8014 
8015   if (Success) {
8016     // Get the fix string from the fixed format specifier.
8017     SmallString<128> buf;
8018     llvm::raw_svector_ostream os(buf);
8019     fixedFS.toString(os);
8020 
8021     EmitFormatDiagnostic(
8022         S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context)
8023                       << Ex->getType() << false << Ex->getSourceRange(),
8024         Ex->getBeginLoc(),
8025         /*IsStringLocation*/ false,
8026         getSpecifierRange(startSpecifier, specifierLen),
8027         FixItHint::CreateReplacement(
8028             getSpecifierRange(startSpecifier, specifierLen), os.str()));
8029   } else {
8030     EmitFormatDiagnostic(S.PDiag(Diag)
8031                              << AT.getRepresentativeTypeName(S.Context)
8032                              << Ex->getType() << false << Ex->getSourceRange(),
8033                          Ex->getBeginLoc(),
8034                          /*IsStringLocation*/ false,
8035                          getSpecifierRange(startSpecifier, specifierLen));
8036   }
8037 
8038   return true;
8039 }
8040 
8041 static void CheckFormatString(
8042     Sema &S, const FormatStringLiteral *FExpr, const Expr *OrigFormatExpr,
8043     ArrayRef<const Expr *> Args, Sema::FormatArgumentPassingKind APK,
8044     unsigned format_idx, unsigned firstDataArg, Sema::FormatStringType Type,
8045     bool inFunctionCall, Sema::VariadicCallType CallType,
8046     llvm::SmallBitVector &CheckedVarArgs, UncoveredArgHandler &UncoveredArg,
8047     bool IgnoreStringsWithoutSpecifiers) {
8048   // CHECK: is the format string a wide literal?
8049   if (!FExpr->isAscii() && !FExpr->isUTF8()) {
8050     CheckFormatHandler::EmitFormatDiagnostic(
8051         S, inFunctionCall, Args[format_idx],
8052         S.PDiag(diag::warn_format_string_is_wide_literal), FExpr->getBeginLoc(),
8053         /*IsStringLocation*/ true, OrigFormatExpr->getSourceRange());
8054     return;
8055   }
8056 
8057   // Str - The format string.  NOTE: this is NOT null-terminated!
8058   StringRef StrRef = FExpr->getString();
8059   const char *Str = StrRef.data();
8060   // Account for cases where the string literal is truncated in a declaration.
8061   const ConstantArrayType *T =
8062     S.Context.getAsConstantArrayType(FExpr->getType());
8063   assert(T && "String literal not of constant array type!");
8064   size_t TypeSize = T->getZExtSize();
8065   size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size());
8066   const unsigned numDataArgs = Args.size() - firstDataArg;
8067 
8068   if (IgnoreStringsWithoutSpecifiers &&
8069       !analyze_format_string::parseFormatStringHasFormattingSpecifiers(
8070           Str, Str + StrLen, S.getLangOpts(), S.Context.getTargetInfo()))
8071     return;
8072 
8073   // Emit a warning if the string literal is truncated and does not contain an
8074   // embedded null character.
8075   if (TypeSize <= StrRef.size() && !StrRef.substr(0, TypeSize).contains('\0')) {
8076     CheckFormatHandler::EmitFormatDiagnostic(
8077         S, inFunctionCall, Args[format_idx],
8078         S.PDiag(diag::warn_printf_format_string_not_null_terminated),
8079         FExpr->getBeginLoc(),
8080         /*IsStringLocation=*/true, OrigFormatExpr->getSourceRange());
8081     return;
8082   }
8083 
8084   // CHECK: empty format string?
8085   if (StrLen == 0 && numDataArgs > 0) {
8086     CheckFormatHandler::EmitFormatDiagnostic(
8087         S, inFunctionCall, Args[format_idx],
8088         S.PDiag(diag::warn_empty_format_string), FExpr->getBeginLoc(),
8089         /*IsStringLocation*/ true, OrigFormatExpr->getSourceRange());
8090     return;
8091   }
8092 
8093   if (Type == Sema::FST_Printf || Type == Sema::FST_NSString ||
8094       Type == Sema::FST_Kprintf || Type == Sema::FST_FreeBSDKPrintf ||
8095       Type == Sema::FST_OSLog || Type == Sema::FST_OSTrace ||
8096       Type == Sema::FST_Syslog) {
8097     CheckPrintfHandler H(
8098         S, FExpr, OrigFormatExpr, Type, firstDataArg, numDataArgs,
8099         (Type == Sema::FST_NSString || Type == Sema::FST_OSTrace), Str, APK,
8100         Args, format_idx, inFunctionCall, CallType, CheckedVarArgs,
8101         UncoveredArg);
8102 
8103     if (!analyze_format_string::ParsePrintfString(
8104             H, Str, Str + StrLen, S.getLangOpts(), S.Context.getTargetInfo(),
8105             Type == Sema::FST_Kprintf || Type == Sema::FST_FreeBSDKPrintf))
8106       H.DoneProcessing();
8107   } else if (Type == Sema::FST_Scanf) {
8108     CheckScanfHandler H(S, FExpr, OrigFormatExpr, Type, firstDataArg,
8109                         numDataArgs, Str, APK, Args, format_idx, inFunctionCall,
8110                         CallType, CheckedVarArgs, UncoveredArg);
8111 
8112     if (!analyze_format_string::ParseScanfString(
8113             H, Str, Str + StrLen, S.getLangOpts(), S.Context.getTargetInfo()))
8114       H.DoneProcessing();
8115   } // TODO: handle other formats
8116 }
8117 
8118 bool Sema::FormatStringHasSArg(const StringLiteral *FExpr) {
8119   // Str - The format string.  NOTE: this is NOT null-terminated!
8120   StringRef StrRef = FExpr->getString();
8121   const char *Str = StrRef.data();
8122   // Account for cases where the string literal is truncated in a declaration.
8123   const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType());
8124   assert(T && "String literal not of constant array type!");
8125   size_t TypeSize = T->getZExtSize();
8126   size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size());
8127   return analyze_format_string::ParseFormatStringHasSArg(Str, Str + StrLen,
8128                                                          getLangOpts(),
8129                                                          Context.getTargetInfo());
8130 }
8131 
8132 //===--- CHECK: Warn on use of wrong absolute value function. -------------===//
8133 
8134 // Returns the related absolute value function that is larger, of 0 if one
8135 // does not exist.
8136 static unsigned getLargerAbsoluteValueFunction(unsigned AbsFunction) {
8137   switch (AbsFunction) {
8138   default:
8139     return 0;
8140 
8141   case Builtin::BI__builtin_abs:
8142     return Builtin::BI__builtin_labs;
8143   case Builtin::BI__builtin_labs:
8144     return Builtin::BI__builtin_llabs;
8145   case Builtin::BI__builtin_llabs:
8146     return 0;
8147 
8148   case Builtin::BI__builtin_fabsf:
8149     return Builtin::BI__builtin_fabs;
8150   case Builtin::BI__builtin_fabs:
8151     return Builtin::BI__builtin_fabsl;
8152   case Builtin::BI__builtin_fabsl:
8153     return 0;
8154 
8155   case Builtin::BI__builtin_cabsf:
8156     return Builtin::BI__builtin_cabs;
8157   case Builtin::BI__builtin_cabs:
8158     return Builtin::BI__builtin_cabsl;
8159   case Builtin::BI__builtin_cabsl:
8160     return 0;
8161 
8162   case Builtin::BIabs:
8163     return Builtin::BIlabs;
8164   case Builtin::BIlabs:
8165     return Builtin::BIllabs;
8166   case Builtin::BIllabs:
8167     return 0;
8168 
8169   case Builtin::BIfabsf:
8170     return Builtin::BIfabs;
8171   case Builtin::BIfabs:
8172     return Builtin::BIfabsl;
8173   case Builtin::BIfabsl:
8174     return 0;
8175 
8176   case Builtin::BIcabsf:
8177    return Builtin::BIcabs;
8178   case Builtin::BIcabs:
8179     return Builtin::BIcabsl;
8180   case Builtin::BIcabsl:
8181     return 0;
8182   }
8183 }
8184 
8185 // Returns the argument type of the absolute value function.
8186 static QualType getAbsoluteValueArgumentType(ASTContext &Context,
8187                                              unsigned AbsType) {
8188   if (AbsType == 0)
8189     return QualType();
8190 
8191   ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None;
8192   QualType BuiltinType = Context.GetBuiltinType(AbsType, Error);
8193   if (Error != ASTContext::GE_None)
8194     return QualType();
8195 
8196   const FunctionProtoType *FT = BuiltinType->getAs<FunctionProtoType>();
8197   if (!FT)
8198     return QualType();
8199 
8200   if (FT->getNumParams() != 1)
8201     return QualType();
8202 
8203   return FT->getParamType(0);
8204 }
8205 
8206 // Returns the best absolute value function, or zero, based on type and
8207 // current absolute value function.
8208 static unsigned getBestAbsFunction(ASTContext &Context, QualType ArgType,
8209                                    unsigned AbsFunctionKind) {
8210   unsigned BestKind = 0;
8211   uint64_t ArgSize = Context.getTypeSize(ArgType);
8212   for (unsigned Kind = AbsFunctionKind; Kind != 0;
8213        Kind = getLargerAbsoluteValueFunction(Kind)) {
8214     QualType ParamType = getAbsoluteValueArgumentType(Context, Kind);
8215     if (Context.getTypeSize(ParamType) >= ArgSize) {
8216       if (BestKind == 0)
8217         BestKind = Kind;
8218       else if (Context.hasSameType(ParamType, ArgType)) {
8219         BestKind = Kind;
8220         break;
8221       }
8222     }
8223   }
8224   return BestKind;
8225 }
8226 
8227 enum AbsoluteValueKind {
8228   AVK_Integer,
8229   AVK_Floating,
8230   AVK_Complex
8231 };
8232 
8233 static AbsoluteValueKind getAbsoluteValueKind(QualType T) {
8234   if (T->isIntegralOrEnumerationType())
8235     return AVK_Integer;
8236   if (T->isRealFloatingType())
8237     return AVK_Floating;
8238   if (T->isAnyComplexType())
8239     return AVK_Complex;
8240 
8241   llvm_unreachable("Type not integer, floating, or complex");
8242 }
8243 
8244 // Changes the absolute value function to a different type.  Preserves whether
8245 // the function is a builtin.
8246 static unsigned changeAbsFunction(unsigned AbsKind,
8247                                   AbsoluteValueKind ValueKind) {
8248   switch (ValueKind) {
8249   case AVK_Integer:
8250     switch (AbsKind) {
8251     default:
8252       return 0;
8253     case Builtin::BI__builtin_fabsf:
8254     case Builtin::BI__builtin_fabs:
8255     case Builtin::BI__builtin_fabsl:
8256     case Builtin::BI__builtin_cabsf:
8257     case Builtin::BI__builtin_cabs:
8258     case Builtin::BI__builtin_cabsl:
8259       return Builtin::BI__builtin_abs;
8260     case Builtin::BIfabsf:
8261     case Builtin::BIfabs:
8262     case Builtin::BIfabsl:
8263     case Builtin::BIcabsf:
8264     case Builtin::BIcabs:
8265     case Builtin::BIcabsl:
8266       return Builtin::BIabs;
8267     }
8268   case AVK_Floating:
8269     switch (AbsKind) {
8270     default:
8271       return 0;
8272     case Builtin::BI__builtin_abs:
8273     case Builtin::BI__builtin_labs:
8274     case Builtin::BI__builtin_llabs:
8275     case Builtin::BI__builtin_cabsf:
8276     case Builtin::BI__builtin_cabs:
8277     case Builtin::BI__builtin_cabsl:
8278       return Builtin::BI__builtin_fabsf;
8279     case Builtin::BIabs:
8280     case Builtin::BIlabs:
8281     case Builtin::BIllabs:
8282     case Builtin::BIcabsf:
8283     case Builtin::BIcabs:
8284     case Builtin::BIcabsl:
8285       return Builtin::BIfabsf;
8286     }
8287   case AVK_Complex:
8288     switch (AbsKind) {
8289     default:
8290       return 0;
8291     case Builtin::BI__builtin_abs:
8292     case Builtin::BI__builtin_labs:
8293     case Builtin::BI__builtin_llabs:
8294     case Builtin::BI__builtin_fabsf:
8295     case Builtin::BI__builtin_fabs:
8296     case Builtin::BI__builtin_fabsl:
8297       return Builtin::BI__builtin_cabsf;
8298     case Builtin::BIabs:
8299     case Builtin::BIlabs:
8300     case Builtin::BIllabs:
8301     case Builtin::BIfabsf:
8302     case Builtin::BIfabs:
8303     case Builtin::BIfabsl:
8304       return Builtin::BIcabsf;
8305     }
8306   }
8307   llvm_unreachable("Unable to convert function");
8308 }
8309 
8310 static unsigned getAbsoluteValueFunctionKind(const FunctionDecl *FDecl) {
8311   const IdentifierInfo *FnInfo = FDecl->getIdentifier();
8312   if (!FnInfo)
8313     return 0;
8314 
8315   switch (FDecl->getBuiltinID()) {
8316   default:
8317     return 0;
8318   case Builtin::BI__builtin_abs:
8319   case Builtin::BI__builtin_fabs:
8320   case Builtin::BI__builtin_fabsf:
8321   case Builtin::BI__builtin_fabsl:
8322   case Builtin::BI__builtin_labs:
8323   case Builtin::BI__builtin_llabs:
8324   case Builtin::BI__builtin_cabs:
8325   case Builtin::BI__builtin_cabsf:
8326   case Builtin::BI__builtin_cabsl:
8327   case Builtin::BIabs:
8328   case Builtin::BIlabs:
8329   case Builtin::BIllabs:
8330   case Builtin::BIfabs:
8331   case Builtin::BIfabsf:
8332   case Builtin::BIfabsl:
8333   case Builtin::BIcabs:
8334   case Builtin::BIcabsf:
8335   case Builtin::BIcabsl:
8336     return FDecl->getBuiltinID();
8337   }
8338   llvm_unreachable("Unknown Builtin type");
8339 }
8340 
8341 // If the replacement is valid, emit a note with replacement function.
8342 // Additionally, suggest including the proper header if not already included.
8343 static void emitReplacement(Sema &S, SourceLocation Loc, SourceRange Range,
8344                             unsigned AbsKind, QualType ArgType) {
8345   bool EmitHeaderHint = true;
8346   const char *HeaderName = nullptr;
8347   StringRef FunctionName;
8348   if (S.getLangOpts().CPlusPlus && !ArgType->isAnyComplexType()) {
8349     FunctionName = "std::abs";
8350     if (ArgType->isIntegralOrEnumerationType()) {
8351       HeaderName = "cstdlib";
8352     } else if (ArgType->isRealFloatingType()) {
8353       HeaderName = "cmath";
8354     } else {
8355       llvm_unreachable("Invalid Type");
8356     }
8357 
8358     // Lookup all std::abs
8359     if (NamespaceDecl *Std = S.getStdNamespace()) {
8360       LookupResult R(S, &S.Context.Idents.get("abs"), Loc, Sema::LookupAnyName);
8361       R.suppressDiagnostics();
8362       S.LookupQualifiedName(R, Std);
8363 
8364       for (const auto *I : R) {
8365         const FunctionDecl *FDecl = nullptr;
8366         if (const UsingShadowDecl *UsingD = dyn_cast<UsingShadowDecl>(I)) {
8367           FDecl = dyn_cast<FunctionDecl>(UsingD->getTargetDecl());
8368         } else {
8369           FDecl = dyn_cast<FunctionDecl>(I);
8370         }
8371         if (!FDecl)
8372           continue;
8373 
8374         // Found std::abs(), check that they are the right ones.
8375         if (FDecl->getNumParams() != 1)
8376           continue;
8377 
8378         // Check that the parameter type can handle the argument.
8379         QualType ParamType = FDecl->getParamDecl(0)->getType();
8380         if (getAbsoluteValueKind(ArgType) == getAbsoluteValueKind(ParamType) &&
8381             S.Context.getTypeSize(ArgType) <=
8382                 S.Context.getTypeSize(ParamType)) {
8383           // Found a function, don't need the header hint.
8384           EmitHeaderHint = false;
8385           break;
8386         }
8387       }
8388     }
8389   } else {
8390     FunctionName = S.Context.BuiltinInfo.getName(AbsKind);
8391     HeaderName = S.Context.BuiltinInfo.getHeaderName(AbsKind);
8392 
8393     if (HeaderName) {
8394       DeclarationName DN(&S.Context.Idents.get(FunctionName));
8395       LookupResult R(S, DN, Loc, Sema::LookupAnyName);
8396       R.suppressDiagnostics();
8397       S.LookupName(R, S.getCurScope());
8398 
8399       if (R.isSingleResult()) {
8400         FunctionDecl *FD = dyn_cast<FunctionDecl>(R.getFoundDecl());
8401         if (FD && FD->getBuiltinID() == AbsKind) {
8402           EmitHeaderHint = false;
8403         } else {
8404           return;
8405         }
8406       } else if (!R.empty()) {
8407         return;
8408       }
8409     }
8410   }
8411 
8412   S.Diag(Loc, diag::note_replace_abs_function)
8413       << FunctionName << FixItHint::CreateReplacement(Range, FunctionName);
8414 
8415   if (!HeaderName)
8416     return;
8417 
8418   if (!EmitHeaderHint)
8419     return;
8420 
8421   S.Diag(Loc, diag::note_include_header_or_declare) << HeaderName
8422                                                     << FunctionName;
8423 }
8424 
8425 template <std::size_t StrLen>
8426 static bool IsStdFunction(const FunctionDecl *FDecl,
8427                           const char (&Str)[StrLen]) {
8428   if (!FDecl)
8429     return false;
8430   if (!FDecl->getIdentifier() || !FDecl->getIdentifier()->isStr(Str))
8431     return false;
8432   if (!FDecl->isInStdNamespace())
8433     return false;
8434 
8435   return true;
8436 }
8437 
8438 enum class MathCheck { NaN, Inf };
8439 static bool IsInfOrNanFunction(StringRef calleeName, MathCheck Check) {
8440   auto MatchesAny = [&](std::initializer_list<llvm::StringRef> names) {
8441     return std::any_of(names.begin(), names.end(), [&](llvm::StringRef name) {
8442       return calleeName == name;
8443     });
8444   };
8445 
8446   switch (Check) {
8447   case MathCheck::NaN:
8448     return MatchesAny({"__builtin_nan", "__builtin_nanf", "__builtin_nanl",
8449                        "__builtin_nanf16", "__builtin_nanf128"});
8450   case MathCheck::Inf:
8451     return MatchesAny({"__builtin_inf", "__builtin_inff", "__builtin_infl",
8452                        "__builtin_inff16", "__builtin_inff128"});
8453   }
8454   llvm_unreachable("unknown MathCheck");
8455 }
8456 
8457 static bool IsInfinityFunction(const FunctionDecl *FDecl) {
8458   if (FDecl->getName() != "infinity")
8459     return false;
8460 
8461   if (const CXXMethodDecl *MDecl = dyn_cast<CXXMethodDecl>(FDecl)) {
8462     const CXXRecordDecl *RDecl = MDecl->getParent();
8463     if (RDecl->getName() != "numeric_limits")
8464       return false;
8465 
8466     if (const NamespaceDecl *NSDecl =
8467             dyn_cast<NamespaceDecl>(RDecl->getDeclContext()))
8468       return NSDecl->isStdNamespace();
8469   }
8470 
8471   return false;
8472 }
8473 
8474 void Sema::CheckInfNaNFunction(const CallExpr *Call,
8475                                const FunctionDecl *FDecl) {
8476   if (!FDecl->getIdentifier())
8477     return;
8478 
8479   FPOptions FPO = Call->getFPFeaturesInEffect(getLangOpts());
8480   if (FPO.getNoHonorNaNs() &&
8481       (IsStdFunction(FDecl, "isnan") || IsStdFunction(FDecl, "isunordered") ||
8482        IsInfOrNanFunction(FDecl->getName(), MathCheck::NaN))) {
8483     Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled)
8484         << 1 << 0 << Call->getSourceRange();
8485     return;
8486   }
8487 
8488   if (FPO.getNoHonorInfs() &&
8489       (IsStdFunction(FDecl, "isinf") || IsStdFunction(FDecl, "isfinite") ||
8490        IsInfinityFunction(FDecl) ||
8491        IsInfOrNanFunction(FDecl->getName(), MathCheck::Inf))) {
8492     Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled)
8493         << 0 << 0 << Call->getSourceRange();
8494   }
8495 }
8496 
8497 void Sema::CheckAbsoluteValueFunction(const CallExpr *Call,
8498                                       const FunctionDecl *FDecl) {
8499   if (Call->getNumArgs() != 1)
8500     return;
8501 
8502   unsigned AbsKind = getAbsoluteValueFunctionKind(FDecl);
8503   bool IsStdAbs = IsStdFunction(FDecl, "abs");
8504   if (AbsKind == 0 && !IsStdAbs)
8505     return;
8506 
8507   QualType ArgType = Call->getArg(0)->IgnoreParenImpCasts()->getType();
8508   QualType ParamType = Call->getArg(0)->getType();
8509 
8510   // Unsigned types cannot be negative.  Suggest removing the absolute value
8511   // function call.
8512   if (ArgType->isUnsignedIntegerType()) {
8513     StringRef FunctionName =
8514         IsStdAbs ? "std::abs" : Context.BuiltinInfo.getName(AbsKind);
8515     Diag(Call->getExprLoc(), diag::warn_unsigned_abs) << ArgType << ParamType;
8516     Diag(Call->getExprLoc(), diag::note_remove_abs)
8517         << FunctionName
8518         << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange());
8519     return;
8520   }
8521 
8522   // Taking the absolute value of a pointer is very suspicious, they probably
8523   // wanted to index into an array, dereference a pointer, call a function, etc.
8524   if (ArgType->isPointerType() || ArgType->canDecayToPointerType()) {
8525     unsigned DiagType = 0;
8526     if (ArgType->isFunctionType())
8527       DiagType = 1;
8528     else if (ArgType->isArrayType())
8529       DiagType = 2;
8530 
8531     Diag(Call->getExprLoc(), diag::warn_pointer_abs) << DiagType << ArgType;
8532     return;
8533   }
8534 
8535   // std::abs has overloads which prevent most of the absolute value problems
8536   // from occurring.
8537   if (IsStdAbs)
8538     return;
8539 
8540   AbsoluteValueKind ArgValueKind = getAbsoluteValueKind(ArgType);
8541   AbsoluteValueKind ParamValueKind = getAbsoluteValueKind(ParamType);
8542 
8543   // The argument and parameter are the same kind.  Check if they are the right
8544   // size.
8545   if (ArgValueKind == ParamValueKind) {
8546     if (Context.getTypeSize(ArgType) <= Context.getTypeSize(ParamType))
8547       return;
8548 
8549     unsigned NewAbsKind = getBestAbsFunction(Context, ArgType, AbsKind);
8550     Diag(Call->getExprLoc(), diag::warn_abs_too_small)
8551         << FDecl << ArgType << ParamType;
8552 
8553     if (NewAbsKind == 0)
8554       return;
8555 
8556     emitReplacement(*this, Call->getExprLoc(),
8557                     Call->getCallee()->getSourceRange(), NewAbsKind, ArgType);
8558     return;
8559   }
8560 
8561   // ArgValueKind != ParamValueKind
8562   // The wrong type of absolute value function was used.  Attempt to find the
8563   // proper one.
8564   unsigned NewAbsKind = changeAbsFunction(AbsKind, ArgValueKind);
8565   NewAbsKind = getBestAbsFunction(Context, ArgType, NewAbsKind);
8566   if (NewAbsKind == 0)
8567     return;
8568 
8569   Diag(Call->getExprLoc(), diag::warn_wrong_absolute_value_type)
8570       << FDecl << ParamValueKind << ArgValueKind;
8571 
8572   emitReplacement(*this, Call->getExprLoc(),
8573                   Call->getCallee()->getSourceRange(), NewAbsKind, ArgType);
8574 }
8575 
8576 //===--- CHECK: Warn on use of std::max and unsigned zero. r---------------===//
8577 void Sema::CheckMaxUnsignedZero(const CallExpr *Call,
8578                                 const FunctionDecl *FDecl) {
8579   if (!Call || !FDecl) return;
8580 
8581   // Ignore template specializations and macros.
8582   if (inTemplateInstantiation()) return;
8583   if (Call->getExprLoc().isMacroID()) return;
8584 
8585   // Only care about the one template argument, two function parameter std::max
8586   if (Call->getNumArgs() != 2) return;
8587   if (!IsStdFunction(FDecl, "max")) return;
8588   const auto * ArgList = FDecl->getTemplateSpecializationArgs();
8589   if (!ArgList) return;
8590   if (ArgList->size() != 1) return;
8591 
8592   // Check that template type argument is unsigned integer.
8593   const auto& TA = ArgList->get(0);
8594   if (TA.getKind() != TemplateArgument::Type) return;
8595   QualType ArgType = TA.getAsType();
8596   if (!ArgType->isUnsignedIntegerType()) return;
8597 
8598   // See if either argument is a literal zero.
8599   auto IsLiteralZeroArg = [](const Expr* E) -> bool {
8600     const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E);
8601     if (!MTE) return false;
8602     const auto *Num = dyn_cast<IntegerLiteral>(MTE->getSubExpr());
8603     if (!Num) return false;
8604     if (Num->getValue() != 0) return false;
8605     return true;
8606   };
8607 
8608   const Expr *FirstArg = Call->getArg(0);
8609   const Expr *SecondArg = Call->getArg(1);
8610   const bool IsFirstArgZero = IsLiteralZeroArg(FirstArg);
8611   const bool IsSecondArgZero = IsLiteralZeroArg(SecondArg);
8612 
8613   // Only warn when exactly one argument is zero.
8614   if (IsFirstArgZero == IsSecondArgZero) return;
8615 
8616   SourceRange FirstRange = FirstArg->getSourceRange();
8617   SourceRange SecondRange = SecondArg->getSourceRange();
8618 
8619   SourceRange ZeroRange = IsFirstArgZero ? FirstRange : SecondRange;
8620 
8621   Diag(Call->getExprLoc(), diag::warn_max_unsigned_zero)
8622       << IsFirstArgZero << Call->getCallee()->getSourceRange() << ZeroRange;
8623 
8624   // Deduce what parts to remove so that "std::max(0u, foo)" becomes "(foo)".
8625   SourceRange RemovalRange;
8626   if (IsFirstArgZero) {
8627     RemovalRange = SourceRange(FirstRange.getBegin(),
8628                                SecondRange.getBegin().getLocWithOffset(-1));
8629   } else {
8630     RemovalRange = SourceRange(getLocForEndOfToken(FirstRange.getEnd()),
8631                                SecondRange.getEnd());
8632   }
8633 
8634   Diag(Call->getExprLoc(), diag::note_remove_max_call)
8635         << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange())
8636         << FixItHint::CreateRemoval(RemovalRange);
8637 }
8638 
8639 //===--- CHECK: Standard memory functions ---------------------------------===//
8640 
8641 /// Takes the expression passed to the size_t parameter of functions
8642 /// such as memcmp, strncat, etc and warns if it's a comparison.
8643 ///
8644 /// This is to catch typos like `if (memcmp(&a, &b, sizeof(a) > 0))`.
8645 static bool CheckMemorySizeofForComparison(Sema &S, const Expr *E,
8646                                            IdentifierInfo *FnName,
8647                                            SourceLocation FnLoc,
8648                                            SourceLocation RParenLoc) {
8649   const BinaryOperator *Size = dyn_cast<BinaryOperator>(E);
8650   if (!Size)
8651     return false;
8652 
8653   // if E is binop and op is <=>, >, <, >=, <=, ==, &&, ||:
8654   if (!Size->isComparisonOp() && !Size->isLogicalOp())
8655     return false;
8656 
8657   SourceRange SizeRange = Size->getSourceRange();
8658   S.Diag(Size->getOperatorLoc(), diag::warn_memsize_comparison)
8659       << SizeRange << FnName;
8660   S.Diag(FnLoc, diag::note_memsize_comparison_paren)
8661       << FnName
8662       << FixItHint::CreateInsertion(
8663              S.getLocForEndOfToken(Size->getLHS()->getEndLoc()), ")")
8664       << FixItHint::CreateRemoval(RParenLoc);
8665   S.Diag(SizeRange.getBegin(), diag::note_memsize_comparison_cast_silence)
8666       << FixItHint::CreateInsertion(SizeRange.getBegin(), "(size_t)(")
8667       << FixItHint::CreateInsertion(S.getLocForEndOfToken(SizeRange.getEnd()),
8668                                     ")");
8669 
8670   return true;
8671 }
8672 
8673 /// Determine whether the given type is or contains a dynamic class type
8674 /// (e.g., whether it has a vtable).
8675 static const CXXRecordDecl *getContainedDynamicClass(QualType T,
8676                                                      bool &IsContained) {
8677   // Look through array types while ignoring qualifiers.
8678   const Type *Ty = T->getBaseElementTypeUnsafe();
8679   IsContained = false;
8680 
8681   const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
8682   RD = RD ? RD->getDefinition() : nullptr;
8683   if (!RD || RD->isInvalidDecl())
8684     return nullptr;
8685 
8686   if (RD->isDynamicClass())
8687     return RD;
8688 
8689   // Check all the fields.  If any bases were dynamic, the class is dynamic.
8690   // It's impossible for a class to transitively contain itself by value, so
8691   // infinite recursion is impossible.
8692   for (auto *FD : RD->fields()) {
8693     bool SubContained;
8694     if (const CXXRecordDecl *ContainedRD =
8695             getContainedDynamicClass(FD->getType(), SubContained)) {
8696       IsContained = true;
8697       return ContainedRD;
8698     }
8699   }
8700 
8701   return nullptr;
8702 }
8703 
8704 static const UnaryExprOrTypeTraitExpr *getAsSizeOfExpr(const Expr *E) {
8705   if (const auto *Unary = dyn_cast<UnaryExprOrTypeTraitExpr>(E))
8706     if (Unary->getKind() == UETT_SizeOf)
8707       return Unary;
8708   return nullptr;
8709 }
8710 
8711 /// If E is a sizeof expression, returns its argument expression,
8712 /// otherwise returns NULL.
8713 static const Expr *getSizeOfExprArg(const Expr *E) {
8714   if (const UnaryExprOrTypeTraitExpr *SizeOf = getAsSizeOfExpr(E))
8715     if (!SizeOf->isArgumentType())
8716       return SizeOf->getArgumentExpr()->IgnoreParenImpCasts();
8717   return nullptr;
8718 }
8719 
8720 /// If E is a sizeof expression, returns its argument type.
8721 static QualType getSizeOfArgType(const Expr *E) {
8722   if (const UnaryExprOrTypeTraitExpr *SizeOf = getAsSizeOfExpr(E))
8723     return SizeOf->getTypeOfArgument();
8724   return QualType();
8725 }
8726 
8727 namespace {
8728 
8729 struct SearchNonTrivialToInitializeField
8730     : DefaultInitializedTypeVisitor<SearchNonTrivialToInitializeField> {
8731   using Super =
8732       DefaultInitializedTypeVisitor<SearchNonTrivialToInitializeField>;
8733 
8734   SearchNonTrivialToInitializeField(const Expr *E, Sema &S) : E(E), S(S) {}
8735 
8736   void visitWithKind(QualType::PrimitiveDefaultInitializeKind PDIK, QualType FT,
8737                      SourceLocation SL) {
8738     if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) {
8739       asDerived().visitArray(PDIK, AT, SL);
8740       return;
8741     }
8742 
8743     Super::visitWithKind(PDIK, FT, SL);
8744   }
8745 
8746   void visitARCStrong(QualType FT, SourceLocation SL) {
8747     S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 1);
8748   }
8749   void visitARCWeak(QualType FT, SourceLocation SL) {
8750     S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 1);
8751   }
8752   void visitStruct(QualType FT, SourceLocation SL) {
8753     for (const FieldDecl *FD : FT->castAs<RecordType>()->getDecl()->fields())
8754       visit(FD->getType(), FD->getLocation());
8755   }
8756   void visitArray(QualType::PrimitiveDefaultInitializeKind PDIK,
8757                   const ArrayType *AT, SourceLocation SL) {
8758     visit(getContext().getBaseElementType(AT), SL);
8759   }
8760   void visitTrivial(QualType FT, SourceLocation SL) {}
8761 
8762   static void diag(QualType RT, const Expr *E, Sema &S) {
8763     SearchNonTrivialToInitializeField(E, S).visitStruct(RT, SourceLocation());
8764   }
8765 
8766   ASTContext &getContext() { return S.getASTContext(); }
8767 
8768   const Expr *E;
8769   Sema &S;
8770 };
8771 
8772 struct SearchNonTrivialToCopyField
8773     : CopiedTypeVisitor<SearchNonTrivialToCopyField, false> {
8774   using Super = CopiedTypeVisitor<SearchNonTrivialToCopyField, false>;
8775 
8776   SearchNonTrivialToCopyField(const Expr *E, Sema &S) : E(E), S(S) {}
8777 
8778   void visitWithKind(QualType::PrimitiveCopyKind PCK, QualType FT,
8779                      SourceLocation SL) {
8780     if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) {
8781       asDerived().visitArray(PCK, AT, SL);
8782       return;
8783     }
8784 
8785     Super::visitWithKind(PCK, FT, SL);
8786   }
8787 
8788   void visitARCStrong(QualType FT, SourceLocation SL) {
8789     S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 0);
8790   }
8791   void visitARCWeak(QualType FT, SourceLocation SL) {
8792     S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 0);
8793   }
8794   void visitStruct(QualType FT, SourceLocation SL) {
8795     for (const FieldDecl *FD : FT->castAs<RecordType>()->getDecl()->fields())
8796       visit(FD->getType(), FD->getLocation());
8797   }
8798   void visitArray(QualType::PrimitiveCopyKind PCK, const ArrayType *AT,
8799                   SourceLocation SL) {
8800     visit(getContext().getBaseElementType(AT), SL);
8801   }
8802   void preVisit(QualType::PrimitiveCopyKind PCK, QualType FT,
8803                 SourceLocation SL) {}
8804   void visitTrivial(QualType FT, SourceLocation SL) {}
8805   void visitVolatileTrivial(QualType FT, SourceLocation SL) {}
8806 
8807   static void diag(QualType RT, const Expr *E, Sema &S) {
8808     SearchNonTrivialToCopyField(E, S).visitStruct(RT, SourceLocation());
8809   }
8810 
8811   ASTContext &getContext() { return S.getASTContext(); }
8812 
8813   const Expr *E;
8814   Sema &S;
8815 };
8816 
8817 }
8818 
8819 /// Detect if \c SizeofExpr is likely to calculate the sizeof an object.
8820 static bool doesExprLikelyComputeSize(const Expr *SizeofExpr) {
8821   SizeofExpr = SizeofExpr->IgnoreParenImpCasts();
8822 
8823   if (const auto *BO = dyn_cast<BinaryOperator>(SizeofExpr)) {
8824     if (BO->getOpcode() != BO_Mul && BO->getOpcode() != BO_Add)
8825       return false;
8826 
8827     return doesExprLikelyComputeSize(BO->getLHS()) ||
8828            doesExprLikelyComputeSize(BO->getRHS());
8829   }
8830 
8831   return getAsSizeOfExpr(SizeofExpr) != nullptr;
8832 }
8833 
8834 /// Check if the ArgLoc originated from a macro passed to the call at CallLoc.
8835 ///
8836 /// \code
8837 ///   #define MACRO 0
8838 ///   foo(MACRO);
8839 ///   foo(0);
8840 /// \endcode
8841 ///
8842 /// This should return true for the first call to foo, but not for the second
8843 /// (regardless of whether foo is a macro or function).
8844 static bool isArgumentExpandedFromMacro(SourceManager &SM,
8845                                         SourceLocation CallLoc,
8846                                         SourceLocation ArgLoc) {
8847   if (!CallLoc.isMacroID())
8848     return SM.getFileID(CallLoc) != SM.getFileID(ArgLoc);
8849 
8850   return SM.getFileID(SM.getImmediateMacroCallerLoc(CallLoc)) !=
8851          SM.getFileID(SM.getImmediateMacroCallerLoc(ArgLoc));
8852 }
8853 
8854 /// Diagnose cases like 'memset(buf, sizeof(buf), 0)', which should have the
8855 /// last two arguments transposed.
8856 static void CheckMemaccessSize(Sema &S, unsigned BId, const CallExpr *Call) {
8857   if (BId != Builtin::BImemset && BId != Builtin::BIbzero)
8858     return;
8859 
8860   const Expr *SizeArg =
8861     Call->getArg(BId == Builtin::BImemset ? 2 : 1)->IgnoreImpCasts();
8862 
8863   auto isLiteralZero = [](const Expr *E) {
8864     return (isa<IntegerLiteral>(E) &&
8865             cast<IntegerLiteral>(E)->getValue() == 0) ||
8866            (isa<CharacterLiteral>(E) &&
8867             cast<CharacterLiteral>(E)->getValue() == 0);
8868   };
8869 
8870   // If we're memsetting or bzeroing 0 bytes, then this is likely an error.
8871   SourceLocation CallLoc = Call->getRParenLoc();
8872   SourceManager &SM = S.getSourceManager();
8873   if (isLiteralZero(SizeArg) &&
8874       !isArgumentExpandedFromMacro(SM, CallLoc, SizeArg->getExprLoc())) {
8875 
8876     SourceLocation DiagLoc = SizeArg->getExprLoc();
8877 
8878     // Some platforms #define bzero to __builtin_memset. See if this is the
8879     // case, and if so, emit a better diagnostic.
8880     if (BId == Builtin::BIbzero ||
8881         (CallLoc.isMacroID() && Lexer::getImmediateMacroName(
8882                                     CallLoc, SM, S.getLangOpts()) == "bzero")) {
8883       S.Diag(DiagLoc, diag::warn_suspicious_bzero_size);
8884       S.Diag(DiagLoc, diag::note_suspicious_bzero_size_silence);
8885     } else if (!isLiteralZero(Call->getArg(1)->IgnoreImpCasts())) {
8886       S.Diag(DiagLoc, diag::warn_suspicious_sizeof_memset) << 0;
8887       S.Diag(DiagLoc, diag::note_suspicious_sizeof_memset_silence) << 0;
8888     }
8889     return;
8890   }
8891 
8892   // If the second argument to a memset is a sizeof expression and the third
8893   // isn't, this is also likely an error. This should catch
8894   // 'memset(buf, sizeof(buf), 0xff)'.
8895   if (BId == Builtin::BImemset &&
8896       doesExprLikelyComputeSize(Call->getArg(1)) &&
8897       !doesExprLikelyComputeSize(Call->getArg(2))) {
8898     SourceLocation DiagLoc = Call->getArg(1)->getExprLoc();
8899     S.Diag(DiagLoc, diag::warn_suspicious_sizeof_memset) << 1;
8900     S.Diag(DiagLoc, diag::note_suspicious_sizeof_memset_silence) << 1;
8901     return;
8902   }
8903 }
8904 
8905 void Sema::CheckMemaccessArguments(const CallExpr *Call,
8906                                    unsigned BId,
8907                                    IdentifierInfo *FnName) {
8908   assert(BId != 0);
8909 
8910   // It is possible to have a non-standard definition of memset.  Validate
8911   // we have enough arguments, and if not, abort further checking.
8912   unsigned ExpectedNumArgs =
8913       (BId == Builtin::BIstrndup || BId == Builtin::BIbzero ? 2 : 3);
8914   if (Call->getNumArgs() < ExpectedNumArgs)
8915     return;
8916 
8917   unsigned LastArg = (BId == Builtin::BImemset || BId == Builtin::BIbzero ||
8918                       BId == Builtin::BIstrndup ? 1 : 2);
8919   unsigned LenArg =
8920       (BId == Builtin::BIbzero || BId == Builtin::BIstrndup ? 1 : 2);
8921   const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts();
8922 
8923   if (CheckMemorySizeofForComparison(*this, LenExpr, FnName,
8924                                      Call->getBeginLoc(), Call->getRParenLoc()))
8925     return;
8926 
8927   // Catch cases like 'memset(buf, sizeof(buf), 0)'.
8928   CheckMemaccessSize(*this, BId, Call);
8929 
8930   // We have special checking when the length is a sizeof expression.
8931   QualType SizeOfArgTy = getSizeOfArgType(LenExpr);
8932   const Expr *SizeOfArg = getSizeOfExprArg(LenExpr);
8933   llvm::FoldingSetNodeID SizeOfArgID;
8934 
8935   // Although widely used, 'bzero' is not a standard function. Be more strict
8936   // with the argument types before allowing diagnostics and only allow the
8937   // form bzero(ptr, sizeof(...)).
8938   QualType FirstArgTy = Call->getArg(0)->IgnoreParenImpCasts()->getType();
8939   if (BId == Builtin::BIbzero && !FirstArgTy->getAs<PointerType>())
8940     return;
8941 
8942   for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) {
8943     const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts();
8944     SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange();
8945 
8946     QualType DestTy = Dest->getType();
8947     QualType PointeeTy;
8948     if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) {
8949       PointeeTy = DestPtrTy->getPointeeType();
8950 
8951       // Never warn about void type pointers. This can be used to suppress
8952       // false positives.
8953       if (PointeeTy->isVoidType())
8954         continue;
8955 
8956       // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by
8957       // actually comparing the expressions for equality. Because computing the
8958       // expression IDs can be expensive, we only do this if the diagnostic is
8959       // enabled.
8960       if (SizeOfArg &&
8961           !Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess,
8962                            SizeOfArg->getExprLoc())) {
8963         // We only compute IDs for expressions if the warning is enabled, and
8964         // cache the sizeof arg's ID.
8965         if (SizeOfArgID == llvm::FoldingSetNodeID())
8966           SizeOfArg->Profile(SizeOfArgID, Context, true);
8967         llvm::FoldingSetNodeID DestID;
8968         Dest->Profile(DestID, Context, true);
8969         if (DestID == SizeOfArgID) {
8970           // TODO: For strncpy() and friends, this could suggest sizeof(dst)
8971           //       over sizeof(src) as well.
8972           unsigned ActionIdx = 0; // Default is to suggest dereferencing.
8973           StringRef ReadableName = FnName->getName();
8974 
8975           if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest))
8976             if (UnaryOp->getOpcode() == UO_AddrOf)
8977               ActionIdx = 1; // If its an address-of operator, just remove it.
8978           if (!PointeeTy->isIncompleteType() &&
8979               (Context.getTypeSize(PointeeTy) == Context.getCharWidth()))
8980             ActionIdx = 2; // If the pointee's size is sizeof(char),
8981                            // suggest an explicit length.
8982 
8983           // If the function is defined as a builtin macro, do not show macro
8984           // expansion.
8985           SourceLocation SL = SizeOfArg->getExprLoc();
8986           SourceRange DSR = Dest->getSourceRange();
8987           SourceRange SSR = SizeOfArg->getSourceRange();
8988           SourceManager &SM = getSourceManager();
8989 
8990           if (SM.isMacroArgExpansion(SL)) {
8991             ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts);
8992             SL = SM.getSpellingLoc(SL);
8993             DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()),
8994                              SM.getSpellingLoc(DSR.getEnd()));
8995             SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()),
8996                              SM.getSpellingLoc(SSR.getEnd()));
8997           }
8998 
8999           DiagRuntimeBehavior(SL, SizeOfArg,
9000                               PDiag(diag::warn_sizeof_pointer_expr_memaccess)
9001                                 << ReadableName
9002                                 << PointeeTy
9003                                 << DestTy
9004                                 << DSR
9005                                 << SSR);
9006           DiagRuntimeBehavior(SL, SizeOfArg,
9007                          PDiag(diag::warn_sizeof_pointer_expr_memaccess_note)
9008                                 << ActionIdx
9009                                 << SSR);
9010 
9011           break;
9012         }
9013       }
9014 
9015       // Also check for cases where the sizeof argument is the exact same
9016       // type as the memory argument, and where it points to a user-defined
9017       // record type.
9018       if (SizeOfArgTy != QualType()) {
9019         if (PointeeTy->isRecordType() &&
9020             Context.typesAreCompatible(SizeOfArgTy, DestTy)) {
9021           DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest,
9022                               PDiag(diag::warn_sizeof_pointer_type_memaccess)
9023                                 << FnName << SizeOfArgTy << ArgIdx
9024                                 << PointeeTy << Dest->getSourceRange()
9025                                 << LenExpr->getSourceRange());
9026           break;
9027         }
9028       }
9029     } else if (DestTy->isArrayType()) {
9030       PointeeTy = DestTy;
9031     }
9032 
9033     if (PointeeTy == QualType())
9034       continue;
9035 
9036     // Always complain about dynamic classes.
9037     bool IsContained;
9038     if (const CXXRecordDecl *ContainedRD =
9039             getContainedDynamicClass(PointeeTy, IsContained)) {
9040 
9041       unsigned OperationType = 0;
9042       const bool IsCmp = BId == Builtin::BImemcmp || BId == Builtin::BIbcmp;
9043       // "overwritten" if we're warning about the destination for any call
9044       // but memcmp; otherwise a verb appropriate to the call.
9045       if (ArgIdx != 0 || IsCmp) {
9046         if (BId == Builtin::BImemcpy)
9047           OperationType = 1;
9048         else if(BId == Builtin::BImemmove)
9049           OperationType = 2;
9050         else if (IsCmp)
9051           OperationType = 3;
9052       }
9053 
9054       DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
9055                           PDiag(diag::warn_dyn_class_memaccess)
9056                               << (IsCmp ? ArgIdx + 2 : ArgIdx) << FnName
9057                               << IsContained << ContainedRD << OperationType
9058                               << Call->getCallee()->getSourceRange());
9059     } else if (PointeeTy.hasNonTrivialObjCLifetime() &&
9060              BId != Builtin::BImemset)
9061       DiagRuntimeBehavior(
9062         Dest->getExprLoc(), Dest,
9063         PDiag(diag::warn_arc_object_memaccess)
9064           << ArgIdx << FnName << PointeeTy
9065           << Call->getCallee()->getSourceRange());
9066     else if (const auto *RT = PointeeTy->getAs<RecordType>()) {
9067 
9068       // FIXME: Do not consider incomplete types even though they may be
9069       // completed later. GCC does not diagnose such code, but we may want to
9070       // consider diagnosing it in the future, perhaps under a different, but
9071       // related, diagnostic group.
9072       bool MayBeTriviallyCopyableCXXRecord =
9073           RT->isIncompleteType() ||
9074           RT->desugar().isTriviallyCopyableType(Context);
9075 
9076       if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) &&
9077           RT->getDecl()->isNonTrivialToPrimitiveDefaultInitialize()) {
9078         DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
9079                             PDiag(diag::warn_cstruct_memaccess)
9080                                 << ArgIdx << FnName << PointeeTy << 0);
9081         SearchNonTrivialToInitializeField::diag(PointeeTy, Dest, *this);
9082       } else if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) &&
9083                  !MayBeTriviallyCopyableCXXRecord && ArgIdx == 0) {
9084         // FIXME: Limiting this warning to dest argument until we decide
9085         // whether it's valid for source argument too.
9086         DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
9087                             PDiag(diag::warn_cxxstruct_memaccess)
9088                                 << FnName << PointeeTy);
9089       } else if ((BId == Builtin::BImemcpy || BId == Builtin::BImemmove) &&
9090                  RT->getDecl()->isNonTrivialToPrimitiveCopy()) {
9091         DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
9092                             PDiag(diag::warn_cstruct_memaccess)
9093                                 << ArgIdx << FnName << PointeeTy << 1);
9094         SearchNonTrivialToCopyField::diag(PointeeTy, Dest, *this);
9095       } else if ((BId == Builtin::BImemcpy || BId == Builtin::BImemmove) &&
9096                  !MayBeTriviallyCopyableCXXRecord && ArgIdx == 0) {
9097         // FIXME: Limiting this warning to dest argument until we decide
9098         // whether it's valid for source argument too.
9099         DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
9100                             PDiag(diag::warn_cxxstruct_memaccess)
9101                                 << FnName << PointeeTy);
9102       } else {
9103         continue;
9104       }
9105     } else
9106       continue;
9107 
9108     DiagRuntimeBehavior(
9109       Dest->getExprLoc(), Dest,
9110       PDiag(diag::note_bad_memaccess_silence)
9111         << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)"));
9112     break;
9113   }
9114 }
9115 
9116 // A little helper routine: ignore addition and subtraction of integer literals.
9117 // This intentionally does not ignore all integer constant expressions because
9118 // we don't want to remove sizeof().
9119 static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) {
9120   Ex = Ex->IgnoreParenCasts();
9121 
9122   while (true) {
9123     const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex);
9124     if (!BO || !BO->isAdditiveOp())
9125       break;
9126 
9127     const Expr *RHS = BO->getRHS()->IgnoreParenCasts();
9128     const Expr *LHS = BO->getLHS()->IgnoreParenCasts();
9129 
9130     if (isa<IntegerLiteral>(RHS))
9131       Ex = LHS;
9132     else if (isa<IntegerLiteral>(LHS))
9133       Ex = RHS;
9134     else
9135       break;
9136   }
9137 
9138   return Ex;
9139 }
9140 
9141 static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty,
9142                                                       ASTContext &Context) {
9143   // Only handle constant-sized or VLAs, but not flexible members.
9144   if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) {
9145     // Only issue the FIXIT for arrays of size > 1.
9146     if (CAT->getZExtSize() <= 1)
9147       return false;
9148   } else if (!Ty->isVariableArrayType()) {
9149     return false;
9150   }
9151   return true;
9152 }
9153 
9154 void Sema::CheckStrlcpycatArguments(const CallExpr *Call,
9155                                     IdentifierInfo *FnName) {
9156 
9157   // Don't crash if the user has the wrong number of arguments
9158   unsigned NumArgs = Call->getNumArgs();
9159   if ((NumArgs != 3) && (NumArgs != 4))
9160     return;
9161 
9162   const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context);
9163   const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context);
9164   const Expr *CompareWithSrc = nullptr;
9165 
9166   if (CheckMemorySizeofForComparison(*this, SizeArg, FnName,
9167                                      Call->getBeginLoc(), Call->getRParenLoc()))
9168     return;
9169 
9170   // Look for 'strlcpy(dst, x, sizeof(x))'
9171   if (const Expr *Ex = getSizeOfExprArg(SizeArg))
9172     CompareWithSrc = Ex;
9173   else {
9174     // Look for 'strlcpy(dst, x, strlen(x))'
9175     if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) {
9176       if (SizeCall->getBuiltinCallee() == Builtin::BIstrlen &&
9177           SizeCall->getNumArgs() == 1)
9178         CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context);
9179     }
9180   }
9181 
9182   if (!CompareWithSrc)
9183     return;
9184 
9185   // Determine if the argument to sizeof/strlen is equal to the source
9186   // argument.  In principle there's all kinds of things you could do
9187   // here, for instance creating an == expression and evaluating it with
9188   // EvaluateAsBooleanCondition, but this uses a more direct technique:
9189   const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg);
9190   if (!SrcArgDRE)
9191     return;
9192 
9193   const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc);
9194   if (!CompareWithSrcDRE ||
9195       SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl())
9196     return;
9197 
9198   const Expr *OriginalSizeArg = Call->getArg(2);
9199   Diag(CompareWithSrcDRE->getBeginLoc(), diag::warn_strlcpycat_wrong_size)
9200       << OriginalSizeArg->getSourceRange() << FnName;
9201 
9202   // Output a FIXIT hint if the destination is an array (rather than a
9203   // pointer to an array).  This could be enhanced to handle some
9204   // pointers if we know the actual size, like if DstArg is 'array+2'
9205   // we could say 'sizeof(array)-2'.
9206   const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts();
9207   if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context))
9208     return;
9209 
9210   SmallString<128> sizeString;
9211   llvm::raw_svector_ostream OS(sizeString);
9212   OS << "sizeof(";
9213   DstArg->printPretty(OS, nullptr, getPrintingPolicy());
9214   OS << ")";
9215 
9216   Diag(OriginalSizeArg->getBeginLoc(), diag::note_strlcpycat_wrong_size)
9217       << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(),
9218                                       OS.str());
9219 }
9220 
9221 /// Check if two expressions refer to the same declaration.
9222 static bool referToTheSameDecl(const Expr *E1, const Expr *E2) {
9223   if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1))
9224     if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2))
9225       return D1->getDecl() == D2->getDecl();
9226   return false;
9227 }
9228 
9229 static const Expr *getStrlenExprArg(const Expr *E) {
9230   if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
9231     const FunctionDecl *FD = CE->getDirectCallee();
9232     if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen)
9233       return nullptr;
9234     return CE->getArg(0)->IgnoreParenCasts();
9235   }
9236   return nullptr;
9237 }
9238 
9239 void Sema::CheckStrncatArguments(const CallExpr *CE,
9240                                  IdentifierInfo *FnName) {
9241   // Don't crash if the user has the wrong number of arguments.
9242   if (CE->getNumArgs() < 3)
9243     return;
9244   const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts();
9245   const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts();
9246   const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts();
9247 
9248   if (CheckMemorySizeofForComparison(*this, LenArg, FnName, CE->getBeginLoc(),
9249                                      CE->getRParenLoc()))
9250     return;
9251 
9252   // Identify common expressions, which are wrongly used as the size argument
9253   // to strncat and may lead to buffer overflows.
9254   unsigned PatternType = 0;
9255   if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) {
9256     // - sizeof(dst)
9257     if (referToTheSameDecl(SizeOfArg, DstArg))
9258       PatternType = 1;
9259     // - sizeof(src)
9260     else if (referToTheSameDecl(SizeOfArg, SrcArg))
9261       PatternType = 2;
9262   } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) {
9263     if (BE->getOpcode() == BO_Sub) {
9264       const Expr *L = BE->getLHS()->IgnoreParenCasts();
9265       const Expr *R = BE->getRHS()->IgnoreParenCasts();
9266       // - sizeof(dst) - strlen(dst)
9267       if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) &&
9268           referToTheSameDecl(DstArg, getStrlenExprArg(R)))
9269         PatternType = 1;
9270       // - sizeof(src) - (anything)
9271       else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L)))
9272         PatternType = 2;
9273     }
9274   }
9275 
9276   if (PatternType == 0)
9277     return;
9278 
9279   // Generate the diagnostic.
9280   SourceLocation SL = LenArg->getBeginLoc();
9281   SourceRange SR = LenArg->getSourceRange();
9282   SourceManager &SM = getSourceManager();
9283 
9284   // If the function is defined as a builtin macro, do not show macro expansion.
9285   if (SM.isMacroArgExpansion(SL)) {
9286     SL = SM.getSpellingLoc(SL);
9287     SR = SourceRange(SM.getSpellingLoc(SR.getBegin()),
9288                      SM.getSpellingLoc(SR.getEnd()));
9289   }
9290 
9291   // Check if the destination is an array (rather than a pointer to an array).
9292   QualType DstTy = DstArg->getType();
9293   bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy,
9294                                                                     Context);
9295   if (!isKnownSizeArray) {
9296     if (PatternType == 1)
9297       Diag(SL, diag::warn_strncat_wrong_size) << SR;
9298     else
9299       Diag(SL, diag::warn_strncat_src_size) << SR;
9300     return;
9301   }
9302 
9303   if (PatternType == 1)
9304     Diag(SL, diag::warn_strncat_large_size) << SR;
9305   else
9306     Diag(SL, diag::warn_strncat_src_size) << SR;
9307 
9308   SmallString<128> sizeString;
9309   llvm::raw_svector_ostream OS(sizeString);
9310   OS << "sizeof(";
9311   DstArg->printPretty(OS, nullptr, getPrintingPolicy());
9312   OS << ") - ";
9313   OS << "strlen(";
9314   DstArg->printPretty(OS, nullptr, getPrintingPolicy());
9315   OS << ") - 1";
9316 
9317   Diag(SL, diag::note_strncat_wrong_size)
9318     << FixItHint::CreateReplacement(SR, OS.str());
9319 }
9320 
9321 namespace {
9322 void CheckFreeArgumentsOnLvalue(Sema &S, const std::string &CalleeName,
9323                                 const UnaryOperator *UnaryExpr, const Decl *D) {
9324   if (isa<FieldDecl, FunctionDecl, VarDecl>(D)) {
9325     S.Diag(UnaryExpr->getBeginLoc(), diag::warn_free_nonheap_object)
9326         << CalleeName << 0 /*object: */ << cast<NamedDecl>(D);
9327     return;
9328   }
9329 }
9330 
9331 void CheckFreeArgumentsAddressof(Sema &S, const std::string &CalleeName,
9332                                  const UnaryOperator *UnaryExpr) {
9333   if (const auto *Lvalue = dyn_cast<DeclRefExpr>(UnaryExpr->getSubExpr())) {
9334     const Decl *D = Lvalue->getDecl();
9335     if (isa<DeclaratorDecl>(D))
9336       if (!dyn_cast<DeclaratorDecl>(D)->getType()->isReferenceType())
9337         return CheckFreeArgumentsOnLvalue(S, CalleeName, UnaryExpr, D);
9338   }
9339 
9340   if (const auto *Lvalue = dyn_cast<MemberExpr>(UnaryExpr->getSubExpr()))
9341     return CheckFreeArgumentsOnLvalue(S, CalleeName, UnaryExpr,
9342                                       Lvalue->getMemberDecl());
9343 }
9344 
9345 void CheckFreeArgumentsPlus(Sema &S, const std::string &CalleeName,
9346                             const UnaryOperator *UnaryExpr) {
9347   const auto *Lambda = dyn_cast<LambdaExpr>(
9348       UnaryExpr->getSubExpr()->IgnoreImplicitAsWritten()->IgnoreParens());
9349   if (!Lambda)
9350     return;
9351 
9352   S.Diag(Lambda->getBeginLoc(), diag::warn_free_nonheap_object)
9353       << CalleeName << 2 /*object: lambda expression*/;
9354 }
9355 
9356 void CheckFreeArgumentsStackArray(Sema &S, const std::string &CalleeName,
9357                                   const DeclRefExpr *Lvalue) {
9358   const auto *Var = dyn_cast<VarDecl>(Lvalue->getDecl());
9359   if (Var == nullptr)
9360     return;
9361 
9362   S.Diag(Lvalue->getBeginLoc(), diag::warn_free_nonheap_object)
9363       << CalleeName << 0 /*object: */ << Var;
9364 }
9365 
9366 void CheckFreeArgumentsCast(Sema &S, const std::string &CalleeName,
9367                             const CastExpr *Cast) {
9368   SmallString<128> SizeString;
9369   llvm::raw_svector_ostream OS(SizeString);
9370 
9371   clang::CastKind Kind = Cast->getCastKind();
9372   if (Kind == clang::CK_BitCast &&
9373       !Cast->getSubExpr()->getType()->isFunctionPointerType())
9374     return;
9375   if (Kind == clang::CK_IntegralToPointer &&
9376       !isa<IntegerLiteral>(
9377           Cast->getSubExpr()->IgnoreParenImpCasts()->IgnoreParens()))
9378     return;
9379 
9380   switch (Cast->getCastKind()) {
9381   case clang::CK_BitCast:
9382   case clang::CK_IntegralToPointer:
9383   case clang::CK_FunctionToPointerDecay:
9384     OS << '\'';
9385     Cast->printPretty(OS, nullptr, S.getPrintingPolicy());
9386     OS << '\'';
9387     break;
9388   default:
9389     return;
9390   }
9391 
9392   S.Diag(Cast->getBeginLoc(), diag::warn_free_nonheap_object)
9393       << CalleeName << 0 /*object: */ << OS.str();
9394 }
9395 } // namespace
9396 
9397 void Sema::CheckFreeArguments(const CallExpr *E) {
9398   const std::string CalleeName =
9399       cast<FunctionDecl>(E->getCalleeDecl())->getQualifiedNameAsString();
9400 
9401   { // Prefer something that doesn't involve a cast to make things simpler.
9402     const Expr *Arg = E->getArg(0)->IgnoreParenCasts();
9403     if (const auto *UnaryExpr = dyn_cast<UnaryOperator>(Arg))
9404       switch (UnaryExpr->getOpcode()) {
9405       case UnaryOperator::Opcode::UO_AddrOf:
9406         return CheckFreeArgumentsAddressof(*this, CalleeName, UnaryExpr);
9407       case UnaryOperator::Opcode::UO_Plus:
9408         return CheckFreeArgumentsPlus(*this, CalleeName, UnaryExpr);
9409       default:
9410         break;
9411       }
9412 
9413     if (const auto *Lvalue = dyn_cast<DeclRefExpr>(Arg))
9414       if (Lvalue->getType()->isArrayType())
9415         return CheckFreeArgumentsStackArray(*this, CalleeName, Lvalue);
9416 
9417     if (const auto *Label = dyn_cast<AddrLabelExpr>(Arg)) {
9418       Diag(Label->getBeginLoc(), diag::warn_free_nonheap_object)
9419           << CalleeName << 0 /*object: */ << Label->getLabel()->getIdentifier();
9420       return;
9421     }
9422 
9423     if (isa<BlockExpr>(Arg)) {
9424       Diag(Arg->getBeginLoc(), diag::warn_free_nonheap_object)
9425           << CalleeName << 1 /*object: block*/;
9426       return;
9427     }
9428   }
9429   // Maybe the cast was important, check after the other cases.
9430   if (const auto *Cast = dyn_cast<CastExpr>(E->getArg(0)))
9431     return CheckFreeArgumentsCast(*this, CalleeName, Cast);
9432 }
9433 
9434 void
9435 Sema::CheckReturnValExpr(Expr *RetValExp, QualType lhsType,
9436                          SourceLocation ReturnLoc,
9437                          bool isObjCMethod,
9438                          const AttrVec *Attrs,
9439                          const FunctionDecl *FD) {
9440   // Check if the return value is null but should not be.
9441   if (((Attrs && hasSpecificAttr<ReturnsNonNullAttr>(*Attrs)) ||
9442        (!isObjCMethod && isNonNullType(lhsType))) &&
9443       CheckNonNullExpr(*this, RetValExp))
9444     Diag(ReturnLoc, diag::warn_null_ret)
9445       << (isObjCMethod ? 1 : 0) << RetValExp->getSourceRange();
9446 
9447   // C++11 [basic.stc.dynamic.allocation]p4:
9448   //   If an allocation function declared with a non-throwing
9449   //   exception-specification fails to allocate storage, it shall return
9450   //   a null pointer. Any other allocation function that fails to allocate
9451   //   storage shall indicate failure only by throwing an exception [...]
9452   if (FD) {
9453     OverloadedOperatorKind Op = FD->getOverloadedOperator();
9454     if (Op == OO_New || Op == OO_Array_New) {
9455       const FunctionProtoType *Proto
9456         = FD->getType()->castAs<FunctionProtoType>();
9457       if (!Proto->isNothrow(/*ResultIfDependent*/true) &&
9458           CheckNonNullExpr(*this, RetValExp))
9459         Diag(ReturnLoc, diag::warn_operator_new_returns_null)
9460           << FD << getLangOpts().CPlusPlus11;
9461     }
9462   }
9463 
9464   if (RetValExp && RetValExp->getType()->isWebAssemblyTableType()) {
9465     Diag(ReturnLoc, diag::err_wasm_table_art) << 1;
9466   }
9467 
9468   // PPC MMA non-pointer types are not allowed as return type. Checking the type
9469   // here prevent the user from using a PPC MMA type as trailing return type.
9470   if (Context.getTargetInfo().getTriple().isPPC64())
9471     PPC().CheckPPCMMAType(RetValExp->getType(), ReturnLoc);
9472 }
9473 
9474 void Sema::CheckFloatComparison(SourceLocation Loc, Expr *LHS, Expr *RHS,
9475                                 BinaryOperatorKind Opcode) {
9476   if (!BinaryOperator::isEqualityOp(Opcode))
9477     return;
9478 
9479   // Match and capture subexpressions such as "(float) X == 0.1".
9480   FloatingLiteral *FPLiteral;
9481   CastExpr *FPCast;
9482   auto getCastAndLiteral = [&FPLiteral, &FPCast](Expr *L, Expr *R) {
9483     FPLiteral = dyn_cast<FloatingLiteral>(L->IgnoreParens());
9484     FPCast = dyn_cast<CastExpr>(R->IgnoreParens());
9485     return FPLiteral && FPCast;
9486   };
9487 
9488   if (getCastAndLiteral(LHS, RHS) || getCastAndLiteral(RHS, LHS)) {
9489     auto *SourceTy = FPCast->getSubExpr()->getType()->getAs<BuiltinType>();
9490     auto *TargetTy = FPLiteral->getType()->getAs<BuiltinType>();
9491     if (SourceTy && TargetTy && SourceTy->isFloatingPoint() &&
9492         TargetTy->isFloatingPoint()) {
9493       bool Lossy;
9494       llvm::APFloat TargetC = FPLiteral->getValue();
9495       TargetC.convert(Context.getFloatTypeSemantics(QualType(SourceTy, 0)),
9496                       llvm::APFloat::rmNearestTiesToEven, &Lossy);
9497       if (Lossy) {
9498         // If the literal cannot be represented in the source type, then a
9499         // check for == is always false and check for != is always true.
9500         Diag(Loc, diag::warn_float_compare_literal)
9501             << (Opcode == BO_EQ) << QualType(SourceTy, 0)
9502             << LHS->getSourceRange() << RHS->getSourceRange();
9503         return;
9504       }
9505     }
9506   }
9507 
9508   // Match a more general floating-point equality comparison (-Wfloat-equal).
9509   Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts();
9510   Expr* RightExprSansParen = RHS->IgnoreParenImpCasts();
9511 
9512   // Special case: check for x == x (which is OK).
9513   // Do not emit warnings for such cases.
9514   if (auto *DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen))
9515     if (auto *DRR = dyn_cast<DeclRefExpr>(RightExprSansParen))
9516       if (DRL->getDecl() == DRR->getDecl())
9517         return;
9518 
9519   // Special case: check for comparisons against literals that can be exactly
9520   //  represented by APFloat.  In such cases, do not emit a warning.  This
9521   //  is a heuristic: often comparison against such literals are used to
9522   //  detect if a value in a variable has not changed.  This clearly can
9523   //  lead to false negatives.
9524   if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) {
9525     if (FLL->isExact())
9526       return;
9527   } else
9528     if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen))
9529       if (FLR->isExact())
9530         return;
9531 
9532   // Check for comparisons with builtin types.
9533   if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen))
9534     if (CL->getBuiltinCallee())
9535       return;
9536 
9537   if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen))
9538     if (CR->getBuiltinCallee())
9539       return;
9540 
9541   // Emit the diagnostic.
9542   Diag(Loc, diag::warn_floatingpoint_eq)
9543     << LHS->getSourceRange() << RHS->getSourceRange();
9544 }
9545 
9546 //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===//
9547 //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===//
9548 
9549 namespace {
9550 
9551 /// Structure recording the 'active' range of an integer-valued
9552 /// expression.
9553 struct IntRange {
9554   /// The number of bits active in the int. Note that this includes exactly one
9555   /// sign bit if !NonNegative.
9556   unsigned Width;
9557 
9558   /// True if the int is known not to have negative values. If so, all leading
9559   /// bits before Width are known zero, otherwise they are known to be the
9560   /// same as the MSB within Width.
9561   bool NonNegative;
9562 
9563   IntRange(unsigned Width, bool NonNegative)
9564       : Width(Width), NonNegative(NonNegative) {}
9565 
9566   /// Number of bits excluding the sign bit.
9567   unsigned valueBits() const {
9568     return NonNegative ? Width : Width - 1;
9569   }
9570 
9571   /// Returns the range of the bool type.
9572   static IntRange forBoolType() {
9573     return IntRange(1, true);
9574   }
9575 
9576   /// Returns the range of an opaque value of the given integral type.
9577   static IntRange forValueOfType(ASTContext &C, QualType T) {
9578     return forValueOfCanonicalType(C,
9579                           T->getCanonicalTypeInternal().getTypePtr());
9580   }
9581 
9582   /// Returns the range of an opaque value of a canonical integral type.
9583   static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) {
9584     assert(T->isCanonicalUnqualified());
9585 
9586     if (const VectorType *VT = dyn_cast<VectorType>(T))
9587       T = VT->getElementType().getTypePtr();
9588     if (const ComplexType *CT = dyn_cast<ComplexType>(T))
9589       T = CT->getElementType().getTypePtr();
9590     if (const AtomicType *AT = dyn_cast<AtomicType>(T))
9591       T = AT->getValueType().getTypePtr();
9592 
9593     if (!C.getLangOpts().CPlusPlus) {
9594       // For enum types in C code, use the underlying datatype.
9595       if (const EnumType *ET = dyn_cast<EnumType>(T))
9596         T = ET->getDecl()->getIntegerType().getDesugaredType(C).getTypePtr();
9597     } else if (const EnumType *ET = dyn_cast<EnumType>(T)) {
9598       // For enum types in C++, use the known bit width of the enumerators.
9599       EnumDecl *Enum = ET->getDecl();
9600       // In C++11, enums can have a fixed underlying type. Use this type to
9601       // compute the range.
9602       if (Enum->isFixed()) {
9603         return IntRange(C.getIntWidth(QualType(T, 0)),
9604                         !ET->isSignedIntegerOrEnumerationType());
9605       }
9606 
9607       unsigned NumPositive = Enum->getNumPositiveBits();
9608       unsigned NumNegative = Enum->getNumNegativeBits();
9609 
9610       if (NumNegative == 0)
9611         return IntRange(NumPositive, true/*NonNegative*/);
9612       else
9613         return IntRange(std::max(NumPositive + 1, NumNegative),
9614                         false/*NonNegative*/);
9615     }
9616 
9617     if (const auto *EIT = dyn_cast<BitIntType>(T))
9618       return IntRange(EIT->getNumBits(), EIT->isUnsigned());
9619 
9620     const BuiltinType *BT = cast<BuiltinType>(T);
9621     assert(BT->isInteger());
9622 
9623     return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
9624   }
9625 
9626   /// Returns the "target" range of a canonical integral type, i.e.
9627   /// the range of values expressible in the type.
9628   ///
9629   /// This matches forValueOfCanonicalType except that enums have the
9630   /// full range of their type, not the range of their enumerators.
9631   static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) {
9632     assert(T->isCanonicalUnqualified());
9633 
9634     if (const VectorType *VT = dyn_cast<VectorType>(T))
9635       T = VT->getElementType().getTypePtr();
9636     if (const ComplexType *CT = dyn_cast<ComplexType>(T))
9637       T = CT->getElementType().getTypePtr();
9638     if (const AtomicType *AT = dyn_cast<AtomicType>(T))
9639       T = AT->getValueType().getTypePtr();
9640     if (const EnumType *ET = dyn_cast<EnumType>(T))
9641       T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr();
9642 
9643     if (const auto *EIT = dyn_cast<BitIntType>(T))
9644       return IntRange(EIT->getNumBits(), EIT->isUnsigned());
9645 
9646     const BuiltinType *BT = cast<BuiltinType>(T);
9647     assert(BT->isInteger());
9648 
9649     return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
9650   }
9651 
9652   /// Returns the supremum of two ranges: i.e. their conservative merge.
9653   static IntRange join(IntRange L, IntRange R) {
9654     bool Unsigned = L.NonNegative && R.NonNegative;
9655     return IntRange(std::max(L.valueBits(), R.valueBits()) + !Unsigned,
9656                     L.NonNegative && R.NonNegative);
9657   }
9658 
9659   /// Return the range of a bitwise-AND of the two ranges.
9660   static IntRange bit_and(IntRange L, IntRange R) {
9661     unsigned Bits = std::max(L.Width, R.Width);
9662     bool NonNegative = false;
9663     if (L.NonNegative) {
9664       Bits = std::min(Bits, L.Width);
9665       NonNegative = true;
9666     }
9667     if (R.NonNegative) {
9668       Bits = std::min(Bits, R.Width);
9669       NonNegative = true;
9670     }
9671     return IntRange(Bits, NonNegative);
9672   }
9673 
9674   /// Return the range of a sum of the two ranges.
9675   static IntRange sum(IntRange L, IntRange R) {
9676     bool Unsigned = L.NonNegative && R.NonNegative;
9677     return IntRange(std::max(L.valueBits(), R.valueBits()) + 1 + !Unsigned,
9678                     Unsigned);
9679   }
9680 
9681   /// Return the range of a difference of the two ranges.
9682   static IntRange difference(IntRange L, IntRange R) {
9683     // We need a 1-bit-wider range if:
9684     //   1) LHS can be negative: least value can be reduced.
9685     //   2) RHS can be negative: greatest value can be increased.
9686     bool CanWiden = !L.NonNegative || !R.NonNegative;
9687     bool Unsigned = L.NonNegative && R.Width == 0;
9688     return IntRange(std::max(L.valueBits(), R.valueBits()) + CanWiden +
9689                         !Unsigned,
9690                     Unsigned);
9691   }
9692 
9693   /// Return the range of a product of the two ranges.
9694   static IntRange product(IntRange L, IntRange R) {
9695     // If both LHS and RHS can be negative, we can form
9696     //   -2^L * -2^R = 2^(L + R)
9697     // which requires L + R + 1 value bits to represent.
9698     bool CanWiden = !L.NonNegative && !R.NonNegative;
9699     bool Unsigned = L.NonNegative && R.NonNegative;
9700     return IntRange(L.valueBits() + R.valueBits() + CanWiden + !Unsigned,
9701                     Unsigned);
9702   }
9703 
9704   /// Return the range of a remainder operation between the two ranges.
9705   static IntRange rem(IntRange L, IntRange R) {
9706     // The result of a remainder can't be larger than the result of
9707     // either side. The sign of the result is the sign of the LHS.
9708     bool Unsigned = L.NonNegative;
9709     return IntRange(std::min(L.valueBits(), R.valueBits()) + !Unsigned,
9710                     Unsigned);
9711   }
9712 };
9713 
9714 } // namespace
9715 
9716 static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value,
9717                               unsigned MaxWidth) {
9718   if (value.isSigned() && value.isNegative())
9719     return IntRange(value.getSignificantBits(), false);
9720 
9721   if (value.getBitWidth() > MaxWidth)
9722     value = value.trunc(MaxWidth);
9723 
9724   // isNonNegative() just checks the sign bit without considering
9725   // signedness.
9726   return IntRange(value.getActiveBits(), true);
9727 }
9728 
9729 static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty,
9730                               unsigned MaxWidth) {
9731   if (result.isInt())
9732     return GetValueRange(C, result.getInt(), MaxWidth);
9733 
9734   if (result.isVector()) {
9735     IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth);
9736     for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) {
9737       IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth);
9738       R = IntRange::join(R, El);
9739     }
9740     return R;
9741   }
9742 
9743   if (result.isComplexInt()) {
9744     IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth);
9745     IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth);
9746     return IntRange::join(R, I);
9747   }
9748 
9749   // This can happen with lossless casts to intptr_t of "based" lvalues.
9750   // Assume it might use arbitrary bits.
9751   // FIXME: The only reason we need to pass the type in here is to get
9752   // the sign right on this one case.  It would be nice if APValue
9753   // preserved this.
9754   assert(result.isLValue() || result.isAddrLabelDiff());
9755   return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType());
9756 }
9757 
9758 static QualType GetExprType(const Expr *E) {
9759   QualType Ty = E->getType();
9760   if (const AtomicType *AtomicRHS = Ty->getAs<AtomicType>())
9761     Ty = AtomicRHS->getValueType();
9762   return Ty;
9763 }
9764 
9765 /// Attempts to estimate an approximate range for the given integer expression.
9766 /// Returns a range if successful, otherwise it returns \c std::nullopt if a
9767 /// reliable estimation cannot be determined.
9768 ///
9769 /// \param MaxWidth The width to which the value will be truncated.
9770 /// \param InConstantContext If \c true, interpret the expression within a
9771 ///        constant context.
9772 /// \param Approximate If \c true, provide a likely range of values by assuming
9773 ///        that arithmetic on narrower types remains within those types.
9774 ///        If \c false, return a range that includes all possible values
9775 ///        resulting from the expression.
9776 /// \returns A range of values that the expression might take, or
9777 ///          std::nullopt if a reliable estimation cannot be determined.
9778 static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E,
9779                                                unsigned MaxWidth,
9780                                                bool InConstantContext,
9781                                                bool Approximate) {
9782   E = E->IgnoreParens();
9783 
9784   // Try a full evaluation first.
9785   Expr::EvalResult result;
9786   if (E->EvaluateAsRValue(result, C, InConstantContext))
9787     return GetValueRange(C, result.Val, GetExprType(E), MaxWidth);
9788 
9789   // I think we only want to look through implicit casts here; if the
9790   // user has an explicit widening cast, we should treat the value as
9791   // being of the new, wider type.
9792   if (const auto *CE = dyn_cast<ImplicitCastExpr>(E)) {
9793     if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue)
9794       return TryGetExprRange(C, CE->getSubExpr(), MaxWidth, InConstantContext,
9795                              Approximate);
9796 
9797     IntRange OutputTypeRange = IntRange::forValueOfType(C, GetExprType(CE));
9798 
9799     bool isIntegerCast = CE->getCastKind() == CK_IntegralCast ||
9800                          CE->getCastKind() == CK_BooleanToSignedIntegral;
9801 
9802     // Assume that non-integer casts can span the full range of the type.
9803     if (!isIntegerCast)
9804       return OutputTypeRange;
9805 
9806     std::optional<IntRange> SubRange = TryGetExprRange(
9807         C, CE->getSubExpr(), std::min(MaxWidth, OutputTypeRange.Width),
9808         InConstantContext, Approximate);
9809     if (!SubRange)
9810       return std::nullopt;
9811 
9812     // Bail out if the subexpr's range is as wide as the cast type.
9813     if (SubRange->Width >= OutputTypeRange.Width)
9814       return OutputTypeRange;
9815 
9816     // Otherwise, we take the smaller width, and we're non-negative if
9817     // either the output type or the subexpr is.
9818     return IntRange(SubRange->Width,
9819                     SubRange->NonNegative || OutputTypeRange.NonNegative);
9820   }
9821 
9822   if (const auto *CO = dyn_cast<ConditionalOperator>(E)) {
9823     // If we can fold the condition, just take that operand.
9824     bool CondResult;
9825     if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C))
9826       return TryGetExprRange(
9827           C, CondResult ? CO->getTrueExpr() : CO->getFalseExpr(), MaxWidth,
9828           InConstantContext, Approximate);
9829 
9830     // Otherwise, conservatively merge.
9831     // TryGetExprRange requires an integer expression, but a throw expression
9832     // results in a void type.
9833     Expr *TrueExpr = CO->getTrueExpr();
9834     if (TrueExpr->getType()->isVoidType())
9835       return std::nullopt;
9836 
9837     std::optional<IntRange> L =
9838         TryGetExprRange(C, TrueExpr, MaxWidth, InConstantContext, Approximate);
9839     if (!L)
9840       return std::nullopt;
9841 
9842     Expr *FalseExpr = CO->getFalseExpr();
9843     if (FalseExpr->getType()->isVoidType())
9844       return std::nullopt;
9845 
9846     std::optional<IntRange> R =
9847         TryGetExprRange(C, FalseExpr, MaxWidth, InConstantContext, Approximate);
9848     if (!R)
9849       return std::nullopt;
9850 
9851     return IntRange::join(*L, *R);
9852   }
9853 
9854   if (const auto *BO = dyn_cast<BinaryOperator>(E)) {
9855     IntRange (*Combine)(IntRange, IntRange) = IntRange::join;
9856 
9857     switch (BO->getOpcode()) {
9858     case BO_Cmp:
9859       llvm_unreachable("builtin <=> should have class type");
9860 
9861     // Boolean-valued operations are single-bit and positive.
9862     case BO_LAnd:
9863     case BO_LOr:
9864     case BO_LT:
9865     case BO_GT:
9866     case BO_LE:
9867     case BO_GE:
9868     case BO_EQ:
9869     case BO_NE:
9870       return IntRange::forBoolType();
9871 
9872     // The type of the assignments is the type of the LHS, so the RHS
9873     // is not necessarily the same type.
9874     case BO_MulAssign:
9875     case BO_DivAssign:
9876     case BO_RemAssign:
9877     case BO_AddAssign:
9878     case BO_SubAssign:
9879     case BO_XorAssign:
9880     case BO_OrAssign:
9881       // TODO: bitfields?
9882       return IntRange::forValueOfType(C, GetExprType(E));
9883 
9884     // Simple assignments just pass through the RHS, which will have
9885     // been coerced to the LHS type.
9886     case BO_Assign:
9887       // TODO: bitfields?
9888       return TryGetExprRange(C, BO->getRHS(), MaxWidth, InConstantContext,
9889                              Approximate);
9890 
9891     // Operations with opaque sources are black-listed.
9892     case BO_PtrMemD:
9893     case BO_PtrMemI:
9894       return IntRange::forValueOfType(C, GetExprType(E));
9895 
9896     // Bitwise-and uses the *infinum* of the two source ranges.
9897     case BO_And:
9898     case BO_AndAssign:
9899       Combine = IntRange::bit_and;
9900       break;
9901 
9902     // Left shift gets black-listed based on a judgement call.
9903     case BO_Shl:
9904       // ...except that we want to treat '1 << (blah)' as logically
9905       // positive.  It's an important idiom.
9906       if (IntegerLiteral *I
9907             = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) {
9908         if (I->getValue() == 1) {
9909           IntRange R = IntRange::forValueOfType(C, GetExprType(E));
9910           return IntRange(R.Width, /*NonNegative*/ true);
9911         }
9912       }
9913       [[fallthrough]];
9914 
9915     case BO_ShlAssign:
9916       return IntRange::forValueOfType(C, GetExprType(E));
9917 
9918     // Right shift by a constant can narrow its left argument.
9919     case BO_Shr:
9920     case BO_ShrAssign: {
9921       std::optional<IntRange> L = TryGetExprRange(
9922           C, BO->getLHS(), MaxWidth, InConstantContext, Approximate);
9923       if (!L)
9924         return std::nullopt;
9925 
9926       // If the shift amount is a positive constant, drop the width by
9927       // that much.
9928       if (std::optional<llvm::APSInt> shift =
9929               BO->getRHS()->getIntegerConstantExpr(C)) {
9930         if (shift->isNonNegative()) {
9931           if (shift->uge(L->Width))
9932             L->Width = (L->NonNegative ? 0 : 1);
9933           else
9934             L->Width -= shift->getZExtValue();
9935         }
9936       }
9937 
9938       return L;
9939     }
9940 
9941     // Comma acts as its right operand.
9942     case BO_Comma:
9943       return TryGetExprRange(C, BO->getRHS(), MaxWidth, InConstantContext,
9944                              Approximate);
9945 
9946     case BO_Add:
9947       if (!Approximate)
9948         Combine = IntRange::sum;
9949       break;
9950 
9951     case BO_Sub:
9952       if (BO->getLHS()->getType()->isPointerType())
9953         return IntRange::forValueOfType(C, GetExprType(E));
9954       if (!Approximate)
9955         Combine = IntRange::difference;
9956       break;
9957 
9958     case BO_Mul:
9959       if (!Approximate)
9960         Combine = IntRange::product;
9961       break;
9962 
9963     // The width of a division result is mostly determined by the size
9964     // of the LHS.
9965     case BO_Div: {
9966       // Don't 'pre-truncate' the operands.
9967       unsigned opWidth = C.getIntWidth(GetExprType(E));
9968       std::optional<IntRange> L = TryGetExprRange(
9969           C, BO->getLHS(), opWidth, InConstantContext, Approximate);
9970       if (!L)
9971         return std::nullopt;
9972 
9973       // If the divisor is constant, use that.
9974       if (std::optional<llvm::APSInt> divisor =
9975               BO->getRHS()->getIntegerConstantExpr(C)) {
9976         unsigned log2 = divisor->logBase2(); // floor(log_2(divisor))
9977         if (log2 >= L->Width)
9978           L->Width = (L->NonNegative ? 0 : 1);
9979         else
9980           L->Width = std::min(L->Width - log2, MaxWidth);
9981         return L;
9982       }
9983 
9984       // Otherwise, just use the LHS's width.
9985       // FIXME: This is wrong if the LHS could be its minimal value and the RHS
9986       // could be -1.
9987       std::optional<IntRange> R = TryGetExprRange(
9988           C, BO->getRHS(), opWidth, InConstantContext, Approximate);
9989       if (!R)
9990         return std::nullopt;
9991 
9992       return IntRange(L->Width, L->NonNegative && R->NonNegative);
9993     }
9994 
9995     case BO_Rem:
9996       Combine = IntRange::rem;
9997       break;
9998 
9999     // The default behavior is okay for these.
10000     case BO_Xor:
10001     case BO_Or:
10002       break;
10003     }
10004 
10005     // Combine the two ranges, but limit the result to the type in which we
10006     // performed the computation.
10007     QualType T = GetExprType(E);
10008     unsigned opWidth = C.getIntWidth(T);
10009     std::optional<IntRange> L = TryGetExprRange(C, BO->getLHS(), opWidth,
10010                                                 InConstantContext, Approximate);
10011     if (!L)
10012       return std::nullopt;
10013 
10014     std::optional<IntRange> R = TryGetExprRange(C, BO->getRHS(), opWidth,
10015                                                 InConstantContext, Approximate);
10016     if (!R)
10017       return std::nullopt;
10018 
10019     IntRange C = Combine(*L, *R);
10020     C.NonNegative |= T->isUnsignedIntegerOrEnumerationType();
10021     C.Width = std::min(C.Width, MaxWidth);
10022     return C;
10023   }
10024 
10025   if (const auto *UO = dyn_cast<UnaryOperator>(E)) {
10026     switch (UO->getOpcode()) {
10027     // Boolean-valued operations are white-listed.
10028     case UO_LNot:
10029       return IntRange::forBoolType();
10030 
10031     // Operations with opaque sources are black-listed.
10032     case UO_Deref:
10033     case UO_AddrOf: // should be impossible
10034       return IntRange::forValueOfType(C, GetExprType(E));
10035 
10036     default:
10037       return TryGetExprRange(C, UO->getSubExpr(), MaxWidth, InConstantContext,
10038                              Approximate);
10039     }
10040   }
10041 
10042   if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E))
10043     return TryGetExprRange(C, OVE->getSourceExpr(), MaxWidth, InConstantContext,
10044                            Approximate);
10045 
10046   if (const auto *BitField = E->getSourceBitField())
10047     return IntRange(BitField->getBitWidthValue(),
10048                     BitField->getType()->isUnsignedIntegerOrEnumerationType());
10049 
10050   if (GetExprType(E)->isVoidType())
10051     return std::nullopt;
10052 
10053   return IntRange::forValueOfType(C, GetExprType(E));
10054 }
10055 
10056 static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E,
10057                                                bool InConstantContext,
10058                                                bool Approximate) {
10059   return TryGetExprRange(C, E, C.getIntWidth(GetExprType(E)), InConstantContext,
10060                          Approximate);
10061 }
10062 
10063 /// Checks whether the given value, which currently has the given
10064 /// source semantics, has the same value when coerced through the
10065 /// target semantics.
10066 static bool IsSameFloatAfterCast(const llvm::APFloat &value,
10067                                  const llvm::fltSemantics &Src,
10068                                  const llvm::fltSemantics &Tgt) {
10069   llvm::APFloat truncated = value;
10070 
10071   bool ignored;
10072   truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored);
10073   truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored);
10074 
10075   return truncated.bitwiseIsEqual(value);
10076 }
10077 
10078 /// Checks whether the given value, which currently has the given
10079 /// source semantics, has the same value when coerced through the
10080 /// target semantics.
10081 ///
10082 /// The value might be a vector of floats (or a complex number).
10083 static bool IsSameFloatAfterCast(const APValue &value,
10084                                  const llvm::fltSemantics &Src,
10085                                  const llvm::fltSemantics &Tgt) {
10086   if (value.isFloat())
10087     return IsSameFloatAfterCast(value.getFloat(), Src, Tgt);
10088 
10089   if (value.isVector()) {
10090     for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i)
10091       if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt))
10092         return false;
10093     return true;
10094   }
10095 
10096   assert(value.isComplexFloat());
10097   return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) &&
10098           IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt));
10099 }
10100 
10101 static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC,
10102                                        bool IsListInit = false);
10103 
10104 static bool IsEnumConstOrFromMacro(Sema &S, Expr *E) {
10105   // Suppress cases where we are comparing against an enum constant.
10106   if (const DeclRefExpr *DR =
10107       dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
10108     if (isa<EnumConstantDecl>(DR->getDecl()))
10109       return true;
10110 
10111   // Suppress cases where the value is expanded from a macro, unless that macro
10112   // is how a language represents a boolean literal. This is the case in both C
10113   // and Objective-C.
10114   SourceLocation BeginLoc = E->getBeginLoc();
10115   if (BeginLoc.isMacroID()) {
10116     StringRef MacroName = Lexer::getImmediateMacroName(
10117         BeginLoc, S.getSourceManager(), S.getLangOpts());
10118     return MacroName != "YES" && MacroName != "NO" &&
10119            MacroName != "true" && MacroName != "false";
10120   }
10121 
10122   return false;
10123 }
10124 
10125 static bool isKnownToHaveUnsignedValue(Expr *E) {
10126   return E->getType()->isIntegerType() &&
10127          (!E->getType()->isSignedIntegerType() ||
10128           !E->IgnoreParenImpCasts()->getType()->isSignedIntegerType());
10129 }
10130 
10131 namespace {
10132 /// The promoted range of values of a type. In general this has the
10133 /// following structure:
10134 ///
10135 ///     |-----------| . . . |-----------|
10136 ///     ^           ^       ^           ^
10137 ///    Min       HoleMin  HoleMax      Max
10138 ///
10139 /// ... where there is only a hole if a signed type is promoted to unsigned
10140 /// (in which case Min and Max are the smallest and largest representable
10141 /// values).
10142 struct PromotedRange {
10143   // Min, or HoleMax if there is a hole.
10144   llvm::APSInt PromotedMin;
10145   // Max, or HoleMin if there is a hole.
10146   llvm::APSInt PromotedMax;
10147 
10148   PromotedRange(IntRange R, unsigned BitWidth, bool Unsigned) {
10149     if (R.Width == 0)
10150       PromotedMin = PromotedMax = llvm::APSInt(BitWidth, Unsigned);
10151     else if (R.Width >= BitWidth && !Unsigned) {
10152       // Promotion made the type *narrower*. This happens when promoting
10153       // a < 32-bit unsigned / <= 32-bit signed bit-field to 'signed int'.
10154       // Treat all values of 'signed int' as being in range for now.
10155       PromotedMin = llvm::APSInt::getMinValue(BitWidth, Unsigned);
10156       PromotedMax = llvm::APSInt::getMaxValue(BitWidth, Unsigned);
10157     } else {
10158       PromotedMin = llvm::APSInt::getMinValue(R.Width, R.NonNegative)
10159                         .extOrTrunc(BitWidth);
10160       PromotedMin.setIsUnsigned(Unsigned);
10161 
10162       PromotedMax = llvm::APSInt::getMaxValue(R.Width, R.NonNegative)
10163                         .extOrTrunc(BitWidth);
10164       PromotedMax.setIsUnsigned(Unsigned);
10165     }
10166   }
10167 
10168   // Determine whether this range is contiguous (has no hole).
10169   bool isContiguous() const { return PromotedMin <= PromotedMax; }
10170 
10171   // Where a constant value is within the range.
10172   enum ComparisonResult {
10173     LT = 0x1,
10174     LE = 0x2,
10175     GT = 0x4,
10176     GE = 0x8,
10177     EQ = 0x10,
10178     NE = 0x20,
10179     InRangeFlag = 0x40,
10180 
10181     Less = LE | LT | NE,
10182     Min = LE | InRangeFlag,
10183     InRange = InRangeFlag,
10184     Max = GE | InRangeFlag,
10185     Greater = GE | GT | NE,
10186 
10187     OnlyValue = LE | GE | EQ | InRangeFlag,
10188     InHole = NE
10189   };
10190 
10191   ComparisonResult compare(const llvm::APSInt &Value) const {
10192     assert(Value.getBitWidth() == PromotedMin.getBitWidth() &&
10193            Value.isUnsigned() == PromotedMin.isUnsigned());
10194     if (!isContiguous()) {
10195       assert(Value.isUnsigned() && "discontiguous range for signed compare");
10196       if (Value.isMinValue()) return Min;
10197       if (Value.isMaxValue()) return Max;
10198       if (Value >= PromotedMin) return InRange;
10199       if (Value <= PromotedMax) return InRange;
10200       return InHole;
10201     }
10202 
10203     switch (llvm::APSInt::compareValues(Value, PromotedMin)) {
10204     case -1: return Less;
10205     case 0: return PromotedMin == PromotedMax ? OnlyValue : Min;
10206     case 1:
10207       switch (llvm::APSInt::compareValues(Value, PromotedMax)) {
10208       case -1: return InRange;
10209       case 0: return Max;
10210       case 1: return Greater;
10211       }
10212     }
10213 
10214     llvm_unreachable("impossible compare result");
10215   }
10216 
10217   static std::optional<StringRef>
10218   constantValue(BinaryOperatorKind Op, ComparisonResult R, bool ConstantOnRHS) {
10219     if (Op == BO_Cmp) {
10220       ComparisonResult LTFlag = LT, GTFlag = GT;
10221       if (ConstantOnRHS) std::swap(LTFlag, GTFlag);
10222 
10223       if (R & EQ) return StringRef("'std::strong_ordering::equal'");
10224       if (R & LTFlag) return StringRef("'std::strong_ordering::less'");
10225       if (R & GTFlag) return StringRef("'std::strong_ordering::greater'");
10226       return std::nullopt;
10227     }
10228 
10229     ComparisonResult TrueFlag, FalseFlag;
10230     if (Op == BO_EQ) {
10231       TrueFlag = EQ;
10232       FalseFlag = NE;
10233     } else if (Op == BO_NE) {
10234       TrueFlag = NE;
10235       FalseFlag = EQ;
10236     } else {
10237       if ((Op == BO_LT || Op == BO_GE) ^ ConstantOnRHS) {
10238         TrueFlag = LT;
10239         FalseFlag = GE;
10240       } else {
10241         TrueFlag = GT;
10242         FalseFlag = LE;
10243       }
10244       if (Op == BO_GE || Op == BO_LE)
10245         std::swap(TrueFlag, FalseFlag);
10246     }
10247     if (R & TrueFlag)
10248       return StringRef("true");
10249     if (R & FalseFlag)
10250       return StringRef("false");
10251     return std::nullopt;
10252   }
10253 };
10254 }
10255 
10256 static bool HasEnumType(Expr *E) {
10257   // Strip off implicit integral promotions.
10258   while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
10259     if (ICE->getCastKind() != CK_IntegralCast &&
10260         ICE->getCastKind() != CK_NoOp)
10261       break;
10262     E = ICE->getSubExpr();
10263   }
10264 
10265   return E->getType()->isEnumeralType();
10266 }
10267 
10268 static int classifyConstantValue(Expr *Constant) {
10269   // The values of this enumeration are used in the diagnostics
10270   // diag::warn_out_of_range_compare and diag::warn_tautological_bool_compare.
10271   enum ConstantValueKind {
10272     Miscellaneous = 0,
10273     LiteralTrue,
10274     LiteralFalse
10275   };
10276   if (auto *BL = dyn_cast<CXXBoolLiteralExpr>(Constant))
10277     return BL->getValue() ? ConstantValueKind::LiteralTrue
10278                           : ConstantValueKind::LiteralFalse;
10279   return ConstantValueKind::Miscellaneous;
10280 }
10281 
10282 static bool CheckTautologicalComparison(Sema &S, BinaryOperator *E,
10283                                         Expr *Constant, Expr *Other,
10284                                         const llvm::APSInt &Value,
10285                                         bool RhsConstant) {
10286   if (S.inTemplateInstantiation())
10287     return false;
10288 
10289   Expr *OriginalOther = Other;
10290 
10291   Constant = Constant->IgnoreParenImpCasts();
10292   Other = Other->IgnoreParenImpCasts();
10293 
10294   // Suppress warnings on tautological comparisons between values of the same
10295   // enumeration type. There are only two ways we could warn on this:
10296   //  - If the constant is outside the range of representable values of
10297   //    the enumeration. In such a case, we should warn about the cast
10298   //    to enumeration type, not about the comparison.
10299   //  - If the constant is the maximum / minimum in-range value. For an
10300   //    enumeratin type, such comparisons can be meaningful and useful.
10301   if (Constant->getType()->isEnumeralType() &&
10302       S.Context.hasSameUnqualifiedType(Constant->getType(), Other->getType()))
10303     return false;
10304 
10305   std::optional<IntRange> OtherValueRange = TryGetExprRange(
10306       S.Context, Other, S.isConstantEvaluatedContext(), /*Approximate=*/false);
10307   if (!OtherValueRange)
10308     return false;
10309 
10310   QualType OtherT = Other->getType();
10311   if (const auto *AT = OtherT->getAs<AtomicType>())
10312     OtherT = AT->getValueType();
10313   IntRange OtherTypeRange = IntRange::forValueOfType(S.Context, OtherT);
10314 
10315   // Special case for ObjC BOOL on targets where its a typedef for a signed char
10316   // (Namely, macOS). FIXME: IntRange::forValueOfType should do this.
10317   bool IsObjCSignedCharBool = S.getLangOpts().ObjC &&
10318                               S.ObjC().NSAPIObj->isObjCBOOLType(OtherT) &&
10319                               OtherT->isSpecificBuiltinType(BuiltinType::SChar);
10320 
10321   // Whether we're treating Other as being a bool because of the form of
10322   // expression despite it having another type (typically 'int' in C).
10323   bool OtherIsBooleanDespiteType =
10324       !OtherT->isBooleanType() && Other->isKnownToHaveBooleanValue();
10325   if (OtherIsBooleanDespiteType || IsObjCSignedCharBool)
10326     OtherTypeRange = *OtherValueRange = IntRange::forBoolType();
10327 
10328   // Check if all values in the range of possible values of this expression
10329   // lead to the same comparison outcome.
10330   PromotedRange OtherPromotedValueRange(*OtherValueRange, Value.getBitWidth(),
10331                                         Value.isUnsigned());
10332   auto Cmp = OtherPromotedValueRange.compare(Value);
10333   auto Result = PromotedRange::constantValue(E->getOpcode(), Cmp, RhsConstant);
10334   if (!Result)
10335     return false;
10336 
10337   // Also consider the range determined by the type alone. This allows us to
10338   // classify the warning under the proper diagnostic group.
10339   bool TautologicalTypeCompare = false;
10340   {
10341     PromotedRange OtherPromotedTypeRange(OtherTypeRange, Value.getBitWidth(),
10342                                          Value.isUnsigned());
10343     auto TypeCmp = OtherPromotedTypeRange.compare(Value);
10344     if (auto TypeResult = PromotedRange::constantValue(E->getOpcode(), TypeCmp,
10345                                                        RhsConstant)) {
10346       TautologicalTypeCompare = true;
10347       Cmp = TypeCmp;
10348       Result = TypeResult;
10349     }
10350   }
10351 
10352   // Don't warn if the non-constant operand actually always evaluates to the
10353   // same value.
10354   if (!TautologicalTypeCompare && OtherValueRange->Width == 0)
10355     return false;
10356 
10357   // Suppress the diagnostic for an in-range comparison if the constant comes
10358   // from a macro or enumerator. We don't want to diagnose
10359   //
10360   //   some_long_value <= INT_MAX
10361   //
10362   // when sizeof(int) == sizeof(long).
10363   bool InRange = Cmp & PromotedRange::InRangeFlag;
10364   if (InRange && IsEnumConstOrFromMacro(S, Constant))
10365     return false;
10366 
10367   // A comparison of an unsigned bit-field against 0 is really a type problem,
10368   // even though at the type level the bit-field might promote to 'signed int'.
10369   if (Other->refersToBitField() && InRange && Value == 0 &&
10370       Other->getType()->isUnsignedIntegerOrEnumerationType())
10371     TautologicalTypeCompare = true;
10372 
10373   // If this is a comparison to an enum constant, include that
10374   // constant in the diagnostic.
10375   const EnumConstantDecl *ED = nullptr;
10376   if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Constant))
10377     ED = dyn_cast<EnumConstantDecl>(DR->getDecl());
10378 
10379   // Should be enough for uint128 (39 decimal digits)
10380   SmallString<64> PrettySourceValue;
10381   llvm::raw_svector_ostream OS(PrettySourceValue);
10382   if (ED) {
10383     OS << '\'' << *ED << "' (" << Value << ")";
10384   } else if (auto *BL = dyn_cast<ObjCBoolLiteralExpr>(
10385                Constant->IgnoreParenImpCasts())) {
10386     OS << (BL->getValue() ? "YES" : "NO");
10387   } else {
10388     OS << Value;
10389   }
10390 
10391   if (!TautologicalTypeCompare) {
10392     S.Diag(E->getOperatorLoc(), diag::warn_tautological_compare_value_range)
10393         << RhsConstant << OtherValueRange->Width << OtherValueRange->NonNegative
10394         << E->getOpcodeStr() << OS.str() << *Result
10395         << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
10396     return true;
10397   }
10398 
10399   if (IsObjCSignedCharBool) {
10400     S.DiagRuntimeBehavior(E->getOperatorLoc(), E,
10401                           S.PDiag(diag::warn_tautological_compare_objc_bool)
10402                               << OS.str() << *Result);
10403     return true;
10404   }
10405 
10406   // FIXME: We use a somewhat different formatting for the in-range cases and
10407   // cases involving boolean values for historical reasons. We should pick a
10408   // consistent way of presenting these diagnostics.
10409   if (!InRange || Other->isKnownToHaveBooleanValue()) {
10410 
10411     S.DiagRuntimeBehavior(
10412         E->getOperatorLoc(), E,
10413         S.PDiag(!InRange ? diag::warn_out_of_range_compare
10414                          : diag::warn_tautological_bool_compare)
10415             << OS.str() << classifyConstantValue(Constant) << OtherT
10416             << OtherIsBooleanDespiteType << *Result
10417             << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange());
10418   } else {
10419     bool IsCharTy = OtherT.withoutLocalFastQualifiers() == S.Context.CharTy;
10420     unsigned Diag =
10421         (isKnownToHaveUnsignedValue(OriginalOther) && Value == 0)
10422             ? (HasEnumType(OriginalOther)
10423                    ? diag::warn_unsigned_enum_always_true_comparison
10424                    : IsCharTy ? diag::warn_unsigned_char_always_true_comparison
10425                               : diag::warn_unsigned_always_true_comparison)
10426             : diag::warn_tautological_constant_compare;
10427 
10428     S.Diag(E->getOperatorLoc(), Diag)
10429         << RhsConstant << OtherT << E->getOpcodeStr() << OS.str() << *Result
10430         << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
10431   }
10432 
10433   return true;
10434 }
10435 
10436 /// Analyze the operands of the given comparison.  Implements the
10437 /// fallback case from AnalyzeComparison.
10438 static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) {
10439   AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
10440   AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
10441 }
10442 
10443 /// Implements -Wsign-compare.
10444 ///
10445 /// \param E the binary operator to check for warnings
10446 static void AnalyzeComparison(Sema &S, BinaryOperator *E) {
10447   // The type the comparison is being performed in.
10448   QualType T = E->getLHS()->getType();
10449 
10450   // Only analyze comparison operators where both sides have been converted to
10451   // the same type.
10452   if (!S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType()))
10453     return AnalyzeImpConvsInComparison(S, E);
10454 
10455   // Don't analyze value-dependent comparisons directly.
10456   if (E->isValueDependent())
10457     return AnalyzeImpConvsInComparison(S, E);
10458 
10459   Expr *LHS = E->getLHS();
10460   Expr *RHS = E->getRHS();
10461 
10462   if (T->isIntegralType(S.Context)) {
10463     std::optional<llvm::APSInt> RHSValue =
10464         RHS->getIntegerConstantExpr(S.Context);
10465     std::optional<llvm::APSInt> LHSValue =
10466         LHS->getIntegerConstantExpr(S.Context);
10467 
10468     // We don't care about expressions whose result is a constant.
10469     if (RHSValue && LHSValue)
10470       return AnalyzeImpConvsInComparison(S, E);
10471 
10472     // We only care about expressions where just one side is literal
10473     if ((bool)RHSValue ^ (bool)LHSValue) {
10474       // Is the constant on the RHS or LHS?
10475       const bool RhsConstant = (bool)RHSValue;
10476       Expr *Const = RhsConstant ? RHS : LHS;
10477       Expr *Other = RhsConstant ? LHS : RHS;
10478       const llvm::APSInt &Value = RhsConstant ? *RHSValue : *LHSValue;
10479 
10480       // Check whether an integer constant comparison results in a value
10481       // of 'true' or 'false'.
10482       if (CheckTautologicalComparison(S, E, Const, Other, Value, RhsConstant))
10483         return AnalyzeImpConvsInComparison(S, E);
10484     }
10485   }
10486 
10487   if (!T->hasUnsignedIntegerRepresentation()) {
10488     // We don't do anything special if this isn't an unsigned integral
10489     // comparison:  we're only interested in integral comparisons, and
10490     // signed comparisons only happen in cases we don't care to warn about.
10491     return AnalyzeImpConvsInComparison(S, E);
10492   }
10493 
10494   LHS = LHS->IgnoreParenImpCasts();
10495   RHS = RHS->IgnoreParenImpCasts();
10496 
10497   if (!S.getLangOpts().CPlusPlus) {
10498     // Avoid warning about comparison of integers with different signs when
10499     // RHS/LHS has a `typeof(E)` type whose sign is different from the sign of
10500     // the type of `E`.
10501     if (const auto *TET = dyn_cast<TypeOfExprType>(LHS->getType()))
10502       LHS = TET->getUnderlyingExpr()->IgnoreParenImpCasts();
10503     if (const auto *TET = dyn_cast<TypeOfExprType>(RHS->getType()))
10504       RHS = TET->getUnderlyingExpr()->IgnoreParenImpCasts();
10505   }
10506 
10507   // Check to see if one of the (unmodified) operands is of different
10508   // signedness.
10509   Expr *signedOperand, *unsignedOperand;
10510   if (LHS->getType()->hasSignedIntegerRepresentation()) {
10511     assert(!RHS->getType()->hasSignedIntegerRepresentation() &&
10512            "unsigned comparison between two signed integer expressions?");
10513     signedOperand = LHS;
10514     unsignedOperand = RHS;
10515   } else if (RHS->getType()->hasSignedIntegerRepresentation()) {
10516     signedOperand = RHS;
10517     unsignedOperand = LHS;
10518   } else {
10519     return AnalyzeImpConvsInComparison(S, E);
10520   }
10521 
10522   // Otherwise, calculate the effective range of the signed operand.
10523   std::optional<IntRange> signedRange =
10524       TryGetExprRange(S.Context, signedOperand, S.isConstantEvaluatedContext(),
10525                       /*Approximate=*/true);
10526   if (!signedRange)
10527     return;
10528 
10529   // Go ahead and analyze implicit conversions in the operands.  Note
10530   // that we skip the implicit conversions on both sides.
10531   AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc());
10532   AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc());
10533 
10534   // If the signed range is non-negative, -Wsign-compare won't fire.
10535   if (signedRange->NonNegative)
10536     return;
10537 
10538   // For (in)equality comparisons, if the unsigned operand is a
10539   // constant which cannot collide with a overflowed signed operand,
10540   // then reinterpreting the signed operand as unsigned will not
10541   // change the result of the comparison.
10542   if (E->isEqualityOp()) {
10543     unsigned comparisonWidth = S.Context.getIntWidth(T);
10544     std::optional<IntRange> unsignedRange = TryGetExprRange(
10545         S.Context, unsignedOperand, S.isConstantEvaluatedContext(),
10546         /*Approximate=*/true);
10547     if (!unsignedRange)
10548       return;
10549 
10550     // We should never be unable to prove that the unsigned operand is
10551     // non-negative.
10552     assert(unsignedRange->NonNegative && "unsigned range includes negative?");
10553 
10554     if (unsignedRange->Width < comparisonWidth)
10555       return;
10556   }
10557 
10558   S.DiagRuntimeBehavior(E->getOperatorLoc(), E,
10559                         S.PDiag(diag::warn_mixed_sign_comparison)
10560                             << LHS->getType() << RHS->getType()
10561                             << LHS->getSourceRange() << RHS->getSourceRange());
10562 }
10563 
10564 /// Analyzes an attempt to assign the given value to a bitfield.
10565 ///
10566 /// Returns true if there was something fishy about the attempt.
10567 static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init,
10568                                       SourceLocation InitLoc) {
10569   assert(Bitfield->isBitField());
10570   if (Bitfield->isInvalidDecl())
10571     return false;
10572 
10573   // White-list bool bitfields.
10574   QualType BitfieldType = Bitfield->getType();
10575   if (BitfieldType->isBooleanType())
10576      return false;
10577 
10578   if (BitfieldType->isEnumeralType()) {
10579     EnumDecl *BitfieldEnumDecl = BitfieldType->castAs<EnumType>()->getDecl();
10580     // If the underlying enum type was not explicitly specified as an unsigned
10581     // type and the enum contain only positive values, MSVC++ will cause an
10582     // inconsistency by storing this as a signed type.
10583     if (S.getLangOpts().CPlusPlus11 &&
10584         !BitfieldEnumDecl->getIntegerTypeSourceInfo() &&
10585         BitfieldEnumDecl->getNumPositiveBits() > 0 &&
10586         BitfieldEnumDecl->getNumNegativeBits() == 0) {
10587       S.Diag(InitLoc, diag::warn_no_underlying_type_specified_for_enum_bitfield)
10588           << BitfieldEnumDecl;
10589     }
10590   }
10591 
10592   // Ignore value- or type-dependent expressions.
10593   if (Bitfield->getBitWidth()->isValueDependent() ||
10594       Bitfield->getBitWidth()->isTypeDependent() ||
10595       Init->isValueDependent() ||
10596       Init->isTypeDependent())
10597     return false;
10598 
10599   Expr *OriginalInit = Init->IgnoreParenImpCasts();
10600   unsigned FieldWidth = Bitfield->getBitWidthValue();
10601 
10602   Expr::EvalResult Result;
10603   if (!OriginalInit->EvaluateAsInt(Result, S.Context,
10604                                    Expr::SE_AllowSideEffects)) {
10605     // The RHS is not constant.  If the RHS has an enum type, make sure the
10606     // bitfield is wide enough to hold all the values of the enum without
10607     // truncation.
10608     if (const auto *EnumTy = OriginalInit->getType()->getAs<EnumType>()) {
10609       EnumDecl *ED = EnumTy->getDecl();
10610       bool SignedBitfield = BitfieldType->isSignedIntegerType();
10611 
10612       // Enum types are implicitly signed on Windows, so check if there are any
10613       // negative enumerators to see if the enum was intended to be signed or
10614       // not.
10615       bool SignedEnum = ED->getNumNegativeBits() > 0;
10616 
10617       // Check for surprising sign changes when assigning enum values to a
10618       // bitfield of different signedness.  If the bitfield is signed and we
10619       // have exactly the right number of bits to store this unsigned enum,
10620       // suggest changing the enum to an unsigned type. This typically happens
10621       // on Windows where unfixed enums always use an underlying type of 'int'.
10622       unsigned DiagID = 0;
10623       if (SignedEnum && !SignedBitfield) {
10624         DiagID = diag::warn_unsigned_bitfield_assigned_signed_enum;
10625       } else if (SignedBitfield && !SignedEnum &&
10626                  ED->getNumPositiveBits() == FieldWidth) {
10627         DiagID = diag::warn_signed_bitfield_enum_conversion;
10628       }
10629 
10630       if (DiagID) {
10631         S.Diag(InitLoc, DiagID) << Bitfield << ED;
10632         TypeSourceInfo *TSI = Bitfield->getTypeSourceInfo();
10633         SourceRange TypeRange =
10634             TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange();
10635         S.Diag(Bitfield->getTypeSpecStartLoc(), diag::note_change_bitfield_sign)
10636             << SignedEnum << TypeRange;
10637       }
10638 
10639       // Compute the required bitwidth. If the enum has negative values, we need
10640       // one more bit than the normal number of positive bits to represent the
10641       // sign bit.
10642       unsigned BitsNeeded = SignedEnum ? std::max(ED->getNumPositiveBits() + 1,
10643                                                   ED->getNumNegativeBits())
10644                                        : ED->getNumPositiveBits();
10645 
10646       // Check the bitwidth.
10647       if (BitsNeeded > FieldWidth) {
10648         Expr *WidthExpr = Bitfield->getBitWidth();
10649         S.Diag(InitLoc, diag::warn_bitfield_too_small_for_enum)
10650             << Bitfield << ED;
10651         S.Diag(WidthExpr->getExprLoc(), diag::note_widen_bitfield)
10652             << BitsNeeded << ED << WidthExpr->getSourceRange();
10653       }
10654     }
10655 
10656     return false;
10657   }
10658 
10659   llvm::APSInt Value = Result.Val.getInt();
10660 
10661   unsigned OriginalWidth = Value.getBitWidth();
10662 
10663   // In C, the macro 'true' from stdbool.h will evaluate to '1'; To reduce
10664   // false positives where the user is demonstrating they intend to use the
10665   // bit-field as a Boolean, check to see if the value is 1 and we're assigning
10666   // to a one-bit bit-field to see if the value came from a macro named 'true'.
10667   bool OneAssignedToOneBitBitfield = FieldWidth == 1 && Value == 1;
10668   if (OneAssignedToOneBitBitfield && !S.LangOpts.CPlusPlus) {
10669     SourceLocation MaybeMacroLoc = OriginalInit->getBeginLoc();
10670     if (S.SourceMgr.isInSystemMacro(MaybeMacroLoc) &&
10671         S.findMacroSpelling(MaybeMacroLoc, "true"))
10672       return false;
10673   }
10674 
10675   if (!Value.isSigned() || Value.isNegative())
10676     if (UnaryOperator *UO = dyn_cast<UnaryOperator>(OriginalInit))
10677       if (UO->getOpcode() == UO_Minus || UO->getOpcode() == UO_Not)
10678         OriginalWidth = Value.getSignificantBits();
10679 
10680   if (OriginalWidth <= FieldWidth)
10681     return false;
10682 
10683   // Compute the value which the bitfield will contain.
10684   llvm::APSInt TruncatedValue = Value.trunc(FieldWidth);
10685   TruncatedValue.setIsSigned(BitfieldType->isSignedIntegerType());
10686 
10687   // Check whether the stored value is equal to the original value.
10688   TruncatedValue = TruncatedValue.extend(OriginalWidth);
10689   if (llvm::APSInt::isSameValue(Value, TruncatedValue))
10690     return false;
10691 
10692   std::string PrettyValue = toString(Value, 10);
10693   std::string PrettyTrunc = toString(TruncatedValue, 10);
10694 
10695   S.Diag(InitLoc, OneAssignedToOneBitBitfield
10696                       ? diag::warn_impcast_single_bit_bitield_precision_constant
10697                       : diag::warn_impcast_bitfield_precision_constant)
10698       << PrettyValue << PrettyTrunc << OriginalInit->getType()
10699       << Init->getSourceRange();
10700 
10701   return true;
10702 }
10703 
10704 /// Analyze the given simple or compound assignment for warning-worthy
10705 /// operations.
10706 static void AnalyzeAssignment(Sema &S, BinaryOperator *E) {
10707   // Just recurse on the LHS.
10708   AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
10709 
10710   // We want to recurse on the RHS as normal unless we're assigning to
10711   // a bitfield.
10712   if (FieldDecl *Bitfield = E->getLHS()->getSourceBitField()) {
10713     if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(),
10714                                   E->getOperatorLoc())) {
10715       // Recurse, ignoring any implicit conversions on the RHS.
10716       return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(),
10717                                         E->getOperatorLoc());
10718     }
10719   }
10720 
10721   AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
10722 
10723   // Diagnose implicitly sequentially-consistent atomic assignment.
10724   if (E->getLHS()->getType()->isAtomicType())
10725     S.Diag(E->getRHS()->getBeginLoc(), diag::warn_atomic_implicit_seq_cst);
10726 }
10727 
10728 /// Diagnose an implicit cast;  purely a helper for CheckImplicitConversion.
10729 static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T,
10730                             SourceLocation CContext, unsigned diag,
10731                             bool pruneControlFlow = false) {
10732   if (pruneControlFlow) {
10733     S.DiagRuntimeBehavior(E->getExprLoc(), E,
10734                           S.PDiag(diag)
10735                               << SourceType << T << E->getSourceRange()
10736                               << SourceRange(CContext));
10737     return;
10738   }
10739   S.Diag(E->getExprLoc(), diag)
10740     << SourceType << T << E->getSourceRange() << SourceRange(CContext);
10741 }
10742 
10743 /// Diagnose an implicit cast;  purely a helper for CheckImplicitConversion.
10744 static void DiagnoseImpCast(Sema &S, Expr *E, QualType T,
10745                             SourceLocation CContext,
10746                             unsigned diag, bool pruneControlFlow = false) {
10747   DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow);
10748 }
10749 
10750 /// Diagnose an implicit cast from a floating point value to an integer value.
10751 static void DiagnoseFloatingImpCast(Sema &S, Expr *E, QualType T,
10752                                     SourceLocation CContext) {
10753   const bool IsBool = T->isSpecificBuiltinType(BuiltinType::Bool);
10754   const bool PruneWarnings = S.inTemplateInstantiation();
10755 
10756   Expr *InnerE = E->IgnoreParenImpCasts();
10757   // We also want to warn on, e.g., "int i = -1.234"
10758   if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE))
10759     if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus)
10760       InnerE = UOp->getSubExpr()->IgnoreParenImpCasts();
10761 
10762   const bool IsLiteral =
10763       isa<FloatingLiteral>(E) || isa<FloatingLiteral>(InnerE);
10764 
10765   llvm::APFloat Value(0.0);
10766   bool IsConstant =
10767     E->EvaluateAsFloat(Value, S.Context, Expr::SE_AllowSideEffects);
10768   if (!IsConstant) {
10769     if (S.ObjC().isSignedCharBool(T)) {
10770       return S.ObjC().adornBoolConversionDiagWithTernaryFixit(
10771           E, S.Diag(CContext, diag::warn_impcast_float_to_objc_signed_char_bool)
10772                  << E->getType());
10773     }
10774 
10775     return DiagnoseImpCast(S, E, T, CContext,
10776                            diag::warn_impcast_float_integer, PruneWarnings);
10777   }
10778 
10779   bool isExact = false;
10780 
10781   llvm::APSInt IntegerValue(S.Context.getIntWidth(T),
10782                             T->hasUnsignedIntegerRepresentation());
10783   llvm::APFloat::opStatus Result = Value.convertToInteger(
10784       IntegerValue, llvm::APFloat::rmTowardZero, &isExact);
10785 
10786   // FIXME: Force the precision of the source value down so we don't print
10787   // digits which are usually useless (we don't really care here if we
10788   // truncate a digit by accident in edge cases).  Ideally, APFloat::toString
10789   // would automatically print the shortest representation, but it's a bit
10790   // tricky to implement.
10791   SmallString<16> PrettySourceValue;
10792   unsigned precision = llvm::APFloat::semanticsPrecision(Value.getSemantics());
10793   precision = (precision * 59 + 195) / 196;
10794   Value.toString(PrettySourceValue, precision);
10795 
10796   if (S.ObjC().isSignedCharBool(T) && IntegerValue != 0 && IntegerValue != 1) {
10797     return S.ObjC().adornBoolConversionDiagWithTernaryFixit(
10798         E, S.Diag(CContext, diag::warn_impcast_constant_value_to_objc_bool)
10799                << PrettySourceValue);
10800   }
10801 
10802   if (Result == llvm::APFloat::opOK && isExact) {
10803     if (IsLiteral) return;
10804     return DiagnoseImpCast(S, E, T, CContext, diag::warn_impcast_float_integer,
10805                            PruneWarnings);
10806   }
10807 
10808   // Conversion of a floating-point value to a non-bool integer where the
10809   // integral part cannot be represented by the integer type is undefined.
10810   if (!IsBool && Result == llvm::APFloat::opInvalidOp)
10811     return DiagnoseImpCast(
10812         S, E, T, CContext,
10813         IsLiteral ? diag::warn_impcast_literal_float_to_integer_out_of_range
10814                   : diag::warn_impcast_float_to_integer_out_of_range,
10815         PruneWarnings);
10816 
10817   unsigned DiagID = 0;
10818   if (IsLiteral) {
10819     // Warn on floating point literal to integer.
10820     DiagID = diag::warn_impcast_literal_float_to_integer;
10821   } else if (IntegerValue == 0) {
10822     if (Value.isZero()) {  // Skip -0.0 to 0 conversion.
10823       return DiagnoseImpCast(S, E, T, CContext,
10824                              diag::warn_impcast_float_integer, PruneWarnings);
10825     }
10826     // Warn on non-zero to zero conversion.
10827     DiagID = diag::warn_impcast_float_to_integer_zero;
10828   } else {
10829     if (IntegerValue.isUnsigned()) {
10830       if (!IntegerValue.isMaxValue()) {
10831         return DiagnoseImpCast(S, E, T, CContext,
10832                                diag::warn_impcast_float_integer, PruneWarnings);
10833       }
10834     } else {  // IntegerValue.isSigned()
10835       if (!IntegerValue.isMaxSignedValue() &&
10836           !IntegerValue.isMinSignedValue()) {
10837         return DiagnoseImpCast(S, E, T, CContext,
10838                                diag::warn_impcast_float_integer, PruneWarnings);
10839       }
10840     }
10841     // Warn on evaluatable floating point expression to integer conversion.
10842     DiagID = diag::warn_impcast_float_to_integer;
10843   }
10844 
10845   SmallString<16> PrettyTargetValue;
10846   if (IsBool)
10847     PrettyTargetValue = Value.isZero() ? "false" : "true";
10848   else
10849     IntegerValue.toString(PrettyTargetValue);
10850 
10851   if (PruneWarnings) {
10852     S.DiagRuntimeBehavior(E->getExprLoc(), E,
10853                           S.PDiag(DiagID)
10854                               << E->getType() << T.getUnqualifiedType()
10855                               << PrettySourceValue << PrettyTargetValue
10856                               << E->getSourceRange() << SourceRange(CContext));
10857   } else {
10858     S.Diag(E->getExprLoc(), DiagID)
10859         << E->getType() << T.getUnqualifiedType() << PrettySourceValue
10860         << PrettyTargetValue << E->getSourceRange() << SourceRange(CContext);
10861   }
10862 }
10863 
10864 /// Analyze the given compound assignment for the possible losing of
10865 /// floating-point precision.
10866 static void AnalyzeCompoundAssignment(Sema &S, BinaryOperator *E) {
10867   assert(isa<CompoundAssignOperator>(E) &&
10868          "Must be compound assignment operation");
10869   // Recurse on the LHS and RHS in here
10870   AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
10871   AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
10872 
10873   if (E->getLHS()->getType()->isAtomicType())
10874     S.Diag(E->getOperatorLoc(), diag::warn_atomic_implicit_seq_cst);
10875 
10876   // Now check the outermost expression
10877   const auto *ResultBT = E->getLHS()->getType()->getAs<BuiltinType>();
10878   const auto *RBT = cast<CompoundAssignOperator>(E)
10879                         ->getComputationResultType()
10880                         ->getAs<BuiltinType>();
10881 
10882   // The below checks assume source is floating point.
10883   if (!ResultBT || !RBT || !RBT->isFloatingPoint()) return;
10884 
10885   // If source is floating point but target is an integer.
10886   if (ResultBT->isInteger())
10887     return DiagnoseImpCast(S, E, E->getRHS()->getType(), E->getLHS()->getType(),
10888                            E->getExprLoc(), diag::warn_impcast_float_integer);
10889 
10890   if (!ResultBT->isFloatingPoint())
10891     return;
10892 
10893   // If both source and target are floating points, warn about losing precision.
10894   int Order = S.getASTContext().getFloatingTypeSemanticOrder(
10895       QualType(ResultBT, 0), QualType(RBT, 0));
10896   if (Order < 0 && !S.SourceMgr.isInSystemMacro(E->getOperatorLoc()))
10897     // warn about dropping FP rank.
10898     DiagnoseImpCast(S, E->getRHS(), E->getLHS()->getType(), E->getOperatorLoc(),
10899                     diag::warn_impcast_float_result_precision);
10900 }
10901 
10902 static std::string PrettyPrintInRange(const llvm::APSInt &Value,
10903                                       IntRange Range) {
10904   if (!Range.Width) return "0";
10905 
10906   llvm::APSInt ValueInRange = Value;
10907   ValueInRange.setIsSigned(!Range.NonNegative);
10908   ValueInRange = ValueInRange.trunc(Range.Width);
10909   return toString(ValueInRange, 10);
10910 }
10911 
10912 static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) {
10913   if (!isa<ImplicitCastExpr>(Ex))
10914     return false;
10915 
10916   Expr *InnerE = Ex->IgnoreParenImpCasts();
10917   const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr();
10918   const Type *Source =
10919     S.Context.getCanonicalType(InnerE->getType()).getTypePtr();
10920   if (Target->isDependentType())
10921     return false;
10922 
10923   const BuiltinType *FloatCandidateBT =
10924     dyn_cast<BuiltinType>(ToBool ? Source : Target);
10925   const Type *BoolCandidateType = ToBool ? Target : Source;
10926 
10927   return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) &&
10928           FloatCandidateBT && (FloatCandidateBT->isFloatingPoint()));
10929 }
10930 
10931 static void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall,
10932                                              SourceLocation CC) {
10933   unsigned NumArgs = TheCall->getNumArgs();
10934   for (unsigned i = 0; i < NumArgs; ++i) {
10935     Expr *CurrA = TheCall->getArg(i);
10936     if (!IsImplicitBoolFloatConversion(S, CurrA, true))
10937       continue;
10938 
10939     bool IsSwapped = ((i > 0) &&
10940         IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false));
10941     IsSwapped |= ((i < (NumArgs - 1)) &&
10942         IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false));
10943     if (IsSwapped) {
10944       // Warn on this floating-point to bool conversion.
10945       DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(),
10946                       CurrA->getType(), CC,
10947                       diag::warn_impcast_floating_point_to_bool);
10948     }
10949   }
10950 }
10951 
10952 static void DiagnoseNullConversion(Sema &S, Expr *E, QualType T,
10953                                    SourceLocation CC) {
10954   if (S.Diags.isIgnored(diag::warn_impcast_null_pointer_to_integer,
10955                         E->getExprLoc()))
10956     return;
10957 
10958   // Don't warn on functions which have return type nullptr_t.
10959   if (isa<CallExpr>(E))
10960     return;
10961 
10962   // Check for NULL (GNUNull) or nullptr (CXX11_nullptr).
10963   const Expr *NewE = E->IgnoreParenImpCasts();
10964   bool IsGNUNullExpr = isa<GNUNullExpr>(NewE);
10965   bool HasNullPtrType = NewE->getType()->isNullPtrType();
10966   if (!IsGNUNullExpr && !HasNullPtrType)
10967     return;
10968 
10969   // Return if target type is a safe conversion.
10970   if (T->isAnyPointerType() || T->isBlockPointerType() ||
10971       T->isMemberPointerType() || !T->isScalarType() || T->isNullPtrType())
10972     return;
10973 
10974   SourceLocation Loc = E->getSourceRange().getBegin();
10975 
10976   // Venture through the macro stacks to get to the source of macro arguments.
10977   // The new location is a better location than the complete location that was
10978   // passed in.
10979   Loc = S.SourceMgr.getTopMacroCallerLoc(Loc);
10980   CC = S.SourceMgr.getTopMacroCallerLoc(CC);
10981 
10982   // __null is usually wrapped in a macro.  Go up a macro if that is the case.
10983   if (IsGNUNullExpr && Loc.isMacroID()) {
10984     StringRef MacroName = Lexer::getImmediateMacroNameForDiagnostics(
10985         Loc, S.SourceMgr, S.getLangOpts());
10986     if (MacroName == "NULL")
10987       Loc = S.SourceMgr.getImmediateExpansionRange(Loc).getBegin();
10988   }
10989 
10990   // Only warn if the null and context location are in the same macro expansion.
10991   if (S.SourceMgr.getFileID(Loc) != S.SourceMgr.getFileID(CC))
10992     return;
10993 
10994   S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer)
10995       << HasNullPtrType << T << SourceRange(CC)
10996       << FixItHint::CreateReplacement(Loc,
10997                                       S.getFixItZeroLiteralForType(T, Loc));
10998 }
10999 
11000 // Helper function to filter out cases for constant width constant conversion.
11001 // Don't warn on char array initialization or for non-decimal values.
11002 static bool isSameWidthConstantConversion(Sema &S, Expr *E, QualType T,
11003                                           SourceLocation CC) {
11004   // If initializing from a constant, and the constant starts with '0',
11005   // then it is a binary, octal, or hexadecimal.  Allow these constants
11006   // to fill all the bits, even if there is a sign change.
11007   if (auto *IntLit = dyn_cast<IntegerLiteral>(E->IgnoreParenImpCasts())) {
11008     const char FirstLiteralCharacter =
11009         S.getSourceManager().getCharacterData(IntLit->getBeginLoc())[0];
11010     if (FirstLiteralCharacter == '0')
11011       return false;
11012   }
11013 
11014   // If the CC location points to a '{', and the type is char, then assume
11015   // assume it is an array initialization.
11016   if (CC.isValid() && T->isCharType()) {
11017     const char FirstContextCharacter =
11018         S.getSourceManager().getCharacterData(CC)[0];
11019     if (FirstContextCharacter == '{')
11020       return false;
11021   }
11022 
11023   return true;
11024 }
11025 
11026 static const IntegerLiteral *getIntegerLiteral(Expr *E) {
11027   const auto *IL = dyn_cast<IntegerLiteral>(E);
11028   if (!IL) {
11029     if (auto *UO = dyn_cast<UnaryOperator>(E)) {
11030       if (UO->getOpcode() == UO_Minus)
11031         return dyn_cast<IntegerLiteral>(UO->getSubExpr());
11032     }
11033   }
11034 
11035   return IL;
11036 }
11037 
11038 static void DiagnoseIntInBoolContext(Sema &S, Expr *E) {
11039   E = E->IgnoreParenImpCasts();
11040   SourceLocation ExprLoc = E->getExprLoc();
11041 
11042   if (const auto *BO = dyn_cast<BinaryOperator>(E)) {
11043     BinaryOperator::Opcode Opc = BO->getOpcode();
11044     Expr::EvalResult Result;
11045     // Do not diagnose unsigned shifts.
11046     if (Opc == BO_Shl) {
11047       const auto *LHS = getIntegerLiteral(BO->getLHS());
11048       const auto *RHS = getIntegerLiteral(BO->getRHS());
11049       if (LHS && LHS->getValue() == 0)
11050         S.Diag(ExprLoc, diag::warn_left_shift_always) << 0;
11051       else if (!E->isValueDependent() && LHS && RHS &&
11052                RHS->getValue().isNonNegative() &&
11053                E->EvaluateAsInt(Result, S.Context, Expr::SE_AllowSideEffects))
11054         S.Diag(ExprLoc, diag::warn_left_shift_always)
11055             << (Result.Val.getInt() != 0);
11056       else if (E->getType()->isSignedIntegerType())
11057         S.Diag(ExprLoc, diag::warn_left_shift_in_bool_context) << E;
11058     }
11059   }
11060 
11061   if (const auto *CO = dyn_cast<ConditionalOperator>(E)) {
11062     const auto *LHS = getIntegerLiteral(CO->getTrueExpr());
11063     const auto *RHS = getIntegerLiteral(CO->getFalseExpr());
11064     if (!LHS || !RHS)
11065       return;
11066     if ((LHS->getValue() == 0 || LHS->getValue() == 1) &&
11067         (RHS->getValue() == 0 || RHS->getValue() == 1))
11068       // Do not diagnose common idioms.
11069       return;
11070     if (LHS->getValue() != 0 && RHS->getValue() != 0)
11071       S.Diag(ExprLoc, diag::warn_integer_constants_in_conditional_always_true);
11072   }
11073 }
11074 
11075 void Sema::CheckImplicitConversion(Expr *E, QualType T, SourceLocation CC,
11076                                    bool *ICContext, bool IsListInit) {
11077   if (E->isTypeDependent() || E->isValueDependent()) return;
11078 
11079   const Type *Source = Context.getCanonicalType(E->getType()).getTypePtr();
11080   const Type *Target = Context.getCanonicalType(T).getTypePtr();
11081   if (Source == Target) return;
11082   if (Target->isDependentType()) return;
11083 
11084   // If the conversion context location is invalid don't complain. We also
11085   // don't want to emit a warning if the issue occurs from the expansion of
11086   // a system macro. The problem is that 'getSpellingLoc()' is slow, so we
11087   // delay this check as long as possible. Once we detect we are in that
11088   // scenario, we just return.
11089   if (CC.isInvalid())
11090     return;
11091 
11092   if (Source->isAtomicType())
11093     Diag(E->getExprLoc(), diag::warn_atomic_implicit_seq_cst);
11094 
11095   // Diagnose implicit casts to bool.
11096   if (Target->isSpecificBuiltinType(BuiltinType::Bool)) {
11097     if (isa<StringLiteral>(E))
11098       // Warn on string literal to bool.  Checks for string literals in logical
11099       // and expressions, for instance, assert(0 && "error here"), are
11100       // prevented by a check in AnalyzeImplicitConversions().
11101       return DiagnoseImpCast(*this, E, T, CC,
11102                              diag::warn_impcast_string_literal_to_bool);
11103     if (isa<ObjCStringLiteral>(E) || isa<ObjCArrayLiteral>(E) ||
11104         isa<ObjCDictionaryLiteral>(E) || isa<ObjCBoxedExpr>(E)) {
11105       // This covers the literal expressions that evaluate to Objective-C
11106       // objects.
11107       return DiagnoseImpCast(*this, E, T, CC,
11108                              diag::warn_impcast_objective_c_literal_to_bool);
11109     }
11110     if (Source->isPointerType() || Source->canDecayToPointerType()) {
11111       // Warn on pointer to bool conversion that is always true.
11112       DiagnoseAlwaysNonNullPointer(E, Expr::NPCK_NotNull, /*IsEqual*/ false,
11113                                    SourceRange(CC));
11114     }
11115   }
11116 
11117   // If the we're converting a constant to an ObjC BOOL on a platform where BOOL
11118   // is a typedef for signed char (macOS), then that constant value has to be 1
11119   // or 0.
11120   if (ObjC().isSignedCharBool(T) && Source->isIntegralType(Context)) {
11121     Expr::EvalResult Result;
11122     if (E->EvaluateAsInt(Result, getASTContext(), Expr::SE_AllowSideEffects)) {
11123       if (Result.Val.getInt() != 1 && Result.Val.getInt() != 0) {
11124         ObjC().adornBoolConversionDiagWithTernaryFixit(
11125             E, Diag(CC, diag::warn_impcast_constant_value_to_objc_bool)
11126                    << toString(Result.Val.getInt(), 10));
11127       }
11128       return;
11129     }
11130   }
11131 
11132   // Check implicit casts from Objective-C collection literals to specialized
11133   // collection types, e.g., NSArray<NSString *> *.
11134   if (auto *ArrayLiteral = dyn_cast<ObjCArrayLiteral>(E))
11135     ObjC().checkArrayLiteral(QualType(Target, 0), ArrayLiteral);
11136   else if (auto *DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(E))
11137     ObjC().checkDictionaryLiteral(QualType(Target, 0), DictionaryLiteral);
11138 
11139   // Strip vector types.
11140   if (isa<VectorType>(Source)) {
11141     if (Target->isSveVLSBuiltinType() &&
11142         (Context.areCompatibleSveTypes(QualType(Target, 0),
11143                                        QualType(Source, 0)) ||
11144          Context.areLaxCompatibleSveTypes(QualType(Target, 0),
11145                                           QualType(Source, 0))))
11146       return;
11147 
11148     if (Target->isRVVVLSBuiltinType() &&
11149         (Context.areCompatibleRVVTypes(QualType(Target, 0),
11150                                        QualType(Source, 0)) ||
11151          Context.areLaxCompatibleRVVTypes(QualType(Target, 0),
11152                                           QualType(Source, 0))))
11153       return;
11154 
11155     if (!isa<VectorType>(Target)) {
11156       if (SourceMgr.isInSystemMacro(CC))
11157         return;
11158       return DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_vector_scalar);
11159     } else if (getLangOpts().HLSL &&
11160                Target->castAs<VectorType>()->getNumElements() <
11161                    Source->castAs<VectorType>()->getNumElements()) {
11162       // Diagnose vector truncation but don't return. We may also want to
11163       // diagnose an element conversion.
11164       DiagnoseImpCast(*this, E, T, CC,
11165                       diag::warn_hlsl_impcast_vector_truncation);
11166     }
11167 
11168     // If the vector cast is cast between two vectors of the same size, it is
11169     // a bitcast, not a conversion, except under HLSL where it is a conversion.
11170     if (!getLangOpts().HLSL &&
11171         Context.getTypeSize(Source) == Context.getTypeSize(Target))
11172       return;
11173 
11174     Source = cast<VectorType>(Source)->getElementType().getTypePtr();
11175     Target = cast<VectorType>(Target)->getElementType().getTypePtr();
11176   }
11177   if (auto VecTy = dyn_cast<VectorType>(Target))
11178     Target = VecTy->getElementType().getTypePtr();
11179 
11180   // Strip complex types.
11181   if (isa<ComplexType>(Source)) {
11182     if (!isa<ComplexType>(Target)) {
11183       if (SourceMgr.isInSystemMacro(CC) || Target->isBooleanType())
11184         return;
11185 
11186       return DiagnoseImpCast(*this, E, T, CC,
11187                              getLangOpts().CPlusPlus
11188                                  ? diag::err_impcast_complex_scalar
11189                                  : diag::warn_impcast_complex_scalar);
11190     }
11191 
11192     Source = cast<ComplexType>(Source)->getElementType().getTypePtr();
11193     Target = cast<ComplexType>(Target)->getElementType().getTypePtr();
11194   }
11195 
11196   const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source);
11197   const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target);
11198 
11199   // Strip SVE vector types
11200   if (SourceBT && SourceBT->isSveVLSBuiltinType()) {
11201     // Need the original target type for vector type checks
11202     const Type *OriginalTarget = Context.getCanonicalType(T).getTypePtr();
11203     // Handle conversion from scalable to fixed when msve-vector-bits is
11204     // specified
11205     if (Context.areCompatibleSveTypes(QualType(OriginalTarget, 0),
11206                                       QualType(Source, 0)) ||
11207         Context.areLaxCompatibleSveTypes(QualType(OriginalTarget, 0),
11208                                          QualType(Source, 0)))
11209       return;
11210 
11211     // If the vector cast is cast between two vectors of the same size, it is
11212     // a bitcast, not a conversion.
11213     if (Context.getTypeSize(Source) == Context.getTypeSize(Target))
11214       return;
11215 
11216     Source = SourceBT->getSveEltType(Context).getTypePtr();
11217   }
11218 
11219   if (TargetBT && TargetBT->isSveVLSBuiltinType())
11220     Target = TargetBT->getSveEltType(Context).getTypePtr();
11221 
11222   // If the source is floating point...
11223   if (SourceBT && SourceBT->isFloatingPoint()) {
11224     // ...and the target is floating point...
11225     if (TargetBT && TargetBT->isFloatingPoint()) {
11226       // ...then warn if we're dropping FP rank.
11227 
11228       int Order = getASTContext().getFloatingTypeSemanticOrder(
11229           QualType(SourceBT, 0), QualType(TargetBT, 0));
11230       if (Order > 0) {
11231         // Don't warn about float constants that are precisely
11232         // representable in the target type.
11233         Expr::EvalResult result;
11234         if (E->EvaluateAsRValue(result, Context)) {
11235           // Value might be a float, a float vector, or a float complex.
11236           if (IsSameFloatAfterCast(
11237                   result.Val,
11238                   Context.getFloatTypeSemantics(QualType(TargetBT, 0)),
11239                   Context.getFloatTypeSemantics(QualType(SourceBT, 0))))
11240             return;
11241         }
11242 
11243         if (SourceMgr.isInSystemMacro(CC))
11244           return;
11245 
11246         DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_float_precision);
11247       }
11248       // ... or possibly if we're increasing rank, too
11249       else if (Order < 0) {
11250         if (SourceMgr.isInSystemMacro(CC))
11251           return;
11252 
11253         DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_double_promotion);
11254       }
11255       return;
11256     }
11257 
11258     // If the target is integral, always warn.
11259     if (TargetBT && TargetBT->isInteger()) {
11260       if (SourceMgr.isInSystemMacro(CC))
11261         return;
11262 
11263       DiagnoseFloatingImpCast(*this, E, T, CC);
11264     }
11265 
11266     // Detect the case where a call result is converted from floating-point to
11267     // to bool, and the final argument to the call is converted from bool, to
11268     // discover this typo:
11269     //
11270     //    bool b = fabs(x < 1.0);  // should be "bool b = fabs(x) < 1.0;"
11271     //
11272     // FIXME: This is an incredibly special case; is there some more general
11273     // way to detect this class of misplaced-parentheses bug?
11274     if (Target->isBooleanType() && isa<CallExpr>(E)) {
11275       // Check last argument of function call to see if it is an
11276       // implicit cast from a type matching the type the result
11277       // is being cast to.
11278       CallExpr *CEx = cast<CallExpr>(E);
11279       if (unsigned NumArgs = CEx->getNumArgs()) {
11280         Expr *LastA = CEx->getArg(NumArgs - 1);
11281         Expr *InnerE = LastA->IgnoreParenImpCasts();
11282         if (isa<ImplicitCastExpr>(LastA) &&
11283             InnerE->getType()->isBooleanType()) {
11284           // Warn on this floating-point to bool conversion
11285           DiagnoseImpCast(*this, E, T, CC,
11286                           diag::warn_impcast_floating_point_to_bool);
11287         }
11288       }
11289     }
11290     return;
11291   }
11292 
11293   // Valid casts involving fixed point types should be accounted for here.
11294   if (Source->isFixedPointType()) {
11295     if (Target->isUnsaturatedFixedPointType()) {
11296       Expr::EvalResult Result;
11297       if (E->EvaluateAsFixedPoint(Result, Context, Expr::SE_AllowSideEffects,
11298                                   isConstantEvaluatedContext())) {
11299         llvm::APFixedPoint Value = Result.Val.getFixedPoint();
11300         llvm::APFixedPoint MaxVal = Context.getFixedPointMax(T);
11301         llvm::APFixedPoint MinVal = Context.getFixedPointMin(T);
11302         if (Value > MaxVal || Value < MinVal) {
11303           DiagRuntimeBehavior(E->getExprLoc(), E,
11304                               PDiag(diag::warn_impcast_fixed_point_range)
11305                                   << Value.toString() << T
11306                                   << E->getSourceRange()
11307                                   << clang::SourceRange(CC));
11308           return;
11309         }
11310       }
11311     } else if (Target->isIntegerType()) {
11312       Expr::EvalResult Result;
11313       if (!isConstantEvaluatedContext() &&
11314           E->EvaluateAsFixedPoint(Result, Context, Expr::SE_AllowSideEffects)) {
11315         llvm::APFixedPoint FXResult = Result.Val.getFixedPoint();
11316 
11317         bool Overflowed;
11318         llvm::APSInt IntResult = FXResult.convertToInt(
11319             Context.getIntWidth(T), Target->isSignedIntegerOrEnumerationType(),
11320             &Overflowed);
11321 
11322         if (Overflowed) {
11323           DiagRuntimeBehavior(E->getExprLoc(), E,
11324                               PDiag(diag::warn_impcast_fixed_point_range)
11325                                   << FXResult.toString() << T
11326                                   << E->getSourceRange()
11327                                   << clang::SourceRange(CC));
11328           return;
11329         }
11330       }
11331     }
11332   } else if (Target->isUnsaturatedFixedPointType()) {
11333     if (Source->isIntegerType()) {
11334       Expr::EvalResult Result;
11335       if (!isConstantEvaluatedContext() &&
11336           E->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects)) {
11337         llvm::APSInt Value = Result.Val.getInt();
11338 
11339         bool Overflowed;
11340         llvm::APFixedPoint IntResult = llvm::APFixedPoint::getFromIntValue(
11341             Value, Context.getFixedPointSemantics(T), &Overflowed);
11342 
11343         if (Overflowed) {
11344           DiagRuntimeBehavior(E->getExprLoc(), E,
11345                               PDiag(diag::warn_impcast_fixed_point_range)
11346                                   << toString(Value, /*Radix=*/10) << T
11347                                   << E->getSourceRange()
11348                                   << clang::SourceRange(CC));
11349           return;
11350         }
11351       }
11352     }
11353   }
11354 
11355   // If we are casting an integer type to a floating point type without
11356   // initialization-list syntax, we might lose accuracy if the floating
11357   // point type has a narrower significand than the integer type.
11358   if (SourceBT && TargetBT && SourceBT->isIntegerType() &&
11359       TargetBT->isFloatingType() && !IsListInit) {
11360     // Determine the number of precision bits in the source integer type.
11361     std::optional<IntRange> SourceRange =
11362         TryGetExprRange(Context, E, isConstantEvaluatedContext(),
11363                         /*Approximate=*/true);
11364     if (!SourceRange)
11365       return;
11366     unsigned int SourcePrecision = SourceRange->Width;
11367 
11368     // Determine the number of precision bits in the
11369     // target floating point type.
11370     unsigned int TargetPrecision = llvm::APFloatBase::semanticsPrecision(
11371         Context.getFloatTypeSemantics(QualType(TargetBT, 0)));
11372 
11373     if (SourcePrecision > 0 && TargetPrecision > 0 &&
11374         SourcePrecision > TargetPrecision) {
11375 
11376       if (std::optional<llvm::APSInt> SourceInt =
11377               E->getIntegerConstantExpr(Context)) {
11378         // If the source integer is a constant, convert it to the target
11379         // floating point type. Issue a warning if the value changes
11380         // during the whole conversion.
11381         llvm::APFloat TargetFloatValue(
11382             Context.getFloatTypeSemantics(QualType(TargetBT, 0)));
11383         llvm::APFloat::opStatus ConversionStatus =
11384             TargetFloatValue.convertFromAPInt(
11385                 *SourceInt, SourceBT->isSignedInteger(),
11386                 llvm::APFloat::rmNearestTiesToEven);
11387 
11388         if (ConversionStatus != llvm::APFloat::opOK) {
11389           SmallString<32> PrettySourceValue;
11390           SourceInt->toString(PrettySourceValue, 10);
11391           SmallString<32> PrettyTargetValue;
11392           TargetFloatValue.toString(PrettyTargetValue, TargetPrecision);
11393 
11394           DiagRuntimeBehavior(
11395               E->getExprLoc(), E,
11396               PDiag(diag::warn_impcast_integer_float_precision_constant)
11397                   << PrettySourceValue << PrettyTargetValue << E->getType() << T
11398                   << E->getSourceRange() << clang::SourceRange(CC));
11399         }
11400       } else {
11401         // Otherwise, the implicit conversion may lose precision.
11402         DiagnoseImpCast(*this, E, T, CC,
11403                         diag::warn_impcast_integer_float_precision);
11404       }
11405     }
11406   }
11407 
11408   DiagnoseNullConversion(*this, E, T, CC);
11409 
11410   DiscardMisalignedMemberAddress(Target, E);
11411 
11412   if (Target->isBooleanType())
11413     DiagnoseIntInBoolContext(*this, E);
11414 
11415   if (!Source->isIntegerType() || !Target->isIntegerType())
11416     return;
11417 
11418   // TODO: remove this early return once the false positives for constant->bool
11419   // in templates, macros, etc, are reduced or removed.
11420   if (Target->isSpecificBuiltinType(BuiltinType::Bool))
11421     return;
11422 
11423   if (ObjC().isSignedCharBool(T) && !Source->isCharType() &&
11424       !E->isKnownToHaveBooleanValue(/*Semantic=*/false)) {
11425     return ObjC().adornBoolConversionDiagWithTernaryFixit(
11426         E, Diag(CC, diag::warn_impcast_int_to_objc_signed_char_bool)
11427                << E->getType());
11428   }
11429   std::optional<IntRange> LikelySourceRange = TryGetExprRange(
11430       Context, E, isConstantEvaluatedContext(), /*Approximate=*/true);
11431   if (!LikelySourceRange)
11432     return;
11433 
11434   IntRange SourceTypeRange =
11435       IntRange::forTargetOfCanonicalType(Context, Source);
11436   IntRange TargetRange = IntRange::forTargetOfCanonicalType(Context, Target);
11437 
11438   if (LikelySourceRange->Width > TargetRange.Width) {
11439     // If the source is a constant, use a default-on diagnostic.
11440     // TODO: this should happen for bitfield stores, too.
11441     Expr::EvalResult Result;
11442     if (E->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects,
11443                          isConstantEvaluatedContext())) {
11444       llvm::APSInt Value(32);
11445       Value = Result.Val.getInt();
11446 
11447       if (SourceMgr.isInSystemMacro(CC))
11448         return;
11449 
11450       std::string PrettySourceValue = toString(Value, 10);
11451       std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange);
11452 
11453       DiagRuntimeBehavior(E->getExprLoc(), E,
11454                           PDiag(diag::warn_impcast_integer_precision_constant)
11455                               << PrettySourceValue << PrettyTargetValue
11456                               << E->getType() << T << E->getSourceRange()
11457                               << SourceRange(CC));
11458       return;
11459     }
11460 
11461     // People want to build with -Wshorten-64-to-32 and not -Wconversion.
11462     if (SourceMgr.isInSystemMacro(CC))
11463       return;
11464 
11465     if (TargetRange.Width == 32 && Context.getIntWidth(E->getType()) == 64)
11466       return DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_integer_64_32,
11467                              /* pruneControlFlow */ true);
11468     return DiagnoseImpCast(*this, E, T, CC,
11469                            diag::warn_impcast_integer_precision);
11470   }
11471 
11472   if (TargetRange.Width > SourceTypeRange.Width) {
11473     if (auto *UO = dyn_cast<UnaryOperator>(E))
11474       if (UO->getOpcode() == UO_Minus)
11475         if (Source->isUnsignedIntegerType()) {
11476           if (Target->isUnsignedIntegerType())
11477             return DiagnoseImpCast(*this, E, T, CC,
11478                                    diag::warn_impcast_high_order_zero_bits);
11479           if (Target->isSignedIntegerType())
11480             return DiagnoseImpCast(*this, E, T, CC,
11481                                    diag::warn_impcast_nonnegative_result);
11482         }
11483   }
11484 
11485   if (TargetRange.Width == LikelySourceRange->Width &&
11486       !TargetRange.NonNegative && LikelySourceRange->NonNegative &&
11487       Source->isSignedIntegerType()) {
11488     // Warn when doing a signed to signed conversion, warn if the positive
11489     // source value is exactly the width of the target type, which will
11490     // cause a negative value to be stored.
11491 
11492     Expr::EvalResult Result;
11493     if (E->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects) &&
11494         !SourceMgr.isInSystemMacro(CC)) {
11495       llvm::APSInt Value = Result.Val.getInt();
11496       if (isSameWidthConstantConversion(*this, E, T, CC)) {
11497         std::string PrettySourceValue = toString(Value, 10);
11498         std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange);
11499 
11500         Diag(E->getExprLoc(),
11501              PDiag(diag::warn_impcast_integer_precision_constant)
11502                  << PrettySourceValue << PrettyTargetValue << E->getType() << T
11503                  << E->getSourceRange() << SourceRange(CC));
11504         return;
11505       }
11506     }
11507 
11508     // Fall through for non-constants to give a sign conversion warning.
11509   }
11510 
11511   if ((!isa<EnumType>(Target) || !isa<EnumType>(Source)) &&
11512       ((TargetRange.NonNegative && !LikelySourceRange->NonNegative) ||
11513        (!TargetRange.NonNegative && LikelySourceRange->NonNegative &&
11514         LikelySourceRange->Width == TargetRange.Width))) {
11515     if (SourceMgr.isInSystemMacro(CC))
11516       return;
11517 
11518     if (SourceBT && SourceBT->isInteger() && TargetBT &&
11519         TargetBT->isInteger() &&
11520         Source->isSignedIntegerType() == Target->isSignedIntegerType()) {
11521       return;
11522     }
11523 
11524     unsigned DiagID = diag::warn_impcast_integer_sign;
11525 
11526     // Traditionally, gcc has warned about this under -Wsign-compare.
11527     // We also want to warn about it in -Wconversion.
11528     // So if -Wconversion is off, use a completely identical diagnostic
11529     // in the sign-compare group.
11530     // The conditional-checking code will
11531     if (ICContext) {
11532       DiagID = diag::warn_impcast_integer_sign_conditional;
11533       *ICContext = true;
11534     }
11535 
11536     return DiagnoseImpCast(*this, E, T, CC, DiagID);
11537   }
11538 
11539   // Diagnose conversions between different enumeration types.
11540   // In C, we pretend that the type of an EnumConstantDecl is its enumeration
11541   // type, to give us better diagnostics.
11542   QualType SourceType = E->getEnumCoercedType(Context);
11543   Source = Context.getCanonicalType(SourceType).getTypePtr();
11544 
11545   if (const EnumType *SourceEnum = Source->getAs<EnumType>())
11546     if (const EnumType *TargetEnum = Target->getAs<EnumType>())
11547       if (SourceEnum->getDecl()->hasNameForLinkage() &&
11548           TargetEnum->getDecl()->hasNameForLinkage() &&
11549           SourceEnum != TargetEnum) {
11550         if (SourceMgr.isInSystemMacro(CC))
11551           return;
11552 
11553         return DiagnoseImpCast(*this, E, SourceType, T, CC,
11554                                diag::warn_impcast_different_enum_types);
11555       }
11556 }
11557 
11558 static void CheckConditionalOperator(Sema &S, AbstractConditionalOperator *E,
11559                                      SourceLocation CC, QualType T);
11560 
11561 static void CheckConditionalOperand(Sema &S, Expr *E, QualType T,
11562                                     SourceLocation CC, bool &ICContext) {
11563   E = E->IgnoreParenImpCasts();
11564   // Diagnose incomplete type for second or third operand in C.
11565   if (!S.getLangOpts().CPlusPlus && E->getType()->isRecordType())
11566     S.RequireCompleteExprType(E, diag::err_incomplete_type);
11567 
11568   if (auto *CO = dyn_cast<AbstractConditionalOperator>(E))
11569     return CheckConditionalOperator(S, CO, CC, T);
11570 
11571   AnalyzeImplicitConversions(S, E, CC);
11572   if (E->getType() != T)
11573     return S.CheckImplicitConversion(E, T, CC, &ICContext);
11574 }
11575 
11576 static void CheckConditionalOperator(Sema &S, AbstractConditionalOperator *E,
11577                                      SourceLocation CC, QualType T) {
11578   AnalyzeImplicitConversions(S, E->getCond(), E->getQuestionLoc());
11579 
11580   Expr *TrueExpr = E->getTrueExpr();
11581   if (auto *BCO = dyn_cast<BinaryConditionalOperator>(E))
11582     TrueExpr = BCO->getCommon();
11583 
11584   bool Suspicious = false;
11585   CheckConditionalOperand(S, TrueExpr, T, CC, Suspicious);
11586   CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious);
11587 
11588   if (T->isBooleanType())
11589     DiagnoseIntInBoolContext(S, E);
11590 
11591   // If -Wconversion would have warned about either of the candidates
11592   // for a signedness conversion to the context type...
11593   if (!Suspicious) return;
11594 
11595   // ...but it's currently ignored...
11596   if (!S.Diags.isIgnored(diag::warn_impcast_integer_sign_conditional, CC))
11597     return;
11598 
11599   // ...then check whether it would have warned about either of the
11600   // candidates for a signedness conversion to the condition type.
11601   if (E->getType() == T) return;
11602 
11603   Suspicious = false;
11604   S.CheckImplicitConversion(TrueExpr->IgnoreParenImpCasts(), E->getType(), CC,
11605                             &Suspicious);
11606   if (!Suspicious)
11607     S.CheckImplicitConversion(E->getFalseExpr()->IgnoreParenImpCasts(),
11608                               E->getType(), CC, &Suspicious);
11609 }
11610 
11611 /// Check conversion of given expression to boolean.
11612 /// Input argument E is a logical expression.
11613 static void CheckBoolLikeConversion(Sema &S, Expr *E, SourceLocation CC) {
11614   // Run the bool-like conversion checks only for C since there bools are
11615   // still not used as the return type from "boolean" operators or as the input
11616   // type for conditional operators.
11617   if (S.getLangOpts().CPlusPlus)
11618     return;
11619   if (E->IgnoreParenImpCasts()->getType()->isAtomicType())
11620     return;
11621   S.CheckImplicitConversion(E->IgnoreParenImpCasts(), S.Context.BoolTy, CC);
11622 }
11623 
11624 namespace {
11625 struct AnalyzeImplicitConversionsWorkItem {
11626   Expr *E;
11627   SourceLocation CC;
11628   bool IsListInit;
11629 };
11630 }
11631 
11632 /// Data recursive variant of AnalyzeImplicitConversions. Subexpressions
11633 /// that should be visited are added to WorkList.
11634 static void AnalyzeImplicitConversions(
11635     Sema &S, AnalyzeImplicitConversionsWorkItem Item,
11636     llvm::SmallVectorImpl<AnalyzeImplicitConversionsWorkItem> &WorkList) {
11637   Expr *OrigE = Item.E;
11638   SourceLocation CC = Item.CC;
11639 
11640   QualType T = OrigE->getType();
11641   Expr *E = OrigE->IgnoreParenImpCasts();
11642 
11643   // Propagate whether we are in a C++ list initialization expression.
11644   // If so, we do not issue warnings for implicit int-float conversion
11645   // precision loss, because C++11 narrowing already handles it.
11646   bool IsListInit = Item.IsListInit ||
11647                     (isa<InitListExpr>(OrigE) && S.getLangOpts().CPlusPlus);
11648 
11649   if (E->isTypeDependent() || E->isValueDependent())
11650     return;
11651 
11652   Expr *SourceExpr = E;
11653   // Examine, but don't traverse into the source expression of an
11654   // OpaqueValueExpr, since it may have multiple parents and we don't want to
11655   // emit duplicate diagnostics. Its fine to examine the form or attempt to
11656   // evaluate it in the context of checking the specific conversion to T though.
11657   if (auto *OVE = dyn_cast<OpaqueValueExpr>(E))
11658     if (auto *Src = OVE->getSourceExpr())
11659       SourceExpr = Src;
11660 
11661   if (const auto *UO = dyn_cast<UnaryOperator>(SourceExpr))
11662     if (UO->getOpcode() == UO_Not &&
11663         UO->getSubExpr()->isKnownToHaveBooleanValue())
11664       S.Diag(UO->getBeginLoc(), diag::warn_bitwise_negation_bool)
11665           << OrigE->getSourceRange() << T->isBooleanType()
11666           << FixItHint::CreateReplacement(UO->getBeginLoc(), "!");
11667 
11668   if (const auto *BO = dyn_cast<BinaryOperator>(SourceExpr))
11669     if ((BO->getOpcode() == BO_And || BO->getOpcode() == BO_Or) &&
11670         BO->getLHS()->isKnownToHaveBooleanValue() &&
11671         BO->getRHS()->isKnownToHaveBooleanValue() &&
11672         BO->getLHS()->HasSideEffects(S.Context) &&
11673         BO->getRHS()->HasSideEffects(S.Context)) {
11674       SourceManager &SM = S.getSourceManager();
11675       const LangOptions &LO = S.getLangOpts();
11676       SourceLocation BLoc = BO->getOperatorLoc();
11677       SourceLocation ELoc = Lexer::getLocForEndOfToken(BLoc, 0, SM, LO);
11678       StringRef SR = clang::Lexer::getSourceText(
11679           clang::CharSourceRange::getTokenRange(BLoc, ELoc), SM, LO);
11680       // To reduce false positives, only issue the diagnostic if the operator
11681       // is explicitly spelled as a punctuator. This suppresses the diagnostic
11682       // when using 'bitand' or 'bitor' either as keywords in C++ or as macros
11683       // in C, along with other macro spellings the user might invent.
11684       if (SR.str() == "&" || SR.str() == "|") {
11685 
11686         S.Diag(BO->getBeginLoc(), diag::warn_bitwise_instead_of_logical)
11687             << (BO->getOpcode() == BO_And ? "&" : "|")
11688             << OrigE->getSourceRange()
11689             << FixItHint::CreateReplacement(
11690                    BO->getOperatorLoc(),
11691                    (BO->getOpcode() == BO_And ? "&&" : "||"));
11692         S.Diag(BO->getBeginLoc(), diag::note_cast_operand_to_int);
11693       }
11694     }
11695 
11696   // For conditional operators, we analyze the arguments as if they
11697   // were being fed directly into the output.
11698   if (auto *CO = dyn_cast<AbstractConditionalOperator>(SourceExpr)) {
11699     CheckConditionalOperator(S, CO, CC, T);
11700     return;
11701   }
11702 
11703   // Check implicit argument conversions for function calls.
11704   if (CallExpr *Call = dyn_cast<CallExpr>(SourceExpr))
11705     CheckImplicitArgumentConversions(S, Call, CC);
11706 
11707   // Go ahead and check any implicit conversions we might have skipped.
11708   // The non-canonical typecheck is just an optimization;
11709   // CheckImplicitConversion will filter out dead implicit conversions.
11710   if (SourceExpr->getType() != T)
11711     S.CheckImplicitConversion(SourceExpr, T, CC, nullptr, IsListInit);
11712 
11713   // Now continue drilling into this expression.
11714 
11715   if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
11716     // The bound subexpressions in a PseudoObjectExpr are not reachable
11717     // as transitive children.
11718     // FIXME: Use a more uniform representation for this.
11719     for (auto *SE : POE->semantics())
11720       if (auto *OVE = dyn_cast<OpaqueValueExpr>(SE))
11721         WorkList.push_back({OVE->getSourceExpr(), CC, IsListInit});
11722   }
11723 
11724   // Skip past explicit casts.
11725   if (auto *CE = dyn_cast<ExplicitCastExpr>(E)) {
11726     E = CE->getSubExpr()->IgnoreParenImpCasts();
11727     if (!CE->getType()->isVoidType() && E->getType()->isAtomicType())
11728       S.Diag(E->getBeginLoc(), diag::warn_atomic_implicit_seq_cst);
11729     WorkList.push_back({E, CC, IsListInit});
11730     return;
11731   }
11732 
11733   if (auto *OutArgE = dyn_cast<HLSLOutArgExpr>(E)) {
11734     WorkList.push_back({OutArgE->getArgLValue(), CC, IsListInit});
11735     // The base expression is only used to initialize the parameter for
11736     // arguments to `inout` parameters, so we only traverse down the base
11737     // expression for `inout` cases.
11738     if (OutArgE->isInOut())
11739       WorkList.push_back(
11740           {OutArgE->getCastedTemporary()->getSourceExpr(), CC, IsListInit});
11741     WorkList.push_back({OutArgE->getWritebackCast(), CC, IsListInit});
11742     return;
11743   }
11744 
11745   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
11746     // Do a somewhat different check with comparison operators.
11747     if (BO->isComparisonOp())
11748       return AnalyzeComparison(S, BO);
11749 
11750     // And with simple assignments.
11751     if (BO->getOpcode() == BO_Assign)
11752       return AnalyzeAssignment(S, BO);
11753     // And with compound assignments.
11754     if (BO->isAssignmentOp())
11755       return AnalyzeCompoundAssignment(S, BO);
11756   }
11757 
11758   // These break the otherwise-useful invariant below.  Fortunately,
11759   // we don't really need to recurse into them, because any internal
11760   // expressions should have been analyzed already when they were
11761   // built into statements.
11762   if (isa<StmtExpr>(E)) return;
11763 
11764   // Don't descend into unevaluated contexts.
11765   if (isa<UnaryExprOrTypeTraitExpr>(E)) return;
11766 
11767   // Now just recurse over the expression's children.
11768   CC = E->getExprLoc();
11769   BinaryOperator *BO = dyn_cast<BinaryOperator>(E);
11770   bool IsLogicalAndOperator = BO && BO->getOpcode() == BO_LAnd;
11771   for (Stmt *SubStmt : E->children()) {
11772     Expr *ChildExpr = dyn_cast_or_null<Expr>(SubStmt);
11773     if (!ChildExpr)
11774       continue;
11775 
11776     if (auto *CSE = dyn_cast<CoroutineSuspendExpr>(E))
11777       if (ChildExpr == CSE->getOperand())
11778         // Do not recurse over a CoroutineSuspendExpr's operand.
11779         // The operand is also a subexpression of getCommonExpr(), and
11780         // recursing into it directly would produce duplicate diagnostics.
11781         continue;
11782 
11783     if (IsLogicalAndOperator &&
11784         isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts()))
11785       // Ignore checking string literals that are in logical and operators.
11786       // This is a common pattern for asserts.
11787       continue;
11788     WorkList.push_back({ChildExpr, CC, IsListInit});
11789   }
11790 
11791   if (BO && BO->isLogicalOp()) {
11792     Expr *SubExpr = BO->getLHS()->IgnoreParenImpCasts();
11793     if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr))
11794       ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc());
11795 
11796     SubExpr = BO->getRHS()->IgnoreParenImpCasts();
11797     if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr))
11798       ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc());
11799   }
11800 
11801   if (const UnaryOperator *U = dyn_cast<UnaryOperator>(E)) {
11802     if (U->getOpcode() == UO_LNot) {
11803       ::CheckBoolLikeConversion(S, U->getSubExpr(), CC);
11804     } else if (U->getOpcode() != UO_AddrOf) {
11805       if (U->getSubExpr()->getType()->isAtomicType())
11806         S.Diag(U->getSubExpr()->getBeginLoc(),
11807                diag::warn_atomic_implicit_seq_cst);
11808     }
11809   }
11810 }
11811 
11812 /// AnalyzeImplicitConversions - Find and report any interesting
11813 /// implicit conversions in the given expression.  There are a couple
11814 /// of competing diagnostics here, -Wconversion and -Wsign-compare.
11815 static void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC,
11816                                        bool IsListInit/*= false*/) {
11817   llvm::SmallVector<AnalyzeImplicitConversionsWorkItem, 16> WorkList;
11818   WorkList.push_back({OrigE, CC, IsListInit});
11819   while (!WorkList.empty())
11820     AnalyzeImplicitConversions(S, WorkList.pop_back_val(), WorkList);
11821 }
11822 
11823 // Helper function for Sema::DiagnoseAlwaysNonNullPointer.
11824 // Returns true when emitting a warning about taking the address of a reference.
11825 static bool CheckForReference(Sema &SemaRef, const Expr *E,
11826                               const PartialDiagnostic &PD) {
11827   E = E->IgnoreParenImpCasts();
11828 
11829   const FunctionDecl *FD = nullptr;
11830 
11831   if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
11832     if (!DRE->getDecl()->getType()->isReferenceType())
11833       return false;
11834   } else if (const MemberExpr *M = dyn_cast<MemberExpr>(E)) {
11835     if (!M->getMemberDecl()->getType()->isReferenceType())
11836       return false;
11837   } else if (const CallExpr *Call = dyn_cast<CallExpr>(E)) {
11838     if (!Call->getCallReturnType(SemaRef.Context)->isReferenceType())
11839       return false;
11840     FD = Call->getDirectCallee();
11841   } else {
11842     return false;
11843   }
11844 
11845   SemaRef.Diag(E->getExprLoc(), PD);
11846 
11847   // If possible, point to location of function.
11848   if (FD) {
11849     SemaRef.Diag(FD->getLocation(), diag::note_reference_is_return_value) << FD;
11850   }
11851 
11852   return true;
11853 }
11854 
11855 // Returns true if the SourceLocation is expanded from any macro body.
11856 // Returns false if the SourceLocation is invalid, is from not in a macro
11857 // expansion, or is from expanded from a top-level macro argument.
11858 static bool IsInAnyMacroBody(const SourceManager &SM, SourceLocation Loc) {
11859   if (Loc.isInvalid())
11860     return false;
11861 
11862   while (Loc.isMacroID()) {
11863     if (SM.isMacroBodyExpansion(Loc))
11864       return true;
11865     Loc = SM.getImmediateMacroCallerLoc(Loc);
11866   }
11867 
11868   return false;
11869 }
11870 
11871 void Sema::DiagnoseAlwaysNonNullPointer(Expr *E,
11872                                         Expr::NullPointerConstantKind NullKind,
11873                                         bool IsEqual, SourceRange Range) {
11874   if (!E)
11875     return;
11876 
11877   // Don't warn inside macros.
11878   if (E->getExprLoc().isMacroID()) {
11879     const SourceManager &SM = getSourceManager();
11880     if (IsInAnyMacroBody(SM, E->getExprLoc()) ||
11881         IsInAnyMacroBody(SM, Range.getBegin()))
11882       return;
11883   }
11884   E = E->IgnoreImpCasts();
11885 
11886   const bool IsCompare = NullKind != Expr::NPCK_NotNull;
11887 
11888   if (isa<CXXThisExpr>(E)) {
11889     unsigned DiagID = IsCompare ? diag::warn_this_null_compare
11890                                 : diag::warn_this_bool_conversion;
11891     Diag(E->getExprLoc(), DiagID) << E->getSourceRange() << Range << IsEqual;
11892     return;
11893   }
11894 
11895   bool IsAddressOf = false;
11896 
11897   if (auto *UO = dyn_cast<UnaryOperator>(E->IgnoreParens())) {
11898     if (UO->getOpcode() != UO_AddrOf)
11899       return;
11900     IsAddressOf = true;
11901     E = UO->getSubExpr();
11902   }
11903 
11904   if (IsAddressOf) {
11905     unsigned DiagID = IsCompare
11906                           ? diag::warn_address_of_reference_null_compare
11907                           : diag::warn_address_of_reference_bool_conversion;
11908     PartialDiagnostic PD = PDiag(DiagID) << E->getSourceRange() << Range
11909                                          << IsEqual;
11910     if (CheckForReference(*this, E, PD)) {
11911       return;
11912     }
11913   }
11914 
11915   auto ComplainAboutNonnullParamOrCall = [&](const Attr *NonnullAttr) {
11916     bool IsParam = isa<NonNullAttr>(NonnullAttr);
11917     std::string Str;
11918     llvm::raw_string_ostream S(Str);
11919     E->printPretty(S, nullptr, getPrintingPolicy());
11920     unsigned DiagID = IsCompare ? diag::warn_nonnull_expr_compare
11921                                 : diag::warn_cast_nonnull_to_bool;
11922     Diag(E->getExprLoc(), DiagID) << IsParam << S.str()
11923       << E->getSourceRange() << Range << IsEqual;
11924     Diag(NonnullAttr->getLocation(), diag::note_declared_nonnull) << IsParam;
11925   };
11926 
11927   // If we have a CallExpr that is tagged with returns_nonnull, we can complain.
11928   if (auto *Call = dyn_cast<CallExpr>(E->IgnoreParenImpCasts())) {
11929     if (auto *Callee = Call->getDirectCallee()) {
11930       if (const Attr *A = Callee->getAttr<ReturnsNonNullAttr>()) {
11931         ComplainAboutNonnullParamOrCall(A);
11932         return;
11933       }
11934     }
11935   }
11936 
11937   // Complain if we are converting a lambda expression to a boolean value
11938   // outside of instantiation.
11939   if (!inTemplateInstantiation()) {
11940     if (const auto *MCallExpr = dyn_cast<CXXMemberCallExpr>(E)) {
11941       if (const auto *MRecordDecl = MCallExpr->getRecordDecl();
11942           MRecordDecl && MRecordDecl->isLambda()) {
11943         Diag(E->getExprLoc(), diag::warn_impcast_pointer_to_bool)
11944             << /*LambdaPointerConversionOperatorType=*/3
11945             << MRecordDecl->getSourceRange() << Range << IsEqual;
11946         return;
11947       }
11948     }
11949   }
11950 
11951   // Expect to find a single Decl.  Skip anything more complicated.
11952   ValueDecl *D = nullptr;
11953   if (DeclRefExpr *R = dyn_cast<DeclRefExpr>(E)) {
11954     D = R->getDecl();
11955   } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) {
11956     D = M->getMemberDecl();
11957   }
11958 
11959   // Weak Decls can be null.
11960   if (!D || D->isWeak())
11961     return;
11962 
11963   // Check for parameter decl with nonnull attribute
11964   if (const auto* PV = dyn_cast<ParmVarDecl>(D)) {
11965     if (getCurFunction() &&
11966         !getCurFunction()->ModifiedNonNullParams.count(PV)) {
11967       if (const Attr *A = PV->getAttr<NonNullAttr>()) {
11968         ComplainAboutNonnullParamOrCall(A);
11969         return;
11970       }
11971 
11972       if (const auto *FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) {
11973         // Skip function template not specialized yet.
11974         if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate)
11975           return;
11976         auto ParamIter = llvm::find(FD->parameters(), PV);
11977         assert(ParamIter != FD->param_end());
11978         unsigned ParamNo = std::distance(FD->param_begin(), ParamIter);
11979 
11980         for (const auto *NonNull : FD->specific_attrs<NonNullAttr>()) {
11981           if (!NonNull->args_size()) {
11982               ComplainAboutNonnullParamOrCall(NonNull);
11983               return;
11984           }
11985 
11986           for (const ParamIdx &ArgNo : NonNull->args()) {
11987             if (ArgNo.getASTIndex() == ParamNo) {
11988               ComplainAboutNonnullParamOrCall(NonNull);
11989               return;
11990             }
11991           }
11992         }
11993       }
11994     }
11995   }
11996 
11997   QualType T = D->getType();
11998   const bool IsArray = T->isArrayType();
11999   const bool IsFunction = T->isFunctionType();
12000 
12001   // Address of function is used to silence the function warning.
12002   if (IsAddressOf && IsFunction) {
12003     return;
12004   }
12005 
12006   // Found nothing.
12007   if (!IsAddressOf && !IsFunction && !IsArray)
12008     return;
12009 
12010   // Pretty print the expression for the diagnostic.
12011   std::string Str;
12012   llvm::raw_string_ostream S(Str);
12013   E->printPretty(S, nullptr, getPrintingPolicy());
12014 
12015   unsigned DiagID = IsCompare ? diag::warn_null_pointer_compare
12016                               : diag::warn_impcast_pointer_to_bool;
12017   enum {
12018     AddressOf,
12019     FunctionPointer,
12020     ArrayPointer
12021   } DiagType;
12022   if (IsAddressOf)
12023     DiagType = AddressOf;
12024   else if (IsFunction)
12025     DiagType = FunctionPointer;
12026   else if (IsArray)
12027     DiagType = ArrayPointer;
12028   else
12029     llvm_unreachable("Could not determine diagnostic.");
12030   Diag(E->getExprLoc(), DiagID) << DiagType << S.str() << E->getSourceRange()
12031                                 << Range << IsEqual;
12032 
12033   if (!IsFunction)
12034     return;
12035 
12036   // Suggest '&' to silence the function warning.
12037   Diag(E->getExprLoc(), diag::note_function_warning_silence)
12038       << FixItHint::CreateInsertion(E->getBeginLoc(), "&");
12039 
12040   // Check to see if '()' fixit should be emitted.
12041   QualType ReturnType;
12042   UnresolvedSet<4> NonTemplateOverloads;
12043   tryExprAsCall(*E, ReturnType, NonTemplateOverloads);
12044   if (ReturnType.isNull())
12045     return;
12046 
12047   if (IsCompare) {
12048     // There are two cases here.  If there is null constant, the only suggest
12049     // for a pointer return type.  If the null is 0, then suggest if the return
12050     // type is a pointer or an integer type.
12051     if (!ReturnType->isPointerType()) {
12052       if (NullKind == Expr::NPCK_ZeroExpression ||
12053           NullKind == Expr::NPCK_ZeroLiteral) {
12054         if (!ReturnType->isIntegerType())
12055           return;
12056       } else {
12057         return;
12058       }
12059     }
12060   } else { // !IsCompare
12061     // For function to bool, only suggest if the function pointer has bool
12062     // return type.
12063     if (!ReturnType->isSpecificBuiltinType(BuiltinType::Bool))
12064       return;
12065   }
12066   Diag(E->getExprLoc(), diag::note_function_to_function_call)
12067       << FixItHint::CreateInsertion(getLocForEndOfToken(E->getEndLoc()), "()");
12068 }
12069 
12070 void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) {
12071   // Don't diagnose in unevaluated contexts.
12072   if (isUnevaluatedContext())
12073     return;
12074 
12075   // Don't diagnose for value- or type-dependent expressions.
12076   if (E->isTypeDependent() || E->isValueDependent())
12077     return;
12078 
12079   // Check for array bounds violations in cases where the check isn't triggered
12080   // elsewhere for other Expr types (like BinaryOperators), e.g. when an
12081   // ArraySubscriptExpr is on the RHS of a variable initialization.
12082   CheckArrayAccess(E);
12083 
12084   // This is not the right CC for (e.g.) a variable initialization.
12085   AnalyzeImplicitConversions(*this, E, CC);
12086 }
12087 
12088 void Sema::CheckBoolLikeConversion(Expr *E, SourceLocation CC) {
12089   ::CheckBoolLikeConversion(*this, E, CC);
12090 }
12091 
12092 void Sema::CheckForIntOverflow (const Expr *E) {
12093   // Use a work list to deal with nested struct initializers.
12094   SmallVector<const Expr *, 2> Exprs(1, E);
12095 
12096   do {
12097     const Expr *OriginalE = Exprs.pop_back_val();
12098     const Expr *E = OriginalE->IgnoreParenCasts();
12099 
12100     if (isa<BinaryOperator, UnaryOperator>(E)) {
12101       E->EvaluateForOverflow(Context);
12102       continue;
12103     }
12104 
12105     if (const auto *InitList = dyn_cast<InitListExpr>(OriginalE))
12106       Exprs.append(InitList->inits().begin(), InitList->inits().end());
12107     else if (isa<ObjCBoxedExpr>(OriginalE))
12108       E->EvaluateForOverflow(Context);
12109     else if (const auto *Call = dyn_cast<CallExpr>(E))
12110       Exprs.append(Call->arg_begin(), Call->arg_end());
12111     else if (const auto *Message = dyn_cast<ObjCMessageExpr>(E))
12112       Exprs.append(Message->arg_begin(), Message->arg_end());
12113     else if (const auto *Construct = dyn_cast<CXXConstructExpr>(E))
12114       Exprs.append(Construct->arg_begin(), Construct->arg_end());
12115     else if (const auto *Temporary = dyn_cast<CXXBindTemporaryExpr>(E))
12116       Exprs.push_back(Temporary->getSubExpr());
12117     else if (const auto *Array = dyn_cast<ArraySubscriptExpr>(E))
12118       Exprs.push_back(Array->getIdx());
12119     else if (const auto *Compound = dyn_cast<CompoundLiteralExpr>(E))
12120       Exprs.push_back(Compound->getInitializer());
12121     else if (const auto *New = dyn_cast<CXXNewExpr>(E);
12122              New && New->isArray()) {
12123       if (auto ArraySize = New->getArraySize())
12124         Exprs.push_back(*ArraySize);
12125     } else if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(OriginalE))
12126       Exprs.push_back(MTE->getSubExpr());
12127   } while (!Exprs.empty());
12128 }
12129 
12130 namespace {
12131 
12132 /// Visitor for expressions which looks for unsequenced operations on the
12133 /// same object.
12134 class SequenceChecker : public ConstEvaluatedExprVisitor<SequenceChecker> {
12135   using Base = ConstEvaluatedExprVisitor<SequenceChecker>;
12136 
12137   /// A tree of sequenced regions within an expression. Two regions are
12138   /// unsequenced if one is an ancestor or a descendent of the other. When we
12139   /// finish processing an expression with sequencing, such as a comma
12140   /// expression, we fold its tree nodes into its parent, since they are
12141   /// unsequenced with respect to nodes we will visit later.
12142   class SequenceTree {
12143     struct Value {
12144       explicit Value(unsigned Parent) : Parent(Parent), Merged(false) {}
12145       unsigned Parent : 31;
12146       LLVM_PREFERRED_TYPE(bool)
12147       unsigned Merged : 1;
12148     };
12149     SmallVector<Value, 8> Values;
12150 
12151   public:
12152     /// A region within an expression which may be sequenced with respect
12153     /// to some other region.
12154     class Seq {
12155       friend class SequenceTree;
12156 
12157       unsigned Index;
12158 
12159       explicit Seq(unsigned N) : Index(N) {}
12160 
12161     public:
12162       Seq() : Index(0) {}
12163     };
12164 
12165     SequenceTree() { Values.push_back(Value(0)); }
12166     Seq root() const { return Seq(0); }
12167 
12168     /// Create a new sequence of operations, which is an unsequenced
12169     /// subset of \p Parent. This sequence of operations is sequenced with
12170     /// respect to other children of \p Parent.
12171     Seq allocate(Seq Parent) {
12172       Values.push_back(Value(Parent.Index));
12173       return Seq(Values.size() - 1);
12174     }
12175 
12176     /// Merge a sequence of operations into its parent.
12177     void merge(Seq S) {
12178       Values[S.Index].Merged = true;
12179     }
12180 
12181     /// Determine whether two operations are unsequenced. This operation
12182     /// is asymmetric: \p Cur should be the more recent sequence, and \p Old
12183     /// should have been merged into its parent as appropriate.
12184     bool isUnsequenced(Seq Cur, Seq Old) {
12185       unsigned C = representative(Cur.Index);
12186       unsigned Target = representative(Old.Index);
12187       while (C >= Target) {
12188         if (C == Target)
12189           return true;
12190         C = Values[C].Parent;
12191       }
12192       return false;
12193     }
12194 
12195   private:
12196     /// Pick a representative for a sequence.
12197     unsigned representative(unsigned K) {
12198       if (Values[K].Merged)
12199         // Perform path compression as we go.
12200         return Values[K].Parent = representative(Values[K].Parent);
12201       return K;
12202     }
12203   };
12204 
12205   /// An object for which we can track unsequenced uses.
12206   using Object = const NamedDecl *;
12207 
12208   /// Different flavors of object usage which we track. We only track the
12209   /// least-sequenced usage of each kind.
12210   enum UsageKind {
12211     /// A read of an object. Multiple unsequenced reads are OK.
12212     UK_Use,
12213 
12214     /// A modification of an object which is sequenced before the value
12215     /// computation of the expression, such as ++n in C++.
12216     UK_ModAsValue,
12217 
12218     /// A modification of an object which is not sequenced before the value
12219     /// computation of the expression, such as n++.
12220     UK_ModAsSideEffect,
12221 
12222     UK_Count = UK_ModAsSideEffect + 1
12223   };
12224 
12225   /// Bundle together a sequencing region and the expression corresponding
12226   /// to a specific usage. One Usage is stored for each usage kind in UsageInfo.
12227   struct Usage {
12228     const Expr *UsageExpr = nullptr;
12229     SequenceTree::Seq Seq;
12230 
12231     Usage() = default;
12232   };
12233 
12234   struct UsageInfo {
12235     Usage Uses[UK_Count];
12236 
12237     /// Have we issued a diagnostic for this object already?
12238     bool Diagnosed = false;
12239 
12240     UsageInfo();
12241   };
12242   using UsageInfoMap = llvm::SmallDenseMap<Object, UsageInfo, 16>;
12243 
12244   Sema &SemaRef;
12245 
12246   /// Sequenced regions within the expression.
12247   SequenceTree Tree;
12248 
12249   /// Declaration modifications and references which we have seen.
12250   UsageInfoMap UsageMap;
12251 
12252   /// The region we are currently within.
12253   SequenceTree::Seq Region;
12254 
12255   /// Filled in with declarations which were modified as a side-effect
12256   /// (that is, post-increment operations).
12257   SmallVectorImpl<std::pair<Object, Usage>> *ModAsSideEffect = nullptr;
12258 
12259   /// Expressions to check later. We defer checking these to reduce
12260   /// stack usage.
12261   SmallVectorImpl<const Expr *> &WorkList;
12262 
12263   /// RAII object wrapping the visitation of a sequenced subexpression of an
12264   /// expression. At the end of this process, the side-effects of the evaluation
12265   /// become sequenced with respect to the value computation of the result, so
12266   /// we downgrade any UK_ModAsSideEffect within the evaluation to
12267   /// UK_ModAsValue.
12268   struct SequencedSubexpression {
12269     SequencedSubexpression(SequenceChecker &Self)
12270       : Self(Self), OldModAsSideEffect(Self.ModAsSideEffect) {
12271       Self.ModAsSideEffect = &ModAsSideEffect;
12272     }
12273 
12274     ~SequencedSubexpression() {
12275       for (const std::pair<Object, Usage> &M : llvm::reverse(ModAsSideEffect)) {
12276         // Add a new usage with usage kind UK_ModAsValue, and then restore
12277         // the previous usage with UK_ModAsSideEffect (thus clearing it if
12278         // the previous one was empty).
12279         UsageInfo &UI = Self.UsageMap[M.first];
12280         auto &SideEffectUsage = UI.Uses[UK_ModAsSideEffect];
12281         Self.addUsage(M.first, UI, SideEffectUsage.UsageExpr, UK_ModAsValue);
12282         SideEffectUsage = M.second;
12283       }
12284       Self.ModAsSideEffect = OldModAsSideEffect;
12285     }
12286 
12287     SequenceChecker &Self;
12288     SmallVector<std::pair<Object, Usage>, 4> ModAsSideEffect;
12289     SmallVectorImpl<std::pair<Object, Usage>> *OldModAsSideEffect;
12290   };
12291 
12292   /// RAII object wrapping the visitation of a subexpression which we might
12293   /// choose to evaluate as a constant. If any subexpression is evaluated and
12294   /// found to be non-constant, this allows us to suppress the evaluation of
12295   /// the outer expression.
12296   class EvaluationTracker {
12297   public:
12298     EvaluationTracker(SequenceChecker &Self)
12299         : Self(Self), Prev(Self.EvalTracker) {
12300       Self.EvalTracker = this;
12301     }
12302 
12303     ~EvaluationTracker() {
12304       Self.EvalTracker = Prev;
12305       if (Prev)
12306         Prev->EvalOK &= EvalOK;
12307     }
12308 
12309     bool evaluate(const Expr *E, bool &Result) {
12310       if (!EvalOK || E->isValueDependent())
12311         return false;
12312       EvalOK = E->EvaluateAsBooleanCondition(
12313           Result, Self.SemaRef.Context,
12314           Self.SemaRef.isConstantEvaluatedContext());
12315       return EvalOK;
12316     }
12317 
12318   private:
12319     SequenceChecker &Self;
12320     EvaluationTracker *Prev;
12321     bool EvalOK = true;
12322   } *EvalTracker = nullptr;
12323 
12324   /// Find the object which is produced by the specified expression,
12325   /// if any.
12326   Object getObject(const Expr *E, bool Mod) const {
12327     E = E->IgnoreParenCasts();
12328     if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
12329       if (Mod && (UO->getOpcode() == UO_PreInc || UO->getOpcode() == UO_PreDec))
12330         return getObject(UO->getSubExpr(), Mod);
12331     } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
12332       if (BO->getOpcode() == BO_Comma)
12333         return getObject(BO->getRHS(), Mod);
12334       if (Mod && BO->isAssignmentOp())
12335         return getObject(BO->getLHS(), Mod);
12336     } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
12337       // FIXME: Check for more interesting cases, like "x.n = ++x.n".
12338       if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenCasts()))
12339         return ME->getMemberDecl();
12340     } else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
12341       // FIXME: If this is a reference, map through to its value.
12342       return DRE->getDecl();
12343     return nullptr;
12344   }
12345 
12346   /// Note that an object \p O was modified or used by an expression
12347   /// \p UsageExpr with usage kind \p UK. \p UI is the \p UsageInfo for
12348   /// the object \p O as obtained via the \p UsageMap.
12349   void addUsage(Object O, UsageInfo &UI, const Expr *UsageExpr, UsageKind UK) {
12350     // Get the old usage for the given object and usage kind.
12351     Usage &U = UI.Uses[UK];
12352     if (!U.UsageExpr || !Tree.isUnsequenced(Region, U.Seq)) {
12353       // If we have a modification as side effect and are in a sequenced
12354       // subexpression, save the old Usage so that we can restore it later
12355       // in SequencedSubexpression::~SequencedSubexpression.
12356       if (UK == UK_ModAsSideEffect && ModAsSideEffect)
12357         ModAsSideEffect->push_back(std::make_pair(O, U));
12358       // Then record the new usage with the current sequencing region.
12359       U.UsageExpr = UsageExpr;
12360       U.Seq = Region;
12361     }
12362   }
12363 
12364   /// Check whether a modification or use of an object \p O in an expression
12365   /// \p UsageExpr conflicts with a prior usage of kind \p OtherKind. \p UI is
12366   /// the \p UsageInfo for the object \p O as obtained via the \p UsageMap.
12367   /// \p IsModMod is true when we are checking for a mod-mod unsequenced
12368   /// usage and false we are checking for a mod-use unsequenced usage.
12369   void checkUsage(Object O, UsageInfo &UI, const Expr *UsageExpr,
12370                   UsageKind OtherKind, bool IsModMod) {
12371     if (UI.Diagnosed)
12372       return;
12373 
12374     const Usage &U = UI.Uses[OtherKind];
12375     if (!U.UsageExpr || !Tree.isUnsequenced(Region, U.Seq))
12376       return;
12377 
12378     const Expr *Mod = U.UsageExpr;
12379     const Expr *ModOrUse = UsageExpr;
12380     if (OtherKind == UK_Use)
12381       std::swap(Mod, ModOrUse);
12382 
12383     SemaRef.DiagRuntimeBehavior(
12384         Mod->getExprLoc(), {Mod, ModOrUse},
12385         SemaRef.PDiag(IsModMod ? diag::warn_unsequenced_mod_mod
12386                                : diag::warn_unsequenced_mod_use)
12387             << O << SourceRange(ModOrUse->getExprLoc()));
12388     UI.Diagnosed = true;
12389   }
12390 
12391   // A note on note{Pre, Post}{Use, Mod}:
12392   //
12393   // (It helps to follow the algorithm with an expression such as
12394   //  "((++k)++, k) = k" or "k = (k++, k++)". Both contain unsequenced
12395   //  operations before C++17 and both are well-defined in C++17).
12396   //
12397   // When visiting a node which uses/modify an object we first call notePreUse
12398   // or notePreMod before visiting its sub-expression(s). At this point the
12399   // children of the current node have not yet been visited and so the eventual
12400   // uses/modifications resulting from the children of the current node have not
12401   // been recorded yet.
12402   //
12403   // We then visit the children of the current node. After that notePostUse or
12404   // notePostMod is called. These will 1) detect an unsequenced modification
12405   // as side effect (as in "k++ + k") and 2) add a new usage with the
12406   // appropriate usage kind.
12407   //
12408   // We also have to be careful that some operation sequences modification as
12409   // side effect as well (for example: || or ,). To account for this we wrap
12410   // the visitation of such a sub-expression (for example: the LHS of || or ,)
12411   // with SequencedSubexpression. SequencedSubexpression is an RAII object
12412   // which record usages which are modifications as side effect, and then
12413   // downgrade them (or more accurately restore the previous usage which was a
12414   // modification as side effect) when exiting the scope of the sequenced
12415   // subexpression.
12416 
12417   void notePreUse(Object O, const Expr *UseExpr) {
12418     UsageInfo &UI = UsageMap[O];
12419     // Uses conflict with other modifications.
12420     checkUsage(O, UI, UseExpr, /*OtherKind=*/UK_ModAsValue, /*IsModMod=*/false);
12421   }
12422 
12423   void notePostUse(Object O, const Expr *UseExpr) {
12424     UsageInfo &UI = UsageMap[O];
12425     checkUsage(O, UI, UseExpr, /*OtherKind=*/UK_ModAsSideEffect,
12426                /*IsModMod=*/false);
12427     addUsage(O, UI, UseExpr, /*UsageKind=*/UK_Use);
12428   }
12429 
12430   void notePreMod(Object O, const Expr *ModExpr) {
12431     UsageInfo &UI = UsageMap[O];
12432     // Modifications conflict with other modifications and with uses.
12433     checkUsage(O, UI, ModExpr, /*OtherKind=*/UK_ModAsValue, /*IsModMod=*/true);
12434     checkUsage(O, UI, ModExpr, /*OtherKind=*/UK_Use, /*IsModMod=*/false);
12435   }
12436 
12437   void notePostMod(Object O, const Expr *ModExpr, UsageKind UK) {
12438     UsageInfo &UI = UsageMap[O];
12439     checkUsage(O, UI, ModExpr, /*OtherKind=*/UK_ModAsSideEffect,
12440                /*IsModMod=*/true);
12441     addUsage(O, UI, ModExpr, /*UsageKind=*/UK);
12442   }
12443 
12444 public:
12445   SequenceChecker(Sema &S, const Expr *E,
12446                   SmallVectorImpl<const Expr *> &WorkList)
12447       : Base(S.Context), SemaRef(S), Region(Tree.root()), WorkList(WorkList) {
12448     Visit(E);
12449     // Silence a -Wunused-private-field since WorkList is now unused.
12450     // TODO: Evaluate if it can be used, and if not remove it.
12451     (void)this->WorkList;
12452   }
12453 
12454   void VisitStmt(const Stmt *S) {
12455     // Skip all statements which aren't expressions for now.
12456   }
12457 
12458   void VisitExpr(const Expr *E) {
12459     // By default, just recurse to evaluated subexpressions.
12460     Base::VisitStmt(E);
12461   }
12462 
12463   void VisitCoroutineSuspendExpr(const CoroutineSuspendExpr *CSE) {
12464     for (auto *Sub : CSE->children()) {
12465       const Expr *ChildExpr = dyn_cast_or_null<Expr>(Sub);
12466       if (!ChildExpr)
12467         continue;
12468 
12469       if (ChildExpr == CSE->getOperand())
12470         // Do not recurse over a CoroutineSuspendExpr's operand.
12471         // The operand is also a subexpression of getCommonExpr(), and
12472         // recursing into it directly could confuse object management
12473         // for the sake of sequence tracking.
12474         continue;
12475 
12476       Visit(Sub);
12477     }
12478   }
12479 
12480   void VisitCastExpr(const CastExpr *E) {
12481     Object O = Object();
12482     if (E->getCastKind() == CK_LValueToRValue)
12483       O = getObject(E->getSubExpr(), false);
12484 
12485     if (O)
12486       notePreUse(O, E);
12487     VisitExpr(E);
12488     if (O)
12489       notePostUse(O, E);
12490   }
12491 
12492   void VisitSequencedExpressions(const Expr *SequencedBefore,
12493                                  const Expr *SequencedAfter) {
12494     SequenceTree::Seq BeforeRegion = Tree.allocate(Region);
12495     SequenceTree::Seq AfterRegion = Tree.allocate(Region);
12496     SequenceTree::Seq OldRegion = Region;
12497 
12498     {
12499       SequencedSubexpression SeqBefore(*this);
12500       Region = BeforeRegion;
12501       Visit(SequencedBefore);
12502     }
12503 
12504     Region = AfterRegion;
12505     Visit(SequencedAfter);
12506 
12507     Region = OldRegion;
12508 
12509     Tree.merge(BeforeRegion);
12510     Tree.merge(AfterRegion);
12511   }
12512 
12513   void VisitArraySubscriptExpr(const ArraySubscriptExpr *ASE) {
12514     // C++17 [expr.sub]p1:
12515     //   The expression E1[E2] is identical (by definition) to *((E1)+(E2)). The
12516     //   expression E1 is sequenced before the expression E2.
12517     if (SemaRef.getLangOpts().CPlusPlus17)
12518       VisitSequencedExpressions(ASE->getLHS(), ASE->getRHS());
12519     else {
12520       Visit(ASE->getLHS());
12521       Visit(ASE->getRHS());
12522     }
12523   }
12524 
12525   void VisitBinPtrMemD(const BinaryOperator *BO) { VisitBinPtrMem(BO); }
12526   void VisitBinPtrMemI(const BinaryOperator *BO) { VisitBinPtrMem(BO); }
12527   void VisitBinPtrMem(const BinaryOperator *BO) {
12528     // C++17 [expr.mptr.oper]p4:
12529     //  Abbreviating pm-expression.*cast-expression as E1.*E2, [...]
12530     //  the expression E1 is sequenced before the expression E2.
12531     if (SemaRef.getLangOpts().CPlusPlus17)
12532       VisitSequencedExpressions(BO->getLHS(), BO->getRHS());
12533     else {
12534       Visit(BO->getLHS());
12535       Visit(BO->getRHS());
12536     }
12537   }
12538 
12539   void VisitBinShl(const BinaryOperator *BO) { VisitBinShlShr(BO); }
12540   void VisitBinShr(const BinaryOperator *BO) { VisitBinShlShr(BO); }
12541   void VisitBinShlShr(const BinaryOperator *BO) {
12542     // C++17 [expr.shift]p4:
12543     //  The expression E1 is sequenced before the expression E2.
12544     if (SemaRef.getLangOpts().CPlusPlus17)
12545       VisitSequencedExpressions(BO->getLHS(), BO->getRHS());
12546     else {
12547       Visit(BO->getLHS());
12548       Visit(BO->getRHS());
12549     }
12550   }
12551 
12552   void VisitBinComma(const BinaryOperator *BO) {
12553     // C++11 [expr.comma]p1:
12554     //   Every value computation and side effect associated with the left
12555     //   expression is sequenced before every value computation and side
12556     //   effect associated with the right expression.
12557     VisitSequencedExpressions(BO->getLHS(), BO->getRHS());
12558   }
12559 
12560   void VisitBinAssign(const BinaryOperator *BO) {
12561     SequenceTree::Seq RHSRegion;
12562     SequenceTree::Seq LHSRegion;
12563     if (SemaRef.getLangOpts().CPlusPlus17) {
12564       RHSRegion = Tree.allocate(Region);
12565       LHSRegion = Tree.allocate(Region);
12566     } else {
12567       RHSRegion = Region;
12568       LHSRegion = Region;
12569     }
12570     SequenceTree::Seq OldRegion = Region;
12571 
12572     // C++11 [expr.ass]p1:
12573     //  [...] the assignment is sequenced after the value computation
12574     //  of the right and left operands, [...]
12575     //
12576     // so check it before inspecting the operands and update the
12577     // map afterwards.
12578     Object O = getObject(BO->getLHS(), /*Mod=*/true);
12579     if (O)
12580       notePreMod(O, BO);
12581 
12582     if (SemaRef.getLangOpts().CPlusPlus17) {
12583       // C++17 [expr.ass]p1:
12584       //  [...] The right operand is sequenced before the left operand. [...]
12585       {
12586         SequencedSubexpression SeqBefore(*this);
12587         Region = RHSRegion;
12588         Visit(BO->getRHS());
12589       }
12590 
12591       Region = LHSRegion;
12592       Visit(BO->getLHS());
12593 
12594       if (O && isa<CompoundAssignOperator>(BO))
12595         notePostUse(O, BO);
12596 
12597     } else {
12598       // C++11 does not specify any sequencing between the LHS and RHS.
12599       Region = LHSRegion;
12600       Visit(BO->getLHS());
12601 
12602       if (O && isa<CompoundAssignOperator>(BO))
12603         notePostUse(O, BO);
12604 
12605       Region = RHSRegion;
12606       Visit(BO->getRHS());
12607     }
12608 
12609     // C++11 [expr.ass]p1:
12610     //  the assignment is sequenced [...] before the value computation of the
12611     //  assignment expression.
12612     // C11 6.5.16/3 has no such rule.
12613     Region = OldRegion;
12614     if (O)
12615       notePostMod(O, BO,
12616                   SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue
12617                                                   : UK_ModAsSideEffect);
12618     if (SemaRef.getLangOpts().CPlusPlus17) {
12619       Tree.merge(RHSRegion);
12620       Tree.merge(LHSRegion);
12621     }
12622   }
12623 
12624   void VisitCompoundAssignOperator(const CompoundAssignOperator *CAO) {
12625     VisitBinAssign(CAO);
12626   }
12627 
12628   void VisitUnaryPreInc(const UnaryOperator *UO) { VisitUnaryPreIncDec(UO); }
12629   void VisitUnaryPreDec(const UnaryOperator *UO) { VisitUnaryPreIncDec(UO); }
12630   void VisitUnaryPreIncDec(const UnaryOperator *UO) {
12631     Object O = getObject(UO->getSubExpr(), true);
12632     if (!O)
12633       return VisitExpr(UO);
12634 
12635     notePreMod(O, UO);
12636     Visit(UO->getSubExpr());
12637     // C++11 [expr.pre.incr]p1:
12638     //   the expression ++x is equivalent to x+=1
12639     notePostMod(O, UO,
12640                 SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue
12641                                                 : UK_ModAsSideEffect);
12642   }
12643 
12644   void VisitUnaryPostInc(const UnaryOperator *UO) { VisitUnaryPostIncDec(UO); }
12645   void VisitUnaryPostDec(const UnaryOperator *UO) { VisitUnaryPostIncDec(UO); }
12646   void VisitUnaryPostIncDec(const UnaryOperator *UO) {
12647     Object O = getObject(UO->getSubExpr(), true);
12648     if (!O)
12649       return VisitExpr(UO);
12650 
12651     notePreMod(O, UO);
12652     Visit(UO->getSubExpr());
12653     notePostMod(O, UO, UK_ModAsSideEffect);
12654   }
12655 
12656   void VisitBinLOr(const BinaryOperator *BO) {
12657     // C++11 [expr.log.or]p2:
12658     //  If the second expression is evaluated, every value computation and
12659     //  side effect associated with the first expression is sequenced before
12660     //  every value computation and side effect associated with the
12661     //  second expression.
12662     SequenceTree::Seq LHSRegion = Tree.allocate(Region);
12663     SequenceTree::Seq RHSRegion = Tree.allocate(Region);
12664     SequenceTree::Seq OldRegion = Region;
12665 
12666     EvaluationTracker Eval(*this);
12667     {
12668       SequencedSubexpression Sequenced(*this);
12669       Region = LHSRegion;
12670       Visit(BO->getLHS());
12671     }
12672 
12673     // C++11 [expr.log.or]p1:
12674     //  [...] the second operand is not evaluated if the first operand
12675     //  evaluates to true.
12676     bool EvalResult = false;
12677     bool EvalOK = Eval.evaluate(BO->getLHS(), EvalResult);
12678     bool ShouldVisitRHS = !EvalOK || !EvalResult;
12679     if (ShouldVisitRHS) {
12680       Region = RHSRegion;
12681       Visit(BO->getRHS());
12682     }
12683 
12684     Region = OldRegion;
12685     Tree.merge(LHSRegion);
12686     Tree.merge(RHSRegion);
12687   }
12688 
12689   void VisitBinLAnd(const BinaryOperator *BO) {
12690     // C++11 [expr.log.and]p2:
12691     //  If the second expression is evaluated, every value computation and
12692     //  side effect associated with the first expression is sequenced before
12693     //  every value computation and side effect associated with the
12694     //  second expression.
12695     SequenceTree::Seq LHSRegion = Tree.allocate(Region);
12696     SequenceTree::Seq RHSRegion = Tree.allocate(Region);
12697     SequenceTree::Seq OldRegion = Region;
12698 
12699     EvaluationTracker Eval(*this);
12700     {
12701       SequencedSubexpression Sequenced(*this);
12702       Region = LHSRegion;
12703       Visit(BO->getLHS());
12704     }
12705 
12706     // C++11 [expr.log.and]p1:
12707     //  [...] the second operand is not evaluated if the first operand is false.
12708     bool EvalResult = false;
12709     bool EvalOK = Eval.evaluate(BO->getLHS(), EvalResult);
12710     bool ShouldVisitRHS = !EvalOK || EvalResult;
12711     if (ShouldVisitRHS) {
12712       Region = RHSRegion;
12713       Visit(BO->getRHS());
12714     }
12715 
12716     Region = OldRegion;
12717     Tree.merge(LHSRegion);
12718     Tree.merge(RHSRegion);
12719   }
12720 
12721   void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO) {
12722     // C++11 [expr.cond]p1:
12723     //  [...] Every value computation and side effect associated with the first
12724     //  expression is sequenced before every value computation and side effect
12725     //  associated with the second or third expression.
12726     SequenceTree::Seq ConditionRegion = Tree.allocate(Region);
12727 
12728     // No sequencing is specified between the true and false expression.
12729     // However since exactly one of both is going to be evaluated we can
12730     // consider them to be sequenced. This is needed to avoid warning on
12731     // something like "x ? y+= 1 : y += 2;" in the case where we will visit
12732     // both the true and false expressions because we can't evaluate x.
12733     // This will still allow us to detect an expression like (pre C++17)
12734     // "(x ? y += 1 : y += 2) = y".
12735     //
12736     // We don't wrap the visitation of the true and false expression with
12737     // SequencedSubexpression because we don't want to downgrade modifications
12738     // as side effect in the true and false expressions after the visition
12739     // is done. (for example in the expression "(x ? y++ : y++) + y" we should
12740     // not warn between the two "y++", but we should warn between the "y++"
12741     // and the "y".
12742     SequenceTree::Seq TrueRegion = Tree.allocate(Region);
12743     SequenceTree::Seq FalseRegion = Tree.allocate(Region);
12744     SequenceTree::Seq OldRegion = Region;
12745 
12746     EvaluationTracker Eval(*this);
12747     {
12748       SequencedSubexpression Sequenced(*this);
12749       Region = ConditionRegion;
12750       Visit(CO->getCond());
12751     }
12752 
12753     // C++11 [expr.cond]p1:
12754     // [...] The first expression is contextually converted to bool (Clause 4).
12755     // It is evaluated and if it is true, the result of the conditional
12756     // expression is the value of the second expression, otherwise that of the
12757     // third expression. Only one of the second and third expressions is
12758     // evaluated. [...]
12759     bool EvalResult = false;
12760     bool EvalOK = Eval.evaluate(CO->getCond(), EvalResult);
12761     bool ShouldVisitTrueExpr = !EvalOK || EvalResult;
12762     bool ShouldVisitFalseExpr = !EvalOK || !EvalResult;
12763     if (ShouldVisitTrueExpr) {
12764       Region = TrueRegion;
12765       Visit(CO->getTrueExpr());
12766     }
12767     if (ShouldVisitFalseExpr) {
12768       Region = FalseRegion;
12769       Visit(CO->getFalseExpr());
12770     }
12771 
12772     Region = OldRegion;
12773     Tree.merge(ConditionRegion);
12774     Tree.merge(TrueRegion);
12775     Tree.merge(FalseRegion);
12776   }
12777 
12778   void VisitCallExpr(const CallExpr *CE) {
12779     // FIXME: CXXNewExpr and CXXDeleteExpr implicitly call functions.
12780 
12781     if (CE->isUnevaluatedBuiltinCall(Context))
12782       return;
12783 
12784     // C++11 [intro.execution]p15:
12785     //   When calling a function [...], every value computation and side effect
12786     //   associated with any argument expression, or with the postfix expression
12787     //   designating the called function, is sequenced before execution of every
12788     //   expression or statement in the body of the function [and thus before
12789     //   the value computation of its result].
12790     SequencedSubexpression Sequenced(*this);
12791     SemaRef.runWithSufficientStackSpace(CE->getExprLoc(), [&] {
12792       // C++17 [expr.call]p5
12793       //   The postfix-expression is sequenced before each expression in the
12794       //   expression-list and any default argument. [...]
12795       SequenceTree::Seq CalleeRegion;
12796       SequenceTree::Seq OtherRegion;
12797       if (SemaRef.getLangOpts().CPlusPlus17) {
12798         CalleeRegion = Tree.allocate(Region);
12799         OtherRegion = Tree.allocate(Region);
12800       } else {
12801         CalleeRegion = Region;
12802         OtherRegion = Region;
12803       }
12804       SequenceTree::Seq OldRegion = Region;
12805 
12806       // Visit the callee expression first.
12807       Region = CalleeRegion;
12808       if (SemaRef.getLangOpts().CPlusPlus17) {
12809         SequencedSubexpression Sequenced(*this);
12810         Visit(CE->getCallee());
12811       } else {
12812         Visit(CE->getCallee());
12813       }
12814 
12815       // Then visit the argument expressions.
12816       Region = OtherRegion;
12817       for (const Expr *Argument : CE->arguments())
12818         Visit(Argument);
12819 
12820       Region = OldRegion;
12821       if (SemaRef.getLangOpts().CPlusPlus17) {
12822         Tree.merge(CalleeRegion);
12823         Tree.merge(OtherRegion);
12824       }
12825     });
12826   }
12827 
12828   void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CXXOCE) {
12829     // C++17 [over.match.oper]p2:
12830     //   [...] the operator notation is first transformed to the equivalent
12831     //   function-call notation as summarized in Table 12 (where @ denotes one
12832     //   of the operators covered in the specified subclause). However, the
12833     //   operands are sequenced in the order prescribed for the built-in
12834     //   operator (Clause 8).
12835     //
12836     // From the above only overloaded binary operators and overloaded call
12837     // operators have sequencing rules in C++17 that we need to handle
12838     // separately.
12839     if (!SemaRef.getLangOpts().CPlusPlus17 ||
12840         (CXXOCE->getNumArgs() != 2 && CXXOCE->getOperator() != OO_Call))
12841       return VisitCallExpr(CXXOCE);
12842 
12843     enum {
12844       NoSequencing,
12845       LHSBeforeRHS,
12846       RHSBeforeLHS,
12847       LHSBeforeRest
12848     } SequencingKind;
12849     switch (CXXOCE->getOperator()) {
12850     case OO_Equal:
12851     case OO_PlusEqual:
12852     case OO_MinusEqual:
12853     case OO_StarEqual:
12854     case OO_SlashEqual:
12855     case OO_PercentEqual:
12856     case OO_CaretEqual:
12857     case OO_AmpEqual:
12858     case OO_PipeEqual:
12859     case OO_LessLessEqual:
12860     case OO_GreaterGreaterEqual:
12861       SequencingKind = RHSBeforeLHS;
12862       break;
12863 
12864     case OO_LessLess:
12865     case OO_GreaterGreater:
12866     case OO_AmpAmp:
12867     case OO_PipePipe:
12868     case OO_Comma:
12869     case OO_ArrowStar:
12870     case OO_Subscript:
12871       SequencingKind = LHSBeforeRHS;
12872       break;
12873 
12874     case OO_Call:
12875       SequencingKind = LHSBeforeRest;
12876       break;
12877 
12878     default:
12879       SequencingKind = NoSequencing;
12880       break;
12881     }
12882 
12883     if (SequencingKind == NoSequencing)
12884       return VisitCallExpr(CXXOCE);
12885 
12886     // This is a call, so all subexpressions are sequenced before the result.
12887     SequencedSubexpression Sequenced(*this);
12888 
12889     SemaRef.runWithSufficientStackSpace(CXXOCE->getExprLoc(), [&] {
12890       assert(SemaRef.getLangOpts().CPlusPlus17 &&
12891              "Should only get there with C++17 and above!");
12892       assert((CXXOCE->getNumArgs() == 2 || CXXOCE->getOperator() == OO_Call) &&
12893              "Should only get there with an overloaded binary operator"
12894              " or an overloaded call operator!");
12895 
12896       if (SequencingKind == LHSBeforeRest) {
12897         assert(CXXOCE->getOperator() == OO_Call &&
12898                "We should only have an overloaded call operator here!");
12899 
12900         // This is very similar to VisitCallExpr, except that we only have the
12901         // C++17 case. The postfix-expression is the first argument of the
12902         // CXXOperatorCallExpr. The expressions in the expression-list, if any,
12903         // are in the following arguments.
12904         //
12905         // Note that we intentionally do not visit the callee expression since
12906         // it is just a decayed reference to a function.
12907         SequenceTree::Seq PostfixExprRegion = Tree.allocate(Region);
12908         SequenceTree::Seq ArgsRegion = Tree.allocate(Region);
12909         SequenceTree::Seq OldRegion = Region;
12910 
12911         assert(CXXOCE->getNumArgs() >= 1 &&
12912                "An overloaded call operator must have at least one argument"
12913                " for the postfix-expression!");
12914         const Expr *PostfixExpr = CXXOCE->getArgs()[0];
12915         llvm::ArrayRef<const Expr *> Args(CXXOCE->getArgs() + 1,
12916                                           CXXOCE->getNumArgs() - 1);
12917 
12918         // Visit the postfix-expression first.
12919         {
12920           Region = PostfixExprRegion;
12921           SequencedSubexpression Sequenced(*this);
12922           Visit(PostfixExpr);
12923         }
12924 
12925         // Then visit the argument expressions.
12926         Region = ArgsRegion;
12927         for (const Expr *Arg : Args)
12928           Visit(Arg);
12929 
12930         Region = OldRegion;
12931         Tree.merge(PostfixExprRegion);
12932         Tree.merge(ArgsRegion);
12933       } else {
12934         assert(CXXOCE->getNumArgs() == 2 &&
12935                "Should only have two arguments here!");
12936         assert((SequencingKind == LHSBeforeRHS ||
12937                 SequencingKind == RHSBeforeLHS) &&
12938                "Unexpected sequencing kind!");
12939 
12940         // We do not visit the callee expression since it is just a decayed
12941         // reference to a function.
12942         const Expr *E1 = CXXOCE->getArg(0);
12943         const Expr *E2 = CXXOCE->getArg(1);
12944         if (SequencingKind == RHSBeforeLHS)
12945           std::swap(E1, E2);
12946 
12947         return VisitSequencedExpressions(E1, E2);
12948       }
12949     });
12950   }
12951 
12952   void VisitCXXConstructExpr(const CXXConstructExpr *CCE) {
12953     // This is a call, so all subexpressions are sequenced before the result.
12954     SequencedSubexpression Sequenced(*this);
12955 
12956     if (!CCE->isListInitialization())
12957       return VisitExpr(CCE);
12958 
12959     // In C++11, list initializations are sequenced.
12960     SequenceExpressionsInOrder(
12961         llvm::ArrayRef(CCE->getArgs(), CCE->getNumArgs()));
12962   }
12963 
12964   void VisitInitListExpr(const InitListExpr *ILE) {
12965     if (!SemaRef.getLangOpts().CPlusPlus11)
12966       return VisitExpr(ILE);
12967 
12968     // In C++11, list initializations are sequenced.
12969     SequenceExpressionsInOrder(ILE->inits());
12970   }
12971 
12972   void VisitCXXParenListInitExpr(const CXXParenListInitExpr *PLIE) {
12973     // C++20 parenthesized list initializations are sequenced. See C++20
12974     // [decl.init.general]p16.5 and [decl.init.general]p16.6.2.2.
12975     SequenceExpressionsInOrder(PLIE->getInitExprs());
12976   }
12977 
12978 private:
12979   void SequenceExpressionsInOrder(ArrayRef<const Expr *> ExpressionList) {
12980     SmallVector<SequenceTree::Seq, 32> Elts;
12981     SequenceTree::Seq Parent = Region;
12982     for (const Expr *E : ExpressionList) {
12983       if (!E)
12984         continue;
12985       Region = Tree.allocate(Parent);
12986       Elts.push_back(Region);
12987       Visit(E);
12988     }
12989 
12990     // Forget that the initializers are sequenced.
12991     Region = Parent;
12992     for (unsigned I = 0; I < Elts.size(); ++I)
12993       Tree.merge(Elts[I]);
12994   }
12995 };
12996 
12997 SequenceChecker::UsageInfo::UsageInfo() = default;
12998 
12999 } // namespace
13000 
13001 void Sema::CheckUnsequencedOperations(const Expr *E) {
13002   SmallVector<const Expr *, 8> WorkList;
13003   WorkList.push_back(E);
13004   while (!WorkList.empty()) {
13005     const Expr *Item = WorkList.pop_back_val();
13006     SequenceChecker(*this, Item, WorkList);
13007   }
13008 }
13009 
13010 void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc,
13011                               bool IsConstexpr) {
13012   llvm::SaveAndRestore ConstantContext(isConstantEvaluatedOverride,
13013                                        IsConstexpr || isa<ConstantExpr>(E));
13014   CheckImplicitConversions(E, CheckLoc);
13015   if (!E->isInstantiationDependent())
13016     CheckUnsequencedOperations(E);
13017   if (!IsConstexpr && !E->isValueDependent())
13018     CheckForIntOverflow(E);
13019   DiagnoseMisalignedMembers();
13020 }
13021 
13022 void Sema::CheckBitFieldInitialization(SourceLocation InitLoc,
13023                                        FieldDecl *BitField,
13024                                        Expr *Init) {
13025   (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc);
13026 }
13027 
13028 static void diagnoseArrayStarInParamType(Sema &S, QualType PType,
13029                                          SourceLocation Loc) {
13030   if (!PType->isVariablyModifiedType())
13031     return;
13032   if (const auto *PointerTy = dyn_cast<PointerType>(PType)) {
13033     diagnoseArrayStarInParamType(S, PointerTy->getPointeeType(), Loc);
13034     return;
13035   }
13036   if (const auto *ReferenceTy = dyn_cast<ReferenceType>(PType)) {
13037     diagnoseArrayStarInParamType(S, ReferenceTy->getPointeeType(), Loc);
13038     return;
13039   }
13040   if (const auto *ParenTy = dyn_cast<ParenType>(PType)) {
13041     diagnoseArrayStarInParamType(S, ParenTy->getInnerType(), Loc);
13042     return;
13043   }
13044 
13045   const ArrayType *AT = S.Context.getAsArrayType(PType);
13046   if (!AT)
13047     return;
13048 
13049   if (AT->getSizeModifier() != ArraySizeModifier::Star) {
13050     diagnoseArrayStarInParamType(S, AT->getElementType(), Loc);
13051     return;
13052   }
13053 
13054   S.Diag(Loc, diag::err_array_star_in_function_definition);
13055 }
13056 
13057 bool Sema::CheckParmsForFunctionDef(ArrayRef<ParmVarDecl *> Parameters,
13058                                     bool CheckParameterNames) {
13059   bool HasInvalidParm = false;
13060   for (ParmVarDecl *Param : Parameters) {
13061     assert(Param && "null in a parameter list");
13062     // C99 6.7.5.3p4: the parameters in a parameter type list in a
13063     // function declarator that is part of a function definition of
13064     // that function shall not have incomplete type.
13065     //
13066     // C++23 [dcl.fct.def.general]/p2
13067     // The type of a parameter [...] for a function definition
13068     // shall not be a (possibly cv-qualified) class type that is incomplete
13069     // or abstract within the function body unless the function is deleted.
13070     if (!Param->isInvalidDecl() &&
13071         (RequireCompleteType(Param->getLocation(), Param->getType(),
13072                              diag::err_typecheck_decl_incomplete_type) ||
13073          RequireNonAbstractType(Param->getBeginLoc(), Param->getOriginalType(),
13074                                 diag::err_abstract_type_in_decl,
13075                                 AbstractParamType))) {
13076       Param->setInvalidDecl();
13077       HasInvalidParm = true;
13078     }
13079 
13080     // C99 6.9.1p5: If the declarator includes a parameter type list, the
13081     // declaration of each parameter shall include an identifier.
13082     if (CheckParameterNames && Param->getIdentifier() == nullptr &&
13083         !Param->isImplicit() && !getLangOpts().CPlusPlus) {
13084       // Diagnose this as an extension in C17 and earlier.
13085       if (!getLangOpts().C23)
13086         Diag(Param->getLocation(), diag::ext_parameter_name_omitted_c23);
13087     }
13088 
13089     // C99 6.7.5.3p12:
13090     //   If the function declarator is not part of a definition of that
13091     //   function, parameters may have incomplete type and may use the [*]
13092     //   notation in their sequences of declarator specifiers to specify
13093     //   variable length array types.
13094     QualType PType = Param->getOriginalType();
13095     // FIXME: This diagnostic should point the '[*]' if source-location
13096     // information is added for it.
13097     diagnoseArrayStarInParamType(*this, PType, Param->getLocation());
13098 
13099     // If the parameter is a c++ class type and it has to be destructed in the
13100     // callee function, declare the destructor so that it can be called by the
13101     // callee function. Do not perform any direct access check on the dtor here.
13102     if (!Param->isInvalidDecl()) {
13103       if (CXXRecordDecl *ClassDecl = Param->getType()->getAsCXXRecordDecl()) {
13104         if (!ClassDecl->isInvalidDecl() &&
13105             !ClassDecl->hasIrrelevantDestructor() &&
13106             !ClassDecl->isDependentContext() &&
13107             ClassDecl->isParamDestroyedInCallee()) {
13108           CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl);
13109           MarkFunctionReferenced(Param->getLocation(), Destructor);
13110           DiagnoseUseOfDecl(Destructor, Param->getLocation());
13111         }
13112       }
13113     }
13114 
13115     // Parameters with the pass_object_size attribute only need to be marked
13116     // constant at function definitions. Because we lack information about
13117     // whether we're on a declaration or definition when we're instantiating the
13118     // attribute, we need to check for constness here.
13119     if (const auto *Attr = Param->getAttr<PassObjectSizeAttr>())
13120       if (!Param->getType().isConstQualified())
13121         Diag(Param->getLocation(), diag::err_attribute_pointers_only)
13122             << Attr->getSpelling() << 1;
13123 
13124     // Check for parameter names shadowing fields from the class.
13125     if (LangOpts.CPlusPlus && !Param->isInvalidDecl()) {
13126       // The owning context for the parameter should be the function, but we
13127       // want to see if this function's declaration context is a record.
13128       DeclContext *DC = Param->getDeclContext();
13129       if (DC && DC->isFunctionOrMethod()) {
13130         if (auto *RD = dyn_cast<CXXRecordDecl>(DC->getParent()))
13131           CheckShadowInheritedFields(Param->getLocation(), Param->getDeclName(),
13132                                      RD, /*DeclIsField*/ false);
13133       }
13134     }
13135 
13136     if (!Param->isInvalidDecl() &&
13137         Param->getOriginalType()->isWebAssemblyTableType()) {
13138       Param->setInvalidDecl();
13139       HasInvalidParm = true;
13140       Diag(Param->getLocation(), diag::err_wasm_table_as_function_parameter);
13141     }
13142   }
13143 
13144   return HasInvalidParm;
13145 }
13146 
13147 std::optional<std::pair<
13148     CharUnits, CharUnits>> static getBaseAlignmentAndOffsetFromPtr(const Expr
13149                                                                        *E,
13150                                                                    ASTContext
13151                                                                        &Ctx);
13152 
13153 /// Compute the alignment and offset of the base class object given the
13154 /// derived-to-base cast expression and the alignment and offset of the derived
13155 /// class object.
13156 static std::pair<CharUnits, CharUnits>
13157 getDerivedToBaseAlignmentAndOffset(const CastExpr *CE, QualType DerivedType,
13158                                    CharUnits BaseAlignment, CharUnits Offset,
13159                                    ASTContext &Ctx) {
13160   for (auto PathI = CE->path_begin(), PathE = CE->path_end(); PathI != PathE;
13161        ++PathI) {
13162     const CXXBaseSpecifier *Base = *PathI;
13163     const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl();
13164     if (Base->isVirtual()) {
13165       // The complete object may have a lower alignment than the non-virtual
13166       // alignment of the base, in which case the base may be misaligned. Choose
13167       // the smaller of the non-virtual alignment and BaseAlignment, which is a
13168       // conservative lower bound of the complete object alignment.
13169       CharUnits NonVirtualAlignment =
13170           Ctx.getASTRecordLayout(BaseDecl).getNonVirtualAlignment();
13171       BaseAlignment = std::min(BaseAlignment, NonVirtualAlignment);
13172       Offset = CharUnits::Zero();
13173     } else {
13174       const ASTRecordLayout &RL =
13175           Ctx.getASTRecordLayout(DerivedType->getAsCXXRecordDecl());
13176       Offset += RL.getBaseClassOffset(BaseDecl);
13177     }
13178     DerivedType = Base->getType();
13179   }
13180 
13181   return std::make_pair(BaseAlignment, Offset);
13182 }
13183 
13184 /// Compute the alignment and offset of a binary additive operator.
13185 static std::optional<std::pair<CharUnits, CharUnits>>
13186 getAlignmentAndOffsetFromBinAddOrSub(const Expr *PtrE, const Expr *IntE,
13187                                      bool IsSub, ASTContext &Ctx) {
13188   QualType PointeeType = PtrE->getType()->getPointeeType();
13189 
13190   if (!PointeeType->isConstantSizeType())
13191     return std::nullopt;
13192 
13193   auto P = getBaseAlignmentAndOffsetFromPtr(PtrE, Ctx);
13194 
13195   if (!P)
13196     return std::nullopt;
13197 
13198   CharUnits EltSize = Ctx.getTypeSizeInChars(PointeeType);
13199   if (std::optional<llvm::APSInt> IdxRes = IntE->getIntegerConstantExpr(Ctx)) {
13200     CharUnits Offset = EltSize * IdxRes->getExtValue();
13201     if (IsSub)
13202       Offset = -Offset;
13203     return std::make_pair(P->first, P->second + Offset);
13204   }
13205 
13206   // If the integer expression isn't a constant expression, compute the lower
13207   // bound of the alignment using the alignment and offset of the pointer
13208   // expression and the element size.
13209   return std::make_pair(
13210       P->first.alignmentAtOffset(P->second).alignmentAtOffset(EltSize),
13211       CharUnits::Zero());
13212 }
13213 
13214 /// This helper function takes an lvalue expression and returns the alignment of
13215 /// a VarDecl and a constant offset from the VarDecl.
13216 std::optional<std::pair<
13217     CharUnits,
13218     CharUnits>> static getBaseAlignmentAndOffsetFromLValue(const Expr *E,
13219                                                            ASTContext &Ctx) {
13220   E = E->IgnoreParens();
13221   switch (E->getStmtClass()) {
13222   default:
13223     break;
13224   case Stmt::CStyleCastExprClass:
13225   case Stmt::CXXStaticCastExprClass:
13226   case Stmt::ImplicitCastExprClass: {
13227     auto *CE = cast<CastExpr>(E);
13228     const Expr *From = CE->getSubExpr();
13229     switch (CE->getCastKind()) {
13230     default:
13231       break;
13232     case CK_NoOp:
13233       return getBaseAlignmentAndOffsetFromLValue(From, Ctx);
13234     case CK_UncheckedDerivedToBase:
13235     case CK_DerivedToBase: {
13236       auto P = getBaseAlignmentAndOffsetFromLValue(From, Ctx);
13237       if (!P)
13238         break;
13239       return getDerivedToBaseAlignmentAndOffset(CE, From->getType(), P->first,
13240                                                 P->second, Ctx);
13241     }
13242     }
13243     break;
13244   }
13245   case Stmt::ArraySubscriptExprClass: {
13246     auto *ASE = cast<ArraySubscriptExpr>(E);
13247     return getAlignmentAndOffsetFromBinAddOrSub(ASE->getBase(), ASE->getIdx(),
13248                                                 false, Ctx);
13249   }
13250   case Stmt::DeclRefExprClass: {
13251     if (auto *VD = dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
13252       // FIXME: If VD is captured by copy or is an escaping __block variable,
13253       // use the alignment of VD's type.
13254       if (!VD->getType()->isReferenceType()) {
13255         // Dependent alignment cannot be resolved -> bail out.
13256         if (VD->hasDependentAlignment())
13257           break;
13258         return std::make_pair(Ctx.getDeclAlign(VD), CharUnits::Zero());
13259       }
13260       if (VD->hasInit())
13261         return getBaseAlignmentAndOffsetFromLValue(VD->getInit(), Ctx);
13262     }
13263     break;
13264   }
13265   case Stmt::MemberExprClass: {
13266     auto *ME = cast<MemberExpr>(E);
13267     auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
13268     if (!FD || FD->getType()->isReferenceType() ||
13269         FD->getParent()->isInvalidDecl())
13270       break;
13271     std::optional<std::pair<CharUnits, CharUnits>> P;
13272     if (ME->isArrow())
13273       P = getBaseAlignmentAndOffsetFromPtr(ME->getBase(), Ctx);
13274     else
13275       P = getBaseAlignmentAndOffsetFromLValue(ME->getBase(), Ctx);
13276     if (!P)
13277       break;
13278     const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(FD->getParent());
13279     uint64_t Offset = Layout.getFieldOffset(FD->getFieldIndex());
13280     return std::make_pair(P->first,
13281                           P->second + CharUnits::fromQuantity(Offset));
13282   }
13283   case Stmt::UnaryOperatorClass: {
13284     auto *UO = cast<UnaryOperator>(E);
13285     switch (UO->getOpcode()) {
13286     default:
13287       break;
13288     case UO_Deref:
13289       return getBaseAlignmentAndOffsetFromPtr(UO->getSubExpr(), Ctx);
13290     }
13291     break;
13292   }
13293   case Stmt::BinaryOperatorClass: {
13294     auto *BO = cast<BinaryOperator>(E);
13295     auto Opcode = BO->getOpcode();
13296     switch (Opcode) {
13297     default:
13298       break;
13299     case BO_Comma:
13300       return getBaseAlignmentAndOffsetFromLValue(BO->getRHS(), Ctx);
13301     }
13302     break;
13303   }
13304   }
13305   return std::nullopt;
13306 }
13307 
13308 /// This helper function takes a pointer expression and returns the alignment of
13309 /// a VarDecl and a constant offset from the VarDecl.
13310 std::optional<std::pair<
13311     CharUnits, CharUnits>> static getBaseAlignmentAndOffsetFromPtr(const Expr
13312                                                                        *E,
13313                                                                    ASTContext
13314                                                                        &Ctx) {
13315   E = E->IgnoreParens();
13316   switch (E->getStmtClass()) {
13317   default:
13318     break;
13319   case Stmt::CStyleCastExprClass:
13320   case Stmt::CXXStaticCastExprClass:
13321   case Stmt::ImplicitCastExprClass: {
13322     auto *CE = cast<CastExpr>(E);
13323     const Expr *From = CE->getSubExpr();
13324     switch (CE->getCastKind()) {
13325     default:
13326       break;
13327     case CK_NoOp:
13328       return getBaseAlignmentAndOffsetFromPtr(From, Ctx);
13329     case CK_ArrayToPointerDecay:
13330       return getBaseAlignmentAndOffsetFromLValue(From, Ctx);
13331     case CK_UncheckedDerivedToBase:
13332     case CK_DerivedToBase: {
13333       auto P = getBaseAlignmentAndOffsetFromPtr(From, Ctx);
13334       if (!P)
13335         break;
13336       return getDerivedToBaseAlignmentAndOffset(
13337           CE, From->getType()->getPointeeType(), P->first, P->second, Ctx);
13338     }
13339     }
13340     break;
13341   }
13342   case Stmt::CXXThisExprClass: {
13343     auto *RD = E->getType()->getPointeeType()->getAsCXXRecordDecl();
13344     CharUnits Alignment = Ctx.getASTRecordLayout(RD).getNonVirtualAlignment();
13345     return std::make_pair(Alignment, CharUnits::Zero());
13346   }
13347   case Stmt::UnaryOperatorClass: {
13348     auto *UO = cast<UnaryOperator>(E);
13349     if (UO->getOpcode() == UO_AddrOf)
13350       return getBaseAlignmentAndOffsetFromLValue(UO->getSubExpr(), Ctx);
13351     break;
13352   }
13353   case Stmt::BinaryOperatorClass: {
13354     auto *BO = cast<BinaryOperator>(E);
13355     auto Opcode = BO->getOpcode();
13356     switch (Opcode) {
13357     default:
13358       break;
13359     case BO_Add:
13360     case BO_Sub: {
13361       const Expr *LHS = BO->getLHS(), *RHS = BO->getRHS();
13362       if (Opcode == BO_Add && !RHS->getType()->isIntegralOrEnumerationType())
13363         std::swap(LHS, RHS);
13364       return getAlignmentAndOffsetFromBinAddOrSub(LHS, RHS, Opcode == BO_Sub,
13365                                                   Ctx);
13366     }
13367     case BO_Comma:
13368       return getBaseAlignmentAndOffsetFromPtr(BO->getRHS(), Ctx);
13369     }
13370     break;
13371   }
13372   }
13373   return std::nullopt;
13374 }
13375 
13376 static CharUnits getPresumedAlignmentOfPointer(const Expr *E, Sema &S) {
13377   // See if we can compute the alignment of a VarDecl and an offset from it.
13378   std::optional<std::pair<CharUnits, CharUnits>> P =
13379       getBaseAlignmentAndOffsetFromPtr(E, S.Context);
13380 
13381   if (P)
13382     return P->first.alignmentAtOffset(P->second);
13383 
13384   // If that failed, return the type's alignment.
13385   return S.Context.getTypeAlignInChars(E->getType()->getPointeeType());
13386 }
13387 
13388 void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) {
13389   // This is actually a lot of work to potentially be doing on every
13390   // cast; don't do it if we're ignoring -Wcast_align (as is the default).
13391   if (getDiagnostics().isIgnored(diag::warn_cast_align, TRange.getBegin()))
13392     return;
13393 
13394   // Ignore dependent types.
13395   if (T->isDependentType() || Op->getType()->isDependentType())
13396     return;
13397 
13398   // Require that the destination be a pointer type.
13399   const PointerType *DestPtr = T->getAs<PointerType>();
13400   if (!DestPtr) return;
13401 
13402   // If the destination has alignment 1, we're done.
13403   QualType DestPointee = DestPtr->getPointeeType();
13404   if (DestPointee->isIncompleteType()) return;
13405   CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee);
13406   if (DestAlign.isOne()) return;
13407 
13408   // Require that the source be a pointer type.
13409   const PointerType *SrcPtr = Op->getType()->getAs<PointerType>();
13410   if (!SrcPtr) return;
13411   QualType SrcPointee = SrcPtr->getPointeeType();
13412 
13413   // Explicitly allow casts from cv void*.  We already implicitly
13414   // allowed casts to cv void*, since they have alignment 1.
13415   // Also allow casts involving incomplete types, which implicitly
13416   // includes 'void'.
13417   if (SrcPointee->isIncompleteType()) return;
13418 
13419   CharUnits SrcAlign = getPresumedAlignmentOfPointer(Op, *this);
13420 
13421   if (SrcAlign >= DestAlign) return;
13422 
13423   Diag(TRange.getBegin(), diag::warn_cast_align)
13424     << Op->getType() << T
13425     << static_cast<unsigned>(SrcAlign.getQuantity())
13426     << static_cast<unsigned>(DestAlign.getQuantity())
13427     << TRange << Op->getSourceRange();
13428 }
13429 
13430 void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
13431                             const ArraySubscriptExpr *ASE,
13432                             bool AllowOnePastEnd, bool IndexNegated) {
13433   // Already diagnosed by the constant evaluator.
13434   if (isConstantEvaluatedContext())
13435     return;
13436 
13437   IndexExpr = IndexExpr->IgnoreParenImpCasts();
13438   if (IndexExpr->isValueDependent())
13439     return;
13440 
13441   const Type *EffectiveType =
13442       BaseExpr->getType()->getPointeeOrArrayElementType();
13443   BaseExpr = BaseExpr->IgnoreParenCasts();
13444   const ConstantArrayType *ArrayTy =
13445       Context.getAsConstantArrayType(BaseExpr->getType());
13446 
13447   LangOptions::StrictFlexArraysLevelKind
13448     StrictFlexArraysLevel = getLangOpts().getStrictFlexArraysLevel();
13449 
13450   const Type *BaseType =
13451       ArrayTy == nullptr ? nullptr : ArrayTy->getElementType().getTypePtr();
13452   bool IsUnboundedArray =
13453       BaseType == nullptr || BaseExpr->isFlexibleArrayMemberLike(
13454                                  Context, StrictFlexArraysLevel,
13455                                  /*IgnoreTemplateOrMacroSubstitution=*/true);
13456   if (EffectiveType->isDependentType() ||
13457       (!IsUnboundedArray && BaseType->isDependentType()))
13458     return;
13459 
13460   Expr::EvalResult Result;
13461   if (!IndexExpr->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects))
13462     return;
13463 
13464   llvm::APSInt index = Result.Val.getInt();
13465   if (IndexNegated) {
13466     index.setIsUnsigned(false);
13467     index = -index;
13468   }
13469 
13470   if (IsUnboundedArray) {
13471     if (EffectiveType->isFunctionType())
13472       return;
13473     if (index.isUnsigned() || !index.isNegative()) {
13474       const auto &ASTC = getASTContext();
13475       unsigned AddrBits = ASTC.getTargetInfo().getPointerWidth(
13476           EffectiveType->getCanonicalTypeInternal().getAddressSpace());
13477       if (index.getBitWidth() < AddrBits)
13478         index = index.zext(AddrBits);
13479       std::optional<CharUnits> ElemCharUnits =
13480           ASTC.getTypeSizeInCharsIfKnown(EffectiveType);
13481       // PR50741 - If EffectiveType has unknown size (e.g., if it's a void
13482       // pointer) bounds-checking isn't meaningful.
13483       if (!ElemCharUnits || ElemCharUnits->isZero())
13484         return;
13485       llvm::APInt ElemBytes(index.getBitWidth(), ElemCharUnits->getQuantity());
13486       // If index has more active bits than address space, we already know
13487       // we have a bounds violation to warn about.  Otherwise, compute
13488       // address of (index + 1)th element, and warn about bounds violation
13489       // only if that address exceeds address space.
13490       if (index.getActiveBits() <= AddrBits) {
13491         bool Overflow;
13492         llvm::APInt Product(index);
13493         Product += 1;
13494         Product = Product.umul_ov(ElemBytes, Overflow);
13495         if (!Overflow && Product.getActiveBits() <= AddrBits)
13496           return;
13497       }
13498 
13499       // Need to compute max possible elements in address space, since that
13500       // is included in diag message.
13501       llvm::APInt MaxElems = llvm::APInt::getMaxValue(AddrBits);
13502       MaxElems = MaxElems.zext(std::max(AddrBits + 1, ElemBytes.getBitWidth()));
13503       MaxElems += 1;
13504       ElemBytes = ElemBytes.zextOrTrunc(MaxElems.getBitWidth());
13505       MaxElems = MaxElems.udiv(ElemBytes);
13506 
13507       unsigned DiagID =
13508           ASE ? diag::warn_array_index_exceeds_max_addressable_bounds
13509               : diag::warn_ptr_arith_exceeds_max_addressable_bounds;
13510 
13511       // Diag message shows element size in bits and in "bytes" (platform-
13512       // dependent CharUnits)
13513       DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr,
13514                           PDiag(DiagID)
13515                               << toString(index, 10, true) << AddrBits
13516                               << (unsigned)ASTC.toBits(*ElemCharUnits)
13517                               << toString(ElemBytes, 10, false)
13518                               << toString(MaxElems, 10, false)
13519                               << (unsigned)MaxElems.getLimitedValue(~0U)
13520                               << IndexExpr->getSourceRange());
13521 
13522       const NamedDecl *ND = nullptr;
13523       // Try harder to find a NamedDecl to point at in the note.
13524       while (const auto *ASE = dyn_cast<ArraySubscriptExpr>(BaseExpr))
13525         BaseExpr = ASE->getBase()->IgnoreParenCasts();
13526       if (const auto *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
13527         ND = DRE->getDecl();
13528       if (const auto *ME = dyn_cast<MemberExpr>(BaseExpr))
13529         ND = ME->getMemberDecl();
13530 
13531       if (ND)
13532         DiagRuntimeBehavior(ND->getBeginLoc(), BaseExpr,
13533                             PDiag(diag::note_array_declared_here) << ND);
13534     }
13535     return;
13536   }
13537 
13538   if (index.isUnsigned() || !index.isNegative()) {
13539     // It is possible that the type of the base expression after
13540     // IgnoreParenCasts is incomplete, even though the type of the base
13541     // expression before IgnoreParenCasts is complete (see PR39746 for an
13542     // example). In this case we have no information about whether the array
13543     // access exceeds the array bounds. However we can still diagnose an array
13544     // access which precedes the array bounds.
13545     if (BaseType->isIncompleteType())
13546       return;
13547 
13548     llvm::APInt size = ArrayTy->getSize();
13549 
13550     if (BaseType != EffectiveType) {
13551       // Make sure we're comparing apples to apples when comparing index to
13552       // size.
13553       uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType);
13554       uint64_t array_typesize = Context.getTypeSize(BaseType);
13555 
13556       // Handle ptrarith_typesize being zero, such as when casting to void*.
13557       // Use the size in bits (what "getTypeSize()" returns) rather than bytes.
13558       if (!ptrarith_typesize)
13559         ptrarith_typesize = Context.getCharWidth();
13560 
13561       if (ptrarith_typesize != array_typesize) {
13562         // There's a cast to a different size type involved.
13563         uint64_t ratio = array_typesize / ptrarith_typesize;
13564 
13565         // TODO: Be smarter about handling cases where array_typesize is not a
13566         // multiple of ptrarith_typesize.
13567         if (ptrarith_typesize * ratio == array_typesize)
13568           size *= llvm::APInt(size.getBitWidth(), ratio);
13569       }
13570     }
13571 
13572     if (size.getBitWidth() > index.getBitWidth())
13573       index = index.zext(size.getBitWidth());
13574     else if (size.getBitWidth() < index.getBitWidth())
13575       size = size.zext(index.getBitWidth());
13576 
13577     // For array subscripting the index must be less than size, but for pointer
13578     // arithmetic also allow the index (offset) to be equal to size since
13579     // computing the next address after the end of the array is legal and
13580     // commonly done e.g. in C++ iterators and range-based for loops.
13581     if (AllowOnePastEnd ? index.ule(size) : index.ult(size))
13582       return;
13583 
13584     // Suppress the warning if the subscript expression (as identified by the
13585     // ']' location) and the index expression are both from macro expansions
13586     // within a system header.
13587     if (ASE) {
13588       SourceLocation RBracketLoc = SourceMgr.getSpellingLoc(
13589           ASE->getRBracketLoc());
13590       if (SourceMgr.isInSystemHeader(RBracketLoc)) {
13591         SourceLocation IndexLoc =
13592             SourceMgr.getSpellingLoc(IndexExpr->getBeginLoc());
13593         if (SourceMgr.isWrittenInSameFile(RBracketLoc, IndexLoc))
13594           return;
13595       }
13596     }
13597 
13598     unsigned DiagID = ASE ? diag::warn_array_index_exceeds_bounds
13599                           : diag::warn_ptr_arith_exceeds_bounds;
13600     unsigned CastMsg = (!ASE || BaseType == EffectiveType) ? 0 : 1;
13601     QualType CastMsgTy = ASE ? ASE->getLHS()->getType() : QualType();
13602 
13603     DiagRuntimeBehavior(
13604         BaseExpr->getBeginLoc(), BaseExpr,
13605         PDiag(DiagID) << toString(index, 10, true) << ArrayTy->desugar()
13606                       << CastMsg << CastMsgTy << IndexExpr->getSourceRange());
13607   } else {
13608     unsigned DiagID = diag::warn_array_index_precedes_bounds;
13609     if (!ASE) {
13610       DiagID = diag::warn_ptr_arith_precedes_bounds;
13611       if (index.isNegative()) index = -index;
13612     }
13613 
13614     DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr,
13615                         PDiag(DiagID) << toString(index, 10, true)
13616                                       << IndexExpr->getSourceRange());
13617   }
13618 
13619   const NamedDecl *ND = nullptr;
13620   // Try harder to find a NamedDecl to point at in the note.
13621   while (const auto *ASE = dyn_cast<ArraySubscriptExpr>(BaseExpr))
13622     BaseExpr = ASE->getBase()->IgnoreParenCasts();
13623   if (const auto *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
13624     ND = DRE->getDecl();
13625   if (const auto *ME = dyn_cast<MemberExpr>(BaseExpr))
13626     ND = ME->getMemberDecl();
13627 
13628   if (ND)
13629     DiagRuntimeBehavior(ND->getBeginLoc(), BaseExpr,
13630                         PDiag(diag::note_array_declared_here) << ND);
13631 }
13632 
13633 void Sema::CheckArrayAccess(const Expr *expr) {
13634   int AllowOnePastEnd = 0;
13635   while (expr) {
13636     expr = expr->IgnoreParenImpCasts();
13637     switch (expr->getStmtClass()) {
13638       case Stmt::ArraySubscriptExprClass: {
13639         const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr);
13640         CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE,
13641                          AllowOnePastEnd > 0);
13642         expr = ASE->getBase();
13643         break;
13644       }
13645       case Stmt::MemberExprClass: {
13646         expr = cast<MemberExpr>(expr)->getBase();
13647         break;
13648       }
13649       case Stmt::ArraySectionExprClass: {
13650         const ArraySectionExpr *ASE = cast<ArraySectionExpr>(expr);
13651         // FIXME: We should probably be checking all of the elements to the
13652         // 'length' here as well.
13653         if (ASE->getLowerBound())
13654           CheckArrayAccess(ASE->getBase(), ASE->getLowerBound(),
13655                            /*ASE=*/nullptr, AllowOnePastEnd > 0);
13656         return;
13657       }
13658       case Stmt::UnaryOperatorClass: {
13659         // Only unwrap the * and & unary operators
13660         const UnaryOperator *UO = cast<UnaryOperator>(expr);
13661         expr = UO->getSubExpr();
13662         switch (UO->getOpcode()) {
13663           case UO_AddrOf:
13664             AllowOnePastEnd++;
13665             break;
13666           case UO_Deref:
13667             AllowOnePastEnd--;
13668             break;
13669           default:
13670             return;
13671         }
13672         break;
13673       }
13674       case Stmt::ConditionalOperatorClass: {
13675         const ConditionalOperator *cond = cast<ConditionalOperator>(expr);
13676         if (const Expr *lhs = cond->getLHS())
13677           CheckArrayAccess(lhs);
13678         if (const Expr *rhs = cond->getRHS())
13679           CheckArrayAccess(rhs);
13680         return;
13681       }
13682       case Stmt::CXXOperatorCallExprClass: {
13683         const auto *OCE = cast<CXXOperatorCallExpr>(expr);
13684         for (const auto *Arg : OCE->arguments())
13685           CheckArrayAccess(Arg);
13686         return;
13687       }
13688       default:
13689         return;
13690     }
13691   }
13692 }
13693 
13694 static bool checkUnsafeAssignLiteral(Sema &S, SourceLocation Loc,
13695                                      Expr *RHS, bool isProperty) {
13696   // Check if RHS is an Objective-C object literal, which also can get
13697   // immediately zapped in a weak reference.  Note that we explicitly
13698   // allow ObjCStringLiterals, since those are designed to never really die.
13699   RHS = RHS->IgnoreParenImpCasts();
13700 
13701   // This enum needs to match with the 'select' in
13702   // warn_objc_arc_literal_assign (off-by-1).
13703   SemaObjC::ObjCLiteralKind Kind = S.ObjC().CheckLiteralKind(RHS);
13704   if (Kind == SemaObjC::LK_String || Kind == SemaObjC::LK_None)
13705     return false;
13706 
13707   S.Diag(Loc, diag::warn_arc_literal_assign)
13708     << (unsigned) Kind
13709     << (isProperty ? 0 : 1)
13710     << RHS->getSourceRange();
13711 
13712   return true;
13713 }
13714 
13715 static bool checkUnsafeAssignObject(Sema &S, SourceLocation Loc,
13716                                     Qualifiers::ObjCLifetime LT,
13717                                     Expr *RHS, bool isProperty) {
13718   // Strip off any implicit cast added to get to the one ARC-specific.
13719   while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
13720     if (cast->getCastKind() == CK_ARCConsumeObject) {
13721       S.Diag(Loc, diag::warn_arc_retained_assign)
13722         << (LT == Qualifiers::OCL_ExplicitNone)
13723         << (isProperty ? 0 : 1)
13724         << RHS->getSourceRange();
13725       return true;
13726     }
13727     RHS = cast->getSubExpr();
13728   }
13729 
13730   if (LT == Qualifiers::OCL_Weak &&
13731       checkUnsafeAssignLiteral(S, Loc, RHS, isProperty))
13732     return true;
13733 
13734   return false;
13735 }
13736 
13737 bool Sema::checkUnsafeAssigns(SourceLocation Loc,
13738                               QualType LHS, Expr *RHS) {
13739   Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime();
13740 
13741   if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone)
13742     return false;
13743 
13744   if (checkUnsafeAssignObject(*this, Loc, LT, RHS, false))
13745     return true;
13746 
13747   return false;
13748 }
13749 
13750 void Sema::checkUnsafeExprAssigns(SourceLocation Loc,
13751                               Expr *LHS, Expr *RHS) {
13752   QualType LHSType;
13753   // PropertyRef on LHS type need be directly obtained from
13754   // its declaration as it has a PseudoType.
13755   ObjCPropertyRefExpr *PRE
13756     = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens());
13757   if (PRE && !PRE->isImplicitProperty()) {
13758     const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
13759     if (PD)
13760       LHSType = PD->getType();
13761   }
13762 
13763   if (LHSType.isNull())
13764     LHSType = LHS->getType();
13765 
13766   Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime();
13767 
13768   if (LT == Qualifiers::OCL_Weak) {
13769     if (!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, Loc))
13770       getCurFunction()->markSafeWeakUse(LHS);
13771   }
13772 
13773   if (checkUnsafeAssigns(Loc, LHSType, RHS))
13774     return;
13775 
13776   // FIXME. Check for other life times.
13777   if (LT != Qualifiers::OCL_None)
13778     return;
13779 
13780   if (PRE) {
13781     if (PRE->isImplicitProperty())
13782       return;
13783     const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
13784     if (!PD)
13785       return;
13786 
13787     unsigned Attributes = PD->getPropertyAttributes();
13788     if (Attributes & ObjCPropertyAttribute::kind_assign) {
13789       // when 'assign' attribute was not explicitly specified
13790       // by user, ignore it and rely on property type itself
13791       // for lifetime info.
13792       unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten();
13793       if (!(AsWrittenAttr & ObjCPropertyAttribute::kind_assign) &&
13794           LHSType->isObjCRetainableType())
13795         return;
13796 
13797       while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
13798         if (cast->getCastKind() == CK_ARCConsumeObject) {
13799           Diag(Loc, diag::warn_arc_retained_property_assign)
13800           << RHS->getSourceRange();
13801           return;
13802         }
13803         RHS = cast->getSubExpr();
13804       }
13805     } else if (Attributes & ObjCPropertyAttribute::kind_weak) {
13806       if (checkUnsafeAssignObject(*this, Loc, Qualifiers::OCL_Weak, RHS, true))
13807         return;
13808     }
13809   }
13810 }
13811 
13812 //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===//
13813 
13814 static bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr,
13815                                         SourceLocation StmtLoc,
13816                                         const NullStmt *Body) {
13817   // Do not warn if the body is a macro that expands to nothing, e.g:
13818   //
13819   // #define CALL(x)
13820   // if (condition)
13821   //   CALL(0);
13822   if (Body->hasLeadingEmptyMacro())
13823     return false;
13824 
13825   // Get line numbers of statement and body.
13826   bool StmtLineInvalid;
13827   unsigned StmtLine = SourceMgr.getPresumedLineNumber(StmtLoc,
13828                                                       &StmtLineInvalid);
13829   if (StmtLineInvalid)
13830     return false;
13831 
13832   bool BodyLineInvalid;
13833   unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(),
13834                                                       &BodyLineInvalid);
13835   if (BodyLineInvalid)
13836     return false;
13837 
13838   // Warn if null statement and body are on the same line.
13839   if (StmtLine != BodyLine)
13840     return false;
13841 
13842   return true;
13843 }
13844 
13845 void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc,
13846                                  const Stmt *Body,
13847                                  unsigned DiagID) {
13848   // Since this is a syntactic check, don't emit diagnostic for template
13849   // instantiations, this just adds noise.
13850   if (CurrentInstantiationScope)
13851     return;
13852 
13853   // The body should be a null statement.
13854   const NullStmt *NBody = dyn_cast<NullStmt>(Body);
13855   if (!NBody)
13856     return;
13857 
13858   // Do the usual checks.
13859   if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody))
13860     return;
13861 
13862   Diag(NBody->getSemiLoc(), DiagID);
13863   Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line);
13864 }
13865 
13866 void Sema::DiagnoseEmptyLoopBody(const Stmt *S,
13867                                  const Stmt *PossibleBody) {
13868   assert(!CurrentInstantiationScope); // Ensured by caller
13869 
13870   SourceLocation StmtLoc;
13871   const Stmt *Body;
13872   unsigned DiagID;
13873   if (const ForStmt *FS = dyn_cast<ForStmt>(S)) {
13874     StmtLoc = FS->getRParenLoc();
13875     Body = FS->getBody();
13876     DiagID = diag::warn_empty_for_body;
13877   } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) {
13878     StmtLoc = WS->getRParenLoc();
13879     Body = WS->getBody();
13880     DiagID = diag::warn_empty_while_body;
13881   } else
13882     return; // Neither `for' nor `while'.
13883 
13884   // The body should be a null statement.
13885   const NullStmt *NBody = dyn_cast<NullStmt>(Body);
13886   if (!NBody)
13887     return;
13888 
13889   // Skip expensive checks if diagnostic is disabled.
13890   if (Diags.isIgnored(DiagID, NBody->getSemiLoc()))
13891     return;
13892 
13893   // Do the usual checks.
13894   if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody))
13895     return;
13896 
13897   // `for(...);' and `while(...);' are popular idioms, so in order to keep
13898   // noise level low, emit diagnostics only if for/while is followed by a
13899   // CompoundStmt, e.g.:
13900   //    for (int i = 0; i < n; i++);
13901   //    {
13902   //      a(i);
13903   //    }
13904   // or if for/while is followed by a statement with more indentation
13905   // than for/while itself:
13906   //    for (int i = 0; i < n; i++);
13907   //      a(i);
13908   bool ProbableTypo = isa<CompoundStmt>(PossibleBody);
13909   if (!ProbableTypo) {
13910     bool BodyColInvalid;
13911     unsigned BodyCol = SourceMgr.getPresumedColumnNumber(
13912         PossibleBody->getBeginLoc(), &BodyColInvalid);
13913     if (BodyColInvalid)
13914       return;
13915 
13916     bool StmtColInvalid;
13917     unsigned StmtCol =
13918         SourceMgr.getPresumedColumnNumber(S->getBeginLoc(), &StmtColInvalid);
13919     if (StmtColInvalid)
13920       return;
13921 
13922     if (BodyCol > StmtCol)
13923       ProbableTypo = true;
13924   }
13925 
13926   if (ProbableTypo) {
13927     Diag(NBody->getSemiLoc(), DiagID);
13928     Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line);
13929   }
13930 }
13931 
13932 //===--- CHECK: Warn on self move with std::move. -------------------------===//
13933 
13934 void Sema::DiagnoseSelfMove(const Expr *LHSExpr, const Expr *RHSExpr,
13935                              SourceLocation OpLoc) {
13936   if (Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, OpLoc))
13937     return;
13938 
13939   if (inTemplateInstantiation())
13940     return;
13941 
13942   // Strip parens and casts away.
13943   LHSExpr = LHSExpr->IgnoreParenImpCasts();
13944   RHSExpr = RHSExpr->IgnoreParenImpCasts();
13945 
13946   // Check for a call to std::move or for a static_cast<T&&>(..) to an xvalue
13947   // which we can treat as an inlined std::move
13948   if (const auto *CE = dyn_cast<CallExpr>(RHSExpr);
13949       CE && CE->getNumArgs() == 1 && CE->isCallToStdMove())
13950     RHSExpr = CE->getArg(0);
13951   else if (const auto *CXXSCE = dyn_cast<CXXStaticCastExpr>(RHSExpr);
13952            CXXSCE && CXXSCE->isXValue())
13953     RHSExpr = CXXSCE->getSubExpr();
13954   else
13955     return;
13956 
13957   const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr);
13958   const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr);
13959 
13960   // Two DeclRefExpr's, check that the decls are the same.
13961   if (LHSDeclRef && RHSDeclRef) {
13962     if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl())
13963       return;
13964     if (LHSDeclRef->getDecl()->getCanonicalDecl() !=
13965         RHSDeclRef->getDecl()->getCanonicalDecl())
13966       return;
13967 
13968     auto D = Diag(OpLoc, diag::warn_self_move)
13969              << LHSExpr->getType() << LHSExpr->getSourceRange()
13970              << RHSExpr->getSourceRange();
13971     if (const FieldDecl *F =
13972             getSelfAssignmentClassMemberCandidate(RHSDeclRef->getDecl()))
13973       D << 1 << F
13974         << FixItHint::CreateInsertion(LHSDeclRef->getBeginLoc(), "this->");
13975     else
13976       D << 0;
13977     return;
13978   }
13979 
13980   // Member variables require a different approach to check for self moves.
13981   // MemberExpr's are the same if every nested MemberExpr refers to the same
13982   // Decl and that the base Expr's are DeclRefExpr's with the same Decl or
13983   // the base Expr's are CXXThisExpr's.
13984   const Expr *LHSBase = LHSExpr;
13985   const Expr *RHSBase = RHSExpr;
13986   const MemberExpr *LHSME = dyn_cast<MemberExpr>(LHSExpr);
13987   const MemberExpr *RHSME = dyn_cast<MemberExpr>(RHSExpr);
13988   if (!LHSME || !RHSME)
13989     return;
13990 
13991   while (LHSME && RHSME) {
13992     if (LHSME->getMemberDecl()->getCanonicalDecl() !=
13993         RHSME->getMemberDecl()->getCanonicalDecl())
13994       return;
13995 
13996     LHSBase = LHSME->getBase();
13997     RHSBase = RHSME->getBase();
13998     LHSME = dyn_cast<MemberExpr>(LHSBase);
13999     RHSME = dyn_cast<MemberExpr>(RHSBase);
14000   }
14001 
14002   LHSDeclRef = dyn_cast<DeclRefExpr>(LHSBase);
14003   RHSDeclRef = dyn_cast<DeclRefExpr>(RHSBase);
14004   if (LHSDeclRef && RHSDeclRef) {
14005     if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl())
14006       return;
14007     if (LHSDeclRef->getDecl()->getCanonicalDecl() !=
14008         RHSDeclRef->getDecl()->getCanonicalDecl())
14009       return;
14010 
14011     Diag(OpLoc, diag::warn_self_move)
14012         << LHSExpr->getType() << 0 << LHSExpr->getSourceRange()
14013         << RHSExpr->getSourceRange();
14014     return;
14015   }
14016 
14017   if (isa<CXXThisExpr>(LHSBase) && isa<CXXThisExpr>(RHSBase))
14018     Diag(OpLoc, diag::warn_self_move)
14019         << LHSExpr->getType() << 0 << LHSExpr->getSourceRange()
14020         << RHSExpr->getSourceRange();
14021 }
14022 
14023 //===--- Layout compatibility ----------------------------------------------//
14024 
14025 static bool isLayoutCompatible(const ASTContext &C, QualType T1, QualType T2);
14026 
14027 /// Check if two enumeration types are layout-compatible.
14028 static bool isLayoutCompatible(const ASTContext &C, const EnumDecl *ED1,
14029                                const EnumDecl *ED2) {
14030   // C++11 [dcl.enum] p8:
14031   // Two enumeration types are layout-compatible if they have the same
14032   // underlying type.
14033   return ED1->isComplete() && ED2->isComplete() &&
14034          C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType());
14035 }
14036 
14037 /// Check if two fields are layout-compatible.
14038 /// Can be used on union members, which are exempt from alignment requirement
14039 /// of common initial sequence.
14040 static bool isLayoutCompatible(const ASTContext &C, const FieldDecl *Field1,
14041                                const FieldDecl *Field2,
14042                                bool AreUnionMembers = false) {
14043   [[maybe_unused]] const Type *Field1Parent =
14044       Field1->getParent()->getTypeForDecl();
14045   [[maybe_unused]] const Type *Field2Parent =
14046       Field2->getParent()->getTypeForDecl();
14047   assert(((Field1Parent->isStructureOrClassType() &&
14048            Field2Parent->isStructureOrClassType()) ||
14049           (Field1Parent->isUnionType() && Field2Parent->isUnionType())) &&
14050          "Can't evaluate layout compatibility between a struct field and a "
14051          "union field.");
14052   assert(((!AreUnionMembers && Field1Parent->isStructureOrClassType()) ||
14053           (AreUnionMembers && Field1Parent->isUnionType())) &&
14054          "AreUnionMembers should be 'true' for union fields (only).");
14055 
14056   if (!isLayoutCompatible(C, Field1->getType(), Field2->getType()))
14057     return false;
14058 
14059   if (Field1->isBitField() != Field2->isBitField())
14060     return false;
14061 
14062   if (Field1->isBitField()) {
14063     // Make sure that the bit-fields are the same length.
14064     unsigned Bits1 = Field1->getBitWidthValue();
14065     unsigned Bits2 = Field2->getBitWidthValue();
14066 
14067     if (Bits1 != Bits2)
14068       return false;
14069   }
14070 
14071   if (Field1->hasAttr<clang::NoUniqueAddressAttr>() ||
14072       Field2->hasAttr<clang::NoUniqueAddressAttr>())
14073     return false;
14074 
14075   if (!AreUnionMembers &&
14076       Field1->getMaxAlignment() != Field2->getMaxAlignment())
14077     return false;
14078 
14079   return true;
14080 }
14081 
14082 /// Check if two standard-layout structs are layout-compatible.
14083 /// (C++11 [class.mem] p17)
14084 static bool isLayoutCompatibleStruct(const ASTContext &C, const RecordDecl *RD1,
14085                                      const RecordDecl *RD2) {
14086   // Get to the class where the fields are declared
14087   if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1))
14088     RD1 = D1CXX->getStandardLayoutBaseWithFields();
14089 
14090   if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2))
14091     RD2 = D2CXX->getStandardLayoutBaseWithFields();
14092 
14093   // Check the fields.
14094   return llvm::equal(RD1->fields(), RD2->fields(),
14095                      [&C](const FieldDecl *F1, const FieldDecl *F2) -> bool {
14096                        return isLayoutCompatible(C, F1, F2);
14097                      });
14098 }
14099 
14100 /// Check if two standard-layout unions are layout-compatible.
14101 /// (C++11 [class.mem] p18)
14102 static bool isLayoutCompatibleUnion(const ASTContext &C, const RecordDecl *RD1,
14103                                     const RecordDecl *RD2) {
14104   llvm::SmallPtrSet<const FieldDecl *, 8> UnmatchedFields;
14105   for (auto *Field2 : RD2->fields())
14106     UnmatchedFields.insert(Field2);
14107 
14108   for (auto *Field1 : RD1->fields()) {
14109     auto I = UnmatchedFields.begin();
14110     auto E = UnmatchedFields.end();
14111 
14112     for ( ; I != E; ++I) {
14113       if (isLayoutCompatible(C, Field1, *I, /*IsUnionMember=*/true)) {
14114         bool Result = UnmatchedFields.erase(*I);
14115         (void) Result;
14116         assert(Result);
14117         break;
14118       }
14119     }
14120     if (I == E)
14121       return false;
14122   }
14123 
14124   return UnmatchedFields.empty();
14125 }
14126 
14127 static bool isLayoutCompatible(const ASTContext &C, const RecordDecl *RD1,
14128                                const RecordDecl *RD2) {
14129   if (RD1->isUnion() != RD2->isUnion())
14130     return false;
14131 
14132   if (RD1->isUnion())
14133     return isLayoutCompatibleUnion(C, RD1, RD2);
14134   else
14135     return isLayoutCompatibleStruct(C, RD1, RD2);
14136 }
14137 
14138 /// Check if two types are layout-compatible in C++11 sense.
14139 static bool isLayoutCompatible(const ASTContext &C, QualType T1, QualType T2) {
14140   if (T1.isNull() || T2.isNull())
14141     return false;
14142 
14143   // C++20 [basic.types] p11:
14144   // Two types cv1 T1 and cv2 T2 are layout-compatible types
14145   // if T1 and T2 are the same type, layout-compatible enumerations (9.7.1),
14146   // or layout-compatible standard-layout class types (11.4).
14147   T1 = T1.getCanonicalType().getUnqualifiedType();
14148   T2 = T2.getCanonicalType().getUnqualifiedType();
14149 
14150   if (C.hasSameType(T1, T2))
14151     return true;
14152 
14153   const Type::TypeClass TC1 = T1->getTypeClass();
14154   const Type::TypeClass TC2 = T2->getTypeClass();
14155 
14156   if (TC1 != TC2)
14157     return false;
14158 
14159   if (TC1 == Type::Enum) {
14160     return isLayoutCompatible(C,
14161                               cast<EnumType>(T1)->getDecl(),
14162                               cast<EnumType>(T2)->getDecl());
14163   } else if (TC1 == Type::Record) {
14164     if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType())
14165       return false;
14166 
14167     return isLayoutCompatible(C,
14168                               cast<RecordType>(T1)->getDecl(),
14169                               cast<RecordType>(T2)->getDecl());
14170   }
14171 
14172   return false;
14173 }
14174 
14175 bool Sema::IsLayoutCompatible(QualType T1, QualType T2) const {
14176   return isLayoutCompatible(getASTContext(), T1, T2);
14177 }
14178 
14179 //===-------------- Pointer interconvertibility ----------------------------//
14180 
14181 bool Sema::IsPointerInterconvertibleBaseOf(const TypeSourceInfo *Base,
14182                                            const TypeSourceInfo *Derived) {
14183   QualType BaseT = Base->getType()->getCanonicalTypeUnqualified();
14184   QualType DerivedT = Derived->getType()->getCanonicalTypeUnqualified();
14185 
14186   if (BaseT->isStructureOrClassType() && DerivedT->isStructureOrClassType() &&
14187       getASTContext().hasSameType(BaseT, DerivedT))
14188     return true;
14189 
14190   if (!IsDerivedFrom(Derived->getTypeLoc().getBeginLoc(), DerivedT, BaseT))
14191     return false;
14192 
14193   // Per [basic.compound]/4.3, containing object has to be standard-layout.
14194   if (DerivedT->getAsCXXRecordDecl()->isStandardLayout())
14195     return true;
14196 
14197   return false;
14198 }
14199 
14200 //===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----//
14201 
14202 /// Given a type tag expression find the type tag itself.
14203 ///
14204 /// \param TypeExpr Type tag expression, as it appears in user's code.
14205 ///
14206 /// \param VD Declaration of an identifier that appears in a type tag.
14207 ///
14208 /// \param MagicValue Type tag magic value.
14209 ///
14210 /// \param isConstantEvaluated whether the evalaution should be performed in
14211 
14212 /// constant context.
14213 static bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx,
14214                             const ValueDecl **VD, uint64_t *MagicValue,
14215                             bool isConstantEvaluated) {
14216   while(true) {
14217     if (!TypeExpr)
14218       return false;
14219 
14220     TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts();
14221 
14222     switch (TypeExpr->getStmtClass()) {
14223     case Stmt::UnaryOperatorClass: {
14224       const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr);
14225       if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) {
14226         TypeExpr = UO->getSubExpr();
14227         continue;
14228       }
14229       return false;
14230     }
14231 
14232     case Stmt::DeclRefExprClass: {
14233       const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr);
14234       *VD = DRE->getDecl();
14235       return true;
14236     }
14237 
14238     case Stmt::IntegerLiteralClass: {
14239       const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr);
14240       llvm::APInt MagicValueAPInt = IL->getValue();
14241       if (MagicValueAPInt.getActiveBits() <= 64) {
14242         *MagicValue = MagicValueAPInt.getZExtValue();
14243         return true;
14244       } else
14245         return false;
14246     }
14247 
14248     case Stmt::BinaryConditionalOperatorClass:
14249     case Stmt::ConditionalOperatorClass: {
14250       const AbstractConditionalOperator *ACO =
14251           cast<AbstractConditionalOperator>(TypeExpr);
14252       bool Result;
14253       if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx,
14254                                                      isConstantEvaluated)) {
14255         if (Result)
14256           TypeExpr = ACO->getTrueExpr();
14257         else
14258           TypeExpr = ACO->getFalseExpr();
14259         continue;
14260       }
14261       return false;
14262     }
14263 
14264     case Stmt::BinaryOperatorClass: {
14265       const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr);
14266       if (BO->getOpcode() == BO_Comma) {
14267         TypeExpr = BO->getRHS();
14268         continue;
14269       }
14270       return false;
14271     }
14272 
14273     default:
14274       return false;
14275     }
14276   }
14277 }
14278 
14279 /// Retrieve the C type corresponding to type tag TypeExpr.
14280 ///
14281 /// \param TypeExpr Expression that specifies a type tag.
14282 ///
14283 /// \param MagicValues Registered magic values.
14284 ///
14285 /// \param FoundWrongKind Set to true if a type tag was found, but of a wrong
14286 ///        kind.
14287 ///
14288 /// \param TypeInfo Information about the corresponding C type.
14289 ///
14290 /// \param isConstantEvaluated whether the evalaution should be performed in
14291 /// constant context.
14292 ///
14293 /// \returns true if the corresponding C type was found.
14294 static bool GetMatchingCType(
14295     const IdentifierInfo *ArgumentKind, const Expr *TypeExpr,
14296     const ASTContext &Ctx,
14297     const llvm::DenseMap<Sema::TypeTagMagicValue, Sema::TypeTagData>
14298         *MagicValues,
14299     bool &FoundWrongKind, Sema::TypeTagData &TypeInfo,
14300     bool isConstantEvaluated) {
14301   FoundWrongKind = false;
14302 
14303   // Variable declaration that has type_tag_for_datatype attribute.
14304   const ValueDecl *VD = nullptr;
14305 
14306   uint64_t MagicValue;
14307 
14308   if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue, isConstantEvaluated))
14309     return false;
14310 
14311   if (VD) {
14312     if (TypeTagForDatatypeAttr *I = VD->getAttr<TypeTagForDatatypeAttr>()) {
14313       if (I->getArgumentKind() != ArgumentKind) {
14314         FoundWrongKind = true;
14315         return false;
14316       }
14317       TypeInfo.Type = I->getMatchingCType();
14318       TypeInfo.LayoutCompatible = I->getLayoutCompatible();
14319       TypeInfo.MustBeNull = I->getMustBeNull();
14320       return true;
14321     }
14322     return false;
14323   }
14324 
14325   if (!MagicValues)
14326     return false;
14327 
14328   llvm::DenseMap<Sema::TypeTagMagicValue,
14329                  Sema::TypeTagData>::const_iterator I =
14330       MagicValues->find(std::make_pair(ArgumentKind, MagicValue));
14331   if (I == MagicValues->end())
14332     return false;
14333 
14334   TypeInfo = I->second;
14335   return true;
14336 }
14337 
14338 void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind,
14339                                       uint64_t MagicValue, QualType Type,
14340                                       bool LayoutCompatible,
14341                                       bool MustBeNull) {
14342   if (!TypeTagForDatatypeMagicValues)
14343     TypeTagForDatatypeMagicValues.reset(
14344         new llvm::DenseMap<TypeTagMagicValue, TypeTagData>);
14345 
14346   TypeTagMagicValue Magic(ArgumentKind, MagicValue);
14347   (*TypeTagForDatatypeMagicValues)[Magic] =
14348       TypeTagData(Type, LayoutCompatible, MustBeNull);
14349 }
14350 
14351 static bool IsSameCharType(QualType T1, QualType T2) {
14352   const BuiltinType *BT1 = T1->getAs<BuiltinType>();
14353   if (!BT1)
14354     return false;
14355 
14356   const BuiltinType *BT2 = T2->getAs<BuiltinType>();
14357   if (!BT2)
14358     return false;
14359 
14360   BuiltinType::Kind T1Kind = BT1->getKind();
14361   BuiltinType::Kind T2Kind = BT2->getKind();
14362 
14363   return (T1Kind == BuiltinType::SChar  && T2Kind == BuiltinType::Char_S) ||
14364          (T1Kind == BuiltinType::UChar  && T2Kind == BuiltinType::Char_U) ||
14365          (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) ||
14366          (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar);
14367 }
14368 
14369 void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr,
14370                                     const ArrayRef<const Expr *> ExprArgs,
14371                                     SourceLocation CallSiteLoc) {
14372   const IdentifierInfo *ArgumentKind = Attr->getArgumentKind();
14373   bool IsPointerAttr = Attr->getIsPointer();
14374 
14375   // Retrieve the argument representing the 'type_tag'.
14376   unsigned TypeTagIdxAST = Attr->getTypeTagIdx().getASTIndex();
14377   if (TypeTagIdxAST >= ExprArgs.size()) {
14378     Diag(CallSiteLoc, diag::err_tag_index_out_of_range)
14379         << 0 << Attr->getTypeTagIdx().getSourceIndex();
14380     return;
14381   }
14382   const Expr *TypeTagExpr = ExprArgs[TypeTagIdxAST];
14383   bool FoundWrongKind;
14384   TypeTagData TypeInfo;
14385   if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context,
14386                         TypeTagForDatatypeMagicValues.get(), FoundWrongKind,
14387                         TypeInfo, isConstantEvaluatedContext())) {
14388     if (FoundWrongKind)
14389       Diag(TypeTagExpr->getExprLoc(),
14390            diag::warn_type_tag_for_datatype_wrong_kind)
14391         << TypeTagExpr->getSourceRange();
14392     return;
14393   }
14394 
14395   // Retrieve the argument representing the 'arg_idx'.
14396   unsigned ArgumentIdxAST = Attr->getArgumentIdx().getASTIndex();
14397   if (ArgumentIdxAST >= ExprArgs.size()) {
14398     Diag(CallSiteLoc, diag::err_tag_index_out_of_range)
14399         << 1 << Attr->getArgumentIdx().getSourceIndex();
14400     return;
14401   }
14402   const Expr *ArgumentExpr = ExprArgs[ArgumentIdxAST];
14403   if (IsPointerAttr) {
14404     // Skip implicit cast of pointer to `void *' (as a function argument).
14405     if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr))
14406       if (ICE->getType()->isVoidPointerType() &&
14407           ICE->getCastKind() == CK_BitCast)
14408         ArgumentExpr = ICE->getSubExpr();
14409   }
14410   QualType ArgumentType = ArgumentExpr->getType();
14411 
14412   // Passing a `void*' pointer shouldn't trigger a warning.
14413   if (IsPointerAttr && ArgumentType->isVoidPointerType())
14414     return;
14415 
14416   if (TypeInfo.MustBeNull) {
14417     // Type tag with matching void type requires a null pointer.
14418     if (!ArgumentExpr->isNullPointerConstant(Context,
14419                                              Expr::NPC_ValueDependentIsNotNull)) {
14420       Diag(ArgumentExpr->getExprLoc(),
14421            diag::warn_type_safety_null_pointer_required)
14422           << ArgumentKind->getName()
14423           << ArgumentExpr->getSourceRange()
14424           << TypeTagExpr->getSourceRange();
14425     }
14426     return;
14427   }
14428 
14429   QualType RequiredType = TypeInfo.Type;
14430   if (IsPointerAttr)
14431     RequiredType = Context.getPointerType(RequiredType);
14432 
14433   bool mismatch = false;
14434   if (!TypeInfo.LayoutCompatible) {
14435     mismatch = !Context.hasSameType(ArgumentType, RequiredType);
14436 
14437     // C++11 [basic.fundamental] p1:
14438     // Plain char, signed char, and unsigned char are three distinct types.
14439     //
14440     // But we treat plain `char' as equivalent to `signed char' or `unsigned
14441     // char' depending on the current char signedness mode.
14442     if (mismatch)
14443       if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(),
14444                                            RequiredType->getPointeeType())) ||
14445           (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType)))
14446         mismatch = false;
14447   } else
14448     if (IsPointerAttr)
14449       mismatch = !isLayoutCompatible(Context,
14450                                      ArgumentType->getPointeeType(),
14451                                      RequiredType->getPointeeType());
14452     else
14453       mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType);
14454 
14455   if (mismatch)
14456     Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch)
14457         << ArgumentType << ArgumentKind
14458         << TypeInfo.LayoutCompatible << RequiredType
14459         << ArgumentExpr->getSourceRange()
14460         << TypeTagExpr->getSourceRange();
14461 }
14462 
14463 void Sema::AddPotentialMisalignedMembers(Expr *E, RecordDecl *RD, ValueDecl *MD,
14464                                          CharUnits Alignment) {
14465   MisalignedMembers.emplace_back(E, RD, MD, Alignment);
14466 }
14467 
14468 void Sema::DiagnoseMisalignedMembers() {
14469   for (MisalignedMember &m : MisalignedMembers) {
14470     const NamedDecl *ND = m.RD;
14471     if (ND->getName().empty()) {
14472       if (const TypedefNameDecl *TD = m.RD->getTypedefNameForAnonDecl())
14473         ND = TD;
14474     }
14475     Diag(m.E->getBeginLoc(), diag::warn_taking_address_of_packed_member)
14476         << m.MD << ND << m.E->getSourceRange();
14477   }
14478   MisalignedMembers.clear();
14479 }
14480 
14481 void Sema::DiscardMisalignedMemberAddress(const Type *T, Expr *E) {
14482   E = E->IgnoreParens();
14483   if (!T->isPointerType() && !T->isIntegerType() && !T->isDependentType())
14484     return;
14485   if (isa<UnaryOperator>(E) &&
14486       cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf) {
14487     auto *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
14488     if (isa<MemberExpr>(Op)) {
14489       auto *MA = llvm::find(MisalignedMembers, MisalignedMember(Op));
14490       if (MA != MisalignedMembers.end() &&
14491           (T->isDependentType() || T->isIntegerType() ||
14492            (T->isPointerType() && (T->getPointeeType()->isIncompleteType() ||
14493                                    Context.getTypeAlignInChars(
14494                                        T->getPointeeType()) <= MA->Alignment))))
14495         MisalignedMembers.erase(MA);
14496     }
14497   }
14498 }
14499 
14500 void Sema::RefersToMemberWithReducedAlignment(
14501     Expr *E,
14502     llvm::function_ref<void(Expr *, RecordDecl *, FieldDecl *, CharUnits)>
14503         Action) {
14504   const auto *ME = dyn_cast<MemberExpr>(E);
14505   if (!ME)
14506     return;
14507 
14508   // No need to check expressions with an __unaligned-qualified type.
14509   if (E->getType().getQualifiers().hasUnaligned())
14510     return;
14511 
14512   // For a chain of MemberExpr like "a.b.c.d" this list
14513   // will keep FieldDecl's like [d, c, b].
14514   SmallVector<FieldDecl *, 4> ReverseMemberChain;
14515   const MemberExpr *TopME = nullptr;
14516   bool AnyIsPacked = false;
14517   do {
14518     QualType BaseType = ME->getBase()->getType();
14519     if (BaseType->isDependentType())
14520       return;
14521     if (ME->isArrow())
14522       BaseType = BaseType->getPointeeType();
14523     RecordDecl *RD = BaseType->castAs<RecordType>()->getDecl();
14524     if (RD->isInvalidDecl())
14525       return;
14526 
14527     ValueDecl *MD = ME->getMemberDecl();
14528     auto *FD = dyn_cast<FieldDecl>(MD);
14529     // We do not care about non-data members.
14530     if (!FD || FD->isInvalidDecl())
14531       return;
14532 
14533     AnyIsPacked =
14534         AnyIsPacked || (RD->hasAttr<PackedAttr>() || MD->hasAttr<PackedAttr>());
14535     ReverseMemberChain.push_back(FD);
14536 
14537     TopME = ME;
14538     ME = dyn_cast<MemberExpr>(ME->getBase()->IgnoreParens());
14539   } while (ME);
14540   assert(TopME && "We did not compute a topmost MemberExpr!");
14541 
14542   // Not the scope of this diagnostic.
14543   if (!AnyIsPacked)
14544     return;
14545 
14546   const Expr *TopBase = TopME->getBase()->IgnoreParenImpCasts();
14547   const auto *DRE = dyn_cast<DeclRefExpr>(TopBase);
14548   // TODO: The innermost base of the member expression may be too complicated.
14549   // For now, just disregard these cases. This is left for future
14550   // improvement.
14551   if (!DRE && !isa<CXXThisExpr>(TopBase))
14552       return;
14553 
14554   // Alignment expected by the whole expression.
14555   CharUnits ExpectedAlignment = Context.getTypeAlignInChars(E->getType());
14556 
14557   // No need to do anything else with this case.
14558   if (ExpectedAlignment.isOne())
14559     return;
14560 
14561   // Synthesize offset of the whole access.
14562   CharUnits Offset;
14563   for (const FieldDecl *FD : llvm::reverse(ReverseMemberChain))
14564     Offset += Context.toCharUnitsFromBits(Context.getFieldOffset(FD));
14565 
14566   // Compute the CompleteObjectAlignment as the alignment of the whole chain.
14567   CharUnits CompleteObjectAlignment = Context.getTypeAlignInChars(
14568       ReverseMemberChain.back()->getParent()->getTypeForDecl());
14569 
14570   // The base expression of the innermost MemberExpr may give
14571   // stronger guarantees than the class containing the member.
14572   if (DRE && !TopME->isArrow()) {
14573     const ValueDecl *VD = DRE->getDecl();
14574     if (!VD->getType()->isReferenceType())
14575       CompleteObjectAlignment =
14576           std::max(CompleteObjectAlignment, Context.getDeclAlign(VD));
14577   }
14578 
14579   // Check if the synthesized offset fulfills the alignment.
14580   if (Offset % ExpectedAlignment != 0 ||
14581       // It may fulfill the offset it but the effective alignment may still be
14582       // lower than the expected expression alignment.
14583       CompleteObjectAlignment < ExpectedAlignment) {
14584     // If this happens, we want to determine a sensible culprit of this.
14585     // Intuitively, watching the chain of member expressions from right to
14586     // left, we start with the required alignment (as required by the field
14587     // type) but some packed attribute in that chain has reduced the alignment.
14588     // It may happen that another packed structure increases it again. But if
14589     // we are here such increase has not been enough. So pointing the first
14590     // FieldDecl that either is packed or else its RecordDecl is,
14591     // seems reasonable.
14592     FieldDecl *FD = nullptr;
14593     CharUnits Alignment;
14594     for (FieldDecl *FDI : ReverseMemberChain) {
14595       if (FDI->hasAttr<PackedAttr>() ||
14596           FDI->getParent()->hasAttr<PackedAttr>()) {
14597         FD = FDI;
14598         Alignment = std::min(
14599             Context.getTypeAlignInChars(FD->getType()),
14600             Context.getTypeAlignInChars(FD->getParent()->getTypeForDecl()));
14601         break;
14602       }
14603     }
14604     assert(FD && "We did not find a packed FieldDecl!");
14605     Action(E, FD->getParent(), FD, Alignment);
14606   }
14607 }
14608 
14609 void Sema::CheckAddressOfPackedMember(Expr *rhs) {
14610   using namespace std::placeholders;
14611 
14612   RefersToMemberWithReducedAlignment(
14613       rhs, std::bind(&Sema::AddPotentialMisalignedMembers, std::ref(*this), _1,
14614                      _2, _3, _4));
14615 }
14616 
14617 bool Sema::PrepareBuiltinElementwiseMathOneArgCall(CallExpr *TheCall) {
14618   if (checkArgCount(TheCall, 1))
14619     return true;
14620 
14621   ExprResult A = UsualUnaryConversions(TheCall->getArg(0));
14622   if (A.isInvalid())
14623     return true;
14624 
14625   TheCall->setArg(0, A.get());
14626   QualType TyA = A.get()->getType();
14627 
14628   if (checkMathBuiltinElementType(*this, A.get()->getBeginLoc(), TyA, 1))
14629     return true;
14630 
14631   TheCall->setType(TyA);
14632   return false;
14633 }
14634 
14635 bool Sema::BuiltinElementwiseMath(CallExpr *TheCall, bool FPOnly) {
14636   QualType Res;
14637   if (BuiltinVectorMath(TheCall, Res, FPOnly))
14638     return true;
14639   TheCall->setType(Res);
14640   return false;
14641 }
14642 
14643 bool Sema::BuiltinVectorToScalarMath(CallExpr *TheCall) {
14644   QualType Res;
14645   if (BuiltinVectorMath(TheCall, Res))
14646     return true;
14647 
14648   if (auto *VecTy0 = Res->getAs<VectorType>())
14649     TheCall->setType(VecTy0->getElementType());
14650   else
14651     TheCall->setType(Res);
14652 
14653   return false;
14654 }
14655 
14656 bool Sema::BuiltinVectorMath(CallExpr *TheCall, QualType &Res, bool FPOnly) {
14657   if (checkArgCount(TheCall, 2))
14658     return true;
14659 
14660   ExprResult A = TheCall->getArg(0);
14661   ExprResult B = TheCall->getArg(1);
14662   // Do standard promotions between the two arguments, returning their common
14663   // type.
14664   Res = UsualArithmeticConversions(A, B, TheCall->getExprLoc(), ACK_Comparison);
14665   if (A.isInvalid() || B.isInvalid())
14666     return true;
14667 
14668   QualType TyA = A.get()->getType();
14669   QualType TyB = B.get()->getType();
14670 
14671   if (Res.isNull() || TyA.getCanonicalType() != TyB.getCanonicalType())
14672     return Diag(A.get()->getBeginLoc(),
14673                 diag::err_typecheck_call_different_arg_types)
14674            << TyA << TyB;
14675 
14676   if (FPOnly) {
14677     if (checkFPMathBuiltinElementType(*this, A.get()->getBeginLoc(), TyA, 1))
14678       return true;
14679   } else {
14680     if (checkMathBuiltinElementType(*this, A.get()->getBeginLoc(), TyA, 1))
14681       return true;
14682   }
14683 
14684   TheCall->setArg(0, A.get());
14685   TheCall->setArg(1, B.get());
14686   return false;
14687 }
14688 
14689 bool Sema::BuiltinElementwiseTernaryMath(CallExpr *TheCall,
14690                                          bool CheckForFloatArgs) {
14691   if (checkArgCount(TheCall, 3))
14692     return true;
14693 
14694   Expr *Args[3];
14695   for (int I = 0; I < 3; ++I) {
14696     ExprResult Converted = UsualUnaryConversions(TheCall->getArg(I));
14697     if (Converted.isInvalid())
14698       return true;
14699     Args[I] = Converted.get();
14700   }
14701 
14702   if (CheckForFloatArgs) {
14703     int ArgOrdinal = 1;
14704     for (Expr *Arg : Args) {
14705       if (checkFPMathBuiltinElementType(*this, Arg->getBeginLoc(),
14706                                         Arg->getType(), ArgOrdinal++))
14707         return true;
14708     }
14709   } else {
14710     int ArgOrdinal = 1;
14711     for (Expr *Arg : Args) {
14712       if (checkMathBuiltinElementType(*this, Arg->getBeginLoc(), Arg->getType(),
14713                                       ArgOrdinal++))
14714         return true;
14715     }
14716   }
14717 
14718   for (int I = 1; I < 3; ++I) {
14719     if (Args[0]->getType().getCanonicalType() !=
14720         Args[I]->getType().getCanonicalType()) {
14721       return Diag(Args[0]->getBeginLoc(),
14722                   diag::err_typecheck_call_different_arg_types)
14723              << Args[0]->getType() << Args[I]->getType();
14724     }
14725 
14726     TheCall->setArg(I, Args[I]);
14727   }
14728 
14729   TheCall->setType(Args[0]->getType());
14730   return false;
14731 }
14732 
14733 bool Sema::PrepareBuiltinReduceMathOneArgCall(CallExpr *TheCall) {
14734   if (checkArgCount(TheCall, 1))
14735     return true;
14736 
14737   ExprResult A = UsualUnaryConversions(TheCall->getArg(0));
14738   if (A.isInvalid())
14739     return true;
14740 
14741   TheCall->setArg(0, A.get());
14742   return false;
14743 }
14744 
14745 bool Sema::BuiltinNonDeterministicValue(CallExpr *TheCall) {
14746   if (checkArgCount(TheCall, 1))
14747     return true;
14748 
14749   ExprResult Arg = TheCall->getArg(0);
14750   QualType TyArg = Arg.get()->getType();
14751 
14752   if (!TyArg->isBuiltinType() && !TyArg->isVectorType())
14753     return Diag(TheCall->getArg(0)->getBeginLoc(), diag::err_builtin_invalid_arg_type)
14754            << 1 << /*vector, integer or floating point ty*/ 0 << TyArg;
14755 
14756   TheCall->setType(TyArg);
14757   return false;
14758 }
14759 
14760 ExprResult Sema::BuiltinMatrixTranspose(CallExpr *TheCall,
14761                                         ExprResult CallResult) {
14762   if (checkArgCount(TheCall, 1))
14763     return ExprError();
14764 
14765   ExprResult MatrixArg = DefaultLvalueConversion(TheCall->getArg(0));
14766   if (MatrixArg.isInvalid())
14767     return MatrixArg;
14768   Expr *Matrix = MatrixArg.get();
14769 
14770   auto *MType = Matrix->getType()->getAs<ConstantMatrixType>();
14771   if (!MType) {
14772     Diag(Matrix->getBeginLoc(), diag::err_builtin_invalid_arg_type)
14773         << 1 << /* matrix ty*/ 1 << Matrix->getType();
14774     return ExprError();
14775   }
14776 
14777   // Create returned matrix type by swapping rows and columns of the argument
14778   // matrix type.
14779   QualType ResultType = Context.getConstantMatrixType(
14780       MType->getElementType(), MType->getNumColumns(), MType->getNumRows());
14781 
14782   // Change the return type to the type of the returned matrix.
14783   TheCall->setType(ResultType);
14784 
14785   // Update call argument to use the possibly converted matrix argument.
14786   TheCall->setArg(0, Matrix);
14787   return CallResult;
14788 }
14789 
14790 // Get and verify the matrix dimensions.
14791 static std::optional<unsigned>
14792 getAndVerifyMatrixDimension(Expr *Expr, StringRef Name, Sema &S) {
14793   SourceLocation ErrorPos;
14794   std::optional<llvm::APSInt> Value =
14795       Expr->getIntegerConstantExpr(S.Context, &ErrorPos);
14796   if (!Value) {
14797     S.Diag(Expr->getBeginLoc(), diag::err_builtin_matrix_scalar_unsigned_arg)
14798         << Name;
14799     return {};
14800   }
14801   uint64_t Dim = Value->getZExtValue();
14802   if (!ConstantMatrixType::isDimensionValid(Dim)) {
14803     S.Diag(Expr->getBeginLoc(), diag::err_builtin_matrix_invalid_dimension)
14804         << Name << ConstantMatrixType::getMaxElementsPerDimension();
14805     return {};
14806   }
14807   return Dim;
14808 }
14809 
14810 ExprResult Sema::BuiltinMatrixColumnMajorLoad(CallExpr *TheCall,
14811                                               ExprResult CallResult) {
14812   if (!getLangOpts().MatrixTypes) {
14813     Diag(TheCall->getBeginLoc(), diag::err_builtin_matrix_disabled);
14814     return ExprError();
14815   }
14816 
14817   if (checkArgCount(TheCall, 4))
14818     return ExprError();
14819 
14820   unsigned PtrArgIdx = 0;
14821   Expr *PtrExpr = TheCall->getArg(PtrArgIdx);
14822   Expr *RowsExpr = TheCall->getArg(1);
14823   Expr *ColumnsExpr = TheCall->getArg(2);
14824   Expr *StrideExpr = TheCall->getArg(3);
14825 
14826   bool ArgError = false;
14827 
14828   // Check pointer argument.
14829   {
14830     ExprResult PtrConv = DefaultFunctionArrayLvalueConversion(PtrExpr);
14831     if (PtrConv.isInvalid())
14832       return PtrConv;
14833     PtrExpr = PtrConv.get();
14834     TheCall->setArg(0, PtrExpr);
14835     if (PtrExpr->isTypeDependent()) {
14836       TheCall->setType(Context.DependentTy);
14837       return TheCall;
14838     }
14839   }
14840 
14841   auto *PtrTy = PtrExpr->getType()->getAs<PointerType>();
14842   QualType ElementTy;
14843   if (!PtrTy) {
14844     Diag(PtrExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type)
14845         << PtrArgIdx + 1 << /*pointer to element ty*/ 2 << PtrExpr->getType();
14846     ArgError = true;
14847   } else {
14848     ElementTy = PtrTy->getPointeeType().getUnqualifiedType();
14849 
14850     if (!ConstantMatrixType::isValidElementType(ElementTy)) {
14851       Diag(PtrExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type)
14852           << PtrArgIdx + 1 << /* pointer to element ty*/ 2
14853           << PtrExpr->getType();
14854       ArgError = true;
14855     }
14856   }
14857 
14858   // Apply default Lvalue conversions and convert the expression to size_t.
14859   auto ApplyArgumentConversions = [this](Expr *E) {
14860     ExprResult Conv = DefaultLvalueConversion(E);
14861     if (Conv.isInvalid())
14862       return Conv;
14863 
14864     return tryConvertExprToType(Conv.get(), Context.getSizeType());
14865   };
14866 
14867   // Apply conversion to row and column expressions.
14868   ExprResult RowsConv = ApplyArgumentConversions(RowsExpr);
14869   if (!RowsConv.isInvalid()) {
14870     RowsExpr = RowsConv.get();
14871     TheCall->setArg(1, RowsExpr);
14872   } else
14873     RowsExpr = nullptr;
14874 
14875   ExprResult ColumnsConv = ApplyArgumentConversions(ColumnsExpr);
14876   if (!ColumnsConv.isInvalid()) {
14877     ColumnsExpr = ColumnsConv.get();
14878     TheCall->setArg(2, ColumnsExpr);
14879   } else
14880     ColumnsExpr = nullptr;
14881 
14882   // If any part of the result matrix type is still pending, just use
14883   // Context.DependentTy, until all parts are resolved.
14884   if ((RowsExpr && RowsExpr->isTypeDependent()) ||
14885       (ColumnsExpr && ColumnsExpr->isTypeDependent())) {
14886     TheCall->setType(Context.DependentTy);
14887     return CallResult;
14888   }
14889 
14890   // Check row and column dimensions.
14891   std::optional<unsigned> MaybeRows;
14892   if (RowsExpr)
14893     MaybeRows = getAndVerifyMatrixDimension(RowsExpr, "row", *this);
14894 
14895   std::optional<unsigned> MaybeColumns;
14896   if (ColumnsExpr)
14897     MaybeColumns = getAndVerifyMatrixDimension(ColumnsExpr, "column", *this);
14898 
14899   // Check stride argument.
14900   ExprResult StrideConv = ApplyArgumentConversions(StrideExpr);
14901   if (StrideConv.isInvalid())
14902     return ExprError();
14903   StrideExpr = StrideConv.get();
14904   TheCall->setArg(3, StrideExpr);
14905 
14906   if (MaybeRows) {
14907     if (std::optional<llvm::APSInt> Value =
14908             StrideExpr->getIntegerConstantExpr(Context)) {
14909       uint64_t Stride = Value->getZExtValue();
14910       if (Stride < *MaybeRows) {
14911         Diag(StrideExpr->getBeginLoc(),
14912              diag::err_builtin_matrix_stride_too_small);
14913         ArgError = true;
14914       }
14915     }
14916   }
14917 
14918   if (ArgError || !MaybeRows || !MaybeColumns)
14919     return ExprError();
14920 
14921   TheCall->setType(
14922       Context.getConstantMatrixType(ElementTy, *MaybeRows, *MaybeColumns));
14923   return CallResult;
14924 }
14925 
14926 ExprResult Sema::BuiltinMatrixColumnMajorStore(CallExpr *TheCall,
14927                                                ExprResult CallResult) {
14928   if (checkArgCount(TheCall, 3))
14929     return ExprError();
14930 
14931   unsigned PtrArgIdx = 1;
14932   Expr *MatrixExpr = TheCall->getArg(0);
14933   Expr *PtrExpr = TheCall->getArg(PtrArgIdx);
14934   Expr *StrideExpr = TheCall->getArg(2);
14935 
14936   bool ArgError = false;
14937 
14938   {
14939     ExprResult MatrixConv = DefaultLvalueConversion(MatrixExpr);
14940     if (MatrixConv.isInvalid())
14941       return MatrixConv;
14942     MatrixExpr = MatrixConv.get();
14943     TheCall->setArg(0, MatrixExpr);
14944   }
14945   if (MatrixExpr->isTypeDependent()) {
14946     TheCall->setType(Context.DependentTy);
14947     return TheCall;
14948   }
14949 
14950   auto *MatrixTy = MatrixExpr->getType()->getAs<ConstantMatrixType>();
14951   if (!MatrixTy) {
14952     Diag(MatrixExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type)
14953         << 1 << /*matrix ty */ 1 << MatrixExpr->getType();
14954     ArgError = true;
14955   }
14956 
14957   {
14958     ExprResult PtrConv = DefaultFunctionArrayLvalueConversion(PtrExpr);
14959     if (PtrConv.isInvalid())
14960       return PtrConv;
14961     PtrExpr = PtrConv.get();
14962     TheCall->setArg(1, PtrExpr);
14963     if (PtrExpr->isTypeDependent()) {
14964       TheCall->setType(Context.DependentTy);
14965       return TheCall;
14966     }
14967   }
14968 
14969   // Check pointer argument.
14970   auto *PtrTy = PtrExpr->getType()->getAs<PointerType>();
14971   if (!PtrTy) {
14972     Diag(PtrExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type)
14973         << PtrArgIdx + 1 << /*pointer to element ty*/ 2 << PtrExpr->getType();
14974     ArgError = true;
14975   } else {
14976     QualType ElementTy = PtrTy->getPointeeType();
14977     if (ElementTy.isConstQualified()) {
14978       Diag(PtrExpr->getBeginLoc(), diag::err_builtin_matrix_store_to_const);
14979       ArgError = true;
14980     }
14981     ElementTy = ElementTy.getUnqualifiedType().getCanonicalType();
14982     if (MatrixTy &&
14983         !Context.hasSameType(ElementTy, MatrixTy->getElementType())) {
14984       Diag(PtrExpr->getBeginLoc(),
14985            diag::err_builtin_matrix_pointer_arg_mismatch)
14986           << ElementTy << MatrixTy->getElementType();
14987       ArgError = true;
14988     }
14989   }
14990 
14991   // Apply default Lvalue conversions and convert the stride expression to
14992   // size_t.
14993   {
14994     ExprResult StrideConv = DefaultLvalueConversion(StrideExpr);
14995     if (StrideConv.isInvalid())
14996       return StrideConv;
14997 
14998     StrideConv = tryConvertExprToType(StrideConv.get(), Context.getSizeType());
14999     if (StrideConv.isInvalid())
15000       return StrideConv;
15001     StrideExpr = StrideConv.get();
15002     TheCall->setArg(2, StrideExpr);
15003   }
15004 
15005   // Check stride argument.
15006   if (MatrixTy) {
15007     if (std::optional<llvm::APSInt> Value =
15008             StrideExpr->getIntegerConstantExpr(Context)) {
15009       uint64_t Stride = Value->getZExtValue();
15010       if (Stride < MatrixTy->getNumRows()) {
15011         Diag(StrideExpr->getBeginLoc(),
15012              diag::err_builtin_matrix_stride_too_small);
15013         ArgError = true;
15014       }
15015     }
15016   }
15017 
15018   if (ArgError)
15019     return ExprError();
15020 
15021   return CallResult;
15022 }
15023 
15024 void Sema::CheckTCBEnforcement(const SourceLocation CallExprLoc,
15025                                const NamedDecl *Callee) {
15026   // This warning does not make sense in code that has no runtime behavior.
15027   if (isUnevaluatedContext())
15028     return;
15029 
15030   const NamedDecl *Caller = getCurFunctionOrMethodDecl();
15031 
15032   if (!Caller || !Caller->hasAttr<EnforceTCBAttr>())
15033     return;
15034 
15035   // Search through the enforce_tcb and enforce_tcb_leaf attributes to find
15036   // all TCBs the callee is a part of.
15037   llvm::StringSet<> CalleeTCBs;
15038   for (const auto *A : Callee->specific_attrs<EnforceTCBAttr>())
15039     CalleeTCBs.insert(A->getTCBName());
15040   for (const auto *A : Callee->specific_attrs<EnforceTCBLeafAttr>())
15041     CalleeTCBs.insert(A->getTCBName());
15042 
15043   // Go through the TCBs the caller is a part of and emit warnings if Caller
15044   // is in a TCB that the Callee is not.
15045   for (const auto *A : Caller->specific_attrs<EnforceTCBAttr>()) {
15046     StringRef CallerTCB = A->getTCBName();
15047     if (CalleeTCBs.count(CallerTCB) == 0) {
15048       this->Diag(CallExprLoc, diag::warn_tcb_enforcement_violation)
15049           << Callee << CallerTCB;
15050     }
15051   }
15052 }
15053