SemaChecking.cpp revision e3fbbe9a486c4c8709726d8c79af33140eb02e53
1//===--- SemaChecking.cpp - Extra Semantic Checking -----------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file implements extra semantic analysis beyond what is enforced
11//  by the C type system.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Sema/SemaInternal.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/CharUnits.h"
18#include "clang/AST/DeclCXX.h"
19#include "clang/AST/DeclObjC.h"
20#include "clang/AST/EvaluatedExprVisitor.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/ExprCXX.h"
23#include "clang/AST/ExprObjC.h"
24#include "clang/AST/StmtCXX.h"
25#include "clang/AST/StmtObjC.h"
26#include "clang/Analysis/Analyses/FormatString.h"
27#include "clang/Basic/CharInfo.h"
28#include "clang/Basic/TargetBuiltins.h"
29#include "clang/Basic/TargetInfo.h"
30#include "clang/Lex/Preprocessor.h"
31#include "clang/Sema/Initialization.h"
32#include "clang/Sema/Lookup.h"
33#include "clang/Sema/ScopeInfo.h"
34#include "clang/Sema/Sema.h"
35#include "llvm/ADT/BitVector.h"
36#include "llvm/ADT/STLExtras.h"
37#include "llvm/ADT/SmallString.h"
38#include "llvm/Support/ConvertUTF.h"
39#include "llvm/Support/raw_ostream.h"
40#include <limits>
41using namespace clang;
42using namespace sema;
43
44SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL,
45                                                    unsigned ByteNo) const {
46  return SL->getLocationOfByte(ByteNo, PP.getSourceManager(),
47                               PP.getLangOpts(), PP.getTargetInfo());
48}
49
50/// Checks that a call expression's argument count is the desired number.
51/// This is useful when doing custom type-checking.  Returns true on error.
52static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) {
53  unsigned argCount = call->getNumArgs();
54  if (argCount == desiredArgCount) return false;
55
56  if (argCount < desiredArgCount)
57    return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args)
58        << 0 /*function call*/ << desiredArgCount << argCount
59        << call->getSourceRange();
60
61  // Highlight all the excess arguments.
62  SourceRange range(call->getArg(desiredArgCount)->getLocStart(),
63                    call->getArg(argCount - 1)->getLocEnd());
64
65  return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args)
66    << 0 /*function call*/ << desiredArgCount << argCount
67    << call->getArg(1)->getSourceRange();
68}
69
70/// Check that the first argument to __builtin_annotation is an integer
71/// and the second argument is a non-wide string literal.
72static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) {
73  if (checkArgCount(S, TheCall, 2))
74    return true;
75
76  // First argument should be an integer.
77  Expr *ValArg = TheCall->getArg(0);
78  QualType Ty = ValArg->getType();
79  if (!Ty->isIntegerType()) {
80    S.Diag(ValArg->getLocStart(), diag::err_builtin_annotation_first_arg)
81      << ValArg->getSourceRange();
82    return true;
83  }
84
85  // Second argument should be a constant string.
86  Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts();
87  StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg);
88  if (!Literal || !Literal->isAscii()) {
89    S.Diag(StrArg->getLocStart(), diag::err_builtin_annotation_second_arg)
90      << StrArg->getSourceRange();
91    return true;
92  }
93
94  TheCall->setType(Ty);
95  return false;
96}
97
98/// Check that the argument to __builtin_addressof is a glvalue, and set the
99/// result type to the corresponding pointer type.
100static bool SemaBuiltinAddressof(Sema &S, CallExpr *TheCall) {
101  if (checkArgCount(S, TheCall, 1))
102    return true;
103
104  ExprResult Arg(S.Owned(TheCall->getArg(0)));
105  QualType ResultType = S.CheckAddressOfOperand(Arg, TheCall->getLocStart());
106  if (ResultType.isNull())
107    return true;
108
109  TheCall->setArg(0, Arg.take());
110  TheCall->setType(ResultType);
111  return false;
112}
113
114ExprResult
115Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
116  ExprResult TheCallResult(Owned(TheCall));
117
118  // Find out if any arguments are required to be integer constant expressions.
119  unsigned ICEArguments = 0;
120  ASTContext::GetBuiltinTypeError Error;
121  Context.GetBuiltinType(BuiltinID, Error, &ICEArguments);
122  if (Error != ASTContext::GE_None)
123    ICEArguments = 0;  // Don't diagnose previously diagnosed errors.
124
125  // If any arguments are required to be ICE's, check and diagnose.
126  for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) {
127    // Skip arguments not required to be ICE's.
128    if ((ICEArguments & (1 << ArgNo)) == 0) continue;
129
130    llvm::APSInt Result;
131    if (SemaBuiltinConstantArg(TheCall, ArgNo, Result))
132      return true;
133    ICEArguments &= ~(1 << ArgNo);
134  }
135
136  switch (BuiltinID) {
137  case Builtin::BI__builtin___CFStringMakeConstantString:
138    assert(TheCall->getNumArgs() == 1 &&
139           "Wrong # arguments to builtin CFStringMakeConstantString");
140    if (CheckObjCString(TheCall->getArg(0)))
141      return ExprError();
142    break;
143  case Builtin::BI__builtin_stdarg_start:
144  case Builtin::BI__builtin_va_start:
145    if (SemaBuiltinVAStart(TheCall))
146      return ExprError();
147    break;
148  case Builtin::BI__builtin_isgreater:
149  case Builtin::BI__builtin_isgreaterequal:
150  case Builtin::BI__builtin_isless:
151  case Builtin::BI__builtin_islessequal:
152  case Builtin::BI__builtin_islessgreater:
153  case Builtin::BI__builtin_isunordered:
154    if (SemaBuiltinUnorderedCompare(TheCall))
155      return ExprError();
156    break;
157  case Builtin::BI__builtin_fpclassify:
158    if (SemaBuiltinFPClassification(TheCall, 6))
159      return ExprError();
160    break;
161  case Builtin::BI__builtin_isfinite:
162  case Builtin::BI__builtin_isinf:
163  case Builtin::BI__builtin_isinf_sign:
164  case Builtin::BI__builtin_isnan:
165  case Builtin::BI__builtin_isnormal:
166    if (SemaBuiltinFPClassification(TheCall, 1))
167      return ExprError();
168    break;
169  case Builtin::BI__builtin_shufflevector:
170    return SemaBuiltinShuffleVector(TheCall);
171    // TheCall will be freed by the smart pointer here, but that's fine, since
172    // SemaBuiltinShuffleVector guts it, but then doesn't release it.
173  case Builtin::BI__builtin_prefetch:
174    if (SemaBuiltinPrefetch(TheCall))
175      return ExprError();
176    break;
177  case Builtin::BI__builtin_object_size:
178    if (SemaBuiltinObjectSize(TheCall))
179      return ExprError();
180    break;
181  case Builtin::BI__builtin_longjmp:
182    if (SemaBuiltinLongjmp(TheCall))
183      return ExprError();
184    break;
185
186  case Builtin::BI__builtin_classify_type:
187    if (checkArgCount(*this, TheCall, 1)) return true;
188    TheCall->setType(Context.IntTy);
189    break;
190  case Builtin::BI__builtin_constant_p:
191    if (checkArgCount(*this, TheCall, 1)) return true;
192    TheCall->setType(Context.IntTy);
193    break;
194  case Builtin::BI__sync_fetch_and_add:
195  case Builtin::BI__sync_fetch_and_add_1:
196  case Builtin::BI__sync_fetch_and_add_2:
197  case Builtin::BI__sync_fetch_and_add_4:
198  case Builtin::BI__sync_fetch_and_add_8:
199  case Builtin::BI__sync_fetch_and_add_16:
200  case Builtin::BI__sync_fetch_and_sub:
201  case Builtin::BI__sync_fetch_and_sub_1:
202  case Builtin::BI__sync_fetch_and_sub_2:
203  case Builtin::BI__sync_fetch_and_sub_4:
204  case Builtin::BI__sync_fetch_and_sub_8:
205  case Builtin::BI__sync_fetch_and_sub_16:
206  case Builtin::BI__sync_fetch_and_or:
207  case Builtin::BI__sync_fetch_and_or_1:
208  case Builtin::BI__sync_fetch_and_or_2:
209  case Builtin::BI__sync_fetch_and_or_4:
210  case Builtin::BI__sync_fetch_and_or_8:
211  case Builtin::BI__sync_fetch_and_or_16:
212  case Builtin::BI__sync_fetch_and_and:
213  case Builtin::BI__sync_fetch_and_and_1:
214  case Builtin::BI__sync_fetch_and_and_2:
215  case Builtin::BI__sync_fetch_and_and_4:
216  case Builtin::BI__sync_fetch_and_and_8:
217  case Builtin::BI__sync_fetch_and_and_16:
218  case Builtin::BI__sync_fetch_and_xor:
219  case Builtin::BI__sync_fetch_and_xor_1:
220  case Builtin::BI__sync_fetch_and_xor_2:
221  case Builtin::BI__sync_fetch_and_xor_4:
222  case Builtin::BI__sync_fetch_and_xor_8:
223  case Builtin::BI__sync_fetch_and_xor_16:
224  case Builtin::BI__sync_add_and_fetch:
225  case Builtin::BI__sync_add_and_fetch_1:
226  case Builtin::BI__sync_add_and_fetch_2:
227  case Builtin::BI__sync_add_and_fetch_4:
228  case Builtin::BI__sync_add_and_fetch_8:
229  case Builtin::BI__sync_add_and_fetch_16:
230  case Builtin::BI__sync_sub_and_fetch:
231  case Builtin::BI__sync_sub_and_fetch_1:
232  case Builtin::BI__sync_sub_and_fetch_2:
233  case Builtin::BI__sync_sub_and_fetch_4:
234  case Builtin::BI__sync_sub_and_fetch_8:
235  case Builtin::BI__sync_sub_and_fetch_16:
236  case Builtin::BI__sync_and_and_fetch:
237  case Builtin::BI__sync_and_and_fetch_1:
238  case Builtin::BI__sync_and_and_fetch_2:
239  case Builtin::BI__sync_and_and_fetch_4:
240  case Builtin::BI__sync_and_and_fetch_8:
241  case Builtin::BI__sync_and_and_fetch_16:
242  case Builtin::BI__sync_or_and_fetch:
243  case Builtin::BI__sync_or_and_fetch_1:
244  case Builtin::BI__sync_or_and_fetch_2:
245  case Builtin::BI__sync_or_and_fetch_4:
246  case Builtin::BI__sync_or_and_fetch_8:
247  case Builtin::BI__sync_or_and_fetch_16:
248  case Builtin::BI__sync_xor_and_fetch:
249  case Builtin::BI__sync_xor_and_fetch_1:
250  case Builtin::BI__sync_xor_and_fetch_2:
251  case Builtin::BI__sync_xor_and_fetch_4:
252  case Builtin::BI__sync_xor_and_fetch_8:
253  case Builtin::BI__sync_xor_and_fetch_16:
254  case Builtin::BI__sync_val_compare_and_swap:
255  case Builtin::BI__sync_val_compare_and_swap_1:
256  case Builtin::BI__sync_val_compare_and_swap_2:
257  case Builtin::BI__sync_val_compare_and_swap_4:
258  case Builtin::BI__sync_val_compare_and_swap_8:
259  case Builtin::BI__sync_val_compare_and_swap_16:
260  case Builtin::BI__sync_bool_compare_and_swap:
261  case Builtin::BI__sync_bool_compare_and_swap_1:
262  case Builtin::BI__sync_bool_compare_and_swap_2:
263  case Builtin::BI__sync_bool_compare_and_swap_4:
264  case Builtin::BI__sync_bool_compare_and_swap_8:
265  case Builtin::BI__sync_bool_compare_and_swap_16:
266  case Builtin::BI__sync_lock_test_and_set:
267  case Builtin::BI__sync_lock_test_and_set_1:
268  case Builtin::BI__sync_lock_test_and_set_2:
269  case Builtin::BI__sync_lock_test_and_set_4:
270  case Builtin::BI__sync_lock_test_and_set_8:
271  case Builtin::BI__sync_lock_test_and_set_16:
272  case Builtin::BI__sync_lock_release:
273  case Builtin::BI__sync_lock_release_1:
274  case Builtin::BI__sync_lock_release_2:
275  case Builtin::BI__sync_lock_release_4:
276  case Builtin::BI__sync_lock_release_8:
277  case Builtin::BI__sync_lock_release_16:
278  case Builtin::BI__sync_swap:
279  case Builtin::BI__sync_swap_1:
280  case Builtin::BI__sync_swap_2:
281  case Builtin::BI__sync_swap_4:
282  case Builtin::BI__sync_swap_8:
283  case Builtin::BI__sync_swap_16:
284    return SemaBuiltinAtomicOverloaded(TheCallResult);
285#define BUILTIN(ID, TYPE, ATTRS)
286#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
287  case Builtin::BI##ID: \
288    return SemaAtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID);
289#include "clang/Basic/Builtins.def"
290  case Builtin::BI__builtin_annotation:
291    if (SemaBuiltinAnnotation(*this, TheCall))
292      return ExprError();
293    break;
294  case Builtin::BI__builtin_addressof:
295    if (SemaBuiltinAddressof(*this, TheCall))
296      return ExprError();
297    break;
298  }
299
300  // Since the target specific builtins for each arch overlap, only check those
301  // of the arch we are compiling for.
302  if (BuiltinID >= Builtin::FirstTSBuiltin) {
303    switch (Context.getTargetInfo().getTriple().getArch()) {
304      case llvm::Triple::arm:
305      case llvm::Triple::thumb:
306        if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall))
307          return ExprError();
308        break;
309      case llvm::Triple::mips:
310      case llvm::Triple::mipsel:
311      case llvm::Triple::mips64:
312      case llvm::Triple::mips64el:
313        if (CheckMipsBuiltinFunctionCall(BuiltinID, TheCall))
314          return ExprError();
315        break;
316      default:
317        break;
318    }
319  }
320
321  return TheCallResult;
322}
323
324// Get the valid immediate range for the specified NEON type code.
325static unsigned RFT(unsigned t, bool shift = false) {
326  NeonTypeFlags Type(t);
327  int IsQuad = Type.isQuad();
328  switch (Type.getEltType()) {
329  case NeonTypeFlags::Int8:
330  case NeonTypeFlags::Poly8:
331    return shift ? 7 : (8 << IsQuad) - 1;
332  case NeonTypeFlags::Int16:
333  case NeonTypeFlags::Poly16:
334    return shift ? 15 : (4 << IsQuad) - 1;
335  case NeonTypeFlags::Int32:
336    return shift ? 31 : (2 << IsQuad) - 1;
337  case NeonTypeFlags::Int64:
338    return shift ? 63 : (1 << IsQuad) - 1;
339  case NeonTypeFlags::Float16:
340    assert(!shift && "cannot shift float types!");
341    return (4 << IsQuad) - 1;
342  case NeonTypeFlags::Float32:
343    assert(!shift && "cannot shift float types!");
344    return (2 << IsQuad) - 1;
345  }
346  llvm_unreachable("Invalid NeonTypeFlag!");
347}
348
349/// getNeonEltType - Return the QualType corresponding to the elements of
350/// the vector type specified by the NeonTypeFlags.  This is used to check
351/// the pointer arguments for Neon load/store intrinsics.
352static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context) {
353  switch (Flags.getEltType()) {
354  case NeonTypeFlags::Int8:
355    return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy;
356  case NeonTypeFlags::Int16:
357    return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy;
358  case NeonTypeFlags::Int32:
359    return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy;
360  case NeonTypeFlags::Int64:
361    return Flags.isUnsigned() ? Context.UnsignedLongLongTy : Context.LongLongTy;
362  case NeonTypeFlags::Poly8:
363    return Context.SignedCharTy;
364  case NeonTypeFlags::Poly16:
365    return Context.ShortTy;
366  case NeonTypeFlags::Float16:
367    return Context.UnsignedShortTy;
368  case NeonTypeFlags::Float32:
369    return Context.FloatTy;
370  }
371  llvm_unreachable("Invalid NeonTypeFlag!");
372}
373
374bool Sema::CheckARMBuiltinExclusiveCall(unsigned BuiltinID, CallExpr *TheCall) {
375  assert((BuiltinID == ARM::BI__builtin_arm_ldrex ||
376          BuiltinID == ARM::BI__builtin_arm_strex) &&
377         "unexpected ARM builtin");
378  bool IsLdrex = BuiltinID == ARM::BI__builtin_arm_ldrex;
379
380  DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
381
382  // Ensure that we have the proper number of arguments.
383  if (checkArgCount(*this, TheCall, IsLdrex ? 1 : 2))
384    return true;
385
386  // Inspect the pointer argument of the atomic builtin.  This should always be
387  // a pointer type, whose element is an integral scalar or pointer type.
388  // Because it is a pointer type, we don't have to worry about any implicit
389  // casts here.
390  Expr *PointerArg = TheCall->getArg(IsLdrex ? 0 : 1);
391  ExprResult PointerArgRes = DefaultFunctionArrayLvalueConversion(PointerArg);
392  if (PointerArgRes.isInvalid())
393    return true;
394  PointerArg = PointerArgRes.take();
395
396  const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>();
397  if (!pointerType) {
398    Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
399      << PointerArg->getType() << PointerArg->getSourceRange();
400    return true;
401  }
402
403  // ldrex takes a "const volatile T*" and strex takes a "volatile T*". Our next
404  // task is to insert the appropriate casts into the AST. First work out just
405  // what the appropriate type is.
406  QualType ValType = pointerType->getPointeeType();
407  QualType AddrType = ValType.getUnqualifiedType().withVolatile();
408  if (IsLdrex)
409    AddrType.addConst();
410
411  // Issue a warning if the cast is dodgy.
412  CastKind CastNeeded = CK_NoOp;
413  if (!AddrType.isAtLeastAsQualifiedAs(ValType)) {
414    CastNeeded = CK_BitCast;
415    Diag(DRE->getLocStart(), diag::ext_typecheck_convert_discards_qualifiers)
416      << PointerArg->getType()
417      << Context.getPointerType(AddrType)
418      << AA_Passing << PointerArg->getSourceRange();
419  }
420
421  // Finally, do the cast and replace the argument with the corrected version.
422  AddrType = Context.getPointerType(AddrType);
423  PointerArgRes = ImpCastExprToType(PointerArg, AddrType, CastNeeded);
424  if (PointerArgRes.isInvalid())
425    return true;
426  PointerArg = PointerArgRes.take();
427
428  TheCall->setArg(IsLdrex ? 0 : 1, PointerArg);
429
430  // In general, we allow ints, floats and pointers to be loaded and stored.
431  if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
432      !ValType->isBlockPointerType() && !ValType->isFloatingType()) {
433    Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intfltptr)
434      << PointerArg->getType() << PointerArg->getSourceRange();
435    return true;
436  }
437
438  // But ARM doesn't have instructions to deal with 128-bit versions.
439  if (Context.getTypeSize(ValType) > 64) {
440    Diag(DRE->getLocStart(), diag::err_atomic_exclusive_builtin_pointer_size)
441      << PointerArg->getType() << PointerArg->getSourceRange();
442    return true;
443  }
444
445  switch (ValType.getObjCLifetime()) {
446  case Qualifiers::OCL_None:
447  case Qualifiers::OCL_ExplicitNone:
448    // okay
449    break;
450
451  case Qualifiers::OCL_Weak:
452  case Qualifiers::OCL_Strong:
453  case Qualifiers::OCL_Autoreleasing:
454    Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
455      << ValType << PointerArg->getSourceRange();
456    return true;
457  }
458
459
460  if (IsLdrex) {
461    TheCall->setType(ValType);
462    return false;
463  }
464
465  // Initialize the argument to be stored.
466  ExprResult ValArg = TheCall->getArg(0);
467  InitializedEntity Entity = InitializedEntity::InitializeParameter(
468      Context, ValType, /*consume*/ false);
469  ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg);
470  if (ValArg.isInvalid())
471    return true;
472
473  TheCall->setArg(0, ValArg.get());
474  return false;
475}
476
477bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
478  llvm::APSInt Result;
479
480  if (BuiltinID == ARM::BI__builtin_arm_ldrex ||
481      BuiltinID == ARM::BI__builtin_arm_strex) {
482    return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall);
483  }
484
485  uint64_t mask = 0;
486  unsigned TV = 0;
487  int PtrArgNum = -1;
488  bool HasConstPtr = false;
489  switch (BuiltinID) {
490#define GET_NEON_OVERLOAD_CHECK
491#include "clang/Basic/arm_neon.inc"
492#undef GET_NEON_OVERLOAD_CHECK
493  }
494
495  // For NEON intrinsics which are overloaded on vector element type, validate
496  // the immediate which specifies which variant to emit.
497  unsigned ImmArg = TheCall->getNumArgs()-1;
498  if (mask) {
499    if (SemaBuiltinConstantArg(TheCall, ImmArg, Result))
500      return true;
501
502    TV = Result.getLimitedValue(64);
503    if ((TV > 63) || (mask & (1ULL << TV)) == 0)
504      return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code)
505        << TheCall->getArg(ImmArg)->getSourceRange();
506  }
507
508  if (PtrArgNum >= 0) {
509    // Check that pointer arguments have the specified type.
510    Expr *Arg = TheCall->getArg(PtrArgNum);
511    if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg))
512      Arg = ICE->getSubExpr();
513    ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg);
514    QualType RHSTy = RHS.get()->getType();
515    QualType EltTy = getNeonEltType(NeonTypeFlags(TV), Context);
516    if (HasConstPtr)
517      EltTy = EltTy.withConst();
518    QualType LHSTy = Context.getPointerType(EltTy);
519    AssignConvertType ConvTy;
520    ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS);
521    if (RHS.isInvalid())
522      return true;
523    if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), LHSTy, RHSTy,
524                                 RHS.get(), AA_Assigning))
525      return true;
526  }
527
528  // For NEON intrinsics which take an immediate value as part of the
529  // instruction, range check them here.
530  unsigned i = 0, l = 0, u = 0;
531  switch (BuiltinID) {
532  default: return false;
533  case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break;
534  case ARM::BI__builtin_arm_usat: i = 1; u = 31; break;
535  case ARM::BI__builtin_arm_vcvtr_f:
536  case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break;
537#define GET_NEON_IMMEDIATE_CHECK
538#include "clang/Basic/arm_neon.inc"
539#undef GET_NEON_IMMEDIATE_CHECK
540  };
541
542  // We can't check the value of a dependent argument.
543  if (TheCall->getArg(i)->isTypeDependent() ||
544      TheCall->getArg(i)->isValueDependent())
545    return false;
546
547  // Check that the immediate argument is actually a constant.
548  if (SemaBuiltinConstantArg(TheCall, i, Result))
549    return true;
550
551  // Range check against the upper/lower values for this isntruction.
552  unsigned Val = Result.getZExtValue();
553  if (Val < l || Val > (u + l))
554    return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
555      << l << u+l << TheCall->getArg(i)->getSourceRange();
556
557  // FIXME: VFP Intrinsics should error if VFP not present.
558  return false;
559}
560
561bool Sema::CheckMipsBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
562  unsigned i = 0, l = 0, u = 0;
563  switch (BuiltinID) {
564  default: return false;
565  case Mips::BI__builtin_mips_wrdsp: i = 1; l = 0; u = 63; break;
566  case Mips::BI__builtin_mips_rddsp: i = 0; l = 0; u = 63; break;
567  case Mips::BI__builtin_mips_append: i = 2; l = 0; u = 31; break;
568  case Mips::BI__builtin_mips_balign: i = 2; l = 0; u = 3; break;
569  case Mips::BI__builtin_mips_precr_sra_ph_w: i = 2; l = 0; u = 31; break;
570  case Mips::BI__builtin_mips_precr_sra_r_ph_w: i = 2; l = 0; u = 31; break;
571  case Mips::BI__builtin_mips_prepend: i = 2; l = 0; u = 31; break;
572  };
573
574  // We can't check the value of a dependent argument.
575  if (TheCall->getArg(i)->isTypeDependent() ||
576      TheCall->getArg(i)->isValueDependent())
577    return false;
578
579  // Check that the immediate argument is actually a constant.
580  llvm::APSInt Result;
581  if (SemaBuiltinConstantArg(TheCall, i, Result))
582    return true;
583
584  // Range check against the upper/lower values for this instruction.
585  unsigned Val = Result.getZExtValue();
586  if (Val < l || Val > u)
587    return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
588      << l << u << TheCall->getArg(i)->getSourceRange();
589
590  return false;
591}
592
593/// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo
594/// parameter with the FormatAttr's correct format_idx and firstDataArg.
595/// Returns true when the format fits the function and the FormatStringInfo has
596/// been populated.
597bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember,
598                               FormatStringInfo *FSI) {
599  FSI->HasVAListArg = Format->getFirstArg() == 0;
600  FSI->FormatIdx = Format->getFormatIdx() - 1;
601  FSI->FirstDataArg = FSI->HasVAListArg ? 0 : Format->getFirstArg() - 1;
602
603  // The way the format attribute works in GCC, the implicit this argument
604  // of member functions is counted. However, it doesn't appear in our own
605  // lists, so decrement format_idx in that case.
606  if (IsCXXMember) {
607    if(FSI->FormatIdx == 0)
608      return false;
609    --FSI->FormatIdx;
610    if (FSI->FirstDataArg != 0)
611      --FSI->FirstDataArg;
612  }
613  return true;
614}
615
616/// Handles the checks for format strings, non-POD arguments to vararg
617/// functions, and NULL arguments passed to non-NULL parameters.
618void Sema::checkCall(NamedDecl *FDecl,
619                     ArrayRef<const Expr *> Args,
620                     unsigned NumProtoArgs,
621                     bool IsMemberFunction,
622                     SourceLocation Loc,
623                     SourceRange Range,
624                     VariadicCallType CallType) {
625  if (CurContext->isDependentContext())
626    return;
627
628  // Printf and scanf checking.
629  bool HandledFormatString = false;
630  if (FDecl)
631    for (specific_attr_iterator<FormatAttr>
632           I = FDecl->specific_attr_begin<FormatAttr>(),
633           E = FDecl->specific_attr_end<FormatAttr>(); I != E ; ++I)
634      if (CheckFormatArguments(*I, Args, IsMemberFunction, CallType, Loc,
635                               Range))
636          HandledFormatString = true;
637
638  // Refuse POD arguments that weren't caught by the format string
639  // checks above.
640  if (!HandledFormatString && CallType != VariadicDoesNotApply)
641    for (unsigned ArgIdx = NumProtoArgs; ArgIdx < Args.size(); ++ArgIdx) {
642      // Args[ArgIdx] can be null in malformed code.
643      if (const Expr *Arg = Args[ArgIdx])
644        variadicArgumentPODCheck(Arg, CallType);
645    }
646
647  if (FDecl) {
648    for (specific_attr_iterator<NonNullAttr>
649           I = FDecl->specific_attr_begin<NonNullAttr>(),
650           E = FDecl->specific_attr_end<NonNullAttr>(); I != E; ++I)
651      CheckNonNullArguments(*I, Args.data(), Loc);
652
653    // Type safety checking.
654    for (specific_attr_iterator<ArgumentWithTypeTagAttr>
655           i = FDecl->specific_attr_begin<ArgumentWithTypeTagAttr>(),
656           e = FDecl->specific_attr_end<ArgumentWithTypeTagAttr>();
657         i != e; ++i) {
658      CheckArgumentWithTypeTag(*i, Args.data());
659    }
660  }
661}
662
663/// CheckConstructorCall - Check a constructor call for correctness and safety
664/// properties not enforced by the C type system.
665void Sema::CheckConstructorCall(FunctionDecl *FDecl,
666                                ArrayRef<const Expr *> Args,
667                                const FunctionProtoType *Proto,
668                                SourceLocation Loc) {
669  VariadicCallType CallType =
670    Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply;
671  checkCall(FDecl, Args, Proto->getNumArgs(),
672            /*IsMemberFunction=*/true, Loc, SourceRange(), CallType);
673}
674
675/// CheckFunctionCall - Check a direct function call for various correctness
676/// and safety properties not strictly enforced by the C type system.
677bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall,
678                             const FunctionProtoType *Proto) {
679  bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) &&
680                              isa<CXXMethodDecl>(FDecl);
681  bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) ||
682                          IsMemberOperatorCall;
683  VariadicCallType CallType = getVariadicCallType(FDecl, Proto,
684                                                  TheCall->getCallee());
685  unsigned NumProtoArgs = Proto ? Proto->getNumArgs() : 0;
686  Expr** Args = TheCall->getArgs();
687  unsigned NumArgs = TheCall->getNumArgs();
688  if (IsMemberOperatorCall) {
689    // If this is a call to a member operator, hide the first argument
690    // from checkCall.
691    // FIXME: Our choice of AST representation here is less than ideal.
692    ++Args;
693    --NumArgs;
694  }
695  checkCall(FDecl, llvm::makeArrayRef<const Expr *>(Args, NumArgs),
696            NumProtoArgs,
697            IsMemberFunction, TheCall->getRParenLoc(),
698            TheCall->getCallee()->getSourceRange(), CallType);
699
700  IdentifierInfo *FnInfo = FDecl->getIdentifier();
701  // None of the checks below are needed for functions that don't have
702  // simple names (e.g., C++ conversion functions).
703  if (!FnInfo)
704    return false;
705
706  unsigned CMId = FDecl->getMemoryFunctionKind();
707  if (CMId == 0)
708    return false;
709
710  // Handle memory setting and copying functions.
711  if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat)
712    CheckStrlcpycatArguments(TheCall, FnInfo);
713  else if (CMId == Builtin::BIstrncat)
714    CheckStrncatArguments(TheCall, FnInfo);
715  else
716    CheckMemaccessArguments(TheCall, CMId, FnInfo);
717
718  return false;
719}
720
721bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac,
722                               ArrayRef<const Expr *> Args) {
723  VariadicCallType CallType =
724      Method->isVariadic() ? VariadicMethod : VariadicDoesNotApply;
725
726  checkCall(Method, Args, Method->param_size(),
727            /*IsMemberFunction=*/false,
728            lbrac, Method->getSourceRange(), CallType);
729
730  return false;
731}
732
733bool Sema::CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall,
734                            const FunctionProtoType *Proto) {
735  const VarDecl *V = dyn_cast<VarDecl>(NDecl);
736  if (!V)
737    return false;
738
739  QualType Ty = V->getType();
740  if (!Ty->isBlockPointerType() && !Ty->isFunctionPointerType())
741    return false;
742
743  VariadicCallType CallType;
744  if (!Proto || !Proto->isVariadic()) {
745    CallType = VariadicDoesNotApply;
746  } else if (Ty->isBlockPointerType()) {
747    CallType = VariadicBlock;
748  } else { // Ty->isFunctionPointerType()
749    CallType = VariadicFunction;
750  }
751  unsigned NumProtoArgs = Proto ? Proto->getNumArgs() : 0;
752
753  checkCall(NDecl,
754            llvm::makeArrayRef<const Expr *>(TheCall->getArgs(),
755                                             TheCall->getNumArgs()),
756            NumProtoArgs, /*IsMemberFunction=*/false,
757            TheCall->getRParenLoc(),
758            TheCall->getCallee()->getSourceRange(), CallType);
759
760  return false;
761}
762
763/// Checks function calls when a FunctionDecl or a NamedDecl is not available,
764/// such as function pointers returned from functions.
765bool Sema::CheckOtherCall(CallExpr *TheCall, const FunctionProtoType *Proto) {
766  VariadicCallType CallType = getVariadicCallType(/*FDecl=*/0, Proto,
767                                                  TheCall->getCallee());
768  unsigned NumProtoArgs = Proto ? Proto->getNumArgs() : 0;
769
770  checkCall(/*FDecl=*/0,
771            llvm::makeArrayRef<const Expr *>(TheCall->getArgs(),
772                                             TheCall->getNumArgs()),
773            NumProtoArgs, /*IsMemberFunction=*/false,
774            TheCall->getRParenLoc(),
775            TheCall->getCallee()->getSourceRange(), CallType);
776
777  return false;
778}
779
780ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult,
781                                         AtomicExpr::AtomicOp Op) {
782  CallExpr *TheCall = cast<CallExpr>(TheCallResult.get());
783  DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
784
785  // All these operations take one of the following forms:
786  enum {
787    // C    __c11_atomic_init(A *, C)
788    Init,
789    // C    __c11_atomic_load(A *, int)
790    Load,
791    // void __atomic_load(A *, CP, int)
792    Copy,
793    // C    __c11_atomic_add(A *, M, int)
794    Arithmetic,
795    // C    __atomic_exchange_n(A *, CP, int)
796    Xchg,
797    // void __atomic_exchange(A *, C *, CP, int)
798    GNUXchg,
799    // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int)
800    C11CmpXchg,
801    // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int)
802    GNUCmpXchg
803  } Form = Init;
804  const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 4, 5, 6 };
805  const unsigned NumVals[] = { 1, 0, 1, 1, 1, 2, 2, 3 };
806  // where:
807  //   C is an appropriate type,
808  //   A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins,
809  //   CP is C for __c11 builtins and GNU _n builtins and is C * otherwise,
810  //   M is C if C is an integer, and ptrdiff_t if C is a pointer, and
811  //   the int parameters are for orderings.
812
813  assert(AtomicExpr::AO__c11_atomic_init == 0 &&
814         AtomicExpr::AO__c11_atomic_fetch_xor + 1 == AtomicExpr::AO__atomic_load
815         && "need to update code for modified C11 atomics");
816  bool IsC11 = Op >= AtomicExpr::AO__c11_atomic_init &&
817               Op <= AtomicExpr::AO__c11_atomic_fetch_xor;
818  bool IsN = Op == AtomicExpr::AO__atomic_load_n ||
819             Op == AtomicExpr::AO__atomic_store_n ||
820             Op == AtomicExpr::AO__atomic_exchange_n ||
821             Op == AtomicExpr::AO__atomic_compare_exchange_n;
822  bool IsAddSub = false;
823
824  switch (Op) {
825  case AtomicExpr::AO__c11_atomic_init:
826    Form = Init;
827    break;
828
829  case AtomicExpr::AO__c11_atomic_load:
830  case AtomicExpr::AO__atomic_load_n:
831    Form = Load;
832    break;
833
834  case AtomicExpr::AO__c11_atomic_store:
835  case AtomicExpr::AO__atomic_load:
836  case AtomicExpr::AO__atomic_store:
837  case AtomicExpr::AO__atomic_store_n:
838    Form = Copy;
839    break;
840
841  case AtomicExpr::AO__c11_atomic_fetch_add:
842  case AtomicExpr::AO__c11_atomic_fetch_sub:
843  case AtomicExpr::AO__atomic_fetch_add:
844  case AtomicExpr::AO__atomic_fetch_sub:
845  case AtomicExpr::AO__atomic_add_fetch:
846  case AtomicExpr::AO__atomic_sub_fetch:
847    IsAddSub = true;
848    // Fall through.
849  case AtomicExpr::AO__c11_atomic_fetch_and:
850  case AtomicExpr::AO__c11_atomic_fetch_or:
851  case AtomicExpr::AO__c11_atomic_fetch_xor:
852  case AtomicExpr::AO__atomic_fetch_and:
853  case AtomicExpr::AO__atomic_fetch_or:
854  case AtomicExpr::AO__atomic_fetch_xor:
855  case AtomicExpr::AO__atomic_fetch_nand:
856  case AtomicExpr::AO__atomic_and_fetch:
857  case AtomicExpr::AO__atomic_or_fetch:
858  case AtomicExpr::AO__atomic_xor_fetch:
859  case AtomicExpr::AO__atomic_nand_fetch:
860    Form = Arithmetic;
861    break;
862
863  case AtomicExpr::AO__c11_atomic_exchange:
864  case AtomicExpr::AO__atomic_exchange_n:
865    Form = Xchg;
866    break;
867
868  case AtomicExpr::AO__atomic_exchange:
869    Form = GNUXchg;
870    break;
871
872  case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
873  case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
874    Form = C11CmpXchg;
875    break;
876
877  case AtomicExpr::AO__atomic_compare_exchange:
878  case AtomicExpr::AO__atomic_compare_exchange_n:
879    Form = GNUCmpXchg;
880    break;
881  }
882
883  // Check we have the right number of arguments.
884  if (TheCall->getNumArgs() < NumArgs[Form]) {
885    Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
886      << 0 << NumArgs[Form] << TheCall->getNumArgs()
887      << TheCall->getCallee()->getSourceRange();
888    return ExprError();
889  } else if (TheCall->getNumArgs() > NumArgs[Form]) {
890    Diag(TheCall->getArg(NumArgs[Form])->getLocStart(),
891         diag::err_typecheck_call_too_many_args)
892      << 0 << NumArgs[Form] << TheCall->getNumArgs()
893      << TheCall->getCallee()->getSourceRange();
894    return ExprError();
895  }
896
897  // Inspect the first argument of the atomic operation.
898  Expr *Ptr = TheCall->getArg(0);
899  Ptr = DefaultFunctionArrayLvalueConversion(Ptr).get();
900  const PointerType *pointerType = Ptr->getType()->getAs<PointerType>();
901  if (!pointerType) {
902    Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
903      << Ptr->getType() << Ptr->getSourceRange();
904    return ExprError();
905  }
906
907  // For a __c11 builtin, this should be a pointer to an _Atomic type.
908  QualType AtomTy = pointerType->getPointeeType(); // 'A'
909  QualType ValType = AtomTy; // 'C'
910  if (IsC11) {
911    if (!AtomTy->isAtomicType()) {
912      Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic)
913        << Ptr->getType() << Ptr->getSourceRange();
914      return ExprError();
915    }
916    if (AtomTy.isConstQualified()) {
917      Diag(DRE->getLocStart(), diag::err_atomic_op_needs_non_const_atomic)
918        << Ptr->getType() << Ptr->getSourceRange();
919      return ExprError();
920    }
921    ValType = AtomTy->getAs<AtomicType>()->getValueType();
922  }
923
924  // For an arithmetic operation, the implied arithmetic must be well-formed.
925  if (Form == Arithmetic) {
926    // gcc does not enforce these rules for GNU atomics, but we do so for sanity.
927    if (IsAddSub && !ValType->isIntegerType() && !ValType->isPointerType()) {
928      Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr)
929        << IsC11 << Ptr->getType() << Ptr->getSourceRange();
930      return ExprError();
931    }
932    if (!IsAddSub && !ValType->isIntegerType()) {
933      Diag(DRE->getLocStart(), diag::err_atomic_op_bitwise_needs_atomic_int)
934        << IsC11 << Ptr->getType() << Ptr->getSourceRange();
935      return ExprError();
936    }
937  } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) {
938    // For __atomic_*_n operations, the value type must be a scalar integral or
939    // pointer type which is 1, 2, 4, 8 or 16 bytes in length.
940    Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr)
941      << IsC11 << Ptr->getType() << Ptr->getSourceRange();
942    return ExprError();
943  }
944
945  if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context)) {
946    // For GNU atomics, require a trivially-copyable type. This is not part of
947    // the GNU atomics specification, but we enforce it for sanity.
948    Diag(DRE->getLocStart(), diag::err_atomic_op_needs_trivial_copy)
949      << Ptr->getType() << Ptr->getSourceRange();
950    return ExprError();
951  }
952
953  // FIXME: For any builtin other than a load, the ValType must not be
954  // const-qualified.
955
956  switch (ValType.getObjCLifetime()) {
957  case Qualifiers::OCL_None:
958  case Qualifiers::OCL_ExplicitNone:
959    // okay
960    break;
961
962  case Qualifiers::OCL_Weak:
963  case Qualifiers::OCL_Strong:
964  case Qualifiers::OCL_Autoreleasing:
965    // FIXME: Can this happen? By this point, ValType should be known
966    // to be trivially copyable.
967    Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
968      << ValType << Ptr->getSourceRange();
969    return ExprError();
970  }
971
972  QualType ResultType = ValType;
973  if (Form == Copy || Form == GNUXchg || Form == Init)
974    ResultType = Context.VoidTy;
975  else if (Form == C11CmpXchg || Form == GNUCmpXchg)
976    ResultType = Context.BoolTy;
977
978  // The type of a parameter passed 'by value'. In the GNU atomics, such
979  // arguments are actually passed as pointers.
980  QualType ByValType = ValType; // 'CP'
981  if (!IsC11 && !IsN)
982    ByValType = Ptr->getType();
983
984  // The first argument --- the pointer --- has a fixed type; we
985  // deduce the types of the rest of the arguments accordingly.  Walk
986  // the remaining arguments, converting them to the deduced value type.
987  for (unsigned i = 1; i != NumArgs[Form]; ++i) {
988    QualType Ty;
989    if (i < NumVals[Form] + 1) {
990      switch (i) {
991      case 1:
992        // The second argument is the non-atomic operand. For arithmetic, this
993        // is always passed by value, and for a compare_exchange it is always
994        // passed by address. For the rest, GNU uses by-address and C11 uses
995        // by-value.
996        assert(Form != Load);
997        if (Form == Init || (Form == Arithmetic && ValType->isIntegerType()))
998          Ty = ValType;
999        else if (Form == Copy || Form == Xchg)
1000          Ty = ByValType;
1001        else if (Form == Arithmetic)
1002          Ty = Context.getPointerDiffType();
1003        else
1004          Ty = Context.getPointerType(ValType.getUnqualifiedType());
1005        break;
1006      case 2:
1007        // The third argument to compare_exchange / GNU exchange is a
1008        // (pointer to a) desired value.
1009        Ty = ByValType;
1010        break;
1011      case 3:
1012        // The fourth argument to GNU compare_exchange is a 'weak' flag.
1013        Ty = Context.BoolTy;
1014        break;
1015      }
1016    } else {
1017      // The order(s) are always converted to int.
1018      Ty = Context.IntTy;
1019    }
1020
1021    InitializedEntity Entity =
1022        InitializedEntity::InitializeParameter(Context, Ty, false);
1023    ExprResult Arg = TheCall->getArg(i);
1024    Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
1025    if (Arg.isInvalid())
1026      return true;
1027    TheCall->setArg(i, Arg.get());
1028  }
1029
1030  // Permute the arguments into a 'consistent' order.
1031  SmallVector<Expr*, 5> SubExprs;
1032  SubExprs.push_back(Ptr);
1033  switch (Form) {
1034  case Init:
1035    // Note, AtomicExpr::getVal1() has a special case for this atomic.
1036    SubExprs.push_back(TheCall->getArg(1)); // Val1
1037    break;
1038  case Load:
1039    SubExprs.push_back(TheCall->getArg(1)); // Order
1040    break;
1041  case Copy:
1042  case Arithmetic:
1043  case Xchg:
1044    SubExprs.push_back(TheCall->getArg(2)); // Order
1045    SubExprs.push_back(TheCall->getArg(1)); // Val1
1046    break;
1047  case GNUXchg:
1048    // Note, AtomicExpr::getVal2() has a special case for this atomic.
1049    SubExprs.push_back(TheCall->getArg(3)); // Order
1050    SubExprs.push_back(TheCall->getArg(1)); // Val1
1051    SubExprs.push_back(TheCall->getArg(2)); // Val2
1052    break;
1053  case C11CmpXchg:
1054    SubExprs.push_back(TheCall->getArg(3)); // Order
1055    SubExprs.push_back(TheCall->getArg(1)); // Val1
1056    SubExprs.push_back(TheCall->getArg(4)); // OrderFail
1057    SubExprs.push_back(TheCall->getArg(2)); // Val2
1058    break;
1059  case GNUCmpXchg:
1060    SubExprs.push_back(TheCall->getArg(4)); // Order
1061    SubExprs.push_back(TheCall->getArg(1)); // Val1
1062    SubExprs.push_back(TheCall->getArg(5)); // OrderFail
1063    SubExprs.push_back(TheCall->getArg(2)); // Val2
1064    SubExprs.push_back(TheCall->getArg(3)); // Weak
1065    break;
1066  }
1067
1068  AtomicExpr *AE = new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(),
1069                                            SubExprs, ResultType, Op,
1070                                            TheCall->getRParenLoc());
1071
1072  if ((Op == AtomicExpr::AO__c11_atomic_load ||
1073       (Op == AtomicExpr::AO__c11_atomic_store)) &&
1074      Context.AtomicUsesUnsupportedLibcall(AE))
1075    Diag(AE->getLocStart(), diag::err_atomic_load_store_uses_lib) <<
1076    ((Op == AtomicExpr::AO__c11_atomic_load) ? 0 : 1);
1077
1078  return Owned(AE);
1079}
1080
1081
1082/// checkBuiltinArgument - Given a call to a builtin function, perform
1083/// normal type-checking on the given argument, updating the call in
1084/// place.  This is useful when a builtin function requires custom
1085/// type-checking for some of its arguments but not necessarily all of
1086/// them.
1087///
1088/// Returns true on error.
1089static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) {
1090  FunctionDecl *Fn = E->getDirectCallee();
1091  assert(Fn && "builtin call without direct callee!");
1092
1093  ParmVarDecl *Param = Fn->getParamDecl(ArgIndex);
1094  InitializedEntity Entity =
1095    InitializedEntity::InitializeParameter(S.Context, Param);
1096
1097  ExprResult Arg = E->getArg(0);
1098  Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg);
1099  if (Arg.isInvalid())
1100    return true;
1101
1102  E->setArg(ArgIndex, Arg.take());
1103  return false;
1104}
1105
1106/// SemaBuiltinAtomicOverloaded - We have a call to a function like
1107/// __sync_fetch_and_add, which is an overloaded function based on the pointer
1108/// type of its first argument.  The main ActOnCallExpr routines have already
1109/// promoted the types of arguments because all of these calls are prototyped as
1110/// void(...).
1111///
1112/// This function goes through and does final semantic checking for these
1113/// builtins,
1114ExprResult
1115Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) {
1116  CallExpr *TheCall = (CallExpr *)TheCallResult.get();
1117  DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
1118  FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
1119
1120  // Ensure that we have at least one argument to do type inference from.
1121  if (TheCall->getNumArgs() < 1) {
1122    Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
1123      << 0 << 1 << TheCall->getNumArgs()
1124      << TheCall->getCallee()->getSourceRange();
1125    return ExprError();
1126  }
1127
1128  // Inspect the first argument of the atomic builtin.  This should always be
1129  // a pointer type, whose element is an integral scalar or pointer type.
1130  // Because it is a pointer type, we don't have to worry about any implicit
1131  // casts here.
1132  // FIXME: We don't allow floating point scalars as input.
1133  Expr *FirstArg = TheCall->getArg(0);
1134  ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg);
1135  if (FirstArgResult.isInvalid())
1136    return ExprError();
1137  FirstArg = FirstArgResult.take();
1138  TheCall->setArg(0, FirstArg);
1139
1140  const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>();
1141  if (!pointerType) {
1142    Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
1143      << FirstArg->getType() << FirstArg->getSourceRange();
1144    return ExprError();
1145  }
1146
1147  QualType ValType = pointerType->getPointeeType();
1148  if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
1149      !ValType->isBlockPointerType()) {
1150    Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr)
1151      << FirstArg->getType() << FirstArg->getSourceRange();
1152    return ExprError();
1153  }
1154
1155  switch (ValType.getObjCLifetime()) {
1156  case Qualifiers::OCL_None:
1157  case Qualifiers::OCL_ExplicitNone:
1158    // okay
1159    break;
1160
1161  case Qualifiers::OCL_Weak:
1162  case Qualifiers::OCL_Strong:
1163  case Qualifiers::OCL_Autoreleasing:
1164    Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
1165      << ValType << FirstArg->getSourceRange();
1166    return ExprError();
1167  }
1168
1169  // Strip any qualifiers off ValType.
1170  ValType = ValType.getUnqualifiedType();
1171
1172  // The majority of builtins return a value, but a few have special return
1173  // types, so allow them to override appropriately below.
1174  QualType ResultType = ValType;
1175
1176  // We need to figure out which concrete builtin this maps onto.  For example,
1177  // __sync_fetch_and_add with a 2 byte object turns into
1178  // __sync_fetch_and_add_2.
1179#define BUILTIN_ROW(x) \
1180  { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \
1181    Builtin::BI##x##_8, Builtin::BI##x##_16 }
1182
1183  static const unsigned BuiltinIndices[][5] = {
1184    BUILTIN_ROW(__sync_fetch_and_add),
1185    BUILTIN_ROW(__sync_fetch_and_sub),
1186    BUILTIN_ROW(__sync_fetch_and_or),
1187    BUILTIN_ROW(__sync_fetch_and_and),
1188    BUILTIN_ROW(__sync_fetch_and_xor),
1189
1190    BUILTIN_ROW(__sync_add_and_fetch),
1191    BUILTIN_ROW(__sync_sub_and_fetch),
1192    BUILTIN_ROW(__sync_and_and_fetch),
1193    BUILTIN_ROW(__sync_or_and_fetch),
1194    BUILTIN_ROW(__sync_xor_and_fetch),
1195
1196    BUILTIN_ROW(__sync_val_compare_and_swap),
1197    BUILTIN_ROW(__sync_bool_compare_and_swap),
1198    BUILTIN_ROW(__sync_lock_test_and_set),
1199    BUILTIN_ROW(__sync_lock_release),
1200    BUILTIN_ROW(__sync_swap)
1201  };
1202#undef BUILTIN_ROW
1203
1204  // Determine the index of the size.
1205  unsigned SizeIndex;
1206  switch (Context.getTypeSizeInChars(ValType).getQuantity()) {
1207  case 1: SizeIndex = 0; break;
1208  case 2: SizeIndex = 1; break;
1209  case 4: SizeIndex = 2; break;
1210  case 8: SizeIndex = 3; break;
1211  case 16: SizeIndex = 4; break;
1212  default:
1213    Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size)
1214      << FirstArg->getType() << FirstArg->getSourceRange();
1215    return ExprError();
1216  }
1217
1218  // Each of these builtins has one pointer argument, followed by some number of
1219  // values (0, 1 or 2) followed by a potentially empty varags list of stuff
1220  // that we ignore.  Find out which row of BuiltinIndices to read from as well
1221  // as the number of fixed args.
1222  unsigned BuiltinID = FDecl->getBuiltinID();
1223  unsigned BuiltinIndex, NumFixed = 1;
1224  switch (BuiltinID) {
1225  default: llvm_unreachable("Unknown overloaded atomic builtin!");
1226  case Builtin::BI__sync_fetch_and_add:
1227  case Builtin::BI__sync_fetch_and_add_1:
1228  case Builtin::BI__sync_fetch_and_add_2:
1229  case Builtin::BI__sync_fetch_and_add_4:
1230  case Builtin::BI__sync_fetch_and_add_8:
1231  case Builtin::BI__sync_fetch_and_add_16:
1232    BuiltinIndex = 0;
1233    break;
1234
1235  case Builtin::BI__sync_fetch_and_sub:
1236  case Builtin::BI__sync_fetch_and_sub_1:
1237  case Builtin::BI__sync_fetch_and_sub_2:
1238  case Builtin::BI__sync_fetch_and_sub_4:
1239  case Builtin::BI__sync_fetch_and_sub_8:
1240  case Builtin::BI__sync_fetch_and_sub_16:
1241    BuiltinIndex = 1;
1242    break;
1243
1244  case Builtin::BI__sync_fetch_and_or:
1245  case Builtin::BI__sync_fetch_and_or_1:
1246  case Builtin::BI__sync_fetch_and_or_2:
1247  case Builtin::BI__sync_fetch_and_or_4:
1248  case Builtin::BI__sync_fetch_and_or_8:
1249  case Builtin::BI__sync_fetch_and_or_16:
1250    BuiltinIndex = 2;
1251    break;
1252
1253  case Builtin::BI__sync_fetch_and_and:
1254  case Builtin::BI__sync_fetch_and_and_1:
1255  case Builtin::BI__sync_fetch_and_and_2:
1256  case Builtin::BI__sync_fetch_and_and_4:
1257  case Builtin::BI__sync_fetch_and_and_8:
1258  case Builtin::BI__sync_fetch_and_and_16:
1259    BuiltinIndex = 3;
1260    break;
1261
1262  case Builtin::BI__sync_fetch_and_xor:
1263  case Builtin::BI__sync_fetch_and_xor_1:
1264  case Builtin::BI__sync_fetch_and_xor_2:
1265  case Builtin::BI__sync_fetch_and_xor_4:
1266  case Builtin::BI__sync_fetch_and_xor_8:
1267  case Builtin::BI__sync_fetch_and_xor_16:
1268    BuiltinIndex = 4;
1269    break;
1270
1271  case Builtin::BI__sync_add_and_fetch:
1272  case Builtin::BI__sync_add_and_fetch_1:
1273  case Builtin::BI__sync_add_and_fetch_2:
1274  case Builtin::BI__sync_add_and_fetch_4:
1275  case Builtin::BI__sync_add_and_fetch_8:
1276  case Builtin::BI__sync_add_and_fetch_16:
1277    BuiltinIndex = 5;
1278    break;
1279
1280  case Builtin::BI__sync_sub_and_fetch:
1281  case Builtin::BI__sync_sub_and_fetch_1:
1282  case Builtin::BI__sync_sub_and_fetch_2:
1283  case Builtin::BI__sync_sub_and_fetch_4:
1284  case Builtin::BI__sync_sub_and_fetch_8:
1285  case Builtin::BI__sync_sub_and_fetch_16:
1286    BuiltinIndex = 6;
1287    break;
1288
1289  case Builtin::BI__sync_and_and_fetch:
1290  case Builtin::BI__sync_and_and_fetch_1:
1291  case Builtin::BI__sync_and_and_fetch_2:
1292  case Builtin::BI__sync_and_and_fetch_4:
1293  case Builtin::BI__sync_and_and_fetch_8:
1294  case Builtin::BI__sync_and_and_fetch_16:
1295    BuiltinIndex = 7;
1296    break;
1297
1298  case Builtin::BI__sync_or_and_fetch:
1299  case Builtin::BI__sync_or_and_fetch_1:
1300  case Builtin::BI__sync_or_and_fetch_2:
1301  case Builtin::BI__sync_or_and_fetch_4:
1302  case Builtin::BI__sync_or_and_fetch_8:
1303  case Builtin::BI__sync_or_and_fetch_16:
1304    BuiltinIndex = 8;
1305    break;
1306
1307  case Builtin::BI__sync_xor_and_fetch:
1308  case Builtin::BI__sync_xor_and_fetch_1:
1309  case Builtin::BI__sync_xor_and_fetch_2:
1310  case Builtin::BI__sync_xor_and_fetch_4:
1311  case Builtin::BI__sync_xor_and_fetch_8:
1312  case Builtin::BI__sync_xor_and_fetch_16:
1313    BuiltinIndex = 9;
1314    break;
1315
1316  case Builtin::BI__sync_val_compare_and_swap:
1317  case Builtin::BI__sync_val_compare_and_swap_1:
1318  case Builtin::BI__sync_val_compare_and_swap_2:
1319  case Builtin::BI__sync_val_compare_and_swap_4:
1320  case Builtin::BI__sync_val_compare_and_swap_8:
1321  case Builtin::BI__sync_val_compare_and_swap_16:
1322    BuiltinIndex = 10;
1323    NumFixed = 2;
1324    break;
1325
1326  case Builtin::BI__sync_bool_compare_and_swap:
1327  case Builtin::BI__sync_bool_compare_and_swap_1:
1328  case Builtin::BI__sync_bool_compare_and_swap_2:
1329  case Builtin::BI__sync_bool_compare_and_swap_4:
1330  case Builtin::BI__sync_bool_compare_and_swap_8:
1331  case Builtin::BI__sync_bool_compare_and_swap_16:
1332    BuiltinIndex = 11;
1333    NumFixed = 2;
1334    ResultType = Context.BoolTy;
1335    break;
1336
1337  case Builtin::BI__sync_lock_test_and_set:
1338  case Builtin::BI__sync_lock_test_and_set_1:
1339  case Builtin::BI__sync_lock_test_and_set_2:
1340  case Builtin::BI__sync_lock_test_and_set_4:
1341  case Builtin::BI__sync_lock_test_and_set_8:
1342  case Builtin::BI__sync_lock_test_and_set_16:
1343    BuiltinIndex = 12;
1344    break;
1345
1346  case Builtin::BI__sync_lock_release:
1347  case Builtin::BI__sync_lock_release_1:
1348  case Builtin::BI__sync_lock_release_2:
1349  case Builtin::BI__sync_lock_release_4:
1350  case Builtin::BI__sync_lock_release_8:
1351  case Builtin::BI__sync_lock_release_16:
1352    BuiltinIndex = 13;
1353    NumFixed = 0;
1354    ResultType = Context.VoidTy;
1355    break;
1356
1357  case Builtin::BI__sync_swap:
1358  case Builtin::BI__sync_swap_1:
1359  case Builtin::BI__sync_swap_2:
1360  case Builtin::BI__sync_swap_4:
1361  case Builtin::BI__sync_swap_8:
1362  case Builtin::BI__sync_swap_16:
1363    BuiltinIndex = 14;
1364    break;
1365  }
1366
1367  // Now that we know how many fixed arguments we expect, first check that we
1368  // have at least that many.
1369  if (TheCall->getNumArgs() < 1+NumFixed) {
1370    Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
1371      << 0 << 1+NumFixed << TheCall->getNumArgs()
1372      << TheCall->getCallee()->getSourceRange();
1373    return ExprError();
1374  }
1375
1376  // Get the decl for the concrete builtin from this, we can tell what the
1377  // concrete integer type we should convert to is.
1378  unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex];
1379  const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID);
1380  FunctionDecl *NewBuiltinDecl;
1381  if (NewBuiltinID == BuiltinID)
1382    NewBuiltinDecl = FDecl;
1383  else {
1384    // Perform builtin lookup to avoid redeclaring it.
1385    DeclarationName DN(&Context.Idents.get(NewBuiltinName));
1386    LookupResult Res(*this, DN, DRE->getLocStart(), LookupOrdinaryName);
1387    LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true);
1388    assert(Res.getFoundDecl());
1389    NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl());
1390    if (NewBuiltinDecl == 0)
1391      return ExprError();
1392  }
1393
1394  // The first argument --- the pointer --- has a fixed type; we
1395  // deduce the types of the rest of the arguments accordingly.  Walk
1396  // the remaining arguments, converting them to the deduced value type.
1397  for (unsigned i = 0; i != NumFixed; ++i) {
1398    ExprResult Arg = TheCall->getArg(i+1);
1399
1400    // GCC does an implicit conversion to the pointer or integer ValType.  This
1401    // can fail in some cases (1i -> int**), check for this error case now.
1402    // Initialize the argument.
1403    InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
1404                                                   ValType, /*consume*/ false);
1405    Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
1406    if (Arg.isInvalid())
1407      return ExprError();
1408
1409    // Okay, we have something that *can* be converted to the right type.  Check
1410    // to see if there is a potentially weird extension going on here.  This can
1411    // happen when you do an atomic operation on something like an char* and
1412    // pass in 42.  The 42 gets converted to char.  This is even more strange
1413    // for things like 45.123 -> char, etc.
1414    // FIXME: Do this check.
1415    TheCall->setArg(i+1, Arg.take());
1416  }
1417
1418  ASTContext& Context = this->getASTContext();
1419
1420  // Create a new DeclRefExpr to refer to the new decl.
1421  DeclRefExpr* NewDRE = DeclRefExpr::Create(
1422      Context,
1423      DRE->getQualifierLoc(),
1424      SourceLocation(),
1425      NewBuiltinDecl,
1426      /*enclosing*/ false,
1427      DRE->getLocation(),
1428      Context.BuiltinFnTy,
1429      DRE->getValueKind());
1430
1431  // Set the callee in the CallExpr.
1432  // FIXME: This loses syntactic information.
1433  QualType CalleePtrTy = Context.getPointerType(NewBuiltinDecl->getType());
1434  ExprResult PromotedCall = ImpCastExprToType(NewDRE, CalleePtrTy,
1435                                              CK_BuiltinFnToFnPtr);
1436  TheCall->setCallee(PromotedCall.take());
1437
1438  // Change the result type of the call to match the original value type. This
1439  // is arbitrary, but the codegen for these builtins ins design to handle it
1440  // gracefully.
1441  TheCall->setType(ResultType);
1442
1443  return TheCallResult;
1444}
1445
1446/// CheckObjCString - Checks that the argument to the builtin
1447/// CFString constructor is correct
1448/// Note: It might also make sense to do the UTF-16 conversion here (would
1449/// simplify the backend).
1450bool Sema::CheckObjCString(Expr *Arg) {
1451  Arg = Arg->IgnoreParenCasts();
1452  StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
1453
1454  if (!Literal || !Literal->isAscii()) {
1455    Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant)
1456      << Arg->getSourceRange();
1457    return true;
1458  }
1459
1460  if (Literal->containsNonAsciiOrNull()) {
1461    StringRef String = Literal->getString();
1462    unsigned NumBytes = String.size();
1463    SmallVector<UTF16, 128> ToBuf(NumBytes);
1464    const UTF8 *FromPtr = (const UTF8 *)String.data();
1465    UTF16 *ToPtr = &ToBuf[0];
1466
1467    ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes,
1468                                                 &ToPtr, ToPtr + NumBytes,
1469                                                 strictConversion);
1470    // Check for conversion failure.
1471    if (Result != conversionOK)
1472      Diag(Arg->getLocStart(),
1473           diag::warn_cfstring_truncated) << Arg->getSourceRange();
1474  }
1475  return false;
1476}
1477
1478/// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity.
1479/// Emit an error and return true on failure, return false on success.
1480bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) {
1481  Expr *Fn = TheCall->getCallee();
1482  if (TheCall->getNumArgs() > 2) {
1483    Diag(TheCall->getArg(2)->getLocStart(),
1484         diag::err_typecheck_call_too_many_args)
1485      << 0 /*function call*/ << 2 << TheCall->getNumArgs()
1486      << Fn->getSourceRange()
1487      << SourceRange(TheCall->getArg(2)->getLocStart(),
1488                     (*(TheCall->arg_end()-1))->getLocEnd());
1489    return true;
1490  }
1491
1492  if (TheCall->getNumArgs() < 2) {
1493    return Diag(TheCall->getLocEnd(),
1494      diag::err_typecheck_call_too_few_args_at_least)
1495      << 0 /*function call*/ << 2 << TheCall->getNumArgs();
1496  }
1497
1498  // Type-check the first argument normally.
1499  if (checkBuiltinArgument(*this, TheCall, 0))
1500    return true;
1501
1502  // Determine whether the current function is variadic or not.
1503  BlockScopeInfo *CurBlock = getCurBlock();
1504  bool isVariadic;
1505  if (CurBlock)
1506    isVariadic = CurBlock->TheDecl->isVariadic();
1507  else if (FunctionDecl *FD = getCurFunctionDecl())
1508    isVariadic = FD->isVariadic();
1509  else
1510    isVariadic = getCurMethodDecl()->isVariadic();
1511
1512  if (!isVariadic) {
1513    Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function);
1514    return true;
1515  }
1516
1517  // Verify that the second argument to the builtin is the last argument of the
1518  // current function or method.
1519  bool SecondArgIsLastNamedArgument = false;
1520  const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts();
1521
1522  // These are valid if SecondArgIsLastNamedArgument is false after the next
1523  // block.
1524  QualType Type;
1525  SourceLocation ParamLoc;
1526
1527  if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) {
1528    if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
1529      // FIXME: This isn't correct for methods (results in bogus warning).
1530      // Get the last formal in the current function.
1531      const ParmVarDecl *LastArg;
1532      if (CurBlock)
1533        LastArg = *(CurBlock->TheDecl->param_end()-1);
1534      else if (FunctionDecl *FD = getCurFunctionDecl())
1535        LastArg = *(FD->param_end()-1);
1536      else
1537        LastArg = *(getCurMethodDecl()->param_end()-1);
1538      SecondArgIsLastNamedArgument = PV == LastArg;
1539
1540      Type = PV->getType();
1541      ParamLoc = PV->getLocation();
1542    }
1543  }
1544
1545  if (!SecondArgIsLastNamedArgument)
1546    Diag(TheCall->getArg(1)->getLocStart(),
1547         diag::warn_second_parameter_of_va_start_not_last_named_argument);
1548  else if (Type->isReferenceType()) {
1549    Diag(Arg->getLocStart(),
1550         diag::warn_va_start_of_reference_type_is_undefined);
1551    Diag(ParamLoc, diag::note_parameter_type) << Type;
1552  }
1553
1554  return false;
1555}
1556
1557/// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and
1558/// friends.  This is declared to take (...), so we have to check everything.
1559bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) {
1560  if (TheCall->getNumArgs() < 2)
1561    return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
1562      << 0 << 2 << TheCall->getNumArgs()/*function call*/;
1563  if (TheCall->getNumArgs() > 2)
1564    return Diag(TheCall->getArg(2)->getLocStart(),
1565                diag::err_typecheck_call_too_many_args)
1566      << 0 /*function call*/ << 2 << TheCall->getNumArgs()
1567      << SourceRange(TheCall->getArg(2)->getLocStart(),
1568                     (*(TheCall->arg_end()-1))->getLocEnd());
1569
1570  ExprResult OrigArg0 = TheCall->getArg(0);
1571  ExprResult OrigArg1 = TheCall->getArg(1);
1572
1573  // Do standard promotions between the two arguments, returning their common
1574  // type.
1575  QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false);
1576  if (OrigArg0.isInvalid() || OrigArg1.isInvalid())
1577    return true;
1578
1579  // Make sure any conversions are pushed back into the call; this is
1580  // type safe since unordered compare builtins are declared as "_Bool
1581  // foo(...)".
1582  TheCall->setArg(0, OrigArg0.get());
1583  TheCall->setArg(1, OrigArg1.get());
1584
1585  if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent())
1586    return false;
1587
1588  // If the common type isn't a real floating type, then the arguments were
1589  // invalid for this operation.
1590  if (Res.isNull() || !Res->isRealFloatingType())
1591    return Diag(OrigArg0.get()->getLocStart(),
1592                diag::err_typecheck_call_invalid_ordered_compare)
1593      << OrigArg0.get()->getType() << OrigArg1.get()->getType()
1594      << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd());
1595
1596  return false;
1597}
1598
1599/// SemaBuiltinSemaBuiltinFPClassification - Handle functions like
1600/// __builtin_isnan and friends.  This is declared to take (...), so we have
1601/// to check everything. We expect the last argument to be a floating point
1602/// value.
1603bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) {
1604  if (TheCall->getNumArgs() < NumArgs)
1605    return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
1606      << 0 << NumArgs << TheCall->getNumArgs()/*function call*/;
1607  if (TheCall->getNumArgs() > NumArgs)
1608    return Diag(TheCall->getArg(NumArgs)->getLocStart(),
1609                diag::err_typecheck_call_too_many_args)
1610      << 0 /*function call*/ << NumArgs << TheCall->getNumArgs()
1611      << SourceRange(TheCall->getArg(NumArgs)->getLocStart(),
1612                     (*(TheCall->arg_end()-1))->getLocEnd());
1613
1614  Expr *OrigArg = TheCall->getArg(NumArgs-1);
1615
1616  if (OrigArg->isTypeDependent())
1617    return false;
1618
1619  // This operation requires a non-_Complex floating-point number.
1620  if (!OrigArg->getType()->isRealFloatingType())
1621    return Diag(OrigArg->getLocStart(),
1622                diag::err_typecheck_call_invalid_unary_fp)
1623      << OrigArg->getType() << OrigArg->getSourceRange();
1624
1625  // If this is an implicit conversion from float -> double, remove it.
1626  if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) {
1627    Expr *CastArg = Cast->getSubExpr();
1628    if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) {
1629      assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) &&
1630             "promotion from float to double is the only expected cast here");
1631      Cast->setSubExpr(0);
1632      TheCall->setArg(NumArgs-1, CastArg);
1633    }
1634  }
1635
1636  return false;
1637}
1638
1639/// SemaBuiltinShuffleVector - Handle __builtin_shufflevector.
1640// This is declared to take (...), so we have to check everything.
1641ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) {
1642  if (TheCall->getNumArgs() < 2)
1643    return ExprError(Diag(TheCall->getLocEnd(),
1644                          diag::err_typecheck_call_too_few_args_at_least)
1645      << 0 /*function call*/ << 2 << TheCall->getNumArgs()
1646      << TheCall->getSourceRange());
1647
1648  // Determine which of the following types of shufflevector we're checking:
1649  // 1) unary, vector mask: (lhs, mask)
1650  // 2) binary, vector mask: (lhs, rhs, mask)
1651  // 3) binary, scalar mask: (lhs, rhs, index, ..., index)
1652  QualType resType = TheCall->getArg(0)->getType();
1653  unsigned numElements = 0;
1654
1655  if (!TheCall->getArg(0)->isTypeDependent() &&
1656      !TheCall->getArg(1)->isTypeDependent()) {
1657    QualType LHSType = TheCall->getArg(0)->getType();
1658    QualType RHSType = TheCall->getArg(1)->getType();
1659
1660    if (!LHSType->isVectorType() || !RHSType->isVectorType()) {
1661      Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector)
1662        << SourceRange(TheCall->getArg(0)->getLocStart(),
1663                       TheCall->getArg(1)->getLocEnd());
1664      return ExprError();
1665    }
1666
1667    numElements = LHSType->getAs<VectorType>()->getNumElements();
1668    unsigned numResElements = TheCall->getNumArgs() - 2;
1669
1670    // Check to see if we have a call with 2 vector arguments, the unary shuffle
1671    // with mask.  If so, verify that RHS is an integer vector type with the
1672    // same number of elts as lhs.
1673    if (TheCall->getNumArgs() == 2) {
1674      if (!RHSType->hasIntegerRepresentation() ||
1675          RHSType->getAs<VectorType>()->getNumElements() != numElements)
1676        Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector)
1677          << SourceRange(TheCall->getArg(1)->getLocStart(),
1678                         TheCall->getArg(1)->getLocEnd());
1679    } else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) {
1680      Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector)
1681        << SourceRange(TheCall->getArg(0)->getLocStart(),
1682                       TheCall->getArg(1)->getLocEnd());
1683      return ExprError();
1684    } else if (numElements != numResElements) {
1685      QualType eltType = LHSType->getAs<VectorType>()->getElementType();
1686      resType = Context.getVectorType(eltType, numResElements,
1687                                      VectorType::GenericVector);
1688    }
1689  }
1690
1691  for (unsigned i = 2; i < TheCall->getNumArgs(); i++) {
1692    if (TheCall->getArg(i)->isTypeDependent() ||
1693        TheCall->getArg(i)->isValueDependent())
1694      continue;
1695
1696    llvm::APSInt Result(32);
1697    if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context))
1698      return ExprError(Diag(TheCall->getLocStart(),
1699                  diag::err_shufflevector_nonconstant_argument)
1700                << TheCall->getArg(i)->getSourceRange());
1701
1702    if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2)
1703      return ExprError(Diag(TheCall->getLocStart(),
1704                  diag::err_shufflevector_argument_too_large)
1705               << TheCall->getArg(i)->getSourceRange());
1706  }
1707
1708  SmallVector<Expr*, 32> exprs;
1709
1710  for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) {
1711    exprs.push_back(TheCall->getArg(i));
1712    TheCall->setArg(i, 0);
1713  }
1714
1715  return Owned(new (Context) ShuffleVectorExpr(Context, exprs, resType,
1716                                            TheCall->getCallee()->getLocStart(),
1717                                            TheCall->getRParenLoc()));
1718}
1719
1720/// SemaBuiltinPrefetch - Handle __builtin_prefetch.
1721// This is declared to take (const void*, ...) and can take two
1722// optional constant int args.
1723bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) {
1724  unsigned NumArgs = TheCall->getNumArgs();
1725
1726  if (NumArgs > 3)
1727    return Diag(TheCall->getLocEnd(),
1728             diag::err_typecheck_call_too_many_args_at_most)
1729             << 0 /*function call*/ << 3 << NumArgs
1730             << TheCall->getSourceRange();
1731
1732  // Argument 0 is checked for us and the remaining arguments must be
1733  // constant integers.
1734  for (unsigned i = 1; i != NumArgs; ++i) {
1735    Expr *Arg = TheCall->getArg(i);
1736
1737    // We can't check the value of a dependent argument.
1738    if (Arg->isTypeDependent() || Arg->isValueDependent())
1739      continue;
1740
1741    llvm::APSInt Result;
1742    if (SemaBuiltinConstantArg(TheCall, i, Result))
1743      return true;
1744
1745    // FIXME: gcc issues a warning and rewrites these to 0. These
1746    // seems especially odd for the third argument since the default
1747    // is 3.
1748    if (i == 1) {
1749      if (Result.getLimitedValue() > 1)
1750        return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
1751             << "0" << "1" << Arg->getSourceRange();
1752    } else {
1753      if (Result.getLimitedValue() > 3)
1754        return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
1755            << "0" << "3" << Arg->getSourceRange();
1756    }
1757  }
1758
1759  return false;
1760}
1761
1762/// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr
1763/// TheCall is a constant expression.
1764bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum,
1765                                  llvm::APSInt &Result) {
1766  Expr *Arg = TheCall->getArg(ArgNum);
1767  DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
1768  FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
1769
1770  if (Arg->isTypeDependent() || Arg->isValueDependent()) return false;
1771
1772  if (!Arg->isIntegerConstantExpr(Result, Context))
1773    return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type)
1774                << FDecl->getDeclName() <<  Arg->getSourceRange();
1775
1776  return false;
1777}
1778
1779/// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr,
1780/// int type). This simply type checks that type is one of the defined
1781/// constants (0-3).
1782// For compatibility check 0-3, llvm only handles 0 and 2.
1783bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) {
1784  llvm::APSInt Result;
1785
1786  // We can't check the value of a dependent argument.
1787  if (TheCall->getArg(1)->isTypeDependent() ||
1788      TheCall->getArg(1)->isValueDependent())
1789    return false;
1790
1791  // Check constant-ness first.
1792  if (SemaBuiltinConstantArg(TheCall, 1, Result))
1793    return true;
1794
1795  Expr *Arg = TheCall->getArg(1);
1796  if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) {
1797    return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
1798             << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
1799  }
1800
1801  return false;
1802}
1803
1804/// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val).
1805/// This checks that val is a constant 1.
1806bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) {
1807  Expr *Arg = TheCall->getArg(1);
1808  llvm::APSInt Result;
1809
1810  // TODO: This is less than ideal. Overload this to take a value.
1811  if (SemaBuiltinConstantArg(TheCall, 1, Result))
1812    return true;
1813
1814  if (Result != 1)
1815    return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val)
1816             << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
1817
1818  return false;
1819}
1820
1821// Determine if an expression is a string literal or constant string.
1822// If this function returns false on the arguments to a function expecting a
1823// format string, we will usually need to emit a warning.
1824// True string literals are then checked by CheckFormatString.
1825Sema::StringLiteralCheckType
1826Sema::checkFormatStringExpr(const Expr *E, ArrayRef<const Expr *> Args,
1827                            bool HasVAListArg,
1828                            unsigned format_idx, unsigned firstDataArg,
1829                            FormatStringType Type, VariadicCallType CallType,
1830                            bool inFunctionCall) {
1831 tryAgain:
1832  if (E->isTypeDependent() || E->isValueDependent())
1833    return SLCT_NotALiteral;
1834
1835  E = E->IgnoreParenCasts();
1836
1837  if (E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull))
1838    // Technically -Wformat-nonliteral does not warn about this case.
1839    // The behavior of printf and friends in this case is implementation
1840    // dependent.  Ideally if the format string cannot be null then
1841    // it should have a 'nonnull' attribute in the function prototype.
1842    return SLCT_CheckedLiteral;
1843
1844  switch (E->getStmtClass()) {
1845  case Stmt::BinaryConditionalOperatorClass:
1846  case Stmt::ConditionalOperatorClass: {
1847    // The expression is a literal if both sub-expressions were, and it was
1848    // completely checked only if both sub-expressions were checked.
1849    const AbstractConditionalOperator *C =
1850        cast<AbstractConditionalOperator>(E);
1851    StringLiteralCheckType Left =
1852        checkFormatStringExpr(C->getTrueExpr(), Args,
1853                              HasVAListArg, format_idx, firstDataArg,
1854                              Type, CallType, inFunctionCall);
1855    if (Left == SLCT_NotALiteral)
1856      return SLCT_NotALiteral;
1857    StringLiteralCheckType Right =
1858        checkFormatStringExpr(C->getFalseExpr(), Args,
1859                              HasVAListArg, format_idx, firstDataArg,
1860                              Type, CallType, inFunctionCall);
1861    return Left < Right ? Left : Right;
1862  }
1863
1864  case Stmt::ImplicitCastExprClass: {
1865    E = cast<ImplicitCastExpr>(E)->getSubExpr();
1866    goto tryAgain;
1867  }
1868
1869  case Stmt::OpaqueValueExprClass:
1870    if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) {
1871      E = src;
1872      goto tryAgain;
1873    }
1874    return SLCT_NotALiteral;
1875
1876  case Stmt::PredefinedExprClass:
1877    // While __func__, etc., are technically not string literals, they
1878    // cannot contain format specifiers and thus are not a security
1879    // liability.
1880    return SLCT_UncheckedLiteral;
1881
1882  case Stmt::DeclRefExprClass: {
1883    const DeclRefExpr *DR = cast<DeclRefExpr>(E);
1884
1885    // As an exception, do not flag errors for variables binding to
1886    // const string literals.
1887    if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
1888      bool isConstant = false;
1889      QualType T = DR->getType();
1890
1891      if (const ArrayType *AT = Context.getAsArrayType(T)) {
1892        isConstant = AT->getElementType().isConstant(Context);
1893      } else if (const PointerType *PT = T->getAs<PointerType>()) {
1894        isConstant = T.isConstant(Context) &&
1895                     PT->getPointeeType().isConstant(Context);
1896      } else if (T->isObjCObjectPointerType()) {
1897        // In ObjC, there is usually no "const ObjectPointer" type,
1898        // so don't check if the pointee type is constant.
1899        isConstant = T.isConstant(Context);
1900      }
1901
1902      if (isConstant) {
1903        if (const Expr *Init = VD->getAnyInitializer()) {
1904          // Look through initializers like const char c[] = { "foo" }
1905          if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) {
1906            if (InitList->isStringLiteralInit())
1907              Init = InitList->getInit(0)->IgnoreParenImpCasts();
1908          }
1909          return checkFormatStringExpr(Init, Args,
1910                                       HasVAListArg, format_idx,
1911                                       firstDataArg, Type, CallType,
1912                                       /*inFunctionCall*/false);
1913        }
1914      }
1915
1916      // For vprintf* functions (i.e., HasVAListArg==true), we add a
1917      // special check to see if the format string is a function parameter
1918      // of the function calling the printf function.  If the function
1919      // has an attribute indicating it is a printf-like function, then we
1920      // should suppress warnings concerning non-literals being used in a call
1921      // to a vprintf function.  For example:
1922      //
1923      // void
1924      // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){
1925      //      va_list ap;
1926      //      va_start(ap, fmt);
1927      //      vprintf(fmt, ap);  // Do NOT emit a warning about "fmt".
1928      //      ...
1929      //
1930      if (HasVAListArg) {
1931        if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) {
1932          if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) {
1933            int PVIndex = PV->getFunctionScopeIndex() + 1;
1934            for (specific_attr_iterator<FormatAttr>
1935                 i = ND->specific_attr_begin<FormatAttr>(),
1936                 e = ND->specific_attr_end<FormatAttr>(); i != e ; ++i) {
1937              FormatAttr *PVFormat = *i;
1938              // adjust for implicit parameter
1939              if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND))
1940                if (MD->isInstance())
1941                  ++PVIndex;
1942              // We also check if the formats are compatible.
1943              // We can't pass a 'scanf' string to a 'printf' function.
1944              if (PVIndex == PVFormat->getFormatIdx() &&
1945                  Type == GetFormatStringType(PVFormat))
1946                return SLCT_UncheckedLiteral;
1947            }
1948          }
1949        }
1950      }
1951    }
1952
1953    return SLCT_NotALiteral;
1954  }
1955
1956  case Stmt::CallExprClass:
1957  case Stmt::CXXMemberCallExprClass: {
1958    const CallExpr *CE = cast<CallExpr>(E);
1959    if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {
1960      if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) {
1961        unsigned ArgIndex = FA->getFormatIdx();
1962        if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND))
1963          if (MD->isInstance())
1964            --ArgIndex;
1965        const Expr *Arg = CE->getArg(ArgIndex - 1);
1966
1967        return checkFormatStringExpr(Arg, Args,
1968                                     HasVAListArg, format_idx, firstDataArg,
1969                                     Type, CallType, inFunctionCall);
1970      } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1971        unsigned BuiltinID = FD->getBuiltinID();
1972        if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString ||
1973            BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) {
1974          const Expr *Arg = CE->getArg(0);
1975          return checkFormatStringExpr(Arg, Args,
1976                                       HasVAListArg, format_idx,
1977                                       firstDataArg, Type, CallType,
1978                                       inFunctionCall);
1979        }
1980      }
1981    }
1982
1983    return SLCT_NotALiteral;
1984  }
1985  case Stmt::ObjCStringLiteralClass:
1986  case Stmt::StringLiteralClass: {
1987    const StringLiteral *StrE = NULL;
1988
1989    if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E))
1990      StrE = ObjCFExpr->getString();
1991    else
1992      StrE = cast<StringLiteral>(E);
1993
1994    if (StrE) {
1995      CheckFormatString(StrE, E, Args, HasVAListArg, format_idx,
1996                        firstDataArg, Type, inFunctionCall, CallType);
1997      return SLCT_CheckedLiteral;
1998    }
1999
2000    return SLCT_NotALiteral;
2001  }
2002
2003  default:
2004    return SLCT_NotALiteral;
2005  }
2006}
2007
2008void
2009Sema::CheckNonNullArguments(const NonNullAttr *NonNull,
2010                            const Expr * const *ExprArgs,
2011                            SourceLocation CallSiteLoc) {
2012  for (NonNullAttr::args_iterator i = NonNull->args_begin(),
2013                                  e = NonNull->args_end();
2014       i != e; ++i) {
2015    const Expr *ArgExpr = ExprArgs[*i];
2016
2017    // As a special case, transparent unions initialized with zero are
2018    // considered null for the purposes of the nonnull attribute.
2019    if (const RecordType *UT = ArgExpr->getType()->getAsUnionType()) {
2020      if (UT->getDecl()->hasAttr<TransparentUnionAttr>())
2021        if (const CompoundLiteralExpr *CLE =
2022            dyn_cast<CompoundLiteralExpr>(ArgExpr))
2023          if (const InitListExpr *ILE =
2024              dyn_cast<InitListExpr>(CLE->getInitializer()))
2025            ArgExpr = ILE->getInit(0);
2026    }
2027
2028    bool Result;
2029    if (ArgExpr->EvaluateAsBooleanCondition(Result, Context) && !Result)
2030      Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange();
2031  }
2032}
2033
2034Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) {
2035  return llvm::StringSwitch<FormatStringType>(Format->getType())
2036  .Case("scanf", FST_Scanf)
2037  .Cases("printf", "printf0", FST_Printf)
2038  .Cases("NSString", "CFString", FST_NSString)
2039  .Case("strftime", FST_Strftime)
2040  .Case("strfmon", FST_Strfmon)
2041  .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf)
2042  .Default(FST_Unknown);
2043}
2044
2045/// CheckFormatArguments - Check calls to printf and scanf (and similar
2046/// functions) for correct use of format strings.
2047/// Returns true if a format string has been fully checked.
2048bool Sema::CheckFormatArguments(const FormatAttr *Format,
2049                                ArrayRef<const Expr *> Args,
2050                                bool IsCXXMember,
2051                                VariadicCallType CallType,
2052                                SourceLocation Loc, SourceRange Range) {
2053  FormatStringInfo FSI;
2054  if (getFormatStringInfo(Format, IsCXXMember, &FSI))
2055    return CheckFormatArguments(Args, FSI.HasVAListArg, FSI.FormatIdx,
2056                                FSI.FirstDataArg, GetFormatStringType(Format),
2057                                CallType, Loc, Range);
2058  return false;
2059}
2060
2061bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args,
2062                                bool HasVAListArg, unsigned format_idx,
2063                                unsigned firstDataArg, FormatStringType Type,
2064                                VariadicCallType CallType,
2065                                SourceLocation Loc, SourceRange Range) {
2066  // CHECK: printf/scanf-like function is called with no format string.
2067  if (format_idx >= Args.size()) {
2068    Diag(Loc, diag::warn_missing_format_string) << Range;
2069    return false;
2070  }
2071
2072  const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts();
2073
2074  // CHECK: format string is not a string literal.
2075  //
2076  // Dynamically generated format strings are difficult to
2077  // automatically vet at compile time.  Requiring that format strings
2078  // are string literals: (1) permits the checking of format strings by
2079  // the compiler and thereby (2) can practically remove the source of
2080  // many format string exploits.
2081
2082  // Format string can be either ObjC string (e.g. @"%d") or
2083  // C string (e.g. "%d")
2084  // ObjC string uses the same format specifiers as C string, so we can use
2085  // the same format string checking logic for both ObjC and C strings.
2086  StringLiteralCheckType CT =
2087      checkFormatStringExpr(OrigFormatExpr, Args, HasVAListArg,
2088                            format_idx, firstDataArg, Type, CallType);
2089  if (CT != SLCT_NotALiteral)
2090    // Literal format string found, check done!
2091    return CT == SLCT_CheckedLiteral;
2092
2093  // Strftime is particular as it always uses a single 'time' argument,
2094  // so it is safe to pass a non-literal string.
2095  if (Type == FST_Strftime)
2096    return false;
2097
2098  // Do not emit diag when the string param is a macro expansion and the
2099  // format is either NSString or CFString. This is a hack to prevent
2100  // diag when using the NSLocalizedString and CFCopyLocalizedString macros
2101  // which are usually used in place of NS and CF string literals.
2102  if (Type == FST_NSString &&
2103      SourceMgr.isInSystemMacro(Args[format_idx]->getLocStart()))
2104    return false;
2105
2106  // If there are no arguments specified, warn with -Wformat-security, otherwise
2107  // warn only with -Wformat-nonliteral.
2108  if (Args.size() == firstDataArg)
2109    Diag(Args[format_idx]->getLocStart(),
2110         diag::warn_format_nonliteral_noargs)
2111      << OrigFormatExpr->getSourceRange();
2112  else
2113    Diag(Args[format_idx]->getLocStart(),
2114         diag::warn_format_nonliteral)
2115           << OrigFormatExpr->getSourceRange();
2116  return false;
2117}
2118
2119namespace {
2120class CheckFormatHandler : public analyze_format_string::FormatStringHandler {
2121protected:
2122  Sema &S;
2123  const StringLiteral *FExpr;
2124  const Expr *OrigFormatExpr;
2125  const unsigned FirstDataArg;
2126  const unsigned NumDataArgs;
2127  const char *Beg; // Start of format string.
2128  const bool HasVAListArg;
2129  ArrayRef<const Expr *> Args;
2130  unsigned FormatIdx;
2131  llvm::BitVector CoveredArgs;
2132  bool usesPositionalArgs;
2133  bool atFirstArg;
2134  bool inFunctionCall;
2135  Sema::VariadicCallType CallType;
2136public:
2137  CheckFormatHandler(Sema &s, const StringLiteral *fexpr,
2138                     const Expr *origFormatExpr, unsigned firstDataArg,
2139                     unsigned numDataArgs, const char *beg, bool hasVAListArg,
2140                     ArrayRef<const Expr *> Args,
2141                     unsigned formatIdx, bool inFunctionCall,
2142                     Sema::VariadicCallType callType)
2143    : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr),
2144      FirstDataArg(firstDataArg), NumDataArgs(numDataArgs),
2145      Beg(beg), HasVAListArg(hasVAListArg),
2146      Args(Args), FormatIdx(formatIdx),
2147      usesPositionalArgs(false), atFirstArg(true),
2148      inFunctionCall(inFunctionCall), CallType(callType) {
2149        CoveredArgs.resize(numDataArgs);
2150        CoveredArgs.reset();
2151      }
2152
2153  void DoneProcessing();
2154
2155  void HandleIncompleteSpecifier(const char *startSpecifier,
2156                                 unsigned specifierLen);
2157
2158  void HandleInvalidLengthModifier(
2159      const analyze_format_string::FormatSpecifier &FS,
2160      const analyze_format_string::ConversionSpecifier &CS,
2161      const char *startSpecifier, unsigned specifierLen, unsigned DiagID);
2162
2163  void HandleNonStandardLengthModifier(
2164      const analyze_format_string::FormatSpecifier &FS,
2165      const char *startSpecifier, unsigned specifierLen);
2166
2167  void HandleNonStandardConversionSpecifier(
2168      const analyze_format_string::ConversionSpecifier &CS,
2169      const char *startSpecifier, unsigned specifierLen);
2170
2171  virtual void HandlePosition(const char *startPos, unsigned posLen);
2172
2173  virtual void HandleInvalidPosition(const char *startSpecifier,
2174                                     unsigned specifierLen,
2175                                     analyze_format_string::PositionContext p);
2176
2177  virtual void HandleZeroPosition(const char *startPos, unsigned posLen);
2178
2179  void HandleNullChar(const char *nullCharacter);
2180
2181  template <typename Range>
2182  static void EmitFormatDiagnostic(Sema &S, bool inFunctionCall,
2183                                   const Expr *ArgumentExpr,
2184                                   PartialDiagnostic PDiag,
2185                                   SourceLocation StringLoc,
2186                                   bool IsStringLocation, Range StringRange,
2187                                   ArrayRef<FixItHint> Fixit = None);
2188
2189protected:
2190  bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc,
2191                                        const char *startSpec,
2192                                        unsigned specifierLen,
2193                                        const char *csStart, unsigned csLen);
2194
2195  void HandlePositionalNonpositionalArgs(SourceLocation Loc,
2196                                         const char *startSpec,
2197                                         unsigned specifierLen);
2198
2199  SourceRange getFormatStringRange();
2200  CharSourceRange getSpecifierRange(const char *startSpecifier,
2201                                    unsigned specifierLen);
2202  SourceLocation getLocationOfByte(const char *x);
2203
2204  const Expr *getDataArg(unsigned i) const;
2205
2206  bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS,
2207                    const analyze_format_string::ConversionSpecifier &CS,
2208                    const char *startSpecifier, unsigned specifierLen,
2209                    unsigned argIndex);
2210
2211  template <typename Range>
2212  void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc,
2213                            bool IsStringLocation, Range StringRange,
2214                            ArrayRef<FixItHint> Fixit = None);
2215
2216  void CheckPositionalAndNonpositionalArgs(
2217      const analyze_format_string::FormatSpecifier *FS);
2218};
2219}
2220
2221SourceRange CheckFormatHandler::getFormatStringRange() {
2222  return OrigFormatExpr->getSourceRange();
2223}
2224
2225CharSourceRange CheckFormatHandler::
2226getSpecifierRange(const char *startSpecifier, unsigned specifierLen) {
2227  SourceLocation Start = getLocationOfByte(startSpecifier);
2228  SourceLocation End   = getLocationOfByte(startSpecifier + specifierLen - 1);
2229
2230  // Advance the end SourceLocation by one due to half-open ranges.
2231  End = End.getLocWithOffset(1);
2232
2233  return CharSourceRange::getCharRange(Start, End);
2234}
2235
2236SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) {
2237  return S.getLocationOfStringLiteralByte(FExpr, x - Beg);
2238}
2239
2240void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier,
2241                                                   unsigned specifierLen){
2242  EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier),
2243                       getLocationOfByte(startSpecifier),
2244                       /*IsStringLocation*/true,
2245                       getSpecifierRange(startSpecifier, specifierLen));
2246}
2247
2248void CheckFormatHandler::HandleInvalidLengthModifier(
2249    const analyze_format_string::FormatSpecifier &FS,
2250    const analyze_format_string::ConversionSpecifier &CS,
2251    const char *startSpecifier, unsigned specifierLen, unsigned DiagID) {
2252  using namespace analyze_format_string;
2253
2254  const LengthModifier &LM = FS.getLengthModifier();
2255  CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength());
2256
2257  // See if we know how to fix this length modifier.
2258  Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier();
2259  if (FixedLM) {
2260    EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(),
2261                         getLocationOfByte(LM.getStart()),
2262                         /*IsStringLocation*/true,
2263                         getSpecifierRange(startSpecifier, specifierLen));
2264
2265    S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier)
2266      << FixedLM->toString()
2267      << FixItHint::CreateReplacement(LMRange, FixedLM->toString());
2268
2269  } else {
2270    FixItHint Hint;
2271    if (DiagID == diag::warn_format_nonsensical_length)
2272      Hint = FixItHint::CreateRemoval(LMRange);
2273
2274    EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(),
2275                         getLocationOfByte(LM.getStart()),
2276                         /*IsStringLocation*/true,
2277                         getSpecifierRange(startSpecifier, specifierLen),
2278                         Hint);
2279  }
2280}
2281
2282void CheckFormatHandler::HandleNonStandardLengthModifier(
2283    const analyze_format_string::FormatSpecifier &FS,
2284    const char *startSpecifier, unsigned specifierLen) {
2285  using namespace analyze_format_string;
2286
2287  const LengthModifier &LM = FS.getLengthModifier();
2288  CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength());
2289
2290  // See if we know how to fix this length modifier.
2291  Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier();
2292  if (FixedLM) {
2293    EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
2294                           << LM.toString() << 0,
2295                         getLocationOfByte(LM.getStart()),
2296                         /*IsStringLocation*/true,
2297                         getSpecifierRange(startSpecifier, specifierLen));
2298
2299    S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier)
2300      << FixedLM->toString()
2301      << FixItHint::CreateReplacement(LMRange, FixedLM->toString());
2302
2303  } else {
2304    EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
2305                           << LM.toString() << 0,
2306                         getLocationOfByte(LM.getStart()),
2307                         /*IsStringLocation*/true,
2308                         getSpecifierRange(startSpecifier, specifierLen));
2309  }
2310}
2311
2312void CheckFormatHandler::HandleNonStandardConversionSpecifier(
2313    const analyze_format_string::ConversionSpecifier &CS,
2314    const char *startSpecifier, unsigned specifierLen) {
2315  using namespace analyze_format_string;
2316
2317  // See if we know how to fix this conversion specifier.
2318  Optional<ConversionSpecifier> FixedCS = CS.getStandardSpecifier();
2319  if (FixedCS) {
2320    EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
2321                          << CS.toString() << /*conversion specifier*/1,
2322                         getLocationOfByte(CS.getStart()),
2323                         /*IsStringLocation*/true,
2324                         getSpecifierRange(startSpecifier, specifierLen));
2325
2326    CharSourceRange CSRange = getSpecifierRange(CS.getStart(), CS.getLength());
2327    S.Diag(getLocationOfByte(CS.getStart()), diag::note_format_fix_specifier)
2328      << FixedCS->toString()
2329      << FixItHint::CreateReplacement(CSRange, FixedCS->toString());
2330  } else {
2331    EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
2332                          << CS.toString() << /*conversion specifier*/1,
2333                         getLocationOfByte(CS.getStart()),
2334                         /*IsStringLocation*/true,
2335                         getSpecifierRange(startSpecifier, specifierLen));
2336  }
2337}
2338
2339void CheckFormatHandler::HandlePosition(const char *startPos,
2340                                        unsigned posLen) {
2341  EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg),
2342                               getLocationOfByte(startPos),
2343                               /*IsStringLocation*/true,
2344                               getSpecifierRange(startPos, posLen));
2345}
2346
2347void
2348CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen,
2349                                     analyze_format_string::PositionContext p) {
2350  EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier)
2351                         << (unsigned) p,
2352                       getLocationOfByte(startPos), /*IsStringLocation*/true,
2353                       getSpecifierRange(startPos, posLen));
2354}
2355
2356void CheckFormatHandler::HandleZeroPosition(const char *startPos,
2357                                            unsigned posLen) {
2358  EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier),
2359                               getLocationOfByte(startPos),
2360                               /*IsStringLocation*/true,
2361                               getSpecifierRange(startPos, posLen));
2362}
2363
2364void CheckFormatHandler::HandleNullChar(const char *nullCharacter) {
2365  if (!isa<ObjCStringLiteral>(OrigFormatExpr)) {
2366    // The presence of a null character is likely an error.
2367    EmitFormatDiagnostic(
2368      S.PDiag(diag::warn_printf_format_string_contains_null_char),
2369      getLocationOfByte(nullCharacter), /*IsStringLocation*/true,
2370      getFormatStringRange());
2371  }
2372}
2373
2374// Note that this may return NULL if there was an error parsing or building
2375// one of the argument expressions.
2376const Expr *CheckFormatHandler::getDataArg(unsigned i) const {
2377  return Args[FirstDataArg + i];
2378}
2379
2380void CheckFormatHandler::DoneProcessing() {
2381    // Does the number of data arguments exceed the number of
2382    // format conversions in the format string?
2383  if (!HasVAListArg) {
2384      // Find any arguments that weren't covered.
2385    CoveredArgs.flip();
2386    signed notCoveredArg = CoveredArgs.find_first();
2387    if (notCoveredArg >= 0) {
2388      assert((unsigned)notCoveredArg < NumDataArgs);
2389      if (const Expr *E = getDataArg((unsigned) notCoveredArg)) {
2390        SourceLocation Loc = E->getLocStart();
2391        if (!S.getSourceManager().isInSystemMacro(Loc)) {
2392          EmitFormatDiagnostic(S.PDiag(diag::warn_printf_data_arg_not_used),
2393                               Loc, /*IsStringLocation*/false,
2394                               getFormatStringRange());
2395        }
2396      }
2397    }
2398  }
2399}
2400
2401bool
2402CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex,
2403                                                     SourceLocation Loc,
2404                                                     const char *startSpec,
2405                                                     unsigned specifierLen,
2406                                                     const char *csStart,
2407                                                     unsigned csLen) {
2408
2409  bool keepGoing = true;
2410  if (argIndex < NumDataArgs) {
2411    // Consider the argument coverered, even though the specifier doesn't
2412    // make sense.
2413    CoveredArgs.set(argIndex);
2414  }
2415  else {
2416    // If argIndex exceeds the number of data arguments we
2417    // don't issue a warning because that is just a cascade of warnings (and
2418    // they may have intended '%%' anyway). We don't want to continue processing
2419    // the format string after this point, however, as we will like just get
2420    // gibberish when trying to match arguments.
2421    keepGoing = false;
2422  }
2423
2424  EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_conversion)
2425                         << StringRef(csStart, csLen),
2426                       Loc, /*IsStringLocation*/true,
2427                       getSpecifierRange(startSpec, specifierLen));
2428
2429  return keepGoing;
2430}
2431
2432void
2433CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc,
2434                                                      const char *startSpec,
2435                                                      unsigned specifierLen) {
2436  EmitFormatDiagnostic(
2437    S.PDiag(diag::warn_format_mix_positional_nonpositional_args),
2438    Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen));
2439}
2440
2441bool
2442CheckFormatHandler::CheckNumArgs(
2443  const analyze_format_string::FormatSpecifier &FS,
2444  const analyze_format_string::ConversionSpecifier &CS,
2445  const char *startSpecifier, unsigned specifierLen, unsigned argIndex) {
2446
2447  if (argIndex >= NumDataArgs) {
2448    PartialDiagnostic PDiag = FS.usesPositionalArg()
2449      ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args)
2450           << (argIndex+1) << NumDataArgs)
2451      : S.PDiag(diag::warn_printf_insufficient_data_args);
2452    EmitFormatDiagnostic(
2453      PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true,
2454      getSpecifierRange(startSpecifier, specifierLen));
2455    return false;
2456  }
2457  return true;
2458}
2459
2460template<typename Range>
2461void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag,
2462                                              SourceLocation Loc,
2463                                              bool IsStringLocation,
2464                                              Range StringRange,
2465                                              ArrayRef<FixItHint> FixIt) {
2466  EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag,
2467                       Loc, IsStringLocation, StringRange, FixIt);
2468}
2469
2470/// \brief If the format string is not within the funcion call, emit a note
2471/// so that the function call and string are in diagnostic messages.
2472///
2473/// \param InFunctionCall if true, the format string is within the function
2474/// call and only one diagnostic message will be produced.  Otherwise, an
2475/// extra note will be emitted pointing to location of the format string.
2476///
2477/// \param ArgumentExpr the expression that is passed as the format string
2478/// argument in the function call.  Used for getting locations when two
2479/// diagnostics are emitted.
2480///
2481/// \param PDiag the callee should already have provided any strings for the
2482/// diagnostic message.  This function only adds locations and fixits
2483/// to diagnostics.
2484///
2485/// \param Loc primary location for diagnostic.  If two diagnostics are
2486/// required, one will be at Loc and a new SourceLocation will be created for
2487/// the other one.
2488///
2489/// \param IsStringLocation if true, Loc points to the format string should be
2490/// used for the note.  Otherwise, Loc points to the argument list and will
2491/// be used with PDiag.
2492///
2493/// \param StringRange some or all of the string to highlight.  This is
2494/// templated so it can accept either a CharSourceRange or a SourceRange.
2495///
2496/// \param FixIt optional fix it hint for the format string.
2497template<typename Range>
2498void CheckFormatHandler::EmitFormatDiagnostic(Sema &S, bool InFunctionCall,
2499                                              const Expr *ArgumentExpr,
2500                                              PartialDiagnostic PDiag,
2501                                              SourceLocation Loc,
2502                                              bool IsStringLocation,
2503                                              Range StringRange,
2504                                              ArrayRef<FixItHint> FixIt) {
2505  if (InFunctionCall) {
2506    const Sema::SemaDiagnosticBuilder &D = S.Diag(Loc, PDiag);
2507    D << StringRange;
2508    for (ArrayRef<FixItHint>::iterator I = FixIt.begin(), E = FixIt.end();
2509         I != E; ++I) {
2510      D << *I;
2511    }
2512  } else {
2513    S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag)
2514      << ArgumentExpr->getSourceRange();
2515
2516    const Sema::SemaDiagnosticBuilder &Note =
2517      S.Diag(IsStringLocation ? Loc : StringRange.getBegin(),
2518             diag::note_format_string_defined);
2519
2520    Note << StringRange;
2521    for (ArrayRef<FixItHint>::iterator I = FixIt.begin(), E = FixIt.end();
2522         I != E; ++I) {
2523      Note << *I;
2524    }
2525  }
2526}
2527
2528//===--- CHECK: Printf format string checking ------------------------------===//
2529
2530namespace {
2531class CheckPrintfHandler : public CheckFormatHandler {
2532  bool ObjCContext;
2533public:
2534  CheckPrintfHandler(Sema &s, const StringLiteral *fexpr,
2535                     const Expr *origFormatExpr, unsigned firstDataArg,
2536                     unsigned numDataArgs, bool isObjC,
2537                     const char *beg, bool hasVAListArg,
2538                     ArrayRef<const Expr *> Args,
2539                     unsigned formatIdx, bool inFunctionCall,
2540                     Sema::VariadicCallType CallType)
2541  : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg,
2542                       numDataArgs, beg, hasVAListArg, Args,
2543                       formatIdx, inFunctionCall, CallType), ObjCContext(isObjC)
2544  {}
2545
2546
2547  bool HandleInvalidPrintfConversionSpecifier(
2548                                      const analyze_printf::PrintfSpecifier &FS,
2549                                      const char *startSpecifier,
2550                                      unsigned specifierLen);
2551
2552  bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS,
2553                             const char *startSpecifier,
2554                             unsigned specifierLen);
2555  bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
2556                       const char *StartSpecifier,
2557                       unsigned SpecifierLen,
2558                       const Expr *E);
2559
2560  bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k,
2561                    const char *startSpecifier, unsigned specifierLen);
2562  void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS,
2563                           const analyze_printf::OptionalAmount &Amt,
2564                           unsigned type,
2565                           const char *startSpecifier, unsigned specifierLen);
2566  void HandleFlag(const analyze_printf::PrintfSpecifier &FS,
2567                  const analyze_printf::OptionalFlag &flag,
2568                  const char *startSpecifier, unsigned specifierLen);
2569  void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS,
2570                         const analyze_printf::OptionalFlag &ignoredFlag,
2571                         const analyze_printf::OptionalFlag &flag,
2572                         const char *startSpecifier, unsigned specifierLen);
2573  bool checkForCStrMembers(const analyze_printf::ArgType &AT,
2574                           const Expr *E, const CharSourceRange &CSR);
2575
2576};
2577}
2578
2579bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier(
2580                                      const analyze_printf::PrintfSpecifier &FS,
2581                                      const char *startSpecifier,
2582                                      unsigned specifierLen) {
2583  const analyze_printf::PrintfConversionSpecifier &CS =
2584    FS.getConversionSpecifier();
2585
2586  return HandleInvalidConversionSpecifier(FS.getArgIndex(),
2587                                          getLocationOfByte(CS.getStart()),
2588                                          startSpecifier, specifierLen,
2589                                          CS.getStart(), CS.getLength());
2590}
2591
2592bool CheckPrintfHandler::HandleAmount(
2593                               const analyze_format_string::OptionalAmount &Amt,
2594                               unsigned k, const char *startSpecifier,
2595                               unsigned specifierLen) {
2596
2597  if (Amt.hasDataArgument()) {
2598    if (!HasVAListArg) {
2599      unsigned argIndex = Amt.getArgIndex();
2600      if (argIndex >= NumDataArgs) {
2601        EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg)
2602                               << k,
2603                             getLocationOfByte(Amt.getStart()),
2604                             /*IsStringLocation*/true,
2605                             getSpecifierRange(startSpecifier, specifierLen));
2606        // Don't do any more checking.  We will just emit
2607        // spurious errors.
2608        return false;
2609      }
2610
2611      // Type check the data argument.  It should be an 'int'.
2612      // Although not in conformance with C99, we also allow the argument to be
2613      // an 'unsigned int' as that is a reasonably safe case.  GCC also
2614      // doesn't emit a warning for that case.
2615      CoveredArgs.set(argIndex);
2616      const Expr *Arg = getDataArg(argIndex);
2617      if (!Arg)
2618        return false;
2619
2620      QualType T = Arg->getType();
2621
2622      const analyze_printf::ArgType &AT = Amt.getArgType(S.Context);
2623      assert(AT.isValid());
2624
2625      if (!AT.matchesType(S.Context, T)) {
2626        EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type)
2627                               << k << AT.getRepresentativeTypeName(S.Context)
2628                               << T << Arg->getSourceRange(),
2629                             getLocationOfByte(Amt.getStart()),
2630                             /*IsStringLocation*/true,
2631                             getSpecifierRange(startSpecifier, specifierLen));
2632        // Don't do any more checking.  We will just emit
2633        // spurious errors.
2634        return false;
2635      }
2636    }
2637  }
2638  return true;
2639}
2640
2641void CheckPrintfHandler::HandleInvalidAmount(
2642                                      const analyze_printf::PrintfSpecifier &FS,
2643                                      const analyze_printf::OptionalAmount &Amt,
2644                                      unsigned type,
2645                                      const char *startSpecifier,
2646                                      unsigned specifierLen) {
2647  const analyze_printf::PrintfConversionSpecifier &CS =
2648    FS.getConversionSpecifier();
2649
2650  FixItHint fixit =
2651    Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant
2652      ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(),
2653                                 Amt.getConstantLength()))
2654      : FixItHint();
2655
2656  EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount)
2657                         << type << CS.toString(),
2658                       getLocationOfByte(Amt.getStart()),
2659                       /*IsStringLocation*/true,
2660                       getSpecifierRange(startSpecifier, specifierLen),
2661                       fixit);
2662}
2663
2664void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS,
2665                                    const analyze_printf::OptionalFlag &flag,
2666                                    const char *startSpecifier,
2667                                    unsigned specifierLen) {
2668  // Warn about pointless flag with a fixit removal.
2669  const analyze_printf::PrintfConversionSpecifier &CS =
2670    FS.getConversionSpecifier();
2671  EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag)
2672                         << flag.toString() << CS.toString(),
2673                       getLocationOfByte(flag.getPosition()),
2674                       /*IsStringLocation*/true,
2675                       getSpecifierRange(startSpecifier, specifierLen),
2676                       FixItHint::CreateRemoval(
2677                         getSpecifierRange(flag.getPosition(), 1)));
2678}
2679
2680void CheckPrintfHandler::HandleIgnoredFlag(
2681                                const analyze_printf::PrintfSpecifier &FS,
2682                                const analyze_printf::OptionalFlag &ignoredFlag,
2683                                const analyze_printf::OptionalFlag &flag,
2684                                const char *startSpecifier,
2685                                unsigned specifierLen) {
2686  // Warn about ignored flag with a fixit removal.
2687  EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag)
2688                         << ignoredFlag.toString() << flag.toString(),
2689                       getLocationOfByte(ignoredFlag.getPosition()),
2690                       /*IsStringLocation*/true,
2691                       getSpecifierRange(startSpecifier, specifierLen),
2692                       FixItHint::CreateRemoval(
2693                         getSpecifierRange(ignoredFlag.getPosition(), 1)));
2694}
2695
2696// Determines if the specified is a C++ class or struct containing
2697// a member with the specified name and kind (e.g. a CXXMethodDecl named
2698// "c_str()").
2699template<typename MemberKind>
2700static llvm::SmallPtrSet<MemberKind*, 1>
2701CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) {
2702  const RecordType *RT = Ty->getAs<RecordType>();
2703  llvm::SmallPtrSet<MemberKind*, 1> Results;
2704
2705  if (!RT)
2706    return Results;
2707  const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
2708  if (!RD)
2709    return Results;
2710
2711  LookupResult R(S, &S.PP.getIdentifierTable().get(Name), SourceLocation(),
2712                 Sema::LookupMemberName);
2713
2714  // We just need to include all members of the right kind turned up by the
2715  // filter, at this point.
2716  if (S.LookupQualifiedName(R, RT->getDecl()))
2717    for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
2718      NamedDecl *decl = (*I)->getUnderlyingDecl();
2719      if (MemberKind *FK = dyn_cast<MemberKind>(decl))
2720        Results.insert(FK);
2721    }
2722  return Results;
2723}
2724
2725// Check if a (w)string was passed when a (w)char* was needed, and offer a
2726// better diagnostic if so. AT is assumed to be valid.
2727// Returns true when a c_str() conversion method is found.
2728bool CheckPrintfHandler::checkForCStrMembers(
2729    const analyze_printf::ArgType &AT, const Expr *E,
2730    const CharSourceRange &CSR) {
2731  typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet;
2732
2733  MethodSet Results =
2734      CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType());
2735
2736  for (MethodSet::iterator MI = Results.begin(), ME = Results.end();
2737       MI != ME; ++MI) {
2738    const CXXMethodDecl *Method = *MI;
2739    if (Method->getNumParams() == 0 &&
2740          AT.matchesType(S.Context, Method->getResultType())) {
2741      // FIXME: Suggest parens if the expression needs them.
2742      SourceLocation EndLoc =
2743          S.getPreprocessor().getLocForEndOfToken(E->getLocEnd());
2744      S.Diag(E->getLocStart(), diag::note_printf_c_str)
2745          << "c_str()"
2746          << FixItHint::CreateInsertion(EndLoc, ".c_str()");
2747      return true;
2748    }
2749  }
2750
2751  return false;
2752}
2753
2754bool
2755CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier
2756                                            &FS,
2757                                          const char *startSpecifier,
2758                                          unsigned specifierLen) {
2759
2760  using namespace analyze_format_string;
2761  using namespace analyze_printf;
2762  const PrintfConversionSpecifier &CS = FS.getConversionSpecifier();
2763
2764  if (FS.consumesDataArgument()) {
2765    if (atFirstArg) {
2766        atFirstArg = false;
2767        usesPositionalArgs = FS.usesPositionalArg();
2768    }
2769    else if (usesPositionalArgs != FS.usesPositionalArg()) {
2770      HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()),
2771                                        startSpecifier, specifierLen);
2772      return false;
2773    }
2774  }
2775
2776  // First check if the field width, precision, and conversion specifier
2777  // have matching data arguments.
2778  if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0,
2779                    startSpecifier, specifierLen)) {
2780    return false;
2781  }
2782
2783  if (!HandleAmount(FS.getPrecision(), /* precision */ 1,
2784                    startSpecifier, specifierLen)) {
2785    return false;
2786  }
2787
2788  if (!CS.consumesDataArgument()) {
2789    // FIXME: Technically specifying a precision or field width here
2790    // makes no sense.  Worth issuing a warning at some point.
2791    return true;
2792  }
2793
2794  // Consume the argument.
2795  unsigned argIndex = FS.getArgIndex();
2796  if (argIndex < NumDataArgs) {
2797    // The check to see if the argIndex is valid will come later.
2798    // We set the bit here because we may exit early from this
2799    // function if we encounter some other error.
2800    CoveredArgs.set(argIndex);
2801  }
2802
2803  // Check for using an Objective-C specific conversion specifier
2804  // in a non-ObjC literal.
2805  if (!ObjCContext && CS.isObjCArg()) {
2806    return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier,
2807                                                  specifierLen);
2808  }
2809
2810  // Check for invalid use of field width
2811  if (!FS.hasValidFieldWidth()) {
2812    HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0,
2813        startSpecifier, specifierLen);
2814  }
2815
2816  // Check for invalid use of precision
2817  if (!FS.hasValidPrecision()) {
2818    HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1,
2819        startSpecifier, specifierLen);
2820  }
2821
2822  // Check each flag does not conflict with any other component.
2823  if (!FS.hasValidThousandsGroupingPrefix())
2824    HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen);
2825  if (!FS.hasValidLeadingZeros())
2826    HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen);
2827  if (!FS.hasValidPlusPrefix())
2828    HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen);
2829  if (!FS.hasValidSpacePrefix())
2830    HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen);
2831  if (!FS.hasValidAlternativeForm())
2832    HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen);
2833  if (!FS.hasValidLeftJustified())
2834    HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen);
2835
2836  // Check that flags are not ignored by another flag
2837  if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+'
2838    HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(),
2839        startSpecifier, specifierLen);
2840  if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-'
2841    HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(),
2842            startSpecifier, specifierLen);
2843
2844  // Check the length modifier is valid with the given conversion specifier.
2845  if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo()))
2846    HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
2847                                diag::warn_format_nonsensical_length);
2848  else if (!FS.hasStandardLengthModifier())
2849    HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen);
2850  else if (!FS.hasStandardLengthConversionCombination())
2851    HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
2852                                diag::warn_format_non_standard_conversion_spec);
2853
2854  if (!FS.hasStandardConversionSpecifier(S.getLangOpts()))
2855    HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen);
2856
2857  // The remaining checks depend on the data arguments.
2858  if (HasVAListArg)
2859    return true;
2860
2861  if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
2862    return false;
2863
2864  const Expr *Arg = getDataArg(argIndex);
2865  if (!Arg)
2866    return true;
2867
2868  return checkFormatExpr(FS, startSpecifier, specifierLen, Arg);
2869}
2870
2871static bool requiresParensToAddCast(const Expr *E) {
2872  // FIXME: We should have a general way to reason about operator
2873  // precedence and whether parens are actually needed here.
2874  // Take care of a few common cases where they aren't.
2875  const Expr *Inside = E->IgnoreImpCasts();
2876  if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Inside))
2877    Inside = POE->getSyntacticForm()->IgnoreImpCasts();
2878
2879  switch (Inside->getStmtClass()) {
2880  case Stmt::ArraySubscriptExprClass:
2881  case Stmt::CallExprClass:
2882  case Stmt::CharacterLiteralClass:
2883  case Stmt::CXXBoolLiteralExprClass:
2884  case Stmt::DeclRefExprClass:
2885  case Stmt::FloatingLiteralClass:
2886  case Stmt::IntegerLiteralClass:
2887  case Stmt::MemberExprClass:
2888  case Stmt::ObjCArrayLiteralClass:
2889  case Stmt::ObjCBoolLiteralExprClass:
2890  case Stmt::ObjCBoxedExprClass:
2891  case Stmt::ObjCDictionaryLiteralClass:
2892  case Stmt::ObjCEncodeExprClass:
2893  case Stmt::ObjCIvarRefExprClass:
2894  case Stmt::ObjCMessageExprClass:
2895  case Stmt::ObjCPropertyRefExprClass:
2896  case Stmt::ObjCStringLiteralClass:
2897  case Stmt::ObjCSubscriptRefExprClass:
2898  case Stmt::ParenExprClass:
2899  case Stmt::StringLiteralClass:
2900  case Stmt::UnaryOperatorClass:
2901    return false;
2902  default:
2903    return true;
2904  }
2905}
2906
2907bool
2908CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
2909                                    const char *StartSpecifier,
2910                                    unsigned SpecifierLen,
2911                                    const Expr *E) {
2912  using namespace analyze_format_string;
2913  using namespace analyze_printf;
2914  // Now type check the data expression that matches the
2915  // format specifier.
2916  const analyze_printf::ArgType &AT = FS.getArgType(S.Context,
2917                                                    ObjCContext);
2918  if (!AT.isValid())
2919    return true;
2920
2921  QualType ExprTy = E->getType();
2922  while (const TypeOfExprType *TET = dyn_cast<TypeOfExprType>(ExprTy)) {
2923    ExprTy = TET->getUnderlyingExpr()->getType();
2924  }
2925
2926  if (AT.matchesType(S.Context, ExprTy))
2927    return true;
2928
2929  // Look through argument promotions for our error message's reported type.
2930  // This includes the integral and floating promotions, but excludes array
2931  // and function pointer decay; seeing that an argument intended to be a
2932  // string has type 'char [6]' is probably more confusing than 'char *'.
2933  if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2934    if (ICE->getCastKind() == CK_IntegralCast ||
2935        ICE->getCastKind() == CK_FloatingCast) {
2936      E = ICE->getSubExpr();
2937      ExprTy = E->getType();
2938
2939      // Check if we didn't match because of an implicit cast from a 'char'
2940      // or 'short' to an 'int'.  This is done because printf is a varargs
2941      // function.
2942      if (ICE->getType() == S.Context.IntTy ||
2943          ICE->getType() == S.Context.UnsignedIntTy) {
2944        // All further checking is done on the subexpression.
2945        if (AT.matchesType(S.Context, ExprTy))
2946          return true;
2947      }
2948    }
2949  } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) {
2950    // Special case for 'a', which has type 'int' in C.
2951    // Note, however, that we do /not/ want to treat multibyte constants like
2952    // 'MooV' as characters! This form is deprecated but still exists.
2953    if (ExprTy == S.Context.IntTy)
2954      if (llvm::isUIntN(S.Context.getCharWidth(), CL->getValue()))
2955        ExprTy = S.Context.CharTy;
2956  }
2957
2958  // %C in an Objective-C context prints a unichar, not a wchar_t.
2959  // If the argument is an integer of some kind, believe the %C and suggest
2960  // a cast instead of changing the conversion specifier.
2961  QualType IntendedTy = ExprTy;
2962  if (ObjCContext &&
2963      FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) {
2964    if (ExprTy->isIntegralOrUnscopedEnumerationType() &&
2965        !ExprTy->isCharType()) {
2966      // 'unichar' is defined as a typedef of unsigned short, but we should
2967      // prefer using the typedef if it is visible.
2968      IntendedTy = S.Context.UnsignedShortTy;
2969
2970      LookupResult Result(S, &S.Context.Idents.get("unichar"), E->getLocStart(),
2971                          Sema::LookupOrdinaryName);
2972      if (S.LookupName(Result, S.getCurScope())) {
2973        NamedDecl *ND = Result.getFoundDecl();
2974        if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
2975          if (TD->getUnderlyingType() == IntendedTy)
2976            IntendedTy = S.Context.getTypedefType(TD);
2977      }
2978    }
2979  }
2980
2981  // Special-case some of Darwin's platform-independence types by suggesting
2982  // casts to primitive types that are known to be large enough.
2983  bool ShouldNotPrintDirectly = false;
2984  if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
2985    // Use a 'while' to peel off layers of typedefs.
2986    QualType TyTy = IntendedTy;
2987    while (const TypedefType *UserTy = TyTy->getAs<TypedefType>()) {
2988      StringRef Name = UserTy->getDecl()->getName();
2989      QualType CastTy = llvm::StringSwitch<QualType>(Name)
2990        .Case("NSInteger", S.Context.LongTy)
2991        .Case("NSUInteger", S.Context.UnsignedLongTy)
2992        .Case("SInt32", S.Context.IntTy)
2993        .Case("UInt32", S.Context.UnsignedIntTy)
2994        .Default(QualType());
2995
2996      if (!CastTy.isNull()) {
2997        ShouldNotPrintDirectly = true;
2998        IntendedTy = CastTy;
2999        break;
3000      }
3001      TyTy = UserTy->desugar();
3002    }
3003  }
3004
3005  // We may be able to offer a FixItHint if it is a supported type.
3006  PrintfSpecifier fixedFS = FS;
3007  bool success = fixedFS.fixType(IntendedTy, S.getLangOpts(),
3008                                 S.Context, ObjCContext);
3009
3010  if (success) {
3011    // Get the fix string from the fixed format specifier
3012    SmallString<16> buf;
3013    llvm::raw_svector_ostream os(buf);
3014    fixedFS.toString(os);
3015
3016    CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen);
3017
3018    if (IntendedTy == ExprTy) {
3019      // In this case, the specifier is wrong and should be changed to match
3020      // the argument.
3021      EmitFormatDiagnostic(
3022        S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
3023          << AT.getRepresentativeTypeName(S.Context) << IntendedTy
3024          << E->getSourceRange(),
3025        E->getLocStart(),
3026        /*IsStringLocation*/false,
3027        SpecRange,
3028        FixItHint::CreateReplacement(SpecRange, os.str()));
3029
3030    } else {
3031      // The canonical type for formatting this value is different from the
3032      // actual type of the expression. (This occurs, for example, with Darwin's
3033      // NSInteger on 32-bit platforms, where it is typedef'd as 'int', but
3034      // should be printed as 'long' for 64-bit compatibility.)
3035      // Rather than emitting a normal format/argument mismatch, we want to
3036      // add a cast to the recommended type (and correct the format string
3037      // if necessary).
3038      SmallString<16> CastBuf;
3039      llvm::raw_svector_ostream CastFix(CastBuf);
3040      CastFix << "(";
3041      IntendedTy.print(CastFix, S.Context.getPrintingPolicy());
3042      CastFix << ")";
3043
3044      SmallVector<FixItHint,4> Hints;
3045      if (!AT.matchesType(S.Context, IntendedTy))
3046        Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str()));
3047
3048      if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) {
3049        // If there's already a cast present, just replace it.
3050        SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc());
3051        Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str()));
3052
3053      } else if (!requiresParensToAddCast(E)) {
3054        // If the expression has high enough precedence,
3055        // just write the C-style cast.
3056        Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(),
3057                                                   CastFix.str()));
3058      } else {
3059        // Otherwise, add parens around the expression as well as the cast.
3060        CastFix << "(";
3061        Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(),
3062                                                   CastFix.str()));
3063
3064        SourceLocation After = S.PP.getLocForEndOfToken(E->getLocEnd());
3065        Hints.push_back(FixItHint::CreateInsertion(After, ")"));
3066      }
3067
3068      if (ShouldNotPrintDirectly) {
3069        // The expression has a type that should not be printed directly.
3070        // We extract the name from the typedef because we don't want to show
3071        // the underlying type in the diagnostic.
3072        StringRef Name = cast<TypedefType>(ExprTy)->getDecl()->getName();
3073
3074        EmitFormatDiagnostic(S.PDiag(diag::warn_format_argument_needs_cast)
3075                               << Name << IntendedTy
3076                               << E->getSourceRange(),
3077                             E->getLocStart(), /*IsStringLocation=*/false,
3078                             SpecRange, Hints);
3079      } else {
3080        // In this case, the expression could be printed using a different
3081        // specifier, but we've decided that the specifier is probably correct
3082        // and we should cast instead. Just use the normal warning message.
3083        EmitFormatDiagnostic(
3084          S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
3085            << AT.getRepresentativeTypeName(S.Context) << ExprTy
3086            << E->getSourceRange(),
3087          E->getLocStart(), /*IsStringLocation*/false,
3088          SpecRange, Hints);
3089      }
3090    }
3091  } else {
3092    const CharSourceRange &CSR = getSpecifierRange(StartSpecifier,
3093                                                   SpecifierLen);
3094    // Since the warning for passing non-POD types to variadic functions
3095    // was deferred until now, we emit a warning for non-POD
3096    // arguments here.
3097    if (S.isValidVarArgType(ExprTy) == Sema::VAK_Invalid) {
3098      unsigned DiagKind;
3099      if (ExprTy->isObjCObjectType())
3100        DiagKind = diag::err_cannot_pass_objc_interface_to_vararg_format;
3101      else
3102        DiagKind = diag::warn_non_pod_vararg_with_format_string;
3103
3104      EmitFormatDiagnostic(
3105        S.PDiag(DiagKind)
3106          << S.getLangOpts().CPlusPlus11
3107          << ExprTy
3108          << CallType
3109          << AT.getRepresentativeTypeName(S.Context)
3110          << CSR
3111          << E->getSourceRange(),
3112        E->getLocStart(), /*IsStringLocation*/false, CSR);
3113
3114      checkForCStrMembers(AT, E, CSR);
3115    } else
3116      EmitFormatDiagnostic(
3117        S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
3118          << AT.getRepresentativeTypeName(S.Context) << ExprTy
3119          << CSR
3120          << E->getSourceRange(),
3121        E->getLocStart(), /*IsStringLocation*/false, CSR);
3122  }
3123
3124  return true;
3125}
3126
3127//===--- CHECK: Scanf format string checking ------------------------------===//
3128
3129namespace {
3130class CheckScanfHandler : public CheckFormatHandler {
3131public:
3132  CheckScanfHandler(Sema &s, const StringLiteral *fexpr,
3133                    const Expr *origFormatExpr, unsigned firstDataArg,
3134                    unsigned numDataArgs, const char *beg, bool hasVAListArg,
3135                    ArrayRef<const Expr *> Args,
3136                    unsigned formatIdx, bool inFunctionCall,
3137                    Sema::VariadicCallType CallType)
3138  : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg,
3139                       numDataArgs, beg, hasVAListArg,
3140                       Args, formatIdx, inFunctionCall, CallType)
3141  {}
3142
3143  bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS,
3144                            const char *startSpecifier,
3145                            unsigned specifierLen);
3146
3147  bool HandleInvalidScanfConversionSpecifier(
3148          const analyze_scanf::ScanfSpecifier &FS,
3149          const char *startSpecifier,
3150          unsigned specifierLen);
3151
3152  void HandleIncompleteScanList(const char *start, const char *end);
3153};
3154}
3155
3156void CheckScanfHandler::HandleIncompleteScanList(const char *start,
3157                                                 const char *end) {
3158  EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete),
3159                       getLocationOfByte(end), /*IsStringLocation*/true,
3160                       getSpecifierRange(start, end - start));
3161}
3162
3163bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier(
3164                                        const analyze_scanf::ScanfSpecifier &FS,
3165                                        const char *startSpecifier,
3166                                        unsigned specifierLen) {
3167
3168  const analyze_scanf::ScanfConversionSpecifier &CS =
3169    FS.getConversionSpecifier();
3170
3171  return HandleInvalidConversionSpecifier(FS.getArgIndex(),
3172                                          getLocationOfByte(CS.getStart()),
3173                                          startSpecifier, specifierLen,
3174                                          CS.getStart(), CS.getLength());
3175}
3176
3177bool CheckScanfHandler::HandleScanfSpecifier(
3178                                       const analyze_scanf::ScanfSpecifier &FS,
3179                                       const char *startSpecifier,
3180                                       unsigned specifierLen) {
3181
3182  using namespace analyze_scanf;
3183  using namespace analyze_format_string;
3184
3185  const ScanfConversionSpecifier &CS = FS.getConversionSpecifier();
3186
3187  // Handle case where '%' and '*' don't consume an argument.  These shouldn't
3188  // be used to decide if we are using positional arguments consistently.
3189  if (FS.consumesDataArgument()) {
3190    if (atFirstArg) {
3191      atFirstArg = false;
3192      usesPositionalArgs = FS.usesPositionalArg();
3193    }
3194    else if (usesPositionalArgs != FS.usesPositionalArg()) {
3195      HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()),
3196                                        startSpecifier, specifierLen);
3197      return false;
3198    }
3199  }
3200
3201  // Check if the field with is non-zero.
3202  const OptionalAmount &Amt = FS.getFieldWidth();
3203  if (Amt.getHowSpecified() == OptionalAmount::Constant) {
3204    if (Amt.getConstantAmount() == 0) {
3205      const CharSourceRange &R = getSpecifierRange(Amt.getStart(),
3206                                                   Amt.getConstantLength());
3207      EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width),
3208                           getLocationOfByte(Amt.getStart()),
3209                           /*IsStringLocation*/true, R,
3210                           FixItHint::CreateRemoval(R));
3211    }
3212  }
3213
3214  if (!FS.consumesDataArgument()) {
3215    // FIXME: Technically specifying a precision or field width here
3216    // makes no sense.  Worth issuing a warning at some point.
3217    return true;
3218  }
3219
3220  // Consume the argument.
3221  unsigned argIndex = FS.getArgIndex();
3222  if (argIndex < NumDataArgs) {
3223      // The check to see if the argIndex is valid will come later.
3224      // We set the bit here because we may exit early from this
3225      // function if we encounter some other error.
3226    CoveredArgs.set(argIndex);
3227  }
3228
3229  // Check the length modifier is valid with the given conversion specifier.
3230  if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo()))
3231    HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
3232                                diag::warn_format_nonsensical_length);
3233  else if (!FS.hasStandardLengthModifier())
3234    HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen);
3235  else if (!FS.hasStandardLengthConversionCombination())
3236    HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
3237                                diag::warn_format_non_standard_conversion_spec);
3238
3239  if (!FS.hasStandardConversionSpecifier(S.getLangOpts()))
3240    HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen);
3241
3242  // The remaining checks depend on the data arguments.
3243  if (HasVAListArg)
3244    return true;
3245
3246  if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
3247    return false;
3248
3249  // Check that the argument type matches the format specifier.
3250  const Expr *Ex = getDataArg(argIndex);
3251  if (!Ex)
3252    return true;
3253
3254  const analyze_format_string::ArgType &AT = FS.getArgType(S.Context);
3255  if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType())) {
3256    ScanfSpecifier fixedFS = FS;
3257    bool success = fixedFS.fixType(Ex->getType(), S.getLangOpts(),
3258                                   S.Context);
3259
3260    if (success) {
3261      // Get the fix string from the fixed format specifier.
3262      SmallString<128> buf;
3263      llvm::raw_svector_ostream os(buf);
3264      fixedFS.toString(os);
3265
3266      EmitFormatDiagnostic(
3267        S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
3268          << AT.getRepresentativeTypeName(S.Context) << Ex->getType()
3269          << Ex->getSourceRange(),
3270        Ex->getLocStart(),
3271        /*IsStringLocation*/false,
3272        getSpecifierRange(startSpecifier, specifierLen),
3273        FixItHint::CreateReplacement(
3274          getSpecifierRange(startSpecifier, specifierLen),
3275          os.str()));
3276    } else {
3277      EmitFormatDiagnostic(
3278        S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
3279          << AT.getRepresentativeTypeName(S.Context) << Ex->getType()
3280          << Ex->getSourceRange(),
3281        Ex->getLocStart(),
3282        /*IsStringLocation*/false,
3283        getSpecifierRange(startSpecifier, specifierLen));
3284    }
3285  }
3286
3287  return true;
3288}
3289
3290void Sema::CheckFormatString(const StringLiteral *FExpr,
3291                             const Expr *OrigFormatExpr,
3292                             ArrayRef<const Expr *> Args,
3293                             bool HasVAListArg, unsigned format_idx,
3294                             unsigned firstDataArg, FormatStringType Type,
3295                             bool inFunctionCall, VariadicCallType CallType) {
3296
3297  // CHECK: is the format string a wide literal?
3298  if (!FExpr->isAscii() && !FExpr->isUTF8()) {
3299    CheckFormatHandler::EmitFormatDiagnostic(
3300      *this, inFunctionCall, Args[format_idx],
3301      PDiag(diag::warn_format_string_is_wide_literal), FExpr->getLocStart(),
3302      /*IsStringLocation*/true, OrigFormatExpr->getSourceRange());
3303    return;
3304  }
3305
3306  // Str - The format string.  NOTE: this is NOT null-terminated!
3307  StringRef StrRef = FExpr->getString();
3308  const char *Str = StrRef.data();
3309  unsigned StrLen = StrRef.size();
3310  const unsigned numDataArgs = Args.size() - firstDataArg;
3311
3312  // CHECK: empty format string?
3313  if (StrLen == 0 && numDataArgs > 0) {
3314    CheckFormatHandler::EmitFormatDiagnostic(
3315      *this, inFunctionCall, Args[format_idx],
3316      PDiag(diag::warn_empty_format_string), FExpr->getLocStart(),
3317      /*IsStringLocation*/true, OrigFormatExpr->getSourceRange());
3318    return;
3319  }
3320
3321  if (Type == FST_Printf || Type == FST_NSString) {
3322    CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg,
3323                         numDataArgs, (Type == FST_NSString),
3324                         Str, HasVAListArg, Args, format_idx,
3325                         inFunctionCall, CallType);
3326
3327    if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen,
3328                                                  getLangOpts(),
3329                                                  Context.getTargetInfo()))
3330      H.DoneProcessing();
3331  } else if (Type == FST_Scanf) {
3332    CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, numDataArgs,
3333                        Str, HasVAListArg, Args, format_idx,
3334                        inFunctionCall, CallType);
3335
3336    if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen,
3337                                                 getLangOpts(),
3338                                                 Context.getTargetInfo()))
3339      H.DoneProcessing();
3340  } // TODO: handle other formats
3341}
3342
3343//===--- CHECK: Standard memory functions ---------------------------------===//
3344
3345/// \brief Determine whether the given type is a dynamic class type (e.g.,
3346/// whether it has a vtable).
3347static bool isDynamicClassType(QualType T) {
3348  if (CXXRecordDecl *Record = T->getAsCXXRecordDecl())
3349    if (CXXRecordDecl *Definition = Record->getDefinition())
3350      if (Definition->isDynamicClass())
3351        return true;
3352
3353  return false;
3354}
3355
3356/// \brief If E is a sizeof expression, returns its argument expression,
3357/// otherwise returns NULL.
3358static const Expr *getSizeOfExprArg(const Expr* E) {
3359  if (const UnaryExprOrTypeTraitExpr *SizeOf =
3360      dyn_cast<UnaryExprOrTypeTraitExpr>(E))
3361    if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType())
3362      return SizeOf->getArgumentExpr()->IgnoreParenImpCasts();
3363
3364  return 0;
3365}
3366
3367/// \brief If E is a sizeof expression, returns its argument type.
3368static QualType getSizeOfArgType(const Expr* E) {
3369  if (const UnaryExprOrTypeTraitExpr *SizeOf =
3370      dyn_cast<UnaryExprOrTypeTraitExpr>(E))
3371    if (SizeOf->getKind() == clang::UETT_SizeOf)
3372      return SizeOf->getTypeOfArgument();
3373
3374  return QualType();
3375}
3376
3377/// \brief Check for dangerous or invalid arguments to memset().
3378///
3379/// This issues warnings on known problematic, dangerous or unspecified
3380/// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp'
3381/// function calls.
3382///
3383/// \param Call The call expression to diagnose.
3384void Sema::CheckMemaccessArguments(const CallExpr *Call,
3385                                   unsigned BId,
3386                                   IdentifierInfo *FnName) {
3387  assert(BId != 0);
3388
3389  // It is possible to have a non-standard definition of memset.  Validate
3390  // we have enough arguments, and if not, abort further checking.
3391  unsigned ExpectedNumArgs = (BId == Builtin::BIstrndup ? 2 : 3);
3392  if (Call->getNumArgs() < ExpectedNumArgs)
3393    return;
3394
3395  unsigned LastArg = (BId == Builtin::BImemset ||
3396                      BId == Builtin::BIstrndup ? 1 : 2);
3397  unsigned LenArg = (BId == Builtin::BIstrndup ? 1 : 2);
3398  const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts();
3399
3400  // We have special checking when the length is a sizeof expression.
3401  QualType SizeOfArgTy = getSizeOfArgType(LenExpr);
3402  const Expr *SizeOfArg = getSizeOfExprArg(LenExpr);
3403  llvm::FoldingSetNodeID SizeOfArgID;
3404
3405  for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) {
3406    const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts();
3407    SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange();
3408
3409    QualType DestTy = Dest->getType();
3410    if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) {
3411      QualType PointeeTy = DestPtrTy->getPointeeType();
3412
3413      // Never warn about void type pointers. This can be used to suppress
3414      // false positives.
3415      if (PointeeTy->isVoidType())
3416        continue;
3417
3418      // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by
3419      // actually comparing the expressions for equality. Because computing the
3420      // expression IDs can be expensive, we only do this if the diagnostic is
3421      // enabled.
3422      if (SizeOfArg &&
3423          Diags.getDiagnosticLevel(diag::warn_sizeof_pointer_expr_memaccess,
3424                                   SizeOfArg->getExprLoc())) {
3425        // We only compute IDs for expressions if the warning is enabled, and
3426        // cache the sizeof arg's ID.
3427        if (SizeOfArgID == llvm::FoldingSetNodeID())
3428          SizeOfArg->Profile(SizeOfArgID, Context, true);
3429        llvm::FoldingSetNodeID DestID;
3430        Dest->Profile(DestID, Context, true);
3431        if (DestID == SizeOfArgID) {
3432          // TODO: For strncpy() and friends, this could suggest sizeof(dst)
3433          //       over sizeof(src) as well.
3434          unsigned ActionIdx = 0; // Default is to suggest dereferencing.
3435          StringRef ReadableName = FnName->getName();
3436
3437          if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest))
3438            if (UnaryOp->getOpcode() == UO_AddrOf)
3439              ActionIdx = 1; // If its an address-of operator, just remove it.
3440          if (!PointeeTy->isIncompleteType() &&
3441              (Context.getTypeSize(PointeeTy) == Context.getCharWidth()))
3442            ActionIdx = 2; // If the pointee's size is sizeof(char),
3443                           // suggest an explicit length.
3444
3445          // If the function is defined as a builtin macro, do not show macro
3446          // expansion.
3447          SourceLocation SL = SizeOfArg->getExprLoc();
3448          SourceRange DSR = Dest->getSourceRange();
3449          SourceRange SSR = SizeOfArg->getSourceRange();
3450          SourceManager &SM  = PP.getSourceManager();
3451
3452          if (SM.isMacroArgExpansion(SL)) {
3453            ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts);
3454            SL = SM.getSpellingLoc(SL);
3455            DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()),
3456                             SM.getSpellingLoc(DSR.getEnd()));
3457            SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()),
3458                             SM.getSpellingLoc(SSR.getEnd()));
3459          }
3460
3461          DiagRuntimeBehavior(SL, SizeOfArg,
3462                              PDiag(diag::warn_sizeof_pointer_expr_memaccess)
3463                                << ReadableName
3464                                << PointeeTy
3465                                << DestTy
3466                                << DSR
3467                                << SSR);
3468          DiagRuntimeBehavior(SL, SizeOfArg,
3469                         PDiag(diag::warn_sizeof_pointer_expr_memaccess_note)
3470                                << ActionIdx
3471                                << SSR);
3472
3473          break;
3474        }
3475      }
3476
3477      // Also check for cases where the sizeof argument is the exact same
3478      // type as the memory argument, and where it points to a user-defined
3479      // record type.
3480      if (SizeOfArgTy != QualType()) {
3481        if (PointeeTy->isRecordType() &&
3482            Context.typesAreCompatible(SizeOfArgTy, DestTy)) {
3483          DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest,
3484                              PDiag(diag::warn_sizeof_pointer_type_memaccess)
3485                                << FnName << SizeOfArgTy << ArgIdx
3486                                << PointeeTy << Dest->getSourceRange()
3487                                << LenExpr->getSourceRange());
3488          break;
3489        }
3490      }
3491
3492      // Always complain about dynamic classes.
3493      if (isDynamicClassType(PointeeTy)) {
3494
3495        unsigned OperationType = 0;
3496        // "overwritten" if we're warning about the destination for any call
3497        // but memcmp; otherwise a verb appropriate to the call.
3498        if (ArgIdx != 0 || BId == Builtin::BImemcmp) {
3499          if (BId == Builtin::BImemcpy)
3500            OperationType = 1;
3501          else if(BId == Builtin::BImemmove)
3502            OperationType = 2;
3503          else if (BId == Builtin::BImemcmp)
3504            OperationType = 3;
3505        }
3506
3507        DiagRuntimeBehavior(
3508          Dest->getExprLoc(), Dest,
3509          PDiag(diag::warn_dyn_class_memaccess)
3510            << (BId == Builtin::BImemcmp ? ArgIdx + 2 : ArgIdx)
3511            << FnName << PointeeTy
3512            << OperationType
3513            << Call->getCallee()->getSourceRange());
3514      } else if (PointeeTy.hasNonTrivialObjCLifetime() &&
3515               BId != Builtin::BImemset)
3516        DiagRuntimeBehavior(
3517          Dest->getExprLoc(), Dest,
3518          PDiag(diag::warn_arc_object_memaccess)
3519            << ArgIdx << FnName << PointeeTy
3520            << Call->getCallee()->getSourceRange());
3521      else
3522        continue;
3523
3524      DiagRuntimeBehavior(
3525        Dest->getExprLoc(), Dest,
3526        PDiag(diag::note_bad_memaccess_silence)
3527          << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)"));
3528      break;
3529    }
3530  }
3531}
3532
3533// A little helper routine: ignore addition and subtraction of integer literals.
3534// This intentionally does not ignore all integer constant expressions because
3535// we don't want to remove sizeof().
3536static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) {
3537  Ex = Ex->IgnoreParenCasts();
3538
3539  for (;;) {
3540    const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex);
3541    if (!BO || !BO->isAdditiveOp())
3542      break;
3543
3544    const Expr *RHS = BO->getRHS()->IgnoreParenCasts();
3545    const Expr *LHS = BO->getLHS()->IgnoreParenCasts();
3546
3547    if (isa<IntegerLiteral>(RHS))
3548      Ex = LHS;
3549    else if (isa<IntegerLiteral>(LHS))
3550      Ex = RHS;
3551    else
3552      break;
3553  }
3554
3555  return Ex;
3556}
3557
3558static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty,
3559                                                      ASTContext &Context) {
3560  // Only handle constant-sized or VLAs, but not flexible members.
3561  if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) {
3562    // Only issue the FIXIT for arrays of size > 1.
3563    if (CAT->getSize().getSExtValue() <= 1)
3564      return false;
3565  } else if (!Ty->isVariableArrayType()) {
3566    return false;
3567  }
3568  return true;
3569}
3570
3571// Warn if the user has made the 'size' argument to strlcpy or strlcat
3572// be the size of the source, instead of the destination.
3573void Sema::CheckStrlcpycatArguments(const CallExpr *Call,
3574                                    IdentifierInfo *FnName) {
3575
3576  // Don't crash if the user has the wrong number of arguments
3577  if (Call->getNumArgs() != 3)
3578    return;
3579
3580  const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context);
3581  const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context);
3582  const Expr *CompareWithSrc = NULL;
3583
3584  // Look for 'strlcpy(dst, x, sizeof(x))'
3585  if (const Expr *Ex = getSizeOfExprArg(SizeArg))
3586    CompareWithSrc = Ex;
3587  else {
3588    // Look for 'strlcpy(dst, x, strlen(x))'
3589    if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) {
3590      if (SizeCall->isBuiltinCall() == Builtin::BIstrlen
3591          && SizeCall->getNumArgs() == 1)
3592        CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context);
3593    }
3594  }
3595
3596  if (!CompareWithSrc)
3597    return;
3598
3599  // Determine if the argument to sizeof/strlen is equal to the source
3600  // argument.  In principle there's all kinds of things you could do
3601  // here, for instance creating an == expression and evaluating it with
3602  // EvaluateAsBooleanCondition, but this uses a more direct technique:
3603  const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg);
3604  if (!SrcArgDRE)
3605    return;
3606
3607  const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc);
3608  if (!CompareWithSrcDRE ||
3609      SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl())
3610    return;
3611
3612  const Expr *OriginalSizeArg = Call->getArg(2);
3613  Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size)
3614    << OriginalSizeArg->getSourceRange() << FnName;
3615
3616  // Output a FIXIT hint if the destination is an array (rather than a
3617  // pointer to an array).  This could be enhanced to handle some
3618  // pointers if we know the actual size, like if DstArg is 'array+2'
3619  // we could say 'sizeof(array)-2'.
3620  const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts();
3621  if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context))
3622    return;
3623
3624  SmallString<128> sizeString;
3625  llvm::raw_svector_ostream OS(sizeString);
3626  OS << "sizeof(";
3627  DstArg->printPretty(OS, 0, getPrintingPolicy());
3628  OS << ")";
3629
3630  Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size)
3631    << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(),
3632                                    OS.str());
3633}
3634
3635/// Check if two expressions refer to the same declaration.
3636static bool referToTheSameDecl(const Expr *E1, const Expr *E2) {
3637  if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1))
3638    if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2))
3639      return D1->getDecl() == D2->getDecl();
3640  return false;
3641}
3642
3643static const Expr *getStrlenExprArg(const Expr *E) {
3644  if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
3645    const FunctionDecl *FD = CE->getDirectCallee();
3646    if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen)
3647      return 0;
3648    return CE->getArg(0)->IgnoreParenCasts();
3649  }
3650  return 0;
3651}
3652
3653// Warn on anti-patterns as the 'size' argument to strncat.
3654// The correct size argument should look like following:
3655//   strncat(dst, src, sizeof(dst) - strlen(dest) - 1);
3656void Sema::CheckStrncatArguments(const CallExpr *CE,
3657                                 IdentifierInfo *FnName) {
3658  // Don't crash if the user has the wrong number of arguments.
3659  if (CE->getNumArgs() < 3)
3660    return;
3661  const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts();
3662  const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts();
3663  const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts();
3664
3665  // Identify common expressions, which are wrongly used as the size argument
3666  // to strncat and may lead to buffer overflows.
3667  unsigned PatternType = 0;
3668  if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) {
3669    // - sizeof(dst)
3670    if (referToTheSameDecl(SizeOfArg, DstArg))
3671      PatternType = 1;
3672    // - sizeof(src)
3673    else if (referToTheSameDecl(SizeOfArg, SrcArg))
3674      PatternType = 2;
3675  } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) {
3676    if (BE->getOpcode() == BO_Sub) {
3677      const Expr *L = BE->getLHS()->IgnoreParenCasts();
3678      const Expr *R = BE->getRHS()->IgnoreParenCasts();
3679      // - sizeof(dst) - strlen(dst)
3680      if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) &&
3681          referToTheSameDecl(DstArg, getStrlenExprArg(R)))
3682        PatternType = 1;
3683      // - sizeof(src) - (anything)
3684      else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L)))
3685        PatternType = 2;
3686    }
3687  }
3688
3689  if (PatternType == 0)
3690    return;
3691
3692  // Generate the diagnostic.
3693  SourceLocation SL = LenArg->getLocStart();
3694  SourceRange SR = LenArg->getSourceRange();
3695  SourceManager &SM  = PP.getSourceManager();
3696
3697  // If the function is defined as a builtin macro, do not show macro expansion.
3698  if (SM.isMacroArgExpansion(SL)) {
3699    SL = SM.getSpellingLoc(SL);
3700    SR = SourceRange(SM.getSpellingLoc(SR.getBegin()),
3701                     SM.getSpellingLoc(SR.getEnd()));
3702  }
3703
3704  // Check if the destination is an array (rather than a pointer to an array).
3705  QualType DstTy = DstArg->getType();
3706  bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy,
3707                                                                    Context);
3708  if (!isKnownSizeArray) {
3709    if (PatternType == 1)
3710      Diag(SL, diag::warn_strncat_wrong_size) << SR;
3711    else
3712      Diag(SL, diag::warn_strncat_src_size) << SR;
3713    return;
3714  }
3715
3716  if (PatternType == 1)
3717    Diag(SL, diag::warn_strncat_large_size) << SR;
3718  else
3719    Diag(SL, diag::warn_strncat_src_size) << SR;
3720
3721  SmallString<128> sizeString;
3722  llvm::raw_svector_ostream OS(sizeString);
3723  OS << "sizeof(";
3724  DstArg->printPretty(OS, 0, getPrintingPolicy());
3725  OS << ") - ";
3726  OS << "strlen(";
3727  DstArg->printPretty(OS, 0, getPrintingPolicy());
3728  OS << ") - 1";
3729
3730  Diag(SL, diag::note_strncat_wrong_size)
3731    << FixItHint::CreateReplacement(SR, OS.str());
3732}
3733
3734//===--- CHECK: Return Address of Stack Variable --------------------------===//
3735
3736static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars,
3737                     Decl *ParentDecl);
3738static Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars,
3739                      Decl *ParentDecl);
3740
3741/// CheckReturnStackAddr - Check if a return statement returns the address
3742///   of a stack variable.
3743void
3744Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
3745                           SourceLocation ReturnLoc) {
3746
3747  Expr *stackE = 0;
3748  SmallVector<DeclRefExpr *, 8> refVars;
3749
3750  // Perform checking for returned stack addresses, local blocks,
3751  // label addresses or references to temporaries.
3752  if (lhsType->isPointerType() ||
3753      (!getLangOpts().ObjCAutoRefCount && lhsType->isBlockPointerType())) {
3754    stackE = EvalAddr(RetValExp, refVars, /*ParentDecl=*/0);
3755  } else if (lhsType->isReferenceType()) {
3756    stackE = EvalVal(RetValExp, refVars, /*ParentDecl=*/0);
3757  }
3758
3759  if (stackE == 0)
3760    return; // Nothing suspicious was found.
3761
3762  SourceLocation diagLoc;
3763  SourceRange diagRange;
3764  if (refVars.empty()) {
3765    diagLoc = stackE->getLocStart();
3766    diagRange = stackE->getSourceRange();
3767  } else {
3768    // We followed through a reference variable. 'stackE' contains the
3769    // problematic expression but we will warn at the return statement pointing
3770    // at the reference variable. We will later display the "trail" of
3771    // reference variables using notes.
3772    diagLoc = refVars[0]->getLocStart();
3773    diagRange = refVars[0]->getSourceRange();
3774  }
3775
3776  if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var.
3777    Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref
3778                                             : diag::warn_ret_stack_addr)
3779     << DR->getDecl()->getDeclName() << diagRange;
3780  } else if (isa<BlockExpr>(stackE)) { // local block.
3781    Diag(diagLoc, diag::err_ret_local_block) << diagRange;
3782  } else if (isa<AddrLabelExpr>(stackE)) { // address of label.
3783    Diag(diagLoc, diag::warn_ret_addr_label) << diagRange;
3784  } else { // local temporary.
3785    Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref
3786                                             : diag::warn_ret_local_temp_addr)
3787     << diagRange;
3788  }
3789
3790  // Display the "trail" of reference variables that we followed until we
3791  // found the problematic expression using notes.
3792  for (unsigned i = 0, e = refVars.size(); i != e; ++i) {
3793    VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl());
3794    // If this var binds to another reference var, show the range of the next
3795    // var, otherwise the var binds to the problematic expression, in which case
3796    // show the range of the expression.
3797    SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange()
3798                                  : stackE->getSourceRange();
3799    Diag(VD->getLocation(), diag::note_ref_var_local_bind)
3800      << VD->getDeclName() << range;
3801  }
3802}
3803
3804/// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that
3805///  check if the expression in a return statement evaluates to an address
3806///  to a location on the stack, a local block, an address of a label, or a
3807///  reference to local temporary. The recursion is used to traverse the
3808///  AST of the return expression, with recursion backtracking when we
3809///  encounter a subexpression that (1) clearly does not lead to one of the
3810///  above problematic expressions (2) is something we cannot determine leads to
3811///  a problematic expression based on such local checking.
3812///
3813///  Both EvalAddr and EvalVal follow through reference variables to evaluate
3814///  the expression that they point to. Such variables are added to the
3815///  'refVars' vector so that we know what the reference variable "trail" was.
3816///
3817///  EvalAddr processes expressions that are pointers that are used as
3818///  references (and not L-values).  EvalVal handles all other values.
3819///  At the base case of the recursion is a check for the above problematic
3820///  expressions.
3821///
3822///  This implementation handles:
3823///
3824///   * pointer-to-pointer casts
3825///   * implicit conversions from array references to pointers
3826///   * taking the address of fields
3827///   * arbitrary interplay between "&" and "*" operators
3828///   * pointer arithmetic from an address of a stack variable
3829///   * taking the address of an array element where the array is on the stack
3830static Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars,
3831                      Decl *ParentDecl) {
3832  if (E->isTypeDependent())
3833      return NULL;
3834
3835  // We should only be called for evaluating pointer expressions.
3836  assert((E->getType()->isAnyPointerType() ||
3837          E->getType()->isBlockPointerType() ||
3838          E->getType()->isObjCQualifiedIdType()) &&
3839         "EvalAddr only works on pointers");
3840
3841  E = E->IgnoreParens();
3842
3843  // Our "symbolic interpreter" is just a dispatch off the currently
3844  // viewed AST node.  We then recursively traverse the AST by calling
3845  // EvalAddr and EvalVal appropriately.
3846  switch (E->getStmtClass()) {
3847  case Stmt::DeclRefExprClass: {
3848    DeclRefExpr *DR = cast<DeclRefExpr>(E);
3849
3850    if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
3851      // If this is a reference variable, follow through to the expression that
3852      // it points to.
3853      if (V->hasLocalStorage() &&
3854          V->getType()->isReferenceType() && V->hasInit()) {
3855        // Add the reference variable to the "trail".
3856        refVars.push_back(DR);
3857        return EvalAddr(V->getInit(), refVars, ParentDecl);
3858      }
3859
3860    return NULL;
3861  }
3862
3863  case Stmt::UnaryOperatorClass: {
3864    // The only unary operator that make sense to handle here
3865    // is AddrOf.  All others don't make sense as pointers.
3866    UnaryOperator *U = cast<UnaryOperator>(E);
3867
3868    if (U->getOpcode() == UO_AddrOf)
3869      return EvalVal(U->getSubExpr(), refVars, ParentDecl);
3870    else
3871      return NULL;
3872  }
3873
3874  case Stmt::BinaryOperatorClass: {
3875    // Handle pointer arithmetic.  All other binary operators are not valid
3876    // in this context.
3877    BinaryOperator *B = cast<BinaryOperator>(E);
3878    BinaryOperatorKind op = B->getOpcode();
3879
3880    if (op != BO_Add && op != BO_Sub)
3881      return NULL;
3882
3883    Expr *Base = B->getLHS();
3884
3885    // Determine which argument is the real pointer base.  It could be
3886    // the RHS argument instead of the LHS.
3887    if (!Base->getType()->isPointerType()) Base = B->getRHS();
3888
3889    assert (Base->getType()->isPointerType());
3890    return EvalAddr(Base, refVars, ParentDecl);
3891  }
3892
3893  // For conditional operators we need to see if either the LHS or RHS are
3894  // valid DeclRefExpr*s.  If one of them is valid, we return it.
3895  case Stmt::ConditionalOperatorClass: {
3896    ConditionalOperator *C = cast<ConditionalOperator>(E);
3897
3898    // Handle the GNU extension for missing LHS.
3899    if (Expr *lhsExpr = C->getLHS()) {
3900    // In C++, we can have a throw-expression, which has 'void' type.
3901      if (!lhsExpr->getType()->isVoidType())
3902        if (Expr* LHS = EvalAddr(lhsExpr, refVars, ParentDecl))
3903          return LHS;
3904    }
3905
3906    // In C++, we can have a throw-expression, which has 'void' type.
3907    if (C->getRHS()->getType()->isVoidType())
3908      return NULL;
3909
3910    return EvalAddr(C->getRHS(), refVars, ParentDecl);
3911  }
3912
3913  case Stmt::BlockExprClass:
3914    if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures())
3915      return E; // local block.
3916    return NULL;
3917
3918  case Stmt::AddrLabelExprClass:
3919    return E; // address of label.
3920
3921  case Stmt::ExprWithCleanupsClass:
3922    return EvalAddr(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,
3923                    ParentDecl);
3924
3925  // For casts, we need to handle conversions from arrays to
3926  // pointer values, and pointer-to-pointer conversions.
3927  case Stmt::ImplicitCastExprClass:
3928  case Stmt::CStyleCastExprClass:
3929  case Stmt::CXXFunctionalCastExprClass:
3930  case Stmt::ObjCBridgedCastExprClass:
3931  case Stmt::CXXStaticCastExprClass:
3932  case Stmt::CXXDynamicCastExprClass:
3933  case Stmt::CXXConstCastExprClass:
3934  case Stmt::CXXReinterpretCastExprClass: {
3935    Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
3936    switch (cast<CastExpr>(E)->getCastKind()) {
3937    case CK_BitCast:
3938    case CK_LValueToRValue:
3939    case CK_NoOp:
3940    case CK_BaseToDerived:
3941    case CK_DerivedToBase:
3942    case CK_UncheckedDerivedToBase:
3943    case CK_Dynamic:
3944    case CK_CPointerToObjCPointerCast:
3945    case CK_BlockPointerToObjCPointerCast:
3946    case CK_AnyPointerToBlockPointerCast:
3947      return EvalAddr(SubExpr, refVars, ParentDecl);
3948
3949    case CK_ArrayToPointerDecay:
3950      return EvalVal(SubExpr, refVars, ParentDecl);
3951
3952    default:
3953      return 0;
3954    }
3955  }
3956
3957  case Stmt::MaterializeTemporaryExprClass:
3958    if (Expr *Result = EvalAddr(
3959                         cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(),
3960                                refVars, ParentDecl))
3961      return Result;
3962
3963    return E;
3964
3965  // Everything else: we simply don't reason about them.
3966  default:
3967    return NULL;
3968  }
3969}
3970
3971
3972///  EvalVal - This function is complements EvalAddr in the mutual recursion.
3973///   See the comments for EvalAddr for more details.
3974static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars,
3975                     Decl *ParentDecl) {
3976do {
3977  // We should only be called for evaluating non-pointer expressions, or
3978  // expressions with a pointer type that are not used as references but instead
3979  // are l-values (e.g., DeclRefExpr with a pointer type).
3980
3981  // Our "symbolic interpreter" is just a dispatch off the currently
3982  // viewed AST node.  We then recursively traverse the AST by calling
3983  // EvalAddr and EvalVal appropriately.
3984
3985  E = E->IgnoreParens();
3986  switch (E->getStmtClass()) {
3987  case Stmt::ImplicitCastExprClass: {
3988    ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E);
3989    if (IE->getValueKind() == VK_LValue) {
3990      E = IE->getSubExpr();
3991      continue;
3992    }
3993    return NULL;
3994  }
3995
3996  case Stmt::ExprWithCleanupsClass:
3997    return EvalVal(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,ParentDecl);
3998
3999  case Stmt::DeclRefExprClass: {
4000    // When we hit a DeclRefExpr we are looking at code that refers to a
4001    // variable's name. If it's not a reference variable we check if it has
4002    // local storage within the function, and if so, return the expression.
4003    DeclRefExpr *DR = cast<DeclRefExpr>(E);
4004
4005    if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) {
4006      // Check if it refers to itself, e.g. "int& i = i;".
4007      if (V == ParentDecl)
4008        return DR;
4009
4010      if (V->hasLocalStorage()) {
4011        if (!V->getType()->isReferenceType())
4012          return DR;
4013
4014        // Reference variable, follow through to the expression that
4015        // it points to.
4016        if (V->hasInit()) {
4017          // Add the reference variable to the "trail".
4018          refVars.push_back(DR);
4019          return EvalVal(V->getInit(), refVars, V);
4020        }
4021      }
4022    }
4023
4024    return NULL;
4025  }
4026
4027  case Stmt::UnaryOperatorClass: {
4028    // The only unary operator that make sense to handle here
4029    // is Deref.  All others don't resolve to a "name."  This includes
4030    // handling all sorts of rvalues passed to a unary operator.
4031    UnaryOperator *U = cast<UnaryOperator>(E);
4032
4033    if (U->getOpcode() == UO_Deref)
4034      return EvalAddr(U->getSubExpr(), refVars, ParentDecl);
4035
4036    return NULL;
4037  }
4038
4039  case Stmt::ArraySubscriptExprClass: {
4040    // Array subscripts are potential references to data on the stack.  We
4041    // retrieve the DeclRefExpr* for the array variable if it indeed
4042    // has local storage.
4043    return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars,ParentDecl);
4044  }
4045
4046  case Stmt::ConditionalOperatorClass: {
4047    // For conditional operators we need to see if either the LHS or RHS are
4048    // non-NULL Expr's.  If one is non-NULL, we return it.
4049    ConditionalOperator *C = cast<ConditionalOperator>(E);
4050
4051    // Handle the GNU extension for missing LHS.
4052    if (Expr *lhsExpr = C->getLHS())
4053      if (Expr *LHS = EvalVal(lhsExpr, refVars, ParentDecl))
4054        return LHS;
4055
4056    return EvalVal(C->getRHS(), refVars, ParentDecl);
4057  }
4058
4059  // Accesses to members are potential references to data on the stack.
4060  case Stmt::MemberExprClass: {
4061    MemberExpr *M = cast<MemberExpr>(E);
4062
4063    // Check for indirect access.  We only want direct field accesses.
4064    if (M->isArrow())
4065      return NULL;
4066
4067    // Check whether the member type is itself a reference, in which case
4068    // we're not going to refer to the member, but to what the member refers to.
4069    if (M->getMemberDecl()->getType()->isReferenceType())
4070      return NULL;
4071
4072    return EvalVal(M->getBase(), refVars, ParentDecl);
4073  }
4074
4075  case Stmt::MaterializeTemporaryExprClass:
4076    if (Expr *Result = EvalVal(
4077                          cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(),
4078                               refVars, ParentDecl))
4079      return Result;
4080
4081    return E;
4082
4083  default:
4084    // Check that we don't return or take the address of a reference to a
4085    // temporary. This is only useful in C++.
4086    if (!E->isTypeDependent() && E->isRValue())
4087      return E;
4088
4089    // Everything else: we simply don't reason about them.
4090    return NULL;
4091  }
4092} while (true);
4093}
4094
4095//===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===//
4096
4097/// Check for comparisons of floating point operands using != and ==.
4098/// Issue a warning if these are no self-comparisons, as they are not likely
4099/// to do what the programmer intended.
4100void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) {
4101  Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts();
4102  Expr* RightExprSansParen = RHS->IgnoreParenImpCasts();
4103
4104  // Special case: check for x == x (which is OK).
4105  // Do not emit warnings for such cases.
4106  if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen))
4107    if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen))
4108      if (DRL->getDecl() == DRR->getDecl())
4109        return;
4110
4111
4112  // Special case: check for comparisons against literals that can be exactly
4113  //  represented by APFloat.  In such cases, do not emit a warning.  This
4114  //  is a heuristic: often comparison against such literals are used to
4115  //  detect if a value in a variable has not changed.  This clearly can
4116  //  lead to false negatives.
4117  if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) {
4118    if (FLL->isExact())
4119      return;
4120  } else
4121    if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen))
4122      if (FLR->isExact())
4123        return;
4124
4125  // Check for comparisons with builtin types.
4126  if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen))
4127    if (CL->isBuiltinCall())
4128      return;
4129
4130  if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen))
4131    if (CR->isBuiltinCall())
4132      return;
4133
4134  // Emit the diagnostic.
4135  Diag(Loc, diag::warn_floatingpoint_eq)
4136    << LHS->getSourceRange() << RHS->getSourceRange();
4137}
4138
4139//===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===//
4140//===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===//
4141
4142namespace {
4143
4144/// Structure recording the 'active' range of an integer-valued
4145/// expression.
4146struct IntRange {
4147  /// The number of bits active in the int.
4148  unsigned Width;
4149
4150  /// True if the int is known not to have negative values.
4151  bool NonNegative;
4152
4153  IntRange(unsigned Width, bool NonNegative)
4154    : Width(Width), NonNegative(NonNegative)
4155  {}
4156
4157  /// Returns the range of the bool type.
4158  static IntRange forBoolType() {
4159    return IntRange(1, true);
4160  }
4161
4162  /// Returns the range of an opaque value of the given integral type.
4163  static IntRange forValueOfType(ASTContext &C, QualType T) {
4164    return forValueOfCanonicalType(C,
4165                          T->getCanonicalTypeInternal().getTypePtr());
4166  }
4167
4168  /// Returns the range of an opaque value of a canonical integral type.
4169  static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) {
4170    assert(T->isCanonicalUnqualified());
4171
4172    if (const VectorType *VT = dyn_cast<VectorType>(T))
4173      T = VT->getElementType().getTypePtr();
4174    if (const ComplexType *CT = dyn_cast<ComplexType>(T))
4175      T = CT->getElementType().getTypePtr();
4176
4177    // For enum types, use the known bit width of the enumerators.
4178    if (const EnumType *ET = dyn_cast<EnumType>(T)) {
4179      EnumDecl *Enum = ET->getDecl();
4180      if (!Enum->isCompleteDefinition())
4181        return IntRange(C.getIntWidth(QualType(T, 0)), false);
4182
4183      unsigned NumPositive = Enum->getNumPositiveBits();
4184      unsigned NumNegative = Enum->getNumNegativeBits();
4185
4186      if (NumNegative == 0)
4187        return IntRange(NumPositive, true/*NonNegative*/);
4188      else
4189        return IntRange(std::max(NumPositive + 1, NumNegative),
4190                        false/*NonNegative*/);
4191    }
4192
4193    const BuiltinType *BT = cast<BuiltinType>(T);
4194    assert(BT->isInteger());
4195
4196    return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
4197  }
4198
4199  /// Returns the "target" range of a canonical integral type, i.e.
4200  /// the range of values expressible in the type.
4201  ///
4202  /// This matches forValueOfCanonicalType except that enums have the
4203  /// full range of their type, not the range of their enumerators.
4204  static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) {
4205    assert(T->isCanonicalUnqualified());
4206
4207    if (const VectorType *VT = dyn_cast<VectorType>(T))
4208      T = VT->getElementType().getTypePtr();
4209    if (const ComplexType *CT = dyn_cast<ComplexType>(T))
4210      T = CT->getElementType().getTypePtr();
4211    if (const EnumType *ET = dyn_cast<EnumType>(T))
4212      T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr();
4213
4214    const BuiltinType *BT = cast<BuiltinType>(T);
4215    assert(BT->isInteger());
4216
4217    return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
4218  }
4219
4220  /// Returns the supremum of two ranges: i.e. their conservative merge.
4221  static IntRange join(IntRange L, IntRange R) {
4222    return IntRange(std::max(L.Width, R.Width),
4223                    L.NonNegative && R.NonNegative);
4224  }
4225
4226  /// Returns the infinum of two ranges: i.e. their aggressive merge.
4227  static IntRange meet(IntRange L, IntRange R) {
4228    return IntRange(std::min(L.Width, R.Width),
4229                    L.NonNegative || R.NonNegative);
4230  }
4231};
4232
4233static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value,
4234                              unsigned MaxWidth) {
4235  if (value.isSigned() && value.isNegative())
4236    return IntRange(value.getMinSignedBits(), false);
4237
4238  if (value.getBitWidth() > MaxWidth)
4239    value = value.trunc(MaxWidth);
4240
4241  // isNonNegative() just checks the sign bit without considering
4242  // signedness.
4243  return IntRange(value.getActiveBits(), true);
4244}
4245
4246static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty,
4247                              unsigned MaxWidth) {
4248  if (result.isInt())
4249    return GetValueRange(C, result.getInt(), MaxWidth);
4250
4251  if (result.isVector()) {
4252    IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth);
4253    for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) {
4254      IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth);
4255      R = IntRange::join(R, El);
4256    }
4257    return R;
4258  }
4259
4260  if (result.isComplexInt()) {
4261    IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth);
4262    IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth);
4263    return IntRange::join(R, I);
4264  }
4265
4266  // This can happen with lossless casts to intptr_t of "based" lvalues.
4267  // Assume it might use arbitrary bits.
4268  // FIXME: The only reason we need to pass the type in here is to get
4269  // the sign right on this one case.  It would be nice if APValue
4270  // preserved this.
4271  assert(result.isLValue() || result.isAddrLabelDiff());
4272  return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType());
4273}
4274
4275static QualType GetExprType(Expr *E) {
4276  QualType Ty = E->getType();
4277  if (const AtomicType *AtomicRHS = Ty->getAs<AtomicType>())
4278    Ty = AtomicRHS->getValueType();
4279  return Ty;
4280}
4281
4282/// Pseudo-evaluate the given integer expression, estimating the
4283/// range of values it might take.
4284///
4285/// \param MaxWidth - the width to which the value will be truncated
4286static IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) {
4287  E = E->IgnoreParens();
4288
4289  // Try a full evaluation first.
4290  Expr::EvalResult result;
4291  if (E->EvaluateAsRValue(result, C))
4292    return GetValueRange(C, result.Val, GetExprType(E), MaxWidth);
4293
4294  // I think we only want to look through implicit casts here; if the
4295  // user has an explicit widening cast, we should treat the value as
4296  // being of the new, wider type.
4297  if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) {
4298    if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue)
4299      return GetExprRange(C, CE->getSubExpr(), MaxWidth);
4300
4301    IntRange OutputTypeRange = IntRange::forValueOfType(C, GetExprType(CE));
4302
4303    bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast);
4304
4305    // Assume that non-integer casts can span the full range of the type.
4306    if (!isIntegerCast)
4307      return OutputTypeRange;
4308
4309    IntRange SubRange
4310      = GetExprRange(C, CE->getSubExpr(),
4311                     std::min(MaxWidth, OutputTypeRange.Width));
4312
4313    // Bail out if the subexpr's range is as wide as the cast type.
4314    if (SubRange.Width >= OutputTypeRange.Width)
4315      return OutputTypeRange;
4316
4317    // Otherwise, we take the smaller width, and we're non-negative if
4318    // either the output type or the subexpr is.
4319    return IntRange(SubRange.Width,
4320                    SubRange.NonNegative || OutputTypeRange.NonNegative);
4321  }
4322
4323  if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
4324    // If we can fold the condition, just take that operand.
4325    bool CondResult;
4326    if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C))
4327      return GetExprRange(C, CondResult ? CO->getTrueExpr()
4328                                        : CO->getFalseExpr(),
4329                          MaxWidth);
4330
4331    // Otherwise, conservatively merge.
4332    IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth);
4333    IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth);
4334    return IntRange::join(L, R);
4335  }
4336
4337  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
4338    switch (BO->getOpcode()) {
4339
4340    // Boolean-valued operations are single-bit and positive.
4341    case BO_LAnd:
4342    case BO_LOr:
4343    case BO_LT:
4344    case BO_GT:
4345    case BO_LE:
4346    case BO_GE:
4347    case BO_EQ:
4348    case BO_NE:
4349      return IntRange::forBoolType();
4350
4351    // The type of the assignments is the type of the LHS, so the RHS
4352    // is not necessarily the same type.
4353    case BO_MulAssign:
4354    case BO_DivAssign:
4355    case BO_RemAssign:
4356    case BO_AddAssign:
4357    case BO_SubAssign:
4358    case BO_XorAssign:
4359    case BO_OrAssign:
4360      // TODO: bitfields?
4361      return IntRange::forValueOfType(C, GetExprType(E));
4362
4363    // Simple assignments just pass through the RHS, which will have
4364    // been coerced to the LHS type.
4365    case BO_Assign:
4366      // TODO: bitfields?
4367      return GetExprRange(C, BO->getRHS(), MaxWidth);
4368
4369    // Operations with opaque sources are black-listed.
4370    case BO_PtrMemD:
4371    case BO_PtrMemI:
4372      return IntRange::forValueOfType(C, GetExprType(E));
4373
4374    // Bitwise-and uses the *infinum* of the two source ranges.
4375    case BO_And:
4376    case BO_AndAssign:
4377      return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth),
4378                            GetExprRange(C, BO->getRHS(), MaxWidth));
4379
4380    // Left shift gets black-listed based on a judgement call.
4381    case BO_Shl:
4382      // ...except that we want to treat '1 << (blah)' as logically
4383      // positive.  It's an important idiom.
4384      if (IntegerLiteral *I
4385            = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) {
4386        if (I->getValue() == 1) {
4387          IntRange R = IntRange::forValueOfType(C, GetExprType(E));
4388          return IntRange(R.Width, /*NonNegative*/ true);
4389        }
4390      }
4391      // fallthrough
4392
4393    case BO_ShlAssign:
4394      return IntRange::forValueOfType(C, GetExprType(E));
4395
4396    // Right shift by a constant can narrow its left argument.
4397    case BO_Shr:
4398    case BO_ShrAssign: {
4399      IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth);
4400
4401      // If the shift amount is a positive constant, drop the width by
4402      // that much.
4403      llvm::APSInt shift;
4404      if (BO->getRHS()->isIntegerConstantExpr(shift, C) &&
4405          shift.isNonNegative()) {
4406        unsigned zext = shift.getZExtValue();
4407        if (zext >= L.Width)
4408          L.Width = (L.NonNegative ? 0 : 1);
4409        else
4410          L.Width -= zext;
4411      }
4412
4413      return L;
4414    }
4415
4416    // Comma acts as its right operand.
4417    case BO_Comma:
4418      return GetExprRange(C, BO->getRHS(), MaxWidth);
4419
4420    // Black-list pointer subtractions.
4421    case BO_Sub:
4422      if (BO->getLHS()->getType()->isPointerType())
4423        return IntRange::forValueOfType(C, GetExprType(E));
4424      break;
4425
4426    // The width of a division result is mostly determined by the size
4427    // of the LHS.
4428    case BO_Div: {
4429      // Don't 'pre-truncate' the operands.
4430      unsigned opWidth = C.getIntWidth(GetExprType(E));
4431      IntRange L = GetExprRange(C, BO->getLHS(), opWidth);
4432
4433      // If the divisor is constant, use that.
4434      llvm::APSInt divisor;
4435      if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) {
4436        unsigned log2 = divisor.logBase2(); // floor(log_2(divisor))
4437        if (log2 >= L.Width)
4438          L.Width = (L.NonNegative ? 0 : 1);
4439        else
4440          L.Width = std::min(L.Width - log2, MaxWidth);
4441        return L;
4442      }
4443
4444      // Otherwise, just use the LHS's width.
4445      IntRange R = GetExprRange(C, BO->getRHS(), opWidth);
4446      return IntRange(L.Width, L.NonNegative && R.NonNegative);
4447    }
4448
4449    // The result of a remainder can't be larger than the result of
4450    // either side.
4451    case BO_Rem: {
4452      // Don't 'pre-truncate' the operands.
4453      unsigned opWidth = C.getIntWidth(GetExprType(E));
4454      IntRange L = GetExprRange(C, BO->getLHS(), opWidth);
4455      IntRange R = GetExprRange(C, BO->getRHS(), opWidth);
4456
4457      IntRange meet = IntRange::meet(L, R);
4458      meet.Width = std::min(meet.Width, MaxWidth);
4459      return meet;
4460    }
4461
4462    // The default behavior is okay for these.
4463    case BO_Mul:
4464    case BO_Add:
4465    case BO_Xor:
4466    case BO_Or:
4467      break;
4468    }
4469
4470    // The default case is to treat the operation as if it were closed
4471    // on the narrowest type that encompasses both operands.
4472    IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth);
4473    IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth);
4474    return IntRange::join(L, R);
4475  }
4476
4477  if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
4478    switch (UO->getOpcode()) {
4479    // Boolean-valued operations are white-listed.
4480    case UO_LNot:
4481      return IntRange::forBoolType();
4482
4483    // Operations with opaque sources are black-listed.
4484    case UO_Deref:
4485    case UO_AddrOf: // should be impossible
4486      return IntRange::forValueOfType(C, GetExprType(E));
4487
4488    default:
4489      return GetExprRange(C, UO->getSubExpr(), MaxWidth);
4490    }
4491  }
4492
4493  if (FieldDecl *BitField = E->getSourceBitField())
4494    return IntRange(BitField->getBitWidthValue(C),
4495                    BitField->getType()->isUnsignedIntegerOrEnumerationType());
4496
4497  return IntRange::forValueOfType(C, GetExprType(E));
4498}
4499
4500static IntRange GetExprRange(ASTContext &C, Expr *E) {
4501  return GetExprRange(C, E, C.getIntWidth(GetExprType(E)));
4502}
4503
4504/// Checks whether the given value, which currently has the given
4505/// source semantics, has the same value when coerced through the
4506/// target semantics.
4507static bool IsSameFloatAfterCast(const llvm::APFloat &value,
4508                                 const llvm::fltSemantics &Src,
4509                                 const llvm::fltSemantics &Tgt) {
4510  llvm::APFloat truncated = value;
4511
4512  bool ignored;
4513  truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored);
4514  truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored);
4515
4516  return truncated.bitwiseIsEqual(value);
4517}
4518
4519/// Checks whether the given value, which currently has the given
4520/// source semantics, has the same value when coerced through the
4521/// target semantics.
4522///
4523/// The value might be a vector of floats (or a complex number).
4524static bool IsSameFloatAfterCast(const APValue &value,
4525                                 const llvm::fltSemantics &Src,
4526                                 const llvm::fltSemantics &Tgt) {
4527  if (value.isFloat())
4528    return IsSameFloatAfterCast(value.getFloat(), Src, Tgt);
4529
4530  if (value.isVector()) {
4531    for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i)
4532      if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt))
4533        return false;
4534    return true;
4535  }
4536
4537  assert(value.isComplexFloat());
4538  return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) &&
4539          IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt));
4540}
4541
4542static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC);
4543
4544static bool IsZero(Sema &S, Expr *E) {
4545  // Suppress cases where we are comparing against an enum constant.
4546  if (const DeclRefExpr *DR =
4547      dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
4548    if (isa<EnumConstantDecl>(DR->getDecl()))
4549      return false;
4550
4551  // Suppress cases where the '0' value is expanded from a macro.
4552  if (E->getLocStart().isMacroID())
4553    return false;
4554
4555  llvm::APSInt Value;
4556  return E->isIntegerConstantExpr(Value, S.Context) && Value == 0;
4557}
4558
4559static bool HasEnumType(Expr *E) {
4560  // Strip off implicit integral promotions.
4561  while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
4562    if (ICE->getCastKind() != CK_IntegralCast &&
4563        ICE->getCastKind() != CK_NoOp)
4564      break;
4565    E = ICE->getSubExpr();
4566  }
4567
4568  return E->getType()->isEnumeralType();
4569}
4570
4571static void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) {
4572  BinaryOperatorKind op = E->getOpcode();
4573  if (E->isValueDependent())
4574    return;
4575
4576  if (op == BO_LT && IsZero(S, E->getRHS())) {
4577    S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
4578      << "< 0" << "false" << HasEnumType(E->getLHS())
4579      << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
4580  } else if (op == BO_GE && IsZero(S, E->getRHS())) {
4581    S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
4582      << ">= 0" << "true" << HasEnumType(E->getLHS())
4583      << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
4584  } else if (op == BO_GT && IsZero(S, E->getLHS())) {
4585    S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison)
4586      << "0 >" << "false" << HasEnumType(E->getRHS())
4587      << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
4588  } else if (op == BO_LE && IsZero(S, E->getLHS())) {
4589    S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison)
4590      << "0 <=" << "true" << HasEnumType(E->getRHS())
4591      << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
4592  }
4593}
4594
4595static void DiagnoseOutOfRangeComparison(Sema &S, BinaryOperator *E,
4596                                         Expr *Constant, Expr *Other,
4597                                         llvm::APSInt Value,
4598                                         bool RhsConstant) {
4599  // 0 values are handled later by CheckTrivialUnsignedComparison().
4600  if (Value == 0)
4601    return;
4602
4603  BinaryOperatorKind op = E->getOpcode();
4604  QualType OtherT = Other->getType();
4605  QualType ConstantT = Constant->getType();
4606  QualType CommonT = E->getLHS()->getType();
4607  if (S.Context.hasSameUnqualifiedType(OtherT, ConstantT))
4608    return;
4609  assert((OtherT->isIntegerType() && ConstantT->isIntegerType())
4610         && "comparison with non-integer type");
4611
4612  bool ConstantSigned = ConstantT->isSignedIntegerType();
4613  bool CommonSigned = CommonT->isSignedIntegerType();
4614
4615  bool EqualityOnly = false;
4616
4617  // TODO: Investigate using GetExprRange() to get tighter bounds on
4618  // on the bit ranges.
4619  IntRange OtherRange = IntRange::forValueOfType(S.Context, OtherT);
4620  unsigned OtherWidth = OtherRange.Width;
4621
4622  if (CommonSigned) {
4623    // The common type is signed, therefore no signed to unsigned conversion.
4624    if (!OtherRange.NonNegative) {
4625      // Check that the constant is representable in type OtherT.
4626      if (ConstantSigned) {
4627        if (OtherWidth >= Value.getMinSignedBits())
4628          return;
4629      } else { // !ConstantSigned
4630        if (OtherWidth >= Value.getActiveBits() + 1)
4631          return;
4632      }
4633    } else { // !OtherSigned
4634      // Check that the constant is representable in type OtherT.
4635      // Negative values are out of range.
4636      if (ConstantSigned) {
4637        if (Value.isNonNegative() && OtherWidth >= Value.getActiveBits())
4638          return;
4639      } else { // !ConstantSigned
4640        if (OtherWidth >= Value.getActiveBits())
4641          return;
4642      }
4643    }
4644  } else {  // !CommonSigned
4645    if (OtherRange.NonNegative) {
4646      if (OtherWidth >= Value.getActiveBits())
4647        return;
4648    } else if (!OtherRange.NonNegative && !ConstantSigned) {
4649      // Check to see if the constant is representable in OtherT.
4650      if (OtherWidth > Value.getActiveBits())
4651        return;
4652      // Check to see if the constant is equivalent to a negative value
4653      // cast to CommonT.
4654      if (S.Context.getIntWidth(ConstantT) == S.Context.getIntWidth(CommonT) &&
4655          Value.isNegative() && Value.getMinSignedBits() <= OtherWidth)
4656        return;
4657      // The constant value rests between values that OtherT can represent after
4658      // conversion.  Relational comparison still works, but equality
4659      // comparisons will be tautological.
4660      EqualityOnly = true;
4661    } else { // OtherSigned && ConstantSigned
4662      assert(0 && "Two signed types converted to unsigned types.");
4663    }
4664  }
4665
4666  bool PositiveConstant = !ConstantSigned || Value.isNonNegative();
4667
4668  bool IsTrue = true;
4669  if (op == BO_EQ || op == BO_NE) {
4670    IsTrue = op == BO_NE;
4671  } else if (EqualityOnly) {
4672    return;
4673  } else if (RhsConstant) {
4674    if (op == BO_GT || op == BO_GE)
4675      IsTrue = !PositiveConstant;
4676    else // op == BO_LT || op == BO_LE
4677      IsTrue = PositiveConstant;
4678  } else {
4679    if (op == BO_LT || op == BO_LE)
4680      IsTrue = !PositiveConstant;
4681    else // op == BO_GT || op == BO_GE
4682      IsTrue = PositiveConstant;
4683  }
4684
4685  // If this is a comparison to an enum constant, include that
4686  // constant in the diagnostic.
4687  const EnumConstantDecl *ED = 0;
4688  if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Constant))
4689    ED = dyn_cast<EnumConstantDecl>(DR->getDecl());
4690
4691  SmallString<64> PrettySourceValue;
4692  llvm::raw_svector_ostream OS(PrettySourceValue);
4693  if (ED)
4694    OS << '\'' << *ED << "' (" << Value << ")";
4695  else
4696    OS << Value;
4697
4698  S.Diag(E->getOperatorLoc(), diag::warn_out_of_range_compare)
4699      << OS.str() << OtherT << IsTrue
4700      << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
4701}
4702
4703/// Analyze the operands of the given comparison.  Implements the
4704/// fallback case from AnalyzeComparison.
4705static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) {
4706  AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
4707  AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
4708}
4709
4710/// \brief Implements -Wsign-compare.
4711///
4712/// \param E the binary operator to check for warnings
4713static void AnalyzeComparison(Sema &S, BinaryOperator *E) {
4714  // The type the comparison is being performed in.
4715  QualType T = E->getLHS()->getType();
4716  assert(S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType())
4717         && "comparison with mismatched types");
4718  if (E->isValueDependent())
4719    return AnalyzeImpConvsInComparison(S, E);
4720
4721  Expr *LHS = E->getLHS()->IgnoreParenImpCasts();
4722  Expr *RHS = E->getRHS()->IgnoreParenImpCasts();
4723
4724  bool IsComparisonConstant = false;
4725
4726  // Check whether an integer constant comparison results in a value
4727  // of 'true' or 'false'.
4728  if (T->isIntegralType(S.Context)) {
4729    llvm::APSInt RHSValue;
4730    bool IsRHSIntegralLiteral =
4731      RHS->isIntegerConstantExpr(RHSValue, S.Context);
4732    llvm::APSInt LHSValue;
4733    bool IsLHSIntegralLiteral =
4734      LHS->isIntegerConstantExpr(LHSValue, S.Context);
4735    if (IsRHSIntegralLiteral && !IsLHSIntegralLiteral)
4736        DiagnoseOutOfRangeComparison(S, E, RHS, LHS, RHSValue, true);
4737    else if (!IsRHSIntegralLiteral && IsLHSIntegralLiteral)
4738      DiagnoseOutOfRangeComparison(S, E, LHS, RHS, LHSValue, false);
4739    else
4740      IsComparisonConstant =
4741        (IsRHSIntegralLiteral && IsLHSIntegralLiteral);
4742  } else if (!T->hasUnsignedIntegerRepresentation())
4743      IsComparisonConstant = E->isIntegerConstantExpr(S.Context);
4744
4745  // We don't do anything special if this isn't an unsigned integral
4746  // comparison:  we're only interested in integral comparisons, and
4747  // signed comparisons only happen in cases we don't care to warn about.
4748  //
4749  // We also don't care about value-dependent expressions or expressions
4750  // whose result is a constant.
4751  if (!T->hasUnsignedIntegerRepresentation() || IsComparisonConstant)
4752    return AnalyzeImpConvsInComparison(S, E);
4753
4754  // Check to see if one of the (unmodified) operands is of different
4755  // signedness.
4756  Expr *signedOperand, *unsignedOperand;
4757  if (LHS->getType()->hasSignedIntegerRepresentation()) {
4758    assert(!RHS->getType()->hasSignedIntegerRepresentation() &&
4759           "unsigned comparison between two signed integer expressions?");
4760    signedOperand = LHS;
4761    unsignedOperand = RHS;
4762  } else if (RHS->getType()->hasSignedIntegerRepresentation()) {
4763    signedOperand = RHS;
4764    unsignedOperand = LHS;
4765  } else {
4766    CheckTrivialUnsignedComparison(S, E);
4767    return AnalyzeImpConvsInComparison(S, E);
4768  }
4769
4770  // Otherwise, calculate the effective range of the signed operand.
4771  IntRange signedRange = GetExprRange(S.Context, signedOperand);
4772
4773  // Go ahead and analyze implicit conversions in the operands.  Note
4774  // that we skip the implicit conversions on both sides.
4775  AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc());
4776  AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc());
4777
4778  // If the signed range is non-negative, -Wsign-compare won't fire,
4779  // but we should still check for comparisons which are always true
4780  // or false.
4781  if (signedRange.NonNegative)
4782    return CheckTrivialUnsignedComparison(S, E);
4783
4784  // For (in)equality comparisons, if the unsigned operand is a
4785  // constant which cannot collide with a overflowed signed operand,
4786  // then reinterpreting the signed operand as unsigned will not
4787  // change the result of the comparison.
4788  if (E->isEqualityOp()) {
4789    unsigned comparisonWidth = S.Context.getIntWidth(T);
4790    IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand);
4791
4792    // We should never be unable to prove that the unsigned operand is
4793    // non-negative.
4794    assert(unsignedRange.NonNegative && "unsigned range includes negative?");
4795
4796    if (unsignedRange.Width < comparisonWidth)
4797      return;
4798  }
4799
4800  S.DiagRuntimeBehavior(E->getOperatorLoc(), E,
4801    S.PDiag(diag::warn_mixed_sign_comparison)
4802      << LHS->getType() << RHS->getType()
4803      << LHS->getSourceRange() << RHS->getSourceRange());
4804}
4805
4806/// Analyzes an attempt to assign the given value to a bitfield.
4807///
4808/// Returns true if there was something fishy about the attempt.
4809static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init,
4810                                      SourceLocation InitLoc) {
4811  assert(Bitfield->isBitField());
4812  if (Bitfield->isInvalidDecl())
4813    return false;
4814
4815  // White-list bool bitfields.
4816  if (Bitfield->getType()->isBooleanType())
4817    return false;
4818
4819  // Ignore value- or type-dependent expressions.
4820  if (Bitfield->getBitWidth()->isValueDependent() ||
4821      Bitfield->getBitWidth()->isTypeDependent() ||
4822      Init->isValueDependent() ||
4823      Init->isTypeDependent())
4824    return false;
4825
4826  Expr *OriginalInit = Init->IgnoreParenImpCasts();
4827
4828  llvm::APSInt Value;
4829  if (!OriginalInit->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects))
4830    return false;
4831
4832  unsigned OriginalWidth = Value.getBitWidth();
4833  unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context);
4834
4835  if (OriginalWidth <= FieldWidth)
4836    return false;
4837
4838  // Compute the value which the bitfield will contain.
4839  llvm::APSInt TruncatedValue = Value.trunc(FieldWidth);
4840  TruncatedValue.setIsSigned(Bitfield->getType()->isSignedIntegerType());
4841
4842  // Check whether the stored value is equal to the original value.
4843  TruncatedValue = TruncatedValue.extend(OriginalWidth);
4844  if (llvm::APSInt::isSameValue(Value, TruncatedValue))
4845    return false;
4846
4847  // Special-case bitfields of width 1: booleans are naturally 0/1, and
4848  // therefore don't strictly fit into a signed bitfield of width 1.
4849  if (FieldWidth == 1 && Value == 1)
4850    return false;
4851
4852  std::string PrettyValue = Value.toString(10);
4853  std::string PrettyTrunc = TruncatedValue.toString(10);
4854
4855  S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant)
4856    << PrettyValue << PrettyTrunc << OriginalInit->getType()
4857    << Init->getSourceRange();
4858
4859  return true;
4860}
4861
4862/// Analyze the given simple or compound assignment for warning-worthy
4863/// operations.
4864static void AnalyzeAssignment(Sema &S, BinaryOperator *E) {
4865  // Just recurse on the LHS.
4866  AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
4867
4868  // We want to recurse on the RHS as normal unless we're assigning to
4869  // a bitfield.
4870  if (FieldDecl *Bitfield = E->getLHS()->getSourceBitField()) {
4871    if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(),
4872                                  E->getOperatorLoc())) {
4873      // Recurse, ignoring any implicit conversions on the RHS.
4874      return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(),
4875                                        E->getOperatorLoc());
4876    }
4877  }
4878
4879  AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
4880}
4881
4882/// Diagnose an implicit cast;  purely a helper for CheckImplicitConversion.
4883static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T,
4884                            SourceLocation CContext, unsigned diag,
4885                            bool pruneControlFlow = false) {
4886  if (pruneControlFlow) {
4887    S.DiagRuntimeBehavior(E->getExprLoc(), E,
4888                          S.PDiag(diag)
4889                            << SourceType << T << E->getSourceRange()
4890                            << SourceRange(CContext));
4891    return;
4892  }
4893  S.Diag(E->getExprLoc(), diag)
4894    << SourceType << T << E->getSourceRange() << SourceRange(CContext);
4895}
4896
4897/// Diagnose an implicit cast;  purely a helper for CheckImplicitConversion.
4898static void DiagnoseImpCast(Sema &S, Expr *E, QualType T,
4899                            SourceLocation CContext, unsigned diag,
4900                            bool pruneControlFlow = false) {
4901  DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow);
4902}
4903
4904/// Diagnose an implicit cast from a literal expression. Does not warn when the
4905/// cast wouldn't lose information.
4906void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T,
4907                                    SourceLocation CContext) {
4908  // Try to convert the literal exactly to an integer. If we can, don't warn.
4909  bool isExact = false;
4910  const llvm::APFloat &Value = FL->getValue();
4911  llvm::APSInt IntegerValue(S.Context.getIntWidth(T),
4912                            T->hasUnsignedIntegerRepresentation());
4913  if (Value.convertToInteger(IntegerValue,
4914                             llvm::APFloat::rmTowardZero, &isExact)
4915      == llvm::APFloat::opOK && isExact)
4916    return;
4917
4918  SmallString<16> PrettySourceValue;
4919  Value.toString(PrettySourceValue);
4920  SmallString<16> PrettyTargetValue;
4921  if (T->isSpecificBuiltinType(BuiltinType::Bool))
4922    PrettyTargetValue = IntegerValue == 0 ? "false" : "true";
4923  else
4924    IntegerValue.toString(PrettyTargetValue);
4925
4926  S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer)
4927    << FL->getType() << T.getUnqualifiedType() << PrettySourceValue
4928    << PrettyTargetValue << FL->getSourceRange() << SourceRange(CContext);
4929}
4930
4931std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) {
4932  if (!Range.Width) return "0";
4933
4934  llvm::APSInt ValueInRange = Value;
4935  ValueInRange.setIsSigned(!Range.NonNegative);
4936  ValueInRange = ValueInRange.trunc(Range.Width);
4937  return ValueInRange.toString(10);
4938}
4939
4940static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) {
4941  if (!isa<ImplicitCastExpr>(Ex))
4942    return false;
4943
4944  Expr *InnerE = Ex->IgnoreParenImpCasts();
4945  const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr();
4946  const Type *Source =
4947    S.Context.getCanonicalType(InnerE->getType()).getTypePtr();
4948  if (Target->isDependentType())
4949    return false;
4950
4951  const BuiltinType *FloatCandidateBT =
4952    dyn_cast<BuiltinType>(ToBool ? Source : Target);
4953  const Type *BoolCandidateType = ToBool ? Target : Source;
4954
4955  return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) &&
4956          FloatCandidateBT && (FloatCandidateBT->isFloatingPoint()));
4957}
4958
4959void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall,
4960                                      SourceLocation CC) {
4961  unsigned NumArgs = TheCall->getNumArgs();
4962  for (unsigned i = 0; i < NumArgs; ++i) {
4963    Expr *CurrA = TheCall->getArg(i);
4964    if (!IsImplicitBoolFloatConversion(S, CurrA, true))
4965      continue;
4966
4967    bool IsSwapped = ((i > 0) &&
4968        IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false));
4969    IsSwapped |= ((i < (NumArgs - 1)) &&
4970        IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false));
4971    if (IsSwapped) {
4972      // Warn on this floating-point to bool conversion.
4973      DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(),
4974                      CurrA->getType(), CC,
4975                      diag::warn_impcast_floating_point_to_bool);
4976    }
4977  }
4978}
4979
4980void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
4981                             SourceLocation CC, bool *ICContext = 0) {
4982  if (E->isTypeDependent() || E->isValueDependent()) return;
4983
4984  const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr();
4985  const Type *Target = S.Context.getCanonicalType(T).getTypePtr();
4986  if (Source == Target) return;
4987  if (Target->isDependentType()) return;
4988
4989  // If the conversion context location is invalid don't complain. We also
4990  // don't want to emit a warning if the issue occurs from the expansion of
4991  // a system macro. The problem is that 'getSpellingLoc()' is slow, so we
4992  // delay this check as long as possible. Once we detect we are in that
4993  // scenario, we just return.
4994  if (CC.isInvalid())
4995    return;
4996
4997  // Diagnose implicit casts to bool.
4998  if (Target->isSpecificBuiltinType(BuiltinType::Bool)) {
4999    if (isa<StringLiteral>(E))
5000      // Warn on string literal to bool.  Checks for string literals in logical
5001      // expressions, for instances, assert(0 && "error here"), is prevented
5002      // by a check in AnalyzeImplicitConversions().
5003      return DiagnoseImpCast(S, E, T, CC,
5004                             diag::warn_impcast_string_literal_to_bool);
5005    if (Source->isFunctionType()) {
5006      // Warn on function to bool. Checks free functions and static member
5007      // functions. Weakly imported functions are excluded from the check,
5008      // since it's common to test their value to check whether the linker
5009      // found a definition for them.
5010      ValueDecl *D = 0;
5011      if (DeclRefExpr* R = dyn_cast<DeclRefExpr>(E)) {
5012        D = R->getDecl();
5013      } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) {
5014        D = M->getMemberDecl();
5015      }
5016
5017      if (D && !D->isWeak()) {
5018        if (FunctionDecl* F = dyn_cast<FunctionDecl>(D)) {
5019          S.Diag(E->getExprLoc(), diag::warn_impcast_function_to_bool)
5020            << F << E->getSourceRange() << SourceRange(CC);
5021          S.Diag(E->getExprLoc(), diag::note_function_to_bool_silence)
5022            << FixItHint::CreateInsertion(E->getExprLoc(), "&");
5023          QualType ReturnType;
5024          UnresolvedSet<4> NonTemplateOverloads;
5025          S.tryExprAsCall(*E, ReturnType, NonTemplateOverloads);
5026          if (!ReturnType.isNull()
5027              && ReturnType->isSpecificBuiltinType(BuiltinType::Bool))
5028            S.Diag(E->getExprLoc(), diag::note_function_to_bool_call)
5029              << FixItHint::CreateInsertion(
5030                 S.getPreprocessor().getLocForEndOfToken(E->getLocEnd()), "()");
5031          return;
5032        }
5033      }
5034    }
5035  }
5036
5037  // Strip vector types.
5038  if (isa<VectorType>(Source)) {
5039    if (!isa<VectorType>(Target)) {
5040      if (S.SourceMgr.isInSystemMacro(CC))
5041        return;
5042      return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar);
5043    }
5044
5045    // If the vector cast is cast between two vectors of the same size, it is
5046    // a bitcast, not a conversion.
5047    if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target))
5048      return;
5049
5050    Source = cast<VectorType>(Source)->getElementType().getTypePtr();
5051    Target = cast<VectorType>(Target)->getElementType().getTypePtr();
5052  }
5053
5054  // Strip complex types.
5055  if (isa<ComplexType>(Source)) {
5056    if (!isa<ComplexType>(Target)) {
5057      if (S.SourceMgr.isInSystemMacro(CC))
5058        return;
5059
5060      return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar);
5061    }
5062
5063    Source = cast<ComplexType>(Source)->getElementType().getTypePtr();
5064    Target = cast<ComplexType>(Target)->getElementType().getTypePtr();
5065  }
5066
5067  const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source);
5068  const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target);
5069
5070  // If the source is floating point...
5071  if (SourceBT && SourceBT->isFloatingPoint()) {
5072    // ...and the target is floating point...
5073    if (TargetBT && TargetBT->isFloatingPoint()) {
5074      // ...then warn if we're dropping FP rank.
5075
5076      // Builtin FP kinds are ordered by increasing FP rank.
5077      if (SourceBT->getKind() > TargetBT->getKind()) {
5078        // Don't warn about float constants that are precisely
5079        // representable in the target type.
5080        Expr::EvalResult result;
5081        if (E->EvaluateAsRValue(result, S.Context)) {
5082          // Value might be a float, a float vector, or a float complex.
5083          if (IsSameFloatAfterCast(result.Val,
5084                   S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)),
5085                   S.Context.getFloatTypeSemantics(QualType(SourceBT, 0))))
5086            return;
5087        }
5088
5089        if (S.SourceMgr.isInSystemMacro(CC))
5090          return;
5091
5092        DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision);
5093      }
5094      return;
5095    }
5096
5097    // If the target is integral, always warn.
5098    if (TargetBT && TargetBT->isInteger()) {
5099      if (S.SourceMgr.isInSystemMacro(CC))
5100        return;
5101
5102      Expr *InnerE = E->IgnoreParenImpCasts();
5103      // We also want to warn on, e.g., "int i = -1.234"
5104      if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE))
5105        if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus)
5106          InnerE = UOp->getSubExpr()->IgnoreParenImpCasts();
5107
5108      if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) {
5109        DiagnoseFloatingLiteralImpCast(S, FL, T, CC);
5110      } else {
5111        DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer);
5112      }
5113    }
5114
5115    // If the target is bool, warn if expr is a function or method call.
5116    if (Target->isSpecificBuiltinType(BuiltinType::Bool) &&
5117        isa<CallExpr>(E)) {
5118      // Check last argument of function call to see if it is an
5119      // implicit cast from a type matching the type the result
5120      // is being cast to.
5121      CallExpr *CEx = cast<CallExpr>(E);
5122      unsigned NumArgs = CEx->getNumArgs();
5123      if (NumArgs > 0) {
5124        Expr *LastA = CEx->getArg(NumArgs - 1);
5125        Expr *InnerE = LastA->IgnoreParenImpCasts();
5126        const Type *InnerType =
5127          S.Context.getCanonicalType(InnerE->getType()).getTypePtr();
5128        if (isa<ImplicitCastExpr>(LastA) && (InnerType == Target)) {
5129          // Warn on this floating-point to bool conversion
5130          DiagnoseImpCast(S, E, T, CC,
5131                          diag::warn_impcast_floating_point_to_bool);
5132        }
5133      }
5134    }
5135    return;
5136  }
5137
5138  if ((E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)
5139           == Expr::NPCK_GNUNull) && !Target->isAnyPointerType()
5140      && !Target->isBlockPointerType() && !Target->isMemberPointerType()
5141      && Target->isScalarType() && !Target->isNullPtrType()) {
5142    SourceLocation Loc = E->getSourceRange().getBegin();
5143    if (Loc.isMacroID())
5144      Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first;
5145    if (!Loc.isMacroID() || CC.isMacroID())
5146      S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer)
5147          << T << clang::SourceRange(CC)
5148          << FixItHint::CreateReplacement(Loc, S.getFixItZeroLiteralForType(T));
5149  }
5150
5151  if (!Source->isIntegerType() || !Target->isIntegerType())
5152    return;
5153
5154  // TODO: remove this early return once the false positives for constant->bool
5155  // in templates, macros, etc, are reduced or removed.
5156  if (Target->isSpecificBuiltinType(BuiltinType::Bool))
5157    return;
5158
5159  IntRange SourceRange = GetExprRange(S.Context, E);
5160  IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target);
5161
5162  if (SourceRange.Width > TargetRange.Width) {
5163    // If the source is a constant, use a default-on diagnostic.
5164    // TODO: this should happen for bitfield stores, too.
5165    llvm::APSInt Value(32);
5166    if (E->isIntegerConstantExpr(Value, S.Context)) {
5167      if (S.SourceMgr.isInSystemMacro(CC))
5168        return;
5169
5170      std::string PrettySourceValue = Value.toString(10);
5171      std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange);
5172
5173      S.DiagRuntimeBehavior(E->getExprLoc(), E,
5174        S.PDiag(diag::warn_impcast_integer_precision_constant)
5175            << PrettySourceValue << PrettyTargetValue
5176            << E->getType() << T << E->getSourceRange()
5177            << clang::SourceRange(CC));
5178      return;
5179    }
5180
5181    // People want to build with -Wshorten-64-to-32 and not -Wconversion.
5182    if (S.SourceMgr.isInSystemMacro(CC))
5183      return;
5184
5185    if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64)
5186      return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32,
5187                             /* pruneControlFlow */ true);
5188    return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision);
5189  }
5190
5191  if ((TargetRange.NonNegative && !SourceRange.NonNegative) ||
5192      (!TargetRange.NonNegative && SourceRange.NonNegative &&
5193       SourceRange.Width == TargetRange.Width)) {
5194
5195    if (S.SourceMgr.isInSystemMacro(CC))
5196      return;
5197
5198    unsigned DiagID = diag::warn_impcast_integer_sign;
5199
5200    // Traditionally, gcc has warned about this under -Wsign-compare.
5201    // We also want to warn about it in -Wconversion.
5202    // So if -Wconversion is off, use a completely identical diagnostic
5203    // in the sign-compare group.
5204    // The conditional-checking code will
5205    if (ICContext) {
5206      DiagID = diag::warn_impcast_integer_sign_conditional;
5207      *ICContext = true;
5208    }
5209
5210    return DiagnoseImpCast(S, E, T, CC, DiagID);
5211  }
5212
5213  // Diagnose conversions between different enumeration types.
5214  // In C, we pretend that the type of an EnumConstantDecl is its enumeration
5215  // type, to give us better diagnostics.
5216  QualType SourceType = E->getType();
5217  if (!S.getLangOpts().CPlusPlus) {
5218    if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
5219      if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
5220        EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext());
5221        SourceType = S.Context.getTypeDeclType(Enum);
5222        Source = S.Context.getCanonicalType(SourceType).getTypePtr();
5223      }
5224  }
5225
5226  if (const EnumType *SourceEnum = Source->getAs<EnumType>())
5227    if (const EnumType *TargetEnum = Target->getAs<EnumType>())
5228      if (SourceEnum->getDecl()->hasNameForLinkage() &&
5229          TargetEnum->getDecl()->hasNameForLinkage() &&
5230          SourceEnum != TargetEnum) {
5231        if (S.SourceMgr.isInSystemMacro(CC))
5232          return;
5233
5234        return DiagnoseImpCast(S, E, SourceType, T, CC,
5235                               diag::warn_impcast_different_enum_types);
5236      }
5237
5238  return;
5239}
5240
5241void CheckConditionalOperator(Sema &S, ConditionalOperator *E,
5242                              SourceLocation CC, QualType T);
5243
5244void CheckConditionalOperand(Sema &S, Expr *E, QualType T,
5245                             SourceLocation CC, bool &ICContext) {
5246  E = E->IgnoreParenImpCasts();
5247
5248  if (isa<ConditionalOperator>(E))
5249    return CheckConditionalOperator(S, cast<ConditionalOperator>(E), CC, T);
5250
5251  AnalyzeImplicitConversions(S, E, CC);
5252  if (E->getType() != T)
5253    return CheckImplicitConversion(S, E, T, CC, &ICContext);
5254  return;
5255}
5256
5257void CheckConditionalOperator(Sema &S, ConditionalOperator *E,
5258                              SourceLocation CC, QualType T) {
5259  AnalyzeImplicitConversions(S, E->getCond(), CC);
5260
5261  bool Suspicious = false;
5262  CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious);
5263  CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious);
5264
5265  // If -Wconversion would have warned about either of the candidates
5266  // for a signedness conversion to the context type...
5267  if (!Suspicious) return;
5268
5269  // ...but it's currently ignored...
5270  if (S.Diags.getDiagnosticLevel(diag::warn_impcast_integer_sign_conditional,
5271                                 CC))
5272    return;
5273
5274  // ...then check whether it would have warned about either of the
5275  // candidates for a signedness conversion to the condition type.
5276  if (E->getType() == T) return;
5277
5278  Suspicious = false;
5279  CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(),
5280                          E->getType(), CC, &Suspicious);
5281  if (!Suspicious)
5282    CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(),
5283                            E->getType(), CC, &Suspicious);
5284}
5285
5286/// AnalyzeImplicitConversions - Find and report any interesting
5287/// implicit conversions in the given expression.  There are a couple
5288/// of competing diagnostics here, -Wconversion and -Wsign-compare.
5289void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) {
5290  QualType T = OrigE->getType();
5291  Expr *E = OrigE->IgnoreParenImpCasts();
5292
5293  if (E->isTypeDependent() || E->isValueDependent())
5294    return;
5295
5296  // For conditional operators, we analyze the arguments as if they
5297  // were being fed directly into the output.
5298  if (isa<ConditionalOperator>(E)) {
5299    ConditionalOperator *CO = cast<ConditionalOperator>(E);
5300    CheckConditionalOperator(S, CO, CC, T);
5301    return;
5302  }
5303
5304  // Check implicit argument conversions for function calls.
5305  if (CallExpr *Call = dyn_cast<CallExpr>(E))
5306    CheckImplicitArgumentConversions(S, Call, CC);
5307
5308  // Go ahead and check any implicit conversions we might have skipped.
5309  // The non-canonical typecheck is just an optimization;
5310  // CheckImplicitConversion will filter out dead implicit conversions.
5311  if (E->getType() != T)
5312    CheckImplicitConversion(S, E, T, CC);
5313
5314  // Now continue drilling into this expression.
5315
5316  if (PseudoObjectExpr * POE = dyn_cast<PseudoObjectExpr>(E)) {
5317    if (POE->getResultExpr())
5318      E = POE->getResultExpr();
5319  }
5320
5321  if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
5322    return AnalyzeImplicitConversions(S, OVE->getSourceExpr(), CC);
5323
5324  // Skip past explicit casts.
5325  if (isa<ExplicitCastExpr>(E)) {
5326    E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts();
5327    return AnalyzeImplicitConversions(S, E, CC);
5328  }
5329
5330  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
5331    // Do a somewhat different check with comparison operators.
5332    if (BO->isComparisonOp())
5333      return AnalyzeComparison(S, BO);
5334
5335    // And with simple assignments.
5336    if (BO->getOpcode() == BO_Assign)
5337      return AnalyzeAssignment(S, BO);
5338  }
5339
5340  // These break the otherwise-useful invariant below.  Fortunately,
5341  // we don't really need to recurse into them, because any internal
5342  // expressions should have been analyzed already when they were
5343  // built into statements.
5344  if (isa<StmtExpr>(E)) return;
5345
5346  // Don't descend into unevaluated contexts.
5347  if (isa<UnaryExprOrTypeTraitExpr>(E)) return;
5348
5349  // Now just recurse over the expression's children.
5350  CC = E->getExprLoc();
5351  BinaryOperator *BO = dyn_cast<BinaryOperator>(E);
5352  bool IsLogicalOperator = BO && BO->isLogicalOp();
5353  for (Stmt::child_range I = E->children(); I; ++I) {
5354    Expr *ChildExpr = dyn_cast_or_null<Expr>(*I);
5355    if (!ChildExpr)
5356      continue;
5357
5358    if (IsLogicalOperator &&
5359        isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts()))
5360      // Ignore checking string literals that are in logical operators.
5361      continue;
5362    AnalyzeImplicitConversions(S, ChildExpr, CC);
5363  }
5364}
5365
5366} // end anonymous namespace
5367
5368/// Diagnoses "dangerous" implicit conversions within the given
5369/// expression (which is a full expression).  Implements -Wconversion
5370/// and -Wsign-compare.
5371///
5372/// \param CC the "context" location of the implicit conversion, i.e.
5373///   the most location of the syntactic entity requiring the implicit
5374///   conversion
5375void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) {
5376  // Don't diagnose in unevaluated contexts.
5377  if (isUnevaluatedContext())
5378    return;
5379
5380  // Don't diagnose for value- or type-dependent expressions.
5381  if (E->isTypeDependent() || E->isValueDependent())
5382    return;
5383
5384  // Check for array bounds violations in cases where the check isn't triggered
5385  // elsewhere for other Expr types (like BinaryOperators), e.g. when an
5386  // ArraySubscriptExpr is on the RHS of a variable initialization.
5387  CheckArrayAccess(E);
5388
5389  // This is not the right CC for (e.g.) a variable initialization.
5390  AnalyzeImplicitConversions(*this, E, CC);
5391}
5392
5393/// Diagnose when expression is an integer constant expression and its evaluation
5394/// results in integer overflow
5395void Sema::CheckForIntOverflow (Expr *E) {
5396  if (isa<BinaryOperator>(E->IgnoreParens())) {
5397    llvm::SmallVector<PartialDiagnosticAt, 4> Diags;
5398    E->EvaluateForOverflow(Context, &Diags);
5399  }
5400}
5401
5402namespace {
5403/// \brief Visitor for expressions which looks for unsequenced operations on the
5404/// same object.
5405class SequenceChecker : public EvaluatedExprVisitor<SequenceChecker> {
5406  typedef EvaluatedExprVisitor<SequenceChecker> Base;
5407
5408  /// \brief A tree of sequenced regions within an expression. Two regions are
5409  /// unsequenced if one is an ancestor or a descendent of the other. When we
5410  /// finish processing an expression with sequencing, such as a comma
5411  /// expression, we fold its tree nodes into its parent, since they are
5412  /// unsequenced with respect to nodes we will visit later.
5413  class SequenceTree {
5414    struct Value {
5415      explicit Value(unsigned Parent) : Parent(Parent), Merged(false) {}
5416      unsigned Parent : 31;
5417      bool Merged : 1;
5418    };
5419    llvm::SmallVector<Value, 8> Values;
5420
5421  public:
5422    /// \brief A region within an expression which may be sequenced with respect
5423    /// to some other region.
5424    class Seq {
5425      explicit Seq(unsigned N) : Index(N) {}
5426      unsigned Index;
5427      friend class SequenceTree;
5428    public:
5429      Seq() : Index(0) {}
5430    };
5431
5432    SequenceTree() { Values.push_back(Value(0)); }
5433    Seq root() const { return Seq(0); }
5434
5435    /// \brief Create a new sequence of operations, which is an unsequenced
5436    /// subset of \p Parent. This sequence of operations is sequenced with
5437    /// respect to other children of \p Parent.
5438    Seq allocate(Seq Parent) {
5439      Values.push_back(Value(Parent.Index));
5440      return Seq(Values.size() - 1);
5441    }
5442
5443    /// \brief Merge a sequence of operations into its parent.
5444    void merge(Seq S) {
5445      Values[S.Index].Merged = true;
5446    }
5447
5448    /// \brief Determine whether two operations are unsequenced. This operation
5449    /// is asymmetric: \p Cur should be the more recent sequence, and \p Old
5450    /// should have been merged into its parent as appropriate.
5451    bool isUnsequenced(Seq Cur, Seq Old) {
5452      unsigned C = representative(Cur.Index);
5453      unsigned Target = representative(Old.Index);
5454      while (C >= Target) {
5455        if (C == Target)
5456          return true;
5457        C = Values[C].Parent;
5458      }
5459      return false;
5460    }
5461
5462  private:
5463    /// \brief Pick a representative for a sequence.
5464    unsigned representative(unsigned K) {
5465      if (Values[K].Merged)
5466        // Perform path compression as we go.
5467        return Values[K].Parent = representative(Values[K].Parent);
5468      return K;
5469    }
5470  };
5471
5472  /// An object for which we can track unsequenced uses.
5473  typedef NamedDecl *Object;
5474
5475  /// Different flavors of object usage which we track. We only track the
5476  /// least-sequenced usage of each kind.
5477  enum UsageKind {
5478    /// A read of an object. Multiple unsequenced reads are OK.
5479    UK_Use,
5480    /// A modification of an object which is sequenced before the value
5481    /// computation of the expression, such as ++n in C++.
5482    UK_ModAsValue,
5483    /// A modification of an object which is not sequenced before the value
5484    /// computation of the expression, such as n++.
5485    UK_ModAsSideEffect,
5486
5487    UK_Count = UK_ModAsSideEffect + 1
5488  };
5489
5490  struct Usage {
5491    Usage() : Use(0), Seq() {}
5492    Expr *Use;
5493    SequenceTree::Seq Seq;
5494  };
5495
5496  struct UsageInfo {
5497    UsageInfo() : Diagnosed(false) {}
5498    Usage Uses[UK_Count];
5499    /// Have we issued a diagnostic for this variable already?
5500    bool Diagnosed;
5501  };
5502  typedef llvm::SmallDenseMap<Object, UsageInfo, 16> UsageInfoMap;
5503
5504  Sema &SemaRef;
5505  /// Sequenced regions within the expression.
5506  SequenceTree Tree;
5507  /// Declaration modifications and references which we have seen.
5508  UsageInfoMap UsageMap;
5509  /// The region we are currently within.
5510  SequenceTree::Seq Region;
5511  /// Filled in with declarations which were modified as a side-effect
5512  /// (that is, post-increment operations).
5513  llvm::SmallVectorImpl<std::pair<Object, Usage> > *ModAsSideEffect;
5514  /// Expressions to check later. We defer checking these to reduce
5515  /// stack usage.
5516  llvm::SmallVectorImpl<Expr*> &WorkList;
5517
5518  /// RAII object wrapping the visitation of a sequenced subexpression of an
5519  /// expression. At the end of this process, the side-effects of the evaluation
5520  /// become sequenced with respect to the value computation of the result, so
5521  /// we downgrade any UK_ModAsSideEffect within the evaluation to
5522  /// UK_ModAsValue.
5523  struct SequencedSubexpression {
5524    SequencedSubexpression(SequenceChecker &Self)
5525      : Self(Self), OldModAsSideEffect(Self.ModAsSideEffect) {
5526      Self.ModAsSideEffect = &ModAsSideEffect;
5527    }
5528    ~SequencedSubexpression() {
5529      for (unsigned I = 0, E = ModAsSideEffect.size(); I != E; ++I) {
5530        UsageInfo &U = Self.UsageMap[ModAsSideEffect[I].first];
5531        U.Uses[UK_ModAsSideEffect] = ModAsSideEffect[I].second;
5532        Self.addUsage(U, ModAsSideEffect[I].first,
5533                      ModAsSideEffect[I].second.Use, UK_ModAsValue);
5534      }
5535      Self.ModAsSideEffect = OldModAsSideEffect;
5536    }
5537
5538    SequenceChecker &Self;
5539    llvm::SmallVector<std::pair<Object, Usage>, 4> ModAsSideEffect;
5540    llvm::SmallVectorImpl<std::pair<Object, Usage> > *OldModAsSideEffect;
5541  };
5542
5543  /// RAII object wrapping the visitation of a subexpression which we might
5544  /// choose to evaluate as a constant. If any subexpression is evaluated and
5545  /// found to be non-constant, this allows us to suppress the evaluation of
5546  /// the outer expression.
5547  class EvaluationTracker {
5548  public:
5549    EvaluationTracker(SequenceChecker &Self)
5550        : Self(Self), Prev(Self.EvalTracker), EvalOK(true) {
5551      Self.EvalTracker = this;
5552    }
5553    ~EvaluationTracker() {
5554      Self.EvalTracker = Prev;
5555      if (Prev)
5556        Prev->EvalOK &= EvalOK;
5557    }
5558
5559    bool evaluate(const Expr *E, bool &Result) {
5560      if (!EvalOK || E->isValueDependent())
5561        return false;
5562      EvalOK = E->EvaluateAsBooleanCondition(Result, Self.SemaRef.Context);
5563      return EvalOK;
5564    }
5565
5566  private:
5567    SequenceChecker &Self;
5568    EvaluationTracker *Prev;
5569    bool EvalOK;
5570  } *EvalTracker;
5571
5572  /// \brief Find the object which is produced by the specified expression,
5573  /// if any.
5574  Object getObject(Expr *E, bool Mod) const {
5575    E = E->IgnoreParenCasts();
5576    if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
5577      if (Mod && (UO->getOpcode() == UO_PreInc || UO->getOpcode() == UO_PreDec))
5578        return getObject(UO->getSubExpr(), Mod);
5579    } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
5580      if (BO->getOpcode() == BO_Comma)
5581        return getObject(BO->getRHS(), Mod);
5582      if (Mod && BO->isAssignmentOp())
5583        return getObject(BO->getLHS(), Mod);
5584    } else if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
5585      // FIXME: Check for more interesting cases, like "x.n = ++x.n".
5586      if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenCasts()))
5587        return ME->getMemberDecl();
5588    } else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
5589      // FIXME: If this is a reference, map through to its value.
5590      return DRE->getDecl();
5591    return 0;
5592  }
5593
5594  /// \brief Note that an object was modified or used by an expression.
5595  void addUsage(UsageInfo &UI, Object O, Expr *Ref, UsageKind UK) {
5596    Usage &U = UI.Uses[UK];
5597    if (!U.Use || !Tree.isUnsequenced(Region, U.Seq)) {
5598      if (UK == UK_ModAsSideEffect && ModAsSideEffect)
5599        ModAsSideEffect->push_back(std::make_pair(O, U));
5600      U.Use = Ref;
5601      U.Seq = Region;
5602    }
5603  }
5604  /// \brief Check whether a modification or use conflicts with a prior usage.
5605  void checkUsage(Object O, UsageInfo &UI, Expr *Ref, UsageKind OtherKind,
5606                  bool IsModMod) {
5607    if (UI.Diagnosed)
5608      return;
5609
5610    const Usage &U = UI.Uses[OtherKind];
5611    if (!U.Use || !Tree.isUnsequenced(Region, U.Seq))
5612      return;
5613
5614    Expr *Mod = U.Use;
5615    Expr *ModOrUse = Ref;
5616    if (OtherKind == UK_Use)
5617      std::swap(Mod, ModOrUse);
5618
5619    SemaRef.Diag(Mod->getExprLoc(),
5620                 IsModMod ? diag::warn_unsequenced_mod_mod
5621                          : diag::warn_unsequenced_mod_use)
5622      << O << SourceRange(ModOrUse->getExprLoc());
5623    UI.Diagnosed = true;
5624  }
5625
5626  void notePreUse(Object O, Expr *Use) {
5627    UsageInfo &U = UsageMap[O];
5628    // Uses conflict with other modifications.
5629    checkUsage(O, U, Use, UK_ModAsValue, false);
5630  }
5631  void notePostUse(Object O, Expr *Use) {
5632    UsageInfo &U = UsageMap[O];
5633    checkUsage(O, U, Use, UK_ModAsSideEffect, false);
5634    addUsage(U, O, Use, UK_Use);
5635  }
5636
5637  void notePreMod(Object O, Expr *Mod) {
5638    UsageInfo &U = UsageMap[O];
5639    // Modifications conflict with other modifications and with uses.
5640    checkUsage(O, U, Mod, UK_ModAsValue, true);
5641    checkUsage(O, U, Mod, UK_Use, false);
5642  }
5643  void notePostMod(Object O, Expr *Use, UsageKind UK) {
5644    UsageInfo &U = UsageMap[O];
5645    checkUsage(O, U, Use, UK_ModAsSideEffect, true);
5646    addUsage(U, O, Use, UK);
5647  }
5648
5649public:
5650  SequenceChecker(Sema &S, Expr *E,
5651                  llvm::SmallVectorImpl<Expr*> &WorkList)
5652    : Base(S.Context), SemaRef(S), Region(Tree.root()),
5653      ModAsSideEffect(0), WorkList(WorkList), EvalTracker(0) {
5654    Visit(E);
5655  }
5656
5657  void VisitStmt(Stmt *S) {
5658    // Skip all statements which aren't expressions for now.
5659  }
5660
5661  void VisitExpr(Expr *E) {
5662    // By default, just recurse to evaluated subexpressions.
5663    Base::VisitStmt(E);
5664  }
5665
5666  void VisitCastExpr(CastExpr *E) {
5667    Object O = Object();
5668    if (E->getCastKind() == CK_LValueToRValue)
5669      O = getObject(E->getSubExpr(), false);
5670
5671    if (O)
5672      notePreUse(O, E);
5673    VisitExpr(E);
5674    if (O)
5675      notePostUse(O, E);
5676  }
5677
5678  void VisitBinComma(BinaryOperator *BO) {
5679    // C++11 [expr.comma]p1:
5680    //   Every value computation and side effect associated with the left
5681    //   expression is sequenced before every value computation and side
5682    //   effect associated with the right expression.
5683    SequenceTree::Seq LHS = Tree.allocate(Region);
5684    SequenceTree::Seq RHS = Tree.allocate(Region);
5685    SequenceTree::Seq OldRegion = Region;
5686
5687    {
5688      SequencedSubexpression SeqLHS(*this);
5689      Region = LHS;
5690      Visit(BO->getLHS());
5691    }
5692
5693    Region = RHS;
5694    Visit(BO->getRHS());
5695
5696    Region = OldRegion;
5697
5698    // Forget that LHS and RHS are sequenced. They are both unsequenced
5699    // with respect to other stuff.
5700    Tree.merge(LHS);
5701    Tree.merge(RHS);
5702  }
5703
5704  void VisitBinAssign(BinaryOperator *BO) {
5705    // The modification is sequenced after the value computation of the LHS
5706    // and RHS, so check it before inspecting the operands and update the
5707    // map afterwards.
5708    Object O = getObject(BO->getLHS(), true);
5709    if (!O)
5710      return VisitExpr(BO);
5711
5712    notePreMod(O, BO);
5713
5714    // C++11 [expr.ass]p7:
5715    //   E1 op= E2 is equivalent to E1 = E1 op E2, except that E1 is evaluated
5716    //   only once.
5717    //
5718    // Therefore, for a compound assignment operator, O is considered used
5719    // everywhere except within the evaluation of E1 itself.
5720    if (isa<CompoundAssignOperator>(BO))
5721      notePreUse(O, BO);
5722
5723    Visit(BO->getLHS());
5724
5725    if (isa<CompoundAssignOperator>(BO))
5726      notePostUse(O, BO);
5727
5728    Visit(BO->getRHS());
5729
5730    // C++11 [expr.ass]p1:
5731    //   the assignment is sequenced [...] before the value computation of the
5732    //   assignment expression.
5733    // C11 6.5.16/3 has no such rule.
5734    notePostMod(O, BO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue
5735                                                       : UK_ModAsSideEffect);
5736  }
5737  void VisitCompoundAssignOperator(CompoundAssignOperator *CAO) {
5738    VisitBinAssign(CAO);
5739  }
5740
5741  void VisitUnaryPreInc(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); }
5742  void VisitUnaryPreDec(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); }
5743  void VisitUnaryPreIncDec(UnaryOperator *UO) {
5744    Object O = getObject(UO->getSubExpr(), true);
5745    if (!O)
5746      return VisitExpr(UO);
5747
5748    notePreMod(O, UO);
5749    Visit(UO->getSubExpr());
5750    // C++11 [expr.pre.incr]p1:
5751    //   the expression ++x is equivalent to x+=1
5752    notePostMod(O, UO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue
5753                                                       : UK_ModAsSideEffect);
5754  }
5755
5756  void VisitUnaryPostInc(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); }
5757  void VisitUnaryPostDec(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); }
5758  void VisitUnaryPostIncDec(UnaryOperator *UO) {
5759    Object O = getObject(UO->getSubExpr(), true);
5760    if (!O)
5761      return VisitExpr(UO);
5762
5763    notePreMod(O, UO);
5764    Visit(UO->getSubExpr());
5765    notePostMod(O, UO, UK_ModAsSideEffect);
5766  }
5767
5768  /// Don't visit the RHS of '&&' or '||' if it might not be evaluated.
5769  void VisitBinLOr(BinaryOperator *BO) {
5770    // The side-effects of the LHS of an '&&' are sequenced before the
5771    // value computation of the RHS, and hence before the value computation
5772    // of the '&&' itself, unless the LHS evaluates to zero. We treat them
5773    // as if they were unconditionally sequenced.
5774    EvaluationTracker Eval(*this);
5775    {
5776      SequencedSubexpression Sequenced(*this);
5777      Visit(BO->getLHS());
5778    }
5779
5780    bool Result;
5781    if (Eval.evaluate(BO->getLHS(), Result)) {
5782      if (!Result)
5783        Visit(BO->getRHS());
5784    } else {
5785      // Check for unsequenced operations in the RHS, treating it as an
5786      // entirely separate evaluation.
5787      //
5788      // FIXME: If there are operations in the RHS which are unsequenced
5789      // with respect to operations outside the RHS, and those operations
5790      // are unconditionally evaluated, diagnose them.
5791      WorkList.push_back(BO->getRHS());
5792    }
5793  }
5794  void VisitBinLAnd(BinaryOperator *BO) {
5795    EvaluationTracker Eval(*this);
5796    {
5797      SequencedSubexpression Sequenced(*this);
5798      Visit(BO->getLHS());
5799    }
5800
5801    bool Result;
5802    if (Eval.evaluate(BO->getLHS(), Result)) {
5803      if (Result)
5804        Visit(BO->getRHS());
5805    } else {
5806      WorkList.push_back(BO->getRHS());
5807    }
5808  }
5809
5810  // Only visit the condition, unless we can be sure which subexpression will
5811  // be chosen.
5812  void VisitAbstractConditionalOperator(AbstractConditionalOperator *CO) {
5813    EvaluationTracker Eval(*this);
5814    {
5815      SequencedSubexpression Sequenced(*this);
5816      Visit(CO->getCond());
5817    }
5818
5819    bool Result;
5820    if (Eval.evaluate(CO->getCond(), Result))
5821      Visit(Result ? CO->getTrueExpr() : CO->getFalseExpr());
5822    else {
5823      WorkList.push_back(CO->getTrueExpr());
5824      WorkList.push_back(CO->getFalseExpr());
5825    }
5826  }
5827
5828  void VisitCallExpr(CallExpr *CE) {
5829    // C++11 [intro.execution]p15:
5830    //   When calling a function [...], every value computation and side effect
5831    //   associated with any argument expression, or with the postfix expression
5832    //   designating the called function, is sequenced before execution of every
5833    //   expression or statement in the body of the function [and thus before
5834    //   the value computation of its result].
5835    SequencedSubexpression Sequenced(*this);
5836    Base::VisitCallExpr(CE);
5837
5838    // FIXME: CXXNewExpr and CXXDeleteExpr implicitly call functions.
5839  }
5840
5841  void VisitCXXConstructExpr(CXXConstructExpr *CCE) {
5842    // This is a call, so all subexpressions are sequenced before the result.
5843    SequencedSubexpression Sequenced(*this);
5844
5845    if (!CCE->isListInitialization())
5846      return VisitExpr(CCE);
5847
5848    // In C++11, list initializations are sequenced.
5849    llvm::SmallVector<SequenceTree::Seq, 32> Elts;
5850    SequenceTree::Seq Parent = Region;
5851    for (CXXConstructExpr::arg_iterator I = CCE->arg_begin(),
5852                                        E = CCE->arg_end();
5853         I != E; ++I) {
5854      Region = Tree.allocate(Parent);
5855      Elts.push_back(Region);
5856      Visit(*I);
5857    }
5858
5859    // Forget that the initializers are sequenced.
5860    Region = Parent;
5861    for (unsigned I = 0; I < Elts.size(); ++I)
5862      Tree.merge(Elts[I]);
5863  }
5864
5865  void VisitInitListExpr(InitListExpr *ILE) {
5866    if (!SemaRef.getLangOpts().CPlusPlus11)
5867      return VisitExpr(ILE);
5868
5869    // In C++11, list initializations are sequenced.
5870    llvm::SmallVector<SequenceTree::Seq, 32> Elts;
5871    SequenceTree::Seq Parent = Region;
5872    for (unsigned I = 0; I < ILE->getNumInits(); ++I) {
5873      Expr *E = ILE->getInit(I);
5874      if (!E) continue;
5875      Region = Tree.allocate(Parent);
5876      Elts.push_back(Region);
5877      Visit(E);
5878    }
5879
5880    // Forget that the initializers are sequenced.
5881    Region = Parent;
5882    for (unsigned I = 0; I < Elts.size(); ++I)
5883      Tree.merge(Elts[I]);
5884  }
5885};
5886}
5887
5888void Sema::CheckUnsequencedOperations(Expr *E) {
5889  llvm::SmallVector<Expr*, 8> WorkList;
5890  WorkList.push_back(E);
5891  while (!WorkList.empty()) {
5892    Expr *Item = WorkList.back();
5893    WorkList.pop_back();
5894    SequenceChecker(*this, Item, WorkList);
5895  }
5896}
5897
5898void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc,
5899                              bool IsConstexpr) {
5900  CheckImplicitConversions(E, CheckLoc);
5901  CheckUnsequencedOperations(E);
5902  if (!IsConstexpr && !E->isValueDependent())
5903    CheckForIntOverflow(E);
5904}
5905
5906void Sema::CheckBitFieldInitialization(SourceLocation InitLoc,
5907                                       FieldDecl *BitField,
5908                                       Expr *Init) {
5909  (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc);
5910}
5911
5912/// CheckParmsForFunctionDef - Check that the parameters of the given
5913/// function are appropriate for the definition of a function. This
5914/// takes care of any checks that cannot be performed on the
5915/// declaration itself, e.g., that the types of each of the function
5916/// parameters are complete.
5917bool Sema::CheckParmsForFunctionDef(ParmVarDecl *const *P,
5918                                    ParmVarDecl *const *PEnd,
5919                                    bool CheckParameterNames) {
5920  bool HasInvalidParm = false;
5921  for (; P != PEnd; ++P) {
5922    ParmVarDecl *Param = *P;
5923
5924    // C99 6.7.5.3p4: the parameters in a parameter type list in a
5925    // function declarator that is part of a function definition of
5926    // that function shall not have incomplete type.
5927    //
5928    // This is also C++ [dcl.fct]p6.
5929    if (!Param->isInvalidDecl() &&
5930        RequireCompleteType(Param->getLocation(), Param->getType(),
5931                            diag::err_typecheck_decl_incomplete_type)) {
5932      Param->setInvalidDecl();
5933      HasInvalidParm = true;
5934    }
5935
5936    // C99 6.9.1p5: If the declarator includes a parameter type list, the
5937    // declaration of each parameter shall include an identifier.
5938    if (CheckParameterNames &&
5939        Param->getIdentifier() == 0 &&
5940        !Param->isImplicit() &&
5941        !getLangOpts().CPlusPlus)
5942      Diag(Param->getLocation(), diag::err_parameter_name_omitted);
5943
5944    // C99 6.7.5.3p12:
5945    //   If the function declarator is not part of a definition of that
5946    //   function, parameters may have incomplete type and may use the [*]
5947    //   notation in their sequences of declarator specifiers to specify
5948    //   variable length array types.
5949    QualType PType = Param->getOriginalType();
5950    while (const ArrayType *AT = Context.getAsArrayType(PType)) {
5951      if (AT->getSizeModifier() == ArrayType::Star) {
5952        // FIXME: This diagnostic should point the '[*]' if source-location
5953        // information is added for it.
5954        Diag(Param->getLocation(), diag::err_array_star_in_function_definition);
5955        break;
5956      }
5957      PType= AT->getElementType();
5958    }
5959
5960    // MSVC destroys objects passed by value in the callee.  Therefore a
5961    // function definition which takes such a parameter must be able to call the
5962    // object's destructor.
5963    if (getLangOpts().CPlusPlus &&
5964        Context.getTargetInfo().getCXXABI().isArgumentDestroyedByCallee()) {
5965      if (const RecordType *RT = Param->getType()->getAs<RecordType>())
5966        FinalizeVarWithDestructor(Param, RT);
5967    }
5968  }
5969
5970  return HasInvalidParm;
5971}
5972
5973/// CheckCastAlign - Implements -Wcast-align, which warns when a
5974/// pointer cast increases the alignment requirements.
5975void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) {
5976  // This is actually a lot of work to potentially be doing on every
5977  // cast; don't do it if we're ignoring -Wcast_align (as is the default).
5978  if (getDiagnostics().getDiagnosticLevel(diag::warn_cast_align,
5979                                          TRange.getBegin())
5980        == DiagnosticsEngine::Ignored)
5981    return;
5982
5983  // Ignore dependent types.
5984  if (T->isDependentType() || Op->getType()->isDependentType())
5985    return;
5986
5987  // Require that the destination be a pointer type.
5988  const PointerType *DestPtr = T->getAs<PointerType>();
5989  if (!DestPtr) return;
5990
5991  // If the destination has alignment 1, we're done.
5992  QualType DestPointee = DestPtr->getPointeeType();
5993  if (DestPointee->isIncompleteType()) return;
5994  CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee);
5995  if (DestAlign.isOne()) return;
5996
5997  // Require that the source be a pointer type.
5998  const PointerType *SrcPtr = Op->getType()->getAs<PointerType>();
5999  if (!SrcPtr) return;
6000  QualType SrcPointee = SrcPtr->getPointeeType();
6001
6002  // Whitelist casts from cv void*.  We already implicitly
6003  // whitelisted casts to cv void*, since they have alignment 1.
6004  // Also whitelist casts involving incomplete types, which implicitly
6005  // includes 'void'.
6006  if (SrcPointee->isIncompleteType()) return;
6007
6008  CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee);
6009  if (SrcAlign >= DestAlign) return;
6010
6011  Diag(TRange.getBegin(), diag::warn_cast_align)
6012    << Op->getType() << T
6013    << static_cast<unsigned>(SrcAlign.getQuantity())
6014    << static_cast<unsigned>(DestAlign.getQuantity())
6015    << TRange << Op->getSourceRange();
6016}
6017
6018static const Type* getElementType(const Expr *BaseExpr) {
6019  const Type* EltType = BaseExpr->getType().getTypePtr();
6020  if (EltType->isAnyPointerType())
6021    return EltType->getPointeeType().getTypePtr();
6022  else if (EltType->isArrayType())
6023    return EltType->getBaseElementTypeUnsafe();
6024  return EltType;
6025}
6026
6027/// \brief Check whether this array fits the idiom of a size-one tail padded
6028/// array member of a struct.
6029///
6030/// We avoid emitting out-of-bounds access warnings for such arrays as they are
6031/// commonly used to emulate flexible arrays in C89 code.
6032static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size,
6033                                    const NamedDecl *ND) {
6034  if (Size != 1 || !ND) return false;
6035
6036  const FieldDecl *FD = dyn_cast<FieldDecl>(ND);
6037  if (!FD) return false;
6038
6039  // Don't consider sizes resulting from macro expansions or template argument
6040  // substitution to form C89 tail-padded arrays.
6041
6042  TypeSourceInfo *TInfo = FD->getTypeSourceInfo();
6043  while (TInfo) {
6044    TypeLoc TL = TInfo->getTypeLoc();
6045    // Look through typedefs.
6046    if (TypedefTypeLoc TTL = TL.getAs<TypedefTypeLoc>()) {
6047      const TypedefNameDecl *TDL = TTL.getTypedefNameDecl();
6048      TInfo = TDL->getTypeSourceInfo();
6049      continue;
6050    }
6051    if (ConstantArrayTypeLoc CTL = TL.getAs<ConstantArrayTypeLoc>()) {
6052      const Expr *SizeExpr = dyn_cast<IntegerLiteral>(CTL.getSizeExpr());
6053      if (!SizeExpr || SizeExpr->getExprLoc().isMacroID())
6054        return false;
6055    }
6056    break;
6057  }
6058
6059  const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext());
6060  if (!RD) return false;
6061  if (RD->isUnion()) return false;
6062  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
6063    if (!CRD->isStandardLayout()) return false;
6064  }
6065
6066  // See if this is the last field decl in the record.
6067  const Decl *D = FD;
6068  while ((D = D->getNextDeclInContext()))
6069    if (isa<FieldDecl>(D))
6070      return false;
6071  return true;
6072}
6073
6074void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
6075                            const ArraySubscriptExpr *ASE,
6076                            bool AllowOnePastEnd, bool IndexNegated) {
6077  IndexExpr = IndexExpr->IgnoreParenImpCasts();
6078  if (IndexExpr->isValueDependent())
6079    return;
6080
6081  const Type *EffectiveType = getElementType(BaseExpr);
6082  BaseExpr = BaseExpr->IgnoreParenCasts();
6083  const ConstantArrayType *ArrayTy =
6084    Context.getAsConstantArrayType(BaseExpr->getType());
6085  if (!ArrayTy)
6086    return;
6087
6088  llvm::APSInt index;
6089  if (!IndexExpr->EvaluateAsInt(index, Context))
6090    return;
6091  if (IndexNegated)
6092    index = -index;
6093
6094  const NamedDecl *ND = NULL;
6095  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
6096    ND = dyn_cast<NamedDecl>(DRE->getDecl());
6097  if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
6098    ND = dyn_cast<NamedDecl>(ME->getMemberDecl());
6099
6100  if (index.isUnsigned() || !index.isNegative()) {
6101    llvm::APInt size = ArrayTy->getSize();
6102    if (!size.isStrictlyPositive())
6103      return;
6104
6105    const Type* BaseType = getElementType(BaseExpr);
6106    if (BaseType != EffectiveType) {
6107      // Make sure we're comparing apples to apples when comparing index to size
6108      uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType);
6109      uint64_t array_typesize = Context.getTypeSize(BaseType);
6110      // Handle ptrarith_typesize being zero, such as when casting to void*
6111      if (!ptrarith_typesize) ptrarith_typesize = 1;
6112      if (ptrarith_typesize != array_typesize) {
6113        // There's a cast to a different size type involved
6114        uint64_t ratio = array_typesize / ptrarith_typesize;
6115        // TODO: Be smarter about handling cases where array_typesize is not a
6116        // multiple of ptrarith_typesize
6117        if (ptrarith_typesize * ratio == array_typesize)
6118          size *= llvm::APInt(size.getBitWidth(), ratio);
6119      }
6120    }
6121
6122    if (size.getBitWidth() > index.getBitWidth())
6123      index = index.zext(size.getBitWidth());
6124    else if (size.getBitWidth() < index.getBitWidth())
6125      size = size.zext(index.getBitWidth());
6126
6127    // For array subscripting the index must be less than size, but for pointer
6128    // arithmetic also allow the index (offset) to be equal to size since
6129    // computing the next address after the end of the array is legal and
6130    // commonly done e.g. in C++ iterators and range-based for loops.
6131    if (AllowOnePastEnd ? index.ule(size) : index.ult(size))
6132      return;
6133
6134    // Also don't warn for arrays of size 1 which are members of some
6135    // structure. These are often used to approximate flexible arrays in C89
6136    // code.
6137    if (IsTailPaddedMemberArray(*this, size, ND))
6138      return;
6139
6140    // Suppress the warning if the subscript expression (as identified by the
6141    // ']' location) and the index expression are both from macro expansions
6142    // within a system header.
6143    if (ASE) {
6144      SourceLocation RBracketLoc = SourceMgr.getSpellingLoc(
6145          ASE->getRBracketLoc());
6146      if (SourceMgr.isInSystemHeader(RBracketLoc)) {
6147        SourceLocation IndexLoc = SourceMgr.getSpellingLoc(
6148            IndexExpr->getLocStart());
6149        if (SourceMgr.isFromSameFile(RBracketLoc, IndexLoc))
6150          return;
6151      }
6152    }
6153
6154    unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds;
6155    if (ASE)
6156      DiagID = diag::warn_array_index_exceeds_bounds;
6157
6158    DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr,
6159                        PDiag(DiagID) << index.toString(10, true)
6160                          << size.toString(10, true)
6161                          << (unsigned)size.getLimitedValue(~0U)
6162                          << IndexExpr->getSourceRange());
6163  } else {
6164    unsigned DiagID = diag::warn_array_index_precedes_bounds;
6165    if (!ASE) {
6166      DiagID = diag::warn_ptr_arith_precedes_bounds;
6167      if (index.isNegative()) index = -index;
6168    }
6169
6170    DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr,
6171                        PDiag(DiagID) << index.toString(10, true)
6172                          << IndexExpr->getSourceRange());
6173  }
6174
6175  if (!ND) {
6176    // Try harder to find a NamedDecl to point at in the note.
6177    while (const ArraySubscriptExpr *ASE =
6178           dyn_cast<ArraySubscriptExpr>(BaseExpr))
6179      BaseExpr = ASE->getBase()->IgnoreParenCasts();
6180    if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
6181      ND = dyn_cast<NamedDecl>(DRE->getDecl());
6182    if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
6183      ND = dyn_cast<NamedDecl>(ME->getMemberDecl());
6184  }
6185
6186  if (ND)
6187    DiagRuntimeBehavior(ND->getLocStart(), BaseExpr,
6188                        PDiag(diag::note_array_index_out_of_bounds)
6189                          << ND->getDeclName());
6190}
6191
6192void Sema::CheckArrayAccess(const Expr *expr) {
6193  int AllowOnePastEnd = 0;
6194  while (expr) {
6195    expr = expr->IgnoreParenImpCasts();
6196    switch (expr->getStmtClass()) {
6197      case Stmt::ArraySubscriptExprClass: {
6198        const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr);
6199        CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE,
6200                         AllowOnePastEnd > 0);
6201        return;
6202      }
6203      case Stmt::UnaryOperatorClass: {
6204        // Only unwrap the * and & unary operators
6205        const UnaryOperator *UO = cast<UnaryOperator>(expr);
6206        expr = UO->getSubExpr();
6207        switch (UO->getOpcode()) {
6208          case UO_AddrOf:
6209            AllowOnePastEnd++;
6210            break;
6211          case UO_Deref:
6212            AllowOnePastEnd--;
6213            break;
6214          default:
6215            return;
6216        }
6217        break;
6218      }
6219      case Stmt::ConditionalOperatorClass: {
6220        const ConditionalOperator *cond = cast<ConditionalOperator>(expr);
6221        if (const Expr *lhs = cond->getLHS())
6222          CheckArrayAccess(lhs);
6223        if (const Expr *rhs = cond->getRHS())
6224          CheckArrayAccess(rhs);
6225        return;
6226      }
6227      default:
6228        return;
6229    }
6230  }
6231}
6232
6233//===--- CHECK: Objective-C retain cycles ----------------------------------//
6234
6235namespace {
6236  struct RetainCycleOwner {
6237    RetainCycleOwner() : Variable(0), Indirect(false) {}
6238    VarDecl *Variable;
6239    SourceRange Range;
6240    SourceLocation Loc;
6241    bool Indirect;
6242
6243    void setLocsFrom(Expr *e) {
6244      Loc = e->getExprLoc();
6245      Range = e->getSourceRange();
6246    }
6247  };
6248}
6249
6250/// Consider whether capturing the given variable can possibly lead to
6251/// a retain cycle.
6252static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) {
6253  // In ARC, it's captured strongly iff the variable has __strong
6254  // lifetime.  In MRR, it's captured strongly if the variable is
6255  // __block and has an appropriate type.
6256  if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
6257    return false;
6258
6259  owner.Variable = var;
6260  if (ref)
6261    owner.setLocsFrom(ref);
6262  return true;
6263}
6264
6265static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) {
6266  while (true) {
6267    e = e->IgnoreParens();
6268    if (CastExpr *cast = dyn_cast<CastExpr>(e)) {
6269      switch (cast->getCastKind()) {
6270      case CK_BitCast:
6271      case CK_LValueBitCast:
6272      case CK_LValueToRValue:
6273      case CK_ARCReclaimReturnedObject:
6274        e = cast->getSubExpr();
6275        continue;
6276
6277      default:
6278        return false;
6279      }
6280    }
6281
6282    if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) {
6283      ObjCIvarDecl *ivar = ref->getDecl();
6284      if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
6285        return false;
6286
6287      // Try to find a retain cycle in the base.
6288      if (!findRetainCycleOwner(S, ref->getBase(), owner))
6289        return false;
6290
6291      if (ref->isFreeIvar()) owner.setLocsFrom(ref);
6292      owner.Indirect = true;
6293      return true;
6294    }
6295
6296    if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) {
6297      VarDecl *var = dyn_cast<VarDecl>(ref->getDecl());
6298      if (!var) return false;
6299      return considerVariable(var, ref, owner);
6300    }
6301
6302    if (MemberExpr *member = dyn_cast<MemberExpr>(e)) {
6303      if (member->isArrow()) return false;
6304
6305      // Don't count this as an indirect ownership.
6306      e = member->getBase();
6307      continue;
6308    }
6309
6310    if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) {
6311      // Only pay attention to pseudo-objects on property references.
6312      ObjCPropertyRefExpr *pre
6313        = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm()
6314                                              ->IgnoreParens());
6315      if (!pre) return false;
6316      if (pre->isImplicitProperty()) return false;
6317      ObjCPropertyDecl *property = pre->getExplicitProperty();
6318      if (!property->isRetaining() &&
6319          !(property->getPropertyIvarDecl() &&
6320            property->getPropertyIvarDecl()->getType()
6321              .getObjCLifetime() == Qualifiers::OCL_Strong))
6322          return false;
6323
6324      owner.Indirect = true;
6325      if (pre->isSuperReceiver()) {
6326        owner.Variable = S.getCurMethodDecl()->getSelfDecl();
6327        if (!owner.Variable)
6328          return false;
6329        owner.Loc = pre->getLocation();
6330        owner.Range = pre->getSourceRange();
6331        return true;
6332      }
6333      e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase())
6334                              ->getSourceExpr());
6335      continue;
6336    }
6337
6338    // Array ivars?
6339
6340    return false;
6341  }
6342}
6343
6344namespace {
6345  struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> {
6346    FindCaptureVisitor(ASTContext &Context, VarDecl *variable)
6347      : EvaluatedExprVisitor<FindCaptureVisitor>(Context),
6348        Variable(variable), Capturer(0) {}
6349
6350    VarDecl *Variable;
6351    Expr *Capturer;
6352
6353    void VisitDeclRefExpr(DeclRefExpr *ref) {
6354      if (ref->getDecl() == Variable && !Capturer)
6355        Capturer = ref;
6356    }
6357
6358    void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) {
6359      if (Capturer) return;
6360      Visit(ref->getBase());
6361      if (Capturer && ref->isFreeIvar())
6362        Capturer = ref;
6363    }
6364
6365    void VisitBlockExpr(BlockExpr *block) {
6366      // Look inside nested blocks
6367      if (block->getBlockDecl()->capturesVariable(Variable))
6368        Visit(block->getBlockDecl()->getBody());
6369    }
6370
6371    void VisitOpaqueValueExpr(OpaqueValueExpr *OVE) {
6372      if (Capturer) return;
6373      if (OVE->getSourceExpr())
6374        Visit(OVE->getSourceExpr());
6375    }
6376  };
6377}
6378
6379/// Check whether the given argument is a block which captures a
6380/// variable.
6381static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) {
6382  assert(owner.Variable && owner.Loc.isValid());
6383
6384  e = e->IgnoreParenCasts();
6385
6386  // Look through [^{...} copy] and Block_copy(^{...}).
6387  if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(e)) {
6388    Selector Cmd = ME->getSelector();
6389    if (Cmd.isUnarySelector() && Cmd.getNameForSlot(0) == "copy") {
6390      e = ME->getInstanceReceiver();
6391      if (!e)
6392        return 0;
6393      e = e->IgnoreParenCasts();
6394    }
6395  } else if (CallExpr *CE = dyn_cast<CallExpr>(e)) {
6396    if (CE->getNumArgs() == 1) {
6397      FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(CE->getCalleeDecl());
6398      if (Fn) {
6399        const IdentifierInfo *FnI = Fn->getIdentifier();
6400        if (FnI && FnI->isStr("_Block_copy")) {
6401          e = CE->getArg(0)->IgnoreParenCasts();
6402        }
6403      }
6404    }
6405  }
6406
6407  BlockExpr *block = dyn_cast<BlockExpr>(e);
6408  if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable))
6409    return 0;
6410
6411  FindCaptureVisitor visitor(S.Context, owner.Variable);
6412  visitor.Visit(block->getBlockDecl()->getBody());
6413  return visitor.Capturer;
6414}
6415
6416static void diagnoseRetainCycle(Sema &S, Expr *capturer,
6417                                RetainCycleOwner &owner) {
6418  assert(capturer);
6419  assert(owner.Variable && owner.Loc.isValid());
6420
6421  S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle)
6422    << owner.Variable << capturer->getSourceRange();
6423  S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner)
6424    << owner.Indirect << owner.Range;
6425}
6426
6427/// Check for a keyword selector that starts with the word 'add' or
6428/// 'set'.
6429static bool isSetterLikeSelector(Selector sel) {
6430  if (sel.isUnarySelector()) return false;
6431
6432  StringRef str = sel.getNameForSlot(0);
6433  while (!str.empty() && str.front() == '_') str = str.substr(1);
6434  if (str.startswith("set"))
6435    str = str.substr(3);
6436  else if (str.startswith("add")) {
6437    // Specially whitelist 'addOperationWithBlock:'.
6438    if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock"))
6439      return false;
6440    str = str.substr(3);
6441  }
6442  else
6443    return false;
6444
6445  if (str.empty()) return true;
6446  return !isLowercase(str.front());
6447}
6448
6449/// Check a message send to see if it's likely to cause a retain cycle.
6450void Sema::checkRetainCycles(ObjCMessageExpr *msg) {
6451  // Only check instance methods whose selector looks like a setter.
6452  if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector()))
6453    return;
6454
6455  // Try to find a variable that the receiver is strongly owned by.
6456  RetainCycleOwner owner;
6457  if (msg->getReceiverKind() == ObjCMessageExpr::Instance) {
6458    if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner))
6459      return;
6460  } else {
6461    assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance);
6462    owner.Variable = getCurMethodDecl()->getSelfDecl();
6463    owner.Loc = msg->getSuperLoc();
6464    owner.Range = msg->getSuperLoc();
6465  }
6466
6467  // Check whether the receiver is captured by any of the arguments.
6468  for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i)
6469    if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner))
6470      return diagnoseRetainCycle(*this, capturer, owner);
6471}
6472
6473/// Check a property assign to see if it's likely to cause a retain cycle.
6474void Sema::checkRetainCycles(Expr *receiver, Expr *argument) {
6475  RetainCycleOwner owner;
6476  if (!findRetainCycleOwner(*this, receiver, owner))
6477    return;
6478
6479  if (Expr *capturer = findCapturingExpr(*this, argument, owner))
6480    diagnoseRetainCycle(*this, capturer, owner);
6481}
6482
6483void Sema::checkRetainCycles(VarDecl *Var, Expr *Init) {
6484  RetainCycleOwner Owner;
6485  if (!considerVariable(Var, /*DeclRefExpr=*/0, Owner))
6486    return;
6487
6488  // Because we don't have an expression for the variable, we have to set the
6489  // location explicitly here.
6490  Owner.Loc = Var->getLocation();
6491  Owner.Range = Var->getSourceRange();
6492
6493  if (Expr *Capturer = findCapturingExpr(*this, Init, Owner))
6494    diagnoseRetainCycle(*this, Capturer, Owner);
6495}
6496
6497static bool checkUnsafeAssignLiteral(Sema &S, SourceLocation Loc,
6498                                     Expr *RHS, bool isProperty) {
6499  // Check if RHS is an Objective-C object literal, which also can get
6500  // immediately zapped in a weak reference.  Note that we explicitly
6501  // allow ObjCStringLiterals, since those are designed to never really die.
6502  RHS = RHS->IgnoreParenImpCasts();
6503
6504  // This enum needs to match with the 'select' in
6505  // warn_objc_arc_literal_assign (off-by-1).
6506  Sema::ObjCLiteralKind Kind = S.CheckLiteralKind(RHS);
6507  if (Kind == Sema::LK_String || Kind == Sema::LK_None)
6508    return false;
6509
6510  S.Diag(Loc, diag::warn_arc_literal_assign)
6511    << (unsigned) Kind
6512    << (isProperty ? 0 : 1)
6513    << RHS->getSourceRange();
6514
6515  return true;
6516}
6517
6518static bool checkUnsafeAssignObject(Sema &S, SourceLocation Loc,
6519                                    Qualifiers::ObjCLifetime LT,
6520                                    Expr *RHS, bool isProperty) {
6521  // Strip off any implicit cast added to get to the one ARC-specific.
6522  while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
6523    if (cast->getCastKind() == CK_ARCConsumeObject) {
6524      S.Diag(Loc, diag::warn_arc_retained_assign)
6525        << (LT == Qualifiers::OCL_ExplicitNone)
6526        << (isProperty ? 0 : 1)
6527        << RHS->getSourceRange();
6528      return true;
6529    }
6530    RHS = cast->getSubExpr();
6531  }
6532
6533  if (LT == Qualifiers::OCL_Weak &&
6534      checkUnsafeAssignLiteral(S, Loc, RHS, isProperty))
6535    return true;
6536
6537  return false;
6538}
6539
6540bool Sema::checkUnsafeAssigns(SourceLocation Loc,
6541                              QualType LHS, Expr *RHS) {
6542  Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime();
6543
6544  if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone)
6545    return false;
6546
6547  if (checkUnsafeAssignObject(*this, Loc, LT, RHS, false))
6548    return true;
6549
6550  return false;
6551}
6552
6553void Sema::checkUnsafeExprAssigns(SourceLocation Loc,
6554                              Expr *LHS, Expr *RHS) {
6555  QualType LHSType;
6556  // PropertyRef on LHS type need be directly obtained from
6557  // its declaration as it has a PsuedoType.
6558  ObjCPropertyRefExpr *PRE
6559    = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens());
6560  if (PRE && !PRE->isImplicitProperty()) {
6561    const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
6562    if (PD)
6563      LHSType = PD->getType();
6564  }
6565
6566  if (LHSType.isNull())
6567    LHSType = LHS->getType();
6568
6569  Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime();
6570
6571  if (LT == Qualifiers::OCL_Weak) {
6572    DiagnosticsEngine::Level Level =
6573      Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, Loc);
6574    if (Level != DiagnosticsEngine::Ignored)
6575      getCurFunction()->markSafeWeakUse(LHS);
6576  }
6577
6578  if (checkUnsafeAssigns(Loc, LHSType, RHS))
6579    return;
6580
6581  // FIXME. Check for other life times.
6582  if (LT != Qualifiers::OCL_None)
6583    return;
6584
6585  if (PRE) {
6586    if (PRE->isImplicitProperty())
6587      return;
6588    const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
6589    if (!PD)
6590      return;
6591
6592    unsigned Attributes = PD->getPropertyAttributes();
6593    if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) {
6594      // when 'assign' attribute was not explicitly specified
6595      // by user, ignore it and rely on property type itself
6596      // for lifetime info.
6597      unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten();
6598      if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) &&
6599          LHSType->isObjCRetainableType())
6600        return;
6601
6602      while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
6603        if (cast->getCastKind() == CK_ARCConsumeObject) {
6604          Diag(Loc, diag::warn_arc_retained_property_assign)
6605          << RHS->getSourceRange();
6606          return;
6607        }
6608        RHS = cast->getSubExpr();
6609      }
6610    }
6611    else if (Attributes & ObjCPropertyDecl::OBJC_PR_weak) {
6612      if (checkUnsafeAssignObject(*this, Loc, Qualifiers::OCL_Weak, RHS, true))
6613        return;
6614    }
6615  }
6616}
6617
6618//===--- CHECK: Empty statement body (-Wempty-body) ---------------------===//
6619
6620namespace {
6621bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr,
6622                                 SourceLocation StmtLoc,
6623                                 const NullStmt *Body) {
6624  // Do not warn if the body is a macro that expands to nothing, e.g:
6625  //
6626  // #define CALL(x)
6627  // if (condition)
6628  //   CALL(0);
6629  //
6630  if (Body->hasLeadingEmptyMacro())
6631    return false;
6632
6633  // Get line numbers of statement and body.
6634  bool StmtLineInvalid;
6635  unsigned StmtLine = SourceMgr.getSpellingLineNumber(StmtLoc,
6636                                                      &StmtLineInvalid);
6637  if (StmtLineInvalid)
6638    return false;
6639
6640  bool BodyLineInvalid;
6641  unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(),
6642                                                      &BodyLineInvalid);
6643  if (BodyLineInvalid)
6644    return false;
6645
6646  // Warn if null statement and body are on the same line.
6647  if (StmtLine != BodyLine)
6648    return false;
6649
6650  return true;
6651}
6652} // Unnamed namespace
6653
6654void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc,
6655                                 const Stmt *Body,
6656                                 unsigned DiagID) {
6657  // Since this is a syntactic check, don't emit diagnostic for template
6658  // instantiations, this just adds noise.
6659  if (CurrentInstantiationScope)
6660    return;
6661
6662  // The body should be a null statement.
6663  const NullStmt *NBody = dyn_cast<NullStmt>(Body);
6664  if (!NBody)
6665    return;
6666
6667  // Do the usual checks.
6668  if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody))
6669    return;
6670
6671  Diag(NBody->getSemiLoc(), DiagID);
6672  Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line);
6673}
6674
6675void Sema::DiagnoseEmptyLoopBody(const Stmt *S,
6676                                 const Stmt *PossibleBody) {
6677  assert(!CurrentInstantiationScope); // Ensured by caller
6678
6679  SourceLocation StmtLoc;
6680  const Stmt *Body;
6681  unsigned DiagID;
6682  if (const ForStmt *FS = dyn_cast<ForStmt>(S)) {
6683    StmtLoc = FS->getRParenLoc();
6684    Body = FS->getBody();
6685    DiagID = diag::warn_empty_for_body;
6686  } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) {
6687    StmtLoc = WS->getCond()->getSourceRange().getEnd();
6688    Body = WS->getBody();
6689    DiagID = diag::warn_empty_while_body;
6690  } else
6691    return; // Neither `for' nor `while'.
6692
6693  // The body should be a null statement.
6694  const NullStmt *NBody = dyn_cast<NullStmt>(Body);
6695  if (!NBody)
6696    return;
6697
6698  // Skip expensive checks if diagnostic is disabled.
6699  if (Diags.getDiagnosticLevel(DiagID, NBody->getSemiLoc()) ==
6700          DiagnosticsEngine::Ignored)
6701    return;
6702
6703  // Do the usual checks.
6704  if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody))
6705    return;
6706
6707  // `for(...);' and `while(...);' are popular idioms, so in order to keep
6708  // noise level low, emit diagnostics only if for/while is followed by a
6709  // CompoundStmt, e.g.:
6710  //    for (int i = 0; i < n; i++);
6711  //    {
6712  //      a(i);
6713  //    }
6714  // or if for/while is followed by a statement with more indentation
6715  // than for/while itself:
6716  //    for (int i = 0; i < n; i++);
6717  //      a(i);
6718  bool ProbableTypo = isa<CompoundStmt>(PossibleBody);
6719  if (!ProbableTypo) {
6720    bool BodyColInvalid;
6721    unsigned BodyCol = SourceMgr.getPresumedColumnNumber(
6722                             PossibleBody->getLocStart(),
6723                             &BodyColInvalid);
6724    if (BodyColInvalid)
6725      return;
6726
6727    bool StmtColInvalid;
6728    unsigned StmtCol = SourceMgr.getPresumedColumnNumber(
6729                             S->getLocStart(),
6730                             &StmtColInvalid);
6731    if (StmtColInvalid)
6732      return;
6733
6734    if (BodyCol > StmtCol)
6735      ProbableTypo = true;
6736  }
6737
6738  if (ProbableTypo) {
6739    Diag(NBody->getSemiLoc(), DiagID);
6740    Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line);
6741  }
6742}
6743
6744//===--- Layout compatibility ----------------------------------------------//
6745
6746namespace {
6747
6748bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2);
6749
6750/// \brief Check if two enumeration types are layout-compatible.
6751bool isLayoutCompatible(ASTContext &C, EnumDecl *ED1, EnumDecl *ED2) {
6752  // C++11 [dcl.enum] p8:
6753  // Two enumeration types are layout-compatible if they have the same
6754  // underlying type.
6755  return ED1->isComplete() && ED2->isComplete() &&
6756         C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType());
6757}
6758
6759/// \brief Check if two fields are layout-compatible.
6760bool isLayoutCompatible(ASTContext &C, FieldDecl *Field1, FieldDecl *Field2) {
6761  if (!isLayoutCompatible(C, Field1->getType(), Field2->getType()))
6762    return false;
6763
6764  if (Field1->isBitField() != Field2->isBitField())
6765    return false;
6766
6767  if (Field1->isBitField()) {
6768    // Make sure that the bit-fields are the same length.
6769    unsigned Bits1 = Field1->getBitWidthValue(C);
6770    unsigned Bits2 = Field2->getBitWidthValue(C);
6771
6772    if (Bits1 != Bits2)
6773      return false;
6774  }
6775
6776  return true;
6777}
6778
6779/// \brief Check if two standard-layout structs are layout-compatible.
6780/// (C++11 [class.mem] p17)
6781bool isLayoutCompatibleStruct(ASTContext &C,
6782                              RecordDecl *RD1,
6783                              RecordDecl *RD2) {
6784  // If both records are C++ classes, check that base classes match.
6785  if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1)) {
6786    // If one of records is a CXXRecordDecl we are in C++ mode,
6787    // thus the other one is a CXXRecordDecl, too.
6788    const CXXRecordDecl *D2CXX = cast<CXXRecordDecl>(RD2);
6789    // Check number of base classes.
6790    if (D1CXX->getNumBases() != D2CXX->getNumBases())
6791      return false;
6792
6793    // Check the base classes.
6794    for (CXXRecordDecl::base_class_const_iterator
6795               Base1 = D1CXX->bases_begin(),
6796           BaseEnd1 = D1CXX->bases_end(),
6797              Base2 = D2CXX->bases_begin();
6798         Base1 != BaseEnd1;
6799         ++Base1, ++Base2) {
6800      if (!isLayoutCompatible(C, Base1->getType(), Base2->getType()))
6801        return false;
6802    }
6803  } else if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2)) {
6804    // If only RD2 is a C++ class, it should have zero base classes.
6805    if (D2CXX->getNumBases() > 0)
6806      return false;
6807  }
6808
6809  // Check the fields.
6810  RecordDecl::field_iterator Field2 = RD2->field_begin(),
6811                             Field2End = RD2->field_end(),
6812                             Field1 = RD1->field_begin(),
6813                             Field1End = RD1->field_end();
6814  for ( ; Field1 != Field1End && Field2 != Field2End; ++Field1, ++Field2) {
6815    if (!isLayoutCompatible(C, *Field1, *Field2))
6816      return false;
6817  }
6818  if (Field1 != Field1End || Field2 != Field2End)
6819    return false;
6820
6821  return true;
6822}
6823
6824/// \brief Check if two standard-layout unions are layout-compatible.
6825/// (C++11 [class.mem] p18)
6826bool isLayoutCompatibleUnion(ASTContext &C,
6827                             RecordDecl *RD1,
6828                             RecordDecl *RD2) {
6829  llvm::SmallPtrSet<FieldDecl *, 8> UnmatchedFields;
6830  for (RecordDecl::field_iterator Field2 = RD2->field_begin(),
6831                                  Field2End = RD2->field_end();
6832       Field2 != Field2End; ++Field2) {
6833    UnmatchedFields.insert(*Field2);
6834  }
6835
6836  for (RecordDecl::field_iterator Field1 = RD1->field_begin(),
6837                                  Field1End = RD1->field_end();
6838       Field1 != Field1End; ++Field1) {
6839    llvm::SmallPtrSet<FieldDecl *, 8>::iterator
6840        I = UnmatchedFields.begin(),
6841        E = UnmatchedFields.end();
6842
6843    for ( ; I != E; ++I) {
6844      if (isLayoutCompatible(C, *Field1, *I)) {
6845        bool Result = UnmatchedFields.erase(*I);
6846        (void) Result;
6847        assert(Result);
6848        break;
6849      }
6850    }
6851    if (I == E)
6852      return false;
6853  }
6854
6855  return UnmatchedFields.empty();
6856}
6857
6858bool isLayoutCompatible(ASTContext &C, RecordDecl *RD1, RecordDecl *RD2) {
6859  if (RD1->isUnion() != RD2->isUnion())
6860    return false;
6861
6862  if (RD1->isUnion())
6863    return isLayoutCompatibleUnion(C, RD1, RD2);
6864  else
6865    return isLayoutCompatibleStruct(C, RD1, RD2);
6866}
6867
6868/// \brief Check if two types are layout-compatible in C++11 sense.
6869bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2) {
6870  if (T1.isNull() || T2.isNull())
6871    return false;
6872
6873  // C++11 [basic.types] p11:
6874  // If two types T1 and T2 are the same type, then T1 and T2 are
6875  // layout-compatible types.
6876  if (C.hasSameType(T1, T2))
6877    return true;
6878
6879  T1 = T1.getCanonicalType().getUnqualifiedType();
6880  T2 = T2.getCanonicalType().getUnqualifiedType();
6881
6882  const Type::TypeClass TC1 = T1->getTypeClass();
6883  const Type::TypeClass TC2 = T2->getTypeClass();
6884
6885  if (TC1 != TC2)
6886    return false;
6887
6888  if (TC1 == Type::Enum) {
6889    return isLayoutCompatible(C,
6890                              cast<EnumType>(T1)->getDecl(),
6891                              cast<EnumType>(T2)->getDecl());
6892  } else if (TC1 == Type::Record) {
6893    if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType())
6894      return false;
6895
6896    return isLayoutCompatible(C,
6897                              cast<RecordType>(T1)->getDecl(),
6898                              cast<RecordType>(T2)->getDecl());
6899  }
6900
6901  return false;
6902}
6903}
6904
6905//===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----//
6906
6907namespace {
6908/// \brief Given a type tag expression find the type tag itself.
6909///
6910/// \param TypeExpr Type tag expression, as it appears in user's code.
6911///
6912/// \param VD Declaration of an identifier that appears in a type tag.
6913///
6914/// \param MagicValue Type tag magic value.
6915bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx,
6916                     const ValueDecl **VD, uint64_t *MagicValue) {
6917  while(true) {
6918    if (!TypeExpr)
6919      return false;
6920
6921    TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts();
6922
6923    switch (TypeExpr->getStmtClass()) {
6924    case Stmt::UnaryOperatorClass: {
6925      const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr);
6926      if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) {
6927        TypeExpr = UO->getSubExpr();
6928        continue;
6929      }
6930      return false;
6931    }
6932
6933    case Stmt::DeclRefExprClass: {
6934      const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr);
6935      *VD = DRE->getDecl();
6936      return true;
6937    }
6938
6939    case Stmt::IntegerLiteralClass: {
6940      const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr);
6941      llvm::APInt MagicValueAPInt = IL->getValue();
6942      if (MagicValueAPInt.getActiveBits() <= 64) {
6943        *MagicValue = MagicValueAPInt.getZExtValue();
6944        return true;
6945      } else
6946        return false;
6947    }
6948
6949    case Stmt::BinaryConditionalOperatorClass:
6950    case Stmt::ConditionalOperatorClass: {
6951      const AbstractConditionalOperator *ACO =
6952          cast<AbstractConditionalOperator>(TypeExpr);
6953      bool Result;
6954      if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx)) {
6955        if (Result)
6956          TypeExpr = ACO->getTrueExpr();
6957        else
6958          TypeExpr = ACO->getFalseExpr();
6959        continue;
6960      }
6961      return false;
6962    }
6963
6964    case Stmt::BinaryOperatorClass: {
6965      const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr);
6966      if (BO->getOpcode() == BO_Comma) {
6967        TypeExpr = BO->getRHS();
6968        continue;
6969      }
6970      return false;
6971    }
6972
6973    default:
6974      return false;
6975    }
6976  }
6977}
6978
6979/// \brief Retrieve the C type corresponding to type tag TypeExpr.
6980///
6981/// \param TypeExpr Expression that specifies a type tag.
6982///
6983/// \param MagicValues Registered magic values.
6984///
6985/// \param FoundWrongKind Set to true if a type tag was found, but of a wrong
6986///        kind.
6987///
6988/// \param TypeInfo Information about the corresponding C type.
6989///
6990/// \returns true if the corresponding C type was found.
6991bool GetMatchingCType(
6992        const IdentifierInfo *ArgumentKind,
6993        const Expr *TypeExpr, const ASTContext &Ctx,
6994        const llvm::DenseMap<Sema::TypeTagMagicValue,
6995                             Sema::TypeTagData> *MagicValues,
6996        bool &FoundWrongKind,
6997        Sema::TypeTagData &TypeInfo) {
6998  FoundWrongKind = false;
6999
7000  // Variable declaration that has type_tag_for_datatype attribute.
7001  const ValueDecl *VD = NULL;
7002
7003  uint64_t MagicValue;
7004
7005  if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue))
7006    return false;
7007
7008  if (VD) {
7009    for (specific_attr_iterator<TypeTagForDatatypeAttr>
7010             I = VD->specific_attr_begin<TypeTagForDatatypeAttr>(),
7011             E = VD->specific_attr_end<TypeTagForDatatypeAttr>();
7012         I != E; ++I) {
7013      if (I->getArgumentKind() != ArgumentKind) {
7014        FoundWrongKind = true;
7015        return false;
7016      }
7017      TypeInfo.Type = I->getMatchingCType();
7018      TypeInfo.LayoutCompatible = I->getLayoutCompatible();
7019      TypeInfo.MustBeNull = I->getMustBeNull();
7020      return true;
7021    }
7022    return false;
7023  }
7024
7025  if (!MagicValues)
7026    return false;
7027
7028  llvm::DenseMap<Sema::TypeTagMagicValue,
7029                 Sema::TypeTagData>::const_iterator I =
7030      MagicValues->find(std::make_pair(ArgumentKind, MagicValue));
7031  if (I == MagicValues->end())
7032    return false;
7033
7034  TypeInfo = I->second;
7035  return true;
7036}
7037} // unnamed namespace
7038
7039void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind,
7040                                      uint64_t MagicValue, QualType Type,
7041                                      bool LayoutCompatible,
7042                                      bool MustBeNull) {
7043  if (!TypeTagForDatatypeMagicValues)
7044    TypeTagForDatatypeMagicValues.reset(
7045        new llvm::DenseMap<TypeTagMagicValue, TypeTagData>);
7046
7047  TypeTagMagicValue Magic(ArgumentKind, MagicValue);
7048  (*TypeTagForDatatypeMagicValues)[Magic] =
7049      TypeTagData(Type, LayoutCompatible, MustBeNull);
7050}
7051
7052namespace {
7053bool IsSameCharType(QualType T1, QualType T2) {
7054  const BuiltinType *BT1 = T1->getAs<BuiltinType>();
7055  if (!BT1)
7056    return false;
7057
7058  const BuiltinType *BT2 = T2->getAs<BuiltinType>();
7059  if (!BT2)
7060    return false;
7061
7062  BuiltinType::Kind T1Kind = BT1->getKind();
7063  BuiltinType::Kind T2Kind = BT2->getKind();
7064
7065  return (T1Kind == BuiltinType::SChar  && T2Kind == BuiltinType::Char_S) ||
7066         (T1Kind == BuiltinType::UChar  && T2Kind == BuiltinType::Char_U) ||
7067         (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) ||
7068         (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar);
7069}
7070} // unnamed namespace
7071
7072void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr,
7073                                    const Expr * const *ExprArgs) {
7074  const IdentifierInfo *ArgumentKind = Attr->getArgumentKind();
7075  bool IsPointerAttr = Attr->getIsPointer();
7076
7077  const Expr *TypeTagExpr = ExprArgs[Attr->getTypeTagIdx()];
7078  bool FoundWrongKind;
7079  TypeTagData TypeInfo;
7080  if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context,
7081                        TypeTagForDatatypeMagicValues.get(),
7082                        FoundWrongKind, TypeInfo)) {
7083    if (FoundWrongKind)
7084      Diag(TypeTagExpr->getExprLoc(),
7085           diag::warn_type_tag_for_datatype_wrong_kind)
7086        << TypeTagExpr->getSourceRange();
7087    return;
7088  }
7089
7090  const Expr *ArgumentExpr = ExprArgs[Attr->getArgumentIdx()];
7091  if (IsPointerAttr) {
7092    // Skip implicit cast of pointer to `void *' (as a function argument).
7093    if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr))
7094      if (ICE->getType()->isVoidPointerType() &&
7095          ICE->getCastKind() == CK_BitCast)
7096        ArgumentExpr = ICE->getSubExpr();
7097  }
7098  QualType ArgumentType = ArgumentExpr->getType();
7099
7100  // Passing a `void*' pointer shouldn't trigger a warning.
7101  if (IsPointerAttr && ArgumentType->isVoidPointerType())
7102    return;
7103
7104  if (TypeInfo.MustBeNull) {
7105    // Type tag with matching void type requires a null pointer.
7106    if (!ArgumentExpr->isNullPointerConstant(Context,
7107                                             Expr::NPC_ValueDependentIsNotNull)) {
7108      Diag(ArgumentExpr->getExprLoc(),
7109           diag::warn_type_safety_null_pointer_required)
7110          << ArgumentKind->getName()
7111          << ArgumentExpr->getSourceRange()
7112          << TypeTagExpr->getSourceRange();
7113    }
7114    return;
7115  }
7116
7117  QualType RequiredType = TypeInfo.Type;
7118  if (IsPointerAttr)
7119    RequiredType = Context.getPointerType(RequiredType);
7120
7121  bool mismatch = false;
7122  if (!TypeInfo.LayoutCompatible) {
7123    mismatch = !Context.hasSameType(ArgumentType, RequiredType);
7124
7125    // C++11 [basic.fundamental] p1:
7126    // Plain char, signed char, and unsigned char are three distinct types.
7127    //
7128    // But we treat plain `char' as equivalent to `signed char' or `unsigned
7129    // char' depending on the current char signedness mode.
7130    if (mismatch)
7131      if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(),
7132                                           RequiredType->getPointeeType())) ||
7133          (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType)))
7134        mismatch = false;
7135  } else
7136    if (IsPointerAttr)
7137      mismatch = !isLayoutCompatible(Context,
7138                                     ArgumentType->getPointeeType(),
7139                                     RequiredType->getPointeeType());
7140    else
7141      mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType);
7142
7143  if (mismatch)
7144    Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch)
7145        << ArgumentType << ArgumentKind->getName()
7146        << TypeInfo.LayoutCompatible << RequiredType
7147        << ArgumentExpr->getSourceRange()
7148        << TypeTagExpr->getSourceRange();
7149}
7150