blob: f764d80df25d2757e33e8afcc69eabcbde2325ad [file] [log] [blame]
Chris Lattner2e64c072007-08-10 20:18:51 +00001//===--- SemaChecking.cpp - Extra Semantic Checking -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Ted Kremenek and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements extra semantic analysis beyond what is enforced
11// by the C type system.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Sema.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/Expr.h"
Ted Kremenek1c1700f2007-08-20 16:18:38 +000019#include "clang/AST/ExprCXX.h"
Chris Lattner2e64c072007-08-10 20:18:51 +000020#include "clang/Lex/Preprocessor.h"
21#include "clang/Lex/LiteralSupport.h"
22#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/Diagnostic.h"
24#include "clang/Basic/LangOptions.h"
25#include "clang/Basic/TargetInfo.h"
26#include "llvm/ADT/SmallString.h"
27#include "llvm/ADT/StringExtras.h"
28using namespace clang;
29
30/// CheckFunctionCall - Check a direct function call for various correctness
31/// and safety properties not strictly enforced by the C type system.
Anders Carlssone7e7aa22007-08-17 05:31:46 +000032bool
Ted Kremenek081ed872007-08-14 17:39:48 +000033Sema::CheckFunctionCall(Expr *Fn,
34 SourceLocation LParenLoc, SourceLocation RParenLoc,
35 FunctionDecl *FDecl,
Chris Lattner2e64c072007-08-10 20:18:51 +000036 Expr** Args, unsigned NumArgsInCall) {
37
38 // Get the IdentifierInfo* for the called function.
39 IdentifierInfo *FnInfo = FDecl->getIdentifier();
40
Anders Carlssone7e7aa22007-08-17 05:31:46 +000041 if (FnInfo->getBuiltinID() ==
42 Builtin::BI__builtin___CFStringMakeConstantString) {
43 assert(NumArgsInCall == 1 &&
Chris Lattnerd58c31c2007-08-30 17:08:17 +000044 "Wrong number of arguments to builtin CFStringMakeConstantString");
Anders Carlssone7e7aa22007-08-17 05:31:46 +000045 return CheckBuiltinCFStringArgument(Args[0]);
Anders Carlssone2674802007-10-12 17:48:41 +000046 } else if (FnInfo->getBuiltinID() == Builtin::BI__builtin_va_start) {
47 if (NumArgsInCall > 2) {
48 Diag(Args[2]->getLocStart(),
49 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
50 SourceRange(Args[2]->getLocStart(),
51 Args[NumArgsInCall - 1]->getLocEnd()));
52 return true;
53 }
54
55 FunctionTypeProto* proto =
56 cast<FunctionTypeProto>(CurFunctionDecl->getType());
57 if (!proto->isVariadic()) {
58 Diag(Fn->getLocStart(),
59 diag::err_va_start_used_in_non_variadic_function);
60 return true;
61 }
62
63 bool SecondArgIsLastNamedArgument = false;
64 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Args[1])) {
65 if (ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
66 ParmVarDecl *LastNamedArg =
67 CurFunctionDecl->getParamDecl(CurFunctionDecl->getNumParams() - 1);
68
69 if (PV == LastNamedArg)
70 SecondArgIsLastNamedArgument = true;
71 }
72 }
73
74 if (!SecondArgIsLastNamedArgument)
75 Diag(Args[1]->getLocStart(),
76 diag::warn_second_parameter_of_va_start_not_last_named_argument);
Anders Carlssone7e7aa22007-08-17 05:31:46 +000077 }
78
Chris Lattner2e64c072007-08-10 20:18:51 +000079 // Search the KnownFunctionIDs for the identifier.
80 unsigned i = 0, e = id_num_known_functions;
Ted Kremenek081ed872007-08-14 17:39:48 +000081 for (; i != e; ++i) { if (KnownFunctionIDs[i] == FnInfo) break; }
Anders Carlsson3e9b43b2007-08-17 15:44:17 +000082 if (i == e) return false;
Chris Lattner2e64c072007-08-10 20:18:51 +000083
84 // Printf checking.
85 if (i <= id_vprintf) {
Ted Kremenek081ed872007-08-14 17:39:48 +000086 // Retrieve the index of the format string parameter and determine
87 // if the function is passed a va_arg argument.
Chris Lattner2e64c072007-08-10 20:18:51 +000088 unsigned format_idx = 0;
Ted Kremenek081ed872007-08-14 17:39:48 +000089 bool HasVAListArg = false;
90
Chris Lattner2e64c072007-08-10 20:18:51 +000091 switch (i) {
92 default: assert(false && "No format string argument index.");
93 case id_printf: format_idx = 0; break;
94 case id_fprintf: format_idx = 1; break;
95 case id_sprintf: format_idx = 1; break;
96 case id_snprintf: format_idx = 2; break;
Ted Kremenek081ed872007-08-14 17:39:48 +000097 case id_asprintf: format_idx = 1; HasVAListArg = true; break;
98 case id_vsnprintf: format_idx = 2; HasVAListArg = true; break;
99 case id_vasprintf: format_idx = 1; HasVAListArg = true; break;
100 case id_vfprintf: format_idx = 1; HasVAListArg = true; break;
101 case id_vsprintf: format_idx = 1; HasVAListArg = true; break;
102 case id_vprintf: format_idx = 0; HasVAListArg = true; break;
103 }
104
105 CheckPrintfArguments(Fn, LParenLoc, RParenLoc, HasVAListArg,
Ted Kremenek45925ab2007-08-17 16:46:58 +0000106 FDecl, format_idx, Args, NumArgsInCall);
Chris Lattner2e64c072007-08-10 20:18:51 +0000107 }
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000108
Anders Carlsson3e9b43b2007-08-17 15:44:17 +0000109 return false;
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000110}
111
112/// CheckBuiltinCFStringArgument - Checks that the argument to the builtin
113/// CFString constructor is correct
Chris Lattnerda050402007-08-25 05:30:33 +0000114bool Sema::CheckBuiltinCFStringArgument(Expr* Arg) {
Chris Lattnere65acc12007-08-25 05:36:18 +0000115 // FIXME: This should go in a helper.
Chris Lattnerda050402007-08-25 05:30:33 +0000116 while (1) {
117 if (ParenExpr *PE = dyn_cast<ParenExpr>(Arg))
118 Arg = PE->getSubExpr();
119 else if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg))
120 Arg = ICE->getSubExpr();
121 else
122 break;
123 }
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000124
125 StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
126
127 if (!Literal || Literal->isWide()) {
128 Diag(Arg->getLocStart(),
129 diag::err_cfstring_literal_not_string_constant,
130 Arg->getSourceRange());
Anders Carlsson3e9b43b2007-08-17 15:44:17 +0000131 return true;
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000132 }
133
134 const char *Data = Literal->getStrData();
135 unsigned Length = Literal->getByteLength();
136
137 for (unsigned i = 0; i < Length; ++i) {
138 if (!isascii(Data[i])) {
139 Diag(PP.AdvanceToTokenCharacter(Arg->getLocStart(), i + 1),
140 diag::warn_cfstring_literal_contains_non_ascii_character,
141 Arg->getSourceRange());
142 break;
143 }
144
145 if (!Data[i]) {
146 Diag(PP.AdvanceToTokenCharacter(Arg->getLocStart(), i + 1),
147 diag::warn_cfstring_literal_contains_nul_character,
148 Arg->getSourceRange());
149 break;
150 }
151 }
152
Anders Carlsson3e9b43b2007-08-17 15:44:17 +0000153 return false;
Chris Lattner2e64c072007-08-10 20:18:51 +0000154}
155
156/// CheckPrintfArguments - Check calls to printf (and similar functions) for
Ted Kremenek081ed872007-08-14 17:39:48 +0000157/// correct use of format strings.
158///
159/// HasVAListArg - A predicate indicating whether the printf-like
160/// function is passed an explicit va_arg argument (e.g., vprintf)
161///
162/// format_idx - The index into Args for the format string.
163///
164/// Improper format strings to functions in the printf family can be
165/// the source of bizarre bugs and very serious security holes. A
166/// good source of information is available in the following paper
167/// (which includes additional references):
Chris Lattner2e64c072007-08-10 20:18:51 +0000168///
169/// FormatGuard: Automatic Protection From printf Format String
170/// Vulnerabilities, Proceedings of the 10th USENIX Security Symposium, 2001.
Ted Kremenek081ed872007-08-14 17:39:48 +0000171///
172/// Functionality implemented:
173///
174/// We can statically check the following properties for string
175/// literal format strings for non v.*printf functions (where the
176/// arguments are passed directly):
177//
178/// (1) Are the number of format conversions equal to the number of
179/// data arguments?
180///
181/// (2) Does each format conversion correctly match the type of the
182/// corresponding data argument? (TODO)
183///
184/// Moreover, for all printf functions we can:
185///
186/// (3) Check for a missing format string (when not caught by type checking).
187///
188/// (4) Check for no-operation flags; e.g. using "#" with format
189/// conversion 'c' (TODO)
190///
191/// (5) Check the use of '%n', a major source of security holes.
192///
193/// (6) Check for malformed format conversions that don't specify anything.
194///
195/// (7) Check for empty format strings. e.g: printf("");
196///
197/// (8) Check that the format string is a wide literal.
198///
199/// All of these checks can be done by parsing the format string.
200///
201/// For now, we ONLY do (1), (3), (5), (6), (7), and (8).
Chris Lattner2e64c072007-08-10 20:18:51 +0000202void
Ted Kremenek081ed872007-08-14 17:39:48 +0000203Sema::CheckPrintfArguments(Expr *Fn,
204 SourceLocation LParenLoc, SourceLocation RParenLoc,
205 bool HasVAListArg, FunctionDecl *FDecl,
Ted Kremenek30596542007-08-10 21:21:05 +0000206 unsigned format_idx, Expr** Args,
207 unsigned NumArgsInCall) {
Ted Kremenek081ed872007-08-14 17:39:48 +0000208 // CHECK: printf-like function is called with no format string.
209 if (format_idx >= NumArgsInCall) {
210 Diag(RParenLoc, diag::warn_printf_missing_format_string,
211 Fn->getSourceRange());
212 return;
213 }
214
Chris Lattnere65acc12007-08-25 05:36:18 +0000215 Expr *OrigFormatExpr = Args[format_idx];
216 // FIXME: This should go in a helper.
217 while (1) {
218 if (ParenExpr *PE = dyn_cast<ParenExpr>(OrigFormatExpr))
219 OrigFormatExpr = PE->getSubExpr();
220 else if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(OrigFormatExpr))
221 OrigFormatExpr = ICE->getSubExpr();
222 else
223 break;
224 }
225
Chris Lattner2e64c072007-08-10 20:18:51 +0000226 // CHECK: format string is not a string literal.
227 //
Ted Kremenek081ed872007-08-14 17:39:48 +0000228 // Dynamically generated format strings are difficult to
229 // automatically vet at compile time. Requiring that format strings
230 // are string literals: (1) permits the checking of format strings by
231 // the compiler and thereby (2) can practically remove the source of
232 // many format string exploits.
Chris Lattnere65acc12007-08-25 05:36:18 +0000233 StringLiteral *FExpr = dyn_cast<StringLiteral>(OrigFormatExpr);
Chris Lattner2e64c072007-08-10 20:18:51 +0000234
Ted Kremenek081ed872007-08-14 17:39:48 +0000235 if (FExpr == NULL) {
236 Diag(Args[format_idx]->getLocStart(),
237 diag::warn_printf_not_string_constant, Fn->getSourceRange());
238 return;
239 }
240
241 // CHECK: is the format string a wide literal?
242 if (FExpr->isWide()) {
243 Diag(Args[format_idx]->getLocStart(),
244 diag::warn_printf_format_string_is_wide_literal,
245 Fn->getSourceRange());
246 return;
247 }
248
249 // Str - The format string. NOTE: this is NOT null-terminated!
250 const char * const Str = FExpr->getStrData();
251
252 // CHECK: empty format string?
253 const unsigned StrLen = FExpr->getByteLength();
254
255 if (StrLen == 0) {
256 Diag(Args[format_idx]->getLocStart(),
257 diag::warn_printf_empty_format_string, Fn->getSourceRange());
258 return;
259 }
260
261 // We process the format string using a binary state machine. The
262 // current state is stored in CurrentState.
263 enum {
264 state_OrdChr,
265 state_Conversion
266 } CurrentState = state_OrdChr;
267
268 // numConversions - The number of conversions seen so far. This is
269 // incremented as we traverse the format string.
270 unsigned numConversions = 0;
271
272 // numDataArgs - The number of data arguments after the format
273 // string. This can only be determined for non vprintf-like
274 // functions. For those functions, this value is 1 (the sole
275 // va_arg argument).
276 unsigned numDataArgs = NumArgsInCall-(format_idx+1);
277
278 // Inspect the format string.
279 unsigned StrIdx = 0;
280
281 // LastConversionIdx - Index within the format string where we last saw
282 // a '%' character that starts a new format conversion.
283 unsigned LastConversionIdx = 0;
284
285 for ( ; StrIdx < StrLen ; ++StrIdx ) {
286
287 // Is the number of detected conversion conversions greater than
288 // the number of matching data arguments? If so, stop.
289 if (!HasVAListArg && numConversions > numDataArgs) break;
290
291 // Handle "\0"
292 if(Str[StrIdx] == '\0' ) {
293 // The string returned by getStrData() is not null-terminated,
294 // so the presence of a null character is likely an error.
295
296 SourceLocation Loc =
297 PP.AdvanceToTokenCharacter(Args[format_idx]->getLocStart(),StrIdx+1);
298
299 Diag(Loc, diag::warn_printf_format_string_contains_null_char,
300 Fn->getSourceRange());
301
302 return;
303 }
304
305 // Ordinary characters (not processing a format conversion).
306 if (CurrentState == state_OrdChr) {
307 if (Str[StrIdx] == '%') {
308 CurrentState = state_Conversion;
309 LastConversionIdx = StrIdx;
310 }
311 continue;
312 }
313
314 // Seen '%'. Now processing a format conversion.
315 switch (Str[StrIdx]) {
Ted Kremenek42166a82007-10-12 00:11:27 +0000316 // Handle dynamic precision specifier.
317 case '*':
318 if (Str[StrIdx-1] == '.') ++numConversions;
319 break;
320
Ted Kremenek081ed872007-08-14 17:39:48 +0000321 // Characters which can terminate a format conversion
322 // (e.g. "%d"). Characters that specify length modifiers or
323 // other flags are handled by the default case below.
324 //
Ted Kremenek42166a82007-10-12 00:11:27 +0000325 // FIXME: additional checks will go into the following cases.
Ted Kremenek081ed872007-08-14 17:39:48 +0000326 case 'i':
327 case 'd':
328 case 'o':
329 case 'u':
330 case 'x':
331 case 'X':
332 case 'D':
333 case 'O':
334 case 'U':
335 case 'e':
336 case 'E':
337 case 'f':
338 case 'F':
339 case 'g':
340 case 'G':
341 case 'a':
342 case 'A':
343 case 'c':
344 case 'C':
345 case 'S':
346 case 's':
Chris Lattner04e04642007-08-26 17:39:38 +0000347 case 'p':
Ted Kremenek081ed872007-08-14 17:39:48 +0000348 ++numConversions;
349 CurrentState = state_OrdChr;
350 break;
351
352 // CHECK: Are we using "%n"? Issue a warning.
353 case 'n': {
354 ++numConversions;
355 CurrentState = state_OrdChr;
356 SourceLocation Loc =
357 PP.AdvanceToTokenCharacter(Args[format_idx]->getLocStart(),
358 LastConversionIdx+1);
359
360 Diag(Loc, diag::warn_printf_write_back, Fn->getSourceRange());
361 break;
362 }
363
364 // Handle "%%"
365 case '%':
366 // Sanity check: Was the first "%" character the previous one?
367 // If not, we will assume that we have a malformed format
368 // conversion, and that the current "%" character is the start
369 // of a new conversion.
370 if (StrIdx - LastConversionIdx == 1)
371 CurrentState = state_OrdChr;
372 else {
373 // Issue a warning: invalid format conversion.
374 SourceLocation Loc =
375 PP.AdvanceToTokenCharacter(Args[format_idx]->getLocStart(),
376 LastConversionIdx+1);
377
378 Diag(Loc, diag::warn_printf_invalid_conversion,
379 std::string(Str+LastConversionIdx, Str+StrIdx),
380 Fn->getSourceRange());
381
382 // This conversion is broken. Advance to the next format
383 // conversion.
384 LastConversionIdx = StrIdx;
385 ++numConversions;
386 }
387
388 break;
389
390 default:
391 // This case catches all other characters: flags, widths, etc.
392 // We should eventually process those as well.
393 break;
394 }
395 }
396
397 if (CurrentState == state_Conversion) {
398 // Issue a warning: invalid format conversion.
399 SourceLocation Loc =
400 PP.AdvanceToTokenCharacter(Args[format_idx]->getLocStart(),
401 LastConversionIdx+1);
402
403 Diag(Loc, diag::warn_printf_invalid_conversion,
Chris Lattner6f65d202007-08-26 17:38:22 +0000404 std::string(Str+LastConversionIdx,
405 Str+std::min(LastConversionIdx+2, StrLen)),
Ted Kremenek081ed872007-08-14 17:39:48 +0000406 Fn->getSourceRange());
407 return;
408 }
409
410 if (!HasVAListArg) {
411 // CHECK: Does the number of format conversions exceed the number
412 // of data arguments?
413 if (numConversions > numDataArgs) {
414 SourceLocation Loc =
415 PP.AdvanceToTokenCharacter(Args[format_idx]->getLocStart(),
416 LastConversionIdx);
417
418 Diag(Loc, diag::warn_printf_insufficient_data_args,
419 Fn->getSourceRange());
420 }
421 // CHECK: Does the number of data arguments exceed the number of
422 // format conversions in the format string?
423 else if (numConversions < numDataArgs)
424 Diag(Args[format_idx+numConversions+1]->getLocStart(),
425 diag::warn_printf_too_many_data_args, Fn->getSourceRange());
426 }
427}
Ted Kremenek45925ab2007-08-17 16:46:58 +0000428
429//===--- CHECK: Return Address of Stack Variable --------------------------===//
430
431static DeclRefExpr* EvalVal(Expr *E);
432static DeclRefExpr* EvalAddr(Expr* E);
433
434/// CheckReturnStackAddr - Check if a return statement returns the address
435/// of a stack variable.
436void
437Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
438 SourceLocation ReturnLoc) {
439
440 // Perform checking for returned stack addresses.
441 if (lhsType->isPointerType()) {
442 if (DeclRefExpr *DR = EvalAddr(RetValExp))
443 Diag(DR->getLocStart(), diag::warn_ret_stack_addr,
444 DR->getDecl()->getIdentifier()->getName(),
445 RetValExp->getSourceRange());
446 }
447 // Perform checking for stack values returned by reference.
448 else if (lhsType->isReferenceType()) {
Ted Kremenek1456f202007-08-27 16:39:17 +0000449 // Check for an implicit cast to a reference.
450 if (ImplicitCastExpr *I = dyn_cast<ImplicitCastExpr>(RetValExp))
451 if (DeclRefExpr *DR = EvalVal(I->getSubExpr()))
452 Diag(DR->getLocStart(), diag::warn_ret_stack_ref,
453 DR->getDecl()->getIdentifier()->getName(),
454 RetValExp->getSourceRange());
Ted Kremenek45925ab2007-08-17 16:46:58 +0000455 }
456}
457
458/// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that
459/// check if the expression in a return statement evaluates to an address
460/// to a location on the stack. The recursion is used to traverse the
461/// AST of the return expression, with recursion backtracking when we
462/// encounter a subexpression that (1) clearly does not lead to the address
463/// of a stack variable or (2) is something we cannot determine leads to
464/// the address of a stack variable based on such local checking.
465///
Ted Kremenekda1300a2007-08-28 17:02:55 +0000466/// EvalAddr processes expressions that are pointers that are used as
467/// references (and not L-values). EvalVal handles all other values.
Ted Kremenek45925ab2007-08-17 16:46:58 +0000468/// At the base case of the recursion is a check for a DeclRefExpr* in
469/// the refers to a stack variable.
470///
471/// This implementation handles:
472///
473/// * pointer-to-pointer casts
474/// * implicit conversions from array references to pointers
475/// * taking the address of fields
476/// * arbitrary interplay between "&" and "*" operators
477/// * pointer arithmetic from an address of a stack variable
478/// * taking the address of an array element where the array is on the stack
479static DeclRefExpr* EvalAddr(Expr *E) {
480
481 // We should only be called for evaluating pointer expressions.
482 assert (E->getType()->isPointerType() && "EvalAddr only works on pointers");
483
484 // Our "symbolic interpreter" is just a dispatch off the currently
485 // viewed AST node. We then recursively traverse the AST by calling
486 // EvalAddr and EvalVal appropriately.
487 switch (E->getStmtClass()) {
488
489 case Stmt::ParenExprClass:
490 // Ignore parentheses.
491 return EvalAddr(cast<ParenExpr>(E)->getSubExpr());
492
493 case Stmt::UnaryOperatorClass: {
494 // The only unary operator that make sense to handle here
495 // is AddrOf. All others don't make sense as pointers.
496 UnaryOperator *U = cast<UnaryOperator>(E);
497
498 if (U->getOpcode() == UnaryOperator::AddrOf)
499 return EvalVal(U->getSubExpr());
500 else
501 return NULL;
502 }
503
504 case Stmt::BinaryOperatorClass: {
505 // Handle pointer arithmetic. All other binary operators are not valid
506 // in this context.
507 BinaryOperator *B = cast<BinaryOperator>(E);
508 BinaryOperator::Opcode op = B->getOpcode();
509
510 if (op != BinaryOperator::Add && op != BinaryOperator::Sub)
511 return NULL;
512
513 Expr *Base = B->getLHS();
514
515 // Determine which argument is the real pointer base. It could be
516 // the RHS argument instead of the LHS.
517 if (!Base->getType()->isPointerType()) Base = B->getRHS();
518
519 assert (Base->getType()->isPointerType());
520 return EvalAddr(Base);
521 }
522
523 // For conditional operators we need to see if either the LHS or RHS are
524 // valid DeclRefExpr*s. If one of them is valid, we return it.
525 case Stmt::ConditionalOperatorClass: {
526 ConditionalOperator *C = cast<ConditionalOperator>(E);
527
528 if (DeclRefExpr* LHS = EvalAddr(C->getLHS()))
529 return LHS;
530 else
531 return EvalAddr(C->getRHS());
532 }
533
534 // For implicit casts, we need to handle conversions from arrays to
535 // pointer values, and implicit pointer-to-pointer conversions.
536 case Stmt::ImplicitCastExprClass: {
537 ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E);
538 Expr* SubExpr = IE->getSubExpr();
539
540 if (SubExpr->getType()->isPointerType())
541 return EvalAddr(SubExpr);
542 else
543 return EvalVal(SubExpr);
544 }
545
546 // For casts, we handle pointer-to-pointer conversions (which
547 // is essentially a no-op from our mini-interpreter's standpoint).
548 // For other casts we abort.
549 case Stmt::CastExprClass: {
550 CastExpr *C = cast<CastExpr>(E);
551 Expr *SubExpr = C->getSubExpr();
552
553 if (SubExpr->getType()->isPointerType())
554 return EvalAddr(SubExpr);
555 else
556 return NULL;
557 }
558
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000559 // C++ casts. For dynamic casts, static casts, and const casts, we
560 // are always converting from a pointer-to-pointer, so we just blow
561 // through the cast. In the case the dynamic cast doesn't fail
562 // (and return NULL), we take the conservative route and report cases
563 // where we return the address of a stack variable. For Reinterpre
564 case Stmt::CXXCastExprClass: {
565 CXXCastExpr *C = cast<CXXCastExpr>(E);
566
567 if (C->getOpcode() == CXXCastExpr::ReinterpretCast) {
568 Expr *S = C->getSubExpr();
569 if (S->getType()->isPointerType())
570 return EvalAddr(S);
571 else
572 return NULL;
573 }
574 else
575 return EvalAddr(C->getSubExpr());
576 }
Ted Kremenek45925ab2007-08-17 16:46:58 +0000577
578 // Everything else: we simply don't reason about them.
579 default:
580 return NULL;
581 }
582}
583
584
585/// EvalVal - This function is complements EvalAddr in the mutual recursion.
586/// See the comments for EvalAddr for more details.
587static DeclRefExpr* EvalVal(Expr *E) {
588
Ted Kremenekda1300a2007-08-28 17:02:55 +0000589 // We should only be called for evaluating non-pointer expressions, or
590 // expressions with a pointer type that are not used as references but instead
591 // are l-values (e.g., DeclRefExpr with a pointer type).
592
Ted Kremenek45925ab2007-08-17 16:46:58 +0000593 // Our "symbolic interpreter" is just a dispatch off the currently
594 // viewed AST node. We then recursively traverse the AST by calling
595 // EvalAddr and EvalVal appropriately.
596 switch (E->getStmtClass()) {
597
598 case Stmt::DeclRefExprClass: {
599 // DeclRefExpr: the base case. When we hit a DeclRefExpr we are looking
600 // at code that refers to a variable's name. We check if it has local
601 // storage within the function, and if so, return the expression.
602 DeclRefExpr *DR = cast<DeclRefExpr>(E);
603
604 if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
605 if(V->hasLocalStorage()) return DR;
606
607 return NULL;
608 }
609
610 case Stmt::ParenExprClass:
611 // Ignore parentheses.
612 return EvalVal(cast<ParenExpr>(E)->getSubExpr());
613
614 case Stmt::UnaryOperatorClass: {
615 // The only unary operator that make sense to handle here
616 // is Deref. All others don't resolve to a "name." This includes
617 // handling all sorts of rvalues passed to a unary operator.
618 UnaryOperator *U = cast<UnaryOperator>(E);
619
620 if (U->getOpcode() == UnaryOperator::Deref)
621 return EvalAddr(U->getSubExpr());
622
623 return NULL;
624 }
625
626 case Stmt::ArraySubscriptExprClass: {
627 // Array subscripts are potential references to data on the stack. We
628 // retrieve the DeclRefExpr* for the array variable if it indeed
629 // has local storage.
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000630 return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase());
Ted Kremenek45925ab2007-08-17 16:46:58 +0000631 }
632
633 case Stmt::ConditionalOperatorClass: {
634 // For conditional operators we need to see if either the LHS or RHS are
635 // non-NULL DeclRefExpr's. If one is non-NULL, we return it.
636 ConditionalOperator *C = cast<ConditionalOperator>(E);
637
638 if (DeclRefExpr *LHS = EvalVal(C->getLHS()))
639 return LHS;
640 else
641 return EvalVal(C->getRHS());
642 }
643
644 // Accesses to members are potential references to data on the stack.
645 case Stmt::MemberExprClass: {
646 MemberExpr *M = cast<MemberExpr>(E);
647
648 // Check for indirect access. We only want direct field accesses.
649 if (!M->isArrow())
650 return EvalVal(M->getBase());
651 else
652 return NULL;
653 }
654
655 // Everything else: we simply don't reason about them.
656 default:
657 return NULL;
658 }
659}