xref: /freebsd/contrib/llvm-project/clang/lib/Sema/SemaChecking.cpp (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 //===- SemaChecking.cpp - Extra Semantic Checking -------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file implements extra semantic analysis beyond what is enforced
10 //  by the C type system.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/AST/APValue.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Attr.h"
17 #include "clang/AST/AttrIterator.h"
18 #include "clang/AST/CharUnits.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/DeclBase.h"
21 #include "clang/AST/DeclCXX.h"
22 #include "clang/AST/DeclObjC.h"
23 #include "clang/AST/DeclarationName.h"
24 #include "clang/AST/EvaluatedExprVisitor.h"
25 #include "clang/AST/Expr.h"
26 #include "clang/AST/ExprCXX.h"
27 #include "clang/AST/ExprObjC.h"
28 #include "clang/AST/ExprOpenMP.h"
29 #include "clang/AST/FormatString.h"
30 #include "clang/AST/NSAPI.h"
31 #include "clang/AST/NonTrivialTypeVisitor.h"
32 #include "clang/AST/OperationKinds.h"
33 #include "clang/AST/RecordLayout.h"
34 #include "clang/AST/Stmt.h"
35 #include "clang/AST/TemplateBase.h"
36 #include "clang/AST/Type.h"
37 #include "clang/AST/TypeLoc.h"
38 #include "clang/AST/UnresolvedSet.h"
39 #include "clang/Basic/AddressSpaces.h"
40 #include "clang/Basic/CharInfo.h"
41 #include "clang/Basic/Diagnostic.h"
42 #include "clang/Basic/IdentifierTable.h"
43 #include "clang/Basic/LLVM.h"
44 #include "clang/Basic/LangOptions.h"
45 #include "clang/Basic/OpenCLOptions.h"
46 #include "clang/Basic/OperatorKinds.h"
47 #include "clang/Basic/PartialDiagnostic.h"
48 #include "clang/Basic/SourceLocation.h"
49 #include "clang/Basic/SourceManager.h"
50 #include "clang/Basic/Specifiers.h"
51 #include "clang/Basic/SyncScope.h"
52 #include "clang/Basic/TargetBuiltins.h"
53 #include "clang/Basic/TargetCXXABI.h"
54 #include "clang/Basic/TargetInfo.h"
55 #include "clang/Basic/TypeTraits.h"
56 #include "clang/Lex/Lexer.h" // TODO: Extract static functions to fix layering.
57 #include "clang/Sema/Initialization.h"
58 #include "clang/Sema/Lookup.h"
59 #include "clang/Sema/Ownership.h"
60 #include "clang/Sema/Scope.h"
61 #include "clang/Sema/ScopeInfo.h"
62 #include "clang/Sema/Sema.h"
63 #include "clang/Sema/SemaInternal.h"
64 #include "llvm/ADT/APFloat.h"
65 #include "llvm/ADT/APInt.h"
66 #include "llvm/ADT/APSInt.h"
67 #include "llvm/ADT/ArrayRef.h"
68 #include "llvm/ADT/DenseMap.h"
69 #include "llvm/ADT/FoldingSet.h"
70 #include "llvm/ADT/None.h"
71 #include "llvm/ADT/Optional.h"
72 #include "llvm/ADT/STLExtras.h"
73 #include "llvm/ADT/SmallBitVector.h"
74 #include "llvm/ADT/SmallPtrSet.h"
75 #include "llvm/ADT/SmallString.h"
76 #include "llvm/ADT/SmallVector.h"
77 #include "llvm/ADT/StringRef.h"
78 #include "llvm/ADT/StringSwitch.h"
79 #include "llvm/ADT/Triple.h"
80 #include "llvm/Support/AtomicOrdering.h"
81 #include "llvm/Support/Casting.h"
82 #include "llvm/Support/Compiler.h"
83 #include "llvm/Support/ConvertUTF.h"
84 #include "llvm/Support/ErrorHandling.h"
85 #include "llvm/Support/Format.h"
86 #include "llvm/Support/Locale.h"
87 #include "llvm/Support/MathExtras.h"
88 #include "llvm/Support/SaveAndRestore.h"
89 #include "llvm/Support/raw_ostream.h"
90 #include <algorithm>
91 #include <bitset>
92 #include <cassert>
93 #include <cstddef>
94 #include <cstdint>
95 #include <functional>
96 #include <limits>
97 #include <string>
98 #include <tuple>
99 #include <utility>
100 
101 using namespace clang;
102 using namespace sema;
103 
104 SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL,
105                                                     unsigned ByteNo) const {
106   return SL->getLocationOfByte(ByteNo, getSourceManager(), LangOpts,
107                                Context.getTargetInfo());
108 }
109 
110 /// Checks that a call expression's argument count is the desired number.
111 /// This is useful when doing custom type-checking.  Returns true on error.
112 static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) {
113   unsigned argCount = call->getNumArgs();
114   if (argCount == desiredArgCount) return false;
115 
116   if (argCount < desiredArgCount)
117     return S.Diag(call->getEndLoc(), diag::err_typecheck_call_too_few_args)
118            << 0 /*function call*/ << desiredArgCount << argCount
119            << call->getSourceRange();
120 
121   // Highlight all the excess arguments.
122   SourceRange range(call->getArg(desiredArgCount)->getBeginLoc(),
123                     call->getArg(argCount - 1)->getEndLoc());
124 
125   return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args)
126     << 0 /*function call*/ << desiredArgCount << argCount
127     << call->getArg(1)->getSourceRange();
128 }
129 
130 /// Check that the first argument to __builtin_annotation is an integer
131 /// and the second argument is a non-wide string literal.
132 static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) {
133   if (checkArgCount(S, TheCall, 2))
134     return true;
135 
136   // First argument should be an integer.
137   Expr *ValArg = TheCall->getArg(0);
138   QualType Ty = ValArg->getType();
139   if (!Ty->isIntegerType()) {
140     S.Diag(ValArg->getBeginLoc(), diag::err_builtin_annotation_first_arg)
141         << ValArg->getSourceRange();
142     return true;
143   }
144 
145   // Second argument should be a constant string.
146   Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts();
147   StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg);
148   if (!Literal || !Literal->isAscii()) {
149     S.Diag(StrArg->getBeginLoc(), diag::err_builtin_annotation_second_arg)
150         << StrArg->getSourceRange();
151     return true;
152   }
153 
154   TheCall->setType(Ty);
155   return false;
156 }
157 
158 static bool SemaBuiltinMSVCAnnotation(Sema &S, CallExpr *TheCall) {
159   // We need at least one argument.
160   if (TheCall->getNumArgs() < 1) {
161     S.Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least)
162         << 0 << 1 << TheCall->getNumArgs()
163         << TheCall->getCallee()->getSourceRange();
164     return true;
165   }
166 
167   // All arguments should be wide string literals.
168   for (Expr *Arg : TheCall->arguments()) {
169     auto *Literal = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
170     if (!Literal || !Literal->isWide()) {
171       S.Diag(Arg->getBeginLoc(), diag::err_msvc_annotation_wide_str)
172           << Arg->getSourceRange();
173       return true;
174     }
175   }
176 
177   return false;
178 }
179 
180 /// Check that the argument to __builtin_addressof is a glvalue, and set the
181 /// result type to the corresponding pointer type.
182 static bool SemaBuiltinAddressof(Sema &S, CallExpr *TheCall) {
183   if (checkArgCount(S, TheCall, 1))
184     return true;
185 
186   ExprResult Arg(TheCall->getArg(0));
187   QualType ResultType = S.CheckAddressOfOperand(Arg, TheCall->getBeginLoc());
188   if (ResultType.isNull())
189     return true;
190 
191   TheCall->setArg(0, Arg.get());
192   TheCall->setType(ResultType);
193   return false;
194 }
195 
196 /// Check the number of arguments and set the result type to
197 /// the argument type.
198 static bool SemaBuiltinPreserveAI(Sema &S, CallExpr *TheCall) {
199   if (checkArgCount(S, TheCall, 1))
200     return true;
201 
202   TheCall->setType(TheCall->getArg(0)->getType());
203   return false;
204 }
205 
206 /// Check that the value argument for __builtin_is_aligned(value, alignment) and
207 /// __builtin_aligned_{up,down}(value, alignment) is an integer or a pointer
208 /// type (but not a function pointer) and that the alignment is a power-of-two.
209 static bool SemaBuiltinAlignment(Sema &S, CallExpr *TheCall, unsigned ID) {
210   if (checkArgCount(S, TheCall, 2))
211     return true;
212 
213   clang::Expr *Source = TheCall->getArg(0);
214   bool IsBooleanAlignBuiltin = ID == Builtin::BI__builtin_is_aligned;
215 
216   auto IsValidIntegerType = [](QualType Ty) {
217     return Ty->isIntegerType() && !Ty->isEnumeralType() && !Ty->isBooleanType();
218   };
219   QualType SrcTy = Source->getType();
220   // We should also be able to use it with arrays (but not functions!).
221   if (SrcTy->canDecayToPointerType() && SrcTy->isArrayType()) {
222     SrcTy = S.Context.getDecayedType(SrcTy);
223   }
224   if ((!SrcTy->isPointerType() && !IsValidIntegerType(SrcTy)) ||
225       SrcTy->isFunctionPointerType()) {
226     // FIXME: this is not quite the right error message since we don't allow
227     // floating point types, or member pointers.
228     S.Diag(Source->getExprLoc(), diag::err_typecheck_expect_scalar_operand)
229         << SrcTy;
230     return true;
231   }
232 
233   clang::Expr *AlignOp = TheCall->getArg(1);
234   if (!IsValidIntegerType(AlignOp->getType())) {
235     S.Diag(AlignOp->getExprLoc(), diag::err_typecheck_expect_int)
236         << AlignOp->getType();
237     return true;
238   }
239   Expr::EvalResult AlignResult;
240   unsigned MaxAlignmentBits = S.Context.getIntWidth(SrcTy) - 1;
241   // We can't check validity of alignment if it is value dependent.
242   if (!AlignOp->isValueDependent() &&
243       AlignOp->EvaluateAsInt(AlignResult, S.Context,
244                              Expr::SE_AllowSideEffects)) {
245     llvm::APSInt AlignValue = AlignResult.Val.getInt();
246     llvm::APSInt MaxValue(
247         llvm::APInt::getOneBitSet(MaxAlignmentBits + 1, MaxAlignmentBits));
248     if (AlignValue < 1) {
249       S.Diag(AlignOp->getExprLoc(), diag::err_alignment_too_small) << 1;
250       return true;
251     }
252     if (llvm::APSInt::compareValues(AlignValue, MaxValue) > 0) {
253       S.Diag(AlignOp->getExprLoc(), diag::err_alignment_too_big)
254           << MaxValue.toString(10);
255       return true;
256     }
257     if (!AlignValue.isPowerOf2()) {
258       S.Diag(AlignOp->getExprLoc(), diag::err_alignment_not_power_of_two);
259       return true;
260     }
261     if (AlignValue == 1) {
262       S.Diag(AlignOp->getExprLoc(), diag::warn_alignment_builtin_useless)
263           << IsBooleanAlignBuiltin;
264     }
265   }
266 
267   ExprResult SrcArg = S.PerformCopyInitialization(
268       InitializedEntity::InitializeParameter(S.Context, SrcTy, false),
269       SourceLocation(), Source);
270   if (SrcArg.isInvalid())
271     return true;
272   TheCall->setArg(0, SrcArg.get());
273   ExprResult AlignArg =
274       S.PerformCopyInitialization(InitializedEntity::InitializeParameter(
275                                       S.Context, AlignOp->getType(), false),
276                                   SourceLocation(), AlignOp);
277   if (AlignArg.isInvalid())
278     return true;
279   TheCall->setArg(1, AlignArg.get());
280   // For align_up/align_down, the return type is the same as the (potentially
281   // decayed) argument type including qualifiers. For is_aligned(), the result
282   // is always bool.
283   TheCall->setType(IsBooleanAlignBuiltin ? S.Context.BoolTy : SrcTy);
284   return false;
285 }
286 
287 static bool SemaBuiltinOverflow(Sema &S, CallExpr *TheCall,
288                                 unsigned BuiltinID) {
289   if (checkArgCount(S, TheCall, 3))
290     return true;
291 
292   // First two arguments should be integers.
293   for (unsigned I = 0; I < 2; ++I) {
294     ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(I));
295     if (Arg.isInvalid()) return true;
296     TheCall->setArg(I, Arg.get());
297 
298     QualType Ty = Arg.get()->getType();
299     if (!Ty->isIntegerType()) {
300       S.Diag(Arg.get()->getBeginLoc(), diag::err_overflow_builtin_must_be_int)
301           << Ty << Arg.get()->getSourceRange();
302       return true;
303     }
304   }
305 
306   // Third argument should be a pointer to a non-const integer.
307   // IRGen correctly handles volatile, restrict, and address spaces, and
308   // the other qualifiers aren't possible.
309   {
310     ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(2));
311     if (Arg.isInvalid()) return true;
312     TheCall->setArg(2, Arg.get());
313 
314     QualType Ty = Arg.get()->getType();
315     const auto *PtrTy = Ty->getAs<PointerType>();
316     if (!PtrTy ||
317         !PtrTy->getPointeeType()->isIntegerType() ||
318         PtrTy->getPointeeType().isConstQualified()) {
319       S.Diag(Arg.get()->getBeginLoc(),
320              diag::err_overflow_builtin_must_be_ptr_int)
321         << Ty << Arg.get()->getSourceRange();
322       return true;
323     }
324   }
325 
326   // Disallow signed ExtIntType args larger than 128 bits to mul function until
327   // we improve backend support.
328   if (BuiltinID == Builtin::BI__builtin_mul_overflow) {
329     for (unsigned I = 0; I < 3; ++I) {
330       const auto Arg = TheCall->getArg(I);
331       // Third argument will be a pointer.
332       auto Ty = I < 2 ? Arg->getType() : Arg->getType()->getPointeeType();
333       if (Ty->isExtIntType() && Ty->isSignedIntegerType() &&
334           S.getASTContext().getIntWidth(Ty) > 128)
335         return S.Diag(Arg->getBeginLoc(),
336                       diag::err_overflow_builtin_ext_int_max_size)
337                << 128;
338     }
339   }
340 
341   return false;
342 }
343 
344 static bool SemaBuiltinCallWithStaticChain(Sema &S, CallExpr *BuiltinCall) {
345   if (checkArgCount(S, BuiltinCall, 2))
346     return true;
347 
348   SourceLocation BuiltinLoc = BuiltinCall->getBeginLoc();
349   Expr *Builtin = BuiltinCall->getCallee()->IgnoreImpCasts();
350   Expr *Call = BuiltinCall->getArg(0);
351   Expr *Chain = BuiltinCall->getArg(1);
352 
353   if (Call->getStmtClass() != Stmt::CallExprClass) {
354     S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_not_call)
355         << Call->getSourceRange();
356     return true;
357   }
358 
359   auto CE = cast<CallExpr>(Call);
360   if (CE->getCallee()->getType()->isBlockPointerType()) {
361     S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_block_call)
362         << Call->getSourceRange();
363     return true;
364   }
365 
366   const Decl *TargetDecl = CE->getCalleeDecl();
367   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl))
368     if (FD->getBuiltinID()) {
369       S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_builtin_call)
370           << Call->getSourceRange();
371       return true;
372     }
373 
374   if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens())) {
375     S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_pdtor_call)
376         << Call->getSourceRange();
377     return true;
378   }
379 
380   ExprResult ChainResult = S.UsualUnaryConversions(Chain);
381   if (ChainResult.isInvalid())
382     return true;
383   if (!ChainResult.get()->getType()->isPointerType()) {
384     S.Diag(BuiltinLoc, diag::err_second_argument_to_cwsc_not_pointer)
385         << Chain->getSourceRange();
386     return true;
387   }
388 
389   QualType ReturnTy = CE->getCallReturnType(S.Context);
390   QualType ArgTys[2] = { ReturnTy, ChainResult.get()->getType() };
391   QualType BuiltinTy = S.Context.getFunctionType(
392       ReturnTy, ArgTys, FunctionProtoType::ExtProtoInfo());
393   QualType BuiltinPtrTy = S.Context.getPointerType(BuiltinTy);
394 
395   Builtin =
396       S.ImpCastExprToType(Builtin, BuiltinPtrTy, CK_BuiltinFnToFnPtr).get();
397 
398   BuiltinCall->setType(CE->getType());
399   BuiltinCall->setValueKind(CE->getValueKind());
400   BuiltinCall->setObjectKind(CE->getObjectKind());
401   BuiltinCall->setCallee(Builtin);
402   BuiltinCall->setArg(1, ChainResult.get());
403 
404   return false;
405 }
406 
407 namespace {
408 
409 class EstimateSizeFormatHandler
410     : public analyze_format_string::FormatStringHandler {
411   size_t Size;
412 
413 public:
414   EstimateSizeFormatHandler(StringRef Format)
415       : Size(std::min(Format.find(0), Format.size()) +
416              1 /* null byte always written by sprintf */) {}
417 
418   bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS,
419                              const char *, unsigned SpecifierLen) override {
420 
421     const size_t FieldWidth = computeFieldWidth(FS);
422     const size_t Precision = computePrecision(FS);
423 
424     // The actual format.
425     switch (FS.getConversionSpecifier().getKind()) {
426     // Just a char.
427     case analyze_format_string::ConversionSpecifier::cArg:
428     case analyze_format_string::ConversionSpecifier::CArg:
429       Size += std::max(FieldWidth, (size_t)1);
430       break;
431     // Just an integer.
432     case analyze_format_string::ConversionSpecifier::dArg:
433     case analyze_format_string::ConversionSpecifier::DArg:
434     case analyze_format_string::ConversionSpecifier::iArg:
435     case analyze_format_string::ConversionSpecifier::oArg:
436     case analyze_format_string::ConversionSpecifier::OArg:
437     case analyze_format_string::ConversionSpecifier::uArg:
438     case analyze_format_string::ConversionSpecifier::UArg:
439     case analyze_format_string::ConversionSpecifier::xArg:
440     case analyze_format_string::ConversionSpecifier::XArg:
441       Size += std::max(FieldWidth, Precision);
442       break;
443 
444     // %g style conversion switches between %f or %e style dynamically.
445     // %f always takes less space, so default to it.
446     case analyze_format_string::ConversionSpecifier::gArg:
447     case analyze_format_string::ConversionSpecifier::GArg:
448 
449     // Floating point number in the form '[+]ddd.ddd'.
450     case analyze_format_string::ConversionSpecifier::fArg:
451     case analyze_format_string::ConversionSpecifier::FArg:
452       Size += std::max(FieldWidth, 1 /* integer part */ +
453                                        (Precision ? 1 + Precision
454                                                   : 0) /* period + decimal */);
455       break;
456 
457     // Floating point number in the form '[-]d.ddde[+-]dd'.
458     case analyze_format_string::ConversionSpecifier::eArg:
459     case analyze_format_string::ConversionSpecifier::EArg:
460       Size +=
461           std::max(FieldWidth,
462                    1 /* integer part */ +
463                        (Precision ? 1 + Precision : 0) /* period + decimal */ +
464                        1 /* e or E letter */ + 2 /* exponent */);
465       break;
466 
467     // Floating point number in the form '[-]0xh.hhhhp±dd'.
468     case analyze_format_string::ConversionSpecifier::aArg:
469     case analyze_format_string::ConversionSpecifier::AArg:
470       Size +=
471           std::max(FieldWidth,
472                    2 /* 0x */ + 1 /* integer part */ +
473                        (Precision ? 1 + Precision : 0) /* period + decimal */ +
474                        1 /* p or P letter */ + 1 /* + or - */ + 1 /* value */);
475       break;
476 
477     // Just a string.
478     case analyze_format_string::ConversionSpecifier::sArg:
479     case analyze_format_string::ConversionSpecifier::SArg:
480       Size += FieldWidth;
481       break;
482 
483     // Just a pointer in the form '0xddd'.
484     case analyze_format_string::ConversionSpecifier::pArg:
485       Size += std::max(FieldWidth, 2 /* leading 0x */ + Precision);
486       break;
487 
488     // A plain percent.
489     case analyze_format_string::ConversionSpecifier::PercentArg:
490       Size += 1;
491       break;
492 
493     default:
494       break;
495     }
496 
497     Size += FS.hasPlusPrefix() || FS.hasSpacePrefix();
498 
499     if (FS.hasAlternativeForm()) {
500       switch (FS.getConversionSpecifier().getKind()) {
501       default:
502         break;
503       // Force a leading '0'.
504       case analyze_format_string::ConversionSpecifier::oArg:
505         Size += 1;
506         break;
507       // Force a leading '0x'.
508       case analyze_format_string::ConversionSpecifier::xArg:
509       case analyze_format_string::ConversionSpecifier::XArg:
510         Size += 2;
511         break;
512       // Force a period '.' before decimal, even if precision is 0.
513       case analyze_format_string::ConversionSpecifier::aArg:
514       case analyze_format_string::ConversionSpecifier::AArg:
515       case analyze_format_string::ConversionSpecifier::eArg:
516       case analyze_format_string::ConversionSpecifier::EArg:
517       case analyze_format_string::ConversionSpecifier::fArg:
518       case analyze_format_string::ConversionSpecifier::FArg:
519       case analyze_format_string::ConversionSpecifier::gArg:
520       case analyze_format_string::ConversionSpecifier::GArg:
521         Size += (Precision ? 0 : 1);
522         break;
523       }
524     }
525     assert(SpecifierLen <= Size && "no underflow");
526     Size -= SpecifierLen;
527     return true;
528   }
529 
530   size_t getSizeLowerBound() const { return Size; }
531 
532 private:
533   static size_t computeFieldWidth(const analyze_printf::PrintfSpecifier &FS) {
534     const analyze_format_string::OptionalAmount &FW = FS.getFieldWidth();
535     size_t FieldWidth = 0;
536     if (FW.getHowSpecified() == analyze_format_string::OptionalAmount::Constant)
537       FieldWidth = FW.getConstantAmount();
538     return FieldWidth;
539   }
540 
541   static size_t computePrecision(const analyze_printf::PrintfSpecifier &FS) {
542     const analyze_format_string::OptionalAmount &FW = FS.getPrecision();
543     size_t Precision = 0;
544 
545     // See man 3 printf for default precision value based on the specifier.
546     switch (FW.getHowSpecified()) {
547     case analyze_format_string::OptionalAmount::NotSpecified:
548       switch (FS.getConversionSpecifier().getKind()) {
549       default:
550         break;
551       case analyze_format_string::ConversionSpecifier::dArg: // %d
552       case analyze_format_string::ConversionSpecifier::DArg: // %D
553       case analyze_format_string::ConversionSpecifier::iArg: // %i
554         Precision = 1;
555         break;
556       case analyze_format_string::ConversionSpecifier::oArg: // %d
557       case analyze_format_string::ConversionSpecifier::OArg: // %D
558       case analyze_format_string::ConversionSpecifier::uArg: // %d
559       case analyze_format_string::ConversionSpecifier::UArg: // %D
560       case analyze_format_string::ConversionSpecifier::xArg: // %d
561       case analyze_format_string::ConversionSpecifier::XArg: // %D
562         Precision = 1;
563         break;
564       case analyze_format_string::ConversionSpecifier::fArg: // %f
565       case analyze_format_string::ConversionSpecifier::FArg: // %F
566       case analyze_format_string::ConversionSpecifier::eArg: // %e
567       case analyze_format_string::ConversionSpecifier::EArg: // %E
568       case analyze_format_string::ConversionSpecifier::gArg: // %g
569       case analyze_format_string::ConversionSpecifier::GArg: // %G
570         Precision = 6;
571         break;
572       case analyze_format_string::ConversionSpecifier::pArg: // %d
573         Precision = 1;
574         break;
575       }
576       break;
577     case analyze_format_string::OptionalAmount::Constant:
578       Precision = FW.getConstantAmount();
579       break;
580     default:
581       break;
582     }
583     return Precision;
584   }
585 };
586 
587 } // namespace
588 
589 /// Check a call to BuiltinID for buffer overflows. If BuiltinID is a
590 /// __builtin_*_chk function, then use the object size argument specified in the
591 /// source. Otherwise, infer the object size using __builtin_object_size.
592 void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD,
593                                                CallExpr *TheCall) {
594   // FIXME: There are some more useful checks we could be doing here:
595   //  - Evaluate strlen of strcpy arguments, use as object size.
596 
597   if (TheCall->isValueDependent() || TheCall->isTypeDependent() ||
598       isConstantEvaluated())
599     return;
600 
601   unsigned BuiltinID = FD->getBuiltinID(/*ConsiderWrappers=*/true);
602   if (!BuiltinID)
603     return;
604 
605   const TargetInfo &TI = getASTContext().getTargetInfo();
606   unsigned SizeTypeWidth = TI.getTypeWidth(TI.getSizeType());
607 
608   unsigned DiagID = 0;
609   bool IsChkVariant = false;
610   Optional<llvm::APSInt> UsedSize;
611   unsigned SizeIndex, ObjectIndex;
612   switch (BuiltinID) {
613   default:
614     return;
615   case Builtin::BIsprintf:
616   case Builtin::BI__builtin___sprintf_chk: {
617     size_t FormatIndex = BuiltinID == Builtin::BIsprintf ? 1 : 3;
618     auto *FormatExpr = TheCall->getArg(FormatIndex)->IgnoreParenImpCasts();
619 
620     if (auto *Format = dyn_cast<StringLiteral>(FormatExpr)) {
621 
622       if (!Format->isAscii() && !Format->isUTF8())
623         return;
624 
625       StringRef FormatStrRef = Format->getString();
626       EstimateSizeFormatHandler H(FormatStrRef);
627       const char *FormatBytes = FormatStrRef.data();
628       const ConstantArrayType *T =
629           Context.getAsConstantArrayType(Format->getType());
630       assert(T && "String literal not of constant array type!");
631       size_t TypeSize = T->getSize().getZExtValue();
632 
633       // In case there's a null byte somewhere.
634       size_t StrLen =
635           std::min(std::max(TypeSize, size_t(1)) - 1, FormatStrRef.find(0));
636       if (!analyze_format_string::ParsePrintfString(
637               H, FormatBytes, FormatBytes + StrLen, getLangOpts(),
638               Context.getTargetInfo(), false)) {
639         DiagID = diag::warn_fortify_source_format_overflow;
640         UsedSize = llvm::APSInt::getUnsigned(H.getSizeLowerBound())
641                        .extOrTrunc(SizeTypeWidth);
642         if (BuiltinID == Builtin::BI__builtin___sprintf_chk) {
643           IsChkVariant = true;
644           ObjectIndex = 2;
645         } else {
646           IsChkVariant = false;
647           ObjectIndex = 0;
648         }
649         break;
650       }
651     }
652     return;
653   }
654   case Builtin::BI__builtin___memcpy_chk:
655   case Builtin::BI__builtin___memmove_chk:
656   case Builtin::BI__builtin___memset_chk:
657   case Builtin::BI__builtin___strlcat_chk:
658   case Builtin::BI__builtin___strlcpy_chk:
659   case Builtin::BI__builtin___strncat_chk:
660   case Builtin::BI__builtin___strncpy_chk:
661   case Builtin::BI__builtin___stpncpy_chk:
662   case Builtin::BI__builtin___memccpy_chk:
663   case Builtin::BI__builtin___mempcpy_chk: {
664     DiagID = diag::warn_builtin_chk_overflow;
665     IsChkVariant = true;
666     SizeIndex = TheCall->getNumArgs() - 2;
667     ObjectIndex = TheCall->getNumArgs() - 1;
668     break;
669   }
670 
671   case Builtin::BI__builtin___snprintf_chk:
672   case Builtin::BI__builtin___vsnprintf_chk: {
673     DiagID = diag::warn_builtin_chk_overflow;
674     IsChkVariant = true;
675     SizeIndex = 1;
676     ObjectIndex = 3;
677     break;
678   }
679 
680   case Builtin::BIstrncat:
681   case Builtin::BI__builtin_strncat:
682   case Builtin::BIstrncpy:
683   case Builtin::BI__builtin_strncpy:
684   case Builtin::BIstpncpy:
685   case Builtin::BI__builtin_stpncpy: {
686     // Whether these functions overflow depends on the runtime strlen of the
687     // string, not just the buffer size, so emitting the "always overflow"
688     // diagnostic isn't quite right. We should still diagnose passing a buffer
689     // size larger than the destination buffer though; this is a runtime abort
690     // in _FORTIFY_SOURCE mode, and is quite suspicious otherwise.
691     DiagID = diag::warn_fortify_source_size_mismatch;
692     SizeIndex = TheCall->getNumArgs() - 1;
693     ObjectIndex = 0;
694     break;
695   }
696 
697   case Builtin::BImemcpy:
698   case Builtin::BI__builtin_memcpy:
699   case Builtin::BImemmove:
700   case Builtin::BI__builtin_memmove:
701   case Builtin::BImemset:
702   case Builtin::BI__builtin_memset:
703   case Builtin::BImempcpy:
704   case Builtin::BI__builtin_mempcpy: {
705     DiagID = diag::warn_fortify_source_overflow;
706     SizeIndex = TheCall->getNumArgs() - 1;
707     ObjectIndex = 0;
708     break;
709   }
710   case Builtin::BIsnprintf:
711   case Builtin::BI__builtin_snprintf:
712   case Builtin::BIvsnprintf:
713   case Builtin::BI__builtin_vsnprintf: {
714     DiagID = diag::warn_fortify_source_size_mismatch;
715     SizeIndex = 1;
716     ObjectIndex = 0;
717     break;
718   }
719   }
720 
721   llvm::APSInt ObjectSize;
722   // For __builtin___*_chk, the object size is explicitly provided by the caller
723   // (usually using __builtin_object_size). Use that value to check this call.
724   if (IsChkVariant) {
725     Expr::EvalResult Result;
726     Expr *SizeArg = TheCall->getArg(ObjectIndex);
727     if (!SizeArg->EvaluateAsInt(Result, getASTContext()))
728       return;
729     ObjectSize = Result.Val.getInt();
730 
731   // Otherwise, try to evaluate an imaginary call to __builtin_object_size.
732   } else {
733     // If the parameter has a pass_object_size attribute, then we should use its
734     // (potentially) more strict checking mode. Otherwise, conservatively assume
735     // type 0.
736     int BOSType = 0;
737     if (const auto *POS =
738             FD->getParamDecl(ObjectIndex)->getAttr<PassObjectSizeAttr>())
739       BOSType = POS->getType();
740 
741     Expr *ObjArg = TheCall->getArg(ObjectIndex);
742     uint64_t Result;
743     if (!ObjArg->tryEvaluateObjectSize(Result, getASTContext(), BOSType))
744       return;
745     // Get the object size in the target's size_t width.
746     ObjectSize = llvm::APSInt::getUnsigned(Result).extOrTrunc(SizeTypeWidth);
747   }
748 
749   // Evaluate the number of bytes of the object that this call will use.
750   if (!UsedSize) {
751     Expr::EvalResult Result;
752     Expr *UsedSizeArg = TheCall->getArg(SizeIndex);
753     if (!UsedSizeArg->EvaluateAsInt(Result, getASTContext()))
754       return;
755     UsedSize = Result.Val.getInt().extOrTrunc(SizeTypeWidth);
756   }
757 
758   if (UsedSize.getValue().ule(ObjectSize))
759     return;
760 
761   StringRef FunctionName = getASTContext().BuiltinInfo.getName(BuiltinID);
762   // Skim off the details of whichever builtin was called to produce a better
763   // diagnostic, as it's unlikley that the user wrote the __builtin explicitly.
764   if (IsChkVariant) {
765     FunctionName = FunctionName.drop_front(std::strlen("__builtin___"));
766     FunctionName = FunctionName.drop_back(std::strlen("_chk"));
767   } else if (FunctionName.startswith("__builtin_")) {
768     FunctionName = FunctionName.drop_front(std::strlen("__builtin_"));
769   }
770 
771   DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall,
772                       PDiag(DiagID)
773                           << FunctionName << ObjectSize.toString(/*Radix=*/10)
774                           << UsedSize.getValue().toString(/*Radix=*/10));
775 }
776 
777 static bool SemaBuiltinSEHScopeCheck(Sema &SemaRef, CallExpr *TheCall,
778                                      Scope::ScopeFlags NeededScopeFlags,
779                                      unsigned DiagID) {
780   // Scopes aren't available during instantiation. Fortunately, builtin
781   // functions cannot be template args so they cannot be formed through template
782   // instantiation. Therefore checking once during the parse is sufficient.
783   if (SemaRef.inTemplateInstantiation())
784     return false;
785 
786   Scope *S = SemaRef.getCurScope();
787   while (S && !S->isSEHExceptScope())
788     S = S->getParent();
789   if (!S || !(S->getFlags() & NeededScopeFlags)) {
790     auto *DRE = cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
791     SemaRef.Diag(TheCall->getExprLoc(), DiagID)
792         << DRE->getDecl()->getIdentifier();
793     return true;
794   }
795 
796   return false;
797 }
798 
799 static inline bool isBlockPointer(Expr *Arg) {
800   return Arg->getType()->isBlockPointerType();
801 }
802 
803 /// OpenCL C v2.0, s6.13.17.2 - Checks that the block parameters are all local
804 /// void*, which is a requirement of device side enqueue.
805 static bool checkOpenCLBlockArgs(Sema &S, Expr *BlockArg) {
806   const BlockPointerType *BPT =
807       cast<BlockPointerType>(BlockArg->getType().getCanonicalType());
808   ArrayRef<QualType> Params =
809       BPT->getPointeeType()->castAs<FunctionProtoType>()->getParamTypes();
810   unsigned ArgCounter = 0;
811   bool IllegalParams = false;
812   // Iterate through the block parameters until either one is found that is not
813   // a local void*, or the block is valid.
814   for (ArrayRef<QualType>::iterator I = Params.begin(), E = Params.end();
815        I != E; ++I, ++ArgCounter) {
816     if (!(*I)->isPointerType() || !(*I)->getPointeeType()->isVoidType() ||
817         (*I)->getPointeeType().getQualifiers().getAddressSpace() !=
818             LangAS::opencl_local) {
819       // Get the location of the error. If a block literal has been passed
820       // (BlockExpr) then we can point straight to the offending argument,
821       // else we just point to the variable reference.
822       SourceLocation ErrorLoc;
823       if (isa<BlockExpr>(BlockArg)) {
824         BlockDecl *BD = cast<BlockExpr>(BlockArg)->getBlockDecl();
825         ErrorLoc = BD->getParamDecl(ArgCounter)->getBeginLoc();
826       } else if (isa<DeclRefExpr>(BlockArg)) {
827         ErrorLoc = cast<DeclRefExpr>(BlockArg)->getBeginLoc();
828       }
829       S.Diag(ErrorLoc,
830              diag::err_opencl_enqueue_kernel_blocks_non_local_void_args);
831       IllegalParams = true;
832     }
833   }
834 
835   return IllegalParams;
836 }
837 
838 static bool checkOpenCLSubgroupExt(Sema &S, CallExpr *Call) {
839   if (!S.getOpenCLOptions().isEnabled("cl_khr_subgroups")) {
840     S.Diag(Call->getBeginLoc(), diag::err_opencl_requires_extension)
841         << 1 << Call->getDirectCallee() << "cl_khr_subgroups";
842     return true;
843   }
844   return false;
845 }
846 
847 static bool SemaOpenCLBuiltinNDRangeAndBlock(Sema &S, CallExpr *TheCall) {
848   if (checkArgCount(S, TheCall, 2))
849     return true;
850 
851   if (checkOpenCLSubgroupExt(S, TheCall))
852     return true;
853 
854   // First argument is an ndrange_t type.
855   Expr *NDRangeArg = TheCall->getArg(0);
856   if (NDRangeArg->getType().getUnqualifiedType().getAsString() != "ndrange_t") {
857     S.Diag(NDRangeArg->getBeginLoc(), diag::err_opencl_builtin_expected_type)
858         << TheCall->getDirectCallee() << "'ndrange_t'";
859     return true;
860   }
861 
862   Expr *BlockArg = TheCall->getArg(1);
863   if (!isBlockPointer(BlockArg)) {
864     S.Diag(BlockArg->getBeginLoc(), diag::err_opencl_builtin_expected_type)
865         << TheCall->getDirectCallee() << "block";
866     return true;
867   }
868   return checkOpenCLBlockArgs(S, BlockArg);
869 }
870 
871 /// OpenCL C v2.0, s6.13.17.6 - Check the argument to the
872 /// get_kernel_work_group_size
873 /// and get_kernel_preferred_work_group_size_multiple builtin functions.
874 static bool SemaOpenCLBuiltinKernelWorkGroupSize(Sema &S, CallExpr *TheCall) {
875   if (checkArgCount(S, TheCall, 1))
876     return true;
877 
878   Expr *BlockArg = TheCall->getArg(0);
879   if (!isBlockPointer(BlockArg)) {
880     S.Diag(BlockArg->getBeginLoc(), diag::err_opencl_builtin_expected_type)
881         << TheCall->getDirectCallee() << "block";
882     return true;
883   }
884   return checkOpenCLBlockArgs(S, BlockArg);
885 }
886 
887 /// Diagnose integer type and any valid implicit conversion to it.
888 static bool checkOpenCLEnqueueIntType(Sema &S, Expr *E,
889                                       const QualType &IntType);
890 
891 static bool checkOpenCLEnqueueLocalSizeArgs(Sema &S, CallExpr *TheCall,
892                                             unsigned Start, unsigned End) {
893   bool IllegalParams = false;
894   for (unsigned I = Start; I <= End; ++I)
895     IllegalParams |= checkOpenCLEnqueueIntType(S, TheCall->getArg(I),
896                                               S.Context.getSizeType());
897   return IllegalParams;
898 }
899 
900 /// OpenCL v2.0, s6.13.17.1 - Check that sizes are provided for all
901 /// 'local void*' parameter of passed block.
902 static bool checkOpenCLEnqueueVariadicArgs(Sema &S, CallExpr *TheCall,
903                                            Expr *BlockArg,
904                                            unsigned NumNonVarArgs) {
905   const BlockPointerType *BPT =
906       cast<BlockPointerType>(BlockArg->getType().getCanonicalType());
907   unsigned NumBlockParams =
908       BPT->getPointeeType()->castAs<FunctionProtoType>()->getNumParams();
909   unsigned TotalNumArgs = TheCall->getNumArgs();
910 
911   // For each argument passed to the block, a corresponding uint needs to
912   // be passed to describe the size of the local memory.
913   if (TotalNumArgs != NumBlockParams + NumNonVarArgs) {
914     S.Diag(TheCall->getBeginLoc(),
915            diag::err_opencl_enqueue_kernel_local_size_args);
916     return true;
917   }
918 
919   // Check that the sizes of the local memory are specified by integers.
920   return checkOpenCLEnqueueLocalSizeArgs(S, TheCall, NumNonVarArgs,
921                                          TotalNumArgs - 1);
922 }
923 
924 /// OpenCL C v2.0, s6.13.17 - Enqueue kernel function contains four different
925 /// overload formats specified in Table 6.13.17.1.
926 /// int enqueue_kernel(queue_t queue,
927 ///                    kernel_enqueue_flags_t flags,
928 ///                    const ndrange_t ndrange,
929 ///                    void (^block)(void))
930 /// int enqueue_kernel(queue_t queue,
931 ///                    kernel_enqueue_flags_t flags,
932 ///                    const ndrange_t ndrange,
933 ///                    uint num_events_in_wait_list,
934 ///                    clk_event_t *event_wait_list,
935 ///                    clk_event_t *event_ret,
936 ///                    void (^block)(void))
937 /// int enqueue_kernel(queue_t queue,
938 ///                    kernel_enqueue_flags_t flags,
939 ///                    const ndrange_t ndrange,
940 ///                    void (^block)(local void*, ...),
941 ///                    uint size0, ...)
942 /// int enqueue_kernel(queue_t queue,
943 ///                    kernel_enqueue_flags_t flags,
944 ///                    const ndrange_t ndrange,
945 ///                    uint num_events_in_wait_list,
946 ///                    clk_event_t *event_wait_list,
947 ///                    clk_event_t *event_ret,
948 ///                    void (^block)(local void*, ...),
949 ///                    uint size0, ...)
950 static bool SemaOpenCLBuiltinEnqueueKernel(Sema &S, CallExpr *TheCall) {
951   unsigned NumArgs = TheCall->getNumArgs();
952 
953   if (NumArgs < 4) {
954     S.Diag(TheCall->getBeginLoc(),
955            diag::err_typecheck_call_too_few_args_at_least)
956         << 0 << 4 << NumArgs;
957     return true;
958   }
959 
960   Expr *Arg0 = TheCall->getArg(0);
961   Expr *Arg1 = TheCall->getArg(1);
962   Expr *Arg2 = TheCall->getArg(2);
963   Expr *Arg3 = TheCall->getArg(3);
964 
965   // First argument always needs to be a queue_t type.
966   if (!Arg0->getType()->isQueueT()) {
967     S.Diag(TheCall->getArg(0)->getBeginLoc(),
968            diag::err_opencl_builtin_expected_type)
969         << TheCall->getDirectCallee() << S.Context.OCLQueueTy;
970     return true;
971   }
972 
973   // Second argument always needs to be a kernel_enqueue_flags_t enum value.
974   if (!Arg1->getType()->isIntegerType()) {
975     S.Diag(TheCall->getArg(1)->getBeginLoc(),
976            diag::err_opencl_builtin_expected_type)
977         << TheCall->getDirectCallee() << "'kernel_enqueue_flags_t' (i.e. uint)";
978     return true;
979   }
980 
981   // Third argument is always an ndrange_t type.
982   if (Arg2->getType().getUnqualifiedType().getAsString() != "ndrange_t") {
983     S.Diag(TheCall->getArg(2)->getBeginLoc(),
984            diag::err_opencl_builtin_expected_type)
985         << TheCall->getDirectCallee() << "'ndrange_t'";
986     return true;
987   }
988 
989   // With four arguments, there is only one form that the function could be
990   // called in: no events and no variable arguments.
991   if (NumArgs == 4) {
992     // check that the last argument is the right block type.
993     if (!isBlockPointer(Arg3)) {
994       S.Diag(Arg3->getBeginLoc(), diag::err_opencl_builtin_expected_type)
995           << TheCall->getDirectCallee() << "block";
996       return true;
997     }
998     // we have a block type, check the prototype
999     const BlockPointerType *BPT =
1000         cast<BlockPointerType>(Arg3->getType().getCanonicalType());
1001     if (BPT->getPointeeType()->castAs<FunctionProtoType>()->getNumParams() > 0) {
1002       S.Diag(Arg3->getBeginLoc(),
1003              diag::err_opencl_enqueue_kernel_blocks_no_args);
1004       return true;
1005     }
1006     return false;
1007   }
1008   // we can have block + varargs.
1009   if (isBlockPointer(Arg3))
1010     return (checkOpenCLBlockArgs(S, Arg3) ||
1011             checkOpenCLEnqueueVariadicArgs(S, TheCall, Arg3, 4));
1012   // last two cases with either exactly 7 args or 7 args and varargs.
1013   if (NumArgs >= 7) {
1014     // check common block argument.
1015     Expr *Arg6 = TheCall->getArg(6);
1016     if (!isBlockPointer(Arg6)) {
1017       S.Diag(Arg6->getBeginLoc(), diag::err_opencl_builtin_expected_type)
1018           << TheCall->getDirectCallee() << "block";
1019       return true;
1020     }
1021     if (checkOpenCLBlockArgs(S, Arg6))
1022       return true;
1023 
1024     // Forth argument has to be any integer type.
1025     if (!Arg3->getType()->isIntegerType()) {
1026       S.Diag(TheCall->getArg(3)->getBeginLoc(),
1027              diag::err_opencl_builtin_expected_type)
1028           << TheCall->getDirectCallee() << "integer";
1029       return true;
1030     }
1031     // check remaining common arguments.
1032     Expr *Arg4 = TheCall->getArg(4);
1033     Expr *Arg5 = TheCall->getArg(5);
1034 
1035     // Fifth argument is always passed as a pointer to clk_event_t.
1036     if (!Arg4->isNullPointerConstant(S.Context,
1037                                      Expr::NPC_ValueDependentIsNotNull) &&
1038         !Arg4->getType()->getPointeeOrArrayElementType()->isClkEventT()) {
1039       S.Diag(TheCall->getArg(4)->getBeginLoc(),
1040              diag::err_opencl_builtin_expected_type)
1041           << TheCall->getDirectCallee()
1042           << S.Context.getPointerType(S.Context.OCLClkEventTy);
1043       return true;
1044     }
1045 
1046     // Sixth argument is always passed as a pointer to clk_event_t.
1047     if (!Arg5->isNullPointerConstant(S.Context,
1048                                      Expr::NPC_ValueDependentIsNotNull) &&
1049         !(Arg5->getType()->isPointerType() &&
1050           Arg5->getType()->getPointeeType()->isClkEventT())) {
1051       S.Diag(TheCall->getArg(5)->getBeginLoc(),
1052              diag::err_opencl_builtin_expected_type)
1053           << TheCall->getDirectCallee()
1054           << S.Context.getPointerType(S.Context.OCLClkEventTy);
1055       return true;
1056     }
1057 
1058     if (NumArgs == 7)
1059       return false;
1060 
1061     return checkOpenCLEnqueueVariadicArgs(S, TheCall, Arg6, 7);
1062   }
1063 
1064   // None of the specific case has been detected, give generic error
1065   S.Diag(TheCall->getBeginLoc(),
1066          diag::err_opencl_enqueue_kernel_incorrect_args);
1067   return true;
1068 }
1069 
1070 /// Returns OpenCL access qual.
1071 static OpenCLAccessAttr *getOpenCLArgAccess(const Decl *D) {
1072     return D->getAttr<OpenCLAccessAttr>();
1073 }
1074 
1075 /// Returns true if pipe element type is different from the pointer.
1076 static bool checkOpenCLPipeArg(Sema &S, CallExpr *Call) {
1077   const Expr *Arg0 = Call->getArg(0);
1078   // First argument type should always be pipe.
1079   if (!Arg0->getType()->isPipeType()) {
1080     S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_first_arg)
1081         << Call->getDirectCallee() << Arg0->getSourceRange();
1082     return true;
1083   }
1084   OpenCLAccessAttr *AccessQual =
1085       getOpenCLArgAccess(cast<DeclRefExpr>(Arg0)->getDecl());
1086   // Validates the access qualifier is compatible with the call.
1087   // OpenCL v2.0 s6.13.16 - The access qualifiers for pipe should only be
1088   // read_only and write_only, and assumed to be read_only if no qualifier is
1089   // specified.
1090   switch (Call->getDirectCallee()->getBuiltinID()) {
1091   case Builtin::BIread_pipe:
1092   case Builtin::BIreserve_read_pipe:
1093   case Builtin::BIcommit_read_pipe:
1094   case Builtin::BIwork_group_reserve_read_pipe:
1095   case Builtin::BIsub_group_reserve_read_pipe:
1096   case Builtin::BIwork_group_commit_read_pipe:
1097   case Builtin::BIsub_group_commit_read_pipe:
1098     if (!(!AccessQual || AccessQual->isReadOnly())) {
1099       S.Diag(Arg0->getBeginLoc(),
1100              diag::err_opencl_builtin_pipe_invalid_access_modifier)
1101           << "read_only" << Arg0->getSourceRange();
1102       return true;
1103     }
1104     break;
1105   case Builtin::BIwrite_pipe:
1106   case Builtin::BIreserve_write_pipe:
1107   case Builtin::BIcommit_write_pipe:
1108   case Builtin::BIwork_group_reserve_write_pipe:
1109   case Builtin::BIsub_group_reserve_write_pipe:
1110   case Builtin::BIwork_group_commit_write_pipe:
1111   case Builtin::BIsub_group_commit_write_pipe:
1112     if (!(AccessQual && AccessQual->isWriteOnly())) {
1113       S.Diag(Arg0->getBeginLoc(),
1114              diag::err_opencl_builtin_pipe_invalid_access_modifier)
1115           << "write_only" << Arg0->getSourceRange();
1116       return true;
1117     }
1118     break;
1119   default:
1120     break;
1121   }
1122   return false;
1123 }
1124 
1125 /// Returns true if pipe element type is different from the pointer.
1126 static bool checkOpenCLPipePacketType(Sema &S, CallExpr *Call, unsigned Idx) {
1127   const Expr *Arg0 = Call->getArg(0);
1128   const Expr *ArgIdx = Call->getArg(Idx);
1129   const PipeType *PipeTy = cast<PipeType>(Arg0->getType());
1130   const QualType EltTy = PipeTy->getElementType();
1131   const PointerType *ArgTy = ArgIdx->getType()->getAs<PointerType>();
1132   // The Idx argument should be a pointer and the type of the pointer and
1133   // the type of pipe element should also be the same.
1134   if (!ArgTy ||
1135       !S.Context.hasSameType(
1136           EltTy, ArgTy->getPointeeType()->getCanonicalTypeInternal())) {
1137     S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg)
1138         << Call->getDirectCallee() << S.Context.getPointerType(EltTy)
1139         << ArgIdx->getType() << ArgIdx->getSourceRange();
1140     return true;
1141   }
1142   return false;
1143 }
1144 
1145 // Performs semantic analysis for the read/write_pipe call.
1146 // \param S Reference to the semantic analyzer.
1147 // \param Call A pointer to the builtin call.
1148 // \return True if a semantic error has been found, false otherwise.
1149 static bool SemaBuiltinRWPipe(Sema &S, CallExpr *Call) {
1150   // OpenCL v2.0 s6.13.16.2 - The built-in read/write
1151   // functions have two forms.
1152   switch (Call->getNumArgs()) {
1153   case 2:
1154     if (checkOpenCLPipeArg(S, Call))
1155       return true;
1156     // The call with 2 arguments should be
1157     // read/write_pipe(pipe T, T*).
1158     // Check packet type T.
1159     if (checkOpenCLPipePacketType(S, Call, 1))
1160       return true;
1161     break;
1162 
1163   case 4: {
1164     if (checkOpenCLPipeArg(S, Call))
1165       return true;
1166     // The call with 4 arguments should be
1167     // read/write_pipe(pipe T, reserve_id_t, uint, T*).
1168     // Check reserve_id_t.
1169     if (!Call->getArg(1)->getType()->isReserveIDT()) {
1170       S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg)
1171           << Call->getDirectCallee() << S.Context.OCLReserveIDTy
1172           << Call->getArg(1)->getType() << Call->getArg(1)->getSourceRange();
1173       return true;
1174     }
1175 
1176     // Check the index.
1177     const Expr *Arg2 = Call->getArg(2);
1178     if (!Arg2->getType()->isIntegerType() &&
1179         !Arg2->getType()->isUnsignedIntegerType()) {
1180       S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg)
1181           << Call->getDirectCallee() << S.Context.UnsignedIntTy
1182           << Arg2->getType() << Arg2->getSourceRange();
1183       return true;
1184     }
1185 
1186     // Check packet type T.
1187     if (checkOpenCLPipePacketType(S, Call, 3))
1188       return true;
1189   } break;
1190   default:
1191     S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_arg_num)
1192         << Call->getDirectCallee() << Call->getSourceRange();
1193     return true;
1194   }
1195 
1196   return false;
1197 }
1198 
1199 // Performs a semantic analysis on the {work_group_/sub_group_
1200 //        /_}reserve_{read/write}_pipe
1201 // \param S Reference to the semantic analyzer.
1202 // \param Call The call to the builtin function to be analyzed.
1203 // \return True if a semantic error was found, false otherwise.
1204 static bool SemaBuiltinReserveRWPipe(Sema &S, CallExpr *Call) {
1205   if (checkArgCount(S, Call, 2))
1206     return true;
1207 
1208   if (checkOpenCLPipeArg(S, Call))
1209     return true;
1210 
1211   // Check the reserve size.
1212   if (!Call->getArg(1)->getType()->isIntegerType() &&
1213       !Call->getArg(1)->getType()->isUnsignedIntegerType()) {
1214     S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg)
1215         << Call->getDirectCallee() << S.Context.UnsignedIntTy
1216         << Call->getArg(1)->getType() << Call->getArg(1)->getSourceRange();
1217     return true;
1218   }
1219 
1220   // Since return type of reserve_read/write_pipe built-in function is
1221   // reserve_id_t, which is not defined in the builtin def file , we used int
1222   // as return type and need to override the return type of these functions.
1223   Call->setType(S.Context.OCLReserveIDTy);
1224 
1225   return false;
1226 }
1227 
1228 // Performs a semantic analysis on {work_group_/sub_group_
1229 //        /_}commit_{read/write}_pipe
1230 // \param S Reference to the semantic analyzer.
1231 // \param Call The call to the builtin function to be analyzed.
1232 // \return True if a semantic error was found, false otherwise.
1233 static bool SemaBuiltinCommitRWPipe(Sema &S, CallExpr *Call) {
1234   if (checkArgCount(S, Call, 2))
1235     return true;
1236 
1237   if (checkOpenCLPipeArg(S, Call))
1238     return true;
1239 
1240   // Check reserve_id_t.
1241   if (!Call->getArg(1)->getType()->isReserveIDT()) {
1242     S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg)
1243         << Call->getDirectCallee() << S.Context.OCLReserveIDTy
1244         << Call->getArg(1)->getType() << Call->getArg(1)->getSourceRange();
1245     return true;
1246   }
1247 
1248   return false;
1249 }
1250 
1251 // Performs a semantic analysis on the call to built-in Pipe
1252 //        Query Functions.
1253 // \param S Reference to the semantic analyzer.
1254 // \param Call The call to the builtin function to be analyzed.
1255 // \return True if a semantic error was found, false otherwise.
1256 static bool SemaBuiltinPipePackets(Sema &S, CallExpr *Call) {
1257   if (checkArgCount(S, Call, 1))
1258     return true;
1259 
1260   if (!Call->getArg(0)->getType()->isPipeType()) {
1261     S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_first_arg)
1262         << Call->getDirectCallee() << Call->getArg(0)->getSourceRange();
1263     return true;
1264   }
1265 
1266   return false;
1267 }
1268 
1269 // OpenCL v2.0 s6.13.9 - Address space qualifier functions.
1270 // Performs semantic analysis for the to_global/local/private call.
1271 // \param S Reference to the semantic analyzer.
1272 // \param BuiltinID ID of the builtin function.
1273 // \param Call A pointer to the builtin call.
1274 // \return True if a semantic error has been found, false otherwise.
1275 static bool SemaOpenCLBuiltinToAddr(Sema &S, unsigned BuiltinID,
1276                                     CallExpr *Call) {
1277   if (Call->getNumArgs() != 1) {
1278     S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_to_addr_arg_num)
1279         << Call->getDirectCallee() << Call->getSourceRange();
1280     return true;
1281   }
1282 
1283   auto RT = Call->getArg(0)->getType();
1284   if (!RT->isPointerType() || RT->getPointeeType()
1285       .getAddressSpace() == LangAS::opencl_constant) {
1286     S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_to_addr_invalid_arg)
1287         << Call->getArg(0) << Call->getDirectCallee() << Call->getSourceRange();
1288     return true;
1289   }
1290 
1291   if (RT->getPointeeType().getAddressSpace() != LangAS::opencl_generic) {
1292     S.Diag(Call->getArg(0)->getBeginLoc(),
1293            diag::warn_opencl_generic_address_space_arg)
1294         << Call->getDirectCallee()->getNameInfo().getAsString()
1295         << Call->getArg(0)->getSourceRange();
1296   }
1297 
1298   RT = RT->getPointeeType();
1299   auto Qual = RT.getQualifiers();
1300   switch (BuiltinID) {
1301   case Builtin::BIto_global:
1302     Qual.setAddressSpace(LangAS::opencl_global);
1303     break;
1304   case Builtin::BIto_local:
1305     Qual.setAddressSpace(LangAS::opencl_local);
1306     break;
1307   case Builtin::BIto_private:
1308     Qual.setAddressSpace(LangAS::opencl_private);
1309     break;
1310   default:
1311     llvm_unreachable("Invalid builtin function");
1312   }
1313   Call->setType(S.Context.getPointerType(S.Context.getQualifiedType(
1314       RT.getUnqualifiedType(), Qual)));
1315 
1316   return false;
1317 }
1318 
1319 static ExprResult SemaBuiltinLaunder(Sema &S, CallExpr *TheCall) {
1320   if (checkArgCount(S, TheCall, 1))
1321     return ExprError();
1322 
1323   // Compute __builtin_launder's parameter type from the argument.
1324   // The parameter type is:
1325   //  * The type of the argument if it's not an array or function type,
1326   //  Otherwise,
1327   //  * The decayed argument type.
1328   QualType ParamTy = [&]() {
1329     QualType ArgTy = TheCall->getArg(0)->getType();
1330     if (const ArrayType *Ty = ArgTy->getAsArrayTypeUnsafe())
1331       return S.Context.getPointerType(Ty->getElementType());
1332     if (ArgTy->isFunctionType()) {
1333       return S.Context.getPointerType(ArgTy);
1334     }
1335     return ArgTy;
1336   }();
1337 
1338   TheCall->setType(ParamTy);
1339 
1340   auto DiagSelect = [&]() -> llvm::Optional<unsigned> {
1341     if (!ParamTy->isPointerType())
1342       return 0;
1343     if (ParamTy->isFunctionPointerType())
1344       return 1;
1345     if (ParamTy->isVoidPointerType())
1346       return 2;
1347     return llvm::Optional<unsigned>{};
1348   }();
1349   if (DiagSelect.hasValue()) {
1350     S.Diag(TheCall->getBeginLoc(), diag::err_builtin_launder_invalid_arg)
1351         << DiagSelect.getValue() << TheCall->getSourceRange();
1352     return ExprError();
1353   }
1354 
1355   // We either have an incomplete class type, or we have a class template
1356   // whose instantiation has not been forced. Example:
1357   //
1358   //   template <class T> struct Foo { T value; };
1359   //   Foo<int> *p = nullptr;
1360   //   auto *d = __builtin_launder(p);
1361   if (S.RequireCompleteType(TheCall->getBeginLoc(), ParamTy->getPointeeType(),
1362                             diag::err_incomplete_type))
1363     return ExprError();
1364 
1365   assert(ParamTy->getPointeeType()->isObjectType() &&
1366          "Unhandled non-object pointer case");
1367 
1368   InitializedEntity Entity =
1369       InitializedEntity::InitializeParameter(S.Context, ParamTy, false);
1370   ExprResult Arg =
1371       S.PerformCopyInitialization(Entity, SourceLocation(), TheCall->getArg(0));
1372   if (Arg.isInvalid())
1373     return ExprError();
1374   TheCall->setArg(0, Arg.get());
1375 
1376   return TheCall;
1377 }
1378 
1379 // Emit an error and return true if the current architecture is not in the list
1380 // of supported architectures.
1381 static bool
1382 CheckBuiltinTargetSupport(Sema &S, unsigned BuiltinID, CallExpr *TheCall,
1383                           ArrayRef<llvm::Triple::ArchType> SupportedArchs) {
1384   llvm::Triple::ArchType CurArch =
1385       S.getASTContext().getTargetInfo().getTriple().getArch();
1386   if (llvm::is_contained(SupportedArchs, CurArch))
1387     return false;
1388   S.Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported)
1389       << TheCall->getSourceRange();
1390   return true;
1391 }
1392 
1393 static void CheckNonNullArgument(Sema &S, const Expr *ArgExpr,
1394                                  SourceLocation CallSiteLoc);
1395 
1396 bool Sema::CheckTSBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
1397                                       CallExpr *TheCall) {
1398   switch (TI.getTriple().getArch()) {
1399   default:
1400     // Some builtins don't require additional checking, so just consider these
1401     // acceptable.
1402     return false;
1403   case llvm::Triple::arm:
1404   case llvm::Triple::armeb:
1405   case llvm::Triple::thumb:
1406   case llvm::Triple::thumbeb:
1407     return CheckARMBuiltinFunctionCall(TI, BuiltinID, TheCall);
1408   case llvm::Triple::aarch64:
1409   case llvm::Triple::aarch64_32:
1410   case llvm::Triple::aarch64_be:
1411     return CheckAArch64BuiltinFunctionCall(TI, BuiltinID, TheCall);
1412   case llvm::Triple::bpfeb:
1413   case llvm::Triple::bpfel:
1414     return CheckBPFBuiltinFunctionCall(BuiltinID, TheCall);
1415   case llvm::Triple::hexagon:
1416     return CheckHexagonBuiltinFunctionCall(BuiltinID, TheCall);
1417   case llvm::Triple::mips:
1418   case llvm::Triple::mipsel:
1419   case llvm::Triple::mips64:
1420   case llvm::Triple::mips64el:
1421     return CheckMipsBuiltinFunctionCall(TI, BuiltinID, TheCall);
1422   case llvm::Triple::systemz:
1423     return CheckSystemZBuiltinFunctionCall(BuiltinID, TheCall);
1424   case llvm::Triple::x86:
1425   case llvm::Triple::x86_64:
1426     return CheckX86BuiltinFunctionCall(TI, BuiltinID, TheCall);
1427   case llvm::Triple::ppc:
1428   case llvm::Triple::ppc64:
1429   case llvm::Triple::ppc64le:
1430     return CheckPPCBuiltinFunctionCall(TI, BuiltinID, TheCall);
1431   case llvm::Triple::amdgcn:
1432     return CheckAMDGCNBuiltinFunctionCall(BuiltinID, TheCall);
1433   }
1434 }
1435 
1436 ExprResult
1437 Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
1438                                CallExpr *TheCall) {
1439   ExprResult TheCallResult(TheCall);
1440 
1441   // Find out if any arguments are required to be integer constant expressions.
1442   unsigned ICEArguments = 0;
1443   ASTContext::GetBuiltinTypeError Error;
1444   Context.GetBuiltinType(BuiltinID, Error, &ICEArguments);
1445   if (Error != ASTContext::GE_None)
1446     ICEArguments = 0;  // Don't diagnose previously diagnosed errors.
1447 
1448   // If any arguments are required to be ICE's, check and diagnose.
1449   for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) {
1450     // Skip arguments not required to be ICE's.
1451     if ((ICEArguments & (1 << ArgNo)) == 0) continue;
1452 
1453     llvm::APSInt Result;
1454     if (SemaBuiltinConstantArg(TheCall, ArgNo, Result))
1455       return true;
1456     ICEArguments &= ~(1 << ArgNo);
1457   }
1458 
1459   switch (BuiltinID) {
1460   case Builtin::BI__builtin___CFStringMakeConstantString:
1461     assert(TheCall->getNumArgs() == 1 &&
1462            "Wrong # arguments to builtin CFStringMakeConstantString");
1463     if (CheckObjCString(TheCall->getArg(0)))
1464       return ExprError();
1465     break;
1466   case Builtin::BI__builtin_ms_va_start:
1467   case Builtin::BI__builtin_stdarg_start:
1468   case Builtin::BI__builtin_va_start:
1469     if (SemaBuiltinVAStart(BuiltinID, TheCall))
1470       return ExprError();
1471     break;
1472   case Builtin::BI__va_start: {
1473     switch (Context.getTargetInfo().getTriple().getArch()) {
1474     case llvm::Triple::aarch64:
1475     case llvm::Triple::arm:
1476     case llvm::Triple::thumb:
1477       if (SemaBuiltinVAStartARMMicrosoft(TheCall))
1478         return ExprError();
1479       break;
1480     default:
1481       if (SemaBuiltinVAStart(BuiltinID, TheCall))
1482         return ExprError();
1483       break;
1484     }
1485     break;
1486   }
1487 
1488   // The acquire, release, and no fence variants are ARM and AArch64 only.
1489   case Builtin::BI_interlockedbittestandset_acq:
1490   case Builtin::BI_interlockedbittestandset_rel:
1491   case Builtin::BI_interlockedbittestandset_nf:
1492   case Builtin::BI_interlockedbittestandreset_acq:
1493   case Builtin::BI_interlockedbittestandreset_rel:
1494   case Builtin::BI_interlockedbittestandreset_nf:
1495     if (CheckBuiltinTargetSupport(
1496             *this, BuiltinID, TheCall,
1497             {llvm::Triple::arm, llvm::Triple::thumb, llvm::Triple::aarch64}))
1498       return ExprError();
1499     break;
1500 
1501   // The 64-bit bittest variants are x64, ARM, and AArch64 only.
1502   case Builtin::BI_bittest64:
1503   case Builtin::BI_bittestandcomplement64:
1504   case Builtin::BI_bittestandreset64:
1505   case Builtin::BI_bittestandset64:
1506   case Builtin::BI_interlockedbittestandreset64:
1507   case Builtin::BI_interlockedbittestandset64:
1508     if (CheckBuiltinTargetSupport(*this, BuiltinID, TheCall,
1509                                   {llvm::Triple::x86_64, llvm::Triple::arm,
1510                                    llvm::Triple::thumb, llvm::Triple::aarch64}))
1511       return ExprError();
1512     break;
1513 
1514   case Builtin::BI__builtin_isgreater:
1515   case Builtin::BI__builtin_isgreaterequal:
1516   case Builtin::BI__builtin_isless:
1517   case Builtin::BI__builtin_islessequal:
1518   case Builtin::BI__builtin_islessgreater:
1519   case Builtin::BI__builtin_isunordered:
1520     if (SemaBuiltinUnorderedCompare(TheCall))
1521       return ExprError();
1522     break;
1523   case Builtin::BI__builtin_fpclassify:
1524     if (SemaBuiltinFPClassification(TheCall, 6))
1525       return ExprError();
1526     break;
1527   case Builtin::BI__builtin_isfinite:
1528   case Builtin::BI__builtin_isinf:
1529   case Builtin::BI__builtin_isinf_sign:
1530   case Builtin::BI__builtin_isnan:
1531   case Builtin::BI__builtin_isnormal:
1532   case Builtin::BI__builtin_signbit:
1533   case Builtin::BI__builtin_signbitf:
1534   case Builtin::BI__builtin_signbitl:
1535     if (SemaBuiltinFPClassification(TheCall, 1))
1536       return ExprError();
1537     break;
1538   case Builtin::BI__builtin_shufflevector:
1539     return SemaBuiltinShuffleVector(TheCall);
1540     // TheCall will be freed by the smart pointer here, but that's fine, since
1541     // SemaBuiltinShuffleVector guts it, but then doesn't release it.
1542   case Builtin::BI__builtin_prefetch:
1543     if (SemaBuiltinPrefetch(TheCall))
1544       return ExprError();
1545     break;
1546   case Builtin::BI__builtin_alloca_with_align:
1547     if (SemaBuiltinAllocaWithAlign(TheCall))
1548       return ExprError();
1549     LLVM_FALLTHROUGH;
1550   case Builtin::BI__builtin_alloca:
1551     Diag(TheCall->getBeginLoc(), diag::warn_alloca)
1552         << TheCall->getDirectCallee();
1553     break;
1554   case Builtin::BI__assume:
1555   case Builtin::BI__builtin_assume:
1556     if (SemaBuiltinAssume(TheCall))
1557       return ExprError();
1558     break;
1559   case Builtin::BI__builtin_assume_aligned:
1560     if (SemaBuiltinAssumeAligned(TheCall))
1561       return ExprError();
1562     break;
1563   case Builtin::BI__builtin_dynamic_object_size:
1564   case Builtin::BI__builtin_object_size:
1565     if (SemaBuiltinConstantArgRange(TheCall, 1, 0, 3))
1566       return ExprError();
1567     break;
1568   case Builtin::BI__builtin_longjmp:
1569     if (SemaBuiltinLongjmp(TheCall))
1570       return ExprError();
1571     break;
1572   case Builtin::BI__builtin_setjmp:
1573     if (SemaBuiltinSetjmp(TheCall))
1574       return ExprError();
1575     break;
1576   case Builtin::BI__builtin_classify_type:
1577     if (checkArgCount(*this, TheCall, 1)) return true;
1578     TheCall->setType(Context.IntTy);
1579     break;
1580   case Builtin::BI__builtin_constant_p: {
1581     if (checkArgCount(*this, TheCall, 1)) return true;
1582     ExprResult Arg = DefaultFunctionArrayLvalueConversion(TheCall->getArg(0));
1583     if (Arg.isInvalid()) return true;
1584     TheCall->setArg(0, Arg.get());
1585     TheCall->setType(Context.IntTy);
1586     break;
1587   }
1588   case Builtin::BI__builtin_launder:
1589     return SemaBuiltinLaunder(*this, TheCall);
1590   case Builtin::BI__sync_fetch_and_add:
1591   case Builtin::BI__sync_fetch_and_add_1:
1592   case Builtin::BI__sync_fetch_and_add_2:
1593   case Builtin::BI__sync_fetch_and_add_4:
1594   case Builtin::BI__sync_fetch_and_add_8:
1595   case Builtin::BI__sync_fetch_and_add_16:
1596   case Builtin::BI__sync_fetch_and_sub:
1597   case Builtin::BI__sync_fetch_and_sub_1:
1598   case Builtin::BI__sync_fetch_and_sub_2:
1599   case Builtin::BI__sync_fetch_and_sub_4:
1600   case Builtin::BI__sync_fetch_and_sub_8:
1601   case Builtin::BI__sync_fetch_and_sub_16:
1602   case Builtin::BI__sync_fetch_and_or:
1603   case Builtin::BI__sync_fetch_and_or_1:
1604   case Builtin::BI__sync_fetch_and_or_2:
1605   case Builtin::BI__sync_fetch_and_or_4:
1606   case Builtin::BI__sync_fetch_and_or_8:
1607   case Builtin::BI__sync_fetch_and_or_16:
1608   case Builtin::BI__sync_fetch_and_and:
1609   case Builtin::BI__sync_fetch_and_and_1:
1610   case Builtin::BI__sync_fetch_and_and_2:
1611   case Builtin::BI__sync_fetch_and_and_4:
1612   case Builtin::BI__sync_fetch_and_and_8:
1613   case Builtin::BI__sync_fetch_and_and_16:
1614   case Builtin::BI__sync_fetch_and_xor:
1615   case Builtin::BI__sync_fetch_and_xor_1:
1616   case Builtin::BI__sync_fetch_and_xor_2:
1617   case Builtin::BI__sync_fetch_and_xor_4:
1618   case Builtin::BI__sync_fetch_and_xor_8:
1619   case Builtin::BI__sync_fetch_and_xor_16:
1620   case Builtin::BI__sync_fetch_and_nand:
1621   case Builtin::BI__sync_fetch_and_nand_1:
1622   case Builtin::BI__sync_fetch_and_nand_2:
1623   case Builtin::BI__sync_fetch_and_nand_4:
1624   case Builtin::BI__sync_fetch_and_nand_8:
1625   case Builtin::BI__sync_fetch_and_nand_16:
1626   case Builtin::BI__sync_add_and_fetch:
1627   case Builtin::BI__sync_add_and_fetch_1:
1628   case Builtin::BI__sync_add_and_fetch_2:
1629   case Builtin::BI__sync_add_and_fetch_4:
1630   case Builtin::BI__sync_add_and_fetch_8:
1631   case Builtin::BI__sync_add_and_fetch_16:
1632   case Builtin::BI__sync_sub_and_fetch:
1633   case Builtin::BI__sync_sub_and_fetch_1:
1634   case Builtin::BI__sync_sub_and_fetch_2:
1635   case Builtin::BI__sync_sub_and_fetch_4:
1636   case Builtin::BI__sync_sub_and_fetch_8:
1637   case Builtin::BI__sync_sub_and_fetch_16:
1638   case Builtin::BI__sync_and_and_fetch:
1639   case Builtin::BI__sync_and_and_fetch_1:
1640   case Builtin::BI__sync_and_and_fetch_2:
1641   case Builtin::BI__sync_and_and_fetch_4:
1642   case Builtin::BI__sync_and_and_fetch_8:
1643   case Builtin::BI__sync_and_and_fetch_16:
1644   case Builtin::BI__sync_or_and_fetch:
1645   case Builtin::BI__sync_or_and_fetch_1:
1646   case Builtin::BI__sync_or_and_fetch_2:
1647   case Builtin::BI__sync_or_and_fetch_4:
1648   case Builtin::BI__sync_or_and_fetch_8:
1649   case Builtin::BI__sync_or_and_fetch_16:
1650   case Builtin::BI__sync_xor_and_fetch:
1651   case Builtin::BI__sync_xor_and_fetch_1:
1652   case Builtin::BI__sync_xor_and_fetch_2:
1653   case Builtin::BI__sync_xor_and_fetch_4:
1654   case Builtin::BI__sync_xor_and_fetch_8:
1655   case Builtin::BI__sync_xor_and_fetch_16:
1656   case Builtin::BI__sync_nand_and_fetch:
1657   case Builtin::BI__sync_nand_and_fetch_1:
1658   case Builtin::BI__sync_nand_and_fetch_2:
1659   case Builtin::BI__sync_nand_and_fetch_4:
1660   case Builtin::BI__sync_nand_and_fetch_8:
1661   case Builtin::BI__sync_nand_and_fetch_16:
1662   case Builtin::BI__sync_val_compare_and_swap:
1663   case Builtin::BI__sync_val_compare_and_swap_1:
1664   case Builtin::BI__sync_val_compare_and_swap_2:
1665   case Builtin::BI__sync_val_compare_and_swap_4:
1666   case Builtin::BI__sync_val_compare_and_swap_8:
1667   case Builtin::BI__sync_val_compare_and_swap_16:
1668   case Builtin::BI__sync_bool_compare_and_swap:
1669   case Builtin::BI__sync_bool_compare_and_swap_1:
1670   case Builtin::BI__sync_bool_compare_and_swap_2:
1671   case Builtin::BI__sync_bool_compare_and_swap_4:
1672   case Builtin::BI__sync_bool_compare_and_swap_8:
1673   case Builtin::BI__sync_bool_compare_and_swap_16:
1674   case Builtin::BI__sync_lock_test_and_set:
1675   case Builtin::BI__sync_lock_test_and_set_1:
1676   case Builtin::BI__sync_lock_test_and_set_2:
1677   case Builtin::BI__sync_lock_test_and_set_4:
1678   case Builtin::BI__sync_lock_test_and_set_8:
1679   case Builtin::BI__sync_lock_test_and_set_16:
1680   case Builtin::BI__sync_lock_release:
1681   case Builtin::BI__sync_lock_release_1:
1682   case Builtin::BI__sync_lock_release_2:
1683   case Builtin::BI__sync_lock_release_4:
1684   case Builtin::BI__sync_lock_release_8:
1685   case Builtin::BI__sync_lock_release_16:
1686   case Builtin::BI__sync_swap:
1687   case Builtin::BI__sync_swap_1:
1688   case Builtin::BI__sync_swap_2:
1689   case Builtin::BI__sync_swap_4:
1690   case Builtin::BI__sync_swap_8:
1691   case Builtin::BI__sync_swap_16:
1692     return SemaBuiltinAtomicOverloaded(TheCallResult);
1693   case Builtin::BI__sync_synchronize:
1694     Diag(TheCall->getBeginLoc(), diag::warn_atomic_implicit_seq_cst)
1695         << TheCall->getCallee()->getSourceRange();
1696     break;
1697   case Builtin::BI__builtin_nontemporal_load:
1698   case Builtin::BI__builtin_nontemporal_store:
1699     return SemaBuiltinNontemporalOverloaded(TheCallResult);
1700   case Builtin::BI__builtin_memcpy_inline: {
1701     clang::Expr *SizeOp = TheCall->getArg(2);
1702     // We warn about copying to or from `nullptr` pointers when `size` is
1703     // greater than 0. When `size` is value dependent we cannot evaluate its
1704     // value so we bail out.
1705     if (SizeOp->isValueDependent())
1706       break;
1707     if (!SizeOp->EvaluateKnownConstInt(Context).isNullValue()) {
1708       CheckNonNullArgument(*this, TheCall->getArg(0), TheCall->getExprLoc());
1709       CheckNonNullArgument(*this, TheCall->getArg(1), TheCall->getExprLoc());
1710     }
1711     break;
1712   }
1713 #define BUILTIN(ID, TYPE, ATTRS)
1714 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1715   case Builtin::BI##ID: \
1716     return SemaAtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID);
1717 #include "clang/Basic/Builtins.def"
1718   case Builtin::BI__annotation:
1719     if (SemaBuiltinMSVCAnnotation(*this, TheCall))
1720       return ExprError();
1721     break;
1722   case Builtin::BI__builtin_annotation:
1723     if (SemaBuiltinAnnotation(*this, TheCall))
1724       return ExprError();
1725     break;
1726   case Builtin::BI__builtin_addressof:
1727     if (SemaBuiltinAddressof(*this, TheCall))
1728       return ExprError();
1729     break;
1730   case Builtin::BI__builtin_is_aligned:
1731   case Builtin::BI__builtin_align_up:
1732   case Builtin::BI__builtin_align_down:
1733     if (SemaBuiltinAlignment(*this, TheCall, BuiltinID))
1734       return ExprError();
1735     break;
1736   case Builtin::BI__builtin_add_overflow:
1737   case Builtin::BI__builtin_sub_overflow:
1738   case Builtin::BI__builtin_mul_overflow:
1739     if (SemaBuiltinOverflow(*this, TheCall, BuiltinID))
1740       return ExprError();
1741     break;
1742   case Builtin::BI__builtin_operator_new:
1743   case Builtin::BI__builtin_operator_delete: {
1744     bool IsDelete = BuiltinID == Builtin::BI__builtin_operator_delete;
1745     ExprResult Res =
1746         SemaBuiltinOperatorNewDeleteOverloaded(TheCallResult, IsDelete);
1747     if (Res.isInvalid())
1748       CorrectDelayedTyposInExpr(TheCallResult.get());
1749     return Res;
1750   }
1751   case Builtin::BI__builtin_dump_struct: {
1752     // We first want to ensure we are called with 2 arguments
1753     if (checkArgCount(*this, TheCall, 2))
1754       return ExprError();
1755     // Ensure that the first argument is of type 'struct XX *'
1756     const Expr *PtrArg = TheCall->getArg(0)->IgnoreParenImpCasts();
1757     const QualType PtrArgType = PtrArg->getType();
1758     if (!PtrArgType->isPointerType() ||
1759         !PtrArgType->getPointeeType()->isRecordType()) {
1760       Diag(PtrArg->getBeginLoc(), diag::err_typecheck_convert_incompatible)
1761           << PtrArgType << "structure pointer" << 1 << 0 << 3 << 1 << PtrArgType
1762           << "structure pointer";
1763       return ExprError();
1764     }
1765 
1766     // Ensure that the second argument is of type 'FunctionType'
1767     const Expr *FnPtrArg = TheCall->getArg(1)->IgnoreImpCasts();
1768     const QualType FnPtrArgType = FnPtrArg->getType();
1769     if (!FnPtrArgType->isPointerType()) {
1770       Diag(FnPtrArg->getBeginLoc(), diag::err_typecheck_convert_incompatible)
1771           << FnPtrArgType << "'int (*)(const char *, ...)'" << 1 << 0 << 3 << 2
1772           << FnPtrArgType << "'int (*)(const char *, ...)'";
1773       return ExprError();
1774     }
1775 
1776     const auto *FuncType =
1777         FnPtrArgType->getPointeeType()->getAs<FunctionType>();
1778 
1779     if (!FuncType) {
1780       Diag(FnPtrArg->getBeginLoc(), diag::err_typecheck_convert_incompatible)
1781           << FnPtrArgType << "'int (*)(const char *, ...)'" << 1 << 0 << 3 << 2
1782           << FnPtrArgType << "'int (*)(const char *, ...)'";
1783       return ExprError();
1784     }
1785 
1786     if (const auto *FT = dyn_cast<FunctionProtoType>(FuncType)) {
1787       if (!FT->getNumParams()) {
1788         Diag(FnPtrArg->getBeginLoc(), diag::err_typecheck_convert_incompatible)
1789             << FnPtrArgType << "'int (*)(const char *, ...)'" << 1 << 0 << 3
1790             << 2 << FnPtrArgType << "'int (*)(const char *, ...)'";
1791         return ExprError();
1792       }
1793       QualType PT = FT->getParamType(0);
1794       if (!FT->isVariadic() || FT->getReturnType() != Context.IntTy ||
1795           !PT->isPointerType() || !PT->getPointeeType()->isCharType() ||
1796           !PT->getPointeeType().isConstQualified()) {
1797         Diag(FnPtrArg->getBeginLoc(), diag::err_typecheck_convert_incompatible)
1798             << FnPtrArgType << "'int (*)(const char *, ...)'" << 1 << 0 << 3
1799             << 2 << FnPtrArgType << "'int (*)(const char *, ...)'";
1800         return ExprError();
1801       }
1802     }
1803 
1804     TheCall->setType(Context.IntTy);
1805     break;
1806   }
1807   case Builtin::BI__builtin_expect_with_probability: {
1808     // We first want to ensure we are called with 3 arguments
1809     if (checkArgCount(*this, TheCall, 3))
1810       return ExprError();
1811     // then check probability is constant float in range [0.0, 1.0]
1812     const Expr *ProbArg = TheCall->getArg(2);
1813     SmallVector<PartialDiagnosticAt, 8> Notes;
1814     Expr::EvalResult Eval;
1815     Eval.Diag = &Notes;
1816     if ((!ProbArg->EvaluateAsConstantExpr(Eval, Expr::EvaluateForCodeGen,
1817                                           Context)) ||
1818         !Eval.Val.isFloat()) {
1819       Diag(ProbArg->getBeginLoc(), diag::err_probability_not_constant_float)
1820           << ProbArg->getSourceRange();
1821       for (const PartialDiagnosticAt &PDiag : Notes)
1822         Diag(PDiag.first, PDiag.second);
1823       return ExprError();
1824     }
1825     llvm::APFloat Probability = Eval.Val.getFloat();
1826     bool LoseInfo = false;
1827     Probability.convert(llvm::APFloat::IEEEdouble(),
1828                         llvm::RoundingMode::Dynamic, &LoseInfo);
1829     if (!(Probability >= llvm::APFloat(0.0) &&
1830           Probability <= llvm::APFloat(1.0))) {
1831       Diag(ProbArg->getBeginLoc(), diag::err_probability_out_of_range)
1832           << ProbArg->getSourceRange();
1833       return ExprError();
1834     }
1835     break;
1836   }
1837   case Builtin::BI__builtin_preserve_access_index:
1838     if (SemaBuiltinPreserveAI(*this, TheCall))
1839       return ExprError();
1840     break;
1841   case Builtin::BI__builtin_call_with_static_chain:
1842     if (SemaBuiltinCallWithStaticChain(*this, TheCall))
1843       return ExprError();
1844     break;
1845   case Builtin::BI__exception_code:
1846   case Builtin::BI_exception_code:
1847     if (SemaBuiltinSEHScopeCheck(*this, TheCall, Scope::SEHExceptScope,
1848                                  diag::err_seh___except_block))
1849       return ExprError();
1850     break;
1851   case Builtin::BI__exception_info:
1852   case Builtin::BI_exception_info:
1853     if (SemaBuiltinSEHScopeCheck(*this, TheCall, Scope::SEHFilterScope,
1854                                  diag::err_seh___except_filter))
1855       return ExprError();
1856     break;
1857   case Builtin::BI__GetExceptionInfo:
1858     if (checkArgCount(*this, TheCall, 1))
1859       return ExprError();
1860 
1861     if (CheckCXXThrowOperand(
1862             TheCall->getBeginLoc(),
1863             Context.getExceptionObjectType(FDecl->getParamDecl(0)->getType()),
1864             TheCall))
1865       return ExprError();
1866 
1867     TheCall->setType(Context.VoidPtrTy);
1868     break;
1869   // OpenCL v2.0, s6.13.16 - Pipe functions
1870   case Builtin::BIread_pipe:
1871   case Builtin::BIwrite_pipe:
1872     // Since those two functions are declared with var args, we need a semantic
1873     // check for the argument.
1874     if (SemaBuiltinRWPipe(*this, TheCall))
1875       return ExprError();
1876     break;
1877   case Builtin::BIreserve_read_pipe:
1878   case Builtin::BIreserve_write_pipe:
1879   case Builtin::BIwork_group_reserve_read_pipe:
1880   case Builtin::BIwork_group_reserve_write_pipe:
1881     if (SemaBuiltinReserveRWPipe(*this, TheCall))
1882       return ExprError();
1883     break;
1884   case Builtin::BIsub_group_reserve_read_pipe:
1885   case Builtin::BIsub_group_reserve_write_pipe:
1886     if (checkOpenCLSubgroupExt(*this, TheCall) ||
1887         SemaBuiltinReserveRWPipe(*this, TheCall))
1888       return ExprError();
1889     break;
1890   case Builtin::BIcommit_read_pipe:
1891   case Builtin::BIcommit_write_pipe:
1892   case Builtin::BIwork_group_commit_read_pipe:
1893   case Builtin::BIwork_group_commit_write_pipe:
1894     if (SemaBuiltinCommitRWPipe(*this, TheCall))
1895       return ExprError();
1896     break;
1897   case Builtin::BIsub_group_commit_read_pipe:
1898   case Builtin::BIsub_group_commit_write_pipe:
1899     if (checkOpenCLSubgroupExt(*this, TheCall) ||
1900         SemaBuiltinCommitRWPipe(*this, TheCall))
1901       return ExprError();
1902     break;
1903   case Builtin::BIget_pipe_num_packets:
1904   case Builtin::BIget_pipe_max_packets:
1905     if (SemaBuiltinPipePackets(*this, TheCall))
1906       return ExprError();
1907     break;
1908   case Builtin::BIto_global:
1909   case Builtin::BIto_local:
1910   case Builtin::BIto_private:
1911     if (SemaOpenCLBuiltinToAddr(*this, BuiltinID, TheCall))
1912       return ExprError();
1913     break;
1914   // OpenCL v2.0, s6.13.17 - Enqueue kernel functions.
1915   case Builtin::BIenqueue_kernel:
1916     if (SemaOpenCLBuiltinEnqueueKernel(*this, TheCall))
1917       return ExprError();
1918     break;
1919   case Builtin::BIget_kernel_work_group_size:
1920   case Builtin::BIget_kernel_preferred_work_group_size_multiple:
1921     if (SemaOpenCLBuiltinKernelWorkGroupSize(*this, TheCall))
1922       return ExprError();
1923     break;
1924   case Builtin::BIget_kernel_max_sub_group_size_for_ndrange:
1925   case Builtin::BIget_kernel_sub_group_count_for_ndrange:
1926     if (SemaOpenCLBuiltinNDRangeAndBlock(*this, TheCall))
1927       return ExprError();
1928     break;
1929   case Builtin::BI__builtin_os_log_format:
1930     Cleanup.setExprNeedsCleanups(true);
1931     LLVM_FALLTHROUGH;
1932   case Builtin::BI__builtin_os_log_format_buffer_size:
1933     if (SemaBuiltinOSLogFormat(TheCall))
1934       return ExprError();
1935     break;
1936   case Builtin::BI__builtin_frame_address:
1937   case Builtin::BI__builtin_return_address: {
1938     if (SemaBuiltinConstantArgRange(TheCall, 0, 0, 0xFFFF))
1939       return ExprError();
1940 
1941     // -Wframe-address warning if non-zero passed to builtin
1942     // return/frame address.
1943     Expr::EvalResult Result;
1944     if (TheCall->getArg(0)->EvaluateAsInt(Result, getASTContext()) &&
1945         Result.Val.getInt() != 0)
1946       Diag(TheCall->getBeginLoc(), diag::warn_frame_address)
1947           << ((BuiltinID == Builtin::BI__builtin_return_address)
1948                   ? "__builtin_return_address"
1949                   : "__builtin_frame_address")
1950           << TheCall->getSourceRange();
1951     break;
1952   }
1953 
1954   case Builtin::BI__builtin_matrix_transpose:
1955     return SemaBuiltinMatrixTranspose(TheCall, TheCallResult);
1956 
1957   case Builtin::BI__builtin_matrix_column_major_load:
1958     return SemaBuiltinMatrixColumnMajorLoad(TheCall, TheCallResult);
1959 
1960   case Builtin::BI__builtin_matrix_column_major_store:
1961     return SemaBuiltinMatrixColumnMajorStore(TheCall, TheCallResult);
1962   }
1963 
1964   // Since the target specific builtins for each arch overlap, only check those
1965   // of the arch we are compiling for.
1966   if (Context.BuiltinInfo.isTSBuiltin(BuiltinID)) {
1967     if (Context.BuiltinInfo.isAuxBuiltinID(BuiltinID)) {
1968       assert(Context.getAuxTargetInfo() &&
1969              "Aux Target Builtin, but not an aux target?");
1970 
1971       if (CheckTSBuiltinFunctionCall(
1972               *Context.getAuxTargetInfo(),
1973               Context.BuiltinInfo.getAuxBuiltinID(BuiltinID), TheCall))
1974         return ExprError();
1975     } else {
1976       if (CheckTSBuiltinFunctionCall(Context.getTargetInfo(), BuiltinID,
1977                                      TheCall))
1978         return ExprError();
1979     }
1980   }
1981 
1982   return TheCallResult;
1983 }
1984 
1985 // Get the valid immediate range for the specified NEON type code.
1986 static unsigned RFT(unsigned t, bool shift = false, bool ForceQuad = false) {
1987   NeonTypeFlags Type(t);
1988   int IsQuad = ForceQuad ? true : Type.isQuad();
1989   switch (Type.getEltType()) {
1990   case NeonTypeFlags::Int8:
1991   case NeonTypeFlags::Poly8:
1992     return shift ? 7 : (8 << IsQuad) - 1;
1993   case NeonTypeFlags::Int16:
1994   case NeonTypeFlags::Poly16:
1995     return shift ? 15 : (4 << IsQuad) - 1;
1996   case NeonTypeFlags::Int32:
1997     return shift ? 31 : (2 << IsQuad) - 1;
1998   case NeonTypeFlags::Int64:
1999   case NeonTypeFlags::Poly64:
2000     return shift ? 63 : (1 << IsQuad) - 1;
2001   case NeonTypeFlags::Poly128:
2002     return shift ? 127 : (1 << IsQuad) - 1;
2003   case NeonTypeFlags::Float16:
2004     assert(!shift && "cannot shift float types!");
2005     return (4 << IsQuad) - 1;
2006   case NeonTypeFlags::Float32:
2007     assert(!shift && "cannot shift float types!");
2008     return (2 << IsQuad) - 1;
2009   case NeonTypeFlags::Float64:
2010     assert(!shift && "cannot shift float types!");
2011     return (1 << IsQuad) - 1;
2012   case NeonTypeFlags::BFloat16:
2013     assert(!shift && "cannot shift float types!");
2014     return (4 << IsQuad) - 1;
2015   }
2016   llvm_unreachable("Invalid NeonTypeFlag!");
2017 }
2018 
2019 /// getNeonEltType - Return the QualType corresponding to the elements of
2020 /// the vector type specified by the NeonTypeFlags.  This is used to check
2021 /// the pointer arguments for Neon load/store intrinsics.
2022 static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context,
2023                                bool IsPolyUnsigned, bool IsInt64Long) {
2024   switch (Flags.getEltType()) {
2025   case NeonTypeFlags::Int8:
2026     return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy;
2027   case NeonTypeFlags::Int16:
2028     return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy;
2029   case NeonTypeFlags::Int32:
2030     return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy;
2031   case NeonTypeFlags::Int64:
2032     if (IsInt64Long)
2033       return Flags.isUnsigned() ? Context.UnsignedLongTy : Context.LongTy;
2034     else
2035       return Flags.isUnsigned() ? Context.UnsignedLongLongTy
2036                                 : Context.LongLongTy;
2037   case NeonTypeFlags::Poly8:
2038     return IsPolyUnsigned ? Context.UnsignedCharTy : Context.SignedCharTy;
2039   case NeonTypeFlags::Poly16:
2040     return IsPolyUnsigned ? Context.UnsignedShortTy : Context.ShortTy;
2041   case NeonTypeFlags::Poly64:
2042     if (IsInt64Long)
2043       return Context.UnsignedLongTy;
2044     else
2045       return Context.UnsignedLongLongTy;
2046   case NeonTypeFlags::Poly128:
2047     break;
2048   case NeonTypeFlags::Float16:
2049     return Context.HalfTy;
2050   case NeonTypeFlags::Float32:
2051     return Context.FloatTy;
2052   case NeonTypeFlags::Float64:
2053     return Context.DoubleTy;
2054   case NeonTypeFlags::BFloat16:
2055     return Context.BFloat16Ty;
2056   }
2057   llvm_unreachable("Invalid NeonTypeFlag!");
2058 }
2059 
2060 bool Sema::CheckSVEBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
2061   // Range check SVE intrinsics that take immediate values.
2062   SmallVector<std::tuple<int,int,int>, 3> ImmChecks;
2063 
2064   switch (BuiltinID) {
2065   default:
2066     return false;
2067 #define GET_SVE_IMMEDIATE_CHECK
2068 #include "clang/Basic/arm_sve_sema_rangechecks.inc"
2069 #undef GET_SVE_IMMEDIATE_CHECK
2070   }
2071 
2072   // Perform all the immediate checks for this builtin call.
2073   bool HasError = false;
2074   for (auto &I : ImmChecks) {
2075     int ArgNum, CheckTy, ElementSizeInBits;
2076     std::tie(ArgNum, CheckTy, ElementSizeInBits) = I;
2077 
2078     typedef bool(*OptionSetCheckFnTy)(int64_t Value);
2079 
2080     // Function that checks whether the operand (ArgNum) is an immediate
2081     // that is one of the predefined values.
2082     auto CheckImmediateInSet = [&](OptionSetCheckFnTy CheckImm,
2083                                    int ErrDiag) -> bool {
2084       // We can't check the value of a dependent argument.
2085       Expr *Arg = TheCall->getArg(ArgNum);
2086       if (Arg->isTypeDependent() || Arg->isValueDependent())
2087         return false;
2088 
2089       // Check constant-ness first.
2090       llvm::APSInt Imm;
2091       if (SemaBuiltinConstantArg(TheCall, ArgNum, Imm))
2092         return true;
2093 
2094       if (!CheckImm(Imm.getSExtValue()))
2095         return Diag(TheCall->getBeginLoc(), ErrDiag) << Arg->getSourceRange();
2096       return false;
2097     };
2098 
2099     switch ((SVETypeFlags::ImmCheckType)CheckTy) {
2100     case SVETypeFlags::ImmCheck0_31:
2101       if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 31))
2102         HasError = true;
2103       break;
2104     case SVETypeFlags::ImmCheck0_13:
2105       if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 13))
2106         HasError = true;
2107       break;
2108     case SVETypeFlags::ImmCheck1_16:
2109       if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 1, 16))
2110         HasError = true;
2111       break;
2112     case SVETypeFlags::ImmCheck0_7:
2113       if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 7))
2114         HasError = true;
2115       break;
2116     case SVETypeFlags::ImmCheckExtract:
2117       if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0,
2118                                       (2048 / ElementSizeInBits) - 1))
2119         HasError = true;
2120       break;
2121     case SVETypeFlags::ImmCheckShiftRight:
2122       if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 1, ElementSizeInBits))
2123         HasError = true;
2124       break;
2125     case SVETypeFlags::ImmCheckShiftRightNarrow:
2126       if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 1,
2127                                       ElementSizeInBits / 2))
2128         HasError = true;
2129       break;
2130     case SVETypeFlags::ImmCheckShiftLeft:
2131       if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0,
2132                                       ElementSizeInBits - 1))
2133         HasError = true;
2134       break;
2135     case SVETypeFlags::ImmCheckLaneIndex:
2136       if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0,
2137                                       (128 / (1 * ElementSizeInBits)) - 1))
2138         HasError = true;
2139       break;
2140     case SVETypeFlags::ImmCheckLaneIndexCompRotate:
2141       if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0,
2142                                       (128 / (2 * ElementSizeInBits)) - 1))
2143         HasError = true;
2144       break;
2145     case SVETypeFlags::ImmCheckLaneIndexDot:
2146       if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0,
2147                                       (128 / (4 * ElementSizeInBits)) - 1))
2148         HasError = true;
2149       break;
2150     case SVETypeFlags::ImmCheckComplexRot90_270:
2151       if (CheckImmediateInSet([](int64_t V) { return V == 90 || V == 270; },
2152                               diag::err_rotation_argument_to_cadd))
2153         HasError = true;
2154       break;
2155     case SVETypeFlags::ImmCheckComplexRotAll90:
2156       if (CheckImmediateInSet(
2157               [](int64_t V) {
2158                 return V == 0 || V == 90 || V == 180 || V == 270;
2159               },
2160               diag::err_rotation_argument_to_cmla))
2161         HasError = true;
2162       break;
2163     case SVETypeFlags::ImmCheck0_1:
2164       if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 1))
2165         HasError = true;
2166       break;
2167     case SVETypeFlags::ImmCheck0_2:
2168       if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 2))
2169         HasError = true;
2170       break;
2171     case SVETypeFlags::ImmCheck0_3:
2172       if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 3))
2173         HasError = true;
2174       break;
2175     }
2176   }
2177 
2178   return HasError;
2179 }
2180 
2181 bool Sema::CheckNeonBuiltinFunctionCall(const TargetInfo &TI,
2182                                         unsigned BuiltinID, CallExpr *TheCall) {
2183   llvm::APSInt Result;
2184   uint64_t mask = 0;
2185   unsigned TV = 0;
2186   int PtrArgNum = -1;
2187   bool HasConstPtr = false;
2188   switch (BuiltinID) {
2189 #define GET_NEON_OVERLOAD_CHECK
2190 #include "clang/Basic/arm_neon.inc"
2191 #include "clang/Basic/arm_fp16.inc"
2192 #undef GET_NEON_OVERLOAD_CHECK
2193   }
2194 
2195   // For NEON intrinsics which are overloaded on vector element type, validate
2196   // the immediate which specifies which variant to emit.
2197   unsigned ImmArg = TheCall->getNumArgs()-1;
2198   if (mask) {
2199     if (SemaBuiltinConstantArg(TheCall, ImmArg, Result))
2200       return true;
2201 
2202     TV = Result.getLimitedValue(64);
2203     if ((TV > 63) || (mask & (1ULL << TV)) == 0)
2204       return Diag(TheCall->getBeginLoc(), diag::err_invalid_neon_type_code)
2205              << TheCall->getArg(ImmArg)->getSourceRange();
2206   }
2207 
2208   if (PtrArgNum >= 0) {
2209     // Check that pointer arguments have the specified type.
2210     Expr *Arg = TheCall->getArg(PtrArgNum);
2211     if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg))
2212       Arg = ICE->getSubExpr();
2213     ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg);
2214     QualType RHSTy = RHS.get()->getType();
2215 
2216     llvm::Triple::ArchType Arch = TI.getTriple().getArch();
2217     bool IsPolyUnsigned = Arch == llvm::Triple::aarch64 ||
2218                           Arch == llvm::Triple::aarch64_32 ||
2219                           Arch == llvm::Triple::aarch64_be;
2220     bool IsInt64Long = TI.getInt64Type() == TargetInfo::SignedLong;
2221     QualType EltTy =
2222         getNeonEltType(NeonTypeFlags(TV), Context, IsPolyUnsigned, IsInt64Long);
2223     if (HasConstPtr)
2224       EltTy = EltTy.withConst();
2225     QualType LHSTy = Context.getPointerType(EltTy);
2226     AssignConvertType ConvTy;
2227     ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS);
2228     if (RHS.isInvalid())
2229       return true;
2230     if (DiagnoseAssignmentResult(ConvTy, Arg->getBeginLoc(), LHSTy, RHSTy,
2231                                  RHS.get(), AA_Assigning))
2232       return true;
2233   }
2234 
2235   // For NEON intrinsics which take an immediate value as part of the
2236   // instruction, range check them here.
2237   unsigned i = 0, l = 0, u = 0;
2238   switch (BuiltinID) {
2239   default:
2240     return false;
2241   #define GET_NEON_IMMEDIATE_CHECK
2242   #include "clang/Basic/arm_neon.inc"
2243   #include "clang/Basic/arm_fp16.inc"
2244   #undef GET_NEON_IMMEDIATE_CHECK
2245   }
2246 
2247   return SemaBuiltinConstantArgRange(TheCall, i, l, u + l);
2248 }
2249 
2250 bool Sema::CheckMVEBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
2251   switch (BuiltinID) {
2252   default:
2253     return false;
2254   #include "clang/Basic/arm_mve_builtin_sema.inc"
2255   }
2256 }
2257 
2258 bool Sema::CheckCDEBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
2259                                        CallExpr *TheCall) {
2260   bool Err = false;
2261   switch (BuiltinID) {
2262   default:
2263     return false;
2264 #include "clang/Basic/arm_cde_builtin_sema.inc"
2265   }
2266 
2267   if (Err)
2268     return true;
2269 
2270   return CheckARMCoprocessorImmediate(TI, TheCall->getArg(0), /*WantCDE*/ true);
2271 }
2272 
2273 bool Sema::CheckARMCoprocessorImmediate(const TargetInfo &TI,
2274                                         const Expr *CoprocArg, bool WantCDE) {
2275   if (isConstantEvaluated())
2276     return false;
2277 
2278   // We can't check the value of a dependent argument.
2279   if (CoprocArg->isTypeDependent() || CoprocArg->isValueDependent())
2280     return false;
2281 
2282   llvm::APSInt CoprocNoAP;
2283   bool IsICE = CoprocArg->isIntegerConstantExpr(CoprocNoAP, Context);
2284   (void)IsICE;
2285   assert(IsICE && "Coprocossor immediate is not a constant expression");
2286   int64_t CoprocNo = CoprocNoAP.getExtValue();
2287   assert(CoprocNo >= 0 && "Coprocessor immediate must be non-negative");
2288 
2289   uint32_t CDECoprocMask = TI.getARMCDECoprocMask();
2290   bool IsCDECoproc = CoprocNo <= 7 && (CDECoprocMask & (1 << CoprocNo));
2291 
2292   if (IsCDECoproc != WantCDE)
2293     return Diag(CoprocArg->getBeginLoc(), diag::err_arm_invalid_coproc)
2294            << (int)CoprocNo << (int)WantCDE << CoprocArg->getSourceRange();
2295 
2296   return false;
2297 }
2298 
2299 bool Sema::CheckARMBuiltinExclusiveCall(unsigned BuiltinID, CallExpr *TheCall,
2300                                         unsigned MaxWidth) {
2301   assert((BuiltinID == ARM::BI__builtin_arm_ldrex ||
2302           BuiltinID == ARM::BI__builtin_arm_ldaex ||
2303           BuiltinID == ARM::BI__builtin_arm_strex ||
2304           BuiltinID == ARM::BI__builtin_arm_stlex ||
2305           BuiltinID == AArch64::BI__builtin_arm_ldrex ||
2306           BuiltinID == AArch64::BI__builtin_arm_ldaex ||
2307           BuiltinID == AArch64::BI__builtin_arm_strex ||
2308           BuiltinID == AArch64::BI__builtin_arm_stlex) &&
2309          "unexpected ARM builtin");
2310   bool IsLdrex = BuiltinID == ARM::BI__builtin_arm_ldrex ||
2311                  BuiltinID == ARM::BI__builtin_arm_ldaex ||
2312                  BuiltinID == AArch64::BI__builtin_arm_ldrex ||
2313                  BuiltinID == AArch64::BI__builtin_arm_ldaex;
2314 
2315   DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
2316 
2317   // Ensure that we have the proper number of arguments.
2318   if (checkArgCount(*this, TheCall, IsLdrex ? 1 : 2))
2319     return true;
2320 
2321   // Inspect the pointer argument of the atomic builtin.  This should always be
2322   // a pointer type, whose element is an integral scalar or pointer type.
2323   // Because it is a pointer type, we don't have to worry about any implicit
2324   // casts here.
2325   Expr *PointerArg = TheCall->getArg(IsLdrex ? 0 : 1);
2326   ExprResult PointerArgRes = DefaultFunctionArrayLvalueConversion(PointerArg);
2327   if (PointerArgRes.isInvalid())
2328     return true;
2329   PointerArg = PointerArgRes.get();
2330 
2331   const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>();
2332   if (!pointerType) {
2333     Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer)
2334         << PointerArg->getType() << PointerArg->getSourceRange();
2335     return true;
2336   }
2337 
2338   // ldrex takes a "const volatile T*" and strex takes a "volatile T*". Our next
2339   // task is to insert the appropriate casts into the AST. First work out just
2340   // what the appropriate type is.
2341   QualType ValType = pointerType->getPointeeType();
2342   QualType AddrType = ValType.getUnqualifiedType().withVolatile();
2343   if (IsLdrex)
2344     AddrType.addConst();
2345 
2346   // Issue a warning if the cast is dodgy.
2347   CastKind CastNeeded = CK_NoOp;
2348   if (!AddrType.isAtLeastAsQualifiedAs(ValType)) {
2349     CastNeeded = CK_BitCast;
2350     Diag(DRE->getBeginLoc(), diag::ext_typecheck_convert_discards_qualifiers)
2351         << PointerArg->getType() << Context.getPointerType(AddrType)
2352         << AA_Passing << PointerArg->getSourceRange();
2353   }
2354 
2355   // Finally, do the cast and replace the argument with the corrected version.
2356   AddrType = Context.getPointerType(AddrType);
2357   PointerArgRes = ImpCastExprToType(PointerArg, AddrType, CastNeeded);
2358   if (PointerArgRes.isInvalid())
2359     return true;
2360   PointerArg = PointerArgRes.get();
2361 
2362   TheCall->setArg(IsLdrex ? 0 : 1, PointerArg);
2363 
2364   // In general, we allow ints, floats and pointers to be loaded and stored.
2365   if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
2366       !ValType->isBlockPointerType() && !ValType->isFloatingType()) {
2367     Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer_intfltptr)
2368         << PointerArg->getType() << PointerArg->getSourceRange();
2369     return true;
2370   }
2371 
2372   // But ARM doesn't have instructions to deal with 128-bit versions.
2373   if (Context.getTypeSize(ValType) > MaxWidth) {
2374     assert(MaxWidth == 64 && "Diagnostic unexpectedly inaccurate");
2375     Diag(DRE->getBeginLoc(), diag::err_atomic_exclusive_builtin_pointer_size)
2376         << PointerArg->getType() << PointerArg->getSourceRange();
2377     return true;
2378   }
2379 
2380   switch (ValType.getObjCLifetime()) {
2381   case Qualifiers::OCL_None:
2382   case Qualifiers::OCL_ExplicitNone:
2383     // okay
2384     break;
2385 
2386   case Qualifiers::OCL_Weak:
2387   case Qualifiers::OCL_Strong:
2388   case Qualifiers::OCL_Autoreleasing:
2389     Diag(DRE->getBeginLoc(), diag::err_arc_atomic_ownership)
2390         << ValType << PointerArg->getSourceRange();
2391     return true;
2392   }
2393 
2394   if (IsLdrex) {
2395     TheCall->setType(ValType);
2396     return false;
2397   }
2398 
2399   // Initialize the argument to be stored.
2400   ExprResult ValArg = TheCall->getArg(0);
2401   InitializedEntity Entity = InitializedEntity::InitializeParameter(
2402       Context, ValType, /*consume*/ false);
2403   ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg);
2404   if (ValArg.isInvalid())
2405     return true;
2406   TheCall->setArg(0, ValArg.get());
2407 
2408   // __builtin_arm_strex always returns an int. It's marked as such in the .def,
2409   // but the custom checker bypasses all default analysis.
2410   TheCall->setType(Context.IntTy);
2411   return false;
2412 }
2413 
2414 bool Sema::CheckARMBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
2415                                        CallExpr *TheCall) {
2416   if (BuiltinID == ARM::BI__builtin_arm_ldrex ||
2417       BuiltinID == ARM::BI__builtin_arm_ldaex ||
2418       BuiltinID == ARM::BI__builtin_arm_strex ||
2419       BuiltinID == ARM::BI__builtin_arm_stlex) {
2420     return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 64);
2421   }
2422 
2423   if (BuiltinID == ARM::BI__builtin_arm_prefetch) {
2424     return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) ||
2425       SemaBuiltinConstantArgRange(TheCall, 2, 0, 1);
2426   }
2427 
2428   if (BuiltinID == ARM::BI__builtin_arm_rsr64 ||
2429       BuiltinID == ARM::BI__builtin_arm_wsr64)
2430     return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 3, false);
2431 
2432   if (BuiltinID == ARM::BI__builtin_arm_rsr ||
2433       BuiltinID == ARM::BI__builtin_arm_rsrp ||
2434       BuiltinID == ARM::BI__builtin_arm_wsr ||
2435       BuiltinID == ARM::BI__builtin_arm_wsrp)
2436     return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true);
2437 
2438   if (CheckNeonBuiltinFunctionCall(TI, BuiltinID, TheCall))
2439     return true;
2440   if (CheckMVEBuiltinFunctionCall(BuiltinID, TheCall))
2441     return true;
2442   if (CheckCDEBuiltinFunctionCall(TI, BuiltinID, TheCall))
2443     return true;
2444 
2445   // For intrinsics which take an immediate value as part of the instruction,
2446   // range check them here.
2447   // FIXME: VFP Intrinsics should error if VFP not present.
2448   switch (BuiltinID) {
2449   default: return false;
2450   case ARM::BI__builtin_arm_ssat:
2451     return SemaBuiltinConstantArgRange(TheCall, 1, 1, 32);
2452   case ARM::BI__builtin_arm_usat:
2453     return SemaBuiltinConstantArgRange(TheCall, 1, 0, 31);
2454   case ARM::BI__builtin_arm_ssat16:
2455     return SemaBuiltinConstantArgRange(TheCall, 1, 1, 16);
2456   case ARM::BI__builtin_arm_usat16:
2457     return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15);
2458   case ARM::BI__builtin_arm_vcvtr_f:
2459   case ARM::BI__builtin_arm_vcvtr_d:
2460     return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1);
2461   case ARM::BI__builtin_arm_dmb:
2462   case ARM::BI__builtin_arm_dsb:
2463   case ARM::BI__builtin_arm_isb:
2464   case ARM::BI__builtin_arm_dbg:
2465     return SemaBuiltinConstantArgRange(TheCall, 0, 0, 15);
2466   case ARM::BI__builtin_arm_cdp:
2467   case ARM::BI__builtin_arm_cdp2:
2468   case ARM::BI__builtin_arm_mcr:
2469   case ARM::BI__builtin_arm_mcr2:
2470   case ARM::BI__builtin_arm_mrc:
2471   case ARM::BI__builtin_arm_mrc2:
2472   case ARM::BI__builtin_arm_mcrr:
2473   case ARM::BI__builtin_arm_mcrr2:
2474   case ARM::BI__builtin_arm_mrrc:
2475   case ARM::BI__builtin_arm_mrrc2:
2476   case ARM::BI__builtin_arm_ldc:
2477   case ARM::BI__builtin_arm_ldcl:
2478   case ARM::BI__builtin_arm_ldc2:
2479   case ARM::BI__builtin_arm_ldc2l:
2480   case ARM::BI__builtin_arm_stc:
2481   case ARM::BI__builtin_arm_stcl:
2482   case ARM::BI__builtin_arm_stc2:
2483   case ARM::BI__builtin_arm_stc2l:
2484     return SemaBuiltinConstantArgRange(TheCall, 0, 0, 15) ||
2485            CheckARMCoprocessorImmediate(TI, TheCall->getArg(0),
2486                                         /*WantCDE*/ false);
2487   }
2488 }
2489 
2490 bool Sema::CheckAArch64BuiltinFunctionCall(const TargetInfo &TI,
2491                                            unsigned BuiltinID,
2492                                            CallExpr *TheCall) {
2493   if (BuiltinID == AArch64::BI__builtin_arm_ldrex ||
2494       BuiltinID == AArch64::BI__builtin_arm_ldaex ||
2495       BuiltinID == AArch64::BI__builtin_arm_strex ||
2496       BuiltinID == AArch64::BI__builtin_arm_stlex) {
2497     return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 128);
2498   }
2499 
2500   if (BuiltinID == AArch64::BI__builtin_arm_prefetch) {
2501     return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) ||
2502       SemaBuiltinConstantArgRange(TheCall, 2, 0, 2) ||
2503       SemaBuiltinConstantArgRange(TheCall, 3, 0, 1) ||
2504       SemaBuiltinConstantArgRange(TheCall, 4, 0, 1);
2505   }
2506 
2507   if (BuiltinID == AArch64::BI__builtin_arm_rsr64 ||
2508       BuiltinID == AArch64::BI__builtin_arm_wsr64)
2509     return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true);
2510 
2511   // Memory Tagging Extensions (MTE) Intrinsics
2512   if (BuiltinID == AArch64::BI__builtin_arm_irg ||
2513       BuiltinID == AArch64::BI__builtin_arm_addg ||
2514       BuiltinID == AArch64::BI__builtin_arm_gmi ||
2515       BuiltinID == AArch64::BI__builtin_arm_ldg ||
2516       BuiltinID == AArch64::BI__builtin_arm_stg ||
2517       BuiltinID == AArch64::BI__builtin_arm_subp) {
2518     return SemaBuiltinARMMemoryTaggingCall(BuiltinID, TheCall);
2519   }
2520 
2521   if (BuiltinID == AArch64::BI__builtin_arm_rsr ||
2522       BuiltinID == AArch64::BI__builtin_arm_rsrp ||
2523       BuiltinID == AArch64::BI__builtin_arm_wsr ||
2524       BuiltinID == AArch64::BI__builtin_arm_wsrp)
2525     return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true);
2526 
2527   // Only check the valid encoding range. Any constant in this range would be
2528   // converted to a register of the form S1_2_C3_C4_5. Let the hardware throw
2529   // an exception for incorrect registers. This matches MSVC behavior.
2530   if (BuiltinID == AArch64::BI_ReadStatusReg ||
2531       BuiltinID == AArch64::BI_WriteStatusReg)
2532     return SemaBuiltinConstantArgRange(TheCall, 0, 0, 0x7fff);
2533 
2534   if (BuiltinID == AArch64::BI__getReg)
2535     return SemaBuiltinConstantArgRange(TheCall, 0, 0, 31);
2536 
2537   if (CheckNeonBuiltinFunctionCall(TI, BuiltinID, TheCall))
2538     return true;
2539 
2540   if (CheckSVEBuiltinFunctionCall(BuiltinID, TheCall))
2541     return true;
2542 
2543   // For intrinsics which take an immediate value as part of the instruction,
2544   // range check them here.
2545   unsigned i = 0, l = 0, u = 0;
2546   switch (BuiltinID) {
2547   default: return false;
2548   case AArch64::BI__builtin_arm_dmb:
2549   case AArch64::BI__builtin_arm_dsb:
2550   case AArch64::BI__builtin_arm_isb: l = 0; u = 15; break;
2551   case AArch64::BI__builtin_arm_tcancel: l = 0; u = 65535; break;
2552   }
2553 
2554   return SemaBuiltinConstantArgRange(TheCall, i, l, u + l);
2555 }
2556 
2557 bool Sema::CheckBPFBuiltinFunctionCall(unsigned BuiltinID,
2558                                        CallExpr *TheCall) {
2559   assert((BuiltinID == BPF::BI__builtin_preserve_field_info ||
2560           BuiltinID == BPF::BI__builtin_btf_type_id) &&
2561          "unexpected ARM builtin");
2562 
2563   if (checkArgCount(*this, TheCall, 2))
2564     return true;
2565 
2566   Expr *Arg;
2567   if (BuiltinID == BPF::BI__builtin_btf_type_id) {
2568     // The second argument needs to be a constant int
2569     llvm::APSInt Value;
2570     Arg = TheCall->getArg(1);
2571     if (!Arg->isIntegerConstantExpr(Value, Context)) {
2572       Diag(Arg->getBeginLoc(), diag::err_btf_type_id_not_const)
2573           << 2 << Arg->getSourceRange();
2574       return true;
2575     }
2576 
2577     TheCall->setType(Context.UnsignedIntTy);
2578     return false;
2579   }
2580 
2581   // The first argument needs to be a record field access.
2582   // If it is an array element access, we delay decision
2583   // to BPF backend to check whether the access is a
2584   // field access or not.
2585   Arg = TheCall->getArg(0);
2586   if (Arg->getType()->getAsPlaceholderType() ||
2587       (Arg->IgnoreParens()->getObjectKind() != OK_BitField &&
2588        !dyn_cast<MemberExpr>(Arg->IgnoreParens()) &&
2589        !dyn_cast<ArraySubscriptExpr>(Arg->IgnoreParens()))) {
2590     Diag(Arg->getBeginLoc(), diag::err_preserve_field_info_not_field)
2591         << 1 << Arg->getSourceRange();
2592     return true;
2593   }
2594 
2595   // The second argument needs to be a constant int
2596   Arg = TheCall->getArg(1);
2597   llvm::APSInt Value;
2598   if (!Arg->isIntegerConstantExpr(Value, Context)) {
2599     Diag(Arg->getBeginLoc(), diag::err_preserve_field_info_not_const)
2600         << 2 << Arg->getSourceRange();
2601     return true;
2602   }
2603 
2604   TheCall->setType(Context.UnsignedIntTy);
2605   return false;
2606 }
2607 
2608 bool Sema::CheckHexagonBuiltinArgument(unsigned BuiltinID, CallExpr *TheCall) {
2609   struct ArgInfo {
2610     uint8_t OpNum;
2611     bool IsSigned;
2612     uint8_t BitWidth;
2613     uint8_t Align;
2614   };
2615   struct BuiltinInfo {
2616     unsigned BuiltinID;
2617     ArgInfo Infos[2];
2618   };
2619 
2620   static BuiltinInfo Infos[] = {
2621     { Hexagon::BI__builtin_circ_ldd,                  {{ 3, true,  4,  3 }} },
2622     { Hexagon::BI__builtin_circ_ldw,                  {{ 3, true,  4,  2 }} },
2623     { Hexagon::BI__builtin_circ_ldh,                  {{ 3, true,  4,  1 }} },
2624     { Hexagon::BI__builtin_circ_lduh,                 {{ 3, true,  4,  1 }} },
2625     { Hexagon::BI__builtin_circ_ldb,                  {{ 3, true,  4,  0 }} },
2626     { Hexagon::BI__builtin_circ_ldub,                 {{ 3, true,  4,  0 }} },
2627     { Hexagon::BI__builtin_circ_std,                  {{ 3, true,  4,  3 }} },
2628     { Hexagon::BI__builtin_circ_stw,                  {{ 3, true,  4,  2 }} },
2629     { Hexagon::BI__builtin_circ_sth,                  {{ 3, true,  4,  1 }} },
2630     { Hexagon::BI__builtin_circ_sthhi,                {{ 3, true,  4,  1 }} },
2631     { Hexagon::BI__builtin_circ_stb,                  {{ 3, true,  4,  0 }} },
2632 
2633     { Hexagon::BI__builtin_HEXAGON_L2_loadrub_pci,    {{ 1, true,  4,  0 }} },
2634     { Hexagon::BI__builtin_HEXAGON_L2_loadrb_pci,     {{ 1, true,  4,  0 }} },
2635     { Hexagon::BI__builtin_HEXAGON_L2_loadruh_pci,    {{ 1, true,  4,  1 }} },
2636     { Hexagon::BI__builtin_HEXAGON_L2_loadrh_pci,     {{ 1, true,  4,  1 }} },
2637     { Hexagon::BI__builtin_HEXAGON_L2_loadri_pci,     {{ 1, true,  4,  2 }} },
2638     { Hexagon::BI__builtin_HEXAGON_L2_loadrd_pci,     {{ 1, true,  4,  3 }} },
2639     { Hexagon::BI__builtin_HEXAGON_S2_storerb_pci,    {{ 1, true,  4,  0 }} },
2640     { Hexagon::BI__builtin_HEXAGON_S2_storerh_pci,    {{ 1, true,  4,  1 }} },
2641     { Hexagon::BI__builtin_HEXAGON_S2_storerf_pci,    {{ 1, true,  4,  1 }} },
2642     { Hexagon::BI__builtin_HEXAGON_S2_storeri_pci,    {{ 1, true,  4,  2 }} },
2643     { Hexagon::BI__builtin_HEXAGON_S2_storerd_pci,    {{ 1, true,  4,  3 }} },
2644 
2645     { Hexagon::BI__builtin_HEXAGON_A2_combineii,      {{ 1, true,  8,  0 }} },
2646     { Hexagon::BI__builtin_HEXAGON_A2_tfrih,          {{ 1, false, 16, 0 }} },
2647     { Hexagon::BI__builtin_HEXAGON_A2_tfril,          {{ 1, false, 16, 0 }} },
2648     { Hexagon::BI__builtin_HEXAGON_A2_tfrpi,          {{ 0, true,  8,  0 }} },
2649     { Hexagon::BI__builtin_HEXAGON_A4_bitspliti,      {{ 1, false, 5,  0 }} },
2650     { Hexagon::BI__builtin_HEXAGON_A4_cmpbeqi,        {{ 1, false, 8,  0 }} },
2651     { Hexagon::BI__builtin_HEXAGON_A4_cmpbgti,        {{ 1, true,  8,  0 }} },
2652     { Hexagon::BI__builtin_HEXAGON_A4_cround_ri,      {{ 1, false, 5,  0 }} },
2653     { Hexagon::BI__builtin_HEXAGON_A4_round_ri,       {{ 1, false, 5,  0 }} },
2654     { Hexagon::BI__builtin_HEXAGON_A4_round_ri_sat,   {{ 1, false, 5,  0 }} },
2655     { Hexagon::BI__builtin_HEXAGON_A4_vcmpbeqi,       {{ 1, false, 8,  0 }} },
2656     { Hexagon::BI__builtin_HEXAGON_A4_vcmpbgti,       {{ 1, true,  8,  0 }} },
2657     { Hexagon::BI__builtin_HEXAGON_A4_vcmpbgtui,      {{ 1, false, 7,  0 }} },
2658     { Hexagon::BI__builtin_HEXAGON_A4_vcmpheqi,       {{ 1, true,  8,  0 }} },
2659     { Hexagon::BI__builtin_HEXAGON_A4_vcmphgti,       {{ 1, true,  8,  0 }} },
2660     { Hexagon::BI__builtin_HEXAGON_A4_vcmphgtui,      {{ 1, false, 7,  0 }} },
2661     { Hexagon::BI__builtin_HEXAGON_A4_vcmpweqi,       {{ 1, true,  8,  0 }} },
2662     { Hexagon::BI__builtin_HEXAGON_A4_vcmpwgti,       {{ 1, true,  8,  0 }} },
2663     { Hexagon::BI__builtin_HEXAGON_A4_vcmpwgtui,      {{ 1, false, 7,  0 }} },
2664     { Hexagon::BI__builtin_HEXAGON_C2_bitsclri,       {{ 1, false, 6,  0 }} },
2665     { Hexagon::BI__builtin_HEXAGON_C2_muxii,          {{ 2, true,  8,  0 }} },
2666     { Hexagon::BI__builtin_HEXAGON_C4_nbitsclri,      {{ 1, false, 6,  0 }} },
2667     { Hexagon::BI__builtin_HEXAGON_F2_dfclass,        {{ 1, false, 5,  0 }} },
2668     { Hexagon::BI__builtin_HEXAGON_F2_dfimm_n,        {{ 0, false, 10, 0 }} },
2669     { Hexagon::BI__builtin_HEXAGON_F2_dfimm_p,        {{ 0, false, 10, 0 }} },
2670     { Hexagon::BI__builtin_HEXAGON_F2_sfclass,        {{ 1, false, 5,  0 }} },
2671     { Hexagon::BI__builtin_HEXAGON_F2_sfimm_n,        {{ 0, false, 10, 0 }} },
2672     { Hexagon::BI__builtin_HEXAGON_F2_sfimm_p,        {{ 0, false, 10, 0 }} },
2673     { Hexagon::BI__builtin_HEXAGON_M4_mpyri_addi,     {{ 2, false, 6,  0 }} },
2674     { Hexagon::BI__builtin_HEXAGON_M4_mpyri_addr_u2,  {{ 1, false, 6,  2 }} },
2675     { Hexagon::BI__builtin_HEXAGON_S2_addasl_rrri,    {{ 2, false, 3,  0 }} },
2676     { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_acc,    {{ 2, false, 6,  0 }} },
2677     { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_and,    {{ 2, false, 6,  0 }} },
2678     { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p,        {{ 1, false, 6,  0 }} },
2679     { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_nac,    {{ 2, false, 6,  0 }} },
2680     { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_or,     {{ 2, false, 6,  0 }} },
2681     { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_xacc,   {{ 2, false, 6,  0 }} },
2682     { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_acc,    {{ 2, false, 5,  0 }} },
2683     { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_and,    {{ 2, false, 5,  0 }} },
2684     { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r,        {{ 1, false, 5,  0 }} },
2685     { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_nac,    {{ 2, false, 5,  0 }} },
2686     { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_or,     {{ 2, false, 5,  0 }} },
2687     { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_sat,    {{ 1, false, 5,  0 }} },
2688     { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_xacc,   {{ 2, false, 5,  0 }} },
2689     { Hexagon::BI__builtin_HEXAGON_S2_asl_i_vh,       {{ 1, false, 4,  0 }} },
2690     { Hexagon::BI__builtin_HEXAGON_S2_asl_i_vw,       {{ 1, false, 5,  0 }} },
2691     { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_acc,    {{ 2, false, 6,  0 }} },
2692     { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_and,    {{ 2, false, 6,  0 }} },
2693     { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p,        {{ 1, false, 6,  0 }} },
2694     { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_nac,    {{ 2, false, 6,  0 }} },
2695     { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_or,     {{ 2, false, 6,  0 }} },
2696     { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_rnd_goodsyntax,
2697                                                       {{ 1, false, 6,  0 }} },
2698     { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_rnd,    {{ 1, false, 6,  0 }} },
2699     { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_acc,    {{ 2, false, 5,  0 }} },
2700     { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_and,    {{ 2, false, 5,  0 }} },
2701     { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r,        {{ 1, false, 5,  0 }} },
2702     { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_nac,    {{ 2, false, 5,  0 }} },
2703     { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_or,     {{ 2, false, 5,  0 }} },
2704     { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_rnd_goodsyntax,
2705                                                       {{ 1, false, 5,  0 }} },
2706     { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_rnd,    {{ 1, false, 5,  0 }} },
2707     { Hexagon::BI__builtin_HEXAGON_S2_asr_i_svw_trun, {{ 1, false, 5,  0 }} },
2708     { Hexagon::BI__builtin_HEXAGON_S2_asr_i_vh,       {{ 1, false, 4,  0 }} },
2709     { Hexagon::BI__builtin_HEXAGON_S2_asr_i_vw,       {{ 1, false, 5,  0 }} },
2710     { Hexagon::BI__builtin_HEXAGON_S2_clrbit_i,       {{ 1, false, 5,  0 }} },
2711     { Hexagon::BI__builtin_HEXAGON_S2_extractu,       {{ 1, false, 5,  0 },
2712                                                        { 2, false, 5,  0 }} },
2713     { Hexagon::BI__builtin_HEXAGON_S2_extractup,      {{ 1, false, 6,  0 },
2714                                                        { 2, false, 6,  0 }} },
2715     { Hexagon::BI__builtin_HEXAGON_S2_insert,         {{ 2, false, 5,  0 },
2716                                                        { 3, false, 5,  0 }} },
2717     { Hexagon::BI__builtin_HEXAGON_S2_insertp,        {{ 2, false, 6,  0 },
2718                                                        { 3, false, 6,  0 }} },
2719     { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_acc,    {{ 2, false, 6,  0 }} },
2720     { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_and,    {{ 2, false, 6,  0 }} },
2721     { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p,        {{ 1, false, 6,  0 }} },
2722     { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_nac,    {{ 2, false, 6,  0 }} },
2723     { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_or,     {{ 2, false, 6,  0 }} },
2724     { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_xacc,   {{ 2, false, 6,  0 }} },
2725     { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_acc,    {{ 2, false, 5,  0 }} },
2726     { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_and,    {{ 2, false, 5,  0 }} },
2727     { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r,        {{ 1, false, 5,  0 }} },
2728     { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_nac,    {{ 2, false, 5,  0 }} },
2729     { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_or,     {{ 2, false, 5,  0 }} },
2730     { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_xacc,   {{ 2, false, 5,  0 }} },
2731     { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_vh,       {{ 1, false, 4,  0 }} },
2732     { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_vw,       {{ 1, false, 5,  0 }} },
2733     { Hexagon::BI__builtin_HEXAGON_S2_setbit_i,       {{ 1, false, 5,  0 }} },
2734     { Hexagon::BI__builtin_HEXAGON_S2_tableidxb_goodsyntax,
2735                                                       {{ 2, false, 4,  0 },
2736                                                        { 3, false, 5,  0 }} },
2737     { Hexagon::BI__builtin_HEXAGON_S2_tableidxd_goodsyntax,
2738                                                       {{ 2, false, 4,  0 },
2739                                                        { 3, false, 5,  0 }} },
2740     { Hexagon::BI__builtin_HEXAGON_S2_tableidxh_goodsyntax,
2741                                                       {{ 2, false, 4,  0 },
2742                                                        { 3, false, 5,  0 }} },
2743     { Hexagon::BI__builtin_HEXAGON_S2_tableidxw_goodsyntax,
2744                                                       {{ 2, false, 4,  0 },
2745                                                        { 3, false, 5,  0 }} },
2746     { Hexagon::BI__builtin_HEXAGON_S2_togglebit_i,    {{ 1, false, 5,  0 }} },
2747     { Hexagon::BI__builtin_HEXAGON_S2_tstbit_i,       {{ 1, false, 5,  0 }} },
2748     { Hexagon::BI__builtin_HEXAGON_S2_valignib,       {{ 2, false, 3,  0 }} },
2749     { Hexagon::BI__builtin_HEXAGON_S2_vspliceib,      {{ 2, false, 3,  0 }} },
2750     { Hexagon::BI__builtin_HEXAGON_S4_addi_asl_ri,    {{ 2, false, 5,  0 }} },
2751     { Hexagon::BI__builtin_HEXAGON_S4_addi_lsr_ri,    {{ 2, false, 5,  0 }} },
2752     { Hexagon::BI__builtin_HEXAGON_S4_andi_asl_ri,    {{ 2, false, 5,  0 }} },
2753     { Hexagon::BI__builtin_HEXAGON_S4_andi_lsr_ri,    {{ 2, false, 5,  0 }} },
2754     { Hexagon::BI__builtin_HEXAGON_S4_clbaddi,        {{ 1, true , 6,  0 }} },
2755     { Hexagon::BI__builtin_HEXAGON_S4_clbpaddi,       {{ 1, true,  6,  0 }} },
2756     { Hexagon::BI__builtin_HEXAGON_S4_extract,        {{ 1, false, 5,  0 },
2757                                                        { 2, false, 5,  0 }} },
2758     { Hexagon::BI__builtin_HEXAGON_S4_extractp,       {{ 1, false, 6,  0 },
2759                                                        { 2, false, 6,  0 }} },
2760     { Hexagon::BI__builtin_HEXAGON_S4_lsli,           {{ 0, true,  6,  0 }} },
2761     { Hexagon::BI__builtin_HEXAGON_S4_ntstbit_i,      {{ 1, false, 5,  0 }} },
2762     { Hexagon::BI__builtin_HEXAGON_S4_ori_asl_ri,     {{ 2, false, 5,  0 }} },
2763     { Hexagon::BI__builtin_HEXAGON_S4_ori_lsr_ri,     {{ 2, false, 5,  0 }} },
2764     { Hexagon::BI__builtin_HEXAGON_S4_subi_asl_ri,    {{ 2, false, 5,  0 }} },
2765     { Hexagon::BI__builtin_HEXAGON_S4_subi_lsr_ri,    {{ 2, false, 5,  0 }} },
2766     { Hexagon::BI__builtin_HEXAGON_S4_vrcrotate_acc,  {{ 3, false, 2,  0 }} },
2767     { Hexagon::BI__builtin_HEXAGON_S4_vrcrotate,      {{ 2, false, 2,  0 }} },
2768     { Hexagon::BI__builtin_HEXAGON_S5_asrhub_rnd_sat_goodsyntax,
2769                                                       {{ 1, false, 4,  0 }} },
2770     { Hexagon::BI__builtin_HEXAGON_S5_asrhub_sat,     {{ 1, false, 4,  0 }} },
2771     { Hexagon::BI__builtin_HEXAGON_S5_vasrhrnd_goodsyntax,
2772                                                       {{ 1, false, 4,  0 }} },
2773     { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p,        {{ 1, false, 6,  0 }} },
2774     { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_acc,    {{ 2, false, 6,  0 }} },
2775     { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_and,    {{ 2, false, 6,  0 }} },
2776     { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_nac,    {{ 2, false, 6,  0 }} },
2777     { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_or,     {{ 2, false, 6,  0 }} },
2778     { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_xacc,   {{ 2, false, 6,  0 }} },
2779     { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r,        {{ 1, false, 5,  0 }} },
2780     { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_acc,    {{ 2, false, 5,  0 }} },
2781     { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_and,    {{ 2, false, 5,  0 }} },
2782     { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_nac,    {{ 2, false, 5,  0 }} },
2783     { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_or,     {{ 2, false, 5,  0 }} },
2784     { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_xacc,   {{ 2, false, 5,  0 }} },
2785     { Hexagon::BI__builtin_HEXAGON_V6_valignbi,       {{ 2, false, 3,  0 }} },
2786     { Hexagon::BI__builtin_HEXAGON_V6_valignbi_128B,  {{ 2, false, 3,  0 }} },
2787     { Hexagon::BI__builtin_HEXAGON_V6_vlalignbi,      {{ 2, false, 3,  0 }} },
2788     { Hexagon::BI__builtin_HEXAGON_V6_vlalignbi_128B, {{ 2, false, 3,  0 }} },
2789     { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi,      {{ 2, false, 1,  0 }} },
2790     { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi_128B, {{ 2, false, 1,  0 }} },
2791     { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi_acc,  {{ 3, false, 1,  0 }} },
2792     { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi_acc_128B,
2793                                                       {{ 3, false, 1,  0 }} },
2794     { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi,       {{ 2, false, 1,  0 }} },
2795     { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi_128B,  {{ 2, false, 1,  0 }} },
2796     { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi_acc,   {{ 3, false, 1,  0 }} },
2797     { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi_acc_128B,
2798                                                       {{ 3, false, 1,  0 }} },
2799     { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi,       {{ 2, false, 1,  0 }} },
2800     { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi_128B,  {{ 2, false, 1,  0 }} },
2801     { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi_acc,   {{ 3, false, 1,  0 }} },
2802     { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi_acc_128B,
2803                                                       {{ 3, false, 1,  0 }} },
2804   };
2805 
2806   // Use a dynamically initialized static to sort the table exactly once on
2807   // first run.
2808   static const bool SortOnce =
2809       (llvm::sort(Infos,
2810                  [](const BuiltinInfo &LHS, const BuiltinInfo &RHS) {
2811                    return LHS.BuiltinID < RHS.BuiltinID;
2812                  }),
2813        true);
2814   (void)SortOnce;
2815 
2816   const BuiltinInfo *F = llvm::partition_point(
2817       Infos, [=](const BuiltinInfo &BI) { return BI.BuiltinID < BuiltinID; });
2818   if (F == std::end(Infos) || F->BuiltinID != BuiltinID)
2819     return false;
2820 
2821   bool Error = false;
2822 
2823   for (const ArgInfo &A : F->Infos) {
2824     // Ignore empty ArgInfo elements.
2825     if (A.BitWidth == 0)
2826       continue;
2827 
2828     int32_t Min = A.IsSigned ? -(1 << (A.BitWidth - 1)) : 0;
2829     int32_t Max = (1 << (A.IsSigned ? A.BitWidth - 1 : A.BitWidth)) - 1;
2830     if (!A.Align) {
2831       Error |= SemaBuiltinConstantArgRange(TheCall, A.OpNum, Min, Max);
2832     } else {
2833       unsigned M = 1 << A.Align;
2834       Min *= M;
2835       Max *= M;
2836       Error |= SemaBuiltinConstantArgRange(TheCall, A.OpNum, Min, Max) |
2837                SemaBuiltinConstantArgMultiple(TheCall, A.OpNum, M);
2838     }
2839   }
2840   return Error;
2841 }
2842 
2843 bool Sema::CheckHexagonBuiltinFunctionCall(unsigned BuiltinID,
2844                                            CallExpr *TheCall) {
2845   return CheckHexagonBuiltinArgument(BuiltinID, TheCall);
2846 }
2847 
2848 bool Sema::CheckMipsBuiltinFunctionCall(const TargetInfo &TI,
2849                                         unsigned BuiltinID, CallExpr *TheCall) {
2850   return CheckMipsBuiltinCpu(TI, BuiltinID, TheCall) ||
2851          CheckMipsBuiltinArgument(BuiltinID, TheCall);
2852 }
2853 
2854 bool Sema::CheckMipsBuiltinCpu(const TargetInfo &TI, unsigned BuiltinID,
2855                                CallExpr *TheCall) {
2856 
2857   if (Mips::BI__builtin_mips_addu_qb <= BuiltinID &&
2858       BuiltinID <= Mips::BI__builtin_mips_lwx) {
2859     if (!TI.hasFeature("dsp"))
2860       return Diag(TheCall->getBeginLoc(), diag::err_mips_builtin_requires_dsp);
2861   }
2862 
2863   if (Mips::BI__builtin_mips_absq_s_qb <= BuiltinID &&
2864       BuiltinID <= Mips::BI__builtin_mips_subuh_r_qb) {
2865     if (!TI.hasFeature("dspr2"))
2866       return Diag(TheCall->getBeginLoc(),
2867                   diag::err_mips_builtin_requires_dspr2);
2868   }
2869 
2870   if (Mips::BI__builtin_msa_add_a_b <= BuiltinID &&
2871       BuiltinID <= Mips::BI__builtin_msa_xori_b) {
2872     if (!TI.hasFeature("msa"))
2873       return Diag(TheCall->getBeginLoc(), diag::err_mips_builtin_requires_msa);
2874   }
2875 
2876   return false;
2877 }
2878 
2879 // CheckMipsBuiltinArgument - Checks the constant value passed to the
2880 // intrinsic is correct. The switch statement is ordered by DSP, MSA. The
2881 // ordering for DSP is unspecified. MSA is ordered by the data format used
2882 // by the underlying instruction i.e., df/m, df/n and then by size.
2883 //
2884 // FIXME: The size tests here should instead be tablegen'd along with the
2885 //        definitions from include/clang/Basic/BuiltinsMips.def.
2886 // FIXME: GCC is strict on signedness for some of these intrinsics, we should
2887 //        be too.
2888 bool Sema::CheckMipsBuiltinArgument(unsigned BuiltinID, CallExpr *TheCall) {
2889   unsigned i = 0, l = 0, u = 0, m = 0;
2890   switch (BuiltinID) {
2891   default: return false;
2892   case Mips::BI__builtin_mips_wrdsp: i = 1; l = 0; u = 63; break;
2893   case Mips::BI__builtin_mips_rddsp: i = 0; l = 0; u = 63; break;
2894   case Mips::BI__builtin_mips_append: i = 2; l = 0; u = 31; break;
2895   case Mips::BI__builtin_mips_balign: i = 2; l = 0; u = 3; break;
2896   case Mips::BI__builtin_mips_precr_sra_ph_w: i = 2; l = 0; u = 31; break;
2897   case Mips::BI__builtin_mips_precr_sra_r_ph_w: i = 2; l = 0; u = 31; break;
2898   case Mips::BI__builtin_mips_prepend: i = 2; l = 0; u = 31; break;
2899   // MSA intrinsics. Instructions (which the intrinsics maps to) which use the
2900   // df/m field.
2901   // These intrinsics take an unsigned 3 bit immediate.
2902   case Mips::BI__builtin_msa_bclri_b:
2903   case Mips::BI__builtin_msa_bnegi_b:
2904   case Mips::BI__builtin_msa_bseti_b:
2905   case Mips::BI__builtin_msa_sat_s_b:
2906   case Mips::BI__builtin_msa_sat_u_b:
2907   case Mips::BI__builtin_msa_slli_b:
2908   case Mips::BI__builtin_msa_srai_b:
2909   case Mips::BI__builtin_msa_srari_b:
2910   case Mips::BI__builtin_msa_srli_b:
2911   case Mips::BI__builtin_msa_srlri_b: i = 1; l = 0; u = 7; break;
2912   case Mips::BI__builtin_msa_binsli_b:
2913   case Mips::BI__builtin_msa_binsri_b: i = 2; l = 0; u = 7; break;
2914   // These intrinsics take an unsigned 4 bit immediate.
2915   case Mips::BI__builtin_msa_bclri_h:
2916   case Mips::BI__builtin_msa_bnegi_h:
2917   case Mips::BI__builtin_msa_bseti_h:
2918   case Mips::BI__builtin_msa_sat_s_h:
2919   case Mips::BI__builtin_msa_sat_u_h:
2920   case Mips::BI__builtin_msa_slli_h:
2921   case Mips::BI__builtin_msa_srai_h:
2922   case Mips::BI__builtin_msa_srari_h:
2923   case Mips::BI__builtin_msa_srli_h:
2924   case Mips::BI__builtin_msa_srlri_h: i = 1; l = 0; u = 15; break;
2925   case Mips::BI__builtin_msa_binsli_h:
2926   case Mips::BI__builtin_msa_binsri_h: i = 2; l = 0; u = 15; break;
2927   // These intrinsics take an unsigned 5 bit immediate.
2928   // The first block of intrinsics actually have an unsigned 5 bit field,
2929   // not a df/n field.
2930   case Mips::BI__builtin_msa_cfcmsa:
2931   case Mips::BI__builtin_msa_ctcmsa: i = 0; l = 0; u = 31; break;
2932   case Mips::BI__builtin_msa_clei_u_b:
2933   case Mips::BI__builtin_msa_clei_u_h:
2934   case Mips::BI__builtin_msa_clei_u_w:
2935   case Mips::BI__builtin_msa_clei_u_d:
2936   case Mips::BI__builtin_msa_clti_u_b:
2937   case Mips::BI__builtin_msa_clti_u_h:
2938   case Mips::BI__builtin_msa_clti_u_w:
2939   case Mips::BI__builtin_msa_clti_u_d:
2940   case Mips::BI__builtin_msa_maxi_u_b:
2941   case Mips::BI__builtin_msa_maxi_u_h:
2942   case Mips::BI__builtin_msa_maxi_u_w:
2943   case Mips::BI__builtin_msa_maxi_u_d:
2944   case Mips::BI__builtin_msa_mini_u_b:
2945   case Mips::BI__builtin_msa_mini_u_h:
2946   case Mips::BI__builtin_msa_mini_u_w:
2947   case Mips::BI__builtin_msa_mini_u_d:
2948   case Mips::BI__builtin_msa_addvi_b:
2949   case Mips::BI__builtin_msa_addvi_h:
2950   case Mips::BI__builtin_msa_addvi_w:
2951   case Mips::BI__builtin_msa_addvi_d:
2952   case Mips::BI__builtin_msa_bclri_w:
2953   case Mips::BI__builtin_msa_bnegi_w:
2954   case Mips::BI__builtin_msa_bseti_w:
2955   case Mips::BI__builtin_msa_sat_s_w:
2956   case Mips::BI__builtin_msa_sat_u_w:
2957   case Mips::BI__builtin_msa_slli_w:
2958   case Mips::BI__builtin_msa_srai_w:
2959   case Mips::BI__builtin_msa_srari_w:
2960   case Mips::BI__builtin_msa_srli_w:
2961   case Mips::BI__builtin_msa_srlri_w:
2962   case Mips::BI__builtin_msa_subvi_b:
2963   case Mips::BI__builtin_msa_subvi_h:
2964   case Mips::BI__builtin_msa_subvi_w:
2965   case Mips::BI__builtin_msa_subvi_d: i = 1; l = 0; u = 31; break;
2966   case Mips::BI__builtin_msa_binsli_w:
2967   case Mips::BI__builtin_msa_binsri_w: i = 2; l = 0; u = 31; break;
2968   // These intrinsics take an unsigned 6 bit immediate.
2969   case Mips::BI__builtin_msa_bclri_d:
2970   case Mips::BI__builtin_msa_bnegi_d:
2971   case Mips::BI__builtin_msa_bseti_d:
2972   case Mips::BI__builtin_msa_sat_s_d:
2973   case Mips::BI__builtin_msa_sat_u_d:
2974   case Mips::BI__builtin_msa_slli_d:
2975   case Mips::BI__builtin_msa_srai_d:
2976   case Mips::BI__builtin_msa_srari_d:
2977   case Mips::BI__builtin_msa_srli_d:
2978   case Mips::BI__builtin_msa_srlri_d: i = 1; l = 0; u = 63; break;
2979   case Mips::BI__builtin_msa_binsli_d:
2980   case Mips::BI__builtin_msa_binsri_d: i = 2; l = 0; u = 63; break;
2981   // These intrinsics take a signed 5 bit immediate.
2982   case Mips::BI__builtin_msa_ceqi_b:
2983   case Mips::BI__builtin_msa_ceqi_h:
2984   case Mips::BI__builtin_msa_ceqi_w:
2985   case Mips::BI__builtin_msa_ceqi_d:
2986   case Mips::BI__builtin_msa_clti_s_b:
2987   case Mips::BI__builtin_msa_clti_s_h:
2988   case Mips::BI__builtin_msa_clti_s_w:
2989   case Mips::BI__builtin_msa_clti_s_d:
2990   case Mips::BI__builtin_msa_clei_s_b:
2991   case Mips::BI__builtin_msa_clei_s_h:
2992   case Mips::BI__builtin_msa_clei_s_w:
2993   case Mips::BI__builtin_msa_clei_s_d:
2994   case Mips::BI__builtin_msa_maxi_s_b:
2995   case Mips::BI__builtin_msa_maxi_s_h:
2996   case Mips::BI__builtin_msa_maxi_s_w:
2997   case Mips::BI__builtin_msa_maxi_s_d:
2998   case Mips::BI__builtin_msa_mini_s_b:
2999   case Mips::BI__builtin_msa_mini_s_h:
3000   case Mips::BI__builtin_msa_mini_s_w:
3001   case Mips::BI__builtin_msa_mini_s_d: i = 1; l = -16; u = 15; break;
3002   // These intrinsics take an unsigned 8 bit immediate.
3003   case Mips::BI__builtin_msa_andi_b:
3004   case Mips::BI__builtin_msa_nori_b:
3005   case Mips::BI__builtin_msa_ori_b:
3006   case Mips::BI__builtin_msa_shf_b:
3007   case Mips::BI__builtin_msa_shf_h:
3008   case Mips::BI__builtin_msa_shf_w:
3009   case Mips::BI__builtin_msa_xori_b: i = 1; l = 0; u = 255; break;
3010   case Mips::BI__builtin_msa_bseli_b:
3011   case Mips::BI__builtin_msa_bmnzi_b:
3012   case Mips::BI__builtin_msa_bmzi_b: i = 2; l = 0; u = 255; break;
3013   // df/n format
3014   // These intrinsics take an unsigned 4 bit immediate.
3015   case Mips::BI__builtin_msa_copy_s_b:
3016   case Mips::BI__builtin_msa_copy_u_b:
3017   case Mips::BI__builtin_msa_insve_b:
3018   case Mips::BI__builtin_msa_splati_b: i = 1; l = 0; u = 15; break;
3019   case Mips::BI__builtin_msa_sldi_b: i = 2; l = 0; u = 15; break;
3020   // These intrinsics take an unsigned 3 bit immediate.
3021   case Mips::BI__builtin_msa_copy_s_h:
3022   case Mips::BI__builtin_msa_copy_u_h:
3023   case Mips::BI__builtin_msa_insve_h:
3024   case Mips::BI__builtin_msa_splati_h: i = 1; l = 0; u = 7; break;
3025   case Mips::BI__builtin_msa_sldi_h: i = 2; l = 0; u = 7; break;
3026   // These intrinsics take an unsigned 2 bit immediate.
3027   case Mips::BI__builtin_msa_copy_s_w:
3028   case Mips::BI__builtin_msa_copy_u_w:
3029   case Mips::BI__builtin_msa_insve_w:
3030   case Mips::BI__builtin_msa_splati_w: i = 1; l = 0; u = 3; break;
3031   case Mips::BI__builtin_msa_sldi_w: i = 2; l = 0; u = 3; break;
3032   // These intrinsics take an unsigned 1 bit immediate.
3033   case Mips::BI__builtin_msa_copy_s_d:
3034   case Mips::BI__builtin_msa_copy_u_d:
3035   case Mips::BI__builtin_msa_insve_d:
3036   case Mips::BI__builtin_msa_splati_d: i = 1; l = 0; u = 1; break;
3037   case Mips::BI__builtin_msa_sldi_d: i = 2; l = 0; u = 1; break;
3038   // Memory offsets and immediate loads.
3039   // These intrinsics take a signed 10 bit immediate.
3040   case Mips::BI__builtin_msa_ldi_b: i = 0; l = -128; u = 255; break;
3041   case Mips::BI__builtin_msa_ldi_h:
3042   case Mips::BI__builtin_msa_ldi_w:
3043   case Mips::BI__builtin_msa_ldi_d: i = 0; l = -512; u = 511; break;
3044   case Mips::BI__builtin_msa_ld_b: i = 1; l = -512; u = 511; m = 1; break;
3045   case Mips::BI__builtin_msa_ld_h: i = 1; l = -1024; u = 1022; m = 2; break;
3046   case Mips::BI__builtin_msa_ld_w: i = 1; l = -2048; u = 2044; m = 4; break;
3047   case Mips::BI__builtin_msa_ld_d: i = 1; l = -4096; u = 4088; m = 8; break;
3048   case Mips::BI__builtin_msa_ldr_d: i = 1; l = -4096; u = 4088; m = 8; break;
3049   case Mips::BI__builtin_msa_ldr_w: i = 1; l = -2048; u = 2044; m = 4; break;
3050   case Mips::BI__builtin_msa_st_b: i = 2; l = -512; u = 511; m = 1; break;
3051   case Mips::BI__builtin_msa_st_h: i = 2; l = -1024; u = 1022; m = 2; break;
3052   case Mips::BI__builtin_msa_st_w: i = 2; l = -2048; u = 2044; m = 4; break;
3053   case Mips::BI__builtin_msa_st_d: i = 2; l = -4096; u = 4088; m = 8; break;
3054   case Mips::BI__builtin_msa_str_d: i = 2; l = -4096; u = 4088; m = 8; break;
3055   case Mips::BI__builtin_msa_str_w: i = 2; l = -2048; u = 2044; m = 4; break;
3056   }
3057 
3058   if (!m)
3059     return SemaBuiltinConstantArgRange(TheCall, i, l, u);
3060 
3061   return SemaBuiltinConstantArgRange(TheCall, i, l, u) ||
3062          SemaBuiltinConstantArgMultiple(TheCall, i, m);
3063 }
3064 
3065 bool Sema::CheckPPCBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
3066                                        CallExpr *TheCall) {
3067   unsigned i = 0, l = 0, u = 0;
3068   bool Is64BitBltin = BuiltinID == PPC::BI__builtin_divde ||
3069                       BuiltinID == PPC::BI__builtin_divdeu ||
3070                       BuiltinID == PPC::BI__builtin_bpermd;
3071   bool IsTarget64Bit = TI.getTypeWidth(TI.getIntPtrType()) == 64;
3072   bool IsBltinExtDiv = BuiltinID == PPC::BI__builtin_divwe ||
3073                        BuiltinID == PPC::BI__builtin_divweu ||
3074                        BuiltinID == PPC::BI__builtin_divde ||
3075                        BuiltinID == PPC::BI__builtin_divdeu;
3076 
3077   if (Is64BitBltin && !IsTarget64Bit)
3078     return Diag(TheCall->getBeginLoc(), diag::err_64_bit_builtin_32_bit_tgt)
3079            << TheCall->getSourceRange();
3080 
3081   if ((IsBltinExtDiv && !TI.hasFeature("extdiv")) ||
3082       (BuiltinID == PPC::BI__builtin_bpermd && !TI.hasFeature("bpermd")))
3083     return Diag(TheCall->getBeginLoc(), diag::err_ppc_builtin_only_on_pwr7)
3084            << TheCall->getSourceRange();
3085 
3086   auto SemaVSXCheck = [&](CallExpr *TheCall) -> bool {
3087     if (!TI.hasFeature("vsx"))
3088       return Diag(TheCall->getBeginLoc(), diag::err_ppc_builtin_only_on_pwr7)
3089              << TheCall->getSourceRange();
3090     return false;
3091   };
3092 
3093   switch (BuiltinID) {
3094   default: return false;
3095   case PPC::BI__builtin_altivec_crypto_vshasigmaw:
3096   case PPC::BI__builtin_altivec_crypto_vshasigmad:
3097     return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) ||
3098            SemaBuiltinConstantArgRange(TheCall, 2, 0, 15);
3099   case PPC::BI__builtin_altivec_dss:
3100     return SemaBuiltinConstantArgRange(TheCall, 0, 0, 3);
3101   case PPC::BI__builtin_tbegin:
3102   case PPC::BI__builtin_tend: i = 0; l = 0; u = 1; break;
3103   case PPC::BI__builtin_tsr: i = 0; l = 0; u = 7; break;
3104   case PPC::BI__builtin_tabortwc:
3105   case PPC::BI__builtin_tabortdc: i = 0; l = 0; u = 31; break;
3106   case PPC::BI__builtin_tabortwci:
3107   case PPC::BI__builtin_tabortdci:
3108     return SemaBuiltinConstantArgRange(TheCall, 0, 0, 31) ||
3109            SemaBuiltinConstantArgRange(TheCall, 2, 0, 31);
3110   case PPC::BI__builtin_altivec_dst:
3111   case PPC::BI__builtin_altivec_dstt:
3112   case PPC::BI__builtin_altivec_dstst:
3113   case PPC::BI__builtin_altivec_dststt:
3114     return SemaBuiltinConstantArgRange(TheCall, 2, 0, 3);
3115   case PPC::BI__builtin_vsx_xxpermdi:
3116   case PPC::BI__builtin_vsx_xxsldwi:
3117     return SemaBuiltinVSX(TheCall);
3118   case PPC::BI__builtin_unpack_vector_int128:
3119     return SemaVSXCheck(TheCall) ||
3120            SemaBuiltinConstantArgRange(TheCall, 1, 0, 1);
3121   case PPC::BI__builtin_pack_vector_int128:
3122     return SemaVSXCheck(TheCall);
3123   case PPC::BI__builtin_altivec_vgnb:
3124      return SemaBuiltinConstantArgRange(TheCall, 1, 2, 7);
3125   case PPC::BI__builtin_vsx_xxeval:
3126      return SemaBuiltinConstantArgRange(TheCall, 3, 0, 255);
3127   case PPC::BI__builtin_altivec_vsldbi:
3128      return SemaBuiltinConstantArgRange(TheCall, 2, 0, 7);
3129   case PPC::BI__builtin_altivec_vsrdbi:
3130      return SemaBuiltinConstantArgRange(TheCall, 2, 0, 7);
3131   case PPC::BI__builtin_vsx_xxpermx:
3132      return SemaBuiltinConstantArgRange(TheCall, 3, 0, 7);
3133   }
3134   return SemaBuiltinConstantArgRange(TheCall, i, l, u);
3135 }
3136 
3137 bool Sema::CheckAMDGCNBuiltinFunctionCall(unsigned BuiltinID,
3138                                           CallExpr *TheCall) {
3139   // position of memory order and scope arguments in the builtin
3140   unsigned OrderIndex, ScopeIndex;
3141   switch (BuiltinID) {
3142   case AMDGPU::BI__builtin_amdgcn_atomic_inc32:
3143   case AMDGPU::BI__builtin_amdgcn_atomic_inc64:
3144   case AMDGPU::BI__builtin_amdgcn_atomic_dec32:
3145   case AMDGPU::BI__builtin_amdgcn_atomic_dec64:
3146     OrderIndex = 2;
3147     ScopeIndex = 3;
3148     break;
3149   case AMDGPU::BI__builtin_amdgcn_fence:
3150     OrderIndex = 0;
3151     ScopeIndex = 1;
3152     break;
3153   default:
3154     return false;
3155   }
3156 
3157   ExprResult Arg = TheCall->getArg(OrderIndex);
3158   auto ArgExpr = Arg.get();
3159   Expr::EvalResult ArgResult;
3160 
3161   if (!ArgExpr->EvaluateAsInt(ArgResult, Context))
3162     return Diag(ArgExpr->getExprLoc(), diag::err_typecheck_expect_int)
3163            << ArgExpr->getType();
3164   int ord = ArgResult.Val.getInt().getZExtValue();
3165 
3166   // Check valididty of memory ordering as per C11 / C++11's memody model.
3167   switch (static_cast<llvm::AtomicOrderingCABI>(ord)) {
3168   case llvm::AtomicOrderingCABI::acquire:
3169   case llvm::AtomicOrderingCABI::release:
3170   case llvm::AtomicOrderingCABI::acq_rel:
3171   case llvm::AtomicOrderingCABI::seq_cst:
3172     break;
3173   default: {
3174     return Diag(ArgExpr->getBeginLoc(),
3175                 diag::warn_atomic_op_has_invalid_memory_order)
3176            << ArgExpr->getSourceRange();
3177   }
3178   }
3179 
3180   Arg = TheCall->getArg(ScopeIndex);
3181   ArgExpr = Arg.get();
3182   Expr::EvalResult ArgResult1;
3183   // Check that sync scope is a constant literal
3184   if (!ArgExpr->EvaluateAsConstantExpr(ArgResult1, Expr::EvaluateForCodeGen,
3185                                        Context))
3186     return Diag(ArgExpr->getExprLoc(), diag::err_expr_not_string_literal)
3187            << ArgExpr->getType();
3188 
3189   return false;
3190 }
3191 
3192 bool Sema::CheckSystemZBuiltinFunctionCall(unsigned BuiltinID,
3193                                            CallExpr *TheCall) {
3194   if (BuiltinID == SystemZ::BI__builtin_tabort) {
3195     Expr *Arg = TheCall->getArg(0);
3196     llvm::APSInt AbortCode(32);
3197     if (Arg->isIntegerConstantExpr(AbortCode, Context) &&
3198         AbortCode.getSExtValue() >= 0 && AbortCode.getSExtValue() < 256)
3199       return Diag(Arg->getBeginLoc(), diag::err_systemz_invalid_tabort_code)
3200              << Arg->getSourceRange();
3201   }
3202 
3203   // For intrinsics which take an immediate value as part of the instruction,
3204   // range check them here.
3205   unsigned i = 0, l = 0, u = 0;
3206   switch (BuiltinID) {
3207   default: return false;
3208   case SystemZ::BI__builtin_s390_lcbb: i = 1; l = 0; u = 15; break;
3209   case SystemZ::BI__builtin_s390_verimb:
3210   case SystemZ::BI__builtin_s390_verimh:
3211   case SystemZ::BI__builtin_s390_verimf:
3212   case SystemZ::BI__builtin_s390_verimg: i = 3; l = 0; u = 255; break;
3213   case SystemZ::BI__builtin_s390_vfaeb:
3214   case SystemZ::BI__builtin_s390_vfaeh:
3215   case SystemZ::BI__builtin_s390_vfaef:
3216   case SystemZ::BI__builtin_s390_vfaebs:
3217   case SystemZ::BI__builtin_s390_vfaehs:
3218   case SystemZ::BI__builtin_s390_vfaefs:
3219   case SystemZ::BI__builtin_s390_vfaezb:
3220   case SystemZ::BI__builtin_s390_vfaezh:
3221   case SystemZ::BI__builtin_s390_vfaezf:
3222   case SystemZ::BI__builtin_s390_vfaezbs:
3223   case SystemZ::BI__builtin_s390_vfaezhs:
3224   case SystemZ::BI__builtin_s390_vfaezfs: i = 2; l = 0; u = 15; break;
3225   case SystemZ::BI__builtin_s390_vfisb:
3226   case SystemZ::BI__builtin_s390_vfidb:
3227     return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15) ||
3228            SemaBuiltinConstantArgRange(TheCall, 2, 0, 15);
3229   case SystemZ::BI__builtin_s390_vftcisb:
3230   case SystemZ::BI__builtin_s390_vftcidb: i = 1; l = 0; u = 4095; break;
3231   case SystemZ::BI__builtin_s390_vlbb: i = 1; l = 0; u = 15; break;
3232   case SystemZ::BI__builtin_s390_vpdi: i = 2; l = 0; u = 15; break;
3233   case SystemZ::BI__builtin_s390_vsldb: i = 2; l = 0; u = 15; break;
3234   case SystemZ::BI__builtin_s390_vstrcb:
3235   case SystemZ::BI__builtin_s390_vstrch:
3236   case SystemZ::BI__builtin_s390_vstrcf:
3237   case SystemZ::BI__builtin_s390_vstrczb:
3238   case SystemZ::BI__builtin_s390_vstrczh:
3239   case SystemZ::BI__builtin_s390_vstrczf:
3240   case SystemZ::BI__builtin_s390_vstrcbs:
3241   case SystemZ::BI__builtin_s390_vstrchs:
3242   case SystemZ::BI__builtin_s390_vstrcfs:
3243   case SystemZ::BI__builtin_s390_vstrczbs:
3244   case SystemZ::BI__builtin_s390_vstrczhs:
3245   case SystemZ::BI__builtin_s390_vstrczfs: i = 3; l = 0; u = 15; break;
3246   case SystemZ::BI__builtin_s390_vmslg: i = 3; l = 0; u = 15; break;
3247   case SystemZ::BI__builtin_s390_vfminsb:
3248   case SystemZ::BI__builtin_s390_vfmaxsb:
3249   case SystemZ::BI__builtin_s390_vfmindb:
3250   case SystemZ::BI__builtin_s390_vfmaxdb: i = 2; l = 0; u = 15; break;
3251   case SystemZ::BI__builtin_s390_vsld: i = 2; l = 0; u = 7; break;
3252   case SystemZ::BI__builtin_s390_vsrd: i = 2; l = 0; u = 7; break;
3253   }
3254   return SemaBuiltinConstantArgRange(TheCall, i, l, u);
3255 }
3256 
3257 /// SemaBuiltinCpuSupports - Handle __builtin_cpu_supports(char *).
3258 /// This checks that the target supports __builtin_cpu_supports and
3259 /// that the string argument is constant and valid.
3260 static bool SemaBuiltinCpuSupports(Sema &S, const TargetInfo &TI,
3261                                    CallExpr *TheCall) {
3262   Expr *Arg = TheCall->getArg(0);
3263 
3264   // Check if the argument is a string literal.
3265   if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts()))
3266     return S.Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal)
3267            << Arg->getSourceRange();
3268 
3269   // Check the contents of the string.
3270   StringRef Feature =
3271       cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString();
3272   if (!TI.validateCpuSupports(Feature))
3273     return S.Diag(TheCall->getBeginLoc(), diag::err_invalid_cpu_supports)
3274            << Arg->getSourceRange();
3275   return false;
3276 }
3277 
3278 /// SemaBuiltinCpuIs - Handle __builtin_cpu_is(char *).
3279 /// This checks that the target supports __builtin_cpu_is and
3280 /// that the string argument is constant and valid.
3281 static bool SemaBuiltinCpuIs(Sema &S, const TargetInfo &TI, CallExpr *TheCall) {
3282   Expr *Arg = TheCall->getArg(0);
3283 
3284   // Check if the argument is a string literal.
3285   if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts()))
3286     return S.Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal)
3287            << Arg->getSourceRange();
3288 
3289   // Check the contents of the string.
3290   StringRef Feature =
3291       cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString();
3292   if (!TI.validateCpuIs(Feature))
3293     return S.Diag(TheCall->getBeginLoc(), diag::err_invalid_cpu_is)
3294            << Arg->getSourceRange();
3295   return false;
3296 }
3297 
3298 // Check if the rounding mode is legal.
3299 bool Sema::CheckX86BuiltinRoundingOrSAE(unsigned BuiltinID, CallExpr *TheCall) {
3300   // Indicates if this instruction has rounding control or just SAE.
3301   bool HasRC = false;
3302 
3303   unsigned ArgNum = 0;
3304   switch (BuiltinID) {
3305   default:
3306     return false;
3307   case X86::BI__builtin_ia32_vcvttsd2si32:
3308   case X86::BI__builtin_ia32_vcvttsd2si64:
3309   case X86::BI__builtin_ia32_vcvttsd2usi32:
3310   case X86::BI__builtin_ia32_vcvttsd2usi64:
3311   case X86::BI__builtin_ia32_vcvttss2si32:
3312   case X86::BI__builtin_ia32_vcvttss2si64:
3313   case X86::BI__builtin_ia32_vcvttss2usi32:
3314   case X86::BI__builtin_ia32_vcvttss2usi64:
3315     ArgNum = 1;
3316     break;
3317   case X86::BI__builtin_ia32_maxpd512:
3318   case X86::BI__builtin_ia32_maxps512:
3319   case X86::BI__builtin_ia32_minpd512:
3320   case X86::BI__builtin_ia32_minps512:
3321     ArgNum = 2;
3322     break;
3323   case X86::BI__builtin_ia32_cvtps2pd512_mask:
3324   case X86::BI__builtin_ia32_cvttpd2dq512_mask:
3325   case X86::BI__builtin_ia32_cvttpd2qq512_mask:
3326   case X86::BI__builtin_ia32_cvttpd2udq512_mask:
3327   case X86::BI__builtin_ia32_cvttpd2uqq512_mask:
3328   case X86::BI__builtin_ia32_cvttps2dq512_mask:
3329   case X86::BI__builtin_ia32_cvttps2qq512_mask:
3330   case X86::BI__builtin_ia32_cvttps2udq512_mask:
3331   case X86::BI__builtin_ia32_cvttps2uqq512_mask:
3332   case X86::BI__builtin_ia32_exp2pd_mask:
3333   case X86::BI__builtin_ia32_exp2ps_mask:
3334   case X86::BI__builtin_ia32_getexppd512_mask:
3335   case X86::BI__builtin_ia32_getexpps512_mask:
3336   case X86::BI__builtin_ia32_rcp28pd_mask:
3337   case X86::BI__builtin_ia32_rcp28ps_mask:
3338   case X86::BI__builtin_ia32_rsqrt28pd_mask:
3339   case X86::BI__builtin_ia32_rsqrt28ps_mask:
3340   case X86::BI__builtin_ia32_vcomisd:
3341   case X86::BI__builtin_ia32_vcomiss:
3342   case X86::BI__builtin_ia32_vcvtph2ps512_mask:
3343     ArgNum = 3;
3344     break;
3345   case X86::BI__builtin_ia32_cmppd512_mask:
3346   case X86::BI__builtin_ia32_cmpps512_mask:
3347   case X86::BI__builtin_ia32_cmpsd_mask:
3348   case X86::BI__builtin_ia32_cmpss_mask:
3349   case X86::BI__builtin_ia32_cvtss2sd_round_mask:
3350   case X86::BI__builtin_ia32_getexpsd128_round_mask:
3351   case X86::BI__builtin_ia32_getexpss128_round_mask:
3352   case X86::BI__builtin_ia32_getmantpd512_mask:
3353   case X86::BI__builtin_ia32_getmantps512_mask:
3354   case X86::BI__builtin_ia32_maxsd_round_mask:
3355   case X86::BI__builtin_ia32_maxss_round_mask:
3356   case X86::BI__builtin_ia32_minsd_round_mask:
3357   case X86::BI__builtin_ia32_minss_round_mask:
3358   case X86::BI__builtin_ia32_rcp28sd_round_mask:
3359   case X86::BI__builtin_ia32_rcp28ss_round_mask:
3360   case X86::BI__builtin_ia32_reducepd512_mask:
3361   case X86::BI__builtin_ia32_reduceps512_mask:
3362   case X86::BI__builtin_ia32_rndscalepd_mask:
3363   case X86::BI__builtin_ia32_rndscaleps_mask:
3364   case X86::BI__builtin_ia32_rsqrt28sd_round_mask:
3365   case X86::BI__builtin_ia32_rsqrt28ss_round_mask:
3366     ArgNum = 4;
3367     break;
3368   case X86::BI__builtin_ia32_fixupimmpd512_mask:
3369   case X86::BI__builtin_ia32_fixupimmpd512_maskz:
3370   case X86::BI__builtin_ia32_fixupimmps512_mask:
3371   case X86::BI__builtin_ia32_fixupimmps512_maskz:
3372   case X86::BI__builtin_ia32_fixupimmsd_mask:
3373   case X86::BI__builtin_ia32_fixupimmsd_maskz:
3374   case X86::BI__builtin_ia32_fixupimmss_mask:
3375   case X86::BI__builtin_ia32_fixupimmss_maskz:
3376   case X86::BI__builtin_ia32_getmantsd_round_mask:
3377   case X86::BI__builtin_ia32_getmantss_round_mask:
3378   case X86::BI__builtin_ia32_rangepd512_mask:
3379   case X86::BI__builtin_ia32_rangeps512_mask:
3380   case X86::BI__builtin_ia32_rangesd128_round_mask:
3381   case X86::BI__builtin_ia32_rangess128_round_mask:
3382   case X86::BI__builtin_ia32_reducesd_mask:
3383   case X86::BI__builtin_ia32_reducess_mask:
3384   case X86::BI__builtin_ia32_rndscalesd_round_mask:
3385   case X86::BI__builtin_ia32_rndscaless_round_mask:
3386     ArgNum = 5;
3387     break;
3388   case X86::BI__builtin_ia32_vcvtsd2si64:
3389   case X86::BI__builtin_ia32_vcvtsd2si32:
3390   case X86::BI__builtin_ia32_vcvtsd2usi32:
3391   case X86::BI__builtin_ia32_vcvtsd2usi64:
3392   case X86::BI__builtin_ia32_vcvtss2si32:
3393   case X86::BI__builtin_ia32_vcvtss2si64:
3394   case X86::BI__builtin_ia32_vcvtss2usi32:
3395   case X86::BI__builtin_ia32_vcvtss2usi64:
3396   case X86::BI__builtin_ia32_sqrtpd512:
3397   case X86::BI__builtin_ia32_sqrtps512:
3398     ArgNum = 1;
3399     HasRC = true;
3400     break;
3401   case X86::BI__builtin_ia32_addpd512:
3402   case X86::BI__builtin_ia32_addps512:
3403   case X86::BI__builtin_ia32_divpd512:
3404   case X86::BI__builtin_ia32_divps512:
3405   case X86::BI__builtin_ia32_mulpd512:
3406   case X86::BI__builtin_ia32_mulps512:
3407   case X86::BI__builtin_ia32_subpd512:
3408   case X86::BI__builtin_ia32_subps512:
3409   case X86::BI__builtin_ia32_cvtsi2sd64:
3410   case X86::BI__builtin_ia32_cvtsi2ss32:
3411   case X86::BI__builtin_ia32_cvtsi2ss64:
3412   case X86::BI__builtin_ia32_cvtusi2sd64:
3413   case X86::BI__builtin_ia32_cvtusi2ss32:
3414   case X86::BI__builtin_ia32_cvtusi2ss64:
3415     ArgNum = 2;
3416     HasRC = true;
3417     break;
3418   case X86::BI__builtin_ia32_cvtdq2ps512_mask:
3419   case X86::BI__builtin_ia32_cvtudq2ps512_mask:
3420   case X86::BI__builtin_ia32_cvtpd2ps512_mask:
3421   case X86::BI__builtin_ia32_cvtpd2dq512_mask:
3422   case X86::BI__builtin_ia32_cvtpd2qq512_mask:
3423   case X86::BI__builtin_ia32_cvtpd2udq512_mask:
3424   case X86::BI__builtin_ia32_cvtpd2uqq512_mask:
3425   case X86::BI__builtin_ia32_cvtps2dq512_mask:
3426   case X86::BI__builtin_ia32_cvtps2qq512_mask:
3427   case X86::BI__builtin_ia32_cvtps2udq512_mask:
3428   case X86::BI__builtin_ia32_cvtps2uqq512_mask:
3429   case X86::BI__builtin_ia32_cvtqq2pd512_mask:
3430   case X86::BI__builtin_ia32_cvtqq2ps512_mask:
3431   case X86::BI__builtin_ia32_cvtuqq2pd512_mask:
3432   case X86::BI__builtin_ia32_cvtuqq2ps512_mask:
3433     ArgNum = 3;
3434     HasRC = true;
3435     break;
3436   case X86::BI__builtin_ia32_addss_round_mask:
3437   case X86::BI__builtin_ia32_addsd_round_mask:
3438   case X86::BI__builtin_ia32_divss_round_mask:
3439   case X86::BI__builtin_ia32_divsd_round_mask:
3440   case X86::BI__builtin_ia32_mulss_round_mask:
3441   case X86::BI__builtin_ia32_mulsd_round_mask:
3442   case X86::BI__builtin_ia32_subss_round_mask:
3443   case X86::BI__builtin_ia32_subsd_round_mask:
3444   case X86::BI__builtin_ia32_scalefpd512_mask:
3445   case X86::BI__builtin_ia32_scalefps512_mask:
3446   case X86::BI__builtin_ia32_scalefsd_round_mask:
3447   case X86::BI__builtin_ia32_scalefss_round_mask:
3448   case X86::BI__builtin_ia32_cvtsd2ss_round_mask:
3449   case X86::BI__builtin_ia32_sqrtsd_round_mask:
3450   case X86::BI__builtin_ia32_sqrtss_round_mask:
3451   case X86::BI__builtin_ia32_vfmaddsd3_mask:
3452   case X86::BI__builtin_ia32_vfmaddsd3_maskz:
3453   case X86::BI__builtin_ia32_vfmaddsd3_mask3:
3454   case X86::BI__builtin_ia32_vfmaddss3_mask:
3455   case X86::BI__builtin_ia32_vfmaddss3_maskz:
3456   case X86::BI__builtin_ia32_vfmaddss3_mask3:
3457   case X86::BI__builtin_ia32_vfmaddpd512_mask:
3458   case X86::BI__builtin_ia32_vfmaddpd512_maskz:
3459   case X86::BI__builtin_ia32_vfmaddpd512_mask3:
3460   case X86::BI__builtin_ia32_vfmsubpd512_mask3:
3461   case X86::BI__builtin_ia32_vfmaddps512_mask:
3462   case X86::BI__builtin_ia32_vfmaddps512_maskz:
3463   case X86::BI__builtin_ia32_vfmaddps512_mask3:
3464   case X86::BI__builtin_ia32_vfmsubps512_mask3:
3465   case X86::BI__builtin_ia32_vfmaddsubpd512_mask:
3466   case X86::BI__builtin_ia32_vfmaddsubpd512_maskz:
3467   case X86::BI__builtin_ia32_vfmaddsubpd512_mask3:
3468   case X86::BI__builtin_ia32_vfmsubaddpd512_mask3:
3469   case X86::BI__builtin_ia32_vfmaddsubps512_mask:
3470   case X86::BI__builtin_ia32_vfmaddsubps512_maskz:
3471   case X86::BI__builtin_ia32_vfmaddsubps512_mask3:
3472   case X86::BI__builtin_ia32_vfmsubaddps512_mask3:
3473     ArgNum = 4;
3474     HasRC = true;
3475     break;
3476   }
3477 
3478   llvm::APSInt Result;
3479 
3480   // We can't check the value of a dependent argument.
3481   Expr *Arg = TheCall->getArg(ArgNum);
3482   if (Arg->isTypeDependent() || Arg->isValueDependent())
3483     return false;
3484 
3485   // Check constant-ness first.
3486   if (SemaBuiltinConstantArg(TheCall, ArgNum, Result))
3487     return true;
3488 
3489   // Make sure rounding mode is either ROUND_CUR_DIRECTION or ROUND_NO_EXC bit
3490   // is set. If the intrinsic has rounding control(bits 1:0), make sure its only
3491   // combined with ROUND_NO_EXC. If the intrinsic does not have rounding
3492   // control, allow ROUND_NO_EXC and ROUND_CUR_DIRECTION together.
3493   if (Result == 4/*ROUND_CUR_DIRECTION*/ ||
3494       Result == 8/*ROUND_NO_EXC*/ ||
3495       (!HasRC && Result == 12/*ROUND_CUR_DIRECTION|ROUND_NO_EXC*/) ||
3496       (HasRC && Result.getZExtValue() >= 8 && Result.getZExtValue() <= 11))
3497     return false;
3498 
3499   return Diag(TheCall->getBeginLoc(), diag::err_x86_builtin_invalid_rounding)
3500          << Arg->getSourceRange();
3501 }
3502 
3503 // Check if the gather/scatter scale is legal.
3504 bool Sema::CheckX86BuiltinGatherScatterScale(unsigned BuiltinID,
3505                                              CallExpr *TheCall) {
3506   unsigned ArgNum = 0;
3507   switch (BuiltinID) {
3508   default:
3509     return false;
3510   case X86::BI__builtin_ia32_gatherpfdpd:
3511   case X86::BI__builtin_ia32_gatherpfdps:
3512   case X86::BI__builtin_ia32_gatherpfqpd:
3513   case X86::BI__builtin_ia32_gatherpfqps:
3514   case X86::BI__builtin_ia32_scatterpfdpd:
3515   case X86::BI__builtin_ia32_scatterpfdps:
3516   case X86::BI__builtin_ia32_scatterpfqpd:
3517   case X86::BI__builtin_ia32_scatterpfqps:
3518     ArgNum = 3;
3519     break;
3520   case X86::BI__builtin_ia32_gatherd_pd:
3521   case X86::BI__builtin_ia32_gatherd_pd256:
3522   case X86::BI__builtin_ia32_gatherq_pd:
3523   case X86::BI__builtin_ia32_gatherq_pd256:
3524   case X86::BI__builtin_ia32_gatherd_ps:
3525   case X86::BI__builtin_ia32_gatherd_ps256:
3526   case X86::BI__builtin_ia32_gatherq_ps:
3527   case X86::BI__builtin_ia32_gatherq_ps256:
3528   case X86::BI__builtin_ia32_gatherd_q:
3529   case X86::BI__builtin_ia32_gatherd_q256:
3530   case X86::BI__builtin_ia32_gatherq_q:
3531   case X86::BI__builtin_ia32_gatherq_q256:
3532   case X86::BI__builtin_ia32_gatherd_d:
3533   case X86::BI__builtin_ia32_gatherd_d256:
3534   case X86::BI__builtin_ia32_gatherq_d:
3535   case X86::BI__builtin_ia32_gatherq_d256:
3536   case X86::BI__builtin_ia32_gather3div2df:
3537   case X86::BI__builtin_ia32_gather3div2di:
3538   case X86::BI__builtin_ia32_gather3div4df:
3539   case X86::BI__builtin_ia32_gather3div4di:
3540   case X86::BI__builtin_ia32_gather3div4sf:
3541   case X86::BI__builtin_ia32_gather3div4si:
3542   case X86::BI__builtin_ia32_gather3div8sf:
3543   case X86::BI__builtin_ia32_gather3div8si:
3544   case X86::BI__builtin_ia32_gather3siv2df:
3545   case X86::BI__builtin_ia32_gather3siv2di:
3546   case X86::BI__builtin_ia32_gather3siv4df:
3547   case X86::BI__builtin_ia32_gather3siv4di:
3548   case X86::BI__builtin_ia32_gather3siv4sf:
3549   case X86::BI__builtin_ia32_gather3siv4si:
3550   case X86::BI__builtin_ia32_gather3siv8sf:
3551   case X86::BI__builtin_ia32_gather3siv8si:
3552   case X86::BI__builtin_ia32_gathersiv8df:
3553   case X86::BI__builtin_ia32_gathersiv16sf:
3554   case X86::BI__builtin_ia32_gatherdiv8df:
3555   case X86::BI__builtin_ia32_gatherdiv16sf:
3556   case X86::BI__builtin_ia32_gathersiv8di:
3557   case X86::BI__builtin_ia32_gathersiv16si:
3558   case X86::BI__builtin_ia32_gatherdiv8di:
3559   case X86::BI__builtin_ia32_gatherdiv16si:
3560   case X86::BI__builtin_ia32_scatterdiv2df:
3561   case X86::BI__builtin_ia32_scatterdiv2di:
3562   case X86::BI__builtin_ia32_scatterdiv4df:
3563   case X86::BI__builtin_ia32_scatterdiv4di:
3564   case X86::BI__builtin_ia32_scatterdiv4sf:
3565   case X86::BI__builtin_ia32_scatterdiv4si:
3566   case X86::BI__builtin_ia32_scatterdiv8sf:
3567   case X86::BI__builtin_ia32_scatterdiv8si:
3568   case X86::BI__builtin_ia32_scattersiv2df:
3569   case X86::BI__builtin_ia32_scattersiv2di:
3570   case X86::BI__builtin_ia32_scattersiv4df:
3571   case X86::BI__builtin_ia32_scattersiv4di:
3572   case X86::BI__builtin_ia32_scattersiv4sf:
3573   case X86::BI__builtin_ia32_scattersiv4si:
3574   case X86::BI__builtin_ia32_scattersiv8sf:
3575   case X86::BI__builtin_ia32_scattersiv8si:
3576   case X86::BI__builtin_ia32_scattersiv8df:
3577   case X86::BI__builtin_ia32_scattersiv16sf:
3578   case X86::BI__builtin_ia32_scatterdiv8df:
3579   case X86::BI__builtin_ia32_scatterdiv16sf:
3580   case X86::BI__builtin_ia32_scattersiv8di:
3581   case X86::BI__builtin_ia32_scattersiv16si:
3582   case X86::BI__builtin_ia32_scatterdiv8di:
3583   case X86::BI__builtin_ia32_scatterdiv16si:
3584     ArgNum = 4;
3585     break;
3586   }
3587 
3588   llvm::APSInt Result;
3589 
3590   // We can't check the value of a dependent argument.
3591   Expr *Arg = TheCall->getArg(ArgNum);
3592   if (Arg->isTypeDependent() || Arg->isValueDependent())
3593     return false;
3594 
3595   // Check constant-ness first.
3596   if (SemaBuiltinConstantArg(TheCall, ArgNum, Result))
3597     return true;
3598 
3599   if (Result == 1 || Result == 2 || Result == 4 || Result == 8)
3600     return false;
3601 
3602   return Diag(TheCall->getBeginLoc(), diag::err_x86_builtin_invalid_scale)
3603          << Arg->getSourceRange();
3604 }
3605 
3606 enum { TileRegLow = 0, TileRegHigh = 7 };
3607 
3608 bool Sema::CheckX86BuiltinTileArgumentsRange(CallExpr *TheCall,
3609                                     ArrayRef<int> ArgNums) {
3610   for (int ArgNum : ArgNums) {
3611     if (SemaBuiltinConstantArgRange(TheCall, ArgNum, TileRegLow, TileRegHigh))
3612       return true;
3613   }
3614   return false;
3615 }
3616 
3617 bool Sema::CheckX86BuiltinTileArgumentsRange(CallExpr *TheCall, int ArgNum) {
3618   return SemaBuiltinConstantArgRange(TheCall, ArgNum, TileRegLow, TileRegHigh);
3619 }
3620 
3621 bool Sema::CheckX86BuiltinTileDuplicate(CallExpr *TheCall,
3622                                         ArrayRef<int> ArgNums) {
3623   // Because the max number of tile register is TileRegHigh + 1, so here we use
3624   // each bit to represent the usage of them in bitset.
3625   std::bitset<TileRegHigh + 1> ArgValues;
3626   for (int ArgNum : ArgNums) {
3627     llvm::APSInt Arg;
3628     SemaBuiltinConstantArg(TheCall, ArgNum, Arg);
3629     int ArgExtValue = Arg.getExtValue();
3630     assert((ArgExtValue >= TileRegLow || ArgExtValue <= TileRegHigh) &&
3631            "Incorrect tile register num.");
3632     if (ArgValues.test(ArgExtValue))
3633       return Diag(TheCall->getBeginLoc(),
3634                   diag::err_x86_builtin_tile_arg_duplicate)
3635              << TheCall->getArg(ArgNum)->getSourceRange();
3636     ArgValues.set(ArgExtValue);
3637   }
3638   return false;
3639 }
3640 
3641 bool Sema::CheckX86BuiltinTileRangeAndDuplicate(CallExpr *TheCall,
3642                                                 ArrayRef<int> ArgNums) {
3643   return CheckX86BuiltinTileArgumentsRange(TheCall, ArgNums) ||
3644          CheckX86BuiltinTileDuplicate(TheCall, ArgNums);
3645 }
3646 
3647 bool Sema::CheckX86BuiltinTileArguments(unsigned BuiltinID, CallExpr *TheCall) {
3648   switch (BuiltinID) {
3649   default:
3650     return false;
3651   case X86::BI__builtin_ia32_tileloadd64:
3652   case X86::BI__builtin_ia32_tileloaddt164:
3653   case X86::BI__builtin_ia32_tilestored64:
3654   case X86::BI__builtin_ia32_tilezero:
3655     return CheckX86BuiltinTileArgumentsRange(TheCall, 0);
3656   case X86::BI__builtin_ia32_tdpbssd:
3657   case X86::BI__builtin_ia32_tdpbsud:
3658   case X86::BI__builtin_ia32_tdpbusd:
3659   case X86::BI__builtin_ia32_tdpbuud:
3660   case X86::BI__builtin_ia32_tdpbf16ps:
3661     return CheckX86BuiltinTileRangeAndDuplicate(TheCall, {0, 1, 2});
3662   }
3663 }
3664 static bool isX86_32Builtin(unsigned BuiltinID) {
3665   // These builtins only work on x86-32 targets.
3666   switch (BuiltinID) {
3667   case X86::BI__builtin_ia32_readeflags_u32:
3668   case X86::BI__builtin_ia32_writeeflags_u32:
3669     return true;
3670   }
3671 
3672   return false;
3673 }
3674 
3675 bool Sema::CheckX86BuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
3676                                        CallExpr *TheCall) {
3677   if (BuiltinID == X86::BI__builtin_cpu_supports)
3678     return SemaBuiltinCpuSupports(*this, TI, TheCall);
3679 
3680   if (BuiltinID == X86::BI__builtin_cpu_is)
3681     return SemaBuiltinCpuIs(*this, TI, TheCall);
3682 
3683   // Check for 32-bit only builtins on a 64-bit target.
3684   const llvm::Triple &TT = TI.getTriple();
3685   if (TT.getArch() != llvm::Triple::x86 && isX86_32Builtin(BuiltinID))
3686     return Diag(TheCall->getCallee()->getBeginLoc(),
3687                 diag::err_32_bit_builtin_64_bit_tgt);
3688 
3689   // If the intrinsic has rounding or SAE make sure its valid.
3690   if (CheckX86BuiltinRoundingOrSAE(BuiltinID, TheCall))
3691     return true;
3692 
3693   // If the intrinsic has a gather/scatter scale immediate make sure its valid.
3694   if (CheckX86BuiltinGatherScatterScale(BuiltinID, TheCall))
3695     return true;
3696 
3697   // If the intrinsic has a tile arguments, make sure they are valid.
3698   if (CheckX86BuiltinTileArguments(BuiltinID, TheCall))
3699     return true;
3700 
3701   // For intrinsics which take an immediate value as part of the instruction,
3702   // range check them here.
3703   int i = 0, l = 0, u = 0;
3704   switch (BuiltinID) {
3705   default:
3706     return false;
3707   case X86::BI__builtin_ia32_vec_ext_v2si:
3708   case X86::BI__builtin_ia32_vec_ext_v2di:
3709   case X86::BI__builtin_ia32_vextractf128_pd256:
3710   case X86::BI__builtin_ia32_vextractf128_ps256:
3711   case X86::BI__builtin_ia32_vextractf128_si256:
3712   case X86::BI__builtin_ia32_extract128i256:
3713   case X86::BI__builtin_ia32_extractf64x4_mask:
3714   case X86::BI__builtin_ia32_extracti64x4_mask:
3715   case X86::BI__builtin_ia32_extractf32x8_mask:
3716   case X86::BI__builtin_ia32_extracti32x8_mask:
3717   case X86::BI__builtin_ia32_extractf64x2_256_mask:
3718   case X86::BI__builtin_ia32_extracti64x2_256_mask:
3719   case X86::BI__builtin_ia32_extractf32x4_256_mask:
3720   case X86::BI__builtin_ia32_extracti32x4_256_mask:
3721     i = 1; l = 0; u = 1;
3722     break;
3723   case X86::BI__builtin_ia32_vec_set_v2di:
3724   case X86::BI__builtin_ia32_vinsertf128_pd256:
3725   case X86::BI__builtin_ia32_vinsertf128_ps256:
3726   case X86::BI__builtin_ia32_vinsertf128_si256:
3727   case X86::BI__builtin_ia32_insert128i256:
3728   case X86::BI__builtin_ia32_insertf32x8:
3729   case X86::BI__builtin_ia32_inserti32x8:
3730   case X86::BI__builtin_ia32_insertf64x4:
3731   case X86::BI__builtin_ia32_inserti64x4:
3732   case X86::BI__builtin_ia32_insertf64x2_256:
3733   case X86::BI__builtin_ia32_inserti64x2_256:
3734   case X86::BI__builtin_ia32_insertf32x4_256:
3735   case X86::BI__builtin_ia32_inserti32x4_256:
3736     i = 2; l = 0; u = 1;
3737     break;
3738   case X86::BI__builtin_ia32_vpermilpd:
3739   case X86::BI__builtin_ia32_vec_ext_v4hi:
3740   case X86::BI__builtin_ia32_vec_ext_v4si:
3741   case X86::BI__builtin_ia32_vec_ext_v4sf:
3742   case X86::BI__builtin_ia32_vec_ext_v4di:
3743   case X86::BI__builtin_ia32_extractf32x4_mask:
3744   case X86::BI__builtin_ia32_extracti32x4_mask:
3745   case X86::BI__builtin_ia32_extractf64x2_512_mask:
3746   case X86::BI__builtin_ia32_extracti64x2_512_mask:
3747     i = 1; l = 0; u = 3;
3748     break;
3749   case X86::BI_mm_prefetch:
3750   case X86::BI__builtin_ia32_vec_ext_v8hi:
3751   case X86::BI__builtin_ia32_vec_ext_v8si:
3752     i = 1; l = 0; u = 7;
3753     break;
3754   case X86::BI__builtin_ia32_sha1rnds4:
3755   case X86::BI__builtin_ia32_blendpd:
3756   case X86::BI__builtin_ia32_shufpd:
3757   case X86::BI__builtin_ia32_vec_set_v4hi:
3758   case X86::BI__builtin_ia32_vec_set_v4si:
3759   case X86::BI__builtin_ia32_vec_set_v4di:
3760   case X86::BI__builtin_ia32_shuf_f32x4_256:
3761   case X86::BI__builtin_ia32_shuf_f64x2_256:
3762   case X86::BI__builtin_ia32_shuf_i32x4_256:
3763   case X86::BI__builtin_ia32_shuf_i64x2_256:
3764   case X86::BI__builtin_ia32_insertf64x2_512:
3765   case X86::BI__builtin_ia32_inserti64x2_512:
3766   case X86::BI__builtin_ia32_insertf32x4:
3767   case X86::BI__builtin_ia32_inserti32x4:
3768     i = 2; l = 0; u = 3;
3769     break;
3770   case X86::BI__builtin_ia32_vpermil2pd:
3771   case X86::BI__builtin_ia32_vpermil2pd256:
3772   case X86::BI__builtin_ia32_vpermil2ps:
3773   case X86::BI__builtin_ia32_vpermil2ps256:
3774     i = 3; l = 0; u = 3;
3775     break;
3776   case X86::BI__builtin_ia32_cmpb128_mask:
3777   case X86::BI__builtin_ia32_cmpw128_mask:
3778   case X86::BI__builtin_ia32_cmpd128_mask:
3779   case X86::BI__builtin_ia32_cmpq128_mask:
3780   case X86::BI__builtin_ia32_cmpb256_mask:
3781   case X86::BI__builtin_ia32_cmpw256_mask:
3782   case X86::BI__builtin_ia32_cmpd256_mask:
3783   case X86::BI__builtin_ia32_cmpq256_mask:
3784   case X86::BI__builtin_ia32_cmpb512_mask:
3785   case X86::BI__builtin_ia32_cmpw512_mask:
3786   case X86::BI__builtin_ia32_cmpd512_mask:
3787   case X86::BI__builtin_ia32_cmpq512_mask:
3788   case X86::BI__builtin_ia32_ucmpb128_mask:
3789   case X86::BI__builtin_ia32_ucmpw128_mask:
3790   case X86::BI__builtin_ia32_ucmpd128_mask:
3791   case X86::BI__builtin_ia32_ucmpq128_mask:
3792   case X86::BI__builtin_ia32_ucmpb256_mask:
3793   case X86::BI__builtin_ia32_ucmpw256_mask:
3794   case X86::BI__builtin_ia32_ucmpd256_mask:
3795   case X86::BI__builtin_ia32_ucmpq256_mask:
3796   case X86::BI__builtin_ia32_ucmpb512_mask:
3797   case X86::BI__builtin_ia32_ucmpw512_mask:
3798   case X86::BI__builtin_ia32_ucmpd512_mask:
3799   case X86::BI__builtin_ia32_ucmpq512_mask:
3800   case X86::BI__builtin_ia32_vpcomub:
3801   case X86::BI__builtin_ia32_vpcomuw:
3802   case X86::BI__builtin_ia32_vpcomud:
3803   case X86::BI__builtin_ia32_vpcomuq:
3804   case X86::BI__builtin_ia32_vpcomb:
3805   case X86::BI__builtin_ia32_vpcomw:
3806   case X86::BI__builtin_ia32_vpcomd:
3807   case X86::BI__builtin_ia32_vpcomq:
3808   case X86::BI__builtin_ia32_vec_set_v8hi:
3809   case X86::BI__builtin_ia32_vec_set_v8si:
3810     i = 2; l = 0; u = 7;
3811     break;
3812   case X86::BI__builtin_ia32_vpermilpd256:
3813   case X86::BI__builtin_ia32_roundps:
3814   case X86::BI__builtin_ia32_roundpd:
3815   case X86::BI__builtin_ia32_roundps256:
3816   case X86::BI__builtin_ia32_roundpd256:
3817   case X86::BI__builtin_ia32_getmantpd128_mask:
3818   case X86::BI__builtin_ia32_getmantpd256_mask:
3819   case X86::BI__builtin_ia32_getmantps128_mask:
3820   case X86::BI__builtin_ia32_getmantps256_mask:
3821   case X86::BI__builtin_ia32_getmantpd512_mask:
3822   case X86::BI__builtin_ia32_getmantps512_mask:
3823   case X86::BI__builtin_ia32_vec_ext_v16qi:
3824   case X86::BI__builtin_ia32_vec_ext_v16hi:
3825     i = 1; l = 0; u = 15;
3826     break;
3827   case X86::BI__builtin_ia32_pblendd128:
3828   case X86::BI__builtin_ia32_blendps:
3829   case X86::BI__builtin_ia32_blendpd256:
3830   case X86::BI__builtin_ia32_shufpd256:
3831   case X86::BI__builtin_ia32_roundss:
3832   case X86::BI__builtin_ia32_roundsd:
3833   case X86::BI__builtin_ia32_rangepd128_mask:
3834   case X86::BI__builtin_ia32_rangepd256_mask:
3835   case X86::BI__builtin_ia32_rangepd512_mask:
3836   case X86::BI__builtin_ia32_rangeps128_mask:
3837   case X86::BI__builtin_ia32_rangeps256_mask:
3838   case X86::BI__builtin_ia32_rangeps512_mask:
3839   case X86::BI__builtin_ia32_getmantsd_round_mask:
3840   case X86::BI__builtin_ia32_getmantss_round_mask:
3841   case X86::BI__builtin_ia32_vec_set_v16qi:
3842   case X86::BI__builtin_ia32_vec_set_v16hi:
3843     i = 2; l = 0; u = 15;
3844     break;
3845   case X86::BI__builtin_ia32_vec_ext_v32qi:
3846     i = 1; l = 0; u = 31;
3847     break;
3848   case X86::BI__builtin_ia32_cmpps:
3849   case X86::BI__builtin_ia32_cmpss:
3850   case X86::BI__builtin_ia32_cmppd:
3851   case X86::BI__builtin_ia32_cmpsd:
3852   case X86::BI__builtin_ia32_cmpps256:
3853   case X86::BI__builtin_ia32_cmppd256:
3854   case X86::BI__builtin_ia32_cmpps128_mask:
3855   case X86::BI__builtin_ia32_cmppd128_mask:
3856   case X86::BI__builtin_ia32_cmpps256_mask:
3857   case X86::BI__builtin_ia32_cmppd256_mask:
3858   case X86::BI__builtin_ia32_cmpps512_mask:
3859   case X86::BI__builtin_ia32_cmppd512_mask:
3860   case X86::BI__builtin_ia32_cmpsd_mask:
3861   case X86::BI__builtin_ia32_cmpss_mask:
3862   case X86::BI__builtin_ia32_vec_set_v32qi:
3863     i = 2; l = 0; u = 31;
3864     break;
3865   case X86::BI__builtin_ia32_permdf256:
3866   case X86::BI__builtin_ia32_permdi256:
3867   case X86::BI__builtin_ia32_permdf512:
3868   case X86::BI__builtin_ia32_permdi512:
3869   case X86::BI__builtin_ia32_vpermilps:
3870   case X86::BI__builtin_ia32_vpermilps256:
3871   case X86::BI__builtin_ia32_vpermilpd512:
3872   case X86::BI__builtin_ia32_vpermilps512:
3873   case X86::BI__builtin_ia32_pshufd:
3874   case X86::BI__builtin_ia32_pshufd256:
3875   case X86::BI__builtin_ia32_pshufd512:
3876   case X86::BI__builtin_ia32_pshufhw:
3877   case X86::BI__builtin_ia32_pshufhw256:
3878   case X86::BI__builtin_ia32_pshufhw512:
3879   case X86::BI__builtin_ia32_pshuflw:
3880   case X86::BI__builtin_ia32_pshuflw256:
3881   case X86::BI__builtin_ia32_pshuflw512:
3882   case X86::BI__builtin_ia32_vcvtps2ph:
3883   case X86::BI__builtin_ia32_vcvtps2ph_mask:
3884   case X86::BI__builtin_ia32_vcvtps2ph256:
3885   case X86::BI__builtin_ia32_vcvtps2ph256_mask:
3886   case X86::BI__builtin_ia32_vcvtps2ph512_mask:
3887   case X86::BI__builtin_ia32_rndscaleps_128_mask:
3888   case X86::BI__builtin_ia32_rndscalepd_128_mask:
3889   case X86::BI__builtin_ia32_rndscaleps_256_mask:
3890   case X86::BI__builtin_ia32_rndscalepd_256_mask:
3891   case X86::BI__builtin_ia32_rndscaleps_mask:
3892   case X86::BI__builtin_ia32_rndscalepd_mask:
3893   case X86::BI__builtin_ia32_reducepd128_mask:
3894   case X86::BI__builtin_ia32_reducepd256_mask:
3895   case X86::BI__builtin_ia32_reducepd512_mask:
3896   case X86::BI__builtin_ia32_reduceps128_mask:
3897   case X86::BI__builtin_ia32_reduceps256_mask:
3898   case X86::BI__builtin_ia32_reduceps512_mask:
3899   case X86::BI__builtin_ia32_prold512:
3900   case X86::BI__builtin_ia32_prolq512:
3901   case X86::BI__builtin_ia32_prold128:
3902   case X86::BI__builtin_ia32_prold256:
3903   case X86::BI__builtin_ia32_prolq128:
3904   case X86::BI__builtin_ia32_prolq256:
3905   case X86::BI__builtin_ia32_prord512:
3906   case X86::BI__builtin_ia32_prorq512:
3907   case X86::BI__builtin_ia32_prord128:
3908   case X86::BI__builtin_ia32_prord256:
3909   case X86::BI__builtin_ia32_prorq128:
3910   case X86::BI__builtin_ia32_prorq256:
3911   case X86::BI__builtin_ia32_fpclasspd128_mask:
3912   case X86::BI__builtin_ia32_fpclasspd256_mask:
3913   case X86::BI__builtin_ia32_fpclassps128_mask:
3914   case X86::BI__builtin_ia32_fpclassps256_mask:
3915   case X86::BI__builtin_ia32_fpclassps512_mask:
3916   case X86::BI__builtin_ia32_fpclasspd512_mask:
3917   case X86::BI__builtin_ia32_fpclasssd_mask:
3918   case X86::BI__builtin_ia32_fpclassss_mask:
3919   case X86::BI__builtin_ia32_pslldqi128_byteshift:
3920   case X86::BI__builtin_ia32_pslldqi256_byteshift:
3921   case X86::BI__builtin_ia32_pslldqi512_byteshift:
3922   case X86::BI__builtin_ia32_psrldqi128_byteshift:
3923   case X86::BI__builtin_ia32_psrldqi256_byteshift:
3924   case X86::BI__builtin_ia32_psrldqi512_byteshift:
3925   case X86::BI__builtin_ia32_kshiftliqi:
3926   case X86::BI__builtin_ia32_kshiftlihi:
3927   case X86::BI__builtin_ia32_kshiftlisi:
3928   case X86::BI__builtin_ia32_kshiftlidi:
3929   case X86::BI__builtin_ia32_kshiftriqi:
3930   case X86::BI__builtin_ia32_kshiftrihi:
3931   case X86::BI__builtin_ia32_kshiftrisi:
3932   case X86::BI__builtin_ia32_kshiftridi:
3933     i = 1; l = 0; u = 255;
3934     break;
3935   case X86::BI__builtin_ia32_vperm2f128_pd256:
3936   case X86::BI__builtin_ia32_vperm2f128_ps256:
3937   case X86::BI__builtin_ia32_vperm2f128_si256:
3938   case X86::BI__builtin_ia32_permti256:
3939   case X86::BI__builtin_ia32_pblendw128:
3940   case X86::BI__builtin_ia32_pblendw256:
3941   case X86::BI__builtin_ia32_blendps256:
3942   case X86::BI__builtin_ia32_pblendd256:
3943   case X86::BI__builtin_ia32_palignr128:
3944   case X86::BI__builtin_ia32_palignr256:
3945   case X86::BI__builtin_ia32_palignr512:
3946   case X86::BI__builtin_ia32_alignq512:
3947   case X86::BI__builtin_ia32_alignd512:
3948   case X86::BI__builtin_ia32_alignd128:
3949   case X86::BI__builtin_ia32_alignd256:
3950   case X86::BI__builtin_ia32_alignq128:
3951   case X86::BI__builtin_ia32_alignq256:
3952   case X86::BI__builtin_ia32_vcomisd:
3953   case X86::BI__builtin_ia32_vcomiss:
3954   case X86::BI__builtin_ia32_shuf_f32x4:
3955   case X86::BI__builtin_ia32_shuf_f64x2:
3956   case X86::BI__builtin_ia32_shuf_i32x4:
3957   case X86::BI__builtin_ia32_shuf_i64x2:
3958   case X86::BI__builtin_ia32_shufpd512:
3959   case X86::BI__builtin_ia32_shufps:
3960   case X86::BI__builtin_ia32_shufps256:
3961   case X86::BI__builtin_ia32_shufps512:
3962   case X86::BI__builtin_ia32_dbpsadbw128:
3963   case X86::BI__builtin_ia32_dbpsadbw256:
3964   case X86::BI__builtin_ia32_dbpsadbw512:
3965   case X86::BI__builtin_ia32_vpshldd128:
3966   case X86::BI__builtin_ia32_vpshldd256:
3967   case X86::BI__builtin_ia32_vpshldd512:
3968   case X86::BI__builtin_ia32_vpshldq128:
3969   case X86::BI__builtin_ia32_vpshldq256:
3970   case X86::BI__builtin_ia32_vpshldq512:
3971   case X86::BI__builtin_ia32_vpshldw128:
3972   case X86::BI__builtin_ia32_vpshldw256:
3973   case X86::BI__builtin_ia32_vpshldw512:
3974   case X86::BI__builtin_ia32_vpshrdd128:
3975   case X86::BI__builtin_ia32_vpshrdd256:
3976   case X86::BI__builtin_ia32_vpshrdd512:
3977   case X86::BI__builtin_ia32_vpshrdq128:
3978   case X86::BI__builtin_ia32_vpshrdq256:
3979   case X86::BI__builtin_ia32_vpshrdq512:
3980   case X86::BI__builtin_ia32_vpshrdw128:
3981   case X86::BI__builtin_ia32_vpshrdw256:
3982   case X86::BI__builtin_ia32_vpshrdw512:
3983     i = 2; l = 0; u = 255;
3984     break;
3985   case X86::BI__builtin_ia32_fixupimmpd512_mask:
3986   case X86::BI__builtin_ia32_fixupimmpd512_maskz:
3987   case X86::BI__builtin_ia32_fixupimmps512_mask:
3988   case X86::BI__builtin_ia32_fixupimmps512_maskz:
3989   case X86::BI__builtin_ia32_fixupimmsd_mask:
3990   case X86::BI__builtin_ia32_fixupimmsd_maskz:
3991   case X86::BI__builtin_ia32_fixupimmss_mask:
3992   case X86::BI__builtin_ia32_fixupimmss_maskz:
3993   case X86::BI__builtin_ia32_fixupimmpd128_mask:
3994   case X86::BI__builtin_ia32_fixupimmpd128_maskz:
3995   case X86::BI__builtin_ia32_fixupimmpd256_mask:
3996   case X86::BI__builtin_ia32_fixupimmpd256_maskz:
3997   case X86::BI__builtin_ia32_fixupimmps128_mask:
3998   case X86::BI__builtin_ia32_fixupimmps128_maskz:
3999   case X86::BI__builtin_ia32_fixupimmps256_mask:
4000   case X86::BI__builtin_ia32_fixupimmps256_maskz:
4001   case X86::BI__builtin_ia32_pternlogd512_mask:
4002   case X86::BI__builtin_ia32_pternlogd512_maskz:
4003   case X86::BI__builtin_ia32_pternlogq512_mask:
4004   case X86::BI__builtin_ia32_pternlogq512_maskz:
4005   case X86::BI__builtin_ia32_pternlogd128_mask:
4006   case X86::BI__builtin_ia32_pternlogd128_maskz:
4007   case X86::BI__builtin_ia32_pternlogd256_mask:
4008   case X86::BI__builtin_ia32_pternlogd256_maskz:
4009   case X86::BI__builtin_ia32_pternlogq128_mask:
4010   case X86::BI__builtin_ia32_pternlogq128_maskz:
4011   case X86::BI__builtin_ia32_pternlogq256_mask:
4012   case X86::BI__builtin_ia32_pternlogq256_maskz:
4013     i = 3; l = 0; u = 255;
4014     break;
4015   case X86::BI__builtin_ia32_gatherpfdpd:
4016   case X86::BI__builtin_ia32_gatherpfdps:
4017   case X86::BI__builtin_ia32_gatherpfqpd:
4018   case X86::BI__builtin_ia32_gatherpfqps:
4019   case X86::BI__builtin_ia32_scatterpfdpd:
4020   case X86::BI__builtin_ia32_scatterpfdps:
4021   case X86::BI__builtin_ia32_scatterpfqpd:
4022   case X86::BI__builtin_ia32_scatterpfqps:
4023     i = 4; l = 2; u = 3;
4024     break;
4025   case X86::BI__builtin_ia32_reducesd_mask:
4026   case X86::BI__builtin_ia32_reducess_mask:
4027   case X86::BI__builtin_ia32_rndscalesd_round_mask:
4028   case X86::BI__builtin_ia32_rndscaless_round_mask:
4029     i = 4; l = 0; u = 255;
4030     break;
4031   }
4032 
4033   // Note that we don't force a hard error on the range check here, allowing
4034   // template-generated or macro-generated dead code to potentially have out-of-
4035   // range values. These need to code generate, but don't need to necessarily
4036   // make any sense. We use a warning that defaults to an error.
4037   return SemaBuiltinConstantArgRange(TheCall, i, l, u, /*RangeIsError*/ false);
4038 }
4039 
4040 /// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo
4041 /// parameter with the FormatAttr's correct format_idx and firstDataArg.
4042 /// Returns true when the format fits the function and the FormatStringInfo has
4043 /// been populated.
4044 bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember,
4045                                FormatStringInfo *FSI) {
4046   FSI->HasVAListArg = Format->getFirstArg() == 0;
4047   FSI->FormatIdx = Format->getFormatIdx() - 1;
4048   FSI->FirstDataArg = FSI->HasVAListArg ? 0 : Format->getFirstArg() - 1;
4049 
4050   // The way the format attribute works in GCC, the implicit this argument
4051   // of member functions is counted. However, it doesn't appear in our own
4052   // lists, so decrement format_idx in that case.
4053   if (IsCXXMember) {
4054     if(FSI->FormatIdx == 0)
4055       return false;
4056     --FSI->FormatIdx;
4057     if (FSI->FirstDataArg != 0)
4058       --FSI->FirstDataArg;
4059   }
4060   return true;
4061 }
4062 
4063 /// Checks if a the given expression evaluates to null.
4064 ///
4065 /// Returns true if the value evaluates to null.
4066 static bool CheckNonNullExpr(Sema &S, const Expr *Expr) {
4067   // If the expression has non-null type, it doesn't evaluate to null.
4068   if (auto nullability
4069         = Expr->IgnoreImplicit()->getType()->getNullability(S.Context)) {
4070     if (*nullability == NullabilityKind::NonNull)
4071       return false;
4072   }
4073 
4074   // As a special case, transparent unions initialized with zero are
4075   // considered null for the purposes of the nonnull attribute.
4076   if (const RecordType *UT = Expr->getType()->getAsUnionType()) {
4077     if (UT->getDecl()->hasAttr<TransparentUnionAttr>())
4078       if (const CompoundLiteralExpr *CLE =
4079           dyn_cast<CompoundLiteralExpr>(Expr))
4080         if (const InitListExpr *ILE =
4081             dyn_cast<InitListExpr>(CLE->getInitializer()))
4082           Expr = ILE->getInit(0);
4083   }
4084 
4085   bool Result;
4086   return (!Expr->isValueDependent() &&
4087           Expr->EvaluateAsBooleanCondition(Result, S.Context) &&
4088           !Result);
4089 }
4090 
4091 static void CheckNonNullArgument(Sema &S,
4092                                  const Expr *ArgExpr,
4093                                  SourceLocation CallSiteLoc) {
4094   if (CheckNonNullExpr(S, ArgExpr))
4095     S.DiagRuntimeBehavior(CallSiteLoc, ArgExpr,
4096                           S.PDiag(diag::warn_null_arg)
4097                               << ArgExpr->getSourceRange());
4098 }
4099 
4100 bool Sema::GetFormatNSStringIdx(const FormatAttr *Format, unsigned &Idx) {
4101   FormatStringInfo FSI;
4102   if ((GetFormatStringType(Format) == FST_NSString) &&
4103       getFormatStringInfo(Format, false, &FSI)) {
4104     Idx = FSI.FormatIdx;
4105     return true;
4106   }
4107   return false;
4108 }
4109 
4110 /// Diagnose use of %s directive in an NSString which is being passed
4111 /// as formatting string to formatting method.
4112 static void
4113 DiagnoseCStringFormatDirectiveInCFAPI(Sema &S,
4114                                         const NamedDecl *FDecl,
4115                                         Expr **Args,
4116                                         unsigned NumArgs) {
4117   unsigned Idx = 0;
4118   bool Format = false;
4119   ObjCStringFormatFamily SFFamily = FDecl->getObjCFStringFormattingFamily();
4120   if (SFFamily == ObjCStringFormatFamily::SFF_CFString) {
4121     Idx = 2;
4122     Format = true;
4123   }
4124   else
4125     for (const auto *I : FDecl->specific_attrs<FormatAttr>()) {
4126       if (S.GetFormatNSStringIdx(I, Idx)) {
4127         Format = true;
4128         break;
4129       }
4130     }
4131   if (!Format || NumArgs <= Idx)
4132     return;
4133   const Expr *FormatExpr = Args[Idx];
4134   if (const CStyleCastExpr *CSCE = dyn_cast<CStyleCastExpr>(FormatExpr))
4135     FormatExpr = CSCE->getSubExpr();
4136   const StringLiteral *FormatString;
4137   if (const ObjCStringLiteral *OSL =
4138       dyn_cast<ObjCStringLiteral>(FormatExpr->IgnoreParenImpCasts()))
4139     FormatString = OSL->getString();
4140   else
4141     FormatString = dyn_cast<StringLiteral>(FormatExpr->IgnoreParenImpCasts());
4142   if (!FormatString)
4143     return;
4144   if (S.FormatStringHasSArg(FormatString)) {
4145     S.Diag(FormatExpr->getExprLoc(), diag::warn_objc_cdirective_format_string)
4146       << "%s" << 1 << 1;
4147     S.Diag(FDecl->getLocation(), diag::note_entity_declared_at)
4148       << FDecl->getDeclName();
4149   }
4150 }
4151 
4152 /// Determine whether the given type has a non-null nullability annotation.
4153 static bool isNonNullType(ASTContext &ctx, QualType type) {
4154   if (auto nullability = type->getNullability(ctx))
4155     return *nullability == NullabilityKind::NonNull;
4156 
4157   return false;
4158 }
4159 
4160 static void CheckNonNullArguments(Sema &S,
4161                                   const NamedDecl *FDecl,
4162                                   const FunctionProtoType *Proto,
4163                                   ArrayRef<const Expr *> Args,
4164                                   SourceLocation CallSiteLoc) {
4165   assert((FDecl || Proto) && "Need a function declaration or prototype");
4166 
4167   // Already checked by by constant evaluator.
4168   if (S.isConstantEvaluated())
4169     return;
4170   // Check the attributes attached to the method/function itself.
4171   llvm::SmallBitVector NonNullArgs;
4172   if (FDecl) {
4173     // Handle the nonnull attribute on the function/method declaration itself.
4174     for (const auto *NonNull : FDecl->specific_attrs<NonNullAttr>()) {
4175       if (!NonNull->args_size()) {
4176         // Easy case: all pointer arguments are nonnull.
4177         for (const auto *Arg : Args)
4178           if (S.isValidPointerAttrType(Arg->getType()))
4179             CheckNonNullArgument(S, Arg, CallSiteLoc);
4180         return;
4181       }
4182 
4183       for (const ParamIdx &Idx : NonNull->args()) {
4184         unsigned IdxAST = Idx.getASTIndex();
4185         if (IdxAST >= Args.size())
4186           continue;
4187         if (NonNullArgs.empty())
4188           NonNullArgs.resize(Args.size());
4189         NonNullArgs.set(IdxAST);
4190       }
4191     }
4192   }
4193 
4194   if (FDecl && (isa<FunctionDecl>(FDecl) || isa<ObjCMethodDecl>(FDecl))) {
4195     // Handle the nonnull attribute on the parameters of the
4196     // function/method.
4197     ArrayRef<ParmVarDecl*> parms;
4198     if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FDecl))
4199       parms = FD->parameters();
4200     else
4201       parms = cast<ObjCMethodDecl>(FDecl)->parameters();
4202 
4203     unsigned ParamIndex = 0;
4204     for (ArrayRef<ParmVarDecl*>::iterator I = parms.begin(), E = parms.end();
4205          I != E; ++I, ++ParamIndex) {
4206       const ParmVarDecl *PVD = *I;
4207       if (PVD->hasAttr<NonNullAttr>() ||
4208           isNonNullType(S.Context, PVD->getType())) {
4209         if (NonNullArgs.empty())
4210           NonNullArgs.resize(Args.size());
4211 
4212         NonNullArgs.set(ParamIndex);
4213       }
4214     }
4215   } else {
4216     // If we have a non-function, non-method declaration but no
4217     // function prototype, try to dig out the function prototype.
4218     if (!Proto) {
4219       if (const ValueDecl *VD = dyn_cast<ValueDecl>(FDecl)) {
4220         QualType type = VD->getType().getNonReferenceType();
4221         if (auto pointerType = type->getAs<PointerType>())
4222           type = pointerType->getPointeeType();
4223         else if (auto blockType = type->getAs<BlockPointerType>())
4224           type = blockType->getPointeeType();
4225         // FIXME: data member pointers?
4226 
4227         // Dig out the function prototype, if there is one.
4228         Proto = type->getAs<FunctionProtoType>();
4229       }
4230     }
4231 
4232     // Fill in non-null argument information from the nullability
4233     // information on the parameter types (if we have them).
4234     if (Proto) {
4235       unsigned Index = 0;
4236       for (auto paramType : Proto->getParamTypes()) {
4237         if (isNonNullType(S.Context, paramType)) {
4238           if (NonNullArgs.empty())
4239             NonNullArgs.resize(Args.size());
4240 
4241           NonNullArgs.set(Index);
4242         }
4243 
4244         ++Index;
4245       }
4246     }
4247   }
4248 
4249   // Check for non-null arguments.
4250   for (unsigned ArgIndex = 0, ArgIndexEnd = NonNullArgs.size();
4251        ArgIndex != ArgIndexEnd; ++ArgIndex) {
4252     if (NonNullArgs[ArgIndex])
4253       CheckNonNullArgument(S, Args[ArgIndex], CallSiteLoc);
4254   }
4255 }
4256 
4257 /// Handles the checks for format strings, non-POD arguments to vararg
4258 /// functions, NULL arguments passed to non-NULL parameters, and diagnose_if
4259 /// attributes.
4260 void Sema::checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto,
4261                      const Expr *ThisArg, ArrayRef<const Expr *> Args,
4262                      bool IsMemberFunction, SourceLocation Loc,
4263                      SourceRange Range, VariadicCallType CallType) {
4264   // FIXME: We should check as much as we can in the template definition.
4265   if (CurContext->isDependentContext())
4266     return;
4267 
4268   // Printf and scanf checking.
4269   llvm::SmallBitVector CheckedVarArgs;
4270   if (FDecl) {
4271     for (const auto *I : FDecl->specific_attrs<FormatAttr>()) {
4272       // Only create vector if there are format attributes.
4273       CheckedVarArgs.resize(Args.size());
4274 
4275       CheckFormatArguments(I, Args, IsMemberFunction, CallType, Loc, Range,
4276                            CheckedVarArgs);
4277     }
4278   }
4279 
4280   // Refuse POD arguments that weren't caught by the format string
4281   // checks above.
4282   auto *FD = dyn_cast_or_null<FunctionDecl>(FDecl);
4283   if (CallType != VariadicDoesNotApply &&
4284       (!FD || FD->getBuiltinID() != Builtin::BI__noop)) {
4285     unsigned NumParams = Proto ? Proto->getNumParams()
4286                        : FDecl && isa<FunctionDecl>(FDecl)
4287                            ? cast<FunctionDecl>(FDecl)->getNumParams()
4288                        : FDecl && isa<ObjCMethodDecl>(FDecl)
4289                            ? cast<ObjCMethodDecl>(FDecl)->param_size()
4290                        : 0;
4291 
4292     for (unsigned ArgIdx = NumParams; ArgIdx < Args.size(); ++ArgIdx) {
4293       // Args[ArgIdx] can be null in malformed code.
4294       if (const Expr *Arg = Args[ArgIdx]) {
4295         if (CheckedVarArgs.empty() || !CheckedVarArgs[ArgIdx])
4296           checkVariadicArgument(Arg, CallType);
4297       }
4298     }
4299   }
4300 
4301   if (FDecl || Proto) {
4302     CheckNonNullArguments(*this, FDecl, Proto, Args, Loc);
4303 
4304     // Type safety checking.
4305     if (FDecl) {
4306       for (const auto *I : FDecl->specific_attrs<ArgumentWithTypeTagAttr>())
4307         CheckArgumentWithTypeTag(I, Args, Loc);
4308     }
4309   }
4310 
4311   if (FDecl && FDecl->hasAttr<AllocAlignAttr>()) {
4312     auto *AA = FDecl->getAttr<AllocAlignAttr>();
4313     const Expr *Arg = Args[AA->getParamIndex().getASTIndex()];
4314     if (!Arg->isValueDependent()) {
4315       Expr::EvalResult Align;
4316       if (Arg->EvaluateAsInt(Align, Context)) {
4317         const llvm::APSInt &I = Align.Val.getInt();
4318         if (!I.isPowerOf2())
4319           Diag(Arg->getExprLoc(), diag::warn_alignment_not_power_of_two)
4320               << Arg->getSourceRange();
4321 
4322         if (I > Sema::MaximumAlignment)
4323           Diag(Arg->getExprLoc(), diag::warn_assume_aligned_too_great)
4324               << Arg->getSourceRange() << Sema::MaximumAlignment;
4325       }
4326     }
4327   }
4328 
4329   if (FD)
4330     diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
4331 }
4332 
4333 /// CheckConstructorCall - Check a constructor call for correctness and safety
4334 /// properties not enforced by the C type system.
4335 void Sema::CheckConstructorCall(FunctionDecl *FDecl,
4336                                 ArrayRef<const Expr *> Args,
4337                                 const FunctionProtoType *Proto,
4338                                 SourceLocation Loc) {
4339   VariadicCallType CallType =
4340     Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply;
4341   checkCall(FDecl, Proto, /*ThisArg=*/nullptr, Args, /*IsMemberFunction=*/true,
4342             Loc, SourceRange(), CallType);
4343 }
4344 
4345 /// CheckFunctionCall - Check a direct function call for various correctness
4346 /// and safety properties not strictly enforced by the C type system.
4347 bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall,
4348                              const FunctionProtoType *Proto) {
4349   bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) &&
4350                               isa<CXXMethodDecl>(FDecl);
4351   bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) ||
4352                           IsMemberOperatorCall;
4353   VariadicCallType CallType = getVariadicCallType(FDecl, Proto,
4354                                                   TheCall->getCallee());
4355   Expr** Args = TheCall->getArgs();
4356   unsigned NumArgs = TheCall->getNumArgs();
4357 
4358   Expr *ImplicitThis = nullptr;
4359   if (IsMemberOperatorCall) {
4360     // If this is a call to a member operator, hide the first argument
4361     // from checkCall.
4362     // FIXME: Our choice of AST representation here is less than ideal.
4363     ImplicitThis = Args[0];
4364     ++Args;
4365     --NumArgs;
4366   } else if (IsMemberFunction)
4367     ImplicitThis =
4368         cast<CXXMemberCallExpr>(TheCall)->getImplicitObjectArgument();
4369 
4370   checkCall(FDecl, Proto, ImplicitThis, llvm::makeArrayRef(Args, NumArgs),
4371             IsMemberFunction, TheCall->getRParenLoc(),
4372             TheCall->getCallee()->getSourceRange(), CallType);
4373 
4374   IdentifierInfo *FnInfo = FDecl->getIdentifier();
4375   // None of the checks below are needed for functions that don't have
4376   // simple names (e.g., C++ conversion functions).
4377   if (!FnInfo)
4378     return false;
4379 
4380   CheckAbsoluteValueFunction(TheCall, FDecl);
4381   CheckMaxUnsignedZero(TheCall, FDecl);
4382 
4383   if (getLangOpts().ObjC)
4384     DiagnoseCStringFormatDirectiveInCFAPI(*this, FDecl, Args, NumArgs);
4385 
4386   unsigned CMId = FDecl->getMemoryFunctionKind();
4387   if (CMId == 0)
4388     return false;
4389 
4390   // Handle memory setting and copying functions.
4391   if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat)
4392     CheckStrlcpycatArguments(TheCall, FnInfo);
4393   else if (CMId == Builtin::BIstrncat)
4394     CheckStrncatArguments(TheCall, FnInfo);
4395   else
4396     CheckMemaccessArguments(TheCall, CMId, FnInfo);
4397 
4398   return false;
4399 }
4400 
4401 bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac,
4402                                ArrayRef<const Expr *> Args) {
4403   VariadicCallType CallType =
4404       Method->isVariadic() ? VariadicMethod : VariadicDoesNotApply;
4405 
4406   checkCall(Method, nullptr, /*ThisArg=*/nullptr, Args,
4407             /*IsMemberFunction=*/false, lbrac, Method->getSourceRange(),
4408             CallType);
4409 
4410   return false;
4411 }
4412 
4413 bool Sema::CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall,
4414                             const FunctionProtoType *Proto) {
4415   QualType Ty;
4416   if (const auto *V = dyn_cast<VarDecl>(NDecl))
4417     Ty = V->getType().getNonReferenceType();
4418   else if (const auto *F = dyn_cast<FieldDecl>(NDecl))
4419     Ty = F->getType().getNonReferenceType();
4420   else
4421     return false;
4422 
4423   if (!Ty->isBlockPointerType() && !Ty->isFunctionPointerType() &&
4424       !Ty->isFunctionProtoType())
4425     return false;
4426 
4427   VariadicCallType CallType;
4428   if (!Proto || !Proto->isVariadic()) {
4429     CallType = VariadicDoesNotApply;
4430   } else if (Ty->isBlockPointerType()) {
4431     CallType = VariadicBlock;
4432   } else { // Ty->isFunctionPointerType()
4433     CallType = VariadicFunction;
4434   }
4435 
4436   checkCall(NDecl, Proto, /*ThisArg=*/nullptr,
4437             llvm::makeArrayRef(TheCall->getArgs(), TheCall->getNumArgs()),
4438             /*IsMemberFunction=*/false, TheCall->getRParenLoc(),
4439             TheCall->getCallee()->getSourceRange(), CallType);
4440 
4441   return false;
4442 }
4443 
4444 /// Checks function calls when a FunctionDecl or a NamedDecl is not available,
4445 /// such as function pointers returned from functions.
4446 bool Sema::CheckOtherCall(CallExpr *TheCall, const FunctionProtoType *Proto) {
4447   VariadicCallType CallType = getVariadicCallType(/*FDecl=*/nullptr, Proto,
4448                                                   TheCall->getCallee());
4449   checkCall(/*FDecl=*/nullptr, Proto, /*ThisArg=*/nullptr,
4450             llvm::makeArrayRef(TheCall->getArgs(), TheCall->getNumArgs()),
4451             /*IsMemberFunction=*/false, TheCall->getRParenLoc(),
4452             TheCall->getCallee()->getSourceRange(), CallType);
4453 
4454   return false;
4455 }
4456 
4457 static bool isValidOrderingForOp(int64_t Ordering, AtomicExpr::AtomicOp Op) {
4458   if (!llvm::isValidAtomicOrderingCABI(Ordering))
4459     return false;
4460 
4461   auto OrderingCABI = (llvm::AtomicOrderingCABI)Ordering;
4462   switch (Op) {
4463   case AtomicExpr::AO__c11_atomic_init:
4464   case AtomicExpr::AO__opencl_atomic_init:
4465     llvm_unreachable("There is no ordering argument for an init");
4466 
4467   case AtomicExpr::AO__c11_atomic_load:
4468   case AtomicExpr::AO__opencl_atomic_load:
4469   case AtomicExpr::AO__atomic_load_n:
4470   case AtomicExpr::AO__atomic_load:
4471     return OrderingCABI != llvm::AtomicOrderingCABI::release &&
4472            OrderingCABI != llvm::AtomicOrderingCABI::acq_rel;
4473 
4474   case AtomicExpr::AO__c11_atomic_store:
4475   case AtomicExpr::AO__opencl_atomic_store:
4476   case AtomicExpr::AO__atomic_store:
4477   case AtomicExpr::AO__atomic_store_n:
4478     return OrderingCABI != llvm::AtomicOrderingCABI::consume &&
4479            OrderingCABI != llvm::AtomicOrderingCABI::acquire &&
4480            OrderingCABI != llvm::AtomicOrderingCABI::acq_rel;
4481 
4482   default:
4483     return true;
4484   }
4485 }
4486 
4487 ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult,
4488                                          AtomicExpr::AtomicOp Op) {
4489   CallExpr *TheCall = cast<CallExpr>(TheCallResult.get());
4490   DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
4491   MultiExprArg Args{TheCall->getArgs(), TheCall->getNumArgs()};
4492   return BuildAtomicExpr({TheCall->getBeginLoc(), TheCall->getEndLoc()},
4493                          DRE->getSourceRange(), TheCall->getRParenLoc(), Args,
4494                          Op);
4495 }
4496 
4497 ExprResult Sema::BuildAtomicExpr(SourceRange CallRange, SourceRange ExprRange,
4498                                  SourceLocation RParenLoc, MultiExprArg Args,
4499                                  AtomicExpr::AtomicOp Op,
4500                                  AtomicArgumentOrder ArgOrder) {
4501   // All the non-OpenCL operations take one of the following forms.
4502   // The OpenCL operations take the __c11 forms with one extra argument for
4503   // synchronization scope.
4504   enum {
4505     // C    __c11_atomic_init(A *, C)
4506     Init,
4507 
4508     // C    __c11_atomic_load(A *, int)
4509     Load,
4510 
4511     // void __atomic_load(A *, CP, int)
4512     LoadCopy,
4513 
4514     // void __atomic_store(A *, CP, int)
4515     Copy,
4516 
4517     // C    __c11_atomic_add(A *, M, int)
4518     Arithmetic,
4519 
4520     // C    __atomic_exchange_n(A *, CP, int)
4521     Xchg,
4522 
4523     // void __atomic_exchange(A *, C *, CP, int)
4524     GNUXchg,
4525 
4526     // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int)
4527     C11CmpXchg,
4528 
4529     // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int)
4530     GNUCmpXchg
4531   } Form = Init;
4532 
4533   const unsigned NumForm = GNUCmpXchg + 1;
4534   const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 3, 4, 5, 6 };
4535   const unsigned NumVals[] = { 1, 0, 1, 1, 1, 1, 2, 2, 3 };
4536   // where:
4537   //   C is an appropriate type,
4538   //   A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins,
4539   //   CP is C for __c11 builtins and GNU _n builtins and is C * otherwise,
4540   //   M is C if C is an integer, and ptrdiff_t if C is a pointer, and
4541   //   the int parameters are for orderings.
4542 
4543   static_assert(sizeof(NumArgs)/sizeof(NumArgs[0]) == NumForm
4544       && sizeof(NumVals)/sizeof(NumVals[0]) == NumForm,
4545       "need to update code for modified forms");
4546   static_assert(AtomicExpr::AO__c11_atomic_init == 0 &&
4547                     AtomicExpr::AO__c11_atomic_fetch_min + 1 ==
4548                         AtomicExpr::AO__atomic_load,
4549                 "need to update code for modified C11 atomics");
4550   bool IsOpenCL = Op >= AtomicExpr::AO__opencl_atomic_init &&
4551                   Op <= AtomicExpr::AO__opencl_atomic_fetch_max;
4552   bool IsC11 = (Op >= AtomicExpr::AO__c11_atomic_init &&
4553                Op <= AtomicExpr::AO__c11_atomic_fetch_min) ||
4554                IsOpenCL;
4555   bool IsN = Op == AtomicExpr::AO__atomic_load_n ||
4556              Op == AtomicExpr::AO__atomic_store_n ||
4557              Op == AtomicExpr::AO__atomic_exchange_n ||
4558              Op == AtomicExpr::AO__atomic_compare_exchange_n;
4559   bool IsAddSub = false;
4560 
4561   switch (Op) {
4562   case AtomicExpr::AO__c11_atomic_init:
4563   case AtomicExpr::AO__opencl_atomic_init:
4564     Form = Init;
4565     break;
4566 
4567   case AtomicExpr::AO__c11_atomic_load:
4568   case AtomicExpr::AO__opencl_atomic_load:
4569   case AtomicExpr::AO__atomic_load_n:
4570     Form = Load;
4571     break;
4572 
4573   case AtomicExpr::AO__atomic_load:
4574     Form = LoadCopy;
4575     break;
4576 
4577   case AtomicExpr::AO__c11_atomic_store:
4578   case AtomicExpr::AO__opencl_atomic_store:
4579   case AtomicExpr::AO__atomic_store:
4580   case AtomicExpr::AO__atomic_store_n:
4581     Form = Copy;
4582     break;
4583 
4584   case AtomicExpr::AO__c11_atomic_fetch_add:
4585   case AtomicExpr::AO__c11_atomic_fetch_sub:
4586   case AtomicExpr::AO__opencl_atomic_fetch_add:
4587   case AtomicExpr::AO__opencl_atomic_fetch_sub:
4588   case AtomicExpr::AO__atomic_fetch_add:
4589   case AtomicExpr::AO__atomic_fetch_sub:
4590   case AtomicExpr::AO__atomic_add_fetch:
4591   case AtomicExpr::AO__atomic_sub_fetch:
4592     IsAddSub = true;
4593     LLVM_FALLTHROUGH;
4594   case AtomicExpr::AO__c11_atomic_fetch_and:
4595   case AtomicExpr::AO__c11_atomic_fetch_or:
4596   case AtomicExpr::AO__c11_atomic_fetch_xor:
4597   case AtomicExpr::AO__opencl_atomic_fetch_and:
4598   case AtomicExpr::AO__opencl_atomic_fetch_or:
4599   case AtomicExpr::AO__opencl_atomic_fetch_xor:
4600   case AtomicExpr::AO__atomic_fetch_and:
4601   case AtomicExpr::AO__atomic_fetch_or:
4602   case AtomicExpr::AO__atomic_fetch_xor:
4603   case AtomicExpr::AO__atomic_fetch_nand:
4604   case AtomicExpr::AO__atomic_and_fetch:
4605   case AtomicExpr::AO__atomic_or_fetch:
4606   case AtomicExpr::AO__atomic_xor_fetch:
4607   case AtomicExpr::AO__atomic_nand_fetch:
4608   case AtomicExpr::AO__c11_atomic_fetch_min:
4609   case AtomicExpr::AO__c11_atomic_fetch_max:
4610   case AtomicExpr::AO__opencl_atomic_fetch_min:
4611   case AtomicExpr::AO__opencl_atomic_fetch_max:
4612   case AtomicExpr::AO__atomic_min_fetch:
4613   case AtomicExpr::AO__atomic_max_fetch:
4614   case AtomicExpr::AO__atomic_fetch_min:
4615   case AtomicExpr::AO__atomic_fetch_max:
4616     Form = Arithmetic;
4617     break;
4618 
4619   case AtomicExpr::AO__c11_atomic_exchange:
4620   case AtomicExpr::AO__opencl_atomic_exchange:
4621   case AtomicExpr::AO__atomic_exchange_n:
4622     Form = Xchg;
4623     break;
4624 
4625   case AtomicExpr::AO__atomic_exchange:
4626     Form = GNUXchg;
4627     break;
4628 
4629   case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
4630   case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
4631   case AtomicExpr::AO__opencl_atomic_compare_exchange_strong:
4632   case AtomicExpr::AO__opencl_atomic_compare_exchange_weak:
4633     Form = C11CmpXchg;
4634     break;
4635 
4636   case AtomicExpr::AO__atomic_compare_exchange:
4637   case AtomicExpr::AO__atomic_compare_exchange_n:
4638     Form = GNUCmpXchg;
4639     break;
4640   }
4641 
4642   unsigned AdjustedNumArgs = NumArgs[Form];
4643   if (IsOpenCL && Op != AtomicExpr::AO__opencl_atomic_init)
4644     ++AdjustedNumArgs;
4645   // Check we have the right number of arguments.
4646   if (Args.size() < AdjustedNumArgs) {
4647     Diag(CallRange.getEnd(), diag::err_typecheck_call_too_few_args)
4648         << 0 << AdjustedNumArgs << static_cast<unsigned>(Args.size())
4649         << ExprRange;
4650     return ExprError();
4651   } else if (Args.size() > AdjustedNumArgs) {
4652     Diag(Args[AdjustedNumArgs]->getBeginLoc(),
4653          diag::err_typecheck_call_too_many_args)
4654         << 0 << AdjustedNumArgs << static_cast<unsigned>(Args.size())
4655         << ExprRange;
4656     return ExprError();
4657   }
4658 
4659   // Inspect the first argument of the atomic operation.
4660   Expr *Ptr = Args[0];
4661   ExprResult ConvertedPtr = DefaultFunctionArrayLvalueConversion(Ptr);
4662   if (ConvertedPtr.isInvalid())
4663     return ExprError();
4664 
4665   Ptr = ConvertedPtr.get();
4666   const PointerType *pointerType = Ptr->getType()->getAs<PointerType>();
4667   if (!pointerType) {
4668     Diag(ExprRange.getBegin(), diag::err_atomic_builtin_must_be_pointer)
4669         << Ptr->getType() << Ptr->getSourceRange();
4670     return ExprError();
4671   }
4672 
4673   // For a __c11 builtin, this should be a pointer to an _Atomic type.
4674   QualType AtomTy = pointerType->getPointeeType(); // 'A'
4675   QualType ValType = AtomTy; // 'C'
4676   if (IsC11) {
4677     if (!AtomTy->isAtomicType()) {
4678       Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_atomic)
4679           << Ptr->getType() << Ptr->getSourceRange();
4680       return ExprError();
4681     }
4682     if ((Form != Load && Form != LoadCopy && AtomTy.isConstQualified()) ||
4683         AtomTy.getAddressSpace() == LangAS::opencl_constant) {
4684       Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_const_atomic)
4685           << (AtomTy.isConstQualified() ? 0 : 1) << Ptr->getType()
4686           << Ptr->getSourceRange();
4687       return ExprError();
4688     }
4689     ValType = AtomTy->castAs<AtomicType>()->getValueType();
4690   } else if (Form != Load && Form != LoadCopy) {
4691     if (ValType.isConstQualified()) {
4692       Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_const_pointer)
4693           << Ptr->getType() << Ptr->getSourceRange();
4694       return ExprError();
4695     }
4696   }
4697 
4698   // For an arithmetic operation, the implied arithmetic must be well-formed.
4699   if (Form == Arithmetic) {
4700     // gcc does not enforce these rules for GNU atomics, but we do so for sanity.
4701     if (IsAddSub && !ValType->isIntegerType()
4702         && !ValType->isPointerType()) {
4703       Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_atomic_int_or_ptr)
4704           << IsC11 << Ptr->getType() << Ptr->getSourceRange();
4705       return ExprError();
4706     }
4707     if (!IsAddSub && !ValType->isIntegerType()) {
4708       Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_atomic_int)
4709           << IsC11 << Ptr->getType() << Ptr->getSourceRange();
4710       return ExprError();
4711     }
4712     if (IsC11 && ValType->isPointerType() &&
4713         RequireCompleteType(Ptr->getBeginLoc(), ValType->getPointeeType(),
4714                             diag::err_incomplete_type)) {
4715       return ExprError();
4716     }
4717   } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) {
4718     // For __atomic_*_n operations, the value type must be a scalar integral or
4719     // pointer type which is 1, 2, 4, 8 or 16 bytes in length.
4720     Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_atomic_int_or_ptr)
4721         << IsC11 << Ptr->getType() << Ptr->getSourceRange();
4722     return ExprError();
4723   }
4724 
4725   if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context) &&
4726       !AtomTy->isScalarType()) {
4727     // For GNU atomics, require a trivially-copyable type. This is not part of
4728     // the GNU atomics specification, but we enforce it for sanity.
4729     Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_trivial_copy)
4730         << Ptr->getType() << Ptr->getSourceRange();
4731     return ExprError();
4732   }
4733 
4734   switch (ValType.getObjCLifetime()) {
4735   case Qualifiers::OCL_None:
4736   case Qualifiers::OCL_ExplicitNone:
4737     // okay
4738     break;
4739 
4740   case Qualifiers::OCL_Weak:
4741   case Qualifiers::OCL_Strong:
4742   case Qualifiers::OCL_Autoreleasing:
4743     // FIXME: Can this happen? By this point, ValType should be known
4744     // to be trivially copyable.
4745     Diag(ExprRange.getBegin(), diag::err_arc_atomic_ownership)
4746         << ValType << Ptr->getSourceRange();
4747     return ExprError();
4748   }
4749 
4750   // All atomic operations have an overload which takes a pointer to a volatile
4751   // 'A'.  We shouldn't let the volatile-ness of the pointee-type inject itself
4752   // into the result or the other operands. Similarly atomic_load takes a
4753   // pointer to a const 'A'.
4754   ValType.removeLocalVolatile();
4755   ValType.removeLocalConst();
4756   QualType ResultType = ValType;
4757   if (Form == Copy || Form == LoadCopy || Form == GNUXchg ||
4758       Form == Init)
4759     ResultType = Context.VoidTy;
4760   else if (Form == C11CmpXchg || Form == GNUCmpXchg)
4761     ResultType = Context.BoolTy;
4762 
4763   // The type of a parameter passed 'by value'. In the GNU atomics, such
4764   // arguments are actually passed as pointers.
4765   QualType ByValType = ValType; // 'CP'
4766   bool IsPassedByAddress = false;
4767   if (!IsC11 && !IsN) {
4768     ByValType = Ptr->getType();
4769     IsPassedByAddress = true;
4770   }
4771 
4772   SmallVector<Expr *, 5> APIOrderedArgs;
4773   if (ArgOrder == Sema::AtomicArgumentOrder::AST) {
4774     APIOrderedArgs.push_back(Args[0]);
4775     switch (Form) {
4776     case Init:
4777     case Load:
4778       APIOrderedArgs.push_back(Args[1]); // Val1/Order
4779       break;
4780     case LoadCopy:
4781     case Copy:
4782     case Arithmetic:
4783     case Xchg:
4784       APIOrderedArgs.push_back(Args[2]); // Val1
4785       APIOrderedArgs.push_back(Args[1]); // Order
4786       break;
4787     case GNUXchg:
4788       APIOrderedArgs.push_back(Args[2]); // Val1
4789       APIOrderedArgs.push_back(Args[3]); // Val2
4790       APIOrderedArgs.push_back(Args[1]); // Order
4791       break;
4792     case C11CmpXchg:
4793       APIOrderedArgs.push_back(Args[2]); // Val1
4794       APIOrderedArgs.push_back(Args[4]); // Val2
4795       APIOrderedArgs.push_back(Args[1]); // Order
4796       APIOrderedArgs.push_back(Args[3]); // OrderFail
4797       break;
4798     case GNUCmpXchg:
4799       APIOrderedArgs.push_back(Args[2]); // Val1
4800       APIOrderedArgs.push_back(Args[4]); // Val2
4801       APIOrderedArgs.push_back(Args[5]); // Weak
4802       APIOrderedArgs.push_back(Args[1]); // Order
4803       APIOrderedArgs.push_back(Args[3]); // OrderFail
4804       break;
4805     }
4806   } else
4807     APIOrderedArgs.append(Args.begin(), Args.end());
4808 
4809   // The first argument's non-CV pointer type is used to deduce the type of
4810   // subsequent arguments, except for:
4811   //  - weak flag (always converted to bool)
4812   //  - memory order (always converted to int)
4813   //  - scope  (always converted to int)
4814   for (unsigned i = 0; i != APIOrderedArgs.size(); ++i) {
4815     QualType Ty;
4816     if (i < NumVals[Form] + 1) {
4817       switch (i) {
4818       case 0:
4819         // The first argument is always a pointer. It has a fixed type.
4820         // It is always dereferenced, a nullptr is undefined.
4821         CheckNonNullArgument(*this, APIOrderedArgs[i], ExprRange.getBegin());
4822         // Nothing else to do: we already know all we want about this pointer.
4823         continue;
4824       case 1:
4825         // The second argument is the non-atomic operand. For arithmetic, this
4826         // is always passed by value, and for a compare_exchange it is always
4827         // passed by address. For the rest, GNU uses by-address and C11 uses
4828         // by-value.
4829         assert(Form != Load);
4830         if (Form == Init || (Form == Arithmetic && ValType->isIntegerType()))
4831           Ty = ValType;
4832         else if (Form == Copy || Form == Xchg) {
4833           if (IsPassedByAddress) {
4834             // The value pointer is always dereferenced, a nullptr is undefined.
4835             CheckNonNullArgument(*this, APIOrderedArgs[i],
4836                                  ExprRange.getBegin());
4837           }
4838           Ty = ByValType;
4839         } else if (Form == Arithmetic)
4840           Ty = Context.getPointerDiffType();
4841         else {
4842           Expr *ValArg = APIOrderedArgs[i];
4843           // The value pointer is always dereferenced, a nullptr is undefined.
4844           CheckNonNullArgument(*this, ValArg, ExprRange.getBegin());
4845           LangAS AS = LangAS::Default;
4846           // Keep address space of non-atomic pointer type.
4847           if (const PointerType *PtrTy =
4848                   ValArg->getType()->getAs<PointerType>()) {
4849             AS = PtrTy->getPointeeType().getAddressSpace();
4850           }
4851           Ty = Context.getPointerType(
4852               Context.getAddrSpaceQualType(ValType.getUnqualifiedType(), AS));
4853         }
4854         break;
4855       case 2:
4856         // The third argument to compare_exchange / GNU exchange is the desired
4857         // value, either by-value (for the C11 and *_n variant) or as a pointer.
4858         if (IsPassedByAddress)
4859           CheckNonNullArgument(*this, APIOrderedArgs[i], ExprRange.getBegin());
4860         Ty = ByValType;
4861         break;
4862       case 3:
4863         // The fourth argument to GNU compare_exchange is a 'weak' flag.
4864         Ty = Context.BoolTy;
4865         break;
4866       }
4867     } else {
4868       // The order(s) and scope are always converted to int.
4869       Ty = Context.IntTy;
4870     }
4871 
4872     InitializedEntity Entity =
4873         InitializedEntity::InitializeParameter(Context, Ty, false);
4874     ExprResult Arg = APIOrderedArgs[i];
4875     Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
4876     if (Arg.isInvalid())
4877       return true;
4878     APIOrderedArgs[i] = Arg.get();
4879   }
4880 
4881   // Permute the arguments into a 'consistent' order.
4882   SmallVector<Expr*, 5> SubExprs;
4883   SubExprs.push_back(Ptr);
4884   switch (Form) {
4885   case Init:
4886     // Note, AtomicExpr::getVal1() has a special case for this atomic.
4887     SubExprs.push_back(APIOrderedArgs[1]); // Val1
4888     break;
4889   case Load:
4890     SubExprs.push_back(APIOrderedArgs[1]); // Order
4891     break;
4892   case LoadCopy:
4893   case Copy:
4894   case Arithmetic:
4895   case Xchg:
4896     SubExprs.push_back(APIOrderedArgs[2]); // Order
4897     SubExprs.push_back(APIOrderedArgs[1]); // Val1
4898     break;
4899   case GNUXchg:
4900     // Note, AtomicExpr::getVal2() has a special case for this atomic.
4901     SubExprs.push_back(APIOrderedArgs[3]); // Order
4902     SubExprs.push_back(APIOrderedArgs[1]); // Val1
4903     SubExprs.push_back(APIOrderedArgs[2]); // Val2
4904     break;
4905   case C11CmpXchg:
4906     SubExprs.push_back(APIOrderedArgs[3]); // Order
4907     SubExprs.push_back(APIOrderedArgs[1]); // Val1
4908     SubExprs.push_back(APIOrderedArgs[4]); // OrderFail
4909     SubExprs.push_back(APIOrderedArgs[2]); // Val2
4910     break;
4911   case GNUCmpXchg:
4912     SubExprs.push_back(APIOrderedArgs[4]); // Order
4913     SubExprs.push_back(APIOrderedArgs[1]); // Val1
4914     SubExprs.push_back(APIOrderedArgs[5]); // OrderFail
4915     SubExprs.push_back(APIOrderedArgs[2]); // Val2
4916     SubExprs.push_back(APIOrderedArgs[3]); // Weak
4917     break;
4918   }
4919 
4920   if (SubExprs.size() >= 2 && Form != Init) {
4921     llvm::APSInt Result(32);
4922     if (SubExprs[1]->isIntegerConstantExpr(Result, Context) &&
4923         !isValidOrderingForOp(Result.getSExtValue(), Op))
4924       Diag(SubExprs[1]->getBeginLoc(),
4925            diag::warn_atomic_op_has_invalid_memory_order)
4926           << SubExprs[1]->getSourceRange();
4927   }
4928 
4929   if (auto ScopeModel = AtomicExpr::getScopeModel(Op)) {
4930     auto *Scope = Args[Args.size() - 1];
4931     llvm::APSInt Result(32);
4932     if (Scope->isIntegerConstantExpr(Result, Context) &&
4933         !ScopeModel->isValid(Result.getZExtValue())) {
4934       Diag(Scope->getBeginLoc(), diag::err_atomic_op_has_invalid_synch_scope)
4935           << Scope->getSourceRange();
4936     }
4937     SubExprs.push_back(Scope);
4938   }
4939 
4940   AtomicExpr *AE = new (Context)
4941       AtomicExpr(ExprRange.getBegin(), SubExprs, ResultType, Op, RParenLoc);
4942 
4943   if ((Op == AtomicExpr::AO__c11_atomic_load ||
4944        Op == AtomicExpr::AO__c11_atomic_store ||
4945        Op == AtomicExpr::AO__opencl_atomic_load ||
4946        Op == AtomicExpr::AO__opencl_atomic_store ) &&
4947       Context.AtomicUsesUnsupportedLibcall(AE))
4948     Diag(AE->getBeginLoc(), diag::err_atomic_load_store_uses_lib)
4949         << ((Op == AtomicExpr::AO__c11_atomic_load ||
4950              Op == AtomicExpr::AO__opencl_atomic_load)
4951                 ? 0
4952                 : 1);
4953 
4954   if (ValType->isExtIntType()) {
4955     Diag(Ptr->getExprLoc(), diag::err_atomic_builtin_ext_int_prohibit);
4956     return ExprError();
4957   }
4958 
4959   return AE;
4960 }
4961 
4962 /// checkBuiltinArgument - Given a call to a builtin function, perform
4963 /// normal type-checking on the given argument, updating the call in
4964 /// place.  This is useful when a builtin function requires custom
4965 /// type-checking for some of its arguments but not necessarily all of
4966 /// them.
4967 ///
4968 /// Returns true on error.
4969 static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) {
4970   FunctionDecl *Fn = E->getDirectCallee();
4971   assert(Fn && "builtin call without direct callee!");
4972 
4973   ParmVarDecl *Param = Fn->getParamDecl(ArgIndex);
4974   InitializedEntity Entity =
4975     InitializedEntity::InitializeParameter(S.Context, Param);
4976 
4977   ExprResult Arg = E->getArg(0);
4978   Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg);
4979   if (Arg.isInvalid())
4980     return true;
4981 
4982   E->setArg(ArgIndex, Arg.get());
4983   return false;
4984 }
4985 
4986 /// We have a call to a function like __sync_fetch_and_add, which is an
4987 /// overloaded function based on the pointer type of its first argument.
4988 /// The main BuildCallExpr routines have already promoted the types of
4989 /// arguments because all of these calls are prototyped as void(...).
4990 ///
4991 /// This function goes through and does final semantic checking for these
4992 /// builtins, as well as generating any warnings.
4993 ExprResult
4994 Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) {
4995   CallExpr *TheCall = static_cast<CallExpr *>(TheCallResult.get());
4996   Expr *Callee = TheCall->getCallee();
4997   DeclRefExpr *DRE = cast<DeclRefExpr>(Callee->IgnoreParenCasts());
4998   FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
4999 
5000   // Ensure that we have at least one argument to do type inference from.
5001   if (TheCall->getNumArgs() < 1) {
5002     Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least)
5003         << 0 << 1 << TheCall->getNumArgs() << Callee->getSourceRange();
5004     return ExprError();
5005   }
5006 
5007   // Inspect the first argument of the atomic builtin.  This should always be
5008   // a pointer type, whose element is an integral scalar or pointer type.
5009   // Because it is a pointer type, we don't have to worry about any implicit
5010   // casts here.
5011   // FIXME: We don't allow floating point scalars as input.
5012   Expr *FirstArg = TheCall->getArg(0);
5013   ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg);
5014   if (FirstArgResult.isInvalid())
5015     return ExprError();
5016   FirstArg = FirstArgResult.get();
5017   TheCall->setArg(0, FirstArg);
5018 
5019   const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>();
5020   if (!pointerType) {
5021     Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer)
5022         << FirstArg->getType() << FirstArg->getSourceRange();
5023     return ExprError();
5024   }
5025 
5026   QualType ValType = pointerType->getPointeeType();
5027   if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
5028       !ValType->isBlockPointerType()) {
5029     Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer_intptr)
5030         << FirstArg->getType() << FirstArg->getSourceRange();
5031     return ExprError();
5032   }
5033 
5034   if (ValType.isConstQualified()) {
5035     Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_cannot_be_const)
5036         << FirstArg->getType() << FirstArg->getSourceRange();
5037     return ExprError();
5038   }
5039 
5040   switch (ValType.getObjCLifetime()) {
5041   case Qualifiers::OCL_None:
5042   case Qualifiers::OCL_ExplicitNone:
5043     // okay
5044     break;
5045 
5046   case Qualifiers::OCL_Weak:
5047   case Qualifiers::OCL_Strong:
5048   case Qualifiers::OCL_Autoreleasing:
5049     Diag(DRE->getBeginLoc(), diag::err_arc_atomic_ownership)
5050         << ValType << FirstArg->getSourceRange();
5051     return ExprError();
5052   }
5053 
5054   // Strip any qualifiers off ValType.
5055   ValType = ValType.getUnqualifiedType();
5056 
5057   // The majority of builtins return a value, but a few have special return
5058   // types, so allow them to override appropriately below.
5059   QualType ResultType = ValType;
5060 
5061   // We need to figure out which concrete builtin this maps onto.  For example,
5062   // __sync_fetch_and_add with a 2 byte object turns into
5063   // __sync_fetch_and_add_2.
5064 #define BUILTIN_ROW(x) \
5065   { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \
5066     Builtin::BI##x##_8, Builtin::BI##x##_16 }
5067 
5068   static const unsigned BuiltinIndices[][5] = {
5069     BUILTIN_ROW(__sync_fetch_and_add),
5070     BUILTIN_ROW(__sync_fetch_and_sub),
5071     BUILTIN_ROW(__sync_fetch_and_or),
5072     BUILTIN_ROW(__sync_fetch_and_and),
5073     BUILTIN_ROW(__sync_fetch_and_xor),
5074     BUILTIN_ROW(__sync_fetch_and_nand),
5075 
5076     BUILTIN_ROW(__sync_add_and_fetch),
5077     BUILTIN_ROW(__sync_sub_and_fetch),
5078     BUILTIN_ROW(__sync_and_and_fetch),
5079     BUILTIN_ROW(__sync_or_and_fetch),
5080     BUILTIN_ROW(__sync_xor_and_fetch),
5081     BUILTIN_ROW(__sync_nand_and_fetch),
5082 
5083     BUILTIN_ROW(__sync_val_compare_and_swap),
5084     BUILTIN_ROW(__sync_bool_compare_and_swap),
5085     BUILTIN_ROW(__sync_lock_test_and_set),
5086     BUILTIN_ROW(__sync_lock_release),
5087     BUILTIN_ROW(__sync_swap)
5088   };
5089 #undef BUILTIN_ROW
5090 
5091   // Determine the index of the size.
5092   unsigned SizeIndex;
5093   switch (Context.getTypeSizeInChars(ValType).getQuantity()) {
5094   case 1: SizeIndex = 0; break;
5095   case 2: SizeIndex = 1; break;
5096   case 4: SizeIndex = 2; break;
5097   case 8: SizeIndex = 3; break;
5098   case 16: SizeIndex = 4; break;
5099   default:
5100     Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_pointer_size)
5101         << FirstArg->getType() << FirstArg->getSourceRange();
5102     return ExprError();
5103   }
5104 
5105   // Each of these builtins has one pointer argument, followed by some number of
5106   // values (0, 1 or 2) followed by a potentially empty varags list of stuff
5107   // that we ignore.  Find out which row of BuiltinIndices to read from as well
5108   // as the number of fixed args.
5109   unsigned BuiltinID = FDecl->getBuiltinID();
5110   unsigned BuiltinIndex, NumFixed = 1;
5111   bool WarnAboutSemanticsChange = false;
5112   switch (BuiltinID) {
5113   default: llvm_unreachable("Unknown overloaded atomic builtin!");
5114   case Builtin::BI__sync_fetch_and_add:
5115   case Builtin::BI__sync_fetch_and_add_1:
5116   case Builtin::BI__sync_fetch_and_add_2:
5117   case Builtin::BI__sync_fetch_and_add_4:
5118   case Builtin::BI__sync_fetch_and_add_8:
5119   case Builtin::BI__sync_fetch_and_add_16:
5120     BuiltinIndex = 0;
5121     break;
5122 
5123   case Builtin::BI__sync_fetch_and_sub:
5124   case Builtin::BI__sync_fetch_and_sub_1:
5125   case Builtin::BI__sync_fetch_and_sub_2:
5126   case Builtin::BI__sync_fetch_and_sub_4:
5127   case Builtin::BI__sync_fetch_and_sub_8:
5128   case Builtin::BI__sync_fetch_and_sub_16:
5129     BuiltinIndex = 1;
5130     break;
5131 
5132   case Builtin::BI__sync_fetch_and_or:
5133   case Builtin::BI__sync_fetch_and_or_1:
5134   case Builtin::BI__sync_fetch_and_or_2:
5135   case Builtin::BI__sync_fetch_and_or_4:
5136   case Builtin::BI__sync_fetch_and_or_8:
5137   case Builtin::BI__sync_fetch_and_or_16:
5138     BuiltinIndex = 2;
5139     break;
5140 
5141   case Builtin::BI__sync_fetch_and_and:
5142   case Builtin::BI__sync_fetch_and_and_1:
5143   case Builtin::BI__sync_fetch_and_and_2:
5144   case Builtin::BI__sync_fetch_and_and_4:
5145   case Builtin::BI__sync_fetch_and_and_8:
5146   case Builtin::BI__sync_fetch_and_and_16:
5147     BuiltinIndex = 3;
5148     break;
5149 
5150   case Builtin::BI__sync_fetch_and_xor:
5151   case Builtin::BI__sync_fetch_and_xor_1:
5152   case Builtin::BI__sync_fetch_and_xor_2:
5153   case Builtin::BI__sync_fetch_and_xor_4:
5154   case Builtin::BI__sync_fetch_and_xor_8:
5155   case Builtin::BI__sync_fetch_and_xor_16:
5156     BuiltinIndex = 4;
5157     break;
5158 
5159   case Builtin::BI__sync_fetch_and_nand:
5160   case Builtin::BI__sync_fetch_and_nand_1:
5161   case Builtin::BI__sync_fetch_and_nand_2:
5162   case Builtin::BI__sync_fetch_and_nand_4:
5163   case Builtin::BI__sync_fetch_and_nand_8:
5164   case Builtin::BI__sync_fetch_and_nand_16:
5165     BuiltinIndex = 5;
5166     WarnAboutSemanticsChange = true;
5167     break;
5168 
5169   case Builtin::BI__sync_add_and_fetch:
5170   case Builtin::BI__sync_add_and_fetch_1:
5171   case Builtin::BI__sync_add_and_fetch_2:
5172   case Builtin::BI__sync_add_and_fetch_4:
5173   case Builtin::BI__sync_add_and_fetch_8:
5174   case Builtin::BI__sync_add_and_fetch_16:
5175     BuiltinIndex = 6;
5176     break;
5177 
5178   case Builtin::BI__sync_sub_and_fetch:
5179   case Builtin::BI__sync_sub_and_fetch_1:
5180   case Builtin::BI__sync_sub_and_fetch_2:
5181   case Builtin::BI__sync_sub_and_fetch_4:
5182   case Builtin::BI__sync_sub_and_fetch_8:
5183   case Builtin::BI__sync_sub_and_fetch_16:
5184     BuiltinIndex = 7;
5185     break;
5186 
5187   case Builtin::BI__sync_and_and_fetch:
5188   case Builtin::BI__sync_and_and_fetch_1:
5189   case Builtin::BI__sync_and_and_fetch_2:
5190   case Builtin::BI__sync_and_and_fetch_4:
5191   case Builtin::BI__sync_and_and_fetch_8:
5192   case Builtin::BI__sync_and_and_fetch_16:
5193     BuiltinIndex = 8;
5194     break;
5195 
5196   case Builtin::BI__sync_or_and_fetch:
5197   case Builtin::BI__sync_or_and_fetch_1:
5198   case Builtin::BI__sync_or_and_fetch_2:
5199   case Builtin::BI__sync_or_and_fetch_4:
5200   case Builtin::BI__sync_or_and_fetch_8:
5201   case Builtin::BI__sync_or_and_fetch_16:
5202     BuiltinIndex = 9;
5203     break;
5204 
5205   case Builtin::BI__sync_xor_and_fetch:
5206   case Builtin::BI__sync_xor_and_fetch_1:
5207   case Builtin::BI__sync_xor_and_fetch_2:
5208   case Builtin::BI__sync_xor_and_fetch_4:
5209   case Builtin::BI__sync_xor_and_fetch_8:
5210   case Builtin::BI__sync_xor_and_fetch_16:
5211     BuiltinIndex = 10;
5212     break;
5213 
5214   case Builtin::BI__sync_nand_and_fetch:
5215   case Builtin::BI__sync_nand_and_fetch_1:
5216   case Builtin::BI__sync_nand_and_fetch_2:
5217   case Builtin::BI__sync_nand_and_fetch_4:
5218   case Builtin::BI__sync_nand_and_fetch_8:
5219   case Builtin::BI__sync_nand_and_fetch_16:
5220     BuiltinIndex = 11;
5221     WarnAboutSemanticsChange = true;
5222     break;
5223 
5224   case Builtin::BI__sync_val_compare_and_swap:
5225   case Builtin::BI__sync_val_compare_and_swap_1:
5226   case Builtin::BI__sync_val_compare_and_swap_2:
5227   case Builtin::BI__sync_val_compare_and_swap_4:
5228   case Builtin::BI__sync_val_compare_and_swap_8:
5229   case Builtin::BI__sync_val_compare_and_swap_16:
5230     BuiltinIndex = 12;
5231     NumFixed = 2;
5232     break;
5233 
5234   case Builtin::BI__sync_bool_compare_and_swap:
5235   case Builtin::BI__sync_bool_compare_and_swap_1:
5236   case Builtin::BI__sync_bool_compare_and_swap_2:
5237   case Builtin::BI__sync_bool_compare_and_swap_4:
5238   case Builtin::BI__sync_bool_compare_and_swap_8:
5239   case Builtin::BI__sync_bool_compare_and_swap_16:
5240     BuiltinIndex = 13;
5241     NumFixed = 2;
5242     ResultType = Context.BoolTy;
5243     break;
5244 
5245   case Builtin::BI__sync_lock_test_and_set:
5246   case Builtin::BI__sync_lock_test_and_set_1:
5247   case Builtin::BI__sync_lock_test_and_set_2:
5248   case Builtin::BI__sync_lock_test_and_set_4:
5249   case Builtin::BI__sync_lock_test_and_set_8:
5250   case Builtin::BI__sync_lock_test_and_set_16:
5251     BuiltinIndex = 14;
5252     break;
5253 
5254   case Builtin::BI__sync_lock_release:
5255   case Builtin::BI__sync_lock_release_1:
5256   case Builtin::BI__sync_lock_release_2:
5257   case Builtin::BI__sync_lock_release_4:
5258   case Builtin::BI__sync_lock_release_8:
5259   case Builtin::BI__sync_lock_release_16:
5260     BuiltinIndex = 15;
5261     NumFixed = 0;
5262     ResultType = Context.VoidTy;
5263     break;
5264 
5265   case Builtin::BI__sync_swap:
5266   case Builtin::BI__sync_swap_1:
5267   case Builtin::BI__sync_swap_2:
5268   case Builtin::BI__sync_swap_4:
5269   case Builtin::BI__sync_swap_8:
5270   case Builtin::BI__sync_swap_16:
5271     BuiltinIndex = 16;
5272     break;
5273   }
5274 
5275   // Now that we know how many fixed arguments we expect, first check that we
5276   // have at least that many.
5277   if (TheCall->getNumArgs() < 1+NumFixed) {
5278     Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least)
5279         << 0 << 1 + NumFixed << TheCall->getNumArgs()
5280         << Callee->getSourceRange();
5281     return ExprError();
5282   }
5283 
5284   Diag(TheCall->getEndLoc(), diag::warn_atomic_implicit_seq_cst)
5285       << Callee->getSourceRange();
5286 
5287   if (WarnAboutSemanticsChange) {
5288     Diag(TheCall->getEndLoc(), diag::warn_sync_fetch_and_nand_semantics_change)
5289         << Callee->getSourceRange();
5290   }
5291 
5292   // Get the decl for the concrete builtin from this, we can tell what the
5293   // concrete integer type we should convert to is.
5294   unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex];
5295   const char *NewBuiltinName = Context.BuiltinInfo.getName(NewBuiltinID);
5296   FunctionDecl *NewBuiltinDecl;
5297   if (NewBuiltinID == BuiltinID)
5298     NewBuiltinDecl = FDecl;
5299   else {
5300     // Perform builtin lookup to avoid redeclaring it.
5301     DeclarationName DN(&Context.Idents.get(NewBuiltinName));
5302     LookupResult Res(*this, DN, DRE->getBeginLoc(), LookupOrdinaryName);
5303     LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true);
5304     assert(Res.getFoundDecl());
5305     NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl());
5306     if (!NewBuiltinDecl)
5307       return ExprError();
5308   }
5309 
5310   // The first argument --- the pointer --- has a fixed type; we
5311   // deduce the types of the rest of the arguments accordingly.  Walk
5312   // the remaining arguments, converting them to the deduced value type.
5313   for (unsigned i = 0; i != NumFixed; ++i) {
5314     ExprResult Arg = TheCall->getArg(i+1);
5315 
5316     // GCC does an implicit conversion to the pointer or integer ValType.  This
5317     // can fail in some cases (1i -> int**), check for this error case now.
5318     // Initialize the argument.
5319     InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
5320                                                    ValType, /*consume*/ false);
5321     Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
5322     if (Arg.isInvalid())
5323       return ExprError();
5324 
5325     // Okay, we have something that *can* be converted to the right type.  Check
5326     // to see if there is a potentially weird extension going on here.  This can
5327     // happen when you do an atomic operation on something like an char* and
5328     // pass in 42.  The 42 gets converted to char.  This is even more strange
5329     // for things like 45.123 -> char, etc.
5330     // FIXME: Do this check.
5331     TheCall->setArg(i+1, Arg.get());
5332   }
5333 
5334   // Create a new DeclRefExpr to refer to the new decl.
5335   DeclRefExpr *NewDRE = DeclRefExpr::Create(
5336       Context, DRE->getQualifierLoc(), SourceLocation(), NewBuiltinDecl,
5337       /*enclosing*/ false, DRE->getLocation(), Context.BuiltinFnTy,
5338       DRE->getValueKind(), nullptr, nullptr, DRE->isNonOdrUse());
5339 
5340   // Set the callee in the CallExpr.
5341   // FIXME: This loses syntactic information.
5342   QualType CalleePtrTy = Context.getPointerType(NewBuiltinDecl->getType());
5343   ExprResult PromotedCall = ImpCastExprToType(NewDRE, CalleePtrTy,
5344                                               CK_BuiltinFnToFnPtr);
5345   TheCall->setCallee(PromotedCall.get());
5346 
5347   // Change the result type of the call to match the original value type. This
5348   // is arbitrary, but the codegen for these builtins ins design to handle it
5349   // gracefully.
5350   TheCall->setType(ResultType);
5351 
5352   // Prohibit use of _ExtInt with atomic builtins.
5353   // The arguments would have already been converted to the first argument's
5354   // type, so only need to check the first argument.
5355   const auto *ExtIntValType = ValType->getAs<ExtIntType>();
5356   if (ExtIntValType && !llvm::isPowerOf2_64(ExtIntValType->getNumBits())) {
5357     Diag(FirstArg->getExprLoc(), diag::err_atomic_builtin_ext_int_size);
5358     return ExprError();
5359   }
5360 
5361   return TheCallResult;
5362 }
5363 
5364 /// SemaBuiltinNontemporalOverloaded - We have a call to
5365 /// __builtin_nontemporal_store or __builtin_nontemporal_load, which is an
5366 /// overloaded function based on the pointer type of its last argument.
5367 ///
5368 /// This function goes through and does final semantic checking for these
5369 /// builtins.
5370 ExprResult Sema::SemaBuiltinNontemporalOverloaded(ExprResult TheCallResult) {
5371   CallExpr *TheCall = (CallExpr *)TheCallResult.get();
5372   DeclRefExpr *DRE =
5373       cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
5374   FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
5375   unsigned BuiltinID = FDecl->getBuiltinID();
5376   assert((BuiltinID == Builtin::BI__builtin_nontemporal_store ||
5377           BuiltinID == Builtin::BI__builtin_nontemporal_load) &&
5378          "Unexpected nontemporal load/store builtin!");
5379   bool isStore = BuiltinID == Builtin::BI__builtin_nontemporal_store;
5380   unsigned numArgs = isStore ? 2 : 1;
5381 
5382   // Ensure that we have the proper number of arguments.
5383   if (checkArgCount(*this, TheCall, numArgs))
5384     return ExprError();
5385 
5386   // Inspect the last argument of the nontemporal builtin.  This should always
5387   // be a pointer type, from which we imply the type of the memory access.
5388   // Because it is a pointer type, we don't have to worry about any implicit
5389   // casts here.
5390   Expr *PointerArg = TheCall->getArg(numArgs - 1);
5391   ExprResult PointerArgResult =
5392       DefaultFunctionArrayLvalueConversion(PointerArg);
5393 
5394   if (PointerArgResult.isInvalid())
5395     return ExprError();
5396   PointerArg = PointerArgResult.get();
5397   TheCall->setArg(numArgs - 1, PointerArg);
5398 
5399   const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>();
5400   if (!pointerType) {
5401     Diag(DRE->getBeginLoc(), diag::err_nontemporal_builtin_must_be_pointer)
5402         << PointerArg->getType() << PointerArg->getSourceRange();
5403     return ExprError();
5404   }
5405 
5406   QualType ValType = pointerType->getPointeeType();
5407 
5408   // Strip any qualifiers off ValType.
5409   ValType = ValType.getUnqualifiedType();
5410   if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
5411       !ValType->isBlockPointerType() && !ValType->isFloatingType() &&
5412       !ValType->isVectorType()) {
5413     Diag(DRE->getBeginLoc(),
5414          diag::err_nontemporal_builtin_must_be_pointer_intfltptr_or_vector)
5415         << PointerArg->getType() << PointerArg->getSourceRange();
5416     return ExprError();
5417   }
5418 
5419   if (!isStore) {
5420     TheCall->setType(ValType);
5421     return TheCallResult;
5422   }
5423 
5424   ExprResult ValArg = TheCall->getArg(0);
5425   InitializedEntity Entity = InitializedEntity::InitializeParameter(
5426       Context, ValType, /*consume*/ false);
5427   ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg);
5428   if (ValArg.isInvalid())
5429     return ExprError();
5430 
5431   TheCall->setArg(0, ValArg.get());
5432   TheCall->setType(Context.VoidTy);
5433   return TheCallResult;
5434 }
5435 
5436 /// CheckObjCString - Checks that the argument to the builtin
5437 /// CFString constructor is correct
5438 /// Note: It might also make sense to do the UTF-16 conversion here (would
5439 /// simplify the backend).
5440 bool Sema::CheckObjCString(Expr *Arg) {
5441   Arg = Arg->IgnoreParenCasts();
5442   StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
5443 
5444   if (!Literal || !Literal->isAscii()) {
5445     Diag(Arg->getBeginLoc(), diag::err_cfstring_literal_not_string_constant)
5446         << Arg->getSourceRange();
5447     return true;
5448   }
5449 
5450   if (Literal->containsNonAsciiOrNull()) {
5451     StringRef String = Literal->getString();
5452     unsigned NumBytes = String.size();
5453     SmallVector<llvm::UTF16, 128> ToBuf(NumBytes);
5454     const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)String.data();
5455     llvm::UTF16 *ToPtr = &ToBuf[0];
5456 
5457     llvm::ConversionResult Result =
5458         llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, &ToPtr,
5459                                  ToPtr + NumBytes, llvm::strictConversion);
5460     // Check for conversion failure.
5461     if (Result != llvm::conversionOK)
5462       Diag(Arg->getBeginLoc(), diag::warn_cfstring_truncated)
5463           << Arg->getSourceRange();
5464   }
5465   return false;
5466 }
5467 
5468 /// CheckObjCString - Checks that the format string argument to the os_log()
5469 /// and os_trace() functions is correct, and converts it to const char *.
5470 ExprResult Sema::CheckOSLogFormatStringArg(Expr *Arg) {
5471   Arg = Arg->IgnoreParenCasts();
5472   auto *Literal = dyn_cast<StringLiteral>(Arg);
5473   if (!Literal) {
5474     if (auto *ObjcLiteral = dyn_cast<ObjCStringLiteral>(Arg)) {
5475       Literal = ObjcLiteral->getString();
5476     }
5477   }
5478 
5479   if (!Literal || (!Literal->isAscii() && !Literal->isUTF8())) {
5480     return ExprError(
5481         Diag(Arg->getBeginLoc(), diag::err_os_log_format_not_string_constant)
5482         << Arg->getSourceRange());
5483   }
5484 
5485   ExprResult Result(Literal);
5486   QualType ResultTy = Context.getPointerType(Context.CharTy.withConst());
5487   InitializedEntity Entity =
5488       InitializedEntity::InitializeParameter(Context, ResultTy, false);
5489   Result = PerformCopyInitialization(Entity, SourceLocation(), Result);
5490   return Result;
5491 }
5492 
5493 /// Check that the user is calling the appropriate va_start builtin for the
5494 /// target and calling convention.
5495 static bool checkVAStartABI(Sema &S, unsigned BuiltinID, Expr *Fn) {
5496   const llvm::Triple &TT = S.Context.getTargetInfo().getTriple();
5497   bool IsX64 = TT.getArch() == llvm::Triple::x86_64;
5498   bool IsAArch64 = (TT.getArch() == llvm::Triple::aarch64 ||
5499                     TT.getArch() == llvm::Triple::aarch64_32);
5500   bool IsWindows = TT.isOSWindows();
5501   bool IsMSVAStart = BuiltinID == Builtin::BI__builtin_ms_va_start;
5502   if (IsX64 || IsAArch64) {
5503     CallingConv CC = CC_C;
5504     if (const FunctionDecl *FD = S.getCurFunctionDecl())
5505       CC = FD->getType()->castAs<FunctionType>()->getCallConv();
5506     if (IsMSVAStart) {
5507       // Don't allow this in System V ABI functions.
5508       if (CC == CC_X86_64SysV || (!IsWindows && CC != CC_Win64))
5509         return S.Diag(Fn->getBeginLoc(),
5510                       diag::err_ms_va_start_used_in_sysv_function);
5511     } else {
5512       // On x86-64/AArch64 Unix, don't allow this in Win64 ABI functions.
5513       // On x64 Windows, don't allow this in System V ABI functions.
5514       // (Yes, that means there's no corresponding way to support variadic
5515       // System V ABI functions on Windows.)
5516       if ((IsWindows && CC == CC_X86_64SysV) ||
5517           (!IsWindows && CC == CC_Win64))
5518         return S.Diag(Fn->getBeginLoc(),
5519                       diag::err_va_start_used_in_wrong_abi_function)
5520                << !IsWindows;
5521     }
5522     return false;
5523   }
5524 
5525   if (IsMSVAStart)
5526     return S.Diag(Fn->getBeginLoc(), diag::err_builtin_x64_aarch64_only);
5527   return false;
5528 }
5529 
5530 static bool checkVAStartIsInVariadicFunction(Sema &S, Expr *Fn,
5531                                              ParmVarDecl **LastParam = nullptr) {
5532   // Determine whether the current function, block, or obj-c method is variadic
5533   // and get its parameter list.
5534   bool IsVariadic = false;
5535   ArrayRef<ParmVarDecl *> Params;
5536   DeclContext *Caller = S.CurContext;
5537   if (auto *Block = dyn_cast<BlockDecl>(Caller)) {
5538     IsVariadic = Block->isVariadic();
5539     Params = Block->parameters();
5540   } else if (auto *FD = dyn_cast<FunctionDecl>(Caller)) {
5541     IsVariadic = FD->isVariadic();
5542     Params = FD->parameters();
5543   } else if (auto *MD = dyn_cast<ObjCMethodDecl>(Caller)) {
5544     IsVariadic = MD->isVariadic();
5545     // FIXME: This isn't correct for methods (results in bogus warning).
5546     Params = MD->parameters();
5547   } else if (isa<CapturedDecl>(Caller)) {
5548     // We don't support va_start in a CapturedDecl.
5549     S.Diag(Fn->getBeginLoc(), diag::err_va_start_captured_stmt);
5550     return true;
5551   } else {
5552     // This must be some other declcontext that parses exprs.
5553     S.Diag(Fn->getBeginLoc(), diag::err_va_start_outside_function);
5554     return true;
5555   }
5556 
5557   if (!IsVariadic) {
5558     S.Diag(Fn->getBeginLoc(), diag::err_va_start_fixed_function);
5559     return true;
5560   }
5561 
5562   if (LastParam)
5563     *LastParam = Params.empty() ? nullptr : Params.back();
5564 
5565   return false;
5566 }
5567 
5568 /// Check the arguments to '__builtin_va_start' or '__builtin_ms_va_start'
5569 /// for validity.  Emit an error and return true on failure; return false
5570 /// on success.
5571 bool Sema::SemaBuiltinVAStart(unsigned BuiltinID, CallExpr *TheCall) {
5572   Expr *Fn = TheCall->getCallee();
5573 
5574   if (checkVAStartABI(*this, BuiltinID, Fn))
5575     return true;
5576 
5577   if (TheCall->getNumArgs() > 2) {
5578     Diag(TheCall->getArg(2)->getBeginLoc(),
5579          diag::err_typecheck_call_too_many_args)
5580         << 0 /*function call*/ << 2 << TheCall->getNumArgs()
5581         << Fn->getSourceRange()
5582         << SourceRange(TheCall->getArg(2)->getBeginLoc(),
5583                        (*(TheCall->arg_end() - 1))->getEndLoc());
5584     return true;
5585   }
5586 
5587   if (TheCall->getNumArgs() < 2) {
5588     return Diag(TheCall->getEndLoc(),
5589                 diag::err_typecheck_call_too_few_args_at_least)
5590            << 0 /*function call*/ << 2 << TheCall->getNumArgs();
5591   }
5592 
5593   // Type-check the first argument normally.
5594   if (checkBuiltinArgument(*this, TheCall, 0))
5595     return true;
5596 
5597   // Check that the current function is variadic, and get its last parameter.
5598   ParmVarDecl *LastParam;
5599   if (checkVAStartIsInVariadicFunction(*this, Fn, &LastParam))
5600     return true;
5601 
5602   // Verify that the second argument to the builtin is the last argument of the
5603   // current function or method.
5604   bool SecondArgIsLastNamedArgument = false;
5605   const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts();
5606 
5607   // These are valid if SecondArgIsLastNamedArgument is false after the next
5608   // block.
5609   QualType Type;
5610   SourceLocation ParamLoc;
5611   bool IsCRegister = false;
5612 
5613   if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) {
5614     if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
5615       SecondArgIsLastNamedArgument = PV == LastParam;
5616 
5617       Type = PV->getType();
5618       ParamLoc = PV->getLocation();
5619       IsCRegister =
5620           PV->getStorageClass() == SC_Register && !getLangOpts().CPlusPlus;
5621     }
5622   }
5623 
5624   if (!SecondArgIsLastNamedArgument)
5625     Diag(TheCall->getArg(1)->getBeginLoc(),
5626          diag::warn_second_arg_of_va_start_not_last_named_param);
5627   else if (IsCRegister || Type->isReferenceType() ||
5628            Type->isSpecificBuiltinType(BuiltinType::Float) || [=] {
5629              // Promotable integers are UB, but enumerations need a bit of
5630              // extra checking to see what their promotable type actually is.
5631              if (!Type->isPromotableIntegerType())
5632                return false;
5633              if (!Type->isEnumeralType())
5634                return true;
5635              const EnumDecl *ED = Type->castAs<EnumType>()->getDecl();
5636              return !(ED &&
5637                       Context.typesAreCompatible(ED->getPromotionType(), Type));
5638            }()) {
5639     unsigned Reason = 0;
5640     if (Type->isReferenceType())  Reason = 1;
5641     else if (IsCRegister)         Reason = 2;
5642     Diag(Arg->getBeginLoc(), diag::warn_va_start_type_is_undefined) << Reason;
5643     Diag(ParamLoc, diag::note_parameter_type) << Type;
5644   }
5645 
5646   TheCall->setType(Context.VoidTy);
5647   return false;
5648 }
5649 
5650 bool Sema::SemaBuiltinVAStartARMMicrosoft(CallExpr *Call) {
5651   // void __va_start(va_list *ap, const char *named_addr, size_t slot_size,
5652   //                 const char *named_addr);
5653 
5654   Expr *Func = Call->getCallee();
5655 
5656   if (Call->getNumArgs() < 3)
5657     return Diag(Call->getEndLoc(),
5658                 diag::err_typecheck_call_too_few_args_at_least)
5659            << 0 /*function call*/ << 3 << Call->getNumArgs();
5660 
5661   // Type-check the first argument normally.
5662   if (checkBuiltinArgument(*this, Call, 0))
5663     return true;
5664 
5665   // Check that the current function is variadic.
5666   if (checkVAStartIsInVariadicFunction(*this, Func))
5667     return true;
5668 
5669   // __va_start on Windows does not validate the parameter qualifiers
5670 
5671   const Expr *Arg1 = Call->getArg(1)->IgnoreParens();
5672   const Type *Arg1Ty = Arg1->getType().getCanonicalType().getTypePtr();
5673 
5674   const Expr *Arg2 = Call->getArg(2)->IgnoreParens();
5675   const Type *Arg2Ty = Arg2->getType().getCanonicalType().getTypePtr();
5676 
5677   const QualType &ConstCharPtrTy =
5678       Context.getPointerType(Context.CharTy.withConst());
5679   if (!Arg1Ty->isPointerType() ||
5680       Arg1Ty->getPointeeType().withoutLocalFastQualifiers() != Context.CharTy)
5681     Diag(Arg1->getBeginLoc(), diag::err_typecheck_convert_incompatible)
5682         << Arg1->getType() << ConstCharPtrTy << 1 /* different class */
5683         << 0                                      /* qualifier difference */
5684         << 3                                      /* parameter mismatch */
5685         << 2 << Arg1->getType() << ConstCharPtrTy;
5686 
5687   const QualType SizeTy = Context.getSizeType();
5688   if (Arg2Ty->getCanonicalTypeInternal().withoutLocalFastQualifiers() != SizeTy)
5689     Diag(Arg2->getBeginLoc(), diag::err_typecheck_convert_incompatible)
5690         << Arg2->getType() << SizeTy << 1 /* different class */
5691         << 0                              /* qualifier difference */
5692         << 3                              /* parameter mismatch */
5693         << 3 << Arg2->getType() << SizeTy;
5694 
5695   return false;
5696 }
5697 
5698 /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and
5699 /// friends.  This is declared to take (...), so we have to check everything.
5700 bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) {
5701   if (TheCall->getNumArgs() < 2)
5702     return Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args)
5703            << 0 << 2 << TheCall->getNumArgs() /*function call*/;
5704   if (TheCall->getNumArgs() > 2)
5705     return Diag(TheCall->getArg(2)->getBeginLoc(),
5706                 diag::err_typecheck_call_too_many_args)
5707            << 0 /*function call*/ << 2 << TheCall->getNumArgs()
5708            << SourceRange(TheCall->getArg(2)->getBeginLoc(),
5709                           (*(TheCall->arg_end() - 1))->getEndLoc());
5710 
5711   ExprResult OrigArg0 = TheCall->getArg(0);
5712   ExprResult OrigArg1 = TheCall->getArg(1);
5713 
5714   // Do standard promotions between the two arguments, returning their common
5715   // type.
5716   QualType Res = UsualArithmeticConversions(
5717       OrigArg0, OrigArg1, TheCall->getExprLoc(), ACK_Comparison);
5718   if (OrigArg0.isInvalid() || OrigArg1.isInvalid())
5719     return true;
5720 
5721   // Make sure any conversions are pushed back into the call; this is
5722   // type safe since unordered compare builtins are declared as "_Bool
5723   // foo(...)".
5724   TheCall->setArg(0, OrigArg0.get());
5725   TheCall->setArg(1, OrigArg1.get());
5726 
5727   if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent())
5728     return false;
5729 
5730   // If the common type isn't a real floating type, then the arguments were
5731   // invalid for this operation.
5732   if (Res.isNull() || !Res->isRealFloatingType())
5733     return Diag(OrigArg0.get()->getBeginLoc(),
5734                 diag::err_typecheck_call_invalid_ordered_compare)
5735            << OrigArg0.get()->getType() << OrigArg1.get()->getType()
5736            << SourceRange(OrigArg0.get()->getBeginLoc(),
5737                           OrigArg1.get()->getEndLoc());
5738 
5739   return false;
5740 }
5741 
5742 /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like
5743 /// __builtin_isnan and friends.  This is declared to take (...), so we have
5744 /// to check everything. We expect the last argument to be a floating point
5745 /// value.
5746 bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) {
5747   if (TheCall->getNumArgs() < NumArgs)
5748     return Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args)
5749            << 0 << NumArgs << TheCall->getNumArgs() /*function call*/;
5750   if (TheCall->getNumArgs() > NumArgs)
5751     return Diag(TheCall->getArg(NumArgs)->getBeginLoc(),
5752                 diag::err_typecheck_call_too_many_args)
5753            << 0 /*function call*/ << NumArgs << TheCall->getNumArgs()
5754            << SourceRange(TheCall->getArg(NumArgs)->getBeginLoc(),
5755                           (*(TheCall->arg_end() - 1))->getEndLoc());
5756 
5757   // __builtin_fpclassify is the only case where NumArgs != 1, so we can count
5758   // on all preceding parameters just being int.  Try all of those.
5759   for (unsigned i = 0; i < NumArgs - 1; ++i) {
5760     Expr *Arg = TheCall->getArg(i);
5761 
5762     if (Arg->isTypeDependent())
5763       return false;
5764 
5765     ExprResult Res = PerformImplicitConversion(Arg, Context.IntTy, AA_Passing);
5766 
5767     if (Res.isInvalid())
5768       return true;
5769     TheCall->setArg(i, Res.get());
5770   }
5771 
5772   Expr *OrigArg = TheCall->getArg(NumArgs-1);
5773 
5774   if (OrigArg->isTypeDependent())
5775     return false;
5776 
5777   // Usual Unary Conversions will convert half to float, which we want for
5778   // machines that use fp16 conversion intrinsics. Else, we wnat to leave the
5779   // type how it is, but do normal L->Rvalue conversions.
5780   if (Context.getTargetInfo().useFP16ConversionIntrinsics())
5781     OrigArg = UsualUnaryConversions(OrigArg).get();
5782   else
5783     OrigArg = DefaultFunctionArrayLvalueConversion(OrigArg).get();
5784   TheCall->setArg(NumArgs - 1, OrigArg);
5785 
5786   // This operation requires a non-_Complex floating-point number.
5787   if (!OrigArg->getType()->isRealFloatingType())
5788     return Diag(OrigArg->getBeginLoc(),
5789                 diag::err_typecheck_call_invalid_unary_fp)
5790            << OrigArg->getType() << OrigArg->getSourceRange();
5791 
5792   return false;
5793 }
5794 
5795 // Customized Sema Checking for VSX builtins that have the following signature:
5796 // vector [...] builtinName(vector [...], vector [...], const int);
5797 // Which takes the same type of vectors (any legal vector type) for the first
5798 // two arguments and takes compile time constant for the third argument.
5799 // Example builtins are :
5800 // vector double vec_xxpermdi(vector double, vector double, int);
5801 // vector short vec_xxsldwi(vector short, vector short, int);
5802 bool Sema::SemaBuiltinVSX(CallExpr *TheCall) {
5803   unsigned ExpectedNumArgs = 3;
5804   if (TheCall->getNumArgs() < ExpectedNumArgs)
5805     return Diag(TheCall->getEndLoc(),
5806                 diag::err_typecheck_call_too_few_args_at_least)
5807            << 0 /*function call*/ << ExpectedNumArgs << TheCall->getNumArgs()
5808            << TheCall->getSourceRange();
5809 
5810   if (TheCall->getNumArgs() > ExpectedNumArgs)
5811     return Diag(TheCall->getEndLoc(),
5812                 diag::err_typecheck_call_too_many_args_at_most)
5813            << 0 /*function call*/ << ExpectedNumArgs << TheCall->getNumArgs()
5814            << TheCall->getSourceRange();
5815 
5816   // Check the third argument is a compile time constant
5817   llvm::APSInt Value;
5818   if(!TheCall->getArg(2)->isIntegerConstantExpr(Value, Context))
5819     return Diag(TheCall->getBeginLoc(),
5820                 diag::err_vsx_builtin_nonconstant_argument)
5821            << 3 /* argument index */ << TheCall->getDirectCallee()
5822            << SourceRange(TheCall->getArg(2)->getBeginLoc(),
5823                           TheCall->getArg(2)->getEndLoc());
5824 
5825   QualType Arg1Ty = TheCall->getArg(0)->getType();
5826   QualType Arg2Ty = TheCall->getArg(1)->getType();
5827 
5828   // Check the type of argument 1 and argument 2 are vectors.
5829   SourceLocation BuiltinLoc = TheCall->getBeginLoc();
5830   if ((!Arg1Ty->isVectorType() && !Arg1Ty->isDependentType()) ||
5831       (!Arg2Ty->isVectorType() && !Arg2Ty->isDependentType())) {
5832     return Diag(BuiltinLoc, diag::err_vec_builtin_non_vector)
5833            << TheCall->getDirectCallee()
5834            << SourceRange(TheCall->getArg(0)->getBeginLoc(),
5835                           TheCall->getArg(1)->getEndLoc());
5836   }
5837 
5838   // Check the first two arguments are the same type.
5839   if (!Context.hasSameUnqualifiedType(Arg1Ty, Arg2Ty)) {
5840     return Diag(BuiltinLoc, diag::err_vec_builtin_incompatible_vector)
5841            << TheCall->getDirectCallee()
5842            << SourceRange(TheCall->getArg(0)->getBeginLoc(),
5843                           TheCall->getArg(1)->getEndLoc());
5844   }
5845 
5846   // When default clang type checking is turned off and the customized type
5847   // checking is used, the returning type of the function must be explicitly
5848   // set. Otherwise it is _Bool by default.
5849   TheCall->setType(Arg1Ty);
5850 
5851   return false;
5852 }
5853 
5854 /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector.
5855 // This is declared to take (...), so we have to check everything.
5856 ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) {
5857   if (TheCall->getNumArgs() < 2)
5858     return ExprError(Diag(TheCall->getEndLoc(),
5859                           diag::err_typecheck_call_too_few_args_at_least)
5860                      << 0 /*function call*/ << 2 << TheCall->getNumArgs()
5861                      << TheCall->getSourceRange());
5862 
5863   // Determine which of the following types of shufflevector we're checking:
5864   // 1) unary, vector mask: (lhs, mask)
5865   // 2) binary, scalar mask: (lhs, rhs, index, ..., index)
5866   QualType resType = TheCall->getArg(0)->getType();
5867   unsigned numElements = 0;
5868 
5869   if (!TheCall->getArg(0)->isTypeDependent() &&
5870       !TheCall->getArg(1)->isTypeDependent()) {
5871     QualType LHSType = TheCall->getArg(0)->getType();
5872     QualType RHSType = TheCall->getArg(1)->getType();
5873 
5874     if (!LHSType->isVectorType() || !RHSType->isVectorType())
5875       return ExprError(
5876           Diag(TheCall->getBeginLoc(), diag::err_vec_builtin_non_vector)
5877           << TheCall->getDirectCallee()
5878           << SourceRange(TheCall->getArg(0)->getBeginLoc(),
5879                          TheCall->getArg(1)->getEndLoc()));
5880 
5881     numElements = LHSType->castAs<VectorType>()->getNumElements();
5882     unsigned numResElements = TheCall->getNumArgs() - 2;
5883 
5884     // Check to see if we have a call with 2 vector arguments, the unary shuffle
5885     // with mask.  If so, verify that RHS is an integer vector type with the
5886     // same number of elts as lhs.
5887     if (TheCall->getNumArgs() == 2) {
5888       if (!RHSType->hasIntegerRepresentation() ||
5889           RHSType->castAs<VectorType>()->getNumElements() != numElements)
5890         return ExprError(Diag(TheCall->getBeginLoc(),
5891                               diag::err_vec_builtin_incompatible_vector)
5892                          << TheCall->getDirectCallee()
5893                          << SourceRange(TheCall->getArg(1)->getBeginLoc(),
5894                                         TheCall->getArg(1)->getEndLoc()));
5895     } else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) {
5896       return ExprError(Diag(TheCall->getBeginLoc(),
5897                             diag::err_vec_builtin_incompatible_vector)
5898                        << TheCall->getDirectCallee()
5899                        << SourceRange(TheCall->getArg(0)->getBeginLoc(),
5900                                       TheCall->getArg(1)->getEndLoc()));
5901     } else if (numElements != numResElements) {
5902       QualType eltType = LHSType->castAs<VectorType>()->getElementType();
5903       resType = Context.getVectorType(eltType, numResElements,
5904                                       VectorType::GenericVector);
5905     }
5906   }
5907 
5908   for (unsigned i = 2; i < TheCall->getNumArgs(); i++) {
5909     if (TheCall->getArg(i)->isTypeDependent() ||
5910         TheCall->getArg(i)->isValueDependent())
5911       continue;
5912 
5913     llvm::APSInt Result(32);
5914     if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context))
5915       return ExprError(Diag(TheCall->getBeginLoc(),
5916                             diag::err_shufflevector_nonconstant_argument)
5917                        << TheCall->getArg(i)->getSourceRange());
5918 
5919     // Allow -1 which will be translated to undef in the IR.
5920     if (Result.isSigned() && Result.isAllOnesValue())
5921       continue;
5922 
5923     if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2)
5924       return ExprError(Diag(TheCall->getBeginLoc(),
5925                             diag::err_shufflevector_argument_too_large)
5926                        << TheCall->getArg(i)->getSourceRange());
5927   }
5928 
5929   SmallVector<Expr*, 32> exprs;
5930 
5931   for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) {
5932     exprs.push_back(TheCall->getArg(i));
5933     TheCall->setArg(i, nullptr);
5934   }
5935 
5936   return new (Context) ShuffleVectorExpr(Context, exprs, resType,
5937                                          TheCall->getCallee()->getBeginLoc(),
5938                                          TheCall->getRParenLoc());
5939 }
5940 
5941 /// SemaConvertVectorExpr - Handle __builtin_convertvector
5942 ExprResult Sema::SemaConvertVectorExpr(Expr *E, TypeSourceInfo *TInfo,
5943                                        SourceLocation BuiltinLoc,
5944                                        SourceLocation RParenLoc) {
5945   ExprValueKind VK = VK_RValue;
5946   ExprObjectKind OK = OK_Ordinary;
5947   QualType DstTy = TInfo->getType();
5948   QualType SrcTy = E->getType();
5949 
5950   if (!SrcTy->isVectorType() && !SrcTy->isDependentType())
5951     return ExprError(Diag(BuiltinLoc,
5952                           diag::err_convertvector_non_vector)
5953                      << E->getSourceRange());
5954   if (!DstTy->isVectorType() && !DstTy->isDependentType())
5955     return ExprError(Diag(BuiltinLoc,
5956                           diag::err_convertvector_non_vector_type));
5957 
5958   if (!SrcTy->isDependentType() && !DstTy->isDependentType()) {
5959     unsigned SrcElts = SrcTy->castAs<VectorType>()->getNumElements();
5960     unsigned DstElts = DstTy->castAs<VectorType>()->getNumElements();
5961     if (SrcElts != DstElts)
5962       return ExprError(Diag(BuiltinLoc,
5963                             diag::err_convertvector_incompatible_vector)
5964                        << E->getSourceRange());
5965   }
5966 
5967   return new (Context)
5968       ConvertVectorExpr(E, TInfo, DstTy, VK, OK, BuiltinLoc, RParenLoc);
5969 }
5970 
5971 /// SemaBuiltinPrefetch - Handle __builtin_prefetch.
5972 // This is declared to take (const void*, ...) and can take two
5973 // optional constant int args.
5974 bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) {
5975   unsigned NumArgs = TheCall->getNumArgs();
5976 
5977   if (NumArgs > 3)
5978     return Diag(TheCall->getEndLoc(),
5979                 diag::err_typecheck_call_too_many_args_at_most)
5980            << 0 /*function call*/ << 3 << NumArgs << TheCall->getSourceRange();
5981 
5982   // Argument 0 is checked for us and the remaining arguments must be
5983   // constant integers.
5984   for (unsigned i = 1; i != NumArgs; ++i)
5985     if (SemaBuiltinConstantArgRange(TheCall, i, 0, i == 1 ? 1 : 3))
5986       return true;
5987 
5988   return false;
5989 }
5990 
5991 /// SemaBuiltinAssume - Handle __assume (MS Extension).
5992 // __assume does not evaluate its arguments, and should warn if its argument
5993 // has side effects.
5994 bool Sema::SemaBuiltinAssume(CallExpr *TheCall) {
5995   Expr *Arg = TheCall->getArg(0);
5996   if (Arg->isInstantiationDependent()) return false;
5997 
5998   if (Arg->HasSideEffects(Context))
5999     Diag(Arg->getBeginLoc(), diag::warn_assume_side_effects)
6000         << Arg->getSourceRange()
6001         << cast<FunctionDecl>(TheCall->getCalleeDecl())->getIdentifier();
6002 
6003   return false;
6004 }
6005 
6006 /// Handle __builtin_alloca_with_align. This is declared
6007 /// as (size_t, size_t) where the second size_t must be a power of 2 greater
6008 /// than 8.
6009 bool Sema::SemaBuiltinAllocaWithAlign(CallExpr *TheCall) {
6010   // The alignment must be a constant integer.
6011   Expr *Arg = TheCall->getArg(1);
6012 
6013   // We can't check the value of a dependent argument.
6014   if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
6015     if (const auto *UE =
6016             dyn_cast<UnaryExprOrTypeTraitExpr>(Arg->IgnoreParenImpCasts()))
6017       if (UE->getKind() == UETT_AlignOf ||
6018           UE->getKind() == UETT_PreferredAlignOf)
6019         Diag(TheCall->getBeginLoc(), diag::warn_alloca_align_alignof)
6020             << Arg->getSourceRange();
6021 
6022     llvm::APSInt Result = Arg->EvaluateKnownConstInt(Context);
6023 
6024     if (!Result.isPowerOf2())
6025       return Diag(TheCall->getBeginLoc(), diag::err_alignment_not_power_of_two)
6026              << Arg->getSourceRange();
6027 
6028     if (Result < Context.getCharWidth())
6029       return Diag(TheCall->getBeginLoc(), diag::err_alignment_too_small)
6030              << (unsigned)Context.getCharWidth() << Arg->getSourceRange();
6031 
6032     if (Result > std::numeric_limits<int32_t>::max())
6033       return Diag(TheCall->getBeginLoc(), diag::err_alignment_too_big)
6034              << std::numeric_limits<int32_t>::max() << Arg->getSourceRange();
6035   }
6036 
6037   return false;
6038 }
6039 
6040 /// Handle __builtin_assume_aligned. This is declared
6041 /// as (const void*, size_t, ...) and can take one optional constant int arg.
6042 bool Sema::SemaBuiltinAssumeAligned(CallExpr *TheCall) {
6043   unsigned NumArgs = TheCall->getNumArgs();
6044 
6045   if (NumArgs > 3)
6046     return Diag(TheCall->getEndLoc(),
6047                 diag::err_typecheck_call_too_many_args_at_most)
6048            << 0 /*function call*/ << 3 << NumArgs << TheCall->getSourceRange();
6049 
6050   // The alignment must be a constant integer.
6051   Expr *Arg = TheCall->getArg(1);
6052 
6053   // We can't check the value of a dependent argument.
6054   if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
6055     llvm::APSInt Result;
6056     if (SemaBuiltinConstantArg(TheCall, 1, Result))
6057       return true;
6058 
6059     if (!Result.isPowerOf2())
6060       return Diag(TheCall->getBeginLoc(), diag::err_alignment_not_power_of_two)
6061              << Arg->getSourceRange();
6062 
6063     if (Result > Sema::MaximumAlignment)
6064       Diag(TheCall->getBeginLoc(), diag::warn_assume_aligned_too_great)
6065           << Arg->getSourceRange() << Sema::MaximumAlignment;
6066   }
6067 
6068   if (NumArgs > 2) {
6069     ExprResult Arg(TheCall->getArg(2));
6070     InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
6071       Context.getSizeType(), false);
6072     Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
6073     if (Arg.isInvalid()) return true;
6074     TheCall->setArg(2, Arg.get());
6075   }
6076 
6077   return false;
6078 }
6079 
6080 bool Sema::SemaBuiltinOSLogFormat(CallExpr *TheCall) {
6081   unsigned BuiltinID =
6082       cast<FunctionDecl>(TheCall->getCalleeDecl())->getBuiltinID();
6083   bool IsSizeCall = BuiltinID == Builtin::BI__builtin_os_log_format_buffer_size;
6084 
6085   unsigned NumArgs = TheCall->getNumArgs();
6086   unsigned NumRequiredArgs = IsSizeCall ? 1 : 2;
6087   if (NumArgs < NumRequiredArgs) {
6088     return Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args)
6089            << 0 /* function call */ << NumRequiredArgs << NumArgs
6090            << TheCall->getSourceRange();
6091   }
6092   if (NumArgs >= NumRequiredArgs + 0x100) {
6093     return Diag(TheCall->getEndLoc(),
6094                 diag::err_typecheck_call_too_many_args_at_most)
6095            << 0 /* function call */ << (NumRequiredArgs + 0xff) << NumArgs
6096            << TheCall->getSourceRange();
6097   }
6098   unsigned i = 0;
6099 
6100   // For formatting call, check buffer arg.
6101   if (!IsSizeCall) {
6102     ExprResult Arg(TheCall->getArg(i));
6103     InitializedEntity Entity = InitializedEntity::InitializeParameter(
6104         Context, Context.VoidPtrTy, false);
6105     Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
6106     if (Arg.isInvalid())
6107       return true;
6108     TheCall->setArg(i, Arg.get());
6109     i++;
6110   }
6111 
6112   // Check string literal arg.
6113   unsigned FormatIdx = i;
6114   {
6115     ExprResult Arg = CheckOSLogFormatStringArg(TheCall->getArg(i));
6116     if (Arg.isInvalid())
6117       return true;
6118     TheCall->setArg(i, Arg.get());
6119     i++;
6120   }
6121 
6122   // Make sure variadic args are scalar.
6123   unsigned FirstDataArg = i;
6124   while (i < NumArgs) {
6125     ExprResult Arg = DefaultVariadicArgumentPromotion(
6126         TheCall->getArg(i), VariadicFunction, nullptr);
6127     if (Arg.isInvalid())
6128       return true;
6129     CharUnits ArgSize = Context.getTypeSizeInChars(Arg.get()->getType());
6130     if (ArgSize.getQuantity() >= 0x100) {
6131       return Diag(Arg.get()->getEndLoc(), diag::err_os_log_argument_too_big)
6132              << i << (int)ArgSize.getQuantity() << 0xff
6133              << TheCall->getSourceRange();
6134     }
6135     TheCall->setArg(i, Arg.get());
6136     i++;
6137   }
6138 
6139   // Check formatting specifiers. NOTE: We're only doing this for the non-size
6140   // call to avoid duplicate diagnostics.
6141   if (!IsSizeCall) {
6142     llvm::SmallBitVector CheckedVarArgs(NumArgs, false);
6143     ArrayRef<const Expr *> Args(TheCall->getArgs(), TheCall->getNumArgs());
6144     bool Success = CheckFormatArguments(
6145         Args, /*HasVAListArg*/ false, FormatIdx, FirstDataArg, FST_OSLog,
6146         VariadicFunction, TheCall->getBeginLoc(), SourceRange(),
6147         CheckedVarArgs);
6148     if (!Success)
6149       return true;
6150   }
6151 
6152   if (IsSizeCall) {
6153     TheCall->setType(Context.getSizeType());
6154   } else {
6155     TheCall->setType(Context.VoidPtrTy);
6156   }
6157   return false;
6158 }
6159 
6160 /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr
6161 /// TheCall is a constant expression.
6162 bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum,
6163                                   llvm::APSInt &Result) {
6164   Expr *Arg = TheCall->getArg(ArgNum);
6165   DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
6166   FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
6167 
6168   if (Arg->isTypeDependent() || Arg->isValueDependent()) return false;
6169 
6170   if (!Arg->isIntegerConstantExpr(Result, Context))
6171     return Diag(TheCall->getBeginLoc(), diag::err_constant_integer_arg_type)
6172            << FDecl->getDeclName() << Arg->getSourceRange();
6173 
6174   return false;
6175 }
6176 
6177 /// SemaBuiltinConstantArgRange - Handle a check if argument ArgNum of CallExpr
6178 /// TheCall is a constant expression in the range [Low, High].
6179 bool Sema::SemaBuiltinConstantArgRange(CallExpr *TheCall, int ArgNum,
6180                                        int Low, int High, bool RangeIsError) {
6181   if (isConstantEvaluated())
6182     return false;
6183   llvm::APSInt Result;
6184 
6185   // We can't check the value of a dependent argument.
6186   Expr *Arg = TheCall->getArg(ArgNum);
6187   if (Arg->isTypeDependent() || Arg->isValueDependent())
6188     return false;
6189 
6190   // Check constant-ness first.
6191   if (SemaBuiltinConstantArg(TheCall, ArgNum, Result))
6192     return true;
6193 
6194   if (Result.getSExtValue() < Low || Result.getSExtValue() > High) {
6195     if (RangeIsError)
6196       return Diag(TheCall->getBeginLoc(), diag::err_argument_invalid_range)
6197              << Result.toString(10) << Low << High << Arg->getSourceRange();
6198     else
6199       // Defer the warning until we know if the code will be emitted so that
6200       // dead code can ignore this.
6201       DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall,
6202                           PDiag(diag::warn_argument_invalid_range)
6203                               << Result.toString(10) << Low << High
6204                               << Arg->getSourceRange());
6205   }
6206 
6207   return false;
6208 }
6209 
6210 /// SemaBuiltinConstantArgMultiple - Handle a check if argument ArgNum of CallExpr
6211 /// TheCall is a constant expression is a multiple of Num..
6212 bool Sema::SemaBuiltinConstantArgMultiple(CallExpr *TheCall, int ArgNum,
6213                                           unsigned Num) {
6214   llvm::APSInt Result;
6215 
6216   // We can't check the value of a dependent argument.
6217   Expr *Arg = TheCall->getArg(ArgNum);
6218   if (Arg->isTypeDependent() || Arg->isValueDependent())
6219     return false;
6220 
6221   // Check constant-ness first.
6222   if (SemaBuiltinConstantArg(TheCall, ArgNum, Result))
6223     return true;
6224 
6225   if (Result.getSExtValue() % Num != 0)
6226     return Diag(TheCall->getBeginLoc(), diag::err_argument_not_multiple)
6227            << Num << Arg->getSourceRange();
6228 
6229   return false;
6230 }
6231 
6232 /// SemaBuiltinConstantArgPower2 - Check if argument ArgNum of TheCall is a
6233 /// constant expression representing a power of 2.
6234 bool Sema::SemaBuiltinConstantArgPower2(CallExpr *TheCall, int ArgNum) {
6235   llvm::APSInt Result;
6236 
6237   // We can't check the value of a dependent argument.
6238   Expr *Arg = TheCall->getArg(ArgNum);
6239   if (Arg->isTypeDependent() || Arg->isValueDependent())
6240     return false;
6241 
6242   // Check constant-ness first.
6243   if (SemaBuiltinConstantArg(TheCall, ArgNum, Result))
6244     return true;
6245 
6246   // Bit-twiddling to test for a power of 2: for x > 0, x & (x-1) is zero if
6247   // and only if x is a power of 2.
6248   if (Result.isStrictlyPositive() && (Result & (Result - 1)) == 0)
6249     return false;
6250 
6251   return Diag(TheCall->getBeginLoc(), diag::err_argument_not_power_of_2)
6252          << Arg->getSourceRange();
6253 }
6254 
6255 static bool IsShiftedByte(llvm::APSInt Value) {
6256   if (Value.isNegative())
6257     return false;
6258 
6259   // Check if it's a shifted byte, by shifting it down
6260   while (true) {
6261     // If the value fits in the bottom byte, the check passes.
6262     if (Value < 0x100)
6263       return true;
6264 
6265     // Otherwise, if the value has _any_ bits in the bottom byte, the check
6266     // fails.
6267     if ((Value & 0xFF) != 0)
6268       return false;
6269 
6270     // If the bottom 8 bits are all 0, but something above that is nonzero,
6271     // then shifting the value right by 8 bits won't affect whether it's a
6272     // shifted byte or not. So do that, and go round again.
6273     Value >>= 8;
6274   }
6275 }
6276 
6277 /// SemaBuiltinConstantArgShiftedByte - Check if argument ArgNum of TheCall is
6278 /// a constant expression representing an arbitrary byte value shifted left by
6279 /// a multiple of 8 bits.
6280 bool Sema::SemaBuiltinConstantArgShiftedByte(CallExpr *TheCall, int ArgNum,
6281                                              unsigned ArgBits) {
6282   llvm::APSInt Result;
6283 
6284   // We can't check the value of a dependent argument.
6285   Expr *Arg = TheCall->getArg(ArgNum);
6286   if (Arg->isTypeDependent() || Arg->isValueDependent())
6287     return false;
6288 
6289   // Check constant-ness first.
6290   if (SemaBuiltinConstantArg(TheCall, ArgNum, Result))
6291     return true;
6292 
6293   // Truncate to the given size.
6294   Result = Result.getLoBits(ArgBits);
6295   Result.setIsUnsigned(true);
6296 
6297   if (IsShiftedByte(Result))
6298     return false;
6299 
6300   return Diag(TheCall->getBeginLoc(), diag::err_argument_not_shifted_byte)
6301          << Arg->getSourceRange();
6302 }
6303 
6304 /// SemaBuiltinConstantArgShiftedByteOr0xFF - Check if argument ArgNum of
6305 /// TheCall is a constant expression representing either a shifted byte value,
6306 /// or a value of the form 0x??FF (i.e. a member of the arithmetic progression
6307 /// 0x00FF, 0x01FF, ..., 0xFFFF). This strange range check is needed for some
6308 /// Arm MVE intrinsics.
6309 bool Sema::SemaBuiltinConstantArgShiftedByteOrXXFF(CallExpr *TheCall,
6310                                                    int ArgNum,
6311                                                    unsigned ArgBits) {
6312   llvm::APSInt Result;
6313 
6314   // We can't check the value of a dependent argument.
6315   Expr *Arg = TheCall->getArg(ArgNum);
6316   if (Arg->isTypeDependent() || Arg->isValueDependent())
6317     return false;
6318 
6319   // Check constant-ness first.
6320   if (SemaBuiltinConstantArg(TheCall, ArgNum, Result))
6321     return true;
6322 
6323   // Truncate to the given size.
6324   Result = Result.getLoBits(ArgBits);
6325   Result.setIsUnsigned(true);
6326 
6327   // Check to see if it's in either of the required forms.
6328   if (IsShiftedByte(Result) ||
6329       (Result > 0 && Result < 0x10000 && (Result & 0xFF) == 0xFF))
6330     return false;
6331 
6332   return Diag(TheCall->getBeginLoc(),
6333               diag::err_argument_not_shifted_byte_or_xxff)
6334          << Arg->getSourceRange();
6335 }
6336 
6337 /// SemaBuiltinARMMemoryTaggingCall - Handle calls of memory tagging extensions
6338 bool Sema::SemaBuiltinARMMemoryTaggingCall(unsigned BuiltinID, CallExpr *TheCall) {
6339   if (BuiltinID == AArch64::BI__builtin_arm_irg) {
6340     if (checkArgCount(*this, TheCall, 2))
6341       return true;
6342     Expr *Arg0 = TheCall->getArg(0);
6343     Expr *Arg1 = TheCall->getArg(1);
6344 
6345     ExprResult FirstArg = DefaultFunctionArrayLvalueConversion(Arg0);
6346     if (FirstArg.isInvalid())
6347       return true;
6348     QualType FirstArgType = FirstArg.get()->getType();
6349     if (!FirstArgType->isAnyPointerType())
6350       return Diag(TheCall->getBeginLoc(), diag::err_memtag_arg_must_be_pointer)
6351                << "first" << FirstArgType << Arg0->getSourceRange();
6352     TheCall->setArg(0, FirstArg.get());
6353 
6354     ExprResult SecArg = DefaultLvalueConversion(Arg1);
6355     if (SecArg.isInvalid())
6356       return true;
6357     QualType SecArgType = SecArg.get()->getType();
6358     if (!SecArgType->isIntegerType())
6359       return Diag(TheCall->getBeginLoc(), diag::err_memtag_arg_must_be_integer)
6360                << "second" << SecArgType << Arg1->getSourceRange();
6361 
6362     // Derive the return type from the pointer argument.
6363     TheCall->setType(FirstArgType);
6364     return false;
6365   }
6366 
6367   if (BuiltinID == AArch64::BI__builtin_arm_addg) {
6368     if (checkArgCount(*this, TheCall, 2))
6369       return true;
6370 
6371     Expr *Arg0 = TheCall->getArg(0);
6372     ExprResult FirstArg = DefaultFunctionArrayLvalueConversion(Arg0);
6373     if (FirstArg.isInvalid())
6374       return true;
6375     QualType FirstArgType = FirstArg.get()->getType();
6376     if (!FirstArgType->isAnyPointerType())
6377       return Diag(TheCall->getBeginLoc(), diag::err_memtag_arg_must_be_pointer)
6378                << "first" << FirstArgType << Arg0->getSourceRange();
6379     TheCall->setArg(0, FirstArg.get());
6380 
6381     // Derive the return type from the pointer argument.
6382     TheCall->setType(FirstArgType);
6383 
6384     // Second arg must be an constant in range [0,15]
6385     return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15);
6386   }
6387 
6388   if (BuiltinID == AArch64::BI__builtin_arm_gmi) {
6389     if (checkArgCount(*this, TheCall, 2))
6390       return true;
6391     Expr *Arg0 = TheCall->getArg(0);
6392     Expr *Arg1 = TheCall->getArg(1);
6393 
6394     ExprResult FirstArg = DefaultFunctionArrayLvalueConversion(Arg0);
6395     if (FirstArg.isInvalid())
6396       return true;
6397     QualType FirstArgType = FirstArg.get()->getType();
6398     if (!FirstArgType->isAnyPointerType())
6399       return Diag(TheCall->getBeginLoc(), diag::err_memtag_arg_must_be_pointer)
6400                << "first" << FirstArgType << Arg0->getSourceRange();
6401 
6402     QualType SecArgType = Arg1->getType();
6403     if (!SecArgType->isIntegerType())
6404       return Diag(TheCall->getBeginLoc(), diag::err_memtag_arg_must_be_integer)
6405                << "second" << SecArgType << Arg1->getSourceRange();
6406     TheCall->setType(Context.IntTy);
6407     return false;
6408   }
6409 
6410   if (BuiltinID == AArch64::BI__builtin_arm_ldg ||
6411       BuiltinID == AArch64::BI__builtin_arm_stg) {
6412     if (checkArgCount(*this, TheCall, 1))
6413       return true;
6414     Expr *Arg0 = TheCall->getArg(0);
6415     ExprResult FirstArg = DefaultFunctionArrayLvalueConversion(Arg0);
6416     if (FirstArg.isInvalid())
6417       return true;
6418 
6419     QualType FirstArgType = FirstArg.get()->getType();
6420     if (!FirstArgType->isAnyPointerType())
6421       return Diag(TheCall->getBeginLoc(), diag::err_memtag_arg_must_be_pointer)
6422                << "first" << FirstArgType << Arg0->getSourceRange();
6423     TheCall->setArg(0, FirstArg.get());
6424 
6425     // Derive the return type from the pointer argument.
6426     if (BuiltinID == AArch64::BI__builtin_arm_ldg)
6427       TheCall->setType(FirstArgType);
6428     return false;
6429   }
6430 
6431   if (BuiltinID == AArch64::BI__builtin_arm_subp) {
6432     Expr *ArgA = TheCall->getArg(0);
6433     Expr *ArgB = TheCall->getArg(1);
6434 
6435     ExprResult ArgExprA = DefaultFunctionArrayLvalueConversion(ArgA);
6436     ExprResult ArgExprB = DefaultFunctionArrayLvalueConversion(ArgB);
6437 
6438     if (ArgExprA.isInvalid() || ArgExprB.isInvalid())
6439       return true;
6440 
6441     QualType ArgTypeA = ArgExprA.get()->getType();
6442     QualType ArgTypeB = ArgExprB.get()->getType();
6443 
6444     auto isNull = [&] (Expr *E) -> bool {
6445       return E->isNullPointerConstant(
6446                         Context, Expr::NPC_ValueDependentIsNotNull); };
6447 
6448     // argument should be either a pointer or null
6449     if (!ArgTypeA->isAnyPointerType() && !isNull(ArgA))
6450       return Diag(TheCall->getBeginLoc(), diag::err_memtag_arg_null_or_pointer)
6451         << "first" << ArgTypeA << ArgA->getSourceRange();
6452 
6453     if (!ArgTypeB->isAnyPointerType() && !isNull(ArgB))
6454       return Diag(TheCall->getBeginLoc(), diag::err_memtag_arg_null_or_pointer)
6455         << "second" << ArgTypeB << ArgB->getSourceRange();
6456 
6457     // Ensure Pointee types are compatible
6458     if (ArgTypeA->isAnyPointerType() && !isNull(ArgA) &&
6459         ArgTypeB->isAnyPointerType() && !isNull(ArgB)) {
6460       QualType pointeeA = ArgTypeA->getPointeeType();
6461       QualType pointeeB = ArgTypeB->getPointeeType();
6462       if (!Context.typesAreCompatible(
6463              Context.getCanonicalType(pointeeA).getUnqualifiedType(),
6464              Context.getCanonicalType(pointeeB).getUnqualifiedType())) {
6465         return Diag(TheCall->getBeginLoc(), diag::err_typecheck_sub_ptr_compatible)
6466           << ArgTypeA <<  ArgTypeB << ArgA->getSourceRange()
6467           << ArgB->getSourceRange();
6468       }
6469     }
6470 
6471     // at least one argument should be pointer type
6472     if (!ArgTypeA->isAnyPointerType() && !ArgTypeB->isAnyPointerType())
6473       return Diag(TheCall->getBeginLoc(), diag::err_memtag_any2arg_pointer)
6474         <<  ArgTypeA << ArgTypeB << ArgA->getSourceRange();
6475 
6476     if (isNull(ArgA)) // adopt type of the other pointer
6477       ArgExprA = ImpCastExprToType(ArgExprA.get(), ArgTypeB, CK_NullToPointer);
6478 
6479     if (isNull(ArgB))
6480       ArgExprB = ImpCastExprToType(ArgExprB.get(), ArgTypeA, CK_NullToPointer);
6481 
6482     TheCall->setArg(0, ArgExprA.get());
6483     TheCall->setArg(1, ArgExprB.get());
6484     TheCall->setType(Context.LongLongTy);
6485     return false;
6486   }
6487   assert(false && "Unhandled ARM MTE intrinsic");
6488   return true;
6489 }
6490 
6491 /// SemaBuiltinARMSpecialReg - Handle a check if argument ArgNum of CallExpr
6492 /// TheCall is an ARM/AArch64 special register string literal.
6493 bool Sema::SemaBuiltinARMSpecialReg(unsigned BuiltinID, CallExpr *TheCall,
6494                                     int ArgNum, unsigned ExpectedFieldNum,
6495                                     bool AllowName) {
6496   bool IsARMBuiltin = BuiltinID == ARM::BI__builtin_arm_rsr64 ||
6497                       BuiltinID == ARM::BI__builtin_arm_wsr64 ||
6498                       BuiltinID == ARM::BI__builtin_arm_rsr ||
6499                       BuiltinID == ARM::BI__builtin_arm_rsrp ||
6500                       BuiltinID == ARM::BI__builtin_arm_wsr ||
6501                       BuiltinID == ARM::BI__builtin_arm_wsrp;
6502   bool IsAArch64Builtin = BuiltinID == AArch64::BI__builtin_arm_rsr64 ||
6503                           BuiltinID == AArch64::BI__builtin_arm_wsr64 ||
6504                           BuiltinID == AArch64::BI__builtin_arm_rsr ||
6505                           BuiltinID == AArch64::BI__builtin_arm_rsrp ||
6506                           BuiltinID == AArch64::BI__builtin_arm_wsr ||
6507                           BuiltinID == AArch64::BI__builtin_arm_wsrp;
6508   assert((IsARMBuiltin || IsAArch64Builtin) && "Unexpected ARM builtin.");
6509 
6510   // We can't check the value of a dependent argument.
6511   Expr *Arg = TheCall->getArg(ArgNum);
6512   if (Arg->isTypeDependent() || Arg->isValueDependent())
6513     return false;
6514 
6515   // Check if the argument is a string literal.
6516   if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts()))
6517     return Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal)
6518            << Arg->getSourceRange();
6519 
6520   // Check the type of special register given.
6521   StringRef Reg = cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString();
6522   SmallVector<StringRef, 6> Fields;
6523   Reg.split(Fields, ":");
6524 
6525   if (Fields.size() != ExpectedFieldNum && !(AllowName && Fields.size() == 1))
6526     return Diag(TheCall->getBeginLoc(), diag::err_arm_invalid_specialreg)
6527            << Arg->getSourceRange();
6528 
6529   // If the string is the name of a register then we cannot check that it is
6530   // valid here but if the string is of one the forms described in ACLE then we
6531   // can check that the supplied fields are integers and within the valid
6532   // ranges.
6533   if (Fields.size() > 1) {
6534     bool FiveFields = Fields.size() == 5;
6535 
6536     bool ValidString = true;
6537     if (IsARMBuiltin) {
6538       ValidString &= Fields[0].startswith_lower("cp") ||
6539                      Fields[0].startswith_lower("p");
6540       if (ValidString)
6541         Fields[0] =
6542           Fields[0].drop_front(Fields[0].startswith_lower("cp") ? 2 : 1);
6543 
6544       ValidString &= Fields[2].startswith_lower("c");
6545       if (ValidString)
6546         Fields[2] = Fields[2].drop_front(1);
6547 
6548       if (FiveFields) {
6549         ValidString &= Fields[3].startswith_lower("c");
6550         if (ValidString)
6551           Fields[3] = Fields[3].drop_front(1);
6552       }
6553     }
6554 
6555     SmallVector<int, 5> Ranges;
6556     if (FiveFields)
6557       Ranges.append({IsAArch64Builtin ? 1 : 15, 7, 15, 15, 7});
6558     else
6559       Ranges.append({15, 7, 15});
6560 
6561     for (unsigned i=0; i<Fields.size(); ++i) {
6562       int IntField;
6563       ValidString &= !Fields[i].getAsInteger(10, IntField);
6564       ValidString &= (IntField >= 0 && IntField <= Ranges[i]);
6565     }
6566 
6567     if (!ValidString)
6568       return Diag(TheCall->getBeginLoc(), diag::err_arm_invalid_specialreg)
6569              << Arg->getSourceRange();
6570   } else if (IsAArch64Builtin && Fields.size() == 1) {
6571     // If the register name is one of those that appear in the condition below
6572     // and the special register builtin being used is one of the write builtins,
6573     // then we require that the argument provided for writing to the register
6574     // is an integer constant expression. This is because it will be lowered to
6575     // an MSR (immediate) instruction, so we need to know the immediate at
6576     // compile time.
6577     if (TheCall->getNumArgs() != 2)
6578       return false;
6579 
6580     std::string RegLower = Reg.lower();
6581     if (RegLower != "spsel" && RegLower != "daifset" && RegLower != "daifclr" &&
6582         RegLower != "pan" && RegLower != "uao")
6583       return false;
6584 
6585     return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15);
6586   }
6587 
6588   return false;
6589 }
6590 
6591 /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val).
6592 /// This checks that the target supports __builtin_longjmp and
6593 /// that val is a constant 1.
6594 bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) {
6595   if (!Context.getTargetInfo().hasSjLjLowering())
6596     return Diag(TheCall->getBeginLoc(), diag::err_builtin_longjmp_unsupported)
6597            << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc());
6598 
6599   Expr *Arg = TheCall->getArg(1);
6600   llvm::APSInt Result;
6601 
6602   // TODO: This is less than ideal. Overload this to take a value.
6603   if (SemaBuiltinConstantArg(TheCall, 1, Result))
6604     return true;
6605 
6606   if (Result != 1)
6607     return Diag(TheCall->getBeginLoc(), diag::err_builtin_longjmp_invalid_val)
6608            << SourceRange(Arg->getBeginLoc(), Arg->getEndLoc());
6609 
6610   return false;
6611 }
6612 
6613 /// SemaBuiltinSetjmp - Handle __builtin_setjmp(void *env[5]).
6614 /// This checks that the target supports __builtin_setjmp.
6615 bool Sema::SemaBuiltinSetjmp(CallExpr *TheCall) {
6616   if (!Context.getTargetInfo().hasSjLjLowering())
6617     return Diag(TheCall->getBeginLoc(), diag::err_builtin_setjmp_unsupported)
6618            << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc());
6619   return false;
6620 }
6621 
6622 namespace {
6623 
6624 class UncoveredArgHandler {
6625   enum { Unknown = -1, AllCovered = -2 };
6626 
6627   signed FirstUncoveredArg = Unknown;
6628   SmallVector<const Expr *, 4> DiagnosticExprs;
6629 
6630 public:
6631   UncoveredArgHandler() = default;
6632 
6633   bool hasUncoveredArg() const {
6634     return (FirstUncoveredArg >= 0);
6635   }
6636 
6637   unsigned getUncoveredArg() const {
6638     assert(hasUncoveredArg() && "no uncovered argument");
6639     return FirstUncoveredArg;
6640   }
6641 
6642   void setAllCovered() {
6643     // A string has been found with all arguments covered, so clear out
6644     // the diagnostics.
6645     DiagnosticExprs.clear();
6646     FirstUncoveredArg = AllCovered;
6647   }
6648 
6649   void Update(signed NewFirstUncoveredArg, const Expr *StrExpr) {
6650     assert(NewFirstUncoveredArg >= 0 && "Outside range");
6651 
6652     // Don't update if a previous string covers all arguments.
6653     if (FirstUncoveredArg == AllCovered)
6654       return;
6655 
6656     // UncoveredArgHandler tracks the highest uncovered argument index
6657     // and with it all the strings that match this index.
6658     if (NewFirstUncoveredArg == FirstUncoveredArg)
6659       DiagnosticExprs.push_back(StrExpr);
6660     else if (NewFirstUncoveredArg > FirstUncoveredArg) {
6661       DiagnosticExprs.clear();
6662       DiagnosticExprs.push_back(StrExpr);
6663       FirstUncoveredArg = NewFirstUncoveredArg;
6664     }
6665   }
6666 
6667   void Diagnose(Sema &S, bool IsFunctionCall, const Expr *ArgExpr);
6668 };
6669 
6670 enum StringLiteralCheckType {
6671   SLCT_NotALiteral,
6672   SLCT_UncheckedLiteral,
6673   SLCT_CheckedLiteral
6674 };
6675 
6676 } // namespace
6677 
6678 static void sumOffsets(llvm::APSInt &Offset, llvm::APSInt Addend,
6679                                      BinaryOperatorKind BinOpKind,
6680                                      bool AddendIsRight) {
6681   unsigned BitWidth = Offset.getBitWidth();
6682   unsigned AddendBitWidth = Addend.getBitWidth();
6683   // There might be negative interim results.
6684   if (Addend.isUnsigned()) {
6685     Addend = Addend.zext(++AddendBitWidth);
6686     Addend.setIsSigned(true);
6687   }
6688   // Adjust the bit width of the APSInts.
6689   if (AddendBitWidth > BitWidth) {
6690     Offset = Offset.sext(AddendBitWidth);
6691     BitWidth = AddendBitWidth;
6692   } else if (BitWidth > AddendBitWidth) {
6693     Addend = Addend.sext(BitWidth);
6694   }
6695 
6696   bool Ov = false;
6697   llvm::APSInt ResOffset = Offset;
6698   if (BinOpKind == BO_Add)
6699     ResOffset = Offset.sadd_ov(Addend, Ov);
6700   else {
6701     assert(AddendIsRight && BinOpKind == BO_Sub &&
6702            "operator must be add or sub with addend on the right");
6703     ResOffset = Offset.ssub_ov(Addend, Ov);
6704   }
6705 
6706   // We add an offset to a pointer here so we should support an offset as big as
6707   // possible.
6708   if (Ov) {
6709     assert(BitWidth <= std::numeric_limits<unsigned>::max() / 2 &&
6710            "index (intermediate) result too big");
6711     Offset = Offset.sext(2 * BitWidth);
6712     sumOffsets(Offset, Addend, BinOpKind, AddendIsRight);
6713     return;
6714   }
6715 
6716   Offset = ResOffset;
6717 }
6718 
6719 namespace {
6720 
6721 // This is a wrapper class around StringLiteral to support offsetted string
6722 // literals as format strings. It takes the offset into account when returning
6723 // the string and its length or the source locations to display notes correctly.
6724 class FormatStringLiteral {
6725   const StringLiteral *FExpr;
6726   int64_t Offset;
6727 
6728  public:
6729   FormatStringLiteral(const StringLiteral *fexpr, int64_t Offset = 0)
6730       : FExpr(fexpr), Offset(Offset) {}
6731 
6732   StringRef getString() const {
6733     return FExpr->getString().drop_front(Offset);
6734   }
6735 
6736   unsigned getByteLength() const {
6737     return FExpr->getByteLength() - getCharByteWidth() * Offset;
6738   }
6739 
6740   unsigned getLength() const { return FExpr->getLength() - Offset; }
6741   unsigned getCharByteWidth() const { return FExpr->getCharByteWidth(); }
6742 
6743   StringLiteral::StringKind getKind() const { return FExpr->getKind(); }
6744 
6745   QualType getType() const { return FExpr->getType(); }
6746 
6747   bool isAscii() const { return FExpr->isAscii(); }
6748   bool isWide() const { return FExpr->isWide(); }
6749   bool isUTF8() const { return FExpr->isUTF8(); }
6750   bool isUTF16() const { return FExpr->isUTF16(); }
6751   bool isUTF32() const { return FExpr->isUTF32(); }
6752   bool isPascal() const { return FExpr->isPascal(); }
6753 
6754   SourceLocation getLocationOfByte(
6755       unsigned ByteNo, const SourceManager &SM, const LangOptions &Features,
6756       const TargetInfo &Target, unsigned *StartToken = nullptr,
6757       unsigned *StartTokenByteOffset = nullptr) const {
6758     return FExpr->getLocationOfByte(ByteNo + Offset, SM, Features, Target,
6759                                     StartToken, StartTokenByteOffset);
6760   }
6761 
6762   SourceLocation getBeginLoc() const LLVM_READONLY {
6763     return FExpr->getBeginLoc().getLocWithOffset(Offset);
6764   }
6765 
6766   SourceLocation getEndLoc() const LLVM_READONLY { return FExpr->getEndLoc(); }
6767 };
6768 
6769 }  // namespace
6770 
6771 static void CheckFormatString(Sema &S, const FormatStringLiteral *FExpr,
6772                               const Expr *OrigFormatExpr,
6773                               ArrayRef<const Expr *> Args,
6774                               bool HasVAListArg, unsigned format_idx,
6775                               unsigned firstDataArg,
6776                               Sema::FormatStringType Type,
6777                               bool inFunctionCall,
6778                               Sema::VariadicCallType CallType,
6779                               llvm::SmallBitVector &CheckedVarArgs,
6780                               UncoveredArgHandler &UncoveredArg,
6781                               bool IgnoreStringsWithoutSpecifiers);
6782 
6783 // Determine if an expression is a string literal or constant string.
6784 // If this function returns false on the arguments to a function expecting a
6785 // format string, we will usually need to emit a warning.
6786 // True string literals are then checked by CheckFormatString.
6787 static StringLiteralCheckType
6788 checkFormatStringExpr(Sema &S, const Expr *E, ArrayRef<const Expr *> Args,
6789                       bool HasVAListArg, unsigned format_idx,
6790                       unsigned firstDataArg, Sema::FormatStringType Type,
6791                       Sema::VariadicCallType CallType, bool InFunctionCall,
6792                       llvm::SmallBitVector &CheckedVarArgs,
6793                       UncoveredArgHandler &UncoveredArg,
6794                       llvm::APSInt Offset,
6795                       bool IgnoreStringsWithoutSpecifiers = false) {
6796   if (S.isConstantEvaluated())
6797     return SLCT_NotALiteral;
6798  tryAgain:
6799   assert(Offset.isSigned() && "invalid offset");
6800 
6801   if (E->isTypeDependent() || E->isValueDependent())
6802     return SLCT_NotALiteral;
6803 
6804   E = E->IgnoreParenCasts();
6805 
6806   if (E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull))
6807     // Technically -Wformat-nonliteral does not warn about this case.
6808     // The behavior of printf and friends in this case is implementation
6809     // dependent.  Ideally if the format string cannot be null then
6810     // it should have a 'nonnull' attribute in the function prototype.
6811     return SLCT_UncheckedLiteral;
6812 
6813   switch (E->getStmtClass()) {
6814   case Stmt::BinaryConditionalOperatorClass:
6815   case Stmt::ConditionalOperatorClass: {
6816     // The expression is a literal if both sub-expressions were, and it was
6817     // completely checked only if both sub-expressions were checked.
6818     const AbstractConditionalOperator *C =
6819         cast<AbstractConditionalOperator>(E);
6820 
6821     // Determine whether it is necessary to check both sub-expressions, for
6822     // example, because the condition expression is a constant that can be
6823     // evaluated at compile time.
6824     bool CheckLeft = true, CheckRight = true;
6825 
6826     bool Cond;
6827     if (C->getCond()->EvaluateAsBooleanCondition(Cond, S.getASTContext(),
6828                                                  S.isConstantEvaluated())) {
6829       if (Cond)
6830         CheckRight = false;
6831       else
6832         CheckLeft = false;
6833     }
6834 
6835     // We need to maintain the offsets for the right and the left hand side
6836     // separately to check if every possible indexed expression is a valid
6837     // string literal. They might have different offsets for different string
6838     // literals in the end.
6839     StringLiteralCheckType Left;
6840     if (!CheckLeft)
6841       Left = SLCT_UncheckedLiteral;
6842     else {
6843       Left = checkFormatStringExpr(S, C->getTrueExpr(), Args,
6844                                    HasVAListArg, format_idx, firstDataArg,
6845                                    Type, CallType, InFunctionCall,
6846                                    CheckedVarArgs, UncoveredArg, Offset,
6847                                    IgnoreStringsWithoutSpecifiers);
6848       if (Left == SLCT_NotALiteral || !CheckRight) {
6849         return Left;
6850       }
6851     }
6852 
6853     StringLiteralCheckType Right = checkFormatStringExpr(
6854         S, C->getFalseExpr(), Args, HasVAListArg, format_idx, firstDataArg,
6855         Type, CallType, InFunctionCall, CheckedVarArgs, UncoveredArg, Offset,
6856         IgnoreStringsWithoutSpecifiers);
6857 
6858     return (CheckLeft && Left < Right) ? Left : Right;
6859   }
6860 
6861   case Stmt::ImplicitCastExprClass:
6862     E = cast<ImplicitCastExpr>(E)->getSubExpr();
6863     goto tryAgain;
6864 
6865   case Stmt::OpaqueValueExprClass:
6866     if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) {
6867       E = src;
6868       goto tryAgain;
6869     }
6870     return SLCT_NotALiteral;
6871 
6872   case Stmt::PredefinedExprClass:
6873     // While __func__, etc., are technically not string literals, they
6874     // cannot contain format specifiers and thus are not a security
6875     // liability.
6876     return SLCT_UncheckedLiteral;
6877 
6878   case Stmt::DeclRefExprClass: {
6879     const DeclRefExpr *DR = cast<DeclRefExpr>(E);
6880 
6881     // As an exception, do not flag errors for variables binding to
6882     // const string literals.
6883     if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
6884       bool isConstant = false;
6885       QualType T = DR->getType();
6886 
6887       if (const ArrayType *AT = S.Context.getAsArrayType(T)) {
6888         isConstant = AT->getElementType().isConstant(S.Context);
6889       } else if (const PointerType *PT = T->getAs<PointerType>()) {
6890         isConstant = T.isConstant(S.Context) &&
6891                      PT->getPointeeType().isConstant(S.Context);
6892       } else if (T->isObjCObjectPointerType()) {
6893         // In ObjC, there is usually no "const ObjectPointer" type,
6894         // so don't check if the pointee type is constant.
6895         isConstant = T.isConstant(S.Context);
6896       }
6897 
6898       if (isConstant) {
6899         if (const Expr *Init = VD->getAnyInitializer()) {
6900           // Look through initializers like const char c[] = { "foo" }
6901           if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) {
6902             if (InitList->isStringLiteralInit())
6903               Init = InitList->getInit(0)->IgnoreParenImpCasts();
6904           }
6905           return checkFormatStringExpr(S, Init, Args,
6906                                        HasVAListArg, format_idx,
6907                                        firstDataArg, Type, CallType,
6908                                        /*InFunctionCall*/ false, CheckedVarArgs,
6909                                        UncoveredArg, Offset);
6910         }
6911       }
6912 
6913       // For vprintf* functions (i.e., HasVAListArg==true), we add a
6914       // special check to see if the format string is a function parameter
6915       // of the function calling the printf function.  If the function
6916       // has an attribute indicating it is a printf-like function, then we
6917       // should suppress warnings concerning non-literals being used in a call
6918       // to a vprintf function.  For example:
6919       //
6920       // void
6921       // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){
6922       //      va_list ap;
6923       //      va_start(ap, fmt);
6924       //      vprintf(fmt, ap);  // Do NOT emit a warning about "fmt".
6925       //      ...
6926       // }
6927       if (HasVAListArg) {
6928         if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) {
6929           if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) {
6930             int PVIndex = PV->getFunctionScopeIndex() + 1;
6931             for (const auto *PVFormat : ND->specific_attrs<FormatAttr>()) {
6932               // adjust for implicit parameter
6933               if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND))
6934                 if (MD->isInstance())
6935                   ++PVIndex;
6936               // We also check if the formats are compatible.
6937               // We can't pass a 'scanf' string to a 'printf' function.
6938               if (PVIndex == PVFormat->getFormatIdx() &&
6939                   Type == S.GetFormatStringType(PVFormat))
6940                 return SLCT_UncheckedLiteral;
6941             }
6942           }
6943         }
6944       }
6945     }
6946 
6947     return SLCT_NotALiteral;
6948   }
6949 
6950   case Stmt::CallExprClass:
6951   case Stmt::CXXMemberCallExprClass: {
6952     const CallExpr *CE = cast<CallExpr>(E);
6953     if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {
6954       bool IsFirst = true;
6955       StringLiteralCheckType CommonResult;
6956       for (const auto *FA : ND->specific_attrs<FormatArgAttr>()) {
6957         const Expr *Arg = CE->getArg(FA->getFormatIdx().getASTIndex());
6958         StringLiteralCheckType Result = checkFormatStringExpr(
6959             S, Arg, Args, HasVAListArg, format_idx, firstDataArg, Type,
6960             CallType, InFunctionCall, CheckedVarArgs, UncoveredArg, Offset,
6961             IgnoreStringsWithoutSpecifiers);
6962         if (IsFirst) {
6963           CommonResult = Result;
6964           IsFirst = false;
6965         }
6966       }
6967       if (!IsFirst)
6968         return CommonResult;
6969 
6970       if (const auto *FD = dyn_cast<FunctionDecl>(ND)) {
6971         unsigned BuiltinID = FD->getBuiltinID();
6972         if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString ||
6973             BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) {
6974           const Expr *Arg = CE->getArg(0);
6975           return checkFormatStringExpr(S, Arg, Args,
6976                                        HasVAListArg, format_idx,
6977                                        firstDataArg, Type, CallType,
6978                                        InFunctionCall, CheckedVarArgs,
6979                                        UncoveredArg, Offset,
6980                                        IgnoreStringsWithoutSpecifiers);
6981         }
6982       }
6983     }
6984 
6985     return SLCT_NotALiteral;
6986   }
6987   case Stmt::ObjCMessageExprClass: {
6988     const auto *ME = cast<ObjCMessageExpr>(E);
6989     if (const auto *MD = ME->getMethodDecl()) {
6990       if (const auto *FA = MD->getAttr<FormatArgAttr>()) {
6991         // As a special case heuristic, if we're using the method -[NSBundle
6992         // localizedStringForKey:value:table:], ignore any key strings that lack
6993         // format specifiers. The idea is that if the key doesn't have any
6994         // format specifiers then its probably just a key to map to the
6995         // localized strings. If it does have format specifiers though, then its
6996         // likely that the text of the key is the format string in the
6997         // programmer's language, and should be checked.
6998         const ObjCInterfaceDecl *IFace;
6999         if (MD->isInstanceMethod() && (IFace = MD->getClassInterface()) &&
7000             IFace->getIdentifier()->isStr("NSBundle") &&
7001             MD->getSelector().isKeywordSelector(
7002                 {"localizedStringForKey", "value", "table"})) {
7003           IgnoreStringsWithoutSpecifiers = true;
7004         }
7005 
7006         const Expr *Arg = ME->getArg(FA->getFormatIdx().getASTIndex());
7007         return checkFormatStringExpr(
7008             S, Arg, Args, HasVAListArg, format_idx, firstDataArg, Type,
7009             CallType, InFunctionCall, CheckedVarArgs, UncoveredArg, Offset,
7010             IgnoreStringsWithoutSpecifiers);
7011       }
7012     }
7013 
7014     return SLCT_NotALiteral;
7015   }
7016   case Stmt::ObjCStringLiteralClass:
7017   case Stmt::StringLiteralClass: {
7018     const StringLiteral *StrE = nullptr;
7019 
7020     if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E))
7021       StrE = ObjCFExpr->getString();
7022     else
7023       StrE = cast<StringLiteral>(E);
7024 
7025     if (StrE) {
7026       if (Offset.isNegative() || Offset > StrE->getLength()) {
7027         // TODO: It would be better to have an explicit warning for out of
7028         // bounds literals.
7029         return SLCT_NotALiteral;
7030       }
7031       FormatStringLiteral FStr(StrE, Offset.sextOrTrunc(64).getSExtValue());
7032       CheckFormatString(S, &FStr, E, Args, HasVAListArg, format_idx,
7033                         firstDataArg, Type, InFunctionCall, CallType,
7034                         CheckedVarArgs, UncoveredArg,
7035                         IgnoreStringsWithoutSpecifiers);
7036       return SLCT_CheckedLiteral;
7037     }
7038 
7039     return SLCT_NotALiteral;
7040   }
7041   case Stmt::BinaryOperatorClass: {
7042     const BinaryOperator *BinOp = cast<BinaryOperator>(E);
7043 
7044     // A string literal + an int offset is still a string literal.
7045     if (BinOp->isAdditiveOp()) {
7046       Expr::EvalResult LResult, RResult;
7047 
7048       bool LIsInt = BinOp->getLHS()->EvaluateAsInt(
7049           LResult, S.Context, Expr::SE_NoSideEffects, S.isConstantEvaluated());
7050       bool RIsInt = BinOp->getRHS()->EvaluateAsInt(
7051           RResult, S.Context, Expr::SE_NoSideEffects, S.isConstantEvaluated());
7052 
7053       if (LIsInt != RIsInt) {
7054         BinaryOperatorKind BinOpKind = BinOp->getOpcode();
7055 
7056         if (LIsInt) {
7057           if (BinOpKind == BO_Add) {
7058             sumOffsets(Offset, LResult.Val.getInt(), BinOpKind, RIsInt);
7059             E = BinOp->getRHS();
7060             goto tryAgain;
7061           }
7062         } else {
7063           sumOffsets(Offset, RResult.Val.getInt(), BinOpKind, RIsInt);
7064           E = BinOp->getLHS();
7065           goto tryAgain;
7066         }
7067       }
7068     }
7069 
7070     return SLCT_NotALiteral;
7071   }
7072   case Stmt::UnaryOperatorClass: {
7073     const UnaryOperator *UnaOp = cast<UnaryOperator>(E);
7074     auto ASE = dyn_cast<ArraySubscriptExpr>(UnaOp->getSubExpr());
7075     if (UnaOp->getOpcode() == UO_AddrOf && ASE) {
7076       Expr::EvalResult IndexResult;
7077       if (ASE->getRHS()->EvaluateAsInt(IndexResult, S.Context,
7078                                        Expr::SE_NoSideEffects,
7079                                        S.isConstantEvaluated())) {
7080         sumOffsets(Offset, IndexResult.Val.getInt(), BO_Add,
7081                    /*RHS is int*/ true);
7082         E = ASE->getBase();
7083         goto tryAgain;
7084       }
7085     }
7086 
7087     return SLCT_NotALiteral;
7088   }
7089 
7090   default:
7091     return SLCT_NotALiteral;
7092   }
7093 }
7094 
7095 Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) {
7096   return llvm::StringSwitch<FormatStringType>(Format->getType()->getName())
7097       .Case("scanf", FST_Scanf)
7098       .Cases("printf", "printf0", FST_Printf)
7099       .Cases("NSString", "CFString", FST_NSString)
7100       .Case("strftime", FST_Strftime)
7101       .Case("strfmon", FST_Strfmon)
7102       .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf)
7103       .Case("freebsd_kprintf", FST_FreeBSDKPrintf)
7104       .Case("os_trace", FST_OSLog)
7105       .Case("os_log", FST_OSLog)
7106       .Default(FST_Unknown);
7107 }
7108 
7109 /// CheckFormatArguments - Check calls to printf and scanf (and similar
7110 /// functions) for correct use of format strings.
7111 /// Returns true if a format string has been fully checked.
7112 bool Sema::CheckFormatArguments(const FormatAttr *Format,
7113                                 ArrayRef<const Expr *> Args,
7114                                 bool IsCXXMember,
7115                                 VariadicCallType CallType,
7116                                 SourceLocation Loc, SourceRange Range,
7117                                 llvm::SmallBitVector &CheckedVarArgs) {
7118   FormatStringInfo FSI;
7119   if (getFormatStringInfo(Format, IsCXXMember, &FSI))
7120     return CheckFormatArguments(Args, FSI.HasVAListArg, FSI.FormatIdx,
7121                                 FSI.FirstDataArg, GetFormatStringType(Format),
7122                                 CallType, Loc, Range, CheckedVarArgs);
7123   return false;
7124 }
7125 
7126 bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args,
7127                                 bool HasVAListArg, unsigned format_idx,
7128                                 unsigned firstDataArg, FormatStringType Type,
7129                                 VariadicCallType CallType,
7130                                 SourceLocation Loc, SourceRange Range,
7131                                 llvm::SmallBitVector &CheckedVarArgs) {
7132   // CHECK: printf/scanf-like function is called with no format string.
7133   if (format_idx >= Args.size()) {
7134     Diag(Loc, diag::warn_missing_format_string) << Range;
7135     return false;
7136   }
7137 
7138   const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts();
7139 
7140   // CHECK: format string is not a string literal.
7141   //
7142   // Dynamically generated format strings are difficult to
7143   // automatically vet at compile time.  Requiring that format strings
7144   // are string literals: (1) permits the checking of format strings by
7145   // the compiler and thereby (2) can practically remove the source of
7146   // many format string exploits.
7147 
7148   // Format string can be either ObjC string (e.g. @"%d") or
7149   // C string (e.g. "%d")
7150   // ObjC string uses the same format specifiers as C string, so we can use
7151   // the same format string checking logic for both ObjC and C strings.
7152   UncoveredArgHandler UncoveredArg;
7153   StringLiteralCheckType CT =
7154       checkFormatStringExpr(*this, OrigFormatExpr, Args, HasVAListArg,
7155                             format_idx, firstDataArg, Type, CallType,
7156                             /*IsFunctionCall*/ true, CheckedVarArgs,
7157                             UncoveredArg,
7158                             /*no string offset*/ llvm::APSInt(64, false) = 0);
7159 
7160   // Generate a diagnostic where an uncovered argument is detected.
7161   if (UncoveredArg.hasUncoveredArg()) {
7162     unsigned ArgIdx = UncoveredArg.getUncoveredArg() + firstDataArg;
7163     assert(ArgIdx < Args.size() && "ArgIdx outside bounds");
7164     UncoveredArg.Diagnose(*this, /*IsFunctionCall*/true, Args[ArgIdx]);
7165   }
7166 
7167   if (CT != SLCT_NotALiteral)
7168     // Literal format string found, check done!
7169     return CT == SLCT_CheckedLiteral;
7170 
7171   // Strftime is particular as it always uses a single 'time' argument,
7172   // so it is safe to pass a non-literal string.
7173   if (Type == FST_Strftime)
7174     return false;
7175 
7176   // Do not emit diag when the string param is a macro expansion and the
7177   // format is either NSString or CFString. This is a hack to prevent
7178   // diag when using the NSLocalizedString and CFCopyLocalizedString macros
7179   // which are usually used in place of NS and CF string literals.
7180   SourceLocation FormatLoc = Args[format_idx]->getBeginLoc();
7181   if (Type == FST_NSString && SourceMgr.isInSystemMacro(FormatLoc))
7182     return false;
7183 
7184   // If there are no arguments specified, warn with -Wformat-security, otherwise
7185   // warn only with -Wformat-nonliteral.
7186   if (Args.size() == firstDataArg) {
7187     Diag(FormatLoc, diag::warn_format_nonliteral_noargs)
7188       << OrigFormatExpr->getSourceRange();
7189     switch (Type) {
7190     default:
7191       break;
7192     case FST_Kprintf:
7193     case FST_FreeBSDKPrintf:
7194     case FST_Printf:
7195       Diag(FormatLoc, diag::note_format_security_fixit)
7196         << FixItHint::CreateInsertion(FormatLoc, "\"%s\", ");
7197       break;
7198     case FST_NSString:
7199       Diag(FormatLoc, diag::note_format_security_fixit)
7200         << FixItHint::CreateInsertion(FormatLoc, "@\"%@\", ");
7201       break;
7202     }
7203   } else {
7204     Diag(FormatLoc, diag::warn_format_nonliteral)
7205       << OrigFormatExpr->getSourceRange();
7206   }
7207   return false;
7208 }
7209 
7210 namespace {
7211 
7212 class CheckFormatHandler : public analyze_format_string::FormatStringHandler {
7213 protected:
7214   Sema &S;
7215   const FormatStringLiteral *FExpr;
7216   const Expr *OrigFormatExpr;
7217   const Sema::FormatStringType FSType;
7218   const unsigned FirstDataArg;
7219   const unsigned NumDataArgs;
7220   const char *Beg; // Start of format string.
7221   const bool HasVAListArg;
7222   ArrayRef<const Expr *> Args;
7223   unsigned FormatIdx;
7224   llvm::SmallBitVector CoveredArgs;
7225   bool usesPositionalArgs = false;
7226   bool atFirstArg = true;
7227   bool inFunctionCall;
7228   Sema::VariadicCallType CallType;
7229   llvm::SmallBitVector &CheckedVarArgs;
7230   UncoveredArgHandler &UncoveredArg;
7231 
7232 public:
7233   CheckFormatHandler(Sema &s, const FormatStringLiteral *fexpr,
7234                      const Expr *origFormatExpr,
7235                      const Sema::FormatStringType type, unsigned firstDataArg,
7236                      unsigned numDataArgs, const char *beg, bool hasVAListArg,
7237                      ArrayRef<const Expr *> Args, unsigned formatIdx,
7238                      bool inFunctionCall, Sema::VariadicCallType callType,
7239                      llvm::SmallBitVector &CheckedVarArgs,
7240                      UncoveredArgHandler &UncoveredArg)
7241       : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), FSType(type),
7242         FirstDataArg(firstDataArg), NumDataArgs(numDataArgs), Beg(beg),
7243         HasVAListArg(hasVAListArg), Args(Args), FormatIdx(formatIdx),
7244         inFunctionCall(inFunctionCall), CallType(callType),
7245         CheckedVarArgs(CheckedVarArgs), UncoveredArg(UncoveredArg) {
7246     CoveredArgs.resize(numDataArgs);
7247     CoveredArgs.reset();
7248   }
7249 
7250   void DoneProcessing();
7251 
7252   void HandleIncompleteSpecifier(const char *startSpecifier,
7253                                  unsigned specifierLen) override;
7254 
7255   void HandleInvalidLengthModifier(
7256                            const analyze_format_string::FormatSpecifier &FS,
7257                            const analyze_format_string::ConversionSpecifier &CS,
7258                            const char *startSpecifier, unsigned specifierLen,
7259                            unsigned DiagID);
7260 
7261   void HandleNonStandardLengthModifier(
7262                     const analyze_format_string::FormatSpecifier &FS,
7263                     const char *startSpecifier, unsigned specifierLen);
7264 
7265   void HandleNonStandardConversionSpecifier(
7266                     const analyze_format_string::ConversionSpecifier &CS,
7267                     const char *startSpecifier, unsigned specifierLen);
7268 
7269   void HandlePosition(const char *startPos, unsigned posLen) override;
7270 
7271   void HandleInvalidPosition(const char *startSpecifier,
7272                              unsigned specifierLen,
7273                              analyze_format_string::PositionContext p) override;
7274 
7275   void HandleZeroPosition(const char *startPos, unsigned posLen) override;
7276 
7277   void HandleNullChar(const char *nullCharacter) override;
7278 
7279   template <typename Range>
7280   static void
7281   EmitFormatDiagnostic(Sema &S, bool inFunctionCall, const Expr *ArgumentExpr,
7282                        const PartialDiagnostic &PDiag, SourceLocation StringLoc,
7283                        bool IsStringLocation, Range StringRange,
7284                        ArrayRef<FixItHint> Fixit = None);
7285 
7286 protected:
7287   bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc,
7288                                         const char *startSpec,
7289                                         unsigned specifierLen,
7290                                         const char *csStart, unsigned csLen);
7291 
7292   void HandlePositionalNonpositionalArgs(SourceLocation Loc,
7293                                          const char *startSpec,
7294                                          unsigned specifierLen);
7295 
7296   SourceRange getFormatStringRange();
7297   CharSourceRange getSpecifierRange(const char *startSpecifier,
7298                                     unsigned specifierLen);
7299   SourceLocation getLocationOfByte(const char *x);
7300 
7301   const Expr *getDataArg(unsigned i) const;
7302 
7303   bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS,
7304                     const analyze_format_string::ConversionSpecifier &CS,
7305                     const char *startSpecifier, unsigned specifierLen,
7306                     unsigned argIndex);
7307 
7308   template <typename Range>
7309   void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc,
7310                             bool IsStringLocation, Range StringRange,
7311                             ArrayRef<FixItHint> Fixit = None);
7312 };
7313 
7314 } // namespace
7315 
7316 SourceRange CheckFormatHandler::getFormatStringRange() {
7317   return OrigFormatExpr->getSourceRange();
7318 }
7319 
7320 CharSourceRange CheckFormatHandler::
7321 getSpecifierRange(const char *startSpecifier, unsigned specifierLen) {
7322   SourceLocation Start = getLocationOfByte(startSpecifier);
7323   SourceLocation End   = getLocationOfByte(startSpecifier + specifierLen - 1);
7324 
7325   // Advance the end SourceLocation by one due to half-open ranges.
7326   End = End.getLocWithOffset(1);
7327 
7328   return CharSourceRange::getCharRange(Start, End);
7329 }
7330 
7331 SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) {
7332   return FExpr->getLocationOfByte(x - Beg, S.getSourceManager(),
7333                                   S.getLangOpts(), S.Context.getTargetInfo());
7334 }
7335 
7336 void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier,
7337                                                    unsigned specifierLen){
7338   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier),
7339                        getLocationOfByte(startSpecifier),
7340                        /*IsStringLocation*/true,
7341                        getSpecifierRange(startSpecifier, specifierLen));
7342 }
7343 
7344 void CheckFormatHandler::HandleInvalidLengthModifier(
7345     const analyze_format_string::FormatSpecifier &FS,
7346     const analyze_format_string::ConversionSpecifier &CS,
7347     const char *startSpecifier, unsigned specifierLen, unsigned DiagID) {
7348   using namespace analyze_format_string;
7349 
7350   const LengthModifier &LM = FS.getLengthModifier();
7351   CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength());
7352 
7353   // See if we know how to fix this length modifier.
7354   Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier();
7355   if (FixedLM) {
7356     EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(),
7357                          getLocationOfByte(LM.getStart()),
7358                          /*IsStringLocation*/true,
7359                          getSpecifierRange(startSpecifier, specifierLen));
7360 
7361     S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier)
7362       << FixedLM->toString()
7363       << FixItHint::CreateReplacement(LMRange, FixedLM->toString());
7364 
7365   } else {
7366     FixItHint Hint;
7367     if (DiagID == diag::warn_format_nonsensical_length)
7368       Hint = FixItHint::CreateRemoval(LMRange);
7369 
7370     EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(),
7371                          getLocationOfByte(LM.getStart()),
7372                          /*IsStringLocation*/true,
7373                          getSpecifierRange(startSpecifier, specifierLen),
7374                          Hint);
7375   }
7376 }
7377 
7378 void CheckFormatHandler::HandleNonStandardLengthModifier(
7379     const analyze_format_string::FormatSpecifier &FS,
7380     const char *startSpecifier, unsigned specifierLen) {
7381   using namespace analyze_format_string;
7382 
7383   const LengthModifier &LM = FS.getLengthModifier();
7384   CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength());
7385 
7386   // See if we know how to fix this length modifier.
7387   Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier();
7388   if (FixedLM) {
7389     EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
7390                            << LM.toString() << 0,
7391                          getLocationOfByte(LM.getStart()),
7392                          /*IsStringLocation*/true,
7393                          getSpecifierRange(startSpecifier, specifierLen));
7394 
7395     S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier)
7396       << FixedLM->toString()
7397       << FixItHint::CreateReplacement(LMRange, FixedLM->toString());
7398 
7399   } else {
7400     EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
7401                            << LM.toString() << 0,
7402                          getLocationOfByte(LM.getStart()),
7403                          /*IsStringLocation*/true,
7404                          getSpecifierRange(startSpecifier, specifierLen));
7405   }
7406 }
7407 
7408 void CheckFormatHandler::HandleNonStandardConversionSpecifier(
7409     const analyze_format_string::ConversionSpecifier &CS,
7410     const char *startSpecifier, unsigned specifierLen) {
7411   using namespace analyze_format_string;
7412 
7413   // See if we know how to fix this conversion specifier.
7414   Optional<ConversionSpecifier> FixedCS = CS.getStandardSpecifier();
7415   if (FixedCS) {
7416     EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
7417                           << CS.toString() << /*conversion specifier*/1,
7418                          getLocationOfByte(CS.getStart()),
7419                          /*IsStringLocation*/true,
7420                          getSpecifierRange(startSpecifier, specifierLen));
7421 
7422     CharSourceRange CSRange = getSpecifierRange(CS.getStart(), CS.getLength());
7423     S.Diag(getLocationOfByte(CS.getStart()), diag::note_format_fix_specifier)
7424       << FixedCS->toString()
7425       << FixItHint::CreateReplacement(CSRange, FixedCS->toString());
7426   } else {
7427     EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
7428                           << CS.toString() << /*conversion specifier*/1,
7429                          getLocationOfByte(CS.getStart()),
7430                          /*IsStringLocation*/true,
7431                          getSpecifierRange(startSpecifier, specifierLen));
7432   }
7433 }
7434 
7435 void CheckFormatHandler::HandlePosition(const char *startPos,
7436                                         unsigned posLen) {
7437   EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg),
7438                                getLocationOfByte(startPos),
7439                                /*IsStringLocation*/true,
7440                                getSpecifierRange(startPos, posLen));
7441 }
7442 
7443 void
7444 CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen,
7445                                      analyze_format_string::PositionContext p) {
7446   EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier)
7447                          << (unsigned) p,
7448                        getLocationOfByte(startPos), /*IsStringLocation*/true,
7449                        getSpecifierRange(startPos, posLen));
7450 }
7451 
7452 void CheckFormatHandler::HandleZeroPosition(const char *startPos,
7453                                             unsigned posLen) {
7454   EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier),
7455                                getLocationOfByte(startPos),
7456                                /*IsStringLocation*/true,
7457                                getSpecifierRange(startPos, posLen));
7458 }
7459 
7460 void CheckFormatHandler::HandleNullChar(const char *nullCharacter) {
7461   if (!isa<ObjCStringLiteral>(OrigFormatExpr)) {
7462     // The presence of a null character is likely an error.
7463     EmitFormatDiagnostic(
7464       S.PDiag(diag::warn_printf_format_string_contains_null_char),
7465       getLocationOfByte(nullCharacter), /*IsStringLocation*/true,
7466       getFormatStringRange());
7467   }
7468 }
7469 
7470 // Note that this may return NULL if there was an error parsing or building
7471 // one of the argument expressions.
7472 const Expr *CheckFormatHandler::getDataArg(unsigned i) const {
7473   return Args[FirstDataArg + i];
7474 }
7475 
7476 void CheckFormatHandler::DoneProcessing() {
7477   // Does the number of data arguments exceed the number of
7478   // format conversions in the format string?
7479   if (!HasVAListArg) {
7480       // Find any arguments that weren't covered.
7481     CoveredArgs.flip();
7482     signed notCoveredArg = CoveredArgs.find_first();
7483     if (notCoveredArg >= 0) {
7484       assert((unsigned)notCoveredArg < NumDataArgs);
7485       UncoveredArg.Update(notCoveredArg, OrigFormatExpr);
7486     } else {
7487       UncoveredArg.setAllCovered();
7488     }
7489   }
7490 }
7491 
7492 void UncoveredArgHandler::Diagnose(Sema &S, bool IsFunctionCall,
7493                                    const Expr *ArgExpr) {
7494   assert(hasUncoveredArg() && DiagnosticExprs.size() > 0 &&
7495          "Invalid state");
7496 
7497   if (!ArgExpr)
7498     return;
7499 
7500   SourceLocation Loc = ArgExpr->getBeginLoc();
7501 
7502   if (S.getSourceManager().isInSystemMacro(Loc))
7503     return;
7504 
7505   PartialDiagnostic PDiag = S.PDiag(diag::warn_printf_data_arg_not_used);
7506   for (auto E : DiagnosticExprs)
7507     PDiag << E->getSourceRange();
7508 
7509   CheckFormatHandler::EmitFormatDiagnostic(
7510                                   S, IsFunctionCall, DiagnosticExprs[0],
7511                                   PDiag, Loc, /*IsStringLocation*/false,
7512                                   DiagnosticExprs[0]->getSourceRange());
7513 }
7514 
7515 bool
7516 CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex,
7517                                                      SourceLocation Loc,
7518                                                      const char *startSpec,
7519                                                      unsigned specifierLen,
7520                                                      const char *csStart,
7521                                                      unsigned csLen) {
7522   bool keepGoing = true;
7523   if (argIndex < NumDataArgs) {
7524     // Consider the argument coverered, even though the specifier doesn't
7525     // make sense.
7526     CoveredArgs.set(argIndex);
7527   }
7528   else {
7529     // If argIndex exceeds the number of data arguments we
7530     // don't issue a warning because that is just a cascade of warnings (and
7531     // they may have intended '%%' anyway). We don't want to continue processing
7532     // the format string after this point, however, as we will like just get
7533     // gibberish when trying to match arguments.
7534     keepGoing = false;
7535   }
7536 
7537   StringRef Specifier(csStart, csLen);
7538 
7539   // If the specifier in non-printable, it could be the first byte of a UTF-8
7540   // sequence. In that case, print the UTF-8 code point. If not, print the byte
7541   // hex value.
7542   std::string CodePointStr;
7543   if (!llvm::sys::locale::isPrint(*csStart)) {
7544     llvm::UTF32 CodePoint;
7545     const llvm::UTF8 **B = reinterpret_cast<const llvm::UTF8 **>(&csStart);
7546     const llvm::UTF8 *E =
7547         reinterpret_cast<const llvm::UTF8 *>(csStart + csLen);
7548     llvm::ConversionResult Result =
7549         llvm::convertUTF8Sequence(B, E, &CodePoint, llvm::strictConversion);
7550 
7551     if (Result != llvm::conversionOK) {
7552       unsigned char FirstChar = *csStart;
7553       CodePoint = (llvm::UTF32)FirstChar;
7554     }
7555 
7556     llvm::raw_string_ostream OS(CodePointStr);
7557     if (CodePoint < 256)
7558       OS << "\\x" << llvm::format("%02x", CodePoint);
7559     else if (CodePoint <= 0xFFFF)
7560       OS << "\\u" << llvm::format("%04x", CodePoint);
7561     else
7562       OS << "\\U" << llvm::format("%08x", CodePoint);
7563     OS.flush();
7564     Specifier = CodePointStr;
7565   }
7566 
7567   EmitFormatDiagnostic(
7568       S.PDiag(diag::warn_format_invalid_conversion) << Specifier, Loc,
7569       /*IsStringLocation*/ true, getSpecifierRange(startSpec, specifierLen));
7570 
7571   return keepGoing;
7572 }
7573 
7574 void
7575 CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc,
7576                                                       const char *startSpec,
7577                                                       unsigned specifierLen) {
7578   EmitFormatDiagnostic(
7579     S.PDiag(diag::warn_format_mix_positional_nonpositional_args),
7580     Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen));
7581 }
7582 
7583 bool
7584 CheckFormatHandler::CheckNumArgs(
7585   const analyze_format_string::FormatSpecifier &FS,
7586   const analyze_format_string::ConversionSpecifier &CS,
7587   const char *startSpecifier, unsigned specifierLen, unsigned argIndex) {
7588 
7589   if (argIndex >= NumDataArgs) {
7590     PartialDiagnostic PDiag = FS.usesPositionalArg()
7591       ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args)
7592            << (argIndex+1) << NumDataArgs)
7593       : S.PDiag(diag::warn_printf_insufficient_data_args);
7594     EmitFormatDiagnostic(
7595       PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true,
7596       getSpecifierRange(startSpecifier, specifierLen));
7597 
7598     // Since more arguments than conversion tokens are given, by extension
7599     // all arguments are covered, so mark this as so.
7600     UncoveredArg.setAllCovered();
7601     return false;
7602   }
7603   return true;
7604 }
7605 
7606 template<typename Range>
7607 void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag,
7608                                               SourceLocation Loc,
7609                                               bool IsStringLocation,
7610                                               Range StringRange,
7611                                               ArrayRef<FixItHint> FixIt) {
7612   EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag,
7613                        Loc, IsStringLocation, StringRange, FixIt);
7614 }
7615 
7616 /// If the format string is not within the function call, emit a note
7617 /// so that the function call and string are in diagnostic messages.
7618 ///
7619 /// \param InFunctionCall if true, the format string is within the function
7620 /// call and only one diagnostic message will be produced.  Otherwise, an
7621 /// extra note will be emitted pointing to location of the format string.
7622 ///
7623 /// \param ArgumentExpr the expression that is passed as the format string
7624 /// argument in the function call.  Used for getting locations when two
7625 /// diagnostics are emitted.
7626 ///
7627 /// \param PDiag the callee should already have provided any strings for the
7628 /// diagnostic message.  This function only adds locations and fixits
7629 /// to diagnostics.
7630 ///
7631 /// \param Loc primary location for diagnostic.  If two diagnostics are
7632 /// required, one will be at Loc and a new SourceLocation will be created for
7633 /// the other one.
7634 ///
7635 /// \param IsStringLocation if true, Loc points to the format string should be
7636 /// used for the note.  Otherwise, Loc points to the argument list and will
7637 /// be used with PDiag.
7638 ///
7639 /// \param StringRange some or all of the string to highlight.  This is
7640 /// templated so it can accept either a CharSourceRange or a SourceRange.
7641 ///
7642 /// \param FixIt optional fix it hint for the format string.
7643 template <typename Range>
7644 void CheckFormatHandler::EmitFormatDiagnostic(
7645     Sema &S, bool InFunctionCall, const Expr *ArgumentExpr,
7646     const PartialDiagnostic &PDiag, SourceLocation Loc, bool IsStringLocation,
7647     Range StringRange, ArrayRef<FixItHint> FixIt) {
7648   if (InFunctionCall) {
7649     const Sema::SemaDiagnosticBuilder &D = S.Diag(Loc, PDiag);
7650     D << StringRange;
7651     D << FixIt;
7652   } else {
7653     S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag)
7654       << ArgumentExpr->getSourceRange();
7655 
7656     const Sema::SemaDiagnosticBuilder &Note =
7657       S.Diag(IsStringLocation ? Loc : StringRange.getBegin(),
7658              diag::note_format_string_defined);
7659 
7660     Note << StringRange;
7661     Note << FixIt;
7662   }
7663 }
7664 
7665 //===--- CHECK: Printf format string checking ------------------------------===//
7666 
7667 namespace {
7668 
7669 class CheckPrintfHandler : public CheckFormatHandler {
7670 public:
7671   CheckPrintfHandler(Sema &s, const FormatStringLiteral *fexpr,
7672                      const Expr *origFormatExpr,
7673                      const Sema::FormatStringType type, unsigned firstDataArg,
7674                      unsigned numDataArgs, bool isObjC, const char *beg,
7675                      bool hasVAListArg, ArrayRef<const Expr *> Args,
7676                      unsigned formatIdx, bool inFunctionCall,
7677                      Sema::VariadicCallType CallType,
7678                      llvm::SmallBitVector &CheckedVarArgs,
7679                      UncoveredArgHandler &UncoveredArg)
7680       : CheckFormatHandler(s, fexpr, origFormatExpr, type, firstDataArg,
7681                            numDataArgs, beg, hasVAListArg, Args, formatIdx,
7682                            inFunctionCall, CallType, CheckedVarArgs,
7683                            UncoveredArg) {}
7684 
7685   bool isObjCContext() const { return FSType == Sema::FST_NSString; }
7686 
7687   /// Returns true if '%@' specifiers are allowed in the format string.
7688   bool allowsObjCArg() const {
7689     return FSType == Sema::FST_NSString || FSType == Sema::FST_OSLog ||
7690            FSType == Sema::FST_OSTrace;
7691   }
7692 
7693   bool HandleInvalidPrintfConversionSpecifier(
7694                                       const analyze_printf::PrintfSpecifier &FS,
7695                                       const char *startSpecifier,
7696                                       unsigned specifierLen) override;
7697 
7698   void handleInvalidMaskType(StringRef MaskType) override;
7699 
7700   bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS,
7701                              const char *startSpecifier,
7702                              unsigned specifierLen) override;
7703   bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
7704                        const char *StartSpecifier,
7705                        unsigned SpecifierLen,
7706                        const Expr *E);
7707 
7708   bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k,
7709                     const char *startSpecifier, unsigned specifierLen);
7710   void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS,
7711                            const analyze_printf::OptionalAmount &Amt,
7712                            unsigned type,
7713                            const char *startSpecifier, unsigned specifierLen);
7714   void HandleFlag(const analyze_printf::PrintfSpecifier &FS,
7715                   const analyze_printf::OptionalFlag &flag,
7716                   const char *startSpecifier, unsigned specifierLen);
7717   void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS,
7718                          const analyze_printf::OptionalFlag &ignoredFlag,
7719                          const analyze_printf::OptionalFlag &flag,
7720                          const char *startSpecifier, unsigned specifierLen);
7721   bool checkForCStrMembers(const analyze_printf::ArgType &AT,
7722                            const Expr *E);
7723 
7724   void HandleEmptyObjCModifierFlag(const char *startFlag,
7725                                    unsigned flagLen) override;
7726 
7727   void HandleInvalidObjCModifierFlag(const char *startFlag,
7728                                             unsigned flagLen) override;
7729 
7730   void HandleObjCFlagsWithNonObjCConversion(const char *flagsStart,
7731                                            const char *flagsEnd,
7732                                            const char *conversionPosition)
7733                                              override;
7734 };
7735 
7736 } // namespace
7737 
7738 bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier(
7739                                       const analyze_printf::PrintfSpecifier &FS,
7740                                       const char *startSpecifier,
7741                                       unsigned specifierLen) {
7742   const analyze_printf::PrintfConversionSpecifier &CS =
7743     FS.getConversionSpecifier();
7744 
7745   return HandleInvalidConversionSpecifier(FS.getArgIndex(),
7746                                           getLocationOfByte(CS.getStart()),
7747                                           startSpecifier, specifierLen,
7748                                           CS.getStart(), CS.getLength());
7749 }
7750 
7751 void CheckPrintfHandler::handleInvalidMaskType(StringRef MaskType) {
7752   S.Diag(getLocationOfByte(MaskType.data()), diag::err_invalid_mask_type_size);
7753 }
7754 
7755 bool CheckPrintfHandler::HandleAmount(
7756                                const analyze_format_string::OptionalAmount &Amt,
7757                                unsigned k, const char *startSpecifier,
7758                                unsigned specifierLen) {
7759   if (Amt.hasDataArgument()) {
7760     if (!HasVAListArg) {
7761       unsigned argIndex = Amt.getArgIndex();
7762       if (argIndex >= NumDataArgs) {
7763         EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg)
7764                                << k,
7765                              getLocationOfByte(Amt.getStart()),
7766                              /*IsStringLocation*/true,
7767                              getSpecifierRange(startSpecifier, specifierLen));
7768         // Don't do any more checking.  We will just emit
7769         // spurious errors.
7770         return false;
7771       }
7772 
7773       // Type check the data argument.  It should be an 'int'.
7774       // Although not in conformance with C99, we also allow the argument to be
7775       // an 'unsigned int' as that is a reasonably safe case.  GCC also
7776       // doesn't emit a warning for that case.
7777       CoveredArgs.set(argIndex);
7778       const Expr *Arg = getDataArg(argIndex);
7779       if (!Arg)
7780         return false;
7781 
7782       QualType T = Arg->getType();
7783 
7784       const analyze_printf::ArgType &AT = Amt.getArgType(S.Context);
7785       assert(AT.isValid());
7786 
7787       if (!AT.matchesType(S.Context, T)) {
7788         EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type)
7789                                << k << AT.getRepresentativeTypeName(S.Context)
7790                                << T << Arg->getSourceRange(),
7791                              getLocationOfByte(Amt.getStart()),
7792                              /*IsStringLocation*/true,
7793                              getSpecifierRange(startSpecifier, specifierLen));
7794         // Don't do any more checking.  We will just emit
7795         // spurious errors.
7796         return false;
7797       }
7798     }
7799   }
7800   return true;
7801 }
7802 
7803 void CheckPrintfHandler::HandleInvalidAmount(
7804                                       const analyze_printf::PrintfSpecifier &FS,
7805                                       const analyze_printf::OptionalAmount &Amt,
7806                                       unsigned type,
7807                                       const char *startSpecifier,
7808                                       unsigned specifierLen) {
7809   const analyze_printf::PrintfConversionSpecifier &CS =
7810     FS.getConversionSpecifier();
7811 
7812   FixItHint fixit =
7813     Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant
7814       ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(),
7815                                  Amt.getConstantLength()))
7816       : FixItHint();
7817 
7818   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount)
7819                          << type << CS.toString(),
7820                        getLocationOfByte(Amt.getStart()),
7821                        /*IsStringLocation*/true,
7822                        getSpecifierRange(startSpecifier, specifierLen),
7823                        fixit);
7824 }
7825 
7826 void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS,
7827                                     const analyze_printf::OptionalFlag &flag,
7828                                     const char *startSpecifier,
7829                                     unsigned specifierLen) {
7830   // Warn about pointless flag with a fixit removal.
7831   const analyze_printf::PrintfConversionSpecifier &CS =
7832     FS.getConversionSpecifier();
7833   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag)
7834                          << flag.toString() << CS.toString(),
7835                        getLocationOfByte(flag.getPosition()),
7836                        /*IsStringLocation*/true,
7837                        getSpecifierRange(startSpecifier, specifierLen),
7838                        FixItHint::CreateRemoval(
7839                          getSpecifierRange(flag.getPosition(), 1)));
7840 }
7841 
7842 void CheckPrintfHandler::HandleIgnoredFlag(
7843                                 const analyze_printf::PrintfSpecifier &FS,
7844                                 const analyze_printf::OptionalFlag &ignoredFlag,
7845                                 const analyze_printf::OptionalFlag &flag,
7846                                 const char *startSpecifier,
7847                                 unsigned specifierLen) {
7848   // Warn about ignored flag with a fixit removal.
7849   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag)
7850                          << ignoredFlag.toString() << flag.toString(),
7851                        getLocationOfByte(ignoredFlag.getPosition()),
7852                        /*IsStringLocation*/true,
7853                        getSpecifierRange(startSpecifier, specifierLen),
7854                        FixItHint::CreateRemoval(
7855                          getSpecifierRange(ignoredFlag.getPosition(), 1)));
7856 }
7857 
7858 void CheckPrintfHandler::HandleEmptyObjCModifierFlag(const char *startFlag,
7859                                                      unsigned flagLen) {
7860   // Warn about an empty flag.
7861   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_empty_objc_flag),
7862                        getLocationOfByte(startFlag),
7863                        /*IsStringLocation*/true,
7864                        getSpecifierRange(startFlag, flagLen));
7865 }
7866 
7867 void CheckPrintfHandler::HandleInvalidObjCModifierFlag(const char *startFlag,
7868                                                        unsigned flagLen) {
7869   // Warn about an invalid flag.
7870   auto Range = getSpecifierRange(startFlag, flagLen);
7871   StringRef flag(startFlag, flagLen);
7872   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_invalid_objc_flag) << flag,
7873                       getLocationOfByte(startFlag),
7874                       /*IsStringLocation*/true,
7875                       Range, FixItHint::CreateRemoval(Range));
7876 }
7877 
7878 void CheckPrintfHandler::HandleObjCFlagsWithNonObjCConversion(
7879     const char *flagsStart, const char *flagsEnd, const char *conversionPosition) {
7880     // Warn about using '[...]' without a '@' conversion.
7881     auto Range = getSpecifierRange(flagsStart, flagsEnd - flagsStart + 1);
7882     auto diag = diag::warn_printf_ObjCflags_without_ObjCConversion;
7883     EmitFormatDiagnostic(S.PDiag(diag) << StringRef(conversionPosition, 1),
7884                          getLocationOfByte(conversionPosition),
7885                          /*IsStringLocation*/true,
7886                          Range, FixItHint::CreateRemoval(Range));
7887 }
7888 
7889 // Determines if the specified is a C++ class or struct containing
7890 // a member with the specified name and kind (e.g. a CXXMethodDecl named
7891 // "c_str()").
7892 template<typename MemberKind>
7893 static llvm::SmallPtrSet<MemberKind*, 1>
7894 CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) {
7895   const RecordType *RT = Ty->getAs<RecordType>();
7896   llvm::SmallPtrSet<MemberKind*, 1> Results;
7897 
7898   if (!RT)
7899     return Results;
7900   const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
7901   if (!RD || !RD->getDefinition())
7902     return Results;
7903 
7904   LookupResult R(S, &S.Context.Idents.get(Name), SourceLocation(),
7905                  Sema::LookupMemberName);
7906   R.suppressDiagnostics();
7907 
7908   // We just need to include all members of the right kind turned up by the
7909   // filter, at this point.
7910   if (S.LookupQualifiedName(R, RT->getDecl()))
7911     for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
7912       NamedDecl *decl = (*I)->getUnderlyingDecl();
7913       if (MemberKind *FK = dyn_cast<MemberKind>(decl))
7914         Results.insert(FK);
7915     }
7916   return Results;
7917 }
7918 
7919 /// Check if we could call '.c_str()' on an object.
7920 ///
7921 /// FIXME: This returns the wrong results in some cases (if cv-qualifiers don't
7922 /// allow the call, or if it would be ambiguous).
7923 bool Sema::hasCStrMethod(const Expr *E) {
7924   using MethodSet = llvm::SmallPtrSet<CXXMethodDecl *, 1>;
7925 
7926   MethodSet Results =
7927       CXXRecordMembersNamed<CXXMethodDecl>("c_str", *this, E->getType());
7928   for (MethodSet::iterator MI = Results.begin(), ME = Results.end();
7929        MI != ME; ++MI)
7930     if ((*MI)->getMinRequiredArguments() == 0)
7931       return true;
7932   return false;
7933 }
7934 
7935 // Check if a (w)string was passed when a (w)char* was needed, and offer a
7936 // better diagnostic if so. AT is assumed to be valid.
7937 // Returns true when a c_str() conversion method is found.
7938 bool CheckPrintfHandler::checkForCStrMembers(
7939     const analyze_printf::ArgType &AT, const Expr *E) {
7940   using MethodSet = llvm::SmallPtrSet<CXXMethodDecl *, 1>;
7941 
7942   MethodSet Results =
7943       CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType());
7944 
7945   for (MethodSet::iterator MI = Results.begin(), ME = Results.end();
7946        MI != ME; ++MI) {
7947     const CXXMethodDecl *Method = *MI;
7948     if (Method->getMinRequiredArguments() == 0 &&
7949         AT.matchesType(S.Context, Method->getReturnType())) {
7950       // FIXME: Suggest parens if the expression needs them.
7951       SourceLocation EndLoc = S.getLocForEndOfToken(E->getEndLoc());
7952       S.Diag(E->getBeginLoc(), diag::note_printf_c_str)
7953           << "c_str()" << FixItHint::CreateInsertion(EndLoc, ".c_str()");
7954       return true;
7955     }
7956   }
7957 
7958   return false;
7959 }
7960 
7961 bool
7962 CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier
7963                                             &FS,
7964                                           const char *startSpecifier,
7965                                           unsigned specifierLen) {
7966   using namespace analyze_format_string;
7967   using namespace analyze_printf;
7968 
7969   const PrintfConversionSpecifier &CS = FS.getConversionSpecifier();
7970 
7971   if (FS.consumesDataArgument()) {
7972     if (atFirstArg) {
7973         atFirstArg = false;
7974         usesPositionalArgs = FS.usesPositionalArg();
7975     }
7976     else if (usesPositionalArgs != FS.usesPositionalArg()) {
7977       HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()),
7978                                         startSpecifier, specifierLen);
7979       return false;
7980     }
7981   }
7982 
7983   // First check if the field width, precision, and conversion specifier
7984   // have matching data arguments.
7985   if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0,
7986                     startSpecifier, specifierLen)) {
7987     return false;
7988   }
7989 
7990   if (!HandleAmount(FS.getPrecision(), /* precision */ 1,
7991                     startSpecifier, specifierLen)) {
7992     return false;
7993   }
7994 
7995   if (!CS.consumesDataArgument()) {
7996     // FIXME: Technically specifying a precision or field width here
7997     // makes no sense.  Worth issuing a warning at some point.
7998     return true;
7999   }
8000 
8001   // Consume the argument.
8002   unsigned argIndex = FS.getArgIndex();
8003   if (argIndex < NumDataArgs) {
8004     // The check to see if the argIndex is valid will come later.
8005     // We set the bit here because we may exit early from this
8006     // function if we encounter some other error.
8007     CoveredArgs.set(argIndex);
8008   }
8009 
8010   // FreeBSD kernel extensions.
8011   if (CS.getKind() == ConversionSpecifier::FreeBSDbArg ||
8012       CS.getKind() == ConversionSpecifier::FreeBSDDArg) {
8013     // We need at least two arguments.
8014     if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex + 1))
8015       return false;
8016 
8017     // Claim the second argument.
8018     CoveredArgs.set(argIndex + 1);
8019 
8020     // Type check the first argument (int for %b, pointer for %D)
8021     const Expr *Ex = getDataArg(argIndex);
8022     const analyze_printf::ArgType &AT =
8023       (CS.getKind() == ConversionSpecifier::FreeBSDbArg) ?
8024         ArgType(S.Context.IntTy) : ArgType::CPointerTy;
8025     if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType()))
8026       EmitFormatDiagnostic(
8027           S.PDiag(diag::warn_format_conversion_argument_type_mismatch)
8028               << AT.getRepresentativeTypeName(S.Context) << Ex->getType()
8029               << false << Ex->getSourceRange(),
8030           Ex->getBeginLoc(), /*IsStringLocation*/ false,
8031           getSpecifierRange(startSpecifier, specifierLen));
8032 
8033     // Type check the second argument (char * for both %b and %D)
8034     Ex = getDataArg(argIndex + 1);
8035     const analyze_printf::ArgType &AT2 = ArgType::CStrTy;
8036     if (AT2.isValid() && !AT2.matchesType(S.Context, Ex->getType()))
8037       EmitFormatDiagnostic(
8038           S.PDiag(diag::warn_format_conversion_argument_type_mismatch)
8039               << AT2.getRepresentativeTypeName(S.Context) << Ex->getType()
8040               << false << Ex->getSourceRange(),
8041           Ex->getBeginLoc(), /*IsStringLocation*/ false,
8042           getSpecifierRange(startSpecifier, specifierLen));
8043 
8044      return true;
8045   }
8046 
8047   // Check for using an Objective-C specific conversion specifier
8048   // in a non-ObjC literal.
8049   if (!allowsObjCArg() && CS.isObjCArg()) {
8050     return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier,
8051                                                   specifierLen);
8052   }
8053 
8054   // %P can only be used with os_log.
8055   if (FSType != Sema::FST_OSLog && CS.getKind() == ConversionSpecifier::PArg) {
8056     return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier,
8057                                                   specifierLen);
8058   }
8059 
8060   // %n is not allowed with os_log.
8061   if (FSType == Sema::FST_OSLog && CS.getKind() == ConversionSpecifier::nArg) {
8062     EmitFormatDiagnostic(S.PDiag(diag::warn_os_log_format_narg),
8063                          getLocationOfByte(CS.getStart()),
8064                          /*IsStringLocation*/ false,
8065                          getSpecifierRange(startSpecifier, specifierLen));
8066 
8067     return true;
8068   }
8069 
8070   // Only scalars are allowed for os_trace.
8071   if (FSType == Sema::FST_OSTrace &&
8072       (CS.getKind() == ConversionSpecifier::PArg ||
8073        CS.getKind() == ConversionSpecifier::sArg ||
8074        CS.getKind() == ConversionSpecifier::ObjCObjArg)) {
8075     return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier,
8076                                                   specifierLen);
8077   }
8078 
8079   // Check for use of public/private annotation outside of os_log().
8080   if (FSType != Sema::FST_OSLog) {
8081     if (FS.isPublic().isSet()) {
8082       EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_annotation)
8083                                << "public",
8084                            getLocationOfByte(FS.isPublic().getPosition()),
8085                            /*IsStringLocation*/ false,
8086                            getSpecifierRange(startSpecifier, specifierLen));
8087     }
8088     if (FS.isPrivate().isSet()) {
8089       EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_annotation)
8090                                << "private",
8091                            getLocationOfByte(FS.isPrivate().getPosition()),
8092                            /*IsStringLocation*/ false,
8093                            getSpecifierRange(startSpecifier, specifierLen));
8094     }
8095   }
8096 
8097   // Check for invalid use of field width
8098   if (!FS.hasValidFieldWidth()) {
8099     HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0,
8100         startSpecifier, specifierLen);
8101   }
8102 
8103   // Check for invalid use of precision
8104   if (!FS.hasValidPrecision()) {
8105     HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1,
8106         startSpecifier, specifierLen);
8107   }
8108 
8109   // Precision is mandatory for %P specifier.
8110   if (CS.getKind() == ConversionSpecifier::PArg &&
8111       FS.getPrecision().getHowSpecified() == OptionalAmount::NotSpecified) {
8112     EmitFormatDiagnostic(S.PDiag(diag::warn_format_P_no_precision),
8113                          getLocationOfByte(startSpecifier),
8114                          /*IsStringLocation*/ false,
8115                          getSpecifierRange(startSpecifier, specifierLen));
8116   }
8117 
8118   // Check each flag does not conflict with any other component.
8119   if (!FS.hasValidThousandsGroupingPrefix())
8120     HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen);
8121   if (!FS.hasValidLeadingZeros())
8122     HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen);
8123   if (!FS.hasValidPlusPrefix())
8124     HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen);
8125   if (!FS.hasValidSpacePrefix())
8126     HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen);
8127   if (!FS.hasValidAlternativeForm())
8128     HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen);
8129   if (!FS.hasValidLeftJustified())
8130     HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen);
8131 
8132   // Check that flags are not ignored by another flag
8133   if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+'
8134     HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(),
8135         startSpecifier, specifierLen);
8136   if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-'
8137     HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(),
8138             startSpecifier, specifierLen);
8139 
8140   // Check the length modifier is valid with the given conversion specifier.
8141   if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo(),
8142                                  S.getLangOpts()))
8143     HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
8144                                 diag::warn_format_nonsensical_length);
8145   else if (!FS.hasStandardLengthModifier())
8146     HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen);
8147   else if (!FS.hasStandardLengthConversionCombination())
8148     HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
8149                                 diag::warn_format_non_standard_conversion_spec);
8150 
8151   if (!FS.hasStandardConversionSpecifier(S.getLangOpts()))
8152     HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen);
8153 
8154   // The remaining checks depend on the data arguments.
8155   if (HasVAListArg)
8156     return true;
8157 
8158   if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
8159     return false;
8160 
8161   const Expr *Arg = getDataArg(argIndex);
8162   if (!Arg)
8163     return true;
8164 
8165   return checkFormatExpr(FS, startSpecifier, specifierLen, Arg);
8166 }
8167 
8168 static bool requiresParensToAddCast(const Expr *E) {
8169   // FIXME: We should have a general way to reason about operator
8170   // precedence and whether parens are actually needed here.
8171   // Take care of a few common cases where they aren't.
8172   const Expr *Inside = E->IgnoreImpCasts();
8173   if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Inside))
8174     Inside = POE->getSyntacticForm()->IgnoreImpCasts();
8175 
8176   switch (Inside->getStmtClass()) {
8177   case Stmt::ArraySubscriptExprClass:
8178   case Stmt::CallExprClass:
8179   case Stmt::CharacterLiteralClass:
8180   case Stmt::CXXBoolLiteralExprClass:
8181   case Stmt::DeclRefExprClass:
8182   case Stmt::FloatingLiteralClass:
8183   case Stmt::IntegerLiteralClass:
8184   case Stmt::MemberExprClass:
8185   case Stmt::ObjCArrayLiteralClass:
8186   case Stmt::ObjCBoolLiteralExprClass:
8187   case Stmt::ObjCBoxedExprClass:
8188   case Stmt::ObjCDictionaryLiteralClass:
8189   case Stmt::ObjCEncodeExprClass:
8190   case Stmt::ObjCIvarRefExprClass:
8191   case Stmt::ObjCMessageExprClass:
8192   case Stmt::ObjCPropertyRefExprClass:
8193   case Stmt::ObjCStringLiteralClass:
8194   case Stmt::ObjCSubscriptRefExprClass:
8195   case Stmt::ParenExprClass:
8196   case Stmt::StringLiteralClass:
8197   case Stmt::UnaryOperatorClass:
8198     return false;
8199   default:
8200     return true;
8201   }
8202 }
8203 
8204 static std::pair<QualType, StringRef>
8205 shouldNotPrintDirectly(const ASTContext &Context,
8206                        QualType IntendedTy,
8207                        const Expr *E) {
8208   // Use a 'while' to peel off layers of typedefs.
8209   QualType TyTy = IntendedTy;
8210   while (const TypedefType *UserTy = TyTy->getAs<TypedefType>()) {
8211     StringRef Name = UserTy->getDecl()->getName();
8212     QualType CastTy = llvm::StringSwitch<QualType>(Name)
8213       .Case("CFIndex", Context.getNSIntegerType())
8214       .Case("NSInteger", Context.getNSIntegerType())
8215       .Case("NSUInteger", Context.getNSUIntegerType())
8216       .Case("SInt32", Context.IntTy)
8217       .Case("UInt32", Context.UnsignedIntTy)
8218       .Default(QualType());
8219 
8220     if (!CastTy.isNull())
8221       return std::make_pair(CastTy, Name);
8222 
8223     TyTy = UserTy->desugar();
8224   }
8225 
8226   // Strip parens if necessary.
8227   if (const ParenExpr *PE = dyn_cast<ParenExpr>(E))
8228     return shouldNotPrintDirectly(Context,
8229                                   PE->getSubExpr()->getType(),
8230                                   PE->getSubExpr());
8231 
8232   // If this is a conditional expression, then its result type is constructed
8233   // via usual arithmetic conversions and thus there might be no necessary
8234   // typedef sugar there.  Recurse to operands to check for NSInteger &
8235   // Co. usage condition.
8236   if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
8237     QualType TrueTy, FalseTy;
8238     StringRef TrueName, FalseName;
8239 
8240     std::tie(TrueTy, TrueName) =
8241       shouldNotPrintDirectly(Context,
8242                              CO->getTrueExpr()->getType(),
8243                              CO->getTrueExpr());
8244     std::tie(FalseTy, FalseName) =
8245       shouldNotPrintDirectly(Context,
8246                              CO->getFalseExpr()->getType(),
8247                              CO->getFalseExpr());
8248 
8249     if (TrueTy == FalseTy)
8250       return std::make_pair(TrueTy, TrueName);
8251     else if (TrueTy.isNull())
8252       return std::make_pair(FalseTy, FalseName);
8253     else if (FalseTy.isNull())
8254       return std::make_pair(TrueTy, TrueName);
8255   }
8256 
8257   return std::make_pair(QualType(), StringRef());
8258 }
8259 
8260 /// Return true if \p ICE is an implicit argument promotion of an arithmetic
8261 /// type. Bit-field 'promotions' from a higher ranked type to a lower ranked
8262 /// type do not count.
8263 static bool
8264 isArithmeticArgumentPromotion(Sema &S, const ImplicitCastExpr *ICE) {
8265   QualType From = ICE->getSubExpr()->getType();
8266   QualType To = ICE->getType();
8267   // It's an integer promotion if the destination type is the promoted
8268   // source type.
8269   if (ICE->getCastKind() == CK_IntegralCast &&
8270       From->isPromotableIntegerType() &&
8271       S.Context.getPromotedIntegerType(From) == To)
8272     return true;
8273   // Look through vector types, since we do default argument promotion for
8274   // those in OpenCL.
8275   if (const auto *VecTy = From->getAs<ExtVectorType>())
8276     From = VecTy->getElementType();
8277   if (const auto *VecTy = To->getAs<ExtVectorType>())
8278     To = VecTy->getElementType();
8279   // It's a floating promotion if the source type is a lower rank.
8280   return ICE->getCastKind() == CK_FloatingCast &&
8281          S.Context.getFloatingTypeOrder(From, To) < 0;
8282 }
8283 
8284 bool
8285 CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
8286                                     const char *StartSpecifier,
8287                                     unsigned SpecifierLen,
8288                                     const Expr *E) {
8289   using namespace analyze_format_string;
8290   using namespace analyze_printf;
8291 
8292   // Now type check the data expression that matches the
8293   // format specifier.
8294   const analyze_printf::ArgType &AT = FS.getArgType(S.Context, isObjCContext());
8295   if (!AT.isValid())
8296     return true;
8297 
8298   QualType ExprTy = E->getType();
8299   while (const TypeOfExprType *TET = dyn_cast<TypeOfExprType>(ExprTy)) {
8300     ExprTy = TET->getUnderlyingExpr()->getType();
8301   }
8302 
8303   // Diagnose attempts to print a boolean value as a character. Unlike other
8304   // -Wformat diagnostics, this is fine from a type perspective, but it still
8305   // doesn't make sense.
8306   if (FS.getConversionSpecifier().getKind() == ConversionSpecifier::cArg &&
8307       E->isKnownToHaveBooleanValue()) {
8308     const CharSourceRange &CSR =
8309         getSpecifierRange(StartSpecifier, SpecifierLen);
8310     SmallString<4> FSString;
8311     llvm::raw_svector_ostream os(FSString);
8312     FS.toString(os);
8313     EmitFormatDiagnostic(S.PDiag(diag::warn_format_bool_as_character)
8314                              << FSString,
8315                          E->getExprLoc(), false, CSR);
8316     return true;
8317   }
8318 
8319   analyze_printf::ArgType::MatchKind Match = AT.matchesType(S.Context, ExprTy);
8320   if (Match == analyze_printf::ArgType::Match)
8321     return true;
8322 
8323   // Look through argument promotions for our error message's reported type.
8324   // This includes the integral and floating promotions, but excludes array
8325   // and function pointer decay (seeing that an argument intended to be a
8326   // string has type 'char [6]' is probably more confusing than 'char *') and
8327   // certain bitfield promotions (bitfields can be 'demoted' to a lesser type).
8328   if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
8329     if (isArithmeticArgumentPromotion(S, ICE)) {
8330       E = ICE->getSubExpr();
8331       ExprTy = E->getType();
8332 
8333       // Check if we didn't match because of an implicit cast from a 'char'
8334       // or 'short' to an 'int'.  This is done because printf is a varargs
8335       // function.
8336       if (ICE->getType() == S.Context.IntTy ||
8337           ICE->getType() == S.Context.UnsignedIntTy) {
8338         // All further checking is done on the subexpression
8339         const analyze_printf::ArgType::MatchKind ImplicitMatch =
8340             AT.matchesType(S.Context, ExprTy);
8341         if (ImplicitMatch == analyze_printf::ArgType::Match)
8342           return true;
8343         if (ImplicitMatch == ArgType::NoMatchPedantic ||
8344             ImplicitMatch == ArgType::NoMatchTypeConfusion)
8345           Match = ImplicitMatch;
8346       }
8347     }
8348   } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) {
8349     // Special case for 'a', which has type 'int' in C.
8350     // Note, however, that we do /not/ want to treat multibyte constants like
8351     // 'MooV' as characters! This form is deprecated but still exists.
8352     if (ExprTy == S.Context.IntTy)
8353       if (llvm::isUIntN(S.Context.getCharWidth(), CL->getValue()))
8354         ExprTy = S.Context.CharTy;
8355   }
8356 
8357   // Look through enums to their underlying type.
8358   bool IsEnum = false;
8359   if (auto EnumTy = ExprTy->getAs<EnumType>()) {
8360     ExprTy = EnumTy->getDecl()->getIntegerType();
8361     IsEnum = true;
8362   }
8363 
8364   // %C in an Objective-C context prints a unichar, not a wchar_t.
8365   // If the argument is an integer of some kind, believe the %C and suggest
8366   // a cast instead of changing the conversion specifier.
8367   QualType IntendedTy = ExprTy;
8368   if (isObjCContext() &&
8369       FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) {
8370     if (ExprTy->isIntegralOrUnscopedEnumerationType() &&
8371         !ExprTy->isCharType()) {
8372       // 'unichar' is defined as a typedef of unsigned short, but we should
8373       // prefer using the typedef if it is visible.
8374       IntendedTy = S.Context.UnsignedShortTy;
8375 
8376       // While we are here, check if the value is an IntegerLiteral that happens
8377       // to be within the valid range.
8378       if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) {
8379         const llvm::APInt &V = IL->getValue();
8380         if (V.getActiveBits() <= S.Context.getTypeSize(IntendedTy))
8381           return true;
8382       }
8383 
8384       LookupResult Result(S, &S.Context.Idents.get("unichar"), E->getBeginLoc(),
8385                           Sema::LookupOrdinaryName);
8386       if (S.LookupName(Result, S.getCurScope())) {
8387         NamedDecl *ND = Result.getFoundDecl();
8388         if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
8389           if (TD->getUnderlyingType() == IntendedTy)
8390             IntendedTy = S.Context.getTypedefType(TD);
8391       }
8392     }
8393   }
8394 
8395   // Special-case some of Darwin's platform-independence types by suggesting
8396   // casts to primitive types that are known to be large enough.
8397   bool ShouldNotPrintDirectly = false; StringRef CastTyName;
8398   if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
8399     QualType CastTy;
8400     std::tie(CastTy, CastTyName) = shouldNotPrintDirectly(S.Context, IntendedTy, E);
8401     if (!CastTy.isNull()) {
8402       // %zi/%zu and %td/%tu are OK to use for NSInteger/NSUInteger of type int
8403       // (long in ASTContext). Only complain to pedants.
8404       if ((CastTyName == "NSInteger" || CastTyName == "NSUInteger") &&
8405           (AT.isSizeT() || AT.isPtrdiffT()) &&
8406           AT.matchesType(S.Context, CastTy))
8407         Match = ArgType::NoMatchPedantic;
8408       IntendedTy = CastTy;
8409       ShouldNotPrintDirectly = true;
8410     }
8411   }
8412 
8413   // We may be able to offer a FixItHint if it is a supported type.
8414   PrintfSpecifier fixedFS = FS;
8415   bool Success =
8416       fixedFS.fixType(IntendedTy, S.getLangOpts(), S.Context, isObjCContext());
8417 
8418   if (Success) {
8419     // Get the fix string from the fixed format specifier
8420     SmallString<16> buf;
8421     llvm::raw_svector_ostream os(buf);
8422     fixedFS.toString(os);
8423 
8424     CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen);
8425 
8426     if (IntendedTy == ExprTy && !ShouldNotPrintDirectly) {
8427       unsigned Diag;
8428       switch (Match) {
8429       case ArgType::Match: llvm_unreachable("expected non-matching");
8430       case ArgType::NoMatchPedantic:
8431         Diag = diag::warn_format_conversion_argument_type_mismatch_pedantic;
8432         break;
8433       case ArgType::NoMatchTypeConfusion:
8434         Diag = diag::warn_format_conversion_argument_type_mismatch_confusion;
8435         break;
8436       case ArgType::NoMatch:
8437         Diag = diag::warn_format_conversion_argument_type_mismatch;
8438         break;
8439       }
8440 
8441       // In this case, the specifier is wrong and should be changed to match
8442       // the argument.
8443       EmitFormatDiagnostic(S.PDiag(Diag)
8444                                << AT.getRepresentativeTypeName(S.Context)
8445                                << IntendedTy << IsEnum << E->getSourceRange(),
8446                            E->getBeginLoc(),
8447                            /*IsStringLocation*/ false, SpecRange,
8448                            FixItHint::CreateReplacement(SpecRange, os.str()));
8449     } else {
8450       // The canonical type for formatting this value is different from the
8451       // actual type of the expression. (This occurs, for example, with Darwin's
8452       // NSInteger on 32-bit platforms, where it is typedef'd as 'int', but
8453       // should be printed as 'long' for 64-bit compatibility.)
8454       // Rather than emitting a normal format/argument mismatch, we want to
8455       // add a cast to the recommended type (and correct the format string
8456       // if necessary).
8457       SmallString<16> CastBuf;
8458       llvm::raw_svector_ostream CastFix(CastBuf);
8459       CastFix << "(";
8460       IntendedTy.print(CastFix, S.Context.getPrintingPolicy());
8461       CastFix << ")";
8462 
8463       SmallVector<FixItHint,4> Hints;
8464       if (!AT.matchesType(S.Context, IntendedTy) || ShouldNotPrintDirectly)
8465         Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str()));
8466 
8467       if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) {
8468         // If there's already a cast present, just replace it.
8469         SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc());
8470         Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str()));
8471 
8472       } else if (!requiresParensToAddCast(E)) {
8473         // If the expression has high enough precedence,
8474         // just write the C-style cast.
8475         Hints.push_back(
8476             FixItHint::CreateInsertion(E->getBeginLoc(), CastFix.str()));
8477       } else {
8478         // Otherwise, add parens around the expression as well as the cast.
8479         CastFix << "(";
8480         Hints.push_back(
8481             FixItHint::CreateInsertion(E->getBeginLoc(), CastFix.str()));
8482 
8483         SourceLocation After = S.getLocForEndOfToken(E->getEndLoc());
8484         Hints.push_back(FixItHint::CreateInsertion(After, ")"));
8485       }
8486 
8487       if (ShouldNotPrintDirectly) {
8488         // The expression has a type that should not be printed directly.
8489         // We extract the name from the typedef because we don't want to show
8490         // the underlying type in the diagnostic.
8491         StringRef Name;
8492         if (const TypedefType *TypedefTy = dyn_cast<TypedefType>(ExprTy))
8493           Name = TypedefTy->getDecl()->getName();
8494         else
8495           Name = CastTyName;
8496         unsigned Diag = Match == ArgType::NoMatchPedantic
8497                             ? diag::warn_format_argument_needs_cast_pedantic
8498                             : diag::warn_format_argument_needs_cast;
8499         EmitFormatDiagnostic(S.PDiag(Diag) << Name << IntendedTy << IsEnum
8500                                            << E->getSourceRange(),
8501                              E->getBeginLoc(), /*IsStringLocation=*/false,
8502                              SpecRange, Hints);
8503       } else {
8504         // In this case, the expression could be printed using a different
8505         // specifier, but we've decided that the specifier is probably correct
8506         // and we should cast instead. Just use the normal warning message.
8507         EmitFormatDiagnostic(
8508             S.PDiag(diag::warn_format_conversion_argument_type_mismatch)
8509                 << AT.getRepresentativeTypeName(S.Context) << ExprTy << IsEnum
8510                 << E->getSourceRange(),
8511             E->getBeginLoc(), /*IsStringLocation*/ false, SpecRange, Hints);
8512       }
8513     }
8514   } else {
8515     const CharSourceRange &CSR = getSpecifierRange(StartSpecifier,
8516                                                    SpecifierLen);
8517     // Since the warning for passing non-POD types to variadic functions
8518     // was deferred until now, we emit a warning for non-POD
8519     // arguments here.
8520     switch (S.isValidVarArgType(ExprTy)) {
8521     case Sema::VAK_Valid:
8522     case Sema::VAK_ValidInCXX11: {
8523       unsigned Diag;
8524       switch (Match) {
8525       case ArgType::Match: llvm_unreachable("expected non-matching");
8526       case ArgType::NoMatchPedantic:
8527         Diag = diag::warn_format_conversion_argument_type_mismatch_pedantic;
8528         break;
8529       case ArgType::NoMatchTypeConfusion:
8530         Diag = diag::warn_format_conversion_argument_type_mismatch_confusion;
8531         break;
8532       case ArgType::NoMatch:
8533         Diag = diag::warn_format_conversion_argument_type_mismatch;
8534         break;
8535       }
8536 
8537       EmitFormatDiagnostic(
8538           S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context) << ExprTy
8539                         << IsEnum << CSR << E->getSourceRange(),
8540           E->getBeginLoc(), /*IsStringLocation*/ false, CSR);
8541       break;
8542     }
8543     case Sema::VAK_Undefined:
8544     case Sema::VAK_MSVCUndefined:
8545       EmitFormatDiagnostic(S.PDiag(diag::warn_non_pod_vararg_with_format_string)
8546                                << S.getLangOpts().CPlusPlus11 << ExprTy
8547                                << CallType
8548                                << AT.getRepresentativeTypeName(S.Context) << CSR
8549                                << E->getSourceRange(),
8550                            E->getBeginLoc(), /*IsStringLocation*/ false, CSR);
8551       checkForCStrMembers(AT, E);
8552       break;
8553 
8554     case Sema::VAK_Invalid:
8555       if (ExprTy->isObjCObjectType())
8556         EmitFormatDiagnostic(
8557             S.PDiag(diag::err_cannot_pass_objc_interface_to_vararg_format)
8558                 << S.getLangOpts().CPlusPlus11 << ExprTy << CallType
8559                 << AT.getRepresentativeTypeName(S.Context) << CSR
8560                 << E->getSourceRange(),
8561             E->getBeginLoc(), /*IsStringLocation*/ false, CSR);
8562       else
8563         // FIXME: If this is an initializer list, suggest removing the braces
8564         // or inserting a cast to the target type.
8565         S.Diag(E->getBeginLoc(), diag::err_cannot_pass_to_vararg_format)
8566             << isa<InitListExpr>(E) << ExprTy << CallType
8567             << AT.getRepresentativeTypeName(S.Context) << E->getSourceRange();
8568       break;
8569     }
8570 
8571     assert(FirstDataArg + FS.getArgIndex() < CheckedVarArgs.size() &&
8572            "format string specifier index out of range");
8573     CheckedVarArgs[FirstDataArg + FS.getArgIndex()] = true;
8574   }
8575 
8576   return true;
8577 }
8578 
8579 //===--- CHECK: Scanf format string checking ------------------------------===//
8580 
8581 namespace {
8582 
8583 class CheckScanfHandler : public CheckFormatHandler {
8584 public:
8585   CheckScanfHandler(Sema &s, const FormatStringLiteral *fexpr,
8586                     const Expr *origFormatExpr, Sema::FormatStringType type,
8587                     unsigned firstDataArg, unsigned numDataArgs,
8588                     const char *beg, bool hasVAListArg,
8589                     ArrayRef<const Expr *> Args, unsigned formatIdx,
8590                     bool inFunctionCall, Sema::VariadicCallType CallType,
8591                     llvm::SmallBitVector &CheckedVarArgs,
8592                     UncoveredArgHandler &UncoveredArg)
8593       : CheckFormatHandler(s, fexpr, origFormatExpr, type, firstDataArg,
8594                            numDataArgs, beg, hasVAListArg, Args, formatIdx,
8595                            inFunctionCall, CallType, CheckedVarArgs,
8596                            UncoveredArg) {}
8597 
8598   bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS,
8599                             const char *startSpecifier,
8600                             unsigned specifierLen) override;
8601 
8602   bool HandleInvalidScanfConversionSpecifier(
8603           const analyze_scanf::ScanfSpecifier &FS,
8604           const char *startSpecifier,
8605           unsigned specifierLen) override;
8606 
8607   void HandleIncompleteScanList(const char *start, const char *end) override;
8608 };
8609 
8610 } // namespace
8611 
8612 void CheckScanfHandler::HandleIncompleteScanList(const char *start,
8613                                                  const char *end) {
8614   EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete),
8615                        getLocationOfByte(end), /*IsStringLocation*/true,
8616                        getSpecifierRange(start, end - start));
8617 }
8618 
8619 bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier(
8620                                         const analyze_scanf::ScanfSpecifier &FS,
8621                                         const char *startSpecifier,
8622                                         unsigned specifierLen) {
8623   const analyze_scanf::ScanfConversionSpecifier &CS =
8624     FS.getConversionSpecifier();
8625 
8626   return HandleInvalidConversionSpecifier(FS.getArgIndex(),
8627                                           getLocationOfByte(CS.getStart()),
8628                                           startSpecifier, specifierLen,
8629                                           CS.getStart(), CS.getLength());
8630 }
8631 
8632 bool CheckScanfHandler::HandleScanfSpecifier(
8633                                        const analyze_scanf::ScanfSpecifier &FS,
8634                                        const char *startSpecifier,
8635                                        unsigned specifierLen) {
8636   using namespace analyze_scanf;
8637   using namespace analyze_format_string;
8638 
8639   const ScanfConversionSpecifier &CS = FS.getConversionSpecifier();
8640 
8641   // Handle case where '%' and '*' don't consume an argument.  These shouldn't
8642   // be used to decide if we are using positional arguments consistently.
8643   if (FS.consumesDataArgument()) {
8644     if (atFirstArg) {
8645       atFirstArg = false;
8646       usesPositionalArgs = FS.usesPositionalArg();
8647     }
8648     else if (usesPositionalArgs != FS.usesPositionalArg()) {
8649       HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()),
8650                                         startSpecifier, specifierLen);
8651       return false;
8652     }
8653   }
8654 
8655   // Check if the field with is non-zero.
8656   const OptionalAmount &Amt = FS.getFieldWidth();
8657   if (Amt.getHowSpecified() == OptionalAmount::Constant) {
8658     if (Amt.getConstantAmount() == 0) {
8659       const CharSourceRange &R = getSpecifierRange(Amt.getStart(),
8660                                                    Amt.getConstantLength());
8661       EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width),
8662                            getLocationOfByte(Amt.getStart()),
8663                            /*IsStringLocation*/true, R,
8664                            FixItHint::CreateRemoval(R));
8665     }
8666   }
8667 
8668   if (!FS.consumesDataArgument()) {
8669     // FIXME: Technically specifying a precision or field width here
8670     // makes no sense.  Worth issuing a warning at some point.
8671     return true;
8672   }
8673 
8674   // Consume the argument.
8675   unsigned argIndex = FS.getArgIndex();
8676   if (argIndex < NumDataArgs) {
8677       // The check to see if the argIndex is valid will come later.
8678       // We set the bit here because we may exit early from this
8679       // function if we encounter some other error.
8680     CoveredArgs.set(argIndex);
8681   }
8682 
8683   // Check the length modifier is valid with the given conversion specifier.
8684   if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo(),
8685                                  S.getLangOpts()))
8686     HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
8687                                 diag::warn_format_nonsensical_length);
8688   else if (!FS.hasStandardLengthModifier())
8689     HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen);
8690   else if (!FS.hasStandardLengthConversionCombination())
8691     HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
8692                                 diag::warn_format_non_standard_conversion_spec);
8693 
8694   if (!FS.hasStandardConversionSpecifier(S.getLangOpts()))
8695     HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen);
8696 
8697   // The remaining checks depend on the data arguments.
8698   if (HasVAListArg)
8699     return true;
8700 
8701   if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
8702     return false;
8703 
8704   // Check that the argument type matches the format specifier.
8705   const Expr *Ex = getDataArg(argIndex);
8706   if (!Ex)
8707     return true;
8708 
8709   const analyze_format_string::ArgType &AT = FS.getArgType(S.Context);
8710 
8711   if (!AT.isValid()) {
8712     return true;
8713   }
8714 
8715   analyze_format_string::ArgType::MatchKind Match =
8716       AT.matchesType(S.Context, Ex->getType());
8717   bool Pedantic = Match == analyze_format_string::ArgType::NoMatchPedantic;
8718   if (Match == analyze_format_string::ArgType::Match)
8719     return true;
8720 
8721   ScanfSpecifier fixedFS = FS;
8722   bool Success = fixedFS.fixType(Ex->getType(), Ex->IgnoreImpCasts()->getType(),
8723                                  S.getLangOpts(), S.Context);
8724 
8725   unsigned Diag =
8726       Pedantic ? diag::warn_format_conversion_argument_type_mismatch_pedantic
8727                : diag::warn_format_conversion_argument_type_mismatch;
8728 
8729   if (Success) {
8730     // Get the fix string from the fixed format specifier.
8731     SmallString<128> buf;
8732     llvm::raw_svector_ostream os(buf);
8733     fixedFS.toString(os);
8734 
8735     EmitFormatDiagnostic(
8736         S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context)
8737                       << Ex->getType() << false << Ex->getSourceRange(),
8738         Ex->getBeginLoc(),
8739         /*IsStringLocation*/ false,
8740         getSpecifierRange(startSpecifier, specifierLen),
8741         FixItHint::CreateReplacement(
8742             getSpecifierRange(startSpecifier, specifierLen), os.str()));
8743   } else {
8744     EmitFormatDiagnostic(S.PDiag(Diag)
8745                              << AT.getRepresentativeTypeName(S.Context)
8746                              << Ex->getType() << false << Ex->getSourceRange(),
8747                          Ex->getBeginLoc(),
8748                          /*IsStringLocation*/ false,
8749                          getSpecifierRange(startSpecifier, specifierLen));
8750   }
8751 
8752   return true;
8753 }
8754 
8755 static void CheckFormatString(Sema &S, const FormatStringLiteral *FExpr,
8756                               const Expr *OrigFormatExpr,
8757                               ArrayRef<const Expr *> Args,
8758                               bool HasVAListArg, unsigned format_idx,
8759                               unsigned firstDataArg,
8760                               Sema::FormatStringType Type,
8761                               bool inFunctionCall,
8762                               Sema::VariadicCallType CallType,
8763                               llvm::SmallBitVector &CheckedVarArgs,
8764                               UncoveredArgHandler &UncoveredArg,
8765                               bool IgnoreStringsWithoutSpecifiers) {
8766   // CHECK: is the format string a wide literal?
8767   if (!FExpr->isAscii() && !FExpr->isUTF8()) {
8768     CheckFormatHandler::EmitFormatDiagnostic(
8769         S, inFunctionCall, Args[format_idx],
8770         S.PDiag(diag::warn_format_string_is_wide_literal), FExpr->getBeginLoc(),
8771         /*IsStringLocation*/ true, OrigFormatExpr->getSourceRange());
8772     return;
8773   }
8774 
8775   // Str - The format string.  NOTE: this is NOT null-terminated!
8776   StringRef StrRef = FExpr->getString();
8777   const char *Str = StrRef.data();
8778   // Account for cases where the string literal is truncated in a declaration.
8779   const ConstantArrayType *T =
8780     S.Context.getAsConstantArrayType(FExpr->getType());
8781   assert(T && "String literal not of constant array type!");
8782   size_t TypeSize = T->getSize().getZExtValue();
8783   size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size());
8784   const unsigned numDataArgs = Args.size() - firstDataArg;
8785 
8786   if (IgnoreStringsWithoutSpecifiers &&
8787       !analyze_format_string::parseFormatStringHasFormattingSpecifiers(
8788           Str, Str + StrLen, S.getLangOpts(), S.Context.getTargetInfo()))
8789     return;
8790 
8791   // Emit a warning if the string literal is truncated and does not contain an
8792   // embedded null character.
8793   if (TypeSize <= StrRef.size() &&
8794       StrRef.substr(0, TypeSize).find('\0') == StringRef::npos) {
8795     CheckFormatHandler::EmitFormatDiagnostic(
8796         S, inFunctionCall, Args[format_idx],
8797         S.PDiag(diag::warn_printf_format_string_not_null_terminated),
8798         FExpr->getBeginLoc(),
8799         /*IsStringLocation=*/true, OrigFormatExpr->getSourceRange());
8800     return;
8801   }
8802 
8803   // CHECK: empty format string?
8804   if (StrLen == 0 && numDataArgs > 0) {
8805     CheckFormatHandler::EmitFormatDiagnostic(
8806         S, inFunctionCall, Args[format_idx],
8807         S.PDiag(diag::warn_empty_format_string), FExpr->getBeginLoc(),
8808         /*IsStringLocation*/ true, OrigFormatExpr->getSourceRange());
8809     return;
8810   }
8811 
8812   if (Type == Sema::FST_Printf || Type == Sema::FST_NSString ||
8813       Type == Sema::FST_FreeBSDKPrintf || Type == Sema::FST_OSLog ||
8814       Type == Sema::FST_OSTrace) {
8815     CheckPrintfHandler H(
8816         S, FExpr, OrigFormatExpr, Type, firstDataArg, numDataArgs,
8817         (Type == Sema::FST_NSString || Type == Sema::FST_OSTrace), Str,
8818         HasVAListArg, Args, format_idx, inFunctionCall, CallType,
8819         CheckedVarArgs, UncoveredArg);
8820 
8821     if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen,
8822                                                   S.getLangOpts(),
8823                                                   S.Context.getTargetInfo(),
8824                                             Type == Sema::FST_FreeBSDKPrintf))
8825       H.DoneProcessing();
8826   } else if (Type == Sema::FST_Scanf) {
8827     CheckScanfHandler H(S, FExpr, OrigFormatExpr, Type, firstDataArg,
8828                         numDataArgs, Str, HasVAListArg, Args, format_idx,
8829                         inFunctionCall, CallType, CheckedVarArgs, UncoveredArg);
8830 
8831     if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen,
8832                                                  S.getLangOpts(),
8833                                                  S.Context.getTargetInfo()))
8834       H.DoneProcessing();
8835   } // TODO: handle other formats
8836 }
8837 
8838 bool Sema::FormatStringHasSArg(const StringLiteral *FExpr) {
8839   // Str - The format string.  NOTE: this is NOT null-terminated!
8840   StringRef StrRef = FExpr->getString();
8841   const char *Str = StrRef.data();
8842   // Account for cases where the string literal is truncated in a declaration.
8843   const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType());
8844   assert(T && "String literal not of constant array type!");
8845   size_t TypeSize = T->getSize().getZExtValue();
8846   size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size());
8847   return analyze_format_string::ParseFormatStringHasSArg(Str, Str + StrLen,
8848                                                          getLangOpts(),
8849                                                          Context.getTargetInfo());
8850 }
8851 
8852 //===--- CHECK: Warn on use of wrong absolute value function. -------------===//
8853 
8854 // Returns the related absolute value function that is larger, of 0 if one
8855 // does not exist.
8856 static unsigned getLargerAbsoluteValueFunction(unsigned AbsFunction) {
8857   switch (AbsFunction) {
8858   default:
8859     return 0;
8860 
8861   case Builtin::BI__builtin_abs:
8862     return Builtin::BI__builtin_labs;
8863   case Builtin::BI__builtin_labs:
8864     return Builtin::BI__builtin_llabs;
8865   case Builtin::BI__builtin_llabs:
8866     return 0;
8867 
8868   case Builtin::BI__builtin_fabsf:
8869     return Builtin::BI__builtin_fabs;
8870   case Builtin::BI__builtin_fabs:
8871     return Builtin::BI__builtin_fabsl;
8872   case Builtin::BI__builtin_fabsl:
8873     return 0;
8874 
8875   case Builtin::BI__builtin_cabsf:
8876     return Builtin::BI__builtin_cabs;
8877   case Builtin::BI__builtin_cabs:
8878     return Builtin::BI__builtin_cabsl;
8879   case Builtin::BI__builtin_cabsl:
8880     return 0;
8881 
8882   case Builtin::BIabs:
8883     return Builtin::BIlabs;
8884   case Builtin::BIlabs:
8885     return Builtin::BIllabs;
8886   case Builtin::BIllabs:
8887     return 0;
8888 
8889   case Builtin::BIfabsf:
8890     return Builtin::BIfabs;
8891   case Builtin::BIfabs:
8892     return Builtin::BIfabsl;
8893   case Builtin::BIfabsl:
8894     return 0;
8895 
8896   case Builtin::BIcabsf:
8897    return Builtin::BIcabs;
8898   case Builtin::BIcabs:
8899     return Builtin::BIcabsl;
8900   case Builtin::BIcabsl:
8901     return 0;
8902   }
8903 }
8904 
8905 // Returns the argument type of the absolute value function.
8906 static QualType getAbsoluteValueArgumentType(ASTContext &Context,
8907                                              unsigned AbsType) {
8908   if (AbsType == 0)
8909     return QualType();
8910 
8911   ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None;
8912   QualType BuiltinType = Context.GetBuiltinType(AbsType, Error);
8913   if (Error != ASTContext::GE_None)
8914     return QualType();
8915 
8916   const FunctionProtoType *FT = BuiltinType->getAs<FunctionProtoType>();
8917   if (!FT)
8918     return QualType();
8919 
8920   if (FT->getNumParams() != 1)
8921     return QualType();
8922 
8923   return FT->getParamType(0);
8924 }
8925 
8926 // Returns the best absolute value function, or zero, based on type and
8927 // current absolute value function.
8928 static unsigned getBestAbsFunction(ASTContext &Context, QualType ArgType,
8929                                    unsigned AbsFunctionKind) {
8930   unsigned BestKind = 0;
8931   uint64_t ArgSize = Context.getTypeSize(ArgType);
8932   for (unsigned Kind = AbsFunctionKind; Kind != 0;
8933        Kind = getLargerAbsoluteValueFunction(Kind)) {
8934     QualType ParamType = getAbsoluteValueArgumentType(Context, Kind);
8935     if (Context.getTypeSize(ParamType) >= ArgSize) {
8936       if (BestKind == 0)
8937         BestKind = Kind;
8938       else if (Context.hasSameType(ParamType, ArgType)) {
8939         BestKind = Kind;
8940         break;
8941       }
8942     }
8943   }
8944   return BestKind;
8945 }
8946 
8947 enum AbsoluteValueKind {
8948   AVK_Integer,
8949   AVK_Floating,
8950   AVK_Complex
8951 };
8952 
8953 static AbsoluteValueKind getAbsoluteValueKind(QualType T) {
8954   if (T->isIntegralOrEnumerationType())
8955     return AVK_Integer;
8956   if (T->isRealFloatingType())
8957     return AVK_Floating;
8958   if (T->isAnyComplexType())
8959     return AVK_Complex;
8960 
8961   llvm_unreachable("Type not integer, floating, or complex");
8962 }
8963 
8964 // Changes the absolute value function to a different type.  Preserves whether
8965 // the function is a builtin.
8966 static unsigned changeAbsFunction(unsigned AbsKind,
8967                                   AbsoluteValueKind ValueKind) {
8968   switch (ValueKind) {
8969   case AVK_Integer:
8970     switch (AbsKind) {
8971     default:
8972       return 0;
8973     case Builtin::BI__builtin_fabsf:
8974     case Builtin::BI__builtin_fabs:
8975     case Builtin::BI__builtin_fabsl:
8976     case Builtin::BI__builtin_cabsf:
8977     case Builtin::BI__builtin_cabs:
8978     case Builtin::BI__builtin_cabsl:
8979       return Builtin::BI__builtin_abs;
8980     case Builtin::BIfabsf:
8981     case Builtin::BIfabs:
8982     case Builtin::BIfabsl:
8983     case Builtin::BIcabsf:
8984     case Builtin::BIcabs:
8985     case Builtin::BIcabsl:
8986       return Builtin::BIabs;
8987     }
8988   case AVK_Floating:
8989     switch (AbsKind) {
8990     default:
8991       return 0;
8992     case Builtin::BI__builtin_abs:
8993     case Builtin::BI__builtin_labs:
8994     case Builtin::BI__builtin_llabs:
8995     case Builtin::BI__builtin_cabsf:
8996     case Builtin::BI__builtin_cabs:
8997     case Builtin::BI__builtin_cabsl:
8998       return Builtin::BI__builtin_fabsf;
8999     case Builtin::BIabs:
9000     case Builtin::BIlabs:
9001     case Builtin::BIllabs:
9002     case Builtin::BIcabsf:
9003     case Builtin::BIcabs:
9004     case Builtin::BIcabsl:
9005       return Builtin::BIfabsf;
9006     }
9007   case AVK_Complex:
9008     switch (AbsKind) {
9009     default:
9010       return 0;
9011     case Builtin::BI__builtin_abs:
9012     case Builtin::BI__builtin_labs:
9013     case Builtin::BI__builtin_llabs:
9014     case Builtin::BI__builtin_fabsf:
9015     case Builtin::BI__builtin_fabs:
9016     case Builtin::BI__builtin_fabsl:
9017       return Builtin::BI__builtin_cabsf;
9018     case Builtin::BIabs:
9019     case Builtin::BIlabs:
9020     case Builtin::BIllabs:
9021     case Builtin::BIfabsf:
9022     case Builtin::BIfabs:
9023     case Builtin::BIfabsl:
9024       return Builtin::BIcabsf;
9025     }
9026   }
9027   llvm_unreachable("Unable to convert function");
9028 }
9029 
9030 static unsigned getAbsoluteValueFunctionKind(const FunctionDecl *FDecl) {
9031   const IdentifierInfo *FnInfo = FDecl->getIdentifier();
9032   if (!FnInfo)
9033     return 0;
9034 
9035   switch (FDecl->getBuiltinID()) {
9036   default:
9037     return 0;
9038   case Builtin::BI__builtin_abs:
9039   case Builtin::BI__builtin_fabs:
9040   case Builtin::BI__builtin_fabsf:
9041   case Builtin::BI__builtin_fabsl:
9042   case Builtin::BI__builtin_labs:
9043   case Builtin::BI__builtin_llabs:
9044   case Builtin::BI__builtin_cabs:
9045   case Builtin::BI__builtin_cabsf:
9046   case Builtin::BI__builtin_cabsl:
9047   case Builtin::BIabs:
9048   case Builtin::BIlabs:
9049   case Builtin::BIllabs:
9050   case Builtin::BIfabs:
9051   case Builtin::BIfabsf:
9052   case Builtin::BIfabsl:
9053   case Builtin::BIcabs:
9054   case Builtin::BIcabsf:
9055   case Builtin::BIcabsl:
9056     return FDecl->getBuiltinID();
9057   }
9058   llvm_unreachable("Unknown Builtin type");
9059 }
9060 
9061 // If the replacement is valid, emit a note with replacement function.
9062 // Additionally, suggest including the proper header if not already included.
9063 static void emitReplacement(Sema &S, SourceLocation Loc, SourceRange Range,
9064                             unsigned AbsKind, QualType ArgType) {
9065   bool EmitHeaderHint = true;
9066   const char *HeaderName = nullptr;
9067   const char *FunctionName = nullptr;
9068   if (S.getLangOpts().CPlusPlus && !ArgType->isAnyComplexType()) {
9069     FunctionName = "std::abs";
9070     if (ArgType->isIntegralOrEnumerationType()) {
9071       HeaderName = "cstdlib";
9072     } else if (ArgType->isRealFloatingType()) {
9073       HeaderName = "cmath";
9074     } else {
9075       llvm_unreachable("Invalid Type");
9076     }
9077 
9078     // Lookup all std::abs
9079     if (NamespaceDecl *Std = S.getStdNamespace()) {
9080       LookupResult R(S, &S.Context.Idents.get("abs"), Loc, Sema::LookupAnyName);
9081       R.suppressDiagnostics();
9082       S.LookupQualifiedName(R, Std);
9083 
9084       for (const auto *I : R) {
9085         const FunctionDecl *FDecl = nullptr;
9086         if (const UsingShadowDecl *UsingD = dyn_cast<UsingShadowDecl>(I)) {
9087           FDecl = dyn_cast<FunctionDecl>(UsingD->getTargetDecl());
9088         } else {
9089           FDecl = dyn_cast<FunctionDecl>(I);
9090         }
9091         if (!FDecl)
9092           continue;
9093 
9094         // Found std::abs(), check that they are the right ones.
9095         if (FDecl->getNumParams() != 1)
9096           continue;
9097 
9098         // Check that the parameter type can handle the argument.
9099         QualType ParamType = FDecl->getParamDecl(0)->getType();
9100         if (getAbsoluteValueKind(ArgType) == getAbsoluteValueKind(ParamType) &&
9101             S.Context.getTypeSize(ArgType) <=
9102                 S.Context.getTypeSize(ParamType)) {
9103           // Found a function, don't need the header hint.
9104           EmitHeaderHint = false;
9105           break;
9106         }
9107       }
9108     }
9109   } else {
9110     FunctionName = S.Context.BuiltinInfo.getName(AbsKind);
9111     HeaderName = S.Context.BuiltinInfo.getHeaderName(AbsKind);
9112 
9113     if (HeaderName) {
9114       DeclarationName DN(&S.Context.Idents.get(FunctionName));
9115       LookupResult R(S, DN, Loc, Sema::LookupAnyName);
9116       R.suppressDiagnostics();
9117       S.LookupName(R, S.getCurScope());
9118 
9119       if (R.isSingleResult()) {
9120         FunctionDecl *FD = dyn_cast<FunctionDecl>(R.getFoundDecl());
9121         if (FD && FD->getBuiltinID() == AbsKind) {
9122           EmitHeaderHint = false;
9123         } else {
9124           return;
9125         }
9126       } else if (!R.empty()) {
9127         return;
9128       }
9129     }
9130   }
9131 
9132   S.Diag(Loc, diag::note_replace_abs_function)
9133       << FunctionName << FixItHint::CreateReplacement(Range, FunctionName);
9134 
9135   if (!HeaderName)
9136     return;
9137 
9138   if (!EmitHeaderHint)
9139     return;
9140 
9141   S.Diag(Loc, diag::note_include_header_or_declare) << HeaderName
9142                                                     << FunctionName;
9143 }
9144 
9145 template <std::size_t StrLen>
9146 static bool IsStdFunction(const FunctionDecl *FDecl,
9147                           const char (&Str)[StrLen]) {
9148   if (!FDecl)
9149     return false;
9150   if (!FDecl->getIdentifier() || !FDecl->getIdentifier()->isStr(Str))
9151     return false;
9152   if (!FDecl->isInStdNamespace())
9153     return false;
9154 
9155   return true;
9156 }
9157 
9158 // Warn when using the wrong abs() function.
9159 void Sema::CheckAbsoluteValueFunction(const CallExpr *Call,
9160                                       const FunctionDecl *FDecl) {
9161   if (Call->getNumArgs() != 1)
9162     return;
9163 
9164   unsigned AbsKind = getAbsoluteValueFunctionKind(FDecl);
9165   bool IsStdAbs = IsStdFunction(FDecl, "abs");
9166   if (AbsKind == 0 && !IsStdAbs)
9167     return;
9168 
9169   QualType ArgType = Call->getArg(0)->IgnoreParenImpCasts()->getType();
9170   QualType ParamType = Call->getArg(0)->getType();
9171 
9172   // Unsigned types cannot be negative.  Suggest removing the absolute value
9173   // function call.
9174   if (ArgType->isUnsignedIntegerType()) {
9175     const char *FunctionName =
9176         IsStdAbs ? "std::abs" : Context.BuiltinInfo.getName(AbsKind);
9177     Diag(Call->getExprLoc(), diag::warn_unsigned_abs) << ArgType << ParamType;
9178     Diag(Call->getExprLoc(), diag::note_remove_abs)
9179         << FunctionName
9180         << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange());
9181     return;
9182   }
9183 
9184   // Taking the absolute value of a pointer is very suspicious, they probably
9185   // wanted to index into an array, dereference a pointer, call a function, etc.
9186   if (ArgType->isPointerType() || ArgType->canDecayToPointerType()) {
9187     unsigned DiagType = 0;
9188     if (ArgType->isFunctionType())
9189       DiagType = 1;
9190     else if (ArgType->isArrayType())
9191       DiagType = 2;
9192 
9193     Diag(Call->getExprLoc(), diag::warn_pointer_abs) << DiagType << ArgType;
9194     return;
9195   }
9196 
9197   // std::abs has overloads which prevent most of the absolute value problems
9198   // from occurring.
9199   if (IsStdAbs)
9200     return;
9201 
9202   AbsoluteValueKind ArgValueKind = getAbsoluteValueKind(ArgType);
9203   AbsoluteValueKind ParamValueKind = getAbsoluteValueKind(ParamType);
9204 
9205   // The argument and parameter are the same kind.  Check if they are the right
9206   // size.
9207   if (ArgValueKind == ParamValueKind) {
9208     if (Context.getTypeSize(ArgType) <= Context.getTypeSize(ParamType))
9209       return;
9210 
9211     unsigned NewAbsKind = getBestAbsFunction(Context, ArgType, AbsKind);
9212     Diag(Call->getExprLoc(), diag::warn_abs_too_small)
9213         << FDecl << ArgType << ParamType;
9214 
9215     if (NewAbsKind == 0)
9216       return;
9217 
9218     emitReplacement(*this, Call->getExprLoc(),
9219                     Call->getCallee()->getSourceRange(), NewAbsKind, ArgType);
9220     return;
9221   }
9222 
9223   // ArgValueKind != ParamValueKind
9224   // The wrong type of absolute value function was used.  Attempt to find the
9225   // proper one.
9226   unsigned NewAbsKind = changeAbsFunction(AbsKind, ArgValueKind);
9227   NewAbsKind = getBestAbsFunction(Context, ArgType, NewAbsKind);
9228   if (NewAbsKind == 0)
9229     return;
9230 
9231   Diag(Call->getExprLoc(), diag::warn_wrong_absolute_value_type)
9232       << FDecl << ParamValueKind << ArgValueKind;
9233 
9234   emitReplacement(*this, Call->getExprLoc(),
9235                   Call->getCallee()->getSourceRange(), NewAbsKind, ArgType);
9236 }
9237 
9238 //===--- CHECK: Warn on use of std::max and unsigned zero. r---------------===//
9239 void Sema::CheckMaxUnsignedZero(const CallExpr *Call,
9240                                 const FunctionDecl *FDecl) {
9241   if (!Call || !FDecl) return;
9242 
9243   // Ignore template specializations and macros.
9244   if (inTemplateInstantiation()) return;
9245   if (Call->getExprLoc().isMacroID()) return;
9246 
9247   // Only care about the one template argument, two function parameter std::max
9248   if (Call->getNumArgs() != 2) return;
9249   if (!IsStdFunction(FDecl, "max")) return;
9250   const auto * ArgList = FDecl->getTemplateSpecializationArgs();
9251   if (!ArgList) return;
9252   if (ArgList->size() != 1) return;
9253 
9254   // Check that template type argument is unsigned integer.
9255   const auto& TA = ArgList->get(0);
9256   if (TA.getKind() != TemplateArgument::Type) return;
9257   QualType ArgType = TA.getAsType();
9258   if (!ArgType->isUnsignedIntegerType()) return;
9259 
9260   // See if either argument is a literal zero.
9261   auto IsLiteralZeroArg = [](const Expr* E) -> bool {
9262     const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E);
9263     if (!MTE) return false;
9264     const auto *Num = dyn_cast<IntegerLiteral>(MTE->getSubExpr());
9265     if (!Num) return false;
9266     if (Num->getValue() != 0) return false;
9267     return true;
9268   };
9269 
9270   const Expr *FirstArg = Call->getArg(0);
9271   const Expr *SecondArg = Call->getArg(1);
9272   const bool IsFirstArgZero = IsLiteralZeroArg(FirstArg);
9273   const bool IsSecondArgZero = IsLiteralZeroArg(SecondArg);
9274 
9275   // Only warn when exactly one argument is zero.
9276   if (IsFirstArgZero == IsSecondArgZero) return;
9277 
9278   SourceRange FirstRange = FirstArg->getSourceRange();
9279   SourceRange SecondRange = SecondArg->getSourceRange();
9280 
9281   SourceRange ZeroRange = IsFirstArgZero ? FirstRange : SecondRange;
9282 
9283   Diag(Call->getExprLoc(), diag::warn_max_unsigned_zero)
9284       << IsFirstArgZero << Call->getCallee()->getSourceRange() << ZeroRange;
9285 
9286   // Deduce what parts to remove so that "std::max(0u, foo)" becomes "(foo)".
9287   SourceRange RemovalRange;
9288   if (IsFirstArgZero) {
9289     RemovalRange = SourceRange(FirstRange.getBegin(),
9290                                SecondRange.getBegin().getLocWithOffset(-1));
9291   } else {
9292     RemovalRange = SourceRange(getLocForEndOfToken(FirstRange.getEnd()),
9293                                SecondRange.getEnd());
9294   }
9295 
9296   Diag(Call->getExprLoc(), diag::note_remove_max_call)
9297         << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange())
9298         << FixItHint::CreateRemoval(RemovalRange);
9299 }
9300 
9301 //===--- CHECK: Standard memory functions ---------------------------------===//
9302 
9303 /// Takes the expression passed to the size_t parameter of functions
9304 /// such as memcmp, strncat, etc and warns if it's a comparison.
9305 ///
9306 /// This is to catch typos like `if (memcmp(&a, &b, sizeof(a) > 0))`.
9307 static bool CheckMemorySizeofForComparison(Sema &S, const Expr *E,
9308                                            IdentifierInfo *FnName,
9309                                            SourceLocation FnLoc,
9310                                            SourceLocation RParenLoc) {
9311   const BinaryOperator *Size = dyn_cast<BinaryOperator>(E);
9312   if (!Size)
9313     return false;
9314 
9315   // if E is binop and op is <=>, >, <, >=, <=, ==, &&, ||:
9316   if (!Size->isComparisonOp() && !Size->isLogicalOp())
9317     return false;
9318 
9319   SourceRange SizeRange = Size->getSourceRange();
9320   S.Diag(Size->getOperatorLoc(), diag::warn_memsize_comparison)
9321       << SizeRange << FnName;
9322   S.Diag(FnLoc, diag::note_memsize_comparison_paren)
9323       << FnName
9324       << FixItHint::CreateInsertion(
9325              S.getLocForEndOfToken(Size->getLHS()->getEndLoc()), ")")
9326       << FixItHint::CreateRemoval(RParenLoc);
9327   S.Diag(SizeRange.getBegin(), diag::note_memsize_comparison_cast_silence)
9328       << FixItHint::CreateInsertion(SizeRange.getBegin(), "(size_t)(")
9329       << FixItHint::CreateInsertion(S.getLocForEndOfToken(SizeRange.getEnd()),
9330                                     ")");
9331 
9332   return true;
9333 }
9334 
9335 /// Determine whether the given type is or contains a dynamic class type
9336 /// (e.g., whether it has a vtable).
9337 static const CXXRecordDecl *getContainedDynamicClass(QualType T,
9338                                                      bool &IsContained) {
9339   // Look through array types while ignoring qualifiers.
9340   const Type *Ty = T->getBaseElementTypeUnsafe();
9341   IsContained = false;
9342 
9343   const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
9344   RD = RD ? RD->getDefinition() : nullptr;
9345   if (!RD || RD->isInvalidDecl())
9346     return nullptr;
9347 
9348   if (RD->isDynamicClass())
9349     return RD;
9350 
9351   // Check all the fields.  If any bases were dynamic, the class is dynamic.
9352   // It's impossible for a class to transitively contain itself by value, so
9353   // infinite recursion is impossible.
9354   for (auto *FD : RD->fields()) {
9355     bool SubContained;
9356     if (const CXXRecordDecl *ContainedRD =
9357             getContainedDynamicClass(FD->getType(), SubContained)) {
9358       IsContained = true;
9359       return ContainedRD;
9360     }
9361   }
9362 
9363   return nullptr;
9364 }
9365 
9366 static const UnaryExprOrTypeTraitExpr *getAsSizeOfExpr(const Expr *E) {
9367   if (const auto *Unary = dyn_cast<UnaryExprOrTypeTraitExpr>(E))
9368     if (Unary->getKind() == UETT_SizeOf)
9369       return Unary;
9370   return nullptr;
9371 }
9372 
9373 /// If E is a sizeof expression, returns its argument expression,
9374 /// otherwise returns NULL.
9375 static const Expr *getSizeOfExprArg(const Expr *E) {
9376   if (const UnaryExprOrTypeTraitExpr *SizeOf = getAsSizeOfExpr(E))
9377     if (!SizeOf->isArgumentType())
9378       return SizeOf->getArgumentExpr()->IgnoreParenImpCasts();
9379   return nullptr;
9380 }
9381 
9382 /// If E is a sizeof expression, returns its argument type.
9383 static QualType getSizeOfArgType(const Expr *E) {
9384   if (const UnaryExprOrTypeTraitExpr *SizeOf = getAsSizeOfExpr(E))
9385     return SizeOf->getTypeOfArgument();
9386   return QualType();
9387 }
9388 
9389 namespace {
9390 
9391 struct SearchNonTrivialToInitializeField
9392     : DefaultInitializedTypeVisitor<SearchNonTrivialToInitializeField> {
9393   using Super =
9394       DefaultInitializedTypeVisitor<SearchNonTrivialToInitializeField>;
9395 
9396   SearchNonTrivialToInitializeField(const Expr *E, Sema &S) : E(E), S(S) {}
9397 
9398   void visitWithKind(QualType::PrimitiveDefaultInitializeKind PDIK, QualType FT,
9399                      SourceLocation SL) {
9400     if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) {
9401       asDerived().visitArray(PDIK, AT, SL);
9402       return;
9403     }
9404 
9405     Super::visitWithKind(PDIK, FT, SL);
9406   }
9407 
9408   void visitARCStrong(QualType FT, SourceLocation SL) {
9409     S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 1);
9410   }
9411   void visitARCWeak(QualType FT, SourceLocation SL) {
9412     S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 1);
9413   }
9414   void visitStruct(QualType FT, SourceLocation SL) {
9415     for (const FieldDecl *FD : FT->castAs<RecordType>()->getDecl()->fields())
9416       visit(FD->getType(), FD->getLocation());
9417   }
9418   void visitArray(QualType::PrimitiveDefaultInitializeKind PDIK,
9419                   const ArrayType *AT, SourceLocation SL) {
9420     visit(getContext().getBaseElementType(AT), SL);
9421   }
9422   void visitTrivial(QualType FT, SourceLocation SL) {}
9423 
9424   static void diag(QualType RT, const Expr *E, Sema &S) {
9425     SearchNonTrivialToInitializeField(E, S).visitStruct(RT, SourceLocation());
9426   }
9427 
9428   ASTContext &getContext() { return S.getASTContext(); }
9429 
9430   const Expr *E;
9431   Sema &S;
9432 };
9433 
9434 struct SearchNonTrivialToCopyField
9435     : CopiedTypeVisitor<SearchNonTrivialToCopyField, false> {
9436   using Super = CopiedTypeVisitor<SearchNonTrivialToCopyField, false>;
9437 
9438   SearchNonTrivialToCopyField(const Expr *E, Sema &S) : E(E), S(S) {}
9439 
9440   void visitWithKind(QualType::PrimitiveCopyKind PCK, QualType FT,
9441                      SourceLocation SL) {
9442     if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) {
9443       asDerived().visitArray(PCK, AT, SL);
9444       return;
9445     }
9446 
9447     Super::visitWithKind(PCK, FT, SL);
9448   }
9449 
9450   void visitARCStrong(QualType FT, SourceLocation SL) {
9451     S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 0);
9452   }
9453   void visitARCWeak(QualType FT, SourceLocation SL) {
9454     S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 0);
9455   }
9456   void visitStruct(QualType FT, SourceLocation SL) {
9457     for (const FieldDecl *FD : FT->castAs<RecordType>()->getDecl()->fields())
9458       visit(FD->getType(), FD->getLocation());
9459   }
9460   void visitArray(QualType::PrimitiveCopyKind PCK, const ArrayType *AT,
9461                   SourceLocation SL) {
9462     visit(getContext().getBaseElementType(AT), SL);
9463   }
9464   void preVisit(QualType::PrimitiveCopyKind PCK, QualType FT,
9465                 SourceLocation SL) {}
9466   void visitTrivial(QualType FT, SourceLocation SL) {}
9467   void visitVolatileTrivial(QualType FT, SourceLocation SL) {}
9468 
9469   static void diag(QualType RT, const Expr *E, Sema &S) {
9470     SearchNonTrivialToCopyField(E, S).visitStruct(RT, SourceLocation());
9471   }
9472 
9473   ASTContext &getContext() { return S.getASTContext(); }
9474 
9475   const Expr *E;
9476   Sema &S;
9477 };
9478 
9479 }
9480 
9481 /// Detect if \c SizeofExpr is likely to calculate the sizeof an object.
9482 static bool doesExprLikelyComputeSize(const Expr *SizeofExpr) {
9483   SizeofExpr = SizeofExpr->IgnoreParenImpCasts();
9484 
9485   if (const auto *BO = dyn_cast<BinaryOperator>(SizeofExpr)) {
9486     if (BO->getOpcode() != BO_Mul && BO->getOpcode() != BO_Add)
9487       return false;
9488 
9489     return doesExprLikelyComputeSize(BO->getLHS()) ||
9490            doesExprLikelyComputeSize(BO->getRHS());
9491   }
9492 
9493   return getAsSizeOfExpr(SizeofExpr) != nullptr;
9494 }
9495 
9496 /// Check if the ArgLoc originated from a macro passed to the call at CallLoc.
9497 ///
9498 /// \code
9499 ///   #define MACRO 0
9500 ///   foo(MACRO);
9501 ///   foo(0);
9502 /// \endcode
9503 ///
9504 /// This should return true for the first call to foo, but not for the second
9505 /// (regardless of whether foo is a macro or function).
9506 static bool isArgumentExpandedFromMacro(SourceManager &SM,
9507                                         SourceLocation CallLoc,
9508                                         SourceLocation ArgLoc) {
9509   if (!CallLoc.isMacroID())
9510     return SM.getFileID(CallLoc) != SM.getFileID(ArgLoc);
9511 
9512   return SM.getFileID(SM.getImmediateMacroCallerLoc(CallLoc)) !=
9513          SM.getFileID(SM.getImmediateMacroCallerLoc(ArgLoc));
9514 }
9515 
9516 /// Diagnose cases like 'memset(buf, sizeof(buf), 0)', which should have the
9517 /// last two arguments transposed.
9518 static void CheckMemaccessSize(Sema &S, unsigned BId, const CallExpr *Call) {
9519   if (BId != Builtin::BImemset && BId != Builtin::BIbzero)
9520     return;
9521 
9522   const Expr *SizeArg =
9523     Call->getArg(BId == Builtin::BImemset ? 2 : 1)->IgnoreImpCasts();
9524 
9525   auto isLiteralZero = [](const Expr *E) {
9526     return isa<IntegerLiteral>(E) && cast<IntegerLiteral>(E)->getValue() == 0;
9527   };
9528 
9529   // If we're memsetting or bzeroing 0 bytes, then this is likely an error.
9530   SourceLocation CallLoc = Call->getRParenLoc();
9531   SourceManager &SM = S.getSourceManager();
9532   if (isLiteralZero(SizeArg) &&
9533       !isArgumentExpandedFromMacro(SM, CallLoc, SizeArg->getExprLoc())) {
9534 
9535     SourceLocation DiagLoc = SizeArg->getExprLoc();
9536 
9537     // Some platforms #define bzero to __builtin_memset. See if this is the
9538     // case, and if so, emit a better diagnostic.
9539     if (BId == Builtin::BIbzero ||
9540         (CallLoc.isMacroID() && Lexer::getImmediateMacroName(
9541                                     CallLoc, SM, S.getLangOpts()) == "bzero")) {
9542       S.Diag(DiagLoc, diag::warn_suspicious_bzero_size);
9543       S.Diag(DiagLoc, diag::note_suspicious_bzero_size_silence);
9544     } else if (!isLiteralZero(Call->getArg(1)->IgnoreImpCasts())) {
9545       S.Diag(DiagLoc, diag::warn_suspicious_sizeof_memset) << 0;
9546       S.Diag(DiagLoc, diag::note_suspicious_sizeof_memset_silence) << 0;
9547     }
9548     return;
9549   }
9550 
9551   // If the second argument to a memset is a sizeof expression and the third
9552   // isn't, this is also likely an error. This should catch
9553   // 'memset(buf, sizeof(buf), 0xff)'.
9554   if (BId == Builtin::BImemset &&
9555       doesExprLikelyComputeSize(Call->getArg(1)) &&
9556       !doesExprLikelyComputeSize(Call->getArg(2))) {
9557     SourceLocation DiagLoc = Call->getArg(1)->getExprLoc();
9558     S.Diag(DiagLoc, diag::warn_suspicious_sizeof_memset) << 1;
9559     S.Diag(DiagLoc, diag::note_suspicious_sizeof_memset_silence) << 1;
9560     return;
9561   }
9562 }
9563 
9564 /// Check for dangerous or invalid arguments to memset().
9565 ///
9566 /// This issues warnings on known problematic, dangerous or unspecified
9567 /// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp'
9568 /// function calls.
9569 ///
9570 /// \param Call The call expression to diagnose.
9571 void Sema::CheckMemaccessArguments(const CallExpr *Call,
9572                                    unsigned BId,
9573                                    IdentifierInfo *FnName) {
9574   assert(BId != 0);
9575 
9576   // It is possible to have a non-standard definition of memset.  Validate
9577   // we have enough arguments, and if not, abort further checking.
9578   unsigned ExpectedNumArgs =
9579       (BId == Builtin::BIstrndup || BId == Builtin::BIbzero ? 2 : 3);
9580   if (Call->getNumArgs() < ExpectedNumArgs)
9581     return;
9582 
9583   unsigned LastArg = (BId == Builtin::BImemset || BId == Builtin::BIbzero ||
9584                       BId == Builtin::BIstrndup ? 1 : 2);
9585   unsigned LenArg =
9586       (BId == Builtin::BIbzero || BId == Builtin::BIstrndup ? 1 : 2);
9587   const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts();
9588 
9589   if (CheckMemorySizeofForComparison(*this, LenExpr, FnName,
9590                                      Call->getBeginLoc(), Call->getRParenLoc()))
9591     return;
9592 
9593   // Catch cases like 'memset(buf, sizeof(buf), 0)'.
9594   CheckMemaccessSize(*this, BId, Call);
9595 
9596   // We have special checking when the length is a sizeof expression.
9597   QualType SizeOfArgTy = getSizeOfArgType(LenExpr);
9598   const Expr *SizeOfArg = getSizeOfExprArg(LenExpr);
9599   llvm::FoldingSetNodeID SizeOfArgID;
9600 
9601   // Although widely used, 'bzero' is not a standard function. Be more strict
9602   // with the argument types before allowing diagnostics and only allow the
9603   // form bzero(ptr, sizeof(...)).
9604   QualType FirstArgTy = Call->getArg(0)->IgnoreParenImpCasts()->getType();
9605   if (BId == Builtin::BIbzero && !FirstArgTy->getAs<PointerType>())
9606     return;
9607 
9608   for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) {
9609     const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts();
9610     SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange();
9611 
9612     QualType DestTy = Dest->getType();
9613     QualType PointeeTy;
9614     if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) {
9615       PointeeTy = DestPtrTy->getPointeeType();
9616 
9617       // Never warn about void type pointers. This can be used to suppress
9618       // false positives.
9619       if (PointeeTy->isVoidType())
9620         continue;
9621 
9622       // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by
9623       // actually comparing the expressions for equality. Because computing the
9624       // expression IDs can be expensive, we only do this if the diagnostic is
9625       // enabled.
9626       if (SizeOfArg &&
9627           !Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess,
9628                            SizeOfArg->getExprLoc())) {
9629         // We only compute IDs for expressions if the warning is enabled, and
9630         // cache the sizeof arg's ID.
9631         if (SizeOfArgID == llvm::FoldingSetNodeID())
9632           SizeOfArg->Profile(SizeOfArgID, Context, true);
9633         llvm::FoldingSetNodeID DestID;
9634         Dest->Profile(DestID, Context, true);
9635         if (DestID == SizeOfArgID) {
9636           // TODO: For strncpy() and friends, this could suggest sizeof(dst)
9637           //       over sizeof(src) as well.
9638           unsigned ActionIdx = 0; // Default is to suggest dereferencing.
9639           StringRef ReadableName = FnName->getName();
9640 
9641           if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest))
9642             if (UnaryOp->getOpcode() == UO_AddrOf)
9643               ActionIdx = 1; // If its an address-of operator, just remove it.
9644           if (!PointeeTy->isIncompleteType() &&
9645               (Context.getTypeSize(PointeeTy) == Context.getCharWidth()))
9646             ActionIdx = 2; // If the pointee's size is sizeof(char),
9647                            // suggest an explicit length.
9648 
9649           // If the function is defined as a builtin macro, do not show macro
9650           // expansion.
9651           SourceLocation SL = SizeOfArg->getExprLoc();
9652           SourceRange DSR = Dest->getSourceRange();
9653           SourceRange SSR = SizeOfArg->getSourceRange();
9654           SourceManager &SM = getSourceManager();
9655 
9656           if (SM.isMacroArgExpansion(SL)) {
9657             ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts);
9658             SL = SM.getSpellingLoc(SL);
9659             DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()),
9660                              SM.getSpellingLoc(DSR.getEnd()));
9661             SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()),
9662                              SM.getSpellingLoc(SSR.getEnd()));
9663           }
9664 
9665           DiagRuntimeBehavior(SL, SizeOfArg,
9666                               PDiag(diag::warn_sizeof_pointer_expr_memaccess)
9667                                 << ReadableName
9668                                 << PointeeTy
9669                                 << DestTy
9670                                 << DSR
9671                                 << SSR);
9672           DiagRuntimeBehavior(SL, SizeOfArg,
9673                          PDiag(diag::warn_sizeof_pointer_expr_memaccess_note)
9674                                 << ActionIdx
9675                                 << SSR);
9676 
9677           break;
9678         }
9679       }
9680 
9681       // Also check for cases where the sizeof argument is the exact same
9682       // type as the memory argument, and where it points to a user-defined
9683       // record type.
9684       if (SizeOfArgTy != QualType()) {
9685         if (PointeeTy->isRecordType() &&
9686             Context.typesAreCompatible(SizeOfArgTy, DestTy)) {
9687           DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest,
9688                               PDiag(diag::warn_sizeof_pointer_type_memaccess)
9689                                 << FnName << SizeOfArgTy << ArgIdx
9690                                 << PointeeTy << Dest->getSourceRange()
9691                                 << LenExpr->getSourceRange());
9692           break;
9693         }
9694       }
9695     } else if (DestTy->isArrayType()) {
9696       PointeeTy = DestTy;
9697     }
9698 
9699     if (PointeeTy == QualType())
9700       continue;
9701 
9702     // Always complain about dynamic classes.
9703     bool IsContained;
9704     if (const CXXRecordDecl *ContainedRD =
9705             getContainedDynamicClass(PointeeTy, IsContained)) {
9706 
9707       unsigned OperationType = 0;
9708       const bool IsCmp = BId == Builtin::BImemcmp || BId == Builtin::BIbcmp;
9709       // "overwritten" if we're warning about the destination for any call
9710       // but memcmp; otherwise a verb appropriate to the call.
9711       if (ArgIdx != 0 || IsCmp) {
9712         if (BId == Builtin::BImemcpy)
9713           OperationType = 1;
9714         else if(BId == Builtin::BImemmove)
9715           OperationType = 2;
9716         else if (IsCmp)
9717           OperationType = 3;
9718       }
9719 
9720       DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
9721                           PDiag(diag::warn_dyn_class_memaccess)
9722                               << (IsCmp ? ArgIdx + 2 : ArgIdx) << FnName
9723                               << IsContained << ContainedRD << OperationType
9724                               << Call->getCallee()->getSourceRange());
9725     } else if (PointeeTy.hasNonTrivialObjCLifetime() &&
9726              BId != Builtin::BImemset)
9727       DiagRuntimeBehavior(
9728         Dest->getExprLoc(), Dest,
9729         PDiag(diag::warn_arc_object_memaccess)
9730           << ArgIdx << FnName << PointeeTy
9731           << Call->getCallee()->getSourceRange());
9732     else if (const auto *RT = PointeeTy->getAs<RecordType>()) {
9733       if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) &&
9734           RT->getDecl()->isNonTrivialToPrimitiveDefaultInitialize()) {
9735         DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
9736                             PDiag(diag::warn_cstruct_memaccess)
9737                                 << ArgIdx << FnName << PointeeTy << 0);
9738         SearchNonTrivialToInitializeField::diag(PointeeTy, Dest, *this);
9739       } else if ((BId == Builtin::BImemcpy || BId == Builtin::BImemmove) &&
9740                  RT->getDecl()->isNonTrivialToPrimitiveCopy()) {
9741         DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
9742                             PDiag(diag::warn_cstruct_memaccess)
9743                                 << ArgIdx << FnName << PointeeTy << 1);
9744         SearchNonTrivialToCopyField::diag(PointeeTy, Dest, *this);
9745       } else {
9746         continue;
9747       }
9748     } else
9749       continue;
9750 
9751     DiagRuntimeBehavior(
9752       Dest->getExprLoc(), Dest,
9753       PDiag(diag::note_bad_memaccess_silence)
9754         << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)"));
9755     break;
9756   }
9757 }
9758 
9759 // A little helper routine: ignore addition and subtraction of integer literals.
9760 // This intentionally does not ignore all integer constant expressions because
9761 // we don't want to remove sizeof().
9762 static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) {
9763   Ex = Ex->IgnoreParenCasts();
9764 
9765   while (true) {
9766     const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex);
9767     if (!BO || !BO->isAdditiveOp())
9768       break;
9769 
9770     const Expr *RHS = BO->getRHS()->IgnoreParenCasts();
9771     const Expr *LHS = BO->getLHS()->IgnoreParenCasts();
9772 
9773     if (isa<IntegerLiteral>(RHS))
9774       Ex = LHS;
9775     else if (isa<IntegerLiteral>(LHS))
9776       Ex = RHS;
9777     else
9778       break;
9779   }
9780 
9781   return Ex;
9782 }
9783 
9784 static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty,
9785                                                       ASTContext &Context) {
9786   // Only handle constant-sized or VLAs, but not flexible members.
9787   if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) {
9788     // Only issue the FIXIT for arrays of size > 1.
9789     if (CAT->getSize().getSExtValue() <= 1)
9790       return false;
9791   } else if (!Ty->isVariableArrayType()) {
9792     return false;
9793   }
9794   return true;
9795 }
9796 
9797 // Warn if the user has made the 'size' argument to strlcpy or strlcat
9798 // be the size of the source, instead of the destination.
9799 void Sema::CheckStrlcpycatArguments(const CallExpr *Call,
9800                                     IdentifierInfo *FnName) {
9801 
9802   // Don't crash if the user has the wrong number of arguments
9803   unsigned NumArgs = Call->getNumArgs();
9804   if ((NumArgs != 3) && (NumArgs != 4))
9805     return;
9806 
9807   const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context);
9808   const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context);
9809   const Expr *CompareWithSrc = nullptr;
9810 
9811   if (CheckMemorySizeofForComparison(*this, SizeArg, FnName,
9812                                      Call->getBeginLoc(), Call->getRParenLoc()))
9813     return;
9814 
9815   // Look for 'strlcpy(dst, x, sizeof(x))'
9816   if (const Expr *Ex = getSizeOfExprArg(SizeArg))
9817     CompareWithSrc = Ex;
9818   else {
9819     // Look for 'strlcpy(dst, x, strlen(x))'
9820     if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) {
9821       if (SizeCall->getBuiltinCallee() == Builtin::BIstrlen &&
9822           SizeCall->getNumArgs() == 1)
9823         CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context);
9824     }
9825   }
9826 
9827   if (!CompareWithSrc)
9828     return;
9829 
9830   // Determine if the argument to sizeof/strlen is equal to the source
9831   // argument.  In principle there's all kinds of things you could do
9832   // here, for instance creating an == expression and evaluating it with
9833   // EvaluateAsBooleanCondition, but this uses a more direct technique:
9834   const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg);
9835   if (!SrcArgDRE)
9836     return;
9837 
9838   const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc);
9839   if (!CompareWithSrcDRE ||
9840       SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl())
9841     return;
9842 
9843   const Expr *OriginalSizeArg = Call->getArg(2);
9844   Diag(CompareWithSrcDRE->getBeginLoc(), diag::warn_strlcpycat_wrong_size)
9845       << OriginalSizeArg->getSourceRange() << FnName;
9846 
9847   // Output a FIXIT hint if the destination is an array (rather than a
9848   // pointer to an array).  This could be enhanced to handle some
9849   // pointers if we know the actual size, like if DstArg is 'array+2'
9850   // we could say 'sizeof(array)-2'.
9851   const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts();
9852   if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context))
9853     return;
9854 
9855   SmallString<128> sizeString;
9856   llvm::raw_svector_ostream OS(sizeString);
9857   OS << "sizeof(";
9858   DstArg->printPretty(OS, nullptr, getPrintingPolicy());
9859   OS << ")";
9860 
9861   Diag(OriginalSizeArg->getBeginLoc(), diag::note_strlcpycat_wrong_size)
9862       << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(),
9863                                       OS.str());
9864 }
9865 
9866 /// Check if two expressions refer to the same declaration.
9867 static bool referToTheSameDecl(const Expr *E1, const Expr *E2) {
9868   if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1))
9869     if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2))
9870       return D1->getDecl() == D2->getDecl();
9871   return false;
9872 }
9873 
9874 static const Expr *getStrlenExprArg(const Expr *E) {
9875   if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
9876     const FunctionDecl *FD = CE->getDirectCallee();
9877     if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen)
9878       return nullptr;
9879     return CE->getArg(0)->IgnoreParenCasts();
9880   }
9881   return nullptr;
9882 }
9883 
9884 // Warn on anti-patterns as the 'size' argument to strncat.
9885 // The correct size argument should look like following:
9886 //   strncat(dst, src, sizeof(dst) - strlen(dest) - 1);
9887 void Sema::CheckStrncatArguments(const CallExpr *CE,
9888                                  IdentifierInfo *FnName) {
9889   // Don't crash if the user has the wrong number of arguments.
9890   if (CE->getNumArgs() < 3)
9891     return;
9892   const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts();
9893   const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts();
9894   const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts();
9895 
9896   if (CheckMemorySizeofForComparison(*this, LenArg, FnName, CE->getBeginLoc(),
9897                                      CE->getRParenLoc()))
9898     return;
9899 
9900   // Identify common expressions, which are wrongly used as the size argument
9901   // to strncat and may lead to buffer overflows.
9902   unsigned PatternType = 0;
9903   if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) {
9904     // - sizeof(dst)
9905     if (referToTheSameDecl(SizeOfArg, DstArg))
9906       PatternType = 1;
9907     // - sizeof(src)
9908     else if (referToTheSameDecl(SizeOfArg, SrcArg))
9909       PatternType = 2;
9910   } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) {
9911     if (BE->getOpcode() == BO_Sub) {
9912       const Expr *L = BE->getLHS()->IgnoreParenCasts();
9913       const Expr *R = BE->getRHS()->IgnoreParenCasts();
9914       // - sizeof(dst) - strlen(dst)
9915       if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) &&
9916           referToTheSameDecl(DstArg, getStrlenExprArg(R)))
9917         PatternType = 1;
9918       // - sizeof(src) - (anything)
9919       else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L)))
9920         PatternType = 2;
9921     }
9922   }
9923 
9924   if (PatternType == 0)
9925     return;
9926 
9927   // Generate the diagnostic.
9928   SourceLocation SL = LenArg->getBeginLoc();
9929   SourceRange SR = LenArg->getSourceRange();
9930   SourceManager &SM = getSourceManager();
9931 
9932   // If the function is defined as a builtin macro, do not show macro expansion.
9933   if (SM.isMacroArgExpansion(SL)) {
9934     SL = SM.getSpellingLoc(SL);
9935     SR = SourceRange(SM.getSpellingLoc(SR.getBegin()),
9936                      SM.getSpellingLoc(SR.getEnd()));
9937   }
9938 
9939   // Check if the destination is an array (rather than a pointer to an array).
9940   QualType DstTy = DstArg->getType();
9941   bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy,
9942                                                                     Context);
9943   if (!isKnownSizeArray) {
9944     if (PatternType == 1)
9945       Diag(SL, diag::warn_strncat_wrong_size) << SR;
9946     else
9947       Diag(SL, diag::warn_strncat_src_size) << SR;
9948     return;
9949   }
9950 
9951   if (PatternType == 1)
9952     Diag(SL, diag::warn_strncat_large_size) << SR;
9953   else
9954     Diag(SL, diag::warn_strncat_src_size) << SR;
9955 
9956   SmallString<128> sizeString;
9957   llvm::raw_svector_ostream OS(sizeString);
9958   OS << "sizeof(";
9959   DstArg->printPretty(OS, nullptr, getPrintingPolicy());
9960   OS << ") - ";
9961   OS << "strlen(";
9962   DstArg->printPretty(OS, nullptr, getPrintingPolicy());
9963   OS << ") - 1";
9964 
9965   Diag(SL, diag::note_strncat_wrong_size)
9966     << FixItHint::CreateReplacement(SR, OS.str());
9967 }
9968 
9969 void
9970 Sema::CheckReturnValExpr(Expr *RetValExp, QualType lhsType,
9971                          SourceLocation ReturnLoc,
9972                          bool isObjCMethod,
9973                          const AttrVec *Attrs,
9974                          const FunctionDecl *FD) {
9975   // Check if the return value is null but should not be.
9976   if (((Attrs && hasSpecificAttr<ReturnsNonNullAttr>(*Attrs)) ||
9977        (!isObjCMethod && isNonNullType(Context, lhsType))) &&
9978       CheckNonNullExpr(*this, RetValExp))
9979     Diag(ReturnLoc, diag::warn_null_ret)
9980       << (isObjCMethod ? 1 : 0) << RetValExp->getSourceRange();
9981 
9982   // C++11 [basic.stc.dynamic.allocation]p4:
9983   //   If an allocation function declared with a non-throwing
9984   //   exception-specification fails to allocate storage, it shall return
9985   //   a null pointer. Any other allocation function that fails to allocate
9986   //   storage shall indicate failure only by throwing an exception [...]
9987   if (FD) {
9988     OverloadedOperatorKind Op = FD->getOverloadedOperator();
9989     if (Op == OO_New || Op == OO_Array_New) {
9990       const FunctionProtoType *Proto
9991         = FD->getType()->castAs<FunctionProtoType>();
9992       if (!Proto->isNothrow(/*ResultIfDependent*/true) &&
9993           CheckNonNullExpr(*this, RetValExp))
9994         Diag(ReturnLoc, diag::warn_operator_new_returns_null)
9995           << FD << getLangOpts().CPlusPlus11;
9996     }
9997   }
9998 }
9999 
10000 //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===//
10001 
10002 /// Check for comparisons of floating point operands using != and ==.
10003 /// Issue a warning if these are no self-comparisons, as they are not likely
10004 /// to do what the programmer intended.
10005 void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) {
10006   Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts();
10007   Expr* RightExprSansParen = RHS->IgnoreParenImpCasts();
10008 
10009   // Special case: check for x == x (which is OK).
10010   // Do not emit warnings for such cases.
10011   if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen))
10012     if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen))
10013       if (DRL->getDecl() == DRR->getDecl())
10014         return;
10015 
10016   // Special case: check for comparisons against literals that can be exactly
10017   //  represented by APFloat.  In such cases, do not emit a warning.  This
10018   //  is a heuristic: often comparison against such literals are used to
10019   //  detect if a value in a variable has not changed.  This clearly can
10020   //  lead to false negatives.
10021   if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) {
10022     if (FLL->isExact())
10023       return;
10024   } else
10025     if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen))
10026       if (FLR->isExact())
10027         return;
10028 
10029   // Check for comparisons with builtin types.
10030   if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen))
10031     if (CL->getBuiltinCallee())
10032       return;
10033 
10034   if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen))
10035     if (CR->getBuiltinCallee())
10036       return;
10037 
10038   // Emit the diagnostic.
10039   Diag(Loc, diag::warn_floatingpoint_eq)
10040     << LHS->getSourceRange() << RHS->getSourceRange();
10041 }
10042 
10043 //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===//
10044 //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===//
10045 
10046 namespace {
10047 
10048 /// Structure recording the 'active' range of an integer-valued
10049 /// expression.
10050 struct IntRange {
10051   /// The number of bits active in the int.
10052   unsigned Width;
10053 
10054   /// True if the int is known not to have negative values.
10055   bool NonNegative;
10056 
10057   IntRange(unsigned Width, bool NonNegative)
10058       : Width(Width), NonNegative(NonNegative) {}
10059 
10060   /// Returns the range of the bool type.
10061   static IntRange forBoolType() {
10062     return IntRange(1, true);
10063   }
10064 
10065   /// Returns the range of an opaque value of the given integral type.
10066   static IntRange forValueOfType(ASTContext &C, QualType T) {
10067     return forValueOfCanonicalType(C,
10068                           T->getCanonicalTypeInternal().getTypePtr());
10069   }
10070 
10071   /// Returns the range of an opaque value of a canonical integral type.
10072   static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) {
10073     assert(T->isCanonicalUnqualified());
10074 
10075     if (const VectorType *VT = dyn_cast<VectorType>(T))
10076       T = VT->getElementType().getTypePtr();
10077     if (const ComplexType *CT = dyn_cast<ComplexType>(T))
10078       T = CT->getElementType().getTypePtr();
10079     if (const AtomicType *AT = dyn_cast<AtomicType>(T))
10080       T = AT->getValueType().getTypePtr();
10081 
10082     if (!C.getLangOpts().CPlusPlus) {
10083       // For enum types in C code, use the underlying datatype.
10084       if (const EnumType *ET = dyn_cast<EnumType>(T))
10085         T = ET->getDecl()->getIntegerType().getDesugaredType(C).getTypePtr();
10086     } else if (const EnumType *ET = dyn_cast<EnumType>(T)) {
10087       // For enum types in C++, use the known bit width of the enumerators.
10088       EnumDecl *Enum = ET->getDecl();
10089       // In C++11, enums can have a fixed underlying type. Use this type to
10090       // compute the range.
10091       if (Enum->isFixed()) {
10092         return IntRange(C.getIntWidth(QualType(T, 0)),
10093                         !ET->isSignedIntegerOrEnumerationType());
10094       }
10095 
10096       unsigned NumPositive = Enum->getNumPositiveBits();
10097       unsigned NumNegative = Enum->getNumNegativeBits();
10098 
10099       if (NumNegative == 0)
10100         return IntRange(NumPositive, true/*NonNegative*/);
10101       else
10102         return IntRange(std::max(NumPositive + 1, NumNegative),
10103                         false/*NonNegative*/);
10104     }
10105 
10106     if (const auto *EIT = dyn_cast<ExtIntType>(T))
10107       return IntRange(EIT->getNumBits(), EIT->isUnsigned());
10108 
10109     const BuiltinType *BT = cast<BuiltinType>(T);
10110     assert(BT->isInteger());
10111 
10112     return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
10113   }
10114 
10115   /// Returns the "target" range of a canonical integral type, i.e.
10116   /// the range of values expressible in the type.
10117   ///
10118   /// This matches forValueOfCanonicalType except that enums have the
10119   /// full range of their type, not the range of their enumerators.
10120   static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) {
10121     assert(T->isCanonicalUnqualified());
10122 
10123     if (const VectorType *VT = dyn_cast<VectorType>(T))
10124       T = VT->getElementType().getTypePtr();
10125     if (const ComplexType *CT = dyn_cast<ComplexType>(T))
10126       T = CT->getElementType().getTypePtr();
10127     if (const AtomicType *AT = dyn_cast<AtomicType>(T))
10128       T = AT->getValueType().getTypePtr();
10129     if (const EnumType *ET = dyn_cast<EnumType>(T))
10130       T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr();
10131 
10132     if (const auto *EIT = dyn_cast<ExtIntType>(T))
10133       return IntRange(EIT->getNumBits(), EIT->isUnsigned());
10134 
10135     const BuiltinType *BT = cast<BuiltinType>(T);
10136     assert(BT->isInteger());
10137 
10138     return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
10139   }
10140 
10141   /// Returns the supremum of two ranges: i.e. their conservative merge.
10142   static IntRange join(IntRange L, IntRange R) {
10143     return IntRange(std::max(L.Width, R.Width),
10144                     L.NonNegative && R.NonNegative);
10145   }
10146 
10147   /// Returns the infinum of two ranges: i.e. their aggressive merge.
10148   static IntRange meet(IntRange L, IntRange R) {
10149     return IntRange(std::min(L.Width, R.Width),
10150                     L.NonNegative || R.NonNegative);
10151   }
10152 };
10153 
10154 } // namespace
10155 
10156 static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value,
10157                               unsigned MaxWidth) {
10158   if (value.isSigned() && value.isNegative())
10159     return IntRange(value.getMinSignedBits(), false);
10160 
10161   if (value.getBitWidth() > MaxWidth)
10162     value = value.trunc(MaxWidth);
10163 
10164   // isNonNegative() just checks the sign bit without considering
10165   // signedness.
10166   return IntRange(value.getActiveBits(), true);
10167 }
10168 
10169 static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty,
10170                               unsigned MaxWidth) {
10171   if (result.isInt())
10172     return GetValueRange(C, result.getInt(), MaxWidth);
10173 
10174   if (result.isVector()) {
10175     IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth);
10176     for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) {
10177       IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth);
10178       R = IntRange::join(R, El);
10179     }
10180     return R;
10181   }
10182 
10183   if (result.isComplexInt()) {
10184     IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth);
10185     IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth);
10186     return IntRange::join(R, I);
10187   }
10188 
10189   // This can happen with lossless casts to intptr_t of "based" lvalues.
10190   // Assume it might use arbitrary bits.
10191   // FIXME: The only reason we need to pass the type in here is to get
10192   // the sign right on this one case.  It would be nice if APValue
10193   // preserved this.
10194   assert(result.isLValue() || result.isAddrLabelDiff());
10195   return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType());
10196 }
10197 
10198 static QualType GetExprType(const Expr *E) {
10199   QualType Ty = E->getType();
10200   if (const AtomicType *AtomicRHS = Ty->getAs<AtomicType>())
10201     Ty = AtomicRHS->getValueType();
10202   return Ty;
10203 }
10204 
10205 /// Pseudo-evaluate the given integer expression, estimating the
10206 /// range of values it might take.
10207 ///
10208 /// \param MaxWidth - the width to which the value will be truncated
10209 static IntRange GetExprRange(ASTContext &C, const Expr *E, unsigned MaxWidth,
10210                              bool InConstantContext) {
10211   E = E->IgnoreParens();
10212 
10213   // Try a full evaluation first.
10214   Expr::EvalResult result;
10215   if (E->EvaluateAsRValue(result, C, InConstantContext))
10216     return GetValueRange(C, result.Val, GetExprType(E), MaxWidth);
10217 
10218   // I think we only want to look through implicit casts here; if the
10219   // user has an explicit widening cast, we should treat the value as
10220   // being of the new, wider type.
10221   if (const auto *CE = dyn_cast<ImplicitCastExpr>(E)) {
10222     if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue)
10223       return GetExprRange(C, CE->getSubExpr(), MaxWidth, InConstantContext);
10224 
10225     IntRange OutputTypeRange = IntRange::forValueOfType(C, GetExprType(CE));
10226 
10227     bool isIntegerCast = CE->getCastKind() == CK_IntegralCast ||
10228                          CE->getCastKind() == CK_BooleanToSignedIntegral;
10229 
10230     // Assume that non-integer casts can span the full range of the type.
10231     if (!isIntegerCast)
10232       return OutputTypeRange;
10233 
10234     IntRange SubRange = GetExprRange(C, CE->getSubExpr(),
10235                                      std::min(MaxWidth, OutputTypeRange.Width),
10236                                      InConstantContext);
10237 
10238     // Bail out if the subexpr's range is as wide as the cast type.
10239     if (SubRange.Width >= OutputTypeRange.Width)
10240       return OutputTypeRange;
10241 
10242     // Otherwise, we take the smaller width, and we're non-negative if
10243     // either the output type or the subexpr is.
10244     return IntRange(SubRange.Width,
10245                     SubRange.NonNegative || OutputTypeRange.NonNegative);
10246   }
10247 
10248   if (const auto *CO = dyn_cast<ConditionalOperator>(E)) {
10249     // If we can fold the condition, just take that operand.
10250     bool CondResult;
10251     if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C))
10252       return GetExprRange(C,
10253                           CondResult ? CO->getTrueExpr() : CO->getFalseExpr(),
10254                           MaxWidth, InConstantContext);
10255 
10256     // Otherwise, conservatively merge.
10257     IntRange L =
10258         GetExprRange(C, CO->getTrueExpr(), MaxWidth, InConstantContext);
10259     IntRange R =
10260         GetExprRange(C, CO->getFalseExpr(), MaxWidth, InConstantContext);
10261     return IntRange::join(L, R);
10262   }
10263 
10264   if (const auto *BO = dyn_cast<BinaryOperator>(E)) {
10265     switch (BO->getOpcode()) {
10266     case BO_Cmp:
10267       llvm_unreachable("builtin <=> should have class type");
10268 
10269     // Boolean-valued operations are single-bit and positive.
10270     case BO_LAnd:
10271     case BO_LOr:
10272     case BO_LT:
10273     case BO_GT:
10274     case BO_LE:
10275     case BO_GE:
10276     case BO_EQ:
10277     case BO_NE:
10278       return IntRange::forBoolType();
10279 
10280     // The type of the assignments is the type of the LHS, so the RHS
10281     // is not necessarily the same type.
10282     case BO_MulAssign:
10283     case BO_DivAssign:
10284     case BO_RemAssign:
10285     case BO_AddAssign:
10286     case BO_SubAssign:
10287     case BO_XorAssign:
10288     case BO_OrAssign:
10289       // TODO: bitfields?
10290       return IntRange::forValueOfType(C, GetExprType(E));
10291 
10292     // Simple assignments just pass through the RHS, which will have
10293     // been coerced to the LHS type.
10294     case BO_Assign:
10295       // TODO: bitfields?
10296       return GetExprRange(C, BO->getRHS(), MaxWidth, InConstantContext);
10297 
10298     // Operations with opaque sources are black-listed.
10299     case BO_PtrMemD:
10300     case BO_PtrMemI:
10301       return IntRange::forValueOfType(C, GetExprType(E));
10302 
10303     // Bitwise-and uses the *infinum* of the two source ranges.
10304     case BO_And:
10305     case BO_AndAssign:
10306       return IntRange::meet(
10307           GetExprRange(C, BO->getLHS(), MaxWidth, InConstantContext),
10308           GetExprRange(C, BO->getRHS(), MaxWidth, InConstantContext));
10309 
10310     // Left shift gets black-listed based on a judgement call.
10311     case BO_Shl:
10312       // ...except that we want to treat '1 << (blah)' as logically
10313       // positive.  It's an important idiom.
10314       if (IntegerLiteral *I
10315             = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) {
10316         if (I->getValue() == 1) {
10317           IntRange R = IntRange::forValueOfType(C, GetExprType(E));
10318           return IntRange(R.Width, /*NonNegative*/ true);
10319         }
10320       }
10321       LLVM_FALLTHROUGH;
10322 
10323     case BO_ShlAssign:
10324       return IntRange::forValueOfType(C, GetExprType(E));
10325 
10326     // Right shift by a constant can narrow its left argument.
10327     case BO_Shr:
10328     case BO_ShrAssign: {
10329       IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth, InConstantContext);
10330 
10331       // If the shift amount is a positive constant, drop the width by
10332       // that much.
10333       llvm::APSInt shift;
10334       if (BO->getRHS()->isIntegerConstantExpr(shift, C) &&
10335           shift.isNonNegative()) {
10336         unsigned zext = shift.getZExtValue();
10337         if (zext >= L.Width)
10338           L.Width = (L.NonNegative ? 0 : 1);
10339         else
10340           L.Width -= zext;
10341       }
10342 
10343       return L;
10344     }
10345 
10346     // Comma acts as its right operand.
10347     case BO_Comma:
10348       return GetExprRange(C, BO->getRHS(), MaxWidth, InConstantContext);
10349 
10350     // Black-list pointer subtractions.
10351     case BO_Sub:
10352       if (BO->getLHS()->getType()->isPointerType())
10353         return IntRange::forValueOfType(C, GetExprType(E));
10354       break;
10355 
10356     // The width of a division result is mostly determined by the size
10357     // of the LHS.
10358     case BO_Div: {
10359       // Don't 'pre-truncate' the operands.
10360       unsigned opWidth = C.getIntWidth(GetExprType(E));
10361       IntRange L = GetExprRange(C, BO->getLHS(), opWidth, InConstantContext);
10362 
10363       // If the divisor is constant, use that.
10364       llvm::APSInt divisor;
10365       if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) {
10366         unsigned log2 = divisor.logBase2(); // floor(log_2(divisor))
10367         if (log2 >= L.Width)
10368           L.Width = (L.NonNegative ? 0 : 1);
10369         else
10370           L.Width = std::min(L.Width - log2, MaxWidth);
10371         return L;
10372       }
10373 
10374       // Otherwise, just use the LHS's width.
10375       IntRange R = GetExprRange(C, BO->getRHS(), opWidth, InConstantContext);
10376       return IntRange(L.Width, L.NonNegative && R.NonNegative);
10377     }
10378 
10379     // The result of a remainder can't be larger than the result of
10380     // either side.
10381     case BO_Rem: {
10382       // Don't 'pre-truncate' the operands.
10383       unsigned opWidth = C.getIntWidth(GetExprType(E));
10384       IntRange L = GetExprRange(C, BO->getLHS(), opWidth, InConstantContext);
10385       IntRange R = GetExprRange(C, BO->getRHS(), opWidth, InConstantContext);
10386 
10387       IntRange meet = IntRange::meet(L, R);
10388       meet.Width = std::min(meet.Width, MaxWidth);
10389       return meet;
10390     }
10391 
10392     // The default behavior is okay for these.
10393     case BO_Mul:
10394     case BO_Add:
10395     case BO_Xor:
10396     case BO_Or:
10397       break;
10398     }
10399 
10400     // The default case is to treat the operation as if it were closed
10401     // on the narrowest type that encompasses both operands.
10402     IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth, InConstantContext);
10403     IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth, InConstantContext);
10404     return IntRange::join(L, R);
10405   }
10406 
10407   if (const auto *UO = dyn_cast<UnaryOperator>(E)) {
10408     switch (UO->getOpcode()) {
10409     // Boolean-valued operations are white-listed.
10410     case UO_LNot:
10411       return IntRange::forBoolType();
10412 
10413     // Operations with opaque sources are black-listed.
10414     case UO_Deref:
10415     case UO_AddrOf: // should be impossible
10416       return IntRange::forValueOfType(C, GetExprType(E));
10417 
10418     default:
10419       return GetExprRange(C, UO->getSubExpr(), MaxWidth, InConstantContext);
10420     }
10421   }
10422 
10423   if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E))
10424     return GetExprRange(C, OVE->getSourceExpr(), MaxWidth, InConstantContext);
10425 
10426   if (const auto *BitField = E->getSourceBitField())
10427     return IntRange(BitField->getBitWidthValue(C),
10428                     BitField->getType()->isUnsignedIntegerOrEnumerationType());
10429 
10430   return IntRange::forValueOfType(C, GetExprType(E));
10431 }
10432 
10433 static IntRange GetExprRange(ASTContext &C, const Expr *E,
10434                              bool InConstantContext) {
10435   return GetExprRange(C, E, C.getIntWidth(GetExprType(E)), InConstantContext);
10436 }
10437 
10438 /// Checks whether the given value, which currently has the given
10439 /// source semantics, has the same value when coerced through the
10440 /// target semantics.
10441 static bool IsSameFloatAfterCast(const llvm::APFloat &value,
10442                                  const llvm::fltSemantics &Src,
10443                                  const llvm::fltSemantics &Tgt) {
10444   llvm::APFloat truncated = value;
10445 
10446   bool ignored;
10447   truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored);
10448   truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored);
10449 
10450   return truncated.bitwiseIsEqual(value);
10451 }
10452 
10453 /// Checks whether the given value, which currently has the given
10454 /// source semantics, has the same value when coerced through the
10455 /// target semantics.
10456 ///
10457 /// The value might be a vector of floats (or a complex number).
10458 static bool IsSameFloatAfterCast(const APValue &value,
10459                                  const llvm::fltSemantics &Src,
10460                                  const llvm::fltSemantics &Tgt) {
10461   if (value.isFloat())
10462     return IsSameFloatAfterCast(value.getFloat(), Src, Tgt);
10463 
10464   if (value.isVector()) {
10465     for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i)
10466       if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt))
10467         return false;
10468     return true;
10469   }
10470 
10471   assert(value.isComplexFloat());
10472   return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) &&
10473           IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt));
10474 }
10475 
10476 static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC,
10477                                        bool IsListInit = false);
10478 
10479 static bool IsEnumConstOrFromMacro(Sema &S, Expr *E) {
10480   // Suppress cases where we are comparing against an enum constant.
10481   if (const DeclRefExpr *DR =
10482       dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
10483     if (isa<EnumConstantDecl>(DR->getDecl()))
10484       return true;
10485 
10486   // Suppress cases where the value is expanded from a macro, unless that macro
10487   // is how a language represents a boolean literal. This is the case in both C
10488   // and Objective-C.
10489   SourceLocation BeginLoc = E->getBeginLoc();
10490   if (BeginLoc.isMacroID()) {
10491     StringRef MacroName = Lexer::getImmediateMacroName(
10492         BeginLoc, S.getSourceManager(), S.getLangOpts());
10493     return MacroName != "YES" && MacroName != "NO" &&
10494            MacroName != "true" && MacroName != "false";
10495   }
10496 
10497   return false;
10498 }
10499 
10500 static bool isKnownToHaveUnsignedValue(Expr *E) {
10501   return E->getType()->isIntegerType() &&
10502          (!E->getType()->isSignedIntegerType() ||
10503           !E->IgnoreParenImpCasts()->getType()->isSignedIntegerType());
10504 }
10505 
10506 namespace {
10507 /// The promoted range of values of a type. In general this has the
10508 /// following structure:
10509 ///
10510 ///     |-----------| . . . |-----------|
10511 ///     ^           ^       ^           ^
10512 ///    Min       HoleMin  HoleMax      Max
10513 ///
10514 /// ... where there is only a hole if a signed type is promoted to unsigned
10515 /// (in which case Min and Max are the smallest and largest representable
10516 /// values).
10517 struct PromotedRange {
10518   // Min, or HoleMax if there is a hole.
10519   llvm::APSInt PromotedMin;
10520   // Max, or HoleMin if there is a hole.
10521   llvm::APSInt PromotedMax;
10522 
10523   PromotedRange(IntRange R, unsigned BitWidth, bool Unsigned) {
10524     if (R.Width == 0)
10525       PromotedMin = PromotedMax = llvm::APSInt(BitWidth, Unsigned);
10526     else if (R.Width >= BitWidth && !Unsigned) {
10527       // Promotion made the type *narrower*. This happens when promoting
10528       // a < 32-bit unsigned / <= 32-bit signed bit-field to 'signed int'.
10529       // Treat all values of 'signed int' as being in range for now.
10530       PromotedMin = llvm::APSInt::getMinValue(BitWidth, Unsigned);
10531       PromotedMax = llvm::APSInt::getMaxValue(BitWidth, Unsigned);
10532     } else {
10533       PromotedMin = llvm::APSInt::getMinValue(R.Width, R.NonNegative)
10534                         .extOrTrunc(BitWidth);
10535       PromotedMin.setIsUnsigned(Unsigned);
10536 
10537       PromotedMax = llvm::APSInt::getMaxValue(R.Width, R.NonNegative)
10538                         .extOrTrunc(BitWidth);
10539       PromotedMax.setIsUnsigned(Unsigned);
10540     }
10541   }
10542 
10543   // Determine whether this range is contiguous (has no hole).
10544   bool isContiguous() const { return PromotedMin <= PromotedMax; }
10545 
10546   // Where a constant value is within the range.
10547   enum ComparisonResult {
10548     LT = 0x1,
10549     LE = 0x2,
10550     GT = 0x4,
10551     GE = 0x8,
10552     EQ = 0x10,
10553     NE = 0x20,
10554     InRangeFlag = 0x40,
10555 
10556     Less = LE | LT | NE,
10557     Min = LE | InRangeFlag,
10558     InRange = InRangeFlag,
10559     Max = GE | InRangeFlag,
10560     Greater = GE | GT | NE,
10561 
10562     OnlyValue = LE | GE | EQ | InRangeFlag,
10563     InHole = NE
10564   };
10565 
10566   ComparisonResult compare(const llvm::APSInt &Value) const {
10567     assert(Value.getBitWidth() == PromotedMin.getBitWidth() &&
10568            Value.isUnsigned() == PromotedMin.isUnsigned());
10569     if (!isContiguous()) {
10570       assert(Value.isUnsigned() && "discontiguous range for signed compare");
10571       if (Value.isMinValue()) return Min;
10572       if (Value.isMaxValue()) return Max;
10573       if (Value >= PromotedMin) return InRange;
10574       if (Value <= PromotedMax) return InRange;
10575       return InHole;
10576     }
10577 
10578     switch (llvm::APSInt::compareValues(Value, PromotedMin)) {
10579     case -1: return Less;
10580     case 0: return PromotedMin == PromotedMax ? OnlyValue : Min;
10581     case 1:
10582       switch (llvm::APSInt::compareValues(Value, PromotedMax)) {
10583       case -1: return InRange;
10584       case 0: return Max;
10585       case 1: return Greater;
10586       }
10587     }
10588 
10589     llvm_unreachable("impossible compare result");
10590   }
10591 
10592   static llvm::Optional<StringRef>
10593   constantValue(BinaryOperatorKind Op, ComparisonResult R, bool ConstantOnRHS) {
10594     if (Op == BO_Cmp) {
10595       ComparisonResult LTFlag = LT, GTFlag = GT;
10596       if (ConstantOnRHS) std::swap(LTFlag, GTFlag);
10597 
10598       if (R & EQ) return StringRef("'std::strong_ordering::equal'");
10599       if (R & LTFlag) return StringRef("'std::strong_ordering::less'");
10600       if (R & GTFlag) return StringRef("'std::strong_ordering::greater'");
10601       return llvm::None;
10602     }
10603 
10604     ComparisonResult TrueFlag, FalseFlag;
10605     if (Op == BO_EQ) {
10606       TrueFlag = EQ;
10607       FalseFlag = NE;
10608     } else if (Op == BO_NE) {
10609       TrueFlag = NE;
10610       FalseFlag = EQ;
10611     } else {
10612       if ((Op == BO_LT || Op == BO_GE) ^ ConstantOnRHS) {
10613         TrueFlag = LT;
10614         FalseFlag = GE;
10615       } else {
10616         TrueFlag = GT;
10617         FalseFlag = LE;
10618       }
10619       if (Op == BO_GE || Op == BO_LE)
10620         std::swap(TrueFlag, FalseFlag);
10621     }
10622     if (R & TrueFlag)
10623       return StringRef("true");
10624     if (R & FalseFlag)
10625       return StringRef("false");
10626     return llvm::None;
10627   }
10628 };
10629 }
10630 
10631 static bool HasEnumType(Expr *E) {
10632   // Strip off implicit integral promotions.
10633   while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
10634     if (ICE->getCastKind() != CK_IntegralCast &&
10635         ICE->getCastKind() != CK_NoOp)
10636       break;
10637     E = ICE->getSubExpr();
10638   }
10639 
10640   return E->getType()->isEnumeralType();
10641 }
10642 
10643 static int classifyConstantValue(Expr *Constant) {
10644   // The values of this enumeration are used in the diagnostics
10645   // diag::warn_out_of_range_compare and diag::warn_tautological_bool_compare.
10646   enum ConstantValueKind {
10647     Miscellaneous = 0,
10648     LiteralTrue,
10649     LiteralFalse
10650   };
10651   if (auto *BL = dyn_cast<CXXBoolLiteralExpr>(Constant))
10652     return BL->getValue() ? ConstantValueKind::LiteralTrue
10653                           : ConstantValueKind::LiteralFalse;
10654   return ConstantValueKind::Miscellaneous;
10655 }
10656 
10657 static bool CheckTautologicalComparison(Sema &S, BinaryOperator *E,
10658                                         Expr *Constant, Expr *Other,
10659                                         const llvm::APSInt &Value,
10660                                         bool RhsConstant) {
10661   if (S.inTemplateInstantiation())
10662     return false;
10663 
10664   Expr *OriginalOther = Other;
10665 
10666   Constant = Constant->IgnoreParenImpCasts();
10667   Other = Other->IgnoreParenImpCasts();
10668 
10669   // Suppress warnings on tautological comparisons between values of the same
10670   // enumeration type. There are only two ways we could warn on this:
10671   //  - If the constant is outside the range of representable values of
10672   //    the enumeration. In such a case, we should warn about the cast
10673   //    to enumeration type, not about the comparison.
10674   //  - If the constant is the maximum / minimum in-range value. For an
10675   //    enumeratin type, such comparisons can be meaningful and useful.
10676   if (Constant->getType()->isEnumeralType() &&
10677       S.Context.hasSameUnqualifiedType(Constant->getType(), Other->getType()))
10678     return false;
10679 
10680   // TODO: Investigate using GetExprRange() to get tighter bounds
10681   // on the bit ranges.
10682   QualType OtherT = Other->getType();
10683   if (const auto *AT = OtherT->getAs<AtomicType>())
10684     OtherT = AT->getValueType();
10685   IntRange OtherRange = IntRange::forValueOfType(S.Context, OtherT);
10686 
10687   // Special case for ObjC BOOL on targets where its a typedef for a signed char
10688   // (Namely, macOS).
10689   bool IsObjCSignedCharBool = S.getLangOpts().ObjC &&
10690                               S.NSAPIObj->isObjCBOOLType(OtherT) &&
10691                               OtherT->isSpecificBuiltinType(BuiltinType::SChar);
10692 
10693   // Whether we're treating Other as being a bool because of the form of
10694   // expression despite it having another type (typically 'int' in C).
10695   bool OtherIsBooleanDespiteType =
10696       !OtherT->isBooleanType() && Other->isKnownToHaveBooleanValue();
10697   if (OtherIsBooleanDespiteType || IsObjCSignedCharBool)
10698     OtherRange = IntRange::forBoolType();
10699 
10700   // Determine the promoted range of the other type and see if a comparison of
10701   // the constant against that range is tautological.
10702   PromotedRange OtherPromotedRange(OtherRange, Value.getBitWidth(),
10703                                    Value.isUnsigned());
10704   auto Cmp = OtherPromotedRange.compare(Value);
10705   auto Result = PromotedRange::constantValue(E->getOpcode(), Cmp, RhsConstant);
10706   if (!Result)
10707     return false;
10708 
10709   // Suppress the diagnostic for an in-range comparison if the constant comes
10710   // from a macro or enumerator. We don't want to diagnose
10711   //
10712   //   some_long_value <= INT_MAX
10713   //
10714   // when sizeof(int) == sizeof(long).
10715   bool InRange = Cmp & PromotedRange::InRangeFlag;
10716   if (InRange && IsEnumConstOrFromMacro(S, Constant))
10717     return false;
10718 
10719   // If this is a comparison to an enum constant, include that
10720   // constant in the diagnostic.
10721   const EnumConstantDecl *ED = nullptr;
10722   if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Constant))
10723     ED = dyn_cast<EnumConstantDecl>(DR->getDecl());
10724 
10725   // Should be enough for uint128 (39 decimal digits)
10726   SmallString<64> PrettySourceValue;
10727   llvm::raw_svector_ostream OS(PrettySourceValue);
10728   if (ED) {
10729     OS << '\'' << *ED << "' (" << Value << ")";
10730   } else if (auto *BL = dyn_cast<ObjCBoolLiteralExpr>(
10731                Constant->IgnoreParenImpCasts())) {
10732     OS << (BL->getValue() ? "YES" : "NO");
10733   } else {
10734     OS << Value;
10735   }
10736 
10737   if (IsObjCSignedCharBool) {
10738     S.DiagRuntimeBehavior(E->getOperatorLoc(), E,
10739                           S.PDiag(diag::warn_tautological_compare_objc_bool)
10740                               << OS.str() << *Result);
10741     return true;
10742   }
10743 
10744   // FIXME: We use a somewhat different formatting for the in-range cases and
10745   // cases involving boolean values for historical reasons. We should pick a
10746   // consistent way of presenting these diagnostics.
10747   if (!InRange || Other->isKnownToHaveBooleanValue()) {
10748 
10749     S.DiagRuntimeBehavior(
10750         E->getOperatorLoc(), E,
10751         S.PDiag(!InRange ? diag::warn_out_of_range_compare
10752                          : diag::warn_tautological_bool_compare)
10753             << OS.str() << classifyConstantValue(Constant) << OtherT
10754             << OtherIsBooleanDespiteType << *Result
10755             << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange());
10756   } else {
10757     unsigned Diag = (isKnownToHaveUnsignedValue(OriginalOther) && Value == 0)
10758                         ? (HasEnumType(OriginalOther)
10759                                ? diag::warn_unsigned_enum_always_true_comparison
10760                                : diag::warn_unsigned_always_true_comparison)
10761                         : diag::warn_tautological_constant_compare;
10762 
10763     S.Diag(E->getOperatorLoc(), Diag)
10764         << RhsConstant << OtherT << E->getOpcodeStr() << OS.str() << *Result
10765         << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
10766   }
10767 
10768   return true;
10769 }
10770 
10771 /// Analyze the operands of the given comparison.  Implements the
10772 /// fallback case from AnalyzeComparison.
10773 static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) {
10774   AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
10775   AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
10776 }
10777 
10778 /// Implements -Wsign-compare.
10779 ///
10780 /// \param E the binary operator to check for warnings
10781 static void AnalyzeComparison(Sema &S, BinaryOperator *E) {
10782   // The type the comparison is being performed in.
10783   QualType T = E->getLHS()->getType();
10784 
10785   // Only analyze comparison operators where both sides have been converted to
10786   // the same type.
10787   if (!S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType()))
10788     return AnalyzeImpConvsInComparison(S, E);
10789 
10790   // Don't analyze value-dependent comparisons directly.
10791   if (E->isValueDependent())
10792     return AnalyzeImpConvsInComparison(S, E);
10793 
10794   Expr *LHS = E->getLHS();
10795   Expr *RHS = E->getRHS();
10796 
10797   if (T->isIntegralType(S.Context)) {
10798     llvm::APSInt RHSValue;
10799     llvm::APSInt LHSValue;
10800 
10801     bool IsRHSIntegralLiteral = RHS->isIntegerConstantExpr(RHSValue, S.Context);
10802     bool IsLHSIntegralLiteral = LHS->isIntegerConstantExpr(LHSValue, S.Context);
10803 
10804     // We don't care about expressions whose result is a constant.
10805     if (IsRHSIntegralLiteral && IsLHSIntegralLiteral)
10806       return AnalyzeImpConvsInComparison(S, E);
10807 
10808     // We only care about expressions where just one side is literal
10809     if (IsRHSIntegralLiteral ^ IsLHSIntegralLiteral) {
10810       // Is the constant on the RHS or LHS?
10811       const bool RhsConstant = IsRHSIntegralLiteral;
10812       Expr *Const = RhsConstant ? RHS : LHS;
10813       Expr *Other = RhsConstant ? LHS : RHS;
10814       const llvm::APSInt &Value = RhsConstant ? RHSValue : LHSValue;
10815 
10816       // Check whether an integer constant comparison results in a value
10817       // of 'true' or 'false'.
10818       if (CheckTautologicalComparison(S, E, Const, Other, Value, RhsConstant))
10819         return AnalyzeImpConvsInComparison(S, E);
10820     }
10821   }
10822 
10823   if (!T->hasUnsignedIntegerRepresentation()) {
10824     // We don't do anything special if this isn't an unsigned integral
10825     // comparison:  we're only interested in integral comparisons, and
10826     // signed comparisons only happen in cases we don't care to warn about.
10827     return AnalyzeImpConvsInComparison(S, E);
10828   }
10829 
10830   LHS = LHS->IgnoreParenImpCasts();
10831   RHS = RHS->IgnoreParenImpCasts();
10832 
10833   if (!S.getLangOpts().CPlusPlus) {
10834     // Avoid warning about comparison of integers with different signs when
10835     // RHS/LHS has a `typeof(E)` type whose sign is different from the sign of
10836     // the type of `E`.
10837     if (const auto *TET = dyn_cast<TypeOfExprType>(LHS->getType()))
10838       LHS = TET->getUnderlyingExpr()->IgnoreParenImpCasts();
10839     if (const auto *TET = dyn_cast<TypeOfExprType>(RHS->getType()))
10840       RHS = TET->getUnderlyingExpr()->IgnoreParenImpCasts();
10841   }
10842 
10843   // Check to see if one of the (unmodified) operands is of different
10844   // signedness.
10845   Expr *signedOperand, *unsignedOperand;
10846   if (LHS->getType()->hasSignedIntegerRepresentation()) {
10847     assert(!RHS->getType()->hasSignedIntegerRepresentation() &&
10848            "unsigned comparison between two signed integer expressions?");
10849     signedOperand = LHS;
10850     unsignedOperand = RHS;
10851   } else if (RHS->getType()->hasSignedIntegerRepresentation()) {
10852     signedOperand = RHS;
10853     unsignedOperand = LHS;
10854   } else {
10855     return AnalyzeImpConvsInComparison(S, E);
10856   }
10857 
10858   // Otherwise, calculate the effective range of the signed operand.
10859   IntRange signedRange =
10860       GetExprRange(S.Context, signedOperand, S.isConstantEvaluated());
10861 
10862   // Go ahead and analyze implicit conversions in the operands.  Note
10863   // that we skip the implicit conversions on both sides.
10864   AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc());
10865   AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc());
10866 
10867   // If the signed range is non-negative, -Wsign-compare won't fire.
10868   if (signedRange.NonNegative)
10869     return;
10870 
10871   // For (in)equality comparisons, if the unsigned operand is a
10872   // constant which cannot collide with a overflowed signed operand,
10873   // then reinterpreting the signed operand as unsigned will not
10874   // change the result of the comparison.
10875   if (E->isEqualityOp()) {
10876     unsigned comparisonWidth = S.Context.getIntWidth(T);
10877     IntRange unsignedRange =
10878         GetExprRange(S.Context, unsignedOperand, S.isConstantEvaluated());
10879 
10880     // We should never be unable to prove that the unsigned operand is
10881     // non-negative.
10882     assert(unsignedRange.NonNegative && "unsigned range includes negative?");
10883 
10884     if (unsignedRange.Width < comparisonWidth)
10885       return;
10886   }
10887 
10888   S.DiagRuntimeBehavior(E->getOperatorLoc(), E,
10889                         S.PDiag(diag::warn_mixed_sign_comparison)
10890                             << LHS->getType() << RHS->getType()
10891                             << LHS->getSourceRange() << RHS->getSourceRange());
10892 }
10893 
10894 /// Analyzes an attempt to assign the given value to a bitfield.
10895 ///
10896 /// Returns true if there was something fishy about the attempt.
10897 static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init,
10898                                       SourceLocation InitLoc) {
10899   assert(Bitfield->isBitField());
10900   if (Bitfield->isInvalidDecl())
10901     return false;
10902 
10903   // White-list bool bitfields.
10904   QualType BitfieldType = Bitfield->getType();
10905   if (BitfieldType->isBooleanType())
10906      return false;
10907 
10908   if (BitfieldType->isEnumeralType()) {
10909     EnumDecl *BitfieldEnumDecl = BitfieldType->castAs<EnumType>()->getDecl();
10910     // If the underlying enum type was not explicitly specified as an unsigned
10911     // type and the enum contain only positive values, MSVC++ will cause an
10912     // inconsistency by storing this as a signed type.
10913     if (S.getLangOpts().CPlusPlus11 &&
10914         !BitfieldEnumDecl->getIntegerTypeSourceInfo() &&
10915         BitfieldEnumDecl->getNumPositiveBits() > 0 &&
10916         BitfieldEnumDecl->getNumNegativeBits() == 0) {
10917       S.Diag(InitLoc, diag::warn_no_underlying_type_specified_for_enum_bitfield)
10918         << BitfieldEnumDecl->getNameAsString();
10919     }
10920   }
10921 
10922   if (Bitfield->getType()->isBooleanType())
10923     return false;
10924 
10925   // Ignore value- or type-dependent expressions.
10926   if (Bitfield->getBitWidth()->isValueDependent() ||
10927       Bitfield->getBitWidth()->isTypeDependent() ||
10928       Init->isValueDependent() ||
10929       Init->isTypeDependent())
10930     return false;
10931 
10932   Expr *OriginalInit = Init->IgnoreParenImpCasts();
10933   unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context);
10934 
10935   Expr::EvalResult Result;
10936   if (!OriginalInit->EvaluateAsInt(Result, S.Context,
10937                                    Expr::SE_AllowSideEffects)) {
10938     // The RHS is not constant.  If the RHS has an enum type, make sure the
10939     // bitfield is wide enough to hold all the values of the enum without
10940     // truncation.
10941     if (const auto *EnumTy = OriginalInit->getType()->getAs<EnumType>()) {
10942       EnumDecl *ED = EnumTy->getDecl();
10943       bool SignedBitfield = BitfieldType->isSignedIntegerType();
10944 
10945       // Enum types are implicitly signed on Windows, so check if there are any
10946       // negative enumerators to see if the enum was intended to be signed or
10947       // not.
10948       bool SignedEnum = ED->getNumNegativeBits() > 0;
10949 
10950       // Check for surprising sign changes when assigning enum values to a
10951       // bitfield of different signedness.  If the bitfield is signed and we
10952       // have exactly the right number of bits to store this unsigned enum,
10953       // suggest changing the enum to an unsigned type. This typically happens
10954       // on Windows where unfixed enums always use an underlying type of 'int'.
10955       unsigned DiagID = 0;
10956       if (SignedEnum && !SignedBitfield) {
10957         DiagID = diag::warn_unsigned_bitfield_assigned_signed_enum;
10958       } else if (SignedBitfield && !SignedEnum &&
10959                  ED->getNumPositiveBits() == FieldWidth) {
10960         DiagID = diag::warn_signed_bitfield_enum_conversion;
10961       }
10962 
10963       if (DiagID) {
10964         S.Diag(InitLoc, DiagID) << Bitfield << ED;
10965         TypeSourceInfo *TSI = Bitfield->getTypeSourceInfo();
10966         SourceRange TypeRange =
10967             TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange();
10968         S.Diag(Bitfield->getTypeSpecStartLoc(), diag::note_change_bitfield_sign)
10969             << SignedEnum << TypeRange;
10970       }
10971 
10972       // Compute the required bitwidth. If the enum has negative values, we need
10973       // one more bit than the normal number of positive bits to represent the
10974       // sign bit.
10975       unsigned BitsNeeded = SignedEnum ? std::max(ED->getNumPositiveBits() + 1,
10976                                                   ED->getNumNegativeBits())
10977                                        : ED->getNumPositiveBits();
10978 
10979       // Check the bitwidth.
10980       if (BitsNeeded > FieldWidth) {
10981         Expr *WidthExpr = Bitfield->getBitWidth();
10982         S.Diag(InitLoc, diag::warn_bitfield_too_small_for_enum)
10983             << Bitfield << ED;
10984         S.Diag(WidthExpr->getExprLoc(), diag::note_widen_bitfield)
10985             << BitsNeeded << ED << WidthExpr->getSourceRange();
10986       }
10987     }
10988 
10989     return false;
10990   }
10991 
10992   llvm::APSInt Value = Result.Val.getInt();
10993 
10994   unsigned OriginalWidth = Value.getBitWidth();
10995 
10996   if (!Value.isSigned() || Value.isNegative())
10997     if (UnaryOperator *UO = dyn_cast<UnaryOperator>(OriginalInit))
10998       if (UO->getOpcode() == UO_Minus || UO->getOpcode() == UO_Not)
10999         OriginalWidth = Value.getMinSignedBits();
11000 
11001   if (OriginalWidth <= FieldWidth)
11002     return false;
11003 
11004   // Compute the value which the bitfield will contain.
11005   llvm::APSInt TruncatedValue = Value.trunc(FieldWidth);
11006   TruncatedValue.setIsSigned(BitfieldType->isSignedIntegerType());
11007 
11008   // Check whether the stored value is equal to the original value.
11009   TruncatedValue = TruncatedValue.extend(OriginalWidth);
11010   if (llvm::APSInt::isSameValue(Value, TruncatedValue))
11011     return false;
11012 
11013   // Special-case bitfields of width 1: booleans are naturally 0/1, and
11014   // therefore don't strictly fit into a signed bitfield of width 1.
11015   if (FieldWidth == 1 && Value == 1)
11016     return false;
11017 
11018   std::string PrettyValue = Value.toString(10);
11019   std::string PrettyTrunc = TruncatedValue.toString(10);
11020 
11021   S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant)
11022     << PrettyValue << PrettyTrunc << OriginalInit->getType()
11023     << Init->getSourceRange();
11024 
11025   return true;
11026 }
11027 
11028 /// Analyze the given simple or compound assignment for warning-worthy
11029 /// operations.
11030 static void AnalyzeAssignment(Sema &S, BinaryOperator *E) {
11031   // Just recurse on the LHS.
11032   AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
11033 
11034   // We want to recurse on the RHS as normal unless we're assigning to
11035   // a bitfield.
11036   if (FieldDecl *Bitfield = E->getLHS()->getSourceBitField()) {
11037     if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(),
11038                                   E->getOperatorLoc())) {
11039       // Recurse, ignoring any implicit conversions on the RHS.
11040       return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(),
11041                                         E->getOperatorLoc());
11042     }
11043   }
11044 
11045   AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
11046 
11047   // Diagnose implicitly sequentially-consistent atomic assignment.
11048   if (E->getLHS()->getType()->isAtomicType())
11049     S.Diag(E->getRHS()->getBeginLoc(), diag::warn_atomic_implicit_seq_cst);
11050 }
11051 
11052 /// Diagnose an implicit cast;  purely a helper for CheckImplicitConversion.
11053 static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T,
11054                             SourceLocation CContext, unsigned diag,
11055                             bool pruneControlFlow = false) {
11056   if (pruneControlFlow) {
11057     S.DiagRuntimeBehavior(E->getExprLoc(), E,
11058                           S.PDiag(diag)
11059                               << SourceType << T << E->getSourceRange()
11060                               << SourceRange(CContext));
11061     return;
11062   }
11063   S.Diag(E->getExprLoc(), diag)
11064     << SourceType << T << E->getSourceRange() << SourceRange(CContext);
11065 }
11066 
11067 /// Diagnose an implicit cast;  purely a helper for CheckImplicitConversion.
11068 static void DiagnoseImpCast(Sema &S, Expr *E, QualType T,
11069                             SourceLocation CContext,
11070                             unsigned diag, bool pruneControlFlow = false) {
11071   DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow);
11072 }
11073 
11074 static bool isObjCSignedCharBool(Sema &S, QualType Ty) {
11075   return Ty->isSpecificBuiltinType(BuiltinType::SChar) &&
11076       S.getLangOpts().ObjC && S.NSAPIObj->isObjCBOOLType(Ty);
11077 }
11078 
11079 static void adornObjCBoolConversionDiagWithTernaryFixit(
11080     Sema &S, Expr *SourceExpr, const Sema::SemaDiagnosticBuilder &Builder) {
11081   Expr *Ignored = SourceExpr->IgnoreImplicit();
11082   if (const auto *OVE = dyn_cast<OpaqueValueExpr>(Ignored))
11083     Ignored = OVE->getSourceExpr();
11084   bool NeedsParens = isa<AbstractConditionalOperator>(Ignored) ||
11085                      isa<BinaryOperator>(Ignored) ||
11086                      isa<CXXOperatorCallExpr>(Ignored);
11087   SourceLocation EndLoc = S.getLocForEndOfToken(SourceExpr->getEndLoc());
11088   if (NeedsParens)
11089     Builder << FixItHint::CreateInsertion(SourceExpr->getBeginLoc(), "(")
11090             << FixItHint::CreateInsertion(EndLoc, ")");
11091   Builder << FixItHint::CreateInsertion(EndLoc, " ? YES : NO");
11092 }
11093 
11094 /// Diagnose an implicit cast from a floating point value to an integer value.
11095 static void DiagnoseFloatingImpCast(Sema &S, Expr *E, QualType T,
11096                                     SourceLocation CContext) {
11097   const bool IsBool = T->isSpecificBuiltinType(BuiltinType::Bool);
11098   const bool PruneWarnings = S.inTemplateInstantiation();
11099 
11100   Expr *InnerE = E->IgnoreParenImpCasts();
11101   // We also want to warn on, e.g., "int i = -1.234"
11102   if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE))
11103     if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus)
11104       InnerE = UOp->getSubExpr()->IgnoreParenImpCasts();
11105 
11106   const bool IsLiteral =
11107       isa<FloatingLiteral>(E) || isa<FloatingLiteral>(InnerE);
11108 
11109   llvm::APFloat Value(0.0);
11110   bool IsConstant =
11111     E->EvaluateAsFloat(Value, S.Context, Expr::SE_AllowSideEffects);
11112   if (!IsConstant) {
11113     if (isObjCSignedCharBool(S, T)) {
11114       return adornObjCBoolConversionDiagWithTernaryFixit(
11115           S, E,
11116           S.Diag(CContext, diag::warn_impcast_float_to_objc_signed_char_bool)
11117               << E->getType());
11118     }
11119 
11120     return DiagnoseImpCast(S, E, T, CContext,
11121                            diag::warn_impcast_float_integer, PruneWarnings);
11122   }
11123 
11124   bool isExact = false;
11125 
11126   llvm::APSInt IntegerValue(S.Context.getIntWidth(T),
11127                             T->hasUnsignedIntegerRepresentation());
11128   llvm::APFloat::opStatus Result = Value.convertToInteger(
11129       IntegerValue, llvm::APFloat::rmTowardZero, &isExact);
11130 
11131   // FIXME: Force the precision of the source value down so we don't print
11132   // digits which are usually useless (we don't really care here if we
11133   // truncate a digit by accident in edge cases).  Ideally, APFloat::toString
11134   // would automatically print the shortest representation, but it's a bit
11135   // tricky to implement.
11136   SmallString<16> PrettySourceValue;
11137   unsigned precision = llvm::APFloat::semanticsPrecision(Value.getSemantics());
11138   precision = (precision * 59 + 195) / 196;
11139   Value.toString(PrettySourceValue, precision);
11140 
11141   if (isObjCSignedCharBool(S, T) && IntegerValue != 0 && IntegerValue != 1) {
11142     return adornObjCBoolConversionDiagWithTernaryFixit(
11143         S, E,
11144         S.Diag(CContext, diag::warn_impcast_constant_value_to_objc_bool)
11145             << PrettySourceValue);
11146   }
11147 
11148   if (Result == llvm::APFloat::opOK && isExact) {
11149     if (IsLiteral) return;
11150     return DiagnoseImpCast(S, E, T, CContext, diag::warn_impcast_float_integer,
11151                            PruneWarnings);
11152   }
11153 
11154   // Conversion of a floating-point value to a non-bool integer where the
11155   // integral part cannot be represented by the integer type is undefined.
11156   if (!IsBool && Result == llvm::APFloat::opInvalidOp)
11157     return DiagnoseImpCast(
11158         S, E, T, CContext,
11159         IsLiteral ? diag::warn_impcast_literal_float_to_integer_out_of_range
11160                   : diag::warn_impcast_float_to_integer_out_of_range,
11161         PruneWarnings);
11162 
11163   unsigned DiagID = 0;
11164   if (IsLiteral) {
11165     // Warn on floating point literal to integer.
11166     DiagID = diag::warn_impcast_literal_float_to_integer;
11167   } else if (IntegerValue == 0) {
11168     if (Value.isZero()) {  // Skip -0.0 to 0 conversion.
11169       return DiagnoseImpCast(S, E, T, CContext,
11170                              diag::warn_impcast_float_integer, PruneWarnings);
11171     }
11172     // Warn on non-zero to zero conversion.
11173     DiagID = diag::warn_impcast_float_to_integer_zero;
11174   } else {
11175     if (IntegerValue.isUnsigned()) {
11176       if (!IntegerValue.isMaxValue()) {
11177         return DiagnoseImpCast(S, E, T, CContext,
11178                                diag::warn_impcast_float_integer, PruneWarnings);
11179       }
11180     } else {  // IntegerValue.isSigned()
11181       if (!IntegerValue.isMaxSignedValue() &&
11182           !IntegerValue.isMinSignedValue()) {
11183         return DiagnoseImpCast(S, E, T, CContext,
11184                                diag::warn_impcast_float_integer, PruneWarnings);
11185       }
11186     }
11187     // Warn on evaluatable floating point expression to integer conversion.
11188     DiagID = diag::warn_impcast_float_to_integer;
11189   }
11190 
11191   SmallString<16> PrettyTargetValue;
11192   if (IsBool)
11193     PrettyTargetValue = Value.isZero() ? "false" : "true";
11194   else
11195     IntegerValue.toString(PrettyTargetValue);
11196 
11197   if (PruneWarnings) {
11198     S.DiagRuntimeBehavior(E->getExprLoc(), E,
11199                           S.PDiag(DiagID)
11200                               << E->getType() << T.getUnqualifiedType()
11201                               << PrettySourceValue << PrettyTargetValue
11202                               << E->getSourceRange() << SourceRange(CContext));
11203   } else {
11204     S.Diag(E->getExprLoc(), DiagID)
11205         << E->getType() << T.getUnqualifiedType() << PrettySourceValue
11206         << PrettyTargetValue << E->getSourceRange() << SourceRange(CContext);
11207   }
11208 }
11209 
11210 /// Analyze the given compound assignment for the possible losing of
11211 /// floating-point precision.
11212 static void AnalyzeCompoundAssignment(Sema &S, BinaryOperator *E) {
11213   assert(isa<CompoundAssignOperator>(E) &&
11214          "Must be compound assignment operation");
11215   // Recurse on the LHS and RHS in here
11216   AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
11217   AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
11218 
11219   if (E->getLHS()->getType()->isAtomicType())
11220     S.Diag(E->getOperatorLoc(), diag::warn_atomic_implicit_seq_cst);
11221 
11222   // Now check the outermost expression
11223   const auto *ResultBT = E->getLHS()->getType()->getAs<BuiltinType>();
11224   const auto *RBT = cast<CompoundAssignOperator>(E)
11225                         ->getComputationResultType()
11226                         ->getAs<BuiltinType>();
11227 
11228   // The below checks assume source is floating point.
11229   if (!ResultBT || !RBT || !RBT->isFloatingPoint()) return;
11230 
11231   // If source is floating point but target is an integer.
11232   if (ResultBT->isInteger())
11233     return DiagnoseImpCast(S, E, E->getRHS()->getType(), E->getLHS()->getType(),
11234                            E->getExprLoc(), diag::warn_impcast_float_integer);
11235 
11236   if (!ResultBT->isFloatingPoint())
11237     return;
11238 
11239   // If both source and target are floating points, warn about losing precision.
11240   int Order = S.getASTContext().getFloatingTypeSemanticOrder(
11241       QualType(ResultBT, 0), QualType(RBT, 0));
11242   if (Order < 0 && !S.SourceMgr.isInSystemMacro(E->getOperatorLoc()))
11243     // warn about dropping FP rank.
11244     DiagnoseImpCast(S, E->getRHS(), E->getLHS()->getType(), E->getOperatorLoc(),
11245                     diag::warn_impcast_float_result_precision);
11246 }
11247 
11248 static std::string PrettyPrintInRange(const llvm::APSInt &Value,
11249                                       IntRange Range) {
11250   if (!Range.Width) return "0";
11251 
11252   llvm::APSInt ValueInRange = Value;
11253   ValueInRange.setIsSigned(!Range.NonNegative);
11254   ValueInRange = ValueInRange.trunc(Range.Width);
11255   return ValueInRange.toString(10);
11256 }
11257 
11258 static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) {
11259   if (!isa<ImplicitCastExpr>(Ex))
11260     return false;
11261 
11262   Expr *InnerE = Ex->IgnoreParenImpCasts();
11263   const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr();
11264   const Type *Source =
11265     S.Context.getCanonicalType(InnerE->getType()).getTypePtr();
11266   if (Target->isDependentType())
11267     return false;
11268 
11269   const BuiltinType *FloatCandidateBT =
11270     dyn_cast<BuiltinType>(ToBool ? Source : Target);
11271   const Type *BoolCandidateType = ToBool ? Target : Source;
11272 
11273   return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) &&
11274           FloatCandidateBT && (FloatCandidateBT->isFloatingPoint()));
11275 }
11276 
11277 static void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall,
11278                                              SourceLocation CC) {
11279   unsigned NumArgs = TheCall->getNumArgs();
11280   for (unsigned i = 0; i < NumArgs; ++i) {
11281     Expr *CurrA = TheCall->getArg(i);
11282     if (!IsImplicitBoolFloatConversion(S, CurrA, true))
11283       continue;
11284 
11285     bool IsSwapped = ((i > 0) &&
11286         IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false));
11287     IsSwapped |= ((i < (NumArgs - 1)) &&
11288         IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false));
11289     if (IsSwapped) {
11290       // Warn on this floating-point to bool conversion.
11291       DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(),
11292                       CurrA->getType(), CC,
11293                       diag::warn_impcast_floating_point_to_bool);
11294     }
11295   }
11296 }
11297 
11298 static void DiagnoseNullConversion(Sema &S, Expr *E, QualType T,
11299                                    SourceLocation CC) {
11300   if (S.Diags.isIgnored(diag::warn_impcast_null_pointer_to_integer,
11301                         E->getExprLoc()))
11302     return;
11303 
11304   // Don't warn on functions which have return type nullptr_t.
11305   if (isa<CallExpr>(E))
11306     return;
11307 
11308   // Check for NULL (GNUNull) or nullptr (CXX11_nullptr).
11309   const Expr::NullPointerConstantKind NullKind =
11310       E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull);
11311   if (NullKind != Expr::NPCK_GNUNull && NullKind != Expr::NPCK_CXX11_nullptr)
11312     return;
11313 
11314   // Return if target type is a safe conversion.
11315   if (T->isAnyPointerType() || T->isBlockPointerType() ||
11316       T->isMemberPointerType() || !T->isScalarType() || T->isNullPtrType())
11317     return;
11318 
11319   SourceLocation Loc = E->getSourceRange().getBegin();
11320 
11321   // Venture through the macro stacks to get to the source of macro arguments.
11322   // The new location is a better location than the complete location that was
11323   // passed in.
11324   Loc = S.SourceMgr.getTopMacroCallerLoc(Loc);
11325   CC = S.SourceMgr.getTopMacroCallerLoc(CC);
11326 
11327   // __null is usually wrapped in a macro.  Go up a macro if that is the case.
11328   if (NullKind == Expr::NPCK_GNUNull && Loc.isMacroID()) {
11329     StringRef MacroName = Lexer::getImmediateMacroNameForDiagnostics(
11330         Loc, S.SourceMgr, S.getLangOpts());
11331     if (MacroName == "NULL")
11332       Loc = S.SourceMgr.getImmediateExpansionRange(Loc).getBegin();
11333   }
11334 
11335   // Only warn if the null and context location are in the same macro expansion.
11336   if (S.SourceMgr.getFileID(Loc) != S.SourceMgr.getFileID(CC))
11337     return;
11338 
11339   S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer)
11340       << (NullKind == Expr::NPCK_CXX11_nullptr) << T << SourceRange(CC)
11341       << FixItHint::CreateReplacement(Loc,
11342                                       S.getFixItZeroLiteralForType(T, Loc));
11343 }
11344 
11345 static void checkObjCArrayLiteral(Sema &S, QualType TargetType,
11346                                   ObjCArrayLiteral *ArrayLiteral);
11347 
11348 static void
11349 checkObjCDictionaryLiteral(Sema &S, QualType TargetType,
11350                            ObjCDictionaryLiteral *DictionaryLiteral);
11351 
11352 /// Check a single element within a collection literal against the
11353 /// target element type.
11354 static void checkObjCCollectionLiteralElement(Sema &S,
11355                                               QualType TargetElementType,
11356                                               Expr *Element,
11357                                               unsigned ElementKind) {
11358   // Skip a bitcast to 'id' or qualified 'id'.
11359   if (auto ICE = dyn_cast<ImplicitCastExpr>(Element)) {
11360     if (ICE->getCastKind() == CK_BitCast &&
11361         ICE->getSubExpr()->getType()->getAs<ObjCObjectPointerType>())
11362       Element = ICE->getSubExpr();
11363   }
11364 
11365   QualType ElementType = Element->getType();
11366   ExprResult ElementResult(Element);
11367   if (ElementType->getAs<ObjCObjectPointerType>() &&
11368       S.CheckSingleAssignmentConstraints(TargetElementType,
11369                                          ElementResult,
11370                                          false, false)
11371         != Sema::Compatible) {
11372     S.Diag(Element->getBeginLoc(), diag::warn_objc_collection_literal_element)
11373         << ElementType << ElementKind << TargetElementType
11374         << Element->getSourceRange();
11375   }
11376 
11377   if (auto ArrayLiteral = dyn_cast<ObjCArrayLiteral>(Element))
11378     checkObjCArrayLiteral(S, TargetElementType, ArrayLiteral);
11379   else if (auto DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(Element))
11380     checkObjCDictionaryLiteral(S, TargetElementType, DictionaryLiteral);
11381 }
11382 
11383 /// Check an Objective-C array literal being converted to the given
11384 /// target type.
11385 static void checkObjCArrayLiteral(Sema &S, QualType TargetType,
11386                                   ObjCArrayLiteral *ArrayLiteral) {
11387   if (!S.NSArrayDecl)
11388     return;
11389 
11390   const auto *TargetObjCPtr = TargetType->getAs<ObjCObjectPointerType>();
11391   if (!TargetObjCPtr)
11392     return;
11393 
11394   if (TargetObjCPtr->isUnspecialized() ||
11395       TargetObjCPtr->getInterfaceDecl()->getCanonicalDecl()
11396         != S.NSArrayDecl->getCanonicalDecl())
11397     return;
11398 
11399   auto TypeArgs = TargetObjCPtr->getTypeArgs();
11400   if (TypeArgs.size() != 1)
11401     return;
11402 
11403   QualType TargetElementType = TypeArgs[0];
11404   for (unsigned I = 0, N = ArrayLiteral->getNumElements(); I != N; ++I) {
11405     checkObjCCollectionLiteralElement(S, TargetElementType,
11406                                       ArrayLiteral->getElement(I),
11407                                       0);
11408   }
11409 }
11410 
11411 /// Check an Objective-C dictionary literal being converted to the given
11412 /// target type.
11413 static void
11414 checkObjCDictionaryLiteral(Sema &S, QualType TargetType,
11415                            ObjCDictionaryLiteral *DictionaryLiteral) {
11416   if (!S.NSDictionaryDecl)
11417     return;
11418 
11419   const auto *TargetObjCPtr = TargetType->getAs<ObjCObjectPointerType>();
11420   if (!TargetObjCPtr)
11421     return;
11422 
11423   if (TargetObjCPtr->isUnspecialized() ||
11424       TargetObjCPtr->getInterfaceDecl()->getCanonicalDecl()
11425         != S.NSDictionaryDecl->getCanonicalDecl())
11426     return;
11427 
11428   auto TypeArgs = TargetObjCPtr->getTypeArgs();
11429   if (TypeArgs.size() != 2)
11430     return;
11431 
11432   QualType TargetKeyType = TypeArgs[0];
11433   QualType TargetObjectType = TypeArgs[1];
11434   for (unsigned I = 0, N = DictionaryLiteral->getNumElements(); I != N; ++I) {
11435     auto Element = DictionaryLiteral->getKeyValueElement(I);
11436     checkObjCCollectionLiteralElement(S, TargetKeyType, Element.Key, 1);
11437     checkObjCCollectionLiteralElement(S, TargetObjectType, Element.Value, 2);
11438   }
11439 }
11440 
11441 // Helper function to filter out cases for constant width constant conversion.
11442 // Don't warn on char array initialization or for non-decimal values.
11443 static bool isSameWidthConstantConversion(Sema &S, Expr *E, QualType T,
11444                                           SourceLocation CC) {
11445   // If initializing from a constant, and the constant starts with '0',
11446   // then it is a binary, octal, or hexadecimal.  Allow these constants
11447   // to fill all the bits, even if there is a sign change.
11448   if (auto *IntLit = dyn_cast<IntegerLiteral>(E->IgnoreParenImpCasts())) {
11449     const char FirstLiteralCharacter =
11450         S.getSourceManager().getCharacterData(IntLit->getBeginLoc())[0];
11451     if (FirstLiteralCharacter == '0')
11452       return false;
11453   }
11454 
11455   // If the CC location points to a '{', and the type is char, then assume
11456   // assume it is an array initialization.
11457   if (CC.isValid() && T->isCharType()) {
11458     const char FirstContextCharacter =
11459         S.getSourceManager().getCharacterData(CC)[0];
11460     if (FirstContextCharacter == '{')
11461       return false;
11462   }
11463 
11464   return true;
11465 }
11466 
11467 static const IntegerLiteral *getIntegerLiteral(Expr *E) {
11468   const auto *IL = dyn_cast<IntegerLiteral>(E);
11469   if (!IL) {
11470     if (auto *UO = dyn_cast<UnaryOperator>(E)) {
11471       if (UO->getOpcode() == UO_Minus)
11472         return dyn_cast<IntegerLiteral>(UO->getSubExpr());
11473     }
11474   }
11475 
11476   return IL;
11477 }
11478 
11479 static void DiagnoseIntInBoolContext(Sema &S, Expr *E) {
11480   E = E->IgnoreParenImpCasts();
11481   SourceLocation ExprLoc = E->getExprLoc();
11482 
11483   if (const auto *BO = dyn_cast<BinaryOperator>(E)) {
11484     BinaryOperator::Opcode Opc = BO->getOpcode();
11485     Expr::EvalResult Result;
11486     // Do not diagnose unsigned shifts.
11487     if (Opc == BO_Shl) {
11488       const auto *LHS = getIntegerLiteral(BO->getLHS());
11489       const auto *RHS = getIntegerLiteral(BO->getRHS());
11490       if (LHS && LHS->getValue() == 0)
11491         S.Diag(ExprLoc, diag::warn_left_shift_always) << 0;
11492       else if (!E->isValueDependent() && LHS && RHS &&
11493                RHS->getValue().isNonNegative() &&
11494                E->EvaluateAsInt(Result, S.Context, Expr::SE_AllowSideEffects))
11495         S.Diag(ExprLoc, diag::warn_left_shift_always)
11496             << (Result.Val.getInt() != 0);
11497       else if (E->getType()->isSignedIntegerType())
11498         S.Diag(ExprLoc, diag::warn_left_shift_in_bool_context) << E;
11499     }
11500   }
11501 
11502   if (const auto *CO = dyn_cast<ConditionalOperator>(E)) {
11503     const auto *LHS = getIntegerLiteral(CO->getTrueExpr());
11504     const auto *RHS = getIntegerLiteral(CO->getFalseExpr());
11505     if (!LHS || !RHS)
11506       return;
11507     if ((LHS->getValue() == 0 || LHS->getValue() == 1) &&
11508         (RHS->getValue() == 0 || RHS->getValue() == 1))
11509       // Do not diagnose common idioms.
11510       return;
11511     if (LHS->getValue() != 0 && RHS->getValue() != 0)
11512       S.Diag(ExprLoc, diag::warn_integer_constants_in_conditional_always_true);
11513   }
11514 }
11515 
11516 static void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
11517                                     SourceLocation CC,
11518                                     bool *ICContext = nullptr,
11519                                     bool IsListInit = false) {
11520   if (E->isTypeDependent() || E->isValueDependent()) return;
11521 
11522   const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr();
11523   const Type *Target = S.Context.getCanonicalType(T).getTypePtr();
11524   if (Source == Target) return;
11525   if (Target->isDependentType()) return;
11526 
11527   // If the conversion context location is invalid don't complain. We also
11528   // don't want to emit a warning if the issue occurs from the expansion of
11529   // a system macro. The problem is that 'getSpellingLoc()' is slow, so we
11530   // delay this check as long as possible. Once we detect we are in that
11531   // scenario, we just return.
11532   if (CC.isInvalid())
11533     return;
11534 
11535   if (Source->isAtomicType())
11536     S.Diag(E->getExprLoc(), diag::warn_atomic_implicit_seq_cst);
11537 
11538   // Diagnose implicit casts to bool.
11539   if (Target->isSpecificBuiltinType(BuiltinType::Bool)) {
11540     if (isa<StringLiteral>(E))
11541       // Warn on string literal to bool.  Checks for string literals in logical
11542       // and expressions, for instance, assert(0 && "error here"), are
11543       // prevented by a check in AnalyzeImplicitConversions().
11544       return DiagnoseImpCast(S, E, T, CC,
11545                              diag::warn_impcast_string_literal_to_bool);
11546     if (isa<ObjCStringLiteral>(E) || isa<ObjCArrayLiteral>(E) ||
11547         isa<ObjCDictionaryLiteral>(E) || isa<ObjCBoxedExpr>(E)) {
11548       // This covers the literal expressions that evaluate to Objective-C
11549       // objects.
11550       return DiagnoseImpCast(S, E, T, CC,
11551                              diag::warn_impcast_objective_c_literal_to_bool);
11552     }
11553     if (Source->isPointerType() || Source->canDecayToPointerType()) {
11554       // Warn on pointer to bool conversion that is always true.
11555       S.DiagnoseAlwaysNonNullPointer(E, Expr::NPCK_NotNull, /*IsEqual*/ false,
11556                                      SourceRange(CC));
11557     }
11558   }
11559 
11560   // If the we're converting a constant to an ObjC BOOL on a platform where BOOL
11561   // is a typedef for signed char (macOS), then that constant value has to be 1
11562   // or 0.
11563   if (isObjCSignedCharBool(S, T) && Source->isIntegralType(S.Context)) {
11564     Expr::EvalResult Result;
11565     if (E->EvaluateAsInt(Result, S.getASTContext(),
11566                          Expr::SE_AllowSideEffects)) {
11567       if (Result.Val.getInt() != 1 && Result.Val.getInt() != 0) {
11568         adornObjCBoolConversionDiagWithTernaryFixit(
11569             S, E,
11570             S.Diag(CC, diag::warn_impcast_constant_value_to_objc_bool)
11571                 << Result.Val.getInt().toString(10));
11572       }
11573       return;
11574     }
11575   }
11576 
11577   // Check implicit casts from Objective-C collection literals to specialized
11578   // collection types, e.g., NSArray<NSString *> *.
11579   if (auto *ArrayLiteral = dyn_cast<ObjCArrayLiteral>(E))
11580     checkObjCArrayLiteral(S, QualType(Target, 0), ArrayLiteral);
11581   else if (auto *DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(E))
11582     checkObjCDictionaryLiteral(S, QualType(Target, 0), DictionaryLiteral);
11583 
11584   // Strip vector types.
11585   if (isa<VectorType>(Source)) {
11586     if (!isa<VectorType>(Target)) {
11587       if (S.SourceMgr.isInSystemMacro(CC))
11588         return;
11589       return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar);
11590     }
11591 
11592     // If the vector cast is cast between two vectors of the same size, it is
11593     // a bitcast, not a conversion.
11594     if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target))
11595       return;
11596 
11597     Source = cast<VectorType>(Source)->getElementType().getTypePtr();
11598     Target = cast<VectorType>(Target)->getElementType().getTypePtr();
11599   }
11600   if (auto VecTy = dyn_cast<VectorType>(Target))
11601     Target = VecTy->getElementType().getTypePtr();
11602 
11603   // Strip complex types.
11604   if (isa<ComplexType>(Source)) {
11605     if (!isa<ComplexType>(Target)) {
11606       if (S.SourceMgr.isInSystemMacro(CC) || Target->isBooleanType())
11607         return;
11608 
11609       return DiagnoseImpCast(S, E, T, CC,
11610                              S.getLangOpts().CPlusPlus
11611                                  ? diag::err_impcast_complex_scalar
11612                                  : diag::warn_impcast_complex_scalar);
11613     }
11614 
11615     Source = cast<ComplexType>(Source)->getElementType().getTypePtr();
11616     Target = cast<ComplexType>(Target)->getElementType().getTypePtr();
11617   }
11618 
11619   const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source);
11620   const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target);
11621 
11622   // If the source is floating point...
11623   if (SourceBT && SourceBT->isFloatingPoint()) {
11624     // ...and the target is floating point...
11625     if (TargetBT && TargetBT->isFloatingPoint()) {
11626       // ...then warn if we're dropping FP rank.
11627 
11628       int Order = S.getASTContext().getFloatingTypeSemanticOrder(
11629           QualType(SourceBT, 0), QualType(TargetBT, 0));
11630       if (Order > 0) {
11631         // Don't warn about float constants that are precisely
11632         // representable in the target type.
11633         Expr::EvalResult result;
11634         if (E->EvaluateAsRValue(result, S.Context)) {
11635           // Value might be a float, a float vector, or a float complex.
11636           if (IsSameFloatAfterCast(result.Val,
11637                    S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)),
11638                    S.Context.getFloatTypeSemantics(QualType(SourceBT, 0))))
11639             return;
11640         }
11641 
11642         if (S.SourceMgr.isInSystemMacro(CC))
11643           return;
11644 
11645         DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision);
11646       }
11647       // ... or possibly if we're increasing rank, too
11648       else if (Order < 0) {
11649         if (S.SourceMgr.isInSystemMacro(CC))
11650           return;
11651 
11652         DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_double_promotion);
11653       }
11654       return;
11655     }
11656 
11657     // If the target is integral, always warn.
11658     if (TargetBT && TargetBT->isInteger()) {
11659       if (S.SourceMgr.isInSystemMacro(CC))
11660         return;
11661 
11662       DiagnoseFloatingImpCast(S, E, T, CC);
11663     }
11664 
11665     // Detect the case where a call result is converted from floating-point to
11666     // to bool, and the final argument to the call is converted from bool, to
11667     // discover this typo:
11668     //
11669     //    bool b = fabs(x < 1.0);  // should be "bool b = fabs(x) < 1.0;"
11670     //
11671     // FIXME: This is an incredibly special case; is there some more general
11672     // way to detect this class of misplaced-parentheses bug?
11673     if (Target->isBooleanType() && isa<CallExpr>(E)) {
11674       // Check last argument of function call to see if it is an
11675       // implicit cast from a type matching the type the result
11676       // is being cast to.
11677       CallExpr *CEx = cast<CallExpr>(E);
11678       if (unsigned NumArgs = CEx->getNumArgs()) {
11679         Expr *LastA = CEx->getArg(NumArgs - 1);
11680         Expr *InnerE = LastA->IgnoreParenImpCasts();
11681         if (isa<ImplicitCastExpr>(LastA) &&
11682             InnerE->getType()->isBooleanType()) {
11683           // Warn on this floating-point to bool conversion
11684           DiagnoseImpCast(S, E, T, CC,
11685                           diag::warn_impcast_floating_point_to_bool);
11686         }
11687       }
11688     }
11689     return;
11690   }
11691 
11692   // Valid casts involving fixed point types should be accounted for here.
11693   if (Source->isFixedPointType()) {
11694     if (Target->isUnsaturatedFixedPointType()) {
11695       Expr::EvalResult Result;
11696       if (E->EvaluateAsFixedPoint(Result, S.Context, Expr::SE_AllowSideEffects,
11697                                   S.isConstantEvaluated())) {
11698         APFixedPoint Value = Result.Val.getFixedPoint();
11699         APFixedPoint MaxVal = S.Context.getFixedPointMax(T);
11700         APFixedPoint MinVal = S.Context.getFixedPointMin(T);
11701         if (Value > MaxVal || Value < MinVal) {
11702           S.DiagRuntimeBehavior(E->getExprLoc(), E,
11703                                 S.PDiag(diag::warn_impcast_fixed_point_range)
11704                                     << Value.toString() << T
11705                                     << E->getSourceRange()
11706                                     << clang::SourceRange(CC));
11707           return;
11708         }
11709       }
11710     } else if (Target->isIntegerType()) {
11711       Expr::EvalResult Result;
11712       if (!S.isConstantEvaluated() &&
11713           E->EvaluateAsFixedPoint(Result, S.Context,
11714                                   Expr::SE_AllowSideEffects)) {
11715         APFixedPoint FXResult = Result.Val.getFixedPoint();
11716 
11717         bool Overflowed;
11718         llvm::APSInt IntResult = FXResult.convertToInt(
11719             S.Context.getIntWidth(T),
11720             Target->isSignedIntegerOrEnumerationType(), &Overflowed);
11721 
11722         if (Overflowed) {
11723           S.DiagRuntimeBehavior(E->getExprLoc(), E,
11724                                 S.PDiag(diag::warn_impcast_fixed_point_range)
11725                                     << FXResult.toString() << T
11726                                     << E->getSourceRange()
11727                                     << clang::SourceRange(CC));
11728           return;
11729         }
11730       }
11731     }
11732   } else if (Target->isUnsaturatedFixedPointType()) {
11733     if (Source->isIntegerType()) {
11734       Expr::EvalResult Result;
11735       if (!S.isConstantEvaluated() &&
11736           E->EvaluateAsInt(Result, S.Context, Expr::SE_AllowSideEffects)) {
11737         llvm::APSInt Value = Result.Val.getInt();
11738 
11739         bool Overflowed;
11740         APFixedPoint IntResult = APFixedPoint::getFromIntValue(
11741             Value, S.Context.getFixedPointSemantics(T), &Overflowed);
11742 
11743         if (Overflowed) {
11744           S.DiagRuntimeBehavior(E->getExprLoc(), E,
11745                                 S.PDiag(diag::warn_impcast_fixed_point_range)
11746                                     << Value.toString(/*Radix=*/10) << T
11747                                     << E->getSourceRange()
11748                                     << clang::SourceRange(CC));
11749           return;
11750         }
11751       }
11752     }
11753   }
11754 
11755   // If we are casting an integer type to a floating point type without
11756   // initialization-list syntax, we might lose accuracy if the floating
11757   // point type has a narrower significand than the integer type.
11758   if (SourceBT && TargetBT && SourceBT->isIntegerType() &&
11759       TargetBT->isFloatingType() && !IsListInit) {
11760     // Determine the number of precision bits in the source integer type.
11761     IntRange SourceRange = GetExprRange(S.Context, E, S.isConstantEvaluated());
11762     unsigned int SourcePrecision = SourceRange.Width;
11763 
11764     // Determine the number of precision bits in the
11765     // target floating point type.
11766     unsigned int TargetPrecision = llvm::APFloatBase::semanticsPrecision(
11767         S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)));
11768 
11769     if (SourcePrecision > 0 && TargetPrecision > 0 &&
11770         SourcePrecision > TargetPrecision) {
11771 
11772       llvm::APSInt SourceInt;
11773       if (E->isIntegerConstantExpr(SourceInt, S.Context)) {
11774         // If the source integer is a constant, convert it to the target
11775         // floating point type. Issue a warning if the value changes
11776         // during the whole conversion.
11777         llvm::APFloat TargetFloatValue(
11778             S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)));
11779         llvm::APFloat::opStatus ConversionStatus =
11780             TargetFloatValue.convertFromAPInt(
11781                 SourceInt, SourceBT->isSignedInteger(),
11782                 llvm::APFloat::rmNearestTiesToEven);
11783 
11784         if (ConversionStatus != llvm::APFloat::opOK) {
11785           std::string PrettySourceValue = SourceInt.toString(10);
11786           SmallString<32> PrettyTargetValue;
11787           TargetFloatValue.toString(PrettyTargetValue, TargetPrecision);
11788 
11789           S.DiagRuntimeBehavior(
11790               E->getExprLoc(), E,
11791               S.PDiag(diag::warn_impcast_integer_float_precision_constant)
11792                   << PrettySourceValue << PrettyTargetValue << E->getType() << T
11793                   << E->getSourceRange() << clang::SourceRange(CC));
11794         }
11795       } else {
11796         // Otherwise, the implicit conversion may lose precision.
11797         DiagnoseImpCast(S, E, T, CC,
11798                         diag::warn_impcast_integer_float_precision);
11799       }
11800     }
11801   }
11802 
11803   DiagnoseNullConversion(S, E, T, CC);
11804 
11805   S.DiscardMisalignedMemberAddress(Target, E);
11806 
11807   if (Target->isBooleanType())
11808     DiagnoseIntInBoolContext(S, E);
11809 
11810   if (!Source->isIntegerType() || !Target->isIntegerType())
11811     return;
11812 
11813   // TODO: remove this early return once the false positives for constant->bool
11814   // in templates, macros, etc, are reduced or removed.
11815   if (Target->isSpecificBuiltinType(BuiltinType::Bool))
11816     return;
11817 
11818   if (isObjCSignedCharBool(S, T) && !Source->isCharType() &&
11819       !E->isKnownToHaveBooleanValue(/*Semantic=*/false)) {
11820     return adornObjCBoolConversionDiagWithTernaryFixit(
11821         S, E,
11822         S.Diag(CC, diag::warn_impcast_int_to_objc_signed_char_bool)
11823             << E->getType());
11824   }
11825 
11826   IntRange SourceRange = GetExprRange(S.Context, E, S.isConstantEvaluated());
11827   IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target);
11828 
11829   if (SourceRange.Width > TargetRange.Width) {
11830     // If the source is a constant, use a default-on diagnostic.
11831     // TODO: this should happen for bitfield stores, too.
11832     Expr::EvalResult Result;
11833     if (E->EvaluateAsInt(Result, S.Context, Expr::SE_AllowSideEffects,
11834                          S.isConstantEvaluated())) {
11835       llvm::APSInt Value(32);
11836       Value = Result.Val.getInt();
11837 
11838       if (S.SourceMgr.isInSystemMacro(CC))
11839         return;
11840 
11841       std::string PrettySourceValue = Value.toString(10);
11842       std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange);
11843 
11844       S.DiagRuntimeBehavior(
11845           E->getExprLoc(), E,
11846           S.PDiag(diag::warn_impcast_integer_precision_constant)
11847               << PrettySourceValue << PrettyTargetValue << E->getType() << T
11848               << E->getSourceRange() << clang::SourceRange(CC));
11849       return;
11850     }
11851 
11852     // People want to build with -Wshorten-64-to-32 and not -Wconversion.
11853     if (S.SourceMgr.isInSystemMacro(CC))
11854       return;
11855 
11856     if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64)
11857       return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32,
11858                              /* pruneControlFlow */ true);
11859     return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision);
11860   }
11861 
11862   if (TargetRange.Width > SourceRange.Width) {
11863     if (auto *UO = dyn_cast<UnaryOperator>(E))
11864       if (UO->getOpcode() == UO_Minus)
11865         if (Source->isUnsignedIntegerType()) {
11866           if (Target->isUnsignedIntegerType())
11867             return DiagnoseImpCast(S, E, T, CC,
11868                                    diag::warn_impcast_high_order_zero_bits);
11869           if (Target->isSignedIntegerType())
11870             return DiagnoseImpCast(S, E, T, CC,
11871                                    diag::warn_impcast_nonnegative_result);
11872         }
11873   }
11874 
11875   if (TargetRange.Width == SourceRange.Width && !TargetRange.NonNegative &&
11876       SourceRange.NonNegative && Source->isSignedIntegerType()) {
11877     // Warn when doing a signed to signed conversion, warn if the positive
11878     // source value is exactly the width of the target type, which will
11879     // cause a negative value to be stored.
11880 
11881     Expr::EvalResult Result;
11882     if (E->EvaluateAsInt(Result, S.Context, Expr::SE_AllowSideEffects) &&
11883         !S.SourceMgr.isInSystemMacro(CC)) {
11884       llvm::APSInt Value = Result.Val.getInt();
11885       if (isSameWidthConstantConversion(S, E, T, CC)) {
11886         std::string PrettySourceValue = Value.toString(10);
11887         std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange);
11888 
11889         S.DiagRuntimeBehavior(
11890             E->getExprLoc(), E,
11891             S.PDiag(diag::warn_impcast_integer_precision_constant)
11892                 << PrettySourceValue << PrettyTargetValue << E->getType() << T
11893                 << E->getSourceRange() << clang::SourceRange(CC));
11894         return;
11895       }
11896     }
11897 
11898     // Fall through for non-constants to give a sign conversion warning.
11899   }
11900 
11901   if ((TargetRange.NonNegative && !SourceRange.NonNegative) ||
11902       (!TargetRange.NonNegative && SourceRange.NonNegative &&
11903        SourceRange.Width == TargetRange.Width)) {
11904     if (S.SourceMgr.isInSystemMacro(CC))
11905       return;
11906 
11907     unsigned DiagID = diag::warn_impcast_integer_sign;
11908 
11909     // Traditionally, gcc has warned about this under -Wsign-compare.
11910     // We also want to warn about it in -Wconversion.
11911     // So if -Wconversion is off, use a completely identical diagnostic
11912     // in the sign-compare group.
11913     // The conditional-checking code will
11914     if (ICContext) {
11915       DiagID = diag::warn_impcast_integer_sign_conditional;
11916       *ICContext = true;
11917     }
11918 
11919     return DiagnoseImpCast(S, E, T, CC, DiagID);
11920   }
11921 
11922   // Diagnose conversions between different enumeration types.
11923   // In C, we pretend that the type of an EnumConstantDecl is its enumeration
11924   // type, to give us better diagnostics.
11925   QualType SourceType = E->getType();
11926   if (!S.getLangOpts().CPlusPlus) {
11927     if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
11928       if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
11929         EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext());
11930         SourceType = S.Context.getTypeDeclType(Enum);
11931         Source = S.Context.getCanonicalType(SourceType).getTypePtr();
11932       }
11933   }
11934 
11935   if (const EnumType *SourceEnum = Source->getAs<EnumType>())
11936     if (const EnumType *TargetEnum = Target->getAs<EnumType>())
11937       if (SourceEnum->getDecl()->hasNameForLinkage() &&
11938           TargetEnum->getDecl()->hasNameForLinkage() &&
11939           SourceEnum != TargetEnum) {
11940         if (S.SourceMgr.isInSystemMacro(CC))
11941           return;
11942 
11943         return DiagnoseImpCast(S, E, SourceType, T, CC,
11944                                diag::warn_impcast_different_enum_types);
11945       }
11946 }
11947 
11948 static void CheckConditionalOperator(Sema &S, AbstractConditionalOperator *E,
11949                                      SourceLocation CC, QualType T);
11950 
11951 static void CheckConditionalOperand(Sema &S, Expr *E, QualType T,
11952                                     SourceLocation CC, bool &ICContext) {
11953   E = E->IgnoreParenImpCasts();
11954 
11955   if (auto *CO = dyn_cast<AbstractConditionalOperator>(E))
11956     return CheckConditionalOperator(S, CO, CC, T);
11957 
11958   AnalyzeImplicitConversions(S, E, CC);
11959   if (E->getType() != T)
11960     return CheckImplicitConversion(S, E, T, CC, &ICContext);
11961 }
11962 
11963 static void CheckConditionalOperator(Sema &S, AbstractConditionalOperator *E,
11964                                      SourceLocation CC, QualType T) {
11965   AnalyzeImplicitConversions(S, E->getCond(), E->getQuestionLoc());
11966 
11967   Expr *TrueExpr = E->getTrueExpr();
11968   if (auto *BCO = dyn_cast<BinaryConditionalOperator>(E))
11969     TrueExpr = BCO->getCommon();
11970 
11971   bool Suspicious = false;
11972   CheckConditionalOperand(S, TrueExpr, T, CC, Suspicious);
11973   CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious);
11974 
11975   if (T->isBooleanType())
11976     DiagnoseIntInBoolContext(S, E);
11977 
11978   // If -Wconversion would have warned about either of the candidates
11979   // for a signedness conversion to the context type...
11980   if (!Suspicious) return;
11981 
11982   // ...but it's currently ignored...
11983   if (!S.Diags.isIgnored(diag::warn_impcast_integer_sign_conditional, CC))
11984     return;
11985 
11986   // ...then check whether it would have warned about either of the
11987   // candidates for a signedness conversion to the condition type.
11988   if (E->getType() == T) return;
11989 
11990   Suspicious = false;
11991   CheckImplicitConversion(S, TrueExpr->IgnoreParenImpCasts(),
11992                           E->getType(), CC, &Suspicious);
11993   if (!Suspicious)
11994     CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(),
11995                             E->getType(), CC, &Suspicious);
11996 }
11997 
11998 /// Check conversion of given expression to boolean.
11999 /// Input argument E is a logical expression.
12000 static void CheckBoolLikeConversion(Sema &S, Expr *E, SourceLocation CC) {
12001   if (S.getLangOpts().Bool)
12002     return;
12003   if (E->IgnoreParenImpCasts()->getType()->isAtomicType())
12004     return;
12005   CheckImplicitConversion(S, E->IgnoreParenImpCasts(), S.Context.BoolTy, CC);
12006 }
12007 
12008 namespace {
12009 struct AnalyzeImplicitConversionsWorkItem {
12010   Expr *E;
12011   SourceLocation CC;
12012   bool IsListInit;
12013 };
12014 }
12015 
12016 /// Data recursive variant of AnalyzeImplicitConversions. Subexpressions
12017 /// that should be visited are added to WorkList.
12018 static void AnalyzeImplicitConversions(
12019     Sema &S, AnalyzeImplicitConversionsWorkItem Item,
12020     llvm::SmallVectorImpl<AnalyzeImplicitConversionsWorkItem> &WorkList) {
12021   Expr *OrigE = Item.E;
12022   SourceLocation CC = Item.CC;
12023 
12024   QualType T = OrigE->getType();
12025   Expr *E = OrigE->IgnoreParenImpCasts();
12026 
12027   // Propagate whether we are in a C++ list initialization expression.
12028   // If so, we do not issue warnings for implicit int-float conversion
12029   // precision loss, because C++11 narrowing already handles it.
12030   bool IsListInit = Item.IsListInit ||
12031                     (isa<InitListExpr>(OrigE) && S.getLangOpts().CPlusPlus);
12032 
12033   if (E->isTypeDependent() || E->isValueDependent())
12034     return;
12035 
12036   Expr *SourceExpr = E;
12037   // Examine, but don't traverse into the source expression of an
12038   // OpaqueValueExpr, since it may have multiple parents and we don't want to
12039   // emit duplicate diagnostics. Its fine to examine the form or attempt to
12040   // evaluate it in the context of checking the specific conversion to T though.
12041   if (auto *OVE = dyn_cast<OpaqueValueExpr>(E))
12042     if (auto *Src = OVE->getSourceExpr())
12043       SourceExpr = Src;
12044 
12045   if (const auto *UO = dyn_cast<UnaryOperator>(SourceExpr))
12046     if (UO->getOpcode() == UO_Not &&
12047         UO->getSubExpr()->isKnownToHaveBooleanValue())
12048       S.Diag(UO->getBeginLoc(), diag::warn_bitwise_negation_bool)
12049           << OrigE->getSourceRange() << T->isBooleanType()
12050           << FixItHint::CreateReplacement(UO->getBeginLoc(), "!");
12051 
12052   // For conditional operators, we analyze the arguments as if they
12053   // were being fed directly into the output.
12054   if (auto *CO = dyn_cast<AbstractConditionalOperator>(SourceExpr)) {
12055     CheckConditionalOperator(S, CO, CC, T);
12056     return;
12057   }
12058 
12059   // Check implicit argument conversions for function calls.
12060   if (CallExpr *Call = dyn_cast<CallExpr>(SourceExpr))
12061     CheckImplicitArgumentConversions(S, Call, CC);
12062 
12063   // Go ahead and check any implicit conversions we might have skipped.
12064   // The non-canonical typecheck is just an optimization;
12065   // CheckImplicitConversion will filter out dead implicit conversions.
12066   if (SourceExpr->getType() != T)
12067     CheckImplicitConversion(S, SourceExpr, T, CC, nullptr, IsListInit);
12068 
12069   // Now continue drilling into this expression.
12070 
12071   if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
12072     // The bound subexpressions in a PseudoObjectExpr are not reachable
12073     // as transitive children.
12074     // FIXME: Use a more uniform representation for this.
12075     for (auto *SE : POE->semantics())
12076       if (auto *OVE = dyn_cast<OpaqueValueExpr>(SE))
12077         WorkList.push_back({OVE->getSourceExpr(), CC, IsListInit});
12078   }
12079 
12080   // Skip past explicit casts.
12081   if (auto *CE = dyn_cast<ExplicitCastExpr>(E)) {
12082     E = CE->getSubExpr()->IgnoreParenImpCasts();
12083     if (!CE->getType()->isVoidType() && E->getType()->isAtomicType())
12084       S.Diag(E->getBeginLoc(), diag::warn_atomic_implicit_seq_cst);
12085     WorkList.push_back({E, CC, IsListInit});
12086     return;
12087   }
12088 
12089   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
12090     // Do a somewhat different check with comparison operators.
12091     if (BO->isComparisonOp())
12092       return AnalyzeComparison(S, BO);
12093 
12094     // And with simple assignments.
12095     if (BO->getOpcode() == BO_Assign)
12096       return AnalyzeAssignment(S, BO);
12097     // And with compound assignments.
12098     if (BO->isAssignmentOp())
12099       return AnalyzeCompoundAssignment(S, BO);
12100   }
12101 
12102   // These break the otherwise-useful invariant below.  Fortunately,
12103   // we don't really need to recurse into them, because any internal
12104   // expressions should have been analyzed already when they were
12105   // built into statements.
12106   if (isa<StmtExpr>(E)) return;
12107 
12108   // Don't descend into unevaluated contexts.
12109   if (isa<UnaryExprOrTypeTraitExpr>(E)) return;
12110 
12111   // Now just recurse over the expression's children.
12112   CC = E->getExprLoc();
12113   BinaryOperator *BO = dyn_cast<BinaryOperator>(E);
12114   bool IsLogicalAndOperator = BO && BO->getOpcode() == BO_LAnd;
12115   for (Stmt *SubStmt : E->children()) {
12116     Expr *ChildExpr = dyn_cast_or_null<Expr>(SubStmt);
12117     if (!ChildExpr)
12118       continue;
12119 
12120     if (IsLogicalAndOperator &&
12121         isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts()))
12122       // Ignore checking string literals that are in logical and operators.
12123       // This is a common pattern for asserts.
12124       continue;
12125     WorkList.push_back({ChildExpr, CC, IsListInit});
12126   }
12127 
12128   if (BO && BO->isLogicalOp()) {
12129     Expr *SubExpr = BO->getLHS()->IgnoreParenImpCasts();
12130     if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr))
12131       ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc());
12132 
12133     SubExpr = BO->getRHS()->IgnoreParenImpCasts();
12134     if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr))
12135       ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc());
12136   }
12137 
12138   if (const UnaryOperator *U = dyn_cast<UnaryOperator>(E)) {
12139     if (U->getOpcode() == UO_LNot) {
12140       ::CheckBoolLikeConversion(S, U->getSubExpr(), CC);
12141     } else if (U->getOpcode() != UO_AddrOf) {
12142       if (U->getSubExpr()->getType()->isAtomicType())
12143         S.Diag(U->getSubExpr()->getBeginLoc(),
12144                diag::warn_atomic_implicit_seq_cst);
12145     }
12146   }
12147 }
12148 
12149 /// AnalyzeImplicitConversions - Find and report any interesting
12150 /// implicit conversions in the given expression.  There are a couple
12151 /// of competing diagnostics here, -Wconversion and -Wsign-compare.
12152 static void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC,
12153                                        bool IsListInit/*= false*/) {
12154   llvm::SmallVector<AnalyzeImplicitConversionsWorkItem, 16> WorkList;
12155   WorkList.push_back({OrigE, CC, IsListInit});
12156   while (!WorkList.empty())
12157     AnalyzeImplicitConversions(S, WorkList.pop_back_val(), WorkList);
12158 }
12159 
12160 /// Diagnose integer type and any valid implicit conversion to it.
12161 static bool checkOpenCLEnqueueIntType(Sema &S, Expr *E, const QualType &IntT) {
12162   // Taking into account implicit conversions,
12163   // allow any integer.
12164   if (!E->getType()->isIntegerType()) {
12165     S.Diag(E->getBeginLoc(),
12166            diag::err_opencl_enqueue_kernel_invalid_local_size_type);
12167     return true;
12168   }
12169   // Potentially emit standard warnings for implicit conversions if enabled
12170   // using -Wconversion.
12171   CheckImplicitConversion(S, E, IntT, E->getBeginLoc());
12172   return false;
12173 }
12174 
12175 // Helper function for Sema::DiagnoseAlwaysNonNullPointer.
12176 // Returns true when emitting a warning about taking the address of a reference.
12177 static bool CheckForReference(Sema &SemaRef, const Expr *E,
12178                               const PartialDiagnostic &PD) {
12179   E = E->IgnoreParenImpCasts();
12180 
12181   const FunctionDecl *FD = nullptr;
12182 
12183   if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
12184     if (!DRE->getDecl()->getType()->isReferenceType())
12185       return false;
12186   } else if (const MemberExpr *M = dyn_cast<MemberExpr>(E)) {
12187     if (!M->getMemberDecl()->getType()->isReferenceType())
12188       return false;
12189   } else if (const CallExpr *Call = dyn_cast<CallExpr>(E)) {
12190     if (!Call->getCallReturnType(SemaRef.Context)->isReferenceType())
12191       return false;
12192     FD = Call->getDirectCallee();
12193   } else {
12194     return false;
12195   }
12196 
12197   SemaRef.Diag(E->getExprLoc(), PD);
12198 
12199   // If possible, point to location of function.
12200   if (FD) {
12201     SemaRef.Diag(FD->getLocation(), diag::note_reference_is_return_value) << FD;
12202   }
12203 
12204   return true;
12205 }
12206 
12207 // Returns true if the SourceLocation is expanded from any macro body.
12208 // Returns false if the SourceLocation is invalid, is from not in a macro
12209 // expansion, or is from expanded from a top-level macro argument.
12210 static bool IsInAnyMacroBody(const SourceManager &SM, SourceLocation Loc) {
12211   if (Loc.isInvalid())
12212     return false;
12213 
12214   while (Loc.isMacroID()) {
12215     if (SM.isMacroBodyExpansion(Loc))
12216       return true;
12217     Loc = SM.getImmediateMacroCallerLoc(Loc);
12218   }
12219 
12220   return false;
12221 }
12222 
12223 /// Diagnose pointers that are always non-null.
12224 /// \param E the expression containing the pointer
12225 /// \param NullKind NPCK_NotNull if E is a cast to bool, otherwise, E is
12226 /// compared to a null pointer
12227 /// \param IsEqual True when the comparison is equal to a null pointer
12228 /// \param Range Extra SourceRange to highlight in the diagnostic
12229 void Sema::DiagnoseAlwaysNonNullPointer(Expr *E,
12230                                         Expr::NullPointerConstantKind NullKind,
12231                                         bool IsEqual, SourceRange Range) {
12232   if (!E)
12233     return;
12234 
12235   // Don't warn inside macros.
12236   if (E->getExprLoc().isMacroID()) {
12237     const SourceManager &SM = getSourceManager();
12238     if (IsInAnyMacroBody(SM, E->getExprLoc()) ||
12239         IsInAnyMacroBody(SM, Range.getBegin()))
12240       return;
12241   }
12242   E = E->IgnoreImpCasts();
12243 
12244   const bool IsCompare = NullKind != Expr::NPCK_NotNull;
12245 
12246   if (isa<CXXThisExpr>(E)) {
12247     unsigned DiagID = IsCompare ? diag::warn_this_null_compare
12248                                 : diag::warn_this_bool_conversion;
12249     Diag(E->getExprLoc(), DiagID) << E->getSourceRange() << Range << IsEqual;
12250     return;
12251   }
12252 
12253   bool IsAddressOf = false;
12254 
12255   if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
12256     if (UO->getOpcode() != UO_AddrOf)
12257       return;
12258     IsAddressOf = true;
12259     E = UO->getSubExpr();
12260   }
12261 
12262   if (IsAddressOf) {
12263     unsigned DiagID = IsCompare
12264                           ? diag::warn_address_of_reference_null_compare
12265                           : diag::warn_address_of_reference_bool_conversion;
12266     PartialDiagnostic PD = PDiag(DiagID) << E->getSourceRange() << Range
12267                                          << IsEqual;
12268     if (CheckForReference(*this, E, PD)) {
12269       return;
12270     }
12271   }
12272 
12273   auto ComplainAboutNonnullParamOrCall = [&](const Attr *NonnullAttr) {
12274     bool IsParam = isa<NonNullAttr>(NonnullAttr);
12275     std::string Str;
12276     llvm::raw_string_ostream S(Str);
12277     E->printPretty(S, nullptr, getPrintingPolicy());
12278     unsigned DiagID = IsCompare ? diag::warn_nonnull_expr_compare
12279                                 : diag::warn_cast_nonnull_to_bool;
12280     Diag(E->getExprLoc(), DiagID) << IsParam << S.str()
12281       << E->getSourceRange() << Range << IsEqual;
12282     Diag(NonnullAttr->getLocation(), diag::note_declared_nonnull) << IsParam;
12283   };
12284 
12285   // If we have a CallExpr that is tagged with returns_nonnull, we can complain.
12286   if (auto *Call = dyn_cast<CallExpr>(E->IgnoreParenImpCasts())) {
12287     if (auto *Callee = Call->getDirectCallee()) {
12288       if (const Attr *A = Callee->getAttr<ReturnsNonNullAttr>()) {
12289         ComplainAboutNonnullParamOrCall(A);
12290         return;
12291       }
12292     }
12293   }
12294 
12295   // Expect to find a single Decl.  Skip anything more complicated.
12296   ValueDecl *D = nullptr;
12297   if (DeclRefExpr *R = dyn_cast<DeclRefExpr>(E)) {
12298     D = R->getDecl();
12299   } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) {
12300     D = M->getMemberDecl();
12301   }
12302 
12303   // Weak Decls can be null.
12304   if (!D || D->isWeak())
12305     return;
12306 
12307   // Check for parameter decl with nonnull attribute
12308   if (const auto* PV = dyn_cast<ParmVarDecl>(D)) {
12309     if (getCurFunction() &&
12310         !getCurFunction()->ModifiedNonNullParams.count(PV)) {
12311       if (const Attr *A = PV->getAttr<NonNullAttr>()) {
12312         ComplainAboutNonnullParamOrCall(A);
12313         return;
12314       }
12315 
12316       if (const auto *FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) {
12317         // Skip function template not specialized yet.
12318         if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate)
12319           return;
12320         auto ParamIter = llvm::find(FD->parameters(), PV);
12321         assert(ParamIter != FD->param_end());
12322         unsigned ParamNo = std::distance(FD->param_begin(), ParamIter);
12323 
12324         for (const auto *NonNull : FD->specific_attrs<NonNullAttr>()) {
12325           if (!NonNull->args_size()) {
12326               ComplainAboutNonnullParamOrCall(NonNull);
12327               return;
12328           }
12329 
12330           for (const ParamIdx &ArgNo : NonNull->args()) {
12331             if (ArgNo.getASTIndex() == ParamNo) {
12332               ComplainAboutNonnullParamOrCall(NonNull);
12333               return;
12334             }
12335           }
12336         }
12337       }
12338     }
12339   }
12340 
12341   QualType T = D->getType();
12342   const bool IsArray = T->isArrayType();
12343   const bool IsFunction = T->isFunctionType();
12344 
12345   // Address of function is used to silence the function warning.
12346   if (IsAddressOf && IsFunction) {
12347     return;
12348   }
12349 
12350   // Found nothing.
12351   if (!IsAddressOf && !IsFunction && !IsArray)
12352     return;
12353 
12354   // Pretty print the expression for the diagnostic.
12355   std::string Str;
12356   llvm::raw_string_ostream S(Str);
12357   E->printPretty(S, nullptr, getPrintingPolicy());
12358 
12359   unsigned DiagID = IsCompare ? diag::warn_null_pointer_compare
12360                               : diag::warn_impcast_pointer_to_bool;
12361   enum {
12362     AddressOf,
12363     FunctionPointer,
12364     ArrayPointer
12365   } DiagType;
12366   if (IsAddressOf)
12367     DiagType = AddressOf;
12368   else if (IsFunction)
12369     DiagType = FunctionPointer;
12370   else if (IsArray)
12371     DiagType = ArrayPointer;
12372   else
12373     llvm_unreachable("Could not determine diagnostic.");
12374   Diag(E->getExprLoc(), DiagID) << DiagType << S.str() << E->getSourceRange()
12375                                 << Range << IsEqual;
12376 
12377   if (!IsFunction)
12378     return;
12379 
12380   // Suggest '&' to silence the function warning.
12381   Diag(E->getExprLoc(), diag::note_function_warning_silence)
12382       << FixItHint::CreateInsertion(E->getBeginLoc(), "&");
12383 
12384   // Check to see if '()' fixit should be emitted.
12385   QualType ReturnType;
12386   UnresolvedSet<4> NonTemplateOverloads;
12387   tryExprAsCall(*E, ReturnType, NonTemplateOverloads);
12388   if (ReturnType.isNull())
12389     return;
12390 
12391   if (IsCompare) {
12392     // There are two cases here.  If there is null constant, the only suggest
12393     // for a pointer return type.  If the null is 0, then suggest if the return
12394     // type is a pointer or an integer type.
12395     if (!ReturnType->isPointerType()) {
12396       if (NullKind == Expr::NPCK_ZeroExpression ||
12397           NullKind == Expr::NPCK_ZeroLiteral) {
12398         if (!ReturnType->isIntegerType())
12399           return;
12400       } else {
12401         return;
12402       }
12403     }
12404   } else { // !IsCompare
12405     // For function to bool, only suggest if the function pointer has bool
12406     // return type.
12407     if (!ReturnType->isSpecificBuiltinType(BuiltinType::Bool))
12408       return;
12409   }
12410   Diag(E->getExprLoc(), diag::note_function_to_function_call)
12411       << FixItHint::CreateInsertion(getLocForEndOfToken(E->getEndLoc()), "()");
12412 }
12413 
12414 /// Diagnoses "dangerous" implicit conversions within the given
12415 /// expression (which is a full expression).  Implements -Wconversion
12416 /// and -Wsign-compare.
12417 ///
12418 /// \param CC the "context" location of the implicit conversion, i.e.
12419 ///   the most location of the syntactic entity requiring the implicit
12420 ///   conversion
12421 void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) {
12422   // Don't diagnose in unevaluated contexts.
12423   if (isUnevaluatedContext())
12424     return;
12425 
12426   // Don't diagnose for value- or type-dependent expressions.
12427   if (E->isTypeDependent() || E->isValueDependent())
12428     return;
12429 
12430   // Check for array bounds violations in cases where the check isn't triggered
12431   // elsewhere for other Expr types (like BinaryOperators), e.g. when an
12432   // ArraySubscriptExpr is on the RHS of a variable initialization.
12433   CheckArrayAccess(E);
12434 
12435   // This is not the right CC for (e.g.) a variable initialization.
12436   AnalyzeImplicitConversions(*this, E, CC);
12437 }
12438 
12439 /// CheckBoolLikeConversion - Check conversion of given expression to boolean.
12440 /// Input argument E is a logical expression.
12441 void Sema::CheckBoolLikeConversion(Expr *E, SourceLocation CC) {
12442   ::CheckBoolLikeConversion(*this, E, CC);
12443 }
12444 
12445 /// Diagnose when expression is an integer constant expression and its evaluation
12446 /// results in integer overflow
12447 void Sema::CheckForIntOverflow (Expr *E) {
12448   // Use a work list to deal with nested struct initializers.
12449   SmallVector<Expr *, 2> Exprs(1, E);
12450 
12451   do {
12452     Expr *OriginalE = Exprs.pop_back_val();
12453     Expr *E = OriginalE->IgnoreParenCasts();
12454 
12455     if (isa<BinaryOperator>(E)) {
12456       E->EvaluateForOverflow(Context);
12457       continue;
12458     }
12459 
12460     if (auto InitList = dyn_cast<InitListExpr>(OriginalE))
12461       Exprs.append(InitList->inits().begin(), InitList->inits().end());
12462     else if (isa<ObjCBoxedExpr>(OriginalE))
12463       E->EvaluateForOverflow(Context);
12464     else if (auto Call = dyn_cast<CallExpr>(E))
12465       Exprs.append(Call->arg_begin(), Call->arg_end());
12466     else if (auto Message = dyn_cast<ObjCMessageExpr>(E))
12467       Exprs.append(Message->arg_begin(), Message->arg_end());
12468   } while (!Exprs.empty());
12469 }
12470 
12471 namespace {
12472 
12473 /// Visitor for expressions which looks for unsequenced operations on the
12474 /// same object.
12475 class SequenceChecker : public ConstEvaluatedExprVisitor<SequenceChecker> {
12476   using Base = ConstEvaluatedExprVisitor<SequenceChecker>;
12477 
12478   /// A tree of sequenced regions within an expression. Two regions are
12479   /// unsequenced if one is an ancestor or a descendent of the other. When we
12480   /// finish processing an expression with sequencing, such as a comma
12481   /// expression, we fold its tree nodes into its parent, since they are
12482   /// unsequenced with respect to nodes we will visit later.
12483   class SequenceTree {
12484     struct Value {
12485       explicit Value(unsigned Parent) : Parent(Parent), Merged(false) {}
12486       unsigned Parent : 31;
12487       unsigned Merged : 1;
12488     };
12489     SmallVector<Value, 8> Values;
12490 
12491   public:
12492     /// A region within an expression which may be sequenced with respect
12493     /// to some other region.
12494     class Seq {
12495       friend class SequenceTree;
12496 
12497       unsigned Index;
12498 
12499       explicit Seq(unsigned N) : Index(N) {}
12500 
12501     public:
12502       Seq() : Index(0) {}
12503     };
12504 
12505     SequenceTree() { Values.push_back(Value(0)); }
12506     Seq root() const { return Seq(0); }
12507 
12508     /// Create a new sequence of operations, which is an unsequenced
12509     /// subset of \p Parent. This sequence of operations is sequenced with
12510     /// respect to other children of \p Parent.
12511     Seq allocate(Seq Parent) {
12512       Values.push_back(Value(Parent.Index));
12513       return Seq(Values.size() - 1);
12514     }
12515 
12516     /// Merge a sequence of operations into its parent.
12517     void merge(Seq S) {
12518       Values[S.Index].Merged = true;
12519     }
12520 
12521     /// Determine whether two operations are unsequenced. This operation
12522     /// is asymmetric: \p Cur should be the more recent sequence, and \p Old
12523     /// should have been merged into its parent as appropriate.
12524     bool isUnsequenced(Seq Cur, Seq Old) {
12525       unsigned C = representative(Cur.Index);
12526       unsigned Target = representative(Old.Index);
12527       while (C >= Target) {
12528         if (C == Target)
12529           return true;
12530         C = Values[C].Parent;
12531       }
12532       return false;
12533     }
12534 
12535   private:
12536     /// Pick a representative for a sequence.
12537     unsigned representative(unsigned K) {
12538       if (Values[K].Merged)
12539         // Perform path compression as we go.
12540         return Values[K].Parent = representative(Values[K].Parent);
12541       return K;
12542     }
12543   };
12544 
12545   /// An object for which we can track unsequenced uses.
12546   using Object = const NamedDecl *;
12547 
12548   /// Different flavors of object usage which we track. We only track the
12549   /// least-sequenced usage of each kind.
12550   enum UsageKind {
12551     /// A read of an object. Multiple unsequenced reads are OK.
12552     UK_Use,
12553 
12554     /// A modification of an object which is sequenced before the value
12555     /// computation of the expression, such as ++n in C++.
12556     UK_ModAsValue,
12557 
12558     /// A modification of an object which is not sequenced before the value
12559     /// computation of the expression, such as n++.
12560     UK_ModAsSideEffect,
12561 
12562     UK_Count = UK_ModAsSideEffect + 1
12563   };
12564 
12565   /// Bundle together a sequencing region and the expression corresponding
12566   /// to a specific usage. One Usage is stored for each usage kind in UsageInfo.
12567   struct Usage {
12568     const Expr *UsageExpr;
12569     SequenceTree::Seq Seq;
12570 
12571     Usage() : UsageExpr(nullptr), Seq() {}
12572   };
12573 
12574   struct UsageInfo {
12575     Usage Uses[UK_Count];
12576 
12577     /// Have we issued a diagnostic for this object already?
12578     bool Diagnosed;
12579 
12580     UsageInfo() : Uses(), Diagnosed(false) {}
12581   };
12582   using UsageInfoMap = llvm::SmallDenseMap<Object, UsageInfo, 16>;
12583 
12584   Sema &SemaRef;
12585 
12586   /// Sequenced regions within the expression.
12587   SequenceTree Tree;
12588 
12589   /// Declaration modifications and references which we have seen.
12590   UsageInfoMap UsageMap;
12591 
12592   /// The region we are currently within.
12593   SequenceTree::Seq Region;
12594 
12595   /// Filled in with declarations which were modified as a side-effect
12596   /// (that is, post-increment operations).
12597   SmallVectorImpl<std::pair<Object, Usage>> *ModAsSideEffect = nullptr;
12598 
12599   /// Expressions to check later. We defer checking these to reduce
12600   /// stack usage.
12601   SmallVectorImpl<const Expr *> &WorkList;
12602 
12603   /// RAII object wrapping the visitation of a sequenced subexpression of an
12604   /// expression. At the end of this process, the side-effects of the evaluation
12605   /// become sequenced with respect to the value computation of the result, so
12606   /// we downgrade any UK_ModAsSideEffect within the evaluation to
12607   /// UK_ModAsValue.
12608   struct SequencedSubexpression {
12609     SequencedSubexpression(SequenceChecker &Self)
12610       : Self(Self), OldModAsSideEffect(Self.ModAsSideEffect) {
12611       Self.ModAsSideEffect = &ModAsSideEffect;
12612     }
12613 
12614     ~SequencedSubexpression() {
12615       for (const std::pair<Object, Usage> &M : llvm::reverse(ModAsSideEffect)) {
12616         // Add a new usage with usage kind UK_ModAsValue, and then restore
12617         // the previous usage with UK_ModAsSideEffect (thus clearing it if
12618         // the previous one was empty).
12619         UsageInfo &UI = Self.UsageMap[M.first];
12620         auto &SideEffectUsage = UI.Uses[UK_ModAsSideEffect];
12621         Self.addUsage(M.first, UI, SideEffectUsage.UsageExpr, UK_ModAsValue);
12622         SideEffectUsage = M.second;
12623       }
12624       Self.ModAsSideEffect = OldModAsSideEffect;
12625     }
12626 
12627     SequenceChecker &Self;
12628     SmallVector<std::pair<Object, Usage>, 4> ModAsSideEffect;
12629     SmallVectorImpl<std::pair<Object, Usage>> *OldModAsSideEffect;
12630   };
12631 
12632   /// RAII object wrapping the visitation of a subexpression which we might
12633   /// choose to evaluate as a constant. If any subexpression is evaluated and
12634   /// found to be non-constant, this allows us to suppress the evaluation of
12635   /// the outer expression.
12636   class EvaluationTracker {
12637   public:
12638     EvaluationTracker(SequenceChecker &Self)
12639         : Self(Self), Prev(Self.EvalTracker) {
12640       Self.EvalTracker = this;
12641     }
12642 
12643     ~EvaluationTracker() {
12644       Self.EvalTracker = Prev;
12645       if (Prev)
12646         Prev->EvalOK &= EvalOK;
12647     }
12648 
12649     bool evaluate(const Expr *E, bool &Result) {
12650       if (!EvalOK || E->isValueDependent())
12651         return false;
12652       EvalOK = E->EvaluateAsBooleanCondition(
12653           Result, Self.SemaRef.Context, Self.SemaRef.isConstantEvaluated());
12654       return EvalOK;
12655     }
12656 
12657   private:
12658     SequenceChecker &Self;
12659     EvaluationTracker *Prev;
12660     bool EvalOK = true;
12661   } *EvalTracker = nullptr;
12662 
12663   /// Find the object which is produced by the specified expression,
12664   /// if any.
12665   Object getObject(const Expr *E, bool Mod) const {
12666     E = E->IgnoreParenCasts();
12667     if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
12668       if (Mod && (UO->getOpcode() == UO_PreInc || UO->getOpcode() == UO_PreDec))
12669         return getObject(UO->getSubExpr(), Mod);
12670     } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
12671       if (BO->getOpcode() == BO_Comma)
12672         return getObject(BO->getRHS(), Mod);
12673       if (Mod && BO->isAssignmentOp())
12674         return getObject(BO->getLHS(), Mod);
12675     } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
12676       // FIXME: Check for more interesting cases, like "x.n = ++x.n".
12677       if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenCasts()))
12678         return ME->getMemberDecl();
12679     } else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
12680       // FIXME: If this is a reference, map through to its value.
12681       return DRE->getDecl();
12682     return nullptr;
12683   }
12684 
12685   /// Note that an object \p O was modified or used by an expression
12686   /// \p UsageExpr with usage kind \p UK. \p UI is the \p UsageInfo for
12687   /// the object \p O as obtained via the \p UsageMap.
12688   void addUsage(Object O, UsageInfo &UI, const Expr *UsageExpr, UsageKind UK) {
12689     // Get the old usage for the given object and usage kind.
12690     Usage &U = UI.Uses[UK];
12691     if (!U.UsageExpr || !Tree.isUnsequenced(Region, U.Seq)) {
12692       // If we have a modification as side effect and are in a sequenced
12693       // subexpression, save the old Usage so that we can restore it later
12694       // in SequencedSubexpression::~SequencedSubexpression.
12695       if (UK == UK_ModAsSideEffect && ModAsSideEffect)
12696         ModAsSideEffect->push_back(std::make_pair(O, U));
12697       // Then record the new usage with the current sequencing region.
12698       U.UsageExpr = UsageExpr;
12699       U.Seq = Region;
12700     }
12701   }
12702 
12703   /// Check whether a modification or use of an object \p O in an expression
12704   /// \p UsageExpr conflicts with a prior usage of kind \p OtherKind. \p UI is
12705   /// the \p UsageInfo for the object \p O as obtained via the \p UsageMap.
12706   /// \p IsModMod is true when we are checking for a mod-mod unsequenced
12707   /// usage and false we are checking for a mod-use unsequenced usage.
12708   void checkUsage(Object O, UsageInfo &UI, const Expr *UsageExpr,
12709                   UsageKind OtherKind, bool IsModMod) {
12710     if (UI.Diagnosed)
12711       return;
12712 
12713     const Usage &U = UI.Uses[OtherKind];
12714     if (!U.UsageExpr || !Tree.isUnsequenced(Region, U.Seq))
12715       return;
12716 
12717     const Expr *Mod = U.UsageExpr;
12718     const Expr *ModOrUse = UsageExpr;
12719     if (OtherKind == UK_Use)
12720       std::swap(Mod, ModOrUse);
12721 
12722     SemaRef.DiagRuntimeBehavior(
12723         Mod->getExprLoc(), {Mod, ModOrUse},
12724         SemaRef.PDiag(IsModMod ? diag::warn_unsequenced_mod_mod
12725                                : diag::warn_unsequenced_mod_use)
12726             << O << SourceRange(ModOrUse->getExprLoc()));
12727     UI.Diagnosed = true;
12728   }
12729 
12730   // A note on note{Pre, Post}{Use, Mod}:
12731   //
12732   // (It helps to follow the algorithm with an expression such as
12733   //  "((++k)++, k) = k" or "k = (k++, k++)". Both contain unsequenced
12734   //  operations before C++17 and both are well-defined in C++17).
12735   //
12736   // When visiting a node which uses/modify an object we first call notePreUse
12737   // or notePreMod before visiting its sub-expression(s). At this point the
12738   // children of the current node have not yet been visited and so the eventual
12739   // uses/modifications resulting from the children of the current node have not
12740   // been recorded yet.
12741   //
12742   // We then visit the children of the current node. After that notePostUse or
12743   // notePostMod is called. These will 1) detect an unsequenced modification
12744   // as side effect (as in "k++ + k") and 2) add a new usage with the
12745   // appropriate usage kind.
12746   //
12747   // We also have to be careful that some operation sequences modification as
12748   // side effect as well (for example: || or ,). To account for this we wrap
12749   // the visitation of such a sub-expression (for example: the LHS of || or ,)
12750   // with SequencedSubexpression. SequencedSubexpression is an RAII object
12751   // which record usages which are modifications as side effect, and then
12752   // downgrade them (or more accurately restore the previous usage which was a
12753   // modification as side effect) when exiting the scope of the sequenced
12754   // subexpression.
12755 
12756   void notePreUse(Object O, const Expr *UseExpr) {
12757     UsageInfo &UI = UsageMap[O];
12758     // Uses conflict with other modifications.
12759     checkUsage(O, UI, UseExpr, /*OtherKind=*/UK_ModAsValue, /*IsModMod=*/false);
12760   }
12761 
12762   void notePostUse(Object O, const Expr *UseExpr) {
12763     UsageInfo &UI = UsageMap[O];
12764     checkUsage(O, UI, UseExpr, /*OtherKind=*/UK_ModAsSideEffect,
12765                /*IsModMod=*/false);
12766     addUsage(O, UI, UseExpr, /*UsageKind=*/UK_Use);
12767   }
12768 
12769   void notePreMod(Object O, const Expr *ModExpr) {
12770     UsageInfo &UI = UsageMap[O];
12771     // Modifications conflict with other modifications and with uses.
12772     checkUsage(O, UI, ModExpr, /*OtherKind=*/UK_ModAsValue, /*IsModMod=*/true);
12773     checkUsage(O, UI, ModExpr, /*OtherKind=*/UK_Use, /*IsModMod=*/false);
12774   }
12775 
12776   void notePostMod(Object O, const Expr *ModExpr, UsageKind UK) {
12777     UsageInfo &UI = UsageMap[O];
12778     checkUsage(O, UI, ModExpr, /*OtherKind=*/UK_ModAsSideEffect,
12779                /*IsModMod=*/true);
12780     addUsage(O, UI, ModExpr, /*UsageKind=*/UK);
12781   }
12782 
12783 public:
12784   SequenceChecker(Sema &S, const Expr *E,
12785                   SmallVectorImpl<const Expr *> &WorkList)
12786       : Base(S.Context), SemaRef(S), Region(Tree.root()), WorkList(WorkList) {
12787     Visit(E);
12788     // Silence a -Wunused-private-field since WorkList is now unused.
12789     // TODO: Evaluate if it can be used, and if not remove it.
12790     (void)this->WorkList;
12791   }
12792 
12793   void VisitStmt(const Stmt *S) {
12794     // Skip all statements which aren't expressions for now.
12795   }
12796 
12797   void VisitExpr(const Expr *E) {
12798     // By default, just recurse to evaluated subexpressions.
12799     Base::VisitStmt(E);
12800   }
12801 
12802   void VisitCastExpr(const CastExpr *E) {
12803     Object O = Object();
12804     if (E->getCastKind() == CK_LValueToRValue)
12805       O = getObject(E->getSubExpr(), false);
12806 
12807     if (O)
12808       notePreUse(O, E);
12809     VisitExpr(E);
12810     if (O)
12811       notePostUse(O, E);
12812   }
12813 
12814   void VisitSequencedExpressions(const Expr *SequencedBefore,
12815                                  const Expr *SequencedAfter) {
12816     SequenceTree::Seq BeforeRegion = Tree.allocate(Region);
12817     SequenceTree::Seq AfterRegion = Tree.allocate(Region);
12818     SequenceTree::Seq OldRegion = Region;
12819 
12820     {
12821       SequencedSubexpression SeqBefore(*this);
12822       Region = BeforeRegion;
12823       Visit(SequencedBefore);
12824     }
12825 
12826     Region = AfterRegion;
12827     Visit(SequencedAfter);
12828 
12829     Region = OldRegion;
12830 
12831     Tree.merge(BeforeRegion);
12832     Tree.merge(AfterRegion);
12833   }
12834 
12835   void VisitArraySubscriptExpr(const ArraySubscriptExpr *ASE) {
12836     // C++17 [expr.sub]p1:
12837     //   The expression E1[E2] is identical (by definition) to *((E1)+(E2)). The
12838     //   expression E1 is sequenced before the expression E2.
12839     if (SemaRef.getLangOpts().CPlusPlus17)
12840       VisitSequencedExpressions(ASE->getLHS(), ASE->getRHS());
12841     else {
12842       Visit(ASE->getLHS());
12843       Visit(ASE->getRHS());
12844     }
12845   }
12846 
12847   void VisitBinPtrMemD(const BinaryOperator *BO) { VisitBinPtrMem(BO); }
12848   void VisitBinPtrMemI(const BinaryOperator *BO) { VisitBinPtrMem(BO); }
12849   void VisitBinPtrMem(const BinaryOperator *BO) {
12850     // C++17 [expr.mptr.oper]p4:
12851     //  Abbreviating pm-expression.*cast-expression as E1.*E2, [...]
12852     //  the expression E1 is sequenced before the expression E2.
12853     if (SemaRef.getLangOpts().CPlusPlus17)
12854       VisitSequencedExpressions(BO->getLHS(), BO->getRHS());
12855     else {
12856       Visit(BO->getLHS());
12857       Visit(BO->getRHS());
12858     }
12859   }
12860 
12861   void VisitBinShl(const BinaryOperator *BO) { VisitBinShlShr(BO); }
12862   void VisitBinShr(const BinaryOperator *BO) { VisitBinShlShr(BO); }
12863   void VisitBinShlShr(const BinaryOperator *BO) {
12864     // C++17 [expr.shift]p4:
12865     //  The expression E1 is sequenced before the expression E2.
12866     if (SemaRef.getLangOpts().CPlusPlus17)
12867       VisitSequencedExpressions(BO->getLHS(), BO->getRHS());
12868     else {
12869       Visit(BO->getLHS());
12870       Visit(BO->getRHS());
12871     }
12872   }
12873 
12874   void VisitBinComma(const BinaryOperator *BO) {
12875     // C++11 [expr.comma]p1:
12876     //   Every value computation and side effect associated with the left
12877     //   expression is sequenced before every value computation and side
12878     //   effect associated with the right expression.
12879     VisitSequencedExpressions(BO->getLHS(), BO->getRHS());
12880   }
12881 
12882   void VisitBinAssign(const BinaryOperator *BO) {
12883     SequenceTree::Seq RHSRegion;
12884     SequenceTree::Seq LHSRegion;
12885     if (SemaRef.getLangOpts().CPlusPlus17) {
12886       RHSRegion = Tree.allocate(Region);
12887       LHSRegion = Tree.allocate(Region);
12888     } else {
12889       RHSRegion = Region;
12890       LHSRegion = Region;
12891     }
12892     SequenceTree::Seq OldRegion = Region;
12893 
12894     // C++11 [expr.ass]p1:
12895     //  [...] the assignment is sequenced after the value computation
12896     //  of the right and left operands, [...]
12897     //
12898     // so check it before inspecting the operands and update the
12899     // map afterwards.
12900     Object O = getObject(BO->getLHS(), /*Mod=*/true);
12901     if (O)
12902       notePreMod(O, BO);
12903 
12904     if (SemaRef.getLangOpts().CPlusPlus17) {
12905       // C++17 [expr.ass]p1:
12906       //  [...] The right operand is sequenced before the left operand. [...]
12907       {
12908         SequencedSubexpression SeqBefore(*this);
12909         Region = RHSRegion;
12910         Visit(BO->getRHS());
12911       }
12912 
12913       Region = LHSRegion;
12914       Visit(BO->getLHS());
12915 
12916       if (O && isa<CompoundAssignOperator>(BO))
12917         notePostUse(O, BO);
12918 
12919     } else {
12920       // C++11 does not specify any sequencing between the LHS and RHS.
12921       Region = LHSRegion;
12922       Visit(BO->getLHS());
12923 
12924       if (O && isa<CompoundAssignOperator>(BO))
12925         notePostUse(O, BO);
12926 
12927       Region = RHSRegion;
12928       Visit(BO->getRHS());
12929     }
12930 
12931     // C++11 [expr.ass]p1:
12932     //  the assignment is sequenced [...] before the value computation of the
12933     //  assignment expression.
12934     // C11 6.5.16/3 has no such rule.
12935     Region = OldRegion;
12936     if (O)
12937       notePostMod(O, BO,
12938                   SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue
12939                                                   : UK_ModAsSideEffect);
12940     if (SemaRef.getLangOpts().CPlusPlus17) {
12941       Tree.merge(RHSRegion);
12942       Tree.merge(LHSRegion);
12943     }
12944   }
12945 
12946   void VisitCompoundAssignOperator(const CompoundAssignOperator *CAO) {
12947     VisitBinAssign(CAO);
12948   }
12949 
12950   void VisitUnaryPreInc(const UnaryOperator *UO) { VisitUnaryPreIncDec(UO); }
12951   void VisitUnaryPreDec(const UnaryOperator *UO) { VisitUnaryPreIncDec(UO); }
12952   void VisitUnaryPreIncDec(const UnaryOperator *UO) {
12953     Object O = getObject(UO->getSubExpr(), true);
12954     if (!O)
12955       return VisitExpr(UO);
12956 
12957     notePreMod(O, UO);
12958     Visit(UO->getSubExpr());
12959     // C++11 [expr.pre.incr]p1:
12960     //   the expression ++x is equivalent to x+=1
12961     notePostMod(O, UO,
12962                 SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue
12963                                                 : UK_ModAsSideEffect);
12964   }
12965 
12966   void VisitUnaryPostInc(const UnaryOperator *UO) { VisitUnaryPostIncDec(UO); }
12967   void VisitUnaryPostDec(const UnaryOperator *UO) { VisitUnaryPostIncDec(UO); }
12968   void VisitUnaryPostIncDec(const UnaryOperator *UO) {
12969     Object O = getObject(UO->getSubExpr(), true);
12970     if (!O)
12971       return VisitExpr(UO);
12972 
12973     notePreMod(O, UO);
12974     Visit(UO->getSubExpr());
12975     notePostMod(O, UO, UK_ModAsSideEffect);
12976   }
12977 
12978   void VisitBinLOr(const BinaryOperator *BO) {
12979     // C++11 [expr.log.or]p2:
12980     //  If the second expression is evaluated, every value computation and
12981     //  side effect associated with the first expression is sequenced before
12982     //  every value computation and side effect associated with the
12983     //  second expression.
12984     SequenceTree::Seq LHSRegion = Tree.allocate(Region);
12985     SequenceTree::Seq RHSRegion = Tree.allocate(Region);
12986     SequenceTree::Seq OldRegion = Region;
12987 
12988     EvaluationTracker Eval(*this);
12989     {
12990       SequencedSubexpression Sequenced(*this);
12991       Region = LHSRegion;
12992       Visit(BO->getLHS());
12993     }
12994 
12995     // C++11 [expr.log.or]p1:
12996     //  [...] the second operand is not evaluated if the first operand
12997     //  evaluates to true.
12998     bool EvalResult = false;
12999     bool EvalOK = Eval.evaluate(BO->getLHS(), EvalResult);
13000     bool ShouldVisitRHS = !EvalOK || (EvalOK && !EvalResult);
13001     if (ShouldVisitRHS) {
13002       Region = RHSRegion;
13003       Visit(BO->getRHS());
13004     }
13005 
13006     Region = OldRegion;
13007     Tree.merge(LHSRegion);
13008     Tree.merge(RHSRegion);
13009   }
13010 
13011   void VisitBinLAnd(const BinaryOperator *BO) {
13012     // C++11 [expr.log.and]p2:
13013     //  If the second expression is evaluated, every value computation and
13014     //  side effect associated with the first expression is sequenced before
13015     //  every value computation and side effect associated with the
13016     //  second expression.
13017     SequenceTree::Seq LHSRegion = Tree.allocate(Region);
13018     SequenceTree::Seq RHSRegion = Tree.allocate(Region);
13019     SequenceTree::Seq OldRegion = Region;
13020 
13021     EvaluationTracker Eval(*this);
13022     {
13023       SequencedSubexpression Sequenced(*this);
13024       Region = LHSRegion;
13025       Visit(BO->getLHS());
13026     }
13027 
13028     // C++11 [expr.log.and]p1:
13029     //  [...] the second operand is not evaluated if the first operand is false.
13030     bool EvalResult = false;
13031     bool EvalOK = Eval.evaluate(BO->getLHS(), EvalResult);
13032     bool ShouldVisitRHS = !EvalOK || (EvalOK && EvalResult);
13033     if (ShouldVisitRHS) {
13034       Region = RHSRegion;
13035       Visit(BO->getRHS());
13036     }
13037 
13038     Region = OldRegion;
13039     Tree.merge(LHSRegion);
13040     Tree.merge(RHSRegion);
13041   }
13042 
13043   void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO) {
13044     // C++11 [expr.cond]p1:
13045     //  [...] Every value computation and side effect associated with the first
13046     //  expression is sequenced before every value computation and side effect
13047     //  associated with the second or third expression.
13048     SequenceTree::Seq ConditionRegion = Tree.allocate(Region);
13049 
13050     // No sequencing is specified between the true and false expression.
13051     // However since exactly one of both is going to be evaluated we can
13052     // consider them to be sequenced. This is needed to avoid warning on
13053     // something like "x ? y+= 1 : y += 2;" in the case where we will visit
13054     // both the true and false expressions because we can't evaluate x.
13055     // This will still allow us to detect an expression like (pre C++17)
13056     // "(x ? y += 1 : y += 2) = y".
13057     //
13058     // We don't wrap the visitation of the true and false expression with
13059     // SequencedSubexpression because we don't want to downgrade modifications
13060     // as side effect in the true and false expressions after the visition
13061     // is done. (for example in the expression "(x ? y++ : y++) + y" we should
13062     // not warn between the two "y++", but we should warn between the "y++"
13063     // and the "y".
13064     SequenceTree::Seq TrueRegion = Tree.allocate(Region);
13065     SequenceTree::Seq FalseRegion = Tree.allocate(Region);
13066     SequenceTree::Seq OldRegion = Region;
13067 
13068     EvaluationTracker Eval(*this);
13069     {
13070       SequencedSubexpression Sequenced(*this);
13071       Region = ConditionRegion;
13072       Visit(CO->getCond());
13073     }
13074 
13075     // C++11 [expr.cond]p1:
13076     // [...] The first expression is contextually converted to bool (Clause 4).
13077     // It is evaluated and if it is true, the result of the conditional
13078     // expression is the value of the second expression, otherwise that of the
13079     // third expression. Only one of the second and third expressions is
13080     // evaluated. [...]
13081     bool EvalResult = false;
13082     bool EvalOK = Eval.evaluate(CO->getCond(), EvalResult);
13083     bool ShouldVisitTrueExpr = !EvalOK || (EvalOK && EvalResult);
13084     bool ShouldVisitFalseExpr = !EvalOK || (EvalOK && !EvalResult);
13085     if (ShouldVisitTrueExpr) {
13086       Region = TrueRegion;
13087       Visit(CO->getTrueExpr());
13088     }
13089     if (ShouldVisitFalseExpr) {
13090       Region = FalseRegion;
13091       Visit(CO->getFalseExpr());
13092     }
13093 
13094     Region = OldRegion;
13095     Tree.merge(ConditionRegion);
13096     Tree.merge(TrueRegion);
13097     Tree.merge(FalseRegion);
13098   }
13099 
13100   void VisitCallExpr(const CallExpr *CE) {
13101     // FIXME: CXXNewExpr and CXXDeleteExpr implicitly call functions.
13102 
13103     if (CE->isUnevaluatedBuiltinCall(Context))
13104       return;
13105 
13106     // C++11 [intro.execution]p15:
13107     //   When calling a function [...], every value computation and side effect
13108     //   associated with any argument expression, or with the postfix expression
13109     //   designating the called function, is sequenced before execution of every
13110     //   expression or statement in the body of the function [and thus before
13111     //   the value computation of its result].
13112     SequencedSubexpression Sequenced(*this);
13113     SemaRef.runWithSufficientStackSpace(CE->getExprLoc(), [&] {
13114       // C++17 [expr.call]p5
13115       //   The postfix-expression is sequenced before each expression in the
13116       //   expression-list and any default argument. [...]
13117       SequenceTree::Seq CalleeRegion;
13118       SequenceTree::Seq OtherRegion;
13119       if (SemaRef.getLangOpts().CPlusPlus17) {
13120         CalleeRegion = Tree.allocate(Region);
13121         OtherRegion = Tree.allocate(Region);
13122       } else {
13123         CalleeRegion = Region;
13124         OtherRegion = Region;
13125       }
13126       SequenceTree::Seq OldRegion = Region;
13127 
13128       // Visit the callee expression first.
13129       Region = CalleeRegion;
13130       if (SemaRef.getLangOpts().CPlusPlus17) {
13131         SequencedSubexpression Sequenced(*this);
13132         Visit(CE->getCallee());
13133       } else {
13134         Visit(CE->getCallee());
13135       }
13136 
13137       // Then visit the argument expressions.
13138       Region = OtherRegion;
13139       for (const Expr *Argument : CE->arguments())
13140         Visit(Argument);
13141 
13142       Region = OldRegion;
13143       if (SemaRef.getLangOpts().CPlusPlus17) {
13144         Tree.merge(CalleeRegion);
13145         Tree.merge(OtherRegion);
13146       }
13147     });
13148   }
13149 
13150   void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CXXOCE) {
13151     // C++17 [over.match.oper]p2:
13152     //   [...] the operator notation is first transformed to the equivalent
13153     //   function-call notation as summarized in Table 12 (where @ denotes one
13154     //   of the operators covered in the specified subclause). However, the
13155     //   operands are sequenced in the order prescribed for the built-in
13156     //   operator (Clause 8).
13157     //
13158     // From the above only overloaded binary operators and overloaded call
13159     // operators have sequencing rules in C++17 that we need to handle
13160     // separately.
13161     if (!SemaRef.getLangOpts().CPlusPlus17 ||
13162         (CXXOCE->getNumArgs() != 2 && CXXOCE->getOperator() != OO_Call))
13163       return VisitCallExpr(CXXOCE);
13164 
13165     enum {
13166       NoSequencing,
13167       LHSBeforeRHS,
13168       RHSBeforeLHS,
13169       LHSBeforeRest
13170     } SequencingKind;
13171     switch (CXXOCE->getOperator()) {
13172     case OO_Equal:
13173     case OO_PlusEqual:
13174     case OO_MinusEqual:
13175     case OO_StarEqual:
13176     case OO_SlashEqual:
13177     case OO_PercentEqual:
13178     case OO_CaretEqual:
13179     case OO_AmpEqual:
13180     case OO_PipeEqual:
13181     case OO_LessLessEqual:
13182     case OO_GreaterGreaterEqual:
13183       SequencingKind = RHSBeforeLHS;
13184       break;
13185 
13186     case OO_LessLess:
13187     case OO_GreaterGreater:
13188     case OO_AmpAmp:
13189     case OO_PipePipe:
13190     case OO_Comma:
13191     case OO_ArrowStar:
13192     case OO_Subscript:
13193       SequencingKind = LHSBeforeRHS;
13194       break;
13195 
13196     case OO_Call:
13197       SequencingKind = LHSBeforeRest;
13198       break;
13199 
13200     default:
13201       SequencingKind = NoSequencing;
13202       break;
13203     }
13204 
13205     if (SequencingKind == NoSequencing)
13206       return VisitCallExpr(CXXOCE);
13207 
13208     // This is a call, so all subexpressions are sequenced before the result.
13209     SequencedSubexpression Sequenced(*this);
13210 
13211     SemaRef.runWithSufficientStackSpace(CXXOCE->getExprLoc(), [&] {
13212       assert(SemaRef.getLangOpts().CPlusPlus17 &&
13213              "Should only get there with C++17 and above!");
13214       assert((CXXOCE->getNumArgs() == 2 || CXXOCE->getOperator() == OO_Call) &&
13215              "Should only get there with an overloaded binary operator"
13216              " or an overloaded call operator!");
13217 
13218       if (SequencingKind == LHSBeforeRest) {
13219         assert(CXXOCE->getOperator() == OO_Call &&
13220                "We should only have an overloaded call operator here!");
13221 
13222         // This is very similar to VisitCallExpr, except that we only have the
13223         // C++17 case. The postfix-expression is the first argument of the
13224         // CXXOperatorCallExpr. The expressions in the expression-list, if any,
13225         // are in the following arguments.
13226         //
13227         // Note that we intentionally do not visit the callee expression since
13228         // it is just a decayed reference to a function.
13229         SequenceTree::Seq PostfixExprRegion = Tree.allocate(Region);
13230         SequenceTree::Seq ArgsRegion = Tree.allocate(Region);
13231         SequenceTree::Seq OldRegion = Region;
13232 
13233         assert(CXXOCE->getNumArgs() >= 1 &&
13234                "An overloaded call operator must have at least one argument"
13235                " for the postfix-expression!");
13236         const Expr *PostfixExpr = CXXOCE->getArgs()[0];
13237         llvm::ArrayRef<const Expr *> Args(CXXOCE->getArgs() + 1,
13238                                           CXXOCE->getNumArgs() - 1);
13239 
13240         // Visit the postfix-expression first.
13241         {
13242           Region = PostfixExprRegion;
13243           SequencedSubexpression Sequenced(*this);
13244           Visit(PostfixExpr);
13245         }
13246 
13247         // Then visit the argument expressions.
13248         Region = ArgsRegion;
13249         for (const Expr *Arg : Args)
13250           Visit(Arg);
13251 
13252         Region = OldRegion;
13253         Tree.merge(PostfixExprRegion);
13254         Tree.merge(ArgsRegion);
13255       } else {
13256         assert(CXXOCE->getNumArgs() == 2 &&
13257                "Should only have two arguments here!");
13258         assert((SequencingKind == LHSBeforeRHS ||
13259                 SequencingKind == RHSBeforeLHS) &&
13260                "Unexpected sequencing kind!");
13261 
13262         // We do not visit the callee expression since it is just a decayed
13263         // reference to a function.
13264         const Expr *E1 = CXXOCE->getArg(0);
13265         const Expr *E2 = CXXOCE->getArg(1);
13266         if (SequencingKind == RHSBeforeLHS)
13267           std::swap(E1, E2);
13268 
13269         return VisitSequencedExpressions(E1, E2);
13270       }
13271     });
13272   }
13273 
13274   void VisitCXXConstructExpr(const CXXConstructExpr *CCE) {
13275     // This is a call, so all subexpressions are sequenced before the result.
13276     SequencedSubexpression Sequenced(*this);
13277 
13278     if (!CCE->isListInitialization())
13279       return VisitExpr(CCE);
13280 
13281     // In C++11, list initializations are sequenced.
13282     SmallVector<SequenceTree::Seq, 32> Elts;
13283     SequenceTree::Seq Parent = Region;
13284     for (CXXConstructExpr::const_arg_iterator I = CCE->arg_begin(),
13285                                               E = CCE->arg_end();
13286          I != E; ++I) {
13287       Region = Tree.allocate(Parent);
13288       Elts.push_back(Region);
13289       Visit(*I);
13290     }
13291 
13292     // Forget that the initializers are sequenced.
13293     Region = Parent;
13294     for (unsigned I = 0; I < Elts.size(); ++I)
13295       Tree.merge(Elts[I]);
13296   }
13297 
13298   void VisitInitListExpr(const InitListExpr *ILE) {
13299     if (!SemaRef.getLangOpts().CPlusPlus11)
13300       return VisitExpr(ILE);
13301 
13302     // In C++11, list initializations are sequenced.
13303     SmallVector<SequenceTree::Seq, 32> Elts;
13304     SequenceTree::Seq Parent = Region;
13305     for (unsigned I = 0; I < ILE->getNumInits(); ++I) {
13306       const Expr *E = ILE->getInit(I);
13307       if (!E)
13308         continue;
13309       Region = Tree.allocate(Parent);
13310       Elts.push_back(Region);
13311       Visit(E);
13312     }
13313 
13314     // Forget that the initializers are sequenced.
13315     Region = Parent;
13316     for (unsigned I = 0; I < Elts.size(); ++I)
13317       Tree.merge(Elts[I]);
13318   }
13319 };
13320 
13321 } // namespace
13322 
13323 void Sema::CheckUnsequencedOperations(const Expr *E) {
13324   SmallVector<const Expr *, 8> WorkList;
13325   WorkList.push_back(E);
13326   while (!WorkList.empty()) {
13327     const Expr *Item = WorkList.pop_back_val();
13328     SequenceChecker(*this, Item, WorkList);
13329   }
13330 }
13331 
13332 void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc,
13333                               bool IsConstexpr) {
13334   llvm::SaveAndRestore<bool> ConstantContext(
13335       isConstantEvaluatedOverride, IsConstexpr || isa<ConstantExpr>(E));
13336   CheckImplicitConversions(E, CheckLoc);
13337   if (!E->isInstantiationDependent())
13338     CheckUnsequencedOperations(E);
13339   if (!IsConstexpr && !E->isValueDependent())
13340     CheckForIntOverflow(E);
13341   DiagnoseMisalignedMembers();
13342 }
13343 
13344 void Sema::CheckBitFieldInitialization(SourceLocation InitLoc,
13345                                        FieldDecl *BitField,
13346                                        Expr *Init) {
13347   (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc);
13348 }
13349 
13350 static void diagnoseArrayStarInParamType(Sema &S, QualType PType,
13351                                          SourceLocation Loc) {
13352   if (!PType->isVariablyModifiedType())
13353     return;
13354   if (const auto *PointerTy = dyn_cast<PointerType>(PType)) {
13355     diagnoseArrayStarInParamType(S, PointerTy->getPointeeType(), Loc);
13356     return;
13357   }
13358   if (const auto *ReferenceTy = dyn_cast<ReferenceType>(PType)) {
13359     diagnoseArrayStarInParamType(S, ReferenceTy->getPointeeType(), Loc);
13360     return;
13361   }
13362   if (const auto *ParenTy = dyn_cast<ParenType>(PType)) {
13363     diagnoseArrayStarInParamType(S, ParenTy->getInnerType(), Loc);
13364     return;
13365   }
13366 
13367   const ArrayType *AT = S.Context.getAsArrayType(PType);
13368   if (!AT)
13369     return;
13370 
13371   if (AT->getSizeModifier() != ArrayType::Star) {
13372     diagnoseArrayStarInParamType(S, AT->getElementType(), Loc);
13373     return;
13374   }
13375 
13376   S.Diag(Loc, diag::err_array_star_in_function_definition);
13377 }
13378 
13379 /// CheckParmsForFunctionDef - Check that the parameters of the given
13380 /// function are appropriate for the definition of a function. This
13381 /// takes care of any checks that cannot be performed on the
13382 /// declaration itself, e.g., that the types of each of the function
13383 /// parameters are complete.
13384 bool Sema::CheckParmsForFunctionDef(ArrayRef<ParmVarDecl *> Parameters,
13385                                     bool CheckParameterNames) {
13386   bool HasInvalidParm = false;
13387   for (ParmVarDecl *Param : Parameters) {
13388     // C99 6.7.5.3p4: the parameters in a parameter type list in a
13389     // function declarator that is part of a function definition of
13390     // that function shall not have incomplete type.
13391     //
13392     // This is also C++ [dcl.fct]p6.
13393     if (!Param->isInvalidDecl() &&
13394         RequireCompleteType(Param->getLocation(), Param->getType(),
13395                             diag::err_typecheck_decl_incomplete_type)) {
13396       Param->setInvalidDecl();
13397       HasInvalidParm = true;
13398     }
13399 
13400     // C99 6.9.1p5: If the declarator includes a parameter type list, the
13401     // declaration of each parameter shall include an identifier.
13402     if (CheckParameterNames && Param->getIdentifier() == nullptr &&
13403         !Param->isImplicit() && !getLangOpts().CPlusPlus) {
13404       // Diagnose this as an extension in C17 and earlier.
13405       if (!getLangOpts().C2x)
13406         Diag(Param->getLocation(), diag::ext_parameter_name_omitted_c2x);
13407     }
13408 
13409     // C99 6.7.5.3p12:
13410     //   If the function declarator is not part of a definition of that
13411     //   function, parameters may have incomplete type and may use the [*]
13412     //   notation in their sequences of declarator specifiers to specify
13413     //   variable length array types.
13414     QualType PType = Param->getOriginalType();
13415     // FIXME: This diagnostic should point the '[*]' if source-location
13416     // information is added for it.
13417     diagnoseArrayStarInParamType(*this, PType, Param->getLocation());
13418 
13419     // If the parameter is a c++ class type and it has to be destructed in the
13420     // callee function, declare the destructor so that it can be called by the
13421     // callee function. Do not perform any direct access check on the dtor here.
13422     if (!Param->isInvalidDecl()) {
13423       if (CXXRecordDecl *ClassDecl = Param->getType()->getAsCXXRecordDecl()) {
13424         if (!ClassDecl->isInvalidDecl() &&
13425             !ClassDecl->hasIrrelevantDestructor() &&
13426             !ClassDecl->isDependentContext() &&
13427             ClassDecl->isParamDestroyedInCallee()) {
13428           CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl);
13429           MarkFunctionReferenced(Param->getLocation(), Destructor);
13430           DiagnoseUseOfDecl(Destructor, Param->getLocation());
13431         }
13432       }
13433     }
13434 
13435     // Parameters with the pass_object_size attribute only need to be marked
13436     // constant at function definitions. Because we lack information about
13437     // whether we're on a declaration or definition when we're instantiating the
13438     // attribute, we need to check for constness here.
13439     if (const auto *Attr = Param->getAttr<PassObjectSizeAttr>())
13440       if (!Param->getType().isConstQualified())
13441         Diag(Param->getLocation(), diag::err_attribute_pointers_only)
13442             << Attr->getSpelling() << 1;
13443 
13444     // Check for parameter names shadowing fields from the class.
13445     if (LangOpts.CPlusPlus && !Param->isInvalidDecl()) {
13446       // The owning context for the parameter should be the function, but we
13447       // want to see if this function's declaration context is a record.
13448       DeclContext *DC = Param->getDeclContext();
13449       if (DC && DC->isFunctionOrMethod()) {
13450         if (auto *RD = dyn_cast<CXXRecordDecl>(DC->getParent()))
13451           CheckShadowInheritedFields(Param->getLocation(), Param->getDeclName(),
13452                                      RD, /*DeclIsField*/ false);
13453       }
13454     }
13455   }
13456 
13457   return HasInvalidParm;
13458 }
13459 
13460 Optional<std::pair<CharUnits, CharUnits>>
13461 static getBaseAlignmentAndOffsetFromPtr(const Expr *E, ASTContext &Ctx);
13462 
13463 /// Compute the alignment and offset of the base class object given the
13464 /// derived-to-base cast expression and the alignment and offset of the derived
13465 /// class object.
13466 static std::pair<CharUnits, CharUnits>
13467 getDerivedToBaseAlignmentAndOffset(const CastExpr *CE, QualType DerivedType,
13468                                    CharUnits BaseAlignment, CharUnits Offset,
13469                                    ASTContext &Ctx) {
13470   for (auto PathI = CE->path_begin(), PathE = CE->path_end(); PathI != PathE;
13471        ++PathI) {
13472     const CXXBaseSpecifier *Base = *PathI;
13473     const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl();
13474     if (Base->isVirtual()) {
13475       // The complete object may have a lower alignment than the non-virtual
13476       // alignment of the base, in which case the base may be misaligned. Choose
13477       // the smaller of the non-virtual alignment and BaseAlignment, which is a
13478       // conservative lower bound of the complete object alignment.
13479       CharUnits NonVirtualAlignment =
13480           Ctx.getASTRecordLayout(BaseDecl).getNonVirtualAlignment();
13481       BaseAlignment = std::min(BaseAlignment, NonVirtualAlignment);
13482       Offset = CharUnits::Zero();
13483     } else {
13484       const ASTRecordLayout &RL =
13485           Ctx.getASTRecordLayout(DerivedType->getAsCXXRecordDecl());
13486       Offset += RL.getBaseClassOffset(BaseDecl);
13487     }
13488     DerivedType = Base->getType();
13489   }
13490 
13491   return std::make_pair(BaseAlignment, Offset);
13492 }
13493 
13494 /// Compute the alignment and offset of a binary additive operator.
13495 static Optional<std::pair<CharUnits, CharUnits>>
13496 getAlignmentAndOffsetFromBinAddOrSub(const Expr *PtrE, const Expr *IntE,
13497                                      bool IsSub, ASTContext &Ctx) {
13498   QualType PointeeType = PtrE->getType()->getPointeeType();
13499 
13500   if (!PointeeType->isConstantSizeType())
13501     return llvm::None;
13502 
13503   auto P = getBaseAlignmentAndOffsetFromPtr(PtrE, Ctx);
13504 
13505   if (!P)
13506     return llvm::None;
13507 
13508   llvm::APSInt IdxRes;
13509   CharUnits EltSize = Ctx.getTypeSizeInChars(PointeeType);
13510   if (IntE->isIntegerConstantExpr(IdxRes, Ctx)) {
13511     CharUnits Offset = EltSize * IdxRes.getExtValue();
13512     if (IsSub)
13513       Offset = -Offset;
13514     return std::make_pair(P->first, P->second + Offset);
13515   }
13516 
13517   // If the integer expression isn't a constant expression, compute the lower
13518   // bound of the alignment using the alignment and offset of the pointer
13519   // expression and the element size.
13520   return std::make_pair(
13521       P->first.alignmentAtOffset(P->second).alignmentAtOffset(EltSize),
13522       CharUnits::Zero());
13523 }
13524 
13525 /// This helper function takes an lvalue expression and returns the alignment of
13526 /// a VarDecl and a constant offset from the VarDecl.
13527 Optional<std::pair<CharUnits, CharUnits>>
13528 static getBaseAlignmentAndOffsetFromLValue(const Expr *E, ASTContext &Ctx) {
13529   E = E->IgnoreParens();
13530   switch (E->getStmtClass()) {
13531   default:
13532     break;
13533   case Stmt::CStyleCastExprClass:
13534   case Stmt::CXXStaticCastExprClass:
13535   case Stmt::ImplicitCastExprClass: {
13536     auto *CE = cast<CastExpr>(E);
13537     const Expr *From = CE->getSubExpr();
13538     switch (CE->getCastKind()) {
13539     default:
13540       break;
13541     case CK_NoOp:
13542       return getBaseAlignmentAndOffsetFromLValue(From, Ctx);
13543     case CK_UncheckedDerivedToBase:
13544     case CK_DerivedToBase: {
13545       auto P = getBaseAlignmentAndOffsetFromLValue(From, Ctx);
13546       if (!P)
13547         break;
13548       return getDerivedToBaseAlignmentAndOffset(CE, From->getType(), P->first,
13549                                                 P->second, Ctx);
13550     }
13551     }
13552     break;
13553   }
13554   case Stmt::ArraySubscriptExprClass: {
13555     auto *ASE = cast<ArraySubscriptExpr>(E);
13556     return getAlignmentAndOffsetFromBinAddOrSub(ASE->getBase(), ASE->getIdx(),
13557                                                 false, Ctx);
13558   }
13559   case Stmt::DeclRefExprClass: {
13560     if (auto *VD = dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
13561       // FIXME: If VD is captured by copy or is an escaping __block variable,
13562       // use the alignment of VD's type.
13563       if (!VD->getType()->isReferenceType())
13564         return std::make_pair(Ctx.getDeclAlign(VD), CharUnits::Zero());
13565       if (VD->hasInit())
13566         return getBaseAlignmentAndOffsetFromLValue(VD->getInit(), Ctx);
13567     }
13568     break;
13569   }
13570   case Stmt::MemberExprClass: {
13571     auto *ME = cast<MemberExpr>(E);
13572     auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
13573     if (!FD || FD->getType()->isReferenceType())
13574       break;
13575     Optional<std::pair<CharUnits, CharUnits>> P;
13576     if (ME->isArrow())
13577       P = getBaseAlignmentAndOffsetFromPtr(ME->getBase(), Ctx);
13578     else
13579       P = getBaseAlignmentAndOffsetFromLValue(ME->getBase(), Ctx);
13580     if (!P)
13581       break;
13582     const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(FD->getParent());
13583     uint64_t Offset = Layout.getFieldOffset(FD->getFieldIndex());
13584     return std::make_pair(P->first,
13585                           P->second + CharUnits::fromQuantity(Offset));
13586   }
13587   case Stmt::UnaryOperatorClass: {
13588     auto *UO = cast<UnaryOperator>(E);
13589     switch (UO->getOpcode()) {
13590     default:
13591       break;
13592     case UO_Deref:
13593       return getBaseAlignmentAndOffsetFromPtr(UO->getSubExpr(), Ctx);
13594     }
13595     break;
13596   }
13597   case Stmt::BinaryOperatorClass: {
13598     auto *BO = cast<BinaryOperator>(E);
13599     auto Opcode = BO->getOpcode();
13600     switch (Opcode) {
13601     default:
13602       break;
13603     case BO_Comma:
13604       return getBaseAlignmentAndOffsetFromLValue(BO->getRHS(), Ctx);
13605     }
13606     break;
13607   }
13608   }
13609   return llvm::None;
13610 }
13611 
13612 /// This helper function takes a pointer expression and returns the alignment of
13613 /// a VarDecl and a constant offset from the VarDecl.
13614 Optional<std::pair<CharUnits, CharUnits>>
13615 static getBaseAlignmentAndOffsetFromPtr(const Expr *E, ASTContext &Ctx) {
13616   E = E->IgnoreParens();
13617   switch (E->getStmtClass()) {
13618   default:
13619     break;
13620   case Stmt::CStyleCastExprClass:
13621   case Stmt::CXXStaticCastExprClass:
13622   case Stmt::ImplicitCastExprClass: {
13623     auto *CE = cast<CastExpr>(E);
13624     const Expr *From = CE->getSubExpr();
13625     switch (CE->getCastKind()) {
13626     default:
13627       break;
13628     case CK_NoOp:
13629       return getBaseAlignmentAndOffsetFromPtr(From, Ctx);
13630     case CK_ArrayToPointerDecay:
13631       return getBaseAlignmentAndOffsetFromLValue(From, Ctx);
13632     case CK_UncheckedDerivedToBase:
13633     case CK_DerivedToBase: {
13634       auto P = getBaseAlignmentAndOffsetFromPtr(From, Ctx);
13635       if (!P)
13636         break;
13637       return getDerivedToBaseAlignmentAndOffset(
13638           CE, From->getType()->getPointeeType(), P->first, P->second, Ctx);
13639     }
13640     }
13641     break;
13642   }
13643   case Stmt::CXXThisExprClass: {
13644     auto *RD = E->getType()->getPointeeType()->getAsCXXRecordDecl();
13645     CharUnits Alignment = Ctx.getASTRecordLayout(RD).getNonVirtualAlignment();
13646     return std::make_pair(Alignment, CharUnits::Zero());
13647   }
13648   case Stmt::UnaryOperatorClass: {
13649     auto *UO = cast<UnaryOperator>(E);
13650     if (UO->getOpcode() == UO_AddrOf)
13651       return getBaseAlignmentAndOffsetFromLValue(UO->getSubExpr(), Ctx);
13652     break;
13653   }
13654   case Stmt::BinaryOperatorClass: {
13655     auto *BO = cast<BinaryOperator>(E);
13656     auto Opcode = BO->getOpcode();
13657     switch (Opcode) {
13658     default:
13659       break;
13660     case BO_Add:
13661     case BO_Sub: {
13662       const Expr *LHS = BO->getLHS(), *RHS = BO->getRHS();
13663       if (Opcode == BO_Add && !RHS->getType()->isIntegralOrEnumerationType())
13664         std::swap(LHS, RHS);
13665       return getAlignmentAndOffsetFromBinAddOrSub(LHS, RHS, Opcode == BO_Sub,
13666                                                   Ctx);
13667     }
13668     case BO_Comma:
13669       return getBaseAlignmentAndOffsetFromPtr(BO->getRHS(), Ctx);
13670     }
13671     break;
13672   }
13673   }
13674   return llvm::None;
13675 }
13676 
13677 static CharUnits getPresumedAlignmentOfPointer(const Expr *E, Sema &S) {
13678   // See if we can compute the alignment of a VarDecl and an offset from it.
13679   Optional<std::pair<CharUnits, CharUnits>> P =
13680       getBaseAlignmentAndOffsetFromPtr(E, S.Context);
13681 
13682   if (P)
13683     return P->first.alignmentAtOffset(P->second);
13684 
13685   // If that failed, return the type's alignment.
13686   return S.Context.getTypeAlignInChars(E->getType()->getPointeeType());
13687 }
13688 
13689 /// CheckCastAlign - Implements -Wcast-align, which warns when a
13690 /// pointer cast increases the alignment requirements.
13691 void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) {
13692   // This is actually a lot of work to potentially be doing on every
13693   // cast; don't do it if we're ignoring -Wcast_align (as is the default).
13694   if (getDiagnostics().isIgnored(diag::warn_cast_align, TRange.getBegin()))
13695     return;
13696 
13697   // Ignore dependent types.
13698   if (T->isDependentType() || Op->getType()->isDependentType())
13699     return;
13700 
13701   // Require that the destination be a pointer type.
13702   const PointerType *DestPtr = T->getAs<PointerType>();
13703   if (!DestPtr) return;
13704 
13705   // If the destination has alignment 1, we're done.
13706   QualType DestPointee = DestPtr->getPointeeType();
13707   if (DestPointee->isIncompleteType()) return;
13708   CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee);
13709   if (DestAlign.isOne()) return;
13710 
13711   // Require that the source be a pointer type.
13712   const PointerType *SrcPtr = Op->getType()->getAs<PointerType>();
13713   if (!SrcPtr) return;
13714   QualType SrcPointee = SrcPtr->getPointeeType();
13715 
13716   // Explicitly allow casts from cv void*.  We already implicitly
13717   // allowed casts to cv void*, since they have alignment 1.
13718   // Also allow casts involving incomplete types, which implicitly
13719   // includes 'void'.
13720   if (SrcPointee->isIncompleteType()) return;
13721 
13722   CharUnits SrcAlign = getPresumedAlignmentOfPointer(Op, *this);
13723 
13724   if (SrcAlign >= DestAlign) return;
13725 
13726   Diag(TRange.getBegin(), diag::warn_cast_align)
13727     << Op->getType() << T
13728     << static_cast<unsigned>(SrcAlign.getQuantity())
13729     << static_cast<unsigned>(DestAlign.getQuantity())
13730     << TRange << Op->getSourceRange();
13731 }
13732 
13733 /// Check whether this array fits the idiom of a size-one tail padded
13734 /// array member of a struct.
13735 ///
13736 /// We avoid emitting out-of-bounds access warnings for such arrays as they are
13737 /// commonly used to emulate flexible arrays in C89 code.
13738 static bool IsTailPaddedMemberArray(Sema &S, const llvm::APInt &Size,
13739                                     const NamedDecl *ND) {
13740   if (Size != 1 || !ND) return false;
13741 
13742   const FieldDecl *FD = dyn_cast<FieldDecl>(ND);
13743   if (!FD) return false;
13744 
13745   // Don't consider sizes resulting from macro expansions or template argument
13746   // substitution to form C89 tail-padded arrays.
13747 
13748   TypeSourceInfo *TInfo = FD->getTypeSourceInfo();
13749   while (TInfo) {
13750     TypeLoc TL = TInfo->getTypeLoc();
13751     // Look through typedefs.
13752     if (TypedefTypeLoc TTL = TL.getAs<TypedefTypeLoc>()) {
13753       const TypedefNameDecl *TDL = TTL.getTypedefNameDecl();
13754       TInfo = TDL->getTypeSourceInfo();
13755       continue;
13756     }
13757     if (ConstantArrayTypeLoc CTL = TL.getAs<ConstantArrayTypeLoc>()) {
13758       const Expr *SizeExpr = dyn_cast<IntegerLiteral>(CTL.getSizeExpr());
13759       if (!SizeExpr || SizeExpr->getExprLoc().isMacroID())
13760         return false;
13761     }
13762     break;
13763   }
13764 
13765   const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext());
13766   if (!RD) return false;
13767   if (RD->isUnion()) return false;
13768   if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
13769     if (!CRD->isStandardLayout()) return false;
13770   }
13771 
13772   // See if this is the last field decl in the record.
13773   const Decl *D = FD;
13774   while ((D = D->getNextDeclInContext()))
13775     if (isa<FieldDecl>(D))
13776       return false;
13777   return true;
13778 }
13779 
13780 void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
13781                             const ArraySubscriptExpr *ASE,
13782                             bool AllowOnePastEnd, bool IndexNegated) {
13783   // Already diagnosed by the constant evaluator.
13784   if (isConstantEvaluated())
13785     return;
13786 
13787   IndexExpr = IndexExpr->IgnoreParenImpCasts();
13788   if (IndexExpr->isValueDependent())
13789     return;
13790 
13791   const Type *EffectiveType =
13792       BaseExpr->getType()->getPointeeOrArrayElementType();
13793   BaseExpr = BaseExpr->IgnoreParenCasts();
13794   const ConstantArrayType *ArrayTy =
13795       Context.getAsConstantArrayType(BaseExpr->getType());
13796 
13797   if (!ArrayTy)
13798     return;
13799 
13800   const Type *BaseType = ArrayTy->getElementType().getTypePtr();
13801   if (EffectiveType->isDependentType() || BaseType->isDependentType())
13802     return;
13803 
13804   Expr::EvalResult Result;
13805   if (!IndexExpr->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects))
13806     return;
13807 
13808   llvm::APSInt index = Result.Val.getInt();
13809   if (IndexNegated)
13810     index = -index;
13811 
13812   const NamedDecl *ND = nullptr;
13813   if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
13814     ND = DRE->getDecl();
13815   if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
13816     ND = ME->getMemberDecl();
13817 
13818   if (index.isUnsigned() || !index.isNegative()) {
13819     // It is possible that the type of the base expression after
13820     // IgnoreParenCasts is incomplete, even though the type of the base
13821     // expression before IgnoreParenCasts is complete (see PR39746 for an
13822     // example). In this case we have no information about whether the array
13823     // access exceeds the array bounds. However we can still diagnose an array
13824     // access which precedes the array bounds.
13825     if (BaseType->isIncompleteType())
13826       return;
13827 
13828     llvm::APInt size = ArrayTy->getSize();
13829     if (!size.isStrictlyPositive())
13830       return;
13831 
13832     if (BaseType != EffectiveType) {
13833       // Make sure we're comparing apples to apples when comparing index to size
13834       uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType);
13835       uint64_t array_typesize = Context.getTypeSize(BaseType);
13836       // Handle ptrarith_typesize being zero, such as when casting to void*
13837       if (!ptrarith_typesize) ptrarith_typesize = 1;
13838       if (ptrarith_typesize != array_typesize) {
13839         // There's a cast to a different size type involved
13840         uint64_t ratio = array_typesize / ptrarith_typesize;
13841         // TODO: Be smarter about handling cases where array_typesize is not a
13842         // multiple of ptrarith_typesize
13843         if (ptrarith_typesize * ratio == array_typesize)
13844           size *= llvm::APInt(size.getBitWidth(), ratio);
13845       }
13846     }
13847 
13848     if (size.getBitWidth() > index.getBitWidth())
13849       index = index.zext(size.getBitWidth());
13850     else if (size.getBitWidth() < index.getBitWidth())
13851       size = size.zext(index.getBitWidth());
13852 
13853     // For array subscripting the index must be less than size, but for pointer
13854     // arithmetic also allow the index (offset) to be equal to size since
13855     // computing the next address after the end of the array is legal and
13856     // commonly done e.g. in C++ iterators and range-based for loops.
13857     if (AllowOnePastEnd ? index.ule(size) : index.ult(size))
13858       return;
13859 
13860     // Also don't warn for arrays of size 1 which are members of some
13861     // structure. These are often used to approximate flexible arrays in C89
13862     // code.
13863     if (IsTailPaddedMemberArray(*this, size, ND))
13864       return;
13865 
13866     // Suppress the warning if the subscript expression (as identified by the
13867     // ']' location) and the index expression are both from macro expansions
13868     // within a system header.
13869     if (ASE) {
13870       SourceLocation RBracketLoc = SourceMgr.getSpellingLoc(
13871           ASE->getRBracketLoc());
13872       if (SourceMgr.isInSystemHeader(RBracketLoc)) {
13873         SourceLocation IndexLoc =
13874             SourceMgr.getSpellingLoc(IndexExpr->getBeginLoc());
13875         if (SourceMgr.isWrittenInSameFile(RBracketLoc, IndexLoc))
13876           return;
13877       }
13878     }
13879 
13880     unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds;
13881     if (ASE)
13882       DiagID = diag::warn_array_index_exceeds_bounds;
13883 
13884     DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr,
13885                         PDiag(DiagID) << index.toString(10, true)
13886                                       << size.toString(10, true)
13887                                       << (unsigned)size.getLimitedValue(~0U)
13888                                       << IndexExpr->getSourceRange());
13889   } else {
13890     unsigned DiagID = diag::warn_array_index_precedes_bounds;
13891     if (!ASE) {
13892       DiagID = diag::warn_ptr_arith_precedes_bounds;
13893       if (index.isNegative()) index = -index;
13894     }
13895 
13896     DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr,
13897                         PDiag(DiagID) << index.toString(10, true)
13898                                       << IndexExpr->getSourceRange());
13899   }
13900 
13901   if (!ND) {
13902     // Try harder to find a NamedDecl to point at in the note.
13903     while (const ArraySubscriptExpr *ASE =
13904            dyn_cast<ArraySubscriptExpr>(BaseExpr))
13905       BaseExpr = ASE->getBase()->IgnoreParenCasts();
13906     if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
13907       ND = DRE->getDecl();
13908     if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
13909       ND = ME->getMemberDecl();
13910   }
13911 
13912   if (ND)
13913     DiagRuntimeBehavior(ND->getBeginLoc(), BaseExpr,
13914                         PDiag(diag::note_array_declared_here)
13915                             << ND->getDeclName());
13916 }
13917 
13918 void Sema::CheckArrayAccess(const Expr *expr) {
13919   int AllowOnePastEnd = 0;
13920   while (expr) {
13921     expr = expr->IgnoreParenImpCasts();
13922     switch (expr->getStmtClass()) {
13923       case Stmt::ArraySubscriptExprClass: {
13924         const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr);
13925         CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE,
13926                          AllowOnePastEnd > 0);
13927         expr = ASE->getBase();
13928         break;
13929       }
13930       case Stmt::MemberExprClass: {
13931         expr = cast<MemberExpr>(expr)->getBase();
13932         break;
13933       }
13934       case Stmt::OMPArraySectionExprClass: {
13935         const OMPArraySectionExpr *ASE = cast<OMPArraySectionExpr>(expr);
13936         if (ASE->getLowerBound())
13937           CheckArrayAccess(ASE->getBase(), ASE->getLowerBound(),
13938                            /*ASE=*/nullptr, AllowOnePastEnd > 0);
13939         return;
13940       }
13941       case Stmt::UnaryOperatorClass: {
13942         // Only unwrap the * and & unary operators
13943         const UnaryOperator *UO = cast<UnaryOperator>(expr);
13944         expr = UO->getSubExpr();
13945         switch (UO->getOpcode()) {
13946           case UO_AddrOf:
13947             AllowOnePastEnd++;
13948             break;
13949           case UO_Deref:
13950             AllowOnePastEnd--;
13951             break;
13952           default:
13953             return;
13954         }
13955         break;
13956       }
13957       case Stmt::ConditionalOperatorClass: {
13958         const ConditionalOperator *cond = cast<ConditionalOperator>(expr);
13959         if (const Expr *lhs = cond->getLHS())
13960           CheckArrayAccess(lhs);
13961         if (const Expr *rhs = cond->getRHS())
13962           CheckArrayAccess(rhs);
13963         return;
13964       }
13965       case Stmt::CXXOperatorCallExprClass: {
13966         const auto *OCE = cast<CXXOperatorCallExpr>(expr);
13967         for (const auto *Arg : OCE->arguments())
13968           CheckArrayAccess(Arg);
13969         return;
13970       }
13971       default:
13972         return;
13973     }
13974   }
13975 }
13976 
13977 //===--- CHECK: Objective-C retain cycles ----------------------------------//
13978 
13979 namespace {
13980 
13981 struct RetainCycleOwner {
13982   VarDecl *Variable = nullptr;
13983   SourceRange Range;
13984   SourceLocation Loc;
13985   bool Indirect = false;
13986 
13987   RetainCycleOwner() = default;
13988 
13989   void setLocsFrom(Expr *e) {
13990     Loc = e->getExprLoc();
13991     Range = e->getSourceRange();
13992   }
13993 };
13994 
13995 } // namespace
13996 
13997 /// Consider whether capturing the given variable can possibly lead to
13998 /// a retain cycle.
13999 static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) {
14000   // In ARC, it's captured strongly iff the variable has __strong
14001   // lifetime.  In MRR, it's captured strongly if the variable is
14002   // __block and has an appropriate type.
14003   if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
14004     return false;
14005 
14006   owner.Variable = var;
14007   if (ref)
14008     owner.setLocsFrom(ref);
14009   return true;
14010 }
14011 
14012 static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) {
14013   while (true) {
14014     e = e->IgnoreParens();
14015     if (CastExpr *cast = dyn_cast<CastExpr>(e)) {
14016       switch (cast->getCastKind()) {
14017       case CK_BitCast:
14018       case CK_LValueBitCast:
14019       case CK_LValueToRValue:
14020       case CK_ARCReclaimReturnedObject:
14021         e = cast->getSubExpr();
14022         continue;
14023 
14024       default:
14025         return false;
14026       }
14027     }
14028 
14029     if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) {
14030       ObjCIvarDecl *ivar = ref->getDecl();
14031       if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
14032         return false;
14033 
14034       // Try to find a retain cycle in the base.
14035       if (!findRetainCycleOwner(S, ref->getBase(), owner))
14036         return false;
14037 
14038       if (ref->isFreeIvar()) owner.setLocsFrom(ref);
14039       owner.Indirect = true;
14040       return true;
14041     }
14042 
14043     if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) {
14044       VarDecl *var = dyn_cast<VarDecl>(ref->getDecl());
14045       if (!var) return false;
14046       return considerVariable(var, ref, owner);
14047     }
14048 
14049     if (MemberExpr *member = dyn_cast<MemberExpr>(e)) {
14050       if (member->isArrow()) return false;
14051 
14052       // Don't count this as an indirect ownership.
14053       e = member->getBase();
14054       continue;
14055     }
14056 
14057     if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) {
14058       // Only pay attention to pseudo-objects on property references.
14059       ObjCPropertyRefExpr *pre
14060         = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm()
14061                                               ->IgnoreParens());
14062       if (!pre) return false;
14063       if (pre->isImplicitProperty()) return false;
14064       ObjCPropertyDecl *property = pre->getExplicitProperty();
14065       if (!property->isRetaining() &&
14066           !(property->getPropertyIvarDecl() &&
14067             property->getPropertyIvarDecl()->getType()
14068               .getObjCLifetime() == Qualifiers::OCL_Strong))
14069           return false;
14070 
14071       owner.Indirect = true;
14072       if (pre->isSuperReceiver()) {
14073         owner.Variable = S.getCurMethodDecl()->getSelfDecl();
14074         if (!owner.Variable)
14075           return false;
14076         owner.Loc = pre->getLocation();
14077         owner.Range = pre->getSourceRange();
14078         return true;
14079       }
14080       e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase())
14081                               ->getSourceExpr());
14082       continue;
14083     }
14084 
14085     // Array ivars?
14086 
14087     return false;
14088   }
14089 }
14090 
14091 namespace {
14092 
14093   struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> {
14094     ASTContext &Context;
14095     VarDecl *Variable;
14096     Expr *Capturer = nullptr;
14097     bool VarWillBeReased = false;
14098 
14099     FindCaptureVisitor(ASTContext &Context, VarDecl *variable)
14100         : EvaluatedExprVisitor<FindCaptureVisitor>(Context),
14101           Context(Context), Variable(variable) {}
14102 
14103     void VisitDeclRefExpr(DeclRefExpr *ref) {
14104       if (ref->getDecl() == Variable && !Capturer)
14105         Capturer = ref;
14106     }
14107 
14108     void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) {
14109       if (Capturer) return;
14110       Visit(ref->getBase());
14111       if (Capturer && ref->isFreeIvar())
14112         Capturer = ref;
14113     }
14114 
14115     void VisitBlockExpr(BlockExpr *block) {
14116       // Look inside nested blocks
14117       if (block->getBlockDecl()->capturesVariable(Variable))
14118         Visit(block->getBlockDecl()->getBody());
14119     }
14120 
14121     void VisitOpaqueValueExpr(OpaqueValueExpr *OVE) {
14122       if (Capturer) return;
14123       if (OVE->getSourceExpr())
14124         Visit(OVE->getSourceExpr());
14125     }
14126 
14127     void VisitBinaryOperator(BinaryOperator *BinOp) {
14128       if (!Variable || VarWillBeReased || BinOp->getOpcode() != BO_Assign)
14129         return;
14130       Expr *LHS = BinOp->getLHS();
14131       if (const DeclRefExpr *DRE = dyn_cast_or_null<DeclRefExpr>(LHS)) {
14132         if (DRE->getDecl() != Variable)
14133           return;
14134         if (Expr *RHS = BinOp->getRHS()) {
14135           RHS = RHS->IgnoreParenCasts();
14136           llvm::APSInt Value;
14137           VarWillBeReased =
14138             (RHS && RHS->isIntegerConstantExpr(Value, Context) && Value == 0);
14139         }
14140       }
14141     }
14142   };
14143 
14144 } // namespace
14145 
14146 /// Check whether the given argument is a block which captures a
14147 /// variable.
14148 static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) {
14149   assert(owner.Variable && owner.Loc.isValid());
14150 
14151   e = e->IgnoreParenCasts();
14152 
14153   // Look through [^{...} copy] and Block_copy(^{...}).
14154   if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(e)) {
14155     Selector Cmd = ME->getSelector();
14156     if (Cmd.isUnarySelector() && Cmd.getNameForSlot(0) == "copy") {
14157       e = ME->getInstanceReceiver();
14158       if (!e)
14159         return nullptr;
14160       e = e->IgnoreParenCasts();
14161     }
14162   } else if (CallExpr *CE = dyn_cast<CallExpr>(e)) {
14163     if (CE->getNumArgs() == 1) {
14164       FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(CE->getCalleeDecl());
14165       if (Fn) {
14166         const IdentifierInfo *FnI = Fn->getIdentifier();
14167         if (FnI && FnI->isStr("_Block_copy")) {
14168           e = CE->getArg(0)->IgnoreParenCasts();
14169         }
14170       }
14171     }
14172   }
14173 
14174   BlockExpr *block = dyn_cast<BlockExpr>(e);
14175   if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable))
14176     return nullptr;
14177 
14178   FindCaptureVisitor visitor(S.Context, owner.Variable);
14179   visitor.Visit(block->getBlockDecl()->getBody());
14180   return visitor.VarWillBeReased ? nullptr : visitor.Capturer;
14181 }
14182 
14183 static void diagnoseRetainCycle(Sema &S, Expr *capturer,
14184                                 RetainCycleOwner &owner) {
14185   assert(capturer);
14186   assert(owner.Variable && owner.Loc.isValid());
14187 
14188   S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle)
14189     << owner.Variable << capturer->getSourceRange();
14190   S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner)
14191     << owner.Indirect << owner.Range;
14192 }
14193 
14194 /// Check for a keyword selector that starts with the word 'add' or
14195 /// 'set'.
14196 static bool isSetterLikeSelector(Selector sel) {
14197   if (sel.isUnarySelector()) return false;
14198 
14199   StringRef str = sel.getNameForSlot(0);
14200   while (!str.empty() && str.front() == '_') str = str.substr(1);
14201   if (str.startswith("set"))
14202     str = str.substr(3);
14203   else if (str.startswith("add")) {
14204     // Specially allow 'addOperationWithBlock:'.
14205     if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock"))
14206       return false;
14207     str = str.substr(3);
14208   }
14209   else
14210     return false;
14211 
14212   if (str.empty()) return true;
14213   return !isLowercase(str.front());
14214 }
14215 
14216 static Optional<int> GetNSMutableArrayArgumentIndex(Sema &S,
14217                                                     ObjCMessageExpr *Message) {
14218   bool IsMutableArray = S.NSAPIObj->isSubclassOfNSClass(
14219                                                 Message->getReceiverInterface(),
14220                                                 NSAPI::ClassId_NSMutableArray);
14221   if (!IsMutableArray) {
14222     return None;
14223   }
14224 
14225   Selector Sel = Message->getSelector();
14226 
14227   Optional<NSAPI::NSArrayMethodKind> MKOpt =
14228     S.NSAPIObj->getNSArrayMethodKind(Sel);
14229   if (!MKOpt) {
14230     return None;
14231   }
14232 
14233   NSAPI::NSArrayMethodKind MK = *MKOpt;
14234 
14235   switch (MK) {
14236     case NSAPI::NSMutableArr_addObject:
14237     case NSAPI::NSMutableArr_insertObjectAtIndex:
14238     case NSAPI::NSMutableArr_setObjectAtIndexedSubscript:
14239       return 0;
14240     case NSAPI::NSMutableArr_replaceObjectAtIndex:
14241       return 1;
14242 
14243     default:
14244       return None;
14245   }
14246 
14247   return None;
14248 }
14249 
14250 static
14251 Optional<int> GetNSMutableDictionaryArgumentIndex(Sema &S,
14252                                                   ObjCMessageExpr *Message) {
14253   bool IsMutableDictionary = S.NSAPIObj->isSubclassOfNSClass(
14254                                             Message->getReceiverInterface(),
14255                                             NSAPI::ClassId_NSMutableDictionary);
14256   if (!IsMutableDictionary) {
14257     return None;
14258   }
14259 
14260   Selector Sel = Message->getSelector();
14261 
14262   Optional<NSAPI::NSDictionaryMethodKind> MKOpt =
14263     S.NSAPIObj->getNSDictionaryMethodKind(Sel);
14264   if (!MKOpt) {
14265     return None;
14266   }
14267 
14268   NSAPI::NSDictionaryMethodKind MK = *MKOpt;
14269 
14270   switch (MK) {
14271     case NSAPI::NSMutableDict_setObjectForKey:
14272     case NSAPI::NSMutableDict_setValueForKey:
14273     case NSAPI::NSMutableDict_setObjectForKeyedSubscript:
14274       return 0;
14275 
14276     default:
14277       return None;
14278   }
14279 
14280   return None;
14281 }
14282 
14283 static Optional<int> GetNSSetArgumentIndex(Sema &S, ObjCMessageExpr *Message) {
14284   bool IsMutableSet = S.NSAPIObj->isSubclassOfNSClass(
14285                                                 Message->getReceiverInterface(),
14286                                                 NSAPI::ClassId_NSMutableSet);
14287 
14288   bool IsMutableOrderedSet = S.NSAPIObj->isSubclassOfNSClass(
14289                                             Message->getReceiverInterface(),
14290                                             NSAPI::ClassId_NSMutableOrderedSet);
14291   if (!IsMutableSet && !IsMutableOrderedSet) {
14292     return None;
14293   }
14294 
14295   Selector Sel = Message->getSelector();
14296 
14297   Optional<NSAPI::NSSetMethodKind> MKOpt = S.NSAPIObj->getNSSetMethodKind(Sel);
14298   if (!MKOpt) {
14299     return None;
14300   }
14301 
14302   NSAPI::NSSetMethodKind MK = *MKOpt;
14303 
14304   switch (MK) {
14305     case NSAPI::NSMutableSet_addObject:
14306     case NSAPI::NSOrderedSet_setObjectAtIndex:
14307     case NSAPI::NSOrderedSet_setObjectAtIndexedSubscript:
14308     case NSAPI::NSOrderedSet_insertObjectAtIndex:
14309       return 0;
14310     case NSAPI::NSOrderedSet_replaceObjectAtIndexWithObject:
14311       return 1;
14312   }
14313 
14314   return None;
14315 }
14316 
14317 void Sema::CheckObjCCircularContainer(ObjCMessageExpr *Message) {
14318   if (!Message->isInstanceMessage()) {
14319     return;
14320   }
14321 
14322   Optional<int> ArgOpt;
14323 
14324   if (!(ArgOpt = GetNSMutableArrayArgumentIndex(*this, Message)) &&
14325       !(ArgOpt = GetNSMutableDictionaryArgumentIndex(*this, Message)) &&
14326       !(ArgOpt = GetNSSetArgumentIndex(*this, Message))) {
14327     return;
14328   }
14329 
14330   int ArgIndex = *ArgOpt;
14331 
14332   Expr *Arg = Message->getArg(ArgIndex)->IgnoreImpCasts();
14333   if (OpaqueValueExpr *OE = dyn_cast<OpaqueValueExpr>(Arg)) {
14334     Arg = OE->getSourceExpr()->IgnoreImpCasts();
14335   }
14336 
14337   if (Message->getReceiverKind() == ObjCMessageExpr::SuperInstance) {
14338     if (DeclRefExpr *ArgRE = dyn_cast<DeclRefExpr>(Arg)) {
14339       if (ArgRE->isObjCSelfExpr()) {
14340         Diag(Message->getSourceRange().getBegin(),
14341              diag::warn_objc_circular_container)
14342           << ArgRE->getDecl() << StringRef("'super'");
14343       }
14344     }
14345   } else {
14346     Expr *Receiver = Message->getInstanceReceiver()->IgnoreImpCasts();
14347 
14348     if (OpaqueValueExpr *OE = dyn_cast<OpaqueValueExpr>(Receiver)) {
14349       Receiver = OE->getSourceExpr()->IgnoreImpCasts();
14350     }
14351 
14352     if (DeclRefExpr *ReceiverRE = dyn_cast<DeclRefExpr>(Receiver)) {
14353       if (DeclRefExpr *ArgRE = dyn_cast<DeclRefExpr>(Arg)) {
14354         if (ReceiverRE->getDecl() == ArgRE->getDecl()) {
14355           ValueDecl *Decl = ReceiverRE->getDecl();
14356           Diag(Message->getSourceRange().getBegin(),
14357                diag::warn_objc_circular_container)
14358             << Decl << Decl;
14359           if (!ArgRE->isObjCSelfExpr()) {
14360             Diag(Decl->getLocation(),
14361                  diag::note_objc_circular_container_declared_here)
14362               << Decl;
14363           }
14364         }
14365       }
14366     } else if (ObjCIvarRefExpr *IvarRE = dyn_cast<ObjCIvarRefExpr>(Receiver)) {
14367       if (ObjCIvarRefExpr *IvarArgRE = dyn_cast<ObjCIvarRefExpr>(Arg)) {
14368         if (IvarRE->getDecl() == IvarArgRE->getDecl()) {
14369           ObjCIvarDecl *Decl = IvarRE->getDecl();
14370           Diag(Message->getSourceRange().getBegin(),
14371                diag::warn_objc_circular_container)
14372             << Decl << Decl;
14373           Diag(Decl->getLocation(),
14374                diag::note_objc_circular_container_declared_here)
14375             << Decl;
14376         }
14377       }
14378     }
14379   }
14380 }
14381 
14382 /// Check a message send to see if it's likely to cause a retain cycle.
14383 void Sema::checkRetainCycles(ObjCMessageExpr *msg) {
14384   // Only check instance methods whose selector looks like a setter.
14385   if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector()))
14386     return;
14387 
14388   // Try to find a variable that the receiver is strongly owned by.
14389   RetainCycleOwner owner;
14390   if (msg->getReceiverKind() == ObjCMessageExpr::Instance) {
14391     if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner))
14392       return;
14393   } else {
14394     assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance);
14395     owner.Variable = getCurMethodDecl()->getSelfDecl();
14396     owner.Loc = msg->getSuperLoc();
14397     owner.Range = msg->getSuperLoc();
14398   }
14399 
14400   // Check whether the receiver is captured by any of the arguments.
14401   const ObjCMethodDecl *MD = msg->getMethodDecl();
14402   for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) {
14403     if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) {
14404       // noescape blocks should not be retained by the method.
14405       if (MD && MD->parameters()[i]->hasAttr<NoEscapeAttr>())
14406         continue;
14407       return diagnoseRetainCycle(*this, capturer, owner);
14408     }
14409   }
14410 }
14411 
14412 /// Check a property assign to see if it's likely to cause a retain cycle.
14413 void Sema::checkRetainCycles(Expr *receiver, Expr *argument) {
14414   RetainCycleOwner owner;
14415   if (!findRetainCycleOwner(*this, receiver, owner))
14416     return;
14417 
14418   if (Expr *capturer = findCapturingExpr(*this, argument, owner))
14419     diagnoseRetainCycle(*this, capturer, owner);
14420 }
14421 
14422 void Sema::checkRetainCycles(VarDecl *Var, Expr *Init) {
14423   RetainCycleOwner Owner;
14424   if (!considerVariable(Var, /*DeclRefExpr=*/nullptr, Owner))
14425     return;
14426 
14427   // Because we don't have an expression for the variable, we have to set the
14428   // location explicitly here.
14429   Owner.Loc = Var->getLocation();
14430   Owner.Range = Var->getSourceRange();
14431 
14432   if (Expr *Capturer = findCapturingExpr(*this, Init, Owner))
14433     diagnoseRetainCycle(*this, Capturer, Owner);
14434 }
14435 
14436 static bool checkUnsafeAssignLiteral(Sema &S, SourceLocation Loc,
14437                                      Expr *RHS, bool isProperty) {
14438   // Check if RHS is an Objective-C object literal, which also can get
14439   // immediately zapped in a weak reference.  Note that we explicitly
14440   // allow ObjCStringLiterals, since those are designed to never really die.
14441   RHS = RHS->IgnoreParenImpCasts();
14442 
14443   // This enum needs to match with the 'select' in
14444   // warn_objc_arc_literal_assign (off-by-1).
14445   Sema::ObjCLiteralKind Kind = S.CheckLiteralKind(RHS);
14446   if (Kind == Sema::LK_String || Kind == Sema::LK_None)
14447     return false;
14448 
14449   S.Diag(Loc, diag::warn_arc_literal_assign)
14450     << (unsigned) Kind
14451     << (isProperty ? 0 : 1)
14452     << RHS->getSourceRange();
14453 
14454   return true;
14455 }
14456 
14457 static bool checkUnsafeAssignObject(Sema &S, SourceLocation Loc,
14458                                     Qualifiers::ObjCLifetime LT,
14459                                     Expr *RHS, bool isProperty) {
14460   // Strip off any implicit cast added to get to the one ARC-specific.
14461   while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
14462     if (cast->getCastKind() == CK_ARCConsumeObject) {
14463       S.Diag(Loc, diag::warn_arc_retained_assign)
14464         << (LT == Qualifiers::OCL_ExplicitNone)
14465         << (isProperty ? 0 : 1)
14466         << RHS->getSourceRange();
14467       return true;
14468     }
14469     RHS = cast->getSubExpr();
14470   }
14471 
14472   if (LT == Qualifiers::OCL_Weak &&
14473       checkUnsafeAssignLiteral(S, Loc, RHS, isProperty))
14474     return true;
14475 
14476   return false;
14477 }
14478 
14479 bool Sema::checkUnsafeAssigns(SourceLocation Loc,
14480                               QualType LHS, Expr *RHS) {
14481   Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime();
14482 
14483   if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone)
14484     return false;
14485 
14486   if (checkUnsafeAssignObject(*this, Loc, LT, RHS, false))
14487     return true;
14488 
14489   return false;
14490 }
14491 
14492 void Sema::checkUnsafeExprAssigns(SourceLocation Loc,
14493                               Expr *LHS, Expr *RHS) {
14494   QualType LHSType;
14495   // PropertyRef on LHS type need be directly obtained from
14496   // its declaration as it has a PseudoType.
14497   ObjCPropertyRefExpr *PRE
14498     = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens());
14499   if (PRE && !PRE->isImplicitProperty()) {
14500     const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
14501     if (PD)
14502       LHSType = PD->getType();
14503   }
14504 
14505   if (LHSType.isNull())
14506     LHSType = LHS->getType();
14507 
14508   Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime();
14509 
14510   if (LT == Qualifiers::OCL_Weak) {
14511     if (!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, Loc))
14512       getCurFunction()->markSafeWeakUse(LHS);
14513   }
14514 
14515   if (checkUnsafeAssigns(Loc, LHSType, RHS))
14516     return;
14517 
14518   // FIXME. Check for other life times.
14519   if (LT != Qualifiers::OCL_None)
14520     return;
14521 
14522   if (PRE) {
14523     if (PRE->isImplicitProperty())
14524       return;
14525     const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
14526     if (!PD)
14527       return;
14528 
14529     unsigned Attributes = PD->getPropertyAttributes();
14530     if (Attributes & ObjCPropertyAttribute::kind_assign) {
14531       // when 'assign' attribute was not explicitly specified
14532       // by user, ignore it and rely on property type itself
14533       // for lifetime info.
14534       unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten();
14535       if (!(AsWrittenAttr & ObjCPropertyAttribute::kind_assign) &&
14536           LHSType->isObjCRetainableType())
14537         return;
14538 
14539       while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
14540         if (cast->getCastKind() == CK_ARCConsumeObject) {
14541           Diag(Loc, diag::warn_arc_retained_property_assign)
14542           << RHS->getSourceRange();
14543           return;
14544         }
14545         RHS = cast->getSubExpr();
14546       }
14547     } else if (Attributes & ObjCPropertyAttribute::kind_weak) {
14548       if (checkUnsafeAssignObject(*this, Loc, Qualifiers::OCL_Weak, RHS, true))
14549         return;
14550     }
14551   }
14552 }
14553 
14554 //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===//
14555 
14556 static bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr,
14557                                         SourceLocation StmtLoc,
14558                                         const NullStmt *Body) {
14559   // Do not warn if the body is a macro that expands to nothing, e.g:
14560   //
14561   // #define CALL(x)
14562   // if (condition)
14563   //   CALL(0);
14564   if (Body->hasLeadingEmptyMacro())
14565     return false;
14566 
14567   // Get line numbers of statement and body.
14568   bool StmtLineInvalid;
14569   unsigned StmtLine = SourceMgr.getPresumedLineNumber(StmtLoc,
14570                                                       &StmtLineInvalid);
14571   if (StmtLineInvalid)
14572     return false;
14573 
14574   bool BodyLineInvalid;
14575   unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(),
14576                                                       &BodyLineInvalid);
14577   if (BodyLineInvalid)
14578     return false;
14579 
14580   // Warn if null statement and body are on the same line.
14581   if (StmtLine != BodyLine)
14582     return false;
14583 
14584   return true;
14585 }
14586 
14587 void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc,
14588                                  const Stmt *Body,
14589                                  unsigned DiagID) {
14590   // Since this is a syntactic check, don't emit diagnostic for template
14591   // instantiations, this just adds noise.
14592   if (CurrentInstantiationScope)
14593     return;
14594 
14595   // The body should be a null statement.
14596   const NullStmt *NBody = dyn_cast<NullStmt>(Body);
14597   if (!NBody)
14598     return;
14599 
14600   // Do the usual checks.
14601   if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody))
14602     return;
14603 
14604   Diag(NBody->getSemiLoc(), DiagID);
14605   Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line);
14606 }
14607 
14608 void Sema::DiagnoseEmptyLoopBody(const Stmt *S,
14609                                  const Stmt *PossibleBody) {
14610   assert(!CurrentInstantiationScope); // Ensured by caller
14611 
14612   SourceLocation StmtLoc;
14613   const Stmt *Body;
14614   unsigned DiagID;
14615   if (const ForStmt *FS = dyn_cast<ForStmt>(S)) {
14616     StmtLoc = FS->getRParenLoc();
14617     Body = FS->getBody();
14618     DiagID = diag::warn_empty_for_body;
14619   } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) {
14620     StmtLoc = WS->getCond()->getSourceRange().getEnd();
14621     Body = WS->getBody();
14622     DiagID = diag::warn_empty_while_body;
14623   } else
14624     return; // Neither `for' nor `while'.
14625 
14626   // The body should be a null statement.
14627   const NullStmt *NBody = dyn_cast<NullStmt>(Body);
14628   if (!NBody)
14629     return;
14630 
14631   // Skip expensive checks if diagnostic is disabled.
14632   if (Diags.isIgnored(DiagID, NBody->getSemiLoc()))
14633     return;
14634 
14635   // Do the usual checks.
14636   if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody))
14637     return;
14638 
14639   // `for(...);' and `while(...);' are popular idioms, so in order to keep
14640   // noise level low, emit diagnostics only if for/while is followed by a
14641   // CompoundStmt, e.g.:
14642   //    for (int i = 0; i < n; i++);
14643   //    {
14644   //      a(i);
14645   //    }
14646   // or if for/while is followed by a statement with more indentation
14647   // than for/while itself:
14648   //    for (int i = 0; i < n; i++);
14649   //      a(i);
14650   bool ProbableTypo = isa<CompoundStmt>(PossibleBody);
14651   if (!ProbableTypo) {
14652     bool BodyColInvalid;
14653     unsigned BodyCol = SourceMgr.getPresumedColumnNumber(
14654         PossibleBody->getBeginLoc(), &BodyColInvalid);
14655     if (BodyColInvalid)
14656       return;
14657 
14658     bool StmtColInvalid;
14659     unsigned StmtCol =
14660         SourceMgr.getPresumedColumnNumber(S->getBeginLoc(), &StmtColInvalid);
14661     if (StmtColInvalid)
14662       return;
14663 
14664     if (BodyCol > StmtCol)
14665       ProbableTypo = true;
14666   }
14667 
14668   if (ProbableTypo) {
14669     Diag(NBody->getSemiLoc(), DiagID);
14670     Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line);
14671   }
14672 }
14673 
14674 //===--- CHECK: Warn on self move with std::move. -------------------------===//
14675 
14676 /// DiagnoseSelfMove - Emits a warning if a value is moved to itself.
14677 void Sema::DiagnoseSelfMove(const Expr *LHSExpr, const Expr *RHSExpr,
14678                              SourceLocation OpLoc) {
14679   if (Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, OpLoc))
14680     return;
14681 
14682   if (inTemplateInstantiation())
14683     return;
14684 
14685   // Strip parens and casts away.
14686   LHSExpr = LHSExpr->IgnoreParenImpCasts();
14687   RHSExpr = RHSExpr->IgnoreParenImpCasts();
14688 
14689   // Check for a call expression
14690   const CallExpr *CE = dyn_cast<CallExpr>(RHSExpr);
14691   if (!CE || CE->getNumArgs() != 1)
14692     return;
14693 
14694   // Check for a call to std::move
14695   if (!CE->isCallToStdMove())
14696     return;
14697 
14698   // Get argument from std::move
14699   RHSExpr = CE->getArg(0);
14700 
14701   const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr);
14702   const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr);
14703 
14704   // Two DeclRefExpr's, check that the decls are the same.
14705   if (LHSDeclRef && RHSDeclRef) {
14706     if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl())
14707       return;
14708     if (LHSDeclRef->getDecl()->getCanonicalDecl() !=
14709         RHSDeclRef->getDecl()->getCanonicalDecl())
14710       return;
14711 
14712     Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType()
14713                                         << LHSExpr->getSourceRange()
14714                                         << RHSExpr->getSourceRange();
14715     return;
14716   }
14717 
14718   // Member variables require a different approach to check for self moves.
14719   // MemberExpr's are the same if every nested MemberExpr refers to the same
14720   // Decl and that the base Expr's are DeclRefExpr's with the same Decl or
14721   // the base Expr's are CXXThisExpr's.
14722   const Expr *LHSBase = LHSExpr;
14723   const Expr *RHSBase = RHSExpr;
14724   const MemberExpr *LHSME = dyn_cast<MemberExpr>(LHSExpr);
14725   const MemberExpr *RHSME = dyn_cast<MemberExpr>(RHSExpr);
14726   if (!LHSME || !RHSME)
14727     return;
14728 
14729   while (LHSME && RHSME) {
14730     if (LHSME->getMemberDecl()->getCanonicalDecl() !=
14731         RHSME->getMemberDecl()->getCanonicalDecl())
14732       return;
14733 
14734     LHSBase = LHSME->getBase();
14735     RHSBase = RHSME->getBase();
14736     LHSME = dyn_cast<MemberExpr>(LHSBase);
14737     RHSME = dyn_cast<MemberExpr>(RHSBase);
14738   }
14739 
14740   LHSDeclRef = dyn_cast<DeclRefExpr>(LHSBase);
14741   RHSDeclRef = dyn_cast<DeclRefExpr>(RHSBase);
14742   if (LHSDeclRef && RHSDeclRef) {
14743     if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl())
14744       return;
14745     if (LHSDeclRef->getDecl()->getCanonicalDecl() !=
14746         RHSDeclRef->getDecl()->getCanonicalDecl())
14747       return;
14748 
14749     Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType()
14750                                         << LHSExpr->getSourceRange()
14751                                         << RHSExpr->getSourceRange();
14752     return;
14753   }
14754 
14755   if (isa<CXXThisExpr>(LHSBase) && isa<CXXThisExpr>(RHSBase))
14756     Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType()
14757                                         << LHSExpr->getSourceRange()
14758                                         << RHSExpr->getSourceRange();
14759 }
14760 
14761 //===--- Layout compatibility ----------------------------------------------//
14762 
14763 static bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2);
14764 
14765 /// Check if two enumeration types are layout-compatible.
14766 static bool isLayoutCompatible(ASTContext &C, EnumDecl *ED1, EnumDecl *ED2) {
14767   // C++11 [dcl.enum] p8:
14768   // Two enumeration types are layout-compatible if they have the same
14769   // underlying type.
14770   return ED1->isComplete() && ED2->isComplete() &&
14771          C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType());
14772 }
14773 
14774 /// Check if two fields are layout-compatible.
14775 static bool isLayoutCompatible(ASTContext &C, FieldDecl *Field1,
14776                                FieldDecl *Field2) {
14777   if (!isLayoutCompatible(C, Field1->getType(), Field2->getType()))
14778     return false;
14779 
14780   if (Field1->isBitField() != Field2->isBitField())
14781     return false;
14782 
14783   if (Field1->isBitField()) {
14784     // Make sure that the bit-fields are the same length.
14785     unsigned Bits1 = Field1->getBitWidthValue(C);
14786     unsigned Bits2 = Field2->getBitWidthValue(C);
14787 
14788     if (Bits1 != Bits2)
14789       return false;
14790   }
14791 
14792   return true;
14793 }
14794 
14795 /// Check if two standard-layout structs are layout-compatible.
14796 /// (C++11 [class.mem] p17)
14797 static bool isLayoutCompatibleStruct(ASTContext &C, RecordDecl *RD1,
14798                                      RecordDecl *RD2) {
14799   // If both records are C++ classes, check that base classes match.
14800   if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1)) {
14801     // If one of records is a CXXRecordDecl we are in C++ mode,
14802     // thus the other one is a CXXRecordDecl, too.
14803     const CXXRecordDecl *D2CXX = cast<CXXRecordDecl>(RD2);
14804     // Check number of base classes.
14805     if (D1CXX->getNumBases() != D2CXX->getNumBases())
14806       return false;
14807 
14808     // Check the base classes.
14809     for (CXXRecordDecl::base_class_const_iterator
14810                Base1 = D1CXX->bases_begin(),
14811            BaseEnd1 = D1CXX->bases_end(),
14812               Base2 = D2CXX->bases_begin();
14813          Base1 != BaseEnd1;
14814          ++Base1, ++Base2) {
14815       if (!isLayoutCompatible(C, Base1->getType(), Base2->getType()))
14816         return false;
14817     }
14818   } else if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2)) {
14819     // If only RD2 is a C++ class, it should have zero base classes.
14820     if (D2CXX->getNumBases() > 0)
14821       return false;
14822   }
14823 
14824   // Check the fields.
14825   RecordDecl::field_iterator Field2 = RD2->field_begin(),
14826                              Field2End = RD2->field_end(),
14827                              Field1 = RD1->field_begin(),
14828                              Field1End = RD1->field_end();
14829   for ( ; Field1 != Field1End && Field2 != Field2End; ++Field1, ++Field2) {
14830     if (!isLayoutCompatible(C, *Field1, *Field2))
14831       return false;
14832   }
14833   if (Field1 != Field1End || Field2 != Field2End)
14834     return false;
14835 
14836   return true;
14837 }
14838 
14839 /// Check if two standard-layout unions are layout-compatible.
14840 /// (C++11 [class.mem] p18)
14841 static bool isLayoutCompatibleUnion(ASTContext &C, RecordDecl *RD1,
14842                                     RecordDecl *RD2) {
14843   llvm::SmallPtrSet<FieldDecl *, 8> UnmatchedFields;
14844   for (auto *Field2 : RD2->fields())
14845     UnmatchedFields.insert(Field2);
14846 
14847   for (auto *Field1 : RD1->fields()) {
14848     llvm::SmallPtrSet<FieldDecl *, 8>::iterator
14849         I = UnmatchedFields.begin(),
14850         E = UnmatchedFields.end();
14851 
14852     for ( ; I != E; ++I) {
14853       if (isLayoutCompatible(C, Field1, *I)) {
14854         bool Result = UnmatchedFields.erase(*I);
14855         (void) Result;
14856         assert(Result);
14857         break;
14858       }
14859     }
14860     if (I == E)
14861       return false;
14862   }
14863 
14864   return UnmatchedFields.empty();
14865 }
14866 
14867 static bool isLayoutCompatible(ASTContext &C, RecordDecl *RD1,
14868                                RecordDecl *RD2) {
14869   if (RD1->isUnion() != RD2->isUnion())
14870     return false;
14871 
14872   if (RD1->isUnion())
14873     return isLayoutCompatibleUnion(C, RD1, RD2);
14874   else
14875     return isLayoutCompatibleStruct(C, RD1, RD2);
14876 }
14877 
14878 /// Check if two types are layout-compatible in C++11 sense.
14879 static bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2) {
14880   if (T1.isNull() || T2.isNull())
14881     return false;
14882 
14883   // C++11 [basic.types] p11:
14884   // If two types T1 and T2 are the same type, then T1 and T2 are
14885   // layout-compatible types.
14886   if (C.hasSameType(T1, T2))
14887     return true;
14888 
14889   T1 = T1.getCanonicalType().getUnqualifiedType();
14890   T2 = T2.getCanonicalType().getUnqualifiedType();
14891 
14892   const Type::TypeClass TC1 = T1->getTypeClass();
14893   const Type::TypeClass TC2 = T2->getTypeClass();
14894 
14895   if (TC1 != TC2)
14896     return false;
14897 
14898   if (TC1 == Type::Enum) {
14899     return isLayoutCompatible(C,
14900                               cast<EnumType>(T1)->getDecl(),
14901                               cast<EnumType>(T2)->getDecl());
14902   } else if (TC1 == Type::Record) {
14903     if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType())
14904       return false;
14905 
14906     return isLayoutCompatible(C,
14907                               cast<RecordType>(T1)->getDecl(),
14908                               cast<RecordType>(T2)->getDecl());
14909   }
14910 
14911   return false;
14912 }
14913 
14914 //===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----//
14915 
14916 /// Given a type tag expression find the type tag itself.
14917 ///
14918 /// \param TypeExpr Type tag expression, as it appears in user's code.
14919 ///
14920 /// \param VD Declaration of an identifier that appears in a type tag.
14921 ///
14922 /// \param MagicValue Type tag magic value.
14923 ///
14924 /// \param isConstantEvaluated wether the evalaution should be performed in
14925 
14926 /// constant context.
14927 static bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx,
14928                             const ValueDecl **VD, uint64_t *MagicValue,
14929                             bool isConstantEvaluated) {
14930   while(true) {
14931     if (!TypeExpr)
14932       return false;
14933 
14934     TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts();
14935 
14936     switch (TypeExpr->getStmtClass()) {
14937     case Stmt::UnaryOperatorClass: {
14938       const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr);
14939       if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) {
14940         TypeExpr = UO->getSubExpr();
14941         continue;
14942       }
14943       return false;
14944     }
14945 
14946     case Stmt::DeclRefExprClass: {
14947       const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr);
14948       *VD = DRE->getDecl();
14949       return true;
14950     }
14951 
14952     case Stmt::IntegerLiteralClass: {
14953       const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr);
14954       llvm::APInt MagicValueAPInt = IL->getValue();
14955       if (MagicValueAPInt.getActiveBits() <= 64) {
14956         *MagicValue = MagicValueAPInt.getZExtValue();
14957         return true;
14958       } else
14959         return false;
14960     }
14961 
14962     case Stmt::BinaryConditionalOperatorClass:
14963     case Stmt::ConditionalOperatorClass: {
14964       const AbstractConditionalOperator *ACO =
14965           cast<AbstractConditionalOperator>(TypeExpr);
14966       bool Result;
14967       if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx,
14968                                                      isConstantEvaluated)) {
14969         if (Result)
14970           TypeExpr = ACO->getTrueExpr();
14971         else
14972           TypeExpr = ACO->getFalseExpr();
14973         continue;
14974       }
14975       return false;
14976     }
14977 
14978     case Stmt::BinaryOperatorClass: {
14979       const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr);
14980       if (BO->getOpcode() == BO_Comma) {
14981         TypeExpr = BO->getRHS();
14982         continue;
14983       }
14984       return false;
14985     }
14986 
14987     default:
14988       return false;
14989     }
14990   }
14991 }
14992 
14993 /// Retrieve the C type corresponding to type tag TypeExpr.
14994 ///
14995 /// \param TypeExpr Expression that specifies a type tag.
14996 ///
14997 /// \param MagicValues Registered magic values.
14998 ///
14999 /// \param FoundWrongKind Set to true if a type tag was found, but of a wrong
15000 ///        kind.
15001 ///
15002 /// \param TypeInfo Information about the corresponding C type.
15003 ///
15004 /// \param isConstantEvaluated wether the evalaution should be performed in
15005 /// constant context.
15006 ///
15007 /// \returns true if the corresponding C type was found.
15008 static bool GetMatchingCType(
15009     const IdentifierInfo *ArgumentKind, const Expr *TypeExpr,
15010     const ASTContext &Ctx,
15011     const llvm::DenseMap<Sema::TypeTagMagicValue, Sema::TypeTagData>
15012         *MagicValues,
15013     bool &FoundWrongKind, Sema::TypeTagData &TypeInfo,
15014     bool isConstantEvaluated) {
15015   FoundWrongKind = false;
15016 
15017   // Variable declaration that has type_tag_for_datatype attribute.
15018   const ValueDecl *VD = nullptr;
15019 
15020   uint64_t MagicValue;
15021 
15022   if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue, isConstantEvaluated))
15023     return false;
15024 
15025   if (VD) {
15026     if (TypeTagForDatatypeAttr *I = VD->getAttr<TypeTagForDatatypeAttr>()) {
15027       if (I->getArgumentKind() != ArgumentKind) {
15028         FoundWrongKind = true;
15029         return false;
15030       }
15031       TypeInfo.Type = I->getMatchingCType();
15032       TypeInfo.LayoutCompatible = I->getLayoutCompatible();
15033       TypeInfo.MustBeNull = I->getMustBeNull();
15034       return true;
15035     }
15036     return false;
15037   }
15038 
15039   if (!MagicValues)
15040     return false;
15041 
15042   llvm::DenseMap<Sema::TypeTagMagicValue,
15043                  Sema::TypeTagData>::const_iterator I =
15044       MagicValues->find(std::make_pair(ArgumentKind, MagicValue));
15045   if (I == MagicValues->end())
15046     return false;
15047 
15048   TypeInfo = I->second;
15049   return true;
15050 }
15051 
15052 void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind,
15053                                       uint64_t MagicValue, QualType Type,
15054                                       bool LayoutCompatible,
15055                                       bool MustBeNull) {
15056   if (!TypeTagForDatatypeMagicValues)
15057     TypeTagForDatatypeMagicValues.reset(
15058         new llvm::DenseMap<TypeTagMagicValue, TypeTagData>);
15059 
15060   TypeTagMagicValue Magic(ArgumentKind, MagicValue);
15061   (*TypeTagForDatatypeMagicValues)[Magic] =
15062       TypeTagData(Type, LayoutCompatible, MustBeNull);
15063 }
15064 
15065 static bool IsSameCharType(QualType T1, QualType T2) {
15066   const BuiltinType *BT1 = T1->getAs<BuiltinType>();
15067   if (!BT1)
15068     return false;
15069 
15070   const BuiltinType *BT2 = T2->getAs<BuiltinType>();
15071   if (!BT2)
15072     return false;
15073 
15074   BuiltinType::Kind T1Kind = BT1->getKind();
15075   BuiltinType::Kind T2Kind = BT2->getKind();
15076 
15077   return (T1Kind == BuiltinType::SChar  && T2Kind == BuiltinType::Char_S) ||
15078          (T1Kind == BuiltinType::UChar  && T2Kind == BuiltinType::Char_U) ||
15079          (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) ||
15080          (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar);
15081 }
15082 
15083 void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr,
15084                                     const ArrayRef<const Expr *> ExprArgs,
15085                                     SourceLocation CallSiteLoc) {
15086   const IdentifierInfo *ArgumentKind = Attr->getArgumentKind();
15087   bool IsPointerAttr = Attr->getIsPointer();
15088 
15089   // Retrieve the argument representing the 'type_tag'.
15090   unsigned TypeTagIdxAST = Attr->getTypeTagIdx().getASTIndex();
15091   if (TypeTagIdxAST >= ExprArgs.size()) {
15092     Diag(CallSiteLoc, diag::err_tag_index_out_of_range)
15093         << 0 << Attr->getTypeTagIdx().getSourceIndex();
15094     return;
15095   }
15096   const Expr *TypeTagExpr = ExprArgs[TypeTagIdxAST];
15097   bool FoundWrongKind;
15098   TypeTagData TypeInfo;
15099   if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context,
15100                         TypeTagForDatatypeMagicValues.get(), FoundWrongKind,
15101                         TypeInfo, isConstantEvaluated())) {
15102     if (FoundWrongKind)
15103       Diag(TypeTagExpr->getExprLoc(),
15104            diag::warn_type_tag_for_datatype_wrong_kind)
15105         << TypeTagExpr->getSourceRange();
15106     return;
15107   }
15108 
15109   // Retrieve the argument representing the 'arg_idx'.
15110   unsigned ArgumentIdxAST = Attr->getArgumentIdx().getASTIndex();
15111   if (ArgumentIdxAST >= ExprArgs.size()) {
15112     Diag(CallSiteLoc, diag::err_tag_index_out_of_range)
15113         << 1 << Attr->getArgumentIdx().getSourceIndex();
15114     return;
15115   }
15116   const Expr *ArgumentExpr = ExprArgs[ArgumentIdxAST];
15117   if (IsPointerAttr) {
15118     // Skip implicit cast of pointer to `void *' (as a function argument).
15119     if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr))
15120       if (ICE->getType()->isVoidPointerType() &&
15121           ICE->getCastKind() == CK_BitCast)
15122         ArgumentExpr = ICE->getSubExpr();
15123   }
15124   QualType ArgumentType = ArgumentExpr->getType();
15125 
15126   // Passing a `void*' pointer shouldn't trigger a warning.
15127   if (IsPointerAttr && ArgumentType->isVoidPointerType())
15128     return;
15129 
15130   if (TypeInfo.MustBeNull) {
15131     // Type tag with matching void type requires a null pointer.
15132     if (!ArgumentExpr->isNullPointerConstant(Context,
15133                                              Expr::NPC_ValueDependentIsNotNull)) {
15134       Diag(ArgumentExpr->getExprLoc(),
15135            diag::warn_type_safety_null_pointer_required)
15136           << ArgumentKind->getName()
15137           << ArgumentExpr->getSourceRange()
15138           << TypeTagExpr->getSourceRange();
15139     }
15140     return;
15141   }
15142 
15143   QualType RequiredType = TypeInfo.Type;
15144   if (IsPointerAttr)
15145     RequiredType = Context.getPointerType(RequiredType);
15146 
15147   bool mismatch = false;
15148   if (!TypeInfo.LayoutCompatible) {
15149     mismatch = !Context.hasSameType(ArgumentType, RequiredType);
15150 
15151     // C++11 [basic.fundamental] p1:
15152     // Plain char, signed char, and unsigned char are three distinct types.
15153     //
15154     // But we treat plain `char' as equivalent to `signed char' or `unsigned
15155     // char' depending on the current char signedness mode.
15156     if (mismatch)
15157       if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(),
15158                                            RequiredType->getPointeeType())) ||
15159           (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType)))
15160         mismatch = false;
15161   } else
15162     if (IsPointerAttr)
15163       mismatch = !isLayoutCompatible(Context,
15164                                      ArgumentType->getPointeeType(),
15165                                      RequiredType->getPointeeType());
15166     else
15167       mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType);
15168 
15169   if (mismatch)
15170     Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch)
15171         << ArgumentType << ArgumentKind
15172         << TypeInfo.LayoutCompatible << RequiredType
15173         << ArgumentExpr->getSourceRange()
15174         << TypeTagExpr->getSourceRange();
15175 }
15176 
15177 void Sema::AddPotentialMisalignedMembers(Expr *E, RecordDecl *RD, ValueDecl *MD,
15178                                          CharUnits Alignment) {
15179   MisalignedMembers.emplace_back(E, RD, MD, Alignment);
15180 }
15181 
15182 void Sema::DiagnoseMisalignedMembers() {
15183   for (MisalignedMember &m : MisalignedMembers) {
15184     const NamedDecl *ND = m.RD;
15185     if (ND->getName().empty()) {
15186       if (const TypedefNameDecl *TD = m.RD->getTypedefNameForAnonDecl())
15187         ND = TD;
15188     }
15189     Diag(m.E->getBeginLoc(), diag::warn_taking_address_of_packed_member)
15190         << m.MD << ND << m.E->getSourceRange();
15191   }
15192   MisalignedMembers.clear();
15193 }
15194 
15195 void Sema::DiscardMisalignedMemberAddress(const Type *T, Expr *E) {
15196   E = E->IgnoreParens();
15197   if (!T->isPointerType() && !T->isIntegerType())
15198     return;
15199   if (isa<UnaryOperator>(E) &&
15200       cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf) {
15201     auto *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
15202     if (isa<MemberExpr>(Op)) {
15203       auto MA = llvm::find(MisalignedMembers, MisalignedMember(Op));
15204       if (MA != MisalignedMembers.end() &&
15205           (T->isIntegerType() ||
15206            (T->isPointerType() && (T->getPointeeType()->isIncompleteType() ||
15207                                    Context.getTypeAlignInChars(
15208                                        T->getPointeeType()) <= MA->Alignment))))
15209         MisalignedMembers.erase(MA);
15210     }
15211   }
15212 }
15213 
15214 void Sema::RefersToMemberWithReducedAlignment(
15215     Expr *E,
15216     llvm::function_ref<void(Expr *, RecordDecl *, FieldDecl *, CharUnits)>
15217         Action) {
15218   const auto *ME = dyn_cast<MemberExpr>(E);
15219   if (!ME)
15220     return;
15221 
15222   // No need to check expressions with an __unaligned-qualified type.
15223   if (E->getType().getQualifiers().hasUnaligned())
15224     return;
15225 
15226   // For a chain of MemberExpr like "a.b.c.d" this list
15227   // will keep FieldDecl's like [d, c, b].
15228   SmallVector<FieldDecl *, 4> ReverseMemberChain;
15229   const MemberExpr *TopME = nullptr;
15230   bool AnyIsPacked = false;
15231   do {
15232     QualType BaseType = ME->getBase()->getType();
15233     if (BaseType->isDependentType())
15234       return;
15235     if (ME->isArrow())
15236       BaseType = BaseType->getPointeeType();
15237     RecordDecl *RD = BaseType->castAs<RecordType>()->getDecl();
15238     if (RD->isInvalidDecl())
15239       return;
15240 
15241     ValueDecl *MD = ME->getMemberDecl();
15242     auto *FD = dyn_cast<FieldDecl>(MD);
15243     // We do not care about non-data members.
15244     if (!FD || FD->isInvalidDecl())
15245       return;
15246 
15247     AnyIsPacked =
15248         AnyIsPacked || (RD->hasAttr<PackedAttr>() || MD->hasAttr<PackedAttr>());
15249     ReverseMemberChain.push_back(FD);
15250 
15251     TopME = ME;
15252     ME = dyn_cast<MemberExpr>(ME->getBase()->IgnoreParens());
15253   } while (ME);
15254   assert(TopME && "We did not compute a topmost MemberExpr!");
15255 
15256   // Not the scope of this diagnostic.
15257   if (!AnyIsPacked)
15258     return;
15259 
15260   const Expr *TopBase = TopME->getBase()->IgnoreParenImpCasts();
15261   const auto *DRE = dyn_cast<DeclRefExpr>(TopBase);
15262   // TODO: The innermost base of the member expression may be too complicated.
15263   // For now, just disregard these cases. This is left for future
15264   // improvement.
15265   if (!DRE && !isa<CXXThisExpr>(TopBase))
15266       return;
15267 
15268   // Alignment expected by the whole expression.
15269   CharUnits ExpectedAlignment = Context.getTypeAlignInChars(E->getType());
15270 
15271   // No need to do anything else with this case.
15272   if (ExpectedAlignment.isOne())
15273     return;
15274 
15275   // Synthesize offset of the whole access.
15276   CharUnits Offset;
15277   for (auto I = ReverseMemberChain.rbegin(); I != ReverseMemberChain.rend();
15278        I++) {
15279     Offset += Context.toCharUnitsFromBits(Context.getFieldOffset(*I));
15280   }
15281 
15282   // Compute the CompleteObjectAlignment as the alignment of the whole chain.
15283   CharUnits CompleteObjectAlignment = Context.getTypeAlignInChars(
15284       ReverseMemberChain.back()->getParent()->getTypeForDecl());
15285 
15286   // The base expression of the innermost MemberExpr may give
15287   // stronger guarantees than the class containing the member.
15288   if (DRE && !TopME->isArrow()) {
15289     const ValueDecl *VD = DRE->getDecl();
15290     if (!VD->getType()->isReferenceType())
15291       CompleteObjectAlignment =
15292           std::max(CompleteObjectAlignment, Context.getDeclAlign(VD));
15293   }
15294 
15295   // Check if the synthesized offset fulfills the alignment.
15296   if (Offset % ExpectedAlignment != 0 ||
15297       // It may fulfill the offset it but the effective alignment may still be
15298       // lower than the expected expression alignment.
15299       CompleteObjectAlignment < ExpectedAlignment) {
15300     // If this happens, we want to determine a sensible culprit of this.
15301     // Intuitively, watching the chain of member expressions from right to
15302     // left, we start with the required alignment (as required by the field
15303     // type) but some packed attribute in that chain has reduced the alignment.
15304     // It may happen that another packed structure increases it again. But if
15305     // we are here such increase has not been enough. So pointing the first
15306     // FieldDecl that either is packed or else its RecordDecl is,
15307     // seems reasonable.
15308     FieldDecl *FD = nullptr;
15309     CharUnits Alignment;
15310     for (FieldDecl *FDI : ReverseMemberChain) {
15311       if (FDI->hasAttr<PackedAttr>() ||
15312           FDI->getParent()->hasAttr<PackedAttr>()) {
15313         FD = FDI;
15314         Alignment = std::min(
15315             Context.getTypeAlignInChars(FD->getType()),
15316             Context.getTypeAlignInChars(FD->getParent()->getTypeForDecl()));
15317         break;
15318       }
15319     }
15320     assert(FD && "We did not find a packed FieldDecl!");
15321     Action(E, FD->getParent(), FD, Alignment);
15322   }
15323 }
15324 
15325 void Sema::CheckAddressOfPackedMember(Expr *rhs) {
15326   using namespace std::placeholders;
15327 
15328   RefersToMemberWithReducedAlignment(
15329       rhs, std::bind(&Sema::AddPotentialMisalignedMembers, std::ref(*this), _1,
15330                      _2, _3, _4));
15331 }
15332 
15333 ExprResult Sema::SemaBuiltinMatrixTranspose(CallExpr *TheCall,
15334                                             ExprResult CallResult) {
15335   if (checkArgCount(*this, TheCall, 1))
15336     return ExprError();
15337 
15338   ExprResult MatrixArg = DefaultLvalueConversion(TheCall->getArg(0));
15339   if (MatrixArg.isInvalid())
15340     return MatrixArg;
15341   Expr *Matrix = MatrixArg.get();
15342 
15343   auto *MType = Matrix->getType()->getAs<ConstantMatrixType>();
15344   if (!MType) {
15345     Diag(Matrix->getBeginLoc(), diag::err_builtin_matrix_arg);
15346     return ExprError();
15347   }
15348 
15349   // Create returned matrix type by swapping rows and columns of the argument
15350   // matrix type.
15351   QualType ResultType = Context.getConstantMatrixType(
15352       MType->getElementType(), MType->getNumColumns(), MType->getNumRows());
15353 
15354   // Change the return type to the type of the returned matrix.
15355   TheCall->setType(ResultType);
15356 
15357   // Update call argument to use the possibly converted matrix argument.
15358   TheCall->setArg(0, Matrix);
15359   return CallResult;
15360 }
15361 
15362 // Get and verify the matrix dimensions.
15363 static llvm::Optional<unsigned>
15364 getAndVerifyMatrixDimension(Expr *Expr, StringRef Name, Sema &S) {
15365   llvm::APSInt Value(64);
15366   SourceLocation ErrorPos;
15367   if (!Expr->isIntegerConstantExpr(Value, S.Context, &ErrorPos)) {
15368     S.Diag(Expr->getBeginLoc(), diag::err_builtin_matrix_scalar_unsigned_arg)
15369         << Name;
15370     return {};
15371   }
15372   uint64_t Dim = Value.getZExtValue();
15373   if (!ConstantMatrixType::isDimensionValid(Dim)) {
15374     S.Diag(Expr->getBeginLoc(), diag::err_builtin_matrix_invalid_dimension)
15375         << Name << ConstantMatrixType::getMaxElementsPerDimension();
15376     return {};
15377   }
15378   return Dim;
15379 }
15380 
15381 ExprResult Sema::SemaBuiltinMatrixColumnMajorLoad(CallExpr *TheCall,
15382                                                   ExprResult CallResult) {
15383   if (!getLangOpts().MatrixTypes) {
15384     Diag(TheCall->getBeginLoc(), diag::err_builtin_matrix_disabled);
15385     return ExprError();
15386   }
15387 
15388   if (checkArgCount(*this, TheCall, 4))
15389     return ExprError();
15390 
15391   unsigned PtrArgIdx = 0;
15392   Expr *PtrExpr = TheCall->getArg(PtrArgIdx);
15393   Expr *RowsExpr = TheCall->getArg(1);
15394   Expr *ColumnsExpr = TheCall->getArg(2);
15395   Expr *StrideExpr = TheCall->getArg(3);
15396 
15397   bool ArgError = false;
15398 
15399   // Check pointer argument.
15400   {
15401     ExprResult PtrConv = DefaultFunctionArrayLvalueConversion(PtrExpr);
15402     if (PtrConv.isInvalid())
15403       return PtrConv;
15404     PtrExpr = PtrConv.get();
15405     TheCall->setArg(0, PtrExpr);
15406     if (PtrExpr->isTypeDependent()) {
15407       TheCall->setType(Context.DependentTy);
15408       return TheCall;
15409     }
15410   }
15411 
15412   auto *PtrTy = PtrExpr->getType()->getAs<PointerType>();
15413   QualType ElementTy;
15414   if (!PtrTy) {
15415     Diag(PtrExpr->getBeginLoc(), diag::err_builtin_matrix_pointer_arg)
15416         << PtrArgIdx + 1;
15417     ArgError = true;
15418   } else {
15419     ElementTy = PtrTy->getPointeeType().getUnqualifiedType();
15420 
15421     if (!ConstantMatrixType::isValidElementType(ElementTy)) {
15422       Diag(PtrExpr->getBeginLoc(), diag::err_builtin_matrix_pointer_arg)
15423           << PtrArgIdx + 1;
15424       ArgError = true;
15425     }
15426   }
15427 
15428   // Apply default Lvalue conversions and convert the expression to size_t.
15429   auto ApplyArgumentConversions = [this](Expr *E) {
15430     ExprResult Conv = DefaultLvalueConversion(E);
15431     if (Conv.isInvalid())
15432       return Conv;
15433 
15434     return tryConvertExprToType(Conv.get(), Context.getSizeType());
15435   };
15436 
15437   // Apply conversion to row and column expressions.
15438   ExprResult RowsConv = ApplyArgumentConversions(RowsExpr);
15439   if (!RowsConv.isInvalid()) {
15440     RowsExpr = RowsConv.get();
15441     TheCall->setArg(1, RowsExpr);
15442   } else
15443     RowsExpr = nullptr;
15444 
15445   ExprResult ColumnsConv = ApplyArgumentConversions(ColumnsExpr);
15446   if (!ColumnsConv.isInvalid()) {
15447     ColumnsExpr = ColumnsConv.get();
15448     TheCall->setArg(2, ColumnsExpr);
15449   } else
15450     ColumnsExpr = nullptr;
15451 
15452   // If any any part of the result matrix type is still pending, just use
15453   // Context.DependentTy, until all parts are resolved.
15454   if ((RowsExpr && RowsExpr->isTypeDependent()) ||
15455       (ColumnsExpr && ColumnsExpr->isTypeDependent())) {
15456     TheCall->setType(Context.DependentTy);
15457     return CallResult;
15458   }
15459 
15460   // Check row and column dimenions.
15461   llvm::Optional<unsigned> MaybeRows;
15462   if (RowsExpr)
15463     MaybeRows = getAndVerifyMatrixDimension(RowsExpr, "row", *this);
15464 
15465   llvm::Optional<unsigned> MaybeColumns;
15466   if (ColumnsExpr)
15467     MaybeColumns = getAndVerifyMatrixDimension(ColumnsExpr, "column", *this);
15468 
15469   // Check stride argument.
15470   ExprResult StrideConv = ApplyArgumentConversions(StrideExpr);
15471   if (StrideConv.isInvalid())
15472     return ExprError();
15473   StrideExpr = StrideConv.get();
15474   TheCall->setArg(3, StrideExpr);
15475 
15476   llvm::APSInt Value(64);
15477   if (MaybeRows && StrideExpr->isIntegerConstantExpr(Value, Context)) {
15478     uint64_t Stride = Value.getZExtValue();
15479     if (Stride < *MaybeRows) {
15480       Diag(StrideExpr->getBeginLoc(),
15481            diag::err_builtin_matrix_stride_too_small);
15482       ArgError = true;
15483     }
15484   }
15485 
15486   if (ArgError || !MaybeRows || !MaybeColumns)
15487     return ExprError();
15488 
15489   TheCall->setType(
15490       Context.getConstantMatrixType(ElementTy, *MaybeRows, *MaybeColumns));
15491   return CallResult;
15492 }
15493 
15494 ExprResult Sema::SemaBuiltinMatrixColumnMajorStore(CallExpr *TheCall,
15495                                                    ExprResult CallResult) {
15496   if (checkArgCount(*this, TheCall, 3))
15497     return ExprError();
15498 
15499   unsigned PtrArgIdx = 1;
15500   Expr *MatrixExpr = TheCall->getArg(0);
15501   Expr *PtrExpr = TheCall->getArg(PtrArgIdx);
15502   Expr *StrideExpr = TheCall->getArg(2);
15503 
15504   bool ArgError = false;
15505 
15506   {
15507     ExprResult MatrixConv = DefaultLvalueConversion(MatrixExpr);
15508     if (MatrixConv.isInvalid())
15509       return MatrixConv;
15510     MatrixExpr = MatrixConv.get();
15511     TheCall->setArg(0, MatrixExpr);
15512   }
15513   if (MatrixExpr->isTypeDependent()) {
15514     TheCall->setType(Context.DependentTy);
15515     return TheCall;
15516   }
15517 
15518   auto *MatrixTy = MatrixExpr->getType()->getAs<ConstantMatrixType>();
15519   if (!MatrixTy) {
15520     Diag(MatrixExpr->getBeginLoc(), diag::err_builtin_matrix_arg) << 0;
15521     ArgError = true;
15522   }
15523 
15524   {
15525     ExprResult PtrConv = DefaultFunctionArrayLvalueConversion(PtrExpr);
15526     if (PtrConv.isInvalid())
15527       return PtrConv;
15528     PtrExpr = PtrConv.get();
15529     TheCall->setArg(1, PtrExpr);
15530     if (PtrExpr->isTypeDependent()) {
15531       TheCall->setType(Context.DependentTy);
15532       return TheCall;
15533     }
15534   }
15535 
15536   // Check pointer argument.
15537   auto *PtrTy = PtrExpr->getType()->getAs<PointerType>();
15538   if (!PtrTy) {
15539     Diag(PtrExpr->getBeginLoc(), diag::err_builtin_matrix_pointer_arg)
15540         << PtrArgIdx + 1;
15541     ArgError = true;
15542   } else {
15543     QualType ElementTy = PtrTy->getPointeeType();
15544     if (ElementTy.isConstQualified()) {
15545       Diag(PtrExpr->getBeginLoc(), diag::err_builtin_matrix_store_to_const);
15546       ArgError = true;
15547     }
15548     ElementTy = ElementTy.getUnqualifiedType().getCanonicalType();
15549     if (MatrixTy &&
15550         !Context.hasSameType(ElementTy, MatrixTy->getElementType())) {
15551       Diag(PtrExpr->getBeginLoc(),
15552            diag::err_builtin_matrix_pointer_arg_mismatch)
15553           << ElementTy << MatrixTy->getElementType();
15554       ArgError = true;
15555     }
15556   }
15557 
15558   // Apply default Lvalue conversions and convert the stride expression to
15559   // size_t.
15560   {
15561     ExprResult StrideConv = DefaultLvalueConversion(StrideExpr);
15562     if (StrideConv.isInvalid())
15563       return StrideConv;
15564 
15565     StrideConv = tryConvertExprToType(StrideConv.get(), Context.getSizeType());
15566     if (StrideConv.isInvalid())
15567       return StrideConv;
15568     StrideExpr = StrideConv.get();
15569     TheCall->setArg(2, StrideExpr);
15570   }
15571 
15572   // Check stride argument.
15573   llvm::APSInt Value(64);
15574   if (MatrixTy && StrideExpr->isIntegerConstantExpr(Value, Context)) {
15575     uint64_t Stride = Value.getZExtValue();
15576     if (Stride < MatrixTy->getNumRows()) {
15577       Diag(StrideExpr->getBeginLoc(),
15578            diag::err_builtin_matrix_stride_too_small);
15579       ArgError = true;
15580     }
15581   }
15582 
15583   if (ArgError)
15584     return ExprError();
15585 
15586   return CallResult;
15587 }
15588