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/Sema.h"
16 #include "clang/Sema/SemaInternal.h"
17 #include "clang/Sema/ScopeInfo.h"
18 #include "clang/Analysis/Analyses/FormatString.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/CharUnits.h"
21 #include "clang/AST/DeclCXX.h"
22 #include "clang/AST/DeclObjC.h"
23 #include "clang/AST/ExprCXX.h"
24 #include "clang/AST/ExprObjC.h"
25 #include "clang/AST/DeclObjC.h"
26 #include "clang/AST/StmtCXX.h"
27 #include "clang/AST/StmtObjC.h"
28 #include "clang/Lex/LiteralSupport.h"
29 #include "clang/Lex/Preprocessor.h"
30 #include "llvm/ADT/BitVector.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "clang/Basic/TargetBuiltins.h"
34 #include "clang/Basic/TargetInfo.h"
35 #include <limits>
36 using namespace clang;
37 using namespace sema;
38 
39 /// getLocationOfStringLiteralByte - Return a source location that points to the
40 /// specified byte of the specified string literal.
41 ///
42 /// Strings are amazingly complex.  They can be formed from multiple tokens and
43 /// can have escape sequences in them in addition to the usual trigraph and
44 /// escaped newline business.  This routine handles this complexity.
45 ///
46 SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL,
47                                                     unsigned ByteNo) const {
48   assert(!SL->isWide() && "This doesn't work for wide strings yet");
49 
50   // Loop over all of the tokens in this string until we find the one that
51   // contains the byte we're looking for.
52   unsigned TokNo = 0;
53   while (1) {
54     assert(TokNo < SL->getNumConcatenated() && "Invalid byte number!");
55     SourceLocation StrTokLoc = SL->getStrTokenLoc(TokNo);
56 
57     // Get the spelling of the string so that we can get the data that makes up
58     // the string literal, not the identifier for the macro it is potentially
59     // expanded through.
60     SourceLocation StrTokSpellingLoc = SourceMgr.getSpellingLoc(StrTokLoc);
61 
62     // Re-lex the token to get its length and original spelling.
63     std::pair<FileID, unsigned> LocInfo =
64       SourceMgr.getDecomposedLoc(StrTokSpellingLoc);
65     bool Invalid = false;
66     llvm::StringRef Buffer = SourceMgr.getBufferData(LocInfo.first, &Invalid);
67     if (Invalid)
68       return StrTokSpellingLoc;
69 
70     const char *StrData = Buffer.data()+LocInfo.second;
71 
72     // Create a langops struct and enable trigraphs.  This is sufficient for
73     // relexing tokens.
74     LangOptions LangOpts;
75     LangOpts.Trigraphs = true;
76 
77     // Create a lexer starting at the beginning of this token.
78     Lexer TheLexer(StrTokSpellingLoc, LangOpts, Buffer.begin(), StrData,
79                    Buffer.end());
80     Token TheTok;
81     TheLexer.LexFromRawLexer(TheTok);
82 
83     // Use the StringLiteralParser to compute the length of the string in bytes.
84     StringLiteralParser SLP(&TheTok, 1, PP, /*Complain=*/false);
85     unsigned TokNumBytes = SLP.GetStringLength();
86 
87     // If the byte is in this token, return the location of the byte.
88     if (ByteNo < TokNumBytes ||
89         (ByteNo == TokNumBytes && TokNo == SL->getNumConcatenated())) {
90       unsigned Offset =
91         StringLiteralParser::getOffsetOfStringByte(TheTok, ByteNo, PP,
92                                                    /*Complain=*/false);
93 
94       // Now that we know the offset of the token in the spelling, use the
95       // preprocessor to get the offset in the original source.
96       return PP.AdvanceToTokenCharacter(StrTokLoc, Offset);
97     }
98 
99     // Move to the next string token.
100     ++TokNo;
101     ByteNo -= TokNumBytes;
102   }
103 }
104 
105 /// CheckablePrintfAttr - does a function call have a "printf" attribute
106 /// and arguments that merit checking?
107 bool Sema::CheckablePrintfAttr(const FormatAttr *Format, CallExpr *TheCall) {
108   if (Format->getType() == "printf") return true;
109   if (Format->getType() == "printf0") {
110     // printf0 allows null "format" string; if so don't check format/args
111     unsigned format_idx = Format->getFormatIdx() - 1;
112     // Does the index refer to the implicit object argument?
113     if (isa<CXXMemberCallExpr>(TheCall)) {
114       if (format_idx == 0)
115         return false;
116       --format_idx;
117     }
118     if (format_idx < TheCall->getNumArgs()) {
119       Expr *Format = TheCall->getArg(format_idx)->IgnoreParenCasts();
120       if (!Format->isNullPointerConstant(Context,
121                                          Expr::NPC_ValueDependentIsNull))
122         return true;
123     }
124   }
125   return false;
126 }
127 
128 ExprResult
129 Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
130   ExprResult TheCallResult(Owned(TheCall));
131 
132   switch (BuiltinID) {
133   case Builtin::BI__builtin___CFStringMakeConstantString:
134     assert(TheCall->getNumArgs() == 1 &&
135            "Wrong # arguments to builtin CFStringMakeConstantString");
136     if (CheckObjCString(TheCall->getArg(0)))
137       return ExprError();
138     break;
139   case Builtin::BI__builtin_stdarg_start:
140   case Builtin::BI__builtin_va_start:
141     if (SemaBuiltinVAStart(TheCall))
142       return ExprError();
143     break;
144   case Builtin::BI__builtin_isgreater:
145   case Builtin::BI__builtin_isgreaterequal:
146   case Builtin::BI__builtin_isless:
147   case Builtin::BI__builtin_islessequal:
148   case Builtin::BI__builtin_islessgreater:
149   case Builtin::BI__builtin_isunordered:
150     if (SemaBuiltinUnorderedCompare(TheCall))
151       return ExprError();
152     break;
153   case Builtin::BI__builtin_fpclassify:
154     if (SemaBuiltinFPClassification(TheCall, 6))
155       return ExprError();
156     break;
157   case Builtin::BI__builtin_isfinite:
158   case Builtin::BI__builtin_isinf:
159   case Builtin::BI__builtin_isinf_sign:
160   case Builtin::BI__builtin_isnan:
161   case Builtin::BI__builtin_isnormal:
162     if (SemaBuiltinFPClassification(TheCall, 1))
163       return ExprError();
164     break;
165   case Builtin::BI__builtin_return_address:
166   case Builtin::BI__builtin_frame_address: {
167     llvm::APSInt Result;
168     if (SemaBuiltinConstantArg(TheCall, 0, Result))
169       return ExprError();
170     break;
171   }
172   case Builtin::BI__builtin_eh_return_data_regno: {
173     llvm::APSInt Result;
174     if (SemaBuiltinConstantArg(TheCall, 0, Result))
175       return ExprError();
176     break;
177   }
178   case Builtin::BI__builtin_shufflevector:
179     return SemaBuiltinShuffleVector(TheCall);
180     // TheCall will be freed by the smart pointer here, but that's fine, since
181     // SemaBuiltinShuffleVector guts it, but then doesn't release it.
182   case Builtin::BI__builtin_prefetch:
183     if (SemaBuiltinPrefetch(TheCall))
184       return ExprError();
185     break;
186   case Builtin::BI__builtin_object_size:
187     if (SemaBuiltinObjectSize(TheCall))
188       return ExprError();
189     break;
190   case Builtin::BI__builtin_longjmp:
191     if (SemaBuiltinLongjmp(TheCall))
192       return ExprError();
193     break;
194   case Builtin::BI__sync_fetch_and_add:
195   case Builtin::BI__sync_fetch_and_sub:
196   case Builtin::BI__sync_fetch_and_or:
197   case Builtin::BI__sync_fetch_and_and:
198   case Builtin::BI__sync_fetch_and_xor:
199   case Builtin::BI__sync_add_and_fetch:
200   case Builtin::BI__sync_sub_and_fetch:
201   case Builtin::BI__sync_and_and_fetch:
202   case Builtin::BI__sync_or_and_fetch:
203   case Builtin::BI__sync_xor_and_fetch:
204   case Builtin::BI__sync_val_compare_and_swap:
205   case Builtin::BI__sync_bool_compare_and_swap:
206   case Builtin::BI__sync_lock_test_and_set:
207   case Builtin::BI__sync_lock_release:
208     return SemaBuiltinAtomicOverloaded(move(TheCallResult));
209   }
210 
211   // Since the target specific builtins for each arch overlap, only check those
212   // of the arch we are compiling for.
213   if (BuiltinID >= Builtin::FirstTSBuiltin) {
214     switch (Context.Target.getTriple().getArch()) {
215       case llvm::Triple::arm:
216       case llvm::Triple::thumb:
217         if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall))
218           return ExprError();
219         break;
220       case llvm::Triple::x86:
221       case llvm::Triple::x86_64:
222         if (CheckX86BuiltinFunctionCall(BuiltinID, TheCall))
223           return ExprError();
224         break;
225       default:
226         break;
227     }
228   }
229 
230   return move(TheCallResult);
231 }
232 
233 bool Sema::CheckX86BuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
234   switch (BuiltinID) {
235   case X86::BI__builtin_ia32_palignr128:
236   case X86::BI__builtin_ia32_palignr: {
237     llvm::APSInt Result;
238     if (SemaBuiltinConstantArg(TheCall, 2, Result))
239       return true;
240     break;
241   }
242   }
243   return false;
244 }
245 
246 // Get the valid immediate range for the specified NEON type code.
247 static unsigned RFT(unsigned t, bool shift = false) {
248   bool quad = t & 0x10;
249 
250   switch (t & 0x7) {
251     case 0: // i8
252       return shift ? 7 : (8 << (int)quad) - 1;
253     case 1: // i16
254       return shift ? 15 : (4 << (int)quad) - 1;
255     case 2: // i32
256       return shift ? 31 : (2 << (int)quad) - 1;
257     case 3: // i64
258       return shift ? 63 : (1 << (int)quad) - 1;
259     case 4: // f32
260       assert(!shift && "cannot shift float types!");
261       return (2 << (int)quad) - 1;
262     case 5: // poly8
263       assert(!shift && "cannot shift polynomial types!");
264       return (8 << (int)quad) - 1;
265     case 6: // poly16
266       assert(!shift && "cannot shift polynomial types!");
267       return (4 << (int)quad) - 1;
268     case 7: // float16
269       assert(!shift && "cannot shift float types!");
270       return (4 << (int)quad) - 1;
271   }
272   return 0;
273 }
274 
275 bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
276   llvm::APSInt Result;
277 
278   unsigned mask = 0;
279   unsigned TV = 0;
280   switch (BuiltinID) {
281 #define GET_NEON_OVERLOAD_CHECK
282 #include "clang/Basic/arm_neon.inc"
283 #undef GET_NEON_OVERLOAD_CHECK
284   }
285 
286   // For NEON intrinsics which are overloaded on vector element type, validate
287   // the immediate which specifies which variant to emit.
288   if (mask) {
289     unsigned ArgNo = TheCall->getNumArgs()-1;
290     if (SemaBuiltinConstantArg(TheCall, ArgNo, Result))
291       return true;
292 
293     TV = Result.getLimitedValue(32);
294     if ((TV > 31) || (mask & (1 << TV)) == 0)
295       return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code)
296         << TheCall->getArg(ArgNo)->getSourceRange();
297   }
298 
299   // For NEON intrinsics which take an immediate value as part of the
300   // instruction, range check them here.
301   unsigned i = 0, l = 0, u = 0;
302   switch (BuiltinID) {
303   default: return false;
304   case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break;
305   case ARM::BI__builtin_arm_usat: i = 1; u = 31; break;
306   case ARM::BI__builtin_arm_vcvtr_f:
307   case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break;
308 #define GET_NEON_IMMEDIATE_CHECK
309 #include "clang/Basic/arm_neon.inc"
310 #undef GET_NEON_IMMEDIATE_CHECK
311   };
312 
313   // Check that the immediate argument is actually a constant.
314   if (SemaBuiltinConstantArg(TheCall, i, Result))
315     return true;
316 
317   // Range check against the upper/lower values for this isntruction.
318   unsigned Val = Result.getZExtValue();
319   if (Val < l || Val > (u + l))
320     return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
321       << l << u+l << TheCall->getArg(i)->getSourceRange();
322 
323   // FIXME: VFP Intrinsics should error if VFP not present.
324   return false;
325 }
326 
327 /// CheckFunctionCall - Check a direct function call for various correctness
328 /// and safety properties not strictly enforced by the C type system.
329 bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall) {
330   // Get the IdentifierInfo* for the called function.
331   IdentifierInfo *FnInfo = FDecl->getIdentifier();
332 
333   // None of the checks below are needed for functions that don't have
334   // simple names (e.g., C++ conversion functions).
335   if (!FnInfo)
336     return false;
337 
338   // FIXME: This mechanism should be abstracted to be less fragile and
339   // more efficient. For example, just map function ids to custom
340   // handlers.
341 
342   // Printf checking.
343   if (const FormatAttr *Format = FDecl->getAttr<FormatAttr>()) {
344     const bool b = Format->getType() == "scanf";
345     if (b || CheckablePrintfAttr(Format, TheCall)) {
346       bool HasVAListArg = Format->getFirstArg() == 0;
347       CheckPrintfScanfArguments(TheCall, HasVAListArg,
348                                 Format->getFormatIdx() - 1,
349                                 HasVAListArg ? 0 : Format->getFirstArg() - 1,
350                                 !b);
351     }
352   }
353 
354   specific_attr_iterator<NonNullAttr>
355     i = FDecl->specific_attr_begin<NonNullAttr>(),
356     e = FDecl->specific_attr_end<NonNullAttr>();
357 
358   for (; i != e; ++i)
359     CheckNonNullArguments(*i, TheCall);
360 
361   return false;
362 }
363 
364 bool Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall) {
365   // Printf checking.
366   const FormatAttr *Format = NDecl->getAttr<FormatAttr>();
367   if (!Format)
368     return false;
369 
370   const VarDecl *V = dyn_cast<VarDecl>(NDecl);
371   if (!V)
372     return false;
373 
374   QualType Ty = V->getType();
375   if (!Ty->isBlockPointerType())
376     return false;
377 
378   const bool b = Format->getType() == "scanf";
379   if (!b && !CheckablePrintfAttr(Format, TheCall))
380     return false;
381 
382   bool HasVAListArg = Format->getFirstArg() == 0;
383   CheckPrintfScanfArguments(TheCall, HasVAListArg, Format->getFormatIdx() - 1,
384                             HasVAListArg ? 0 : Format->getFirstArg() - 1, !b);
385 
386   return false;
387 }
388 
389 /// SemaBuiltinAtomicOverloaded - We have a call to a function like
390 /// __sync_fetch_and_add, which is an overloaded function based on the pointer
391 /// type of its first argument.  The main ActOnCallExpr routines have already
392 /// promoted the types of arguments because all of these calls are prototyped as
393 /// void(...).
394 ///
395 /// This function goes through and does final semantic checking for these
396 /// builtins,
397 ExprResult
398 Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) {
399   CallExpr *TheCall = (CallExpr *)TheCallResult.get();
400   DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
401   FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
402 
403   // Ensure that we have at least one argument to do type inference from.
404   if (TheCall->getNumArgs() < 1) {
405     Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
406       << 0 << 1 << TheCall->getNumArgs()
407       << TheCall->getCallee()->getSourceRange();
408     return ExprError();
409   }
410 
411   // Inspect the first argument of the atomic builtin.  This should always be
412   // a pointer type, whose element is an integral scalar or pointer type.
413   // Because it is a pointer type, we don't have to worry about any implicit
414   // casts here.
415   // FIXME: We don't allow floating point scalars as input.
416   Expr *FirstArg = TheCall->getArg(0);
417   if (!FirstArg->getType()->isPointerType()) {
418     Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
419       << FirstArg->getType() << FirstArg->getSourceRange();
420     return ExprError();
421   }
422 
423   QualType ValType =
424     FirstArg->getType()->getAs<PointerType>()->getPointeeType();
425   if (!ValType->isIntegerType() && !ValType->isPointerType() &&
426       !ValType->isBlockPointerType()) {
427     Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr)
428       << FirstArg->getType() << FirstArg->getSourceRange();
429     return ExprError();
430   }
431 
432   // The majority of builtins return a value, but a few have special return
433   // types, so allow them to override appropriately below.
434   QualType ResultType = ValType;
435 
436   // We need to figure out which concrete builtin this maps onto.  For example,
437   // __sync_fetch_and_add with a 2 byte object turns into
438   // __sync_fetch_and_add_2.
439 #define BUILTIN_ROW(x) \
440   { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \
441     Builtin::BI##x##_8, Builtin::BI##x##_16 }
442 
443   static const unsigned BuiltinIndices[][5] = {
444     BUILTIN_ROW(__sync_fetch_and_add),
445     BUILTIN_ROW(__sync_fetch_and_sub),
446     BUILTIN_ROW(__sync_fetch_and_or),
447     BUILTIN_ROW(__sync_fetch_and_and),
448     BUILTIN_ROW(__sync_fetch_and_xor),
449 
450     BUILTIN_ROW(__sync_add_and_fetch),
451     BUILTIN_ROW(__sync_sub_and_fetch),
452     BUILTIN_ROW(__sync_and_and_fetch),
453     BUILTIN_ROW(__sync_or_and_fetch),
454     BUILTIN_ROW(__sync_xor_and_fetch),
455 
456     BUILTIN_ROW(__sync_val_compare_and_swap),
457     BUILTIN_ROW(__sync_bool_compare_and_swap),
458     BUILTIN_ROW(__sync_lock_test_and_set),
459     BUILTIN_ROW(__sync_lock_release)
460   };
461 #undef BUILTIN_ROW
462 
463   // Determine the index of the size.
464   unsigned SizeIndex;
465   switch (Context.getTypeSizeInChars(ValType).getQuantity()) {
466   case 1: SizeIndex = 0; break;
467   case 2: SizeIndex = 1; break;
468   case 4: SizeIndex = 2; break;
469   case 8: SizeIndex = 3; break;
470   case 16: SizeIndex = 4; break;
471   default:
472     Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size)
473       << FirstArg->getType() << FirstArg->getSourceRange();
474     return ExprError();
475   }
476 
477   // Each of these builtins has one pointer argument, followed by some number of
478   // values (0, 1 or 2) followed by a potentially empty varags list of stuff
479   // that we ignore.  Find out which row of BuiltinIndices to read from as well
480   // as the number of fixed args.
481   unsigned BuiltinID = FDecl->getBuiltinID();
482   unsigned BuiltinIndex, NumFixed = 1;
483   switch (BuiltinID) {
484   default: assert(0 && "Unknown overloaded atomic builtin!");
485   case Builtin::BI__sync_fetch_and_add: BuiltinIndex = 0; break;
486   case Builtin::BI__sync_fetch_and_sub: BuiltinIndex = 1; break;
487   case Builtin::BI__sync_fetch_and_or:  BuiltinIndex = 2; break;
488   case Builtin::BI__sync_fetch_and_and: BuiltinIndex = 3; break;
489   case Builtin::BI__sync_fetch_and_xor: BuiltinIndex = 4; break;
490 
491   case Builtin::BI__sync_add_and_fetch: BuiltinIndex = 5; break;
492   case Builtin::BI__sync_sub_and_fetch: BuiltinIndex = 6; break;
493   case Builtin::BI__sync_and_and_fetch: BuiltinIndex = 7; break;
494   case Builtin::BI__sync_or_and_fetch:  BuiltinIndex = 8; break;
495   case Builtin::BI__sync_xor_and_fetch: BuiltinIndex = 9; break;
496 
497   case Builtin::BI__sync_val_compare_and_swap:
498     BuiltinIndex = 10;
499     NumFixed = 2;
500     break;
501   case Builtin::BI__sync_bool_compare_and_swap:
502     BuiltinIndex = 11;
503     NumFixed = 2;
504     ResultType = Context.BoolTy;
505     break;
506   case Builtin::BI__sync_lock_test_and_set: BuiltinIndex = 12; break;
507   case Builtin::BI__sync_lock_release:
508     BuiltinIndex = 13;
509     NumFixed = 0;
510     ResultType = Context.VoidTy;
511     break;
512   }
513 
514   // Now that we know how many fixed arguments we expect, first check that we
515   // have at least that many.
516   if (TheCall->getNumArgs() < 1+NumFixed) {
517     Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
518       << 0 << 1+NumFixed << TheCall->getNumArgs()
519       << TheCall->getCallee()->getSourceRange();
520     return ExprError();
521   }
522 
523   // Get the decl for the concrete builtin from this, we can tell what the
524   // concrete integer type we should convert to is.
525   unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex];
526   const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID);
527   IdentifierInfo *NewBuiltinII = PP.getIdentifierInfo(NewBuiltinName);
528   FunctionDecl *NewBuiltinDecl =
529     cast<FunctionDecl>(LazilyCreateBuiltin(NewBuiltinII, NewBuiltinID,
530                                            TUScope, false, DRE->getLocStart()));
531 
532   // The first argument --- the pointer --- has a fixed type; we
533   // deduce the types of the rest of the arguments accordingly.  Walk
534   // the remaining arguments, converting them to the deduced value type.
535   for (unsigned i = 0; i != NumFixed; ++i) {
536     Expr *Arg = TheCall->getArg(i+1);
537 
538     // If the argument is an implicit cast, then there was a promotion due to
539     // "...", just remove it now.
540     if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
541       Arg = ICE->getSubExpr();
542       ICE->setSubExpr(0);
543       TheCall->setArg(i+1, Arg);
544     }
545 
546     // GCC does an implicit conversion to the pointer or integer ValType.  This
547     // can fail in some cases (1i -> int**), check for this error case now.
548     CastKind Kind = CK_Unknown;
549     CXXCastPath BasePath;
550     if (CheckCastTypes(Arg->getSourceRange(), ValType, Arg, Kind, BasePath))
551       return ExprError();
552 
553     // Okay, we have something that *can* be converted to the right type.  Check
554     // to see if there is a potentially weird extension going on here.  This can
555     // happen when you do an atomic operation on something like an char* and
556     // pass in 42.  The 42 gets converted to char.  This is even more strange
557     // for things like 45.123 -> char, etc.
558     // FIXME: Do this check.
559     ImpCastExprToType(Arg, ValType, Kind, VK_RValue, &BasePath);
560     TheCall->setArg(i+1, Arg);
561   }
562 
563   // Switch the DeclRefExpr to refer to the new decl.
564   DRE->setDecl(NewBuiltinDecl);
565   DRE->setType(NewBuiltinDecl->getType());
566 
567   // Set the callee in the CallExpr.
568   // FIXME: This leaks the original parens and implicit casts.
569   Expr *PromotedCall = DRE;
570   UsualUnaryConversions(PromotedCall);
571   TheCall->setCallee(PromotedCall);
572 
573   // Change the result type of the call to match the original value type. This
574   // is arbitrary, but the codegen for these builtins ins design to handle it
575   // gracefully.
576   TheCall->setType(ResultType);
577 
578   return move(TheCallResult);
579 }
580 
581 
582 /// CheckObjCString - Checks that the argument to the builtin
583 /// CFString constructor is correct
584 /// FIXME: GCC currently emits the following warning:
585 /// "warning: input conversion stopped due to an input byte that does not
586 ///           belong to the input codeset UTF-8"
587 /// Note: It might also make sense to do the UTF-16 conversion here (would
588 /// simplify the backend).
589 bool Sema::CheckObjCString(Expr *Arg) {
590   Arg = Arg->IgnoreParenCasts();
591   StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
592 
593   if (!Literal || Literal->isWide()) {
594     Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant)
595       << Arg->getSourceRange();
596     return true;
597   }
598 
599   size_t NulPos = Literal->getString().find('\0');
600   if (NulPos != llvm::StringRef::npos) {
601     Diag(getLocationOfStringLiteralByte(Literal, NulPos),
602          diag::warn_cfstring_literal_contains_nul_character)
603       << Arg->getSourceRange();
604   }
605 
606   return false;
607 }
608 
609 /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity.
610 /// Emit an error and return true on failure, return false on success.
611 bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) {
612   Expr *Fn = TheCall->getCallee();
613   if (TheCall->getNumArgs() > 2) {
614     Diag(TheCall->getArg(2)->getLocStart(),
615          diag::err_typecheck_call_too_many_args)
616       << 0 /*function call*/ << 2 << TheCall->getNumArgs()
617       << Fn->getSourceRange()
618       << SourceRange(TheCall->getArg(2)->getLocStart(),
619                      (*(TheCall->arg_end()-1))->getLocEnd());
620     return true;
621   }
622 
623   if (TheCall->getNumArgs() < 2) {
624     return Diag(TheCall->getLocEnd(),
625       diag::err_typecheck_call_too_few_args_at_least)
626       << 0 /*function call*/ << 2 << TheCall->getNumArgs();
627   }
628 
629   // Determine whether the current function is variadic or not.
630   BlockScopeInfo *CurBlock = getCurBlock();
631   bool isVariadic;
632   if (CurBlock)
633     isVariadic = CurBlock->TheDecl->isVariadic();
634   else if (FunctionDecl *FD = getCurFunctionDecl())
635     isVariadic = FD->isVariadic();
636   else
637     isVariadic = getCurMethodDecl()->isVariadic();
638 
639   if (!isVariadic) {
640     Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function);
641     return true;
642   }
643 
644   // Verify that the second argument to the builtin is the last argument of the
645   // current function or method.
646   bool SecondArgIsLastNamedArgument = false;
647   const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts();
648 
649   if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) {
650     if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
651       // FIXME: This isn't correct for methods (results in bogus warning).
652       // Get the last formal in the current function.
653       const ParmVarDecl *LastArg;
654       if (CurBlock)
655         LastArg = *(CurBlock->TheDecl->param_end()-1);
656       else if (FunctionDecl *FD = getCurFunctionDecl())
657         LastArg = *(FD->param_end()-1);
658       else
659         LastArg = *(getCurMethodDecl()->param_end()-1);
660       SecondArgIsLastNamedArgument = PV == LastArg;
661     }
662   }
663 
664   if (!SecondArgIsLastNamedArgument)
665     Diag(TheCall->getArg(1)->getLocStart(),
666          diag::warn_second_parameter_of_va_start_not_last_named_argument);
667   return false;
668 }
669 
670 /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and
671 /// friends.  This is declared to take (...), so we have to check everything.
672 bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) {
673   if (TheCall->getNumArgs() < 2)
674     return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
675       << 0 << 2 << TheCall->getNumArgs()/*function call*/;
676   if (TheCall->getNumArgs() > 2)
677     return Diag(TheCall->getArg(2)->getLocStart(),
678                 diag::err_typecheck_call_too_many_args)
679       << 0 /*function call*/ << 2 << TheCall->getNumArgs()
680       << SourceRange(TheCall->getArg(2)->getLocStart(),
681                      (*(TheCall->arg_end()-1))->getLocEnd());
682 
683   Expr *OrigArg0 = TheCall->getArg(0);
684   Expr *OrigArg1 = TheCall->getArg(1);
685 
686   // Do standard promotions between the two arguments, returning their common
687   // type.
688   QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false);
689 
690   // Make sure any conversions are pushed back into the call; this is
691   // type safe since unordered compare builtins are declared as "_Bool
692   // foo(...)".
693   TheCall->setArg(0, OrigArg0);
694   TheCall->setArg(1, OrigArg1);
695 
696   if (OrigArg0->isTypeDependent() || OrigArg1->isTypeDependent())
697     return false;
698 
699   // If the common type isn't a real floating type, then the arguments were
700   // invalid for this operation.
701   if (!Res->isRealFloatingType())
702     return Diag(OrigArg0->getLocStart(),
703                 diag::err_typecheck_call_invalid_ordered_compare)
704       << OrigArg0->getType() << OrigArg1->getType()
705       << SourceRange(OrigArg0->getLocStart(), OrigArg1->getLocEnd());
706 
707   return false;
708 }
709 
710 /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like
711 /// __builtin_isnan and friends.  This is declared to take (...), so we have
712 /// to check everything. We expect the last argument to be a floating point
713 /// value.
714 bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) {
715   if (TheCall->getNumArgs() < NumArgs)
716     return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
717       << 0 << NumArgs << TheCall->getNumArgs()/*function call*/;
718   if (TheCall->getNumArgs() > NumArgs)
719     return Diag(TheCall->getArg(NumArgs)->getLocStart(),
720                 diag::err_typecheck_call_too_many_args)
721       << 0 /*function call*/ << NumArgs << TheCall->getNumArgs()
722       << SourceRange(TheCall->getArg(NumArgs)->getLocStart(),
723                      (*(TheCall->arg_end()-1))->getLocEnd());
724 
725   Expr *OrigArg = TheCall->getArg(NumArgs-1);
726 
727   if (OrigArg->isTypeDependent())
728     return false;
729 
730   // This operation requires a non-_Complex floating-point number.
731   if (!OrigArg->getType()->isRealFloatingType())
732     return Diag(OrigArg->getLocStart(),
733                 diag::err_typecheck_call_invalid_unary_fp)
734       << OrigArg->getType() << OrigArg->getSourceRange();
735 
736   // If this is an implicit conversion from float -> double, remove it.
737   if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) {
738     Expr *CastArg = Cast->getSubExpr();
739     if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) {
740       assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) &&
741              "promotion from float to double is the only expected cast here");
742       Cast->setSubExpr(0);
743       TheCall->setArg(NumArgs-1, CastArg);
744       OrigArg = CastArg;
745     }
746   }
747 
748   return false;
749 }
750 
751 /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector.
752 // This is declared to take (...), so we have to check everything.
753 ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) {
754   if (TheCall->getNumArgs() < 2)
755     return ExprError(Diag(TheCall->getLocEnd(),
756                           diag::err_typecheck_call_too_few_args_at_least)
757       << 0 /*function call*/ << 2 << TheCall->getNumArgs()
758       << TheCall->getSourceRange());
759 
760   // Determine which of the following types of shufflevector we're checking:
761   // 1) unary, vector mask: (lhs, mask)
762   // 2) binary, vector mask: (lhs, rhs, mask)
763   // 3) binary, scalar mask: (lhs, rhs, index, ..., index)
764   QualType resType = TheCall->getArg(0)->getType();
765   unsigned numElements = 0;
766 
767   if (!TheCall->getArg(0)->isTypeDependent() &&
768       !TheCall->getArg(1)->isTypeDependent()) {
769     QualType LHSType = TheCall->getArg(0)->getType();
770     QualType RHSType = TheCall->getArg(1)->getType();
771 
772     if (!LHSType->isVectorType() || !RHSType->isVectorType()) {
773       Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector)
774         << SourceRange(TheCall->getArg(0)->getLocStart(),
775                        TheCall->getArg(1)->getLocEnd());
776       return ExprError();
777     }
778 
779     numElements = LHSType->getAs<VectorType>()->getNumElements();
780     unsigned numResElements = TheCall->getNumArgs() - 2;
781 
782     // Check to see if we have a call with 2 vector arguments, the unary shuffle
783     // with mask.  If so, verify that RHS is an integer vector type with the
784     // same number of elts as lhs.
785     if (TheCall->getNumArgs() == 2) {
786       if (!RHSType->hasIntegerRepresentation() ||
787           RHSType->getAs<VectorType>()->getNumElements() != numElements)
788         Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector)
789           << SourceRange(TheCall->getArg(1)->getLocStart(),
790                          TheCall->getArg(1)->getLocEnd());
791       numResElements = numElements;
792     }
793     else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) {
794       Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector)
795         << SourceRange(TheCall->getArg(0)->getLocStart(),
796                        TheCall->getArg(1)->getLocEnd());
797       return ExprError();
798     } else if (numElements != numResElements) {
799       QualType eltType = LHSType->getAs<VectorType>()->getElementType();
800       resType = Context.getVectorType(eltType, numResElements,
801                                       VectorType::NotAltiVec);
802     }
803   }
804 
805   for (unsigned i = 2; i < TheCall->getNumArgs(); i++) {
806     if (TheCall->getArg(i)->isTypeDependent() ||
807         TheCall->getArg(i)->isValueDependent())
808       continue;
809 
810     llvm::APSInt Result(32);
811     if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context))
812       return ExprError(Diag(TheCall->getLocStart(),
813                   diag::err_shufflevector_nonconstant_argument)
814                 << TheCall->getArg(i)->getSourceRange());
815 
816     if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2)
817       return ExprError(Diag(TheCall->getLocStart(),
818                   diag::err_shufflevector_argument_too_large)
819                << TheCall->getArg(i)->getSourceRange());
820   }
821 
822   llvm::SmallVector<Expr*, 32> exprs;
823 
824   for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) {
825     exprs.push_back(TheCall->getArg(i));
826     TheCall->setArg(i, 0);
827   }
828 
829   return Owned(new (Context) ShuffleVectorExpr(Context, exprs.begin(),
830                                             exprs.size(), resType,
831                                             TheCall->getCallee()->getLocStart(),
832                                             TheCall->getRParenLoc()));
833 }
834 
835 /// SemaBuiltinPrefetch - Handle __builtin_prefetch.
836 // This is declared to take (const void*, ...) and can take two
837 // optional constant int args.
838 bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) {
839   unsigned NumArgs = TheCall->getNumArgs();
840 
841   if (NumArgs > 3)
842     return Diag(TheCall->getLocEnd(),
843              diag::err_typecheck_call_too_many_args_at_most)
844              << 0 /*function call*/ << 3 << NumArgs
845              << TheCall->getSourceRange();
846 
847   // Argument 0 is checked for us and the remaining arguments must be
848   // constant integers.
849   for (unsigned i = 1; i != NumArgs; ++i) {
850     Expr *Arg = TheCall->getArg(i);
851 
852     llvm::APSInt Result;
853     if (SemaBuiltinConstantArg(TheCall, i, Result))
854       return true;
855 
856     // FIXME: gcc issues a warning and rewrites these to 0. These
857     // seems especially odd for the third argument since the default
858     // is 3.
859     if (i == 1) {
860       if (Result.getLimitedValue() > 1)
861         return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
862              << "0" << "1" << Arg->getSourceRange();
863     } else {
864       if (Result.getLimitedValue() > 3)
865         return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
866             << "0" << "3" << Arg->getSourceRange();
867     }
868   }
869 
870   return false;
871 }
872 
873 /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr
874 /// TheCall is a constant expression.
875 bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum,
876                                   llvm::APSInt &Result) {
877   Expr *Arg = TheCall->getArg(ArgNum);
878   DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
879   FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
880 
881   if (Arg->isTypeDependent() || Arg->isValueDependent()) return false;
882 
883   if (!Arg->isIntegerConstantExpr(Result, Context))
884     return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type)
885                 << FDecl->getDeclName() <<  Arg->getSourceRange();
886 
887   return false;
888 }
889 
890 /// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr,
891 /// int type). This simply type checks that type is one of the defined
892 /// constants (0-3).
893 // For compatability check 0-3, llvm only handles 0 and 2.
894 bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) {
895   llvm::APSInt Result;
896 
897   // Check constant-ness first.
898   if (SemaBuiltinConstantArg(TheCall, 1, Result))
899     return true;
900 
901   Expr *Arg = TheCall->getArg(1);
902   if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) {
903     return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
904              << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
905   }
906 
907   return false;
908 }
909 
910 /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val).
911 /// This checks that val is a constant 1.
912 bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) {
913   Expr *Arg = TheCall->getArg(1);
914   llvm::APSInt Result;
915 
916   // TODO: This is less than ideal. Overload this to take a value.
917   if (SemaBuiltinConstantArg(TheCall, 1, Result))
918     return true;
919 
920   if (Result != 1)
921     return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val)
922              << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
923 
924   return false;
925 }
926 
927 // Handle i > 1 ? "x" : "y", recursivelly
928 bool Sema::SemaCheckStringLiteral(const Expr *E, const CallExpr *TheCall,
929                                   bool HasVAListArg,
930                                   unsigned format_idx, unsigned firstDataArg,
931                                   bool isPrintf) {
932 
933   if (E->isTypeDependent() || E->isValueDependent())
934     return false;
935 
936   switch (E->getStmtClass()) {
937   case Stmt::ConditionalOperatorClass: {
938     const ConditionalOperator *C = cast<ConditionalOperator>(E);
939     return SemaCheckStringLiteral(C->getTrueExpr(), TheCall, HasVAListArg,
940                                   format_idx, firstDataArg, isPrintf)
941         && SemaCheckStringLiteral(C->getRHS(), TheCall, HasVAListArg,
942                                   format_idx, firstDataArg, isPrintf);
943   }
944 
945   case Stmt::ImplicitCastExprClass: {
946     const ImplicitCastExpr *Expr = cast<ImplicitCastExpr>(E);
947     return SemaCheckStringLiteral(Expr->getSubExpr(), TheCall, HasVAListArg,
948                                   format_idx, firstDataArg, isPrintf);
949   }
950 
951   case Stmt::ParenExprClass: {
952     const ParenExpr *Expr = cast<ParenExpr>(E);
953     return SemaCheckStringLiteral(Expr->getSubExpr(), TheCall, HasVAListArg,
954                                   format_idx, firstDataArg, isPrintf);
955   }
956 
957   case Stmt::DeclRefExprClass: {
958     const DeclRefExpr *DR = cast<DeclRefExpr>(E);
959 
960     // As an exception, do not flag errors for variables binding to
961     // const string literals.
962     if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
963       bool isConstant = false;
964       QualType T = DR->getType();
965 
966       if (const ArrayType *AT = Context.getAsArrayType(T)) {
967         isConstant = AT->getElementType().isConstant(Context);
968       } else if (const PointerType *PT = T->getAs<PointerType>()) {
969         isConstant = T.isConstant(Context) &&
970                      PT->getPointeeType().isConstant(Context);
971       }
972 
973       if (isConstant) {
974         if (const Expr *Init = VD->getAnyInitializer())
975           return SemaCheckStringLiteral(Init, TheCall,
976                                         HasVAListArg, format_idx, firstDataArg,
977                                         isPrintf);
978       }
979 
980       // For vprintf* functions (i.e., HasVAListArg==true), we add a
981       // special check to see if the format string is a function parameter
982       // of the function calling the printf function.  If the function
983       // has an attribute indicating it is a printf-like function, then we
984       // should suppress warnings concerning non-literals being used in a call
985       // to a vprintf function.  For example:
986       //
987       // void
988       // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){
989       //      va_list ap;
990       //      va_start(ap, fmt);
991       //      vprintf(fmt, ap);  // Do NOT emit a warning about "fmt".
992       //      ...
993       //
994       //
995       //  FIXME: We don't have full attribute support yet, so just check to see
996       //    if the argument is a DeclRefExpr that references a parameter.  We'll
997       //    add proper support for checking the attribute later.
998       if (HasVAListArg)
999         if (isa<ParmVarDecl>(VD))
1000           return true;
1001     }
1002 
1003     return false;
1004   }
1005 
1006   case Stmt::CallExprClass: {
1007     const CallExpr *CE = cast<CallExpr>(E);
1008     if (const ImplicitCastExpr *ICE
1009           = dyn_cast<ImplicitCastExpr>(CE->getCallee())) {
1010       if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
1011         if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) {
1012           if (const FormatArgAttr *FA = FD->getAttr<FormatArgAttr>()) {
1013             unsigned ArgIndex = FA->getFormatIdx();
1014             const Expr *Arg = CE->getArg(ArgIndex - 1);
1015 
1016             return SemaCheckStringLiteral(Arg, TheCall, HasVAListArg,
1017                                           format_idx, firstDataArg, isPrintf);
1018           }
1019         }
1020       }
1021     }
1022 
1023     return false;
1024   }
1025   case Stmt::ObjCStringLiteralClass:
1026   case Stmt::StringLiteralClass: {
1027     const StringLiteral *StrE = NULL;
1028 
1029     if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E))
1030       StrE = ObjCFExpr->getString();
1031     else
1032       StrE = cast<StringLiteral>(E);
1033 
1034     if (StrE) {
1035       CheckFormatString(StrE, E, TheCall, HasVAListArg, format_idx,
1036                         firstDataArg, isPrintf);
1037       return true;
1038     }
1039 
1040     return false;
1041   }
1042 
1043   default:
1044     return false;
1045   }
1046 }
1047 
1048 void
1049 Sema::CheckNonNullArguments(const NonNullAttr *NonNull,
1050                             const CallExpr *TheCall) {
1051   for (NonNullAttr::args_iterator i = NonNull->args_begin(),
1052                                   e = NonNull->args_end();
1053        i != e; ++i) {
1054     const Expr *ArgExpr = TheCall->getArg(*i);
1055     if (ArgExpr->isNullPointerConstant(Context,
1056                                        Expr::NPC_ValueDependentIsNotNull))
1057       Diag(TheCall->getCallee()->getLocStart(), diag::warn_null_arg)
1058         << ArgExpr->getSourceRange();
1059   }
1060 }
1061 
1062 /// CheckPrintfScanfArguments - Check calls to printf and scanf (and similar
1063 /// functions) for correct use of format strings.
1064 void
1065 Sema::CheckPrintfScanfArguments(const CallExpr *TheCall, bool HasVAListArg,
1066                                 unsigned format_idx, unsigned firstDataArg,
1067                                 bool isPrintf) {
1068 
1069   const Expr *Fn = TheCall->getCallee();
1070 
1071   // The way the format attribute works in GCC, the implicit this argument
1072   // of member functions is counted. However, it doesn't appear in our own
1073   // lists, so decrement format_idx in that case.
1074   if (isa<CXXMemberCallExpr>(TheCall)) {
1075     // Catch a format attribute mistakenly referring to the object argument.
1076     if (format_idx == 0)
1077       return;
1078     --format_idx;
1079     if(firstDataArg != 0)
1080       --firstDataArg;
1081   }
1082 
1083   // CHECK: printf/scanf-like function is called with no format string.
1084   if (format_idx >= TheCall->getNumArgs()) {
1085     Diag(TheCall->getRParenLoc(), diag::warn_missing_format_string)
1086       << Fn->getSourceRange();
1087     return;
1088   }
1089 
1090   const Expr *OrigFormatExpr = TheCall->getArg(format_idx)->IgnoreParenCasts();
1091 
1092   // CHECK: format string is not a string literal.
1093   //
1094   // Dynamically generated format strings are difficult to
1095   // automatically vet at compile time.  Requiring that format strings
1096   // are string literals: (1) permits the checking of format strings by
1097   // the compiler and thereby (2) can practically remove the source of
1098   // many format string exploits.
1099 
1100   // Format string can be either ObjC string (e.g. @"%d") or
1101   // C string (e.g. "%d")
1102   // ObjC string uses the same format specifiers as C string, so we can use
1103   // the same format string checking logic for both ObjC and C strings.
1104   if (SemaCheckStringLiteral(OrigFormatExpr, TheCall, HasVAListArg, format_idx,
1105                              firstDataArg, isPrintf))
1106     return;  // Literal format string found, check done!
1107 
1108   // If there are no arguments specified, warn with -Wformat-security, otherwise
1109   // warn only with -Wformat-nonliteral.
1110   if (TheCall->getNumArgs() == format_idx+1)
1111     Diag(TheCall->getArg(format_idx)->getLocStart(),
1112          diag::warn_format_nonliteral_noargs)
1113       << OrigFormatExpr->getSourceRange();
1114   else
1115     Diag(TheCall->getArg(format_idx)->getLocStart(),
1116          diag::warn_format_nonliteral)
1117            << OrigFormatExpr->getSourceRange();
1118 }
1119 
1120 namespace {
1121 class CheckFormatHandler : public analyze_format_string::FormatStringHandler {
1122 protected:
1123   Sema &S;
1124   const StringLiteral *FExpr;
1125   const Expr *OrigFormatExpr;
1126   const unsigned FirstDataArg;
1127   const unsigned NumDataArgs;
1128   const bool IsObjCLiteral;
1129   const char *Beg; // Start of format string.
1130   const bool HasVAListArg;
1131   const CallExpr *TheCall;
1132   unsigned FormatIdx;
1133   llvm::BitVector CoveredArgs;
1134   bool usesPositionalArgs;
1135   bool atFirstArg;
1136 public:
1137   CheckFormatHandler(Sema &s, const StringLiteral *fexpr,
1138                      const Expr *origFormatExpr, unsigned firstDataArg,
1139                      unsigned numDataArgs, bool isObjCLiteral,
1140                      const char *beg, bool hasVAListArg,
1141                      const CallExpr *theCall, unsigned formatIdx)
1142     : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr),
1143       FirstDataArg(firstDataArg),
1144       NumDataArgs(numDataArgs),
1145       IsObjCLiteral(isObjCLiteral), Beg(beg),
1146       HasVAListArg(hasVAListArg),
1147       TheCall(theCall), FormatIdx(formatIdx),
1148       usesPositionalArgs(false), atFirstArg(true) {
1149         CoveredArgs.resize(numDataArgs);
1150         CoveredArgs.reset();
1151       }
1152 
1153   void DoneProcessing();
1154 
1155   void HandleIncompleteSpecifier(const char *startSpecifier,
1156                                  unsigned specifierLen);
1157 
1158   virtual void HandleInvalidPosition(const char *startSpecifier,
1159                                      unsigned specifierLen,
1160                                      analyze_format_string::PositionContext p);
1161 
1162   virtual void HandleZeroPosition(const char *startPos, unsigned posLen);
1163 
1164   void HandleNullChar(const char *nullCharacter);
1165 
1166 protected:
1167   bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc,
1168                                         const char *startSpec,
1169                                         unsigned specifierLen,
1170                                         const char *csStart, unsigned csLen);
1171 
1172   SourceRange getFormatStringRange();
1173   CharSourceRange getSpecifierRange(const char *startSpecifier,
1174                                     unsigned specifierLen);
1175   SourceLocation getLocationOfByte(const char *x);
1176 
1177   const Expr *getDataArg(unsigned i) const;
1178 
1179   bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS,
1180                     const analyze_format_string::ConversionSpecifier &CS,
1181                     const char *startSpecifier, unsigned specifierLen,
1182                     unsigned argIndex);
1183 };
1184 }
1185 
1186 SourceRange CheckFormatHandler::getFormatStringRange() {
1187   return OrigFormatExpr->getSourceRange();
1188 }
1189 
1190 CharSourceRange CheckFormatHandler::
1191 getSpecifierRange(const char *startSpecifier, unsigned specifierLen) {
1192   SourceLocation Start = getLocationOfByte(startSpecifier);
1193   SourceLocation End   = getLocationOfByte(startSpecifier + specifierLen - 1);
1194 
1195   // Advance the end SourceLocation by one due to half-open ranges.
1196   End = End.getFileLocWithOffset(1);
1197 
1198   return CharSourceRange::getCharRange(Start, End);
1199 }
1200 
1201 SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) {
1202   return S.getLocationOfStringLiteralByte(FExpr, x - Beg);
1203 }
1204 
1205 void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier,
1206                                                    unsigned specifierLen){
1207   SourceLocation Loc = getLocationOfByte(startSpecifier);
1208   S.Diag(Loc, diag::warn_printf_incomplete_specifier)
1209     << getSpecifierRange(startSpecifier, specifierLen);
1210 }
1211 
1212 void
1213 CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen,
1214                                      analyze_format_string::PositionContext p) {
1215   SourceLocation Loc = getLocationOfByte(startPos);
1216   S.Diag(Loc, diag::warn_format_invalid_positional_specifier)
1217     << (unsigned) p << getSpecifierRange(startPos, posLen);
1218 }
1219 
1220 void CheckFormatHandler::HandleZeroPosition(const char *startPos,
1221                                             unsigned posLen) {
1222   SourceLocation Loc = getLocationOfByte(startPos);
1223   S.Diag(Loc, diag::warn_format_zero_positional_specifier)
1224     << getSpecifierRange(startPos, posLen);
1225 }
1226 
1227 void CheckFormatHandler::HandleNullChar(const char *nullCharacter) {
1228   // The presence of a null character is likely an error.
1229   S.Diag(getLocationOfByte(nullCharacter),
1230          diag::warn_printf_format_string_contains_null_char)
1231     << getFormatStringRange();
1232 }
1233 
1234 const Expr *CheckFormatHandler::getDataArg(unsigned i) const {
1235   return TheCall->getArg(FirstDataArg + i);
1236 }
1237 
1238 void CheckFormatHandler::DoneProcessing() {
1239     // Does the number of data arguments exceed the number of
1240     // format conversions in the format string?
1241   if (!HasVAListArg) {
1242       // Find any arguments that weren't covered.
1243     CoveredArgs.flip();
1244     signed notCoveredArg = CoveredArgs.find_first();
1245     if (notCoveredArg >= 0) {
1246       assert((unsigned)notCoveredArg < NumDataArgs);
1247       S.Diag(getDataArg((unsigned) notCoveredArg)->getLocStart(),
1248              diag::warn_printf_data_arg_not_used)
1249       << getFormatStringRange();
1250     }
1251   }
1252 }
1253 
1254 bool
1255 CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex,
1256                                                      SourceLocation Loc,
1257                                                      const char *startSpec,
1258                                                      unsigned specifierLen,
1259                                                      const char *csStart,
1260                                                      unsigned csLen) {
1261 
1262   bool keepGoing = true;
1263   if (argIndex < NumDataArgs) {
1264     // Consider the argument coverered, even though the specifier doesn't
1265     // make sense.
1266     CoveredArgs.set(argIndex);
1267   }
1268   else {
1269     // If argIndex exceeds the number of data arguments we
1270     // don't issue a warning because that is just a cascade of warnings (and
1271     // they may have intended '%%' anyway). We don't want to continue processing
1272     // the format string after this point, however, as we will like just get
1273     // gibberish when trying to match arguments.
1274     keepGoing = false;
1275   }
1276 
1277   S.Diag(Loc, diag::warn_format_invalid_conversion)
1278     << llvm::StringRef(csStart, csLen)
1279     << getSpecifierRange(startSpec, specifierLen);
1280 
1281   return keepGoing;
1282 }
1283 
1284 bool
1285 CheckFormatHandler::CheckNumArgs(
1286   const analyze_format_string::FormatSpecifier &FS,
1287   const analyze_format_string::ConversionSpecifier &CS,
1288   const char *startSpecifier, unsigned specifierLen, unsigned argIndex) {
1289 
1290   if (argIndex >= NumDataArgs) {
1291     if (FS.usesPositionalArg())  {
1292       S.Diag(getLocationOfByte(CS.getStart()),
1293              diag::warn_printf_positional_arg_exceeds_data_args)
1294       << (argIndex+1) << NumDataArgs
1295       << getSpecifierRange(startSpecifier, specifierLen);
1296     }
1297     else {
1298       S.Diag(getLocationOfByte(CS.getStart()),
1299              diag::warn_printf_insufficient_data_args)
1300       << getSpecifierRange(startSpecifier, specifierLen);
1301     }
1302 
1303     return false;
1304   }
1305   return true;
1306 }
1307 
1308 //===--- CHECK: Printf format string checking ------------------------------===//
1309 
1310 namespace {
1311 class CheckPrintfHandler : public CheckFormatHandler {
1312 public:
1313   CheckPrintfHandler(Sema &s, const StringLiteral *fexpr,
1314                      const Expr *origFormatExpr, unsigned firstDataArg,
1315                      unsigned numDataArgs, bool isObjCLiteral,
1316                      const char *beg, bool hasVAListArg,
1317                      const CallExpr *theCall, unsigned formatIdx)
1318   : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg,
1319                        numDataArgs, isObjCLiteral, beg, hasVAListArg,
1320                        theCall, formatIdx) {}
1321 
1322 
1323   bool HandleInvalidPrintfConversionSpecifier(
1324                                       const analyze_printf::PrintfSpecifier &FS,
1325                                       const char *startSpecifier,
1326                                       unsigned specifierLen);
1327 
1328   bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS,
1329                              const char *startSpecifier,
1330                              unsigned specifierLen);
1331 
1332   bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k,
1333                     const char *startSpecifier, unsigned specifierLen);
1334   void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS,
1335                            const analyze_printf::OptionalAmount &Amt,
1336                            unsigned type,
1337                            const char *startSpecifier, unsigned specifierLen);
1338   void HandleFlag(const analyze_printf::PrintfSpecifier &FS,
1339                   const analyze_printf::OptionalFlag &flag,
1340                   const char *startSpecifier, unsigned specifierLen);
1341   void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS,
1342                          const analyze_printf::OptionalFlag &ignoredFlag,
1343                          const analyze_printf::OptionalFlag &flag,
1344                          const char *startSpecifier, unsigned specifierLen);
1345 };
1346 }
1347 
1348 bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier(
1349                                       const analyze_printf::PrintfSpecifier &FS,
1350                                       const char *startSpecifier,
1351                                       unsigned specifierLen) {
1352   const analyze_printf::PrintfConversionSpecifier &CS =
1353     FS.getConversionSpecifier();
1354 
1355   return HandleInvalidConversionSpecifier(FS.getArgIndex(),
1356                                           getLocationOfByte(CS.getStart()),
1357                                           startSpecifier, specifierLen,
1358                                           CS.getStart(), CS.getLength());
1359 }
1360 
1361 bool CheckPrintfHandler::HandleAmount(
1362                                const analyze_format_string::OptionalAmount &Amt,
1363                                unsigned k, const char *startSpecifier,
1364                                unsigned specifierLen) {
1365 
1366   if (Amt.hasDataArgument()) {
1367     if (!HasVAListArg) {
1368       unsigned argIndex = Amt.getArgIndex();
1369       if (argIndex >= NumDataArgs) {
1370         S.Diag(getLocationOfByte(Amt.getStart()),
1371                diag::warn_printf_asterisk_missing_arg)
1372           << k << getSpecifierRange(startSpecifier, specifierLen);
1373         // Don't do any more checking.  We will just emit
1374         // spurious errors.
1375         return false;
1376       }
1377 
1378       // Type check the data argument.  It should be an 'int'.
1379       // Although not in conformance with C99, we also allow the argument to be
1380       // an 'unsigned int' as that is a reasonably safe case.  GCC also
1381       // doesn't emit a warning for that case.
1382       CoveredArgs.set(argIndex);
1383       const Expr *Arg = getDataArg(argIndex);
1384       QualType T = Arg->getType();
1385 
1386       const analyze_printf::ArgTypeResult &ATR = Amt.getArgType(S.Context);
1387       assert(ATR.isValid());
1388 
1389       if (!ATR.matchesType(S.Context, T)) {
1390         S.Diag(getLocationOfByte(Amt.getStart()),
1391                diag::warn_printf_asterisk_wrong_type)
1392           << k
1393           << ATR.getRepresentativeType(S.Context) << T
1394           << getSpecifierRange(startSpecifier, specifierLen)
1395           << Arg->getSourceRange();
1396         // Don't do any more checking.  We will just emit
1397         // spurious errors.
1398         return false;
1399       }
1400     }
1401   }
1402   return true;
1403 }
1404 
1405 void CheckPrintfHandler::HandleInvalidAmount(
1406                                       const analyze_printf::PrintfSpecifier &FS,
1407                                       const analyze_printf::OptionalAmount &Amt,
1408                                       unsigned type,
1409                                       const char *startSpecifier,
1410                                       unsigned specifierLen) {
1411   const analyze_printf::PrintfConversionSpecifier &CS =
1412     FS.getConversionSpecifier();
1413   switch (Amt.getHowSpecified()) {
1414   case analyze_printf::OptionalAmount::Constant:
1415     S.Diag(getLocationOfByte(Amt.getStart()),
1416         diag::warn_printf_nonsensical_optional_amount)
1417       << type
1418       << CS.toString()
1419       << getSpecifierRange(startSpecifier, specifierLen)
1420       << FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(),
1421           Amt.getConstantLength()));
1422     break;
1423 
1424   default:
1425     S.Diag(getLocationOfByte(Amt.getStart()),
1426         diag::warn_printf_nonsensical_optional_amount)
1427       << type
1428       << CS.toString()
1429       << getSpecifierRange(startSpecifier, specifierLen);
1430     break;
1431   }
1432 }
1433 
1434 void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS,
1435                                     const analyze_printf::OptionalFlag &flag,
1436                                     const char *startSpecifier,
1437                                     unsigned specifierLen) {
1438   // Warn about pointless flag with a fixit removal.
1439   const analyze_printf::PrintfConversionSpecifier &CS =
1440     FS.getConversionSpecifier();
1441   S.Diag(getLocationOfByte(flag.getPosition()),
1442       diag::warn_printf_nonsensical_flag)
1443     << flag.toString() << CS.toString()
1444     << getSpecifierRange(startSpecifier, specifierLen)
1445     << FixItHint::CreateRemoval(getSpecifierRange(flag.getPosition(), 1));
1446 }
1447 
1448 void CheckPrintfHandler::HandleIgnoredFlag(
1449                                 const analyze_printf::PrintfSpecifier &FS,
1450                                 const analyze_printf::OptionalFlag &ignoredFlag,
1451                                 const analyze_printf::OptionalFlag &flag,
1452                                 const char *startSpecifier,
1453                                 unsigned specifierLen) {
1454   // Warn about ignored flag with a fixit removal.
1455   S.Diag(getLocationOfByte(ignoredFlag.getPosition()),
1456       diag::warn_printf_ignored_flag)
1457     << ignoredFlag.toString() << flag.toString()
1458     << getSpecifierRange(startSpecifier, specifierLen)
1459     << FixItHint::CreateRemoval(getSpecifierRange(
1460         ignoredFlag.getPosition(), 1));
1461 }
1462 
1463 bool
1464 CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier
1465                                             &FS,
1466                                           const char *startSpecifier,
1467                                           unsigned specifierLen) {
1468 
1469   using namespace analyze_format_string;
1470   using namespace analyze_printf;
1471   const PrintfConversionSpecifier &CS = FS.getConversionSpecifier();
1472 
1473   if (FS.consumesDataArgument()) {
1474     if (atFirstArg) {
1475         atFirstArg = false;
1476         usesPositionalArgs = FS.usesPositionalArg();
1477     }
1478     else if (usesPositionalArgs != FS.usesPositionalArg()) {
1479       // Cannot mix-and-match positional and non-positional arguments.
1480       S.Diag(getLocationOfByte(CS.getStart()),
1481              diag::warn_format_mix_positional_nonpositional_args)
1482         << getSpecifierRange(startSpecifier, specifierLen);
1483       return false;
1484     }
1485   }
1486 
1487   // First check if the field width, precision, and conversion specifier
1488   // have matching data arguments.
1489   if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0,
1490                     startSpecifier, specifierLen)) {
1491     return false;
1492   }
1493 
1494   if (!HandleAmount(FS.getPrecision(), /* precision */ 1,
1495                     startSpecifier, specifierLen)) {
1496     return false;
1497   }
1498 
1499   if (!CS.consumesDataArgument()) {
1500     // FIXME: Technically specifying a precision or field width here
1501     // makes no sense.  Worth issuing a warning at some point.
1502     return true;
1503   }
1504 
1505   // Consume the argument.
1506   unsigned argIndex = FS.getArgIndex();
1507   if (argIndex < NumDataArgs) {
1508     // The check to see if the argIndex is valid will come later.
1509     // We set the bit here because we may exit early from this
1510     // function if we encounter some other error.
1511     CoveredArgs.set(argIndex);
1512   }
1513 
1514   // FreeBSD extensions
1515   if (CS.getKind() == ConversionSpecifier::bArg || CS.getKind() == ConversionSpecifier::DArg) {
1516      // claim the second argument
1517      CoveredArgs.set(argIndex + 1);
1518 
1519     // Now type check the data expression that matches the
1520     // format specifier.
1521     const Expr *Ex = getDataArg(argIndex);
1522     const analyze_printf::ArgTypeResult &ATR =
1523       (CS.getKind() == ConversionSpecifier::bArg) ?
1524         ArgTypeResult(S.Context.IntTy) : ArgTypeResult::CStrTy;
1525     if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType()))
1526       S.Diag(getLocationOfByte(CS.getStart()),
1527              diag::warn_printf_conversion_argument_type_mismatch)
1528         << ATR.getRepresentativeType(S.Context) << Ex->getType()
1529         << getSpecifierRange(startSpecifier, specifierLen)
1530         << Ex->getSourceRange();
1531 
1532     // Now type check the data expression that matches the
1533     // format specifier.
1534     Ex = getDataArg(argIndex + 1);
1535     const analyze_printf::ArgTypeResult &ATR2 = ArgTypeResult::CStrTy;
1536     if (ATR2.isValid() && !ATR2.matchesType(S.Context, Ex->getType()))
1537       S.Diag(getLocationOfByte(CS.getStart()),
1538              diag::warn_printf_conversion_argument_type_mismatch)
1539         << ATR2.getRepresentativeType(S.Context) << Ex->getType()
1540         << getSpecifierRange(startSpecifier, specifierLen)
1541         << Ex->getSourceRange();
1542 
1543      return true;
1544   }
1545   // END OF FREEBSD EXTENSIONS
1546 
1547   // Check for using an Objective-C specific conversion specifier
1548   // in a non-ObjC literal.
1549   if (!IsObjCLiteral && CS.isObjCArg()) {
1550     return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier,
1551                                                   specifierLen);
1552   }
1553 
1554   // Check for invalid use of field width
1555   if (!FS.hasValidFieldWidth()) {
1556     HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0,
1557         startSpecifier, specifierLen);
1558   }
1559 
1560   // Check for invalid use of precision
1561   if (!FS.hasValidPrecision()) {
1562     HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1,
1563         startSpecifier, specifierLen);
1564   }
1565 
1566   // Check each flag does not conflict with any other component.
1567   if (!FS.hasValidLeadingZeros())
1568     HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen);
1569   if (!FS.hasValidPlusPrefix())
1570     HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen);
1571   if (!FS.hasValidSpacePrefix())
1572     HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen);
1573   if (!FS.hasValidAlternativeForm())
1574     HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen);
1575   if (!FS.hasValidLeftJustified())
1576     HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen);
1577 
1578   // Check that flags are not ignored by another flag
1579   if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+'
1580     HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(),
1581         startSpecifier, specifierLen);
1582   if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-'
1583     HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(),
1584             startSpecifier, specifierLen);
1585 
1586   // Check the length modifier is valid with the given conversion specifier.
1587   const LengthModifier &LM = FS.getLengthModifier();
1588   if (!FS.hasValidLengthModifier())
1589     S.Diag(getLocationOfByte(LM.getStart()),
1590         diag::warn_format_nonsensical_length)
1591       << LM.toString() << CS.toString()
1592       << getSpecifierRange(startSpecifier, specifierLen)
1593       << FixItHint::CreateRemoval(getSpecifierRange(LM.getStart(),
1594           LM.getLength()));
1595 
1596   // Are we using '%n'?
1597   if (CS.getKind() == ConversionSpecifier::nArg) {
1598     // Issue a warning about this being a possible security issue.
1599     S.Diag(getLocationOfByte(CS.getStart()), diag::warn_printf_write_back)
1600       << getSpecifierRange(startSpecifier, specifierLen);
1601     // Continue checking the other format specifiers.
1602     return true;
1603   }
1604 
1605   // The remaining checks depend on the data arguments.
1606   if (HasVAListArg)
1607     return true;
1608 
1609   if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
1610     return false;
1611 
1612   // Now type check the data expression that matches the
1613   // format specifier.
1614   const Expr *Ex = getDataArg(argIndex);
1615   const analyze_printf::ArgTypeResult &ATR = FS.getArgType(S.Context);
1616   if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) {
1617     // Check if we didn't match because of an implicit cast from a 'char'
1618     // or 'short' to an 'int'.  This is done because printf is a varargs
1619     // function.
1620     if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Ex))
1621       if (ICE->getType() == S.Context.IntTy)
1622         if (ATR.matchesType(S.Context, ICE->getSubExpr()->getType()))
1623           return true;
1624 
1625     // We may be able to offer a FixItHint if it is a supported type.
1626     PrintfSpecifier fixedFS = FS;
1627     bool success = fixedFS.fixType(Ex->getType());
1628 
1629     if (success) {
1630       // Get the fix string from the fixed format specifier
1631       llvm::SmallString<128> buf;
1632       llvm::raw_svector_ostream os(buf);
1633       fixedFS.toString(os);
1634 
1635       // FIXME: getRepresentativeType() perhaps should return a string
1636       // instead of a QualType to better handle when the representative
1637       // type is 'wint_t' (which is defined in the system headers).
1638       S.Diag(getLocationOfByte(CS.getStart()),
1639           diag::warn_printf_conversion_argument_type_mismatch)
1640         << ATR.getRepresentativeType(S.Context) << Ex->getType()
1641         << getSpecifierRange(startSpecifier, specifierLen)
1642         << Ex->getSourceRange()
1643         << FixItHint::CreateReplacement(
1644             getSpecifierRange(startSpecifier, specifierLen),
1645             os.str());
1646     }
1647     else {
1648       S.Diag(getLocationOfByte(CS.getStart()),
1649              diag::warn_printf_conversion_argument_type_mismatch)
1650         << ATR.getRepresentativeType(S.Context) << Ex->getType()
1651         << getSpecifierRange(startSpecifier, specifierLen)
1652         << Ex->getSourceRange();
1653     }
1654   }
1655 
1656   return true;
1657 }
1658 
1659 //===--- CHECK: Scanf format string checking ------------------------------===//
1660 
1661 namespace {
1662 class CheckScanfHandler : public CheckFormatHandler {
1663 public:
1664   CheckScanfHandler(Sema &s, const StringLiteral *fexpr,
1665                     const Expr *origFormatExpr, unsigned firstDataArg,
1666                     unsigned numDataArgs, bool isObjCLiteral,
1667                     const char *beg, bool hasVAListArg,
1668                     const CallExpr *theCall, unsigned formatIdx)
1669   : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg,
1670                        numDataArgs, isObjCLiteral, beg, hasVAListArg,
1671                        theCall, formatIdx) {}
1672 
1673   bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS,
1674                             const char *startSpecifier,
1675                             unsigned specifierLen);
1676 
1677   bool HandleInvalidScanfConversionSpecifier(
1678           const analyze_scanf::ScanfSpecifier &FS,
1679           const char *startSpecifier,
1680           unsigned specifierLen);
1681 
1682   void HandleIncompleteScanList(const char *start, const char *end);
1683 };
1684 }
1685 
1686 void CheckScanfHandler::HandleIncompleteScanList(const char *start,
1687                                                  const char *end) {
1688   S.Diag(getLocationOfByte(end), diag::warn_scanf_scanlist_incomplete)
1689     << getSpecifierRange(start, end - start);
1690 }
1691 
1692 bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier(
1693                                         const analyze_scanf::ScanfSpecifier &FS,
1694                                         const char *startSpecifier,
1695                                         unsigned specifierLen) {
1696 
1697   const analyze_scanf::ScanfConversionSpecifier &CS =
1698     FS.getConversionSpecifier();
1699 
1700   return HandleInvalidConversionSpecifier(FS.getArgIndex(),
1701                                           getLocationOfByte(CS.getStart()),
1702                                           startSpecifier, specifierLen,
1703                                           CS.getStart(), CS.getLength());
1704 }
1705 
1706 bool CheckScanfHandler::HandleScanfSpecifier(
1707                                        const analyze_scanf::ScanfSpecifier &FS,
1708                                        const char *startSpecifier,
1709                                        unsigned specifierLen) {
1710 
1711   using namespace analyze_scanf;
1712   using namespace analyze_format_string;
1713 
1714   const ScanfConversionSpecifier &CS = FS.getConversionSpecifier();
1715 
1716   // Handle case where '%' and '*' don't consume an argument.  These shouldn't
1717   // be used to decide if we are using positional arguments consistently.
1718   if (FS.consumesDataArgument()) {
1719     if (atFirstArg) {
1720       atFirstArg = false;
1721       usesPositionalArgs = FS.usesPositionalArg();
1722     }
1723     else if (usesPositionalArgs != FS.usesPositionalArg()) {
1724       // Cannot mix-and-match positional and non-positional arguments.
1725       S.Diag(getLocationOfByte(CS.getStart()),
1726              diag::warn_format_mix_positional_nonpositional_args)
1727         << getSpecifierRange(startSpecifier, specifierLen);
1728       return false;
1729     }
1730   }
1731 
1732   // Check if the field with is non-zero.
1733   const OptionalAmount &Amt = FS.getFieldWidth();
1734   if (Amt.getHowSpecified() == OptionalAmount::Constant) {
1735     if (Amt.getConstantAmount() == 0) {
1736       const CharSourceRange &R = getSpecifierRange(Amt.getStart(),
1737                                                    Amt.getConstantLength());
1738       S.Diag(getLocationOfByte(Amt.getStart()),
1739              diag::warn_scanf_nonzero_width)
1740         << R << FixItHint::CreateRemoval(R);
1741     }
1742   }
1743 
1744   if (!FS.consumesDataArgument()) {
1745     // FIXME: Technically specifying a precision or field width here
1746     // makes no sense.  Worth issuing a warning at some point.
1747     return true;
1748   }
1749 
1750   // Consume the argument.
1751   unsigned argIndex = FS.getArgIndex();
1752   if (argIndex < NumDataArgs) {
1753       // The check to see if the argIndex is valid will come later.
1754       // We set the bit here because we may exit early from this
1755       // function if we encounter some other error.
1756     CoveredArgs.set(argIndex);
1757   }
1758 
1759   // Check the length modifier is valid with the given conversion specifier.
1760   const LengthModifier &LM = FS.getLengthModifier();
1761   if (!FS.hasValidLengthModifier()) {
1762     S.Diag(getLocationOfByte(LM.getStart()),
1763            diag::warn_format_nonsensical_length)
1764       << LM.toString() << CS.toString()
1765       << getSpecifierRange(startSpecifier, specifierLen)
1766       << FixItHint::CreateRemoval(getSpecifierRange(LM.getStart(),
1767                                                     LM.getLength()));
1768   }
1769 
1770   // The remaining checks depend on the data arguments.
1771   if (HasVAListArg)
1772     return true;
1773 
1774   if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
1775     return false;
1776 
1777   // FIXME: Check that the argument type matches the format specifier.
1778 
1779   return true;
1780 }
1781 
1782 void Sema::CheckFormatString(const StringLiteral *FExpr,
1783                              const Expr *OrigFormatExpr,
1784                              const CallExpr *TheCall, bool HasVAListArg,
1785                              unsigned format_idx, unsigned firstDataArg,
1786                              bool isPrintf) {
1787 
1788   // CHECK: is the format string a wide literal?
1789   if (FExpr->isWide()) {
1790     Diag(FExpr->getLocStart(),
1791          diag::warn_format_string_is_wide_literal)
1792     << OrigFormatExpr->getSourceRange();
1793     return;
1794   }
1795 
1796   // Str - The format string.  NOTE: this is NOT null-terminated!
1797   llvm::StringRef StrRef = FExpr->getString();
1798   const char *Str = StrRef.data();
1799   unsigned StrLen = StrRef.size();
1800 
1801   // CHECK: empty format string?
1802   if (StrLen == 0) {
1803     Diag(FExpr->getLocStart(), diag::warn_empty_format_string)
1804     << OrigFormatExpr->getSourceRange();
1805     return;
1806   }
1807 
1808   if (isPrintf) {
1809     CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg,
1810                          TheCall->getNumArgs() - firstDataArg,
1811                          isa<ObjCStringLiteral>(OrigFormatExpr), Str,
1812                          HasVAListArg, TheCall, format_idx);
1813 
1814     bool FormatExtensions = getLangOptions().FormatExtensions;
1815     if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen,
1816                                                   FormatExtensions))
1817       H.DoneProcessing();
1818   }
1819   else {
1820     CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg,
1821                         TheCall->getNumArgs() - firstDataArg,
1822                         isa<ObjCStringLiteral>(OrigFormatExpr), Str,
1823                         HasVAListArg, TheCall, format_idx);
1824 
1825     if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen))
1826       H.DoneProcessing();
1827   }
1828 }
1829 
1830 //===--- CHECK: Return Address of Stack Variable --------------------------===//
1831 
1832 static DeclRefExpr* EvalVal(Expr *E);
1833 static DeclRefExpr* EvalAddr(Expr* E);
1834 
1835 /// CheckReturnStackAddr - Check if a return statement returns the address
1836 ///   of a stack variable.
1837 void
1838 Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
1839                            SourceLocation ReturnLoc) {
1840 
1841   // Perform checking for returned stack addresses.
1842   if (lhsType->isPointerType() || lhsType->isBlockPointerType()) {
1843     if (DeclRefExpr *DR = EvalAddr(RetValExp))
1844       Diag(DR->getLocStart(), diag::warn_ret_stack_addr)
1845        << DR->getDecl()->getDeclName() << RetValExp->getSourceRange();
1846 
1847     // Skip over implicit cast expressions when checking for block expressions.
1848     RetValExp = RetValExp->IgnoreParenCasts();
1849 
1850     if (BlockExpr *C = dyn_cast<BlockExpr>(RetValExp))
1851       if (C->hasBlockDeclRefExprs())
1852         Diag(C->getLocStart(), diag::err_ret_local_block)
1853           << C->getSourceRange();
1854 
1855     if (AddrLabelExpr *ALE = dyn_cast<AddrLabelExpr>(RetValExp))
1856       Diag(ALE->getLocStart(), diag::warn_ret_addr_label)
1857         << ALE->getSourceRange();
1858 
1859   } else if (lhsType->isReferenceType()) {
1860     // Perform checking for stack values returned by reference.
1861     // Check for a reference to the stack
1862     if (DeclRefExpr *DR = EvalVal(RetValExp))
1863       Diag(DR->getLocStart(), diag::warn_ret_stack_ref)
1864         << DR->getDecl()->getDeclName() << RetValExp->getSourceRange();
1865   }
1866 }
1867 
1868 /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that
1869 ///  check if the expression in a return statement evaluates to an address
1870 ///  to a location on the stack.  The recursion is used to traverse the
1871 ///  AST of the return expression, with recursion backtracking when we
1872 ///  encounter a subexpression that (1) clearly does not lead to the address
1873 ///  of a stack variable or (2) is something we cannot determine leads to
1874 ///  the address of a stack variable based on such local checking.
1875 ///
1876 ///  EvalAddr processes expressions that are pointers that are used as
1877 ///  references (and not L-values).  EvalVal handles all other values.
1878 ///  At the base case of the recursion is a check for a DeclRefExpr* in
1879 ///  the refers to a stack variable.
1880 ///
1881 ///  This implementation handles:
1882 ///
1883 ///   * pointer-to-pointer casts
1884 ///   * implicit conversions from array references to pointers
1885 ///   * taking the address of fields
1886 ///   * arbitrary interplay between "&" and "*" operators
1887 ///   * pointer arithmetic from an address of a stack variable
1888 ///   * taking the address of an array element where the array is on the stack
1889 static DeclRefExpr* EvalAddr(Expr *E) {
1890   // We should only be called for evaluating pointer expressions.
1891   assert((E->getType()->isAnyPointerType() ||
1892           E->getType()->isBlockPointerType() ||
1893           E->getType()->isObjCQualifiedIdType()) &&
1894          "EvalAddr only works on pointers");
1895 
1896   // Our "symbolic interpreter" is just a dispatch off the currently
1897   // viewed AST node.  We then recursively traverse the AST by calling
1898   // EvalAddr and EvalVal appropriately.
1899   switch (E->getStmtClass()) {
1900   case Stmt::ParenExprClass:
1901     // Ignore parentheses.
1902     return EvalAddr(cast<ParenExpr>(E)->getSubExpr());
1903 
1904   case Stmt::UnaryOperatorClass: {
1905     // The only unary operator that make sense to handle here
1906     // is AddrOf.  All others don't make sense as pointers.
1907     UnaryOperator *U = cast<UnaryOperator>(E);
1908 
1909     if (U->getOpcode() == UO_AddrOf)
1910       return EvalVal(U->getSubExpr());
1911     else
1912       return NULL;
1913   }
1914 
1915   case Stmt::BinaryOperatorClass: {
1916     // Handle pointer arithmetic.  All other binary operators are not valid
1917     // in this context.
1918     BinaryOperator *B = cast<BinaryOperator>(E);
1919     BinaryOperatorKind op = B->getOpcode();
1920 
1921     if (op != BO_Add && op != BO_Sub)
1922       return NULL;
1923 
1924     Expr *Base = B->getLHS();
1925 
1926     // Determine which argument is the real pointer base.  It could be
1927     // the RHS argument instead of the LHS.
1928     if (!Base->getType()->isPointerType()) Base = B->getRHS();
1929 
1930     assert (Base->getType()->isPointerType());
1931     return EvalAddr(Base);
1932   }
1933 
1934   // For conditional operators we need to see if either the LHS or RHS are
1935   // valid DeclRefExpr*s.  If one of them is valid, we return it.
1936   case Stmt::ConditionalOperatorClass: {
1937     ConditionalOperator *C = cast<ConditionalOperator>(E);
1938 
1939     // Handle the GNU extension for missing LHS.
1940     if (Expr *lhsExpr = C->getLHS())
1941       if (DeclRefExpr* LHS = EvalAddr(lhsExpr))
1942         return LHS;
1943 
1944      return EvalAddr(C->getRHS());
1945   }
1946 
1947   // For casts, we need to handle conversions from arrays to
1948   // pointer values, and pointer-to-pointer conversions.
1949   case Stmt::ImplicitCastExprClass:
1950   case Stmt::CStyleCastExprClass:
1951   case Stmt::CXXFunctionalCastExprClass: {
1952     Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
1953     QualType T = SubExpr->getType();
1954 
1955     if (SubExpr->getType()->isPointerType() ||
1956         SubExpr->getType()->isBlockPointerType() ||
1957         SubExpr->getType()->isObjCQualifiedIdType())
1958       return EvalAddr(SubExpr);
1959     else if (T->isArrayType())
1960       return EvalVal(SubExpr);
1961     else
1962       return 0;
1963   }
1964 
1965   // C++ casts.  For dynamic casts, static casts, and const casts, we
1966   // are always converting from a pointer-to-pointer, so we just blow
1967   // through the cast.  In the case the dynamic cast doesn't fail (and
1968   // return NULL), we take the conservative route and report cases
1969   // where we return the address of a stack variable.  For Reinterpre
1970   // FIXME: The comment about is wrong; we're not always converting
1971   // from pointer to pointer. I'm guessing that this code should also
1972   // handle references to objects.
1973   case Stmt::CXXStaticCastExprClass:
1974   case Stmt::CXXDynamicCastExprClass:
1975   case Stmt::CXXConstCastExprClass:
1976   case Stmt::CXXReinterpretCastExprClass: {
1977       Expr *S = cast<CXXNamedCastExpr>(E)->getSubExpr();
1978       if (S->getType()->isPointerType() || S->getType()->isBlockPointerType())
1979         return EvalAddr(S);
1980       else
1981         return NULL;
1982   }
1983 
1984   // Everything else: we simply don't reason about them.
1985   default:
1986     return NULL;
1987   }
1988 }
1989 
1990 
1991 ///  EvalVal - This function is complements EvalAddr in the mutual recursion.
1992 ///   See the comments for EvalAddr for more details.
1993 static DeclRefExpr* EvalVal(Expr *E) {
1994 do {
1995   // We should only be called for evaluating non-pointer expressions, or
1996   // expressions with a pointer type that are not used as references but instead
1997   // are l-values (e.g., DeclRefExpr with a pointer type).
1998 
1999   // Our "symbolic interpreter" is just a dispatch off the currently
2000   // viewed AST node.  We then recursively traverse the AST by calling
2001   // EvalAddr and EvalVal appropriately.
2002   switch (E->getStmtClass()) {
2003   case Stmt::ImplicitCastExprClass: {
2004     ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E);
2005     if (IE->getValueKind() == VK_LValue) {
2006       E = IE->getSubExpr();
2007       continue;
2008     }
2009     return NULL;
2010   }
2011 
2012   case Stmt::DeclRefExprClass: {
2013     // DeclRefExpr: the base case.  When we hit a DeclRefExpr we are looking
2014     //  at code that refers to a variable's name.  We check if it has local
2015     //  storage within the function, and if so, return the expression.
2016     DeclRefExpr *DR = cast<DeclRefExpr>(E);
2017 
2018     if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
2019       if (V->hasLocalStorage() && !V->getType()->isReferenceType()) return DR;
2020 
2021     return NULL;
2022   }
2023 
2024   case Stmt::ParenExprClass: {
2025     // Ignore parentheses.
2026     E = cast<ParenExpr>(E)->getSubExpr();
2027     continue;
2028   }
2029 
2030   case Stmt::UnaryOperatorClass: {
2031     // The only unary operator that make sense to handle here
2032     // is Deref.  All others don't resolve to a "name."  This includes
2033     // handling all sorts of rvalues passed to a unary operator.
2034     UnaryOperator *U = cast<UnaryOperator>(E);
2035 
2036     if (U->getOpcode() == UO_Deref)
2037       return EvalAddr(U->getSubExpr());
2038 
2039     return NULL;
2040   }
2041 
2042   case Stmt::ArraySubscriptExprClass: {
2043     // Array subscripts are potential references to data on the stack.  We
2044     // retrieve the DeclRefExpr* for the array variable if it indeed
2045     // has local storage.
2046     return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase());
2047   }
2048 
2049   case Stmt::ConditionalOperatorClass: {
2050     // For conditional operators we need to see if either the LHS or RHS are
2051     // non-NULL DeclRefExpr's.  If one is non-NULL, we return it.
2052     ConditionalOperator *C = cast<ConditionalOperator>(E);
2053 
2054     // Handle the GNU extension for missing LHS.
2055     if (Expr *lhsExpr = C->getLHS())
2056       if (DeclRefExpr *LHS = EvalVal(lhsExpr))
2057         return LHS;
2058 
2059     return EvalVal(C->getRHS());
2060   }
2061 
2062   // Accesses to members are potential references to data on the stack.
2063   case Stmt::MemberExprClass: {
2064     MemberExpr *M = cast<MemberExpr>(E);
2065 
2066     // Check for indirect access.  We only want direct field accesses.
2067     if (M->isArrow())
2068       return NULL;
2069 
2070     // Check whether the member type is itself a reference, in which case
2071     // we're not going to refer to the member, but to what the member refers to.
2072     if (M->getMemberDecl()->getType()->isReferenceType())
2073       return NULL;
2074 
2075     return EvalVal(M->getBase());
2076   }
2077 
2078   // Everything else: we simply don't reason about them.
2079   default:
2080     return NULL;
2081   }
2082 } while (true);
2083 }
2084 
2085 //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===//
2086 
2087 /// Check for comparisons of floating point operands using != and ==.
2088 /// Issue a warning if these are no self-comparisons, as they are not likely
2089 /// to do what the programmer intended.
2090 void Sema::CheckFloatComparison(SourceLocation loc, Expr* lex, Expr *rex) {
2091   bool EmitWarning = true;
2092 
2093   Expr* LeftExprSansParen = lex->IgnoreParens();
2094   Expr* RightExprSansParen = rex->IgnoreParens();
2095 
2096   // Special case: check for x == x (which is OK).
2097   // Do not emit warnings for such cases.
2098   if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen))
2099     if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen))
2100       if (DRL->getDecl() == DRR->getDecl())
2101         EmitWarning = false;
2102 
2103 
2104   // Special case: check for comparisons against literals that can be exactly
2105   //  represented by APFloat.  In such cases, do not emit a warning.  This
2106   //  is a heuristic: often comparison against such literals are used to
2107   //  detect if a value in a variable has not changed.  This clearly can
2108   //  lead to false negatives.
2109   if (EmitWarning) {
2110     if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) {
2111       if (FLL->isExact())
2112         EmitWarning = false;
2113     } else
2114       if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)){
2115         if (FLR->isExact())
2116           EmitWarning = false;
2117     }
2118   }
2119 
2120   // Check for comparisons with builtin types.
2121   if (EmitWarning)
2122     if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen))
2123       if (CL->isBuiltinCall(Context))
2124         EmitWarning = false;
2125 
2126   if (EmitWarning)
2127     if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen))
2128       if (CR->isBuiltinCall(Context))
2129         EmitWarning = false;
2130 
2131   // Emit the diagnostic.
2132   if (EmitWarning)
2133     Diag(loc, diag::warn_floatingpoint_eq)
2134       << lex->getSourceRange() << rex->getSourceRange();
2135 }
2136 
2137 //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===//
2138 //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===//
2139 
2140 namespace {
2141 
2142 /// Structure recording the 'active' range of an integer-valued
2143 /// expression.
2144 struct IntRange {
2145   /// The number of bits active in the int.
2146   unsigned Width;
2147 
2148   /// True if the int is known not to have negative values.
2149   bool NonNegative;
2150 
2151   IntRange(unsigned Width, bool NonNegative)
2152     : Width(Width), NonNegative(NonNegative)
2153   {}
2154 
2155   // Returns the range of the bool type.
2156   static IntRange forBoolType() {
2157     return IntRange(1, true);
2158   }
2159 
2160   // Returns the range of an integral type.
2161   static IntRange forType(ASTContext &C, QualType T) {
2162     return forCanonicalType(C, T->getCanonicalTypeInternal().getTypePtr());
2163   }
2164 
2165   // Returns the range of an integeral type based on its canonical
2166   // representation.
2167   static IntRange forCanonicalType(ASTContext &C, const Type *T) {
2168     assert(T->isCanonicalUnqualified());
2169 
2170     if (const VectorType *VT = dyn_cast<VectorType>(T))
2171       T = VT->getElementType().getTypePtr();
2172     if (const ComplexType *CT = dyn_cast<ComplexType>(T))
2173       T = CT->getElementType().getTypePtr();
2174 
2175     if (const EnumType *ET = dyn_cast<EnumType>(T)) {
2176       EnumDecl *Enum = ET->getDecl();
2177       unsigned NumPositive = Enum->getNumPositiveBits();
2178       unsigned NumNegative = Enum->getNumNegativeBits();
2179 
2180       return IntRange(std::max(NumPositive, NumNegative), NumNegative == 0);
2181     }
2182 
2183     const BuiltinType *BT = cast<BuiltinType>(T);
2184     assert(BT->isInteger());
2185 
2186     return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
2187   }
2188 
2189   // Returns the supremum of two ranges: i.e. their conservative merge.
2190   static IntRange join(IntRange L, IntRange R) {
2191     return IntRange(std::max(L.Width, R.Width),
2192                     L.NonNegative && R.NonNegative);
2193   }
2194 
2195   // Returns the infinum of two ranges: i.e. their aggressive merge.
2196   static IntRange meet(IntRange L, IntRange R) {
2197     return IntRange(std::min(L.Width, R.Width),
2198                     L.NonNegative || R.NonNegative);
2199   }
2200 };
2201 
2202 IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, unsigned MaxWidth) {
2203   if (value.isSigned() && value.isNegative())
2204     return IntRange(value.getMinSignedBits(), false);
2205 
2206   if (value.getBitWidth() > MaxWidth)
2207     value.trunc(MaxWidth);
2208 
2209   // isNonNegative() just checks the sign bit without considering
2210   // signedness.
2211   return IntRange(value.getActiveBits(), true);
2212 }
2213 
2214 IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty,
2215                        unsigned MaxWidth) {
2216   if (result.isInt())
2217     return GetValueRange(C, result.getInt(), MaxWidth);
2218 
2219   if (result.isVector()) {
2220     IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth);
2221     for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) {
2222       IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth);
2223       R = IntRange::join(R, El);
2224     }
2225     return R;
2226   }
2227 
2228   if (result.isComplexInt()) {
2229     IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth);
2230     IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth);
2231     return IntRange::join(R, I);
2232   }
2233 
2234   // This can happen with lossless casts to intptr_t of "based" lvalues.
2235   // Assume it might use arbitrary bits.
2236   // FIXME: The only reason we need to pass the type in here is to get
2237   // the sign right on this one case.  It would be nice if APValue
2238   // preserved this.
2239   assert(result.isLValue());
2240   return IntRange(MaxWidth, Ty->isUnsignedIntegerType());
2241 }
2242 
2243 /// Pseudo-evaluate the given integer expression, estimating the
2244 /// range of values it might take.
2245 ///
2246 /// \param MaxWidth - the width to which the value will be truncated
2247 IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) {
2248   E = E->IgnoreParens();
2249 
2250   // Try a full evaluation first.
2251   Expr::EvalResult result;
2252   if (E->Evaluate(result, C))
2253     return GetValueRange(C, result.Val, E->getType(), MaxWidth);
2254 
2255   // I think we only want to look through implicit casts here; if the
2256   // user has an explicit widening cast, we should treat the value as
2257   // being of the new, wider type.
2258   if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) {
2259     if (CE->getCastKind() == CK_NoOp)
2260       return GetExprRange(C, CE->getSubExpr(), MaxWidth);
2261 
2262     IntRange OutputTypeRange = IntRange::forType(C, CE->getType());
2263 
2264     bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast);
2265     if (!isIntegerCast && CE->getCastKind() == CK_Unknown)
2266       isIntegerCast = CE->getSubExpr()->getType()->isIntegerType();
2267 
2268     // Assume that non-integer casts can span the full range of the type.
2269     if (!isIntegerCast)
2270       return OutputTypeRange;
2271 
2272     IntRange SubRange
2273       = GetExprRange(C, CE->getSubExpr(),
2274                      std::min(MaxWidth, OutputTypeRange.Width));
2275 
2276     // Bail out if the subexpr's range is as wide as the cast type.
2277     if (SubRange.Width >= OutputTypeRange.Width)
2278       return OutputTypeRange;
2279 
2280     // Otherwise, we take the smaller width, and we're non-negative if
2281     // either the output type or the subexpr is.
2282     return IntRange(SubRange.Width,
2283                     SubRange.NonNegative || OutputTypeRange.NonNegative);
2284   }
2285 
2286   if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
2287     // If we can fold the condition, just take that operand.
2288     bool CondResult;
2289     if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C))
2290       return GetExprRange(C, CondResult ? CO->getTrueExpr()
2291                                         : CO->getFalseExpr(),
2292                           MaxWidth);
2293 
2294     // Otherwise, conservatively merge.
2295     IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth);
2296     IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth);
2297     return IntRange::join(L, R);
2298   }
2299 
2300   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2301     switch (BO->getOpcode()) {
2302 
2303     // Boolean-valued operations are single-bit and positive.
2304     case BO_LAnd:
2305     case BO_LOr:
2306     case BO_LT:
2307     case BO_GT:
2308     case BO_LE:
2309     case BO_GE:
2310     case BO_EQ:
2311     case BO_NE:
2312       return IntRange::forBoolType();
2313 
2314     // The type of these compound assignments is the type of the LHS,
2315     // so the RHS is not necessarily an integer.
2316     case BO_MulAssign:
2317     case BO_DivAssign:
2318     case BO_RemAssign:
2319     case BO_AddAssign:
2320     case BO_SubAssign:
2321       return IntRange::forType(C, E->getType());
2322 
2323     // Operations with opaque sources are black-listed.
2324     case BO_PtrMemD:
2325     case BO_PtrMemI:
2326       return IntRange::forType(C, E->getType());
2327 
2328     // Bitwise-and uses the *infinum* of the two source ranges.
2329     case BO_And:
2330     case BO_AndAssign:
2331       return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth),
2332                             GetExprRange(C, BO->getRHS(), MaxWidth));
2333 
2334     // Left shift gets black-listed based on a judgement call.
2335     case BO_Shl:
2336       // ...except that we want to treat '1 << (blah)' as logically
2337       // positive.  It's an important idiom.
2338       if (IntegerLiteral *I
2339             = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) {
2340         if (I->getValue() == 1) {
2341           IntRange R = IntRange::forType(C, E->getType());
2342           return IntRange(R.Width, /*NonNegative*/ true);
2343         }
2344       }
2345       // fallthrough
2346 
2347     case BO_ShlAssign:
2348       return IntRange::forType(C, E->getType());
2349 
2350     // Right shift by a constant can narrow its left argument.
2351     case BO_Shr:
2352     case BO_ShrAssign: {
2353       IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth);
2354 
2355       // If the shift amount is a positive constant, drop the width by
2356       // that much.
2357       llvm::APSInt shift;
2358       if (BO->getRHS()->isIntegerConstantExpr(shift, C) &&
2359           shift.isNonNegative()) {
2360         unsigned zext = shift.getZExtValue();
2361         if (zext >= L.Width)
2362           L.Width = (L.NonNegative ? 0 : 1);
2363         else
2364           L.Width -= zext;
2365       }
2366 
2367       return L;
2368     }
2369 
2370     // Comma acts as its right operand.
2371     case BO_Comma:
2372       return GetExprRange(C, BO->getRHS(), MaxWidth);
2373 
2374     // Black-list pointer subtractions.
2375     case BO_Sub:
2376       if (BO->getLHS()->getType()->isPointerType())
2377         return IntRange::forType(C, E->getType());
2378       // fallthrough
2379 
2380     default:
2381       break;
2382     }
2383 
2384     // Treat every other operator as if it were closed on the
2385     // narrowest type that encompasses both operands.
2386     IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth);
2387     IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth);
2388     return IntRange::join(L, R);
2389   }
2390 
2391   if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
2392     switch (UO->getOpcode()) {
2393     // Boolean-valued operations are white-listed.
2394     case UO_LNot:
2395       return IntRange::forBoolType();
2396 
2397     // Operations with opaque sources are black-listed.
2398     case UO_Deref:
2399     case UO_AddrOf: // should be impossible
2400       return IntRange::forType(C, E->getType());
2401 
2402     default:
2403       return GetExprRange(C, UO->getSubExpr(), MaxWidth);
2404     }
2405   }
2406 
2407   if (dyn_cast<OffsetOfExpr>(E)) {
2408     IntRange::forType(C, E->getType());
2409   }
2410 
2411   FieldDecl *BitField = E->getBitField();
2412   if (BitField) {
2413     llvm::APSInt BitWidthAP = BitField->getBitWidth()->EvaluateAsInt(C);
2414     unsigned BitWidth = BitWidthAP.getZExtValue();
2415 
2416     return IntRange(BitWidth, BitField->getType()->isUnsignedIntegerType());
2417   }
2418 
2419   return IntRange::forType(C, E->getType());
2420 }
2421 
2422 IntRange GetExprRange(ASTContext &C, Expr *E) {
2423   return GetExprRange(C, E, C.getIntWidth(E->getType()));
2424 }
2425 
2426 /// Checks whether the given value, which currently has the given
2427 /// source semantics, has the same value when coerced through the
2428 /// target semantics.
2429 bool IsSameFloatAfterCast(const llvm::APFloat &value,
2430                           const llvm::fltSemantics &Src,
2431                           const llvm::fltSemantics &Tgt) {
2432   llvm::APFloat truncated = value;
2433 
2434   bool ignored;
2435   truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored);
2436   truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored);
2437 
2438   return truncated.bitwiseIsEqual(value);
2439 }
2440 
2441 /// Checks whether the given value, which currently has the given
2442 /// source semantics, has the same value when coerced through the
2443 /// target semantics.
2444 ///
2445 /// The value might be a vector of floats (or a complex number).
2446 bool IsSameFloatAfterCast(const APValue &value,
2447                           const llvm::fltSemantics &Src,
2448                           const llvm::fltSemantics &Tgt) {
2449   if (value.isFloat())
2450     return IsSameFloatAfterCast(value.getFloat(), Src, Tgt);
2451 
2452   if (value.isVector()) {
2453     for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i)
2454       if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt))
2455         return false;
2456     return true;
2457   }
2458 
2459   assert(value.isComplexFloat());
2460   return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) &&
2461           IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt));
2462 }
2463 
2464 void AnalyzeImplicitConversions(Sema &S, Expr *E);
2465 
2466 bool IsZero(Sema &S, Expr *E) {
2467   llvm::APSInt Value;
2468   return E->isIntegerConstantExpr(Value, S.Context) && Value == 0;
2469 }
2470 
2471 void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) {
2472   BinaryOperatorKind op = E->getOpcode();
2473   if (op == BO_LT && IsZero(S, E->getRHS())) {
2474     S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
2475       << "< 0" << "false"
2476       << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
2477   } else if (op == BO_GE && IsZero(S, E->getRHS())) {
2478     S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
2479       << ">= 0" << "true"
2480       << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
2481   } else if (op == BO_GT && IsZero(S, E->getLHS())) {
2482     S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison)
2483       << "0 >" << "false"
2484       << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
2485   } else if (op == BO_LE && IsZero(S, E->getLHS())) {
2486     S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison)
2487       << "0 <=" << "true"
2488       << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
2489   }
2490 }
2491 
2492 /// Analyze the operands of the given comparison.  Implements the
2493 /// fallback case from AnalyzeComparison.
2494 void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) {
2495   AnalyzeImplicitConversions(S, E->getLHS());
2496   AnalyzeImplicitConversions(S, E->getRHS());
2497 }
2498 
2499 /// \brief Implements -Wsign-compare.
2500 ///
2501 /// \param lex the left-hand expression
2502 /// \param rex the right-hand expression
2503 /// \param OpLoc the location of the joining operator
2504 /// \param BinOpc binary opcode or 0
2505 void AnalyzeComparison(Sema &S, BinaryOperator *E) {
2506   // The type the comparison is being performed in.
2507   QualType T = E->getLHS()->getType();
2508   assert(S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType())
2509          && "comparison with mismatched types");
2510 
2511   // We don't do anything special if this isn't an unsigned integral
2512   // comparison:  we're only interested in integral comparisons, and
2513   // signed comparisons only happen in cases we don't care to warn about.
2514   if (!T->hasUnsignedIntegerRepresentation())
2515     return AnalyzeImpConvsInComparison(S, E);
2516 
2517   Expr *lex = E->getLHS()->IgnoreParenImpCasts();
2518   Expr *rex = E->getRHS()->IgnoreParenImpCasts();
2519 
2520   // Check to see if one of the (unmodified) operands is of different
2521   // signedness.
2522   Expr *signedOperand, *unsignedOperand;
2523   if (lex->getType()->hasSignedIntegerRepresentation()) {
2524     assert(!rex->getType()->hasSignedIntegerRepresentation() &&
2525            "unsigned comparison between two signed integer expressions?");
2526     signedOperand = lex;
2527     unsignedOperand = rex;
2528   } else if (rex->getType()->hasSignedIntegerRepresentation()) {
2529     signedOperand = rex;
2530     unsignedOperand = lex;
2531   } else {
2532     CheckTrivialUnsignedComparison(S, E);
2533     return AnalyzeImpConvsInComparison(S, E);
2534   }
2535 
2536   // Otherwise, calculate the effective range of the signed operand.
2537   IntRange signedRange = GetExprRange(S.Context, signedOperand);
2538 
2539   // Go ahead and analyze implicit conversions in the operands.  Note
2540   // that we skip the implicit conversions on both sides.
2541   AnalyzeImplicitConversions(S, lex);
2542   AnalyzeImplicitConversions(S, rex);
2543 
2544   // If the signed range is non-negative, -Wsign-compare won't fire,
2545   // but we should still check for comparisons which are always true
2546   // or false.
2547   if (signedRange.NonNegative)
2548     return CheckTrivialUnsignedComparison(S, E);
2549 
2550   // For (in)equality comparisons, if the unsigned operand is a
2551   // constant which cannot collide with a overflowed signed operand,
2552   // then reinterpreting the signed operand as unsigned will not
2553   // change the result of the comparison.
2554   if (E->isEqualityOp()) {
2555     unsigned comparisonWidth = S.Context.getIntWidth(T);
2556     IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand);
2557 
2558     // We should never be unable to prove that the unsigned operand is
2559     // non-negative.
2560     assert(unsignedRange.NonNegative && "unsigned range includes negative?");
2561 
2562     if (unsignedRange.Width < comparisonWidth)
2563       return;
2564   }
2565 
2566   S.Diag(E->getOperatorLoc(), diag::warn_mixed_sign_comparison)
2567     << lex->getType() << rex->getType()
2568     << lex->getSourceRange() << rex->getSourceRange();
2569 }
2570 
2571 /// Diagnose an implicit cast;  purely a helper for CheckImplicitConversion.
2572 void DiagnoseImpCast(Sema &S, Expr *E, QualType T, unsigned diag) {
2573   S.Diag(E->getExprLoc(), diag) << E->getType() << T << E->getSourceRange();
2574 }
2575 
2576 void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
2577                              bool *ICContext = 0) {
2578   if (E->isTypeDependent() || E->isValueDependent()) return;
2579 
2580   const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr();
2581   const Type *Target = S.Context.getCanonicalType(T).getTypePtr();
2582   if (Source == Target) return;
2583   if (Target->isDependentType()) return;
2584 
2585   // Never diagnose implicit casts to bool.
2586   if (Target->isSpecificBuiltinType(BuiltinType::Bool))
2587     return;
2588 
2589   // Strip vector types.
2590   if (isa<VectorType>(Source)) {
2591     if (!isa<VectorType>(Target))
2592       return DiagnoseImpCast(S, E, T, diag::warn_impcast_vector_scalar);
2593 
2594     Source = cast<VectorType>(Source)->getElementType().getTypePtr();
2595     Target = cast<VectorType>(Target)->getElementType().getTypePtr();
2596   }
2597 
2598   // Strip complex types.
2599   if (isa<ComplexType>(Source)) {
2600     if (!isa<ComplexType>(Target))
2601       return DiagnoseImpCast(S, E, T, diag::warn_impcast_complex_scalar);
2602 
2603     Source = cast<ComplexType>(Source)->getElementType().getTypePtr();
2604     Target = cast<ComplexType>(Target)->getElementType().getTypePtr();
2605   }
2606 
2607   const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source);
2608   const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target);
2609 
2610   // If the source is floating point...
2611   if (SourceBT && SourceBT->isFloatingPoint()) {
2612     // ...and the target is floating point...
2613     if (TargetBT && TargetBT->isFloatingPoint()) {
2614       // ...then warn if we're dropping FP rank.
2615 
2616       // Builtin FP kinds are ordered by increasing FP rank.
2617       if (SourceBT->getKind() > TargetBT->getKind()) {
2618         // Don't warn about float constants that are precisely
2619         // representable in the target type.
2620         Expr::EvalResult result;
2621         if (E->Evaluate(result, S.Context)) {
2622           // Value might be a float, a float vector, or a float complex.
2623           if (IsSameFloatAfterCast(result.Val,
2624                    S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)),
2625                    S.Context.getFloatTypeSemantics(QualType(SourceBT, 0))))
2626             return;
2627         }
2628 
2629         DiagnoseImpCast(S, E, T, diag::warn_impcast_float_precision);
2630       }
2631       return;
2632     }
2633 
2634     // If the target is integral, always warn.
2635     if ((TargetBT && TargetBT->isInteger()))
2636       // TODO: don't warn for integer values?
2637       DiagnoseImpCast(S, E, T, diag::warn_impcast_float_integer);
2638 
2639     return;
2640   }
2641 
2642   if (!Source->isIntegerType() || !Target->isIntegerType())
2643     return;
2644 
2645   IntRange SourceRange = GetExprRange(S.Context, E);
2646   IntRange TargetRange = IntRange::forCanonicalType(S.Context, Target);
2647 
2648   if (SourceRange.Width > TargetRange.Width) {
2649     // People want to build with -Wshorten-64-to-32 and not -Wconversion
2650     // and by god we'll let them.
2651     if (SourceRange.Width == 64 && TargetRange.Width == 32)
2652       return DiagnoseImpCast(S, E, T, diag::warn_impcast_integer_64_32);
2653     return DiagnoseImpCast(S, E, T, diag::warn_impcast_integer_precision);
2654   }
2655 
2656   if ((TargetRange.NonNegative && !SourceRange.NonNegative) ||
2657       (!TargetRange.NonNegative && SourceRange.NonNegative &&
2658        SourceRange.Width == TargetRange.Width)) {
2659     unsigned DiagID = diag::warn_impcast_integer_sign;
2660 
2661     // Traditionally, gcc has warned about this under -Wsign-compare.
2662     // We also want to warn about it in -Wconversion.
2663     // So if -Wconversion is off, use a completely identical diagnostic
2664     // in the sign-compare group.
2665     // The conditional-checking code will
2666     if (ICContext) {
2667       DiagID = diag::warn_impcast_integer_sign_conditional;
2668       *ICContext = true;
2669     }
2670 
2671     return DiagnoseImpCast(S, E, T, DiagID);
2672   }
2673 
2674   return;
2675 }
2676 
2677 void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T);
2678 
2679 void CheckConditionalOperand(Sema &S, Expr *E, QualType T,
2680                              bool &ICContext) {
2681   E = E->IgnoreParenImpCasts();
2682 
2683   if (isa<ConditionalOperator>(E))
2684     return CheckConditionalOperator(S, cast<ConditionalOperator>(E), T);
2685 
2686   AnalyzeImplicitConversions(S, E);
2687   if (E->getType() != T)
2688     return CheckImplicitConversion(S, E, T, &ICContext);
2689   return;
2690 }
2691 
2692 void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T) {
2693   AnalyzeImplicitConversions(S, E->getCond());
2694 
2695   bool Suspicious = false;
2696   CheckConditionalOperand(S, E->getTrueExpr(), T, Suspicious);
2697   CheckConditionalOperand(S, E->getFalseExpr(), T, Suspicious);
2698 
2699   // If -Wconversion would have warned about either of the candidates
2700   // for a signedness conversion to the context type...
2701   if (!Suspicious) return;
2702 
2703   // ...but it's currently ignored...
2704   if (S.Diags.getDiagnosticLevel(diag::warn_impcast_integer_sign_conditional))
2705     return;
2706 
2707   // ...and -Wsign-compare isn't...
2708   if (!S.Diags.getDiagnosticLevel(diag::warn_mixed_sign_conditional))
2709     return;
2710 
2711   // ...then check whether it would have warned about either of the
2712   // candidates for a signedness conversion to the condition type.
2713   if (E->getType() != T) {
2714     Suspicious = false;
2715     CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(),
2716                             E->getType(), &Suspicious);
2717     if (!Suspicious)
2718       CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(),
2719                               E->getType(), &Suspicious);
2720     if (!Suspicious)
2721       return;
2722   }
2723 
2724   // If so, emit a diagnostic under -Wsign-compare.
2725   Expr *lex = E->getTrueExpr()->IgnoreParenImpCasts();
2726   Expr *rex = E->getFalseExpr()->IgnoreParenImpCasts();
2727   S.Diag(E->getQuestionLoc(), diag::warn_mixed_sign_conditional)
2728     << lex->getType() << rex->getType()
2729     << lex->getSourceRange() << rex->getSourceRange();
2730 }
2731 
2732 /// AnalyzeImplicitConversions - Find and report any interesting
2733 /// implicit conversions in the given expression.  There are a couple
2734 /// of competing diagnostics here, -Wconversion and -Wsign-compare.
2735 void AnalyzeImplicitConversions(Sema &S, Expr *OrigE) {
2736   QualType T = OrigE->getType();
2737   Expr *E = OrigE->IgnoreParenImpCasts();
2738 
2739   // For conditional operators, we analyze the arguments as if they
2740   // were being fed directly into the output.
2741   if (isa<ConditionalOperator>(E)) {
2742     ConditionalOperator *CO = cast<ConditionalOperator>(E);
2743     CheckConditionalOperator(S, CO, T);
2744     return;
2745   }
2746 
2747   // Go ahead and check any implicit conversions we might have skipped.
2748   // The non-canonical typecheck is just an optimization;
2749   // CheckImplicitConversion will filter out dead implicit conversions.
2750   if (E->getType() != T)
2751     CheckImplicitConversion(S, E, T);
2752 
2753   // Now continue drilling into this expression.
2754 
2755   // Skip past explicit casts.
2756   if (isa<ExplicitCastExpr>(E)) {
2757     E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts();
2758     return AnalyzeImplicitConversions(S, E);
2759   }
2760 
2761   // Do a somewhat different check with comparison operators.
2762   if (isa<BinaryOperator>(E) && cast<BinaryOperator>(E)->isComparisonOp())
2763     return AnalyzeComparison(S, cast<BinaryOperator>(E));
2764 
2765   // These break the otherwise-useful invariant below.  Fortunately,
2766   // we don't really need to recurse into them, because any internal
2767   // expressions should have been analyzed already when they were
2768   // built into statements.
2769   if (isa<StmtExpr>(E)) return;
2770 
2771   // Don't descend into unevaluated contexts.
2772   if (isa<SizeOfAlignOfExpr>(E)) return;
2773 
2774   // Now just recurse over the expression's children.
2775   for (Stmt::child_iterator I = E->child_begin(), IE = E->child_end();
2776          I != IE; ++I)
2777     AnalyzeImplicitConversions(S, cast<Expr>(*I));
2778 }
2779 
2780 } // end anonymous namespace
2781 
2782 /// Diagnoses "dangerous" implicit conversions within the given
2783 /// expression (which is a full expression).  Implements -Wconversion
2784 /// and -Wsign-compare.
2785 void Sema::CheckImplicitConversions(Expr *E) {
2786   // Don't diagnose in unevaluated contexts.
2787   if (ExprEvalContexts.back().Context == Sema::Unevaluated)
2788     return;
2789 
2790   // Don't diagnose for value- or type-dependent expressions.
2791   if (E->isTypeDependent() || E->isValueDependent())
2792     return;
2793 
2794   AnalyzeImplicitConversions(*this, E);
2795 }
2796 
2797 /// CheckParmsForFunctionDef - Check that the parameters of the given
2798 /// function are appropriate for the definition of a function. This
2799 /// takes care of any checks that cannot be performed on the
2800 /// declaration itself, e.g., that the types of each of the function
2801 /// parameters are complete.
2802 bool Sema::CheckParmsForFunctionDef(FunctionDecl *FD) {
2803   bool HasInvalidParm = false;
2804   for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) {
2805     ParmVarDecl *Param = FD->getParamDecl(p);
2806 
2807     // C99 6.7.5.3p4: the parameters in a parameter type list in a
2808     // function declarator that is part of a function definition of
2809     // that function shall not have incomplete type.
2810     //
2811     // This is also C++ [dcl.fct]p6.
2812     if (!Param->isInvalidDecl() &&
2813         RequireCompleteType(Param->getLocation(), Param->getType(),
2814                                diag::err_typecheck_decl_incomplete_type)) {
2815       Param->setInvalidDecl();
2816       HasInvalidParm = true;
2817     }
2818 
2819     // C99 6.9.1p5: If the declarator includes a parameter type list, the
2820     // declaration of each parameter shall include an identifier.
2821     if (Param->getIdentifier() == 0 &&
2822         !Param->isImplicit() &&
2823         !getLangOptions().CPlusPlus)
2824       Diag(Param->getLocation(), diag::err_parameter_name_omitted);
2825 
2826     // C99 6.7.5.3p12:
2827     //   If the function declarator is not part of a definition of that
2828     //   function, parameters may have incomplete type and may use the [*]
2829     //   notation in their sequences of declarator specifiers to specify
2830     //   variable length array types.
2831     QualType PType = Param->getOriginalType();
2832     if (const ArrayType *AT = Context.getAsArrayType(PType)) {
2833       if (AT->getSizeModifier() == ArrayType::Star) {
2834         // FIXME: This diagnosic should point the the '[*]' if source-location
2835         // information is added for it.
2836         Diag(Param->getLocation(), diag::err_array_star_in_function_definition);
2837       }
2838     }
2839   }
2840 
2841   return HasInvalidParm;
2842 }
2843 
2844 /// CheckCastAlign - Implements -Wcast-align, which warns when a
2845 /// pointer cast increases the alignment requirements.
2846 void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) {
2847   // This is actually a lot of work to potentially be doing on every
2848   // cast; don't do it if we're ignoring -Wcast_align (as is the default).
2849   if (getDiagnostics().getDiagnosticLevel(diag::warn_cast_align)
2850         == Diagnostic::Ignored)
2851     return;
2852 
2853   // Ignore dependent types.
2854   if (T->isDependentType() || Op->getType()->isDependentType())
2855     return;
2856 
2857   // Require that the destination be a pointer type.
2858   const PointerType *DestPtr = T->getAs<PointerType>();
2859   if (!DestPtr) return;
2860 
2861   // If the destination has alignment 1, we're done.
2862   QualType DestPointee = DestPtr->getPointeeType();
2863   if (DestPointee->isIncompleteType()) return;
2864   CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee);
2865   if (DestAlign.isOne()) return;
2866 
2867   // Require that the source be a pointer type.
2868   const PointerType *SrcPtr = Op->getType()->getAs<PointerType>();
2869   if (!SrcPtr) return;
2870   QualType SrcPointee = SrcPtr->getPointeeType();
2871 
2872   // Whitelist casts from cv void*.  We already implicitly
2873   // whitelisted casts to cv void*, since they have alignment 1.
2874   // Also whitelist casts involving incomplete types, which implicitly
2875   // includes 'void'.
2876   if (SrcPointee->isIncompleteType()) return;
2877 
2878   CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee);
2879   if (SrcAlign >= DestAlign) return;
2880 
2881   Diag(TRange.getBegin(), diag::warn_cast_align)
2882     << Op->getType() << T
2883     << static_cast<unsigned>(SrcAlign.getQuantity())
2884     << static_cast<unsigned>(DestAlign.getQuantity())
2885     << TRange << Op->getSourceRange();
2886 }
2887 
2888