blob: a76463fac8fd38338af784b60420baa2d17ddddc [file] [log] [blame]
Chris Lattner59907c42007-08-10 20:18:51 +00001//===--- SemaChecking.cpp - Extra Semantic Checking -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner59907c42007-08-10 20:18:51 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements extra semantic analysis beyond what is enforced
11// by the C type system.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Sema.h"
16#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000017#include "clang/AST/DeclObjC.h"
Ted Kremenek23245122007-08-20 16:18:38 +000018#include "clang/AST/ExprCXX.h"
Ted Kremenek7ff22b22008-06-16 18:00:42 +000019#include "clang/AST/ExprObjC.h"
Chris Lattner719e6152009-02-18 19:21:10 +000020#include "clang/Lex/LiteralSupport.h"
Chris Lattner59907c42007-08-10 20:18:51 +000021#include "clang/Lex/Preprocessor.h"
Chris Lattner59907c42007-08-10 20:18:51 +000022using namespace clang;
23
Chris Lattner60800082009-02-18 17:49:48 +000024/// getLocationOfStringLiteralByte - Return a source location that points to the
25/// specified byte of the specified string literal.
26///
27/// Strings are amazingly complex. They can be formed from multiple tokens and
28/// can have escape sequences in them in addition to the usual trigraph and
29/// escaped newline business. This routine handles this complexity.
30///
31SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL,
32 unsigned ByteNo) const {
33 assert(!SL->isWide() && "This doesn't work for wide strings yet");
34
35 // Loop over all of the tokens in this string until we find the one that
36 // contains the byte we're looking for.
37 unsigned TokNo = 0;
38 while (1) {
39 assert(TokNo < SL->getNumConcatenated() && "Invalid byte number!");
40 SourceLocation StrTokLoc = SL->getStrTokenLoc(TokNo);
41
42 // Get the spelling of the string so that we can get the data that makes up
43 // the string literal, not the identifier for the macro it is potentially
44 // expanded through.
45 SourceLocation StrTokSpellingLoc = SourceMgr.getSpellingLoc(StrTokLoc);
46
47 // Re-lex the token to get its length and original spelling.
48 std::pair<FileID, unsigned> LocInfo =
49 SourceMgr.getDecomposedLoc(StrTokSpellingLoc);
50 std::pair<const char *,const char *> Buffer =
51 SourceMgr.getBufferData(LocInfo.first);
52 const char *StrData = Buffer.first+LocInfo.second;
53
54 // Create a langops struct and enable trigraphs. This is sufficient for
55 // relexing tokens.
56 LangOptions LangOpts;
57 LangOpts.Trigraphs = true;
58
59 // Create a lexer starting at the beginning of this token.
60 Lexer TheLexer(StrTokSpellingLoc, LangOpts, Buffer.first, StrData,
61 Buffer.second);
62 Token TheTok;
63 TheLexer.LexFromRawLexer(TheTok);
64
Chris Lattner443e53c2009-02-18 19:26:42 +000065 // Use the StringLiteralParser to compute the length of the string in bytes.
66 StringLiteralParser SLP(&TheTok, 1, PP);
67 unsigned TokNumBytes = SLP.GetStringLength();
Chris Lattnerd0d082f2009-02-18 18:34:12 +000068
Chris Lattner2197c962009-02-18 18:52:52 +000069 // If the byte is in this token, return the location of the byte.
Chris Lattner60800082009-02-18 17:49:48 +000070 if (ByteNo < TokNumBytes ||
71 (ByteNo == TokNumBytes && TokNo == SL->getNumConcatenated())) {
Chris Lattner719e6152009-02-18 19:21:10 +000072 unsigned Offset =
73 StringLiteralParser::getOffsetOfStringByte(TheTok, ByteNo, PP);
74
75 // Now that we know the offset of the token in the spelling, use the
76 // preprocessor to get the offset in the original source.
77 return PP.AdvanceToTokenCharacter(StrTokLoc, Offset);
Chris Lattner60800082009-02-18 17:49:48 +000078 }
79
80 // Move to the next string token.
81 ++TokNo;
82 ByteNo -= TokNumBytes;
83 }
84}
85
86
Chris Lattner59907c42007-08-10 20:18:51 +000087/// CheckFunctionCall - Check a direct function call for various correctness
88/// and safety properties not strictly enforced by the C type system.
Sebastian Redl0eb23302009-01-19 00:08:26 +000089Action::OwningExprResult
90Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall) {
91 OwningExprResult TheCallResult(Owned(TheCall));
Chris Lattner59907c42007-08-10 20:18:51 +000092 // Get the IdentifierInfo* for the called function.
93 IdentifierInfo *FnInfo = FDecl->getIdentifier();
Douglas Gregor2def4832008-11-17 20:34:05 +000094
95 // None of the checks below are needed for functions that don't have
96 // simple names (e.g., C++ conversion functions).
97 if (!FnInfo)
Sebastian Redl0eb23302009-01-19 00:08:26 +000098 return move(TheCallResult);
Douglas Gregor2def4832008-11-17 20:34:05 +000099
Douglas Gregor3c385e52009-02-14 18:57:46 +0000100 switch (FDecl->getBuiltinID(Context)) {
Chris Lattner30ce3442007-12-19 23:59:04 +0000101 case Builtin::BI__builtin___CFStringMakeConstantString:
Chris Lattner925e60d2007-12-28 05:29:59 +0000102 assert(TheCall->getNumArgs() == 1 &&
Chris Lattner1b9a0792007-12-20 00:26:33 +0000103 "Wrong # arguments to builtin CFStringMakeConstantString");
Chris Lattner69039812009-02-18 06:01:06 +0000104 if (CheckObjCString(TheCall->getArg(0)))
Sebastian Redl0eb23302009-01-19 00:08:26 +0000105 return ExprError();
106 return move(TheCallResult);
Ted Kremenek49ff7a12008-07-09 17:58:53 +0000107 case Builtin::BI__builtin_stdarg_start:
Chris Lattner30ce3442007-12-19 23:59:04 +0000108 case Builtin::BI__builtin_va_start:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000109 if (SemaBuiltinVAStart(TheCall))
110 return ExprError();
111 return move(TheCallResult);
Chris Lattner1b9a0792007-12-20 00:26:33 +0000112 case Builtin::BI__builtin_isgreater:
113 case Builtin::BI__builtin_isgreaterequal:
114 case Builtin::BI__builtin_isless:
115 case Builtin::BI__builtin_islessequal:
116 case Builtin::BI__builtin_islessgreater:
117 case Builtin::BI__builtin_isunordered:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000118 if (SemaBuiltinUnorderedCompare(TheCall))
119 return ExprError();
120 return move(TheCallResult);
Eli Friedman6cfda232008-05-20 08:23:37 +0000121 case Builtin::BI__builtin_return_address:
122 case Builtin::BI__builtin_frame_address:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000123 if (SemaBuiltinStackAddress(TheCall))
124 return ExprError();
125 return move(TheCallResult);
Eli Friedmand38617c2008-05-14 19:38:39 +0000126 case Builtin::BI__builtin_shufflevector:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000127 return SemaBuiltinShuffleVector(TheCall);
128 // TheCall will be freed by the smart pointer here, but that's fine, since
129 // SemaBuiltinShuffleVector guts it, but then doesn't release it.
Daniel Dunbar4493f792008-07-21 22:59:13 +0000130 case Builtin::BI__builtin_prefetch:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000131 if (SemaBuiltinPrefetch(TheCall))
132 return ExprError();
133 return move(TheCallResult);
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000134 case Builtin::BI__builtin_object_size:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000135 if (SemaBuiltinObjectSize(TheCall))
136 return ExprError();
Eli Friedman586d6a82009-05-03 06:04:26 +0000137 return move(TheCallResult);
Eli Friedmand875fed2009-05-03 04:46:36 +0000138 case Builtin::BI__builtin_longjmp:
139 if (SemaBuiltinLongjmp(TheCall))
140 return ExprError();
Eli Friedman586d6a82009-05-03 06:04:26 +0000141 return move(TheCallResult);
Chris Lattner5caa3702009-05-08 06:58:22 +0000142 case Builtin::BI__sync_fetch_and_add:
143 case Builtin::BI__sync_fetch_and_sub:
144 case Builtin::BI__sync_fetch_and_or:
145 case Builtin::BI__sync_fetch_and_and:
146 case Builtin::BI__sync_fetch_and_xor:
Chris Lattnereebd9d22009-05-13 04:37:52 +0000147 case Builtin::BI__sync_fetch_and_nand:
Chris Lattner5caa3702009-05-08 06:58:22 +0000148 case Builtin::BI__sync_add_and_fetch:
149 case Builtin::BI__sync_sub_and_fetch:
150 case Builtin::BI__sync_and_and_fetch:
151 case Builtin::BI__sync_or_and_fetch:
152 case Builtin::BI__sync_xor_and_fetch:
Chris Lattnereebd9d22009-05-13 04:37:52 +0000153 case Builtin::BI__sync_nand_and_fetch:
Chris Lattner5caa3702009-05-08 06:58:22 +0000154 case Builtin::BI__sync_val_compare_and_swap:
155 case Builtin::BI__sync_bool_compare_and_swap:
156 case Builtin::BI__sync_lock_test_and_set:
157 case Builtin::BI__sync_lock_release:
158 if (SemaBuiltinAtomicOverloaded(TheCall))
159 return ExprError();
160 return move(TheCallResult);
Anders Carlsson71993dd2007-08-17 05:31:46 +0000161 }
Daniel Dunbarde454282008-10-02 18:44:07 +0000162
163 // FIXME: This mechanism should be abstracted to be less fragile and
164 // more efficient. For example, just map function ids to custom
165 // handlers.
166
Chris Lattner59907c42007-08-10 20:18:51 +0000167 // Printf checking.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000168 if (const FormatAttr *Format = FDecl->getAttr<FormatAttr>()) {
169 if (Format->getType() == "printf") {
Ted Kremenek3d692df2009-02-27 17:58:43 +0000170 bool HasVAListArg = Format->getFirstArg() == 0;
171 if (!HasVAListArg) {
172 if (const FunctionProtoType *Proto
173 = FDecl->getType()->getAsFunctionProtoType())
Douglas Gregor3c385e52009-02-14 18:57:46 +0000174 HasVAListArg = !Proto->isVariadic();
Ted Kremenek3d692df2009-02-27 17:58:43 +0000175 }
Douglas Gregor3c385e52009-02-14 18:57:46 +0000176 CheckPrintfArguments(TheCall, HasVAListArg, Format->getFormatIdx() - 1,
Ted Kremenek3d692df2009-02-27 17:58:43 +0000177 HasVAListArg ? 0 : Format->getFirstArg() - 1);
Douglas Gregor3c385e52009-02-14 18:57:46 +0000178 }
Chris Lattner59907c42007-08-10 20:18:51 +0000179 }
Sebastian Redl0eb23302009-01-19 00:08:26 +0000180
181 return move(TheCallResult);
Anders Carlsson71993dd2007-08-17 05:31:46 +0000182}
183
Fariborz Jahanian725165f2009-05-18 21:05:18 +0000184Action::OwningExprResult
185Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall) {
186
187 OwningExprResult TheCallResult(Owned(TheCall));
188 // Printf checking.
189 const FormatAttr *Format = NDecl->getAttr<FormatAttr>();
190 if (!Format)
191 return move(TheCallResult);
192 const VarDecl *V = dyn_cast<VarDecl>(NDecl);
193 if (!V)
194 return move(TheCallResult);
195 QualType Ty = V->getType();
196 if (!Ty->isBlockPointerType())
197 return move(TheCallResult);
198 if (Format->getType() == "printf") {
199 bool HasVAListArg = Format->getFirstArg() == 0;
200 if (!HasVAListArg) {
201 const FunctionType *FT =
202 Ty->getAsBlockPointerType()->getPointeeType()->getAsFunctionType();
203 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
204 HasVAListArg = !Proto->isVariadic();
205 }
206 CheckPrintfArguments(TheCall, HasVAListArg, Format->getFormatIdx() - 1,
207 HasVAListArg ? 0 : Format->getFirstArg() - 1);
208 }
209 return move(TheCallResult);
210}
211
Chris Lattner5caa3702009-05-08 06:58:22 +0000212/// SemaBuiltinAtomicOverloaded - We have a call to a function like
213/// __sync_fetch_and_add, which is an overloaded function based on the pointer
214/// type of its first argument. The main ActOnCallExpr routines have already
215/// promoted the types of arguments because all of these calls are prototyped as
216/// void(...).
217///
218/// This function goes through and does final semantic checking for these
219/// builtins,
220bool Sema::SemaBuiltinAtomicOverloaded(CallExpr *TheCall) {
221 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
222 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
223
224 // Ensure that we have at least one argument to do type inference from.
225 if (TheCall->getNumArgs() < 1)
226 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
227 << 0 << TheCall->getCallee()->getSourceRange();
228
229 // Inspect the first argument of the atomic builtin. This should always be
230 // a pointer type, whose element is an integral scalar or pointer type.
231 // Because it is a pointer type, we don't have to worry about any implicit
232 // casts here.
233 Expr *FirstArg = TheCall->getArg(0);
234 if (!FirstArg->getType()->isPointerType())
235 return Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
236 << FirstArg->getType() << FirstArg->getSourceRange();
237
238 QualType ValType = FirstArg->getType()->getAsPointerType()->getPointeeType();
239 if (!ValType->isIntegerType() && !ValType->isPointerType() &&
240 !ValType->isBlockPointerType())
241 return Diag(DRE->getLocStart(),
242 diag::err_atomic_builtin_must_be_pointer_intptr)
243 << FirstArg->getType() << FirstArg->getSourceRange();
244
245 // We need to figure out which concrete builtin this maps onto. For example,
246 // __sync_fetch_and_add with a 2 byte object turns into
247 // __sync_fetch_and_add_2.
248#define BUILTIN_ROW(x) \
249 { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \
250 Builtin::BI##x##_8, Builtin::BI##x##_16 }
251
252 static const unsigned BuiltinIndices[][5] = {
253 BUILTIN_ROW(__sync_fetch_and_add),
254 BUILTIN_ROW(__sync_fetch_and_sub),
255 BUILTIN_ROW(__sync_fetch_and_or),
256 BUILTIN_ROW(__sync_fetch_and_and),
257 BUILTIN_ROW(__sync_fetch_and_xor),
Chris Lattnereebd9d22009-05-13 04:37:52 +0000258 BUILTIN_ROW(__sync_fetch_and_nand),
Chris Lattner5caa3702009-05-08 06:58:22 +0000259
260 BUILTIN_ROW(__sync_add_and_fetch),
261 BUILTIN_ROW(__sync_sub_and_fetch),
262 BUILTIN_ROW(__sync_and_and_fetch),
263 BUILTIN_ROW(__sync_or_and_fetch),
264 BUILTIN_ROW(__sync_xor_and_fetch),
Chris Lattnereebd9d22009-05-13 04:37:52 +0000265 BUILTIN_ROW(__sync_nand_and_fetch),
Chris Lattner5caa3702009-05-08 06:58:22 +0000266
267 BUILTIN_ROW(__sync_val_compare_and_swap),
268 BUILTIN_ROW(__sync_bool_compare_and_swap),
269 BUILTIN_ROW(__sync_lock_test_and_set),
270 BUILTIN_ROW(__sync_lock_release)
271 };
272#undef BUILTIN_ROW
273
274 // Determine the index of the size.
275 unsigned SizeIndex;
276 switch (Context.getTypeSize(ValType)/8) {
277 case 1: SizeIndex = 0; break;
278 case 2: SizeIndex = 1; break;
279 case 4: SizeIndex = 2; break;
280 case 8: SizeIndex = 3; break;
281 case 16: SizeIndex = 4; break;
282 default:
283 return Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size)
284 << FirstArg->getType() << FirstArg->getSourceRange();
285 }
286
287 // Each of these builtins has one pointer argument, followed by some number of
288 // values (0, 1 or 2) followed by a potentially empty varags list of stuff
289 // that we ignore. Find out which row of BuiltinIndices to read from as well
290 // as the number of fixed args.
291 unsigned BuiltinID = FDecl->getBuiltinID(Context);
292 unsigned BuiltinIndex, NumFixed = 1;
293 switch (BuiltinID) {
294 default: assert(0 && "Unknown overloaded atomic builtin!");
295 case Builtin::BI__sync_fetch_and_add: BuiltinIndex = 0; break;
296 case Builtin::BI__sync_fetch_and_sub: BuiltinIndex = 1; break;
297 case Builtin::BI__sync_fetch_and_or: BuiltinIndex = 2; break;
298 case Builtin::BI__sync_fetch_and_and: BuiltinIndex = 3; break;
299 case Builtin::BI__sync_fetch_and_xor: BuiltinIndex = 4; break;
Chris Lattnereebd9d22009-05-13 04:37:52 +0000300 case Builtin::BI__sync_fetch_and_nand:BuiltinIndex = 5; break;
Chris Lattner5caa3702009-05-08 06:58:22 +0000301
Chris Lattnereebd9d22009-05-13 04:37:52 +0000302 case Builtin::BI__sync_add_and_fetch: BuiltinIndex = 6; break;
303 case Builtin::BI__sync_sub_and_fetch: BuiltinIndex = 7; break;
304 case Builtin::BI__sync_and_and_fetch: BuiltinIndex = 8; break;
305 case Builtin::BI__sync_or_and_fetch: BuiltinIndex = 9; break;
306 case Builtin::BI__sync_xor_and_fetch: BuiltinIndex =10; break;
307 case Builtin::BI__sync_nand_and_fetch:BuiltinIndex =11; break;
Chris Lattner5caa3702009-05-08 06:58:22 +0000308
309 case Builtin::BI__sync_val_compare_and_swap:
Chris Lattnereebd9d22009-05-13 04:37:52 +0000310 BuiltinIndex = 12;
Chris Lattner5caa3702009-05-08 06:58:22 +0000311 NumFixed = 2;
312 break;
313 case Builtin::BI__sync_bool_compare_and_swap:
Chris Lattnereebd9d22009-05-13 04:37:52 +0000314 BuiltinIndex = 13;
Chris Lattner5caa3702009-05-08 06:58:22 +0000315 NumFixed = 2;
316 break;
Chris Lattnereebd9d22009-05-13 04:37:52 +0000317 case Builtin::BI__sync_lock_test_and_set: BuiltinIndex = 14; break;
Chris Lattner5caa3702009-05-08 06:58:22 +0000318 case Builtin::BI__sync_lock_release:
Chris Lattnereebd9d22009-05-13 04:37:52 +0000319 BuiltinIndex = 15;
Chris Lattner5caa3702009-05-08 06:58:22 +0000320 NumFixed = 0;
321 break;
322 }
323
324 // Now that we know how many fixed arguments we expect, first check that we
325 // have at least that many.
326 if (TheCall->getNumArgs() < 1+NumFixed)
327 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
328 << 0 << TheCall->getCallee()->getSourceRange();
329
Chris Lattnere7ac0a92009-05-08 15:36:58 +0000330
331 // Get the decl for the concrete builtin from this, we can tell what the
332 // concrete integer type we should convert to is.
333 unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex];
334 const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID);
335 IdentifierInfo *NewBuiltinII = PP.getIdentifierInfo(NewBuiltinName);
336 FunctionDecl *NewBuiltinDecl =
337 cast<FunctionDecl>(LazilyCreateBuiltin(NewBuiltinII, NewBuiltinID,
338 TUScope, false, DRE->getLocStart()));
339 const FunctionProtoType *BuiltinFT =
340 NewBuiltinDecl->getType()->getAsFunctionProtoType();
341 ValType = BuiltinFT->getArgType(0)->getAsPointerType()->getPointeeType();
342
343 // If the first type needs to be converted (e.g. void** -> int*), do it now.
344 if (BuiltinFT->getArgType(0) != FirstArg->getType()) {
345 ImpCastExprToType(FirstArg, BuiltinFT->getArgType(0), false);
346 TheCall->setArg(0, FirstArg);
347 }
348
Chris Lattner5caa3702009-05-08 06:58:22 +0000349 // Next, walk the valid ones promoting to the right type.
350 for (unsigned i = 0; i != NumFixed; ++i) {
351 Expr *Arg = TheCall->getArg(i+1);
352
353 // If the argument is an implicit cast, then there was a promotion due to
354 // "...", just remove it now.
355 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
356 Arg = ICE->getSubExpr();
357 ICE->setSubExpr(0);
358 ICE->Destroy(Context);
359 TheCall->setArg(i+1, Arg);
360 }
361
362 // GCC does an implicit conversion to the pointer or integer ValType. This
363 // can fail in some cases (1i -> int**), check for this error case now.
364 if (CheckCastTypes(Arg->getSourceRange(), ValType, Arg))
365 return true;
366
367 // Okay, we have something that *can* be converted to the right type. Check
368 // to see if there is a potentially weird extension going on here. This can
369 // happen when you do an atomic operation on something like an char* and
370 // pass in 42. The 42 gets converted to char. This is even more strange
371 // for things like 45.123 -> char, etc.
372 // FIXME: Do this check.
373 ImpCastExprToType(Arg, ValType, false);
374 TheCall->setArg(i+1, Arg);
375 }
376
Chris Lattner5caa3702009-05-08 06:58:22 +0000377 // Switch the DeclRefExpr to refer to the new decl.
378 DRE->setDecl(NewBuiltinDecl);
379 DRE->setType(NewBuiltinDecl->getType());
380
381 // Set the callee in the CallExpr.
382 // FIXME: This leaks the original parens and implicit casts.
383 Expr *PromotedCall = DRE;
384 UsualUnaryConversions(PromotedCall);
385 TheCall->setCallee(PromotedCall);
386
387
388 // Change the result type of the call to match the result type of the decl.
389 TheCall->setType(NewBuiltinDecl->getResultType());
390 return false;
391}
392
393
Chris Lattner69039812009-02-18 06:01:06 +0000394/// CheckObjCString - Checks that the argument to the builtin
Anders Carlsson71993dd2007-08-17 05:31:46 +0000395/// CFString constructor is correct
Steve Narofffd942622009-04-13 20:26:29 +0000396/// FIXME: GCC currently emits the following warning:
397/// "warning: input conversion stopped due to an input byte that does not
398/// belong to the input codeset UTF-8"
399/// Note: It might also make sense to do the UTF-16 conversion here (would
400/// simplify the backend).
Chris Lattner69039812009-02-18 06:01:06 +0000401bool Sema::CheckObjCString(Expr *Arg) {
Chris Lattner56f34942008-02-13 01:02:39 +0000402 Arg = Arg->IgnoreParenCasts();
Anders Carlsson71993dd2007-08-17 05:31:46 +0000403 StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
404
405 if (!Literal || Literal->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000406 Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant)
407 << Arg->getSourceRange();
Anders Carlsson9cdc4d32007-08-17 15:44:17 +0000408 return true;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000409 }
410
411 const char *Data = Literal->getStrData();
412 unsigned Length = Literal->getByteLength();
413
414 for (unsigned i = 0; i < Length; ++i) {
Anders Carlsson71993dd2007-08-17 05:31:46 +0000415 if (!Data[i]) {
Chris Lattner60800082009-02-18 17:49:48 +0000416 Diag(getLocationOfStringLiteralByte(Literal, i),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000417 diag::warn_cfstring_literal_contains_nul_character)
418 << Arg->getSourceRange();
Anders Carlsson71993dd2007-08-17 05:31:46 +0000419 break;
420 }
421 }
422
Anders Carlsson9cdc4d32007-08-17 15:44:17 +0000423 return false;
Chris Lattner59907c42007-08-10 20:18:51 +0000424}
425
Chris Lattnerc27c6652007-12-20 00:05:45 +0000426/// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity.
427/// Emit an error and return true on failure, return false on success.
Chris Lattner925e60d2007-12-28 05:29:59 +0000428bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) {
429 Expr *Fn = TheCall->getCallee();
430 if (TheCall->getNumArgs() > 2) {
Chris Lattner2c21a072008-11-21 18:44:24 +0000431 Diag(TheCall->getArg(2)->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000432 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000433 << 0 /*function call*/ << Fn->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000434 << SourceRange(TheCall->getArg(2)->getLocStart(),
435 (*(TheCall->arg_end()-1))->getLocEnd());
Chris Lattner30ce3442007-12-19 23:59:04 +0000436 return true;
437 }
Eli Friedman56f20ae2008-12-15 22:05:35 +0000438
439 if (TheCall->getNumArgs() < 2) {
440 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
441 << 0 /*function call*/;
442 }
443
Chris Lattnerc27c6652007-12-20 00:05:45 +0000444 // Determine whether the current function is variadic or not.
445 bool isVariadic;
Steve Naroffcd9c5142009-04-15 19:33:47 +0000446 if (CurBlock)
447 isVariadic = CurBlock->isVariadic;
448 else if (getCurFunctionDecl()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000449 if (FunctionProtoType* FTP =
450 dyn_cast<FunctionProtoType>(getCurFunctionDecl()->getType()))
Eli Friedman56f20ae2008-12-15 22:05:35 +0000451 isVariadic = FTP->isVariadic();
452 else
453 isVariadic = false;
454 } else {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000455 isVariadic = getCurMethodDecl()->isVariadic();
Eli Friedman56f20ae2008-12-15 22:05:35 +0000456 }
Chris Lattner30ce3442007-12-19 23:59:04 +0000457
Chris Lattnerc27c6652007-12-20 00:05:45 +0000458 if (!isVariadic) {
Chris Lattner30ce3442007-12-19 23:59:04 +0000459 Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function);
460 return true;
461 }
462
463 // Verify that the second argument to the builtin is the last argument of the
464 // current function or method.
465 bool SecondArgIsLastNamedArgument = false;
Anders Carlssone2c14102008-02-13 01:22:59 +0000466 const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts();
Anders Carlsson88cf2262008-02-11 04:20:54 +0000467
468 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) {
469 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
Chris Lattner30ce3442007-12-19 23:59:04 +0000470 // FIXME: This isn't correct for methods (results in bogus warning).
471 // Get the last formal in the current function.
Anders Carlsson88cf2262008-02-11 04:20:54 +0000472 const ParmVarDecl *LastArg;
Steve Naroffcd9c5142009-04-15 19:33:47 +0000473 if (CurBlock)
474 LastArg = *(CurBlock->TheDecl->param_end()-1);
475 else if (FunctionDecl *FD = getCurFunctionDecl())
Chris Lattner371f2582008-12-04 23:50:19 +0000476 LastArg = *(FD->param_end()-1);
Chris Lattner30ce3442007-12-19 23:59:04 +0000477 else
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000478 LastArg = *(getCurMethodDecl()->param_end()-1);
Chris Lattner30ce3442007-12-19 23:59:04 +0000479 SecondArgIsLastNamedArgument = PV == LastArg;
480 }
481 }
482
483 if (!SecondArgIsLastNamedArgument)
Chris Lattner925e60d2007-12-28 05:29:59 +0000484 Diag(TheCall->getArg(1)->getLocStart(),
Chris Lattner30ce3442007-12-19 23:59:04 +0000485 diag::warn_second_parameter_of_va_start_not_last_named_argument);
486 return false;
Eli Friedman6cfda232008-05-20 08:23:37 +0000487}
Chris Lattner30ce3442007-12-19 23:59:04 +0000488
Chris Lattner1b9a0792007-12-20 00:26:33 +0000489/// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and
490/// friends. This is declared to take (...), so we have to check everything.
Chris Lattner925e60d2007-12-28 05:29:59 +0000491bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) {
492 if (TheCall->getNumArgs() < 2)
Chris Lattner2c21a072008-11-21 18:44:24 +0000493 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
494 << 0 /*function call*/;
Chris Lattner925e60d2007-12-28 05:29:59 +0000495 if (TheCall->getNumArgs() > 2)
496 return Diag(TheCall->getArg(2)->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000497 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000498 << 0 /*function call*/
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000499 << SourceRange(TheCall->getArg(2)->getLocStart(),
500 (*(TheCall->arg_end()-1))->getLocEnd());
Chris Lattner1b9a0792007-12-20 00:26:33 +0000501
Chris Lattner925e60d2007-12-28 05:29:59 +0000502 Expr *OrigArg0 = TheCall->getArg(0);
503 Expr *OrigArg1 = TheCall->getArg(1);
Chris Lattner1b9a0792007-12-20 00:26:33 +0000504
505 // Do standard promotions between the two arguments, returning their common
506 // type.
Chris Lattner925e60d2007-12-28 05:29:59 +0000507 QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false);
Daniel Dunbar403bc2b2009-02-19 19:28:43 +0000508
509 // Make sure any conversions are pushed back into the call; this is
510 // type safe since unordered compare builtins are declared as "_Bool
511 // foo(...)".
512 TheCall->setArg(0, OrigArg0);
513 TheCall->setArg(1, OrigArg1);
Chris Lattner1b9a0792007-12-20 00:26:33 +0000514
515 // If the common type isn't a real floating type, then the arguments were
516 // invalid for this operation.
517 if (!Res->isRealFloatingType())
Chris Lattner925e60d2007-12-28 05:29:59 +0000518 return Diag(OrigArg0->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000519 diag::err_typecheck_call_invalid_ordered_compare)
Chris Lattnerd1625842008-11-24 06:25:27 +0000520 << OrigArg0->getType() << OrigArg1->getType()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000521 << SourceRange(OrigArg0->getLocStart(), OrigArg1->getLocEnd());
Chris Lattner1b9a0792007-12-20 00:26:33 +0000522
523 return false;
524}
525
Eli Friedman6cfda232008-05-20 08:23:37 +0000526bool Sema::SemaBuiltinStackAddress(CallExpr *TheCall) {
527 // The signature for these builtins is exact; the only thing we need
528 // to check is that the argument is a constant.
529 SourceLocation Loc;
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000530 if (!TheCall->getArg(0)->isIntegerConstantExpr(Context, &Loc))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000531 return Diag(Loc, diag::err_stack_const_level) << TheCall->getSourceRange();
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000532
Eli Friedman6cfda232008-05-20 08:23:37 +0000533 return false;
534}
535
Eli Friedmand38617c2008-05-14 19:38:39 +0000536/// SemaBuiltinShuffleVector - Handle __builtin_shufflevector.
537// This is declared to take (...), so we have to check everything.
Sebastian Redl0eb23302009-01-19 00:08:26 +0000538Action::OwningExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) {
Eli Friedmand38617c2008-05-14 19:38:39 +0000539 if (TheCall->getNumArgs() < 3)
Sebastian Redl0eb23302009-01-19 00:08:26 +0000540 return ExprError(Diag(TheCall->getLocEnd(),
541 diag::err_typecheck_call_too_few_args)
542 << 0 /*function call*/ << TheCall->getSourceRange());
Eli Friedmand38617c2008-05-14 19:38:39 +0000543
544 QualType FAType = TheCall->getArg(0)->getType();
545 QualType SAType = TheCall->getArg(1)->getType();
546
547 if (!FAType->isVectorType() || !SAType->isVectorType()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000548 Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector)
549 << SourceRange(TheCall->getArg(0)->getLocStart(),
550 TheCall->getArg(1)->getLocEnd());
Sebastian Redl0eb23302009-01-19 00:08:26 +0000551 return ExprError();
Eli Friedmand38617c2008-05-14 19:38:39 +0000552 }
553
Chris Lattnerb77792e2008-07-26 22:17:49 +0000554 if (Context.getCanonicalType(FAType).getUnqualifiedType() !=
555 Context.getCanonicalType(SAType).getUnqualifiedType()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000556 Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector)
557 << SourceRange(TheCall->getArg(0)->getLocStart(),
558 TheCall->getArg(1)->getLocEnd());
Sebastian Redl0eb23302009-01-19 00:08:26 +0000559 return ExprError();
Eli Friedmand38617c2008-05-14 19:38:39 +0000560 }
561
562 unsigned numElements = FAType->getAsVectorType()->getNumElements();
563 if (TheCall->getNumArgs() != numElements+2) {
564 if (TheCall->getNumArgs() < numElements+2)
Sebastian Redl0eb23302009-01-19 00:08:26 +0000565 return ExprError(Diag(TheCall->getLocEnd(),
566 diag::err_typecheck_call_too_few_args)
567 << 0 /*function call*/ << TheCall->getSourceRange());
568 return ExprError(Diag(TheCall->getLocEnd(),
569 diag::err_typecheck_call_too_many_args)
570 << 0 /*function call*/ << TheCall->getSourceRange());
Eli Friedmand38617c2008-05-14 19:38:39 +0000571 }
572
573 for (unsigned i = 2; i < TheCall->getNumArgs(); i++) {
574 llvm::APSInt Result(32);
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000575 if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context))
Sebastian Redl0eb23302009-01-19 00:08:26 +0000576 return ExprError(Diag(TheCall->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000577 diag::err_shufflevector_nonconstant_argument)
Sebastian Redl0eb23302009-01-19 00:08:26 +0000578 << TheCall->getArg(i)->getSourceRange());
579
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000580 if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2)
Sebastian Redl0eb23302009-01-19 00:08:26 +0000581 return ExprError(Diag(TheCall->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000582 diag::err_shufflevector_argument_too_large)
Sebastian Redl0eb23302009-01-19 00:08:26 +0000583 << TheCall->getArg(i)->getSourceRange());
Eli Friedmand38617c2008-05-14 19:38:39 +0000584 }
585
586 llvm::SmallVector<Expr*, 32> exprs;
587
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000588 for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) {
Eli Friedmand38617c2008-05-14 19:38:39 +0000589 exprs.push_back(TheCall->getArg(i));
590 TheCall->setArg(i, 0);
591 }
592
Ted Kremenek8189cde2009-02-07 01:47:29 +0000593 return Owned(new (Context) ShuffleVectorExpr(exprs.begin(), numElements+2,
594 FAType,
595 TheCall->getCallee()->getLocStart(),
596 TheCall->getRParenLoc()));
Eli Friedmand38617c2008-05-14 19:38:39 +0000597}
Chris Lattner30ce3442007-12-19 23:59:04 +0000598
Daniel Dunbar4493f792008-07-21 22:59:13 +0000599/// SemaBuiltinPrefetch - Handle __builtin_prefetch.
600// This is declared to take (const void*, ...) and can take two
601// optional constant int args.
602bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000603 unsigned NumArgs = TheCall->getNumArgs();
Daniel Dunbar4493f792008-07-21 22:59:13 +0000604
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000605 if (NumArgs > 3)
606 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000607 << 0 /*function call*/ << TheCall->getSourceRange();
Daniel Dunbar4493f792008-07-21 22:59:13 +0000608
609 // Argument 0 is checked for us and the remaining arguments must be
610 // constant integers.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000611 for (unsigned i = 1; i != NumArgs; ++i) {
Daniel Dunbar4493f792008-07-21 22:59:13 +0000612 Expr *Arg = TheCall->getArg(i);
613 QualType RWType = Arg->getType();
614
615 const BuiltinType *BT = RWType->getAsBuiltinType();
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000616 llvm::APSInt Result;
Daniel Dunbar4493f792008-07-21 22:59:13 +0000617 if (!BT || BT->getKind() != BuiltinType::Int ||
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000618 !Arg->isIntegerConstantExpr(Result, Context))
619 return Diag(TheCall->getLocStart(), diag::err_prefetch_invalid_argument)
620 << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
Daniel Dunbar4493f792008-07-21 22:59:13 +0000621
622 // FIXME: gcc issues a warning and rewrites these to 0. These
623 // seems especially odd for the third argument since the default
624 // is 3.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000625 if (i == 1) {
Daniel Dunbar4493f792008-07-21 22:59:13 +0000626 if (Result.getSExtValue() < 0 || Result.getSExtValue() > 1)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000627 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
628 << "0" << "1" << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
Daniel Dunbar4493f792008-07-21 22:59:13 +0000629 } else {
630 if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000631 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
632 << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
Daniel Dunbar4493f792008-07-21 22:59:13 +0000633 }
634 }
635
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000636 return false;
Daniel Dunbar4493f792008-07-21 22:59:13 +0000637}
638
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000639/// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr,
640/// int type). This simply type checks that type is one of the defined
641/// constants (0-3).
642bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) {
643 Expr *Arg = TheCall->getArg(1);
644 QualType ArgType = Arg->getType();
645 const BuiltinType *BT = ArgType->getAsBuiltinType();
646 llvm::APSInt Result(32);
647 if (!BT || BT->getKind() != BuiltinType::Int ||
648 !Arg->isIntegerConstantExpr(Result, Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000649 return Diag(TheCall->getLocStart(), diag::err_object_size_invalid_argument)
650 << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000651 }
652
653 if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000654 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
655 << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000656 }
657
658 return false;
659}
660
Eli Friedman586d6a82009-05-03 06:04:26 +0000661/// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val).
Eli Friedmand875fed2009-05-03 04:46:36 +0000662/// This checks that val is a constant 1.
663bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) {
664 Expr *Arg = TheCall->getArg(1);
665 llvm::APSInt Result(32);
666 if (!Arg->isIntegerConstantExpr(Result, Context) || Result != 1)
667 return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val)
668 << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
669
670 return false;
671}
672
Ted Kremenekd30ef872009-01-12 23:09:09 +0000673// Handle i > 1 ? "x" : "y", recursivelly
Ted Kremenek082d9362009-03-20 21:35:28 +0000674bool Sema::SemaCheckStringLiteral(const Expr *E, const CallExpr *TheCall,
675 bool HasVAListArg,
Douglas Gregor3c385e52009-02-14 18:57:46 +0000676 unsigned format_idx, unsigned firstDataArg) {
Ted Kremenekd30ef872009-01-12 23:09:09 +0000677
678 switch (E->getStmtClass()) {
679 case Stmt::ConditionalOperatorClass: {
Ted Kremenek082d9362009-03-20 21:35:28 +0000680 const ConditionalOperator *C = cast<ConditionalOperator>(E);
Ted Kremenekd30ef872009-01-12 23:09:09 +0000681 return SemaCheckStringLiteral(C->getLHS(), TheCall,
Douglas Gregor3c385e52009-02-14 18:57:46 +0000682 HasVAListArg, format_idx, firstDataArg)
Ted Kremenekd30ef872009-01-12 23:09:09 +0000683 && SemaCheckStringLiteral(C->getRHS(), TheCall,
Douglas Gregor3c385e52009-02-14 18:57:46 +0000684 HasVAListArg, format_idx, firstDataArg);
Ted Kremenekd30ef872009-01-12 23:09:09 +0000685 }
686
687 case Stmt::ImplicitCastExprClass: {
Ted Kremenek082d9362009-03-20 21:35:28 +0000688 const ImplicitCastExpr *Expr = cast<ImplicitCastExpr>(E);
Ted Kremenekd30ef872009-01-12 23:09:09 +0000689 return SemaCheckStringLiteral(Expr->getSubExpr(), TheCall, HasVAListArg,
Douglas Gregor3c385e52009-02-14 18:57:46 +0000690 format_idx, firstDataArg);
Ted Kremenekd30ef872009-01-12 23:09:09 +0000691 }
692
693 case Stmt::ParenExprClass: {
Ted Kremenek082d9362009-03-20 21:35:28 +0000694 const ParenExpr *Expr = cast<ParenExpr>(E);
Ted Kremenekd30ef872009-01-12 23:09:09 +0000695 return SemaCheckStringLiteral(Expr->getSubExpr(), TheCall, HasVAListArg,
Douglas Gregor3c385e52009-02-14 18:57:46 +0000696 format_idx, firstDataArg);
Ted Kremenekd30ef872009-01-12 23:09:09 +0000697 }
Ted Kremenek082d9362009-03-20 21:35:28 +0000698
699 case Stmt::DeclRefExprClass: {
700 const DeclRefExpr *DR = cast<DeclRefExpr>(E);
701
702 // As an exception, do not flag errors for variables binding to
703 // const string literals.
704 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
705 bool isConstant = false;
706 QualType T = DR->getType();
Ted Kremenekd30ef872009-01-12 23:09:09 +0000707
Ted Kremenek082d9362009-03-20 21:35:28 +0000708 if (const ArrayType *AT = Context.getAsArrayType(T)) {
709 isConstant = AT->getElementType().isConstant(Context);
710 }
711 else if (const PointerType *PT = T->getAsPointerType()) {
712 isConstant = T.isConstant(Context) &&
713 PT->getPointeeType().isConstant(Context);
714 }
715
716 if (isConstant) {
717 const VarDecl *Def = 0;
718 if (const Expr *Init = VD->getDefinition(Def))
719 return SemaCheckStringLiteral(Init, TheCall,
720 HasVAListArg, format_idx, firstDataArg);
721 }
722 }
723
724 return false;
725 }
Ted Kremenekd30ef872009-01-12 23:09:09 +0000726
Ted Kremenek082d9362009-03-20 21:35:28 +0000727 case Stmt::ObjCStringLiteralClass:
728 case Stmt::StringLiteralClass: {
729 const StringLiteral *StrE = NULL;
730
731 if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E))
Ted Kremenekd30ef872009-01-12 23:09:09 +0000732 StrE = ObjCFExpr->getString();
733 else
Ted Kremenek082d9362009-03-20 21:35:28 +0000734 StrE = cast<StringLiteral>(E);
735
Ted Kremenekd30ef872009-01-12 23:09:09 +0000736 if (StrE) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000737 CheckPrintfString(StrE, E, TheCall, HasVAListArg, format_idx,
738 firstDataArg);
Ted Kremenekd30ef872009-01-12 23:09:09 +0000739 return true;
740 }
741
742 return false;
743 }
Ted Kremenek082d9362009-03-20 21:35:28 +0000744
745 default:
746 return false;
Ted Kremenekd30ef872009-01-12 23:09:09 +0000747 }
748}
749
750
Chris Lattner59907c42007-08-10 20:18:51 +0000751/// CheckPrintfArguments - Check calls to printf (and similar functions) for
Ted Kremenek71895b92007-08-14 17:39:48 +0000752/// correct use of format strings.
753///
754/// HasVAListArg - A predicate indicating whether the printf-like
755/// function is passed an explicit va_arg argument (e.g., vprintf)
756///
757/// format_idx - The index into Args for the format string.
758///
759/// Improper format strings to functions in the printf family can be
760/// the source of bizarre bugs and very serious security holes. A
761/// good source of information is available in the following paper
762/// (which includes additional references):
Chris Lattner59907c42007-08-10 20:18:51 +0000763///
764/// FormatGuard: Automatic Protection From printf Format String
765/// Vulnerabilities, Proceedings of the 10th USENIX Security Symposium, 2001.
Ted Kremenek71895b92007-08-14 17:39:48 +0000766///
767/// Functionality implemented:
768///
769/// We can statically check the following properties for string
770/// literal format strings for non v.*printf functions (where the
771/// arguments are passed directly):
772//
773/// (1) Are the number of format conversions equal to the number of
774/// data arguments?
775///
776/// (2) Does each format conversion correctly match the type of the
777/// corresponding data argument? (TODO)
778///
779/// Moreover, for all printf functions we can:
780///
781/// (3) Check for a missing format string (when not caught by type checking).
782///
783/// (4) Check for no-operation flags; e.g. using "#" with format
784/// conversion 'c' (TODO)
785///
786/// (5) Check the use of '%n', a major source of security holes.
787///
788/// (6) Check for malformed format conversions that don't specify anything.
789///
790/// (7) Check for empty format strings. e.g: printf("");
791///
792/// (8) Check that the format string is a wide literal.
793///
Ted Kremenek6d439592008-03-03 16:50:00 +0000794/// (9) Also check the arguments of functions with the __format__ attribute.
795/// (TODO).
796///
Ted Kremenek71895b92007-08-14 17:39:48 +0000797/// All of these checks can be done by parsing the format string.
798///
799/// For now, we ONLY do (1), (3), (5), (6), (7), and (8).
Chris Lattner59907c42007-08-10 20:18:51 +0000800void
Ted Kremenek082d9362009-03-20 21:35:28 +0000801Sema::CheckPrintfArguments(const CallExpr *TheCall, bool HasVAListArg,
Douglas Gregor3c385e52009-02-14 18:57:46 +0000802 unsigned format_idx, unsigned firstDataArg) {
Ted Kremenek082d9362009-03-20 21:35:28 +0000803 const Expr *Fn = TheCall->getCallee();
Chris Lattner925e60d2007-12-28 05:29:59 +0000804
Ted Kremenek71895b92007-08-14 17:39:48 +0000805 // CHECK: printf-like function is called with no format string.
Chris Lattner925e60d2007-12-28 05:29:59 +0000806 if (format_idx >= TheCall->getNumArgs()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000807 Diag(TheCall->getRParenLoc(), diag::warn_printf_missing_format_string)
808 << Fn->getSourceRange();
Ted Kremenek71895b92007-08-14 17:39:48 +0000809 return;
810 }
811
Ted Kremenek082d9362009-03-20 21:35:28 +0000812 const Expr *OrigFormatExpr = TheCall->getArg(format_idx)->IgnoreParenCasts();
Chris Lattner459e8482007-08-25 05:36:18 +0000813
Chris Lattner59907c42007-08-10 20:18:51 +0000814 // CHECK: format string is not a string literal.
815 //
Ted Kremenek71895b92007-08-14 17:39:48 +0000816 // Dynamically generated format strings are difficult to
817 // automatically vet at compile time. Requiring that format strings
818 // are string literals: (1) permits the checking of format strings by
819 // the compiler and thereby (2) can practically remove the source of
820 // many format string exploits.
Ted Kremenek7ff22b22008-06-16 18:00:42 +0000821
822 // Format string can be either ObjC string (e.g. @"%d") or
823 // C string (e.g. "%d")
824 // ObjC string uses the same format specifiers as C string, so we can use
825 // the same format string checking logic for both ObjC and C strings.
Chris Lattner1cd3e1f2009-04-29 04:49:34 +0000826 if (SemaCheckStringLiteral(OrigFormatExpr, TheCall, HasVAListArg, format_idx,
827 firstDataArg))
828 return; // Literal format string found, check done!
Ted Kremenek7ff22b22008-06-16 18:00:42 +0000829
Chris Lattner1cd3e1f2009-04-29 04:49:34 +0000830 // For vprintf* functions (i.e., HasVAListArg==true), we add a
831 // special check to see if the format string is a function parameter
832 // of the function calling the printf function. If the function
833 // has an attribute indicating it is a printf-like function, then we
834 // should suppress warnings concerning non-literals being used in a call
835 // to a vprintf function. For example:
836 //
837 // void
838 // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...) {
839 // va_list ap;
840 // va_start(ap, fmt);
841 // vprintf(fmt, ap); // Do NOT emit a warning about "fmt".
842 // ...
843 //
844 //
845 // FIXME: We don't have full attribute support yet, so just check to see
846 // if the argument is a DeclRefExpr that references a parameter. We'll
847 // add proper support for checking the attribute later.
848 if (HasVAListArg)
849 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(OrigFormatExpr))
850 if (isa<ParmVarDecl>(DR->getDecl()))
851 return;
Ted Kremenekd30ef872009-01-12 23:09:09 +0000852
Chris Lattner655f1412009-04-29 04:59:47 +0000853 // If there are no arguments specified, warn with -Wformat-security, otherwise
854 // warn only with -Wformat-nonliteral.
855 if (TheCall->getNumArgs() == format_idx+1)
856 Diag(TheCall->getArg(format_idx)->getLocStart(),
857 diag::warn_printf_nonliteral_noargs)
858 << OrigFormatExpr->getSourceRange();
859 else
860 Diag(TheCall->getArg(format_idx)->getLocStart(),
861 diag::warn_printf_nonliteral)
862 << OrigFormatExpr->getSourceRange();
Ted Kremenekd30ef872009-01-12 23:09:09 +0000863}
Ted Kremenek71895b92007-08-14 17:39:48 +0000864
Ted Kremenek082d9362009-03-20 21:35:28 +0000865void Sema::CheckPrintfString(const StringLiteral *FExpr,
866 const Expr *OrigFormatExpr,
867 const CallExpr *TheCall, bool HasVAListArg,
868 unsigned format_idx, unsigned firstDataArg) {
Ted Kremenekd30ef872009-01-12 23:09:09 +0000869
Ted Kremenek082d9362009-03-20 21:35:28 +0000870 const ObjCStringLiteral *ObjCFExpr =
871 dyn_cast<ObjCStringLiteral>(OrigFormatExpr);
872
Ted Kremenek71895b92007-08-14 17:39:48 +0000873 // CHECK: is the format string a wide literal?
874 if (FExpr->isWide()) {
Chris Lattner925e60d2007-12-28 05:29:59 +0000875 Diag(FExpr->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000876 diag::warn_printf_format_string_is_wide_literal)
877 << OrigFormatExpr->getSourceRange();
Ted Kremenek71895b92007-08-14 17:39:48 +0000878 return;
879 }
880
881 // Str - The format string. NOTE: this is NOT null-terminated!
Chris Lattnerb9fc8562009-04-29 04:12:34 +0000882 const char *Str = FExpr->getStrData();
Ted Kremenek71895b92007-08-14 17:39:48 +0000883
884 // CHECK: empty format string?
Chris Lattnerb9fc8562009-04-29 04:12:34 +0000885 unsigned StrLen = FExpr->getByteLength();
Ted Kremenek71895b92007-08-14 17:39:48 +0000886
887 if (StrLen == 0) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000888 Diag(FExpr->getLocStart(), diag::warn_printf_empty_format_string)
889 << OrigFormatExpr->getSourceRange();
Ted Kremenek71895b92007-08-14 17:39:48 +0000890 return;
891 }
892
893 // We process the format string using a binary state machine. The
894 // current state is stored in CurrentState.
895 enum {
896 state_OrdChr,
897 state_Conversion
898 } CurrentState = state_OrdChr;
899
900 // numConversions - The number of conversions seen so far. This is
901 // incremented as we traverse the format string.
902 unsigned numConversions = 0;
903
904 // numDataArgs - The number of data arguments after the format
905 // string. This can only be determined for non vprintf-like
906 // functions. For those functions, this value is 1 (the sole
907 // va_arg argument).
Douglas Gregor3c385e52009-02-14 18:57:46 +0000908 unsigned numDataArgs = TheCall->getNumArgs()-firstDataArg;
Ted Kremenek71895b92007-08-14 17:39:48 +0000909
910 // Inspect the format string.
911 unsigned StrIdx = 0;
912
913 // LastConversionIdx - Index within the format string where we last saw
914 // a '%' character that starts a new format conversion.
915 unsigned LastConversionIdx = 0;
916
Chris Lattner925e60d2007-12-28 05:29:59 +0000917 for (; StrIdx < StrLen; ++StrIdx) {
Chris Lattner998568f2007-12-28 05:38:24 +0000918
Ted Kremenek71895b92007-08-14 17:39:48 +0000919 // Is the number of detected conversion conversions greater than
920 // the number of matching data arguments? If so, stop.
921 if (!HasVAListArg && numConversions > numDataArgs) break;
922
923 // Handle "\0"
Chris Lattner925e60d2007-12-28 05:29:59 +0000924 if (Str[StrIdx] == '\0') {
Ted Kremenek71895b92007-08-14 17:39:48 +0000925 // The string returned by getStrData() is not null-terminated,
926 // so the presence of a null character is likely an error.
Chris Lattner60800082009-02-18 17:49:48 +0000927 Diag(getLocationOfStringLiteralByte(FExpr, StrIdx),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000928 diag::warn_printf_format_string_contains_null_char)
929 << OrigFormatExpr->getSourceRange();
Ted Kremenek71895b92007-08-14 17:39:48 +0000930 return;
931 }
932
933 // Ordinary characters (not processing a format conversion).
934 if (CurrentState == state_OrdChr) {
935 if (Str[StrIdx] == '%') {
936 CurrentState = state_Conversion;
937 LastConversionIdx = StrIdx;
938 }
939 continue;
940 }
941
942 // Seen '%'. Now processing a format conversion.
943 switch (Str[StrIdx]) {
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000944 // Handle dynamic precision or width specifier.
945 case '*': {
946 ++numConversions;
947
Ted Kremenek42ae3e82009-05-13 16:06:05 +0000948 if (!HasVAListArg) {
949 if (numConversions > numDataArgs) {
950 SourceLocation Loc = getLocationOfStringLiteralByte(FExpr, StrIdx);
Ted Kremenek580b6642007-10-12 20:51:52 +0000951
Ted Kremenek42ae3e82009-05-13 16:06:05 +0000952 if (Str[StrIdx-1] == '.')
953 Diag(Loc, diag::warn_printf_asterisk_precision_missing_arg)
954 << OrigFormatExpr->getSourceRange();
955 else
956 Diag(Loc, diag::warn_printf_asterisk_width_missing_arg)
957 << OrigFormatExpr->getSourceRange();
958
959 // Don't do any more checking. We'll just emit spurious errors.
960 return;
961 }
962
963 // Perform type checking on width/precision specifier.
964 const Expr *E = TheCall->getArg(format_idx+numConversions);
965 if (const BuiltinType *BT = E->getType()->getAsBuiltinType())
966 if (BT->getKind() == BuiltinType::Int)
967 break;
Ted Kremenek580b6642007-10-12 20:51:52 +0000968
Ted Kremenek42ae3e82009-05-13 16:06:05 +0000969 SourceLocation Loc = getLocationOfStringLiteralByte(FExpr, StrIdx);
970
971 if (Str[StrIdx-1] == '.')
972 Diag(Loc, diag::warn_printf_asterisk_precision_wrong_type)
973 << E->getType() << E->getSourceRange();
974 else
975 Diag(Loc, diag::warn_printf_asterisk_width_wrong_type)
976 << E->getType() << E->getSourceRange();
977
978 break;
Ted Kremenek580b6642007-10-12 20:51:52 +0000979 }
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000980 }
981
982 // Characters which can terminate a format conversion
983 // (e.g. "%d"). Characters that specify length modifiers or
984 // other flags are handled by the default case below.
985 //
986 // FIXME: additional checks will go into the following cases.
987 case 'i':
988 case 'd':
989 case 'o':
990 case 'u':
991 case 'x':
992 case 'X':
993 case 'D':
994 case 'O':
995 case 'U':
996 case 'e':
997 case 'E':
998 case 'f':
999 case 'F':
1000 case 'g':
1001 case 'G':
1002 case 'a':
1003 case 'A':
1004 case 'c':
1005 case 'C':
1006 case 'S':
1007 case 's':
1008 case 'p':
1009 ++numConversions;
1010 CurrentState = state_OrdChr;
1011 break;
1012
1013 // CHECK: Are we using "%n"? Issue a warning.
1014 case 'n': {
1015 ++numConversions;
1016 CurrentState = state_OrdChr;
Chris Lattner60800082009-02-18 17:49:48 +00001017 SourceLocation Loc = getLocationOfStringLiteralByte(FExpr,
1018 LastConversionIdx);
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001019
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001020 Diag(Loc, diag::warn_printf_write_back)<<OrigFormatExpr->getSourceRange();
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001021 break;
1022 }
Ted Kremenek7ff22b22008-06-16 18:00:42 +00001023
1024 // Handle "%@"
1025 case '@':
1026 // %@ is allowed in ObjC format strings only.
1027 if(ObjCFExpr != NULL)
1028 CurrentState = state_OrdChr;
1029 else {
1030 // Issue a warning: invalid format conversion.
Chris Lattner60800082009-02-18 17:49:48 +00001031 SourceLocation Loc =
1032 getLocationOfStringLiteralByte(FExpr, LastConversionIdx);
Ted Kremenek7ff22b22008-06-16 18:00:42 +00001033
Chris Lattnerd3a94e22008-11-20 06:06:08 +00001034 Diag(Loc, diag::warn_printf_invalid_conversion)
1035 << std::string(Str+LastConversionIdx,
1036 Str+std::min(LastConversionIdx+2, StrLen))
1037 << OrigFormatExpr->getSourceRange();
Ted Kremenek7ff22b22008-06-16 18:00:42 +00001038 }
1039 ++numConversions;
1040 break;
1041
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001042 // Handle "%%"
1043 case '%':
1044 // Sanity check: Was the first "%" character the previous one?
1045 // If not, we will assume that we have a malformed format
1046 // conversion, and that the current "%" character is the start
1047 // of a new conversion.
1048 if (StrIdx - LastConversionIdx == 1)
1049 CurrentState = state_OrdChr;
1050 else {
1051 // Issue a warning: invalid format conversion.
Chris Lattner60800082009-02-18 17:49:48 +00001052 SourceLocation Loc =
1053 getLocationOfStringLiteralByte(FExpr, LastConversionIdx);
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001054
Chris Lattnerd3a94e22008-11-20 06:06:08 +00001055 Diag(Loc, diag::warn_printf_invalid_conversion)
1056 << std::string(Str+LastConversionIdx, Str+StrIdx)
1057 << OrigFormatExpr->getSourceRange();
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001058
1059 // This conversion is broken. Advance to the next format
1060 // conversion.
1061 LastConversionIdx = StrIdx;
1062 ++numConversions;
Ted Kremenek71895b92007-08-14 17:39:48 +00001063 }
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001064 break;
Ted Kremenek71895b92007-08-14 17:39:48 +00001065
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001066 default:
1067 // This case catches all other characters: flags, widths, etc.
1068 // We should eventually process those as well.
1069 break;
Ted Kremenek71895b92007-08-14 17:39:48 +00001070 }
1071 }
1072
1073 if (CurrentState == state_Conversion) {
1074 // Issue a warning: invalid format conversion.
Chris Lattner60800082009-02-18 17:49:48 +00001075 SourceLocation Loc =
1076 getLocationOfStringLiteralByte(FExpr, LastConversionIdx);
Ted Kremenek71895b92007-08-14 17:39:48 +00001077
Chris Lattnerd3a94e22008-11-20 06:06:08 +00001078 Diag(Loc, diag::warn_printf_invalid_conversion)
1079 << std::string(Str+LastConversionIdx,
1080 Str+std::min(LastConversionIdx+2, StrLen))
1081 << OrigFormatExpr->getSourceRange();
Ted Kremenek71895b92007-08-14 17:39:48 +00001082 return;
1083 }
1084
1085 if (!HasVAListArg) {
1086 // CHECK: Does the number of format conversions exceed the number
1087 // of data arguments?
1088 if (numConversions > numDataArgs) {
Chris Lattner60800082009-02-18 17:49:48 +00001089 SourceLocation Loc =
1090 getLocationOfStringLiteralByte(FExpr, LastConversionIdx);
Ted Kremenek71895b92007-08-14 17:39:48 +00001091
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001092 Diag(Loc, diag::warn_printf_insufficient_data_args)
1093 << OrigFormatExpr->getSourceRange();
Ted Kremenek71895b92007-08-14 17:39:48 +00001094 }
1095 // CHECK: Does the number of data arguments exceed the number of
1096 // format conversions in the format string?
1097 else if (numConversions < numDataArgs)
Chris Lattner925e60d2007-12-28 05:29:59 +00001098 Diag(TheCall->getArg(format_idx+numConversions+1)->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001099 diag::warn_printf_too_many_data_args)
1100 << OrigFormatExpr->getSourceRange();
Ted Kremenek71895b92007-08-14 17:39:48 +00001101 }
1102}
Ted Kremenek06de2762007-08-17 16:46:58 +00001103
1104//===--- CHECK: Return Address of Stack Variable --------------------------===//
1105
1106static DeclRefExpr* EvalVal(Expr *E);
1107static DeclRefExpr* EvalAddr(Expr* E);
1108
1109/// CheckReturnStackAddr - Check if a return statement returns the address
1110/// of a stack variable.
1111void
1112Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
1113 SourceLocation ReturnLoc) {
Chris Lattner56f34942008-02-13 01:02:39 +00001114
Ted Kremenek06de2762007-08-17 16:46:58 +00001115 // Perform checking for returned stack addresses.
Steve Naroffdd972f22008-09-05 22:11:13 +00001116 if (lhsType->isPointerType() || lhsType->isBlockPointerType()) {
Ted Kremenek06de2762007-08-17 16:46:58 +00001117 if (DeclRefExpr *DR = EvalAddr(RetValExp))
Chris Lattner3c73c412008-11-19 08:23:25 +00001118 Diag(DR->getLocStart(), diag::warn_ret_stack_addr)
Chris Lattner08631c52008-11-23 21:45:46 +00001119 << DR->getDecl()->getDeclName() << RetValExp->getSourceRange();
Steve Naroffc50a4a52008-09-16 22:25:10 +00001120
1121 // Skip over implicit cast expressions when checking for block expressions.
1122 if (ImplicitCastExpr *IcExpr =
1123 dyn_cast_or_null<ImplicitCastExpr>(RetValExp))
1124 RetValExp = IcExpr->getSubExpr();
1125
Steve Naroff61f40a22008-09-10 19:17:48 +00001126 if (BlockExpr *C = dyn_cast_or_null<BlockExpr>(RetValExp))
Mike Stump397195b2009-04-17 00:09:41 +00001127 if (C->hasBlockDeclRefExprs())
1128 Diag(C->getLocStart(), diag::err_ret_local_block)
1129 << C->getSourceRange();
Ted Kremenek06de2762007-08-17 16:46:58 +00001130 }
1131 // Perform checking for stack values returned by reference.
1132 else if (lhsType->isReferenceType()) {
Douglas Gregor49badde2008-10-27 19:41:14 +00001133 // Check for a reference to the stack
1134 if (DeclRefExpr *DR = EvalVal(RetValExp))
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001135 Diag(DR->getLocStart(), diag::warn_ret_stack_ref)
Chris Lattner08631c52008-11-23 21:45:46 +00001136 << DR->getDecl()->getDeclName() << RetValExp->getSourceRange();
Ted Kremenek06de2762007-08-17 16:46:58 +00001137 }
1138}
1139
1140/// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that
1141/// check if the expression in a return statement evaluates to an address
1142/// to a location on the stack. The recursion is used to traverse the
1143/// AST of the return expression, with recursion backtracking when we
1144/// encounter a subexpression that (1) clearly does not lead to the address
1145/// of a stack variable or (2) is something we cannot determine leads to
1146/// the address of a stack variable based on such local checking.
1147///
Ted Kremeneke8c600f2007-08-28 17:02:55 +00001148/// EvalAddr processes expressions that are pointers that are used as
1149/// references (and not L-values). EvalVal handles all other values.
Ted Kremenek06de2762007-08-17 16:46:58 +00001150/// At the base case of the recursion is a check for a DeclRefExpr* in
1151/// the refers to a stack variable.
1152///
1153/// This implementation handles:
1154///
1155/// * pointer-to-pointer casts
1156/// * implicit conversions from array references to pointers
1157/// * taking the address of fields
1158/// * arbitrary interplay between "&" and "*" operators
1159/// * pointer arithmetic from an address of a stack variable
1160/// * taking the address of an array element where the array is on the stack
1161static DeclRefExpr* EvalAddr(Expr *E) {
Ted Kremenek06de2762007-08-17 16:46:58 +00001162 // We should only be called for evaluating pointer expressions.
Steve Naroffdd972f22008-09-05 22:11:13 +00001163 assert((E->getType()->isPointerType() ||
1164 E->getType()->isBlockPointerType() ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001165 E->getType()->isObjCQualifiedIdType()) &&
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001166 "EvalAddr only works on pointers");
Ted Kremenek06de2762007-08-17 16:46:58 +00001167
1168 // Our "symbolic interpreter" is just a dispatch off the currently
1169 // viewed AST node. We then recursively traverse the AST by calling
1170 // EvalAddr and EvalVal appropriately.
1171 switch (E->getStmtClass()) {
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001172 case Stmt::ParenExprClass:
1173 // Ignore parentheses.
1174 return EvalAddr(cast<ParenExpr>(E)->getSubExpr());
Ted Kremenek06de2762007-08-17 16:46:58 +00001175
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001176 case Stmt::UnaryOperatorClass: {
1177 // The only unary operator that make sense to handle here
1178 // is AddrOf. All others don't make sense as pointers.
1179 UnaryOperator *U = cast<UnaryOperator>(E);
Ted Kremenek06de2762007-08-17 16:46:58 +00001180
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001181 if (U->getOpcode() == UnaryOperator::AddrOf)
1182 return EvalVal(U->getSubExpr());
1183 else
Ted Kremenek06de2762007-08-17 16:46:58 +00001184 return NULL;
1185 }
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001186
1187 case Stmt::BinaryOperatorClass: {
1188 // Handle pointer arithmetic. All other binary operators are not valid
1189 // in this context.
1190 BinaryOperator *B = cast<BinaryOperator>(E);
1191 BinaryOperator::Opcode op = B->getOpcode();
1192
1193 if (op != BinaryOperator::Add && op != BinaryOperator::Sub)
1194 return NULL;
1195
1196 Expr *Base = B->getLHS();
1197
1198 // Determine which argument is the real pointer base. It could be
1199 // the RHS argument instead of the LHS.
1200 if (!Base->getType()->isPointerType()) Base = B->getRHS();
1201
1202 assert (Base->getType()->isPointerType());
1203 return EvalAddr(Base);
1204 }
Steve Naroff61f40a22008-09-10 19:17:48 +00001205
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001206 // For conditional operators we need to see if either the LHS or RHS are
1207 // valid DeclRefExpr*s. If one of them is valid, we return it.
1208 case Stmt::ConditionalOperatorClass: {
1209 ConditionalOperator *C = cast<ConditionalOperator>(E);
1210
1211 // Handle the GNU extension for missing LHS.
1212 if (Expr *lhsExpr = C->getLHS())
1213 if (DeclRefExpr* LHS = EvalAddr(lhsExpr))
1214 return LHS;
1215
1216 return EvalAddr(C->getRHS());
1217 }
1218
Ted Kremenek54b52742008-08-07 00:49:01 +00001219 // For casts, we need to handle conversions from arrays to
1220 // pointer values, and pointer-to-pointer conversions.
Douglas Gregor49badde2008-10-27 19:41:14 +00001221 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001222 case Stmt::CStyleCastExprClass:
Douglas Gregor49badde2008-10-27 19:41:14 +00001223 case Stmt::CXXFunctionalCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001224 Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
Ted Kremenek54b52742008-08-07 00:49:01 +00001225 QualType T = SubExpr->getType();
1226
Steve Naroffdd972f22008-09-05 22:11:13 +00001227 if (SubExpr->getType()->isPointerType() ||
1228 SubExpr->getType()->isBlockPointerType() ||
1229 SubExpr->getType()->isObjCQualifiedIdType())
Ted Kremenek54b52742008-08-07 00:49:01 +00001230 return EvalAddr(SubExpr);
1231 else if (T->isArrayType())
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001232 return EvalVal(SubExpr);
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001233 else
Ted Kremenek54b52742008-08-07 00:49:01 +00001234 return 0;
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001235 }
1236
1237 // C++ casts. For dynamic casts, static casts, and const casts, we
1238 // are always converting from a pointer-to-pointer, so we just blow
Douglas Gregor49badde2008-10-27 19:41:14 +00001239 // through the cast. In the case the dynamic cast doesn't fail (and
1240 // return NULL), we take the conservative route and report cases
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001241 // where we return the address of a stack variable. For Reinterpre
Douglas Gregor49badde2008-10-27 19:41:14 +00001242 // FIXME: The comment about is wrong; we're not always converting
1243 // from pointer to pointer. I'm guessing that this code should also
1244 // handle references to objects.
1245 case Stmt::CXXStaticCastExprClass:
1246 case Stmt::CXXDynamicCastExprClass:
1247 case Stmt::CXXConstCastExprClass:
1248 case Stmt::CXXReinterpretCastExprClass: {
1249 Expr *S = cast<CXXNamedCastExpr>(E)->getSubExpr();
Steve Naroffdd972f22008-09-05 22:11:13 +00001250 if (S->getType()->isPointerType() || S->getType()->isBlockPointerType())
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001251 return EvalAddr(S);
1252 else
1253 return NULL;
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00001254 }
1255
1256 // Everything else: we simply don't reason about them.
1257 default:
1258 return NULL;
1259 }
Ted Kremenek06de2762007-08-17 16:46:58 +00001260}
1261
1262
1263/// EvalVal - This function is complements EvalAddr in the mutual recursion.
1264/// See the comments for EvalAddr for more details.
1265static DeclRefExpr* EvalVal(Expr *E) {
1266
Ted Kremeneke8c600f2007-08-28 17:02:55 +00001267 // We should only be called for evaluating non-pointer expressions, or
1268 // expressions with a pointer type that are not used as references but instead
1269 // are l-values (e.g., DeclRefExpr with a pointer type).
1270
Ted Kremenek06de2762007-08-17 16:46:58 +00001271 // Our "symbolic interpreter" is just a dispatch off the currently
1272 // viewed AST node. We then recursively traverse the AST by calling
1273 // EvalAddr and EvalVal appropriately.
1274 switch (E->getStmtClass()) {
Douglas Gregor1a49af92009-01-06 05:10:23 +00001275 case Stmt::DeclRefExprClass:
1276 case Stmt::QualifiedDeclRefExprClass: {
Ted Kremenek06de2762007-08-17 16:46:58 +00001277 // DeclRefExpr: the base case. When we hit a DeclRefExpr we are looking
1278 // at code that refers to a variable's name. We check if it has local
1279 // storage within the function, and if so, return the expression.
1280 DeclRefExpr *DR = cast<DeclRefExpr>(E);
1281
1282 if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001283 if(V->hasLocalStorage() && !V->getType()->isReferenceType()) return DR;
Ted Kremenek06de2762007-08-17 16:46:58 +00001284
1285 return NULL;
1286 }
1287
1288 case Stmt::ParenExprClass:
1289 // Ignore parentheses.
1290 return EvalVal(cast<ParenExpr>(E)->getSubExpr());
1291
1292 case Stmt::UnaryOperatorClass: {
1293 // The only unary operator that make sense to handle here
1294 // is Deref. All others don't resolve to a "name." This includes
1295 // handling all sorts of rvalues passed to a unary operator.
1296 UnaryOperator *U = cast<UnaryOperator>(E);
1297
1298 if (U->getOpcode() == UnaryOperator::Deref)
1299 return EvalAddr(U->getSubExpr());
1300
1301 return NULL;
1302 }
1303
1304 case Stmt::ArraySubscriptExprClass: {
1305 // Array subscripts are potential references to data on the stack. We
1306 // retrieve the DeclRefExpr* for the array variable if it indeed
1307 // has local storage.
Ted Kremenek23245122007-08-20 16:18:38 +00001308 return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase());
Ted Kremenek06de2762007-08-17 16:46:58 +00001309 }
1310
1311 case Stmt::ConditionalOperatorClass: {
1312 // For conditional operators we need to see if either the LHS or RHS are
1313 // non-NULL DeclRefExpr's. If one is non-NULL, we return it.
1314 ConditionalOperator *C = cast<ConditionalOperator>(E);
1315
Anders Carlsson39073232007-11-30 19:04:31 +00001316 // Handle the GNU extension for missing LHS.
1317 if (Expr *lhsExpr = C->getLHS())
1318 if (DeclRefExpr *LHS = EvalVal(lhsExpr))
1319 return LHS;
1320
1321 return EvalVal(C->getRHS());
Ted Kremenek06de2762007-08-17 16:46:58 +00001322 }
1323
1324 // Accesses to members are potential references to data on the stack.
1325 case Stmt::MemberExprClass: {
1326 MemberExpr *M = cast<MemberExpr>(E);
1327
1328 // Check for indirect access. We only want direct field accesses.
1329 if (!M->isArrow())
1330 return EvalVal(M->getBase());
1331 else
1332 return NULL;
1333 }
1334
1335 // Everything else: we simply don't reason about them.
1336 default:
1337 return NULL;
1338 }
1339}
Ted Kremenek588e5eb2007-11-25 00:58:00 +00001340
1341//===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===//
1342
1343/// Check for comparisons of floating point operands using != and ==.
1344/// Issue a warning if these are no self-comparisons, as they are not likely
1345/// to do what the programmer intended.
1346void Sema::CheckFloatComparison(SourceLocation loc, Expr* lex, Expr *rex) {
1347 bool EmitWarning = true;
1348
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001349 Expr* LeftExprSansParen = lex->IgnoreParens();
Ted Kremenek32e97b62008-01-17 17:55:13 +00001350 Expr* RightExprSansParen = rex->IgnoreParens();
Ted Kremenek588e5eb2007-11-25 00:58:00 +00001351
1352 // Special case: check for x == x (which is OK).
1353 // Do not emit warnings for such cases.
1354 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen))
1355 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen))
1356 if (DRL->getDecl() == DRR->getDecl())
1357 EmitWarning = false;
1358
Ted Kremenek1b500bb2007-11-29 00:59:04 +00001359
1360 // Special case: check for comparisons against literals that can be exactly
1361 // represented by APFloat. In such cases, do not emit a warning. This
1362 // is a heuristic: often comparison against such literals are used to
1363 // detect if a value in a variable has not changed. This clearly can
1364 // lead to false negatives.
1365 if (EmitWarning) {
1366 if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) {
1367 if (FLL->isExact())
1368 EmitWarning = false;
1369 }
1370 else
1371 if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)){
1372 if (FLR->isExact())
1373 EmitWarning = false;
1374 }
1375 }
1376
Ted Kremenek588e5eb2007-11-25 00:58:00 +00001377 // Check for comparisons with builtin types.
Sebastian Redl0eb23302009-01-19 00:08:26 +00001378 if (EmitWarning)
Ted Kremenek588e5eb2007-11-25 00:58:00 +00001379 if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001380 if (CL->isBuiltinCall(Context))
Ted Kremenek588e5eb2007-11-25 00:58:00 +00001381 EmitWarning = false;
1382
Sebastian Redl0eb23302009-01-19 00:08:26 +00001383 if (EmitWarning)
Ted Kremenek588e5eb2007-11-25 00:58:00 +00001384 if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001385 if (CR->isBuiltinCall(Context))
Ted Kremenek588e5eb2007-11-25 00:58:00 +00001386 EmitWarning = false;
1387
1388 // Emit the diagnostic.
1389 if (EmitWarning)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001390 Diag(loc, diag::warn_floatingpoint_eq)
1391 << lex->getSourceRange() << rex->getSourceRange();
Ted Kremenek588e5eb2007-11-25 00:58:00 +00001392}