blob: e5e80db2befe82b25ef8695b325a9deec0c422f0 [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 Lattner59907c42007-08-10 20:18:51 +000020#include "clang/Lex/Preprocessor.h"
Chris Lattner59907c42007-08-10 20:18:51 +000021#include "clang/Basic/Diagnostic.h"
Ted Kremenek588e5eb2007-11-25 00:58:00 +000022#include "SemaUtil.h"
Chris Lattner59907c42007-08-10 20:18:51 +000023using namespace clang;
24
25/// CheckFunctionCall - Check a direct function call for various correctness
26/// and safety properties not strictly enforced by the C type system.
Eli Friedmand38617c2008-05-14 19:38:39 +000027Action::ExprResult
Eli Friedmane8018702008-05-16 17:51:27 +000028Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCallRaw) {
29 llvm::OwningPtr<CallExpr> TheCall(TheCallRaw);
Chris Lattner59907c42007-08-10 20:18:51 +000030 // Get the IdentifierInfo* for the called function.
31 IdentifierInfo *FnInfo = FDecl->getIdentifier();
32
Chris Lattner30ce3442007-12-19 23:59:04 +000033 switch (FnInfo->getBuiltinID()) {
34 case Builtin::BI__builtin___CFStringMakeConstantString:
Chris Lattner925e60d2007-12-28 05:29:59 +000035 assert(TheCall->getNumArgs() == 1 &&
Chris Lattner1b9a0792007-12-20 00:26:33 +000036 "Wrong # arguments to builtin CFStringMakeConstantString");
Eli Friedmane8018702008-05-16 17:51:27 +000037 if (CheckBuiltinCFStringArgument(TheCall->getArg(0)))
Eli Friedmand38617c2008-05-14 19:38:39 +000038 return true;
Eli Friedmane8018702008-05-16 17:51:27 +000039 return TheCall.take();
Ted Kremenek49ff7a12008-07-09 17:58:53 +000040 case Builtin::BI__builtin_stdarg_start:
Chris Lattner30ce3442007-12-19 23:59:04 +000041 case Builtin::BI__builtin_va_start:
Chris Lattnerb7cfe882008-06-30 18:32:54 +000042 if (SemaBuiltinVAStart(TheCall.get()))
Eli Friedmand38617c2008-05-14 19:38:39 +000043 return true;
Eli Friedmane8018702008-05-16 17:51:27 +000044 return TheCall.take();
Chris Lattner1b9a0792007-12-20 00:26:33 +000045 case Builtin::BI__builtin_isgreater:
46 case Builtin::BI__builtin_isgreaterequal:
47 case Builtin::BI__builtin_isless:
48 case Builtin::BI__builtin_islessequal:
49 case Builtin::BI__builtin_islessgreater:
50 case Builtin::BI__builtin_isunordered:
Eli Friedmane8018702008-05-16 17:51:27 +000051 if (SemaBuiltinUnorderedCompare(TheCall.get()))
Eli Friedmand38617c2008-05-14 19:38:39 +000052 return true;
Eli Friedmane8018702008-05-16 17:51:27 +000053 return TheCall.take();
Eli Friedman6cfda232008-05-20 08:23:37 +000054 case Builtin::BI__builtin_return_address:
55 case Builtin::BI__builtin_frame_address:
56 if (SemaBuiltinStackAddress(TheCall.get()))
57 return true;
58 return TheCall.take();
Eli Friedmand38617c2008-05-14 19:38:39 +000059 case Builtin::BI__builtin_shufflevector:
Eli Friedmane8018702008-05-16 17:51:27 +000060 return SemaBuiltinShuffleVector(TheCall.get());
Daniel Dunbar4493f792008-07-21 22:59:13 +000061 case Builtin::BI__builtin_prefetch:
62 if (SemaBuiltinPrefetch(TheCall.get()))
63 return true;
64 return TheCall.take();
Anders Carlsson71993dd2007-08-17 05:31:46 +000065 }
66
Chris Lattner59907c42007-08-10 20:18:51 +000067 // Search the KnownFunctionIDs for the identifier.
68 unsigned i = 0, e = id_num_known_functions;
Ted Kremenek71895b92007-08-14 17:39:48 +000069 for (; i != e; ++i) { if (KnownFunctionIDs[i] == FnInfo) break; }
Eli Friedmane8018702008-05-16 17:51:27 +000070 if (i == e) return TheCall.take();
Chris Lattner59907c42007-08-10 20:18:51 +000071
72 // Printf checking.
73 if (i <= id_vprintf) {
Ted Kremenek71895b92007-08-14 17:39:48 +000074 // Retrieve the index of the format string parameter and determine
75 // if the function is passed a va_arg argument.
Chris Lattner59907c42007-08-10 20:18:51 +000076 unsigned format_idx = 0;
Ted Kremenek71895b92007-08-14 17:39:48 +000077 bool HasVAListArg = false;
78
Chris Lattner59907c42007-08-10 20:18:51 +000079 switch (i) {
Chris Lattner30ce3442007-12-19 23:59:04 +000080 default: assert(false && "No format string argument index.");
81 case id_printf: format_idx = 0; break;
82 case id_fprintf: format_idx = 1; break;
83 case id_sprintf: format_idx = 1; break;
84 case id_snprintf: format_idx = 2; break;
85 case id_asprintf: format_idx = 1; break;
Ted Kremenek7ff22b22008-06-16 18:00:42 +000086 case id_NSLog: format_idx = 0; break;
Chris Lattner30ce3442007-12-19 23:59:04 +000087 case id_vsnprintf: format_idx = 2; HasVAListArg = true; break;
88 case id_vasprintf: format_idx = 1; HasVAListArg = true; break;
89 case id_vfprintf: format_idx = 1; HasVAListArg = true; break;
90 case id_vsprintf: format_idx = 1; HasVAListArg = true; break;
91 case id_vprintf: format_idx = 0; HasVAListArg = true; break;
Ted Kremenek71895b92007-08-14 17:39:48 +000092 }
93
Eli Friedmane8018702008-05-16 17:51:27 +000094 CheckPrintfArguments(TheCall.get(), HasVAListArg, format_idx);
Chris Lattner59907c42007-08-10 20:18:51 +000095 }
Anders Carlsson71993dd2007-08-17 05:31:46 +000096
Eli Friedmane8018702008-05-16 17:51:27 +000097 return TheCall.take();
Anders Carlsson71993dd2007-08-17 05:31:46 +000098}
99
100/// CheckBuiltinCFStringArgument - Checks that the argument to the builtin
101/// CFString constructor is correct
Chris Lattnercc6f65d2007-08-25 05:30:33 +0000102bool Sema::CheckBuiltinCFStringArgument(Expr* Arg) {
Chris Lattner56f34942008-02-13 01:02:39 +0000103 Arg = Arg->IgnoreParenCasts();
Anders Carlsson71993dd2007-08-17 05:31:46 +0000104
105 StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
106
107 if (!Literal || Literal->isWide()) {
108 Diag(Arg->getLocStart(),
109 diag::err_cfstring_literal_not_string_constant,
110 Arg->getSourceRange());
Anders Carlsson9cdc4d32007-08-17 15:44:17 +0000111 return true;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000112 }
113
114 const char *Data = Literal->getStrData();
115 unsigned Length = Literal->getByteLength();
116
117 for (unsigned i = 0; i < Length; ++i) {
118 if (!isascii(Data[i])) {
119 Diag(PP.AdvanceToTokenCharacter(Arg->getLocStart(), i + 1),
120 diag::warn_cfstring_literal_contains_non_ascii_character,
121 Arg->getSourceRange());
122 break;
123 }
124
125 if (!Data[i]) {
126 Diag(PP.AdvanceToTokenCharacter(Arg->getLocStart(), i + 1),
127 diag::warn_cfstring_literal_contains_nul_character,
128 Arg->getSourceRange());
129 break;
130 }
131 }
132
Anders Carlsson9cdc4d32007-08-17 15:44:17 +0000133 return false;
Chris Lattner59907c42007-08-10 20:18:51 +0000134}
135
Chris Lattnerc27c6652007-12-20 00:05:45 +0000136/// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity.
137/// Emit an error and return true on failure, return false on success.
Chris Lattner925e60d2007-12-28 05:29:59 +0000138bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) {
139 Expr *Fn = TheCall->getCallee();
140 if (TheCall->getNumArgs() > 2) {
141 Diag(TheCall->getArg(2)->getLocStart(),
Chris Lattner30ce3442007-12-19 23:59:04 +0000142 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattner925e60d2007-12-28 05:29:59 +0000143 SourceRange(TheCall->getArg(2)->getLocStart(),
144 (*(TheCall->arg_end()-1))->getLocEnd()));
Chris Lattner30ce3442007-12-19 23:59:04 +0000145 return true;
146 }
147
Chris Lattnerc27c6652007-12-20 00:05:45 +0000148 // Determine whether the current function is variadic or not.
149 bool isVariadic;
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000150 if (getCurFunctionDecl())
Chris Lattnerc27c6652007-12-20 00:05:45 +0000151 isVariadic =
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000152 cast<FunctionTypeProto>(getCurFunctionDecl()->getType())->isVariadic();
Chris Lattner30ce3442007-12-19 23:59:04 +0000153 else
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000154 isVariadic = getCurMethodDecl()->isVariadic();
Chris Lattner30ce3442007-12-19 23:59:04 +0000155
Chris Lattnerc27c6652007-12-20 00:05:45 +0000156 if (!isVariadic) {
Chris Lattner30ce3442007-12-19 23:59:04 +0000157 Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function);
158 return true;
159 }
160
161 // Verify that the second argument to the builtin is the last argument of the
162 // current function or method.
163 bool SecondArgIsLastNamedArgument = false;
Anders Carlssone2c14102008-02-13 01:22:59 +0000164 const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts();
Anders Carlsson88cf2262008-02-11 04:20:54 +0000165
166 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) {
167 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
Chris Lattner30ce3442007-12-19 23:59:04 +0000168 // FIXME: This isn't correct for methods (results in bogus warning).
169 // Get the last formal in the current function.
Anders Carlsson88cf2262008-02-11 04:20:54 +0000170 const ParmVarDecl *LastArg;
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000171 if (getCurFunctionDecl())
172 LastArg = *(getCurFunctionDecl()->param_end()-1);
Chris Lattner30ce3442007-12-19 23:59:04 +0000173 else
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000174 LastArg = *(getCurMethodDecl()->param_end()-1);
Chris Lattner30ce3442007-12-19 23:59:04 +0000175 SecondArgIsLastNamedArgument = PV == LastArg;
176 }
177 }
178
179 if (!SecondArgIsLastNamedArgument)
Chris Lattner925e60d2007-12-28 05:29:59 +0000180 Diag(TheCall->getArg(1)->getLocStart(),
Chris Lattner30ce3442007-12-19 23:59:04 +0000181 diag::warn_second_parameter_of_va_start_not_last_named_argument);
182 return false;
Eli Friedman6cfda232008-05-20 08:23:37 +0000183}
Chris Lattner30ce3442007-12-19 23:59:04 +0000184
Chris Lattner1b9a0792007-12-20 00:26:33 +0000185/// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and
186/// friends. This is declared to take (...), so we have to check everything.
Chris Lattner925e60d2007-12-28 05:29:59 +0000187bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) {
188 if (TheCall->getNumArgs() < 2)
189 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args);
190 if (TheCall->getNumArgs() > 2)
191 return Diag(TheCall->getArg(2)->getLocStart(),
192 diag::err_typecheck_call_too_many_args,
193 SourceRange(TheCall->getArg(2)->getLocStart(),
194 (*(TheCall->arg_end()-1))->getLocEnd()));
Chris Lattner1b9a0792007-12-20 00:26:33 +0000195
Chris Lattner925e60d2007-12-28 05:29:59 +0000196 Expr *OrigArg0 = TheCall->getArg(0);
197 Expr *OrigArg1 = TheCall->getArg(1);
Chris Lattner1b9a0792007-12-20 00:26:33 +0000198
199 // Do standard promotions between the two arguments, returning their common
200 // type.
Chris Lattner925e60d2007-12-28 05:29:59 +0000201 QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false);
Chris Lattner1b9a0792007-12-20 00:26:33 +0000202
203 // If the common type isn't a real floating type, then the arguments were
204 // invalid for this operation.
205 if (!Res->isRealFloatingType())
Chris Lattner925e60d2007-12-28 05:29:59 +0000206 return Diag(OrigArg0->getLocStart(),
Chris Lattner1b9a0792007-12-20 00:26:33 +0000207 diag::err_typecheck_call_invalid_ordered_compare,
208 OrigArg0->getType().getAsString(),
209 OrigArg1->getType().getAsString(),
Chris Lattner925e60d2007-12-28 05:29:59 +0000210 SourceRange(OrigArg0->getLocStart(), OrigArg1->getLocEnd()));
Chris Lattner1b9a0792007-12-20 00:26:33 +0000211
212 return false;
213}
214
Eli Friedman6cfda232008-05-20 08:23:37 +0000215bool Sema::SemaBuiltinStackAddress(CallExpr *TheCall) {
216 // The signature for these builtins is exact; the only thing we need
217 // to check is that the argument is a constant.
218 SourceLocation Loc;
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000219 if (!TheCall->getArg(0)->isIntegerConstantExpr(Context, &Loc))
Eli Friedman6cfda232008-05-20 08:23:37 +0000220 return Diag(Loc, diag::err_stack_const_level, TheCall->getSourceRange());
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000221
Eli Friedman6cfda232008-05-20 08:23:37 +0000222 return false;
223}
224
Eli Friedmand38617c2008-05-14 19:38:39 +0000225/// SemaBuiltinShuffleVector - Handle __builtin_shufflevector.
226// This is declared to take (...), so we have to check everything.
227Action::ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) {
228 if (TheCall->getNumArgs() < 3)
229 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args,
230 TheCall->getSourceRange());
231
232 QualType FAType = TheCall->getArg(0)->getType();
233 QualType SAType = TheCall->getArg(1)->getType();
234
235 if (!FAType->isVectorType() || !SAType->isVectorType()) {
236 Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector,
237 SourceRange(TheCall->getArg(0)->getLocStart(),
238 TheCall->getArg(1)->getLocEnd()));
Eli Friedmand38617c2008-05-14 19:38:39 +0000239 return true;
240 }
241
Chris Lattnerb77792e2008-07-26 22:17:49 +0000242 if (Context.getCanonicalType(FAType).getUnqualifiedType() !=
243 Context.getCanonicalType(SAType).getUnqualifiedType()) {
Eli Friedmand38617c2008-05-14 19:38:39 +0000244 Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector,
245 SourceRange(TheCall->getArg(0)->getLocStart(),
246 TheCall->getArg(1)->getLocEnd()));
Eli Friedmand38617c2008-05-14 19:38:39 +0000247 return true;
248 }
249
250 unsigned numElements = FAType->getAsVectorType()->getNumElements();
251 if (TheCall->getNumArgs() != numElements+2) {
252 if (TheCall->getNumArgs() < numElements+2)
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000253 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args,
254 TheCall->getSourceRange());
255 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_many_args,
256 TheCall->getSourceRange());
Eli Friedmand38617c2008-05-14 19:38:39 +0000257 }
258
259 for (unsigned i = 2; i < TheCall->getNumArgs(); i++) {
260 llvm::APSInt Result(32);
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000261 if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context))
262 return Diag(TheCall->getLocStart(),
263 diag::err_shufflevector_nonconstant_argument,
264 TheCall->getArg(i)->getSourceRange());
265
266 if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2)
267 return Diag(TheCall->getLocStart(),
268 diag::err_shufflevector_argument_too_large,
269 TheCall->getArg(i)->getSourceRange());
Eli Friedmand38617c2008-05-14 19:38:39 +0000270 }
271
272 llvm::SmallVector<Expr*, 32> exprs;
273
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000274 for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) {
Eli Friedmand38617c2008-05-14 19:38:39 +0000275 exprs.push_back(TheCall->getArg(i));
276 TheCall->setArg(i, 0);
277 }
278
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000279 return new ShuffleVectorExpr(exprs.begin(), numElements+2, FAType,
280 TheCall->getCallee()->getLocStart(),
281 TheCall->getRParenLoc());
Eli Friedmand38617c2008-05-14 19:38:39 +0000282}
Chris Lattner30ce3442007-12-19 23:59:04 +0000283
Daniel Dunbar4493f792008-07-21 22:59:13 +0000284/// SemaBuiltinPrefetch - Handle __builtin_prefetch.
285// This is declared to take (const void*, ...) and can take two
286// optional constant int args.
287bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) {
288 unsigned numArgs = TheCall->getNumArgs();
289 bool res = false;
290
291 if (numArgs > 3) {
292 res |= Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_many_args,
293 TheCall->getSourceRange());
294 }
295
296 // Argument 0 is checked for us and the remaining arguments must be
297 // constant integers.
298 for (unsigned i=1; i<numArgs; ++i) {
299 Expr *Arg = TheCall->getArg(i);
300 QualType RWType = Arg->getType();
301
302 const BuiltinType *BT = RWType->getAsBuiltinType();
303 // FIXME: 32 is wrong, needs to be proper width of Int
304 llvm::APSInt Result(32);
305 if (!BT || BT->getKind() != BuiltinType::Int ||
306 !Arg->isIntegerConstantExpr(Result, Context)) {
307 if (Diag(TheCall->getLocStart(), diag::err_prefetch_invalid_argument,
308 SourceRange(Arg->getLocStart(), Arg->getLocEnd()))) {
309 res = true;
310 continue;
311 }
312 }
313
314 // FIXME: gcc issues a warning and rewrites these to 0. These
315 // seems especially odd for the third argument since the default
316 // is 3.
317 if (i==1) {
318 if (Result.getSExtValue() < 0 || Result.getSExtValue() > 1)
319 res |= Diag(TheCall->getLocStart(), diag::err_prefetch_invalid_range,
320 "0", "1",
321 SourceRange(Arg->getLocStart(), Arg->getLocEnd()));
322 } else {
323 if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3)
324 res |= Diag(TheCall->getLocStart(), diag::err_prefetch_invalid_range,
325 "0", "3",
326 SourceRange(Arg->getLocStart(), Arg->getLocEnd()));
327 }
328 }
329
330 return res;
331}
332
Chris Lattner59907c42007-08-10 20:18:51 +0000333/// CheckPrintfArguments - Check calls to printf (and similar functions) for
Ted Kremenek71895b92007-08-14 17:39:48 +0000334/// correct use of format strings.
335///
336/// HasVAListArg - A predicate indicating whether the printf-like
337/// function is passed an explicit va_arg argument (e.g., vprintf)
338///
339/// format_idx - The index into Args for the format string.
340///
341/// Improper format strings to functions in the printf family can be
342/// the source of bizarre bugs and very serious security holes. A
343/// good source of information is available in the following paper
344/// (which includes additional references):
Chris Lattner59907c42007-08-10 20:18:51 +0000345///
346/// FormatGuard: Automatic Protection From printf Format String
347/// Vulnerabilities, Proceedings of the 10th USENIX Security Symposium, 2001.
Ted Kremenek71895b92007-08-14 17:39:48 +0000348///
349/// Functionality implemented:
350///
351/// We can statically check the following properties for string
352/// literal format strings for non v.*printf functions (where the
353/// arguments are passed directly):
354//
355/// (1) Are the number of format conversions equal to the number of
356/// data arguments?
357///
358/// (2) Does each format conversion correctly match the type of the
359/// corresponding data argument? (TODO)
360///
361/// Moreover, for all printf functions we can:
362///
363/// (3) Check for a missing format string (when not caught by type checking).
364///
365/// (4) Check for no-operation flags; e.g. using "#" with format
366/// conversion 'c' (TODO)
367///
368/// (5) Check the use of '%n', a major source of security holes.
369///
370/// (6) Check for malformed format conversions that don't specify anything.
371///
372/// (7) Check for empty format strings. e.g: printf("");
373///
374/// (8) Check that the format string is a wide literal.
375///
Ted Kremenek6d439592008-03-03 16:50:00 +0000376/// (9) Also check the arguments of functions with the __format__ attribute.
377/// (TODO).
378///
Ted Kremenek71895b92007-08-14 17:39:48 +0000379/// All of these checks can be done by parsing the format string.
380///
381/// For now, we ONLY do (1), (3), (5), (6), (7), and (8).
Chris Lattner59907c42007-08-10 20:18:51 +0000382void
Chris Lattner925e60d2007-12-28 05:29:59 +0000383Sema::CheckPrintfArguments(CallExpr *TheCall, bool HasVAListArg,
384 unsigned format_idx) {
385 Expr *Fn = TheCall->getCallee();
386
Ted Kremenek71895b92007-08-14 17:39:48 +0000387 // CHECK: printf-like function is called with no format string.
Chris Lattner925e60d2007-12-28 05:29:59 +0000388 if (format_idx >= TheCall->getNumArgs()) {
389 Diag(TheCall->getRParenLoc(), diag::warn_printf_missing_format_string,
Ted Kremenek71895b92007-08-14 17:39:48 +0000390 Fn->getSourceRange());
391 return;
392 }
393
Chris Lattner56f34942008-02-13 01:02:39 +0000394 Expr *OrigFormatExpr = TheCall->getArg(format_idx)->IgnoreParenCasts();
Chris Lattner459e8482007-08-25 05:36:18 +0000395
Chris Lattner59907c42007-08-10 20:18:51 +0000396 // CHECK: format string is not a string literal.
397 //
Ted Kremenek71895b92007-08-14 17:39:48 +0000398 // Dynamically generated format strings are difficult to
399 // automatically vet at compile time. Requiring that format strings
400 // are string literals: (1) permits the checking of format strings by
401 // the compiler and thereby (2) can practically remove the source of
402 // many format string exploits.
Ted Kremenek7ff22b22008-06-16 18:00:42 +0000403
404 // Format string can be either ObjC string (e.g. @"%d") or
405 // C string (e.g. "%d")
406 // ObjC string uses the same format specifiers as C string, so we can use
407 // the same format string checking logic for both ObjC and C strings.
408 ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(OrigFormatExpr);
409 StringLiteral *FExpr = NULL;
410
411 if(ObjCFExpr != NULL)
412 FExpr = ObjCFExpr->getString();
413 else
414 FExpr = dyn_cast<StringLiteral>(OrigFormatExpr);
415
Ted Kremenek71895b92007-08-14 17:39:48 +0000416 if (FExpr == NULL) {
Ted Kremenek4a336462007-12-17 19:03:13 +0000417 // For vprintf* functions (i.e., HasVAListArg==true), we add a
418 // special check to see if the format string is a function parameter
419 // of the function calling the printf function. If the function
420 // has an attribute indicating it is a printf-like function, then we
421 // should suppress warnings concerning non-literals being used in a call
422 // to a vprintf function. For example:
423 //
424 // void
425 // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...) {
426 // va_list ap;
427 // va_start(ap, fmt);
428 // vprintf(fmt, ap); // Do NOT emit a warning about "fmt".
429 // ...
430 //
431 //
432 // FIXME: We don't have full attribute support yet, so just check to see
433 // if the argument is a DeclRefExpr that references a parameter. We'll
434 // add proper support for checking the attribute later.
435 if (HasVAListArg)
Chris Lattner998568f2007-12-28 05:38:24 +0000436 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(OrigFormatExpr))
437 if (isa<ParmVarDecl>(DR->getDecl()))
Ted Kremenek4a336462007-12-17 19:03:13 +0000438 return;
439
Chris Lattner925e60d2007-12-28 05:29:59 +0000440 Diag(TheCall->getArg(format_idx)->getLocStart(),
Ted Kremenek9801c8b2008-07-25 22:03:03 +0000441 diag::warn_printf_not_string_constant,
442 OrigFormatExpr->getSourceRange());
Ted Kremenek71895b92007-08-14 17:39:48 +0000443 return;
444 }
445
446 // CHECK: is the format string a wide literal?
447 if (FExpr->isWide()) {
Chris Lattner925e60d2007-12-28 05:29:59 +0000448 Diag(FExpr->getLocStart(),
Ted Kremenek9801c8b2008-07-25 22:03:03 +0000449 diag::warn_printf_format_string_is_wide_literal,
450 OrigFormatExpr->getSourceRange());
Ted Kremenek71895b92007-08-14 17:39:48 +0000451 return;
452 }
453
454 // Str - The format string. NOTE: this is NOT null-terminated!
455 const char * const Str = FExpr->getStrData();
456
457 // CHECK: empty format string?
458 const unsigned StrLen = FExpr->getByteLength();
459
460 if (StrLen == 0) {
Chris Lattner925e60d2007-12-28 05:29:59 +0000461 Diag(FExpr->getLocStart(), diag::warn_printf_empty_format_string,
Ted Kremenek9801c8b2008-07-25 22:03:03 +0000462 OrigFormatExpr->getSourceRange());
Ted Kremenek71895b92007-08-14 17:39:48 +0000463 return;
464 }
465
466 // We process the format string using a binary state machine. The
467 // current state is stored in CurrentState.
468 enum {
469 state_OrdChr,
470 state_Conversion
471 } CurrentState = state_OrdChr;
472
473 // numConversions - The number of conversions seen so far. This is
474 // incremented as we traverse the format string.
475 unsigned numConversions = 0;
476
477 // numDataArgs - The number of data arguments after the format
478 // string. This can only be determined for non vprintf-like
479 // functions. For those functions, this value is 1 (the sole
480 // va_arg argument).
Chris Lattner925e60d2007-12-28 05:29:59 +0000481 unsigned numDataArgs = TheCall->getNumArgs()-(format_idx+1);
Ted Kremenek71895b92007-08-14 17:39:48 +0000482
483 // Inspect the format string.
484 unsigned StrIdx = 0;
485
486 // LastConversionIdx - Index within the format string where we last saw
487 // a '%' character that starts a new format conversion.
488 unsigned LastConversionIdx = 0;
489
Chris Lattner925e60d2007-12-28 05:29:59 +0000490 for (; StrIdx < StrLen; ++StrIdx) {
Chris Lattner998568f2007-12-28 05:38:24 +0000491
Ted Kremenek71895b92007-08-14 17:39:48 +0000492 // Is the number of detected conversion conversions greater than
493 // the number of matching data arguments? If so, stop.
494 if (!HasVAListArg && numConversions > numDataArgs) break;
495
496 // Handle "\0"
Chris Lattner925e60d2007-12-28 05:29:59 +0000497 if (Str[StrIdx] == '\0') {
Ted Kremenek71895b92007-08-14 17:39:48 +0000498 // The string returned by getStrData() is not null-terminated,
499 // so the presence of a null character is likely an error.
Chris Lattner998568f2007-12-28 05:38:24 +0000500 Diag(PP.AdvanceToTokenCharacter(FExpr->getLocStart(), StrIdx+1),
501 diag::warn_printf_format_string_contains_null_char,
Ted Kremenek9801c8b2008-07-25 22:03:03 +0000502 OrigFormatExpr->getSourceRange());
Ted Kremenek71895b92007-08-14 17:39:48 +0000503 return;
504 }
505
506 // Ordinary characters (not processing a format conversion).
507 if (CurrentState == state_OrdChr) {
508 if (Str[StrIdx] == '%') {
509 CurrentState = state_Conversion;
510 LastConversionIdx = StrIdx;
511 }
512 continue;
513 }
514
515 // Seen '%'. Now processing a format conversion.
516 switch (Str[StrIdx]) {
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000517 // Handle dynamic precision or width specifier.
518 case '*': {
519 ++numConversions;
520
521 if (!HasVAListArg && numConversions > numDataArgs) {
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000522 SourceLocation Loc = FExpr->getLocStart();
523 Loc = PP.AdvanceToTokenCharacter(Loc, StrIdx+1);
Ted Kremenek580b6642007-10-12 20:51:52 +0000524
Ted Kremenek580b6642007-10-12 20:51:52 +0000525 if (Str[StrIdx-1] == '.')
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000526 Diag(Loc, diag::warn_printf_asterisk_precision_missing_arg,
Ted Kremenek9801c8b2008-07-25 22:03:03 +0000527 OrigFormatExpr->getSourceRange());
Ted Kremenek580b6642007-10-12 20:51:52 +0000528 else
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000529 Diag(Loc, diag::warn_printf_asterisk_width_missing_arg,
Ted Kremenek9801c8b2008-07-25 22:03:03 +0000530 OrigFormatExpr->getSourceRange());
Ted Kremenek580b6642007-10-12 20:51:52 +0000531
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000532 // Don't do any more checking. We'll just emit spurious errors.
533 return;
Ted Kremenek580b6642007-10-12 20:51:52 +0000534 }
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000535
536 // Perform type checking on width/precision specifier.
537 Expr *E = TheCall->getArg(format_idx+numConversions);
538 if (const BuiltinType *BT = E->getType()->getAsBuiltinType())
539 if (BT->getKind() == BuiltinType::Int)
540 break;
Ted Kremenek71895b92007-08-14 17:39:48 +0000541
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000542 SourceLocation Loc =
543 PP.AdvanceToTokenCharacter(FExpr->getLocStart(), StrIdx+1);
544
545 if (Str[StrIdx-1] == '.')
546 Diag(Loc, diag::warn_printf_asterisk_precision_wrong_type,
547 E->getType().getAsString(), E->getSourceRange());
548 else
549 Diag(Loc, diag::warn_printf_asterisk_width_wrong_type,
550 E->getType().getAsString(), E->getSourceRange());
551
552 break;
553 }
554
555 // Characters which can terminate a format conversion
556 // (e.g. "%d"). Characters that specify length modifiers or
557 // other flags are handled by the default case below.
558 //
559 // FIXME: additional checks will go into the following cases.
560 case 'i':
561 case 'd':
562 case 'o':
563 case 'u':
564 case 'x':
565 case 'X':
566 case 'D':
567 case 'O':
568 case 'U':
569 case 'e':
570 case 'E':
571 case 'f':
572 case 'F':
573 case 'g':
574 case 'G':
575 case 'a':
576 case 'A':
577 case 'c':
578 case 'C':
579 case 'S':
580 case 's':
581 case 'p':
582 ++numConversions;
583 CurrentState = state_OrdChr;
584 break;
585
586 // CHECK: Are we using "%n"? Issue a warning.
587 case 'n': {
588 ++numConversions;
589 CurrentState = state_OrdChr;
590 SourceLocation Loc = PP.AdvanceToTokenCharacter(FExpr->getLocStart(),
591 LastConversionIdx+1);
592
Ted Kremenek9801c8b2008-07-25 22:03:03 +0000593 Diag(Loc, diag::warn_printf_write_back, OrigFormatExpr->getSourceRange());
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000594 break;
595 }
Ted Kremenek7ff22b22008-06-16 18:00:42 +0000596
597 // Handle "%@"
598 case '@':
599 // %@ is allowed in ObjC format strings only.
600 if(ObjCFExpr != NULL)
601 CurrentState = state_OrdChr;
602 else {
603 // Issue a warning: invalid format conversion.
604 SourceLocation Loc = PP.AdvanceToTokenCharacter(FExpr->getLocStart(),
605 LastConversionIdx+1);
606
607 Diag(Loc, diag::warn_printf_invalid_conversion,
608 std::string(Str+LastConversionIdx,
609 Str+std::min(LastConversionIdx+2, StrLen)),
Ted Kremenek9801c8b2008-07-25 22:03:03 +0000610 OrigFormatExpr->getSourceRange());
Ted Kremenek7ff22b22008-06-16 18:00:42 +0000611 }
612 ++numConversions;
613 break;
614
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000615 // Handle "%%"
616 case '%':
617 // Sanity check: Was the first "%" character the previous one?
618 // If not, we will assume that we have a malformed format
619 // conversion, and that the current "%" character is the start
620 // of a new conversion.
621 if (StrIdx - LastConversionIdx == 1)
622 CurrentState = state_OrdChr;
623 else {
624 // Issue a warning: invalid format conversion.
Chris Lattner925e60d2007-12-28 05:29:59 +0000625 SourceLocation Loc = PP.AdvanceToTokenCharacter(FExpr->getLocStart(),
626 LastConversionIdx+1);
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000627
628 Diag(Loc, diag::warn_printf_invalid_conversion,
629 std::string(Str+LastConversionIdx, Str+StrIdx),
Ted Kremenek9801c8b2008-07-25 22:03:03 +0000630 OrigFormatExpr->getSourceRange());
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000631
632 // This conversion is broken. Advance to the next format
633 // conversion.
634 LastConversionIdx = StrIdx;
635 ++numConversions;
Ted Kremenek71895b92007-08-14 17:39:48 +0000636 }
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000637 break;
Ted Kremenek71895b92007-08-14 17:39:48 +0000638
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000639 default:
640 // This case catches all other characters: flags, widths, etc.
641 // We should eventually process those as well.
642 break;
Ted Kremenek71895b92007-08-14 17:39:48 +0000643 }
644 }
645
646 if (CurrentState == state_Conversion) {
647 // Issue a warning: invalid format conversion.
Chris Lattner925e60d2007-12-28 05:29:59 +0000648 SourceLocation Loc = PP.AdvanceToTokenCharacter(FExpr->getLocStart(),
649 LastConversionIdx+1);
Ted Kremenek71895b92007-08-14 17:39:48 +0000650
651 Diag(Loc, diag::warn_printf_invalid_conversion,
Chris Lattnera9e2ea12007-08-26 17:38:22 +0000652 std::string(Str+LastConversionIdx,
653 Str+std::min(LastConversionIdx+2, StrLen)),
Ted Kremenek9801c8b2008-07-25 22:03:03 +0000654 OrigFormatExpr->getSourceRange());
Ted Kremenek71895b92007-08-14 17:39:48 +0000655 return;
656 }
657
658 if (!HasVAListArg) {
659 // CHECK: Does the number of format conversions exceed the number
660 // of data arguments?
661 if (numConversions > numDataArgs) {
Chris Lattner925e60d2007-12-28 05:29:59 +0000662 SourceLocation Loc = PP.AdvanceToTokenCharacter(FExpr->getLocStart(),
663 LastConversionIdx);
Ted Kremenek71895b92007-08-14 17:39:48 +0000664
665 Diag(Loc, diag::warn_printf_insufficient_data_args,
Ted Kremenek9801c8b2008-07-25 22:03:03 +0000666 OrigFormatExpr->getSourceRange());
Ted Kremenek71895b92007-08-14 17:39:48 +0000667 }
668 // CHECK: Does the number of data arguments exceed the number of
669 // format conversions in the format string?
670 else if (numConversions < numDataArgs)
Chris Lattner925e60d2007-12-28 05:29:59 +0000671 Diag(TheCall->getArg(format_idx+numConversions+1)->getLocStart(),
Ted Kremenek9801c8b2008-07-25 22:03:03 +0000672 diag::warn_printf_too_many_data_args,
673 OrigFormatExpr->getSourceRange());
Ted Kremenek71895b92007-08-14 17:39:48 +0000674 }
675}
Ted Kremenek06de2762007-08-17 16:46:58 +0000676
677//===--- CHECK: Return Address of Stack Variable --------------------------===//
678
679static DeclRefExpr* EvalVal(Expr *E);
680static DeclRefExpr* EvalAddr(Expr* E);
681
682/// CheckReturnStackAddr - Check if a return statement returns the address
683/// of a stack variable.
684void
685Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
686 SourceLocation ReturnLoc) {
Chris Lattner56f34942008-02-13 01:02:39 +0000687
Ted Kremenek06de2762007-08-17 16:46:58 +0000688 // Perform checking for returned stack addresses.
689 if (lhsType->isPointerType()) {
690 if (DeclRefExpr *DR = EvalAddr(RetValExp))
691 Diag(DR->getLocStart(), diag::warn_ret_stack_addr,
692 DR->getDecl()->getIdentifier()->getName(),
693 RetValExp->getSourceRange());
694 }
695 // Perform checking for stack values returned by reference.
696 else if (lhsType->isReferenceType()) {
Ted Kremenek96eabe02007-08-27 16:39:17 +0000697 // Check for an implicit cast to a reference.
698 if (ImplicitCastExpr *I = dyn_cast<ImplicitCastExpr>(RetValExp))
699 if (DeclRefExpr *DR = EvalVal(I->getSubExpr()))
700 Diag(DR->getLocStart(), diag::warn_ret_stack_ref,
701 DR->getDecl()->getIdentifier()->getName(),
702 RetValExp->getSourceRange());
Ted Kremenek06de2762007-08-17 16:46:58 +0000703 }
704}
705
706/// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that
707/// check if the expression in a return statement evaluates to an address
708/// to a location on the stack. The recursion is used to traverse the
709/// AST of the return expression, with recursion backtracking when we
710/// encounter a subexpression that (1) clearly does not lead to the address
711/// of a stack variable or (2) is something we cannot determine leads to
712/// the address of a stack variable based on such local checking.
713///
Ted Kremeneke8c600f2007-08-28 17:02:55 +0000714/// EvalAddr processes expressions that are pointers that are used as
715/// references (and not L-values). EvalVal handles all other values.
Ted Kremenek06de2762007-08-17 16:46:58 +0000716/// At the base case of the recursion is a check for a DeclRefExpr* in
717/// the refers to a stack variable.
718///
719/// This implementation handles:
720///
721/// * pointer-to-pointer casts
722/// * implicit conversions from array references to pointers
723/// * taking the address of fields
724/// * arbitrary interplay between "&" and "*" operators
725/// * pointer arithmetic from an address of a stack variable
726/// * taking the address of an array element where the array is on the stack
727static DeclRefExpr* EvalAddr(Expr *E) {
Ted Kremenek06de2762007-08-17 16:46:58 +0000728 // We should only be called for evaluating pointer expressions.
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000729 assert((E->getType()->isPointerType() ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000730 E->getType()->isObjCQualifiedIdType()) &&
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000731 "EvalAddr only works on pointers");
Ted Kremenek06de2762007-08-17 16:46:58 +0000732
733 // Our "symbolic interpreter" is just a dispatch off the currently
734 // viewed AST node. We then recursively traverse the AST by calling
735 // EvalAddr and EvalVal appropriately.
736 switch (E->getStmtClass()) {
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000737 case Stmt::ParenExprClass:
738 // Ignore parentheses.
739 return EvalAddr(cast<ParenExpr>(E)->getSubExpr());
Ted Kremenek06de2762007-08-17 16:46:58 +0000740
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000741 case Stmt::UnaryOperatorClass: {
742 // The only unary operator that make sense to handle here
743 // is AddrOf. All others don't make sense as pointers.
744 UnaryOperator *U = cast<UnaryOperator>(E);
Ted Kremenek06de2762007-08-17 16:46:58 +0000745
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000746 if (U->getOpcode() == UnaryOperator::AddrOf)
747 return EvalVal(U->getSubExpr());
748 else
Ted Kremenek06de2762007-08-17 16:46:58 +0000749 return NULL;
750 }
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000751
752 case Stmt::BinaryOperatorClass: {
753 // Handle pointer arithmetic. All other binary operators are not valid
754 // in this context.
755 BinaryOperator *B = cast<BinaryOperator>(E);
756 BinaryOperator::Opcode op = B->getOpcode();
757
758 if (op != BinaryOperator::Add && op != BinaryOperator::Sub)
759 return NULL;
760
761 Expr *Base = B->getLHS();
762
763 // Determine which argument is the real pointer base. It could be
764 // the RHS argument instead of the LHS.
765 if (!Base->getType()->isPointerType()) Base = B->getRHS();
766
767 assert (Base->getType()->isPointerType());
768 return EvalAddr(Base);
769 }
770
771 // For conditional operators we need to see if either the LHS or RHS are
772 // valid DeclRefExpr*s. If one of them is valid, we return it.
773 case Stmt::ConditionalOperatorClass: {
774 ConditionalOperator *C = cast<ConditionalOperator>(E);
775
776 // Handle the GNU extension for missing LHS.
777 if (Expr *lhsExpr = C->getLHS())
778 if (DeclRefExpr* LHS = EvalAddr(lhsExpr))
779 return LHS;
780
781 return EvalAddr(C->getRHS());
782 }
783
Ted Kremenek54b52742008-08-07 00:49:01 +0000784 // For casts, we need to handle conversions from arrays to
785 // pointer values, and pointer-to-pointer conversions.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000786 case Stmt::ExplicitCastExprClass:
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000787 case Stmt::ImplicitCastExprClass: {
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000788
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000789 Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
Ted Kremenek54b52742008-08-07 00:49:01 +0000790 QualType T = SubExpr->getType();
791
792 if (T->isPointerType() || T->isObjCQualifiedIdType())
793 return EvalAddr(SubExpr);
794 else if (T->isArrayType())
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000795 return EvalVal(SubExpr);
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000796 else
Ted Kremenek54b52742008-08-07 00:49:01 +0000797 return 0;
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000798 }
799
800 // C++ casts. For dynamic casts, static casts, and const casts, we
801 // are always converting from a pointer-to-pointer, so we just blow
802 // through the cast. In the case the dynamic cast doesn't fail
803 // (and return NULL), we take the conservative route and report cases
804 // where we return the address of a stack variable. For Reinterpre
805 case Stmt::CXXCastExprClass: {
806 CXXCastExpr *C = cast<CXXCastExpr>(E);
807
808 if (C->getOpcode() == CXXCastExpr::ReinterpretCast) {
809 Expr *S = C->getSubExpr();
810 if (S->getType()->isPointerType())
811 return EvalAddr(S);
812 else
813 return NULL;
814 }
815 else
816 return EvalAddr(C->getSubExpr());
817 }
818
819 // Everything else: we simply don't reason about them.
820 default:
821 return NULL;
822 }
Ted Kremenek06de2762007-08-17 16:46:58 +0000823}
824
825
826/// EvalVal - This function is complements EvalAddr in the mutual recursion.
827/// See the comments for EvalAddr for more details.
828static DeclRefExpr* EvalVal(Expr *E) {
829
Ted Kremeneke8c600f2007-08-28 17:02:55 +0000830 // We should only be called for evaluating non-pointer expressions, or
831 // expressions with a pointer type that are not used as references but instead
832 // are l-values (e.g., DeclRefExpr with a pointer type).
833
Ted Kremenek06de2762007-08-17 16:46:58 +0000834 // Our "symbolic interpreter" is just a dispatch off the currently
835 // viewed AST node. We then recursively traverse the AST by calling
836 // EvalAddr and EvalVal appropriately.
837 switch (E->getStmtClass()) {
Ted Kremenek06de2762007-08-17 16:46:58 +0000838 case Stmt::DeclRefExprClass: {
839 // DeclRefExpr: the base case. When we hit a DeclRefExpr we are looking
840 // at code that refers to a variable's name. We check if it has local
841 // storage within the function, and if so, return the expression.
842 DeclRefExpr *DR = cast<DeclRefExpr>(E);
843
844 if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
845 if(V->hasLocalStorage()) return DR;
846
847 return NULL;
848 }
849
850 case Stmt::ParenExprClass:
851 // Ignore parentheses.
852 return EvalVal(cast<ParenExpr>(E)->getSubExpr());
853
854 case Stmt::UnaryOperatorClass: {
855 // The only unary operator that make sense to handle here
856 // is Deref. All others don't resolve to a "name." This includes
857 // handling all sorts of rvalues passed to a unary operator.
858 UnaryOperator *U = cast<UnaryOperator>(E);
859
860 if (U->getOpcode() == UnaryOperator::Deref)
861 return EvalAddr(U->getSubExpr());
862
863 return NULL;
864 }
865
866 case Stmt::ArraySubscriptExprClass: {
867 // Array subscripts are potential references to data on the stack. We
868 // retrieve the DeclRefExpr* for the array variable if it indeed
869 // has local storage.
Ted Kremenek23245122007-08-20 16:18:38 +0000870 return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase());
Ted Kremenek06de2762007-08-17 16:46:58 +0000871 }
872
873 case Stmt::ConditionalOperatorClass: {
874 // For conditional operators we need to see if either the LHS or RHS are
875 // non-NULL DeclRefExpr's. If one is non-NULL, we return it.
876 ConditionalOperator *C = cast<ConditionalOperator>(E);
877
Anders Carlsson39073232007-11-30 19:04:31 +0000878 // Handle the GNU extension for missing LHS.
879 if (Expr *lhsExpr = C->getLHS())
880 if (DeclRefExpr *LHS = EvalVal(lhsExpr))
881 return LHS;
882
883 return EvalVal(C->getRHS());
Ted Kremenek06de2762007-08-17 16:46:58 +0000884 }
885
886 // Accesses to members are potential references to data on the stack.
887 case Stmt::MemberExprClass: {
888 MemberExpr *M = cast<MemberExpr>(E);
889
890 // Check for indirect access. We only want direct field accesses.
891 if (!M->isArrow())
892 return EvalVal(M->getBase());
893 else
894 return NULL;
895 }
896
897 // Everything else: we simply don't reason about them.
898 default:
899 return NULL;
900 }
901}
Ted Kremenek588e5eb2007-11-25 00:58:00 +0000902
903//===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===//
904
905/// Check for comparisons of floating point operands using != and ==.
906/// Issue a warning if these are no self-comparisons, as they are not likely
907/// to do what the programmer intended.
908void Sema::CheckFloatComparison(SourceLocation loc, Expr* lex, Expr *rex) {
909 bool EmitWarning = true;
910
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000911 Expr* LeftExprSansParen = lex->IgnoreParens();
Ted Kremenek32e97b62008-01-17 17:55:13 +0000912 Expr* RightExprSansParen = rex->IgnoreParens();
Ted Kremenek588e5eb2007-11-25 00:58:00 +0000913
914 // Special case: check for x == x (which is OK).
915 // Do not emit warnings for such cases.
916 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen))
917 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen))
918 if (DRL->getDecl() == DRR->getDecl())
919 EmitWarning = false;
920
Ted Kremenek1b500bb2007-11-29 00:59:04 +0000921
922 // Special case: check for comparisons against literals that can be exactly
923 // represented by APFloat. In such cases, do not emit a warning. This
924 // is a heuristic: often comparison against such literals are used to
925 // detect if a value in a variable has not changed. This clearly can
926 // lead to false negatives.
927 if (EmitWarning) {
928 if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) {
929 if (FLL->isExact())
930 EmitWarning = false;
931 }
932 else
933 if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)){
934 if (FLR->isExact())
935 EmitWarning = false;
936 }
937 }
938
Ted Kremenek588e5eb2007-11-25 00:58:00 +0000939 // Check for comparisons with builtin types.
940 if (EmitWarning)
941 if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen))
942 if (isCallBuiltin(CL))
943 EmitWarning = false;
944
945 if (EmitWarning)
946 if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen))
947 if (isCallBuiltin(CR))
948 EmitWarning = false;
949
950 // Emit the diagnostic.
951 if (EmitWarning)
952 Diag(loc, diag::warn_floatingpoint_eq,
953 lex->getSourceRange(),rex->getSourceRange());
954}