1 //===--- SemaChecking.cpp - Extra Semantic Checking -----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements extra semantic analysis beyond what is enforced
11 //  by the C type system.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/Sema/SemaInternal.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/CharUnits.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/DeclObjC.h"
20 #include "clang/AST/EvaluatedExprVisitor.h"
21 #include "clang/AST/Expr.h"
22 #include "clang/AST/ExprCXX.h"
23 #include "clang/AST/ExprObjC.h"
24 #include "clang/AST/ExprOpenMP.h"
25 #include "clang/AST/StmtCXX.h"
26 #include "clang/AST/StmtObjC.h"
27 #include "clang/Analysis/Analyses/FormatString.h"
28 #include "clang/Basic/CharInfo.h"
29 #include "clang/Basic/TargetBuiltins.h"
30 #include "clang/Basic/TargetInfo.h"
31 #include "clang/Lex/Lexer.h" // TODO: Extract static functions to fix layering.
32 #include "clang/Sema/Initialization.h"
33 #include "clang/Sema/Lookup.h"
34 #include "clang/Sema/ScopeInfo.h"
35 #include "clang/Sema/Sema.h"
36 #include "llvm/ADT/STLExtras.h"
37 #include "llvm/ADT/SmallBitVector.h"
38 #include "llvm/ADT/SmallString.h"
39 #include "llvm/Support/ConvertUTF.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include <limits>
42 using namespace clang;
43 using namespace sema;
44 
45 SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL,
46                                                     unsigned ByteNo) const {
47   return SL->getLocationOfByte(ByteNo, getSourceManager(), LangOpts,
48                                Context.getTargetInfo());
49 }
50 
51 /// Checks that a call expression's argument count is the desired number.
52 /// This is useful when doing custom type-checking.  Returns true on error.
53 static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) {
54   unsigned argCount = call->getNumArgs();
55   if (argCount == desiredArgCount) return false;
56 
57   if (argCount < desiredArgCount)
58     return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args)
59         << 0 /*function call*/ << desiredArgCount << argCount
60         << call->getSourceRange();
61 
62   // Highlight all the excess arguments.
63   SourceRange range(call->getArg(desiredArgCount)->getLocStart(),
64                     call->getArg(argCount - 1)->getLocEnd());
65 
66   return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args)
67     << 0 /*function call*/ << desiredArgCount << argCount
68     << call->getArg(1)->getSourceRange();
69 }
70 
71 /// Check that the first argument to __builtin_annotation is an integer
72 /// and the second argument is a non-wide string literal.
73 static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) {
74   if (checkArgCount(S, TheCall, 2))
75     return true;
76 
77   // First argument should be an integer.
78   Expr *ValArg = TheCall->getArg(0);
79   QualType Ty = ValArg->getType();
80   if (!Ty->isIntegerType()) {
81     S.Diag(ValArg->getLocStart(), diag::err_builtin_annotation_first_arg)
82       << ValArg->getSourceRange();
83     return true;
84   }
85 
86   // Second argument should be a constant string.
87   Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts();
88   StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg);
89   if (!Literal || !Literal->isAscii()) {
90     S.Diag(StrArg->getLocStart(), diag::err_builtin_annotation_second_arg)
91       << StrArg->getSourceRange();
92     return true;
93   }
94 
95   TheCall->setType(Ty);
96   return false;
97 }
98 
99 /// Check that the argument to __builtin_addressof is a glvalue, and set the
100 /// result type to the corresponding pointer type.
101 static bool SemaBuiltinAddressof(Sema &S, CallExpr *TheCall) {
102   if (checkArgCount(S, TheCall, 1))
103     return true;
104 
105   ExprResult Arg(TheCall->getArg(0));
106   QualType ResultType = S.CheckAddressOfOperand(Arg, TheCall->getLocStart());
107   if (ResultType.isNull())
108     return true;
109 
110   TheCall->setArg(0, Arg.get());
111   TheCall->setType(ResultType);
112   return false;
113 }
114 
115 static void SemaBuiltinMemChkCall(Sema &S, FunctionDecl *FDecl,
116 		                  CallExpr *TheCall, unsigned SizeIdx,
117                                   unsigned DstSizeIdx) {
118   if (TheCall->getNumArgs() <= SizeIdx ||
119       TheCall->getNumArgs() <= DstSizeIdx)
120     return;
121 
122   const Expr *SizeArg = TheCall->getArg(SizeIdx);
123   const Expr *DstSizeArg = TheCall->getArg(DstSizeIdx);
124 
125   llvm::APSInt Size, DstSize;
126 
127   // find out if both sizes are known at compile time
128   if (!SizeArg->EvaluateAsInt(Size, S.Context) ||
129       !DstSizeArg->EvaluateAsInt(DstSize, S.Context))
130     return;
131 
132   if (Size.ule(DstSize))
133     return;
134 
135   // confirmed overflow so generate the diagnostic.
136   IdentifierInfo *FnName = FDecl->getIdentifier();
137   SourceLocation SL = TheCall->getLocStart();
138   SourceRange SR = TheCall->getSourceRange();
139 
140   S.Diag(SL, diag::warn_memcpy_chk_overflow) << SR << FnName;
141 }
142 
143 static bool SemaBuiltinCallWithStaticChain(Sema &S, CallExpr *BuiltinCall) {
144   if (checkArgCount(S, BuiltinCall, 2))
145     return true;
146 
147   SourceLocation BuiltinLoc = BuiltinCall->getLocStart();
148   Expr *Builtin = BuiltinCall->getCallee()->IgnoreImpCasts();
149   Expr *Call = BuiltinCall->getArg(0);
150   Expr *Chain = BuiltinCall->getArg(1);
151 
152   if (Call->getStmtClass() != Stmt::CallExprClass) {
153     S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_not_call)
154         << Call->getSourceRange();
155     return true;
156   }
157 
158   auto CE = cast<CallExpr>(Call);
159   if (CE->getCallee()->getType()->isBlockPointerType()) {
160     S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_block_call)
161         << Call->getSourceRange();
162     return true;
163   }
164 
165   const Decl *TargetDecl = CE->getCalleeDecl();
166   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl))
167     if (FD->getBuiltinID()) {
168       S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_builtin_call)
169           << Call->getSourceRange();
170       return true;
171     }
172 
173   if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens())) {
174     S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_pdtor_call)
175         << Call->getSourceRange();
176     return true;
177   }
178 
179   ExprResult ChainResult = S.UsualUnaryConversions(Chain);
180   if (ChainResult.isInvalid())
181     return true;
182   if (!ChainResult.get()->getType()->isPointerType()) {
183     S.Diag(BuiltinLoc, diag::err_second_argument_to_cwsc_not_pointer)
184         << Chain->getSourceRange();
185     return true;
186   }
187 
188   QualType ReturnTy = CE->getCallReturnType(S.Context);
189   QualType ArgTys[2] = { ReturnTy, ChainResult.get()->getType() };
190   QualType BuiltinTy = S.Context.getFunctionType(
191       ReturnTy, ArgTys, FunctionProtoType::ExtProtoInfo());
192   QualType BuiltinPtrTy = S.Context.getPointerType(BuiltinTy);
193 
194   Builtin =
195       S.ImpCastExprToType(Builtin, BuiltinPtrTy, CK_BuiltinFnToFnPtr).get();
196 
197   BuiltinCall->setType(CE->getType());
198   BuiltinCall->setValueKind(CE->getValueKind());
199   BuiltinCall->setObjectKind(CE->getObjectKind());
200   BuiltinCall->setCallee(Builtin);
201   BuiltinCall->setArg(1, ChainResult.get());
202 
203   return false;
204 }
205 
206 static bool SemaBuiltinSEHScopeCheck(Sema &SemaRef, CallExpr *TheCall,
207                                      Scope::ScopeFlags NeededScopeFlags,
208                                      unsigned DiagID) {
209   // Scopes aren't available during instantiation. Fortunately, builtin
210   // functions cannot be template args so they cannot be formed through template
211   // instantiation. Therefore checking once during the parse is sufficient.
212   if (!SemaRef.ActiveTemplateInstantiations.empty())
213     return false;
214 
215   Scope *S = SemaRef.getCurScope();
216   while (S && !S->isSEHExceptScope())
217     S = S->getParent();
218   if (!S || !(S->getFlags() & NeededScopeFlags)) {
219     auto *DRE = cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
220     SemaRef.Diag(TheCall->getExprLoc(), DiagID)
221         << DRE->getDecl()->getIdentifier();
222     return true;
223   }
224 
225   return false;
226 }
227 
228 ExprResult
229 Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
230                                CallExpr *TheCall) {
231   ExprResult TheCallResult(TheCall);
232 
233   // Find out if any arguments are required to be integer constant expressions.
234   unsigned ICEArguments = 0;
235   ASTContext::GetBuiltinTypeError Error;
236   Context.GetBuiltinType(BuiltinID, Error, &ICEArguments);
237   if (Error != ASTContext::GE_None)
238     ICEArguments = 0;  // Don't diagnose previously diagnosed errors.
239 
240   // If any arguments are required to be ICE's, check and diagnose.
241   for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) {
242     // Skip arguments not required to be ICE's.
243     if ((ICEArguments & (1 << ArgNo)) == 0) continue;
244 
245     llvm::APSInt Result;
246     if (SemaBuiltinConstantArg(TheCall, ArgNo, Result))
247       return true;
248     ICEArguments &= ~(1 << ArgNo);
249   }
250 
251   switch (BuiltinID) {
252   case Builtin::BI__builtin___CFStringMakeConstantString:
253     assert(TheCall->getNumArgs() == 1 &&
254            "Wrong # arguments to builtin CFStringMakeConstantString");
255     if (CheckObjCString(TheCall->getArg(0)))
256       return ExprError();
257     break;
258   case Builtin::BI__builtin_stdarg_start:
259   case Builtin::BI__builtin_va_start:
260     if (SemaBuiltinVAStart(TheCall))
261       return ExprError();
262     break;
263   case Builtin::BI__va_start: {
264     switch (Context.getTargetInfo().getTriple().getArch()) {
265     case llvm::Triple::arm:
266     case llvm::Triple::thumb:
267       if (SemaBuiltinVAStartARM(TheCall))
268         return ExprError();
269       break;
270     default:
271       if (SemaBuiltinVAStart(TheCall))
272         return ExprError();
273       break;
274     }
275     break;
276   }
277   case Builtin::BI__builtin_isgreater:
278   case Builtin::BI__builtin_isgreaterequal:
279   case Builtin::BI__builtin_isless:
280   case Builtin::BI__builtin_islessequal:
281   case Builtin::BI__builtin_islessgreater:
282   case Builtin::BI__builtin_isunordered:
283     if (SemaBuiltinUnorderedCompare(TheCall))
284       return ExprError();
285     break;
286   case Builtin::BI__builtin_fpclassify:
287     if (SemaBuiltinFPClassification(TheCall, 6))
288       return ExprError();
289     break;
290   case Builtin::BI__builtin_isfinite:
291   case Builtin::BI__builtin_isinf:
292   case Builtin::BI__builtin_isinf_sign:
293   case Builtin::BI__builtin_isnan:
294   case Builtin::BI__builtin_isnormal:
295     if (SemaBuiltinFPClassification(TheCall, 1))
296       return ExprError();
297     break;
298   case Builtin::BI__builtin_shufflevector:
299     return SemaBuiltinShuffleVector(TheCall);
300     // TheCall will be freed by the smart pointer here, but that's fine, since
301     // SemaBuiltinShuffleVector guts it, but then doesn't release it.
302   case Builtin::BI__builtin_prefetch:
303     if (SemaBuiltinPrefetch(TheCall))
304       return ExprError();
305     break;
306   case Builtin::BI__assume:
307   case Builtin::BI__builtin_assume:
308     if (SemaBuiltinAssume(TheCall))
309       return ExprError();
310     break;
311   case Builtin::BI__builtin_assume_aligned:
312     if (SemaBuiltinAssumeAligned(TheCall))
313       return ExprError();
314     break;
315   case Builtin::BI__builtin_object_size:
316     if (SemaBuiltinConstantArgRange(TheCall, 1, 0, 3))
317       return ExprError();
318     break;
319   case Builtin::BI__builtin_longjmp:
320     if (SemaBuiltinLongjmp(TheCall))
321       return ExprError();
322     break;
323   case Builtin::BI__builtin_setjmp:
324     if (SemaBuiltinSetjmp(TheCall))
325       return ExprError();
326     break;
327   case Builtin::BI_setjmp:
328   case Builtin::BI_setjmpex:
329     if (checkArgCount(*this, TheCall, 1))
330       return true;
331     break;
332 
333   case Builtin::BI__builtin_classify_type:
334     if (checkArgCount(*this, TheCall, 1)) return true;
335     TheCall->setType(Context.IntTy);
336     break;
337   case Builtin::BI__builtin_constant_p:
338     if (checkArgCount(*this, TheCall, 1)) return true;
339     TheCall->setType(Context.IntTy);
340     break;
341   case Builtin::BI__sync_fetch_and_add:
342   case Builtin::BI__sync_fetch_and_add_1:
343   case Builtin::BI__sync_fetch_and_add_2:
344   case Builtin::BI__sync_fetch_and_add_4:
345   case Builtin::BI__sync_fetch_and_add_8:
346   case Builtin::BI__sync_fetch_and_add_16:
347   case Builtin::BI__sync_fetch_and_sub:
348   case Builtin::BI__sync_fetch_and_sub_1:
349   case Builtin::BI__sync_fetch_and_sub_2:
350   case Builtin::BI__sync_fetch_and_sub_4:
351   case Builtin::BI__sync_fetch_and_sub_8:
352   case Builtin::BI__sync_fetch_and_sub_16:
353   case Builtin::BI__sync_fetch_and_or:
354   case Builtin::BI__sync_fetch_and_or_1:
355   case Builtin::BI__sync_fetch_and_or_2:
356   case Builtin::BI__sync_fetch_and_or_4:
357   case Builtin::BI__sync_fetch_and_or_8:
358   case Builtin::BI__sync_fetch_and_or_16:
359   case Builtin::BI__sync_fetch_and_and:
360   case Builtin::BI__sync_fetch_and_and_1:
361   case Builtin::BI__sync_fetch_and_and_2:
362   case Builtin::BI__sync_fetch_and_and_4:
363   case Builtin::BI__sync_fetch_and_and_8:
364   case Builtin::BI__sync_fetch_and_and_16:
365   case Builtin::BI__sync_fetch_and_xor:
366   case Builtin::BI__sync_fetch_and_xor_1:
367   case Builtin::BI__sync_fetch_and_xor_2:
368   case Builtin::BI__sync_fetch_and_xor_4:
369   case Builtin::BI__sync_fetch_and_xor_8:
370   case Builtin::BI__sync_fetch_and_xor_16:
371   case Builtin::BI__sync_fetch_and_nand:
372   case Builtin::BI__sync_fetch_and_nand_1:
373   case Builtin::BI__sync_fetch_and_nand_2:
374   case Builtin::BI__sync_fetch_and_nand_4:
375   case Builtin::BI__sync_fetch_and_nand_8:
376   case Builtin::BI__sync_fetch_and_nand_16:
377   case Builtin::BI__sync_add_and_fetch:
378   case Builtin::BI__sync_add_and_fetch_1:
379   case Builtin::BI__sync_add_and_fetch_2:
380   case Builtin::BI__sync_add_and_fetch_4:
381   case Builtin::BI__sync_add_and_fetch_8:
382   case Builtin::BI__sync_add_and_fetch_16:
383   case Builtin::BI__sync_sub_and_fetch:
384   case Builtin::BI__sync_sub_and_fetch_1:
385   case Builtin::BI__sync_sub_and_fetch_2:
386   case Builtin::BI__sync_sub_and_fetch_4:
387   case Builtin::BI__sync_sub_and_fetch_8:
388   case Builtin::BI__sync_sub_and_fetch_16:
389   case Builtin::BI__sync_and_and_fetch:
390   case Builtin::BI__sync_and_and_fetch_1:
391   case Builtin::BI__sync_and_and_fetch_2:
392   case Builtin::BI__sync_and_and_fetch_4:
393   case Builtin::BI__sync_and_and_fetch_8:
394   case Builtin::BI__sync_and_and_fetch_16:
395   case Builtin::BI__sync_or_and_fetch:
396   case Builtin::BI__sync_or_and_fetch_1:
397   case Builtin::BI__sync_or_and_fetch_2:
398   case Builtin::BI__sync_or_and_fetch_4:
399   case Builtin::BI__sync_or_and_fetch_8:
400   case Builtin::BI__sync_or_and_fetch_16:
401   case Builtin::BI__sync_xor_and_fetch:
402   case Builtin::BI__sync_xor_and_fetch_1:
403   case Builtin::BI__sync_xor_and_fetch_2:
404   case Builtin::BI__sync_xor_and_fetch_4:
405   case Builtin::BI__sync_xor_and_fetch_8:
406   case Builtin::BI__sync_xor_and_fetch_16:
407   case Builtin::BI__sync_nand_and_fetch:
408   case Builtin::BI__sync_nand_and_fetch_1:
409   case Builtin::BI__sync_nand_and_fetch_2:
410   case Builtin::BI__sync_nand_and_fetch_4:
411   case Builtin::BI__sync_nand_and_fetch_8:
412   case Builtin::BI__sync_nand_and_fetch_16:
413   case Builtin::BI__sync_val_compare_and_swap:
414   case Builtin::BI__sync_val_compare_and_swap_1:
415   case Builtin::BI__sync_val_compare_and_swap_2:
416   case Builtin::BI__sync_val_compare_and_swap_4:
417   case Builtin::BI__sync_val_compare_and_swap_8:
418   case Builtin::BI__sync_val_compare_and_swap_16:
419   case Builtin::BI__sync_bool_compare_and_swap:
420   case Builtin::BI__sync_bool_compare_and_swap_1:
421   case Builtin::BI__sync_bool_compare_and_swap_2:
422   case Builtin::BI__sync_bool_compare_and_swap_4:
423   case Builtin::BI__sync_bool_compare_and_swap_8:
424   case Builtin::BI__sync_bool_compare_and_swap_16:
425   case Builtin::BI__sync_lock_test_and_set:
426   case Builtin::BI__sync_lock_test_and_set_1:
427   case Builtin::BI__sync_lock_test_and_set_2:
428   case Builtin::BI__sync_lock_test_and_set_4:
429   case Builtin::BI__sync_lock_test_and_set_8:
430   case Builtin::BI__sync_lock_test_and_set_16:
431   case Builtin::BI__sync_lock_release:
432   case Builtin::BI__sync_lock_release_1:
433   case Builtin::BI__sync_lock_release_2:
434   case Builtin::BI__sync_lock_release_4:
435   case Builtin::BI__sync_lock_release_8:
436   case Builtin::BI__sync_lock_release_16:
437   case Builtin::BI__sync_swap:
438   case Builtin::BI__sync_swap_1:
439   case Builtin::BI__sync_swap_2:
440   case Builtin::BI__sync_swap_4:
441   case Builtin::BI__sync_swap_8:
442   case Builtin::BI__sync_swap_16:
443     return SemaBuiltinAtomicOverloaded(TheCallResult);
444 #define BUILTIN(ID, TYPE, ATTRS)
445 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
446   case Builtin::BI##ID: \
447     return SemaAtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID);
448 #include "clang/Basic/Builtins.def"
449   case Builtin::BI__builtin_annotation:
450     if (SemaBuiltinAnnotation(*this, TheCall))
451       return ExprError();
452     break;
453   case Builtin::BI__builtin_addressof:
454     if (SemaBuiltinAddressof(*this, TheCall))
455       return ExprError();
456     break;
457   case Builtin::BI__builtin_operator_new:
458   case Builtin::BI__builtin_operator_delete:
459     if (!getLangOpts().CPlusPlus) {
460       Diag(TheCall->getExprLoc(), diag::err_builtin_requires_language)
461         << (BuiltinID == Builtin::BI__builtin_operator_new
462                 ? "__builtin_operator_new"
463                 : "__builtin_operator_delete")
464         << "C++";
465       return ExprError();
466     }
467     // CodeGen assumes it can find the global new and delete to call,
468     // so ensure that they are declared.
469     DeclareGlobalNewDelete();
470     break;
471 
472   // check secure string manipulation functions where overflows
473   // are detectable at compile time
474   case Builtin::BI__builtin___memcpy_chk:
475   case Builtin::BI__builtin___memmove_chk:
476   case Builtin::BI__builtin___memset_chk:
477   case Builtin::BI__builtin___strlcat_chk:
478   case Builtin::BI__builtin___strlcpy_chk:
479   case Builtin::BI__builtin___strncat_chk:
480   case Builtin::BI__builtin___strncpy_chk:
481   case Builtin::BI__builtin___stpncpy_chk:
482     SemaBuiltinMemChkCall(*this, FDecl, TheCall, 2, 3);
483     break;
484   case Builtin::BI__builtin___memccpy_chk:
485     SemaBuiltinMemChkCall(*this, FDecl, TheCall, 3, 4);
486     break;
487   case Builtin::BI__builtin___snprintf_chk:
488   case Builtin::BI__builtin___vsnprintf_chk:
489     SemaBuiltinMemChkCall(*this, FDecl, TheCall, 1, 3);
490     break;
491 
492   case Builtin::BI__builtin_call_with_static_chain:
493     if (SemaBuiltinCallWithStaticChain(*this, TheCall))
494       return ExprError();
495     break;
496 
497   case Builtin::BI__exception_code:
498   case Builtin::BI_exception_code: {
499     if (SemaBuiltinSEHScopeCheck(*this, TheCall, Scope::SEHExceptScope,
500                                  diag::err_seh___except_block))
501       return ExprError();
502     break;
503   }
504   case Builtin::BI__exception_info:
505   case Builtin::BI_exception_info: {
506     if (SemaBuiltinSEHScopeCheck(*this, TheCall, Scope::SEHFilterScope,
507                                  diag::err_seh___except_filter))
508       return ExprError();
509     break;
510   }
511 
512   case Builtin::BI__GetExceptionInfo:
513     if (checkArgCount(*this, TheCall, 1))
514       return ExprError();
515 
516     if (CheckCXXThrowOperand(
517             TheCall->getLocStart(),
518             Context.getExceptionObjectType(FDecl->getParamDecl(0)->getType()),
519             TheCall))
520       return ExprError();
521 
522     TheCall->setType(Context.VoidPtrTy);
523     break;
524 
525   }
526 
527   // Since the target specific builtins for each arch overlap, only check those
528   // of the arch we are compiling for.
529   if (BuiltinID >= Builtin::FirstTSBuiltin) {
530     switch (Context.getTargetInfo().getTriple().getArch()) {
531       case llvm::Triple::arm:
532       case llvm::Triple::armeb:
533       case llvm::Triple::thumb:
534       case llvm::Triple::thumbeb:
535         if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall))
536           return ExprError();
537         break;
538       case llvm::Triple::aarch64:
539       case llvm::Triple::aarch64_be:
540         if (CheckAArch64BuiltinFunctionCall(BuiltinID, TheCall))
541           return ExprError();
542         break;
543       case llvm::Triple::mips:
544       case llvm::Triple::mipsel:
545       case llvm::Triple::mips64:
546       case llvm::Triple::mips64el:
547         if (CheckMipsBuiltinFunctionCall(BuiltinID, TheCall))
548           return ExprError();
549         break;
550       case llvm::Triple::systemz:
551         if (CheckSystemZBuiltinFunctionCall(BuiltinID, TheCall))
552           return ExprError();
553         break;
554       case llvm::Triple::x86:
555       case llvm::Triple::x86_64:
556         if (CheckX86BuiltinFunctionCall(BuiltinID, TheCall))
557           return ExprError();
558         break;
559       case llvm::Triple::ppc:
560       case llvm::Triple::ppc64:
561       case llvm::Triple::ppc64le:
562         if (CheckPPCBuiltinFunctionCall(BuiltinID, TheCall))
563           return ExprError();
564         break;
565       default:
566         break;
567     }
568   }
569 
570   return TheCallResult;
571 }
572 
573 // Get the valid immediate range for the specified NEON type code.
574 static unsigned RFT(unsigned t, bool shift = false, bool ForceQuad = false) {
575   NeonTypeFlags Type(t);
576   int IsQuad = ForceQuad ? true : Type.isQuad();
577   switch (Type.getEltType()) {
578   case NeonTypeFlags::Int8:
579   case NeonTypeFlags::Poly8:
580     return shift ? 7 : (8 << IsQuad) - 1;
581   case NeonTypeFlags::Int16:
582   case NeonTypeFlags::Poly16:
583     return shift ? 15 : (4 << IsQuad) - 1;
584   case NeonTypeFlags::Int32:
585     return shift ? 31 : (2 << IsQuad) - 1;
586   case NeonTypeFlags::Int64:
587   case NeonTypeFlags::Poly64:
588     return shift ? 63 : (1 << IsQuad) - 1;
589   case NeonTypeFlags::Poly128:
590     return shift ? 127 : (1 << IsQuad) - 1;
591   case NeonTypeFlags::Float16:
592     assert(!shift && "cannot shift float types!");
593     return (4 << IsQuad) - 1;
594   case NeonTypeFlags::Float32:
595     assert(!shift && "cannot shift float types!");
596     return (2 << IsQuad) - 1;
597   case NeonTypeFlags::Float64:
598     assert(!shift && "cannot shift float types!");
599     return (1 << IsQuad) - 1;
600   }
601   llvm_unreachable("Invalid NeonTypeFlag!");
602 }
603 
604 /// getNeonEltType - Return the QualType corresponding to the elements of
605 /// the vector type specified by the NeonTypeFlags.  This is used to check
606 /// the pointer arguments for Neon load/store intrinsics.
607 static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context,
608                                bool IsPolyUnsigned, bool IsInt64Long) {
609   switch (Flags.getEltType()) {
610   case NeonTypeFlags::Int8:
611     return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy;
612   case NeonTypeFlags::Int16:
613     return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy;
614   case NeonTypeFlags::Int32:
615     return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy;
616   case NeonTypeFlags::Int64:
617     if (IsInt64Long)
618       return Flags.isUnsigned() ? Context.UnsignedLongTy : Context.LongTy;
619     else
620       return Flags.isUnsigned() ? Context.UnsignedLongLongTy
621                                 : Context.LongLongTy;
622   case NeonTypeFlags::Poly8:
623     return IsPolyUnsigned ? Context.UnsignedCharTy : Context.SignedCharTy;
624   case NeonTypeFlags::Poly16:
625     return IsPolyUnsigned ? Context.UnsignedShortTy : Context.ShortTy;
626   case NeonTypeFlags::Poly64:
627     if (IsInt64Long)
628       return Context.UnsignedLongTy;
629     else
630       return Context.UnsignedLongLongTy;
631   case NeonTypeFlags::Poly128:
632     break;
633   case NeonTypeFlags::Float16:
634     return Context.HalfTy;
635   case NeonTypeFlags::Float32:
636     return Context.FloatTy;
637   case NeonTypeFlags::Float64:
638     return Context.DoubleTy;
639   }
640   llvm_unreachable("Invalid NeonTypeFlag!");
641 }
642 
643 bool Sema::CheckNeonBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
644   llvm::APSInt Result;
645   uint64_t mask = 0;
646   unsigned TV = 0;
647   int PtrArgNum = -1;
648   bool HasConstPtr = false;
649   switch (BuiltinID) {
650 #define GET_NEON_OVERLOAD_CHECK
651 #include "clang/Basic/arm_neon.inc"
652 #undef GET_NEON_OVERLOAD_CHECK
653   }
654 
655   // For NEON intrinsics which are overloaded on vector element type, validate
656   // the immediate which specifies which variant to emit.
657   unsigned ImmArg = TheCall->getNumArgs()-1;
658   if (mask) {
659     if (SemaBuiltinConstantArg(TheCall, ImmArg, Result))
660       return true;
661 
662     TV = Result.getLimitedValue(64);
663     if ((TV > 63) || (mask & (1ULL << TV)) == 0)
664       return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code)
665         << TheCall->getArg(ImmArg)->getSourceRange();
666   }
667 
668   if (PtrArgNum >= 0) {
669     // Check that pointer arguments have the specified type.
670     Expr *Arg = TheCall->getArg(PtrArgNum);
671     if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg))
672       Arg = ICE->getSubExpr();
673     ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg);
674     QualType RHSTy = RHS.get()->getType();
675 
676     llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch();
677     bool IsPolyUnsigned = Arch == llvm::Triple::aarch64;
678     bool IsInt64Long =
679         Context.getTargetInfo().getInt64Type() == TargetInfo::SignedLong;
680     QualType EltTy =
681         getNeonEltType(NeonTypeFlags(TV), Context, IsPolyUnsigned, IsInt64Long);
682     if (HasConstPtr)
683       EltTy = EltTy.withConst();
684     QualType LHSTy = Context.getPointerType(EltTy);
685     AssignConvertType ConvTy;
686     ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS);
687     if (RHS.isInvalid())
688       return true;
689     if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), LHSTy, RHSTy,
690                                  RHS.get(), AA_Assigning))
691       return true;
692   }
693 
694   // For NEON intrinsics which take an immediate value as part of the
695   // instruction, range check them here.
696   unsigned i = 0, l = 0, u = 0;
697   switch (BuiltinID) {
698   default:
699     return false;
700 #define GET_NEON_IMMEDIATE_CHECK
701 #include "clang/Basic/arm_neon.inc"
702 #undef GET_NEON_IMMEDIATE_CHECK
703   }
704 
705   return SemaBuiltinConstantArgRange(TheCall, i, l, u + l);
706 }
707 
708 bool Sema::CheckARMBuiltinExclusiveCall(unsigned BuiltinID, CallExpr *TheCall,
709                                         unsigned MaxWidth) {
710   assert((BuiltinID == ARM::BI__builtin_arm_ldrex ||
711           BuiltinID == ARM::BI__builtin_arm_ldaex ||
712           BuiltinID == ARM::BI__builtin_arm_strex ||
713           BuiltinID == ARM::BI__builtin_arm_stlex ||
714           BuiltinID == AArch64::BI__builtin_arm_ldrex ||
715           BuiltinID == AArch64::BI__builtin_arm_ldaex ||
716           BuiltinID == AArch64::BI__builtin_arm_strex ||
717           BuiltinID == AArch64::BI__builtin_arm_stlex) &&
718          "unexpected ARM builtin");
719   bool IsLdrex = BuiltinID == ARM::BI__builtin_arm_ldrex ||
720                  BuiltinID == ARM::BI__builtin_arm_ldaex ||
721                  BuiltinID == AArch64::BI__builtin_arm_ldrex ||
722                  BuiltinID == AArch64::BI__builtin_arm_ldaex;
723 
724   DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
725 
726   // Ensure that we have the proper number of arguments.
727   if (checkArgCount(*this, TheCall, IsLdrex ? 1 : 2))
728     return true;
729 
730   // Inspect the pointer argument of the atomic builtin.  This should always be
731   // a pointer type, whose element is an integral scalar or pointer type.
732   // Because it is a pointer type, we don't have to worry about any implicit
733   // casts here.
734   Expr *PointerArg = TheCall->getArg(IsLdrex ? 0 : 1);
735   ExprResult PointerArgRes = DefaultFunctionArrayLvalueConversion(PointerArg);
736   if (PointerArgRes.isInvalid())
737     return true;
738   PointerArg = PointerArgRes.get();
739 
740   const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>();
741   if (!pointerType) {
742     Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
743       << PointerArg->getType() << PointerArg->getSourceRange();
744     return true;
745   }
746 
747   // ldrex takes a "const volatile T*" and strex takes a "volatile T*". Our next
748   // task is to insert the appropriate casts into the AST. First work out just
749   // what the appropriate type is.
750   QualType ValType = pointerType->getPointeeType();
751   QualType AddrType = ValType.getUnqualifiedType().withVolatile();
752   if (IsLdrex)
753     AddrType.addConst();
754 
755   // Issue a warning if the cast is dodgy.
756   CastKind CastNeeded = CK_NoOp;
757   if (!AddrType.isAtLeastAsQualifiedAs(ValType)) {
758     CastNeeded = CK_BitCast;
759     Diag(DRE->getLocStart(), diag::ext_typecheck_convert_discards_qualifiers)
760       << PointerArg->getType()
761       << Context.getPointerType(AddrType)
762       << AA_Passing << PointerArg->getSourceRange();
763   }
764 
765   // Finally, do the cast and replace the argument with the corrected version.
766   AddrType = Context.getPointerType(AddrType);
767   PointerArgRes = ImpCastExprToType(PointerArg, AddrType, CastNeeded);
768   if (PointerArgRes.isInvalid())
769     return true;
770   PointerArg = PointerArgRes.get();
771 
772   TheCall->setArg(IsLdrex ? 0 : 1, PointerArg);
773 
774   // In general, we allow ints, floats and pointers to be loaded and stored.
775   if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
776       !ValType->isBlockPointerType() && !ValType->isFloatingType()) {
777     Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intfltptr)
778       << PointerArg->getType() << PointerArg->getSourceRange();
779     return true;
780   }
781 
782   // But ARM doesn't have instructions to deal with 128-bit versions.
783   if (Context.getTypeSize(ValType) > MaxWidth) {
784     assert(MaxWidth == 64 && "Diagnostic unexpectedly inaccurate");
785     Diag(DRE->getLocStart(), diag::err_atomic_exclusive_builtin_pointer_size)
786       << PointerArg->getType() << PointerArg->getSourceRange();
787     return true;
788   }
789 
790   switch (ValType.getObjCLifetime()) {
791   case Qualifiers::OCL_None:
792   case Qualifiers::OCL_ExplicitNone:
793     // okay
794     break;
795 
796   case Qualifiers::OCL_Weak:
797   case Qualifiers::OCL_Strong:
798   case Qualifiers::OCL_Autoreleasing:
799     Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
800       << ValType << PointerArg->getSourceRange();
801     return true;
802   }
803 
804 
805   if (IsLdrex) {
806     TheCall->setType(ValType);
807     return false;
808   }
809 
810   // Initialize the argument to be stored.
811   ExprResult ValArg = TheCall->getArg(0);
812   InitializedEntity Entity = InitializedEntity::InitializeParameter(
813       Context, ValType, /*consume*/ false);
814   ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg);
815   if (ValArg.isInvalid())
816     return true;
817   TheCall->setArg(0, ValArg.get());
818 
819   // __builtin_arm_strex always returns an int. It's marked as such in the .def,
820   // but the custom checker bypasses all default analysis.
821   TheCall->setType(Context.IntTy);
822   return false;
823 }
824 
825 bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
826   llvm::APSInt Result;
827 
828   if (BuiltinID == ARM::BI__builtin_arm_ldrex ||
829       BuiltinID == ARM::BI__builtin_arm_ldaex ||
830       BuiltinID == ARM::BI__builtin_arm_strex ||
831       BuiltinID == ARM::BI__builtin_arm_stlex) {
832     return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 64);
833   }
834 
835   if (BuiltinID == ARM::BI__builtin_arm_prefetch) {
836     return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) ||
837       SemaBuiltinConstantArgRange(TheCall, 2, 0, 1);
838   }
839 
840   if (BuiltinID == ARM::BI__builtin_arm_rsr64 ||
841       BuiltinID == ARM::BI__builtin_arm_wsr64)
842     return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 3, false);
843 
844   if (BuiltinID == ARM::BI__builtin_arm_rsr ||
845       BuiltinID == ARM::BI__builtin_arm_rsrp ||
846       BuiltinID == ARM::BI__builtin_arm_wsr ||
847       BuiltinID == ARM::BI__builtin_arm_wsrp)
848     return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true);
849 
850   if (CheckNeonBuiltinFunctionCall(BuiltinID, TheCall))
851     return true;
852 
853   // For intrinsics which take an immediate value as part of the instruction,
854   // range check them here.
855   unsigned i = 0, l = 0, u = 0;
856   switch (BuiltinID) {
857   default: return false;
858   case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break;
859   case ARM::BI__builtin_arm_usat: i = 1; u = 31; break;
860   case ARM::BI__builtin_arm_vcvtr_f:
861   case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break;
862   case ARM::BI__builtin_arm_dmb:
863   case ARM::BI__builtin_arm_dsb:
864   case ARM::BI__builtin_arm_isb:
865   case ARM::BI__builtin_arm_dbg: l = 0; u = 15; break;
866   }
867 
868   // FIXME: VFP Intrinsics should error if VFP not present.
869   return SemaBuiltinConstantArgRange(TheCall, i, l, u + l);
870 }
871 
872 bool Sema::CheckAArch64BuiltinFunctionCall(unsigned BuiltinID,
873                                          CallExpr *TheCall) {
874   llvm::APSInt Result;
875 
876   if (BuiltinID == AArch64::BI__builtin_arm_ldrex ||
877       BuiltinID == AArch64::BI__builtin_arm_ldaex ||
878       BuiltinID == AArch64::BI__builtin_arm_strex ||
879       BuiltinID == AArch64::BI__builtin_arm_stlex) {
880     return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 128);
881   }
882 
883   if (BuiltinID == AArch64::BI__builtin_arm_prefetch) {
884     return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) ||
885       SemaBuiltinConstantArgRange(TheCall, 2, 0, 2) ||
886       SemaBuiltinConstantArgRange(TheCall, 3, 0, 1) ||
887       SemaBuiltinConstantArgRange(TheCall, 4, 0, 1);
888   }
889 
890   if (BuiltinID == AArch64::BI__builtin_arm_rsr64 ||
891       BuiltinID == AArch64::BI__builtin_arm_wsr64)
892     return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, false);
893 
894   if (BuiltinID == AArch64::BI__builtin_arm_rsr ||
895       BuiltinID == AArch64::BI__builtin_arm_rsrp ||
896       BuiltinID == AArch64::BI__builtin_arm_wsr ||
897       BuiltinID == AArch64::BI__builtin_arm_wsrp)
898     return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true);
899 
900   if (CheckNeonBuiltinFunctionCall(BuiltinID, TheCall))
901     return true;
902 
903   // For intrinsics which take an immediate value as part of the instruction,
904   // range check them here.
905   unsigned i = 0, l = 0, u = 0;
906   switch (BuiltinID) {
907   default: return false;
908   case AArch64::BI__builtin_arm_dmb:
909   case AArch64::BI__builtin_arm_dsb:
910   case AArch64::BI__builtin_arm_isb: l = 0; u = 15; break;
911   }
912 
913   return SemaBuiltinConstantArgRange(TheCall, i, l, u + l);
914 }
915 
916 bool Sema::CheckMipsBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
917   unsigned i = 0, l = 0, u = 0;
918   switch (BuiltinID) {
919   default: return false;
920   case Mips::BI__builtin_mips_wrdsp: i = 1; l = 0; u = 63; break;
921   case Mips::BI__builtin_mips_rddsp: i = 0; l = 0; u = 63; break;
922   case Mips::BI__builtin_mips_append: i = 2; l = 0; u = 31; break;
923   case Mips::BI__builtin_mips_balign: i = 2; l = 0; u = 3; break;
924   case Mips::BI__builtin_mips_precr_sra_ph_w: i = 2; l = 0; u = 31; break;
925   case Mips::BI__builtin_mips_precr_sra_r_ph_w: i = 2; l = 0; u = 31; break;
926   case Mips::BI__builtin_mips_prepend: i = 2; l = 0; u = 31; break;
927   }
928 
929   return SemaBuiltinConstantArgRange(TheCall, i, l, u);
930 }
931 
932 bool Sema::CheckPPCBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
933   unsigned i = 0, l = 0, u = 0;
934   bool Is64BitBltin = BuiltinID == PPC::BI__builtin_divde ||
935                       BuiltinID == PPC::BI__builtin_divdeu ||
936                       BuiltinID == PPC::BI__builtin_bpermd;
937   bool IsTarget64Bit = Context.getTargetInfo()
938                               .getTypeWidth(Context
939                                             .getTargetInfo()
940                                             .getIntPtrType()) == 64;
941   bool IsBltinExtDiv = BuiltinID == PPC::BI__builtin_divwe ||
942                        BuiltinID == PPC::BI__builtin_divweu ||
943                        BuiltinID == PPC::BI__builtin_divde ||
944                        BuiltinID == PPC::BI__builtin_divdeu;
945 
946   if (Is64BitBltin && !IsTarget64Bit)
947       return Diag(TheCall->getLocStart(), diag::err_64_bit_builtin_32_bit_tgt)
948              << TheCall->getSourceRange();
949 
950   if ((IsBltinExtDiv && !Context.getTargetInfo().hasFeature("extdiv")) ||
951       (BuiltinID == PPC::BI__builtin_bpermd &&
952        !Context.getTargetInfo().hasFeature("bpermd")))
953     return Diag(TheCall->getLocStart(), diag::err_ppc_builtin_only_on_pwr7)
954            << TheCall->getSourceRange();
955 
956   switch (BuiltinID) {
957   default: return false;
958   case PPC::BI__builtin_altivec_crypto_vshasigmaw:
959   case PPC::BI__builtin_altivec_crypto_vshasigmad:
960     return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) ||
961            SemaBuiltinConstantArgRange(TheCall, 2, 0, 15);
962   case PPC::BI__builtin_tbegin:
963   case PPC::BI__builtin_tend: i = 0; l = 0; u = 1; break;
964   case PPC::BI__builtin_tsr: i = 0; l = 0; u = 7; break;
965   case PPC::BI__builtin_tabortwc:
966   case PPC::BI__builtin_tabortdc: i = 0; l = 0; u = 31; break;
967   case PPC::BI__builtin_tabortwci:
968   case PPC::BI__builtin_tabortdci:
969     return SemaBuiltinConstantArgRange(TheCall, 0, 0, 31) ||
970            SemaBuiltinConstantArgRange(TheCall, 2, 0, 31);
971   }
972   return SemaBuiltinConstantArgRange(TheCall, i, l, u);
973 }
974 
975 bool Sema::CheckSystemZBuiltinFunctionCall(unsigned BuiltinID,
976                                            CallExpr *TheCall) {
977   if (BuiltinID == SystemZ::BI__builtin_tabort) {
978     Expr *Arg = TheCall->getArg(0);
979     llvm::APSInt AbortCode(32);
980     if (Arg->isIntegerConstantExpr(AbortCode, Context) &&
981         AbortCode.getSExtValue() >= 0 && AbortCode.getSExtValue() < 256)
982       return Diag(Arg->getLocStart(), diag::err_systemz_invalid_tabort_code)
983              << Arg->getSourceRange();
984   }
985 
986   // For intrinsics which take an immediate value as part of the instruction,
987   // range check them here.
988   unsigned i = 0, l = 0, u = 0;
989   switch (BuiltinID) {
990   default: return false;
991   case SystemZ::BI__builtin_s390_lcbb: i = 1; l = 0; u = 15; break;
992   case SystemZ::BI__builtin_s390_verimb:
993   case SystemZ::BI__builtin_s390_verimh:
994   case SystemZ::BI__builtin_s390_verimf:
995   case SystemZ::BI__builtin_s390_verimg: i = 3; l = 0; u = 255; break;
996   case SystemZ::BI__builtin_s390_vfaeb:
997   case SystemZ::BI__builtin_s390_vfaeh:
998   case SystemZ::BI__builtin_s390_vfaef:
999   case SystemZ::BI__builtin_s390_vfaebs:
1000   case SystemZ::BI__builtin_s390_vfaehs:
1001   case SystemZ::BI__builtin_s390_vfaefs:
1002   case SystemZ::BI__builtin_s390_vfaezb:
1003   case SystemZ::BI__builtin_s390_vfaezh:
1004   case SystemZ::BI__builtin_s390_vfaezf:
1005   case SystemZ::BI__builtin_s390_vfaezbs:
1006   case SystemZ::BI__builtin_s390_vfaezhs:
1007   case SystemZ::BI__builtin_s390_vfaezfs: i = 2; l = 0; u = 15; break;
1008   case SystemZ::BI__builtin_s390_vfidb:
1009     return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15) ||
1010            SemaBuiltinConstantArgRange(TheCall, 2, 0, 15);
1011   case SystemZ::BI__builtin_s390_vftcidb: i = 1; l = 0; u = 4095; break;
1012   case SystemZ::BI__builtin_s390_vlbb: i = 1; l = 0; u = 15; break;
1013   case SystemZ::BI__builtin_s390_vpdi: i = 2; l = 0; u = 15; break;
1014   case SystemZ::BI__builtin_s390_vsldb: i = 2; l = 0; u = 15; break;
1015   case SystemZ::BI__builtin_s390_vstrcb:
1016   case SystemZ::BI__builtin_s390_vstrch:
1017   case SystemZ::BI__builtin_s390_vstrcf:
1018   case SystemZ::BI__builtin_s390_vstrczb:
1019   case SystemZ::BI__builtin_s390_vstrczh:
1020   case SystemZ::BI__builtin_s390_vstrczf:
1021   case SystemZ::BI__builtin_s390_vstrcbs:
1022   case SystemZ::BI__builtin_s390_vstrchs:
1023   case SystemZ::BI__builtin_s390_vstrcfs:
1024   case SystemZ::BI__builtin_s390_vstrczbs:
1025   case SystemZ::BI__builtin_s390_vstrczhs:
1026   case SystemZ::BI__builtin_s390_vstrczfs: i = 3; l = 0; u = 15; break;
1027   }
1028   return SemaBuiltinConstantArgRange(TheCall, i, l, u);
1029 }
1030 
1031 bool Sema::CheckX86BuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
1032   unsigned i = 0, l = 0, u = 0;
1033   switch (BuiltinID) {
1034   default: return false;
1035   case X86::BI__builtin_cpu_supports:
1036     return SemaBuiltinCpuSupports(TheCall);
1037   case X86::BI_mm_prefetch: i = 1; l = 0; u = 3; break;
1038   case X86::BI__builtin_ia32_sha1rnds4: i = 2, l = 0; u = 3; break;
1039   case X86::BI__builtin_ia32_vpermil2pd:
1040   case X86::BI__builtin_ia32_vpermil2pd256:
1041   case X86::BI__builtin_ia32_vpermil2ps:
1042   case X86::BI__builtin_ia32_vpermil2ps256: i = 3, l = 0; u = 3; break;
1043   case X86::BI__builtin_ia32_cmpb128_mask:
1044   case X86::BI__builtin_ia32_cmpw128_mask:
1045   case X86::BI__builtin_ia32_cmpd128_mask:
1046   case X86::BI__builtin_ia32_cmpq128_mask:
1047   case X86::BI__builtin_ia32_cmpb256_mask:
1048   case X86::BI__builtin_ia32_cmpw256_mask:
1049   case X86::BI__builtin_ia32_cmpd256_mask:
1050   case X86::BI__builtin_ia32_cmpq256_mask:
1051   case X86::BI__builtin_ia32_cmpb512_mask:
1052   case X86::BI__builtin_ia32_cmpw512_mask:
1053   case X86::BI__builtin_ia32_cmpd512_mask:
1054   case X86::BI__builtin_ia32_cmpq512_mask:
1055   case X86::BI__builtin_ia32_ucmpb128_mask:
1056   case X86::BI__builtin_ia32_ucmpw128_mask:
1057   case X86::BI__builtin_ia32_ucmpd128_mask:
1058   case X86::BI__builtin_ia32_ucmpq128_mask:
1059   case X86::BI__builtin_ia32_ucmpb256_mask:
1060   case X86::BI__builtin_ia32_ucmpw256_mask:
1061   case X86::BI__builtin_ia32_ucmpd256_mask:
1062   case X86::BI__builtin_ia32_ucmpq256_mask:
1063   case X86::BI__builtin_ia32_ucmpb512_mask:
1064   case X86::BI__builtin_ia32_ucmpw512_mask:
1065   case X86::BI__builtin_ia32_ucmpd512_mask:
1066   case X86::BI__builtin_ia32_ucmpq512_mask: i = 2; l = 0; u = 7; break;
1067   case X86::BI__builtin_ia32_roundps:
1068   case X86::BI__builtin_ia32_roundpd:
1069   case X86::BI__builtin_ia32_roundps256:
1070   case X86::BI__builtin_ia32_roundpd256: i = 1, l = 0; u = 15; break;
1071   case X86::BI__builtin_ia32_roundss:
1072   case X86::BI__builtin_ia32_roundsd: i = 2, l = 0; u = 15; break;
1073   case X86::BI__builtin_ia32_cmpps:
1074   case X86::BI__builtin_ia32_cmpss:
1075   case X86::BI__builtin_ia32_cmppd:
1076   case X86::BI__builtin_ia32_cmpsd:
1077   case X86::BI__builtin_ia32_cmpps256:
1078   case X86::BI__builtin_ia32_cmppd256:
1079   case X86::BI__builtin_ia32_cmpps512_mask:
1080   case X86::BI__builtin_ia32_cmppd512_mask: i = 2; l = 0; u = 31; break;
1081   case X86::BI__builtin_ia32_vpcomub:
1082   case X86::BI__builtin_ia32_vpcomuw:
1083   case X86::BI__builtin_ia32_vpcomud:
1084   case X86::BI__builtin_ia32_vpcomuq:
1085   case X86::BI__builtin_ia32_vpcomb:
1086   case X86::BI__builtin_ia32_vpcomw:
1087   case X86::BI__builtin_ia32_vpcomd:
1088   case X86::BI__builtin_ia32_vpcomq: i = 2; l = 0; u = 7; break;
1089   }
1090   return SemaBuiltinConstantArgRange(TheCall, i, l, u);
1091 }
1092 
1093 /// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo
1094 /// parameter with the FormatAttr's correct format_idx and firstDataArg.
1095 /// Returns true when the format fits the function and the FormatStringInfo has
1096 /// been populated.
1097 bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember,
1098                                FormatStringInfo *FSI) {
1099   FSI->HasVAListArg = Format->getFirstArg() == 0;
1100   FSI->FormatIdx = Format->getFormatIdx() - 1;
1101   FSI->FirstDataArg = FSI->HasVAListArg ? 0 : Format->getFirstArg() - 1;
1102 
1103   // The way the format attribute works in GCC, the implicit this argument
1104   // of member functions is counted. However, it doesn't appear in our own
1105   // lists, so decrement format_idx in that case.
1106   if (IsCXXMember) {
1107     if(FSI->FormatIdx == 0)
1108       return false;
1109     --FSI->FormatIdx;
1110     if (FSI->FirstDataArg != 0)
1111       --FSI->FirstDataArg;
1112   }
1113   return true;
1114 }
1115 
1116 /// Checks if a the given expression evaluates to null.
1117 ///
1118 /// \brief Returns true if the value evaluates to null.
1119 static bool CheckNonNullExpr(Sema &S,
1120                              const Expr *Expr) {
1121   // If the expression has non-null type, it doesn't evaluate to null.
1122   if (auto nullability
1123         = Expr->IgnoreImplicit()->getType()->getNullability(S.Context)) {
1124     if (*nullability == NullabilityKind::NonNull)
1125       return false;
1126   }
1127 
1128   // As a special case, transparent unions initialized with zero are
1129   // considered null for the purposes of the nonnull attribute.
1130   if (const RecordType *UT = Expr->getType()->getAsUnionType()) {
1131     if (UT->getDecl()->hasAttr<TransparentUnionAttr>())
1132       if (const CompoundLiteralExpr *CLE =
1133           dyn_cast<CompoundLiteralExpr>(Expr))
1134         if (const InitListExpr *ILE =
1135             dyn_cast<InitListExpr>(CLE->getInitializer()))
1136           Expr = ILE->getInit(0);
1137   }
1138 
1139   bool Result;
1140   return (!Expr->isValueDependent() &&
1141           Expr->EvaluateAsBooleanCondition(Result, S.Context) &&
1142           !Result);
1143 }
1144 
1145 static void CheckNonNullArgument(Sema &S,
1146                                  const Expr *ArgExpr,
1147                                  SourceLocation CallSiteLoc) {
1148   if (CheckNonNullExpr(S, ArgExpr))
1149     S.Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange();
1150 }
1151 
1152 bool Sema::GetFormatNSStringIdx(const FormatAttr *Format, unsigned &Idx) {
1153   FormatStringInfo FSI;
1154   if ((GetFormatStringType(Format) == FST_NSString) &&
1155       getFormatStringInfo(Format, false, &FSI)) {
1156     Idx = FSI.FormatIdx;
1157     return true;
1158   }
1159   return false;
1160 }
1161 /// \brief Diagnose use of %s directive in an NSString which is being passed
1162 /// as formatting string to formatting method.
1163 static void
1164 DiagnoseCStringFormatDirectiveInCFAPI(Sema &S,
1165                                         const NamedDecl *FDecl,
1166                                         Expr **Args,
1167                                         unsigned NumArgs) {
1168   unsigned Idx = 0;
1169   bool Format = false;
1170   ObjCStringFormatFamily SFFamily = FDecl->getObjCFStringFormattingFamily();
1171   if (SFFamily == ObjCStringFormatFamily::SFF_CFString) {
1172     Idx = 2;
1173     Format = true;
1174   }
1175   else
1176     for (const auto *I : FDecl->specific_attrs<FormatAttr>()) {
1177       if (S.GetFormatNSStringIdx(I, Idx)) {
1178         Format = true;
1179         break;
1180       }
1181     }
1182   if (!Format || NumArgs <= Idx)
1183     return;
1184   const Expr *FormatExpr = Args[Idx];
1185   if (const CStyleCastExpr *CSCE = dyn_cast<CStyleCastExpr>(FormatExpr))
1186     FormatExpr = CSCE->getSubExpr();
1187   const StringLiteral *FormatString;
1188   if (const ObjCStringLiteral *OSL =
1189       dyn_cast<ObjCStringLiteral>(FormatExpr->IgnoreParenImpCasts()))
1190     FormatString = OSL->getString();
1191   else
1192     FormatString = dyn_cast<StringLiteral>(FormatExpr->IgnoreParenImpCasts());
1193   if (!FormatString)
1194     return;
1195   if (S.FormatStringHasSArg(FormatString)) {
1196     S.Diag(FormatExpr->getExprLoc(), diag::warn_objc_cdirective_format_string)
1197       << "%s" << 1 << 1;
1198     S.Diag(FDecl->getLocation(), diag::note_entity_declared_at)
1199       << FDecl->getDeclName();
1200   }
1201 }
1202 
1203 /// Determine whether the given type has a non-null nullability annotation.
1204 static bool isNonNullType(ASTContext &ctx, QualType type) {
1205   if (auto nullability = type->getNullability(ctx))
1206     return *nullability == NullabilityKind::NonNull;
1207 
1208   return false;
1209 }
1210 
1211 static void CheckNonNullArguments(Sema &S,
1212                                   const NamedDecl *FDecl,
1213                                   const FunctionProtoType *Proto,
1214                                   ArrayRef<const Expr *> Args,
1215                                   SourceLocation CallSiteLoc) {
1216   assert((FDecl || Proto) && "Need a function declaration or prototype");
1217 
1218   // Check the attributes attached to the method/function itself.
1219   llvm::SmallBitVector NonNullArgs;
1220   if (FDecl) {
1221     // Handle the nonnull attribute on the function/method declaration itself.
1222     for (const auto *NonNull : FDecl->specific_attrs<NonNullAttr>()) {
1223       if (!NonNull->args_size()) {
1224         // Easy case: all pointer arguments are nonnull.
1225         for (const auto *Arg : Args)
1226           if (S.isValidPointerAttrType(Arg->getType()))
1227             CheckNonNullArgument(S, Arg, CallSiteLoc);
1228         return;
1229       }
1230 
1231       for (unsigned Val : NonNull->args()) {
1232         if (Val >= Args.size())
1233           continue;
1234         if (NonNullArgs.empty())
1235           NonNullArgs.resize(Args.size());
1236         NonNullArgs.set(Val);
1237       }
1238     }
1239   }
1240 
1241   if (FDecl && (isa<FunctionDecl>(FDecl) || isa<ObjCMethodDecl>(FDecl))) {
1242     // Handle the nonnull attribute on the parameters of the
1243     // function/method.
1244     ArrayRef<ParmVarDecl*> parms;
1245     if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FDecl))
1246       parms = FD->parameters();
1247     else
1248       parms = cast<ObjCMethodDecl>(FDecl)->parameters();
1249 
1250     unsigned ParamIndex = 0;
1251     for (ArrayRef<ParmVarDecl*>::iterator I = parms.begin(), E = parms.end();
1252          I != E; ++I, ++ParamIndex) {
1253       const ParmVarDecl *PVD = *I;
1254       if (PVD->hasAttr<NonNullAttr>() ||
1255           isNonNullType(S.Context, PVD->getType())) {
1256         if (NonNullArgs.empty())
1257           NonNullArgs.resize(Args.size());
1258 
1259         NonNullArgs.set(ParamIndex);
1260       }
1261     }
1262   } else {
1263     // If we have a non-function, non-method declaration but no
1264     // function prototype, try to dig out the function prototype.
1265     if (!Proto) {
1266       if (const ValueDecl *VD = dyn_cast<ValueDecl>(FDecl)) {
1267         QualType type = VD->getType().getNonReferenceType();
1268         if (auto pointerType = type->getAs<PointerType>())
1269           type = pointerType->getPointeeType();
1270         else if (auto blockType = type->getAs<BlockPointerType>())
1271           type = blockType->getPointeeType();
1272         // FIXME: data member pointers?
1273 
1274         // Dig out the function prototype, if there is one.
1275         Proto = type->getAs<FunctionProtoType>();
1276       }
1277     }
1278 
1279     // Fill in non-null argument information from the nullability
1280     // information on the parameter types (if we have them).
1281     if (Proto) {
1282       unsigned Index = 0;
1283       for (auto paramType : Proto->getParamTypes()) {
1284         if (isNonNullType(S.Context, paramType)) {
1285           if (NonNullArgs.empty())
1286             NonNullArgs.resize(Args.size());
1287 
1288           NonNullArgs.set(Index);
1289         }
1290 
1291         ++Index;
1292       }
1293     }
1294   }
1295 
1296   // Check for non-null arguments.
1297   for (unsigned ArgIndex = 0, ArgIndexEnd = NonNullArgs.size();
1298        ArgIndex != ArgIndexEnd; ++ArgIndex) {
1299     if (NonNullArgs[ArgIndex])
1300       CheckNonNullArgument(S, Args[ArgIndex], CallSiteLoc);
1301   }
1302 }
1303 
1304 /// Handles the checks for format strings, non-POD arguments to vararg
1305 /// functions, and NULL arguments passed to non-NULL parameters.
1306 void Sema::checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto,
1307                      ArrayRef<const Expr *> Args, bool IsMemberFunction,
1308                      SourceLocation Loc, SourceRange Range,
1309                      VariadicCallType CallType) {
1310   // FIXME: We should check as much as we can in the template definition.
1311   if (CurContext->isDependentContext())
1312     return;
1313 
1314   // Printf and scanf checking.
1315   llvm::SmallBitVector CheckedVarArgs;
1316   if (FDecl) {
1317     for (const auto *I : FDecl->specific_attrs<FormatAttr>()) {
1318       // Only create vector if there are format attributes.
1319       CheckedVarArgs.resize(Args.size());
1320 
1321       CheckFormatArguments(I, Args, IsMemberFunction, CallType, Loc, Range,
1322                            CheckedVarArgs);
1323     }
1324   }
1325 
1326   // Refuse POD arguments that weren't caught by the format string
1327   // checks above.
1328   if (CallType != VariadicDoesNotApply) {
1329     unsigned NumParams = Proto ? Proto->getNumParams()
1330                        : FDecl && isa<FunctionDecl>(FDecl)
1331                            ? cast<FunctionDecl>(FDecl)->getNumParams()
1332                        : FDecl && isa<ObjCMethodDecl>(FDecl)
1333                            ? cast<ObjCMethodDecl>(FDecl)->param_size()
1334                        : 0;
1335 
1336     for (unsigned ArgIdx = NumParams; ArgIdx < Args.size(); ++ArgIdx) {
1337       // Args[ArgIdx] can be null in malformed code.
1338       if (const Expr *Arg = Args[ArgIdx]) {
1339         if (CheckedVarArgs.empty() || !CheckedVarArgs[ArgIdx])
1340           checkVariadicArgument(Arg, CallType);
1341       }
1342     }
1343   }
1344 
1345   if (FDecl || Proto) {
1346     CheckNonNullArguments(*this, FDecl, Proto, Args, Loc);
1347 
1348     // Type safety checking.
1349     if (FDecl) {
1350       for (const auto *I : FDecl->specific_attrs<ArgumentWithTypeTagAttr>())
1351         CheckArgumentWithTypeTag(I, Args.data());
1352     }
1353   }
1354 }
1355 
1356 /// CheckConstructorCall - Check a constructor call for correctness and safety
1357 /// properties not enforced by the C type system.
1358 void Sema::CheckConstructorCall(FunctionDecl *FDecl,
1359                                 ArrayRef<const Expr *> Args,
1360                                 const FunctionProtoType *Proto,
1361                                 SourceLocation Loc) {
1362   VariadicCallType CallType =
1363     Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply;
1364   checkCall(FDecl, Proto, Args, /*IsMemberFunction=*/true, Loc, SourceRange(),
1365             CallType);
1366 }
1367 
1368 /// CheckFunctionCall - Check a direct function call for various correctness
1369 /// and safety properties not strictly enforced by the C type system.
1370 bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall,
1371                              const FunctionProtoType *Proto) {
1372   bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) &&
1373                               isa<CXXMethodDecl>(FDecl);
1374   bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) ||
1375                           IsMemberOperatorCall;
1376   VariadicCallType CallType = getVariadicCallType(FDecl, Proto,
1377                                                   TheCall->getCallee());
1378   Expr** Args = TheCall->getArgs();
1379   unsigned NumArgs = TheCall->getNumArgs();
1380   if (IsMemberOperatorCall) {
1381     // If this is a call to a member operator, hide the first argument
1382     // from checkCall.
1383     // FIXME: Our choice of AST representation here is less than ideal.
1384     ++Args;
1385     --NumArgs;
1386   }
1387   checkCall(FDecl, Proto, llvm::makeArrayRef(Args, NumArgs),
1388             IsMemberFunction, TheCall->getRParenLoc(),
1389             TheCall->getCallee()->getSourceRange(), CallType);
1390 
1391   IdentifierInfo *FnInfo = FDecl->getIdentifier();
1392   // None of the checks below are needed for functions that don't have
1393   // simple names (e.g., C++ conversion functions).
1394   if (!FnInfo)
1395     return false;
1396 
1397   CheckAbsoluteValueFunction(TheCall, FDecl, FnInfo);
1398   if (getLangOpts().ObjC1)
1399     DiagnoseCStringFormatDirectiveInCFAPI(*this, FDecl, Args, NumArgs);
1400 
1401   unsigned CMId = FDecl->getMemoryFunctionKind();
1402   if (CMId == 0)
1403     return false;
1404 
1405   // Handle memory setting and copying functions.
1406   if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat)
1407     CheckStrlcpycatArguments(TheCall, FnInfo);
1408   else if (CMId == Builtin::BIstrncat)
1409     CheckStrncatArguments(TheCall, FnInfo);
1410   else
1411     CheckMemaccessArguments(TheCall, CMId, FnInfo);
1412 
1413   return false;
1414 }
1415 
1416 bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac,
1417                                ArrayRef<const Expr *> Args) {
1418   VariadicCallType CallType =
1419       Method->isVariadic() ? VariadicMethod : VariadicDoesNotApply;
1420 
1421   checkCall(Method, nullptr, Args,
1422             /*IsMemberFunction=*/false, lbrac, Method->getSourceRange(),
1423             CallType);
1424 
1425   return false;
1426 }
1427 
1428 bool Sema::CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall,
1429                             const FunctionProtoType *Proto) {
1430   QualType Ty;
1431   if (const auto *V = dyn_cast<VarDecl>(NDecl))
1432     Ty = V->getType().getNonReferenceType();
1433   else if (const auto *F = dyn_cast<FieldDecl>(NDecl))
1434     Ty = F->getType().getNonReferenceType();
1435   else
1436     return false;
1437 
1438   if (!Ty->isBlockPointerType() && !Ty->isFunctionPointerType() &&
1439       !Ty->isFunctionProtoType())
1440     return false;
1441 
1442   VariadicCallType CallType;
1443   if (!Proto || !Proto->isVariadic()) {
1444     CallType = VariadicDoesNotApply;
1445   } else if (Ty->isBlockPointerType()) {
1446     CallType = VariadicBlock;
1447   } else { // Ty->isFunctionPointerType()
1448     CallType = VariadicFunction;
1449   }
1450 
1451   checkCall(NDecl, Proto,
1452             llvm::makeArrayRef(TheCall->getArgs(), TheCall->getNumArgs()),
1453             /*IsMemberFunction=*/false, TheCall->getRParenLoc(),
1454             TheCall->getCallee()->getSourceRange(), CallType);
1455 
1456   return false;
1457 }
1458 
1459 /// Checks function calls when a FunctionDecl or a NamedDecl is not available,
1460 /// such as function pointers returned from functions.
1461 bool Sema::CheckOtherCall(CallExpr *TheCall, const FunctionProtoType *Proto) {
1462   VariadicCallType CallType = getVariadicCallType(/*FDecl=*/nullptr, Proto,
1463                                                   TheCall->getCallee());
1464   checkCall(/*FDecl=*/nullptr, Proto,
1465             llvm::makeArrayRef(TheCall->getArgs(), TheCall->getNumArgs()),
1466             /*IsMemberFunction=*/false, TheCall->getRParenLoc(),
1467             TheCall->getCallee()->getSourceRange(), CallType);
1468 
1469   return false;
1470 }
1471 
1472 static bool isValidOrderingForOp(int64_t Ordering, AtomicExpr::AtomicOp Op) {
1473   if (Ordering < AtomicExpr::AO_ABI_memory_order_relaxed ||
1474       Ordering > AtomicExpr::AO_ABI_memory_order_seq_cst)
1475     return false;
1476 
1477   switch (Op) {
1478   case AtomicExpr::AO__c11_atomic_init:
1479     llvm_unreachable("There is no ordering argument for an init");
1480 
1481   case AtomicExpr::AO__c11_atomic_load:
1482   case AtomicExpr::AO__atomic_load_n:
1483   case AtomicExpr::AO__atomic_load:
1484     return Ordering != AtomicExpr::AO_ABI_memory_order_release &&
1485            Ordering != AtomicExpr::AO_ABI_memory_order_acq_rel;
1486 
1487   case AtomicExpr::AO__c11_atomic_store:
1488   case AtomicExpr::AO__atomic_store:
1489   case AtomicExpr::AO__atomic_store_n:
1490     return Ordering != AtomicExpr::AO_ABI_memory_order_consume &&
1491            Ordering != AtomicExpr::AO_ABI_memory_order_acquire &&
1492            Ordering != AtomicExpr::AO_ABI_memory_order_acq_rel;
1493 
1494   default:
1495     return true;
1496   }
1497 }
1498 
1499 ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult,
1500                                          AtomicExpr::AtomicOp Op) {
1501   CallExpr *TheCall = cast<CallExpr>(TheCallResult.get());
1502   DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
1503 
1504   // All these operations take one of the following forms:
1505   enum {
1506     // C    __c11_atomic_init(A *, C)
1507     Init,
1508     // C    __c11_atomic_load(A *, int)
1509     Load,
1510     // void __atomic_load(A *, CP, int)
1511     Copy,
1512     // C    __c11_atomic_add(A *, M, int)
1513     Arithmetic,
1514     // C    __atomic_exchange_n(A *, CP, int)
1515     Xchg,
1516     // void __atomic_exchange(A *, C *, CP, int)
1517     GNUXchg,
1518     // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int)
1519     C11CmpXchg,
1520     // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int)
1521     GNUCmpXchg
1522   } Form = Init;
1523   const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 4, 5, 6 };
1524   const unsigned NumVals[] = { 1, 0, 1, 1, 1, 2, 2, 3 };
1525   // where:
1526   //   C is an appropriate type,
1527   //   A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins,
1528   //   CP is C for __c11 builtins and GNU _n builtins and is C * otherwise,
1529   //   M is C if C is an integer, and ptrdiff_t if C is a pointer, and
1530   //   the int parameters are for orderings.
1531 
1532   static_assert(AtomicExpr::AO__c11_atomic_init == 0 &&
1533                     AtomicExpr::AO__c11_atomic_fetch_xor + 1 ==
1534                         AtomicExpr::AO__atomic_load,
1535                 "need to update code for modified C11 atomics");
1536   bool IsC11 = Op >= AtomicExpr::AO__c11_atomic_init &&
1537                Op <= AtomicExpr::AO__c11_atomic_fetch_xor;
1538   bool IsN = Op == AtomicExpr::AO__atomic_load_n ||
1539              Op == AtomicExpr::AO__atomic_store_n ||
1540              Op == AtomicExpr::AO__atomic_exchange_n ||
1541              Op == AtomicExpr::AO__atomic_compare_exchange_n;
1542   bool IsAddSub = false;
1543 
1544   switch (Op) {
1545   case AtomicExpr::AO__c11_atomic_init:
1546     Form = Init;
1547     break;
1548 
1549   case AtomicExpr::AO__c11_atomic_load:
1550   case AtomicExpr::AO__atomic_load_n:
1551     Form = Load;
1552     break;
1553 
1554   case AtomicExpr::AO__c11_atomic_store:
1555   case AtomicExpr::AO__atomic_load:
1556   case AtomicExpr::AO__atomic_store:
1557   case AtomicExpr::AO__atomic_store_n:
1558     Form = Copy;
1559     break;
1560 
1561   case AtomicExpr::AO__c11_atomic_fetch_add:
1562   case AtomicExpr::AO__c11_atomic_fetch_sub:
1563   case AtomicExpr::AO__atomic_fetch_add:
1564   case AtomicExpr::AO__atomic_fetch_sub:
1565   case AtomicExpr::AO__atomic_add_fetch:
1566   case AtomicExpr::AO__atomic_sub_fetch:
1567     IsAddSub = true;
1568     // Fall through.
1569   case AtomicExpr::AO__c11_atomic_fetch_and:
1570   case AtomicExpr::AO__c11_atomic_fetch_or:
1571   case AtomicExpr::AO__c11_atomic_fetch_xor:
1572   case AtomicExpr::AO__atomic_fetch_and:
1573   case AtomicExpr::AO__atomic_fetch_or:
1574   case AtomicExpr::AO__atomic_fetch_xor:
1575   case AtomicExpr::AO__atomic_fetch_nand:
1576   case AtomicExpr::AO__atomic_and_fetch:
1577   case AtomicExpr::AO__atomic_or_fetch:
1578   case AtomicExpr::AO__atomic_xor_fetch:
1579   case AtomicExpr::AO__atomic_nand_fetch:
1580     Form = Arithmetic;
1581     break;
1582 
1583   case AtomicExpr::AO__c11_atomic_exchange:
1584   case AtomicExpr::AO__atomic_exchange_n:
1585     Form = Xchg;
1586     break;
1587 
1588   case AtomicExpr::AO__atomic_exchange:
1589     Form = GNUXchg;
1590     break;
1591 
1592   case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
1593   case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
1594     Form = C11CmpXchg;
1595     break;
1596 
1597   case AtomicExpr::AO__atomic_compare_exchange:
1598   case AtomicExpr::AO__atomic_compare_exchange_n:
1599     Form = GNUCmpXchg;
1600     break;
1601   }
1602 
1603   // Check we have the right number of arguments.
1604   if (TheCall->getNumArgs() < NumArgs[Form]) {
1605     Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
1606       << 0 << NumArgs[Form] << TheCall->getNumArgs()
1607       << TheCall->getCallee()->getSourceRange();
1608     return ExprError();
1609   } else if (TheCall->getNumArgs() > NumArgs[Form]) {
1610     Diag(TheCall->getArg(NumArgs[Form])->getLocStart(),
1611          diag::err_typecheck_call_too_many_args)
1612       << 0 << NumArgs[Form] << TheCall->getNumArgs()
1613       << TheCall->getCallee()->getSourceRange();
1614     return ExprError();
1615   }
1616 
1617   // Inspect the first argument of the atomic operation.
1618   Expr *Ptr = TheCall->getArg(0);
1619   Ptr = DefaultFunctionArrayLvalueConversion(Ptr).get();
1620   const PointerType *pointerType = Ptr->getType()->getAs<PointerType>();
1621   if (!pointerType) {
1622     Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
1623       << Ptr->getType() << Ptr->getSourceRange();
1624     return ExprError();
1625   }
1626 
1627   // For a __c11 builtin, this should be a pointer to an _Atomic type.
1628   QualType AtomTy = pointerType->getPointeeType(); // 'A'
1629   QualType ValType = AtomTy; // 'C'
1630   if (IsC11) {
1631     if (!AtomTy->isAtomicType()) {
1632       Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic)
1633         << Ptr->getType() << Ptr->getSourceRange();
1634       return ExprError();
1635     }
1636     if (AtomTy.isConstQualified()) {
1637       Diag(DRE->getLocStart(), diag::err_atomic_op_needs_non_const_atomic)
1638         << Ptr->getType() << Ptr->getSourceRange();
1639       return ExprError();
1640     }
1641     ValType = AtomTy->getAs<AtomicType>()->getValueType();
1642   }
1643 
1644   // For an arithmetic operation, the implied arithmetic must be well-formed.
1645   if (Form == Arithmetic) {
1646     // gcc does not enforce these rules for GNU atomics, but we do so for sanity.
1647     if (IsAddSub && !ValType->isIntegerType() && !ValType->isPointerType()) {
1648       Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr)
1649         << IsC11 << Ptr->getType() << Ptr->getSourceRange();
1650       return ExprError();
1651     }
1652     if (!IsAddSub && !ValType->isIntegerType()) {
1653       Diag(DRE->getLocStart(), diag::err_atomic_op_bitwise_needs_atomic_int)
1654         << IsC11 << Ptr->getType() << Ptr->getSourceRange();
1655       return ExprError();
1656     }
1657     if (IsC11 && ValType->isPointerType() &&
1658         RequireCompleteType(Ptr->getLocStart(), ValType->getPointeeType(),
1659                             diag::err_incomplete_type)) {
1660       return ExprError();
1661     }
1662   } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) {
1663     // For __atomic_*_n operations, the value type must be a scalar integral or
1664     // pointer type which is 1, 2, 4, 8 or 16 bytes in length.
1665     Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr)
1666       << IsC11 << Ptr->getType() << Ptr->getSourceRange();
1667     return ExprError();
1668   }
1669 
1670   if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context) &&
1671       !AtomTy->isScalarType()) {
1672     // For GNU atomics, require a trivially-copyable type. This is not part of
1673     // the GNU atomics specification, but we enforce it for sanity.
1674     Diag(DRE->getLocStart(), diag::err_atomic_op_needs_trivial_copy)
1675       << Ptr->getType() << Ptr->getSourceRange();
1676     return ExprError();
1677   }
1678 
1679   // FIXME: For any builtin other than a load, the ValType must not be
1680   // const-qualified.
1681 
1682   switch (ValType.getObjCLifetime()) {
1683   case Qualifiers::OCL_None:
1684   case Qualifiers::OCL_ExplicitNone:
1685     // okay
1686     break;
1687 
1688   case Qualifiers::OCL_Weak:
1689   case Qualifiers::OCL_Strong:
1690   case Qualifiers::OCL_Autoreleasing:
1691     // FIXME: Can this happen? By this point, ValType should be known
1692     // to be trivially copyable.
1693     Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
1694       << ValType << Ptr->getSourceRange();
1695     return ExprError();
1696   }
1697 
1698   // atomic_fetch_or takes a pointer to a volatile 'A'.  We shouldn't let the
1699   // volatile-ness of the pointee-type inject itself into the result or the
1700   // other operands.
1701   ValType.removeLocalVolatile();
1702   QualType ResultType = ValType;
1703   if (Form == Copy || Form == GNUXchg || Form == Init)
1704     ResultType = Context.VoidTy;
1705   else if (Form == C11CmpXchg || Form == GNUCmpXchg)
1706     ResultType = Context.BoolTy;
1707 
1708   // The type of a parameter passed 'by value'. In the GNU atomics, such
1709   // arguments are actually passed as pointers.
1710   QualType ByValType = ValType; // 'CP'
1711   if (!IsC11 && !IsN)
1712     ByValType = Ptr->getType();
1713 
1714   // The first argument --- the pointer --- has a fixed type; we
1715   // deduce the types of the rest of the arguments accordingly.  Walk
1716   // the remaining arguments, converting them to the deduced value type.
1717   for (unsigned i = 1; i != NumArgs[Form]; ++i) {
1718     QualType Ty;
1719     if (i < NumVals[Form] + 1) {
1720       switch (i) {
1721       case 1:
1722         // The second argument is the non-atomic operand. For arithmetic, this
1723         // is always passed by value, and for a compare_exchange it is always
1724         // passed by address. For the rest, GNU uses by-address and C11 uses
1725         // by-value.
1726         assert(Form != Load);
1727         if (Form == Init || (Form == Arithmetic && ValType->isIntegerType()))
1728           Ty = ValType;
1729         else if (Form == Copy || Form == Xchg)
1730           Ty = ByValType;
1731         else if (Form == Arithmetic)
1732           Ty = Context.getPointerDiffType();
1733         else
1734           Ty = Context.getPointerType(ValType.getUnqualifiedType());
1735         break;
1736       case 2:
1737         // The third argument to compare_exchange / GNU exchange is a
1738         // (pointer to a) desired value.
1739         Ty = ByValType;
1740         break;
1741       case 3:
1742         // The fourth argument to GNU compare_exchange is a 'weak' flag.
1743         Ty = Context.BoolTy;
1744         break;
1745       }
1746     } else {
1747       // The order(s) are always converted to int.
1748       Ty = Context.IntTy;
1749     }
1750 
1751     InitializedEntity Entity =
1752         InitializedEntity::InitializeParameter(Context, Ty, false);
1753     ExprResult Arg = TheCall->getArg(i);
1754     Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
1755     if (Arg.isInvalid())
1756       return true;
1757     TheCall->setArg(i, Arg.get());
1758   }
1759 
1760   // Permute the arguments into a 'consistent' order.
1761   SmallVector<Expr*, 5> SubExprs;
1762   SubExprs.push_back(Ptr);
1763   switch (Form) {
1764   case Init:
1765     // Note, AtomicExpr::getVal1() has a special case for this atomic.
1766     SubExprs.push_back(TheCall->getArg(1)); // Val1
1767     break;
1768   case Load:
1769     SubExprs.push_back(TheCall->getArg(1)); // Order
1770     break;
1771   case Copy:
1772   case Arithmetic:
1773   case Xchg:
1774     SubExprs.push_back(TheCall->getArg(2)); // Order
1775     SubExprs.push_back(TheCall->getArg(1)); // Val1
1776     break;
1777   case GNUXchg:
1778     // Note, AtomicExpr::getVal2() has a special case for this atomic.
1779     SubExprs.push_back(TheCall->getArg(3)); // Order
1780     SubExprs.push_back(TheCall->getArg(1)); // Val1
1781     SubExprs.push_back(TheCall->getArg(2)); // Val2
1782     break;
1783   case C11CmpXchg:
1784     SubExprs.push_back(TheCall->getArg(3)); // Order
1785     SubExprs.push_back(TheCall->getArg(1)); // Val1
1786     SubExprs.push_back(TheCall->getArg(4)); // OrderFail
1787     SubExprs.push_back(TheCall->getArg(2)); // Val2
1788     break;
1789   case GNUCmpXchg:
1790     SubExprs.push_back(TheCall->getArg(4)); // Order
1791     SubExprs.push_back(TheCall->getArg(1)); // Val1
1792     SubExprs.push_back(TheCall->getArg(5)); // OrderFail
1793     SubExprs.push_back(TheCall->getArg(2)); // Val2
1794     SubExprs.push_back(TheCall->getArg(3)); // Weak
1795     break;
1796   }
1797 
1798   if (SubExprs.size() >= 2 && Form != Init) {
1799     llvm::APSInt Result(32);
1800     if (SubExprs[1]->isIntegerConstantExpr(Result, Context) &&
1801         !isValidOrderingForOp(Result.getSExtValue(), Op))
1802       Diag(SubExprs[1]->getLocStart(),
1803            diag::warn_atomic_op_has_invalid_memory_order)
1804           << SubExprs[1]->getSourceRange();
1805   }
1806 
1807   AtomicExpr *AE = new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(),
1808                                             SubExprs, ResultType, Op,
1809                                             TheCall->getRParenLoc());
1810 
1811   if ((Op == AtomicExpr::AO__c11_atomic_load ||
1812        (Op == AtomicExpr::AO__c11_atomic_store)) &&
1813       Context.AtomicUsesUnsupportedLibcall(AE))
1814     Diag(AE->getLocStart(), diag::err_atomic_load_store_uses_lib) <<
1815     ((Op == AtomicExpr::AO__c11_atomic_load) ? 0 : 1);
1816 
1817   return AE;
1818 }
1819 
1820 
1821 /// checkBuiltinArgument - Given a call to a builtin function, perform
1822 /// normal type-checking on the given argument, updating the call in
1823 /// place.  This is useful when a builtin function requires custom
1824 /// type-checking for some of its arguments but not necessarily all of
1825 /// them.
1826 ///
1827 /// Returns true on error.
1828 static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) {
1829   FunctionDecl *Fn = E->getDirectCallee();
1830   assert(Fn && "builtin call without direct callee!");
1831 
1832   ParmVarDecl *Param = Fn->getParamDecl(ArgIndex);
1833   InitializedEntity Entity =
1834     InitializedEntity::InitializeParameter(S.Context, Param);
1835 
1836   ExprResult Arg = E->getArg(0);
1837   Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg);
1838   if (Arg.isInvalid())
1839     return true;
1840 
1841   E->setArg(ArgIndex, Arg.get());
1842   return false;
1843 }
1844 
1845 /// SemaBuiltinAtomicOverloaded - We have a call to a function like
1846 /// __sync_fetch_and_add, which is an overloaded function based on the pointer
1847 /// type of its first argument.  The main ActOnCallExpr routines have already
1848 /// promoted the types of arguments because all of these calls are prototyped as
1849 /// void(...).
1850 ///
1851 /// This function goes through and does final semantic checking for these
1852 /// builtins,
1853 ExprResult
1854 Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) {
1855   CallExpr *TheCall = (CallExpr *)TheCallResult.get();
1856   DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
1857   FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
1858 
1859   // Ensure that we have at least one argument to do type inference from.
1860   if (TheCall->getNumArgs() < 1) {
1861     Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
1862       << 0 << 1 << TheCall->getNumArgs()
1863       << TheCall->getCallee()->getSourceRange();
1864     return ExprError();
1865   }
1866 
1867   // Inspect the first argument of the atomic builtin.  This should always be
1868   // a pointer type, whose element is an integral scalar or pointer type.
1869   // Because it is a pointer type, we don't have to worry about any implicit
1870   // casts here.
1871   // FIXME: We don't allow floating point scalars as input.
1872   Expr *FirstArg = TheCall->getArg(0);
1873   ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg);
1874   if (FirstArgResult.isInvalid())
1875     return ExprError();
1876   FirstArg = FirstArgResult.get();
1877   TheCall->setArg(0, FirstArg);
1878 
1879   const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>();
1880   if (!pointerType) {
1881     Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
1882       << FirstArg->getType() << FirstArg->getSourceRange();
1883     return ExprError();
1884   }
1885 
1886   QualType ValType = pointerType->getPointeeType();
1887   if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
1888       !ValType->isBlockPointerType()) {
1889     Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr)
1890       << FirstArg->getType() << FirstArg->getSourceRange();
1891     return ExprError();
1892   }
1893 
1894   switch (ValType.getObjCLifetime()) {
1895   case Qualifiers::OCL_None:
1896   case Qualifiers::OCL_ExplicitNone:
1897     // okay
1898     break;
1899 
1900   case Qualifiers::OCL_Weak:
1901   case Qualifiers::OCL_Strong:
1902   case Qualifiers::OCL_Autoreleasing:
1903     Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
1904       << ValType << FirstArg->getSourceRange();
1905     return ExprError();
1906   }
1907 
1908   // Strip any qualifiers off ValType.
1909   ValType = ValType.getUnqualifiedType();
1910 
1911   // The majority of builtins return a value, but a few have special return
1912   // types, so allow them to override appropriately below.
1913   QualType ResultType = ValType;
1914 
1915   // We need to figure out which concrete builtin this maps onto.  For example,
1916   // __sync_fetch_and_add with a 2 byte object turns into
1917   // __sync_fetch_and_add_2.
1918 #define BUILTIN_ROW(x) \
1919   { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \
1920     Builtin::BI##x##_8, Builtin::BI##x##_16 }
1921 
1922   static const unsigned BuiltinIndices[][5] = {
1923     BUILTIN_ROW(__sync_fetch_and_add),
1924     BUILTIN_ROW(__sync_fetch_and_sub),
1925     BUILTIN_ROW(__sync_fetch_and_or),
1926     BUILTIN_ROW(__sync_fetch_and_and),
1927     BUILTIN_ROW(__sync_fetch_and_xor),
1928     BUILTIN_ROW(__sync_fetch_and_nand),
1929 
1930     BUILTIN_ROW(__sync_add_and_fetch),
1931     BUILTIN_ROW(__sync_sub_and_fetch),
1932     BUILTIN_ROW(__sync_and_and_fetch),
1933     BUILTIN_ROW(__sync_or_and_fetch),
1934     BUILTIN_ROW(__sync_xor_and_fetch),
1935     BUILTIN_ROW(__sync_nand_and_fetch),
1936 
1937     BUILTIN_ROW(__sync_val_compare_and_swap),
1938     BUILTIN_ROW(__sync_bool_compare_and_swap),
1939     BUILTIN_ROW(__sync_lock_test_and_set),
1940     BUILTIN_ROW(__sync_lock_release),
1941     BUILTIN_ROW(__sync_swap)
1942   };
1943 #undef BUILTIN_ROW
1944 
1945   // Determine the index of the size.
1946   unsigned SizeIndex;
1947   switch (Context.getTypeSizeInChars(ValType).getQuantity()) {
1948   case 1: SizeIndex = 0; break;
1949   case 2: SizeIndex = 1; break;
1950   case 4: SizeIndex = 2; break;
1951   case 8: SizeIndex = 3; break;
1952   case 16: SizeIndex = 4; break;
1953   default:
1954     Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size)
1955       << FirstArg->getType() << FirstArg->getSourceRange();
1956     return ExprError();
1957   }
1958 
1959   // Each of these builtins has one pointer argument, followed by some number of
1960   // values (0, 1 or 2) followed by a potentially empty varags list of stuff
1961   // that we ignore.  Find out which row of BuiltinIndices to read from as well
1962   // as the number of fixed args.
1963   unsigned BuiltinID = FDecl->getBuiltinID();
1964   unsigned BuiltinIndex, NumFixed = 1;
1965   bool WarnAboutSemanticsChange = false;
1966   switch (BuiltinID) {
1967   default: llvm_unreachable("Unknown overloaded atomic builtin!");
1968   case Builtin::BI__sync_fetch_and_add:
1969   case Builtin::BI__sync_fetch_and_add_1:
1970   case Builtin::BI__sync_fetch_and_add_2:
1971   case Builtin::BI__sync_fetch_and_add_4:
1972   case Builtin::BI__sync_fetch_and_add_8:
1973   case Builtin::BI__sync_fetch_and_add_16:
1974     BuiltinIndex = 0;
1975     break;
1976 
1977   case Builtin::BI__sync_fetch_and_sub:
1978   case Builtin::BI__sync_fetch_and_sub_1:
1979   case Builtin::BI__sync_fetch_and_sub_2:
1980   case Builtin::BI__sync_fetch_and_sub_4:
1981   case Builtin::BI__sync_fetch_and_sub_8:
1982   case Builtin::BI__sync_fetch_and_sub_16:
1983     BuiltinIndex = 1;
1984     break;
1985 
1986   case Builtin::BI__sync_fetch_and_or:
1987   case Builtin::BI__sync_fetch_and_or_1:
1988   case Builtin::BI__sync_fetch_and_or_2:
1989   case Builtin::BI__sync_fetch_and_or_4:
1990   case Builtin::BI__sync_fetch_and_or_8:
1991   case Builtin::BI__sync_fetch_and_or_16:
1992     BuiltinIndex = 2;
1993     break;
1994 
1995   case Builtin::BI__sync_fetch_and_and:
1996   case Builtin::BI__sync_fetch_and_and_1:
1997   case Builtin::BI__sync_fetch_and_and_2:
1998   case Builtin::BI__sync_fetch_and_and_4:
1999   case Builtin::BI__sync_fetch_and_and_8:
2000   case Builtin::BI__sync_fetch_and_and_16:
2001     BuiltinIndex = 3;
2002     break;
2003 
2004   case Builtin::BI__sync_fetch_and_xor:
2005   case Builtin::BI__sync_fetch_and_xor_1:
2006   case Builtin::BI__sync_fetch_and_xor_2:
2007   case Builtin::BI__sync_fetch_and_xor_4:
2008   case Builtin::BI__sync_fetch_and_xor_8:
2009   case Builtin::BI__sync_fetch_and_xor_16:
2010     BuiltinIndex = 4;
2011     break;
2012 
2013   case Builtin::BI__sync_fetch_and_nand:
2014   case Builtin::BI__sync_fetch_and_nand_1:
2015   case Builtin::BI__sync_fetch_and_nand_2:
2016   case Builtin::BI__sync_fetch_and_nand_4:
2017   case Builtin::BI__sync_fetch_and_nand_8:
2018   case Builtin::BI__sync_fetch_and_nand_16:
2019     BuiltinIndex = 5;
2020     WarnAboutSemanticsChange = true;
2021     break;
2022 
2023   case Builtin::BI__sync_add_and_fetch:
2024   case Builtin::BI__sync_add_and_fetch_1:
2025   case Builtin::BI__sync_add_and_fetch_2:
2026   case Builtin::BI__sync_add_and_fetch_4:
2027   case Builtin::BI__sync_add_and_fetch_8:
2028   case Builtin::BI__sync_add_and_fetch_16:
2029     BuiltinIndex = 6;
2030     break;
2031 
2032   case Builtin::BI__sync_sub_and_fetch:
2033   case Builtin::BI__sync_sub_and_fetch_1:
2034   case Builtin::BI__sync_sub_and_fetch_2:
2035   case Builtin::BI__sync_sub_and_fetch_4:
2036   case Builtin::BI__sync_sub_and_fetch_8:
2037   case Builtin::BI__sync_sub_and_fetch_16:
2038     BuiltinIndex = 7;
2039     break;
2040 
2041   case Builtin::BI__sync_and_and_fetch:
2042   case Builtin::BI__sync_and_and_fetch_1:
2043   case Builtin::BI__sync_and_and_fetch_2:
2044   case Builtin::BI__sync_and_and_fetch_4:
2045   case Builtin::BI__sync_and_and_fetch_8:
2046   case Builtin::BI__sync_and_and_fetch_16:
2047     BuiltinIndex = 8;
2048     break;
2049 
2050   case Builtin::BI__sync_or_and_fetch:
2051   case Builtin::BI__sync_or_and_fetch_1:
2052   case Builtin::BI__sync_or_and_fetch_2:
2053   case Builtin::BI__sync_or_and_fetch_4:
2054   case Builtin::BI__sync_or_and_fetch_8:
2055   case Builtin::BI__sync_or_and_fetch_16:
2056     BuiltinIndex = 9;
2057     break;
2058 
2059   case Builtin::BI__sync_xor_and_fetch:
2060   case Builtin::BI__sync_xor_and_fetch_1:
2061   case Builtin::BI__sync_xor_and_fetch_2:
2062   case Builtin::BI__sync_xor_and_fetch_4:
2063   case Builtin::BI__sync_xor_and_fetch_8:
2064   case Builtin::BI__sync_xor_and_fetch_16:
2065     BuiltinIndex = 10;
2066     break;
2067 
2068   case Builtin::BI__sync_nand_and_fetch:
2069   case Builtin::BI__sync_nand_and_fetch_1:
2070   case Builtin::BI__sync_nand_and_fetch_2:
2071   case Builtin::BI__sync_nand_and_fetch_4:
2072   case Builtin::BI__sync_nand_and_fetch_8:
2073   case Builtin::BI__sync_nand_and_fetch_16:
2074     BuiltinIndex = 11;
2075     WarnAboutSemanticsChange = true;
2076     break;
2077 
2078   case Builtin::BI__sync_val_compare_and_swap:
2079   case Builtin::BI__sync_val_compare_and_swap_1:
2080   case Builtin::BI__sync_val_compare_and_swap_2:
2081   case Builtin::BI__sync_val_compare_and_swap_4:
2082   case Builtin::BI__sync_val_compare_and_swap_8:
2083   case Builtin::BI__sync_val_compare_and_swap_16:
2084     BuiltinIndex = 12;
2085     NumFixed = 2;
2086     break;
2087 
2088   case Builtin::BI__sync_bool_compare_and_swap:
2089   case Builtin::BI__sync_bool_compare_and_swap_1:
2090   case Builtin::BI__sync_bool_compare_and_swap_2:
2091   case Builtin::BI__sync_bool_compare_and_swap_4:
2092   case Builtin::BI__sync_bool_compare_and_swap_8:
2093   case Builtin::BI__sync_bool_compare_and_swap_16:
2094     BuiltinIndex = 13;
2095     NumFixed = 2;
2096     ResultType = Context.BoolTy;
2097     break;
2098 
2099   case Builtin::BI__sync_lock_test_and_set:
2100   case Builtin::BI__sync_lock_test_and_set_1:
2101   case Builtin::BI__sync_lock_test_and_set_2:
2102   case Builtin::BI__sync_lock_test_and_set_4:
2103   case Builtin::BI__sync_lock_test_and_set_8:
2104   case Builtin::BI__sync_lock_test_and_set_16:
2105     BuiltinIndex = 14;
2106     break;
2107 
2108   case Builtin::BI__sync_lock_release:
2109   case Builtin::BI__sync_lock_release_1:
2110   case Builtin::BI__sync_lock_release_2:
2111   case Builtin::BI__sync_lock_release_4:
2112   case Builtin::BI__sync_lock_release_8:
2113   case Builtin::BI__sync_lock_release_16:
2114     BuiltinIndex = 15;
2115     NumFixed = 0;
2116     ResultType = Context.VoidTy;
2117     break;
2118 
2119   case Builtin::BI__sync_swap:
2120   case Builtin::BI__sync_swap_1:
2121   case Builtin::BI__sync_swap_2:
2122   case Builtin::BI__sync_swap_4:
2123   case Builtin::BI__sync_swap_8:
2124   case Builtin::BI__sync_swap_16:
2125     BuiltinIndex = 16;
2126     break;
2127   }
2128 
2129   // Now that we know how many fixed arguments we expect, first check that we
2130   // have at least that many.
2131   if (TheCall->getNumArgs() < 1+NumFixed) {
2132     Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
2133       << 0 << 1+NumFixed << TheCall->getNumArgs()
2134       << TheCall->getCallee()->getSourceRange();
2135     return ExprError();
2136   }
2137 
2138   if (WarnAboutSemanticsChange) {
2139     Diag(TheCall->getLocEnd(), diag::warn_sync_fetch_and_nand_semantics_change)
2140       << TheCall->getCallee()->getSourceRange();
2141   }
2142 
2143   // Get the decl for the concrete builtin from this, we can tell what the
2144   // concrete integer type we should convert to is.
2145   unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex];
2146   const char *NewBuiltinName = Context.BuiltinInfo.getName(NewBuiltinID);
2147   FunctionDecl *NewBuiltinDecl;
2148   if (NewBuiltinID == BuiltinID)
2149     NewBuiltinDecl = FDecl;
2150   else {
2151     // Perform builtin lookup to avoid redeclaring it.
2152     DeclarationName DN(&Context.Idents.get(NewBuiltinName));
2153     LookupResult Res(*this, DN, DRE->getLocStart(), LookupOrdinaryName);
2154     LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true);
2155     assert(Res.getFoundDecl());
2156     NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl());
2157     if (!NewBuiltinDecl)
2158       return ExprError();
2159   }
2160 
2161   // The first argument --- the pointer --- has a fixed type; we
2162   // deduce the types of the rest of the arguments accordingly.  Walk
2163   // the remaining arguments, converting them to the deduced value type.
2164   for (unsigned i = 0; i != NumFixed; ++i) {
2165     ExprResult Arg = TheCall->getArg(i+1);
2166 
2167     // GCC does an implicit conversion to the pointer or integer ValType.  This
2168     // can fail in some cases (1i -> int**), check for this error case now.
2169     // Initialize the argument.
2170     InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
2171                                                    ValType, /*consume*/ false);
2172     Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
2173     if (Arg.isInvalid())
2174       return ExprError();
2175 
2176     // Okay, we have something that *can* be converted to the right type.  Check
2177     // to see if there is a potentially weird extension going on here.  This can
2178     // happen when you do an atomic operation on something like an char* and
2179     // pass in 42.  The 42 gets converted to char.  This is even more strange
2180     // for things like 45.123 -> char, etc.
2181     // FIXME: Do this check.
2182     TheCall->setArg(i+1, Arg.get());
2183   }
2184 
2185   ASTContext& Context = this->getASTContext();
2186 
2187   // Create a new DeclRefExpr to refer to the new decl.
2188   DeclRefExpr* NewDRE = DeclRefExpr::Create(
2189       Context,
2190       DRE->getQualifierLoc(),
2191       SourceLocation(),
2192       NewBuiltinDecl,
2193       /*enclosing*/ false,
2194       DRE->getLocation(),
2195       Context.BuiltinFnTy,
2196       DRE->getValueKind());
2197 
2198   // Set the callee in the CallExpr.
2199   // FIXME: This loses syntactic information.
2200   QualType CalleePtrTy = Context.getPointerType(NewBuiltinDecl->getType());
2201   ExprResult PromotedCall = ImpCastExprToType(NewDRE, CalleePtrTy,
2202                                               CK_BuiltinFnToFnPtr);
2203   TheCall->setCallee(PromotedCall.get());
2204 
2205   // Change the result type of the call to match the original value type. This
2206   // is arbitrary, but the codegen for these builtins ins design to handle it
2207   // gracefully.
2208   TheCall->setType(ResultType);
2209 
2210   return TheCallResult;
2211 }
2212 
2213 /// CheckObjCString - Checks that the argument to the builtin
2214 /// CFString constructor is correct
2215 /// Note: It might also make sense to do the UTF-16 conversion here (would
2216 /// simplify the backend).
2217 bool Sema::CheckObjCString(Expr *Arg) {
2218   Arg = Arg->IgnoreParenCasts();
2219   StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
2220 
2221   if (!Literal || !Literal->isAscii()) {
2222     Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant)
2223       << Arg->getSourceRange();
2224     return true;
2225   }
2226 
2227   if (Literal->containsNonAsciiOrNull()) {
2228     StringRef String = Literal->getString();
2229     unsigned NumBytes = String.size();
2230     SmallVector<UTF16, 128> ToBuf(NumBytes);
2231     const UTF8 *FromPtr = (const UTF8 *)String.data();
2232     UTF16 *ToPtr = &ToBuf[0];
2233 
2234     ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes,
2235                                                  &ToPtr, ToPtr + NumBytes,
2236                                                  strictConversion);
2237     // Check for conversion failure.
2238     if (Result != conversionOK)
2239       Diag(Arg->getLocStart(),
2240            diag::warn_cfstring_truncated) << Arg->getSourceRange();
2241   }
2242   return false;
2243 }
2244 
2245 /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity.
2246 /// Emit an error and return true on failure, return false on success.
2247 bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) {
2248   Expr *Fn = TheCall->getCallee();
2249   if (TheCall->getNumArgs() > 2) {
2250     Diag(TheCall->getArg(2)->getLocStart(),
2251          diag::err_typecheck_call_too_many_args)
2252       << 0 /*function call*/ << 2 << TheCall->getNumArgs()
2253       << Fn->getSourceRange()
2254       << SourceRange(TheCall->getArg(2)->getLocStart(),
2255                      (*(TheCall->arg_end()-1))->getLocEnd());
2256     return true;
2257   }
2258 
2259   if (TheCall->getNumArgs() < 2) {
2260     return Diag(TheCall->getLocEnd(),
2261       diag::err_typecheck_call_too_few_args_at_least)
2262       << 0 /*function call*/ << 2 << TheCall->getNumArgs();
2263   }
2264 
2265   // Type-check the first argument normally.
2266   if (checkBuiltinArgument(*this, TheCall, 0))
2267     return true;
2268 
2269   // Determine whether the current function is variadic or not.
2270   BlockScopeInfo *CurBlock = getCurBlock();
2271   bool isVariadic;
2272   if (CurBlock)
2273     isVariadic = CurBlock->TheDecl->isVariadic();
2274   else if (FunctionDecl *FD = getCurFunctionDecl())
2275     isVariadic = FD->isVariadic();
2276   else
2277     isVariadic = getCurMethodDecl()->isVariadic();
2278 
2279   if (!isVariadic) {
2280     Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function);
2281     return true;
2282   }
2283 
2284   // Verify that the second argument to the builtin is the last argument of the
2285   // current function or method.
2286   bool SecondArgIsLastNamedArgument = false;
2287   const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts();
2288 
2289   // These are valid if SecondArgIsLastNamedArgument is false after the next
2290   // block.
2291   QualType Type;
2292   SourceLocation ParamLoc;
2293 
2294   if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) {
2295     if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
2296       // FIXME: This isn't correct for methods (results in bogus warning).
2297       // Get the last formal in the current function.
2298       const ParmVarDecl *LastArg;
2299       if (CurBlock)
2300         LastArg = *(CurBlock->TheDecl->param_end()-1);
2301       else if (FunctionDecl *FD = getCurFunctionDecl())
2302         LastArg = *(FD->param_end()-1);
2303       else
2304         LastArg = *(getCurMethodDecl()->param_end()-1);
2305       SecondArgIsLastNamedArgument = PV == LastArg;
2306 
2307       Type = PV->getType();
2308       ParamLoc = PV->getLocation();
2309     }
2310   }
2311 
2312   if (!SecondArgIsLastNamedArgument)
2313     Diag(TheCall->getArg(1)->getLocStart(),
2314          diag::warn_second_parameter_of_va_start_not_last_named_argument);
2315   else if (Type->isReferenceType()) {
2316     Diag(Arg->getLocStart(),
2317          diag::warn_va_start_of_reference_type_is_undefined);
2318     Diag(ParamLoc, diag::note_parameter_type) << Type;
2319   }
2320 
2321   TheCall->setType(Context.VoidTy);
2322   return false;
2323 }
2324 
2325 bool Sema::SemaBuiltinVAStartARM(CallExpr *Call) {
2326   // void __va_start(va_list *ap, const char *named_addr, size_t slot_size,
2327   //                 const char *named_addr);
2328 
2329   Expr *Func = Call->getCallee();
2330 
2331   if (Call->getNumArgs() < 3)
2332     return Diag(Call->getLocEnd(),
2333                 diag::err_typecheck_call_too_few_args_at_least)
2334            << 0 /*function call*/ << 3 << Call->getNumArgs();
2335 
2336   // Determine whether the current function is variadic or not.
2337   bool IsVariadic;
2338   if (BlockScopeInfo *CurBlock = getCurBlock())
2339     IsVariadic = CurBlock->TheDecl->isVariadic();
2340   else if (FunctionDecl *FD = getCurFunctionDecl())
2341     IsVariadic = FD->isVariadic();
2342   else if (ObjCMethodDecl *MD = getCurMethodDecl())
2343     IsVariadic = MD->isVariadic();
2344   else
2345     llvm_unreachable("unexpected statement type");
2346 
2347   if (!IsVariadic) {
2348     Diag(Func->getLocStart(), diag::err_va_start_used_in_non_variadic_function);
2349     return true;
2350   }
2351 
2352   // Type-check the first argument normally.
2353   if (checkBuiltinArgument(*this, Call, 0))
2354     return true;
2355 
2356   const struct {
2357     unsigned ArgNo;
2358     QualType Type;
2359   } ArgumentTypes[] = {
2360     { 1, Context.getPointerType(Context.CharTy.withConst()) },
2361     { 2, Context.getSizeType() },
2362   };
2363 
2364   for (const auto &AT : ArgumentTypes) {
2365     const Expr *Arg = Call->getArg(AT.ArgNo)->IgnoreParens();
2366     if (Arg->getType().getCanonicalType() == AT.Type.getCanonicalType())
2367       continue;
2368     Diag(Arg->getLocStart(), diag::err_typecheck_convert_incompatible)
2369       << Arg->getType() << AT.Type << 1 /* different class */
2370       << 0 /* qualifier difference */ << 3 /* parameter mismatch */
2371       << AT.ArgNo + 1 << Arg->getType() << AT.Type;
2372   }
2373 
2374   return false;
2375 }
2376 
2377 /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and
2378 /// friends.  This is declared to take (...), so we have to check everything.
2379 bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) {
2380   if (TheCall->getNumArgs() < 2)
2381     return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
2382       << 0 << 2 << TheCall->getNumArgs()/*function call*/;
2383   if (TheCall->getNumArgs() > 2)
2384     return Diag(TheCall->getArg(2)->getLocStart(),
2385                 diag::err_typecheck_call_too_many_args)
2386       << 0 /*function call*/ << 2 << TheCall->getNumArgs()
2387       << SourceRange(TheCall->getArg(2)->getLocStart(),
2388                      (*(TheCall->arg_end()-1))->getLocEnd());
2389 
2390   ExprResult OrigArg0 = TheCall->getArg(0);
2391   ExprResult OrigArg1 = TheCall->getArg(1);
2392 
2393   // Do standard promotions between the two arguments, returning their common
2394   // type.
2395   QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false);
2396   if (OrigArg0.isInvalid() || OrigArg1.isInvalid())
2397     return true;
2398 
2399   // Make sure any conversions are pushed back into the call; this is
2400   // type safe since unordered compare builtins are declared as "_Bool
2401   // foo(...)".
2402   TheCall->setArg(0, OrigArg0.get());
2403   TheCall->setArg(1, OrigArg1.get());
2404 
2405   if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent())
2406     return false;
2407 
2408   // If the common type isn't a real floating type, then the arguments were
2409   // invalid for this operation.
2410   if (Res.isNull() || !Res->isRealFloatingType())
2411     return Diag(OrigArg0.get()->getLocStart(),
2412                 diag::err_typecheck_call_invalid_ordered_compare)
2413       << OrigArg0.get()->getType() << OrigArg1.get()->getType()
2414       << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd());
2415 
2416   return false;
2417 }
2418 
2419 /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like
2420 /// __builtin_isnan and friends.  This is declared to take (...), so we have
2421 /// to check everything. We expect the last argument to be a floating point
2422 /// value.
2423 bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) {
2424   if (TheCall->getNumArgs() < NumArgs)
2425     return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
2426       << 0 << NumArgs << TheCall->getNumArgs()/*function call*/;
2427   if (TheCall->getNumArgs() > NumArgs)
2428     return Diag(TheCall->getArg(NumArgs)->getLocStart(),
2429                 diag::err_typecheck_call_too_many_args)
2430       << 0 /*function call*/ << NumArgs << TheCall->getNumArgs()
2431       << SourceRange(TheCall->getArg(NumArgs)->getLocStart(),
2432                      (*(TheCall->arg_end()-1))->getLocEnd());
2433 
2434   Expr *OrigArg = TheCall->getArg(NumArgs-1);
2435 
2436   if (OrigArg->isTypeDependent())
2437     return false;
2438 
2439   // This operation requires a non-_Complex floating-point number.
2440   if (!OrigArg->getType()->isRealFloatingType())
2441     return Diag(OrigArg->getLocStart(),
2442                 diag::err_typecheck_call_invalid_unary_fp)
2443       << OrigArg->getType() << OrigArg->getSourceRange();
2444 
2445   // If this is an implicit conversion from float -> double, remove it.
2446   if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) {
2447     Expr *CastArg = Cast->getSubExpr();
2448     if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) {
2449       assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) &&
2450              "promotion from float to double is the only expected cast here");
2451       Cast->setSubExpr(nullptr);
2452       TheCall->setArg(NumArgs-1, CastArg);
2453     }
2454   }
2455 
2456   return false;
2457 }
2458 
2459 /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector.
2460 // This is declared to take (...), so we have to check everything.
2461 ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) {
2462   if (TheCall->getNumArgs() < 2)
2463     return ExprError(Diag(TheCall->getLocEnd(),
2464                           diag::err_typecheck_call_too_few_args_at_least)
2465                      << 0 /*function call*/ << 2 << TheCall->getNumArgs()
2466                      << TheCall->getSourceRange());
2467 
2468   // Determine which of the following types of shufflevector we're checking:
2469   // 1) unary, vector mask: (lhs, mask)
2470   // 2) binary, vector mask: (lhs, rhs, mask)
2471   // 3) binary, scalar mask: (lhs, rhs, index, ..., index)
2472   QualType resType = TheCall->getArg(0)->getType();
2473   unsigned numElements = 0;
2474 
2475   if (!TheCall->getArg(0)->isTypeDependent() &&
2476       !TheCall->getArg(1)->isTypeDependent()) {
2477     QualType LHSType = TheCall->getArg(0)->getType();
2478     QualType RHSType = TheCall->getArg(1)->getType();
2479 
2480     if (!LHSType->isVectorType() || !RHSType->isVectorType())
2481       return ExprError(Diag(TheCall->getLocStart(),
2482                             diag::err_shufflevector_non_vector)
2483                        << SourceRange(TheCall->getArg(0)->getLocStart(),
2484                                       TheCall->getArg(1)->getLocEnd()));
2485 
2486     numElements = LHSType->getAs<VectorType>()->getNumElements();
2487     unsigned numResElements = TheCall->getNumArgs() - 2;
2488 
2489     // Check to see if we have a call with 2 vector arguments, the unary shuffle
2490     // with mask.  If so, verify that RHS is an integer vector type with the
2491     // same number of elts as lhs.
2492     if (TheCall->getNumArgs() == 2) {
2493       if (!RHSType->hasIntegerRepresentation() ||
2494           RHSType->getAs<VectorType>()->getNumElements() != numElements)
2495         return ExprError(Diag(TheCall->getLocStart(),
2496                               diag::err_shufflevector_incompatible_vector)
2497                          << SourceRange(TheCall->getArg(1)->getLocStart(),
2498                                         TheCall->getArg(1)->getLocEnd()));
2499     } else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) {
2500       return ExprError(Diag(TheCall->getLocStart(),
2501                             diag::err_shufflevector_incompatible_vector)
2502                        << SourceRange(TheCall->getArg(0)->getLocStart(),
2503                                       TheCall->getArg(1)->getLocEnd()));
2504     } else if (numElements != numResElements) {
2505       QualType eltType = LHSType->getAs<VectorType>()->getElementType();
2506       resType = Context.getVectorType(eltType, numResElements,
2507                                       VectorType::GenericVector);
2508     }
2509   }
2510 
2511   for (unsigned i = 2; i < TheCall->getNumArgs(); i++) {
2512     if (TheCall->getArg(i)->isTypeDependent() ||
2513         TheCall->getArg(i)->isValueDependent())
2514       continue;
2515 
2516     llvm::APSInt Result(32);
2517     if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context))
2518       return ExprError(Diag(TheCall->getLocStart(),
2519                             diag::err_shufflevector_nonconstant_argument)
2520                        << TheCall->getArg(i)->getSourceRange());
2521 
2522     // Allow -1 which will be translated to undef in the IR.
2523     if (Result.isSigned() && Result.isAllOnesValue())
2524       continue;
2525 
2526     if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2)
2527       return ExprError(Diag(TheCall->getLocStart(),
2528                             diag::err_shufflevector_argument_too_large)
2529                        << TheCall->getArg(i)->getSourceRange());
2530   }
2531 
2532   SmallVector<Expr*, 32> exprs;
2533 
2534   for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) {
2535     exprs.push_back(TheCall->getArg(i));
2536     TheCall->setArg(i, nullptr);
2537   }
2538 
2539   return new (Context) ShuffleVectorExpr(Context, exprs, resType,
2540                                          TheCall->getCallee()->getLocStart(),
2541                                          TheCall->getRParenLoc());
2542 }
2543 
2544 /// SemaConvertVectorExpr - Handle __builtin_convertvector
2545 ExprResult Sema::SemaConvertVectorExpr(Expr *E, TypeSourceInfo *TInfo,
2546                                        SourceLocation BuiltinLoc,
2547                                        SourceLocation RParenLoc) {
2548   ExprValueKind VK = VK_RValue;
2549   ExprObjectKind OK = OK_Ordinary;
2550   QualType DstTy = TInfo->getType();
2551   QualType SrcTy = E->getType();
2552 
2553   if (!SrcTy->isVectorType() && !SrcTy->isDependentType())
2554     return ExprError(Diag(BuiltinLoc,
2555                           diag::err_convertvector_non_vector)
2556                      << E->getSourceRange());
2557   if (!DstTy->isVectorType() && !DstTy->isDependentType())
2558     return ExprError(Diag(BuiltinLoc,
2559                           diag::err_convertvector_non_vector_type));
2560 
2561   if (!SrcTy->isDependentType() && !DstTy->isDependentType()) {
2562     unsigned SrcElts = SrcTy->getAs<VectorType>()->getNumElements();
2563     unsigned DstElts = DstTy->getAs<VectorType>()->getNumElements();
2564     if (SrcElts != DstElts)
2565       return ExprError(Diag(BuiltinLoc,
2566                             diag::err_convertvector_incompatible_vector)
2567                        << E->getSourceRange());
2568   }
2569 
2570   return new (Context)
2571       ConvertVectorExpr(E, TInfo, DstTy, VK, OK, BuiltinLoc, RParenLoc);
2572 }
2573 
2574 /// SemaBuiltinPrefetch - Handle __builtin_prefetch.
2575 // This is declared to take (const void*, ...) and can take two
2576 // optional constant int args.
2577 bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) {
2578   unsigned NumArgs = TheCall->getNumArgs();
2579 
2580   if (NumArgs > 3)
2581     return Diag(TheCall->getLocEnd(),
2582              diag::err_typecheck_call_too_many_args_at_most)
2583              << 0 /*function call*/ << 3 << NumArgs
2584              << TheCall->getSourceRange();
2585 
2586   // Argument 0 is checked for us and the remaining arguments must be
2587   // constant integers.
2588   for (unsigned i = 1; i != NumArgs; ++i)
2589     if (SemaBuiltinConstantArgRange(TheCall, i, 0, i == 1 ? 1 : 3))
2590       return true;
2591 
2592   return false;
2593 }
2594 
2595 /// SemaBuiltinAssume - Handle __assume (MS Extension).
2596 // __assume does not evaluate its arguments, and should warn if its argument
2597 // has side effects.
2598 bool Sema::SemaBuiltinAssume(CallExpr *TheCall) {
2599   Expr *Arg = TheCall->getArg(0);
2600   if (Arg->isInstantiationDependent()) return false;
2601 
2602   if (Arg->HasSideEffects(Context))
2603     Diag(Arg->getLocStart(), diag::warn_assume_side_effects)
2604       << Arg->getSourceRange()
2605       << cast<FunctionDecl>(TheCall->getCalleeDecl())->getIdentifier();
2606 
2607   return false;
2608 }
2609 
2610 /// Handle __builtin_assume_aligned. This is declared
2611 /// as (const void*, size_t, ...) and can take one optional constant int arg.
2612 bool Sema::SemaBuiltinAssumeAligned(CallExpr *TheCall) {
2613   unsigned NumArgs = TheCall->getNumArgs();
2614 
2615   if (NumArgs > 3)
2616     return Diag(TheCall->getLocEnd(),
2617              diag::err_typecheck_call_too_many_args_at_most)
2618              << 0 /*function call*/ << 3 << NumArgs
2619              << TheCall->getSourceRange();
2620 
2621   // The alignment must be a constant integer.
2622   Expr *Arg = TheCall->getArg(1);
2623 
2624   // We can't check the value of a dependent argument.
2625   if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
2626     llvm::APSInt Result;
2627     if (SemaBuiltinConstantArg(TheCall, 1, Result))
2628       return true;
2629 
2630     if (!Result.isPowerOf2())
2631       return Diag(TheCall->getLocStart(),
2632                   diag::err_alignment_not_power_of_two)
2633            << Arg->getSourceRange();
2634   }
2635 
2636   if (NumArgs > 2) {
2637     ExprResult Arg(TheCall->getArg(2));
2638     InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
2639       Context.getSizeType(), false);
2640     Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
2641     if (Arg.isInvalid()) return true;
2642     TheCall->setArg(2, Arg.get());
2643   }
2644 
2645   return false;
2646 }
2647 
2648 /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr
2649 /// TheCall is a constant expression.
2650 bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum,
2651                                   llvm::APSInt &Result) {
2652   Expr *Arg = TheCall->getArg(ArgNum);
2653   DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
2654   FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
2655 
2656   if (Arg->isTypeDependent() || Arg->isValueDependent()) return false;
2657 
2658   if (!Arg->isIntegerConstantExpr(Result, Context))
2659     return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type)
2660                 << FDecl->getDeclName() <<  Arg->getSourceRange();
2661 
2662   return false;
2663 }
2664 
2665 /// SemaBuiltinConstantArgRange - Handle a check if argument ArgNum of CallExpr
2666 /// TheCall is a constant expression in the range [Low, High].
2667 bool Sema::SemaBuiltinConstantArgRange(CallExpr *TheCall, int ArgNum,
2668                                        int Low, int High) {
2669   llvm::APSInt Result;
2670 
2671   // We can't check the value of a dependent argument.
2672   Expr *Arg = TheCall->getArg(ArgNum);
2673   if (Arg->isTypeDependent() || Arg->isValueDependent())
2674     return false;
2675 
2676   // Check constant-ness first.
2677   if (SemaBuiltinConstantArg(TheCall, ArgNum, Result))
2678     return true;
2679 
2680   if (Result.getSExtValue() < Low || Result.getSExtValue() > High)
2681     return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
2682       << Low << High << Arg->getSourceRange();
2683 
2684   return false;
2685 }
2686 
2687 /// SemaBuiltinARMSpecialReg - Handle a check if argument ArgNum of CallExpr
2688 /// TheCall is an ARM/AArch64 special register string literal.
2689 bool Sema::SemaBuiltinARMSpecialReg(unsigned BuiltinID, CallExpr *TheCall,
2690                                     int ArgNum, unsigned ExpectedFieldNum,
2691                                     bool AllowName) {
2692   bool IsARMBuiltin = BuiltinID == ARM::BI__builtin_arm_rsr64 ||
2693                       BuiltinID == ARM::BI__builtin_arm_wsr64 ||
2694                       BuiltinID == ARM::BI__builtin_arm_rsr ||
2695                       BuiltinID == ARM::BI__builtin_arm_rsrp ||
2696                       BuiltinID == ARM::BI__builtin_arm_wsr ||
2697                       BuiltinID == ARM::BI__builtin_arm_wsrp;
2698   bool IsAArch64Builtin = BuiltinID == AArch64::BI__builtin_arm_rsr64 ||
2699                           BuiltinID == AArch64::BI__builtin_arm_wsr64 ||
2700                           BuiltinID == AArch64::BI__builtin_arm_rsr ||
2701                           BuiltinID == AArch64::BI__builtin_arm_rsrp ||
2702                           BuiltinID == AArch64::BI__builtin_arm_wsr ||
2703                           BuiltinID == AArch64::BI__builtin_arm_wsrp;
2704   assert((IsARMBuiltin || IsAArch64Builtin) && "Unexpected ARM builtin.");
2705 
2706   // We can't check the value of a dependent argument.
2707   Expr *Arg = TheCall->getArg(ArgNum);
2708   if (Arg->isTypeDependent() || Arg->isValueDependent())
2709     return false;
2710 
2711   // Check if the argument is a string literal.
2712   if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts()))
2713     return Diag(TheCall->getLocStart(), diag::err_expr_not_string_literal)
2714            << Arg->getSourceRange();
2715 
2716   // Check the type of special register given.
2717   StringRef Reg = cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString();
2718   SmallVector<StringRef, 6> Fields;
2719   Reg.split(Fields, ":");
2720 
2721   if (Fields.size() != ExpectedFieldNum && !(AllowName && Fields.size() == 1))
2722     return Diag(TheCall->getLocStart(), diag::err_arm_invalid_specialreg)
2723            << Arg->getSourceRange();
2724 
2725   // If the string is the name of a register then we cannot check that it is
2726   // valid here but if the string is of one the forms described in ACLE then we
2727   // can check that the supplied fields are integers and within the valid
2728   // ranges.
2729   if (Fields.size() > 1) {
2730     bool FiveFields = Fields.size() == 5;
2731 
2732     bool ValidString = true;
2733     if (IsARMBuiltin) {
2734       ValidString &= Fields[0].startswith_lower("cp") ||
2735                      Fields[0].startswith_lower("p");
2736       if (ValidString)
2737         Fields[0] =
2738           Fields[0].drop_front(Fields[0].startswith_lower("cp") ? 2 : 1);
2739 
2740       ValidString &= Fields[2].startswith_lower("c");
2741       if (ValidString)
2742         Fields[2] = Fields[2].drop_front(1);
2743 
2744       if (FiveFields) {
2745         ValidString &= Fields[3].startswith_lower("c");
2746         if (ValidString)
2747           Fields[3] = Fields[3].drop_front(1);
2748       }
2749     }
2750 
2751     SmallVector<int, 5> Ranges;
2752     if (FiveFields)
2753       Ranges.append({IsAArch64Builtin ? 1 : 15, 7, 7, 15, 15});
2754     else
2755       Ranges.append({15, 7, 15});
2756 
2757     for (unsigned i=0; i<Fields.size(); ++i) {
2758       int IntField;
2759       ValidString &= !Fields[i].getAsInteger(10, IntField);
2760       ValidString &= (IntField >= 0 && IntField <= Ranges[i]);
2761     }
2762 
2763     if (!ValidString)
2764       return Diag(TheCall->getLocStart(), diag::err_arm_invalid_specialreg)
2765              << Arg->getSourceRange();
2766 
2767   } else if (IsAArch64Builtin && Fields.size() == 1) {
2768     // If the register name is one of those that appear in the condition below
2769     // and the special register builtin being used is one of the write builtins,
2770     // then we require that the argument provided for writing to the register
2771     // is an integer constant expression. This is because it will be lowered to
2772     // an MSR (immediate) instruction, so we need to know the immediate at
2773     // compile time.
2774     if (TheCall->getNumArgs() != 2)
2775       return false;
2776 
2777     std::string RegLower = Reg.lower();
2778     if (RegLower != "spsel" && RegLower != "daifset" && RegLower != "daifclr" &&
2779         RegLower != "pan" && RegLower != "uao")
2780       return false;
2781 
2782     return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15);
2783   }
2784 
2785   return false;
2786 }
2787 
2788 /// SemaBuiltinCpuSupports - Handle __builtin_cpu_supports(char *).
2789 /// This checks that the target supports __builtin_cpu_supports and
2790 /// that the string argument is constant and valid.
2791 bool Sema::SemaBuiltinCpuSupports(CallExpr *TheCall) {
2792   Expr *Arg = TheCall->getArg(0);
2793 
2794   // Check if the argument is a string literal.
2795   if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts()))
2796     return Diag(TheCall->getLocStart(), diag::err_expr_not_string_literal)
2797            << Arg->getSourceRange();
2798 
2799   // Check the contents of the string.
2800   StringRef Feature =
2801       cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString();
2802   if (!Context.getTargetInfo().validateCpuSupports(Feature))
2803     return Diag(TheCall->getLocStart(), diag::err_invalid_cpu_supports)
2804            << Arg->getSourceRange();
2805   return false;
2806 }
2807 
2808 /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val).
2809 /// This checks that the target supports __builtin_longjmp and
2810 /// that val is a constant 1.
2811 bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) {
2812   if (!Context.getTargetInfo().hasSjLjLowering())
2813     return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_unsupported)
2814              << SourceRange(TheCall->getLocStart(), TheCall->getLocEnd());
2815 
2816   Expr *Arg = TheCall->getArg(1);
2817   llvm::APSInt Result;
2818 
2819   // TODO: This is less than ideal. Overload this to take a value.
2820   if (SemaBuiltinConstantArg(TheCall, 1, Result))
2821     return true;
2822 
2823   if (Result != 1)
2824     return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val)
2825              << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
2826 
2827   return false;
2828 }
2829 
2830 
2831 /// SemaBuiltinSetjmp - Handle __builtin_setjmp(void *env[5]).
2832 /// This checks that the target supports __builtin_setjmp.
2833 bool Sema::SemaBuiltinSetjmp(CallExpr *TheCall) {
2834   if (!Context.getTargetInfo().hasSjLjLowering())
2835     return Diag(TheCall->getLocStart(), diag::err_builtin_setjmp_unsupported)
2836              << SourceRange(TheCall->getLocStart(), TheCall->getLocEnd());
2837   return false;
2838 }
2839 
2840 namespace {
2841 enum StringLiteralCheckType {
2842   SLCT_NotALiteral,
2843   SLCT_UncheckedLiteral,
2844   SLCT_CheckedLiteral
2845 };
2846 }
2847 
2848 // Determine if an expression is a string literal or constant string.
2849 // If this function returns false on the arguments to a function expecting a
2850 // format string, we will usually need to emit a warning.
2851 // True string literals are then checked by CheckFormatString.
2852 static StringLiteralCheckType
2853 checkFormatStringExpr(Sema &S, const Expr *E, ArrayRef<const Expr *> Args,
2854                       bool HasVAListArg, unsigned format_idx,
2855                       unsigned firstDataArg, Sema::FormatStringType Type,
2856                       Sema::VariadicCallType CallType, bool InFunctionCall,
2857                       llvm::SmallBitVector &CheckedVarArgs) {
2858  tryAgain:
2859   if (E->isTypeDependent() || E->isValueDependent())
2860     return SLCT_NotALiteral;
2861 
2862   E = E->IgnoreParenCasts();
2863 
2864   if (E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull))
2865     // Technically -Wformat-nonliteral does not warn about this case.
2866     // The behavior of printf and friends in this case is implementation
2867     // dependent.  Ideally if the format string cannot be null then
2868     // it should have a 'nonnull' attribute in the function prototype.
2869     return SLCT_UncheckedLiteral;
2870 
2871   switch (E->getStmtClass()) {
2872   case Stmt::BinaryConditionalOperatorClass:
2873   case Stmt::ConditionalOperatorClass: {
2874     // The expression is a literal if both sub-expressions were, and it was
2875     // completely checked only if both sub-expressions were checked.
2876     const AbstractConditionalOperator *C =
2877         cast<AbstractConditionalOperator>(E);
2878     StringLiteralCheckType Left =
2879         checkFormatStringExpr(S, C->getTrueExpr(), Args,
2880                               HasVAListArg, format_idx, firstDataArg,
2881                               Type, CallType, InFunctionCall, CheckedVarArgs);
2882     if (Left == SLCT_NotALiteral)
2883       return SLCT_NotALiteral;
2884     StringLiteralCheckType Right =
2885         checkFormatStringExpr(S, C->getFalseExpr(), Args,
2886                               HasVAListArg, format_idx, firstDataArg,
2887                               Type, CallType, InFunctionCall, CheckedVarArgs);
2888     return Left < Right ? Left : Right;
2889   }
2890 
2891   case Stmt::ImplicitCastExprClass: {
2892     E = cast<ImplicitCastExpr>(E)->getSubExpr();
2893     goto tryAgain;
2894   }
2895 
2896   case Stmt::OpaqueValueExprClass:
2897     if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) {
2898       E = src;
2899       goto tryAgain;
2900     }
2901     return SLCT_NotALiteral;
2902 
2903   case Stmt::PredefinedExprClass:
2904     // While __func__, etc., are technically not string literals, they
2905     // cannot contain format specifiers and thus are not a security
2906     // liability.
2907     return SLCT_UncheckedLiteral;
2908 
2909   case Stmt::DeclRefExprClass: {
2910     const DeclRefExpr *DR = cast<DeclRefExpr>(E);
2911 
2912     // As an exception, do not flag errors for variables binding to
2913     // const string literals.
2914     if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
2915       bool isConstant = false;
2916       QualType T = DR->getType();
2917 
2918       if (const ArrayType *AT = S.Context.getAsArrayType(T)) {
2919         isConstant = AT->getElementType().isConstant(S.Context);
2920       } else if (const PointerType *PT = T->getAs<PointerType>()) {
2921         isConstant = T.isConstant(S.Context) &&
2922                      PT->getPointeeType().isConstant(S.Context);
2923       } else if (T->isObjCObjectPointerType()) {
2924         // In ObjC, there is usually no "const ObjectPointer" type,
2925         // so don't check if the pointee type is constant.
2926         isConstant = T.isConstant(S.Context);
2927       }
2928 
2929       if (isConstant) {
2930         if (const Expr *Init = VD->getAnyInitializer()) {
2931           // Look through initializers like const char c[] = { "foo" }
2932           if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) {
2933             if (InitList->isStringLiteralInit())
2934               Init = InitList->getInit(0)->IgnoreParenImpCasts();
2935           }
2936           return checkFormatStringExpr(S, Init, Args,
2937                                        HasVAListArg, format_idx,
2938                                        firstDataArg, Type, CallType,
2939                                        /*InFunctionCall*/false, CheckedVarArgs);
2940         }
2941       }
2942 
2943       // For vprintf* functions (i.e., HasVAListArg==true), we add a
2944       // special check to see if the format string is a function parameter
2945       // of the function calling the printf function.  If the function
2946       // has an attribute indicating it is a printf-like function, then we
2947       // should suppress warnings concerning non-literals being used in a call
2948       // to a vprintf function.  For example:
2949       //
2950       // void
2951       // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){
2952       //      va_list ap;
2953       //      va_start(ap, fmt);
2954       //      vprintf(fmt, ap);  // Do NOT emit a warning about "fmt".
2955       //      ...
2956       // }
2957       if (HasVAListArg) {
2958         if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) {
2959           if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) {
2960             int PVIndex = PV->getFunctionScopeIndex() + 1;
2961             for (const auto *PVFormat : ND->specific_attrs<FormatAttr>()) {
2962               // adjust for implicit parameter
2963               if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND))
2964                 if (MD->isInstance())
2965                   ++PVIndex;
2966               // We also check if the formats are compatible.
2967               // We can't pass a 'scanf' string to a 'printf' function.
2968               if (PVIndex == PVFormat->getFormatIdx() &&
2969                   Type == S.GetFormatStringType(PVFormat))
2970                 return SLCT_UncheckedLiteral;
2971             }
2972           }
2973         }
2974       }
2975     }
2976 
2977     return SLCT_NotALiteral;
2978   }
2979 
2980   case Stmt::CallExprClass:
2981   case Stmt::CXXMemberCallExprClass: {
2982     const CallExpr *CE = cast<CallExpr>(E);
2983     if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {
2984       if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) {
2985         unsigned ArgIndex = FA->getFormatIdx();
2986         if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND))
2987           if (MD->isInstance())
2988             --ArgIndex;
2989         const Expr *Arg = CE->getArg(ArgIndex - 1);
2990 
2991         return checkFormatStringExpr(S, Arg, Args,
2992                                      HasVAListArg, format_idx, firstDataArg,
2993                                      Type, CallType, InFunctionCall,
2994                                      CheckedVarArgs);
2995       } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2996         unsigned BuiltinID = FD->getBuiltinID();
2997         if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString ||
2998             BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) {
2999           const Expr *Arg = CE->getArg(0);
3000           return checkFormatStringExpr(S, Arg, Args,
3001                                        HasVAListArg, format_idx,
3002                                        firstDataArg, Type, CallType,
3003                                        InFunctionCall, CheckedVarArgs);
3004         }
3005       }
3006     }
3007 
3008     return SLCT_NotALiteral;
3009   }
3010   case Stmt::ObjCStringLiteralClass:
3011   case Stmt::StringLiteralClass: {
3012     const StringLiteral *StrE = nullptr;
3013 
3014     if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E))
3015       StrE = ObjCFExpr->getString();
3016     else
3017       StrE = cast<StringLiteral>(E);
3018 
3019     if (StrE) {
3020       S.CheckFormatString(StrE, E, Args, HasVAListArg, format_idx, firstDataArg,
3021                           Type, InFunctionCall, CallType, CheckedVarArgs);
3022       return SLCT_CheckedLiteral;
3023     }
3024 
3025     return SLCT_NotALiteral;
3026   }
3027 
3028   default:
3029     return SLCT_NotALiteral;
3030   }
3031 }
3032 
3033 Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) {
3034   return llvm::StringSwitch<FormatStringType>(Format->getType()->getName())
3035   .Case("scanf", FST_Scanf)
3036   .Cases("printf", "printf0", FST_Printf)
3037   .Cases("NSString", "CFString", FST_NSString)
3038   .Case("strftime", FST_Strftime)
3039   .Case("strfmon", FST_Strfmon)
3040   .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf)
3041   .Case("freebsd_kprintf", FST_FreeBSDKPrintf)
3042   .Case("os_trace", FST_OSTrace)
3043   .Default(FST_Unknown);
3044 }
3045 
3046 /// CheckFormatArguments - Check calls to printf and scanf (and similar
3047 /// functions) for correct use of format strings.
3048 /// Returns true if a format string has been fully checked.
3049 bool Sema::CheckFormatArguments(const FormatAttr *Format,
3050                                 ArrayRef<const Expr *> Args,
3051                                 bool IsCXXMember,
3052                                 VariadicCallType CallType,
3053                                 SourceLocation Loc, SourceRange Range,
3054                                 llvm::SmallBitVector &CheckedVarArgs) {
3055   FormatStringInfo FSI;
3056   if (getFormatStringInfo(Format, IsCXXMember, &FSI))
3057     return CheckFormatArguments(Args, FSI.HasVAListArg, FSI.FormatIdx,
3058                                 FSI.FirstDataArg, GetFormatStringType(Format),
3059                                 CallType, Loc, Range, CheckedVarArgs);
3060   return false;
3061 }
3062 
3063 bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args,
3064                                 bool HasVAListArg, unsigned format_idx,
3065                                 unsigned firstDataArg, FormatStringType Type,
3066                                 VariadicCallType CallType,
3067                                 SourceLocation Loc, SourceRange Range,
3068                                 llvm::SmallBitVector &CheckedVarArgs) {
3069   // CHECK: printf/scanf-like function is called with no format string.
3070   if (format_idx >= Args.size()) {
3071     Diag(Loc, diag::warn_missing_format_string) << Range;
3072     return false;
3073   }
3074 
3075   const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts();
3076 
3077   // CHECK: format string is not a string literal.
3078   //
3079   // Dynamically generated format strings are difficult to
3080   // automatically vet at compile time.  Requiring that format strings
3081   // are string literals: (1) permits the checking of format strings by
3082   // the compiler and thereby (2) can practically remove the source of
3083   // many format string exploits.
3084 
3085   // Format string can be either ObjC string (e.g. @"%d") or
3086   // C string (e.g. "%d")
3087   // ObjC string uses the same format specifiers as C string, so we can use
3088   // the same format string checking logic for both ObjC and C strings.
3089   StringLiteralCheckType CT =
3090       checkFormatStringExpr(*this, OrigFormatExpr, Args, HasVAListArg,
3091                             format_idx, firstDataArg, Type, CallType,
3092                             /*IsFunctionCall*/true, CheckedVarArgs);
3093   if (CT != SLCT_NotALiteral)
3094     // Literal format string found, check done!
3095     return CT == SLCT_CheckedLiteral;
3096 
3097   // Strftime is particular as it always uses a single 'time' argument,
3098   // so it is safe to pass a non-literal string.
3099   if (Type == FST_Strftime)
3100     return false;
3101 
3102   // Do not emit diag when the string param is a macro expansion and the
3103   // format is either NSString or CFString. This is a hack to prevent
3104   // diag when using the NSLocalizedString and CFCopyLocalizedString macros
3105   // which are usually used in place of NS and CF string literals.
3106   if (Type == FST_NSString &&
3107       SourceMgr.isInSystemMacro(Args[format_idx]->getLocStart()))
3108     return false;
3109 
3110   // If there are no arguments specified, warn with -Wformat-security, otherwise
3111   // warn only with -Wformat-nonliteral.
3112   if (Args.size() == firstDataArg)
3113     Diag(Args[format_idx]->getLocStart(),
3114          diag::warn_format_nonliteral_noargs)
3115       << OrigFormatExpr->getSourceRange();
3116   else
3117     Diag(Args[format_idx]->getLocStart(),
3118          diag::warn_format_nonliteral)
3119            << OrigFormatExpr->getSourceRange();
3120   return false;
3121 }
3122 
3123 namespace {
3124 class CheckFormatHandler : public analyze_format_string::FormatStringHandler {
3125 protected:
3126   Sema &S;
3127   const StringLiteral *FExpr;
3128   const Expr *OrigFormatExpr;
3129   const unsigned FirstDataArg;
3130   const unsigned NumDataArgs;
3131   const char *Beg; // Start of format string.
3132   const bool HasVAListArg;
3133   ArrayRef<const Expr *> Args;
3134   unsigned FormatIdx;
3135   llvm::SmallBitVector CoveredArgs;
3136   bool usesPositionalArgs;
3137   bool atFirstArg;
3138   bool inFunctionCall;
3139   Sema::VariadicCallType CallType;
3140   llvm::SmallBitVector &CheckedVarArgs;
3141 public:
3142   CheckFormatHandler(Sema &s, const StringLiteral *fexpr,
3143                      const Expr *origFormatExpr, unsigned firstDataArg,
3144                      unsigned numDataArgs, const char *beg, bool hasVAListArg,
3145                      ArrayRef<const Expr *> Args,
3146                      unsigned formatIdx, bool inFunctionCall,
3147                      Sema::VariadicCallType callType,
3148                      llvm::SmallBitVector &CheckedVarArgs)
3149     : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr),
3150       FirstDataArg(firstDataArg), NumDataArgs(numDataArgs),
3151       Beg(beg), HasVAListArg(hasVAListArg),
3152       Args(Args), FormatIdx(formatIdx),
3153       usesPositionalArgs(false), atFirstArg(true),
3154       inFunctionCall(inFunctionCall), CallType(callType),
3155       CheckedVarArgs(CheckedVarArgs) {
3156     CoveredArgs.resize(numDataArgs);
3157     CoveredArgs.reset();
3158   }
3159 
3160   void DoneProcessing();
3161 
3162   void HandleIncompleteSpecifier(const char *startSpecifier,
3163                                  unsigned specifierLen) override;
3164 
3165   void HandleInvalidLengthModifier(
3166                            const analyze_format_string::FormatSpecifier &FS,
3167                            const analyze_format_string::ConversionSpecifier &CS,
3168                            const char *startSpecifier, unsigned specifierLen,
3169                            unsigned DiagID);
3170 
3171   void HandleNonStandardLengthModifier(
3172                     const analyze_format_string::FormatSpecifier &FS,
3173                     const char *startSpecifier, unsigned specifierLen);
3174 
3175   void HandleNonStandardConversionSpecifier(
3176                     const analyze_format_string::ConversionSpecifier &CS,
3177                     const char *startSpecifier, unsigned specifierLen);
3178 
3179   void HandlePosition(const char *startPos, unsigned posLen) override;
3180 
3181   void HandleInvalidPosition(const char *startSpecifier,
3182                              unsigned specifierLen,
3183                              analyze_format_string::PositionContext p) override;
3184 
3185   void HandleZeroPosition(const char *startPos, unsigned posLen) override;
3186 
3187   void HandleNullChar(const char *nullCharacter) override;
3188 
3189   template <typename Range>
3190   static void EmitFormatDiagnostic(Sema &S, bool inFunctionCall,
3191                                    const Expr *ArgumentExpr,
3192                                    PartialDiagnostic PDiag,
3193                                    SourceLocation StringLoc,
3194                                    bool IsStringLocation, Range StringRange,
3195                                    ArrayRef<FixItHint> Fixit = None);
3196 
3197 protected:
3198   bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc,
3199                                         const char *startSpec,
3200                                         unsigned specifierLen,
3201                                         const char *csStart, unsigned csLen);
3202 
3203   void HandlePositionalNonpositionalArgs(SourceLocation Loc,
3204                                          const char *startSpec,
3205                                          unsigned specifierLen);
3206 
3207   SourceRange getFormatStringRange();
3208   CharSourceRange getSpecifierRange(const char *startSpecifier,
3209                                     unsigned specifierLen);
3210   SourceLocation getLocationOfByte(const char *x);
3211 
3212   const Expr *getDataArg(unsigned i) const;
3213 
3214   bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS,
3215                     const analyze_format_string::ConversionSpecifier &CS,
3216                     const char *startSpecifier, unsigned specifierLen,
3217                     unsigned argIndex);
3218 
3219   template <typename Range>
3220   void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc,
3221                             bool IsStringLocation, Range StringRange,
3222                             ArrayRef<FixItHint> Fixit = None);
3223 };
3224 }
3225 
3226 SourceRange CheckFormatHandler::getFormatStringRange() {
3227   return OrigFormatExpr->getSourceRange();
3228 }
3229 
3230 CharSourceRange CheckFormatHandler::
3231 getSpecifierRange(const char *startSpecifier, unsigned specifierLen) {
3232   SourceLocation Start = getLocationOfByte(startSpecifier);
3233   SourceLocation End   = getLocationOfByte(startSpecifier + specifierLen - 1);
3234 
3235   // Advance the end SourceLocation by one due to half-open ranges.
3236   End = End.getLocWithOffset(1);
3237 
3238   return CharSourceRange::getCharRange(Start, End);
3239 }
3240 
3241 SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) {
3242   return S.getLocationOfStringLiteralByte(FExpr, x - Beg);
3243 }
3244 
3245 void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier,
3246                                                    unsigned specifierLen){
3247   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier),
3248                        getLocationOfByte(startSpecifier),
3249                        /*IsStringLocation*/true,
3250                        getSpecifierRange(startSpecifier, specifierLen));
3251 }
3252 
3253 void CheckFormatHandler::HandleInvalidLengthModifier(
3254     const analyze_format_string::FormatSpecifier &FS,
3255     const analyze_format_string::ConversionSpecifier &CS,
3256     const char *startSpecifier, unsigned specifierLen, unsigned DiagID) {
3257   using namespace analyze_format_string;
3258 
3259   const LengthModifier &LM = FS.getLengthModifier();
3260   CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength());
3261 
3262   // See if we know how to fix this length modifier.
3263   Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier();
3264   if (FixedLM) {
3265     EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(),
3266                          getLocationOfByte(LM.getStart()),
3267                          /*IsStringLocation*/true,
3268                          getSpecifierRange(startSpecifier, specifierLen));
3269 
3270     S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier)
3271       << FixedLM->toString()
3272       << FixItHint::CreateReplacement(LMRange, FixedLM->toString());
3273 
3274   } else {
3275     FixItHint Hint;
3276     if (DiagID == diag::warn_format_nonsensical_length)
3277       Hint = FixItHint::CreateRemoval(LMRange);
3278 
3279     EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(),
3280                          getLocationOfByte(LM.getStart()),
3281                          /*IsStringLocation*/true,
3282                          getSpecifierRange(startSpecifier, specifierLen),
3283                          Hint);
3284   }
3285 }
3286 
3287 void CheckFormatHandler::HandleNonStandardLengthModifier(
3288     const analyze_format_string::FormatSpecifier &FS,
3289     const char *startSpecifier, unsigned specifierLen) {
3290   using namespace analyze_format_string;
3291 
3292   const LengthModifier &LM = FS.getLengthModifier();
3293   CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength());
3294 
3295   // See if we know how to fix this length modifier.
3296   Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier();
3297   if (FixedLM) {
3298     EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
3299                            << LM.toString() << 0,
3300                          getLocationOfByte(LM.getStart()),
3301                          /*IsStringLocation*/true,
3302                          getSpecifierRange(startSpecifier, specifierLen));
3303 
3304     S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier)
3305       << FixedLM->toString()
3306       << FixItHint::CreateReplacement(LMRange, FixedLM->toString());
3307 
3308   } else {
3309     EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
3310                            << LM.toString() << 0,
3311                          getLocationOfByte(LM.getStart()),
3312                          /*IsStringLocation*/true,
3313                          getSpecifierRange(startSpecifier, specifierLen));
3314   }
3315 }
3316 
3317 void CheckFormatHandler::HandleNonStandardConversionSpecifier(
3318     const analyze_format_string::ConversionSpecifier &CS,
3319     const char *startSpecifier, unsigned specifierLen) {
3320   using namespace analyze_format_string;
3321 
3322   // See if we know how to fix this conversion specifier.
3323   Optional<ConversionSpecifier> FixedCS = CS.getStandardSpecifier();
3324   if (FixedCS) {
3325     EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
3326                           << CS.toString() << /*conversion specifier*/1,
3327                          getLocationOfByte(CS.getStart()),
3328                          /*IsStringLocation*/true,
3329                          getSpecifierRange(startSpecifier, specifierLen));
3330 
3331     CharSourceRange CSRange = getSpecifierRange(CS.getStart(), CS.getLength());
3332     S.Diag(getLocationOfByte(CS.getStart()), diag::note_format_fix_specifier)
3333       << FixedCS->toString()
3334       << FixItHint::CreateReplacement(CSRange, FixedCS->toString());
3335   } else {
3336     EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
3337                           << CS.toString() << /*conversion specifier*/1,
3338                          getLocationOfByte(CS.getStart()),
3339                          /*IsStringLocation*/true,
3340                          getSpecifierRange(startSpecifier, specifierLen));
3341   }
3342 }
3343 
3344 void CheckFormatHandler::HandlePosition(const char *startPos,
3345                                         unsigned posLen) {
3346   EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg),
3347                                getLocationOfByte(startPos),
3348                                /*IsStringLocation*/true,
3349                                getSpecifierRange(startPos, posLen));
3350 }
3351 
3352 void
3353 CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen,
3354                                      analyze_format_string::PositionContext p) {
3355   EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier)
3356                          << (unsigned) p,
3357                        getLocationOfByte(startPos), /*IsStringLocation*/true,
3358                        getSpecifierRange(startPos, posLen));
3359 }
3360 
3361 void CheckFormatHandler::HandleZeroPosition(const char *startPos,
3362                                             unsigned posLen) {
3363   EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier),
3364                                getLocationOfByte(startPos),
3365                                /*IsStringLocation*/true,
3366                                getSpecifierRange(startPos, posLen));
3367 }
3368 
3369 void CheckFormatHandler::HandleNullChar(const char *nullCharacter) {
3370   if (!isa<ObjCStringLiteral>(OrigFormatExpr)) {
3371     // The presence of a null character is likely an error.
3372     EmitFormatDiagnostic(
3373       S.PDiag(diag::warn_printf_format_string_contains_null_char),
3374       getLocationOfByte(nullCharacter), /*IsStringLocation*/true,
3375       getFormatStringRange());
3376   }
3377 }
3378 
3379 // Note that this may return NULL if there was an error parsing or building
3380 // one of the argument expressions.
3381 const Expr *CheckFormatHandler::getDataArg(unsigned i) const {
3382   return Args[FirstDataArg + i];
3383 }
3384 
3385 void CheckFormatHandler::DoneProcessing() {
3386     // Does the number of data arguments exceed the number of
3387     // format conversions in the format string?
3388   if (!HasVAListArg) {
3389       // Find any arguments that weren't covered.
3390     CoveredArgs.flip();
3391     signed notCoveredArg = CoveredArgs.find_first();
3392     if (notCoveredArg >= 0) {
3393       assert((unsigned)notCoveredArg < NumDataArgs);
3394       if (const Expr *E = getDataArg((unsigned) notCoveredArg)) {
3395         SourceLocation Loc = E->getLocStart();
3396         if (!S.getSourceManager().isInSystemMacro(Loc)) {
3397           EmitFormatDiagnostic(S.PDiag(diag::warn_printf_data_arg_not_used),
3398                                Loc, /*IsStringLocation*/false,
3399                                getFormatStringRange());
3400         }
3401       }
3402     }
3403   }
3404 }
3405 
3406 bool
3407 CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex,
3408                                                      SourceLocation Loc,
3409                                                      const char *startSpec,
3410                                                      unsigned specifierLen,
3411                                                      const char *csStart,
3412                                                      unsigned csLen) {
3413 
3414   bool keepGoing = true;
3415   if (argIndex < NumDataArgs) {
3416     // Consider the argument coverered, even though the specifier doesn't
3417     // make sense.
3418     CoveredArgs.set(argIndex);
3419   }
3420   else {
3421     // If argIndex exceeds the number of data arguments we
3422     // don't issue a warning because that is just a cascade of warnings (and
3423     // they may have intended '%%' anyway). We don't want to continue processing
3424     // the format string after this point, however, as we will like just get
3425     // gibberish when trying to match arguments.
3426     keepGoing = false;
3427   }
3428 
3429   EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_conversion)
3430                          << StringRef(csStart, csLen),
3431                        Loc, /*IsStringLocation*/true,
3432                        getSpecifierRange(startSpec, specifierLen));
3433 
3434   return keepGoing;
3435 }
3436 
3437 void
3438 CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc,
3439                                                       const char *startSpec,
3440                                                       unsigned specifierLen) {
3441   EmitFormatDiagnostic(
3442     S.PDiag(diag::warn_format_mix_positional_nonpositional_args),
3443     Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen));
3444 }
3445 
3446 bool
3447 CheckFormatHandler::CheckNumArgs(
3448   const analyze_format_string::FormatSpecifier &FS,
3449   const analyze_format_string::ConversionSpecifier &CS,
3450   const char *startSpecifier, unsigned specifierLen, unsigned argIndex) {
3451 
3452   if (argIndex >= NumDataArgs) {
3453     PartialDiagnostic PDiag = FS.usesPositionalArg()
3454       ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args)
3455            << (argIndex+1) << NumDataArgs)
3456       : S.PDiag(diag::warn_printf_insufficient_data_args);
3457     EmitFormatDiagnostic(
3458       PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true,
3459       getSpecifierRange(startSpecifier, specifierLen));
3460     return false;
3461   }
3462   return true;
3463 }
3464 
3465 template<typename Range>
3466 void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag,
3467                                               SourceLocation Loc,
3468                                               bool IsStringLocation,
3469                                               Range StringRange,
3470                                               ArrayRef<FixItHint> FixIt) {
3471   EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag,
3472                        Loc, IsStringLocation, StringRange, FixIt);
3473 }
3474 
3475 /// \brief If the format string is not within the funcion call, emit a note
3476 /// so that the function call and string are in diagnostic messages.
3477 ///
3478 /// \param InFunctionCall if true, the format string is within the function
3479 /// call and only one diagnostic message will be produced.  Otherwise, an
3480 /// extra note will be emitted pointing to location of the format string.
3481 ///
3482 /// \param ArgumentExpr the expression that is passed as the format string
3483 /// argument in the function call.  Used for getting locations when two
3484 /// diagnostics are emitted.
3485 ///
3486 /// \param PDiag the callee should already have provided any strings for the
3487 /// diagnostic message.  This function only adds locations and fixits
3488 /// to diagnostics.
3489 ///
3490 /// \param Loc primary location for diagnostic.  If two diagnostics are
3491 /// required, one will be at Loc and a new SourceLocation will be created for
3492 /// the other one.
3493 ///
3494 /// \param IsStringLocation if true, Loc points to the format string should be
3495 /// used for the note.  Otherwise, Loc points to the argument list and will
3496 /// be used with PDiag.
3497 ///
3498 /// \param StringRange some or all of the string to highlight.  This is
3499 /// templated so it can accept either a CharSourceRange or a SourceRange.
3500 ///
3501 /// \param FixIt optional fix it hint for the format string.
3502 template<typename Range>
3503 void CheckFormatHandler::EmitFormatDiagnostic(Sema &S, bool InFunctionCall,
3504                                               const Expr *ArgumentExpr,
3505                                               PartialDiagnostic PDiag,
3506                                               SourceLocation Loc,
3507                                               bool IsStringLocation,
3508                                               Range StringRange,
3509                                               ArrayRef<FixItHint> FixIt) {
3510   if (InFunctionCall) {
3511     const Sema::SemaDiagnosticBuilder &D = S.Diag(Loc, PDiag);
3512     D << StringRange;
3513     D << FixIt;
3514   } else {
3515     S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag)
3516       << ArgumentExpr->getSourceRange();
3517 
3518     const Sema::SemaDiagnosticBuilder &Note =
3519       S.Diag(IsStringLocation ? Loc : StringRange.getBegin(),
3520              diag::note_format_string_defined);
3521 
3522     Note << StringRange;
3523     Note << FixIt;
3524   }
3525 }
3526 
3527 //===--- CHECK: Printf format string checking ------------------------------===//
3528 
3529 namespace {
3530 class CheckPrintfHandler : public CheckFormatHandler {
3531   bool ObjCContext;
3532 public:
3533   CheckPrintfHandler(Sema &s, const StringLiteral *fexpr,
3534                      const Expr *origFormatExpr, unsigned firstDataArg,
3535                      unsigned numDataArgs, bool isObjC,
3536                      const char *beg, bool hasVAListArg,
3537                      ArrayRef<const Expr *> Args,
3538                      unsigned formatIdx, bool inFunctionCall,
3539                      Sema::VariadicCallType CallType,
3540                      llvm::SmallBitVector &CheckedVarArgs)
3541     : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg,
3542                          numDataArgs, beg, hasVAListArg, Args,
3543                          formatIdx, inFunctionCall, CallType, CheckedVarArgs),
3544       ObjCContext(isObjC)
3545   {}
3546 
3547 
3548   bool HandleInvalidPrintfConversionSpecifier(
3549                                       const analyze_printf::PrintfSpecifier &FS,
3550                                       const char *startSpecifier,
3551                                       unsigned specifierLen) override;
3552 
3553   bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS,
3554                              const char *startSpecifier,
3555                              unsigned specifierLen) override;
3556   bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
3557                        const char *StartSpecifier,
3558                        unsigned SpecifierLen,
3559                        const Expr *E);
3560 
3561   bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k,
3562                     const char *startSpecifier, unsigned specifierLen);
3563   void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS,
3564                            const analyze_printf::OptionalAmount &Amt,
3565                            unsigned type,
3566                            const char *startSpecifier, unsigned specifierLen);
3567   void HandleFlag(const analyze_printf::PrintfSpecifier &FS,
3568                   const analyze_printf::OptionalFlag &flag,
3569                   const char *startSpecifier, unsigned specifierLen);
3570   void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS,
3571                          const analyze_printf::OptionalFlag &ignoredFlag,
3572                          const analyze_printf::OptionalFlag &flag,
3573                          const char *startSpecifier, unsigned specifierLen);
3574   bool checkForCStrMembers(const analyze_printf::ArgType &AT,
3575                            const Expr *E);
3576 
3577   void HandleEmptyObjCModifierFlag(const char *startFlag,
3578                                    unsigned flagLen) override;
3579 
3580   void HandleInvalidObjCModifierFlag(const char *startFlag,
3581                                             unsigned flagLen) override;
3582 
3583   void HandleObjCFlagsWithNonObjCConversion(const char *flagsStart,
3584                                            const char *flagsEnd,
3585                                            const char *conversionPosition)
3586                                              override;
3587 };
3588 }
3589 
3590 bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier(
3591                                       const analyze_printf::PrintfSpecifier &FS,
3592                                       const char *startSpecifier,
3593                                       unsigned specifierLen) {
3594   const analyze_printf::PrintfConversionSpecifier &CS =
3595     FS.getConversionSpecifier();
3596 
3597   return HandleInvalidConversionSpecifier(FS.getArgIndex(),
3598                                           getLocationOfByte(CS.getStart()),
3599                                           startSpecifier, specifierLen,
3600                                           CS.getStart(), CS.getLength());
3601 }
3602 
3603 bool CheckPrintfHandler::HandleAmount(
3604                                const analyze_format_string::OptionalAmount &Amt,
3605                                unsigned k, const char *startSpecifier,
3606                                unsigned specifierLen) {
3607 
3608   if (Amt.hasDataArgument()) {
3609     if (!HasVAListArg) {
3610       unsigned argIndex = Amt.getArgIndex();
3611       if (argIndex >= NumDataArgs) {
3612         EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg)
3613                                << k,
3614                              getLocationOfByte(Amt.getStart()),
3615                              /*IsStringLocation*/true,
3616                              getSpecifierRange(startSpecifier, specifierLen));
3617         // Don't do any more checking.  We will just emit
3618         // spurious errors.
3619         return false;
3620       }
3621 
3622       // Type check the data argument.  It should be an 'int'.
3623       // Although not in conformance with C99, we also allow the argument to be
3624       // an 'unsigned int' as that is a reasonably safe case.  GCC also
3625       // doesn't emit a warning for that case.
3626       CoveredArgs.set(argIndex);
3627       const Expr *Arg = getDataArg(argIndex);
3628       if (!Arg)
3629         return false;
3630 
3631       QualType T = Arg->getType();
3632 
3633       const analyze_printf::ArgType &AT = Amt.getArgType(S.Context);
3634       assert(AT.isValid());
3635 
3636       if (!AT.matchesType(S.Context, T)) {
3637         EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type)
3638                                << k << AT.getRepresentativeTypeName(S.Context)
3639                                << T << Arg->getSourceRange(),
3640                              getLocationOfByte(Amt.getStart()),
3641                              /*IsStringLocation*/true,
3642                              getSpecifierRange(startSpecifier, specifierLen));
3643         // Don't do any more checking.  We will just emit
3644         // spurious errors.
3645         return false;
3646       }
3647     }
3648   }
3649   return true;
3650 }
3651 
3652 void CheckPrintfHandler::HandleInvalidAmount(
3653                                       const analyze_printf::PrintfSpecifier &FS,
3654                                       const analyze_printf::OptionalAmount &Amt,
3655                                       unsigned type,
3656                                       const char *startSpecifier,
3657                                       unsigned specifierLen) {
3658   const analyze_printf::PrintfConversionSpecifier &CS =
3659     FS.getConversionSpecifier();
3660 
3661   FixItHint fixit =
3662     Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant
3663       ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(),
3664                                  Amt.getConstantLength()))
3665       : FixItHint();
3666 
3667   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount)
3668                          << type << CS.toString(),
3669                        getLocationOfByte(Amt.getStart()),
3670                        /*IsStringLocation*/true,
3671                        getSpecifierRange(startSpecifier, specifierLen),
3672                        fixit);
3673 }
3674 
3675 void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS,
3676                                     const analyze_printf::OptionalFlag &flag,
3677                                     const char *startSpecifier,
3678                                     unsigned specifierLen) {
3679   // Warn about pointless flag with a fixit removal.
3680   const analyze_printf::PrintfConversionSpecifier &CS =
3681     FS.getConversionSpecifier();
3682   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag)
3683                          << flag.toString() << CS.toString(),
3684                        getLocationOfByte(flag.getPosition()),
3685                        /*IsStringLocation*/true,
3686                        getSpecifierRange(startSpecifier, specifierLen),
3687                        FixItHint::CreateRemoval(
3688                          getSpecifierRange(flag.getPosition(), 1)));
3689 }
3690 
3691 void CheckPrintfHandler::HandleIgnoredFlag(
3692                                 const analyze_printf::PrintfSpecifier &FS,
3693                                 const analyze_printf::OptionalFlag &ignoredFlag,
3694                                 const analyze_printf::OptionalFlag &flag,
3695                                 const char *startSpecifier,
3696                                 unsigned specifierLen) {
3697   // Warn about ignored flag with a fixit removal.
3698   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag)
3699                          << ignoredFlag.toString() << flag.toString(),
3700                        getLocationOfByte(ignoredFlag.getPosition()),
3701                        /*IsStringLocation*/true,
3702                        getSpecifierRange(startSpecifier, specifierLen),
3703                        FixItHint::CreateRemoval(
3704                          getSpecifierRange(ignoredFlag.getPosition(), 1)));
3705 }
3706 
3707 //  void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc,
3708 //                            bool IsStringLocation, Range StringRange,
3709 //                            ArrayRef<FixItHint> Fixit = None);
3710 
3711 void CheckPrintfHandler::HandleEmptyObjCModifierFlag(const char *startFlag,
3712                                                      unsigned flagLen) {
3713   // Warn about an empty flag.
3714   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_empty_objc_flag),
3715                        getLocationOfByte(startFlag),
3716                        /*IsStringLocation*/true,
3717                        getSpecifierRange(startFlag, flagLen));
3718 }
3719 
3720 void CheckPrintfHandler::HandleInvalidObjCModifierFlag(const char *startFlag,
3721                                                        unsigned flagLen) {
3722   // Warn about an invalid flag.
3723   auto Range = getSpecifierRange(startFlag, flagLen);
3724   StringRef flag(startFlag, flagLen);
3725   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_invalid_objc_flag) << flag,
3726                       getLocationOfByte(startFlag),
3727                       /*IsStringLocation*/true,
3728                       Range, FixItHint::CreateRemoval(Range));
3729 }
3730 
3731 void CheckPrintfHandler::HandleObjCFlagsWithNonObjCConversion(
3732     const char *flagsStart, const char *flagsEnd, const char *conversionPosition) {
3733     // Warn about using '[...]' without a '@' conversion.
3734     auto Range = getSpecifierRange(flagsStart, flagsEnd - flagsStart + 1);
3735     auto diag = diag::warn_printf_ObjCflags_without_ObjCConversion;
3736     EmitFormatDiagnostic(S.PDiag(diag) << StringRef(conversionPosition, 1),
3737                          getLocationOfByte(conversionPosition),
3738                          /*IsStringLocation*/true,
3739                          Range, FixItHint::CreateRemoval(Range));
3740 }
3741 
3742 // Determines if the specified is a C++ class or struct containing
3743 // a member with the specified name and kind (e.g. a CXXMethodDecl named
3744 // "c_str()").
3745 template<typename MemberKind>
3746 static llvm::SmallPtrSet<MemberKind*, 1>
3747 CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) {
3748   const RecordType *RT = Ty->getAs<RecordType>();
3749   llvm::SmallPtrSet<MemberKind*, 1> Results;
3750 
3751   if (!RT)
3752     return Results;
3753   const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
3754   if (!RD || !RD->getDefinition())
3755     return Results;
3756 
3757   LookupResult R(S, &S.Context.Idents.get(Name), SourceLocation(),
3758                  Sema::LookupMemberName);
3759   R.suppressDiagnostics();
3760 
3761   // We just need to include all members of the right kind turned up by the
3762   // filter, at this point.
3763   if (S.LookupQualifiedName(R, RT->getDecl()))
3764     for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
3765       NamedDecl *decl = (*I)->getUnderlyingDecl();
3766       if (MemberKind *FK = dyn_cast<MemberKind>(decl))
3767         Results.insert(FK);
3768     }
3769   return Results;
3770 }
3771 
3772 /// Check if we could call '.c_str()' on an object.
3773 ///
3774 /// FIXME: This returns the wrong results in some cases (if cv-qualifiers don't
3775 /// allow the call, or if it would be ambiguous).
3776 bool Sema::hasCStrMethod(const Expr *E) {
3777   typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet;
3778   MethodSet Results =
3779       CXXRecordMembersNamed<CXXMethodDecl>("c_str", *this, E->getType());
3780   for (MethodSet::iterator MI = Results.begin(), ME = Results.end();
3781        MI != ME; ++MI)
3782     if ((*MI)->getMinRequiredArguments() == 0)
3783       return true;
3784   return false;
3785 }
3786 
3787 // Check if a (w)string was passed when a (w)char* was needed, and offer a
3788 // better diagnostic if so. AT is assumed to be valid.
3789 // Returns true when a c_str() conversion method is found.
3790 bool CheckPrintfHandler::checkForCStrMembers(
3791     const analyze_printf::ArgType &AT, const Expr *E) {
3792   typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet;
3793 
3794   MethodSet Results =
3795       CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType());
3796 
3797   for (MethodSet::iterator MI = Results.begin(), ME = Results.end();
3798        MI != ME; ++MI) {
3799     const CXXMethodDecl *Method = *MI;
3800     if (Method->getMinRequiredArguments() == 0 &&
3801         AT.matchesType(S.Context, Method->getReturnType())) {
3802       // FIXME: Suggest parens if the expression needs them.
3803       SourceLocation EndLoc = S.getLocForEndOfToken(E->getLocEnd());
3804       S.Diag(E->getLocStart(), diag::note_printf_c_str)
3805           << "c_str()"
3806           << FixItHint::CreateInsertion(EndLoc, ".c_str()");
3807       return true;
3808     }
3809   }
3810 
3811   return false;
3812 }
3813 
3814 bool
3815 CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier
3816                                             &FS,
3817                                           const char *startSpecifier,
3818                                           unsigned specifierLen) {
3819 
3820   using namespace analyze_format_string;
3821   using namespace analyze_printf;
3822   const PrintfConversionSpecifier &CS = FS.getConversionSpecifier();
3823 
3824   if (FS.consumesDataArgument()) {
3825     if (atFirstArg) {
3826         atFirstArg = false;
3827         usesPositionalArgs = FS.usesPositionalArg();
3828     }
3829     else if (usesPositionalArgs != FS.usesPositionalArg()) {
3830       HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()),
3831                                         startSpecifier, specifierLen);
3832       return false;
3833     }
3834   }
3835 
3836   // First check if the field width, precision, and conversion specifier
3837   // have matching data arguments.
3838   if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0,
3839                     startSpecifier, specifierLen)) {
3840     return false;
3841   }
3842 
3843   if (!HandleAmount(FS.getPrecision(), /* precision */ 1,
3844                     startSpecifier, specifierLen)) {
3845     return false;
3846   }
3847 
3848   if (!CS.consumesDataArgument()) {
3849     // FIXME: Technically specifying a precision or field width here
3850     // makes no sense.  Worth issuing a warning at some point.
3851     return true;
3852   }
3853 
3854   // Consume the argument.
3855   unsigned argIndex = FS.getArgIndex();
3856   if (argIndex < NumDataArgs) {
3857     // The check to see if the argIndex is valid will come later.
3858     // We set the bit here because we may exit early from this
3859     // function if we encounter some other error.
3860     CoveredArgs.set(argIndex);
3861   }
3862 
3863   // FreeBSD kernel extensions.
3864   if (CS.getKind() == ConversionSpecifier::FreeBSDbArg ||
3865       CS.getKind() == ConversionSpecifier::FreeBSDDArg) {
3866     // We need at least two arguments.
3867     if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex + 1))
3868       return false;
3869 
3870     // Claim the second argument.
3871     CoveredArgs.set(argIndex + 1);
3872 
3873     // Type check the first argument (int for %b, pointer for %D)
3874     const Expr *Ex = getDataArg(argIndex);
3875     const analyze_printf::ArgType &AT =
3876       (CS.getKind() == ConversionSpecifier::FreeBSDbArg) ?
3877         ArgType(S.Context.IntTy) : ArgType::CPointerTy;
3878     if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType()))
3879       EmitFormatDiagnostic(
3880         S.PDiag(diag::warn_format_conversion_argument_type_mismatch)
3881         << AT.getRepresentativeTypeName(S.Context) << Ex->getType()
3882         << false << Ex->getSourceRange(),
3883         Ex->getLocStart(), /*IsStringLocation*/false,
3884         getSpecifierRange(startSpecifier, specifierLen));
3885 
3886     // Type check the second argument (char * for both %b and %D)
3887     Ex = getDataArg(argIndex + 1);
3888     const analyze_printf::ArgType &AT2 = ArgType::CStrTy;
3889     if (AT2.isValid() && !AT2.matchesType(S.Context, Ex->getType()))
3890       EmitFormatDiagnostic(
3891         S.PDiag(diag::warn_format_conversion_argument_type_mismatch)
3892         << AT2.getRepresentativeTypeName(S.Context) << Ex->getType()
3893         << false << Ex->getSourceRange(),
3894         Ex->getLocStart(), /*IsStringLocation*/false,
3895         getSpecifierRange(startSpecifier, specifierLen));
3896 
3897      return true;
3898   }
3899 
3900   // Check for using an Objective-C specific conversion specifier
3901   // in a non-ObjC literal.
3902   if (!ObjCContext && CS.isObjCArg()) {
3903     return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier,
3904                                                   specifierLen);
3905   }
3906 
3907   // Check for invalid use of field width
3908   if (!FS.hasValidFieldWidth()) {
3909     HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0,
3910         startSpecifier, specifierLen);
3911   }
3912 
3913   // Check for invalid use of precision
3914   if (!FS.hasValidPrecision()) {
3915     HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1,
3916         startSpecifier, specifierLen);
3917   }
3918 
3919   // Check each flag does not conflict with any other component.
3920   if (!FS.hasValidThousandsGroupingPrefix())
3921     HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen);
3922   if (!FS.hasValidLeadingZeros())
3923     HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen);
3924   if (!FS.hasValidPlusPrefix())
3925     HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen);
3926   if (!FS.hasValidSpacePrefix())
3927     HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen);
3928   if (!FS.hasValidAlternativeForm())
3929     HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen);
3930   if (!FS.hasValidLeftJustified())
3931     HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen);
3932 
3933   // Check that flags are not ignored by another flag
3934   if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+'
3935     HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(),
3936         startSpecifier, specifierLen);
3937   if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-'
3938     HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(),
3939             startSpecifier, specifierLen);
3940 
3941   // Check the length modifier is valid with the given conversion specifier.
3942   if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo()))
3943     HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
3944                                 diag::warn_format_nonsensical_length);
3945   else if (!FS.hasStandardLengthModifier())
3946     HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen);
3947   else if (!FS.hasStandardLengthConversionCombination())
3948     HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
3949                                 diag::warn_format_non_standard_conversion_spec);
3950 
3951   if (!FS.hasStandardConversionSpecifier(S.getLangOpts()))
3952     HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen);
3953 
3954   // The remaining checks depend on the data arguments.
3955   if (HasVAListArg)
3956     return true;
3957 
3958   if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
3959     return false;
3960 
3961   const Expr *Arg = getDataArg(argIndex);
3962   if (!Arg)
3963     return true;
3964 
3965   return checkFormatExpr(FS, startSpecifier, specifierLen, Arg);
3966 }
3967 
3968 static bool requiresParensToAddCast(const Expr *E) {
3969   // FIXME: We should have a general way to reason about operator
3970   // precedence and whether parens are actually needed here.
3971   // Take care of a few common cases where they aren't.
3972   const Expr *Inside = E->IgnoreImpCasts();
3973   if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Inside))
3974     Inside = POE->getSyntacticForm()->IgnoreImpCasts();
3975 
3976   switch (Inside->getStmtClass()) {
3977   case Stmt::ArraySubscriptExprClass:
3978   case Stmt::CallExprClass:
3979   case Stmt::CharacterLiteralClass:
3980   case Stmt::CXXBoolLiteralExprClass:
3981   case Stmt::DeclRefExprClass:
3982   case Stmt::FloatingLiteralClass:
3983   case Stmt::IntegerLiteralClass:
3984   case Stmt::MemberExprClass:
3985   case Stmt::ObjCArrayLiteralClass:
3986   case Stmt::ObjCBoolLiteralExprClass:
3987   case Stmt::ObjCBoxedExprClass:
3988   case Stmt::ObjCDictionaryLiteralClass:
3989   case Stmt::ObjCEncodeExprClass:
3990   case Stmt::ObjCIvarRefExprClass:
3991   case Stmt::ObjCMessageExprClass:
3992   case Stmt::ObjCPropertyRefExprClass:
3993   case Stmt::ObjCStringLiteralClass:
3994   case Stmt::ObjCSubscriptRefExprClass:
3995   case Stmt::ParenExprClass:
3996   case Stmt::StringLiteralClass:
3997   case Stmt::UnaryOperatorClass:
3998     return false;
3999   default:
4000     return true;
4001   }
4002 }
4003 
4004 static std::pair<QualType, StringRef>
4005 shouldNotPrintDirectly(const ASTContext &Context,
4006                        QualType IntendedTy,
4007                        const Expr *E) {
4008   // Use a 'while' to peel off layers of typedefs.
4009   QualType TyTy = IntendedTy;
4010   while (const TypedefType *UserTy = TyTy->getAs<TypedefType>()) {
4011     StringRef Name = UserTy->getDecl()->getName();
4012     QualType CastTy = llvm::StringSwitch<QualType>(Name)
4013       .Case("NSInteger", Context.LongTy)
4014       .Case("NSUInteger", Context.UnsignedLongTy)
4015       .Case("SInt32", Context.IntTy)
4016       .Case("UInt32", Context.UnsignedIntTy)
4017       .Default(QualType());
4018 
4019     if (!CastTy.isNull())
4020       return std::make_pair(CastTy, Name);
4021 
4022     TyTy = UserTy->desugar();
4023   }
4024 
4025   // Strip parens if necessary.
4026   if (const ParenExpr *PE = dyn_cast<ParenExpr>(E))
4027     return shouldNotPrintDirectly(Context,
4028                                   PE->getSubExpr()->getType(),
4029                                   PE->getSubExpr());
4030 
4031   // If this is a conditional expression, then its result type is constructed
4032   // via usual arithmetic conversions and thus there might be no necessary
4033   // typedef sugar there.  Recurse to operands to check for NSInteger &
4034   // Co. usage condition.
4035   if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
4036     QualType TrueTy, FalseTy;
4037     StringRef TrueName, FalseName;
4038 
4039     std::tie(TrueTy, TrueName) =
4040       shouldNotPrintDirectly(Context,
4041                              CO->getTrueExpr()->getType(),
4042                              CO->getTrueExpr());
4043     std::tie(FalseTy, FalseName) =
4044       shouldNotPrintDirectly(Context,
4045                              CO->getFalseExpr()->getType(),
4046                              CO->getFalseExpr());
4047 
4048     if (TrueTy == FalseTy)
4049       return std::make_pair(TrueTy, TrueName);
4050     else if (TrueTy.isNull())
4051       return std::make_pair(FalseTy, FalseName);
4052     else if (FalseTy.isNull())
4053       return std::make_pair(TrueTy, TrueName);
4054   }
4055 
4056   return std::make_pair(QualType(), StringRef());
4057 }
4058 
4059 bool
4060 CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
4061                                     const char *StartSpecifier,
4062                                     unsigned SpecifierLen,
4063                                     const Expr *E) {
4064   using namespace analyze_format_string;
4065   using namespace analyze_printf;
4066   // Now type check the data expression that matches the
4067   // format specifier.
4068   const analyze_printf::ArgType &AT = FS.getArgType(S.Context,
4069                                                     ObjCContext);
4070   if (!AT.isValid())
4071     return true;
4072 
4073   QualType ExprTy = E->getType();
4074   while (const TypeOfExprType *TET = dyn_cast<TypeOfExprType>(ExprTy)) {
4075     ExprTy = TET->getUnderlyingExpr()->getType();
4076   }
4077 
4078   analyze_printf::ArgType::MatchKind match = AT.matchesType(S.Context, ExprTy);
4079 
4080   if (match == analyze_printf::ArgType::Match) {
4081     return true;
4082   }
4083 
4084   // Look through argument promotions for our error message's reported type.
4085   // This includes the integral and floating promotions, but excludes array
4086   // and function pointer decay; seeing that an argument intended to be a
4087   // string has type 'char [6]' is probably more confusing than 'char *'.
4088   if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
4089     if (ICE->getCastKind() == CK_IntegralCast ||
4090         ICE->getCastKind() == CK_FloatingCast) {
4091       E = ICE->getSubExpr();
4092       ExprTy = E->getType();
4093 
4094       // Check if we didn't match because of an implicit cast from a 'char'
4095       // or 'short' to an 'int'.  This is done because printf is a varargs
4096       // function.
4097       if (ICE->getType() == S.Context.IntTy ||
4098           ICE->getType() == S.Context.UnsignedIntTy) {
4099         // All further checking is done on the subexpression.
4100         if (AT.matchesType(S.Context, ExprTy))
4101           return true;
4102       }
4103     }
4104   } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) {
4105     // Special case for 'a', which has type 'int' in C.
4106     // Note, however, that we do /not/ want to treat multibyte constants like
4107     // 'MooV' as characters! This form is deprecated but still exists.
4108     if (ExprTy == S.Context.IntTy)
4109       if (llvm::isUIntN(S.Context.getCharWidth(), CL->getValue()))
4110         ExprTy = S.Context.CharTy;
4111   }
4112 
4113   // Look through enums to their underlying type.
4114   bool IsEnum = false;
4115   if (auto EnumTy = ExprTy->getAs<EnumType>()) {
4116     ExprTy = EnumTy->getDecl()->getIntegerType();
4117     IsEnum = true;
4118   }
4119 
4120   // %C in an Objective-C context prints a unichar, not a wchar_t.
4121   // If the argument is an integer of some kind, believe the %C and suggest
4122   // a cast instead of changing the conversion specifier.
4123   QualType IntendedTy = ExprTy;
4124   if (ObjCContext &&
4125       FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) {
4126     if (ExprTy->isIntegralOrUnscopedEnumerationType() &&
4127         !ExprTy->isCharType()) {
4128       // 'unichar' is defined as a typedef of unsigned short, but we should
4129       // prefer using the typedef if it is visible.
4130       IntendedTy = S.Context.UnsignedShortTy;
4131 
4132       // While we are here, check if the value is an IntegerLiteral that happens
4133       // to be within the valid range.
4134       if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) {
4135         const llvm::APInt &V = IL->getValue();
4136         if (V.getActiveBits() <= S.Context.getTypeSize(IntendedTy))
4137           return true;
4138       }
4139 
4140       LookupResult Result(S, &S.Context.Idents.get("unichar"), E->getLocStart(),
4141                           Sema::LookupOrdinaryName);
4142       if (S.LookupName(Result, S.getCurScope())) {
4143         NamedDecl *ND = Result.getFoundDecl();
4144         if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
4145           if (TD->getUnderlyingType() == IntendedTy)
4146             IntendedTy = S.Context.getTypedefType(TD);
4147       }
4148     }
4149   }
4150 
4151   // Special-case some of Darwin's platform-independence types by suggesting
4152   // casts to primitive types that are known to be large enough.
4153   bool ShouldNotPrintDirectly = false; StringRef CastTyName;
4154   if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
4155     QualType CastTy;
4156     std::tie(CastTy, CastTyName) = shouldNotPrintDirectly(S.Context, IntendedTy, E);
4157     if (!CastTy.isNull()) {
4158       IntendedTy = CastTy;
4159       ShouldNotPrintDirectly = true;
4160     }
4161   }
4162 
4163   // We may be able to offer a FixItHint if it is a supported type.
4164   PrintfSpecifier fixedFS = FS;
4165   bool success = fixedFS.fixType(IntendedTy, S.getLangOpts(),
4166                                  S.Context, ObjCContext);
4167 
4168   if (success) {
4169     // Get the fix string from the fixed format specifier
4170     SmallString<16> buf;
4171     llvm::raw_svector_ostream os(buf);
4172     fixedFS.toString(os);
4173 
4174     CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen);
4175 
4176     if (IntendedTy == ExprTy && !ShouldNotPrintDirectly) {
4177       unsigned diag = diag::warn_format_conversion_argument_type_mismatch;
4178       if (match == analyze_format_string::ArgType::NoMatchPedantic) {
4179         diag = diag::warn_format_conversion_argument_type_mismatch_pedantic;
4180       }
4181       // In this case, the specifier is wrong and should be changed to match
4182       // the argument.
4183       EmitFormatDiagnostic(S.PDiag(diag)
4184                                << AT.getRepresentativeTypeName(S.Context)
4185                                << IntendedTy << IsEnum << E->getSourceRange(),
4186                            E->getLocStart(),
4187                            /*IsStringLocation*/ false, SpecRange,
4188                            FixItHint::CreateReplacement(SpecRange, os.str()));
4189 
4190     } else {
4191       // The canonical type for formatting this value is different from the
4192       // actual type of the expression. (This occurs, for example, with Darwin's
4193       // NSInteger on 32-bit platforms, where it is typedef'd as 'int', but
4194       // should be printed as 'long' for 64-bit compatibility.)
4195       // Rather than emitting a normal format/argument mismatch, we want to
4196       // add a cast to the recommended type (and correct the format string
4197       // if necessary).
4198       SmallString<16> CastBuf;
4199       llvm::raw_svector_ostream CastFix(CastBuf);
4200       CastFix << "(";
4201       IntendedTy.print(CastFix, S.Context.getPrintingPolicy());
4202       CastFix << ")";
4203 
4204       SmallVector<FixItHint,4> Hints;
4205       if (!AT.matchesType(S.Context, IntendedTy))
4206         Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str()));
4207 
4208       if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) {
4209         // If there's already a cast present, just replace it.
4210         SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc());
4211         Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str()));
4212 
4213       } else if (!requiresParensToAddCast(E)) {
4214         // If the expression has high enough precedence,
4215         // just write the C-style cast.
4216         Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(),
4217                                                    CastFix.str()));
4218       } else {
4219         // Otherwise, add parens around the expression as well as the cast.
4220         CastFix << "(";
4221         Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(),
4222                                                    CastFix.str()));
4223 
4224         SourceLocation After = S.getLocForEndOfToken(E->getLocEnd());
4225         Hints.push_back(FixItHint::CreateInsertion(After, ")"));
4226       }
4227 
4228       if (ShouldNotPrintDirectly) {
4229         // The expression has a type that should not be printed directly.
4230         // We extract the name from the typedef because we don't want to show
4231         // the underlying type in the diagnostic.
4232         StringRef Name;
4233         if (const TypedefType *TypedefTy = dyn_cast<TypedefType>(ExprTy))
4234           Name = TypedefTy->getDecl()->getName();
4235         else
4236           Name = CastTyName;
4237         EmitFormatDiagnostic(S.PDiag(diag::warn_format_argument_needs_cast)
4238                                << Name << IntendedTy << IsEnum
4239                                << E->getSourceRange(),
4240                              E->getLocStart(), /*IsStringLocation=*/false,
4241                              SpecRange, Hints);
4242       } else {
4243         // In this case, the expression could be printed using a different
4244         // specifier, but we've decided that the specifier is probably correct
4245         // and we should cast instead. Just use the normal warning message.
4246         EmitFormatDiagnostic(
4247           S.PDiag(diag::warn_format_conversion_argument_type_mismatch)
4248             << AT.getRepresentativeTypeName(S.Context) << ExprTy << IsEnum
4249             << E->getSourceRange(),
4250           E->getLocStart(), /*IsStringLocation*/false,
4251           SpecRange, Hints);
4252       }
4253     }
4254   } else {
4255     const CharSourceRange &CSR = getSpecifierRange(StartSpecifier,
4256                                                    SpecifierLen);
4257     // Since the warning for passing non-POD types to variadic functions
4258     // was deferred until now, we emit a warning for non-POD
4259     // arguments here.
4260     switch (S.isValidVarArgType(ExprTy)) {
4261     case Sema::VAK_Valid:
4262     case Sema::VAK_ValidInCXX11: {
4263       unsigned diag = diag::warn_format_conversion_argument_type_mismatch;
4264       if (match == analyze_printf::ArgType::NoMatchPedantic) {
4265         diag = diag::warn_format_conversion_argument_type_mismatch_pedantic;
4266       }
4267 
4268       EmitFormatDiagnostic(
4269           S.PDiag(diag) << AT.getRepresentativeTypeName(S.Context) << ExprTy
4270                         << IsEnum << CSR << E->getSourceRange(),
4271           E->getLocStart(), /*IsStringLocation*/ false, CSR);
4272       break;
4273     }
4274     case Sema::VAK_Undefined:
4275     case Sema::VAK_MSVCUndefined:
4276       EmitFormatDiagnostic(
4277         S.PDiag(diag::warn_non_pod_vararg_with_format_string)
4278           << S.getLangOpts().CPlusPlus11
4279           << ExprTy
4280           << CallType
4281           << AT.getRepresentativeTypeName(S.Context)
4282           << CSR
4283           << E->getSourceRange(),
4284         E->getLocStart(), /*IsStringLocation*/false, CSR);
4285       checkForCStrMembers(AT, E);
4286       break;
4287 
4288     case Sema::VAK_Invalid:
4289       if (ExprTy->isObjCObjectType())
4290         EmitFormatDiagnostic(
4291           S.PDiag(diag::err_cannot_pass_objc_interface_to_vararg_format)
4292             << S.getLangOpts().CPlusPlus11
4293             << ExprTy
4294             << CallType
4295             << AT.getRepresentativeTypeName(S.Context)
4296             << CSR
4297             << E->getSourceRange(),
4298           E->getLocStart(), /*IsStringLocation*/false, CSR);
4299       else
4300         // FIXME: If this is an initializer list, suggest removing the braces
4301         // or inserting a cast to the target type.
4302         S.Diag(E->getLocStart(), diag::err_cannot_pass_to_vararg_format)
4303           << isa<InitListExpr>(E) << ExprTy << CallType
4304           << AT.getRepresentativeTypeName(S.Context)
4305           << E->getSourceRange();
4306       break;
4307     }
4308 
4309     assert(FirstDataArg + FS.getArgIndex() < CheckedVarArgs.size() &&
4310            "format string specifier index out of range");
4311     CheckedVarArgs[FirstDataArg + FS.getArgIndex()] = true;
4312   }
4313 
4314   return true;
4315 }
4316 
4317 //===--- CHECK: Scanf format string checking ------------------------------===//
4318 
4319 namespace {
4320 class CheckScanfHandler : public CheckFormatHandler {
4321 public:
4322   CheckScanfHandler(Sema &s, const StringLiteral *fexpr,
4323                     const Expr *origFormatExpr, unsigned firstDataArg,
4324                     unsigned numDataArgs, const char *beg, bool hasVAListArg,
4325                     ArrayRef<const Expr *> Args,
4326                     unsigned formatIdx, bool inFunctionCall,
4327                     Sema::VariadicCallType CallType,
4328                     llvm::SmallBitVector &CheckedVarArgs)
4329     : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg,
4330                          numDataArgs, beg, hasVAListArg,
4331                          Args, formatIdx, inFunctionCall, CallType,
4332                          CheckedVarArgs)
4333   {}
4334 
4335   bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS,
4336                             const char *startSpecifier,
4337                             unsigned specifierLen) override;
4338 
4339   bool HandleInvalidScanfConversionSpecifier(
4340           const analyze_scanf::ScanfSpecifier &FS,
4341           const char *startSpecifier,
4342           unsigned specifierLen) override;
4343 
4344   void HandleIncompleteScanList(const char *start, const char *end) override;
4345 };
4346 }
4347 
4348 void CheckScanfHandler::HandleIncompleteScanList(const char *start,
4349                                                  const char *end) {
4350   EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete),
4351                        getLocationOfByte(end), /*IsStringLocation*/true,
4352                        getSpecifierRange(start, end - start));
4353 }
4354 
4355 bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier(
4356                                         const analyze_scanf::ScanfSpecifier &FS,
4357                                         const char *startSpecifier,
4358                                         unsigned specifierLen) {
4359 
4360   const analyze_scanf::ScanfConversionSpecifier &CS =
4361     FS.getConversionSpecifier();
4362 
4363   return HandleInvalidConversionSpecifier(FS.getArgIndex(),
4364                                           getLocationOfByte(CS.getStart()),
4365                                           startSpecifier, specifierLen,
4366                                           CS.getStart(), CS.getLength());
4367 }
4368 
4369 bool CheckScanfHandler::HandleScanfSpecifier(
4370                                        const analyze_scanf::ScanfSpecifier &FS,
4371                                        const char *startSpecifier,
4372                                        unsigned specifierLen) {
4373 
4374   using namespace analyze_scanf;
4375   using namespace analyze_format_string;
4376 
4377   const ScanfConversionSpecifier &CS = FS.getConversionSpecifier();
4378 
4379   // Handle case where '%' and '*' don't consume an argument.  These shouldn't
4380   // be used to decide if we are using positional arguments consistently.
4381   if (FS.consumesDataArgument()) {
4382     if (atFirstArg) {
4383       atFirstArg = false;
4384       usesPositionalArgs = FS.usesPositionalArg();
4385     }
4386     else if (usesPositionalArgs != FS.usesPositionalArg()) {
4387       HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()),
4388                                         startSpecifier, specifierLen);
4389       return false;
4390     }
4391   }
4392 
4393   // Check if the field with is non-zero.
4394   const OptionalAmount &Amt = FS.getFieldWidth();
4395   if (Amt.getHowSpecified() == OptionalAmount::Constant) {
4396     if (Amt.getConstantAmount() == 0) {
4397       const CharSourceRange &R = getSpecifierRange(Amt.getStart(),
4398                                                    Amt.getConstantLength());
4399       EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width),
4400                            getLocationOfByte(Amt.getStart()),
4401                            /*IsStringLocation*/true, R,
4402                            FixItHint::CreateRemoval(R));
4403     }
4404   }
4405 
4406   if (!FS.consumesDataArgument()) {
4407     // FIXME: Technically specifying a precision or field width here
4408     // makes no sense.  Worth issuing a warning at some point.
4409     return true;
4410   }
4411 
4412   // Consume the argument.
4413   unsigned argIndex = FS.getArgIndex();
4414   if (argIndex < NumDataArgs) {
4415       // The check to see if the argIndex is valid will come later.
4416       // We set the bit here because we may exit early from this
4417       // function if we encounter some other error.
4418     CoveredArgs.set(argIndex);
4419   }
4420 
4421   // Check the length modifier is valid with the given conversion specifier.
4422   if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo()))
4423     HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
4424                                 diag::warn_format_nonsensical_length);
4425   else if (!FS.hasStandardLengthModifier())
4426     HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen);
4427   else if (!FS.hasStandardLengthConversionCombination())
4428     HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
4429                                 diag::warn_format_non_standard_conversion_spec);
4430 
4431   if (!FS.hasStandardConversionSpecifier(S.getLangOpts()))
4432     HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen);
4433 
4434   // The remaining checks depend on the data arguments.
4435   if (HasVAListArg)
4436     return true;
4437 
4438   if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
4439     return false;
4440 
4441   // Check that the argument type matches the format specifier.
4442   const Expr *Ex = getDataArg(argIndex);
4443   if (!Ex)
4444     return true;
4445 
4446   const analyze_format_string::ArgType &AT = FS.getArgType(S.Context);
4447 
4448   if (!AT.isValid()) {
4449     return true;
4450   }
4451 
4452   analyze_format_string::ArgType::MatchKind match =
4453       AT.matchesType(S.Context, Ex->getType());
4454   if (match == analyze_format_string::ArgType::Match) {
4455     return true;
4456   }
4457 
4458   ScanfSpecifier fixedFS = FS;
4459   bool success = fixedFS.fixType(Ex->getType(), Ex->IgnoreImpCasts()->getType(),
4460                                  S.getLangOpts(), S.Context);
4461 
4462   unsigned diag = diag::warn_format_conversion_argument_type_mismatch;
4463   if (match == analyze_format_string::ArgType::NoMatchPedantic) {
4464     diag = diag::warn_format_conversion_argument_type_mismatch_pedantic;
4465   }
4466 
4467   if (success) {
4468     // Get the fix string from the fixed format specifier.
4469     SmallString<128> buf;
4470     llvm::raw_svector_ostream os(buf);
4471     fixedFS.toString(os);
4472 
4473     EmitFormatDiagnostic(
4474         S.PDiag(diag) << AT.getRepresentativeTypeName(S.Context)
4475                       << Ex->getType() << false << Ex->getSourceRange(),
4476         Ex->getLocStart(),
4477         /*IsStringLocation*/ false,
4478         getSpecifierRange(startSpecifier, specifierLen),
4479         FixItHint::CreateReplacement(
4480             getSpecifierRange(startSpecifier, specifierLen), os.str()));
4481   } else {
4482     EmitFormatDiagnostic(S.PDiag(diag)
4483                              << AT.getRepresentativeTypeName(S.Context)
4484                              << Ex->getType() << false << Ex->getSourceRange(),
4485                          Ex->getLocStart(),
4486                          /*IsStringLocation*/ false,
4487                          getSpecifierRange(startSpecifier, specifierLen));
4488   }
4489 
4490   return true;
4491 }
4492 
4493 void Sema::CheckFormatString(const StringLiteral *FExpr,
4494                              const Expr *OrigFormatExpr,
4495                              ArrayRef<const Expr *> Args,
4496                              bool HasVAListArg, unsigned format_idx,
4497                              unsigned firstDataArg, FormatStringType Type,
4498                              bool inFunctionCall, VariadicCallType CallType,
4499                              llvm::SmallBitVector &CheckedVarArgs) {
4500 
4501   // CHECK: is the format string a wide literal?
4502   if (!FExpr->isAscii() && !FExpr->isUTF8()) {
4503     CheckFormatHandler::EmitFormatDiagnostic(
4504       *this, inFunctionCall, Args[format_idx],
4505       PDiag(diag::warn_format_string_is_wide_literal), FExpr->getLocStart(),
4506       /*IsStringLocation*/true, OrigFormatExpr->getSourceRange());
4507     return;
4508   }
4509 
4510   // Str - The format string.  NOTE: this is NOT null-terminated!
4511   StringRef StrRef = FExpr->getString();
4512   const char *Str = StrRef.data();
4513   // Account for cases where the string literal is truncated in a declaration.
4514   const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType());
4515   assert(T && "String literal not of constant array type!");
4516   size_t TypeSize = T->getSize().getZExtValue();
4517   size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size());
4518   const unsigned numDataArgs = Args.size() - firstDataArg;
4519 
4520   // Emit a warning if the string literal is truncated and does not contain an
4521   // embedded null character.
4522   if (TypeSize <= StrRef.size() &&
4523       StrRef.substr(0, TypeSize).find('\0') == StringRef::npos) {
4524     CheckFormatHandler::EmitFormatDiagnostic(
4525         *this, inFunctionCall, Args[format_idx],
4526         PDiag(diag::warn_printf_format_string_not_null_terminated),
4527         FExpr->getLocStart(),
4528         /*IsStringLocation=*/true, OrigFormatExpr->getSourceRange());
4529     return;
4530   }
4531 
4532   // CHECK: empty format string?
4533   if (StrLen == 0 && numDataArgs > 0) {
4534     CheckFormatHandler::EmitFormatDiagnostic(
4535       *this, inFunctionCall, Args[format_idx],
4536       PDiag(diag::warn_empty_format_string), FExpr->getLocStart(),
4537       /*IsStringLocation*/true, OrigFormatExpr->getSourceRange());
4538     return;
4539   }
4540 
4541   if (Type == FST_Printf || Type == FST_NSString ||
4542       Type == FST_FreeBSDKPrintf || Type == FST_OSTrace) {
4543     CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg,
4544                          numDataArgs, (Type == FST_NSString || Type == FST_OSTrace),
4545                          Str, HasVAListArg, Args, format_idx,
4546                          inFunctionCall, CallType, CheckedVarArgs);
4547 
4548     if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen,
4549                                                   getLangOpts(),
4550                                                   Context.getTargetInfo(),
4551                                                   Type == FST_FreeBSDKPrintf))
4552       H.DoneProcessing();
4553   } else if (Type == FST_Scanf) {
4554     CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, numDataArgs,
4555                         Str, HasVAListArg, Args, format_idx,
4556                         inFunctionCall, CallType, CheckedVarArgs);
4557 
4558     if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen,
4559                                                  getLangOpts(),
4560                                                  Context.getTargetInfo()))
4561       H.DoneProcessing();
4562   } // TODO: handle other formats
4563 }
4564 
4565 bool Sema::FormatStringHasSArg(const StringLiteral *FExpr) {
4566   // Str - The format string.  NOTE: this is NOT null-terminated!
4567   StringRef StrRef = FExpr->getString();
4568   const char *Str = StrRef.data();
4569   // Account for cases where the string literal is truncated in a declaration.
4570   const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType());
4571   assert(T && "String literal not of constant array type!");
4572   size_t TypeSize = T->getSize().getZExtValue();
4573   size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size());
4574   return analyze_format_string::ParseFormatStringHasSArg(Str, Str + StrLen,
4575                                                          getLangOpts(),
4576                                                          Context.getTargetInfo());
4577 }
4578 
4579 //===--- CHECK: Warn on use of wrong absolute value function. -------------===//
4580 
4581 // Returns the related absolute value function that is larger, of 0 if one
4582 // does not exist.
4583 static unsigned getLargerAbsoluteValueFunction(unsigned AbsFunction) {
4584   switch (AbsFunction) {
4585   default:
4586     return 0;
4587 
4588   case Builtin::BI__builtin_abs:
4589     return Builtin::BI__builtin_labs;
4590   case Builtin::BI__builtin_labs:
4591     return Builtin::BI__builtin_llabs;
4592   case Builtin::BI__builtin_llabs:
4593     return 0;
4594 
4595   case Builtin::BI__builtin_fabsf:
4596     return Builtin::BI__builtin_fabs;
4597   case Builtin::BI__builtin_fabs:
4598     return Builtin::BI__builtin_fabsl;
4599   case Builtin::BI__builtin_fabsl:
4600     return 0;
4601 
4602   case Builtin::BI__builtin_cabsf:
4603     return Builtin::BI__builtin_cabs;
4604   case Builtin::BI__builtin_cabs:
4605     return Builtin::BI__builtin_cabsl;
4606   case Builtin::BI__builtin_cabsl:
4607     return 0;
4608 
4609   case Builtin::BIabs:
4610     return Builtin::BIlabs;
4611   case Builtin::BIlabs:
4612     return Builtin::BIllabs;
4613   case Builtin::BIllabs:
4614     return 0;
4615 
4616   case Builtin::BIfabsf:
4617     return Builtin::BIfabs;
4618   case Builtin::BIfabs:
4619     return Builtin::BIfabsl;
4620   case Builtin::BIfabsl:
4621     return 0;
4622 
4623   case Builtin::BIcabsf:
4624    return Builtin::BIcabs;
4625   case Builtin::BIcabs:
4626     return Builtin::BIcabsl;
4627   case Builtin::BIcabsl:
4628     return 0;
4629   }
4630 }
4631 
4632 // Returns the argument type of the absolute value function.
4633 static QualType getAbsoluteValueArgumentType(ASTContext &Context,
4634                                              unsigned AbsType) {
4635   if (AbsType == 0)
4636     return QualType();
4637 
4638   ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None;
4639   QualType BuiltinType = Context.GetBuiltinType(AbsType, Error);
4640   if (Error != ASTContext::GE_None)
4641     return QualType();
4642 
4643   const FunctionProtoType *FT = BuiltinType->getAs<FunctionProtoType>();
4644   if (!FT)
4645     return QualType();
4646 
4647   if (FT->getNumParams() != 1)
4648     return QualType();
4649 
4650   return FT->getParamType(0);
4651 }
4652 
4653 // Returns the best absolute value function, or zero, based on type and
4654 // current absolute value function.
4655 static unsigned getBestAbsFunction(ASTContext &Context, QualType ArgType,
4656                                    unsigned AbsFunctionKind) {
4657   unsigned BestKind = 0;
4658   uint64_t ArgSize = Context.getTypeSize(ArgType);
4659   for (unsigned Kind = AbsFunctionKind; Kind != 0;
4660        Kind = getLargerAbsoluteValueFunction(Kind)) {
4661     QualType ParamType = getAbsoluteValueArgumentType(Context, Kind);
4662     if (Context.getTypeSize(ParamType) >= ArgSize) {
4663       if (BestKind == 0)
4664         BestKind = Kind;
4665       else if (Context.hasSameType(ParamType, ArgType)) {
4666         BestKind = Kind;
4667         break;
4668       }
4669     }
4670   }
4671   return BestKind;
4672 }
4673 
4674 enum AbsoluteValueKind {
4675   AVK_Integer,
4676   AVK_Floating,
4677   AVK_Complex
4678 };
4679 
4680 static AbsoluteValueKind getAbsoluteValueKind(QualType T) {
4681   if (T->isIntegralOrEnumerationType())
4682     return AVK_Integer;
4683   if (T->isRealFloatingType())
4684     return AVK_Floating;
4685   if (T->isAnyComplexType())
4686     return AVK_Complex;
4687 
4688   llvm_unreachable("Type not integer, floating, or complex");
4689 }
4690 
4691 // Changes the absolute value function to a different type.  Preserves whether
4692 // the function is a builtin.
4693 static unsigned changeAbsFunction(unsigned AbsKind,
4694                                   AbsoluteValueKind ValueKind) {
4695   switch (ValueKind) {
4696   case AVK_Integer:
4697     switch (AbsKind) {
4698     default:
4699       return 0;
4700     case Builtin::BI__builtin_fabsf:
4701     case Builtin::BI__builtin_fabs:
4702     case Builtin::BI__builtin_fabsl:
4703     case Builtin::BI__builtin_cabsf:
4704     case Builtin::BI__builtin_cabs:
4705     case Builtin::BI__builtin_cabsl:
4706       return Builtin::BI__builtin_abs;
4707     case Builtin::BIfabsf:
4708     case Builtin::BIfabs:
4709     case Builtin::BIfabsl:
4710     case Builtin::BIcabsf:
4711     case Builtin::BIcabs:
4712     case Builtin::BIcabsl:
4713       return Builtin::BIabs;
4714     }
4715   case AVK_Floating:
4716     switch (AbsKind) {
4717     default:
4718       return 0;
4719     case Builtin::BI__builtin_abs:
4720     case Builtin::BI__builtin_labs:
4721     case Builtin::BI__builtin_llabs:
4722     case Builtin::BI__builtin_cabsf:
4723     case Builtin::BI__builtin_cabs:
4724     case Builtin::BI__builtin_cabsl:
4725       return Builtin::BI__builtin_fabsf;
4726     case Builtin::BIabs:
4727     case Builtin::BIlabs:
4728     case Builtin::BIllabs:
4729     case Builtin::BIcabsf:
4730     case Builtin::BIcabs:
4731     case Builtin::BIcabsl:
4732       return Builtin::BIfabsf;
4733     }
4734   case AVK_Complex:
4735     switch (AbsKind) {
4736     default:
4737       return 0;
4738     case Builtin::BI__builtin_abs:
4739     case Builtin::BI__builtin_labs:
4740     case Builtin::BI__builtin_llabs:
4741     case Builtin::BI__builtin_fabsf:
4742     case Builtin::BI__builtin_fabs:
4743     case Builtin::BI__builtin_fabsl:
4744       return Builtin::BI__builtin_cabsf;
4745     case Builtin::BIabs:
4746     case Builtin::BIlabs:
4747     case Builtin::BIllabs:
4748     case Builtin::BIfabsf:
4749     case Builtin::BIfabs:
4750     case Builtin::BIfabsl:
4751       return Builtin::BIcabsf;
4752     }
4753   }
4754   llvm_unreachable("Unable to convert function");
4755 }
4756 
4757 static unsigned getAbsoluteValueFunctionKind(const FunctionDecl *FDecl) {
4758   const IdentifierInfo *FnInfo = FDecl->getIdentifier();
4759   if (!FnInfo)
4760     return 0;
4761 
4762   switch (FDecl->getBuiltinID()) {
4763   default:
4764     return 0;
4765   case Builtin::BI__builtin_abs:
4766   case Builtin::BI__builtin_fabs:
4767   case Builtin::BI__builtin_fabsf:
4768   case Builtin::BI__builtin_fabsl:
4769   case Builtin::BI__builtin_labs:
4770   case Builtin::BI__builtin_llabs:
4771   case Builtin::BI__builtin_cabs:
4772   case Builtin::BI__builtin_cabsf:
4773   case Builtin::BI__builtin_cabsl:
4774   case Builtin::BIabs:
4775   case Builtin::BIlabs:
4776   case Builtin::BIllabs:
4777   case Builtin::BIfabs:
4778   case Builtin::BIfabsf:
4779   case Builtin::BIfabsl:
4780   case Builtin::BIcabs:
4781   case Builtin::BIcabsf:
4782   case Builtin::BIcabsl:
4783     return FDecl->getBuiltinID();
4784   }
4785   llvm_unreachable("Unknown Builtin type");
4786 }
4787 
4788 // If the replacement is valid, emit a note with replacement function.
4789 // Additionally, suggest including the proper header if not already included.
4790 static void emitReplacement(Sema &S, SourceLocation Loc, SourceRange Range,
4791                             unsigned AbsKind, QualType ArgType) {
4792   bool EmitHeaderHint = true;
4793   const char *HeaderName = nullptr;
4794   const char *FunctionName = nullptr;
4795   if (S.getLangOpts().CPlusPlus && !ArgType->isAnyComplexType()) {
4796     FunctionName = "std::abs";
4797     if (ArgType->isIntegralOrEnumerationType()) {
4798       HeaderName = "cstdlib";
4799     } else if (ArgType->isRealFloatingType()) {
4800       HeaderName = "cmath";
4801     } else {
4802       llvm_unreachable("Invalid Type");
4803     }
4804 
4805     // Lookup all std::abs
4806     if (NamespaceDecl *Std = S.getStdNamespace()) {
4807       LookupResult R(S, &S.Context.Idents.get("abs"), Loc, Sema::LookupAnyName);
4808       R.suppressDiagnostics();
4809       S.LookupQualifiedName(R, Std);
4810 
4811       for (const auto *I : R) {
4812         const FunctionDecl *FDecl = nullptr;
4813         if (const UsingShadowDecl *UsingD = dyn_cast<UsingShadowDecl>(I)) {
4814           FDecl = dyn_cast<FunctionDecl>(UsingD->getTargetDecl());
4815         } else {
4816           FDecl = dyn_cast<FunctionDecl>(I);
4817         }
4818         if (!FDecl)
4819           continue;
4820 
4821         // Found std::abs(), check that they are the right ones.
4822         if (FDecl->getNumParams() != 1)
4823           continue;
4824 
4825         // Check that the parameter type can handle the argument.
4826         QualType ParamType = FDecl->getParamDecl(0)->getType();
4827         if (getAbsoluteValueKind(ArgType) == getAbsoluteValueKind(ParamType) &&
4828             S.Context.getTypeSize(ArgType) <=
4829                 S.Context.getTypeSize(ParamType)) {
4830           // Found a function, don't need the header hint.
4831           EmitHeaderHint = false;
4832           break;
4833         }
4834       }
4835     }
4836   } else {
4837     FunctionName = S.Context.BuiltinInfo.getName(AbsKind);
4838     HeaderName = S.Context.BuiltinInfo.getHeaderName(AbsKind);
4839 
4840     if (HeaderName) {
4841       DeclarationName DN(&S.Context.Idents.get(FunctionName));
4842       LookupResult R(S, DN, Loc, Sema::LookupAnyName);
4843       R.suppressDiagnostics();
4844       S.LookupName(R, S.getCurScope());
4845 
4846       if (R.isSingleResult()) {
4847         FunctionDecl *FD = dyn_cast<FunctionDecl>(R.getFoundDecl());
4848         if (FD && FD->getBuiltinID() == AbsKind) {
4849           EmitHeaderHint = false;
4850         } else {
4851           return;
4852         }
4853       } else if (!R.empty()) {
4854         return;
4855       }
4856     }
4857   }
4858 
4859   S.Diag(Loc, diag::note_replace_abs_function)
4860       << FunctionName << FixItHint::CreateReplacement(Range, FunctionName);
4861 
4862   if (!HeaderName)
4863     return;
4864 
4865   if (!EmitHeaderHint)
4866     return;
4867 
4868   S.Diag(Loc, diag::note_include_header_or_declare) << HeaderName
4869                                                     << FunctionName;
4870 }
4871 
4872 static bool IsFunctionStdAbs(const FunctionDecl *FDecl) {
4873   if (!FDecl)
4874     return false;
4875 
4876   if (!FDecl->getIdentifier() || !FDecl->getIdentifier()->isStr("abs"))
4877     return false;
4878 
4879   const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(FDecl->getDeclContext());
4880 
4881   while (ND && ND->isInlineNamespace()) {
4882     ND = dyn_cast<NamespaceDecl>(ND->getDeclContext());
4883   }
4884 
4885   if (!ND || !ND->getIdentifier() || !ND->getIdentifier()->isStr("std"))
4886     return false;
4887 
4888   if (!isa<TranslationUnitDecl>(ND->getDeclContext()))
4889     return false;
4890 
4891   return true;
4892 }
4893 
4894 // Warn when using the wrong abs() function.
4895 void Sema::CheckAbsoluteValueFunction(const CallExpr *Call,
4896                                       const FunctionDecl *FDecl,
4897                                       IdentifierInfo *FnInfo) {
4898   if (Call->getNumArgs() != 1)
4899     return;
4900 
4901   unsigned AbsKind = getAbsoluteValueFunctionKind(FDecl);
4902   bool IsStdAbs = IsFunctionStdAbs(FDecl);
4903   if (AbsKind == 0 && !IsStdAbs)
4904     return;
4905 
4906   QualType ArgType = Call->getArg(0)->IgnoreParenImpCasts()->getType();
4907   QualType ParamType = Call->getArg(0)->getType();
4908 
4909   // Unsigned types cannot be negative.  Suggest removing the absolute value
4910   // function call.
4911   if (ArgType->isUnsignedIntegerType()) {
4912     const char *FunctionName =
4913         IsStdAbs ? "std::abs" : Context.BuiltinInfo.getName(AbsKind);
4914     Diag(Call->getExprLoc(), diag::warn_unsigned_abs) << ArgType << ParamType;
4915     Diag(Call->getExprLoc(), diag::note_remove_abs)
4916         << FunctionName
4917         << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange());
4918     return;
4919   }
4920 
4921   // std::abs has overloads which prevent most of the absolute value problems
4922   // from occurring.
4923   if (IsStdAbs)
4924     return;
4925 
4926   AbsoluteValueKind ArgValueKind = getAbsoluteValueKind(ArgType);
4927   AbsoluteValueKind ParamValueKind = getAbsoluteValueKind(ParamType);
4928 
4929   // The argument and parameter are the same kind.  Check if they are the right
4930   // size.
4931   if (ArgValueKind == ParamValueKind) {
4932     if (Context.getTypeSize(ArgType) <= Context.getTypeSize(ParamType))
4933       return;
4934 
4935     unsigned NewAbsKind = getBestAbsFunction(Context, ArgType, AbsKind);
4936     Diag(Call->getExprLoc(), diag::warn_abs_too_small)
4937         << FDecl << ArgType << ParamType;
4938 
4939     if (NewAbsKind == 0)
4940       return;
4941 
4942     emitReplacement(*this, Call->getExprLoc(),
4943                     Call->getCallee()->getSourceRange(), NewAbsKind, ArgType);
4944     return;
4945   }
4946 
4947   // ArgValueKind != ParamValueKind
4948   // The wrong type of absolute value function was used.  Attempt to find the
4949   // proper one.
4950   unsigned NewAbsKind = changeAbsFunction(AbsKind, ArgValueKind);
4951   NewAbsKind = getBestAbsFunction(Context, ArgType, NewAbsKind);
4952   if (NewAbsKind == 0)
4953     return;
4954 
4955   Diag(Call->getExprLoc(), diag::warn_wrong_absolute_value_type)
4956       << FDecl << ParamValueKind << ArgValueKind;
4957 
4958   emitReplacement(*this, Call->getExprLoc(),
4959                   Call->getCallee()->getSourceRange(), NewAbsKind, ArgType);
4960   return;
4961 }
4962 
4963 //===--- CHECK: Standard memory functions ---------------------------------===//
4964 
4965 /// \brief Takes the expression passed to the size_t parameter of functions
4966 /// such as memcmp, strncat, etc and warns if it's a comparison.
4967 ///
4968 /// This is to catch typos like `if (memcmp(&a, &b, sizeof(a) > 0))`.
4969 static bool CheckMemorySizeofForComparison(Sema &S, const Expr *E,
4970                                            IdentifierInfo *FnName,
4971                                            SourceLocation FnLoc,
4972                                            SourceLocation RParenLoc) {
4973   const BinaryOperator *Size = dyn_cast<BinaryOperator>(E);
4974   if (!Size)
4975     return false;
4976 
4977   // if E is binop and op is >, <, >=, <=, ==, &&, ||:
4978   if (!Size->isComparisonOp() && !Size->isEqualityOp() && !Size->isLogicalOp())
4979     return false;
4980 
4981   SourceRange SizeRange = Size->getSourceRange();
4982   S.Diag(Size->getOperatorLoc(), diag::warn_memsize_comparison)
4983       << SizeRange << FnName;
4984   S.Diag(FnLoc, diag::note_memsize_comparison_paren)
4985       << FnName << FixItHint::CreateInsertion(
4986                        S.getLocForEndOfToken(Size->getLHS()->getLocEnd()), ")")
4987       << FixItHint::CreateRemoval(RParenLoc);
4988   S.Diag(SizeRange.getBegin(), diag::note_memsize_comparison_cast_silence)
4989       << FixItHint::CreateInsertion(SizeRange.getBegin(), "(size_t)(")
4990       << FixItHint::CreateInsertion(S.getLocForEndOfToken(SizeRange.getEnd()),
4991                                     ")");
4992 
4993   return true;
4994 }
4995 
4996 /// \brief Determine whether the given type is or contains a dynamic class type
4997 /// (e.g., whether it has a vtable).
4998 static const CXXRecordDecl *getContainedDynamicClass(QualType T,
4999                                                      bool &IsContained) {
5000   // Look through array types while ignoring qualifiers.
5001   const Type *Ty = T->getBaseElementTypeUnsafe();
5002   IsContained = false;
5003 
5004   const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
5005   RD = RD ? RD->getDefinition() : nullptr;
5006   if (!RD)
5007     return nullptr;
5008 
5009   if (RD->isDynamicClass())
5010     return RD;
5011 
5012   // Check all the fields.  If any bases were dynamic, the class is dynamic.
5013   // It's impossible for a class to transitively contain itself by value, so
5014   // infinite recursion is impossible.
5015   for (auto *FD : RD->fields()) {
5016     bool SubContained;
5017     if (const CXXRecordDecl *ContainedRD =
5018             getContainedDynamicClass(FD->getType(), SubContained)) {
5019       IsContained = true;
5020       return ContainedRD;
5021     }
5022   }
5023 
5024   return nullptr;
5025 }
5026 
5027 /// \brief If E is a sizeof expression, returns its argument expression,
5028 /// otherwise returns NULL.
5029 static const Expr *getSizeOfExprArg(const Expr *E) {
5030   if (const UnaryExprOrTypeTraitExpr *SizeOf =
5031       dyn_cast<UnaryExprOrTypeTraitExpr>(E))
5032     if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType())
5033       return SizeOf->getArgumentExpr()->IgnoreParenImpCasts();
5034 
5035   return nullptr;
5036 }
5037 
5038 /// \brief If E is a sizeof expression, returns its argument type.
5039 static QualType getSizeOfArgType(const Expr *E) {
5040   if (const UnaryExprOrTypeTraitExpr *SizeOf =
5041       dyn_cast<UnaryExprOrTypeTraitExpr>(E))
5042     if (SizeOf->getKind() == clang::UETT_SizeOf)
5043       return SizeOf->getTypeOfArgument();
5044 
5045   return QualType();
5046 }
5047 
5048 /// \brief Check for dangerous or invalid arguments to memset().
5049 ///
5050 /// This issues warnings on known problematic, dangerous or unspecified
5051 /// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp'
5052 /// function calls.
5053 ///
5054 /// \param Call The call expression to diagnose.
5055 void Sema::CheckMemaccessArguments(const CallExpr *Call,
5056                                    unsigned BId,
5057                                    IdentifierInfo *FnName) {
5058   assert(BId != 0);
5059 
5060   // It is possible to have a non-standard definition of memset.  Validate
5061   // we have enough arguments, and if not, abort further checking.
5062   unsigned ExpectedNumArgs = (BId == Builtin::BIstrndup ? 2 : 3);
5063   if (Call->getNumArgs() < ExpectedNumArgs)
5064     return;
5065 
5066   unsigned LastArg = (BId == Builtin::BImemset ||
5067                       BId == Builtin::BIstrndup ? 1 : 2);
5068   unsigned LenArg = (BId == Builtin::BIstrndup ? 1 : 2);
5069   const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts();
5070 
5071   if (CheckMemorySizeofForComparison(*this, LenExpr, FnName,
5072                                      Call->getLocStart(), Call->getRParenLoc()))
5073     return;
5074 
5075   // We have special checking when the length is a sizeof expression.
5076   QualType SizeOfArgTy = getSizeOfArgType(LenExpr);
5077   const Expr *SizeOfArg = getSizeOfExprArg(LenExpr);
5078   llvm::FoldingSetNodeID SizeOfArgID;
5079 
5080   for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) {
5081     const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts();
5082     SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange();
5083 
5084     QualType DestTy = Dest->getType();
5085     QualType PointeeTy;
5086     if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) {
5087       PointeeTy = DestPtrTy->getPointeeType();
5088 
5089       // Never warn about void type pointers. This can be used to suppress
5090       // false positives.
5091       if (PointeeTy->isVoidType())
5092         continue;
5093 
5094       // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by
5095       // actually comparing the expressions for equality. Because computing the
5096       // expression IDs can be expensive, we only do this if the diagnostic is
5097       // enabled.
5098       if (SizeOfArg &&
5099           !Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess,
5100                            SizeOfArg->getExprLoc())) {
5101         // We only compute IDs for expressions if the warning is enabled, and
5102         // cache the sizeof arg's ID.
5103         if (SizeOfArgID == llvm::FoldingSetNodeID())
5104           SizeOfArg->Profile(SizeOfArgID, Context, true);
5105         llvm::FoldingSetNodeID DestID;
5106         Dest->Profile(DestID, Context, true);
5107         if (DestID == SizeOfArgID) {
5108           // TODO: For strncpy() and friends, this could suggest sizeof(dst)
5109           //       over sizeof(src) as well.
5110           unsigned ActionIdx = 0; // Default is to suggest dereferencing.
5111           StringRef ReadableName = FnName->getName();
5112 
5113           if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest))
5114             if (UnaryOp->getOpcode() == UO_AddrOf)
5115               ActionIdx = 1; // If its an address-of operator, just remove it.
5116           if (!PointeeTy->isIncompleteType() &&
5117               (Context.getTypeSize(PointeeTy) == Context.getCharWidth()))
5118             ActionIdx = 2; // If the pointee's size is sizeof(char),
5119                            // suggest an explicit length.
5120 
5121           // If the function is defined as a builtin macro, do not show macro
5122           // expansion.
5123           SourceLocation SL = SizeOfArg->getExprLoc();
5124           SourceRange DSR = Dest->getSourceRange();
5125           SourceRange SSR = SizeOfArg->getSourceRange();
5126           SourceManager &SM = getSourceManager();
5127 
5128           if (SM.isMacroArgExpansion(SL)) {
5129             ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts);
5130             SL = SM.getSpellingLoc(SL);
5131             DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()),
5132                              SM.getSpellingLoc(DSR.getEnd()));
5133             SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()),
5134                              SM.getSpellingLoc(SSR.getEnd()));
5135           }
5136 
5137           DiagRuntimeBehavior(SL, SizeOfArg,
5138                               PDiag(diag::warn_sizeof_pointer_expr_memaccess)
5139                                 << ReadableName
5140                                 << PointeeTy
5141                                 << DestTy
5142                                 << DSR
5143                                 << SSR);
5144           DiagRuntimeBehavior(SL, SizeOfArg,
5145                          PDiag(diag::warn_sizeof_pointer_expr_memaccess_note)
5146                                 << ActionIdx
5147                                 << SSR);
5148 
5149           break;
5150         }
5151       }
5152 
5153       // Also check for cases where the sizeof argument is the exact same
5154       // type as the memory argument, and where it points to a user-defined
5155       // record type.
5156       if (SizeOfArgTy != QualType()) {
5157         if (PointeeTy->isRecordType() &&
5158             Context.typesAreCompatible(SizeOfArgTy, DestTy)) {
5159           DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest,
5160                               PDiag(diag::warn_sizeof_pointer_type_memaccess)
5161                                 << FnName << SizeOfArgTy << ArgIdx
5162                                 << PointeeTy << Dest->getSourceRange()
5163                                 << LenExpr->getSourceRange());
5164           break;
5165         }
5166       }
5167     } else if (DestTy->isArrayType()) {
5168       PointeeTy = DestTy;
5169     }
5170 
5171     if (PointeeTy == QualType())
5172       continue;
5173 
5174     // Always complain about dynamic classes.
5175     bool IsContained;
5176     if (const CXXRecordDecl *ContainedRD =
5177             getContainedDynamicClass(PointeeTy, IsContained)) {
5178 
5179       unsigned OperationType = 0;
5180       // "overwritten" if we're warning about the destination for any call
5181       // but memcmp; otherwise a verb appropriate to the call.
5182       if (ArgIdx != 0 || BId == Builtin::BImemcmp) {
5183         if (BId == Builtin::BImemcpy)
5184           OperationType = 1;
5185         else if(BId == Builtin::BImemmove)
5186           OperationType = 2;
5187         else if (BId == Builtin::BImemcmp)
5188           OperationType = 3;
5189       }
5190 
5191       DiagRuntimeBehavior(
5192         Dest->getExprLoc(), Dest,
5193         PDiag(diag::warn_dyn_class_memaccess)
5194           << (BId == Builtin::BImemcmp ? ArgIdx + 2 : ArgIdx)
5195           << FnName << IsContained << ContainedRD << OperationType
5196           << Call->getCallee()->getSourceRange());
5197     } else if (PointeeTy.hasNonTrivialObjCLifetime() &&
5198              BId != Builtin::BImemset)
5199       DiagRuntimeBehavior(
5200         Dest->getExprLoc(), Dest,
5201         PDiag(diag::warn_arc_object_memaccess)
5202           << ArgIdx << FnName << PointeeTy
5203           << Call->getCallee()->getSourceRange());
5204     else
5205       continue;
5206 
5207     DiagRuntimeBehavior(
5208       Dest->getExprLoc(), Dest,
5209       PDiag(diag::note_bad_memaccess_silence)
5210         << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)"));
5211     break;
5212   }
5213 
5214 }
5215 
5216 // A little helper routine: ignore addition and subtraction of integer literals.
5217 // This intentionally does not ignore all integer constant expressions because
5218 // we don't want to remove sizeof().
5219 static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) {
5220   Ex = Ex->IgnoreParenCasts();
5221 
5222   for (;;) {
5223     const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex);
5224     if (!BO || !BO->isAdditiveOp())
5225       break;
5226 
5227     const Expr *RHS = BO->getRHS()->IgnoreParenCasts();
5228     const Expr *LHS = BO->getLHS()->IgnoreParenCasts();
5229 
5230     if (isa<IntegerLiteral>(RHS))
5231       Ex = LHS;
5232     else if (isa<IntegerLiteral>(LHS))
5233       Ex = RHS;
5234     else
5235       break;
5236   }
5237 
5238   return Ex;
5239 }
5240 
5241 static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty,
5242                                                       ASTContext &Context) {
5243   // Only handle constant-sized or VLAs, but not flexible members.
5244   if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) {
5245     // Only issue the FIXIT for arrays of size > 1.
5246     if (CAT->getSize().getSExtValue() <= 1)
5247       return false;
5248   } else if (!Ty->isVariableArrayType()) {
5249     return false;
5250   }
5251   return true;
5252 }
5253 
5254 // Warn if the user has made the 'size' argument to strlcpy or strlcat
5255 // be the size of the source, instead of the destination.
5256 void Sema::CheckStrlcpycatArguments(const CallExpr *Call,
5257                                     IdentifierInfo *FnName) {
5258 
5259   // Don't crash if the user has the wrong number of arguments
5260   unsigned NumArgs = Call->getNumArgs();
5261   if ((NumArgs != 3) && (NumArgs != 4))
5262     return;
5263 
5264   const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context);
5265   const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context);
5266   const Expr *CompareWithSrc = nullptr;
5267 
5268   if (CheckMemorySizeofForComparison(*this, SizeArg, FnName,
5269                                      Call->getLocStart(), Call->getRParenLoc()))
5270     return;
5271 
5272   // Look for 'strlcpy(dst, x, sizeof(x))'
5273   if (const Expr *Ex = getSizeOfExprArg(SizeArg))
5274     CompareWithSrc = Ex;
5275   else {
5276     // Look for 'strlcpy(dst, x, strlen(x))'
5277     if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) {
5278       if (SizeCall->getBuiltinCallee() == Builtin::BIstrlen &&
5279           SizeCall->getNumArgs() == 1)
5280         CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context);
5281     }
5282   }
5283 
5284   if (!CompareWithSrc)
5285     return;
5286 
5287   // Determine if the argument to sizeof/strlen is equal to the source
5288   // argument.  In principle there's all kinds of things you could do
5289   // here, for instance creating an == expression and evaluating it with
5290   // EvaluateAsBooleanCondition, but this uses a more direct technique:
5291   const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg);
5292   if (!SrcArgDRE)
5293     return;
5294 
5295   const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc);
5296   if (!CompareWithSrcDRE ||
5297       SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl())
5298     return;
5299 
5300   const Expr *OriginalSizeArg = Call->getArg(2);
5301   Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size)
5302     << OriginalSizeArg->getSourceRange() << FnName;
5303 
5304   // Output a FIXIT hint if the destination is an array (rather than a
5305   // pointer to an array).  This could be enhanced to handle some
5306   // pointers if we know the actual size, like if DstArg is 'array+2'
5307   // we could say 'sizeof(array)-2'.
5308   const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts();
5309   if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context))
5310     return;
5311 
5312   SmallString<128> sizeString;
5313   llvm::raw_svector_ostream OS(sizeString);
5314   OS << "sizeof(";
5315   DstArg->printPretty(OS, nullptr, getPrintingPolicy());
5316   OS << ")";
5317 
5318   Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size)
5319     << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(),
5320                                     OS.str());
5321 }
5322 
5323 /// Check if two expressions refer to the same declaration.
5324 static bool referToTheSameDecl(const Expr *E1, const Expr *E2) {
5325   if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1))
5326     if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2))
5327       return D1->getDecl() == D2->getDecl();
5328   return false;
5329 }
5330 
5331 static const Expr *getStrlenExprArg(const Expr *E) {
5332   if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
5333     const FunctionDecl *FD = CE->getDirectCallee();
5334     if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen)
5335       return nullptr;
5336     return CE->getArg(0)->IgnoreParenCasts();
5337   }
5338   return nullptr;
5339 }
5340 
5341 // Warn on anti-patterns as the 'size' argument to strncat.
5342 // The correct size argument should look like following:
5343 //   strncat(dst, src, sizeof(dst) - strlen(dest) - 1);
5344 void Sema::CheckStrncatArguments(const CallExpr *CE,
5345                                  IdentifierInfo *FnName) {
5346   // Don't crash if the user has the wrong number of arguments.
5347   if (CE->getNumArgs() < 3)
5348     return;
5349   const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts();
5350   const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts();
5351   const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts();
5352 
5353   if (CheckMemorySizeofForComparison(*this, LenArg, FnName, CE->getLocStart(),
5354                                      CE->getRParenLoc()))
5355     return;
5356 
5357   // Identify common expressions, which are wrongly used as the size argument
5358   // to strncat and may lead to buffer overflows.
5359   unsigned PatternType = 0;
5360   if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) {
5361     // - sizeof(dst)
5362     if (referToTheSameDecl(SizeOfArg, DstArg))
5363       PatternType = 1;
5364     // - sizeof(src)
5365     else if (referToTheSameDecl(SizeOfArg, SrcArg))
5366       PatternType = 2;
5367   } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) {
5368     if (BE->getOpcode() == BO_Sub) {
5369       const Expr *L = BE->getLHS()->IgnoreParenCasts();
5370       const Expr *R = BE->getRHS()->IgnoreParenCasts();
5371       // - sizeof(dst) - strlen(dst)
5372       if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) &&
5373           referToTheSameDecl(DstArg, getStrlenExprArg(R)))
5374         PatternType = 1;
5375       // - sizeof(src) - (anything)
5376       else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L)))
5377         PatternType = 2;
5378     }
5379   }
5380 
5381   if (PatternType == 0)
5382     return;
5383 
5384   // Generate the diagnostic.
5385   SourceLocation SL = LenArg->getLocStart();
5386   SourceRange SR = LenArg->getSourceRange();
5387   SourceManager &SM = getSourceManager();
5388 
5389   // If the function is defined as a builtin macro, do not show macro expansion.
5390   if (SM.isMacroArgExpansion(SL)) {
5391     SL = SM.getSpellingLoc(SL);
5392     SR = SourceRange(SM.getSpellingLoc(SR.getBegin()),
5393                      SM.getSpellingLoc(SR.getEnd()));
5394   }
5395 
5396   // Check if the destination is an array (rather than a pointer to an array).
5397   QualType DstTy = DstArg->getType();
5398   bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy,
5399                                                                     Context);
5400   if (!isKnownSizeArray) {
5401     if (PatternType == 1)
5402       Diag(SL, diag::warn_strncat_wrong_size) << SR;
5403     else
5404       Diag(SL, diag::warn_strncat_src_size) << SR;
5405     return;
5406   }
5407 
5408   if (PatternType == 1)
5409     Diag(SL, diag::warn_strncat_large_size) << SR;
5410   else
5411     Diag(SL, diag::warn_strncat_src_size) << SR;
5412 
5413   SmallString<128> sizeString;
5414   llvm::raw_svector_ostream OS(sizeString);
5415   OS << "sizeof(";
5416   DstArg->printPretty(OS, nullptr, getPrintingPolicy());
5417   OS << ") - ";
5418   OS << "strlen(";
5419   DstArg->printPretty(OS, nullptr, getPrintingPolicy());
5420   OS << ") - 1";
5421 
5422   Diag(SL, diag::note_strncat_wrong_size)
5423     << FixItHint::CreateReplacement(SR, OS.str());
5424 }
5425 
5426 //===--- CHECK: Return Address of Stack Variable --------------------------===//
5427 
5428 static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars,
5429                      Decl *ParentDecl);
5430 static Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars,
5431                       Decl *ParentDecl);
5432 
5433 /// CheckReturnStackAddr - Check if a return statement returns the address
5434 ///   of a stack variable.
5435 static void
5436 CheckReturnStackAddr(Sema &S, Expr *RetValExp, QualType lhsType,
5437                      SourceLocation ReturnLoc) {
5438 
5439   Expr *stackE = nullptr;
5440   SmallVector<DeclRefExpr *, 8> refVars;
5441 
5442   // Perform checking for returned stack addresses, local blocks,
5443   // label addresses or references to temporaries.
5444   if (lhsType->isPointerType() ||
5445       (!S.getLangOpts().ObjCAutoRefCount && lhsType->isBlockPointerType())) {
5446     stackE = EvalAddr(RetValExp, refVars, /*ParentDecl=*/nullptr);
5447   } else if (lhsType->isReferenceType()) {
5448     stackE = EvalVal(RetValExp, refVars, /*ParentDecl=*/nullptr);
5449   }
5450 
5451   if (!stackE)
5452     return; // Nothing suspicious was found.
5453 
5454   SourceLocation diagLoc;
5455   SourceRange diagRange;
5456   if (refVars.empty()) {
5457     diagLoc = stackE->getLocStart();
5458     diagRange = stackE->getSourceRange();
5459   } else {
5460     // We followed through a reference variable. 'stackE' contains the
5461     // problematic expression but we will warn at the return statement pointing
5462     // at the reference variable. We will later display the "trail" of
5463     // reference variables using notes.
5464     diagLoc = refVars[0]->getLocStart();
5465     diagRange = refVars[0]->getSourceRange();
5466   }
5467 
5468   if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var.
5469     S.Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref
5470                                              : diag::warn_ret_stack_addr)
5471      << DR->getDecl()->getDeclName() << diagRange;
5472   } else if (isa<BlockExpr>(stackE)) { // local block.
5473     S.Diag(diagLoc, diag::err_ret_local_block) << diagRange;
5474   } else if (isa<AddrLabelExpr>(stackE)) { // address of label.
5475     S.Diag(diagLoc, diag::warn_ret_addr_label) << diagRange;
5476   } else { // local temporary.
5477     S.Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref
5478                                                : diag::warn_ret_local_temp_addr)
5479      << diagRange;
5480   }
5481 
5482   // Display the "trail" of reference variables that we followed until we
5483   // found the problematic expression using notes.
5484   for (unsigned i = 0, e = refVars.size(); i != e; ++i) {
5485     VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl());
5486     // If this var binds to another reference var, show the range of the next
5487     // var, otherwise the var binds to the problematic expression, in which case
5488     // show the range of the expression.
5489     SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange()
5490                                   : stackE->getSourceRange();
5491     S.Diag(VD->getLocation(), diag::note_ref_var_local_bind)
5492         << VD->getDeclName() << range;
5493   }
5494 }
5495 
5496 /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that
5497 ///  check if the expression in a return statement evaluates to an address
5498 ///  to a location on the stack, a local block, an address of a label, or a
5499 ///  reference to local temporary. The recursion is used to traverse the
5500 ///  AST of the return expression, with recursion backtracking when we
5501 ///  encounter a subexpression that (1) clearly does not lead to one of the
5502 ///  above problematic expressions (2) is something we cannot determine leads to
5503 ///  a problematic expression based on such local checking.
5504 ///
5505 ///  Both EvalAddr and EvalVal follow through reference variables to evaluate
5506 ///  the expression that they point to. Such variables are added to the
5507 ///  'refVars' vector so that we know what the reference variable "trail" was.
5508 ///
5509 ///  EvalAddr processes expressions that are pointers that are used as
5510 ///  references (and not L-values).  EvalVal handles all other values.
5511 ///  At the base case of the recursion is a check for the above problematic
5512 ///  expressions.
5513 ///
5514 ///  This implementation handles:
5515 ///
5516 ///   * pointer-to-pointer casts
5517 ///   * implicit conversions from array references to pointers
5518 ///   * taking the address of fields
5519 ///   * arbitrary interplay between "&" and "*" operators
5520 ///   * pointer arithmetic from an address of a stack variable
5521 ///   * taking the address of an array element where the array is on the stack
5522 static Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars,
5523                       Decl *ParentDecl) {
5524   if (E->isTypeDependent())
5525     return nullptr;
5526 
5527   // We should only be called for evaluating pointer expressions.
5528   assert((E->getType()->isAnyPointerType() ||
5529           E->getType()->isBlockPointerType() ||
5530           E->getType()->isObjCQualifiedIdType()) &&
5531          "EvalAddr only works on pointers");
5532 
5533   E = E->IgnoreParens();
5534 
5535   // Our "symbolic interpreter" is just a dispatch off the currently
5536   // viewed AST node.  We then recursively traverse the AST by calling
5537   // EvalAddr and EvalVal appropriately.
5538   switch (E->getStmtClass()) {
5539   case Stmt::DeclRefExprClass: {
5540     DeclRefExpr *DR = cast<DeclRefExpr>(E);
5541 
5542     // If we leave the immediate function, the lifetime isn't about to end.
5543     if (DR->refersToEnclosingVariableOrCapture())
5544       return nullptr;
5545 
5546     if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
5547       // If this is a reference variable, follow through to the expression that
5548       // it points to.
5549       if (V->hasLocalStorage() &&
5550           V->getType()->isReferenceType() && V->hasInit()) {
5551         // Add the reference variable to the "trail".
5552         refVars.push_back(DR);
5553         return EvalAddr(V->getInit(), refVars, ParentDecl);
5554       }
5555 
5556     return nullptr;
5557   }
5558 
5559   case Stmt::UnaryOperatorClass: {
5560     // The only unary operator that make sense to handle here
5561     // is AddrOf.  All others don't make sense as pointers.
5562     UnaryOperator *U = cast<UnaryOperator>(E);
5563 
5564     if (U->getOpcode() == UO_AddrOf)
5565       return EvalVal(U->getSubExpr(), refVars, ParentDecl);
5566     else
5567       return nullptr;
5568   }
5569 
5570   case Stmt::BinaryOperatorClass: {
5571     // Handle pointer arithmetic.  All other binary operators are not valid
5572     // in this context.
5573     BinaryOperator *B = cast<BinaryOperator>(E);
5574     BinaryOperatorKind op = B->getOpcode();
5575 
5576     if (op != BO_Add && op != BO_Sub)
5577       return nullptr;
5578 
5579     Expr *Base = B->getLHS();
5580 
5581     // Determine which argument is the real pointer base.  It could be
5582     // the RHS argument instead of the LHS.
5583     if (!Base->getType()->isPointerType()) Base = B->getRHS();
5584 
5585     assert (Base->getType()->isPointerType());
5586     return EvalAddr(Base, refVars, ParentDecl);
5587   }
5588 
5589   // For conditional operators we need to see if either the LHS or RHS are
5590   // valid DeclRefExpr*s.  If one of them is valid, we return it.
5591   case Stmt::ConditionalOperatorClass: {
5592     ConditionalOperator *C = cast<ConditionalOperator>(E);
5593 
5594     // Handle the GNU extension for missing LHS.
5595     // FIXME: That isn't a ConditionalOperator, so doesn't get here.
5596     if (Expr *LHSExpr = C->getLHS()) {
5597       // In C++, we can have a throw-expression, which has 'void' type.
5598       if (!LHSExpr->getType()->isVoidType())
5599         if (Expr *LHS = EvalAddr(LHSExpr, refVars, ParentDecl))
5600           return LHS;
5601     }
5602 
5603     // In C++, we can have a throw-expression, which has 'void' type.
5604     if (C->getRHS()->getType()->isVoidType())
5605       return nullptr;
5606 
5607     return EvalAddr(C->getRHS(), refVars, ParentDecl);
5608   }
5609 
5610   case Stmt::BlockExprClass:
5611     if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures())
5612       return E; // local block.
5613     return nullptr;
5614 
5615   case Stmt::AddrLabelExprClass:
5616     return E; // address of label.
5617 
5618   case Stmt::ExprWithCleanupsClass:
5619     return EvalAddr(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,
5620                     ParentDecl);
5621 
5622   // For casts, we need to handle conversions from arrays to
5623   // pointer values, and pointer-to-pointer conversions.
5624   case Stmt::ImplicitCastExprClass:
5625   case Stmt::CStyleCastExprClass:
5626   case Stmt::CXXFunctionalCastExprClass:
5627   case Stmt::ObjCBridgedCastExprClass:
5628   case Stmt::CXXStaticCastExprClass:
5629   case Stmt::CXXDynamicCastExprClass:
5630   case Stmt::CXXConstCastExprClass:
5631   case Stmt::CXXReinterpretCastExprClass: {
5632     Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
5633     switch (cast<CastExpr>(E)->getCastKind()) {
5634     case CK_LValueToRValue:
5635     case CK_NoOp:
5636     case CK_BaseToDerived:
5637     case CK_DerivedToBase:
5638     case CK_UncheckedDerivedToBase:
5639     case CK_Dynamic:
5640     case CK_CPointerToObjCPointerCast:
5641     case CK_BlockPointerToObjCPointerCast:
5642     case CK_AnyPointerToBlockPointerCast:
5643       return EvalAddr(SubExpr, refVars, ParentDecl);
5644 
5645     case CK_ArrayToPointerDecay:
5646       return EvalVal(SubExpr, refVars, ParentDecl);
5647 
5648     case CK_BitCast:
5649       if (SubExpr->getType()->isAnyPointerType() ||
5650           SubExpr->getType()->isBlockPointerType() ||
5651           SubExpr->getType()->isObjCQualifiedIdType())
5652         return EvalAddr(SubExpr, refVars, ParentDecl);
5653       else
5654         return nullptr;
5655 
5656     default:
5657       return nullptr;
5658     }
5659   }
5660 
5661   case Stmt::MaterializeTemporaryExprClass:
5662     if (Expr *Result = EvalAddr(
5663                          cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(),
5664                                 refVars, ParentDecl))
5665       return Result;
5666 
5667     return E;
5668 
5669   // Everything else: we simply don't reason about them.
5670   default:
5671     return nullptr;
5672   }
5673 }
5674 
5675 
5676 ///  EvalVal - This function is complements EvalAddr in the mutual recursion.
5677 ///   See the comments for EvalAddr for more details.
5678 static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars,
5679                      Decl *ParentDecl) {
5680 do {
5681   // We should only be called for evaluating non-pointer expressions, or
5682   // expressions with a pointer type that are not used as references but instead
5683   // are l-values (e.g., DeclRefExpr with a pointer type).
5684 
5685   // Our "symbolic interpreter" is just a dispatch off the currently
5686   // viewed AST node.  We then recursively traverse the AST by calling
5687   // EvalAddr and EvalVal appropriately.
5688 
5689   E = E->IgnoreParens();
5690   switch (E->getStmtClass()) {
5691   case Stmt::ImplicitCastExprClass: {
5692     ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E);
5693     if (IE->getValueKind() == VK_LValue) {
5694       E = IE->getSubExpr();
5695       continue;
5696     }
5697     return nullptr;
5698   }
5699 
5700   case Stmt::ExprWithCleanupsClass:
5701     return EvalVal(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,ParentDecl);
5702 
5703   case Stmt::DeclRefExprClass: {
5704     // When we hit a DeclRefExpr we are looking at code that refers to a
5705     // variable's name. If it's not a reference variable we check if it has
5706     // local storage within the function, and if so, return the expression.
5707     DeclRefExpr *DR = cast<DeclRefExpr>(E);
5708 
5709     // If we leave the immediate function, the lifetime isn't about to end.
5710     if (DR->refersToEnclosingVariableOrCapture())
5711       return nullptr;
5712 
5713     if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) {
5714       // Check if it refers to itself, e.g. "int& i = i;".
5715       if (V == ParentDecl)
5716         return DR;
5717 
5718       if (V->hasLocalStorage()) {
5719         if (!V->getType()->isReferenceType())
5720           return DR;
5721 
5722         // Reference variable, follow through to the expression that
5723         // it points to.
5724         if (V->hasInit()) {
5725           // Add the reference variable to the "trail".
5726           refVars.push_back(DR);
5727           return EvalVal(V->getInit(), refVars, V);
5728         }
5729       }
5730     }
5731 
5732     return nullptr;
5733   }
5734 
5735   case Stmt::UnaryOperatorClass: {
5736     // The only unary operator that make sense to handle here
5737     // is Deref.  All others don't resolve to a "name."  This includes
5738     // handling all sorts of rvalues passed to a unary operator.
5739     UnaryOperator *U = cast<UnaryOperator>(E);
5740 
5741     if (U->getOpcode() == UO_Deref)
5742       return EvalAddr(U->getSubExpr(), refVars, ParentDecl);
5743 
5744     return nullptr;
5745   }
5746 
5747   case Stmt::ArraySubscriptExprClass: {
5748     // Array subscripts are potential references to data on the stack.  We
5749     // retrieve the DeclRefExpr* for the array variable if it indeed
5750     // has local storage.
5751     return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars,ParentDecl);
5752   }
5753 
5754   case Stmt::OMPArraySectionExprClass: {
5755     return EvalAddr(cast<OMPArraySectionExpr>(E)->getBase(), refVars,
5756                     ParentDecl);
5757   }
5758 
5759   case Stmt::ConditionalOperatorClass: {
5760     // For conditional operators we need to see if either the LHS or RHS are
5761     // non-NULL Expr's.  If one is non-NULL, we return it.
5762     ConditionalOperator *C = cast<ConditionalOperator>(E);
5763 
5764     // Handle the GNU extension for missing LHS.
5765     if (Expr *LHSExpr = C->getLHS()) {
5766       // In C++, we can have a throw-expression, which has 'void' type.
5767       if (!LHSExpr->getType()->isVoidType())
5768         if (Expr *LHS = EvalVal(LHSExpr, refVars, ParentDecl))
5769           return LHS;
5770     }
5771 
5772     // In C++, we can have a throw-expression, which has 'void' type.
5773     if (C->getRHS()->getType()->isVoidType())
5774       return nullptr;
5775 
5776     return EvalVal(C->getRHS(), refVars, ParentDecl);
5777   }
5778 
5779   // Accesses to members are potential references to data on the stack.
5780   case Stmt::MemberExprClass: {
5781     MemberExpr *M = cast<MemberExpr>(E);
5782 
5783     // Check for indirect access.  We only want direct field accesses.
5784     if (M->isArrow())
5785       return nullptr;
5786 
5787     // Check whether the member type is itself a reference, in which case
5788     // we're not going to refer to the member, but to what the member refers to.
5789     if (M->getMemberDecl()->getType()->isReferenceType())
5790       return nullptr;
5791 
5792     return EvalVal(M->getBase(), refVars, ParentDecl);
5793   }
5794 
5795   case Stmt::MaterializeTemporaryExprClass:
5796     if (Expr *Result = EvalVal(
5797                           cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(),
5798                                refVars, ParentDecl))
5799       return Result;
5800 
5801     return E;
5802 
5803   default:
5804     // Check that we don't return or take the address of a reference to a
5805     // temporary. This is only useful in C++.
5806     if (!E->isTypeDependent() && E->isRValue())
5807       return E;
5808 
5809     // Everything else: we simply don't reason about them.
5810     return nullptr;
5811   }
5812 } while (true);
5813 }
5814 
5815 void
5816 Sema::CheckReturnValExpr(Expr *RetValExp, QualType lhsType,
5817                          SourceLocation ReturnLoc,
5818                          bool isObjCMethod,
5819                          const AttrVec *Attrs,
5820                          const FunctionDecl *FD) {
5821   CheckReturnStackAddr(*this, RetValExp, lhsType, ReturnLoc);
5822 
5823   // Check if the return value is null but should not be.
5824   if (((Attrs && hasSpecificAttr<ReturnsNonNullAttr>(*Attrs)) ||
5825        (!isObjCMethod && isNonNullType(Context, lhsType))) &&
5826       CheckNonNullExpr(*this, RetValExp))
5827     Diag(ReturnLoc, diag::warn_null_ret)
5828       << (isObjCMethod ? 1 : 0) << RetValExp->getSourceRange();
5829 
5830   // C++11 [basic.stc.dynamic.allocation]p4:
5831   //   If an allocation function declared with a non-throwing
5832   //   exception-specification fails to allocate storage, it shall return
5833   //   a null pointer. Any other allocation function that fails to allocate
5834   //   storage shall indicate failure only by throwing an exception [...]
5835   if (FD) {
5836     OverloadedOperatorKind Op = FD->getOverloadedOperator();
5837     if (Op == OO_New || Op == OO_Array_New) {
5838       const FunctionProtoType *Proto
5839         = FD->getType()->castAs<FunctionProtoType>();
5840       if (!Proto->isNothrow(Context, /*ResultIfDependent*/true) &&
5841           CheckNonNullExpr(*this, RetValExp))
5842         Diag(ReturnLoc, diag::warn_operator_new_returns_null)
5843           << FD << getLangOpts().CPlusPlus11;
5844     }
5845   }
5846 }
5847 
5848 //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===//
5849 
5850 /// Check for comparisons of floating point operands using != and ==.
5851 /// Issue a warning if these are no self-comparisons, as they are not likely
5852 /// to do what the programmer intended.
5853 void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) {
5854   Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts();
5855   Expr* RightExprSansParen = RHS->IgnoreParenImpCasts();
5856 
5857   // Special case: check for x == x (which is OK).
5858   // Do not emit warnings for such cases.
5859   if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen))
5860     if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen))
5861       if (DRL->getDecl() == DRR->getDecl())
5862         return;
5863 
5864 
5865   // Special case: check for comparisons against literals that can be exactly
5866   //  represented by APFloat.  In such cases, do not emit a warning.  This
5867   //  is a heuristic: often comparison against such literals are used to
5868   //  detect if a value in a variable has not changed.  This clearly can
5869   //  lead to false negatives.
5870   if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) {
5871     if (FLL->isExact())
5872       return;
5873   } else
5874     if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen))
5875       if (FLR->isExact())
5876         return;
5877 
5878   // Check for comparisons with builtin types.
5879   if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen))
5880     if (CL->getBuiltinCallee())
5881       return;
5882 
5883   if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen))
5884     if (CR->getBuiltinCallee())
5885       return;
5886 
5887   // Emit the diagnostic.
5888   Diag(Loc, diag::warn_floatingpoint_eq)
5889     << LHS->getSourceRange() << RHS->getSourceRange();
5890 }
5891 
5892 //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===//
5893 //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===//
5894 
5895 namespace {
5896 
5897 /// Structure recording the 'active' range of an integer-valued
5898 /// expression.
5899 struct IntRange {
5900   /// The number of bits active in the int.
5901   unsigned Width;
5902 
5903   /// True if the int is known not to have negative values.
5904   bool NonNegative;
5905 
5906   IntRange(unsigned Width, bool NonNegative)
5907     : Width(Width), NonNegative(NonNegative)
5908   {}
5909 
5910   /// Returns the range of the bool type.
5911   static IntRange forBoolType() {
5912     return IntRange(1, true);
5913   }
5914 
5915   /// Returns the range of an opaque value of the given integral type.
5916   static IntRange forValueOfType(ASTContext &C, QualType T) {
5917     return forValueOfCanonicalType(C,
5918                           T->getCanonicalTypeInternal().getTypePtr());
5919   }
5920 
5921   /// Returns the range of an opaque value of a canonical integral type.
5922   static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) {
5923     assert(T->isCanonicalUnqualified());
5924 
5925     if (const VectorType *VT = dyn_cast<VectorType>(T))
5926       T = VT->getElementType().getTypePtr();
5927     if (const ComplexType *CT = dyn_cast<ComplexType>(T))
5928       T = CT->getElementType().getTypePtr();
5929     if (const AtomicType *AT = dyn_cast<AtomicType>(T))
5930       T = AT->getValueType().getTypePtr();
5931 
5932     // For enum types, use the known bit width of the enumerators.
5933     if (const EnumType *ET = dyn_cast<EnumType>(T)) {
5934       EnumDecl *Enum = ET->getDecl();
5935       if (!Enum->isCompleteDefinition())
5936         return IntRange(C.getIntWidth(QualType(T, 0)), false);
5937 
5938       unsigned NumPositive = Enum->getNumPositiveBits();
5939       unsigned NumNegative = Enum->getNumNegativeBits();
5940 
5941       if (NumNegative == 0)
5942         return IntRange(NumPositive, true/*NonNegative*/);
5943       else
5944         return IntRange(std::max(NumPositive + 1, NumNegative),
5945                         false/*NonNegative*/);
5946     }
5947 
5948     const BuiltinType *BT = cast<BuiltinType>(T);
5949     assert(BT->isInteger());
5950 
5951     return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
5952   }
5953 
5954   /// Returns the "target" range of a canonical integral type, i.e.
5955   /// the range of values expressible in the type.
5956   ///
5957   /// This matches forValueOfCanonicalType except that enums have the
5958   /// full range of their type, not the range of their enumerators.
5959   static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) {
5960     assert(T->isCanonicalUnqualified());
5961 
5962     if (const VectorType *VT = dyn_cast<VectorType>(T))
5963       T = VT->getElementType().getTypePtr();
5964     if (const ComplexType *CT = dyn_cast<ComplexType>(T))
5965       T = CT->getElementType().getTypePtr();
5966     if (const AtomicType *AT = dyn_cast<AtomicType>(T))
5967       T = AT->getValueType().getTypePtr();
5968     if (const EnumType *ET = dyn_cast<EnumType>(T))
5969       T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr();
5970 
5971     const BuiltinType *BT = cast<BuiltinType>(T);
5972     assert(BT->isInteger());
5973 
5974     return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
5975   }
5976 
5977   /// Returns the supremum of two ranges: i.e. their conservative merge.
5978   static IntRange join(IntRange L, IntRange R) {
5979     return IntRange(std::max(L.Width, R.Width),
5980                     L.NonNegative && R.NonNegative);
5981   }
5982 
5983   /// Returns the infinum of two ranges: i.e. their aggressive merge.
5984   static IntRange meet(IntRange L, IntRange R) {
5985     return IntRange(std::min(L.Width, R.Width),
5986                     L.NonNegative || R.NonNegative);
5987   }
5988 };
5989 
5990 static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value,
5991                               unsigned MaxWidth) {
5992   if (value.isSigned() && value.isNegative())
5993     return IntRange(value.getMinSignedBits(), false);
5994 
5995   if (value.getBitWidth() > MaxWidth)
5996     value = value.trunc(MaxWidth);
5997 
5998   // isNonNegative() just checks the sign bit without considering
5999   // signedness.
6000   return IntRange(value.getActiveBits(), true);
6001 }
6002 
6003 static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty,
6004                               unsigned MaxWidth) {
6005   if (result.isInt())
6006     return GetValueRange(C, result.getInt(), MaxWidth);
6007 
6008   if (result.isVector()) {
6009     IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth);
6010     for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) {
6011       IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth);
6012       R = IntRange::join(R, El);
6013     }
6014     return R;
6015   }
6016 
6017   if (result.isComplexInt()) {
6018     IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth);
6019     IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth);
6020     return IntRange::join(R, I);
6021   }
6022 
6023   // This can happen with lossless casts to intptr_t of "based" lvalues.
6024   // Assume it might use arbitrary bits.
6025   // FIXME: The only reason we need to pass the type in here is to get
6026   // the sign right on this one case.  It would be nice if APValue
6027   // preserved this.
6028   assert(result.isLValue() || result.isAddrLabelDiff());
6029   return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType());
6030 }
6031 
6032 static QualType GetExprType(Expr *E) {
6033   QualType Ty = E->getType();
6034   if (const AtomicType *AtomicRHS = Ty->getAs<AtomicType>())
6035     Ty = AtomicRHS->getValueType();
6036   return Ty;
6037 }
6038 
6039 /// Pseudo-evaluate the given integer expression, estimating the
6040 /// range of values it might take.
6041 ///
6042 /// \param MaxWidth - the width to which the value will be truncated
6043 static IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) {
6044   E = E->IgnoreParens();
6045 
6046   // Try a full evaluation first.
6047   Expr::EvalResult result;
6048   if (E->EvaluateAsRValue(result, C))
6049     return GetValueRange(C, result.Val, GetExprType(E), MaxWidth);
6050 
6051   // I think we only want to look through implicit casts here; if the
6052   // user has an explicit widening cast, we should treat the value as
6053   // being of the new, wider type.
6054   if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) {
6055     if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue)
6056       return GetExprRange(C, CE->getSubExpr(), MaxWidth);
6057 
6058     IntRange OutputTypeRange = IntRange::forValueOfType(C, GetExprType(CE));
6059 
6060     bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast);
6061 
6062     // Assume that non-integer casts can span the full range of the type.
6063     if (!isIntegerCast)
6064       return OutputTypeRange;
6065 
6066     IntRange SubRange
6067       = GetExprRange(C, CE->getSubExpr(),
6068                      std::min(MaxWidth, OutputTypeRange.Width));
6069 
6070     // Bail out if the subexpr's range is as wide as the cast type.
6071     if (SubRange.Width >= OutputTypeRange.Width)
6072       return OutputTypeRange;
6073 
6074     // Otherwise, we take the smaller width, and we're non-negative if
6075     // either the output type or the subexpr is.
6076     return IntRange(SubRange.Width,
6077                     SubRange.NonNegative || OutputTypeRange.NonNegative);
6078   }
6079 
6080   if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
6081     // If we can fold the condition, just take that operand.
6082     bool CondResult;
6083     if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C))
6084       return GetExprRange(C, CondResult ? CO->getTrueExpr()
6085                                         : CO->getFalseExpr(),
6086                           MaxWidth);
6087 
6088     // Otherwise, conservatively merge.
6089     IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth);
6090     IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth);
6091     return IntRange::join(L, R);
6092   }
6093 
6094   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
6095     switch (BO->getOpcode()) {
6096 
6097     // Boolean-valued operations are single-bit and positive.
6098     case BO_LAnd:
6099     case BO_LOr:
6100     case BO_LT:
6101     case BO_GT:
6102     case BO_LE:
6103     case BO_GE:
6104     case BO_EQ:
6105     case BO_NE:
6106       return IntRange::forBoolType();
6107 
6108     // The type of the assignments is the type of the LHS, so the RHS
6109     // is not necessarily the same type.
6110     case BO_MulAssign:
6111     case BO_DivAssign:
6112     case BO_RemAssign:
6113     case BO_AddAssign:
6114     case BO_SubAssign:
6115     case BO_XorAssign:
6116     case BO_OrAssign:
6117       // TODO: bitfields?
6118       return IntRange::forValueOfType(C, GetExprType(E));
6119 
6120     // Simple assignments just pass through the RHS, which will have
6121     // been coerced to the LHS type.
6122     case BO_Assign:
6123       // TODO: bitfields?
6124       return GetExprRange(C, BO->getRHS(), MaxWidth);
6125 
6126     // Operations with opaque sources are black-listed.
6127     case BO_PtrMemD:
6128     case BO_PtrMemI:
6129       return IntRange::forValueOfType(C, GetExprType(E));
6130 
6131     // Bitwise-and uses the *infinum* of the two source ranges.
6132     case BO_And:
6133     case BO_AndAssign:
6134       return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth),
6135                             GetExprRange(C, BO->getRHS(), MaxWidth));
6136 
6137     // Left shift gets black-listed based on a judgement call.
6138     case BO_Shl:
6139       // ...except that we want to treat '1 << (blah)' as logically
6140       // positive.  It's an important idiom.
6141       if (IntegerLiteral *I
6142             = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) {
6143         if (I->getValue() == 1) {
6144           IntRange R = IntRange::forValueOfType(C, GetExprType(E));
6145           return IntRange(R.Width, /*NonNegative*/ true);
6146         }
6147       }
6148       // fallthrough
6149 
6150     case BO_ShlAssign:
6151       return IntRange::forValueOfType(C, GetExprType(E));
6152 
6153     // Right shift by a constant can narrow its left argument.
6154     case BO_Shr:
6155     case BO_ShrAssign: {
6156       IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth);
6157 
6158       // If the shift amount is a positive constant, drop the width by
6159       // that much.
6160       llvm::APSInt shift;
6161       if (BO->getRHS()->isIntegerConstantExpr(shift, C) &&
6162           shift.isNonNegative()) {
6163         unsigned zext = shift.getZExtValue();
6164         if (zext >= L.Width)
6165           L.Width = (L.NonNegative ? 0 : 1);
6166         else
6167           L.Width -= zext;
6168       }
6169 
6170       return L;
6171     }
6172 
6173     // Comma acts as its right operand.
6174     case BO_Comma:
6175       return GetExprRange(C, BO->getRHS(), MaxWidth);
6176 
6177     // Black-list pointer subtractions.
6178     case BO_Sub:
6179       if (BO->getLHS()->getType()->isPointerType())
6180         return IntRange::forValueOfType(C, GetExprType(E));
6181       break;
6182 
6183     // The width of a division result is mostly determined by the size
6184     // of the LHS.
6185     case BO_Div: {
6186       // Don't 'pre-truncate' the operands.
6187       unsigned opWidth = C.getIntWidth(GetExprType(E));
6188       IntRange L = GetExprRange(C, BO->getLHS(), opWidth);
6189 
6190       // If the divisor is constant, use that.
6191       llvm::APSInt divisor;
6192       if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) {
6193         unsigned log2 = divisor.logBase2(); // floor(log_2(divisor))
6194         if (log2 >= L.Width)
6195           L.Width = (L.NonNegative ? 0 : 1);
6196         else
6197           L.Width = std::min(L.Width - log2, MaxWidth);
6198         return L;
6199       }
6200 
6201       // Otherwise, just use the LHS's width.
6202       IntRange R = GetExprRange(C, BO->getRHS(), opWidth);
6203       return IntRange(L.Width, L.NonNegative && R.NonNegative);
6204     }
6205 
6206     // The result of a remainder can't be larger than the result of
6207     // either side.
6208     case BO_Rem: {
6209       // Don't 'pre-truncate' the operands.
6210       unsigned opWidth = C.getIntWidth(GetExprType(E));
6211       IntRange L = GetExprRange(C, BO->getLHS(), opWidth);
6212       IntRange R = GetExprRange(C, BO->getRHS(), opWidth);
6213 
6214       IntRange meet = IntRange::meet(L, R);
6215       meet.Width = std::min(meet.Width, MaxWidth);
6216       return meet;
6217     }
6218 
6219     // The default behavior is okay for these.
6220     case BO_Mul:
6221     case BO_Add:
6222     case BO_Xor:
6223     case BO_Or:
6224       break;
6225     }
6226 
6227     // The default case is to treat the operation as if it were closed
6228     // on the narrowest type that encompasses both operands.
6229     IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth);
6230     IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth);
6231     return IntRange::join(L, R);
6232   }
6233 
6234   if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
6235     switch (UO->getOpcode()) {
6236     // Boolean-valued operations are white-listed.
6237     case UO_LNot:
6238       return IntRange::forBoolType();
6239 
6240     // Operations with opaque sources are black-listed.
6241     case UO_Deref:
6242     case UO_AddrOf: // should be impossible
6243       return IntRange::forValueOfType(C, GetExprType(E));
6244 
6245     default:
6246       return GetExprRange(C, UO->getSubExpr(), MaxWidth);
6247     }
6248   }
6249 
6250   if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
6251     return GetExprRange(C, OVE->getSourceExpr(), MaxWidth);
6252 
6253   if (FieldDecl *BitField = E->getSourceBitField())
6254     return IntRange(BitField->getBitWidthValue(C),
6255                     BitField->getType()->isUnsignedIntegerOrEnumerationType());
6256 
6257   return IntRange::forValueOfType(C, GetExprType(E));
6258 }
6259 
6260 static IntRange GetExprRange(ASTContext &C, Expr *E) {
6261   return GetExprRange(C, E, C.getIntWidth(GetExprType(E)));
6262 }
6263 
6264 /// Checks whether the given value, which currently has the given
6265 /// source semantics, has the same value when coerced through the
6266 /// target semantics.
6267 static bool IsSameFloatAfterCast(const llvm::APFloat &value,
6268                                  const llvm::fltSemantics &Src,
6269                                  const llvm::fltSemantics &Tgt) {
6270   llvm::APFloat truncated = value;
6271 
6272   bool ignored;
6273   truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored);
6274   truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored);
6275 
6276   return truncated.bitwiseIsEqual(value);
6277 }
6278 
6279 /// Checks whether the given value, which currently has the given
6280 /// source semantics, has the same value when coerced through the
6281 /// target semantics.
6282 ///
6283 /// The value might be a vector of floats (or a complex number).
6284 static bool IsSameFloatAfterCast(const APValue &value,
6285                                  const llvm::fltSemantics &Src,
6286                                  const llvm::fltSemantics &Tgt) {
6287   if (value.isFloat())
6288     return IsSameFloatAfterCast(value.getFloat(), Src, Tgt);
6289 
6290   if (value.isVector()) {
6291     for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i)
6292       if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt))
6293         return false;
6294     return true;
6295   }
6296 
6297   assert(value.isComplexFloat());
6298   return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) &&
6299           IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt));
6300 }
6301 
6302 static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC);
6303 
6304 static bool IsZero(Sema &S, Expr *E) {
6305   // Suppress cases where we are comparing against an enum constant.
6306   if (const DeclRefExpr *DR =
6307       dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
6308     if (isa<EnumConstantDecl>(DR->getDecl()))
6309       return false;
6310 
6311   // Suppress cases where the '0' value is expanded from a macro.
6312   if (E->getLocStart().isMacroID())
6313     return false;
6314 
6315   llvm::APSInt Value;
6316   return E->isIntegerConstantExpr(Value, S.Context) && Value == 0;
6317 }
6318 
6319 static bool HasEnumType(Expr *E) {
6320   // Strip off implicit integral promotions.
6321   while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
6322     if (ICE->getCastKind() != CK_IntegralCast &&
6323         ICE->getCastKind() != CK_NoOp)
6324       break;
6325     E = ICE->getSubExpr();
6326   }
6327 
6328   return E->getType()->isEnumeralType();
6329 }
6330 
6331 static void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) {
6332   // Disable warning in template instantiations.
6333   if (!S.ActiveTemplateInstantiations.empty())
6334     return;
6335 
6336   BinaryOperatorKind op = E->getOpcode();
6337   if (E->isValueDependent())
6338     return;
6339 
6340   if (op == BO_LT && IsZero(S, E->getRHS())) {
6341     S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
6342       << "< 0" << "false" << HasEnumType(E->getLHS())
6343       << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
6344   } else if (op == BO_GE && IsZero(S, E->getRHS())) {
6345     S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
6346       << ">= 0" << "true" << HasEnumType(E->getLHS())
6347       << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
6348   } else if (op == BO_GT && IsZero(S, E->getLHS())) {
6349     S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison)
6350       << "0 >" << "false" << HasEnumType(E->getRHS())
6351       << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
6352   } else if (op == BO_LE && IsZero(S, E->getLHS())) {
6353     S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison)
6354       << "0 <=" << "true" << HasEnumType(E->getRHS())
6355       << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
6356   }
6357 }
6358 
6359 static void DiagnoseOutOfRangeComparison(Sema &S, BinaryOperator *E,
6360                                          Expr *Constant, Expr *Other,
6361                                          llvm::APSInt Value,
6362                                          bool RhsConstant) {
6363   // Disable warning in template instantiations.
6364   if (!S.ActiveTemplateInstantiations.empty())
6365     return;
6366 
6367   // TODO: Investigate using GetExprRange() to get tighter bounds
6368   // on the bit ranges.
6369   QualType OtherT = Other->getType();
6370   if (const auto *AT = OtherT->getAs<AtomicType>())
6371     OtherT = AT->getValueType();
6372   IntRange OtherRange = IntRange::forValueOfType(S.Context, OtherT);
6373   unsigned OtherWidth = OtherRange.Width;
6374 
6375   bool OtherIsBooleanType = Other->isKnownToHaveBooleanValue();
6376 
6377   // 0 values are handled later by CheckTrivialUnsignedComparison().
6378   if ((Value == 0) && (!OtherIsBooleanType))
6379     return;
6380 
6381   BinaryOperatorKind op = E->getOpcode();
6382   bool IsTrue = true;
6383 
6384   // Used for diagnostic printout.
6385   enum {
6386     LiteralConstant = 0,
6387     CXXBoolLiteralTrue,
6388     CXXBoolLiteralFalse
6389   } LiteralOrBoolConstant = LiteralConstant;
6390 
6391   if (!OtherIsBooleanType) {
6392     QualType ConstantT = Constant->getType();
6393     QualType CommonT = E->getLHS()->getType();
6394 
6395     if (S.Context.hasSameUnqualifiedType(OtherT, ConstantT))
6396       return;
6397     assert((OtherT->isIntegerType() && ConstantT->isIntegerType()) &&
6398            "comparison with non-integer type");
6399 
6400     bool ConstantSigned = ConstantT->isSignedIntegerType();
6401     bool CommonSigned = CommonT->isSignedIntegerType();
6402 
6403     bool EqualityOnly = false;
6404 
6405     if (CommonSigned) {
6406       // The common type is signed, therefore no signed to unsigned conversion.
6407       if (!OtherRange.NonNegative) {
6408         // Check that the constant is representable in type OtherT.
6409         if (ConstantSigned) {
6410           if (OtherWidth >= Value.getMinSignedBits())
6411             return;
6412         } else { // !ConstantSigned
6413           if (OtherWidth >= Value.getActiveBits() + 1)
6414             return;
6415         }
6416       } else { // !OtherSigned
6417                // Check that the constant is representable in type OtherT.
6418         // Negative values are out of range.
6419         if (ConstantSigned) {
6420           if (Value.isNonNegative() && OtherWidth >= Value.getActiveBits())
6421             return;
6422         } else { // !ConstantSigned
6423           if (OtherWidth >= Value.getActiveBits())
6424             return;
6425         }
6426       }
6427     } else { // !CommonSigned
6428       if (OtherRange.NonNegative) {
6429         if (OtherWidth >= Value.getActiveBits())
6430           return;
6431       } else { // OtherSigned
6432         assert(!ConstantSigned &&
6433                "Two signed types converted to unsigned types.");
6434         // Check to see if the constant is representable in OtherT.
6435         if (OtherWidth > Value.getActiveBits())
6436           return;
6437         // Check to see if the constant is equivalent to a negative value
6438         // cast to CommonT.
6439         if (S.Context.getIntWidth(ConstantT) ==
6440                 S.Context.getIntWidth(CommonT) &&
6441             Value.isNegative() && Value.getMinSignedBits() <= OtherWidth)
6442           return;
6443         // The constant value rests between values that OtherT can represent
6444         // after conversion.  Relational comparison still works, but equality
6445         // comparisons will be tautological.
6446         EqualityOnly = true;
6447       }
6448     }
6449 
6450     bool PositiveConstant = !ConstantSigned || Value.isNonNegative();
6451 
6452     if (op == BO_EQ || op == BO_NE) {
6453       IsTrue = op == BO_NE;
6454     } else if (EqualityOnly) {
6455       return;
6456     } else if (RhsConstant) {
6457       if (op == BO_GT || op == BO_GE)
6458         IsTrue = !PositiveConstant;
6459       else // op == BO_LT || op == BO_LE
6460         IsTrue = PositiveConstant;
6461     } else {
6462       if (op == BO_LT || op == BO_LE)
6463         IsTrue = !PositiveConstant;
6464       else // op == BO_GT || op == BO_GE
6465         IsTrue = PositiveConstant;
6466     }
6467   } else {
6468     // Other isKnownToHaveBooleanValue
6469     enum CompareBoolWithConstantResult { AFals, ATrue, Unkwn };
6470     enum ConstantValue { LT_Zero, Zero, One, GT_One, SizeOfConstVal };
6471     enum ConstantSide { Lhs, Rhs, SizeOfConstSides };
6472 
6473     static const struct LinkedConditions {
6474       CompareBoolWithConstantResult BO_LT_OP[SizeOfConstSides][SizeOfConstVal];
6475       CompareBoolWithConstantResult BO_GT_OP[SizeOfConstSides][SizeOfConstVal];
6476       CompareBoolWithConstantResult BO_LE_OP[SizeOfConstSides][SizeOfConstVal];
6477       CompareBoolWithConstantResult BO_GE_OP[SizeOfConstSides][SizeOfConstVal];
6478       CompareBoolWithConstantResult BO_EQ_OP[SizeOfConstSides][SizeOfConstVal];
6479       CompareBoolWithConstantResult BO_NE_OP[SizeOfConstSides][SizeOfConstVal];
6480 
6481     } TruthTable = {
6482         // Constant on LHS.              | Constant on RHS.              |
6483         // LT_Zero| Zero  | One   |GT_One| LT_Zero| Zero  | One   |GT_One|
6484         { { ATrue, Unkwn, AFals, AFals }, { AFals, AFals, Unkwn, ATrue } },
6485         { { AFals, AFals, Unkwn, ATrue }, { ATrue, Unkwn, AFals, AFals } },
6486         { { ATrue, ATrue, Unkwn, AFals }, { AFals, Unkwn, ATrue, ATrue } },
6487         { { AFals, Unkwn, ATrue, ATrue }, { ATrue, ATrue, Unkwn, AFals } },
6488         { { AFals, Unkwn, Unkwn, AFals }, { AFals, Unkwn, Unkwn, AFals } },
6489         { { ATrue, Unkwn, Unkwn, ATrue }, { ATrue, Unkwn, Unkwn, ATrue } }
6490       };
6491 
6492     bool ConstantIsBoolLiteral = isa<CXXBoolLiteralExpr>(Constant);
6493 
6494     enum ConstantValue ConstVal = Zero;
6495     if (Value.isUnsigned() || Value.isNonNegative()) {
6496       if (Value == 0) {
6497         LiteralOrBoolConstant =
6498             ConstantIsBoolLiteral ? CXXBoolLiteralFalse : LiteralConstant;
6499         ConstVal = Zero;
6500       } else if (Value == 1) {
6501         LiteralOrBoolConstant =
6502             ConstantIsBoolLiteral ? CXXBoolLiteralTrue : LiteralConstant;
6503         ConstVal = One;
6504       } else {
6505         LiteralOrBoolConstant = LiteralConstant;
6506         ConstVal = GT_One;
6507       }
6508     } else {
6509       ConstVal = LT_Zero;
6510     }
6511 
6512     CompareBoolWithConstantResult CmpRes;
6513 
6514     switch (op) {
6515     case BO_LT:
6516       CmpRes = TruthTable.BO_LT_OP[RhsConstant][ConstVal];
6517       break;
6518     case BO_GT:
6519       CmpRes = TruthTable.BO_GT_OP[RhsConstant][ConstVal];
6520       break;
6521     case BO_LE:
6522       CmpRes = TruthTable.BO_LE_OP[RhsConstant][ConstVal];
6523       break;
6524     case BO_GE:
6525       CmpRes = TruthTable.BO_GE_OP[RhsConstant][ConstVal];
6526       break;
6527     case BO_EQ:
6528       CmpRes = TruthTable.BO_EQ_OP[RhsConstant][ConstVal];
6529       break;
6530     case BO_NE:
6531       CmpRes = TruthTable.BO_NE_OP[RhsConstant][ConstVal];
6532       break;
6533     default:
6534       CmpRes = Unkwn;
6535       break;
6536     }
6537 
6538     if (CmpRes == AFals) {
6539       IsTrue = false;
6540     } else if (CmpRes == ATrue) {
6541       IsTrue = true;
6542     } else {
6543       return;
6544     }
6545   }
6546 
6547   // If this is a comparison to an enum constant, include that
6548   // constant in the diagnostic.
6549   const EnumConstantDecl *ED = nullptr;
6550   if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Constant))
6551     ED = dyn_cast<EnumConstantDecl>(DR->getDecl());
6552 
6553   SmallString<64> PrettySourceValue;
6554   llvm::raw_svector_ostream OS(PrettySourceValue);
6555   if (ED)
6556     OS << '\'' << *ED << "' (" << Value << ")";
6557   else
6558     OS << Value;
6559 
6560   S.DiagRuntimeBehavior(
6561     E->getOperatorLoc(), E,
6562     S.PDiag(diag::warn_out_of_range_compare)
6563         << OS.str() << LiteralOrBoolConstant
6564         << OtherT << (OtherIsBooleanType && !OtherT->isBooleanType()) << IsTrue
6565         << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange());
6566 }
6567 
6568 /// Analyze the operands of the given comparison.  Implements the
6569 /// fallback case from AnalyzeComparison.
6570 static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) {
6571   AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
6572   AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
6573 }
6574 
6575 /// \brief Implements -Wsign-compare.
6576 ///
6577 /// \param E the binary operator to check for warnings
6578 static void AnalyzeComparison(Sema &S, BinaryOperator *E) {
6579   // The type the comparison is being performed in.
6580   QualType T = E->getLHS()->getType();
6581 
6582   // Only analyze comparison operators where both sides have been converted to
6583   // the same type.
6584   if (!S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType()))
6585     return AnalyzeImpConvsInComparison(S, E);
6586 
6587   // Don't analyze value-dependent comparisons directly.
6588   if (E->isValueDependent())
6589     return AnalyzeImpConvsInComparison(S, E);
6590 
6591   Expr *LHS = E->getLHS()->IgnoreParenImpCasts();
6592   Expr *RHS = E->getRHS()->IgnoreParenImpCasts();
6593 
6594   bool IsComparisonConstant = false;
6595 
6596   // Check whether an integer constant comparison results in a value
6597   // of 'true' or 'false'.
6598   if (T->isIntegralType(S.Context)) {
6599     llvm::APSInt RHSValue;
6600     bool IsRHSIntegralLiteral =
6601       RHS->isIntegerConstantExpr(RHSValue, S.Context);
6602     llvm::APSInt LHSValue;
6603     bool IsLHSIntegralLiteral =
6604       LHS->isIntegerConstantExpr(LHSValue, S.Context);
6605     if (IsRHSIntegralLiteral && !IsLHSIntegralLiteral)
6606         DiagnoseOutOfRangeComparison(S, E, RHS, LHS, RHSValue, true);
6607     else if (!IsRHSIntegralLiteral && IsLHSIntegralLiteral)
6608       DiagnoseOutOfRangeComparison(S, E, LHS, RHS, LHSValue, false);
6609     else
6610       IsComparisonConstant =
6611         (IsRHSIntegralLiteral && IsLHSIntegralLiteral);
6612   } else if (!T->hasUnsignedIntegerRepresentation())
6613       IsComparisonConstant = E->isIntegerConstantExpr(S.Context);
6614 
6615   // We don't do anything special if this isn't an unsigned integral
6616   // comparison:  we're only interested in integral comparisons, and
6617   // signed comparisons only happen in cases we don't care to warn about.
6618   //
6619   // We also don't care about value-dependent expressions or expressions
6620   // whose result is a constant.
6621   if (!T->hasUnsignedIntegerRepresentation() || IsComparisonConstant)
6622     return AnalyzeImpConvsInComparison(S, E);
6623 
6624   // Check to see if one of the (unmodified) operands is of different
6625   // signedness.
6626   Expr *signedOperand, *unsignedOperand;
6627   if (LHS->getType()->hasSignedIntegerRepresentation()) {
6628     assert(!RHS->getType()->hasSignedIntegerRepresentation() &&
6629            "unsigned comparison between two signed integer expressions?");
6630     signedOperand = LHS;
6631     unsignedOperand = RHS;
6632   } else if (RHS->getType()->hasSignedIntegerRepresentation()) {
6633     signedOperand = RHS;
6634     unsignedOperand = LHS;
6635   } else {
6636     CheckTrivialUnsignedComparison(S, E);
6637     return AnalyzeImpConvsInComparison(S, E);
6638   }
6639 
6640   // Otherwise, calculate the effective range of the signed operand.
6641   IntRange signedRange = GetExprRange(S.Context, signedOperand);
6642 
6643   // Go ahead and analyze implicit conversions in the operands.  Note
6644   // that we skip the implicit conversions on both sides.
6645   AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc());
6646   AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc());
6647 
6648   // If the signed range is non-negative, -Wsign-compare won't fire,
6649   // but we should still check for comparisons which are always true
6650   // or false.
6651   if (signedRange.NonNegative)
6652     return CheckTrivialUnsignedComparison(S, E);
6653 
6654   // For (in)equality comparisons, if the unsigned operand is a
6655   // constant which cannot collide with a overflowed signed operand,
6656   // then reinterpreting the signed operand as unsigned will not
6657   // change the result of the comparison.
6658   if (E->isEqualityOp()) {
6659     unsigned comparisonWidth = S.Context.getIntWidth(T);
6660     IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand);
6661 
6662     // We should never be unable to prove that the unsigned operand is
6663     // non-negative.
6664     assert(unsignedRange.NonNegative && "unsigned range includes negative?");
6665 
6666     if (unsignedRange.Width < comparisonWidth)
6667       return;
6668   }
6669 
6670   S.DiagRuntimeBehavior(E->getOperatorLoc(), E,
6671     S.PDiag(diag::warn_mixed_sign_comparison)
6672       << LHS->getType() << RHS->getType()
6673       << LHS->getSourceRange() << RHS->getSourceRange());
6674 }
6675 
6676 /// Analyzes an attempt to assign the given value to a bitfield.
6677 ///
6678 /// Returns true if there was something fishy about the attempt.
6679 static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init,
6680                                       SourceLocation InitLoc) {
6681   assert(Bitfield->isBitField());
6682   if (Bitfield->isInvalidDecl())
6683     return false;
6684 
6685   // White-list bool bitfields.
6686   if (Bitfield->getType()->isBooleanType())
6687     return false;
6688 
6689   // Ignore value- or type-dependent expressions.
6690   if (Bitfield->getBitWidth()->isValueDependent() ||
6691       Bitfield->getBitWidth()->isTypeDependent() ||
6692       Init->isValueDependent() ||
6693       Init->isTypeDependent())
6694     return false;
6695 
6696   Expr *OriginalInit = Init->IgnoreParenImpCasts();
6697 
6698   llvm::APSInt Value;
6699   if (!OriginalInit->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects))
6700     return false;
6701 
6702   unsigned OriginalWidth = Value.getBitWidth();
6703   unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context);
6704 
6705   if (OriginalWidth <= FieldWidth)
6706     return false;
6707 
6708   // Compute the value which the bitfield will contain.
6709   llvm::APSInt TruncatedValue = Value.trunc(FieldWidth);
6710   TruncatedValue.setIsSigned(Bitfield->getType()->isSignedIntegerType());
6711 
6712   // Check whether the stored value is equal to the original value.
6713   TruncatedValue = TruncatedValue.extend(OriginalWidth);
6714   if (llvm::APSInt::isSameValue(Value, TruncatedValue))
6715     return false;
6716 
6717   // Special-case bitfields of width 1: booleans are naturally 0/1, and
6718   // therefore don't strictly fit into a signed bitfield of width 1.
6719   if (FieldWidth == 1 && Value == 1)
6720     return false;
6721 
6722   std::string PrettyValue = Value.toString(10);
6723   std::string PrettyTrunc = TruncatedValue.toString(10);
6724 
6725   S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant)
6726     << PrettyValue << PrettyTrunc << OriginalInit->getType()
6727     << Init->getSourceRange();
6728 
6729   return true;
6730 }
6731 
6732 /// Analyze the given simple or compound assignment for warning-worthy
6733 /// operations.
6734 static void AnalyzeAssignment(Sema &S, BinaryOperator *E) {
6735   // Just recurse on the LHS.
6736   AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
6737 
6738   // We want to recurse on the RHS as normal unless we're assigning to
6739   // a bitfield.
6740   if (FieldDecl *Bitfield = E->getLHS()->getSourceBitField()) {
6741     if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(),
6742                                   E->getOperatorLoc())) {
6743       // Recurse, ignoring any implicit conversions on the RHS.
6744       return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(),
6745                                         E->getOperatorLoc());
6746     }
6747   }
6748 
6749   AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
6750 }
6751 
6752 /// Diagnose an implicit cast;  purely a helper for CheckImplicitConversion.
6753 static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T,
6754                             SourceLocation CContext, unsigned diag,
6755                             bool pruneControlFlow = false) {
6756   if (pruneControlFlow) {
6757     S.DiagRuntimeBehavior(E->getExprLoc(), E,
6758                           S.PDiag(diag)
6759                             << SourceType << T << E->getSourceRange()
6760                             << SourceRange(CContext));
6761     return;
6762   }
6763   S.Diag(E->getExprLoc(), diag)
6764     << SourceType << T << E->getSourceRange() << SourceRange(CContext);
6765 }
6766 
6767 /// Diagnose an implicit cast;  purely a helper for CheckImplicitConversion.
6768 static void DiagnoseImpCast(Sema &S, Expr *E, QualType T,
6769                             SourceLocation CContext, unsigned diag,
6770                             bool pruneControlFlow = false) {
6771   DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow);
6772 }
6773 
6774 /// Diagnose an implicit cast from a literal expression. Does not warn when the
6775 /// cast wouldn't lose information.
6776 void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T,
6777                                     SourceLocation CContext) {
6778   // Try to convert the literal exactly to an integer. If we can, don't warn.
6779   bool isExact = false;
6780   const llvm::APFloat &Value = FL->getValue();
6781   llvm::APSInt IntegerValue(S.Context.getIntWidth(T),
6782                             T->hasUnsignedIntegerRepresentation());
6783   if (Value.convertToInteger(IntegerValue,
6784                              llvm::APFloat::rmTowardZero, &isExact)
6785       == llvm::APFloat::opOK && isExact)
6786     return;
6787 
6788   // FIXME: Force the precision of the source value down so we don't print
6789   // digits which are usually useless (we don't really care here if we
6790   // truncate a digit by accident in edge cases).  Ideally, APFloat::toString
6791   // would automatically print the shortest representation, but it's a bit
6792   // tricky to implement.
6793   SmallString<16> PrettySourceValue;
6794   unsigned precision = llvm::APFloat::semanticsPrecision(Value.getSemantics());
6795   precision = (precision * 59 + 195) / 196;
6796   Value.toString(PrettySourceValue, precision);
6797 
6798   SmallString<16> PrettyTargetValue;
6799   if (T->isSpecificBuiltinType(BuiltinType::Bool))
6800     PrettyTargetValue = IntegerValue == 0 ? "false" : "true";
6801   else
6802     IntegerValue.toString(PrettyTargetValue);
6803 
6804   S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer)
6805     << FL->getType() << T.getUnqualifiedType() << PrettySourceValue
6806     << PrettyTargetValue << FL->getSourceRange() << SourceRange(CContext);
6807 }
6808 
6809 std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) {
6810   if (!Range.Width) return "0";
6811 
6812   llvm::APSInt ValueInRange = Value;
6813   ValueInRange.setIsSigned(!Range.NonNegative);
6814   ValueInRange = ValueInRange.trunc(Range.Width);
6815   return ValueInRange.toString(10);
6816 }
6817 
6818 static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) {
6819   if (!isa<ImplicitCastExpr>(Ex))
6820     return false;
6821 
6822   Expr *InnerE = Ex->IgnoreParenImpCasts();
6823   const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr();
6824   const Type *Source =
6825     S.Context.getCanonicalType(InnerE->getType()).getTypePtr();
6826   if (Target->isDependentType())
6827     return false;
6828 
6829   const BuiltinType *FloatCandidateBT =
6830     dyn_cast<BuiltinType>(ToBool ? Source : Target);
6831   const Type *BoolCandidateType = ToBool ? Target : Source;
6832 
6833   return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) &&
6834           FloatCandidateBT && (FloatCandidateBT->isFloatingPoint()));
6835 }
6836 
6837 void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall,
6838                                       SourceLocation CC) {
6839   unsigned NumArgs = TheCall->getNumArgs();
6840   for (unsigned i = 0; i < NumArgs; ++i) {
6841     Expr *CurrA = TheCall->getArg(i);
6842     if (!IsImplicitBoolFloatConversion(S, CurrA, true))
6843       continue;
6844 
6845     bool IsSwapped = ((i > 0) &&
6846         IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false));
6847     IsSwapped |= ((i < (NumArgs - 1)) &&
6848         IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false));
6849     if (IsSwapped) {
6850       // Warn on this floating-point to bool conversion.
6851       DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(),
6852                       CurrA->getType(), CC,
6853                       diag::warn_impcast_floating_point_to_bool);
6854     }
6855   }
6856 }
6857 
6858 static void DiagnoseNullConversion(Sema &S, Expr *E, QualType T,
6859                                    SourceLocation CC) {
6860   if (S.Diags.isIgnored(diag::warn_impcast_null_pointer_to_integer,
6861                         E->getExprLoc()))
6862     return;
6863 
6864   // Check for NULL (GNUNull) or nullptr (CXX11_nullptr).
6865   const Expr::NullPointerConstantKind NullKind =
6866       E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull);
6867   if (NullKind != Expr::NPCK_GNUNull && NullKind != Expr::NPCK_CXX11_nullptr)
6868     return;
6869 
6870   // Return if target type is a safe conversion.
6871   if (T->isAnyPointerType() || T->isBlockPointerType() ||
6872       T->isMemberPointerType() || !T->isScalarType() || T->isNullPtrType())
6873     return;
6874 
6875   SourceLocation Loc = E->getSourceRange().getBegin();
6876 
6877   // __null is usually wrapped in a macro.  Go up a macro if that is the case.
6878   if (NullKind == Expr::NPCK_GNUNull) {
6879     if (Loc.isMacroID())
6880       Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first;
6881   }
6882 
6883   // Only warn if the null and context location are in the same macro expansion.
6884   if (S.SourceMgr.getFileID(Loc) != S.SourceMgr.getFileID(CC))
6885     return;
6886 
6887   S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer)
6888       << (NullKind == Expr::NPCK_CXX11_nullptr) << T << clang::SourceRange(CC)
6889       << FixItHint::CreateReplacement(Loc,
6890                                       S.getFixItZeroLiteralForType(T, Loc));
6891 }
6892 
6893 static void checkObjCArrayLiteral(Sema &S, QualType TargetType,
6894                                   ObjCArrayLiteral *ArrayLiteral);
6895 static void checkObjCDictionaryLiteral(Sema &S, QualType TargetType,
6896                                        ObjCDictionaryLiteral *DictionaryLiteral);
6897 
6898 /// Check a single element within a collection literal against the
6899 /// target element type.
6900 static void checkObjCCollectionLiteralElement(Sema &S,
6901                                               QualType TargetElementType,
6902                                               Expr *Element,
6903                                               unsigned ElementKind) {
6904   // Skip a bitcast to 'id' or qualified 'id'.
6905   if (auto ICE = dyn_cast<ImplicitCastExpr>(Element)) {
6906     if (ICE->getCastKind() == CK_BitCast &&
6907         ICE->getSubExpr()->getType()->getAs<ObjCObjectPointerType>())
6908       Element = ICE->getSubExpr();
6909   }
6910 
6911   QualType ElementType = Element->getType();
6912   ExprResult ElementResult(Element);
6913   if (ElementType->getAs<ObjCObjectPointerType>() &&
6914       S.CheckSingleAssignmentConstraints(TargetElementType,
6915                                          ElementResult,
6916                                          false, false)
6917         != Sema::Compatible) {
6918     S.Diag(Element->getLocStart(),
6919            diag::warn_objc_collection_literal_element)
6920       << ElementType << ElementKind << TargetElementType
6921       << Element->getSourceRange();
6922   }
6923 
6924   if (auto ArrayLiteral = dyn_cast<ObjCArrayLiteral>(Element))
6925     checkObjCArrayLiteral(S, TargetElementType, ArrayLiteral);
6926   else if (auto DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(Element))
6927     checkObjCDictionaryLiteral(S, TargetElementType, DictionaryLiteral);
6928 }
6929 
6930 /// Check an Objective-C array literal being converted to the given
6931 /// target type.
6932 static void checkObjCArrayLiteral(Sema &S, QualType TargetType,
6933                                   ObjCArrayLiteral *ArrayLiteral) {
6934   if (!S.NSArrayDecl)
6935     return;
6936 
6937   const auto *TargetObjCPtr = TargetType->getAs<ObjCObjectPointerType>();
6938   if (!TargetObjCPtr)
6939     return;
6940 
6941   if (TargetObjCPtr->isUnspecialized() ||
6942       TargetObjCPtr->getInterfaceDecl()->getCanonicalDecl()
6943         != S.NSArrayDecl->getCanonicalDecl())
6944     return;
6945 
6946   auto TypeArgs = TargetObjCPtr->getTypeArgs();
6947   if (TypeArgs.size() != 1)
6948     return;
6949 
6950   QualType TargetElementType = TypeArgs[0];
6951   for (unsigned I = 0, N = ArrayLiteral->getNumElements(); I != N; ++I) {
6952     checkObjCCollectionLiteralElement(S, TargetElementType,
6953                                       ArrayLiteral->getElement(I),
6954                                       0);
6955   }
6956 }
6957 
6958 /// Check an Objective-C dictionary literal being converted to the given
6959 /// target type.
6960 static void checkObjCDictionaryLiteral(
6961               Sema &S, QualType TargetType,
6962               ObjCDictionaryLiteral *DictionaryLiteral) {
6963   if (!S.NSDictionaryDecl)
6964     return;
6965 
6966   const auto *TargetObjCPtr = TargetType->getAs<ObjCObjectPointerType>();
6967   if (!TargetObjCPtr)
6968     return;
6969 
6970   if (TargetObjCPtr->isUnspecialized() ||
6971       TargetObjCPtr->getInterfaceDecl()->getCanonicalDecl()
6972         != S.NSDictionaryDecl->getCanonicalDecl())
6973     return;
6974 
6975   auto TypeArgs = TargetObjCPtr->getTypeArgs();
6976   if (TypeArgs.size() != 2)
6977     return;
6978 
6979   QualType TargetKeyType = TypeArgs[0];
6980   QualType TargetObjectType = TypeArgs[1];
6981   for (unsigned I = 0, N = DictionaryLiteral->getNumElements(); I != N; ++I) {
6982     auto Element = DictionaryLiteral->getKeyValueElement(I);
6983     checkObjCCollectionLiteralElement(S, TargetKeyType, Element.Key, 1);
6984     checkObjCCollectionLiteralElement(S, TargetObjectType, Element.Value, 2);
6985   }
6986 }
6987 
6988 void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
6989                              SourceLocation CC, bool *ICContext = nullptr) {
6990   if (E->isTypeDependent() || E->isValueDependent()) return;
6991 
6992   const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr();
6993   const Type *Target = S.Context.getCanonicalType(T).getTypePtr();
6994   if (Source == Target) return;
6995   if (Target->isDependentType()) return;
6996 
6997   // If the conversion context location is invalid don't complain. We also
6998   // don't want to emit a warning if the issue occurs from the expansion of
6999   // a system macro. The problem is that 'getSpellingLoc()' is slow, so we
7000   // delay this check as long as possible. Once we detect we are in that
7001   // scenario, we just return.
7002   if (CC.isInvalid())
7003     return;
7004 
7005   // Diagnose implicit casts to bool.
7006   if (Target->isSpecificBuiltinType(BuiltinType::Bool)) {
7007     if (isa<StringLiteral>(E))
7008       // Warn on string literal to bool.  Checks for string literals in logical
7009       // and expressions, for instance, assert(0 && "error here"), are
7010       // prevented by a check in AnalyzeImplicitConversions().
7011       return DiagnoseImpCast(S, E, T, CC,
7012                              diag::warn_impcast_string_literal_to_bool);
7013     if (isa<ObjCStringLiteral>(E) || isa<ObjCArrayLiteral>(E) ||
7014         isa<ObjCDictionaryLiteral>(E) || isa<ObjCBoxedExpr>(E)) {
7015       // This covers the literal expressions that evaluate to Objective-C
7016       // objects.
7017       return DiagnoseImpCast(S, E, T, CC,
7018                              diag::warn_impcast_objective_c_literal_to_bool);
7019     }
7020     if (Source->isPointerType() || Source->canDecayToPointerType()) {
7021       // Warn on pointer to bool conversion that is always true.
7022       S.DiagnoseAlwaysNonNullPointer(E, Expr::NPCK_NotNull, /*IsEqual*/ false,
7023                                      SourceRange(CC));
7024     }
7025   }
7026 
7027   // Check implicit casts from Objective-C collection literals to specialized
7028   // collection types, e.g., NSArray<NSString *> *.
7029   if (auto *ArrayLiteral = dyn_cast<ObjCArrayLiteral>(E))
7030     checkObjCArrayLiteral(S, QualType(Target, 0), ArrayLiteral);
7031   else if (auto *DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(E))
7032     checkObjCDictionaryLiteral(S, QualType(Target, 0), DictionaryLiteral);
7033 
7034   // Strip vector types.
7035   if (isa<VectorType>(Source)) {
7036     if (!isa<VectorType>(Target)) {
7037       if (S.SourceMgr.isInSystemMacro(CC))
7038         return;
7039       return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar);
7040     }
7041 
7042     // If the vector cast is cast between two vectors of the same size, it is
7043     // a bitcast, not a conversion.
7044     if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target))
7045       return;
7046 
7047     Source = cast<VectorType>(Source)->getElementType().getTypePtr();
7048     Target = cast<VectorType>(Target)->getElementType().getTypePtr();
7049   }
7050   if (auto VecTy = dyn_cast<VectorType>(Target))
7051     Target = VecTy->getElementType().getTypePtr();
7052 
7053   // Strip complex types.
7054   if (isa<ComplexType>(Source)) {
7055     if (!isa<ComplexType>(Target)) {
7056       if (S.SourceMgr.isInSystemMacro(CC))
7057         return;
7058 
7059       return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar);
7060     }
7061 
7062     Source = cast<ComplexType>(Source)->getElementType().getTypePtr();
7063     Target = cast<ComplexType>(Target)->getElementType().getTypePtr();
7064   }
7065 
7066   const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source);
7067   const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target);
7068 
7069   // If the source is floating point...
7070   if (SourceBT && SourceBT->isFloatingPoint()) {
7071     // ...and the target is floating point...
7072     if (TargetBT && TargetBT->isFloatingPoint()) {
7073       // ...then warn if we're dropping FP rank.
7074 
7075       // Builtin FP kinds are ordered by increasing FP rank.
7076       if (SourceBT->getKind() > TargetBT->getKind()) {
7077         // Don't warn about float constants that are precisely
7078         // representable in the target type.
7079         Expr::EvalResult result;
7080         if (E->EvaluateAsRValue(result, S.Context)) {
7081           // Value might be a float, a float vector, or a float complex.
7082           if (IsSameFloatAfterCast(result.Val,
7083                    S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)),
7084                    S.Context.getFloatTypeSemantics(QualType(SourceBT, 0))))
7085             return;
7086         }
7087 
7088         if (S.SourceMgr.isInSystemMacro(CC))
7089           return;
7090 
7091         DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision);
7092       }
7093       return;
7094     }
7095 
7096     // If the target is integral, always warn.
7097     if (TargetBT && TargetBT->isInteger()) {
7098       if (S.SourceMgr.isInSystemMacro(CC))
7099         return;
7100 
7101       Expr *InnerE = E->IgnoreParenImpCasts();
7102       // We also want to warn on, e.g., "int i = -1.234"
7103       if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE))
7104         if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus)
7105           InnerE = UOp->getSubExpr()->IgnoreParenImpCasts();
7106 
7107       if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) {
7108         DiagnoseFloatingLiteralImpCast(S, FL, T, CC);
7109       } else {
7110         DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer);
7111       }
7112     }
7113 
7114     // If the target is bool, warn if expr is a function or method call.
7115     if (Target->isSpecificBuiltinType(BuiltinType::Bool) &&
7116         isa<CallExpr>(E)) {
7117       // Check last argument of function call to see if it is an
7118       // implicit cast from a type matching the type the result
7119       // is being cast to.
7120       CallExpr *CEx = cast<CallExpr>(E);
7121       unsigned NumArgs = CEx->getNumArgs();
7122       if (NumArgs > 0) {
7123         Expr *LastA = CEx->getArg(NumArgs - 1);
7124         Expr *InnerE = LastA->IgnoreParenImpCasts();
7125         const Type *InnerType =
7126           S.Context.getCanonicalType(InnerE->getType()).getTypePtr();
7127         if (isa<ImplicitCastExpr>(LastA) && (InnerType == Target)) {
7128           // Warn on this floating-point to bool conversion
7129           DiagnoseImpCast(S, E, T, CC,
7130                           diag::warn_impcast_floating_point_to_bool);
7131         }
7132       }
7133     }
7134     return;
7135   }
7136 
7137   DiagnoseNullConversion(S, E, T, CC);
7138 
7139   if (!Source->isIntegerType() || !Target->isIntegerType())
7140     return;
7141 
7142   // TODO: remove this early return once the false positives for constant->bool
7143   // in templates, macros, etc, are reduced or removed.
7144   if (Target->isSpecificBuiltinType(BuiltinType::Bool))
7145     return;
7146 
7147   IntRange SourceRange = GetExprRange(S.Context, E);
7148   IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target);
7149 
7150   if (SourceRange.Width > TargetRange.Width) {
7151     // If the source is a constant, use a default-on diagnostic.
7152     // TODO: this should happen for bitfield stores, too.
7153     llvm::APSInt Value(32);
7154     if (E->isIntegerConstantExpr(Value, S.Context)) {
7155       if (S.SourceMgr.isInSystemMacro(CC))
7156         return;
7157 
7158       std::string PrettySourceValue = Value.toString(10);
7159       std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange);
7160 
7161       S.DiagRuntimeBehavior(E->getExprLoc(), E,
7162         S.PDiag(diag::warn_impcast_integer_precision_constant)
7163             << PrettySourceValue << PrettyTargetValue
7164             << E->getType() << T << E->getSourceRange()
7165             << clang::SourceRange(CC));
7166       return;
7167     }
7168 
7169     // People want to build with -Wshorten-64-to-32 and not -Wconversion.
7170     if (S.SourceMgr.isInSystemMacro(CC))
7171       return;
7172 
7173     if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64)
7174       return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32,
7175                              /* pruneControlFlow */ true);
7176     return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision);
7177   }
7178 
7179   if ((TargetRange.NonNegative && !SourceRange.NonNegative) ||
7180       (!TargetRange.NonNegative && SourceRange.NonNegative &&
7181        SourceRange.Width == TargetRange.Width)) {
7182 
7183     if (S.SourceMgr.isInSystemMacro(CC))
7184       return;
7185 
7186     unsigned DiagID = diag::warn_impcast_integer_sign;
7187 
7188     // Traditionally, gcc has warned about this under -Wsign-compare.
7189     // We also want to warn about it in -Wconversion.
7190     // So if -Wconversion is off, use a completely identical diagnostic
7191     // in the sign-compare group.
7192     // The conditional-checking code will
7193     if (ICContext) {
7194       DiagID = diag::warn_impcast_integer_sign_conditional;
7195       *ICContext = true;
7196     }
7197 
7198     return DiagnoseImpCast(S, E, T, CC, DiagID);
7199   }
7200 
7201   // Diagnose conversions between different enumeration types.
7202   // In C, we pretend that the type of an EnumConstantDecl is its enumeration
7203   // type, to give us better diagnostics.
7204   QualType SourceType = E->getType();
7205   if (!S.getLangOpts().CPlusPlus) {
7206     if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
7207       if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
7208         EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext());
7209         SourceType = S.Context.getTypeDeclType(Enum);
7210         Source = S.Context.getCanonicalType(SourceType).getTypePtr();
7211       }
7212   }
7213 
7214   if (const EnumType *SourceEnum = Source->getAs<EnumType>())
7215     if (const EnumType *TargetEnum = Target->getAs<EnumType>())
7216       if (SourceEnum->getDecl()->hasNameForLinkage() &&
7217           TargetEnum->getDecl()->hasNameForLinkage() &&
7218           SourceEnum != TargetEnum) {
7219         if (S.SourceMgr.isInSystemMacro(CC))
7220           return;
7221 
7222         return DiagnoseImpCast(S, E, SourceType, T, CC,
7223                                diag::warn_impcast_different_enum_types);
7224       }
7225 
7226   return;
7227 }
7228 
7229 void CheckConditionalOperator(Sema &S, ConditionalOperator *E,
7230                               SourceLocation CC, QualType T);
7231 
7232 void CheckConditionalOperand(Sema &S, Expr *E, QualType T,
7233                              SourceLocation CC, bool &ICContext) {
7234   E = E->IgnoreParenImpCasts();
7235 
7236   if (isa<ConditionalOperator>(E))
7237     return CheckConditionalOperator(S, cast<ConditionalOperator>(E), CC, T);
7238 
7239   AnalyzeImplicitConversions(S, E, CC);
7240   if (E->getType() != T)
7241     return CheckImplicitConversion(S, E, T, CC, &ICContext);
7242   return;
7243 }
7244 
7245 void CheckConditionalOperator(Sema &S, ConditionalOperator *E,
7246                               SourceLocation CC, QualType T) {
7247   AnalyzeImplicitConversions(S, E->getCond(), E->getQuestionLoc());
7248 
7249   bool Suspicious = false;
7250   CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious);
7251   CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious);
7252 
7253   // If -Wconversion would have warned about either of the candidates
7254   // for a signedness conversion to the context type...
7255   if (!Suspicious) return;
7256 
7257   // ...but it's currently ignored...
7258   if (!S.Diags.isIgnored(diag::warn_impcast_integer_sign_conditional, CC))
7259     return;
7260 
7261   // ...then check whether it would have warned about either of the
7262   // candidates for a signedness conversion to the condition type.
7263   if (E->getType() == T) return;
7264 
7265   Suspicious = false;
7266   CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(),
7267                           E->getType(), CC, &Suspicious);
7268   if (!Suspicious)
7269     CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(),
7270                             E->getType(), CC, &Suspicious);
7271 }
7272 
7273 /// CheckBoolLikeConversion - Check conversion of given expression to boolean.
7274 /// Input argument E is a logical expression.
7275 static void CheckBoolLikeConversion(Sema &S, Expr *E, SourceLocation CC) {
7276   if (S.getLangOpts().Bool)
7277     return;
7278   CheckImplicitConversion(S, E->IgnoreParenImpCasts(), S.Context.BoolTy, CC);
7279 }
7280 
7281 /// AnalyzeImplicitConversions - Find and report any interesting
7282 /// implicit conversions in the given expression.  There are a couple
7283 /// of competing diagnostics here, -Wconversion and -Wsign-compare.
7284 void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) {
7285   QualType T = OrigE->getType();
7286   Expr *E = OrigE->IgnoreParenImpCasts();
7287 
7288   if (E->isTypeDependent() || E->isValueDependent())
7289     return;
7290 
7291   // For conditional operators, we analyze the arguments as if they
7292   // were being fed directly into the output.
7293   if (isa<ConditionalOperator>(E)) {
7294     ConditionalOperator *CO = cast<ConditionalOperator>(E);
7295     CheckConditionalOperator(S, CO, CC, T);
7296     return;
7297   }
7298 
7299   // Check implicit argument conversions for function calls.
7300   if (CallExpr *Call = dyn_cast<CallExpr>(E))
7301     CheckImplicitArgumentConversions(S, Call, CC);
7302 
7303   // Go ahead and check any implicit conversions we might have skipped.
7304   // The non-canonical typecheck is just an optimization;
7305   // CheckImplicitConversion will filter out dead implicit conversions.
7306   if (E->getType() != T)
7307     CheckImplicitConversion(S, E, T, CC);
7308 
7309   // Now continue drilling into this expression.
7310 
7311   if (PseudoObjectExpr * POE = dyn_cast<PseudoObjectExpr>(E)) {
7312     if (POE->getResultExpr())
7313       E = POE->getResultExpr();
7314   }
7315 
7316   if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) {
7317     if (OVE->getSourceExpr())
7318       AnalyzeImplicitConversions(S, OVE->getSourceExpr(), CC);
7319     return;
7320   }
7321 
7322   // Skip past explicit casts.
7323   if (isa<ExplicitCastExpr>(E)) {
7324     E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts();
7325     return AnalyzeImplicitConversions(S, E, CC);
7326   }
7327 
7328   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
7329     // Do a somewhat different check with comparison operators.
7330     if (BO->isComparisonOp())
7331       return AnalyzeComparison(S, BO);
7332 
7333     // And with simple assignments.
7334     if (BO->getOpcode() == BO_Assign)
7335       return AnalyzeAssignment(S, BO);
7336   }
7337 
7338   // These break the otherwise-useful invariant below.  Fortunately,
7339   // we don't really need to recurse into them, because any internal
7340   // expressions should have been analyzed already when they were
7341   // built into statements.
7342   if (isa<StmtExpr>(E)) return;
7343 
7344   // Don't descend into unevaluated contexts.
7345   if (isa<UnaryExprOrTypeTraitExpr>(E)) return;
7346 
7347   // Now just recurse over the expression's children.
7348   CC = E->getExprLoc();
7349   BinaryOperator *BO = dyn_cast<BinaryOperator>(E);
7350   bool IsLogicalAndOperator = BO && BO->getOpcode() == BO_LAnd;
7351   for (Stmt *SubStmt : E->children()) {
7352     Expr *ChildExpr = dyn_cast_or_null<Expr>(SubStmt);
7353     if (!ChildExpr)
7354       continue;
7355 
7356     if (IsLogicalAndOperator &&
7357         isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts()))
7358       // Ignore checking string literals that are in logical and operators.
7359       // This is a common pattern for asserts.
7360       continue;
7361     AnalyzeImplicitConversions(S, ChildExpr, CC);
7362   }
7363 
7364   if (BO && BO->isLogicalOp()) {
7365     Expr *SubExpr = BO->getLHS()->IgnoreParenImpCasts();
7366     if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr))
7367       ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc());
7368 
7369     SubExpr = BO->getRHS()->IgnoreParenImpCasts();
7370     if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr))
7371       ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc());
7372   }
7373 
7374   if (const UnaryOperator *U = dyn_cast<UnaryOperator>(E))
7375     if (U->getOpcode() == UO_LNot)
7376       ::CheckBoolLikeConversion(S, U->getSubExpr(), CC);
7377 }
7378 
7379 } // end anonymous namespace
7380 
7381 enum {
7382   AddressOf,
7383   FunctionPointer,
7384   ArrayPointer
7385 };
7386 
7387 // Helper function for Sema::DiagnoseAlwaysNonNullPointer.
7388 // Returns true when emitting a warning about taking the address of a reference.
7389 static bool CheckForReference(Sema &SemaRef, const Expr *E,
7390                               PartialDiagnostic PD) {
7391   E = E->IgnoreParenImpCasts();
7392 
7393   const FunctionDecl *FD = nullptr;
7394 
7395   if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
7396     if (!DRE->getDecl()->getType()->isReferenceType())
7397       return false;
7398   } else if (const MemberExpr *M = dyn_cast<MemberExpr>(E)) {
7399     if (!M->getMemberDecl()->getType()->isReferenceType())
7400       return false;
7401   } else if (const CallExpr *Call = dyn_cast<CallExpr>(E)) {
7402     if (!Call->getCallReturnType(SemaRef.Context)->isReferenceType())
7403       return false;
7404     FD = Call->getDirectCallee();
7405   } else {
7406     return false;
7407   }
7408 
7409   SemaRef.Diag(E->getExprLoc(), PD);
7410 
7411   // If possible, point to location of function.
7412   if (FD) {
7413     SemaRef.Diag(FD->getLocation(), diag::note_reference_is_return_value) << FD;
7414   }
7415 
7416   return true;
7417 }
7418 
7419 // Returns true if the SourceLocation is expanded from any macro body.
7420 // Returns false if the SourceLocation is invalid, is from not in a macro
7421 // expansion, or is from expanded from a top-level macro argument.
7422 static bool IsInAnyMacroBody(const SourceManager &SM, SourceLocation Loc) {
7423   if (Loc.isInvalid())
7424     return false;
7425 
7426   while (Loc.isMacroID()) {
7427     if (SM.isMacroBodyExpansion(Loc))
7428       return true;
7429     Loc = SM.getImmediateMacroCallerLoc(Loc);
7430   }
7431 
7432   return false;
7433 }
7434 
7435 /// \brief Diagnose pointers that are always non-null.
7436 /// \param E the expression containing the pointer
7437 /// \param NullKind NPCK_NotNull if E is a cast to bool, otherwise, E is
7438 /// compared to a null pointer
7439 /// \param IsEqual True when the comparison is equal to a null pointer
7440 /// \param Range Extra SourceRange to highlight in the diagnostic
7441 void Sema::DiagnoseAlwaysNonNullPointer(Expr *E,
7442                                         Expr::NullPointerConstantKind NullKind,
7443                                         bool IsEqual, SourceRange Range) {
7444   if (!E)
7445     return;
7446 
7447   // Don't warn inside macros.
7448   if (E->getExprLoc().isMacroID()) {
7449     const SourceManager &SM = getSourceManager();
7450     if (IsInAnyMacroBody(SM, E->getExprLoc()) ||
7451         IsInAnyMacroBody(SM, Range.getBegin()))
7452       return;
7453   }
7454   E = E->IgnoreImpCasts();
7455 
7456   const bool IsCompare = NullKind != Expr::NPCK_NotNull;
7457 
7458   if (isa<CXXThisExpr>(E)) {
7459     unsigned DiagID = IsCompare ? diag::warn_this_null_compare
7460                                 : diag::warn_this_bool_conversion;
7461     Diag(E->getExprLoc(), DiagID) << E->getSourceRange() << Range << IsEqual;
7462     return;
7463   }
7464 
7465   bool IsAddressOf = false;
7466 
7467   if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
7468     if (UO->getOpcode() != UO_AddrOf)
7469       return;
7470     IsAddressOf = true;
7471     E = UO->getSubExpr();
7472   }
7473 
7474   if (IsAddressOf) {
7475     unsigned DiagID = IsCompare
7476                           ? diag::warn_address_of_reference_null_compare
7477                           : diag::warn_address_of_reference_bool_conversion;
7478     PartialDiagnostic PD = PDiag(DiagID) << E->getSourceRange() << Range
7479                                          << IsEqual;
7480     if (CheckForReference(*this, E, PD)) {
7481       return;
7482     }
7483   }
7484 
7485   // Expect to find a single Decl.  Skip anything more complicated.
7486   ValueDecl *D = nullptr;
7487   if (DeclRefExpr *R = dyn_cast<DeclRefExpr>(E)) {
7488     D = R->getDecl();
7489   } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) {
7490     D = M->getMemberDecl();
7491   }
7492 
7493   // Weak Decls can be null.
7494   if (!D || D->isWeak())
7495     return;
7496 
7497   // Check for parameter decl with nonnull attribute
7498   if (const ParmVarDecl* PV = dyn_cast<ParmVarDecl>(D)) {
7499     if (getCurFunction() && !getCurFunction()->ModifiedNonNullParams.count(PV))
7500       if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) {
7501         unsigned NumArgs = FD->getNumParams();
7502         llvm::SmallBitVector AttrNonNull(NumArgs);
7503         for (const auto *NonNull : FD->specific_attrs<NonNullAttr>()) {
7504           if (!NonNull->args_size()) {
7505             AttrNonNull.set(0, NumArgs);
7506             break;
7507           }
7508           for (unsigned Val : NonNull->args()) {
7509             if (Val >= NumArgs)
7510               continue;
7511             AttrNonNull.set(Val);
7512           }
7513         }
7514         if (!AttrNonNull.empty())
7515           for (unsigned i = 0; i < NumArgs; ++i)
7516             if (FD->getParamDecl(i) == PV &&
7517                 (AttrNonNull[i] || PV->hasAttr<NonNullAttr>())) {
7518               std::string Str;
7519               llvm::raw_string_ostream S(Str);
7520               E->printPretty(S, nullptr, getPrintingPolicy());
7521               unsigned DiagID = IsCompare ? diag::warn_nonnull_parameter_compare
7522                                           : diag::warn_cast_nonnull_to_bool;
7523               Diag(E->getExprLoc(), DiagID) << S.str() << E->getSourceRange()
7524                 << Range << IsEqual;
7525               return;
7526             }
7527       }
7528     }
7529 
7530   QualType T = D->getType();
7531   const bool IsArray = T->isArrayType();
7532   const bool IsFunction = T->isFunctionType();
7533 
7534   // Address of function is used to silence the function warning.
7535   if (IsAddressOf && IsFunction) {
7536     return;
7537   }
7538 
7539   // Found nothing.
7540   if (!IsAddressOf && !IsFunction && !IsArray)
7541     return;
7542 
7543   // Pretty print the expression for the diagnostic.
7544   std::string Str;
7545   llvm::raw_string_ostream S(Str);
7546   E->printPretty(S, nullptr, getPrintingPolicy());
7547 
7548   unsigned DiagID = IsCompare ? diag::warn_null_pointer_compare
7549                               : diag::warn_impcast_pointer_to_bool;
7550   unsigned DiagType;
7551   if (IsAddressOf)
7552     DiagType = AddressOf;
7553   else if (IsFunction)
7554     DiagType = FunctionPointer;
7555   else if (IsArray)
7556     DiagType = ArrayPointer;
7557   else
7558     llvm_unreachable("Could not determine diagnostic.");
7559   Diag(E->getExprLoc(), DiagID) << DiagType << S.str() << E->getSourceRange()
7560                                 << Range << IsEqual;
7561 
7562   if (!IsFunction)
7563     return;
7564 
7565   // Suggest '&' to silence the function warning.
7566   Diag(E->getExprLoc(), diag::note_function_warning_silence)
7567       << FixItHint::CreateInsertion(E->getLocStart(), "&");
7568 
7569   // Check to see if '()' fixit should be emitted.
7570   QualType ReturnType;
7571   UnresolvedSet<4> NonTemplateOverloads;
7572   tryExprAsCall(*E, ReturnType, NonTemplateOverloads);
7573   if (ReturnType.isNull())
7574     return;
7575 
7576   if (IsCompare) {
7577     // There are two cases here.  If there is null constant, the only suggest
7578     // for a pointer return type.  If the null is 0, then suggest if the return
7579     // type is a pointer or an integer type.
7580     if (!ReturnType->isPointerType()) {
7581       if (NullKind == Expr::NPCK_ZeroExpression ||
7582           NullKind == Expr::NPCK_ZeroLiteral) {
7583         if (!ReturnType->isIntegerType())
7584           return;
7585       } else {
7586         return;
7587       }
7588     }
7589   } else { // !IsCompare
7590     // For function to bool, only suggest if the function pointer has bool
7591     // return type.
7592     if (!ReturnType->isSpecificBuiltinType(BuiltinType::Bool))
7593       return;
7594   }
7595   Diag(E->getExprLoc(), diag::note_function_to_function_call)
7596       << FixItHint::CreateInsertion(getLocForEndOfToken(E->getLocEnd()), "()");
7597 }
7598 
7599 
7600 /// Diagnoses "dangerous" implicit conversions within the given
7601 /// expression (which is a full expression).  Implements -Wconversion
7602 /// and -Wsign-compare.
7603 ///
7604 /// \param CC the "context" location of the implicit conversion, i.e.
7605 ///   the most location of the syntactic entity requiring the implicit
7606 ///   conversion
7607 void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) {
7608   // Don't diagnose in unevaluated contexts.
7609   if (isUnevaluatedContext())
7610     return;
7611 
7612   // Don't diagnose for value- or type-dependent expressions.
7613   if (E->isTypeDependent() || E->isValueDependent())
7614     return;
7615 
7616   // Check for array bounds violations in cases where the check isn't triggered
7617   // elsewhere for other Expr types (like BinaryOperators), e.g. when an
7618   // ArraySubscriptExpr is on the RHS of a variable initialization.
7619   CheckArrayAccess(E);
7620 
7621   // This is not the right CC for (e.g.) a variable initialization.
7622   AnalyzeImplicitConversions(*this, E, CC);
7623 }
7624 
7625 /// CheckBoolLikeConversion - Check conversion of given expression to boolean.
7626 /// Input argument E is a logical expression.
7627 void Sema::CheckBoolLikeConversion(Expr *E, SourceLocation CC) {
7628   ::CheckBoolLikeConversion(*this, E, CC);
7629 }
7630 
7631 /// Diagnose when expression is an integer constant expression and its evaluation
7632 /// results in integer overflow
7633 void Sema::CheckForIntOverflow (Expr *E) {
7634   if (isa<BinaryOperator>(E->IgnoreParenCasts()))
7635     E->IgnoreParenCasts()->EvaluateForOverflow(Context);
7636 }
7637 
7638 namespace {
7639 /// \brief Visitor for expressions which looks for unsequenced operations on the
7640 /// same object.
7641 class SequenceChecker : public EvaluatedExprVisitor<SequenceChecker> {
7642   typedef EvaluatedExprVisitor<SequenceChecker> Base;
7643 
7644   /// \brief A tree of sequenced regions within an expression. Two regions are
7645   /// unsequenced if one is an ancestor or a descendent of the other. When we
7646   /// finish processing an expression with sequencing, such as a comma
7647   /// expression, we fold its tree nodes into its parent, since they are
7648   /// unsequenced with respect to nodes we will visit later.
7649   class SequenceTree {
7650     struct Value {
7651       explicit Value(unsigned Parent) : Parent(Parent), Merged(false) {}
7652       unsigned Parent : 31;
7653       bool Merged : 1;
7654     };
7655     SmallVector<Value, 8> Values;
7656 
7657   public:
7658     /// \brief A region within an expression which may be sequenced with respect
7659     /// to some other region.
7660     class Seq {
7661       explicit Seq(unsigned N) : Index(N) {}
7662       unsigned Index;
7663       friend class SequenceTree;
7664     public:
7665       Seq() : Index(0) {}
7666     };
7667 
7668     SequenceTree() { Values.push_back(Value(0)); }
7669     Seq root() const { return Seq(0); }
7670 
7671     /// \brief Create a new sequence of operations, which is an unsequenced
7672     /// subset of \p Parent. This sequence of operations is sequenced with
7673     /// respect to other children of \p Parent.
7674     Seq allocate(Seq Parent) {
7675       Values.push_back(Value(Parent.Index));
7676       return Seq(Values.size() - 1);
7677     }
7678 
7679     /// \brief Merge a sequence of operations into its parent.
7680     void merge(Seq S) {
7681       Values[S.Index].Merged = true;
7682     }
7683 
7684     /// \brief Determine whether two operations are unsequenced. This operation
7685     /// is asymmetric: \p Cur should be the more recent sequence, and \p Old
7686     /// should have been merged into its parent as appropriate.
7687     bool isUnsequenced(Seq Cur, Seq Old) {
7688       unsigned C = representative(Cur.Index);
7689       unsigned Target = representative(Old.Index);
7690       while (C >= Target) {
7691         if (C == Target)
7692           return true;
7693         C = Values[C].Parent;
7694       }
7695       return false;
7696     }
7697 
7698   private:
7699     /// \brief Pick a representative for a sequence.
7700     unsigned representative(unsigned K) {
7701       if (Values[K].Merged)
7702         // Perform path compression as we go.
7703         return Values[K].Parent = representative(Values[K].Parent);
7704       return K;
7705     }
7706   };
7707 
7708   /// An object for which we can track unsequenced uses.
7709   typedef NamedDecl *Object;
7710 
7711   /// Different flavors of object usage which we track. We only track the
7712   /// least-sequenced usage of each kind.
7713   enum UsageKind {
7714     /// A read of an object. Multiple unsequenced reads are OK.
7715     UK_Use,
7716     /// A modification of an object which is sequenced before the value
7717     /// computation of the expression, such as ++n in C++.
7718     UK_ModAsValue,
7719     /// A modification of an object which is not sequenced before the value
7720     /// computation of the expression, such as n++.
7721     UK_ModAsSideEffect,
7722 
7723     UK_Count = UK_ModAsSideEffect + 1
7724   };
7725 
7726   struct Usage {
7727     Usage() : Use(nullptr), Seq() {}
7728     Expr *Use;
7729     SequenceTree::Seq Seq;
7730   };
7731 
7732   struct UsageInfo {
7733     UsageInfo() : Diagnosed(false) {}
7734     Usage Uses[UK_Count];
7735     /// Have we issued a diagnostic for this variable already?
7736     bool Diagnosed;
7737   };
7738   typedef llvm::SmallDenseMap<Object, UsageInfo, 16> UsageInfoMap;
7739 
7740   Sema &SemaRef;
7741   /// Sequenced regions within the expression.
7742   SequenceTree Tree;
7743   /// Declaration modifications and references which we have seen.
7744   UsageInfoMap UsageMap;
7745   /// The region we are currently within.
7746   SequenceTree::Seq Region;
7747   /// Filled in with declarations which were modified as a side-effect
7748   /// (that is, post-increment operations).
7749   SmallVectorImpl<std::pair<Object, Usage> > *ModAsSideEffect;
7750   /// Expressions to check later. We defer checking these to reduce
7751   /// stack usage.
7752   SmallVectorImpl<Expr *> &WorkList;
7753 
7754   /// RAII object wrapping the visitation of a sequenced subexpression of an
7755   /// expression. At the end of this process, the side-effects of the evaluation
7756   /// become sequenced with respect to the value computation of the result, so
7757   /// we downgrade any UK_ModAsSideEffect within the evaluation to
7758   /// UK_ModAsValue.
7759   struct SequencedSubexpression {
7760     SequencedSubexpression(SequenceChecker &Self)
7761       : Self(Self), OldModAsSideEffect(Self.ModAsSideEffect) {
7762       Self.ModAsSideEffect = &ModAsSideEffect;
7763     }
7764     ~SequencedSubexpression() {
7765       for (auto MI = ModAsSideEffect.rbegin(), ME = ModAsSideEffect.rend();
7766            MI != ME; ++MI) {
7767         UsageInfo &U = Self.UsageMap[MI->first];
7768         auto &SideEffectUsage = U.Uses[UK_ModAsSideEffect];
7769         Self.addUsage(U, MI->first, SideEffectUsage.Use, UK_ModAsValue);
7770         SideEffectUsage = MI->second;
7771       }
7772       Self.ModAsSideEffect = OldModAsSideEffect;
7773     }
7774 
7775     SequenceChecker &Self;
7776     SmallVector<std::pair<Object, Usage>, 4> ModAsSideEffect;
7777     SmallVectorImpl<std::pair<Object, Usage> > *OldModAsSideEffect;
7778   };
7779 
7780   /// RAII object wrapping the visitation of a subexpression which we might
7781   /// choose to evaluate as a constant. If any subexpression is evaluated and
7782   /// found to be non-constant, this allows us to suppress the evaluation of
7783   /// the outer expression.
7784   class EvaluationTracker {
7785   public:
7786     EvaluationTracker(SequenceChecker &Self)
7787         : Self(Self), Prev(Self.EvalTracker), EvalOK(true) {
7788       Self.EvalTracker = this;
7789     }
7790     ~EvaluationTracker() {
7791       Self.EvalTracker = Prev;
7792       if (Prev)
7793         Prev->EvalOK &= EvalOK;
7794     }
7795 
7796     bool evaluate(const Expr *E, bool &Result) {
7797       if (!EvalOK || E->isValueDependent())
7798         return false;
7799       EvalOK = E->EvaluateAsBooleanCondition(Result, Self.SemaRef.Context);
7800       return EvalOK;
7801     }
7802 
7803   private:
7804     SequenceChecker &Self;
7805     EvaluationTracker *Prev;
7806     bool EvalOK;
7807   } *EvalTracker;
7808 
7809   /// \brief Find the object which is produced by the specified expression,
7810   /// if any.
7811   Object getObject(Expr *E, bool Mod) const {
7812     E = E->IgnoreParenCasts();
7813     if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
7814       if (Mod && (UO->getOpcode() == UO_PreInc || UO->getOpcode() == UO_PreDec))
7815         return getObject(UO->getSubExpr(), Mod);
7816     } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
7817       if (BO->getOpcode() == BO_Comma)
7818         return getObject(BO->getRHS(), Mod);
7819       if (Mod && BO->isAssignmentOp())
7820         return getObject(BO->getLHS(), Mod);
7821     } else if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
7822       // FIXME: Check for more interesting cases, like "x.n = ++x.n".
7823       if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenCasts()))
7824         return ME->getMemberDecl();
7825     } else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
7826       // FIXME: If this is a reference, map through to its value.
7827       return DRE->getDecl();
7828     return nullptr;
7829   }
7830 
7831   /// \brief Note that an object was modified or used by an expression.
7832   void addUsage(UsageInfo &UI, Object O, Expr *Ref, UsageKind UK) {
7833     Usage &U = UI.Uses[UK];
7834     if (!U.Use || !Tree.isUnsequenced(Region, U.Seq)) {
7835       if (UK == UK_ModAsSideEffect && ModAsSideEffect)
7836         ModAsSideEffect->push_back(std::make_pair(O, U));
7837       U.Use = Ref;
7838       U.Seq = Region;
7839     }
7840   }
7841   /// \brief Check whether a modification or use conflicts with a prior usage.
7842   void checkUsage(Object O, UsageInfo &UI, Expr *Ref, UsageKind OtherKind,
7843                   bool IsModMod) {
7844     if (UI.Diagnosed)
7845       return;
7846 
7847     const Usage &U = UI.Uses[OtherKind];
7848     if (!U.Use || !Tree.isUnsequenced(Region, U.Seq))
7849       return;
7850 
7851     Expr *Mod = U.Use;
7852     Expr *ModOrUse = Ref;
7853     if (OtherKind == UK_Use)
7854       std::swap(Mod, ModOrUse);
7855 
7856     SemaRef.Diag(Mod->getExprLoc(),
7857                  IsModMod ? diag::warn_unsequenced_mod_mod
7858                           : diag::warn_unsequenced_mod_use)
7859       << O << SourceRange(ModOrUse->getExprLoc());
7860     UI.Diagnosed = true;
7861   }
7862 
7863   void notePreUse(Object O, Expr *Use) {
7864     UsageInfo &U = UsageMap[O];
7865     // Uses conflict with other modifications.
7866     checkUsage(O, U, Use, UK_ModAsValue, false);
7867   }
7868   void notePostUse(Object O, Expr *Use) {
7869     UsageInfo &U = UsageMap[O];
7870     checkUsage(O, U, Use, UK_ModAsSideEffect, false);
7871     addUsage(U, O, Use, UK_Use);
7872   }
7873 
7874   void notePreMod(Object O, Expr *Mod) {
7875     UsageInfo &U = UsageMap[O];
7876     // Modifications conflict with other modifications and with uses.
7877     checkUsage(O, U, Mod, UK_ModAsValue, true);
7878     checkUsage(O, U, Mod, UK_Use, false);
7879   }
7880   void notePostMod(Object O, Expr *Use, UsageKind UK) {
7881     UsageInfo &U = UsageMap[O];
7882     checkUsage(O, U, Use, UK_ModAsSideEffect, true);
7883     addUsage(U, O, Use, UK);
7884   }
7885 
7886 public:
7887   SequenceChecker(Sema &S, Expr *E, SmallVectorImpl<Expr *> &WorkList)
7888       : Base(S.Context), SemaRef(S), Region(Tree.root()),
7889         ModAsSideEffect(nullptr), WorkList(WorkList), EvalTracker(nullptr) {
7890     Visit(E);
7891   }
7892 
7893   void VisitStmt(Stmt *S) {
7894     // Skip all statements which aren't expressions for now.
7895   }
7896 
7897   void VisitExpr(Expr *E) {
7898     // By default, just recurse to evaluated subexpressions.
7899     Base::VisitStmt(E);
7900   }
7901 
7902   void VisitCastExpr(CastExpr *E) {
7903     Object O = Object();
7904     if (E->getCastKind() == CK_LValueToRValue)
7905       O = getObject(E->getSubExpr(), false);
7906 
7907     if (O)
7908       notePreUse(O, E);
7909     VisitExpr(E);
7910     if (O)
7911       notePostUse(O, E);
7912   }
7913 
7914   void VisitBinComma(BinaryOperator *BO) {
7915     // C++11 [expr.comma]p1:
7916     //   Every value computation and side effect associated with the left
7917     //   expression is sequenced before every value computation and side
7918     //   effect associated with the right expression.
7919     SequenceTree::Seq LHS = Tree.allocate(Region);
7920     SequenceTree::Seq RHS = Tree.allocate(Region);
7921     SequenceTree::Seq OldRegion = Region;
7922 
7923     {
7924       SequencedSubexpression SeqLHS(*this);
7925       Region = LHS;
7926       Visit(BO->getLHS());
7927     }
7928 
7929     Region = RHS;
7930     Visit(BO->getRHS());
7931 
7932     Region = OldRegion;
7933 
7934     // Forget that LHS and RHS are sequenced. They are both unsequenced
7935     // with respect to other stuff.
7936     Tree.merge(LHS);
7937     Tree.merge(RHS);
7938   }
7939 
7940   void VisitBinAssign(BinaryOperator *BO) {
7941     // The modification is sequenced after the value computation of the LHS
7942     // and RHS, so check it before inspecting the operands and update the
7943     // map afterwards.
7944     Object O = getObject(BO->getLHS(), true);
7945     if (!O)
7946       return VisitExpr(BO);
7947 
7948     notePreMod(O, BO);
7949 
7950     // C++11 [expr.ass]p7:
7951     //   E1 op= E2 is equivalent to E1 = E1 op E2, except that E1 is evaluated
7952     //   only once.
7953     //
7954     // Therefore, for a compound assignment operator, O is considered used
7955     // everywhere except within the evaluation of E1 itself.
7956     if (isa<CompoundAssignOperator>(BO))
7957       notePreUse(O, BO);
7958 
7959     Visit(BO->getLHS());
7960 
7961     if (isa<CompoundAssignOperator>(BO))
7962       notePostUse(O, BO);
7963 
7964     Visit(BO->getRHS());
7965 
7966     // C++11 [expr.ass]p1:
7967     //   the assignment is sequenced [...] before the value computation of the
7968     //   assignment expression.
7969     // C11 6.5.16/3 has no such rule.
7970     notePostMod(O, BO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue
7971                                                        : UK_ModAsSideEffect);
7972   }
7973   void VisitCompoundAssignOperator(CompoundAssignOperator *CAO) {
7974     VisitBinAssign(CAO);
7975   }
7976 
7977   void VisitUnaryPreInc(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); }
7978   void VisitUnaryPreDec(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); }
7979   void VisitUnaryPreIncDec(UnaryOperator *UO) {
7980     Object O = getObject(UO->getSubExpr(), true);
7981     if (!O)
7982       return VisitExpr(UO);
7983 
7984     notePreMod(O, UO);
7985     Visit(UO->getSubExpr());
7986     // C++11 [expr.pre.incr]p1:
7987     //   the expression ++x is equivalent to x+=1
7988     notePostMod(O, UO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue
7989                                                        : UK_ModAsSideEffect);
7990   }
7991 
7992   void VisitUnaryPostInc(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); }
7993   void VisitUnaryPostDec(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); }
7994   void VisitUnaryPostIncDec(UnaryOperator *UO) {
7995     Object O = getObject(UO->getSubExpr(), true);
7996     if (!O)
7997       return VisitExpr(UO);
7998 
7999     notePreMod(O, UO);
8000     Visit(UO->getSubExpr());
8001     notePostMod(O, UO, UK_ModAsSideEffect);
8002   }
8003 
8004   /// Don't visit the RHS of '&&' or '||' if it might not be evaluated.
8005   void VisitBinLOr(BinaryOperator *BO) {
8006     // The side-effects of the LHS of an '&&' are sequenced before the
8007     // value computation of the RHS, and hence before the value computation
8008     // of the '&&' itself, unless the LHS evaluates to zero. We treat them
8009     // as if they were unconditionally sequenced.
8010     EvaluationTracker Eval(*this);
8011     {
8012       SequencedSubexpression Sequenced(*this);
8013       Visit(BO->getLHS());
8014     }
8015 
8016     bool Result;
8017     if (Eval.evaluate(BO->getLHS(), Result)) {
8018       if (!Result)
8019         Visit(BO->getRHS());
8020     } else {
8021       // Check for unsequenced operations in the RHS, treating it as an
8022       // entirely separate evaluation.
8023       //
8024       // FIXME: If there are operations in the RHS which are unsequenced
8025       // with respect to operations outside the RHS, and those operations
8026       // are unconditionally evaluated, diagnose them.
8027       WorkList.push_back(BO->getRHS());
8028     }
8029   }
8030   void VisitBinLAnd(BinaryOperator *BO) {
8031     EvaluationTracker Eval(*this);
8032     {
8033       SequencedSubexpression Sequenced(*this);
8034       Visit(BO->getLHS());
8035     }
8036 
8037     bool Result;
8038     if (Eval.evaluate(BO->getLHS(), Result)) {
8039       if (Result)
8040         Visit(BO->getRHS());
8041     } else {
8042       WorkList.push_back(BO->getRHS());
8043     }
8044   }
8045 
8046   // Only visit the condition, unless we can be sure which subexpression will
8047   // be chosen.
8048   void VisitAbstractConditionalOperator(AbstractConditionalOperator *CO) {
8049     EvaluationTracker Eval(*this);
8050     {
8051       SequencedSubexpression Sequenced(*this);
8052       Visit(CO->getCond());
8053     }
8054 
8055     bool Result;
8056     if (Eval.evaluate(CO->getCond(), Result))
8057       Visit(Result ? CO->getTrueExpr() : CO->getFalseExpr());
8058     else {
8059       WorkList.push_back(CO->getTrueExpr());
8060       WorkList.push_back(CO->getFalseExpr());
8061     }
8062   }
8063 
8064   void VisitCallExpr(CallExpr *CE) {
8065     // C++11 [intro.execution]p15:
8066     //   When calling a function [...], every value computation and side effect
8067     //   associated with any argument expression, or with the postfix expression
8068     //   designating the called function, is sequenced before execution of every
8069     //   expression or statement in the body of the function [and thus before
8070     //   the value computation of its result].
8071     SequencedSubexpression Sequenced(*this);
8072     Base::VisitCallExpr(CE);
8073 
8074     // FIXME: CXXNewExpr and CXXDeleteExpr implicitly call functions.
8075   }
8076 
8077   void VisitCXXConstructExpr(CXXConstructExpr *CCE) {
8078     // This is a call, so all subexpressions are sequenced before the result.
8079     SequencedSubexpression Sequenced(*this);
8080 
8081     if (!CCE->isListInitialization())
8082       return VisitExpr(CCE);
8083 
8084     // In C++11, list initializations are sequenced.
8085     SmallVector<SequenceTree::Seq, 32> Elts;
8086     SequenceTree::Seq Parent = Region;
8087     for (CXXConstructExpr::arg_iterator I = CCE->arg_begin(),
8088                                         E = CCE->arg_end();
8089          I != E; ++I) {
8090       Region = Tree.allocate(Parent);
8091       Elts.push_back(Region);
8092       Visit(*I);
8093     }
8094 
8095     // Forget that the initializers are sequenced.
8096     Region = Parent;
8097     for (unsigned I = 0; I < Elts.size(); ++I)
8098       Tree.merge(Elts[I]);
8099   }
8100 
8101   void VisitInitListExpr(InitListExpr *ILE) {
8102     if (!SemaRef.getLangOpts().CPlusPlus11)
8103       return VisitExpr(ILE);
8104 
8105     // In C++11, list initializations are sequenced.
8106     SmallVector<SequenceTree::Seq, 32> Elts;
8107     SequenceTree::Seq Parent = Region;
8108     for (unsigned I = 0; I < ILE->getNumInits(); ++I) {
8109       Expr *E = ILE->getInit(I);
8110       if (!E) continue;
8111       Region = Tree.allocate(Parent);
8112       Elts.push_back(Region);
8113       Visit(E);
8114     }
8115 
8116     // Forget that the initializers are sequenced.
8117     Region = Parent;
8118     for (unsigned I = 0; I < Elts.size(); ++I)
8119       Tree.merge(Elts[I]);
8120   }
8121 };
8122 }
8123 
8124 void Sema::CheckUnsequencedOperations(Expr *E) {
8125   SmallVector<Expr *, 8> WorkList;
8126   WorkList.push_back(E);
8127   while (!WorkList.empty()) {
8128     Expr *Item = WorkList.pop_back_val();
8129     SequenceChecker(*this, Item, WorkList);
8130   }
8131 }
8132 
8133 void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc,
8134                               bool IsConstexpr) {
8135   CheckImplicitConversions(E, CheckLoc);
8136   CheckUnsequencedOperations(E);
8137   if (!IsConstexpr && !E->isValueDependent())
8138     CheckForIntOverflow(E);
8139 }
8140 
8141 void Sema::CheckBitFieldInitialization(SourceLocation InitLoc,
8142                                        FieldDecl *BitField,
8143                                        Expr *Init) {
8144   (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc);
8145 }
8146 
8147 static void diagnoseArrayStarInParamType(Sema &S, QualType PType,
8148                                          SourceLocation Loc) {
8149   if (!PType->isVariablyModifiedType())
8150     return;
8151   if (const auto *PointerTy = dyn_cast<PointerType>(PType)) {
8152     diagnoseArrayStarInParamType(S, PointerTy->getPointeeType(), Loc);
8153     return;
8154   }
8155   if (const auto *ReferenceTy = dyn_cast<ReferenceType>(PType)) {
8156     diagnoseArrayStarInParamType(S, ReferenceTy->getPointeeType(), Loc);
8157     return;
8158   }
8159   if (const auto *ParenTy = dyn_cast<ParenType>(PType)) {
8160     diagnoseArrayStarInParamType(S, ParenTy->getInnerType(), Loc);
8161     return;
8162   }
8163 
8164   const ArrayType *AT = S.Context.getAsArrayType(PType);
8165   if (!AT)
8166     return;
8167 
8168   if (AT->getSizeModifier() != ArrayType::Star) {
8169     diagnoseArrayStarInParamType(S, AT->getElementType(), Loc);
8170     return;
8171   }
8172 
8173   S.Diag(Loc, diag::err_array_star_in_function_definition);
8174 }
8175 
8176 /// CheckParmsForFunctionDef - Check that the parameters of the given
8177 /// function are appropriate for the definition of a function. This
8178 /// takes care of any checks that cannot be performed on the
8179 /// declaration itself, e.g., that the types of each of the function
8180 /// parameters are complete.
8181 bool Sema::CheckParmsForFunctionDef(ParmVarDecl *const *P,
8182                                     ParmVarDecl *const *PEnd,
8183                                     bool CheckParameterNames) {
8184   bool HasInvalidParm = false;
8185   for (; P != PEnd; ++P) {
8186     ParmVarDecl *Param = *P;
8187 
8188     // C99 6.7.5.3p4: the parameters in a parameter type list in a
8189     // function declarator that is part of a function definition of
8190     // that function shall not have incomplete type.
8191     //
8192     // This is also C++ [dcl.fct]p6.
8193     if (!Param->isInvalidDecl() &&
8194         RequireCompleteType(Param->getLocation(), Param->getType(),
8195                             diag::err_typecheck_decl_incomplete_type)) {
8196       Param->setInvalidDecl();
8197       HasInvalidParm = true;
8198     }
8199 
8200     // C99 6.9.1p5: If the declarator includes a parameter type list, the
8201     // declaration of each parameter shall include an identifier.
8202     if (CheckParameterNames &&
8203         Param->getIdentifier() == nullptr &&
8204         !Param->isImplicit() &&
8205         !getLangOpts().CPlusPlus)
8206       Diag(Param->getLocation(), diag::err_parameter_name_omitted);
8207 
8208     // C99 6.7.5.3p12:
8209     //   If the function declarator is not part of a definition of that
8210     //   function, parameters may have incomplete type and may use the [*]
8211     //   notation in their sequences of declarator specifiers to specify
8212     //   variable length array types.
8213     QualType PType = Param->getOriginalType();
8214     // FIXME: This diagnostic should point the '[*]' if source-location
8215     // information is added for it.
8216     diagnoseArrayStarInParamType(*this, PType, Param->getLocation());
8217 
8218     // MSVC destroys objects passed by value in the callee.  Therefore a
8219     // function definition which takes such a parameter must be able to call the
8220     // object's destructor.  However, we don't perform any direct access check
8221     // on the dtor.
8222     if (getLangOpts().CPlusPlus && Context.getTargetInfo()
8223                                        .getCXXABI()
8224                                        .areArgsDestroyedLeftToRightInCallee()) {
8225       if (!Param->isInvalidDecl()) {
8226         if (const RecordType *RT = Param->getType()->getAs<RecordType>()) {
8227           CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl());
8228           if (!ClassDecl->isInvalidDecl() &&
8229               !ClassDecl->hasIrrelevantDestructor() &&
8230               !ClassDecl->isDependentContext()) {
8231             CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl);
8232             MarkFunctionReferenced(Param->getLocation(), Destructor);
8233             DiagnoseUseOfDecl(Destructor, Param->getLocation());
8234           }
8235         }
8236       }
8237     }
8238   }
8239 
8240   return HasInvalidParm;
8241 }
8242 
8243 /// CheckCastAlign - Implements -Wcast-align, which warns when a
8244 /// pointer cast increases the alignment requirements.
8245 void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) {
8246   // This is actually a lot of work to potentially be doing on every
8247   // cast; don't do it if we're ignoring -Wcast_align (as is the default).
8248   if (getDiagnostics().isIgnored(diag::warn_cast_align, TRange.getBegin()))
8249     return;
8250 
8251   // Ignore dependent types.
8252   if (T->isDependentType() || Op->getType()->isDependentType())
8253     return;
8254 
8255   // Require that the destination be a pointer type.
8256   const PointerType *DestPtr = T->getAs<PointerType>();
8257   if (!DestPtr) return;
8258 
8259   // If the destination has alignment 1, we're done.
8260   QualType DestPointee = DestPtr->getPointeeType();
8261   if (DestPointee->isIncompleteType()) return;
8262   CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee);
8263   if (DestAlign.isOne()) return;
8264 
8265   // Require that the source be a pointer type.
8266   const PointerType *SrcPtr = Op->getType()->getAs<PointerType>();
8267   if (!SrcPtr) return;
8268   QualType SrcPointee = SrcPtr->getPointeeType();
8269 
8270   // Whitelist casts from cv void*.  We already implicitly
8271   // whitelisted casts to cv void*, since they have alignment 1.
8272   // Also whitelist casts involving incomplete types, which implicitly
8273   // includes 'void'.
8274   if (SrcPointee->isIncompleteType()) return;
8275 
8276   CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee);
8277   if (SrcAlign >= DestAlign) return;
8278 
8279   Diag(TRange.getBegin(), diag::warn_cast_align)
8280     << Op->getType() << T
8281     << static_cast<unsigned>(SrcAlign.getQuantity())
8282     << static_cast<unsigned>(DestAlign.getQuantity())
8283     << TRange << Op->getSourceRange();
8284 }
8285 
8286 static const Type* getElementType(const Expr *BaseExpr) {
8287   const Type* EltType = BaseExpr->getType().getTypePtr();
8288   if (EltType->isAnyPointerType())
8289     return EltType->getPointeeType().getTypePtr();
8290   else if (EltType->isArrayType())
8291     return EltType->getBaseElementTypeUnsafe();
8292   return EltType;
8293 }
8294 
8295 /// \brief Check whether this array fits the idiom of a size-one tail padded
8296 /// array member of a struct.
8297 ///
8298 /// We avoid emitting out-of-bounds access warnings for such arrays as they are
8299 /// commonly used to emulate flexible arrays in C89 code.
8300 static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size,
8301                                     const NamedDecl *ND) {
8302   if (Size != 1 || !ND) return false;
8303 
8304   const FieldDecl *FD = dyn_cast<FieldDecl>(ND);
8305   if (!FD) return false;
8306 
8307   // Don't consider sizes resulting from macro expansions or template argument
8308   // substitution to form C89 tail-padded arrays.
8309 
8310   TypeSourceInfo *TInfo = FD->getTypeSourceInfo();
8311   while (TInfo) {
8312     TypeLoc TL = TInfo->getTypeLoc();
8313     // Look through typedefs.
8314     if (TypedefTypeLoc TTL = TL.getAs<TypedefTypeLoc>()) {
8315       const TypedefNameDecl *TDL = TTL.getTypedefNameDecl();
8316       TInfo = TDL->getTypeSourceInfo();
8317       continue;
8318     }
8319     if (ConstantArrayTypeLoc CTL = TL.getAs<ConstantArrayTypeLoc>()) {
8320       const Expr *SizeExpr = dyn_cast<IntegerLiteral>(CTL.getSizeExpr());
8321       if (!SizeExpr || SizeExpr->getExprLoc().isMacroID())
8322         return false;
8323     }
8324     break;
8325   }
8326 
8327   const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext());
8328   if (!RD) return false;
8329   if (RD->isUnion()) return false;
8330   if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
8331     if (!CRD->isStandardLayout()) return false;
8332   }
8333 
8334   // See if this is the last field decl in the record.
8335   const Decl *D = FD;
8336   while ((D = D->getNextDeclInContext()))
8337     if (isa<FieldDecl>(D))
8338       return false;
8339   return true;
8340 }
8341 
8342 void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
8343                             const ArraySubscriptExpr *ASE,
8344                             bool AllowOnePastEnd, bool IndexNegated) {
8345   IndexExpr = IndexExpr->IgnoreParenImpCasts();
8346   if (IndexExpr->isValueDependent())
8347     return;
8348 
8349   const Type *EffectiveType = getElementType(BaseExpr);
8350   BaseExpr = BaseExpr->IgnoreParenCasts();
8351   const ConstantArrayType *ArrayTy =
8352     Context.getAsConstantArrayType(BaseExpr->getType());
8353   if (!ArrayTy)
8354     return;
8355 
8356   llvm::APSInt index;
8357   if (!IndexExpr->EvaluateAsInt(index, Context))
8358     return;
8359   if (IndexNegated)
8360     index = -index;
8361 
8362   const NamedDecl *ND = nullptr;
8363   if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
8364     ND = dyn_cast<NamedDecl>(DRE->getDecl());
8365   if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
8366     ND = dyn_cast<NamedDecl>(ME->getMemberDecl());
8367 
8368   if (index.isUnsigned() || !index.isNegative()) {
8369     llvm::APInt size = ArrayTy->getSize();
8370     if (!size.isStrictlyPositive())
8371       return;
8372 
8373     const Type* BaseType = getElementType(BaseExpr);
8374     if (BaseType != EffectiveType) {
8375       // Make sure we're comparing apples to apples when comparing index to size
8376       uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType);
8377       uint64_t array_typesize = Context.getTypeSize(BaseType);
8378       // Handle ptrarith_typesize being zero, such as when casting to void*
8379       if (!ptrarith_typesize) ptrarith_typesize = 1;
8380       if (ptrarith_typesize != array_typesize) {
8381         // There's a cast to a different size type involved
8382         uint64_t ratio = array_typesize / ptrarith_typesize;
8383         // TODO: Be smarter about handling cases where array_typesize is not a
8384         // multiple of ptrarith_typesize
8385         if (ptrarith_typesize * ratio == array_typesize)
8386           size *= llvm::APInt(size.getBitWidth(), ratio);
8387       }
8388     }
8389 
8390     if (size.getBitWidth() > index.getBitWidth())
8391       index = index.zext(size.getBitWidth());
8392     else if (size.getBitWidth() < index.getBitWidth())
8393       size = size.zext(index.getBitWidth());
8394 
8395     // For array subscripting the index must be less than size, but for pointer
8396     // arithmetic also allow the index (offset) to be equal to size since
8397     // computing the next address after the end of the array is legal and
8398     // commonly done e.g. in C++ iterators and range-based for loops.
8399     if (AllowOnePastEnd ? index.ule(size) : index.ult(size))
8400       return;
8401 
8402     // Also don't warn for arrays of size 1 which are members of some
8403     // structure. These are often used to approximate flexible arrays in C89
8404     // code.
8405     if (IsTailPaddedMemberArray(*this, size, ND))
8406       return;
8407 
8408     // Suppress the warning if the subscript expression (as identified by the
8409     // ']' location) and the index expression are both from macro expansions
8410     // within a system header.
8411     if (ASE) {
8412       SourceLocation RBracketLoc = SourceMgr.getSpellingLoc(
8413           ASE->getRBracketLoc());
8414       if (SourceMgr.isInSystemHeader(RBracketLoc)) {
8415         SourceLocation IndexLoc = SourceMgr.getSpellingLoc(
8416             IndexExpr->getLocStart());
8417         if (SourceMgr.isWrittenInSameFile(RBracketLoc, IndexLoc))
8418           return;
8419       }
8420     }
8421 
8422     unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds;
8423     if (ASE)
8424       DiagID = diag::warn_array_index_exceeds_bounds;
8425 
8426     DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr,
8427                         PDiag(DiagID) << index.toString(10, true)
8428                           << size.toString(10, true)
8429                           << (unsigned)size.getLimitedValue(~0U)
8430                           << IndexExpr->getSourceRange());
8431   } else {
8432     unsigned DiagID = diag::warn_array_index_precedes_bounds;
8433     if (!ASE) {
8434       DiagID = diag::warn_ptr_arith_precedes_bounds;
8435       if (index.isNegative()) index = -index;
8436     }
8437 
8438     DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr,
8439                         PDiag(DiagID) << index.toString(10, true)
8440                           << IndexExpr->getSourceRange());
8441   }
8442 
8443   if (!ND) {
8444     // Try harder to find a NamedDecl to point at in the note.
8445     while (const ArraySubscriptExpr *ASE =
8446            dyn_cast<ArraySubscriptExpr>(BaseExpr))
8447       BaseExpr = ASE->getBase()->IgnoreParenCasts();
8448     if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
8449       ND = dyn_cast<NamedDecl>(DRE->getDecl());
8450     if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
8451       ND = dyn_cast<NamedDecl>(ME->getMemberDecl());
8452   }
8453 
8454   if (ND)
8455     DiagRuntimeBehavior(ND->getLocStart(), BaseExpr,
8456                         PDiag(diag::note_array_index_out_of_bounds)
8457                           << ND->getDeclName());
8458 }
8459 
8460 void Sema::CheckArrayAccess(const Expr *expr) {
8461   int AllowOnePastEnd = 0;
8462   while (expr) {
8463     expr = expr->IgnoreParenImpCasts();
8464     switch (expr->getStmtClass()) {
8465       case Stmt::ArraySubscriptExprClass: {
8466         const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr);
8467         CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE,
8468                          AllowOnePastEnd > 0);
8469         return;
8470       }
8471       case Stmt::OMPArraySectionExprClass: {
8472         const OMPArraySectionExpr *ASE = cast<OMPArraySectionExpr>(expr);
8473         if (ASE->getLowerBound())
8474           CheckArrayAccess(ASE->getBase(), ASE->getLowerBound(),
8475                            /*ASE=*/nullptr, AllowOnePastEnd > 0);
8476         return;
8477       }
8478       case Stmt::UnaryOperatorClass: {
8479         // Only unwrap the * and & unary operators
8480         const UnaryOperator *UO = cast<UnaryOperator>(expr);
8481         expr = UO->getSubExpr();
8482         switch (UO->getOpcode()) {
8483           case UO_AddrOf:
8484             AllowOnePastEnd++;
8485             break;
8486           case UO_Deref:
8487             AllowOnePastEnd--;
8488             break;
8489           default:
8490             return;
8491         }
8492         break;
8493       }
8494       case Stmt::ConditionalOperatorClass: {
8495         const ConditionalOperator *cond = cast<ConditionalOperator>(expr);
8496         if (const Expr *lhs = cond->getLHS())
8497           CheckArrayAccess(lhs);
8498         if (const Expr *rhs = cond->getRHS())
8499           CheckArrayAccess(rhs);
8500         return;
8501       }
8502       default:
8503         return;
8504     }
8505   }
8506 }
8507 
8508 //===--- CHECK: Objective-C retain cycles ----------------------------------//
8509 
8510 namespace {
8511   struct RetainCycleOwner {
8512     RetainCycleOwner() : Variable(nullptr), Indirect(false) {}
8513     VarDecl *Variable;
8514     SourceRange Range;
8515     SourceLocation Loc;
8516     bool Indirect;
8517 
8518     void setLocsFrom(Expr *e) {
8519       Loc = e->getExprLoc();
8520       Range = e->getSourceRange();
8521     }
8522   };
8523 }
8524 
8525 /// Consider whether capturing the given variable can possibly lead to
8526 /// a retain cycle.
8527 static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) {
8528   // In ARC, it's captured strongly iff the variable has __strong
8529   // lifetime.  In MRR, it's captured strongly if the variable is
8530   // __block and has an appropriate type.
8531   if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
8532     return false;
8533 
8534   owner.Variable = var;
8535   if (ref)
8536     owner.setLocsFrom(ref);
8537   return true;
8538 }
8539 
8540 static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) {
8541   while (true) {
8542     e = e->IgnoreParens();
8543     if (CastExpr *cast = dyn_cast<CastExpr>(e)) {
8544       switch (cast->getCastKind()) {
8545       case CK_BitCast:
8546       case CK_LValueBitCast:
8547       case CK_LValueToRValue:
8548       case CK_ARCReclaimReturnedObject:
8549         e = cast->getSubExpr();
8550         continue;
8551 
8552       default:
8553         return false;
8554       }
8555     }
8556 
8557     if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) {
8558       ObjCIvarDecl *ivar = ref->getDecl();
8559       if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
8560         return false;
8561 
8562       // Try to find a retain cycle in the base.
8563       if (!findRetainCycleOwner(S, ref->getBase(), owner))
8564         return false;
8565 
8566       if (ref->isFreeIvar()) owner.setLocsFrom(ref);
8567       owner.Indirect = true;
8568       return true;
8569     }
8570 
8571     if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) {
8572       VarDecl *var = dyn_cast<VarDecl>(ref->getDecl());
8573       if (!var) return false;
8574       return considerVariable(var, ref, owner);
8575     }
8576 
8577     if (MemberExpr *member = dyn_cast<MemberExpr>(e)) {
8578       if (member->isArrow()) return false;
8579 
8580       // Don't count this as an indirect ownership.
8581       e = member->getBase();
8582       continue;
8583     }
8584 
8585     if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) {
8586       // Only pay attention to pseudo-objects on property references.
8587       ObjCPropertyRefExpr *pre
8588         = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm()
8589                                               ->IgnoreParens());
8590       if (!pre) return false;
8591       if (pre->isImplicitProperty()) return false;
8592       ObjCPropertyDecl *property = pre->getExplicitProperty();
8593       if (!property->isRetaining() &&
8594           !(property->getPropertyIvarDecl() &&
8595             property->getPropertyIvarDecl()->getType()
8596               .getObjCLifetime() == Qualifiers::OCL_Strong))
8597           return false;
8598 
8599       owner.Indirect = true;
8600       if (pre->isSuperReceiver()) {
8601         owner.Variable = S.getCurMethodDecl()->getSelfDecl();
8602         if (!owner.Variable)
8603           return false;
8604         owner.Loc = pre->getLocation();
8605         owner.Range = pre->getSourceRange();
8606         return true;
8607       }
8608       e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase())
8609                               ->getSourceExpr());
8610       continue;
8611     }
8612 
8613     // Array ivars?
8614 
8615     return false;
8616   }
8617 }
8618 
8619 namespace {
8620   struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> {
8621     FindCaptureVisitor(ASTContext &Context, VarDecl *variable)
8622       : EvaluatedExprVisitor<FindCaptureVisitor>(Context),
8623         Context(Context), Variable(variable), Capturer(nullptr),
8624         VarWillBeReased(false) {}
8625     ASTContext &Context;
8626     VarDecl *Variable;
8627     Expr *Capturer;
8628     bool VarWillBeReased;
8629 
8630     void VisitDeclRefExpr(DeclRefExpr *ref) {
8631       if (ref->getDecl() == Variable && !Capturer)
8632         Capturer = ref;
8633     }
8634 
8635     void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) {
8636       if (Capturer) return;
8637       Visit(ref->getBase());
8638       if (Capturer && ref->isFreeIvar())
8639         Capturer = ref;
8640     }
8641 
8642     void VisitBlockExpr(BlockExpr *block) {
8643       // Look inside nested blocks
8644       if (block->getBlockDecl()->capturesVariable(Variable))
8645         Visit(block->getBlockDecl()->getBody());
8646     }
8647 
8648     void VisitOpaqueValueExpr(OpaqueValueExpr *OVE) {
8649       if (Capturer) return;
8650       if (OVE->getSourceExpr())
8651         Visit(OVE->getSourceExpr());
8652     }
8653     void VisitBinaryOperator(BinaryOperator *BinOp) {
8654       if (!Variable || VarWillBeReased || BinOp->getOpcode() != BO_Assign)
8655         return;
8656       Expr *LHS = BinOp->getLHS();
8657       if (const DeclRefExpr *DRE = dyn_cast_or_null<DeclRefExpr>(LHS)) {
8658         if (DRE->getDecl() != Variable)
8659           return;
8660         if (Expr *RHS = BinOp->getRHS()) {
8661           RHS = RHS->IgnoreParenCasts();
8662           llvm::APSInt Value;
8663           VarWillBeReased =
8664             (RHS && RHS->isIntegerConstantExpr(Value, Context) && Value == 0);
8665         }
8666       }
8667     }
8668   };
8669 }
8670 
8671 /// Check whether the given argument is a block which captures a
8672 /// variable.
8673 static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) {
8674   assert(owner.Variable && owner.Loc.isValid());
8675 
8676   e = e->IgnoreParenCasts();
8677 
8678   // Look through [^{...} copy] and Block_copy(^{...}).
8679   if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(e)) {
8680     Selector Cmd = ME->getSelector();
8681     if (Cmd.isUnarySelector() && Cmd.getNameForSlot(0) == "copy") {
8682       e = ME->getInstanceReceiver();
8683       if (!e)
8684         return nullptr;
8685       e = e->IgnoreParenCasts();
8686     }
8687   } else if (CallExpr *CE = dyn_cast<CallExpr>(e)) {
8688     if (CE->getNumArgs() == 1) {
8689       FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(CE->getCalleeDecl());
8690       if (Fn) {
8691         const IdentifierInfo *FnI = Fn->getIdentifier();
8692         if (FnI && FnI->isStr("_Block_copy")) {
8693           e = CE->getArg(0)->IgnoreParenCasts();
8694         }
8695       }
8696     }
8697   }
8698 
8699   BlockExpr *block = dyn_cast<BlockExpr>(e);
8700   if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable))
8701     return nullptr;
8702 
8703   FindCaptureVisitor visitor(S.Context, owner.Variable);
8704   visitor.Visit(block->getBlockDecl()->getBody());
8705   return visitor.VarWillBeReased ? nullptr : visitor.Capturer;
8706 }
8707 
8708 static void diagnoseRetainCycle(Sema &S, Expr *capturer,
8709                                 RetainCycleOwner &owner) {
8710   assert(capturer);
8711   assert(owner.Variable && owner.Loc.isValid());
8712 
8713   S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle)
8714     << owner.Variable << capturer->getSourceRange();
8715   S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner)
8716     << owner.Indirect << owner.Range;
8717 }
8718 
8719 /// Check for a keyword selector that starts with the word 'add' or
8720 /// 'set'.
8721 static bool isSetterLikeSelector(Selector sel) {
8722   if (sel.isUnarySelector()) return false;
8723 
8724   StringRef str = sel.getNameForSlot(0);
8725   while (!str.empty() && str.front() == '_') str = str.substr(1);
8726   if (str.startswith("set"))
8727     str = str.substr(3);
8728   else if (str.startswith("add")) {
8729     // Specially whitelist 'addOperationWithBlock:'.
8730     if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock"))
8731       return false;
8732     str = str.substr(3);
8733   }
8734   else
8735     return false;
8736 
8737   if (str.empty()) return true;
8738   return !isLowercase(str.front());
8739 }
8740 
8741 static Optional<int> GetNSMutableArrayArgumentIndex(Sema &S,
8742                                                     ObjCMessageExpr *Message) {
8743   bool IsMutableArray = S.NSAPIObj->isSubclassOfNSClass(
8744                                                 Message->getReceiverInterface(),
8745                                                 NSAPI::ClassId_NSMutableArray);
8746   if (!IsMutableArray) {
8747     return None;
8748   }
8749 
8750   Selector Sel = Message->getSelector();
8751 
8752   Optional<NSAPI::NSArrayMethodKind> MKOpt =
8753     S.NSAPIObj->getNSArrayMethodKind(Sel);
8754   if (!MKOpt) {
8755     return None;
8756   }
8757 
8758   NSAPI::NSArrayMethodKind MK = *MKOpt;
8759 
8760   switch (MK) {
8761     case NSAPI::NSMutableArr_addObject:
8762     case NSAPI::NSMutableArr_insertObjectAtIndex:
8763     case NSAPI::NSMutableArr_setObjectAtIndexedSubscript:
8764       return 0;
8765     case NSAPI::NSMutableArr_replaceObjectAtIndex:
8766       return 1;
8767 
8768     default:
8769       return None;
8770   }
8771 
8772   return None;
8773 }
8774 
8775 static
8776 Optional<int> GetNSMutableDictionaryArgumentIndex(Sema &S,
8777                                                   ObjCMessageExpr *Message) {
8778   bool IsMutableDictionary = S.NSAPIObj->isSubclassOfNSClass(
8779                                             Message->getReceiverInterface(),
8780                                             NSAPI::ClassId_NSMutableDictionary);
8781   if (!IsMutableDictionary) {
8782     return None;
8783   }
8784 
8785   Selector Sel = Message->getSelector();
8786 
8787   Optional<NSAPI::NSDictionaryMethodKind> MKOpt =
8788     S.NSAPIObj->getNSDictionaryMethodKind(Sel);
8789   if (!MKOpt) {
8790     return None;
8791   }
8792 
8793   NSAPI::NSDictionaryMethodKind MK = *MKOpt;
8794 
8795   switch (MK) {
8796     case NSAPI::NSMutableDict_setObjectForKey:
8797     case NSAPI::NSMutableDict_setValueForKey:
8798     case NSAPI::NSMutableDict_setObjectForKeyedSubscript:
8799       return 0;
8800 
8801     default:
8802       return None;
8803   }
8804 
8805   return None;
8806 }
8807 
8808 static Optional<int> GetNSSetArgumentIndex(Sema &S, ObjCMessageExpr *Message) {
8809   bool IsMutableSet = S.NSAPIObj->isSubclassOfNSClass(
8810                                                 Message->getReceiverInterface(),
8811                                                 NSAPI::ClassId_NSMutableSet);
8812 
8813   bool IsMutableOrderedSet = S.NSAPIObj->isSubclassOfNSClass(
8814                                             Message->getReceiverInterface(),
8815                                             NSAPI::ClassId_NSMutableOrderedSet);
8816   if (!IsMutableSet && !IsMutableOrderedSet) {
8817     return None;
8818   }
8819 
8820   Selector Sel = Message->getSelector();
8821 
8822   Optional<NSAPI::NSSetMethodKind> MKOpt = S.NSAPIObj->getNSSetMethodKind(Sel);
8823   if (!MKOpt) {
8824     return None;
8825   }
8826 
8827   NSAPI::NSSetMethodKind MK = *MKOpt;
8828 
8829   switch (MK) {
8830     case NSAPI::NSMutableSet_addObject:
8831     case NSAPI::NSOrderedSet_setObjectAtIndex:
8832     case NSAPI::NSOrderedSet_setObjectAtIndexedSubscript:
8833     case NSAPI::NSOrderedSet_insertObjectAtIndex:
8834       return 0;
8835     case NSAPI::NSOrderedSet_replaceObjectAtIndexWithObject:
8836       return 1;
8837   }
8838 
8839   return None;
8840 }
8841 
8842 void Sema::CheckObjCCircularContainer(ObjCMessageExpr *Message) {
8843   if (!Message->isInstanceMessage()) {
8844     return;
8845   }
8846 
8847   Optional<int> ArgOpt;
8848 
8849   if (!(ArgOpt = GetNSMutableArrayArgumentIndex(*this, Message)) &&
8850       !(ArgOpt = GetNSMutableDictionaryArgumentIndex(*this, Message)) &&
8851       !(ArgOpt = GetNSSetArgumentIndex(*this, Message))) {
8852     return;
8853   }
8854 
8855   int ArgIndex = *ArgOpt;
8856 
8857   Expr *Arg = Message->getArg(ArgIndex)->IgnoreImpCasts();
8858   if (OpaqueValueExpr *OE = dyn_cast<OpaqueValueExpr>(Arg)) {
8859     Arg = OE->getSourceExpr()->IgnoreImpCasts();
8860   }
8861 
8862   if (Message->getReceiverKind() == ObjCMessageExpr::SuperInstance) {
8863     if (DeclRefExpr *ArgRE = dyn_cast<DeclRefExpr>(Arg)) {
8864       if (ArgRE->isObjCSelfExpr()) {
8865         Diag(Message->getSourceRange().getBegin(),
8866              diag::warn_objc_circular_container)
8867           << ArgRE->getDecl()->getName() << StringRef("super");
8868       }
8869     }
8870   } else {
8871     Expr *Receiver = Message->getInstanceReceiver()->IgnoreImpCasts();
8872 
8873     if (OpaqueValueExpr *OE = dyn_cast<OpaqueValueExpr>(Receiver)) {
8874       Receiver = OE->getSourceExpr()->IgnoreImpCasts();
8875     }
8876 
8877     if (DeclRefExpr *ReceiverRE = dyn_cast<DeclRefExpr>(Receiver)) {
8878       if (DeclRefExpr *ArgRE = dyn_cast<DeclRefExpr>(Arg)) {
8879         if (ReceiverRE->getDecl() == ArgRE->getDecl()) {
8880           ValueDecl *Decl = ReceiverRE->getDecl();
8881           Diag(Message->getSourceRange().getBegin(),
8882                diag::warn_objc_circular_container)
8883             << Decl->getName() << Decl->getName();
8884           if (!ArgRE->isObjCSelfExpr()) {
8885             Diag(Decl->getLocation(),
8886                  diag::note_objc_circular_container_declared_here)
8887               << Decl->getName();
8888           }
8889         }
8890       }
8891     } else if (ObjCIvarRefExpr *IvarRE = dyn_cast<ObjCIvarRefExpr>(Receiver)) {
8892       if (ObjCIvarRefExpr *IvarArgRE = dyn_cast<ObjCIvarRefExpr>(Arg)) {
8893         if (IvarRE->getDecl() == IvarArgRE->getDecl()) {
8894           ObjCIvarDecl *Decl = IvarRE->getDecl();
8895           Diag(Message->getSourceRange().getBegin(),
8896                diag::warn_objc_circular_container)
8897             << Decl->getName() << Decl->getName();
8898           Diag(Decl->getLocation(),
8899                diag::note_objc_circular_container_declared_here)
8900             << Decl->getName();
8901         }
8902       }
8903     }
8904   }
8905 
8906 }
8907 
8908 /// Check a message send to see if it's likely to cause a retain cycle.
8909 void Sema::checkRetainCycles(ObjCMessageExpr *msg) {
8910   // Only check instance methods whose selector looks like a setter.
8911   if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector()))
8912     return;
8913 
8914   // Try to find a variable that the receiver is strongly owned by.
8915   RetainCycleOwner owner;
8916   if (msg->getReceiverKind() == ObjCMessageExpr::Instance) {
8917     if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner))
8918       return;
8919   } else {
8920     assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance);
8921     owner.Variable = getCurMethodDecl()->getSelfDecl();
8922     owner.Loc = msg->getSuperLoc();
8923     owner.Range = msg->getSuperLoc();
8924   }
8925 
8926   // Check whether the receiver is captured by any of the arguments.
8927   for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i)
8928     if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner))
8929       return diagnoseRetainCycle(*this, capturer, owner);
8930 }
8931 
8932 /// Check a property assign to see if it's likely to cause a retain cycle.
8933 void Sema::checkRetainCycles(Expr *receiver, Expr *argument) {
8934   RetainCycleOwner owner;
8935   if (!findRetainCycleOwner(*this, receiver, owner))
8936     return;
8937 
8938   if (Expr *capturer = findCapturingExpr(*this, argument, owner))
8939     diagnoseRetainCycle(*this, capturer, owner);
8940 }
8941 
8942 void Sema::checkRetainCycles(VarDecl *Var, Expr *Init) {
8943   RetainCycleOwner Owner;
8944   if (!considerVariable(Var, /*DeclRefExpr=*/nullptr, Owner))
8945     return;
8946 
8947   // Because we don't have an expression for the variable, we have to set the
8948   // location explicitly here.
8949   Owner.Loc = Var->getLocation();
8950   Owner.Range = Var->getSourceRange();
8951 
8952   if (Expr *Capturer = findCapturingExpr(*this, Init, Owner))
8953     diagnoseRetainCycle(*this, Capturer, Owner);
8954 }
8955 
8956 static bool checkUnsafeAssignLiteral(Sema &S, SourceLocation Loc,
8957                                      Expr *RHS, bool isProperty) {
8958   // Check if RHS is an Objective-C object literal, which also can get
8959   // immediately zapped in a weak reference.  Note that we explicitly
8960   // allow ObjCStringLiterals, since those are designed to never really die.
8961   RHS = RHS->IgnoreParenImpCasts();
8962 
8963   // This enum needs to match with the 'select' in
8964   // warn_objc_arc_literal_assign (off-by-1).
8965   Sema::ObjCLiteralKind Kind = S.CheckLiteralKind(RHS);
8966   if (Kind == Sema::LK_String || Kind == Sema::LK_None)
8967     return false;
8968 
8969   S.Diag(Loc, diag::warn_arc_literal_assign)
8970     << (unsigned) Kind
8971     << (isProperty ? 0 : 1)
8972     << RHS->getSourceRange();
8973 
8974   return true;
8975 }
8976 
8977 static bool checkUnsafeAssignObject(Sema &S, SourceLocation Loc,
8978                                     Qualifiers::ObjCLifetime LT,
8979                                     Expr *RHS, bool isProperty) {
8980   // Strip off any implicit cast added to get to the one ARC-specific.
8981   while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
8982     if (cast->getCastKind() == CK_ARCConsumeObject) {
8983       S.Diag(Loc, diag::warn_arc_retained_assign)
8984         << (LT == Qualifiers::OCL_ExplicitNone)
8985         << (isProperty ? 0 : 1)
8986         << RHS->getSourceRange();
8987       return true;
8988     }
8989     RHS = cast->getSubExpr();
8990   }
8991 
8992   if (LT == Qualifiers::OCL_Weak &&
8993       checkUnsafeAssignLiteral(S, Loc, RHS, isProperty))
8994     return true;
8995 
8996   return false;
8997 }
8998 
8999 bool Sema::checkUnsafeAssigns(SourceLocation Loc,
9000                               QualType LHS, Expr *RHS) {
9001   Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime();
9002 
9003   if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone)
9004     return false;
9005 
9006   if (checkUnsafeAssignObject(*this, Loc, LT, RHS, false))
9007     return true;
9008 
9009   return false;
9010 }
9011 
9012 void Sema::checkUnsafeExprAssigns(SourceLocation Loc,
9013                               Expr *LHS, Expr *RHS) {
9014   QualType LHSType;
9015   // PropertyRef on LHS type need be directly obtained from
9016   // its declaration as it has a PseudoType.
9017   ObjCPropertyRefExpr *PRE
9018     = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens());
9019   if (PRE && !PRE->isImplicitProperty()) {
9020     const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
9021     if (PD)
9022       LHSType = PD->getType();
9023   }
9024 
9025   if (LHSType.isNull())
9026     LHSType = LHS->getType();
9027 
9028   Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime();
9029 
9030   if (LT == Qualifiers::OCL_Weak) {
9031     if (!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, Loc))
9032       getCurFunction()->markSafeWeakUse(LHS);
9033   }
9034 
9035   if (checkUnsafeAssigns(Loc, LHSType, RHS))
9036     return;
9037 
9038   // FIXME. Check for other life times.
9039   if (LT != Qualifiers::OCL_None)
9040     return;
9041 
9042   if (PRE) {
9043     if (PRE->isImplicitProperty())
9044       return;
9045     const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
9046     if (!PD)
9047       return;
9048 
9049     unsigned Attributes = PD->getPropertyAttributes();
9050     if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) {
9051       // when 'assign' attribute was not explicitly specified
9052       // by user, ignore it and rely on property type itself
9053       // for lifetime info.
9054       unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten();
9055       if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) &&
9056           LHSType->isObjCRetainableType())
9057         return;
9058 
9059       while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
9060         if (cast->getCastKind() == CK_ARCConsumeObject) {
9061           Diag(Loc, diag::warn_arc_retained_property_assign)
9062           << RHS->getSourceRange();
9063           return;
9064         }
9065         RHS = cast->getSubExpr();
9066       }
9067     }
9068     else if (Attributes & ObjCPropertyDecl::OBJC_PR_weak) {
9069       if (checkUnsafeAssignObject(*this, Loc, Qualifiers::OCL_Weak, RHS, true))
9070         return;
9071     }
9072   }
9073 }
9074 
9075 //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===//
9076 
9077 namespace {
9078 bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr,
9079                                  SourceLocation StmtLoc,
9080                                  const NullStmt *Body) {
9081   // Do not warn if the body is a macro that expands to nothing, e.g:
9082   //
9083   // #define CALL(x)
9084   // if (condition)
9085   //   CALL(0);
9086   //
9087   if (Body->hasLeadingEmptyMacro())
9088     return false;
9089 
9090   // Get line numbers of statement and body.
9091   bool StmtLineInvalid;
9092   unsigned StmtLine = SourceMgr.getPresumedLineNumber(StmtLoc,
9093                                                       &StmtLineInvalid);
9094   if (StmtLineInvalid)
9095     return false;
9096 
9097   bool BodyLineInvalid;
9098   unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(),
9099                                                       &BodyLineInvalid);
9100   if (BodyLineInvalid)
9101     return false;
9102 
9103   // Warn if null statement and body are on the same line.
9104   if (StmtLine != BodyLine)
9105     return false;
9106 
9107   return true;
9108 }
9109 } // Unnamed namespace
9110 
9111 void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc,
9112                                  const Stmt *Body,
9113                                  unsigned DiagID) {
9114   // Since this is a syntactic check, don't emit diagnostic for template
9115   // instantiations, this just adds noise.
9116   if (CurrentInstantiationScope)
9117     return;
9118 
9119   // The body should be a null statement.
9120   const NullStmt *NBody = dyn_cast<NullStmt>(Body);
9121   if (!NBody)
9122     return;
9123 
9124   // Do the usual checks.
9125   if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody))
9126     return;
9127 
9128   Diag(NBody->getSemiLoc(), DiagID);
9129   Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line);
9130 }
9131 
9132 void Sema::DiagnoseEmptyLoopBody(const Stmt *S,
9133                                  const Stmt *PossibleBody) {
9134   assert(!CurrentInstantiationScope); // Ensured by caller
9135 
9136   SourceLocation StmtLoc;
9137   const Stmt *Body;
9138   unsigned DiagID;
9139   if (const ForStmt *FS = dyn_cast<ForStmt>(S)) {
9140     StmtLoc = FS->getRParenLoc();
9141     Body = FS->getBody();
9142     DiagID = diag::warn_empty_for_body;
9143   } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) {
9144     StmtLoc = WS->getCond()->getSourceRange().getEnd();
9145     Body = WS->getBody();
9146     DiagID = diag::warn_empty_while_body;
9147   } else
9148     return; // Neither `for' nor `while'.
9149 
9150   // The body should be a null statement.
9151   const NullStmt *NBody = dyn_cast<NullStmt>(Body);
9152   if (!NBody)
9153     return;
9154 
9155   // Skip expensive checks if diagnostic is disabled.
9156   if (Diags.isIgnored(DiagID, NBody->getSemiLoc()))
9157     return;
9158 
9159   // Do the usual checks.
9160   if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody))
9161     return;
9162 
9163   // `for(...);' and `while(...);' are popular idioms, so in order to keep
9164   // noise level low, emit diagnostics only if for/while is followed by a
9165   // CompoundStmt, e.g.:
9166   //    for (int i = 0; i < n; i++);
9167   //    {
9168   //      a(i);
9169   //    }
9170   // or if for/while is followed by a statement with more indentation
9171   // than for/while itself:
9172   //    for (int i = 0; i < n; i++);
9173   //      a(i);
9174   bool ProbableTypo = isa<CompoundStmt>(PossibleBody);
9175   if (!ProbableTypo) {
9176     bool BodyColInvalid;
9177     unsigned BodyCol = SourceMgr.getPresumedColumnNumber(
9178                              PossibleBody->getLocStart(),
9179                              &BodyColInvalid);
9180     if (BodyColInvalid)
9181       return;
9182 
9183     bool StmtColInvalid;
9184     unsigned StmtCol = SourceMgr.getPresumedColumnNumber(
9185                              S->getLocStart(),
9186                              &StmtColInvalid);
9187     if (StmtColInvalid)
9188       return;
9189 
9190     if (BodyCol > StmtCol)
9191       ProbableTypo = true;
9192   }
9193 
9194   if (ProbableTypo) {
9195     Diag(NBody->getSemiLoc(), DiagID);
9196     Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line);
9197   }
9198 }
9199 
9200 //===--- CHECK: Warn on self move with std::move. -------------------------===//
9201 
9202 /// DiagnoseSelfMove - Emits a warning if a value is moved to itself.
9203 void Sema::DiagnoseSelfMove(const Expr *LHSExpr, const Expr *RHSExpr,
9204                              SourceLocation OpLoc) {
9205 
9206   if (Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, OpLoc))
9207     return;
9208 
9209   if (!ActiveTemplateInstantiations.empty())
9210     return;
9211 
9212   // Strip parens and casts away.
9213   LHSExpr = LHSExpr->IgnoreParenImpCasts();
9214   RHSExpr = RHSExpr->IgnoreParenImpCasts();
9215 
9216   // Check for a call expression
9217   const CallExpr *CE = dyn_cast<CallExpr>(RHSExpr);
9218   if (!CE || CE->getNumArgs() != 1)
9219     return;
9220 
9221   // Check for a call to std::move
9222   const FunctionDecl *FD = CE->getDirectCallee();
9223   if (!FD || !FD->isInStdNamespace() || !FD->getIdentifier() ||
9224       !FD->getIdentifier()->isStr("move"))
9225     return;
9226 
9227   // Get argument from std::move
9228   RHSExpr = CE->getArg(0);
9229 
9230   const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr);
9231   const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr);
9232 
9233   // Two DeclRefExpr's, check that the decls are the same.
9234   if (LHSDeclRef && RHSDeclRef) {
9235     if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl())
9236       return;
9237     if (LHSDeclRef->getDecl()->getCanonicalDecl() !=
9238         RHSDeclRef->getDecl()->getCanonicalDecl())
9239       return;
9240 
9241     Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType()
9242                                         << LHSExpr->getSourceRange()
9243                                         << RHSExpr->getSourceRange();
9244     return;
9245   }
9246 
9247   // Member variables require a different approach to check for self moves.
9248   // MemberExpr's are the same if every nested MemberExpr refers to the same
9249   // Decl and that the base Expr's are DeclRefExpr's with the same Decl or
9250   // the base Expr's are CXXThisExpr's.
9251   const Expr *LHSBase = LHSExpr;
9252   const Expr *RHSBase = RHSExpr;
9253   const MemberExpr *LHSME = dyn_cast<MemberExpr>(LHSExpr);
9254   const MemberExpr *RHSME = dyn_cast<MemberExpr>(RHSExpr);
9255   if (!LHSME || !RHSME)
9256     return;
9257 
9258   while (LHSME && RHSME) {
9259     if (LHSME->getMemberDecl()->getCanonicalDecl() !=
9260         RHSME->getMemberDecl()->getCanonicalDecl())
9261       return;
9262 
9263     LHSBase = LHSME->getBase();
9264     RHSBase = RHSME->getBase();
9265     LHSME = dyn_cast<MemberExpr>(LHSBase);
9266     RHSME = dyn_cast<MemberExpr>(RHSBase);
9267   }
9268 
9269   LHSDeclRef = dyn_cast<DeclRefExpr>(LHSBase);
9270   RHSDeclRef = dyn_cast<DeclRefExpr>(RHSBase);
9271   if (LHSDeclRef && RHSDeclRef) {
9272     if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl())
9273       return;
9274     if (LHSDeclRef->getDecl()->getCanonicalDecl() !=
9275         RHSDeclRef->getDecl()->getCanonicalDecl())
9276       return;
9277 
9278     Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType()
9279                                         << LHSExpr->getSourceRange()
9280                                         << RHSExpr->getSourceRange();
9281     return;
9282   }
9283 
9284   if (isa<CXXThisExpr>(LHSBase) && isa<CXXThisExpr>(RHSBase))
9285     Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType()
9286                                         << LHSExpr->getSourceRange()
9287                                         << RHSExpr->getSourceRange();
9288 }
9289 
9290 //===--- Layout compatibility ----------------------------------------------//
9291 
9292 namespace {
9293 
9294 bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2);
9295 
9296 /// \brief Check if two enumeration types are layout-compatible.
9297 bool isLayoutCompatible(ASTContext &C, EnumDecl *ED1, EnumDecl *ED2) {
9298   // C++11 [dcl.enum] p8:
9299   // Two enumeration types are layout-compatible if they have the same
9300   // underlying type.
9301   return ED1->isComplete() && ED2->isComplete() &&
9302          C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType());
9303 }
9304 
9305 /// \brief Check if two fields are layout-compatible.
9306 bool isLayoutCompatible(ASTContext &C, FieldDecl *Field1, FieldDecl *Field2) {
9307   if (!isLayoutCompatible(C, Field1->getType(), Field2->getType()))
9308     return false;
9309 
9310   if (Field1->isBitField() != Field2->isBitField())
9311     return false;
9312 
9313   if (Field1->isBitField()) {
9314     // Make sure that the bit-fields are the same length.
9315     unsigned Bits1 = Field1->getBitWidthValue(C);
9316     unsigned Bits2 = Field2->getBitWidthValue(C);
9317 
9318     if (Bits1 != Bits2)
9319       return false;
9320   }
9321 
9322   return true;
9323 }
9324 
9325 /// \brief Check if two standard-layout structs are layout-compatible.
9326 /// (C++11 [class.mem] p17)
9327 bool isLayoutCompatibleStruct(ASTContext &C,
9328                               RecordDecl *RD1,
9329                               RecordDecl *RD2) {
9330   // If both records are C++ classes, check that base classes match.
9331   if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1)) {
9332     // If one of records is a CXXRecordDecl we are in C++ mode,
9333     // thus the other one is a CXXRecordDecl, too.
9334     const CXXRecordDecl *D2CXX = cast<CXXRecordDecl>(RD2);
9335     // Check number of base classes.
9336     if (D1CXX->getNumBases() != D2CXX->getNumBases())
9337       return false;
9338 
9339     // Check the base classes.
9340     for (CXXRecordDecl::base_class_const_iterator
9341                Base1 = D1CXX->bases_begin(),
9342            BaseEnd1 = D1CXX->bases_end(),
9343               Base2 = D2CXX->bases_begin();
9344          Base1 != BaseEnd1;
9345          ++Base1, ++Base2) {
9346       if (!isLayoutCompatible(C, Base1->getType(), Base2->getType()))
9347         return false;
9348     }
9349   } else if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2)) {
9350     // If only RD2 is a C++ class, it should have zero base classes.
9351     if (D2CXX->getNumBases() > 0)
9352       return false;
9353   }
9354 
9355   // Check the fields.
9356   RecordDecl::field_iterator Field2 = RD2->field_begin(),
9357                              Field2End = RD2->field_end(),
9358                              Field1 = RD1->field_begin(),
9359                              Field1End = RD1->field_end();
9360   for ( ; Field1 != Field1End && Field2 != Field2End; ++Field1, ++Field2) {
9361     if (!isLayoutCompatible(C, *Field1, *Field2))
9362       return false;
9363   }
9364   if (Field1 != Field1End || Field2 != Field2End)
9365     return false;
9366 
9367   return true;
9368 }
9369 
9370 /// \brief Check if two standard-layout unions are layout-compatible.
9371 /// (C++11 [class.mem] p18)
9372 bool isLayoutCompatibleUnion(ASTContext &C,
9373                              RecordDecl *RD1,
9374                              RecordDecl *RD2) {
9375   llvm::SmallPtrSet<FieldDecl *, 8> UnmatchedFields;
9376   for (auto *Field2 : RD2->fields())
9377     UnmatchedFields.insert(Field2);
9378 
9379   for (auto *Field1 : RD1->fields()) {
9380     llvm::SmallPtrSet<FieldDecl *, 8>::iterator
9381         I = UnmatchedFields.begin(),
9382         E = UnmatchedFields.end();
9383 
9384     for ( ; I != E; ++I) {
9385       if (isLayoutCompatible(C, Field1, *I)) {
9386         bool Result = UnmatchedFields.erase(*I);
9387         (void) Result;
9388         assert(Result);
9389         break;
9390       }
9391     }
9392     if (I == E)
9393       return false;
9394   }
9395 
9396   return UnmatchedFields.empty();
9397 }
9398 
9399 bool isLayoutCompatible(ASTContext &C, RecordDecl *RD1, RecordDecl *RD2) {
9400   if (RD1->isUnion() != RD2->isUnion())
9401     return false;
9402 
9403   if (RD1->isUnion())
9404     return isLayoutCompatibleUnion(C, RD1, RD2);
9405   else
9406     return isLayoutCompatibleStruct(C, RD1, RD2);
9407 }
9408 
9409 /// \brief Check if two types are layout-compatible in C++11 sense.
9410 bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2) {
9411   if (T1.isNull() || T2.isNull())
9412     return false;
9413 
9414   // C++11 [basic.types] p11:
9415   // If two types T1 and T2 are the same type, then T1 and T2 are
9416   // layout-compatible types.
9417   if (C.hasSameType(T1, T2))
9418     return true;
9419 
9420   T1 = T1.getCanonicalType().getUnqualifiedType();
9421   T2 = T2.getCanonicalType().getUnqualifiedType();
9422 
9423   const Type::TypeClass TC1 = T1->getTypeClass();
9424   const Type::TypeClass TC2 = T2->getTypeClass();
9425 
9426   if (TC1 != TC2)
9427     return false;
9428 
9429   if (TC1 == Type::Enum) {
9430     return isLayoutCompatible(C,
9431                               cast<EnumType>(T1)->getDecl(),
9432                               cast<EnumType>(T2)->getDecl());
9433   } else if (TC1 == Type::Record) {
9434     if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType())
9435       return false;
9436 
9437     return isLayoutCompatible(C,
9438                               cast<RecordType>(T1)->getDecl(),
9439                               cast<RecordType>(T2)->getDecl());
9440   }
9441 
9442   return false;
9443 }
9444 }
9445 
9446 //===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----//
9447 
9448 namespace {
9449 /// \brief Given a type tag expression find the type tag itself.
9450 ///
9451 /// \param TypeExpr Type tag expression, as it appears in user's code.
9452 ///
9453 /// \param VD Declaration of an identifier that appears in a type tag.
9454 ///
9455 /// \param MagicValue Type tag magic value.
9456 bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx,
9457                      const ValueDecl **VD, uint64_t *MagicValue) {
9458   while(true) {
9459     if (!TypeExpr)
9460       return false;
9461 
9462     TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts();
9463 
9464     switch (TypeExpr->getStmtClass()) {
9465     case Stmt::UnaryOperatorClass: {
9466       const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr);
9467       if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) {
9468         TypeExpr = UO->getSubExpr();
9469         continue;
9470       }
9471       return false;
9472     }
9473 
9474     case Stmt::DeclRefExprClass: {
9475       const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr);
9476       *VD = DRE->getDecl();
9477       return true;
9478     }
9479 
9480     case Stmt::IntegerLiteralClass: {
9481       const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr);
9482       llvm::APInt MagicValueAPInt = IL->getValue();
9483       if (MagicValueAPInt.getActiveBits() <= 64) {
9484         *MagicValue = MagicValueAPInt.getZExtValue();
9485         return true;
9486       } else
9487         return false;
9488     }
9489 
9490     case Stmt::BinaryConditionalOperatorClass:
9491     case Stmt::ConditionalOperatorClass: {
9492       const AbstractConditionalOperator *ACO =
9493           cast<AbstractConditionalOperator>(TypeExpr);
9494       bool Result;
9495       if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx)) {
9496         if (Result)
9497           TypeExpr = ACO->getTrueExpr();
9498         else
9499           TypeExpr = ACO->getFalseExpr();
9500         continue;
9501       }
9502       return false;
9503     }
9504 
9505     case Stmt::BinaryOperatorClass: {
9506       const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr);
9507       if (BO->getOpcode() == BO_Comma) {
9508         TypeExpr = BO->getRHS();
9509         continue;
9510       }
9511       return false;
9512     }
9513 
9514     default:
9515       return false;
9516     }
9517   }
9518 }
9519 
9520 /// \brief Retrieve the C type corresponding to type tag TypeExpr.
9521 ///
9522 /// \param TypeExpr Expression that specifies a type tag.
9523 ///
9524 /// \param MagicValues Registered magic values.
9525 ///
9526 /// \param FoundWrongKind Set to true if a type tag was found, but of a wrong
9527 ///        kind.
9528 ///
9529 /// \param TypeInfo Information about the corresponding C type.
9530 ///
9531 /// \returns true if the corresponding C type was found.
9532 bool GetMatchingCType(
9533         const IdentifierInfo *ArgumentKind,
9534         const Expr *TypeExpr, const ASTContext &Ctx,
9535         const llvm::DenseMap<Sema::TypeTagMagicValue,
9536                              Sema::TypeTagData> *MagicValues,
9537         bool &FoundWrongKind,
9538         Sema::TypeTagData &TypeInfo) {
9539   FoundWrongKind = false;
9540 
9541   // Variable declaration that has type_tag_for_datatype attribute.
9542   const ValueDecl *VD = nullptr;
9543 
9544   uint64_t MagicValue;
9545 
9546   if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue))
9547     return false;
9548 
9549   if (VD) {
9550     if (TypeTagForDatatypeAttr *I = VD->getAttr<TypeTagForDatatypeAttr>()) {
9551       if (I->getArgumentKind() != ArgumentKind) {
9552         FoundWrongKind = true;
9553         return false;
9554       }
9555       TypeInfo.Type = I->getMatchingCType();
9556       TypeInfo.LayoutCompatible = I->getLayoutCompatible();
9557       TypeInfo.MustBeNull = I->getMustBeNull();
9558       return true;
9559     }
9560     return false;
9561   }
9562 
9563   if (!MagicValues)
9564     return false;
9565 
9566   llvm::DenseMap<Sema::TypeTagMagicValue,
9567                  Sema::TypeTagData>::const_iterator I =
9568       MagicValues->find(std::make_pair(ArgumentKind, MagicValue));
9569   if (I == MagicValues->end())
9570     return false;
9571 
9572   TypeInfo = I->second;
9573   return true;
9574 }
9575 } // unnamed namespace
9576 
9577 void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind,
9578                                       uint64_t MagicValue, QualType Type,
9579                                       bool LayoutCompatible,
9580                                       bool MustBeNull) {
9581   if (!TypeTagForDatatypeMagicValues)
9582     TypeTagForDatatypeMagicValues.reset(
9583         new llvm::DenseMap<TypeTagMagicValue, TypeTagData>);
9584 
9585   TypeTagMagicValue Magic(ArgumentKind, MagicValue);
9586   (*TypeTagForDatatypeMagicValues)[Magic] =
9587       TypeTagData(Type, LayoutCompatible, MustBeNull);
9588 }
9589 
9590 namespace {
9591 bool IsSameCharType(QualType T1, QualType T2) {
9592   const BuiltinType *BT1 = T1->getAs<BuiltinType>();
9593   if (!BT1)
9594     return false;
9595 
9596   const BuiltinType *BT2 = T2->getAs<BuiltinType>();
9597   if (!BT2)
9598     return false;
9599 
9600   BuiltinType::Kind T1Kind = BT1->getKind();
9601   BuiltinType::Kind T2Kind = BT2->getKind();
9602 
9603   return (T1Kind == BuiltinType::SChar  && T2Kind == BuiltinType::Char_S) ||
9604          (T1Kind == BuiltinType::UChar  && T2Kind == BuiltinType::Char_U) ||
9605          (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) ||
9606          (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar);
9607 }
9608 } // unnamed namespace
9609 
9610 void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr,
9611                                     const Expr * const *ExprArgs) {
9612   const IdentifierInfo *ArgumentKind = Attr->getArgumentKind();
9613   bool IsPointerAttr = Attr->getIsPointer();
9614 
9615   const Expr *TypeTagExpr = ExprArgs[Attr->getTypeTagIdx()];
9616   bool FoundWrongKind;
9617   TypeTagData TypeInfo;
9618   if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context,
9619                         TypeTagForDatatypeMagicValues.get(),
9620                         FoundWrongKind, TypeInfo)) {
9621     if (FoundWrongKind)
9622       Diag(TypeTagExpr->getExprLoc(),
9623            diag::warn_type_tag_for_datatype_wrong_kind)
9624         << TypeTagExpr->getSourceRange();
9625     return;
9626   }
9627 
9628   const Expr *ArgumentExpr = ExprArgs[Attr->getArgumentIdx()];
9629   if (IsPointerAttr) {
9630     // Skip implicit cast of pointer to `void *' (as a function argument).
9631     if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr))
9632       if (ICE->getType()->isVoidPointerType() &&
9633           ICE->getCastKind() == CK_BitCast)
9634         ArgumentExpr = ICE->getSubExpr();
9635   }
9636   QualType ArgumentType = ArgumentExpr->getType();
9637 
9638   // Passing a `void*' pointer shouldn't trigger a warning.
9639   if (IsPointerAttr && ArgumentType->isVoidPointerType())
9640     return;
9641 
9642   if (TypeInfo.MustBeNull) {
9643     // Type tag with matching void type requires a null pointer.
9644     if (!ArgumentExpr->isNullPointerConstant(Context,
9645                                              Expr::NPC_ValueDependentIsNotNull)) {
9646       Diag(ArgumentExpr->getExprLoc(),
9647            diag::warn_type_safety_null_pointer_required)
9648           << ArgumentKind->getName()
9649           << ArgumentExpr->getSourceRange()
9650           << TypeTagExpr->getSourceRange();
9651     }
9652     return;
9653   }
9654 
9655   QualType RequiredType = TypeInfo.Type;
9656   if (IsPointerAttr)
9657     RequiredType = Context.getPointerType(RequiredType);
9658 
9659   bool mismatch = false;
9660   if (!TypeInfo.LayoutCompatible) {
9661     mismatch = !Context.hasSameType(ArgumentType, RequiredType);
9662 
9663     // C++11 [basic.fundamental] p1:
9664     // Plain char, signed char, and unsigned char are three distinct types.
9665     //
9666     // But we treat plain `char' as equivalent to `signed char' or `unsigned
9667     // char' depending on the current char signedness mode.
9668     if (mismatch)
9669       if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(),
9670                                            RequiredType->getPointeeType())) ||
9671           (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType)))
9672         mismatch = false;
9673   } else
9674     if (IsPointerAttr)
9675       mismatch = !isLayoutCompatible(Context,
9676                                      ArgumentType->getPointeeType(),
9677                                      RequiredType->getPointeeType());
9678     else
9679       mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType);
9680 
9681   if (mismatch)
9682     Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch)
9683         << ArgumentType << ArgumentKind
9684         << TypeInfo.LayoutCompatible << RequiredType
9685         << ArgumentExpr->getSourceRange()
9686         << TypeTagExpr->getSourceRange();
9687 }
9688 
9689