]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/lib/Sema/SemaChecking.cpp
MFC r234353:
[FreeBSD/stable/9.git] / contrib / llvm / tools / clang / lib / Sema / SemaChecking.cpp
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/Initialization.h"
16 #include "clang/Sema/Sema.h"
17 #include "clang/Sema/SemaInternal.h"
18 #include "clang/Sema/Initialization.h"
19 #include "clang/Sema/ScopeInfo.h"
20 #include "clang/Analysis/Analyses/FormatString.h"
21 #include "clang/AST/ASTContext.h"
22 #include "clang/AST/CharUnits.h"
23 #include "clang/AST/DeclCXX.h"
24 #include "clang/AST/DeclObjC.h"
25 #include "clang/AST/ExprCXX.h"
26 #include "clang/AST/ExprObjC.h"
27 #include "clang/AST/EvaluatedExprVisitor.h"
28 #include "clang/AST/DeclObjC.h"
29 #include "clang/AST/StmtCXX.h"
30 #include "clang/AST/StmtObjC.h"
31 #include "clang/Lex/Preprocessor.h"
32 #include "llvm/ADT/BitVector.h"
33 #include "llvm/ADT/SmallString.h"
34 #include "llvm/ADT/STLExtras.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "clang/Basic/TargetBuiltins.h"
37 #include "clang/Basic/TargetInfo.h"
38 #include "clang/Basic/ConvertUTF.h"
39 #include <limits>
40 using namespace clang;
41 using namespace sema;
42
43 SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL,
44                                                     unsigned ByteNo) const {
45   return SL->getLocationOfByte(ByteNo, PP.getSourceManager(),
46                                PP.getLangOpts(), PP.getTargetInfo());
47 }
48
49 /// Checks that a call expression's argument count is the desired number.
50 /// This is useful when doing custom type-checking.  Returns true on error.
51 static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) {
52   unsigned argCount = call->getNumArgs();
53   if (argCount == desiredArgCount) return false;
54
55   if (argCount < desiredArgCount)
56     return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args)
57         << 0 /*function call*/ << desiredArgCount << argCount
58         << call->getSourceRange();
59
60   // Highlight all the excess arguments.
61   SourceRange range(call->getArg(desiredArgCount)->getLocStart(),
62                     call->getArg(argCount - 1)->getLocEnd());
63     
64   return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args)
65     << 0 /*function call*/ << desiredArgCount << argCount
66     << call->getArg(1)->getSourceRange();
67 }
68
69 /// CheckBuiltinAnnotationString - Checks that string argument to the builtin
70 /// annotation is a non wide string literal.
71 static bool CheckBuiltinAnnotationString(Sema &S, Expr *Arg) {
72   Arg = Arg->IgnoreParenCasts();
73   StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
74   if (!Literal || !Literal->isAscii()) {
75     S.Diag(Arg->getLocStart(), diag::err_builtin_annotation_not_string_constant)
76       << Arg->getSourceRange();
77     return true;
78   }
79   return false;
80 }
81
82 ExprResult
83 Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
84   ExprResult TheCallResult(Owned(TheCall));
85
86   // Find out if any arguments are required to be integer constant expressions.
87   unsigned ICEArguments = 0;
88   ASTContext::GetBuiltinTypeError Error;
89   Context.GetBuiltinType(BuiltinID, Error, &ICEArguments);
90   if (Error != ASTContext::GE_None)
91     ICEArguments = 0;  // Don't diagnose previously diagnosed errors.
92   
93   // If any arguments are required to be ICE's, check and diagnose.
94   for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) {
95     // Skip arguments not required to be ICE's.
96     if ((ICEArguments & (1 << ArgNo)) == 0) continue;
97     
98     llvm::APSInt Result;
99     if (SemaBuiltinConstantArg(TheCall, ArgNo, Result))
100       return true;
101     ICEArguments &= ~(1 << ArgNo);
102   }
103   
104   switch (BuiltinID) {
105   case Builtin::BI__builtin___CFStringMakeConstantString:
106     assert(TheCall->getNumArgs() == 1 &&
107            "Wrong # arguments to builtin CFStringMakeConstantString");
108     if (CheckObjCString(TheCall->getArg(0)))
109       return ExprError();
110     break;
111   case Builtin::BI__builtin_stdarg_start:
112   case Builtin::BI__builtin_va_start:
113     if (SemaBuiltinVAStart(TheCall))
114       return ExprError();
115     break;
116   case Builtin::BI__builtin_isgreater:
117   case Builtin::BI__builtin_isgreaterequal:
118   case Builtin::BI__builtin_isless:
119   case Builtin::BI__builtin_islessequal:
120   case Builtin::BI__builtin_islessgreater:
121   case Builtin::BI__builtin_isunordered:
122     if (SemaBuiltinUnorderedCompare(TheCall))
123       return ExprError();
124     break;
125   case Builtin::BI__builtin_fpclassify:
126     if (SemaBuiltinFPClassification(TheCall, 6))
127       return ExprError();
128     break;
129   case Builtin::BI__builtin_isfinite:
130   case Builtin::BI__builtin_isinf:
131   case Builtin::BI__builtin_isinf_sign:
132   case Builtin::BI__builtin_isnan:
133   case Builtin::BI__builtin_isnormal:
134     if (SemaBuiltinFPClassification(TheCall, 1))
135       return ExprError();
136     break;
137   case Builtin::BI__builtin_shufflevector:
138     return SemaBuiltinShuffleVector(TheCall);
139     // TheCall will be freed by the smart pointer here, but that's fine, since
140     // SemaBuiltinShuffleVector guts it, but then doesn't release it.
141   case Builtin::BI__builtin_prefetch:
142     if (SemaBuiltinPrefetch(TheCall))
143       return ExprError();
144     break;
145   case Builtin::BI__builtin_object_size:
146     if (SemaBuiltinObjectSize(TheCall))
147       return ExprError();
148     break;
149   case Builtin::BI__builtin_longjmp:
150     if (SemaBuiltinLongjmp(TheCall))
151       return ExprError();
152     break;
153
154   case Builtin::BI__builtin_classify_type:
155     if (checkArgCount(*this, TheCall, 1)) return true;
156     TheCall->setType(Context.IntTy);
157     break;
158   case Builtin::BI__builtin_constant_p:
159     if (checkArgCount(*this, TheCall, 1)) return true;
160     TheCall->setType(Context.IntTy);
161     break;
162   case Builtin::BI__sync_fetch_and_add:
163   case Builtin::BI__sync_fetch_and_add_1:
164   case Builtin::BI__sync_fetch_and_add_2:
165   case Builtin::BI__sync_fetch_and_add_4:
166   case Builtin::BI__sync_fetch_and_add_8:
167   case Builtin::BI__sync_fetch_and_add_16:
168   case Builtin::BI__sync_fetch_and_sub:
169   case Builtin::BI__sync_fetch_and_sub_1:
170   case Builtin::BI__sync_fetch_and_sub_2:
171   case Builtin::BI__sync_fetch_and_sub_4:
172   case Builtin::BI__sync_fetch_and_sub_8:
173   case Builtin::BI__sync_fetch_and_sub_16:
174   case Builtin::BI__sync_fetch_and_or:
175   case Builtin::BI__sync_fetch_and_or_1:
176   case Builtin::BI__sync_fetch_and_or_2:
177   case Builtin::BI__sync_fetch_and_or_4:
178   case Builtin::BI__sync_fetch_and_or_8:
179   case Builtin::BI__sync_fetch_and_or_16:
180   case Builtin::BI__sync_fetch_and_and:
181   case Builtin::BI__sync_fetch_and_and_1:
182   case Builtin::BI__sync_fetch_and_and_2:
183   case Builtin::BI__sync_fetch_and_and_4:
184   case Builtin::BI__sync_fetch_and_and_8:
185   case Builtin::BI__sync_fetch_and_and_16:
186   case Builtin::BI__sync_fetch_and_xor:
187   case Builtin::BI__sync_fetch_and_xor_1:
188   case Builtin::BI__sync_fetch_and_xor_2:
189   case Builtin::BI__sync_fetch_and_xor_4:
190   case Builtin::BI__sync_fetch_and_xor_8:
191   case Builtin::BI__sync_fetch_and_xor_16:
192   case Builtin::BI__sync_add_and_fetch:
193   case Builtin::BI__sync_add_and_fetch_1:
194   case Builtin::BI__sync_add_and_fetch_2:
195   case Builtin::BI__sync_add_and_fetch_4:
196   case Builtin::BI__sync_add_and_fetch_8:
197   case Builtin::BI__sync_add_and_fetch_16:
198   case Builtin::BI__sync_sub_and_fetch:
199   case Builtin::BI__sync_sub_and_fetch_1:
200   case Builtin::BI__sync_sub_and_fetch_2:
201   case Builtin::BI__sync_sub_and_fetch_4:
202   case Builtin::BI__sync_sub_and_fetch_8:
203   case Builtin::BI__sync_sub_and_fetch_16:
204   case Builtin::BI__sync_and_and_fetch:
205   case Builtin::BI__sync_and_and_fetch_1:
206   case Builtin::BI__sync_and_and_fetch_2:
207   case Builtin::BI__sync_and_and_fetch_4:
208   case Builtin::BI__sync_and_and_fetch_8:
209   case Builtin::BI__sync_and_and_fetch_16:
210   case Builtin::BI__sync_or_and_fetch:
211   case Builtin::BI__sync_or_and_fetch_1:
212   case Builtin::BI__sync_or_and_fetch_2:
213   case Builtin::BI__sync_or_and_fetch_4:
214   case Builtin::BI__sync_or_and_fetch_8:
215   case Builtin::BI__sync_or_and_fetch_16:
216   case Builtin::BI__sync_xor_and_fetch:
217   case Builtin::BI__sync_xor_and_fetch_1:
218   case Builtin::BI__sync_xor_and_fetch_2:
219   case Builtin::BI__sync_xor_and_fetch_4:
220   case Builtin::BI__sync_xor_and_fetch_8:
221   case Builtin::BI__sync_xor_and_fetch_16:
222   case Builtin::BI__sync_val_compare_and_swap:
223   case Builtin::BI__sync_val_compare_and_swap_1:
224   case Builtin::BI__sync_val_compare_and_swap_2:
225   case Builtin::BI__sync_val_compare_and_swap_4:
226   case Builtin::BI__sync_val_compare_and_swap_8:
227   case Builtin::BI__sync_val_compare_and_swap_16:
228   case Builtin::BI__sync_bool_compare_and_swap:
229   case Builtin::BI__sync_bool_compare_and_swap_1:
230   case Builtin::BI__sync_bool_compare_and_swap_2:
231   case Builtin::BI__sync_bool_compare_and_swap_4:
232   case Builtin::BI__sync_bool_compare_and_swap_8:
233   case Builtin::BI__sync_bool_compare_and_swap_16:
234   case Builtin::BI__sync_lock_test_and_set:
235   case Builtin::BI__sync_lock_test_and_set_1:
236   case Builtin::BI__sync_lock_test_and_set_2:
237   case Builtin::BI__sync_lock_test_and_set_4:
238   case Builtin::BI__sync_lock_test_and_set_8:
239   case Builtin::BI__sync_lock_test_and_set_16:
240   case Builtin::BI__sync_lock_release:
241   case Builtin::BI__sync_lock_release_1:
242   case Builtin::BI__sync_lock_release_2:
243   case Builtin::BI__sync_lock_release_4:
244   case Builtin::BI__sync_lock_release_8:
245   case Builtin::BI__sync_lock_release_16:
246   case Builtin::BI__sync_swap:
247   case Builtin::BI__sync_swap_1:
248   case Builtin::BI__sync_swap_2:
249   case Builtin::BI__sync_swap_4:
250   case Builtin::BI__sync_swap_8:
251   case Builtin::BI__sync_swap_16:
252     return SemaBuiltinAtomicOverloaded(move(TheCallResult));
253 #define BUILTIN(ID, TYPE, ATTRS)
254 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
255   case Builtin::BI##ID: \
256     return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::AO##ID);
257 #include "clang/Basic/Builtins.def"
258   case Builtin::BI__builtin_annotation:
259     if (CheckBuiltinAnnotationString(*this, TheCall->getArg(1)))
260       return ExprError();
261     break;
262   }
263   
264   // Since the target specific builtins for each arch overlap, only check those
265   // of the arch we are compiling for.
266   if (BuiltinID >= Builtin::FirstTSBuiltin) {
267     switch (Context.getTargetInfo().getTriple().getArch()) {
268       case llvm::Triple::arm:
269       case llvm::Triple::thumb:
270         if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall))
271           return ExprError();
272         break;
273       default:
274         break;
275     }
276   }
277
278   return move(TheCallResult);
279 }
280
281 // Get the valid immediate range for the specified NEON type code.
282 static unsigned RFT(unsigned t, bool shift = false) {
283   NeonTypeFlags Type(t);
284   int IsQuad = Type.isQuad();
285   switch (Type.getEltType()) {
286   case NeonTypeFlags::Int8:
287   case NeonTypeFlags::Poly8:
288     return shift ? 7 : (8 << IsQuad) - 1;
289   case NeonTypeFlags::Int16:
290   case NeonTypeFlags::Poly16:
291     return shift ? 15 : (4 << IsQuad) - 1;
292   case NeonTypeFlags::Int32:
293     return shift ? 31 : (2 << IsQuad) - 1;
294   case NeonTypeFlags::Int64:
295     return shift ? 63 : (1 << IsQuad) - 1;
296   case NeonTypeFlags::Float16:
297     assert(!shift && "cannot shift float types!");
298     return (4 << IsQuad) - 1;
299   case NeonTypeFlags::Float32:
300     assert(!shift && "cannot shift float types!");
301     return (2 << IsQuad) - 1;
302   }
303   llvm_unreachable("Invalid NeonTypeFlag!");
304 }
305
306 /// getNeonEltType - Return the QualType corresponding to the elements of
307 /// the vector type specified by the NeonTypeFlags.  This is used to check
308 /// the pointer arguments for Neon load/store intrinsics.
309 static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context) {
310   switch (Flags.getEltType()) {
311   case NeonTypeFlags::Int8:
312     return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy;
313   case NeonTypeFlags::Int16:
314     return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy;
315   case NeonTypeFlags::Int32:
316     return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy;
317   case NeonTypeFlags::Int64:
318     return Flags.isUnsigned() ? Context.UnsignedLongLongTy : Context.LongLongTy;
319   case NeonTypeFlags::Poly8:
320     return Context.SignedCharTy;
321   case NeonTypeFlags::Poly16:
322     return Context.ShortTy;
323   case NeonTypeFlags::Float16:
324     return Context.UnsignedShortTy;
325   case NeonTypeFlags::Float32:
326     return Context.FloatTy;
327   }
328   llvm_unreachable("Invalid NeonTypeFlag!");
329 }
330
331 bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
332   llvm::APSInt Result;
333
334   unsigned mask = 0;
335   unsigned TV = 0;
336   int PtrArgNum = -1;
337   bool HasConstPtr = false;
338   switch (BuiltinID) {
339 #define GET_NEON_OVERLOAD_CHECK
340 #include "clang/Basic/arm_neon.inc"
341 #undef GET_NEON_OVERLOAD_CHECK
342   }
343   
344   // For NEON intrinsics which are overloaded on vector element type, validate
345   // the immediate which specifies which variant to emit.
346   unsigned ImmArg = TheCall->getNumArgs()-1;
347   if (mask) {
348     if (SemaBuiltinConstantArg(TheCall, ImmArg, Result))
349       return true;
350     
351     TV = Result.getLimitedValue(64);
352     if ((TV > 63) || (mask & (1 << TV)) == 0)
353       return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code)
354         << TheCall->getArg(ImmArg)->getSourceRange();
355   }
356
357   if (PtrArgNum >= 0) {
358     // Check that pointer arguments have the specified type.
359     Expr *Arg = TheCall->getArg(PtrArgNum);
360     if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg))
361       Arg = ICE->getSubExpr();
362     ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg);
363     QualType RHSTy = RHS.get()->getType();
364     QualType EltTy = getNeonEltType(NeonTypeFlags(TV), Context);
365     if (HasConstPtr)
366       EltTy = EltTy.withConst();
367     QualType LHSTy = Context.getPointerType(EltTy);
368     AssignConvertType ConvTy;
369     ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS);
370     if (RHS.isInvalid())
371       return true;
372     if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), LHSTy, RHSTy,
373                                  RHS.get(), AA_Assigning))
374       return true;
375   }
376   
377   // For NEON intrinsics which take an immediate value as part of the 
378   // instruction, range check them here.
379   unsigned i = 0, l = 0, u = 0;
380   switch (BuiltinID) {
381   default: return false;
382   case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break;
383   case ARM::BI__builtin_arm_usat: i = 1; u = 31; break;
384   case ARM::BI__builtin_arm_vcvtr_f:
385   case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break;
386 #define GET_NEON_IMMEDIATE_CHECK
387 #include "clang/Basic/arm_neon.inc"
388 #undef GET_NEON_IMMEDIATE_CHECK
389   };
390
391   // Check that the immediate argument is actually a constant.
392   if (SemaBuiltinConstantArg(TheCall, i, Result))
393     return true;
394
395   // Range check against the upper/lower values for this isntruction.
396   unsigned Val = Result.getZExtValue();
397   if (Val < l || Val > (u + l))
398     return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
399       << l << u+l << TheCall->getArg(i)->getSourceRange();
400
401   // FIXME: VFP Intrinsics should error if VFP not present.
402   return false;
403 }
404
405 /// CheckFunctionCall - Check a direct function call for various correctness
406 /// and safety properties not strictly enforced by the C type system.
407 bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall) {
408   // Get the IdentifierInfo* for the called function.
409   IdentifierInfo *FnInfo = FDecl->getIdentifier();
410
411   // None of the checks below are needed for functions that don't have
412   // simple names (e.g., C++ conversion functions).
413   if (!FnInfo)
414     return false;
415
416   // FIXME: This mechanism should be abstracted to be less fragile and
417   // more efficient. For example, just map function ids to custom
418   // handlers.
419
420   // Printf and scanf checking.
421   for (specific_attr_iterator<FormatAttr>
422          i = FDecl->specific_attr_begin<FormatAttr>(),
423          e = FDecl->specific_attr_end<FormatAttr>(); i != e ; ++i) {
424     CheckFormatArguments(*i, TheCall);
425   }
426
427   for (specific_attr_iterator<NonNullAttr>
428          i = FDecl->specific_attr_begin<NonNullAttr>(),
429          e = FDecl->specific_attr_end<NonNullAttr>(); i != e; ++i) {
430     CheckNonNullArguments(*i, TheCall->getArgs(),
431                           TheCall->getCallee()->getLocStart());
432   }
433
434   unsigned CMId = FDecl->getMemoryFunctionKind();
435   if (CMId == 0)
436     return false;
437
438   // Handle memory setting and copying functions.
439   if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat)
440     CheckStrlcpycatArguments(TheCall, FnInfo);
441   else if (CMId == Builtin::BIstrncat)
442     CheckStrncatArguments(TheCall, FnInfo);
443   else
444     CheckMemaccessArguments(TheCall, CMId, FnInfo);
445
446   return false;
447 }
448
449 bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac, 
450                                Expr **Args, unsigned NumArgs) {
451   for (specific_attr_iterator<FormatAttr>
452        i = Method->specific_attr_begin<FormatAttr>(),
453        e = Method->specific_attr_end<FormatAttr>(); i != e ; ++i) {
454
455     CheckFormatArguments(*i, Args, NumArgs, false, lbrac, 
456                          Method->getSourceRange());
457   }
458
459   // diagnose nonnull arguments.
460   for (specific_attr_iterator<NonNullAttr>
461        i = Method->specific_attr_begin<NonNullAttr>(),
462        e = Method->specific_attr_end<NonNullAttr>(); i != e; ++i) {
463     CheckNonNullArguments(*i, Args, lbrac);
464   }
465
466   return false;
467 }
468
469 bool Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall) {
470   const VarDecl *V = dyn_cast<VarDecl>(NDecl);
471   if (!V)
472     return false;
473
474   QualType Ty = V->getType();
475   if (!Ty->isBlockPointerType())
476     return false;
477
478   // format string checking.
479   for (specific_attr_iterator<FormatAttr>
480        i = NDecl->specific_attr_begin<FormatAttr>(),
481        e = NDecl->specific_attr_end<FormatAttr>(); i != e ; ++i) {
482     CheckFormatArguments(*i, TheCall);
483   }
484
485   return false;
486 }
487
488 ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult,
489                                          AtomicExpr::AtomicOp Op) {
490   CallExpr *TheCall = cast<CallExpr>(TheCallResult.get());
491   DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
492
493   // All these operations take one of the following forms:
494   enum {
495     // C    __c11_atomic_init(A *, C)
496     Init,
497     // C    __c11_atomic_load(A *, int)
498     Load,
499     // void __atomic_load(A *, CP, int)
500     Copy,
501     // C    __c11_atomic_add(A *, M, int)
502     Arithmetic,
503     // C    __atomic_exchange_n(A *, CP, int)
504     Xchg,
505     // void __atomic_exchange(A *, C *, CP, int)
506     GNUXchg,
507     // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int)
508     C11CmpXchg,
509     // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int)
510     GNUCmpXchg
511   } Form = Init;
512   const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 4, 5, 6 };
513   const unsigned NumVals[] = { 1, 0, 1, 1, 1, 2, 2, 3 };
514   // where:
515   //   C is an appropriate type,
516   //   A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins,
517   //   CP is C for __c11 builtins and GNU _n builtins and is C * otherwise,
518   //   M is C if C is an integer, and ptrdiff_t if C is a pointer, and
519   //   the int parameters are for orderings.
520
521   assert(AtomicExpr::AO__c11_atomic_init == 0 &&
522          AtomicExpr::AO__c11_atomic_fetch_xor + 1 == AtomicExpr::AO__atomic_load
523          && "need to update code for modified C11 atomics");
524   bool IsC11 = Op >= AtomicExpr::AO__c11_atomic_init &&
525                Op <= AtomicExpr::AO__c11_atomic_fetch_xor;
526   bool IsN = Op == AtomicExpr::AO__atomic_load_n ||
527              Op == AtomicExpr::AO__atomic_store_n ||
528              Op == AtomicExpr::AO__atomic_exchange_n ||
529              Op == AtomicExpr::AO__atomic_compare_exchange_n;
530   bool IsAddSub = false;
531
532   switch (Op) {
533   case AtomicExpr::AO__c11_atomic_init:
534     Form = Init;
535     break;
536
537   case AtomicExpr::AO__c11_atomic_load:
538   case AtomicExpr::AO__atomic_load_n:
539     Form = Load;
540     break;
541
542   case AtomicExpr::AO__c11_atomic_store:
543   case AtomicExpr::AO__atomic_load:
544   case AtomicExpr::AO__atomic_store:
545   case AtomicExpr::AO__atomic_store_n:
546     Form = Copy;
547     break;
548
549   case AtomicExpr::AO__c11_atomic_fetch_add:
550   case AtomicExpr::AO__c11_atomic_fetch_sub:
551   case AtomicExpr::AO__atomic_fetch_add:
552   case AtomicExpr::AO__atomic_fetch_sub:
553   case AtomicExpr::AO__atomic_add_fetch:
554   case AtomicExpr::AO__atomic_sub_fetch:
555     IsAddSub = true;
556     // Fall through.
557   case AtomicExpr::AO__c11_atomic_fetch_and:
558   case AtomicExpr::AO__c11_atomic_fetch_or:
559   case AtomicExpr::AO__c11_atomic_fetch_xor:
560   case AtomicExpr::AO__atomic_fetch_and:
561   case AtomicExpr::AO__atomic_fetch_or:
562   case AtomicExpr::AO__atomic_fetch_xor:
563   case AtomicExpr::AO__atomic_fetch_nand:
564   case AtomicExpr::AO__atomic_and_fetch:
565   case AtomicExpr::AO__atomic_or_fetch:
566   case AtomicExpr::AO__atomic_xor_fetch:
567   case AtomicExpr::AO__atomic_nand_fetch:
568     Form = Arithmetic;
569     break;
570
571   case AtomicExpr::AO__c11_atomic_exchange:
572   case AtomicExpr::AO__atomic_exchange_n:
573     Form = Xchg;
574     break;
575
576   case AtomicExpr::AO__atomic_exchange:
577     Form = GNUXchg;
578     break;
579
580   case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
581   case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
582     Form = C11CmpXchg;
583     break;
584
585   case AtomicExpr::AO__atomic_compare_exchange:
586   case AtomicExpr::AO__atomic_compare_exchange_n:
587     Form = GNUCmpXchg;
588     break;
589   }
590
591   // Check we have the right number of arguments.
592   if (TheCall->getNumArgs() < NumArgs[Form]) {
593     Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
594       << 0 << NumArgs[Form] << TheCall->getNumArgs()
595       << TheCall->getCallee()->getSourceRange();
596     return ExprError();
597   } else if (TheCall->getNumArgs() > NumArgs[Form]) {
598     Diag(TheCall->getArg(NumArgs[Form])->getLocStart(),
599          diag::err_typecheck_call_too_many_args)
600       << 0 << NumArgs[Form] << TheCall->getNumArgs()
601       << TheCall->getCallee()->getSourceRange();
602     return ExprError();
603   }
604
605   // Inspect the first argument of the atomic operation.
606   Expr *Ptr = TheCall->getArg(0);
607   Ptr = DefaultFunctionArrayLvalueConversion(Ptr).get();
608   const PointerType *pointerType = Ptr->getType()->getAs<PointerType>();
609   if (!pointerType) {
610     Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
611       << Ptr->getType() << Ptr->getSourceRange();
612     return ExprError();
613   }
614
615   // For a __c11 builtin, this should be a pointer to an _Atomic type.
616   QualType AtomTy = pointerType->getPointeeType(); // 'A'
617   QualType ValType = AtomTy; // 'C'
618   if (IsC11) {
619     if (!AtomTy->isAtomicType()) {
620       Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic)
621         << Ptr->getType() << Ptr->getSourceRange();
622       return ExprError();
623     }
624     ValType = AtomTy->getAs<AtomicType>()->getValueType();
625   }
626
627   // For an arithmetic operation, the implied arithmetic must be well-formed.
628   if (Form == Arithmetic) {
629     // gcc does not enforce these rules for GNU atomics, but we do so for sanity.
630     if (IsAddSub && !ValType->isIntegerType() && !ValType->isPointerType()) {
631       Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr)
632         << IsC11 << Ptr->getType() << Ptr->getSourceRange();
633       return ExprError();
634     }
635     if (!IsAddSub && !ValType->isIntegerType()) {
636       Diag(DRE->getLocStart(), diag::err_atomic_op_bitwise_needs_atomic_int)
637         << IsC11 << Ptr->getType() << Ptr->getSourceRange();
638       return ExprError();
639     }
640   } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) {
641     // For __atomic_*_n operations, the value type must be a scalar integral or
642     // pointer type which is 1, 2, 4, 8 or 16 bytes in length.
643     Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr)
644       << IsC11 << Ptr->getType() << Ptr->getSourceRange();
645     return ExprError();
646   }
647
648   if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context)) {
649     // For GNU atomics, require a trivially-copyable type. This is not part of
650     // the GNU atomics specification, but we enforce it for sanity.
651     Diag(DRE->getLocStart(), diag::err_atomic_op_needs_trivial_copy)
652       << Ptr->getType() << Ptr->getSourceRange();
653     return ExprError();
654   }
655
656   // FIXME: For any builtin other than a load, the ValType must not be
657   // const-qualified.
658
659   switch (ValType.getObjCLifetime()) {
660   case Qualifiers::OCL_None:
661   case Qualifiers::OCL_ExplicitNone:
662     // okay
663     break;
664
665   case Qualifiers::OCL_Weak:
666   case Qualifiers::OCL_Strong:
667   case Qualifiers::OCL_Autoreleasing:
668     // FIXME: Can this happen? By this point, ValType should be known
669     // to be trivially copyable.
670     Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
671       << ValType << Ptr->getSourceRange();
672     return ExprError();
673   }
674
675   QualType ResultType = ValType;
676   if (Form == Copy || Form == GNUXchg || Form == Init)
677     ResultType = Context.VoidTy;
678   else if (Form == C11CmpXchg || Form == GNUCmpXchg)
679     ResultType = Context.BoolTy;
680
681   // The type of a parameter passed 'by value'. In the GNU atomics, such
682   // arguments are actually passed as pointers.
683   QualType ByValType = ValType; // 'CP'
684   if (!IsC11 && !IsN)
685     ByValType = Ptr->getType();
686
687   // The first argument --- the pointer --- has a fixed type; we
688   // deduce the types of the rest of the arguments accordingly.  Walk
689   // the remaining arguments, converting them to the deduced value type.
690   for (unsigned i = 1; i != NumArgs[Form]; ++i) {
691     QualType Ty;
692     if (i < NumVals[Form] + 1) {
693       switch (i) {
694       case 1:
695         // The second argument is the non-atomic operand. For arithmetic, this
696         // is always passed by value, and for a compare_exchange it is always
697         // passed by address. For the rest, GNU uses by-address and C11 uses
698         // by-value.
699         assert(Form != Load);
700         if (Form == Init || (Form == Arithmetic && ValType->isIntegerType()))
701           Ty = ValType;
702         else if (Form == Copy || Form == Xchg)
703           Ty = ByValType;
704         else if (Form == Arithmetic)
705           Ty = Context.getPointerDiffType();
706         else
707           Ty = Context.getPointerType(ValType.getUnqualifiedType());
708         break;
709       case 2:
710         // The third argument to compare_exchange / GNU exchange is a
711         // (pointer to a) desired value.
712         Ty = ByValType;
713         break;
714       case 3:
715         // The fourth argument to GNU compare_exchange is a 'weak' flag.
716         Ty = Context.BoolTy;
717         break;
718       }
719     } else {
720       // The order(s) are always converted to int.
721       Ty = Context.IntTy;
722     }
723
724     InitializedEntity Entity =
725         InitializedEntity::InitializeParameter(Context, Ty, false);
726     ExprResult Arg = TheCall->getArg(i);
727     Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
728     if (Arg.isInvalid())
729       return true;
730     TheCall->setArg(i, Arg.get());
731   }
732
733   // Permute the arguments into a 'consistent' order.
734   SmallVector<Expr*, 5> SubExprs;
735   SubExprs.push_back(Ptr);
736   switch (Form) {
737   case Init:
738     // Note, AtomicExpr::getVal1() has a special case for this atomic.
739     SubExprs.push_back(TheCall->getArg(1)); // Val1
740     break;
741   case Load:
742     SubExprs.push_back(TheCall->getArg(1)); // Order
743     break;
744   case Copy:
745   case Arithmetic:
746   case Xchg:
747     SubExprs.push_back(TheCall->getArg(2)); // Order
748     SubExprs.push_back(TheCall->getArg(1)); // Val1
749     break;
750   case GNUXchg:
751     // Note, AtomicExpr::getVal2() has a special case for this atomic.
752     SubExprs.push_back(TheCall->getArg(3)); // Order
753     SubExprs.push_back(TheCall->getArg(1)); // Val1
754     SubExprs.push_back(TheCall->getArg(2)); // Val2
755     break;
756   case C11CmpXchg:
757     SubExprs.push_back(TheCall->getArg(3)); // Order
758     SubExprs.push_back(TheCall->getArg(1)); // Val1
759     SubExprs.push_back(TheCall->getArg(4)); // OrderFail
760     SubExprs.push_back(TheCall->getArg(2)); // Val2
761     break;
762   case GNUCmpXchg:
763     SubExprs.push_back(TheCall->getArg(4)); // Order
764     SubExprs.push_back(TheCall->getArg(1)); // Val1
765     SubExprs.push_back(TheCall->getArg(5)); // OrderFail
766     SubExprs.push_back(TheCall->getArg(2)); // Val2
767     SubExprs.push_back(TheCall->getArg(3)); // Weak
768     break;
769   }
770
771   return Owned(new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(),
772                                         SubExprs.data(), SubExprs.size(),
773                                         ResultType, Op,
774                                         TheCall->getRParenLoc()));
775 }
776
777
778 /// checkBuiltinArgument - Given a call to a builtin function, perform
779 /// normal type-checking on the given argument, updating the call in
780 /// place.  This is useful when a builtin function requires custom
781 /// type-checking for some of its arguments but not necessarily all of
782 /// them.
783 ///
784 /// Returns true on error.
785 static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) {
786   FunctionDecl *Fn = E->getDirectCallee();
787   assert(Fn && "builtin call without direct callee!");
788
789   ParmVarDecl *Param = Fn->getParamDecl(ArgIndex);
790   InitializedEntity Entity =
791     InitializedEntity::InitializeParameter(S.Context, Param);
792
793   ExprResult Arg = E->getArg(0);
794   Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg);
795   if (Arg.isInvalid())
796     return true;
797
798   E->setArg(ArgIndex, Arg.take());
799   return false;
800 }
801
802 /// SemaBuiltinAtomicOverloaded - We have a call to a function like
803 /// __sync_fetch_and_add, which is an overloaded function based on the pointer
804 /// type of its first argument.  The main ActOnCallExpr routines have already
805 /// promoted the types of arguments because all of these calls are prototyped as
806 /// void(...).
807 ///
808 /// This function goes through and does final semantic checking for these
809 /// builtins,
810 ExprResult
811 Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) {
812   CallExpr *TheCall = (CallExpr *)TheCallResult.get();
813   DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
814   FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
815
816   // Ensure that we have at least one argument to do type inference from.
817   if (TheCall->getNumArgs() < 1) {
818     Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
819       << 0 << 1 << TheCall->getNumArgs()
820       << TheCall->getCallee()->getSourceRange();
821     return ExprError();
822   }
823
824   // Inspect the first argument of the atomic builtin.  This should always be
825   // a pointer type, whose element is an integral scalar or pointer type.
826   // Because it is a pointer type, we don't have to worry about any implicit
827   // casts here.
828   // FIXME: We don't allow floating point scalars as input.
829   Expr *FirstArg = TheCall->getArg(0);
830   ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg);
831   if (FirstArgResult.isInvalid())
832     return ExprError();
833   FirstArg = FirstArgResult.take();
834   TheCall->setArg(0, FirstArg);
835
836   const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>();
837   if (!pointerType) {
838     Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
839       << FirstArg->getType() << FirstArg->getSourceRange();
840     return ExprError();
841   }
842
843   QualType ValType = pointerType->getPointeeType();
844   if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
845       !ValType->isBlockPointerType()) {
846     Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr)
847       << FirstArg->getType() << FirstArg->getSourceRange();
848     return ExprError();
849   }
850
851   switch (ValType.getObjCLifetime()) {
852   case Qualifiers::OCL_None:
853   case Qualifiers::OCL_ExplicitNone:
854     // okay
855     break;
856
857   case Qualifiers::OCL_Weak:
858   case Qualifiers::OCL_Strong:
859   case Qualifiers::OCL_Autoreleasing:
860     Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
861       << ValType << FirstArg->getSourceRange();
862     return ExprError();
863   }
864
865   // Strip any qualifiers off ValType.
866   ValType = ValType.getUnqualifiedType();
867
868   // The majority of builtins return a value, but a few have special return
869   // types, so allow them to override appropriately below.
870   QualType ResultType = ValType;
871
872   // We need to figure out which concrete builtin this maps onto.  For example,
873   // __sync_fetch_and_add with a 2 byte object turns into
874   // __sync_fetch_and_add_2.
875 #define BUILTIN_ROW(x) \
876   { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \
877     Builtin::BI##x##_8, Builtin::BI##x##_16 }
878
879   static const unsigned BuiltinIndices[][5] = {
880     BUILTIN_ROW(__sync_fetch_and_add),
881     BUILTIN_ROW(__sync_fetch_and_sub),
882     BUILTIN_ROW(__sync_fetch_and_or),
883     BUILTIN_ROW(__sync_fetch_and_and),
884     BUILTIN_ROW(__sync_fetch_and_xor),
885
886     BUILTIN_ROW(__sync_add_and_fetch),
887     BUILTIN_ROW(__sync_sub_and_fetch),
888     BUILTIN_ROW(__sync_and_and_fetch),
889     BUILTIN_ROW(__sync_or_and_fetch),
890     BUILTIN_ROW(__sync_xor_and_fetch),
891
892     BUILTIN_ROW(__sync_val_compare_and_swap),
893     BUILTIN_ROW(__sync_bool_compare_and_swap),
894     BUILTIN_ROW(__sync_lock_test_and_set),
895     BUILTIN_ROW(__sync_lock_release),
896     BUILTIN_ROW(__sync_swap)
897   };
898 #undef BUILTIN_ROW
899
900   // Determine the index of the size.
901   unsigned SizeIndex;
902   switch (Context.getTypeSizeInChars(ValType).getQuantity()) {
903   case 1: SizeIndex = 0; break;
904   case 2: SizeIndex = 1; break;
905   case 4: SizeIndex = 2; break;
906   case 8: SizeIndex = 3; break;
907   case 16: SizeIndex = 4; break;
908   default:
909     Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size)
910       << FirstArg->getType() << FirstArg->getSourceRange();
911     return ExprError();
912   }
913
914   // Each of these builtins has one pointer argument, followed by some number of
915   // values (0, 1 or 2) followed by a potentially empty varags list of stuff
916   // that we ignore.  Find out which row of BuiltinIndices to read from as well
917   // as the number of fixed args.
918   unsigned BuiltinID = FDecl->getBuiltinID();
919   unsigned BuiltinIndex, NumFixed = 1;
920   switch (BuiltinID) {
921   default: llvm_unreachable("Unknown overloaded atomic builtin!");
922   case Builtin::BI__sync_fetch_and_add: 
923   case Builtin::BI__sync_fetch_and_add_1:
924   case Builtin::BI__sync_fetch_and_add_2:
925   case Builtin::BI__sync_fetch_and_add_4:
926   case Builtin::BI__sync_fetch_and_add_8:
927   case Builtin::BI__sync_fetch_and_add_16:
928     BuiltinIndex = 0; 
929     break;
930       
931   case Builtin::BI__sync_fetch_and_sub: 
932   case Builtin::BI__sync_fetch_and_sub_1:
933   case Builtin::BI__sync_fetch_and_sub_2:
934   case Builtin::BI__sync_fetch_and_sub_4:
935   case Builtin::BI__sync_fetch_and_sub_8:
936   case Builtin::BI__sync_fetch_and_sub_16:
937     BuiltinIndex = 1; 
938     break;
939       
940   case Builtin::BI__sync_fetch_and_or:  
941   case Builtin::BI__sync_fetch_and_or_1:
942   case Builtin::BI__sync_fetch_and_or_2:
943   case Builtin::BI__sync_fetch_and_or_4:
944   case Builtin::BI__sync_fetch_and_or_8:
945   case Builtin::BI__sync_fetch_and_or_16:
946     BuiltinIndex = 2; 
947     break;
948       
949   case Builtin::BI__sync_fetch_and_and: 
950   case Builtin::BI__sync_fetch_and_and_1:
951   case Builtin::BI__sync_fetch_and_and_2:
952   case Builtin::BI__sync_fetch_and_and_4:
953   case Builtin::BI__sync_fetch_and_and_8:
954   case Builtin::BI__sync_fetch_and_and_16:
955     BuiltinIndex = 3; 
956     break;
957
958   case Builtin::BI__sync_fetch_and_xor: 
959   case Builtin::BI__sync_fetch_and_xor_1:
960   case Builtin::BI__sync_fetch_and_xor_2:
961   case Builtin::BI__sync_fetch_and_xor_4:
962   case Builtin::BI__sync_fetch_and_xor_8:
963   case Builtin::BI__sync_fetch_and_xor_16:
964     BuiltinIndex = 4; 
965     break;
966
967   case Builtin::BI__sync_add_and_fetch: 
968   case Builtin::BI__sync_add_and_fetch_1:
969   case Builtin::BI__sync_add_and_fetch_2:
970   case Builtin::BI__sync_add_and_fetch_4:
971   case Builtin::BI__sync_add_and_fetch_8:
972   case Builtin::BI__sync_add_and_fetch_16:
973     BuiltinIndex = 5; 
974     break;
975       
976   case Builtin::BI__sync_sub_and_fetch: 
977   case Builtin::BI__sync_sub_and_fetch_1:
978   case Builtin::BI__sync_sub_and_fetch_2:
979   case Builtin::BI__sync_sub_and_fetch_4:
980   case Builtin::BI__sync_sub_and_fetch_8:
981   case Builtin::BI__sync_sub_and_fetch_16:
982     BuiltinIndex = 6; 
983     break;
984       
985   case Builtin::BI__sync_and_and_fetch: 
986   case Builtin::BI__sync_and_and_fetch_1:
987   case Builtin::BI__sync_and_and_fetch_2:
988   case Builtin::BI__sync_and_and_fetch_4:
989   case Builtin::BI__sync_and_and_fetch_8:
990   case Builtin::BI__sync_and_and_fetch_16:
991     BuiltinIndex = 7; 
992     break;
993       
994   case Builtin::BI__sync_or_and_fetch:  
995   case Builtin::BI__sync_or_and_fetch_1:
996   case Builtin::BI__sync_or_and_fetch_2:
997   case Builtin::BI__sync_or_and_fetch_4:
998   case Builtin::BI__sync_or_and_fetch_8:
999   case Builtin::BI__sync_or_and_fetch_16:
1000     BuiltinIndex = 8; 
1001     break;
1002       
1003   case Builtin::BI__sync_xor_and_fetch: 
1004   case Builtin::BI__sync_xor_and_fetch_1:
1005   case Builtin::BI__sync_xor_and_fetch_2:
1006   case Builtin::BI__sync_xor_and_fetch_4:
1007   case Builtin::BI__sync_xor_and_fetch_8:
1008   case Builtin::BI__sync_xor_and_fetch_16:
1009     BuiltinIndex = 9; 
1010     break;
1011
1012   case Builtin::BI__sync_val_compare_and_swap:
1013   case Builtin::BI__sync_val_compare_and_swap_1:
1014   case Builtin::BI__sync_val_compare_and_swap_2:
1015   case Builtin::BI__sync_val_compare_and_swap_4:
1016   case Builtin::BI__sync_val_compare_and_swap_8:
1017   case Builtin::BI__sync_val_compare_and_swap_16:
1018     BuiltinIndex = 10;
1019     NumFixed = 2;
1020     break;
1021       
1022   case Builtin::BI__sync_bool_compare_and_swap:
1023   case Builtin::BI__sync_bool_compare_and_swap_1:
1024   case Builtin::BI__sync_bool_compare_and_swap_2:
1025   case Builtin::BI__sync_bool_compare_and_swap_4:
1026   case Builtin::BI__sync_bool_compare_and_swap_8:
1027   case Builtin::BI__sync_bool_compare_and_swap_16:
1028     BuiltinIndex = 11;
1029     NumFixed = 2;
1030     ResultType = Context.BoolTy;
1031     break;
1032       
1033   case Builtin::BI__sync_lock_test_and_set: 
1034   case Builtin::BI__sync_lock_test_and_set_1:
1035   case Builtin::BI__sync_lock_test_and_set_2:
1036   case Builtin::BI__sync_lock_test_and_set_4:
1037   case Builtin::BI__sync_lock_test_and_set_8:
1038   case Builtin::BI__sync_lock_test_and_set_16:
1039     BuiltinIndex = 12; 
1040     break;
1041       
1042   case Builtin::BI__sync_lock_release:
1043   case Builtin::BI__sync_lock_release_1:
1044   case Builtin::BI__sync_lock_release_2:
1045   case Builtin::BI__sync_lock_release_4:
1046   case Builtin::BI__sync_lock_release_8:
1047   case Builtin::BI__sync_lock_release_16:
1048     BuiltinIndex = 13;
1049     NumFixed = 0;
1050     ResultType = Context.VoidTy;
1051     break;
1052       
1053   case Builtin::BI__sync_swap: 
1054   case Builtin::BI__sync_swap_1:
1055   case Builtin::BI__sync_swap_2:
1056   case Builtin::BI__sync_swap_4:
1057   case Builtin::BI__sync_swap_8:
1058   case Builtin::BI__sync_swap_16:
1059     BuiltinIndex = 14; 
1060     break;
1061   }
1062
1063   // Now that we know how many fixed arguments we expect, first check that we
1064   // have at least that many.
1065   if (TheCall->getNumArgs() < 1+NumFixed) {
1066     Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
1067       << 0 << 1+NumFixed << TheCall->getNumArgs()
1068       << TheCall->getCallee()->getSourceRange();
1069     return ExprError();
1070   }
1071
1072   // Get the decl for the concrete builtin from this, we can tell what the
1073   // concrete integer type we should convert to is.
1074   unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex];
1075   const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID);
1076   IdentifierInfo *NewBuiltinII = PP.getIdentifierInfo(NewBuiltinName);
1077   FunctionDecl *NewBuiltinDecl =
1078     cast<FunctionDecl>(LazilyCreateBuiltin(NewBuiltinII, NewBuiltinID,
1079                                            TUScope, false, DRE->getLocStart()));
1080
1081   // The first argument --- the pointer --- has a fixed type; we
1082   // deduce the types of the rest of the arguments accordingly.  Walk
1083   // the remaining arguments, converting them to the deduced value type.
1084   for (unsigned i = 0; i != NumFixed; ++i) {
1085     ExprResult Arg = TheCall->getArg(i+1);
1086
1087     // GCC does an implicit conversion to the pointer or integer ValType.  This
1088     // can fail in some cases (1i -> int**), check for this error case now.
1089     // Initialize the argument.
1090     InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
1091                                                    ValType, /*consume*/ false);
1092     Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
1093     if (Arg.isInvalid())
1094       return ExprError();
1095
1096     // Okay, we have something that *can* be converted to the right type.  Check
1097     // to see if there is a potentially weird extension going on here.  This can
1098     // happen when you do an atomic operation on something like an char* and
1099     // pass in 42.  The 42 gets converted to char.  This is even more strange
1100     // for things like 45.123 -> char, etc.
1101     // FIXME: Do this check.
1102     TheCall->setArg(i+1, Arg.take());
1103   }
1104
1105   ASTContext& Context = this->getASTContext();
1106
1107   // Create a new DeclRefExpr to refer to the new decl.
1108   DeclRefExpr* NewDRE = DeclRefExpr::Create(
1109       Context,
1110       DRE->getQualifierLoc(),
1111       SourceLocation(),
1112       NewBuiltinDecl,
1113       /*enclosing*/ false,
1114       DRE->getLocation(),
1115       NewBuiltinDecl->getType(),
1116       DRE->getValueKind());
1117
1118   // Set the callee in the CallExpr.
1119   // FIXME: This leaks the original parens and implicit casts.
1120   ExprResult PromotedCall = UsualUnaryConversions(NewDRE);
1121   if (PromotedCall.isInvalid())
1122     return ExprError();
1123   TheCall->setCallee(PromotedCall.take());
1124
1125   // Change the result type of the call to match the original value type. This
1126   // is arbitrary, but the codegen for these builtins ins design to handle it
1127   // gracefully.
1128   TheCall->setType(ResultType);
1129
1130   return move(TheCallResult);
1131 }
1132
1133 /// CheckObjCString - Checks that the argument to the builtin
1134 /// CFString constructor is correct
1135 /// Note: It might also make sense to do the UTF-16 conversion here (would
1136 /// simplify the backend).
1137 bool Sema::CheckObjCString(Expr *Arg) {
1138   Arg = Arg->IgnoreParenCasts();
1139   StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
1140
1141   if (!Literal || !Literal->isAscii()) {
1142     Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant)
1143       << Arg->getSourceRange();
1144     return true;
1145   }
1146
1147   if (Literal->containsNonAsciiOrNull()) {
1148     StringRef String = Literal->getString();
1149     unsigned NumBytes = String.size();
1150     SmallVector<UTF16, 128> ToBuf(NumBytes);
1151     const UTF8 *FromPtr = (UTF8 *)String.data();
1152     UTF16 *ToPtr = &ToBuf[0];
1153     
1154     ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes,
1155                                                  &ToPtr, ToPtr + NumBytes,
1156                                                  strictConversion);
1157     // Check for conversion failure.
1158     if (Result != conversionOK)
1159       Diag(Arg->getLocStart(),
1160            diag::warn_cfstring_truncated) << Arg->getSourceRange();
1161   }
1162   return false;
1163 }
1164
1165 /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity.
1166 /// Emit an error and return true on failure, return false on success.
1167 bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) {
1168   Expr *Fn = TheCall->getCallee();
1169   if (TheCall->getNumArgs() > 2) {
1170     Diag(TheCall->getArg(2)->getLocStart(),
1171          diag::err_typecheck_call_too_many_args)
1172       << 0 /*function call*/ << 2 << TheCall->getNumArgs()
1173       << Fn->getSourceRange()
1174       << SourceRange(TheCall->getArg(2)->getLocStart(),
1175                      (*(TheCall->arg_end()-1))->getLocEnd());
1176     return true;
1177   }
1178
1179   if (TheCall->getNumArgs() < 2) {
1180     return Diag(TheCall->getLocEnd(),
1181       diag::err_typecheck_call_too_few_args_at_least)
1182       << 0 /*function call*/ << 2 << TheCall->getNumArgs();
1183   }
1184
1185   // Type-check the first argument normally.
1186   if (checkBuiltinArgument(*this, TheCall, 0))
1187     return true;
1188
1189   // Determine whether the current function is variadic or not.
1190   BlockScopeInfo *CurBlock = getCurBlock();
1191   bool isVariadic;
1192   if (CurBlock)
1193     isVariadic = CurBlock->TheDecl->isVariadic();
1194   else if (FunctionDecl *FD = getCurFunctionDecl())
1195     isVariadic = FD->isVariadic();
1196   else
1197     isVariadic = getCurMethodDecl()->isVariadic();
1198
1199   if (!isVariadic) {
1200     Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function);
1201     return true;
1202   }
1203
1204   // Verify that the second argument to the builtin is the last argument of the
1205   // current function or method.
1206   bool SecondArgIsLastNamedArgument = false;
1207   const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts();
1208
1209   if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) {
1210     if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
1211       // FIXME: This isn't correct for methods (results in bogus warning).
1212       // Get the last formal in the current function.
1213       const ParmVarDecl *LastArg;
1214       if (CurBlock)
1215         LastArg = *(CurBlock->TheDecl->param_end()-1);
1216       else if (FunctionDecl *FD = getCurFunctionDecl())
1217         LastArg = *(FD->param_end()-1);
1218       else
1219         LastArg = *(getCurMethodDecl()->param_end()-1);
1220       SecondArgIsLastNamedArgument = PV == LastArg;
1221     }
1222   }
1223
1224   if (!SecondArgIsLastNamedArgument)
1225     Diag(TheCall->getArg(1)->getLocStart(),
1226          diag::warn_second_parameter_of_va_start_not_last_named_argument);
1227   return false;
1228 }
1229
1230 /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and
1231 /// friends.  This is declared to take (...), so we have to check everything.
1232 bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) {
1233   if (TheCall->getNumArgs() < 2)
1234     return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
1235       << 0 << 2 << TheCall->getNumArgs()/*function call*/;
1236   if (TheCall->getNumArgs() > 2)
1237     return Diag(TheCall->getArg(2)->getLocStart(),
1238                 diag::err_typecheck_call_too_many_args)
1239       << 0 /*function call*/ << 2 << TheCall->getNumArgs()
1240       << SourceRange(TheCall->getArg(2)->getLocStart(),
1241                      (*(TheCall->arg_end()-1))->getLocEnd());
1242
1243   ExprResult OrigArg0 = TheCall->getArg(0);
1244   ExprResult OrigArg1 = TheCall->getArg(1);
1245
1246   // Do standard promotions between the two arguments, returning their common
1247   // type.
1248   QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false);
1249   if (OrigArg0.isInvalid() || OrigArg1.isInvalid())
1250     return true;
1251
1252   // Make sure any conversions are pushed back into the call; this is
1253   // type safe since unordered compare builtins are declared as "_Bool
1254   // foo(...)".
1255   TheCall->setArg(0, OrigArg0.get());
1256   TheCall->setArg(1, OrigArg1.get());
1257
1258   if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent())
1259     return false;
1260
1261   // If the common type isn't a real floating type, then the arguments were
1262   // invalid for this operation.
1263   if (!Res->isRealFloatingType())
1264     return Diag(OrigArg0.get()->getLocStart(),
1265                 diag::err_typecheck_call_invalid_ordered_compare)
1266       << OrigArg0.get()->getType() << OrigArg1.get()->getType()
1267       << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd());
1268
1269   return false;
1270 }
1271
1272 /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like
1273 /// __builtin_isnan and friends.  This is declared to take (...), so we have
1274 /// to check everything. We expect the last argument to be a floating point
1275 /// value.
1276 bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) {
1277   if (TheCall->getNumArgs() < NumArgs)
1278     return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
1279       << 0 << NumArgs << TheCall->getNumArgs()/*function call*/;
1280   if (TheCall->getNumArgs() > NumArgs)
1281     return Diag(TheCall->getArg(NumArgs)->getLocStart(),
1282                 diag::err_typecheck_call_too_many_args)
1283       << 0 /*function call*/ << NumArgs << TheCall->getNumArgs()
1284       << SourceRange(TheCall->getArg(NumArgs)->getLocStart(),
1285                      (*(TheCall->arg_end()-1))->getLocEnd());
1286
1287   Expr *OrigArg = TheCall->getArg(NumArgs-1);
1288
1289   if (OrigArg->isTypeDependent())
1290     return false;
1291
1292   // This operation requires a non-_Complex floating-point number.
1293   if (!OrigArg->getType()->isRealFloatingType())
1294     return Diag(OrigArg->getLocStart(),
1295                 diag::err_typecheck_call_invalid_unary_fp)
1296       << OrigArg->getType() << OrigArg->getSourceRange();
1297
1298   // If this is an implicit conversion from float -> double, remove it.
1299   if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) {
1300     Expr *CastArg = Cast->getSubExpr();
1301     if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) {
1302       assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) &&
1303              "promotion from float to double is the only expected cast here");
1304       Cast->setSubExpr(0);
1305       TheCall->setArg(NumArgs-1, CastArg);
1306     }
1307   }
1308   
1309   return false;
1310 }
1311
1312 /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector.
1313 // This is declared to take (...), so we have to check everything.
1314 ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) {
1315   if (TheCall->getNumArgs() < 2)
1316     return ExprError(Diag(TheCall->getLocEnd(),
1317                           diag::err_typecheck_call_too_few_args_at_least)
1318       << 0 /*function call*/ << 2 << TheCall->getNumArgs()
1319       << TheCall->getSourceRange());
1320
1321   // Determine which of the following types of shufflevector we're checking:
1322   // 1) unary, vector mask: (lhs, mask)
1323   // 2) binary, vector mask: (lhs, rhs, mask)
1324   // 3) binary, scalar mask: (lhs, rhs, index, ..., index)
1325   QualType resType = TheCall->getArg(0)->getType();
1326   unsigned numElements = 0;
1327   
1328   if (!TheCall->getArg(0)->isTypeDependent() &&
1329       !TheCall->getArg(1)->isTypeDependent()) {
1330     QualType LHSType = TheCall->getArg(0)->getType();
1331     QualType RHSType = TheCall->getArg(1)->getType();
1332     
1333     if (!LHSType->isVectorType() || !RHSType->isVectorType()) {
1334       Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector)
1335         << SourceRange(TheCall->getArg(0)->getLocStart(),
1336                        TheCall->getArg(1)->getLocEnd());
1337       return ExprError();
1338     }
1339     
1340     numElements = LHSType->getAs<VectorType>()->getNumElements();
1341     unsigned numResElements = TheCall->getNumArgs() - 2;
1342
1343     // Check to see if we have a call with 2 vector arguments, the unary shuffle
1344     // with mask.  If so, verify that RHS is an integer vector type with the
1345     // same number of elts as lhs.
1346     if (TheCall->getNumArgs() == 2) {
1347       if (!RHSType->hasIntegerRepresentation() || 
1348           RHSType->getAs<VectorType>()->getNumElements() != numElements)
1349         Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector)
1350           << SourceRange(TheCall->getArg(1)->getLocStart(),
1351                          TheCall->getArg(1)->getLocEnd());
1352       numResElements = numElements;
1353     }
1354     else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) {
1355       Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector)
1356         << SourceRange(TheCall->getArg(0)->getLocStart(),
1357                        TheCall->getArg(1)->getLocEnd());
1358       return ExprError();
1359     } else if (numElements != numResElements) {
1360       QualType eltType = LHSType->getAs<VectorType>()->getElementType();
1361       resType = Context.getVectorType(eltType, numResElements,
1362                                       VectorType::GenericVector);
1363     }
1364   }
1365
1366   for (unsigned i = 2; i < TheCall->getNumArgs(); i++) {
1367     if (TheCall->getArg(i)->isTypeDependent() ||
1368         TheCall->getArg(i)->isValueDependent())
1369       continue;
1370
1371     llvm::APSInt Result(32);
1372     if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context))
1373       return ExprError(Diag(TheCall->getLocStart(),
1374                   diag::err_shufflevector_nonconstant_argument)
1375                 << TheCall->getArg(i)->getSourceRange());
1376
1377     if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2)
1378       return ExprError(Diag(TheCall->getLocStart(),
1379                   diag::err_shufflevector_argument_too_large)
1380                << TheCall->getArg(i)->getSourceRange());
1381   }
1382
1383   SmallVector<Expr*, 32> exprs;
1384
1385   for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) {
1386     exprs.push_back(TheCall->getArg(i));
1387     TheCall->setArg(i, 0);
1388   }
1389
1390   return Owned(new (Context) ShuffleVectorExpr(Context, exprs.begin(),
1391                                             exprs.size(), resType,
1392                                             TheCall->getCallee()->getLocStart(),
1393                                             TheCall->getRParenLoc()));
1394 }
1395
1396 /// SemaBuiltinPrefetch - Handle __builtin_prefetch.
1397 // This is declared to take (const void*, ...) and can take two
1398 // optional constant int args.
1399 bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) {
1400   unsigned NumArgs = TheCall->getNumArgs();
1401
1402   if (NumArgs > 3)
1403     return Diag(TheCall->getLocEnd(),
1404              diag::err_typecheck_call_too_many_args_at_most)
1405              << 0 /*function call*/ << 3 << NumArgs
1406              << TheCall->getSourceRange();
1407
1408   // Argument 0 is checked for us and the remaining arguments must be
1409   // constant integers.
1410   for (unsigned i = 1; i != NumArgs; ++i) {
1411     Expr *Arg = TheCall->getArg(i);
1412     
1413     llvm::APSInt Result;
1414     if (SemaBuiltinConstantArg(TheCall, i, Result))
1415       return true;
1416
1417     // FIXME: gcc issues a warning and rewrites these to 0. These
1418     // seems especially odd for the third argument since the default
1419     // is 3.
1420     if (i == 1) {
1421       if (Result.getLimitedValue() > 1)
1422         return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
1423              << "0" << "1" << Arg->getSourceRange();
1424     } else {
1425       if (Result.getLimitedValue() > 3)
1426         return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
1427             << "0" << "3" << Arg->getSourceRange();
1428     }
1429   }
1430
1431   return false;
1432 }
1433
1434 /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr
1435 /// TheCall is a constant expression.
1436 bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum,
1437                                   llvm::APSInt &Result) {
1438   Expr *Arg = TheCall->getArg(ArgNum);
1439   DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
1440   FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
1441   
1442   if (Arg->isTypeDependent() || Arg->isValueDependent()) return false;
1443   
1444   if (!Arg->isIntegerConstantExpr(Result, Context))
1445     return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type)
1446                 << FDecl->getDeclName() <<  Arg->getSourceRange();
1447   
1448   return false;
1449 }
1450
1451 /// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr,
1452 /// int type). This simply type checks that type is one of the defined
1453 /// constants (0-3).
1454 // For compatibility check 0-3, llvm only handles 0 and 2.
1455 bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) {
1456   llvm::APSInt Result;
1457   
1458   // Check constant-ness first.
1459   if (SemaBuiltinConstantArg(TheCall, 1, Result))
1460     return true;
1461
1462   Expr *Arg = TheCall->getArg(1);
1463   if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) {
1464     return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
1465              << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
1466   }
1467
1468   return false;
1469 }
1470
1471 /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val).
1472 /// This checks that val is a constant 1.
1473 bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) {
1474   Expr *Arg = TheCall->getArg(1);
1475   llvm::APSInt Result;
1476
1477   // TODO: This is less than ideal. Overload this to take a value.
1478   if (SemaBuiltinConstantArg(TheCall, 1, Result))
1479     return true;
1480   
1481   if (Result != 1)
1482     return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val)
1483              << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
1484
1485   return false;
1486 }
1487
1488 // Handle i > 1 ? "x" : "y", recursively.
1489 bool Sema::SemaCheckStringLiteral(const Expr *E, Expr **Args,
1490                                   unsigned NumArgs, bool HasVAListArg,
1491                                   unsigned format_idx, unsigned firstDataArg,
1492                                   FormatStringType Type, bool inFunctionCall) {
1493  tryAgain:
1494   if (E->isTypeDependent() || E->isValueDependent())
1495     return false;
1496
1497   E = E->IgnoreParenCasts();
1498
1499   if (E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull))
1500     // Technically -Wformat-nonliteral does not warn about this case.
1501     // The behavior of printf and friends in this case is implementation
1502     // dependent.  Ideally if the format string cannot be null then
1503     // it should have a 'nonnull' attribute in the function prototype.
1504     return true;
1505
1506   switch (E->getStmtClass()) {
1507   case Stmt::BinaryConditionalOperatorClass:
1508   case Stmt::ConditionalOperatorClass: {
1509     const AbstractConditionalOperator *C = cast<AbstractConditionalOperator>(E);
1510     return SemaCheckStringLiteral(C->getTrueExpr(), Args, NumArgs, HasVAListArg,
1511                                   format_idx, firstDataArg, Type,
1512                                   inFunctionCall)
1513        && SemaCheckStringLiteral(C->getFalseExpr(), Args, NumArgs, HasVAListArg,
1514                                  format_idx, firstDataArg, Type,
1515                                  inFunctionCall);
1516   }
1517
1518   case Stmt::ImplicitCastExprClass: {
1519     E = cast<ImplicitCastExpr>(E)->getSubExpr();
1520     goto tryAgain;
1521   }
1522
1523   case Stmt::OpaqueValueExprClass:
1524     if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) {
1525       E = src;
1526       goto tryAgain;
1527     }
1528     return false;
1529
1530   case Stmt::PredefinedExprClass:
1531     // While __func__, etc., are technically not string literals, they
1532     // cannot contain format specifiers and thus are not a security
1533     // liability.
1534     return true;
1535       
1536   case Stmt::DeclRefExprClass: {
1537     const DeclRefExpr *DR = cast<DeclRefExpr>(E);
1538
1539     // As an exception, do not flag errors for variables binding to
1540     // const string literals.
1541     if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
1542       bool isConstant = false;
1543       QualType T = DR->getType();
1544
1545       if (const ArrayType *AT = Context.getAsArrayType(T)) {
1546         isConstant = AT->getElementType().isConstant(Context);
1547       } else if (const PointerType *PT = T->getAs<PointerType>()) {
1548         isConstant = T.isConstant(Context) &&
1549                      PT->getPointeeType().isConstant(Context);
1550       } else if (T->isObjCObjectPointerType()) {
1551         // In ObjC, there is usually no "const ObjectPointer" type,
1552         // so don't check if the pointee type is constant.
1553         isConstant = T.isConstant(Context);
1554       }
1555
1556       if (isConstant) {
1557         if (const Expr *Init = VD->getAnyInitializer())
1558           return SemaCheckStringLiteral(Init, Args, NumArgs,
1559                                         HasVAListArg, format_idx, firstDataArg,
1560                                         Type, /*inFunctionCall*/false);
1561       }
1562
1563       // For vprintf* functions (i.e., HasVAListArg==true), we add a
1564       // special check to see if the format string is a function parameter
1565       // of the function calling the printf function.  If the function
1566       // has an attribute indicating it is a printf-like function, then we
1567       // should suppress warnings concerning non-literals being used in a call
1568       // to a vprintf function.  For example:
1569       //
1570       // void
1571       // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){
1572       //      va_list ap;
1573       //      va_start(ap, fmt);
1574       //      vprintf(fmt, ap);  // Do NOT emit a warning about "fmt".
1575       //      ...
1576       //
1577       if (HasVAListArg) {
1578         if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) {
1579           if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) {
1580             int PVIndex = PV->getFunctionScopeIndex() + 1;
1581             for (specific_attr_iterator<FormatAttr>
1582                  i = ND->specific_attr_begin<FormatAttr>(),
1583                  e = ND->specific_attr_end<FormatAttr>(); i != e ; ++i) {
1584               FormatAttr *PVFormat = *i;
1585               // adjust for implicit parameter
1586               if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND))
1587                 if (MD->isInstance())
1588                   ++PVIndex;
1589               // We also check if the formats are compatible.
1590               // We can't pass a 'scanf' string to a 'printf' function.
1591               if (PVIndex == PVFormat->getFormatIdx() &&
1592                   Type == GetFormatStringType(PVFormat))
1593                 return true;
1594             }
1595           }
1596         }
1597       }
1598     }
1599
1600     return false;
1601   }
1602
1603   case Stmt::CallExprClass:
1604   case Stmt::CXXMemberCallExprClass: {
1605     const CallExpr *CE = cast<CallExpr>(E);
1606     if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {
1607       if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) {
1608         unsigned ArgIndex = FA->getFormatIdx();
1609         if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND))
1610           if (MD->isInstance())
1611             --ArgIndex;
1612         const Expr *Arg = CE->getArg(ArgIndex - 1);
1613
1614         return SemaCheckStringLiteral(Arg, Args, NumArgs, HasVAListArg,
1615                                       format_idx, firstDataArg, Type,
1616                                       inFunctionCall);
1617       }
1618     }
1619
1620     return false;
1621   }
1622   case Stmt::ObjCStringLiteralClass:
1623   case Stmt::StringLiteralClass: {
1624     const StringLiteral *StrE = NULL;
1625
1626     if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E))
1627       StrE = ObjCFExpr->getString();
1628     else
1629       StrE = cast<StringLiteral>(E);
1630
1631     if (StrE) {
1632       CheckFormatString(StrE, E, Args, NumArgs, HasVAListArg, format_idx,
1633                         firstDataArg, Type, inFunctionCall);
1634       return true;
1635     }
1636
1637     return false;
1638   }
1639
1640   default:
1641     return false;
1642   }
1643 }
1644
1645 void
1646 Sema::CheckNonNullArguments(const NonNullAttr *NonNull,
1647                             const Expr * const *ExprArgs,
1648                             SourceLocation CallSiteLoc) {
1649   for (NonNullAttr::args_iterator i = NonNull->args_begin(),
1650                                   e = NonNull->args_end();
1651        i != e; ++i) {
1652     const Expr *ArgExpr = ExprArgs[*i];
1653     if (ArgExpr->isNullPointerConstant(Context,
1654                                        Expr::NPC_ValueDependentIsNotNull))
1655       Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange();
1656   }
1657 }
1658
1659 Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) {
1660   return llvm::StringSwitch<FormatStringType>(Format->getType())
1661   .Case("scanf", FST_Scanf)
1662   .Cases("printf", "printf0", FST_Printf)
1663   .Cases("NSString", "CFString", FST_NSString)
1664   .Case("strftime", FST_Strftime)
1665   .Case("strfmon", FST_Strfmon)
1666   .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf)
1667   .Default(FST_Unknown);
1668 }
1669
1670 /// CheckPrintfScanfArguments - Check calls to printf and scanf (and similar
1671 /// functions) for correct use of format strings.
1672 void Sema::CheckFormatArguments(const FormatAttr *Format, CallExpr *TheCall) {
1673   bool IsCXXMember = false;
1674   // The way the format attribute works in GCC, the implicit this argument
1675   // of member functions is counted. However, it doesn't appear in our own
1676   // lists, so decrement format_idx in that case.
1677   IsCXXMember = isa<CXXMemberCallExpr>(TheCall);
1678   CheckFormatArguments(Format, TheCall->getArgs(), TheCall->getNumArgs(),
1679                        IsCXXMember, TheCall->getRParenLoc(), 
1680                        TheCall->getCallee()->getSourceRange());
1681 }
1682
1683 void Sema::CheckFormatArguments(const FormatAttr *Format, Expr **Args,
1684                                 unsigned NumArgs, bool IsCXXMember,
1685                                 SourceLocation Loc, SourceRange Range) {
1686   bool HasVAListArg = Format->getFirstArg() == 0;
1687   unsigned format_idx = Format->getFormatIdx() - 1;
1688   unsigned firstDataArg = HasVAListArg ? 0 : Format->getFirstArg() - 1;
1689   if (IsCXXMember) {
1690     if (format_idx == 0)
1691       return;
1692     --format_idx;
1693     if(firstDataArg != 0)
1694       --firstDataArg;
1695   }
1696   CheckFormatArguments(Args, NumArgs, HasVAListArg, format_idx,
1697                        firstDataArg, GetFormatStringType(Format), Loc, Range);
1698 }
1699
1700 void Sema::CheckFormatArguments(Expr **Args, unsigned NumArgs,
1701                                 bool HasVAListArg, unsigned format_idx,
1702                                 unsigned firstDataArg, FormatStringType Type,
1703                                 SourceLocation Loc, SourceRange Range) {
1704   // CHECK: printf/scanf-like function is called with no format string.
1705   if (format_idx >= NumArgs) {
1706     Diag(Loc, diag::warn_missing_format_string) << Range;
1707     return;
1708   }
1709
1710   const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts();
1711
1712   // CHECK: format string is not a string literal.
1713   //
1714   // Dynamically generated format strings are difficult to
1715   // automatically vet at compile time.  Requiring that format strings
1716   // are string literals: (1) permits the checking of format strings by
1717   // the compiler and thereby (2) can practically remove the source of
1718   // many format string exploits.
1719
1720   // Format string can be either ObjC string (e.g. @"%d") or
1721   // C string (e.g. "%d")
1722   // ObjC string uses the same format specifiers as C string, so we can use
1723   // the same format string checking logic for both ObjC and C strings.
1724   if (SemaCheckStringLiteral(OrigFormatExpr, Args, NumArgs, HasVAListArg,
1725                              format_idx, firstDataArg, Type))
1726     return;  // Literal format string found, check done!
1727
1728   // Strftime is particular as it always uses a single 'time' argument,
1729   // so it is safe to pass a non-literal string.
1730   if (Type == FST_Strftime)
1731     return;
1732
1733   // Do not emit diag when the string param is a macro expansion and the
1734   // format is either NSString or CFString. This is a hack to prevent
1735   // diag when using the NSLocalizedString and CFCopyLocalizedString macros
1736   // which are usually used in place of NS and CF string literals.
1737   if (Type == FST_NSString && Args[format_idx]->getLocStart().isMacroID())
1738     return;
1739
1740   // If there are no arguments specified, warn with -Wformat-security, otherwise
1741   // warn only with -Wformat-nonliteral.
1742   if (NumArgs == format_idx+1)
1743     Diag(Args[format_idx]->getLocStart(),
1744          diag::warn_format_nonliteral_noargs)
1745       << OrigFormatExpr->getSourceRange();
1746   else
1747     Diag(Args[format_idx]->getLocStart(),
1748          diag::warn_format_nonliteral)
1749            << OrigFormatExpr->getSourceRange();
1750 }
1751
1752 namespace {
1753 class CheckFormatHandler : public analyze_format_string::FormatStringHandler {
1754 protected:
1755   Sema &S;
1756   const StringLiteral *FExpr;
1757   const Expr *OrigFormatExpr;
1758   const unsigned FirstDataArg;
1759   const unsigned NumDataArgs;
1760   const bool IsObjCLiteral;
1761   const char *Beg; // Start of format string.
1762   const bool HasVAListArg;
1763   const Expr * const *Args;
1764   const unsigned NumArgs;
1765   unsigned FormatIdx;
1766   llvm::BitVector CoveredArgs;
1767   bool usesPositionalArgs;
1768   bool atFirstArg;
1769   bool inFunctionCall;
1770 public:
1771   CheckFormatHandler(Sema &s, const StringLiteral *fexpr,
1772                      const Expr *origFormatExpr, unsigned firstDataArg,
1773                      unsigned numDataArgs, bool isObjCLiteral,
1774                      const char *beg, bool hasVAListArg,
1775                      Expr **args, unsigned numArgs,
1776                      unsigned formatIdx, bool inFunctionCall)
1777     : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr),
1778       FirstDataArg(firstDataArg),
1779       NumDataArgs(numDataArgs),
1780       IsObjCLiteral(isObjCLiteral), Beg(beg),
1781       HasVAListArg(hasVAListArg),
1782       Args(args), NumArgs(numArgs), FormatIdx(formatIdx),
1783       usesPositionalArgs(false), atFirstArg(true),
1784       inFunctionCall(inFunctionCall) {
1785         CoveredArgs.resize(numDataArgs);
1786         CoveredArgs.reset();
1787       }
1788
1789   void DoneProcessing();
1790
1791   void HandleIncompleteSpecifier(const char *startSpecifier,
1792                                  unsigned specifierLen);
1793
1794   void HandleNonStandardLengthModifier(
1795       const analyze_format_string::LengthModifier &LM,
1796       const char *startSpecifier, unsigned specifierLen);
1797
1798   void HandleNonStandardConversionSpecifier(
1799       const analyze_format_string::ConversionSpecifier &CS,
1800       const char *startSpecifier, unsigned specifierLen);
1801
1802   void HandleNonStandardConversionSpecification(
1803       const analyze_format_string::LengthModifier &LM,
1804       const analyze_format_string::ConversionSpecifier &CS,
1805       const char *startSpecifier, unsigned specifierLen);
1806
1807   virtual void HandlePosition(const char *startPos, unsigned posLen);
1808
1809   virtual void HandleInvalidPosition(const char *startSpecifier,
1810                                      unsigned specifierLen,
1811                                      analyze_format_string::PositionContext p);
1812
1813   virtual void HandleZeroPosition(const char *startPos, unsigned posLen);
1814
1815   void HandleNullChar(const char *nullCharacter);
1816
1817   template <typename Range>
1818   static void EmitFormatDiagnostic(Sema &S, bool inFunctionCall,
1819                                    const Expr *ArgumentExpr,
1820                                    PartialDiagnostic PDiag,
1821                                    SourceLocation StringLoc,
1822                                    bool IsStringLocation, Range StringRange,
1823                                    FixItHint Fixit = FixItHint());
1824
1825 protected:
1826   bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc,
1827                                         const char *startSpec,
1828                                         unsigned specifierLen,
1829                                         const char *csStart, unsigned csLen);
1830
1831   void HandlePositionalNonpositionalArgs(SourceLocation Loc,
1832                                          const char *startSpec,
1833                                          unsigned specifierLen);
1834   
1835   SourceRange getFormatStringRange();
1836   CharSourceRange getSpecifierRange(const char *startSpecifier,
1837                                     unsigned specifierLen);
1838   SourceLocation getLocationOfByte(const char *x);
1839
1840   const Expr *getDataArg(unsigned i) const;
1841   
1842   bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS,
1843                     const analyze_format_string::ConversionSpecifier &CS,
1844                     const char *startSpecifier, unsigned specifierLen,
1845                     unsigned argIndex);
1846
1847   template <typename Range>
1848   void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc,
1849                             bool IsStringLocation, Range StringRange,
1850                             FixItHint Fixit = FixItHint());
1851
1852   void CheckPositionalAndNonpositionalArgs(
1853       const analyze_format_string::FormatSpecifier *FS);
1854 };
1855 }
1856
1857 SourceRange CheckFormatHandler::getFormatStringRange() {
1858   return OrigFormatExpr->getSourceRange();
1859 }
1860
1861 CharSourceRange CheckFormatHandler::
1862 getSpecifierRange(const char *startSpecifier, unsigned specifierLen) {
1863   SourceLocation Start = getLocationOfByte(startSpecifier);
1864   SourceLocation End   = getLocationOfByte(startSpecifier + specifierLen - 1);
1865
1866   // Advance the end SourceLocation by one due to half-open ranges.
1867   End = End.getLocWithOffset(1);
1868
1869   return CharSourceRange::getCharRange(Start, End);
1870 }
1871
1872 SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) {
1873   return S.getLocationOfStringLiteralByte(FExpr, x - Beg);
1874 }
1875
1876 void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier,
1877                                                    unsigned specifierLen){
1878   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier),
1879                        getLocationOfByte(startSpecifier),
1880                        /*IsStringLocation*/true,
1881                        getSpecifierRange(startSpecifier, specifierLen));
1882 }
1883
1884 void CheckFormatHandler::HandleNonStandardLengthModifier(
1885     const analyze_format_string::LengthModifier &LM,
1886     const char *startSpecifier, unsigned specifierLen) {
1887   EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) << LM.toString()
1888                        << 0,
1889                        getLocationOfByte(LM.getStart()),
1890                        /*IsStringLocation*/true,
1891                        getSpecifierRange(startSpecifier, specifierLen));
1892 }
1893
1894 void CheckFormatHandler::HandleNonStandardConversionSpecifier(
1895     const analyze_format_string::ConversionSpecifier &CS,
1896     const char *startSpecifier, unsigned specifierLen) {
1897   EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) << CS.toString()
1898                        << 1,
1899                        getLocationOfByte(CS.getStart()),
1900                        /*IsStringLocation*/true,
1901                        getSpecifierRange(startSpecifier, specifierLen));
1902 }
1903
1904 void CheckFormatHandler::HandleNonStandardConversionSpecification(
1905     const analyze_format_string::LengthModifier &LM,
1906     const analyze_format_string::ConversionSpecifier &CS,
1907     const char *startSpecifier, unsigned specifierLen) {
1908   EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_conversion_spec)
1909                        << LM.toString() << CS.toString(),
1910                        getLocationOfByte(LM.getStart()),
1911                        /*IsStringLocation*/true,
1912                        getSpecifierRange(startSpecifier, specifierLen));
1913 }
1914
1915 void CheckFormatHandler::HandlePosition(const char *startPos,
1916                                         unsigned posLen) {
1917   EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg),
1918                                getLocationOfByte(startPos),
1919                                /*IsStringLocation*/true,
1920                                getSpecifierRange(startPos, posLen));
1921 }
1922
1923 void
1924 CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen,
1925                                      analyze_format_string::PositionContext p) {
1926   EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier)
1927                          << (unsigned) p,
1928                        getLocationOfByte(startPos), /*IsStringLocation*/true,
1929                        getSpecifierRange(startPos, posLen));
1930 }
1931
1932 void CheckFormatHandler::HandleZeroPosition(const char *startPos,
1933                                             unsigned posLen) {
1934   EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier),
1935                                getLocationOfByte(startPos),
1936                                /*IsStringLocation*/true,
1937                                getSpecifierRange(startPos, posLen));
1938 }
1939
1940 void CheckFormatHandler::HandleNullChar(const char *nullCharacter) {
1941   if (!IsObjCLiteral) {
1942     // The presence of a null character is likely an error.
1943     EmitFormatDiagnostic(
1944       S.PDiag(diag::warn_printf_format_string_contains_null_char),
1945       getLocationOfByte(nullCharacter), /*IsStringLocation*/true,
1946       getFormatStringRange());
1947   }
1948 }
1949
1950 const Expr *CheckFormatHandler::getDataArg(unsigned i) const {
1951   return Args[FirstDataArg + i];
1952 }
1953
1954 void CheckFormatHandler::DoneProcessing() {
1955     // Does the number of data arguments exceed the number of
1956     // format conversions in the format string?
1957   if (!HasVAListArg) {
1958       // Find any arguments that weren't covered.
1959     CoveredArgs.flip();
1960     signed notCoveredArg = CoveredArgs.find_first();
1961     if (notCoveredArg >= 0) {
1962       assert((unsigned)notCoveredArg < NumDataArgs);
1963       EmitFormatDiagnostic(S.PDiag(diag::warn_printf_data_arg_not_used),
1964                            getDataArg((unsigned) notCoveredArg)->getLocStart(),
1965                            /*IsStringLocation*/false, getFormatStringRange());
1966     }
1967   }
1968 }
1969
1970 bool
1971 CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex,
1972                                                      SourceLocation Loc,
1973                                                      const char *startSpec,
1974                                                      unsigned specifierLen,
1975                                                      const char *csStart,
1976                                                      unsigned csLen) {
1977   
1978   bool keepGoing = true;
1979   if (argIndex < NumDataArgs) {
1980     // Consider the argument coverered, even though the specifier doesn't
1981     // make sense.
1982     CoveredArgs.set(argIndex);
1983   }
1984   else {
1985     // If argIndex exceeds the number of data arguments we
1986     // don't issue a warning because that is just a cascade of warnings (and
1987     // they may have intended '%%' anyway). We don't want to continue processing
1988     // the format string after this point, however, as we will like just get
1989     // gibberish when trying to match arguments.
1990     keepGoing = false;
1991   }
1992   
1993   EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_conversion)
1994                          << StringRef(csStart, csLen),
1995                        Loc, /*IsStringLocation*/true,
1996                        getSpecifierRange(startSpec, specifierLen));
1997   
1998   return keepGoing;
1999 }
2000
2001 void
2002 CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc,
2003                                                       const char *startSpec,
2004                                                       unsigned specifierLen) {
2005   EmitFormatDiagnostic(
2006     S.PDiag(diag::warn_format_mix_positional_nonpositional_args),
2007     Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen));
2008 }
2009
2010 bool
2011 CheckFormatHandler::CheckNumArgs(
2012   const analyze_format_string::FormatSpecifier &FS,
2013   const analyze_format_string::ConversionSpecifier &CS,
2014   const char *startSpecifier, unsigned specifierLen, unsigned argIndex) {
2015
2016   if (argIndex >= NumDataArgs) {
2017     PartialDiagnostic PDiag = FS.usesPositionalArg()
2018       ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args)
2019            << (argIndex+1) << NumDataArgs)
2020       : S.PDiag(diag::warn_printf_insufficient_data_args);
2021     EmitFormatDiagnostic(
2022       PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true,
2023       getSpecifierRange(startSpecifier, specifierLen));
2024     return false;
2025   }
2026   return true;
2027 }
2028
2029 template<typename Range>
2030 void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag,
2031                                               SourceLocation Loc,
2032                                               bool IsStringLocation,
2033                                               Range StringRange,
2034                                               FixItHint FixIt) {
2035   EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag,
2036                        Loc, IsStringLocation, StringRange, FixIt);
2037 }
2038
2039 /// \brief If the format string is not within the funcion call, emit a note
2040 /// so that the function call and string are in diagnostic messages.
2041 ///
2042 /// \param inFunctionCall if true, the format string is within the function
2043 /// call and only one diagnostic message will be produced.  Otherwise, an
2044 /// extra note will be emitted pointing to location of the format string.
2045 ///
2046 /// \param ArgumentExpr the expression that is passed as the format string
2047 /// argument in the function call.  Used for getting locations when two
2048 /// diagnostics are emitted.
2049 ///
2050 /// \param PDiag the callee should already have provided any strings for the
2051 /// diagnostic message.  This function only adds locations and fixits
2052 /// to diagnostics.
2053 ///
2054 /// \param Loc primary location for diagnostic.  If two diagnostics are
2055 /// required, one will be at Loc and a new SourceLocation will be created for
2056 /// the other one.
2057 ///
2058 /// \param IsStringLocation if true, Loc points to the format string should be
2059 /// used for the note.  Otherwise, Loc points to the argument list and will
2060 /// be used with PDiag.
2061 ///
2062 /// \param StringRange some or all of the string to highlight.  This is
2063 /// templated so it can accept either a CharSourceRange or a SourceRange.
2064 ///
2065 /// \param Fixit optional fix it hint for the format string.
2066 template<typename Range>
2067 void CheckFormatHandler::EmitFormatDiagnostic(Sema &S, bool InFunctionCall,
2068                                               const Expr *ArgumentExpr,
2069                                               PartialDiagnostic PDiag,
2070                                               SourceLocation Loc,
2071                                               bool IsStringLocation,
2072                                               Range StringRange,
2073                                               FixItHint FixIt) {
2074   if (InFunctionCall)
2075     S.Diag(Loc, PDiag) << StringRange << FixIt;
2076   else {
2077     S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag)
2078       << ArgumentExpr->getSourceRange();
2079     S.Diag(IsStringLocation ? Loc : StringRange.getBegin(),
2080            diag::note_format_string_defined)
2081       << StringRange << FixIt;
2082   }
2083 }
2084
2085 //===--- CHECK: Printf format string checking ------------------------------===//
2086
2087 namespace {
2088 class CheckPrintfHandler : public CheckFormatHandler {
2089 public:
2090   CheckPrintfHandler(Sema &s, const StringLiteral *fexpr,
2091                      const Expr *origFormatExpr, unsigned firstDataArg,
2092                      unsigned numDataArgs, bool isObjCLiteral,
2093                      const char *beg, bool hasVAListArg,
2094                      Expr **Args, unsigned NumArgs,
2095                      unsigned formatIdx, bool inFunctionCall)
2096   : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg,
2097                        numDataArgs, isObjCLiteral, beg, hasVAListArg,
2098                        Args, NumArgs, formatIdx, inFunctionCall) {}
2099   
2100   
2101   bool HandleInvalidPrintfConversionSpecifier(
2102                                       const analyze_printf::PrintfSpecifier &FS,
2103                                       const char *startSpecifier,
2104                                       unsigned specifierLen);
2105   
2106   bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS,
2107                              const char *startSpecifier,
2108                              unsigned specifierLen);
2109   
2110   bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k,
2111                     const char *startSpecifier, unsigned specifierLen);
2112   void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS,
2113                            const analyze_printf::OptionalAmount &Amt,
2114                            unsigned type,
2115                            const char *startSpecifier, unsigned specifierLen);
2116   void HandleFlag(const analyze_printf::PrintfSpecifier &FS,
2117                   const analyze_printf::OptionalFlag &flag,
2118                   const char *startSpecifier, unsigned specifierLen);
2119   void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS,
2120                          const analyze_printf::OptionalFlag &ignoredFlag,
2121                          const analyze_printf::OptionalFlag &flag,
2122                          const char *startSpecifier, unsigned specifierLen);
2123 };  
2124 }
2125
2126 bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier(
2127                                       const analyze_printf::PrintfSpecifier &FS,
2128                                       const char *startSpecifier,
2129                                       unsigned specifierLen) {
2130   const analyze_printf::PrintfConversionSpecifier &CS =
2131     FS.getConversionSpecifier();
2132   
2133   return HandleInvalidConversionSpecifier(FS.getArgIndex(),
2134                                           getLocationOfByte(CS.getStart()),
2135                                           startSpecifier, specifierLen,
2136                                           CS.getStart(), CS.getLength());
2137 }
2138
2139 bool CheckPrintfHandler::HandleAmount(
2140                                const analyze_format_string::OptionalAmount &Amt,
2141                                unsigned k, const char *startSpecifier,
2142                                unsigned specifierLen) {
2143
2144   if (Amt.hasDataArgument()) {
2145     if (!HasVAListArg) {
2146       unsigned argIndex = Amt.getArgIndex();
2147       if (argIndex >= NumDataArgs) {
2148         EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg)
2149                                << k,
2150                              getLocationOfByte(Amt.getStart()),
2151                              /*IsStringLocation*/true,
2152                              getSpecifierRange(startSpecifier, specifierLen));
2153         // Don't do any more checking.  We will just emit
2154         // spurious errors.
2155         return false;
2156       }
2157
2158       // Type check the data argument.  It should be an 'int'.
2159       // Although not in conformance with C99, we also allow the argument to be
2160       // an 'unsigned int' as that is a reasonably safe case.  GCC also
2161       // doesn't emit a warning for that case.
2162       CoveredArgs.set(argIndex);
2163       const Expr *Arg = getDataArg(argIndex);
2164       QualType T = Arg->getType();
2165
2166       const analyze_printf::ArgTypeResult &ATR = Amt.getArgType(S.Context);
2167       assert(ATR.isValid());
2168
2169       if (!ATR.matchesType(S.Context, T)) {
2170         EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type)
2171                                << k << ATR.getRepresentativeTypeName(S.Context)
2172                                << T << Arg->getSourceRange(),
2173                              getLocationOfByte(Amt.getStart()),
2174                              /*IsStringLocation*/true,
2175                              getSpecifierRange(startSpecifier, specifierLen));
2176         // Don't do any more checking.  We will just emit
2177         // spurious errors.
2178         return false;
2179       }
2180     }
2181   }
2182   return true;
2183 }
2184
2185 void CheckPrintfHandler::HandleInvalidAmount(
2186                                       const analyze_printf::PrintfSpecifier &FS,
2187                                       const analyze_printf::OptionalAmount &Amt,
2188                                       unsigned type,
2189                                       const char *startSpecifier,
2190                                       unsigned specifierLen) {
2191   const analyze_printf::PrintfConversionSpecifier &CS =
2192     FS.getConversionSpecifier();
2193
2194   FixItHint fixit =
2195     Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant
2196       ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(),
2197                                  Amt.getConstantLength()))
2198       : FixItHint();
2199
2200   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount)
2201                          << type << CS.toString(),
2202                        getLocationOfByte(Amt.getStart()),
2203                        /*IsStringLocation*/true,
2204                        getSpecifierRange(startSpecifier, specifierLen),
2205                        fixit);
2206 }
2207
2208 void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS,
2209                                     const analyze_printf::OptionalFlag &flag,
2210                                     const char *startSpecifier,
2211                                     unsigned specifierLen) {
2212   // Warn about pointless flag with a fixit removal.
2213   const analyze_printf::PrintfConversionSpecifier &CS =
2214     FS.getConversionSpecifier();
2215   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag)
2216                          << flag.toString() << CS.toString(),
2217                        getLocationOfByte(flag.getPosition()),
2218                        /*IsStringLocation*/true,
2219                        getSpecifierRange(startSpecifier, specifierLen),
2220                        FixItHint::CreateRemoval(
2221                          getSpecifierRange(flag.getPosition(), 1)));
2222 }
2223
2224 void CheckPrintfHandler::HandleIgnoredFlag(
2225                                 const analyze_printf::PrintfSpecifier &FS,
2226                                 const analyze_printf::OptionalFlag &ignoredFlag,
2227                                 const analyze_printf::OptionalFlag &flag,
2228                                 const char *startSpecifier,
2229                                 unsigned specifierLen) {
2230   // Warn about ignored flag with a fixit removal.
2231   EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag)
2232                          << ignoredFlag.toString() << flag.toString(),
2233                        getLocationOfByte(ignoredFlag.getPosition()),
2234                        /*IsStringLocation*/true,
2235                        getSpecifierRange(startSpecifier, specifierLen),
2236                        FixItHint::CreateRemoval(
2237                          getSpecifierRange(ignoredFlag.getPosition(), 1)));
2238 }
2239
2240 bool
2241 CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier
2242                                             &FS,
2243                                           const char *startSpecifier,
2244                                           unsigned specifierLen) {
2245
2246   using namespace analyze_format_string;
2247   using namespace analyze_printf;  
2248   const PrintfConversionSpecifier &CS = FS.getConversionSpecifier();
2249
2250   if (FS.consumesDataArgument()) {
2251     if (atFirstArg) {
2252         atFirstArg = false;
2253         usesPositionalArgs = FS.usesPositionalArg();
2254     }
2255     else if (usesPositionalArgs != FS.usesPositionalArg()) {
2256       HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()),
2257                                         startSpecifier, specifierLen);
2258       return false;
2259     }
2260   }
2261
2262   // First check if the field width, precision, and conversion specifier
2263   // have matching data arguments.
2264   if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0,
2265                     startSpecifier, specifierLen)) {
2266     return false;
2267   }
2268
2269   if (!HandleAmount(FS.getPrecision(), /* precision */ 1,
2270                     startSpecifier, specifierLen)) {
2271     return false;
2272   }
2273
2274   if (!CS.consumesDataArgument()) {
2275     // FIXME: Technically specifying a precision or field width here
2276     // makes no sense.  Worth issuing a warning at some point.
2277     return true;
2278   }
2279
2280   // Consume the argument.
2281   unsigned argIndex = FS.getArgIndex();
2282   if (argIndex < NumDataArgs) {
2283     // The check to see if the argIndex is valid will come later.
2284     // We set the bit here because we may exit early from this
2285     // function if we encounter some other error.
2286     CoveredArgs.set(argIndex);
2287   }
2288
2289   // FreeBSD extensions
2290   if (CS.getKind() == ConversionSpecifier::bArg || CS.getKind() == ConversionSpecifier::DArg) { 
2291      // claim the second argument
2292      CoveredArgs.set(argIndex + 1);
2293
2294     // Now type check the data expression that matches the
2295     // format specifier.
2296     const Expr *Ex = getDataArg(argIndex);
2297     const analyze_printf::ArgTypeResult &ATR = 
2298       (CS.getKind() == ConversionSpecifier::bArg) ?
2299         ArgTypeResult(S.Context.IntTy) : ArgTypeResult::CStrTy;
2300     if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType()))
2301       S.Diag(getLocationOfByte(CS.getStart()),
2302              diag::warn_printf_conversion_argument_type_mismatch)
2303         << ATR.getRepresentativeType(S.Context) << Ex->getType()
2304         << getSpecifierRange(startSpecifier, specifierLen)
2305         << Ex->getSourceRange();
2306
2307     // Now type check the data expression that matches the
2308     // format specifier.
2309     Ex = getDataArg(argIndex + 1);
2310     const analyze_printf::ArgTypeResult &ATR2 = ArgTypeResult::CStrTy;
2311     if (ATR2.isValid() && !ATR2.matchesType(S.Context, Ex->getType()))
2312       S.Diag(getLocationOfByte(CS.getStart()),
2313              diag::warn_printf_conversion_argument_type_mismatch)
2314         << ATR2.getRepresentativeType(S.Context) << Ex->getType()
2315         << getSpecifierRange(startSpecifier, specifierLen)
2316         << Ex->getSourceRange();
2317
2318      return true;
2319   }
2320   // END OF FREEBSD EXTENSIONS
2321
2322   // Check for using an Objective-C specific conversion specifier
2323   // in a non-ObjC literal.
2324   if (!IsObjCLiteral && CS.isObjCArg()) {
2325     return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier,
2326                                                   specifierLen);
2327   }
2328
2329   // Check for invalid use of field width
2330   if (!FS.hasValidFieldWidth()) {
2331     HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0,
2332         startSpecifier, specifierLen);
2333   }
2334
2335   // Check for invalid use of precision
2336   if (!FS.hasValidPrecision()) {
2337     HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1,
2338         startSpecifier, specifierLen);
2339   }
2340
2341   // Check each flag does not conflict with any other component.
2342   if (!FS.hasValidThousandsGroupingPrefix())
2343     HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen);
2344   if (!FS.hasValidLeadingZeros())
2345     HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen);
2346   if (!FS.hasValidPlusPrefix())
2347     HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen);
2348   if (!FS.hasValidSpacePrefix())
2349     HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen);
2350   if (!FS.hasValidAlternativeForm())
2351     HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen);
2352   if (!FS.hasValidLeftJustified())
2353     HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen);
2354
2355   // Check that flags are not ignored by another flag
2356   if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+'
2357     HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(),
2358         startSpecifier, specifierLen);
2359   if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-'
2360     HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(),
2361             startSpecifier, specifierLen);
2362
2363   // Check the length modifier is valid with the given conversion specifier.
2364   const LengthModifier &LM = FS.getLengthModifier();
2365   if (!FS.hasValidLengthModifier())
2366     EmitFormatDiagnostic(S.PDiag(diag::warn_format_nonsensical_length)
2367                            << LM.toString() << CS.toString(),
2368                          getLocationOfByte(LM.getStart()),
2369                          /*IsStringLocation*/true,
2370                          getSpecifierRange(startSpecifier, specifierLen),
2371                          FixItHint::CreateRemoval(
2372                            getSpecifierRange(LM.getStart(),
2373                                              LM.getLength())));
2374   if (!FS.hasStandardLengthModifier())
2375     HandleNonStandardLengthModifier(LM, startSpecifier, specifierLen);
2376   if (!FS.hasStandardConversionSpecifier(S.getLangOpts()))
2377     HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen);
2378   if (!FS.hasStandardLengthConversionCombination())
2379     HandleNonStandardConversionSpecification(LM, CS, startSpecifier,
2380                                              specifierLen);
2381
2382   // Are we using '%n'?
2383   if (CS.getKind() == ConversionSpecifier::nArg) {
2384     // Issue a warning about this being a possible security issue.
2385     EmitFormatDiagnostic(S.PDiag(diag::warn_printf_write_back),
2386                          getLocationOfByte(CS.getStart()),
2387                          /*IsStringLocation*/true,
2388                          getSpecifierRange(startSpecifier, specifierLen));
2389     // Continue checking the other format specifiers.
2390     return true;
2391   }
2392
2393   // The remaining checks depend on the data arguments.
2394   if (HasVAListArg)
2395     return true;
2396
2397   if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
2398     return false;
2399
2400   // Now type check the data expression that matches the
2401   // format specifier.
2402   const Expr *Ex = getDataArg(argIndex);
2403   const analyze_printf::ArgTypeResult &ATR = FS.getArgType(S.Context,
2404                                                            IsObjCLiteral);
2405   if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) {
2406     // Check if we didn't match because of an implicit cast from a 'char'
2407     // or 'short' to an 'int'.  This is done because printf is a varargs
2408     // function.
2409     if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Ex))
2410       if (ICE->getType() == S.Context.IntTy) {
2411         // All further checking is done on the subexpression.
2412         Ex = ICE->getSubExpr();
2413         if (ATR.matchesType(S.Context, Ex->getType()))
2414           return true;
2415       }
2416
2417     // We may be able to offer a FixItHint if it is a supported type.
2418     PrintfSpecifier fixedFS = FS;
2419     bool success = fixedFS.fixType(Ex->getType(), S.getLangOpts(),
2420                                    S.Context, IsObjCLiteral);
2421
2422     if (success) {
2423       // Get the fix string from the fixed format specifier
2424       SmallString<128> buf;
2425       llvm::raw_svector_ostream os(buf);
2426       fixedFS.toString(os);
2427
2428       EmitFormatDiagnostic(
2429         S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
2430           << ATR.getRepresentativeTypeName(S.Context) << Ex->getType()
2431           << Ex->getSourceRange(),
2432         getLocationOfByte(CS.getStart()),
2433         /*IsStringLocation*/true,
2434         getSpecifierRange(startSpecifier, specifierLen),
2435         FixItHint::CreateReplacement(
2436           getSpecifierRange(startSpecifier, specifierLen),
2437           os.str()));
2438     }
2439     else {
2440       EmitFormatDiagnostic(
2441         S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
2442           << ATR.getRepresentativeTypeName(S.Context) << Ex->getType()
2443           << getSpecifierRange(startSpecifier, specifierLen)
2444           << Ex->getSourceRange(),
2445         getLocationOfByte(CS.getStart()),
2446         true,
2447         getSpecifierRange(startSpecifier, specifierLen));
2448     }
2449   }
2450
2451   return true;
2452 }
2453
2454 //===--- CHECK: Scanf format string checking ------------------------------===//
2455
2456 namespace {  
2457 class CheckScanfHandler : public CheckFormatHandler {
2458 public:
2459   CheckScanfHandler(Sema &s, const StringLiteral *fexpr,
2460                     const Expr *origFormatExpr, unsigned firstDataArg,
2461                     unsigned numDataArgs, bool isObjCLiteral,
2462                     const char *beg, bool hasVAListArg,
2463                     Expr **Args, unsigned NumArgs,
2464                     unsigned formatIdx, bool inFunctionCall)
2465   : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg,
2466                        numDataArgs, isObjCLiteral, beg, hasVAListArg,
2467                        Args, NumArgs, formatIdx, inFunctionCall) {}
2468   
2469   bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS,
2470                             const char *startSpecifier,
2471                             unsigned specifierLen);
2472   
2473   bool HandleInvalidScanfConversionSpecifier(
2474           const analyze_scanf::ScanfSpecifier &FS,
2475           const char *startSpecifier,
2476           unsigned specifierLen);
2477
2478   void HandleIncompleteScanList(const char *start, const char *end);
2479 };
2480 }
2481
2482 void CheckScanfHandler::HandleIncompleteScanList(const char *start,
2483                                                  const char *end) {
2484   EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete),
2485                        getLocationOfByte(end), /*IsStringLocation*/true,
2486                        getSpecifierRange(start, end - start));
2487 }
2488
2489 bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier(
2490                                         const analyze_scanf::ScanfSpecifier &FS,
2491                                         const char *startSpecifier,
2492                                         unsigned specifierLen) {
2493
2494   const analyze_scanf::ScanfConversionSpecifier &CS =
2495     FS.getConversionSpecifier();
2496
2497   return HandleInvalidConversionSpecifier(FS.getArgIndex(),
2498                                           getLocationOfByte(CS.getStart()),
2499                                           startSpecifier, specifierLen,
2500                                           CS.getStart(), CS.getLength());
2501 }
2502
2503 bool CheckScanfHandler::HandleScanfSpecifier(
2504                                        const analyze_scanf::ScanfSpecifier &FS,
2505                                        const char *startSpecifier,
2506                                        unsigned specifierLen) {
2507   
2508   using namespace analyze_scanf;
2509   using namespace analyze_format_string;  
2510
2511   const ScanfConversionSpecifier &CS = FS.getConversionSpecifier();
2512
2513   // Handle case where '%' and '*' don't consume an argument.  These shouldn't
2514   // be used to decide if we are using positional arguments consistently.
2515   if (FS.consumesDataArgument()) {
2516     if (atFirstArg) {
2517       atFirstArg = false;
2518       usesPositionalArgs = FS.usesPositionalArg();
2519     }
2520     else if (usesPositionalArgs != FS.usesPositionalArg()) {
2521       HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()),
2522                                         startSpecifier, specifierLen);
2523       return false;
2524     }
2525   }
2526   
2527   // Check if the field with is non-zero.
2528   const OptionalAmount &Amt = FS.getFieldWidth();
2529   if (Amt.getHowSpecified() == OptionalAmount::Constant) {
2530     if (Amt.getConstantAmount() == 0) {
2531       const CharSourceRange &R = getSpecifierRange(Amt.getStart(),
2532                                                    Amt.getConstantLength());
2533       EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width),
2534                            getLocationOfByte(Amt.getStart()),
2535                            /*IsStringLocation*/true, R,
2536                            FixItHint::CreateRemoval(R));
2537     }
2538   }
2539   
2540   if (!FS.consumesDataArgument()) {
2541     // FIXME: Technically specifying a precision or field width here
2542     // makes no sense.  Worth issuing a warning at some point.
2543     return true;
2544   }
2545   
2546   // Consume the argument.
2547   unsigned argIndex = FS.getArgIndex();
2548   if (argIndex < NumDataArgs) {
2549       // The check to see if the argIndex is valid will come later.
2550       // We set the bit here because we may exit early from this
2551       // function if we encounter some other error.
2552     CoveredArgs.set(argIndex);
2553   }
2554   
2555   // Check the length modifier is valid with the given conversion specifier.
2556   const LengthModifier &LM = FS.getLengthModifier();
2557   if (!FS.hasValidLengthModifier()) {
2558     const CharSourceRange &R = getSpecifierRange(LM.getStart(), LM.getLength());
2559     EmitFormatDiagnostic(S.PDiag(diag::warn_format_nonsensical_length)
2560                          << LM.toString() << CS.toString()
2561                          << getSpecifierRange(startSpecifier, specifierLen),
2562                          getLocationOfByte(LM.getStart()),
2563                          /*IsStringLocation*/true, R,
2564                          FixItHint::CreateRemoval(R));
2565   }
2566
2567   if (!FS.hasStandardLengthModifier())
2568     HandleNonStandardLengthModifier(LM, startSpecifier, specifierLen);
2569   if (!FS.hasStandardConversionSpecifier(S.getLangOpts()))
2570     HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen);
2571   if (!FS.hasStandardLengthConversionCombination())
2572     HandleNonStandardConversionSpecification(LM, CS, startSpecifier,
2573                                              specifierLen);
2574
2575   // The remaining checks depend on the data arguments.
2576   if (HasVAListArg)
2577     return true;
2578   
2579   if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
2580     return false;
2581   
2582   // Check that the argument type matches the format specifier.
2583   const Expr *Ex = getDataArg(argIndex);
2584   const analyze_scanf::ScanfArgTypeResult &ATR = FS.getArgType(S.Context);
2585   if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) {
2586     ScanfSpecifier fixedFS = FS;
2587     bool success = fixedFS.fixType(Ex->getType(), S.getLangOpts(),
2588                                    S.Context);
2589
2590     if (success) {
2591       // Get the fix string from the fixed format specifier.
2592       SmallString<128> buf;
2593       llvm::raw_svector_ostream os(buf);
2594       fixedFS.toString(os);
2595
2596       EmitFormatDiagnostic(
2597         S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
2598           << ATR.getRepresentativeTypeName(S.Context) << Ex->getType()
2599           << Ex->getSourceRange(),
2600         getLocationOfByte(CS.getStart()),
2601         /*IsStringLocation*/true,
2602         getSpecifierRange(startSpecifier, specifierLen),
2603         FixItHint::CreateReplacement(
2604           getSpecifierRange(startSpecifier, specifierLen),
2605           os.str()));
2606     } else {
2607       EmitFormatDiagnostic(
2608         S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
2609           << ATR.getRepresentativeTypeName(S.Context) << Ex->getType()
2610           << Ex->getSourceRange(),
2611         getLocationOfByte(CS.getStart()),
2612         /*IsStringLocation*/true,
2613         getSpecifierRange(startSpecifier, specifierLen));
2614     }
2615   }
2616
2617   return true;
2618 }
2619
2620 void Sema::CheckFormatString(const StringLiteral *FExpr,
2621                              const Expr *OrigFormatExpr,
2622                              Expr **Args, unsigned NumArgs,
2623                              bool HasVAListArg, unsigned format_idx,
2624                              unsigned firstDataArg, FormatStringType Type,
2625                              bool inFunctionCall) {
2626   
2627   // CHECK: is the format string a wide literal?
2628   if (!FExpr->isAscii()) {
2629     CheckFormatHandler::EmitFormatDiagnostic(
2630       *this, inFunctionCall, Args[format_idx],
2631       PDiag(diag::warn_format_string_is_wide_literal), FExpr->getLocStart(),
2632       /*IsStringLocation*/true, OrigFormatExpr->getSourceRange());
2633     return;
2634   }
2635   
2636   // Str - The format string.  NOTE: this is NOT null-terminated!
2637   StringRef StrRef = FExpr->getString();
2638   const char *Str = StrRef.data();
2639   unsigned StrLen = StrRef.size();
2640   const unsigned numDataArgs = NumArgs - firstDataArg;
2641   
2642   // CHECK: empty format string?
2643   if (StrLen == 0 && numDataArgs > 0) {
2644     CheckFormatHandler::EmitFormatDiagnostic(
2645       *this, inFunctionCall, Args[format_idx],
2646       PDiag(diag::warn_empty_format_string), FExpr->getLocStart(),
2647       /*IsStringLocation*/true, OrigFormatExpr->getSourceRange());
2648     return;
2649   }
2650   
2651   if (Type == FST_Printf || Type == FST_NSString) {
2652     CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg,
2653                          numDataArgs, isa<ObjCStringLiteral>(OrigFormatExpr),
2654                          Str, HasVAListArg, Args, NumArgs, format_idx,
2655                          inFunctionCall);
2656   
2657     if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen,
2658                                                   getLangOpts()))
2659       H.DoneProcessing();
2660   } else if (Type == FST_Scanf) {
2661     CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg,
2662                         numDataArgs, isa<ObjCStringLiteral>(OrigFormatExpr),
2663                         Str, HasVAListArg, Args, NumArgs, format_idx,
2664                         inFunctionCall);
2665     
2666     if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen,
2667                                                  getLangOpts()))
2668       H.DoneProcessing();
2669   } // TODO: handle other formats
2670 }
2671
2672 //===--- CHECK: Standard memory functions ---------------------------------===//
2673
2674 /// \brief Determine whether the given type is a dynamic class type (e.g.,
2675 /// whether it has a vtable).
2676 static bool isDynamicClassType(QualType T) {
2677   if (CXXRecordDecl *Record = T->getAsCXXRecordDecl())
2678     if (CXXRecordDecl *Definition = Record->getDefinition())
2679       if (Definition->isDynamicClass())
2680         return true;
2681   
2682   return false;
2683 }
2684
2685 /// \brief If E is a sizeof expression, returns its argument expression,
2686 /// otherwise returns NULL.
2687 static const Expr *getSizeOfExprArg(const Expr* E) {
2688   if (const UnaryExprOrTypeTraitExpr *SizeOf =
2689       dyn_cast<UnaryExprOrTypeTraitExpr>(E))
2690     if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType())
2691       return SizeOf->getArgumentExpr()->IgnoreParenImpCasts();
2692
2693   return 0;
2694 }
2695
2696 /// \brief If E is a sizeof expression, returns its argument type.
2697 static QualType getSizeOfArgType(const Expr* E) {
2698   if (const UnaryExprOrTypeTraitExpr *SizeOf =
2699       dyn_cast<UnaryExprOrTypeTraitExpr>(E))
2700     if (SizeOf->getKind() == clang::UETT_SizeOf)
2701       return SizeOf->getTypeOfArgument();
2702
2703   return QualType();
2704 }
2705
2706 /// \brief Check for dangerous or invalid arguments to memset().
2707 ///
2708 /// This issues warnings on known problematic, dangerous or unspecified
2709 /// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp'
2710 /// function calls.
2711 ///
2712 /// \param Call The call expression to diagnose.
2713 void Sema::CheckMemaccessArguments(const CallExpr *Call,
2714                                    unsigned BId,
2715                                    IdentifierInfo *FnName) {
2716   assert(BId != 0);
2717
2718   // It is possible to have a non-standard definition of memset.  Validate
2719   // we have enough arguments, and if not, abort further checking.
2720   unsigned ExpectedNumArgs = (BId == Builtin::BIstrndup ? 2 : 3);
2721   if (Call->getNumArgs() < ExpectedNumArgs)
2722     return;
2723
2724   unsigned LastArg = (BId == Builtin::BImemset ||
2725                       BId == Builtin::BIstrndup ? 1 : 2);
2726   unsigned LenArg = (BId == Builtin::BIstrndup ? 1 : 2);
2727   const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts();
2728
2729   // We have special checking when the length is a sizeof expression.
2730   QualType SizeOfArgTy = getSizeOfArgType(LenExpr);
2731   const Expr *SizeOfArg = getSizeOfExprArg(LenExpr);
2732   llvm::FoldingSetNodeID SizeOfArgID;
2733
2734   for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) {
2735     const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts();
2736     SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange();
2737
2738     QualType DestTy = Dest->getType();
2739     if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) {
2740       QualType PointeeTy = DestPtrTy->getPointeeType();
2741
2742       // Never warn about void type pointers. This can be used to suppress
2743       // false positives.
2744       if (PointeeTy->isVoidType())
2745         continue;
2746
2747       // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by
2748       // actually comparing the expressions for equality. Because computing the
2749       // expression IDs can be expensive, we only do this if the diagnostic is
2750       // enabled.
2751       if (SizeOfArg &&
2752           Diags.getDiagnosticLevel(diag::warn_sizeof_pointer_expr_memaccess,
2753                                    SizeOfArg->getExprLoc())) {
2754         // We only compute IDs for expressions if the warning is enabled, and
2755         // cache the sizeof arg's ID.
2756         if (SizeOfArgID == llvm::FoldingSetNodeID())
2757           SizeOfArg->Profile(SizeOfArgID, Context, true);
2758         llvm::FoldingSetNodeID DestID;
2759         Dest->Profile(DestID, Context, true);
2760         if (DestID == SizeOfArgID) {
2761           // TODO: For strncpy() and friends, this could suggest sizeof(dst)
2762           //       over sizeof(src) as well.
2763           unsigned ActionIdx = 0; // Default is to suggest dereferencing.
2764           if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest))
2765             if (UnaryOp->getOpcode() == UO_AddrOf)
2766               ActionIdx = 1; // If its an address-of operator, just remove it.
2767           if (Context.getTypeSize(PointeeTy) == Context.getCharWidth())
2768             ActionIdx = 2; // If the pointee's size is sizeof(char),
2769                            // suggest an explicit length.
2770           unsigned DestSrcSelect =
2771             (BId == Builtin::BIstrndup ? 1 : ArgIdx);
2772           DiagRuntimeBehavior(SizeOfArg->getExprLoc(), Dest,
2773                               PDiag(diag::warn_sizeof_pointer_expr_memaccess)
2774                                 << FnName << DestSrcSelect << ActionIdx
2775                                 << Dest->getSourceRange()
2776                                 << SizeOfArg->getSourceRange());
2777           break;
2778         }
2779       }
2780
2781       // Also check for cases where the sizeof argument is the exact same
2782       // type as the memory argument, and where it points to a user-defined
2783       // record type.
2784       if (SizeOfArgTy != QualType()) {
2785         if (PointeeTy->isRecordType() &&
2786             Context.typesAreCompatible(SizeOfArgTy, DestTy)) {
2787           DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest,
2788                               PDiag(diag::warn_sizeof_pointer_type_memaccess)
2789                                 << FnName << SizeOfArgTy << ArgIdx
2790                                 << PointeeTy << Dest->getSourceRange()
2791                                 << LenExpr->getSourceRange());
2792           break;
2793         }
2794       }
2795
2796       // Always complain about dynamic classes.
2797       if (isDynamicClassType(PointeeTy)) {
2798
2799         unsigned OperationType = 0;
2800         // "overwritten" if we're warning about the destination for any call
2801         // but memcmp; otherwise a verb appropriate to the call.
2802         if (ArgIdx != 0 || BId == Builtin::BImemcmp) {
2803           if (BId == Builtin::BImemcpy)
2804             OperationType = 1;
2805           else if(BId == Builtin::BImemmove)
2806             OperationType = 2;
2807           else if (BId == Builtin::BImemcmp)
2808             OperationType = 3;
2809         }
2810           
2811         DiagRuntimeBehavior(
2812           Dest->getExprLoc(), Dest,
2813           PDiag(diag::warn_dyn_class_memaccess)
2814             << (BId == Builtin::BImemcmp ? ArgIdx + 2 : ArgIdx)
2815             << FnName << PointeeTy
2816             << OperationType
2817             << Call->getCallee()->getSourceRange());
2818       } else if (PointeeTy.hasNonTrivialObjCLifetime() &&
2819                BId != Builtin::BImemset)
2820         DiagRuntimeBehavior(
2821           Dest->getExprLoc(), Dest,
2822           PDiag(diag::warn_arc_object_memaccess)
2823             << ArgIdx << FnName << PointeeTy
2824             << Call->getCallee()->getSourceRange());
2825       else
2826         continue;
2827
2828       DiagRuntimeBehavior(
2829         Dest->getExprLoc(), Dest,
2830         PDiag(diag::note_bad_memaccess_silence)
2831           << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)"));
2832       break;
2833     }
2834   }
2835 }
2836
2837 // A little helper routine: ignore addition and subtraction of integer literals.
2838 // This intentionally does not ignore all integer constant expressions because
2839 // we don't want to remove sizeof().
2840 static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) {
2841   Ex = Ex->IgnoreParenCasts();
2842
2843   for (;;) {
2844     const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex);
2845     if (!BO || !BO->isAdditiveOp())
2846       break;
2847
2848     const Expr *RHS = BO->getRHS()->IgnoreParenCasts();
2849     const Expr *LHS = BO->getLHS()->IgnoreParenCasts();
2850     
2851     if (isa<IntegerLiteral>(RHS))
2852       Ex = LHS;
2853     else if (isa<IntegerLiteral>(LHS))
2854       Ex = RHS;
2855     else
2856       break;
2857   }
2858
2859   return Ex;
2860 }
2861
2862 // Warn if the user has made the 'size' argument to strlcpy or strlcat
2863 // be the size of the source, instead of the destination.
2864 void Sema::CheckStrlcpycatArguments(const CallExpr *Call,
2865                                     IdentifierInfo *FnName) {
2866
2867   // Don't crash if the user has the wrong number of arguments
2868   if (Call->getNumArgs() != 3)
2869     return;
2870
2871   const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context);
2872   const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context);
2873   const Expr *CompareWithSrc = NULL;
2874   
2875   // Look for 'strlcpy(dst, x, sizeof(x))'
2876   if (const Expr *Ex = getSizeOfExprArg(SizeArg))
2877     CompareWithSrc = Ex;
2878   else {
2879     // Look for 'strlcpy(dst, x, strlen(x))'
2880     if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) {
2881       if (SizeCall->isBuiltinCall() == Builtin::BIstrlen
2882           && SizeCall->getNumArgs() == 1)
2883         CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context);
2884     }
2885   }
2886
2887   if (!CompareWithSrc)
2888     return;
2889
2890   // Determine if the argument to sizeof/strlen is equal to the source
2891   // argument.  In principle there's all kinds of things you could do
2892   // here, for instance creating an == expression and evaluating it with
2893   // EvaluateAsBooleanCondition, but this uses a more direct technique:
2894   const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg);
2895   if (!SrcArgDRE)
2896     return;
2897   
2898   const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc);
2899   if (!CompareWithSrcDRE || 
2900       SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl())
2901     return;
2902   
2903   const Expr *OriginalSizeArg = Call->getArg(2);
2904   Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size)
2905     << OriginalSizeArg->getSourceRange() << FnName;
2906   
2907   // Output a FIXIT hint if the destination is an array (rather than a
2908   // pointer to an array).  This could be enhanced to handle some
2909   // pointers if we know the actual size, like if DstArg is 'array+2'
2910   // we could say 'sizeof(array)-2'.
2911   const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts();
2912   QualType DstArgTy = DstArg->getType();
2913   
2914   // Only handle constant-sized or VLAs, but not flexible members.
2915   if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(DstArgTy)) {
2916     // Only issue the FIXIT for arrays of size > 1.
2917     if (CAT->getSize().getSExtValue() <= 1)
2918       return;
2919   } else if (!DstArgTy->isVariableArrayType()) {
2920     return;
2921   }
2922
2923   SmallString<128> sizeString;
2924   llvm::raw_svector_ostream OS(sizeString);
2925   OS << "sizeof(";
2926   DstArg->printPretty(OS, Context, 0, getPrintingPolicy());
2927   OS << ")";
2928   
2929   Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size)
2930     << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(),
2931                                     OS.str());
2932 }
2933
2934 /// Check if two expressions refer to the same declaration.
2935 static bool referToTheSameDecl(const Expr *E1, const Expr *E2) {
2936   if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1))
2937     if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2))
2938       return D1->getDecl() == D2->getDecl();
2939   return false;
2940 }
2941
2942 static const Expr *getStrlenExprArg(const Expr *E) {
2943   if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
2944     const FunctionDecl *FD = CE->getDirectCallee();
2945     if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen)
2946       return 0;
2947     return CE->getArg(0)->IgnoreParenCasts();
2948   }
2949   return 0;
2950 }
2951
2952 // Warn on anti-patterns as the 'size' argument to strncat.
2953 // The correct size argument should look like following:
2954 //   strncat(dst, src, sizeof(dst) - strlen(dest) - 1);
2955 void Sema::CheckStrncatArguments(const CallExpr *CE,
2956                                  IdentifierInfo *FnName) {
2957   // Don't crash if the user has the wrong number of arguments.
2958   if (CE->getNumArgs() < 3)
2959     return;
2960   const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts();
2961   const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts();
2962   const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts();
2963
2964   // Identify common expressions, which are wrongly used as the size argument
2965   // to strncat and may lead to buffer overflows.
2966   unsigned PatternType = 0;
2967   if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) {
2968     // - sizeof(dst)
2969     if (referToTheSameDecl(SizeOfArg, DstArg))
2970       PatternType = 1;
2971     // - sizeof(src)
2972     else if (referToTheSameDecl(SizeOfArg, SrcArg))
2973       PatternType = 2;
2974   } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) {
2975     if (BE->getOpcode() == BO_Sub) {
2976       const Expr *L = BE->getLHS()->IgnoreParenCasts();
2977       const Expr *R = BE->getRHS()->IgnoreParenCasts();
2978       // - sizeof(dst) - strlen(dst)
2979       if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) &&
2980           referToTheSameDecl(DstArg, getStrlenExprArg(R)))
2981         PatternType = 1;
2982       // - sizeof(src) - (anything)
2983       else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L)))
2984         PatternType = 2;
2985     }
2986   }
2987
2988   if (PatternType == 0)
2989     return;
2990
2991   // Generate the diagnostic.
2992   SourceLocation SL = LenArg->getLocStart();
2993   SourceRange SR = LenArg->getSourceRange();
2994   SourceManager &SM  = PP.getSourceManager();
2995
2996   // If the function is defined as a builtin macro, do not show macro expansion.
2997   if (SM.isMacroArgExpansion(SL)) {
2998     SL = SM.getSpellingLoc(SL);
2999     SR = SourceRange(SM.getSpellingLoc(SR.getBegin()),
3000                      SM.getSpellingLoc(SR.getEnd()));
3001   }
3002
3003   if (PatternType == 1)
3004     Diag(SL, diag::warn_strncat_large_size) << SR;
3005   else
3006     Diag(SL, diag::warn_strncat_src_size) << SR;
3007
3008   // Output a FIXIT hint if the destination is an array (rather than a
3009   // pointer to an array).  This could be enhanced to handle some
3010   // pointers if we know the actual size, like if DstArg is 'array+2'
3011   // we could say 'sizeof(array)-2'.
3012   QualType DstArgTy = DstArg->getType();
3013
3014   // Only handle constant-sized or VLAs, but not flexible members.
3015   if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(DstArgTy)) {
3016     // Only issue the FIXIT for arrays of size > 1.
3017     if (CAT->getSize().getSExtValue() <= 1)
3018       return;
3019   } else if (!DstArgTy->isVariableArrayType()) {
3020     return;
3021   }
3022
3023   SmallString<128> sizeString;
3024   llvm::raw_svector_ostream OS(sizeString);
3025   OS << "sizeof(";
3026   DstArg->printPretty(OS, Context, 0, getPrintingPolicy());
3027   OS << ") - ";
3028   OS << "strlen(";
3029   DstArg->printPretty(OS, Context, 0, getPrintingPolicy());
3030   OS << ") - 1";
3031
3032   Diag(SL, diag::note_strncat_wrong_size)
3033     << FixItHint::CreateReplacement(SR, OS.str());
3034 }
3035
3036 //===--- CHECK: Return Address of Stack Variable --------------------------===//
3037
3038 static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars);
3039 static Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars);
3040
3041 /// CheckReturnStackAddr - Check if a return statement returns the address
3042 ///   of a stack variable.
3043 void
3044 Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
3045                            SourceLocation ReturnLoc) {
3046
3047   Expr *stackE = 0;
3048   SmallVector<DeclRefExpr *, 8> refVars;
3049
3050   // Perform checking for returned stack addresses, local blocks,
3051   // label addresses or references to temporaries.
3052   if (lhsType->isPointerType() ||
3053       (!getLangOpts().ObjCAutoRefCount && lhsType->isBlockPointerType())) {
3054     stackE = EvalAddr(RetValExp, refVars);
3055   } else if (lhsType->isReferenceType()) {
3056     stackE = EvalVal(RetValExp, refVars);
3057   }
3058
3059   if (stackE == 0)
3060     return; // Nothing suspicious was found.
3061
3062   SourceLocation diagLoc;
3063   SourceRange diagRange;
3064   if (refVars.empty()) {
3065     diagLoc = stackE->getLocStart();
3066     diagRange = stackE->getSourceRange();
3067   } else {
3068     // We followed through a reference variable. 'stackE' contains the
3069     // problematic expression but we will warn at the return statement pointing
3070     // at the reference variable. We will later display the "trail" of
3071     // reference variables using notes.
3072     diagLoc = refVars[0]->getLocStart();
3073     diagRange = refVars[0]->getSourceRange();
3074   }
3075
3076   if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var.
3077     Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref
3078                                              : diag::warn_ret_stack_addr)
3079      << DR->getDecl()->getDeclName() << diagRange;
3080   } else if (isa<BlockExpr>(stackE)) { // local block.
3081     Diag(diagLoc, diag::err_ret_local_block) << diagRange;
3082   } else if (isa<AddrLabelExpr>(stackE)) { // address of label.
3083     Diag(diagLoc, diag::warn_ret_addr_label) << diagRange;
3084   } else { // local temporary.
3085     Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref
3086                                              : diag::warn_ret_local_temp_addr)
3087      << diagRange;
3088   }
3089
3090   // Display the "trail" of reference variables that we followed until we
3091   // found the problematic expression using notes.
3092   for (unsigned i = 0, e = refVars.size(); i != e; ++i) {
3093     VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl());
3094     // If this var binds to another reference var, show the range of the next
3095     // var, otherwise the var binds to the problematic expression, in which case
3096     // show the range of the expression.
3097     SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange()
3098                                   : stackE->getSourceRange();
3099     Diag(VD->getLocation(), diag::note_ref_var_local_bind)
3100       << VD->getDeclName() << range;
3101   }
3102 }
3103
3104 /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that
3105 ///  check if the expression in a return statement evaluates to an address
3106 ///  to a location on the stack, a local block, an address of a label, or a
3107 ///  reference to local temporary. The recursion is used to traverse the
3108 ///  AST of the return expression, with recursion backtracking when we
3109 ///  encounter a subexpression that (1) clearly does not lead to one of the
3110 ///  above problematic expressions (2) is something we cannot determine leads to
3111 ///  a problematic expression based on such local checking.
3112 ///
3113 ///  Both EvalAddr and EvalVal follow through reference variables to evaluate
3114 ///  the expression that they point to. Such variables are added to the
3115 ///  'refVars' vector so that we know what the reference variable "trail" was.
3116 ///
3117 ///  EvalAddr processes expressions that are pointers that are used as
3118 ///  references (and not L-values).  EvalVal handles all other values.
3119 ///  At the base case of the recursion is a check for the above problematic
3120 ///  expressions.
3121 ///
3122 ///  This implementation handles:
3123 ///
3124 ///   * pointer-to-pointer casts
3125 ///   * implicit conversions from array references to pointers
3126 ///   * taking the address of fields
3127 ///   * arbitrary interplay between "&" and "*" operators
3128 ///   * pointer arithmetic from an address of a stack variable
3129 ///   * taking the address of an array element where the array is on the stack
3130 static Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars) {
3131   if (E->isTypeDependent())
3132       return NULL;
3133
3134   // We should only be called for evaluating pointer expressions.
3135   assert((E->getType()->isAnyPointerType() ||
3136           E->getType()->isBlockPointerType() ||
3137           E->getType()->isObjCQualifiedIdType()) &&
3138          "EvalAddr only works on pointers");
3139
3140   E = E->IgnoreParens();
3141
3142   // Our "symbolic interpreter" is just a dispatch off the currently
3143   // viewed AST node.  We then recursively traverse the AST by calling
3144   // EvalAddr and EvalVal appropriately.
3145   switch (E->getStmtClass()) {
3146   case Stmt::DeclRefExprClass: {
3147     DeclRefExpr *DR = cast<DeclRefExpr>(E);
3148
3149     if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
3150       // If this is a reference variable, follow through to the expression that
3151       // it points to.
3152       if (V->hasLocalStorage() &&
3153           V->getType()->isReferenceType() && V->hasInit()) {
3154         // Add the reference variable to the "trail".
3155         refVars.push_back(DR);
3156         return EvalAddr(V->getInit(), refVars);
3157       }
3158
3159     return NULL;
3160   }
3161
3162   case Stmt::UnaryOperatorClass: {
3163     // The only unary operator that make sense to handle here
3164     // is AddrOf.  All others don't make sense as pointers.
3165     UnaryOperator *U = cast<UnaryOperator>(E);
3166
3167     if (U->getOpcode() == UO_AddrOf)
3168       return EvalVal(U->getSubExpr(), refVars);
3169     else
3170       return NULL;
3171   }
3172
3173   case Stmt::BinaryOperatorClass: {
3174     // Handle pointer arithmetic.  All other binary operators are not valid
3175     // in this context.
3176     BinaryOperator *B = cast<BinaryOperator>(E);
3177     BinaryOperatorKind op = B->getOpcode();
3178
3179     if (op != BO_Add && op != BO_Sub)
3180       return NULL;
3181
3182     Expr *Base = B->getLHS();
3183
3184     // Determine which argument is the real pointer base.  It could be
3185     // the RHS argument instead of the LHS.
3186     if (!Base->getType()->isPointerType()) Base = B->getRHS();
3187
3188     assert (Base->getType()->isPointerType());
3189     return EvalAddr(Base, refVars);
3190   }
3191
3192   // For conditional operators we need to see if either the LHS or RHS are
3193   // valid DeclRefExpr*s.  If one of them is valid, we return it.
3194   case Stmt::ConditionalOperatorClass: {
3195     ConditionalOperator *C = cast<ConditionalOperator>(E);
3196
3197     // Handle the GNU extension for missing LHS.
3198     if (Expr *lhsExpr = C->getLHS()) {
3199     // In C++, we can have a throw-expression, which has 'void' type.
3200       if (!lhsExpr->getType()->isVoidType())
3201         if (Expr* LHS = EvalAddr(lhsExpr, refVars))
3202           return LHS;
3203     }
3204
3205     // In C++, we can have a throw-expression, which has 'void' type.
3206     if (C->getRHS()->getType()->isVoidType())
3207       return NULL;
3208
3209     return EvalAddr(C->getRHS(), refVars);
3210   }
3211   
3212   case Stmt::BlockExprClass:
3213     if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures())
3214       return E; // local block.
3215     return NULL;
3216
3217   case Stmt::AddrLabelExprClass:
3218     return E; // address of label.
3219
3220   case Stmt::ExprWithCleanupsClass:
3221     return EvalAddr(cast<ExprWithCleanups>(E)->getSubExpr(), refVars);
3222
3223   // For casts, we need to handle conversions from arrays to
3224   // pointer values, and pointer-to-pointer conversions.
3225   case Stmt::ImplicitCastExprClass:
3226   case Stmt::CStyleCastExprClass:
3227   case Stmt::CXXFunctionalCastExprClass:
3228   case Stmt::ObjCBridgedCastExprClass:
3229   case Stmt::CXXStaticCastExprClass:
3230   case Stmt::CXXDynamicCastExprClass:
3231   case Stmt::CXXConstCastExprClass:
3232   case Stmt::CXXReinterpretCastExprClass: {
3233     Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
3234     switch (cast<CastExpr>(E)->getCastKind()) {
3235     case CK_BitCast:
3236     case CK_LValueToRValue:
3237     case CK_NoOp:
3238     case CK_BaseToDerived:
3239     case CK_DerivedToBase:
3240     case CK_UncheckedDerivedToBase:
3241     case CK_Dynamic:
3242     case CK_CPointerToObjCPointerCast:
3243     case CK_BlockPointerToObjCPointerCast:
3244     case CK_AnyPointerToBlockPointerCast:
3245       return EvalAddr(SubExpr, refVars);
3246
3247     case CK_ArrayToPointerDecay:
3248       return EvalVal(SubExpr, refVars);
3249
3250     default:
3251       return 0;
3252     }
3253   }
3254
3255   case Stmt::MaterializeTemporaryExprClass:
3256     if (Expr *Result = EvalAddr(
3257                          cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(),
3258                                 refVars))
3259       return Result;
3260       
3261     return E;
3262       
3263   // Everything else: we simply don't reason about them.
3264   default:
3265     return NULL;
3266   }
3267 }
3268
3269
3270 ///  EvalVal - This function is complements EvalAddr in the mutual recursion.
3271 ///   See the comments for EvalAddr for more details.
3272 static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars) {
3273 do {
3274   // We should only be called for evaluating non-pointer expressions, or
3275   // expressions with a pointer type that are not used as references but instead
3276   // are l-values (e.g., DeclRefExpr with a pointer type).
3277
3278   // Our "symbolic interpreter" is just a dispatch off the currently
3279   // viewed AST node.  We then recursively traverse the AST by calling
3280   // EvalAddr and EvalVal appropriately.
3281
3282   E = E->IgnoreParens();
3283   switch (E->getStmtClass()) {
3284   case Stmt::ImplicitCastExprClass: {
3285     ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E);
3286     if (IE->getValueKind() == VK_LValue) {
3287       E = IE->getSubExpr();
3288       continue;
3289     }
3290     return NULL;
3291   }
3292
3293   case Stmt::ExprWithCleanupsClass:
3294     return EvalVal(cast<ExprWithCleanups>(E)->getSubExpr(), refVars);
3295
3296   case Stmt::DeclRefExprClass: {
3297     // When we hit a DeclRefExpr we are looking at code that refers to a
3298     // variable's name. If it's not a reference variable we check if it has
3299     // local storage within the function, and if so, return the expression.
3300     DeclRefExpr *DR = cast<DeclRefExpr>(E);
3301
3302     if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
3303       if (V->hasLocalStorage()) {
3304         if (!V->getType()->isReferenceType())
3305           return DR;
3306
3307         // Reference variable, follow through to the expression that
3308         // it points to.
3309         if (V->hasInit()) {
3310           // Add the reference variable to the "trail".
3311           refVars.push_back(DR);
3312           return EvalVal(V->getInit(), refVars);
3313         }
3314       }
3315
3316     return NULL;
3317   }
3318
3319   case Stmt::UnaryOperatorClass: {
3320     // The only unary operator that make sense to handle here
3321     // is Deref.  All others don't resolve to a "name."  This includes
3322     // handling all sorts of rvalues passed to a unary operator.
3323     UnaryOperator *U = cast<UnaryOperator>(E);
3324
3325     if (U->getOpcode() == UO_Deref)
3326       return EvalAddr(U->getSubExpr(), refVars);
3327
3328     return NULL;
3329   }
3330
3331   case Stmt::ArraySubscriptExprClass: {
3332     // Array subscripts are potential references to data on the stack.  We
3333     // retrieve the DeclRefExpr* for the array variable if it indeed
3334     // has local storage.
3335     return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars);
3336   }
3337
3338   case Stmt::ConditionalOperatorClass: {
3339     // For conditional operators we need to see if either the LHS or RHS are
3340     // non-NULL Expr's.  If one is non-NULL, we return it.
3341     ConditionalOperator *C = cast<ConditionalOperator>(E);
3342
3343     // Handle the GNU extension for missing LHS.
3344     if (Expr *lhsExpr = C->getLHS())
3345       if (Expr *LHS = EvalVal(lhsExpr, refVars))
3346         return LHS;
3347
3348     return EvalVal(C->getRHS(), refVars);
3349   }
3350
3351   // Accesses to members are potential references to data on the stack.
3352   case Stmt::MemberExprClass: {
3353     MemberExpr *M = cast<MemberExpr>(E);
3354
3355     // Check for indirect access.  We only want direct field accesses.
3356     if (M->isArrow())
3357       return NULL;
3358
3359     // Check whether the member type is itself a reference, in which case
3360     // we're not going to refer to the member, but to what the member refers to.
3361     if (M->getMemberDecl()->getType()->isReferenceType())
3362       return NULL;
3363
3364     return EvalVal(M->getBase(), refVars);
3365   }
3366
3367   case Stmt::MaterializeTemporaryExprClass:
3368     if (Expr *Result = EvalVal(
3369                           cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(),
3370                                refVars))
3371       return Result;
3372       
3373     return E;
3374
3375   default:
3376     // Check that we don't return or take the address of a reference to a
3377     // temporary. This is only useful in C++.
3378     if (!E->isTypeDependent() && E->isRValue())
3379       return E;
3380
3381     // Everything else: we simply don't reason about them.
3382     return NULL;
3383   }
3384 } while (true);
3385 }
3386
3387 //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===//
3388
3389 /// Check for comparisons of floating point operands using != and ==.
3390 /// Issue a warning if these are no self-comparisons, as they are not likely
3391 /// to do what the programmer intended.
3392 void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) {
3393   bool EmitWarning = true;
3394
3395   Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts();
3396   Expr* RightExprSansParen = RHS->IgnoreParenImpCasts();
3397
3398   // Special case: check for x == x (which is OK).
3399   // Do not emit warnings for such cases.
3400   if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen))
3401     if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen))
3402       if (DRL->getDecl() == DRR->getDecl())
3403         EmitWarning = false;
3404
3405
3406   // Special case: check for comparisons against literals that can be exactly
3407   //  represented by APFloat.  In such cases, do not emit a warning.  This
3408   //  is a heuristic: often comparison against such literals are used to
3409   //  detect if a value in a variable has not changed.  This clearly can
3410   //  lead to false negatives.
3411   if (EmitWarning) {
3412     if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) {
3413       if (FLL->isExact())
3414         EmitWarning = false;
3415     } else
3416       if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)){
3417         if (FLR->isExact())
3418           EmitWarning = false;
3419     }
3420   }
3421
3422   // Check for comparisons with builtin types.
3423   if (EmitWarning)
3424     if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen))
3425       if (CL->isBuiltinCall())
3426         EmitWarning = false;
3427
3428   if (EmitWarning)
3429     if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen))
3430       if (CR->isBuiltinCall())
3431         EmitWarning = false;
3432
3433   // Emit the diagnostic.
3434   if (EmitWarning)
3435     Diag(Loc, diag::warn_floatingpoint_eq)
3436       << LHS->getSourceRange() << RHS->getSourceRange();
3437 }
3438
3439 //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===//
3440 //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===//
3441
3442 namespace {
3443
3444 /// Structure recording the 'active' range of an integer-valued
3445 /// expression.
3446 struct IntRange {
3447   /// The number of bits active in the int.
3448   unsigned Width;
3449
3450   /// True if the int is known not to have negative values.
3451   bool NonNegative;
3452
3453   IntRange(unsigned Width, bool NonNegative)
3454     : Width(Width), NonNegative(NonNegative)
3455   {}
3456
3457   /// Returns the range of the bool type.
3458   static IntRange forBoolType() {
3459     return IntRange(1, true);
3460   }
3461
3462   /// Returns the range of an opaque value of the given integral type.
3463   static IntRange forValueOfType(ASTContext &C, QualType T) {
3464     return forValueOfCanonicalType(C,
3465                           T->getCanonicalTypeInternal().getTypePtr());
3466   }
3467
3468   /// Returns the range of an opaque value of a canonical integral type.
3469   static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) {
3470     assert(T->isCanonicalUnqualified());
3471
3472     if (const VectorType *VT = dyn_cast<VectorType>(T))
3473       T = VT->getElementType().getTypePtr();
3474     if (const ComplexType *CT = dyn_cast<ComplexType>(T))
3475       T = CT->getElementType().getTypePtr();
3476
3477     // For enum types, use the known bit width of the enumerators.
3478     if (const EnumType *ET = dyn_cast<EnumType>(T)) {
3479       EnumDecl *Enum = ET->getDecl();
3480       if (!Enum->isCompleteDefinition())
3481         return IntRange(C.getIntWidth(QualType(T, 0)), false);
3482
3483       unsigned NumPositive = Enum->getNumPositiveBits();
3484       unsigned NumNegative = Enum->getNumNegativeBits();
3485
3486       return IntRange(std::max(NumPositive, NumNegative), NumNegative == 0);
3487     }
3488
3489     const BuiltinType *BT = cast<BuiltinType>(T);
3490     assert(BT->isInteger());
3491
3492     return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
3493   }
3494
3495   /// Returns the "target" range of a canonical integral type, i.e.
3496   /// the range of values expressible in the type.
3497   ///
3498   /// This matches forValueOfCanonicalType except that enums have the
3499   /// full range of their type, not the range of their enumerators.
3500   static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) {
3501     assert(T->isCanonicalUnqualified());
3502
3503     if (const VectorType *VT = dyn_cast<VectorType>(T))
3504       T = VT->getElementType().getTypePtr();
3505     if (const ComplexType *CT = dyn_cast<ComplexType>(T))
3506       T = CT->getElementType().getTypePtr();
3507     if (const EnumType *ET = dyn_cast<EnumType>(T))
3508       T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr();
3509
3510     const BuiltinType *BT = cast<BuiltinType>(T);
3511     assert(BT->isInteger());
3512
3513     return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
3514   }
3515
3516   /// Returns the supremum of two ranges: i.e. their conservative merge.
3517   static IntRange join(IntRange L, IntRange R) {
3518     return IntRange(std::max(L.Width, R.Width),
3519                     L.NonNegative && R.NonNegative);
3520   }
3521
3522   /// Returns the infinum of two ranges: i.e. their aggressive merge.
3523   static IntRange meet(IntRange L, IntRange R) {
3524     return IntRange(std::min(L.Width, R.Width),
3525                     L.NonNegative || R.NonNegative);
3526   }
3527 };
3528
3529 static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value,
3530                               unsigned MaxWidth) {
3531   if (value.isSigned() && value.isNegative())
3532     return IntRange(value.getMinSignedBits(), false);
3533
3534   if (value.getBitWidth() > MaxWidth)
3535     value = value.trunc(MaxWidth);
3536
3537   // isNonNegative() just checks the sign bit without considering
3538   // signedness.
3539   return IntRange(value.getActiveBits(), true);
3540 }
3541
3542 static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty,
3543                               unsigned MaxWidth) {
3544   if (result.isInt())
3545     return GetValueRange(C, result.getInt(), MaxWidth);
3546
3547   if (result.isVector()) {
3548     IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth);
3549     for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) {
3550       IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth);
3551       R = IntRange::join(R, El);
3552     }
3553     return R;
3554   }
3555
3556   if (result.isComplexInt()) {
3557     IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth);
3558     IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth);
3559     return IntRange::join(R, I);
3560   }
3561
3562   // This can happen with lossless casts to intptr_t of "based" lvalues.
3563   // Assume it might use arbitrary bits.
3564   // FIXME: The only reason we need to pass the type in here is to get
3565   // the sign right on this one case.  It would be nice if APValue
3566   // preserved this.
3567   assert(result.isLValue() || result.isAddrLabelDiff());
3568   return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType());
3569 }
3570
3571 /// Pseudo-evaluate the given integer expression, estimating the
3572 /// range of values it might take.
3573 ///
3574 /// \param MaxWidth - the width to which the value will be truncated
3575 static IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) {
3576   E = E->IgnoreParens();
3577
3578   // Try a full evaluation first.
3579   Expr::EvalResult result;
3580   if (E->EvaluateAsRValue(result, C))
3581     return GetValueRange(C, result.Val, E->getType(), MaxWidth);
3582
3583   // I think we only want to look through implicit casts here; if the
3584   // user has an explicit widening cast, we should treat the value as
3585   // being of the new, wider type.
3586   if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) {
3587     if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue)
3588       return GetExprRange(C, CE->getSubExpr(), MaxWidth);
3589
3590     IntRange OutputTypeRange = IntRange::forValueOfType(C, CE->getType());
3591
3592     bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast);
3593
3594     // Assume that non-integer casts can span the full range of the type.
3595     if (!isIntegerCast)
3596       return OutputTypeRange;
3597
3598     IntRange SubRange
3599       = GetExprRange(C, CE->getSubExpr(),
3600                      std::min(MaxWidth, OutputTypeRange.Width));
3601
3602     // Bail out if the subexpr's range is as wide as the cast type.
3603     if (SubRange.Width >= OutputTypeRange.Width)
3604       return OutputTypeRange;
3605
3606     // Otherwise, we take the smaller width, and we're non-negative if
3607     // either the output type or the subexpr is.
3608     return IntRange(SubRange.Width,
3609                     SubRange.NonNegative || OutputTypeRange.NonNegative);
3610   }
3611
3612   if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
3613     // If we can fold the condition, just take that operand.
3614     bool CondResult;
3615     if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C))
3616       return GetExprRange(C, CondResult ? CO->getTrueExpr()
3617                                         : CO->getFalseExpr(),
3618                           MaxWidth);
3619
3620     // Otherwise, conservatively merge.
3621     IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth);
3622     IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth);
3623     return IntRange::join(L, R);
3624   }
3625
3626   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3627     switch (BO->getOpcode()) {
3628
3629     // Boolean-valued operations are single-bit and positive.
3630     case BO_LAnd:
3631     case BO_LOr:
3632     case BO_LT:
3633     case BO_GT:
3634     case BO_LE:
3635     case BO_GE:
3636     case BO_EQ:
3637     case BO_NE:
3638       return IntRange::forBoolType();
3639
3640     // The type of the assignments is the type of the LHS, so the RHS
3641     // is not necessarily the same type.
3642     case BO_MulAssign:
3643     case BO_DivAssign:
3644     case BO_RemAssign:
3645     case BO_AddAssign:
3646     case BO_SubAssign:
3647     case BO_XorAssign:
3648     case BO_OrAssign:
3649       // TODO: bitfields?
3650       return IntRange::forValueOfType(C, E->getType());
3651
3652     // Simple assignments just pass through the RHS, which will have
3653     // been coerced to the LHS type.
3654     case BO_Assign:
3655       // TODO: bitfields?
3656       return GetExprRange(C, BO->getRHS(), MaxWidth);
3657
3658     // Operations with opaque sources are black-listed.
3659     case BO_PtrMemD:
3660     case BO_PtrMemI:
3661       return IntRange::forValueOfType(C, E->getType());
3662
3663     // Bitwise-and uses the *infinum* of the two source ranges.
3664     case BO_And:
3665     case BO_AndAssign:
3666       return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth),
3667                             GetExprRange(C, BO->getRHS(), MaxWidth));
3668
3669     // Left shift gets black-listed based on a judgement call.
3670     case BO_Shl:
3671       // ...except that we want to treat '1 << (blah)' as logically
3672       // positive.  It's an important idiom.
3673       if (IntegerLiteral *I
3674             = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) {
3675         if (I->getValue() == 1) {
3676           IntRange R = IntRange::forValueOfType(C, E->getType());
3677           return IntRange(R.Width, /*NonNegative*/ true);
3678         }
3679       }
3680       // fallthrough
3681
3682     case BO_ShlAssign:
3683       return IntRange::forValueOfType(C, E->getType());
3684
3685     // Right shift by a constant can narrow its left argument.
3686     case BO_Shr:
3687     case BO_ShrAssign: {
3688       IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth);
3689
3690       // If the shift amount is a positive constant, drop the width by
3691       // that much.
3692       llvm::APSInt shift;
3693       if (BO->getRHS()->isIntegerConstantExpr(shift, C) &&
3694           shift.isNonNegative()) {
3695         unsigned zext = shift.getZExtValue();
3696         if (zext >= L.Width)
3697           L.Width = (L.NonNegative ? 0 : 1);
3698         else
3699           L.Width -= zext;
3700       }
3701
3702       return L;
3703     }
3704
3705     // Comma acts as its right operand.
3706     case BO_Comma:
3707       return GetExprRange(C, BO->getRHS(), MaxWidth);
3708
3709     // Black-list pointer subtractions.
3710     case BO_Sub:
3711       if (BO->getLHS()->getType()->isPointerType())
3712         return IntRange::forValueOfType(C, E->getType());
3713       break;
3714
3715     // The width of a division result is mostly determined by the size
3716     // of the LHS.
3717     case BO_Div: {
3718       // Don't 'pre-truncate' the operands.
3719       unsigned opWidth = C.getIntWidth(E->getType());
3720       IntRange L = GetExprRange(C, BO->getLHS(), opWidth);
3721
3722       // If the divisor is constant, use that.
3723       llvm::APSInt divisor;
3724       if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) {
3725         unsigned log2 = divisor.logBase2(); // floor(log_2(divisor))
3726         if (log2 >= L.Width)
3727           L.Width = (L.NonNegative ? 0 : 1);
3728         else
3729           L.Width = std::min(L.Width - log2, MaxWidth);
3730         return L;
3731       }
3732
3733       // Otherwise, just use the LHS's width.
3734       IntRange R = GetExprRange(C, BO->getRHS(), opWidth);
3735       return IntRange(L.Width, L.NonNegative && R.NonNegative);
3736     }
3737
3738     // The result of a remainder can't be larger than the result of
3739     // either side.
3740     case BO_Rem: {
3741       // Don't 'pre-truncate' the operands.
3742       unsigned opWidth = C.getIntWidth(E->getType());
3743       IntRange L = GetExprRange(C, BO->getLHS(), opWidth);
3744       IntRange R = GetExprRange(C, BO->getRHS(), opWidth);
3745
3746       IntRange meet = IntRange::meet(L, R);
3747       meet.Width = std::min(meet.Width, MaxWidth);
3748       return meet;
3749     }
3750
3751     // The default behavior is okay for these.
3752     case BO_Mul:
3753     case BO_Add:
3754     case BO_Xor:
3755     case BO_Or:
3756       break;
3757     }
3758
3759     // The default case is to treat the operation as if it were closed
3760     // on the narrowest type that encompasses both operands.
3761     IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth);
3762     IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth);
3763     return IntRange::join(L, R);
3764   }
3765
3766   if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
3767     switch (UO->getOpcode()) {
3768     // Boolean-valued operations are white-listed.
3769     case UO_LNot:
3770       return IntRange::forBoolType();
3771
3772     // Operations with opaque sources are black-listed.
3773     case UO_Deref:
3774     case UO_AddrOf: // should be impossible
3775       return IntRange::forValueOfType(C, E->getType());
3776
3777     default:
3778       return GetExprRange(C, UO->getSubExpr(), MaxWidth);
3779     }
3780   }
3781   
3782   if (dyn_cast<OffsetOfExpr>(E)) {
3783     IntRange::forValueOfType(C, E->getType());
3784   }
3785
3786   if (FieldDecl *BitField = E->getBitField())
3787     return IntRange(BitField->getBitWidthValue(C),
3788                     BitField->getType()->isUnsignedIntegerOrEnumerationType());
3789
3790   return IntRange::forValueOfType(C, E->getType());
3791 }
3792
3793 static IntRange GetExprRange(ASTContext &C, Expr *E) {
3794   return GetExprRange(C, E, C.getIntWidth(E->getType()));
3795 }
3796
3797 /// Checks whether the given value, which currently has the given
3798 /// source semantics, has the same value when coerced through the
3799 /// target semantics.
3800 static bool IsSameFloatAfterCast(const llvm::APFloat &value,
3801                                  const llvm::fltSemantics &Src,
3802                                  const llvm::fltSemantics &Tgt) {
3803   llvm::APFloat truncated = value;
3804
3805   bool ignored;
3806   truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored);
3807   truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored);
3808
3809   return truncated.bitwiseIsEqual(value);
3810 }
3811
3812 /// Checks whether the given value, which currently has the given
3813 /// source semantics, has the same value when coerced through the
3814 /// target semantics.
3815 ///
3816 /// The value might be a vector of floats (or a complex number).
3817 static bool IsSameFloatAfterCast(const APValue &value,
3818                                  const llvm::fltSemantics &Src,
3819                                  const llvm::fltSemantics &Tgt) {
3820   if (value.isFloat())
3821     return IsSameFloatAfterCast(value.getFloat(), Src, Tgt);
3822
3823   if (value.isVector()) {
3824     for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i)
3825       if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt))
3826         return false;
3827     return true;
3828   }
3829
3830   assert(value.isComplexFloat());
3831   return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) &&
3832           IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt));
3833 }
3834
3835 static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC);
3836
3837 static bool IsZero(Sema &S, Expr *E) {
3838   // Suppress cases where we are comparing against an enum constant.
3839   if (const DeclRefExpr *DR =
3840       dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
3841     if (isa<EnumConstantDecl>(DR->getDecl()))
3842       return false;
3843
3844   // Suppress cases where the '0' value is expanded from a macro.
3845   if (E->getLocStart().isMacroID())
3846     return false;
3847
3848   llvm::APSInt Value;
3849   return E->isIntegerConstantExpr(Value, S.Context) && Value == 0;
3850 }
3851
3852 static bool HasEnumType(Expr *E) {
3853   // Strip off implicit integral promotions.
3854   while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
3855     if (ICE->getCastKind() != CK_IntegralCast &&
3856         ICE->getCastKind() != CK_NoOp)
3857       break;
3858     E = ICE->getSubExpr();
3859   }
3860
3861   return E->getType()->isEnumeralType();
3862 }
3863
3864 static void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) {
3865   BinaryOperatorKind op = E->getOpcode();
3866   if (E->isValueDependent())
3867     return;
3868
3869   if (op == BO_LT && IsZero(S, E->getRHS())) {
3870     S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
3871       << "< 0" << "false" << HasEnumType(E->getLHS())
3872       << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
3873   } else if (op == BO_GE && IsZero(S, E->getRHS())) {
3874     S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
3875       << ">= 0" << "true" << HasEnumType(E->getLHS())
3876       << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
3877   } else if (op == BO_GT && IsZero(S, E->getLHS())) {
3878     S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison)
3879       << "0 >" << "false" << HasEnumType(E->getRHS())
3880       << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
3881   } else if (op == BO_LE && IsZero(S, E->getLHS())) {
3882     S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison)
3883       << "0 <=" << "true" << HasEnumType(E->getRHS())
3884       << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
3885   }
3886 }
3887
3888 /// Analyze the operands of the given comparison.  Implements the
3889 /// fallback case from AnalyzeComparison.
3890 static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) {
3891   AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
3892   AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
3893 }
3894
3895 /// \brief Implements -Wsign-compare.
3896 ///
3897 /// \param E the binary operator to check for warnings
3898 static void AnalyzeComparison(Sema &S, BinaryOperator *E) {
3899   // The type the comparison is being performed in.
3900   QualType T = E->getLHS()->getType();
3901   assert(S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType())
3902          && "comparison with mismatched types");
3903
3904   // We don't do anything special if this isn't an unsigned integral
3905   // comparison:  we're only interested in integral comparisons, and
3906   // signed comparisons only happen in cases we don't care to warn about.
3907   //
3908   // We also don't care about value-dependent expressions or expressions
3909   // whose result is a constant.
3910   if (!T->hasUnsignedIntegerRepresentation()
3911       || E->isValueDependent() || E->isIntegerConstantExpr(S.Context))
3912     return AnalyzeImpConvsInComparison(S, E);
3913
3914   Expr *LHS = E->getLHS()->IgnoreParenImpCasts();
3915   Expr *RHS = E->getRHS()->IgnoreParenImpCasts();
3916
3917   // Check to see if one of the (unmodified) operands is of different
3918   // signedness.
3919   Expr *signedOperand, *unsignedOperand;
3920   if (LHS->getType()->hasSignedIntegerRepresentation()) {
3921     assert(!RHS->getType()->hasSignedIntegerRepresentation() &&
3922            "unsigned comparison between two signed integer expressions?");
3923     signedOperand = LHS;
3924     unsignedOperand = RHS;
3925   } else if (RHS->getType()->hasSignedIntegerRepresentation()) {
3926     signedOperand = RHS;
3927     unsignedOperand = LHS;
3928   } else {
3929     CheckTrivialUnsignedComparison(S, E);
3930     return AnalyzeImpConvsInComparison(S, E);
3931   }
3932
3933   // Otherwise, calculate the effective range of the signed operand.
3934   IntRange signedRange = GetExprRange(S.Context, signedOperand);
3935
3936   // Go ahead and analyze implicit conversions in the operands.  Note
3937   // that we skip the implicit conversions on both sides.
3938   AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc());
3939   AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc());
3940
3941   // If the signed range is non-negative, -Wsign-compare won't fire,
3942   // but we should still check for comparisons which are always true
3943   // or false.
3944   if (signedRange.NonNegative)
3945     return CheckTrivialUnsignedComparison(S, E);
3946
3947   // For (in)equality comparisons, if the unsigned operand is a
3948   // constant which cannot collide with a overflowed signed operand,
3949   // then reinterpreting the signed operand as unsigned will not
3950   // change the result of the comparison.
3951   if (E->isEqualityOp()) {
3952     unsigned comparisonWidth = S.Context.getIntWidth(T);
3953     IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand);
3954
3955     // We should never be unable to prove that the unsigned operand is
3956     // non-negative.
3957     assert(unsignedRange.NonNegative && "unsigned range includes negative?");
3958
3959     if (unsignedRange.Width < comparisonWidth)
3960       return;
3961   }
3962
3963   S.Diag(E->getOperatorLoc(), diag::warn_mixed_sign_comparison)
3964     << LHS->getType() << RHS->getType()
3965     << LHS->getSourceRange() << RHS->getSourceRange();
3966 }
3967
3968 /// Analyzes an attempt to assign the given value to a bitfield.
3969 ///
3970 /// Returns true if there was something fishy about the attempt.
3971 static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init,
3972                                       SourceLocation InitLoc) {
3973   assert(Bitfield->isBitField());
3974   if (Bitfield->isInvalidDecl())
3975     return false;
3976
3977   // White-list bool bitfields.
3978   if (Bitfield->getType()->isBooleanType())
3979     return false;
3980
3981   // Ignore value- or type-dependent expressions.
3982   if (Bitfield->getBitWidth()->isValueDependent() ||
3983       Bitfield->getBitWidth()->isTypeDependent() ||
3984       Init->isValueDependent() ||
3985       Init->isTypeDependent())
3986     return false;
3987
3988   Expr *OriginalInit = Init->IgnoreParenImpCasts();
3989
3990   llvm::APSInt Value;
3991   if (!OriginalInit->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects))
3992     return false;
3993
3994   unsigned OriginalWidth = Value.getBitWidth();
3995   unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context);
3996
3997   if (OriginalWidth <= FieldWidth)
3998     return false;
3999
4000   // Compute the value which the bitfield will contain.
4001   llvm::APSInt TruncatedValue = Value.trunc(FieldWidth);
4002   TruncatedValue.setIsSigned(Bitfield->getType()->isSignedIntegerType());
4003
4004   // Check whether the stored value is equal to the original value.
4005   TruncatedValue = TruncatedValue.extend(OriginalWidth);
4006   if (Value == TruncatedValue)
4007     return false;
4008
4009   // Special-case bitfields of width 1: booleans are naturally 0/1, and
4010   // therefore don't strictly fit into a signed bitfield of width 1.
4011   if (FieldWidth == 1 && Value == 1)
4012     return false;
4013
4014   std::string PrettyValue = Value.toString(10);
4015   std::string PrettyTrunc = TruncatedValue.toString(10);
4016
4017   S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant)
4018     << PrettyValue << PrettyTrunc << OriginalInit->getType()
4019     << Init->getSourceRange();
4020
4021   return true;
4022 }
4023
4024 /// Analyze the given simple or compound assignment for warning-worthy
4025 /// operations.
4026 static void AnalyzeAssignment(Sema &S, BinaryOperator *E) {
4027   // Just recurse on the LHS.
4028   AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
4029
4030   // We want to recurse on the RHS as normal unless we're assigning to
4031   // a bitfield.
4032   if (FieldDecl *Bitfield = E->getLHS()->getBitField()) {
4033     if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(),
4034                                   E->getOperatorLoc())) {
4035       // Recurse, ignoring any implicit conversions on the RHS.
4036       return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(),
4037                                         E->getOperatorLoc());
4038     }
4039   }
4040
4041   AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
4042 }
4043
4044 /// Diagnose an implicit cast;  purely a helper for CheckImplicitConversion.
4045 static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, 
4046                             SourceLocation CContext, unsigned diag,
4047                             bool pruneControlFlow = false) {
4048   if (pruneControlFlow) {
4049     S.DiagRuntimeBehavior(E->getExprLoc(), E,
4050                           S.PDiag(diag)
4051                             << SourceType << T << E->getSourceRange()
4052                             << SourceRange(CContext));
4053     return;
4054   }
4055   S.Diag(E->getExprLoc(), diag)
4056     << SourceType << T << E->getSourceRange() << SourceRange(CContext);
4057 }
4058
4059 /// Diagnose an implicit cast;  purely a helper for CheckImplicitConversion.
4060 static void DiagnoseImpCast(Sema &S, Expr *E, QualType T,
4061                             SourceLocation CContext, unsigned diag,
4062                             bool pruneControlFlow = false) {
4063   DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow);
4064 }
4065
4066 /// Diagnose an implicit cast from a literal expression. Does not warn when the
4067 /// cast wouldn't lose information.
4068 void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T,
4069                                     SourceLocation CContext) {
4070   // Try to convert the literal exactly to an integer. If we can, don't warn.
4071   bool isExact = false;
4072   const llvm::APFloat &Value = FL->getValue();
4073   llvm::APSInt IntegerValue(S.Context.getIntWidth(T),
4074                             T->hasUnsignedIntegerRepresentation());
4075   if (Value.convertToInteger(IntegerValue,
4076                              llvm::APFloat::rmTowardZero, &isExact)
4077       == llvm::APFloat::opOK && isExact)
4078     return;
4079
4080   S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer)
4081     << FL->getType() << T << FL->getSourceRange() << SourceRange(CContext);
4082 }
4083
4084 std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) {
4085   if (!Range.Width) return "0";
4086
4087   llvm::APSInt ValueInRange = Value;
4088   ValueInRange.setIsSigned(!Range.NonNegative);
4089   ValueInRange = ValueInRange.trunc(Range.Width);
4090   return ValueInRange.toString(10);
4091 }
4092
4093 void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
4094                              SourceLocation CC, bool *ICContext = 0) {
4095   if (E->isTypeDependent() || E->isValueDependent()) return;
4096
4097   const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr();
4098   const Type *Target = S.Context.getCanonicalType(T).getTypePtr();
4099   if (Source == Target) return;
4100   if (Target->isDependentType()) return;
4101
4102   // If the conversion context location is invalid don't complain. We also
4103   // don't want to emit a warning if the issue occurs from the expansion of
4104   // a system macro. The problem is that 'getSpellingLoc()' is slow, so we
4105   // delay this check as long as possible. Once we detect we are in that
4106   // scenario, we just return.
4107   if (CC.isInvalid())
4108     return;
4109
4110   // Diagnose implicit casts to bool.
4111   if (Target->isSpecificBuiltinType(BuiltinType::Bool)) {
4112     if (isa<StringLiteral>(E))
4113       // Warn on string literal to bool.  Checks for string literals in logical
4114       // expressions, for instances, assert(0 && "error here"), is prevented
4115       // by a check in AnalyzeImplicitConversions().
4116       return DiagnoseImpCast(S, E, T, CC,
4117                              diag::warn_impcast_string_literal_to_bool);
4118     if (Source->isFunctionType()) {
4119       // Warn on function to bool. Checks free functions and static member
4120       // functions. Weakly imported functions are excluded from the check,
4121       // since it's common to test their value to check whether the linker
4122       // found a definition for them.
4123       ValueDecl *D = 0;
4124       if (DeclRefExpr* R = dyn_cast<DeclRefExpr>(E)) {
4125         D = R->getDecl();
4126       } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) {
4127         D = M->getMemberDecl();
4128       }
4129
4130       if (D && !D->isWeak()) {
4131         if (FunctionDecl* F = dyn_cast<FunctionDecl>(D)) {
4132           S.Diag(E->getExprLoc(), diag::warn_impcast_function_to_bool)
4133             << F << E->getSourceRange() << SourceRange(CC);
4134           S.Diag(E->getExprLoc(), diag::note_function_to_bool_silence)
4135             << FixItHint::CreateInsertion(E->getExprLoc(), "&");
4136           QualType ReturnType;
4137           UnresolvedSet<4> NonTemplateOverloads;
4138           S.isExprCallable(*E, ReturnType, NonTemplateOverloads);
4139           if (!ReturnType.isNull() 
4140               && ReturnType->isSpecificBuiltinType(BuiltinType::Bool))
4141             S.Diag(E->getExprLoc(), diag::note_function_to_bool_call)
4142               << FixItHint::CreateInsertion(
4143                  S.getPreprocessor().getLocForEndOfToken(E->getLocEnd()), "()");
4144           return;
4145         }
4146       }
4147     }
4148     return; // Other casts to bool are not checked.
4149   }
4150
4151   // Strip vector types.
4152   if (isa<VectorType>(Source)) {
4153     if (!isa<VectorType>(Target)) {
4154       if (S.SourceMgr.isInSystemMacro(CC))
4155         return;
4156       return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar);
4157     }
4158     
4159     // If the vector cast is cast between two vectors of the same size, it is
4160     // a bitcast, not a conversion.
4161     if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target))
4162       return;
4163
4164     Source = cast<VectorType>(Source)->getElementType().getTypePtr();
4165     Target = cast<VectorType>(Target)->getElementType().getTypePtr();
4166   }
4167
4168   // Strip complex types.
4169   if (isa<ComplexType>(Source)) {
4170     if (!isa<ComplexType>(Target)) {
4171       if (S.SourceMgr.isInSystemMacro(CC))
4172         return;
4173
4174       return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar);
4175     }
4176
4177     Source = cast<ComplexType>(Source)->getElementType().getTypePtr();
4178     Target = cast<ComplexType>(Target)->getElementType().getTypePtr();
4179   }
4180
4181   const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source);
4182   const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target);
4183
4184   // If the source is floating point...
4185   if (SourceBT && SourceBT->isFloatingPoint()) {
4186     // ...and the target is floating point...
4187     if (TargetBT && TargetBT->isFloatingPoint()) {
4188       // ...then warn if we're dropping FP rank.
4189
4190       // Builtin FP kinds are ordered by increasing FP rank.
4191       if (SourceBT->getKind() > TargetBT->getKind()) {
4192         // Don't warn about float constants that are precisely
4193         // representable in the target type.
4194         Expr::EvalResult result;
4195         if (E->EvaluateAsRValue(result, S.Context)) {
4196           // Value might be a float, a float vector, or a float complex.
4197           if (IsSameFloatAfterCast(result.Val,
4198                    S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)),
4199                    S.Context.getFloatTypeSemantics(QualType(SourceBT, 0))))
4200             return;
4201         }
4202
4203         if (S.SourceMgr.isInSystemMacro(CC))
4204           return;
4205
4206         DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision);
4207       }
4208       return;
4209     }
4210
4211     // If the target is integral, always warn.    
4212     if ((TargetBT && TargetBT->isInteger())) {
4213       if (S.SourceMgr.isInSystemMacro(CC))
4214         return;
4215       
4216       Expr *InnerE = E->IgnoreParenImpCasts();
4217       // We also want to warn on, e.g., "int i = -1.234"
4218       if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE))
4219         if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus)
4220           InnerE = UOp->getSubExpr()->IgnoreParenImpCasts();
4221
4222       if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) {
4223         DiagnoseFloatingLiteralImpCast(S, FL, T, CC);
4224       } else {
4225         DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer);
4226       }
4227     }
4228
4229     return;
4230   }
4231
4232   if (!Source->isIntegerType() || !Target->isIntegerType())
4233     return;
4234
4235   if ((E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)
4236            == Expr::NPCK_GNUNull) && Target->isIntegerType()) {
4237     SourceLocation Loc = E->getSourceRange().getBegin();
4238     if (Loc.isMacroID())
4239       Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first;
4240     S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer)
4241         << T << Loc << clang::SourceRange(CC);
4242     return;
4243   }
4244
4245   IntRange SourceRange = GetExprRange(S.Context, E);
4246   IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target);
4247
4248   if (SourceRange.Width > TargetRange.Width) {
4249     // If the source is a constant, use a default-on diagnostic.
4250     // TODO: this should happen for bitfield stores, too.
4251     llvm::APSInt Value(32);
4252     if (E->isIntegerConstantExpr(Value, S.Context)) {
4253       if (S.SourceMgr.isInSystemMacro(CC))
4254         return;
4255
4256       std::string PrettySourceValue = Value.toString(10);
4257       std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange);
4258
4259       S.DiagRuntimeBehavior(E->getExprLoc(), E,
4260         S.PDiag(diag::warn_impcast_integer_precision_constant)
4261             << PrettySourceValue << PrettyTargetValue
4262             << E->getType() << T << E->getSourceRange()
4263             << clang::SourceRange(CC));
4264       return;
4265     }
4266
4267     // People want to build with -Wshorten-64-to-32 and not -Wconversion.
4268     if (S.SourceMgr.isInSystemMacro(CC))
4269       return;
4270     
4271     if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64)
4272       return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32,
4273                              /* pruneControlFlow */ true);
4274     return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision);
4275   }
4276
4277   if ((TargetRange.NonNegative && !SourceRange.NonNegative) ||
4278       (!TargetRange.NonNegative && SourceRange.NonNegative &&
4279        SourceRange.Width == TargetRange.Width)) {
4280         
4281     if (S.SourceMgr.isInSystemMacro(CC))
4282       return;
4283
4284     unsigned DiagID = diag::warn_impcast_integer_sign;
4285
4286     // Traditionally, gcc has warned about this under -Wsign-compare.
4287     // We also want to warn about it in -Wconversion.
4288     // So if -Wconversion is off, use a completely identical diagnostic
4289     // in the sign-compare group.
4290     // The conditional-checking code will 
4291     if (ICContext) {
4292       DiagID = diag::warn_impcast_integer_sign_conditional;
4293       *ICContext = true;
4294     }
4295
4296     return DiagnoseImpCast(S, E, T, CC, DiagID);
4297   }
4298
4299   // Diagnose conversions between different enumeration types.
4300   // In C, we pretend that the type of an EnumConstantDecl is its enumeration
4301   // type, to give us better diagnostics.
4302   QualType SourceType = E->getType();
4303   if (!S.getLangOpts().CPlusPlus) {
4304     if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
4305       if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
4306         EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext());
4307         SourceType = S.Context.getTypeDeclType(Enum);
4308         Source = S.Context.getCanonicalType(SourceType).getTypePtr();
4309       }
4310   }
4311   
4312   if (const EnumType *SourceEnum = Source->getAs<EnumType>())
4313     if (const EnumType *TargetEnum = Target->getAs<EnumType>())
4314       if ((SourceEnum->getDecl()->getIdentifier() || 
4315            SourceEnum->getDecl()->getTypedefNameForAnonDecl()) &&
4316           (TargetEnum->getDecl()->getIdentifier() ||
4317            TargetEnum->getDecl()->getTypedefNameForAnonDecl()) &&
4318           SourceEnum != TargetEnum) {
4319         if (S.SourceMgr.isInSystemMacro(CC))
4320           return;
4321
4322         return DiagnoseImpCast(S, E, SourceType, T, CC, 
4323                                diag::warn_impcast_different_enum_types);
4324       }
4325   
4326   return;
4327 }
4328
4329 void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T);
4330
4331 void CheckConditionalOperand(Sema &S, Expr *E, QualType T,
4332                              SourceLocation CC, bool &ICContext) {
4333   E = E->IgnoreParenImpCasts();
4334
4335   if (isa<ConditionalOperator>(E))
4336     return CheckConditionalOperator(S, cast<ConditionalOperator>(E), T);
4337
4338   AnalyzeImplicitConversions(S, E, CC);
4339   if (E->getType() != T)
4340     return CheckImplicitConversion(S, E, T, CC, &ICContext);
4341   return;
4342 }
4343
4344 void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T) {
4345   SourceLocation CC = E->getQuestionLoc();
4346
4347   AnalyzeImplicitConversions(S, E->getCond(), CC);
4348
4349   bool Suspicious = false;
4350   CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious);
4351   CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious);
4352
4353   // If -Wconversion would have warned about either of the candidates
4354   // for a signedness conversion to the context type...
4355   if (!Suspicious) return;
4356
4357   // ...but it's currently ignored...
4358   if (S.Diags.getDiagnosticLevel(diag::warn_impcast_integer_sign_conditional,
4359                                  CC))
4360     return;
4361
4362   // ...then check whether it would have warned about either of the
4363   // candidates for a signedness conversion to the condition type.
4364   if (E->getType() == T) return;
4365  
4366   Suspicious = false;
4367   CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(),
4368                           E->getType(), CC, &Suspicious);
4369   if (!Suspicious)
4370     CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(),
4371                             E->getType(), CC, &Suspicious);
4372 }
4373
4374 /// AnalyzeImplicitConversions - Find and report any interesting
4375 /// implicit conversions in the given expression.  There are a couple
4376 /// of competing diagnostics here, -Wconversion and -Wsign-compare.
4377 void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) {
4378   QualType T = OrigE->getType();
4379   Expr *E = OrigE->IgnoreParenImpCasts();
4380
4381   if (E->isTypeDependent() || E->isValueDependent())
4382     return;
4383
4384   // For conditional operators, we analyze the arguments as if they
4385   // were being fed directly into the output.
4386   if (isa<ConditionalOperator>(E)) {
4387     ConditionalOperator *CO = cast<ConditionalOperator>(E);
4388     CheckConditionalOperator(S, CO, T);
4389     return;
4390   }
4391
4392   // Go ahead and check any implicit conversions we might have skipped.
4393   // The non-canonical typecheck is just an optimization;
4394   // CheckImplicitConversion will filter out dead implicit conversions.
4395   if (E->getType() != T)
4396     CheckImplicitConversion(S, E, T, CC);
4397
4398   // Now continue drilling into this expression.
4399
4400   // Skip past explicit casts.
4401   if (isa<ExplicitCastExpr>(E)) {
4402     E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts();
4403     return AnalyzeImplicitConversions(S, E, CC);
4404   }
4405
4406   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
4407     // Do a somewhat different check with comparison operators.
4408     if (BO->isComparisonOp())
4409       return AnalyzeComparison(S, BO);
4410
4411     // And with simple assignments.
4412     if (BO->getOpcode() == BO_Assign)
4413       return AnalyzeAssignment(S, BO);
4414   }
4415
4416   // These break the otherwise-useful invariant below.  Fortunately,
4417   // we don't really need to recurse into them, because any internal
4418   // expressions should have been analyzed already when they were
4419   // built into statements.
4420   if (isa<StmtExpr>(E)) return;
4421
4422   // Don't descend into unevaluated contexts.
4423   if (isa<UnaryExprOrTypeTraitExpr>(E)) return;
4424
4425   // Now just recurse over the expression's children.
4426   CC = E->getExprLoc();
4427   BinaryOperator *BO = dyn_cast<BinaryOperator>(E);
4428   bool IsLogicalOperator = BO && BO->isLogicalOp();
4429   for (Stmt::child_range I = E->children(); I; ++I) {
4430     Expr *ChildExpr = dyn_cast_or_null<Expr>(*I);
4431     if (!ChildExpr)
4432       continue;
4433
4434     if (IsLogicalOperator &&
4435         isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts()))
4436       // Ignore checking string literals that are in logical operators.
4437       continue;
4438     AnalyzeImplicitConversions(S, ChildExpr, CC);
4439   }
4440 }
4441
4442 } // end anonymous namespace
4443
4444 /// Diagnoses "dangerous" implicit conversions within the given
4445 /// expression (which is a full expression).  Implements -Wconversion
4446 /// and -Wsign-compare.
4447 ///
4448 /// \param CC the "context" location of the implicit conversion, i.e.
4449 ///   the most location of the syntactic entity requiring the implicit
4450 ///   conversion
4451 void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) {
4452   // Don't diagnose in unevaluated contexts.
4453   if (ExprEvalContexts.back().Context == Sema::Unevaluated)
4454     return;
4455
4456   // Don't diagnose for value- or type-dependent expressions.
4457   if (E->isTypeDependent() || E->isValueDependent())
4458     return;
4459
4460   // Check for array bounds violations in cases where the check isn't triggered
4461   // elsewhere for other Expr types (like BinaryOperators), e.g. when an
4462   // ArraySubscriptExpr is on the RHS of a variable initialization.
4463   CheckArrayAccess(E);
4464
4465   // This is not the right CC for (e.g.) a variable initialization.
4466   AnalyzeImplicitConversions(*this, E, CC);
4467 }
4468
4469 void Sema::CheckBitFieldInitialization(SourceLocation InitLoc,
4470                                        FieldDecl *BitField,
4471                                        Expr *Init) {
4472   (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc);
4473 }
4474
4475 /// CheckParmsForFunctionDef - Check that the parameters of the given
4476 /// function are appropriate for the definition of a function. This
4477 /// takes care of any checks that cannot be performed on the
4478 /// declaration itself, e.g., that the types of each of the function
4479 /// parameters are complete.
4480 bool Sema::CheckParmsForFunctionDef(ParmVarDecl **P, ParmVarDecl **PEnd,
4481                                     bool CheckParameterNames) {
4482   bool HasInvalidParm = false;
4483   for (; P != PEnd; ++P) {
4484     ParmVarDecl *Param = *P;
4485     
4486     // C99 6.7.5.3p4: the parameters in a parameter type list in a
4487     // function declarator that is part of a function definition of
4488     // that function shall not have incomplete type.
4489     //
4490     // This is also C++ [dcl.fct]p6.
4491     if (!Param->isInvalidDecl() &&
4492         RequireCompleteType(Param->getLocation(), Param->getType(),
4493                                diag::err_typecheck_decl_incomplete_type)) {
4494       Param->setInvalidDecl();
4495       HasInvalidParm = true;
4496     }
4497
4498     // C99 6.9.1p5: If the declarator includes a parameter type list, the
4499     // declaration of each parameter shall include an identifier.
4500     if (CheckParameterNames &&
4501         Param->getIdentifier() == 0 &&
4502         !Param->isImplicit() &&
4503         !getLangOpts().CPlusPlus)
4504       Diag(Param->getLocation(), diag::err_parameter_name_omitted);
4505
4506     // C99 6.7.5.3p12:
4507     //   If the function declarator is not part of a definition of that
4508     //   function, parameters may have incomplete type and may use the [*]
4509     //   notation in their sequences of declarator specifiers to specify
4510     //   variable length array types.
4511     QualType PType = Param->getOriginalType();
4512     if (const ArrayType *AT = Context.getAsArrayType(PType)) {
4513       if (AT->getSizeModifier() == ArrayType::Star) {
4514         // FIXME: This diagnosic should point the the '[*]' if source-location
4515         // information is added for it.
4516         Diag(Param->getLocation(), diag::err_array_star_in_function_definition);
4517       }
4518     }
4519   }
4520
4521   return HasInvalidParm;
4522 }
4523
4524 /// CheckCastAlign - Implements -Wcast-align, which warns when a
4525 /// pointer cast increases the alignment requirements.
4526 void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) {
4527   // This is actually a lot of work to potentially be doing on every
4528   // cast; don't do it if we're ignoring -Wcast_align (as is the default).
4529   if (getDiagnostics().getDiagnosticLevel(diag::warn_cast_align,
4530                                           TRange.getBegin())
4531         == DiagnosticsEngine::Ignored)
4532     return;
4533
4534   // Ignore dependent types.
4535   if (T->isDependentType() || Op->getType()->isDependentType())
4536     return;
4537
4538   // Require that the destination be a pointer type.
4539   const PointerType *DestPtr = T->getAs<PointerType>();
4540   if (!DestPtr) return;
4541
4542   // If the destination has alignment 1, we're done.
4543   QualType DestPointee = DestPtr->getPointeeType();
4544   if (DestPointee->isIncompleteType()) return;
4545   CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee);
4546   if (DestAlign.isOne()) return;
4547
4548   // Require that the source be a pointer type.
4549   const PointerType *SrcPtr = Op->getType()->getAs<PointerType>();
4550   if (!SrcPtr) return;
4551   QualType SrcPointee = SrcPtr->getPointeeType();
4552
4553   // Whitelist casts from cv void*.  We already implicitly
4554   // whitelisted casts to cv void*, since they have alignment 1.
4555   // Also whitelist casts involving incomplete types, which implicitly
4556   // includes 'void'.
4557   if (SrcPointee->isIncompleteType()) return;
4558
4559   CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee);
4560   if (SrcAlign >= DestAlign) return;
4561
4562   Diag(TRange.getBegin(), diag::warn_cast_align)
4563     << Op->getType() << T
4564     << static_cast<unsigned>(SrcAlign.getQuantity())
4565     << static_cast<unsigned>(DestAlign.getQuantity())
4566     << TRange << Op->getSourceRange();
4567 }
4568
4569 static const Type* getElementType(const Expr *BaseExpr) {
4570   const Type* EltType = BaseExpr->getType().getTypePtr();
4571   if (EltType->isAnyPointerType())
4572     return EltType->getPointeeType().getTypePtr();
4573   else if (EltType->isArrayType())
4574     return EltType->getBaseElementTypeUnsafe();
4575   return EltType;
4576 }
4577
4578 /// \brief Check whether this array fits the idiom of a size-one tail padded
4579 /// array member of a struct.
4580 ///
4581 /// We avoid emitting out-of-bounds access warnings for such arrays as they are
4582 /// commonly used to emulate flexible arrays in C89 code.
4583 static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size,
4584                                     const NamedDecl *ND) {
4585   if (Size != 1 || !ND) return false;
4586
4587   const FieldDecl *FD = dyn_cast<FieldDecl>(ND);
4588   if (!FD) return false;
4589
4590   // Don't consider sizes resulting from macro expansions or template argument
4591   // substitution to form C89 tail-padded arrays.
4592   ConstantArrayTypeLoc TL =
4593     cast<ConstantArrayTypeLoc>(FD->getTypeSourceInfo()->getTypeLoc());
4594   const Expr *SizeExpr = dyn_cast<IntegerLiteral>(TL.getSizeExpr());
4595   if (!SizeExpr || SizeExpr->getExprLoc().isMacroID())
4596     return false;
4597
4598   const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext());
4599   if (!RD) return false;
4600   if (RD->isUnion()) return false;
4601   if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
4602     if (!CRD->isStandardLayout()) return false;
4603   }
4604
4605   // See if this is the last field decl in the record.
4606   const Decl *D = FD;
4607   while ((D = D->getNextDeclInContext()))
4608     if (isa<FieldDecl>(D))
4609       return false;
4610   return true;
4611 }
4612
4613 void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
4614                             const ArraySubscriptExpr *ASE,
4615                             bool AllowOnePastEnd, bool IndexNegated) {
4616   IndexExpr = IndexExpr->IgnoreParenImpCasts();
4617   if (IndexExpr->isValueDependent())
4618     return;
4619
4620   const Type *EffectiveType = getElementType(BaseExpr);
4621   BaseExpr = BaseExpr->IgnoreParenCasts();
4622   const ConstantArrayType *ArrayTy =
4623     Context.getAsConstantArrayType(BaseExpr->getType());
4624   if (!ArrayTy)
4625     return;
4626
4627   llvm::APSInt index;
4628   if (!IndexExpr->EvaluateAsInt(index, Context))
4629     return;
4630   if (IndexNegated)
4631     index = -index;
4632
4633   const NamedDecl *ND = NULL;
4634   if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
4635     ND = dyn_cast<NamedDecl>(DRE->getDecl());
4636   if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
4637     ND = dyn_cast<NamedDecl>(ME->getMemberDecl());
4638
4639   if (index.isUnsigned() || !index.isNegative()) {
4640     llvm::APInt size = ArrayTy->getSize();
4641     if (!size.isStrictlyPositive())
4642       return;
4643
4644     const Type* BaseType = getElementType(BaseExpr);
4645     if (BaseType != EffectiveType) {
4646       // Make sure we're comparing apples to apples when comparing index to size
4647       uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType);
4648       uint64_t array_typesize = Context.getTypeSize(BaseType);
4649       // Handle ptrarith_typesize being zero, such as when casting to void*
4650       if (!ptrarith_typesize) ptrarith_typesize = 1;
4651       if (ptrarith_typesize != array_typesize) {
4652         // There's a cast to a different size type involved
4653         uint64_t ratio = array_typesize / ptrarith_typesize;
4654         // TODO: Be smarter about handling cases where array_typesize is not a
4655         // multiple of ptrarith_typesize
4656         if (ptrarith_typesize * ratio == array_typesize)
4657           size *= llvm::APInt(size.getBitWidth(), ratio);
4658       }
4659     }
4660
4661     if (size.getBitWidth() > index.getBitWidth())
4662       index = index.zext(size.getBitWidth());
4663     else if (size.getBitWidth() < index.getBitWidth())
4664       size = size.zext(index.getBitWidth());
4665
4666     // For array subscripting the index must be less than size, but for pointer
4667     // arithmetic also allow the index (offset) to be equal to size since
4668     // computing the next address after the end of the array is legal and
4669     // commonly done e.g. in C++ iterators and range-based for loops.
4670     if (AllowOnePastEnd ? index.ule(size) : index.ult(size))
4671       return;
4672
4673     // Also don't warn for arrays of size 1 which are members of some
4674     // structure. These are often used to approximate flexible arrays in C89
4675     // code.
4676     if (IsTailPaddedMemberArray(*this, size, ND))
4677       return;
4678
4679     // Suppress the warning if the subscript expression (as identified by the
4680     // ']' location) and the index expression are both from macro expansions
4681     // within a system header.
4682     if (ASE) {
4683       SourceLocation RBracketLoc = SourceMgr.getSpellingLoc(
4684           ASE->getRBracketLoc());
4685       if (SourceMgr.isInSystemHeader(RBracketLoc)) {
4686         SourceLocation IndexLoc = SourceMgr.getSpellingLoc(
4687             IndexExpr->getLocStart());
4688         if (SourceMgr.isFromSameFile(RBracketLoc, IndexLoc))
4689           return;
4690       }
4691     }
4692
4693     unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds;
4694     if (ASE)
4695       DiagID = diag::warn_array_index_exceeds_bounds;
4696
4697     DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr,
4698                         PDiag(DiagID) << index.toString(10, true)
4699                           << size.toString(10, true)
4700                           << (unsigned)size.getLimitedValue(~0U)
4701                           << IndexExpr->getSourceRange());
4702   } else {
4703     unsigned DiagID = diag::warn_array_index_precedes_bounds;
4704     if (!ASE) {
4705       DiagID = diag::warn_ptr_arith_precedes_bounds;
4706       if (index.isNegative()) index = -index;
4707     }
4708
4709     DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr,
4710                         PDiag(DiagID) << index.toString(10, true)
4711                           << IndexExpr->getSourceRange());
4712   }
4713
4714   if (!ND) {
4715     // Try harder to find a NamedDecl to point at in the note.
4716     while (const ArraySubscriptExpr *ASE =
4717            dyn_cast<ArraySubscriptExpr>(BaseExpr))
4718       BaseExpr = ASE->getBase()->IgnoreParenCasts();
4719     if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
4720       ND = dyn_cast<NamedDecl>(DRE->getDecl());
4721     if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
4722       ND = dyn_cast<NamedDecl>(ME->getMemberDecl());
4723   }
4724
4725   if (ND)
4726     DiagRuntimeBehavior(ND->getLocStart(), BaseExpr,
4727                         PDiag(diag::note_array_index_out_of_bounds)
4728                           << ND->getDeclName());
4729 }
4730
4731 void Sema::CheckArrayAccess(const Expr *expr) {
4732   int AllowOnePastEnd = 0;
4733   while (expr) {
4734     expr = expr->IgnoreParenImpCasts();
4735     switch (expr->getStmtClass()) {
4736       case Stmt::ArraySubscriptExprClass: {
4737         const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr);
4738         CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE,
4739                          AllowOnePastEnd > 0);
4740         return;
4741       }
4742       case Stmt::UnaryOperatorClass: {
4743         // Only unwrap the * and & unary operators
4744         const UnaryOperator *UO = cast<UnaryOperator>(expr);
4745         expr = UO->getSubExpr();
4746         switch (UO->getOpcode()) {
4747           case UO_AddrOf:
4748             AllowOnePastEnd++;
4749             break;
4750           case UO_Deref:
4751             AllowOnePastEnd--;
4752             break;
4753           default:
4754             return;
4755         }
4756         break;
4757       }
4758       case Stmt::ConditionalOperatorClass: {
4759         const ConditionalOperator *cond = cast<ConditionalOperator>(expr);
4760         if (const Expr *lhs = cond->getLHS())
4761           CheckArrayAccess(lhs);
4762         if (const Expr *rhs = cond->getRHS())
4763           CheckArrayAccess(rhs);
4764         return;
4765       }
4766       default:
4767         return;
4768     }
4769   }
4770 }
4771
4772 //===--- CHECK: Objective-C retain cycles ----------------------------------//
4773
4774 namespace {
4775   struct RetainCycleOwner {
4776     RetainCycleOwner() : Variable(0), Indirect(false) {}
4777     VarDecl *Variable;
4778     SourceRange Range;
4779     SourceLocation Loc;
4780     bool Indirect;
4781
4782     void setLocsFrom(Expr *e) {
4783       Loc = e->getExprLoc();
4784       Range = e->getSourceRange();
4785     }
4786   };
4787 }
4788
4789 /// Consider whether capturing the given variable can possibly lead to
4790 /// a retain cycle.
4791 static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) {
4792   // In ARC, it's captured strongly iff the variable has __strong
4793   // lifetime.  In MRR, it's captured strongly if the variable is
4794   // __block and has an appropriate type.
4795   if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
4796     return false;
4797
4798   owner.Variable = var;
4799   owner.setLocsFrom(ref);
4800   return true;
4801 }
4802
4803 static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) {
4804   while (true) {
4805     e = e->IgnoreParens();
4806     if (CastExpr *cast = dyn_cast<CastExpr>(e)) {
4807       switch (cast->getCastKind()) {
4808       case CK_BitCast:
4809       case CK_LValueBitCast:
4810       case CK_LValueToRValue:
4811       case CK_ARCReclaimReturnedObject:
4812         e = cast->getSubExpr();
4813         continue;
4814
4815       default:
4816         return false;
4817       }
4818     }
4819
4820     if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) {
4821       ObjCIvarDecl *ivar = ref->getDecl();
4822       if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
4823         return false;
4824
4825       // Try to find a retain cycle in the base.
4826       if (!findRetainCycleOwner(S, ref->getBase(), owner))
4827         return false;
4828
4829       if (ref->isFreeIvar()) owner.setLocsFrom(ref);
4830       owner.Indirect = true;
4831       return true;
4832     }
4833
4834     if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) {
4835       VarDecl *var = dyn_cast<VarDecl>(ref->getDecl());
4836       if (!var) return false;
4837       return considerVariable(var, ref, owner);
4838     }
4839
4840     if (MemberExpr *member = dyn_cast<MemberExpr>(e)) {
4841       if (member->isArrow()) return false;
4842
4843       // Don't count this as an indirect ownership.
4844       e = member->getBase();
4845       continue;
4846     }
4847
4848     if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) {
4849       // Only pay attention to pseudo-objects on property references.
4850       ObjCPropertyRefExpr *pre
4851         = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm()
4852                                               ->IgnoreParens());
4853       if (!pre) return false;
4854       if (pre->isImplicitProperty()) return false;
4855       ObjCPropertyDecl *property = pre->getExplicitProperty();
4856       if (!property->isRetaining() &&
4857           !(property->getPropertyIvarDecl() &&
4858             property->getPropertyIvarDecl()->getType()
4859               .getObjCLifetime() == Qualifiers::OCL_Strong))
4860           return false;
4861
4862       owner.Indirect = true;
4863       if (pre->isSuperReceiver()) {
4864         owner.Variable = S.getCurMethodDecl()->getSelfDecl();
4865         if (!owner.Variable)
4866           return false;
4867         owner.Loc = pre->getLocation();
4868         owner.Range = pre->getSourceRange();
4869         return true;
4870       }
4871       e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase())
4872                               ->getSourceExpr());
4873       continue;
4874     }
4875
4876     // Array ivars?
4877
4878     return false;
4879   }
4880 }
4881
4882 namespace {
4883   struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> {
4884     FindCaptureVisitor(ASTContext &Context, VarDecl *variable)
4885       : EvaluatedExprVisitor<FindCaptureVisitor>(Context),
4886         Variable(variable), Capturer(0) {}
4887
4888     VarDecl *Variable;
4889     Expr *Capturer;
4890
4891     void VisitDeclRefExpr(DeclRefExpr *ref) {
4892       if (ref->getDecl() == Variable && !Capturer)
4893         Capturer = ref;
4894     }
4895
4896     void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) {
4897       if (Capturer) return;
4898       Visit(ref->getBase());
4899       if (Capturer && ref->isFreeIvar())
4900         Capturer = ref;
4901     }
4902
4903     void VisitBlockExpr(BlockExpr *block) {
4904       // Look inside nested blocks 
4905       if (block->getBlockDecl()->capturesVariable(Variable))
4906         Visit(block->getBlockDecl()->getBody());
4907     }
4908   };
4909 }
4910
4911 /// Check whether the given argument is a block which captures a
4912 /// variable.
4913 static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) {
4914   assert(owner.Variable && owner.Loc.isValid());
4915
4916   e = e->IgnoreParenCasts();
4917   BlockExpr *block = dyn_cast<BlockExpr>(e);
4918   if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable))
4919     return 0;
4920
4921   FindCaptureVisitor visitor(S.Context, owner.Variable);
4922   visitor.Visit(block->getBlockDecl()->getBody());
4923   return visitor.Capturer;
4924 }
4925
4926 static void diagnoseRetainCycle(Sema &S, Expr *capturer,
4927                                 RetainCycleOwner &owner) {
4928   assert(capturer);
4929   assert(owner.Variable && owner.Loc.isValid());
4930
4931   S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle)
4932     << owner.Variable << capturer->getSourceRange();
4933   S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner)
4934     << owner.Indirect << owner.Range;
4935 }
4936
4937 /// Check for a keyword selector that starts with the word 'add' or
4938 /// 'set'.
4939 static bool isSetterLikeSelector(Selector sel) {
4940   if (sel.isUnarySelector()) return false;
4941
4942   StringRef str = sel.getNameForSlot(0);
4943   while (!str.empty() && str.front() == '_') str = str.substr(1);
4944   if (str.startswith("set"))
4945     str = str.substr(3);
4946   else if (str.startswith("add")) {
4947     // Specially whitelist 'addOperationWithBlock:'.
4948     if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock"))
4949       return false;
4950     str = str.substr(3);
4951   }
4952   else
4953     return false;
4954
4955   if (str.empty()) return true;
4956   return !islower(str.front());
4957 }
4958
4959 /// Check a message send to see if it's likely to cause a retain cycle.
4960 void Sema::checkRetainCycles(ObjCMessageExpr *msg) {
4961   // Only check instance methods whose selector looks like a setter.
4962   if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector()))
4963     return;
4964
4965   // Try to find a variable that the receiver is strongly owned by.
4966   RetainCycleOwner owner;
4967   if (msg->getReceiverKind() == ObjCMessageExpr::Instance) {
4968     if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner))
4969       return;
4970   } else {
4971     assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance);
4972     owner.Variable = getCurMethodDecl()->getSelfDecl();
4973     owner.Loc = msg->getSuperLoc();
4974     owner.Range = msg->getSuperLoc();
4975   }
4976
4977   // Check whether the receiver is captured by any of the arguments.
4978   for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i)
4979     if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner))
4980       return diagnoseRetainCycle(*this, capturer, owner);
4981 }
4982
4983 /// Check a property assign to see if it's likely to cause a retain cycle.
4984 void Sema::checkRetainCycles(Expr *receiver, Expr *argument) {
4985   RetainCycleOwner owner;
4986   if (!findRetainCycleOwner(*this, receiver, owner))
4987     return;
4988
4989   if (Expr *capturer = findCapturingExpr(*this, argument, owner))
4990     diagnoseRetainCycle(*this, capturer, owner);
4991 }
4992
4993 bool Sema::checkUnsafeAssigns(SourceLocation Loc,
4994                               QualType LHS, Expr *RHS) {
4995   Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime();
4996   if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone)
4997     return false;
4998   // strip off any implicit cast added to get to the one arc-specific
4999   while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
5000     if (cast->getCastKind() == CK_ARCConsumeObject) {
5001       Diag(Loc, diag::warn_arc_retained_assign)
5002         << (LT == Qualifiers::OCL_ExplicitNone) 
5003         << RHS->getSourceRange();
5004       return true;
5005     }
5006     RHS = cast->getSubExpr();
5007   }
5008   return false;
5009 }
5010
5011 void Sema::checkUnsafeExprAssigns(SourceLocation Loc,
5012                               Expr *LHS, Expr *RHS) {
5013   QualType LHSType;
5014   // PropertyRef on LHS type need be directly obtained from
5015   // its declaration as it has a PsuedoType.
5016   ObjCPropertyRefExpr *PRE
5017     = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens());
5018   if (PRE && !PRE->isImplicitProperty()) {
5019     const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
5020     if (PD)
5021       LHSType = PD->getType();
5022   }
5023   
5024   if (LHSType.isNull())
5025     LHSType = LHS->getType();
5026   if (checkUnsafeAssigns(Loc, LHSType, RHS))
5027     return;
5028   Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime();
5029   // FIXME. Check for other life times.
5030   if (LT != Qualifiers::OCL_None)
5031     return;
5032   
5033   if (PRE) {
5034     if (PRE->isImplicitProperty())
5035       return;
5036     const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
5037     if (!PD)
5038       return;
5039     
5040     unsigned Attributes = PD->getPropertyAttributes();
5041     if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) {
5042       // when 'assign' attribute was not explicitly specified
5043       // by user, ignore it and rely on property type itself
5044       // for lifetime info.
5045       unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten();
5046       if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) &&
5047           LHSType->isObjCRetainableType())
5048         return;
5049         
5050       while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
5051         if (cast->getCastKind() == CK_ARCConsumeObject) {
5052           Diag(Loc, diag::warn_arc_retained_property_assign)
5053           << RHS->getSourceRange();
5054           return;
5055         }
5056         RHS = cast->getSubExpr();
5057       }
5058     }
5059   }
5060 }
5061
5062 //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===//
5063
5064 namespace {
5065 bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr,
5066                                  SourceLocation StmtLoc,
5067                                  const NullStmt *Body) {
5068   // Do not warn if the body is a macro that expands to nothing, e.g:
5069   //
5070   // #define CALL(x)
5071   // if (condition)
5072   //   CALL(0);
5073   //
5074   if (Body->hasLeadingEmptyMacro())
5075     return false;
5076
5077   // Get line numbers of statement and body.
5078   bool StmtLineInvalid;
5079   unsigned StmtLine = SourceMgr.getSpellingLineNumber(StmtLoc,
5080                                                       &StmtLineInvalid);
5081   if (StmtLineInvalid)
5082     return false;
5083
5084   bool BodyLineInvalid;
5085   unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(),
5086                                                       &BodyLineInvalid);
5087   if (BodyLineInvalid)
5088     return false;
5089
5090   // Warn if null statement and body are on the same line.
5091   if (StmtLine != BodyLine)
5092     return false;
5093
5094   return true;
5095 }
5096 } // Unnamed namespace
5097
5098 void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc,
5099                                  const Stmt *Body,
5100                                  unsigned DiagID) {
5101   // Since this is a syntactic check, don't emit diagnostic for template
5102   // instantiations, this just adds noise.
5103   if (CurrentInstantiationScope)
5104     return;
5105
5106   // The body should be a null statement.
5107   const NullStmt *NBody = dyn_cast<NullStmt>(Body);
5108   if (!NBody)
5109     return;
5110
5111   // Do the usual checks.
5112   if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody))
5113     return;
5114
5115   Diag(NBody->getSemiLoc(), DiagID);
5116   Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line);
5117 }
5118
5119 void Sema::DiagnoseEmptyLoopBody(const Stmt *S,
5120                                  const Stmt *PossibleBody) {
5121   assert(!CurrentInstantiationScope); // Ensured by caller
5122
5123   SourceLocation StmtLoc;
5124   const Stmt *Body;
5125   unsigned DiagID;
5126   if (const ForStmt *FS = dyn_cast<ForStmt>(S)) {
5127     StmtLoc = FS->getRParenLoc();
5128     Body = FS->getBody();
5129     DiagID = diag::warn_empty_for_body;
5130   } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) {
5131     StmtLoc = WS->getCond()->getSourceRange().getEnd();
5132     Body = WS->getBody();
5133     DiagID = diag::warn_empty_while_body;
5134   } else
5135     return; // Neither `for' nor `while'.
5136
5137   // The body should be a null statement.
5138   const NullStmt *NBody = dyn_cast<NullStmt>(Body);
5139   if (!NBody)
5140     return;
5141
5142   // Skip expensive checks if diagnostic is disabled.
5143   if (Diags.getDiagnosticLevel(DiagID, NBody->getSemiLoc()) ==
5144           DiagnosticsEngine::Ignored)
5145     return;
5146
5147   // Do the usual checks.
5148   if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody))
5149     return;
5150
5151   // `for(...);' and `while(...);' are popular idioms, so in order to keep
5152   // noise level low, emit diagnostics only if for/while is followed by a
5153   // CompoundStmt, e.g.:
5154   //    for (int i = 0; i < n; i++);
5155   //    {
5156   //      a(i);
5157   //    }
5158   // or if for/while is followed by a statement with more indentation
5159   // than for/while itself:
5160   //    for (int i = 0; i < n; i++);
5161   //      a(i);
5162   bool ProbableTypo = isa<CompoundStmt>(PossibleBody);
5163   if (!ProbableTypo) {
5164     bool BodyColInvalid;
5165     unsigned BodyCol = SourceMgr.getPresumedColumnNumber(
5166                              PossibleBody->getLocStart(),
5167                              &BodyColInvalid);
5168     if (BodyColInvalid)
5169       return;
5170
5171     bool StmtColInvalid;
5172     unsigned StmtCol = SourceMgr.getPresumedColumnNumber(
5173                              S->getLocStart(),
5174                              &StmtColInvalid);
5175     if (StmtColInvalid)
5176       return;
5177
5178     if (BodyCol > StmtCol)
5179       ProbableTypo = true;
5180   }
5181
5182   if (ProbableTypo) {
5183     Diag(NBody->getSemiLoc(), DiagID);
5184     Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line);
5185   }
5186 }