blob: e03507b79684b523da3dd301f2f14668caed296d [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 &&
44 "Wrong number of arguments to builtin CFStringMakeConstantString");
45 return CheckBuiltinCFStringArgument(Args[0]);
46 }
47
Chris Lattner2e64c072007-08-10 20:18:51 +000048 // Search the KnownFunctionIDs for the identifier.
49 unsigned i = 0, e = id_num_known_functions;
Ted Kremenek081ed872007-08-14 17:39:48 +000050 for (; i != e; ++i) { if (KnownFunctionIDs[i] == FnInfo) break; }
Anders Carlsson3e9b43b2007-08-17 15:44:17 +000051 if (i == e) return false;
Chris Lattner2e64c072007-08-10 20:18:51 +000052
53 // Printf checking.
54 if (i <= id_vprintf) {
Ted Kremenek081ed872007-08-14 17:39:48 +000055 // Retrieve the index of the format string parameter and determine
56 // if the function is passed a va_arg argument.
Chris Lattner2e64c072007-08-10 20:18:51 +000057 unsigned format_idx = 0;
Ted Kremenek081ed872007-08-14 17:39:48 +000058 bool HasVAListArg = false;
59
Chris Lattner2e64c072007-08-10 20:18:51 +000060 switch (i) {
61 default: assert(false && "No format string argument index.");
62 case id_printf: format_idx = 0; break;
63 case id_fprintf: format_idx = 1; break;
64 case id_sprintf: format_idx = 1; break;
65 case id_snprintf: format_idx = 2; break;
Ted Kremenek081ed872007-08-14 17:39:48 +000066 case id_asprintf: format_idx = 1; HasVAListArg = true; break;
67 case id_vsnprintf: format_idx = 2; HasVAListArg = true; break;
68 case id_vasprintf: format_idx = 1; HasVAListArg = true; break;
69 case id_vfprintf: format_idx = 1; HasVAListArg = true; break;
70 case id_vsprintf: format_idx = 1; HasVAListArg = true; break;
71 case id_vprintf: format_idx = 0; HasVAListArg = true; break;
72 }
73
74 CheckPrintfArguments(Fn, LParenLoc, RParenLoc, HasVAListArg,
Ted Kremenek45925ab2007-08-17 16:46:58 +000075 FDecl, format_idx, Args, NumArgsInCall);
Chris Lattner2e64c072007-08-10 20:18:51 +000076 }
Anders Carlssone7e7aa22007-08-17 05:31:46 +000077
Anders Carlsson3e9b43b2007-08-17 15:44:17 +000078 return false;
Anders Carlssone7e7aa22007-08-17 05:31:46 +000079}
80
81/// CheckBuiltinCFStringArgument - Checks that the argument to the builtin
82/// CFString constructor is correct
Chris Lattnerda050402007-08-25 05:30:33 +000083bool Sema::CheckBuiltinCFStringArgument(Expr* Arg) {
Chris Lattnere65acc12007-08-25 05:36:18 +000084 // FIXME: This should go in a helper.
Chris Lattnerda050402007-08-25 05:30:33 +000085 while (1) {
86 if (ParenExpr *PE = dyn_cast<ParenExpr>(Arg))
87 Arg = PE->getSubExpr();
88 else if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg))
89 Arg = ICE->getSubExpr();
90 else
91 break;
92 }
Anders Carlssone7e7aa22007-08-17 05:31:46 +000093
94 StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
95
96 if (!Literal || Literal->isWide()) {
97 Diag(Arg->getLocStart(),
98 diag::err_cfstring_literal_not_string_constant,
99 Arg->getSourceRange());
Anders Carlsson3e9b43b2007-08-17 15:44:17 +0000100 return true;
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000101 }
102
103 const char *Data = Literal->getStrData();
104 unsigned Length = Literal->getByteLength();
105
106 for (unsigned i = 0; i < Length; ++i) {
107 if (!isascii(Data[i])) {
108 Diag(PP.AdvanceToTokenCharacter(Arg->getLocStart(), i + 1),
109 diag::warn_cfstring_literal_contains_non_ascii_character,
110 Arg->getSourceRange());
111 break;
112 }
113
114 if (!Data[i]) {
115 Diag(PP.AdvanceToTokenCharacter(Arg->getLocStart(), i + 1),
116 diag::warn_cfstring_literal_contains_nul_character,
117 Arg->getSourceRange());
118 break;
119 }
120 }
121
Anders Carlsson3e9b43b2007-08-17 15:44:17 +0000122 return false;
Chris Lattner2e64c072007-08-10 20:18:51 +0000123}
124
125/// CheckPrintfArguments - Check calls to printf (and similar functions) for
Ted Kremenek081ed872007-08-14 17:39:48 +0000126/// correct use of format strings.
127///
128/// HasVAListArg - A predicate indicating whether the printf-like
129/// function is passed an explicit va_arg argument (e.g., vprintf)
130///
131/// format_idx - The index into Args for the format string.
132///
133/// Improper format strings to functions in the printf family can be
134/// the source of bizarre bugs and very serious security holes. A
135/// good source of information is available in the following paper
136/// (which includes additional references):
Chris Lattner2e64c072007-08-10 20:18:51 +0000137///
138/// FormatGuard: Automatic Protection From printf Format String
139/// Vulnerabilities, Proceedings of the 10th USENIX Security Symposium, 2001.
Ted Kremenek081ed872007-08-14 17:39:48 +0000140///
141/// Functionality implemented:
142///
143/// We can statically check the following properties for string
144/// literal format strings for non v.*printf functions (where the
145/// arguments are passed directly):
146//
147/// (1) Are the number of format conversions equal to the number of
148/// data arguments?
149///
150/// (2) Does each format conversion correctly match the type of the
151/// corresponding data argument? (TODO)
152///
153/// Moreover, for all printf functions we can:
154///
155/// (3) Check for a missing format string (when not caught by type checking).
156///
157/// (4) Check for no-operation flags; e.g. using "#" with format
158/// conversion 'c' (TODO)
159///
160/// (5) Check the use of '%n', a major source of security holes.
161///
162/// (6) Check for malformed format conversions that don't specify anything.
163///
164/// (7) Check for empty format strings. e.g: printf("");
165///
166/// (8) Check that the format string is a wide literal.
167///
168/// All of these checks can be done by parsing the format string.
169///
170/// For now, we ONLY do (1), (3), (5), (6), (7), and (8).
Chris Lattner2e64c072007-08-10 20:18:51 +0000171void
Ted Kremenek081ed872007-08-14 17:39:48 +0000172Sema::CheckPrintfArguments(Expr *Fn,
173 SourceLocation LParenLoc, SourceLocation RParenLoc,
174 bool HasVAListArg, FunctionDecl *FDecl,
Ted Kremenek30596542007-08-10 21:21:05 +0000175 unsigned format_idx, Expr** Args,
176 unsigned NumArgsInCall) {
Ted Kremenek081ed872007-08-14 17:39:48 +0000177 // CHECK: printf-like function is called with no format string.
178 if (format_idx >= NumArgsInCall) {
179 Diag(RParenLoc, diag::warn_printf_missing_format_string,
180 Fn->getSourceRange());
181 return;
182 }
183
Chris Lattnere65acc12007-08-25 05:36:18 +0000184 Expr *OrigFormatExpr = Args[format_idx];
185 // FIXME: This should go in a helper.
186 while (1) {
187 if (ParenExpr *PE = dyn_cast<ParenExpr>(OrigFormatExpr))
188 OrigFormatExpr = PE->getSubExpr();
189 else if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(OrigFormatExpr))
190 OrigFormatExpr = ICE->getSubExpr();
191 else
192 break;
193 }
194
Chris Lattner2e64c072007-08-10 20:18:51 +0000195 // CHECK: format string is not a string literal.
196 //
Ted Kremenek081ed872007-08-14 17:39:48 +0000197 // Dynamically generated format strings are difficult to
198 // automatically vet at compile time. Requiring that format strings
199 // are string literals: (1) permits the checking of format strings by
200 // the compiler and thereby (2) can practically remove the source of
201 // many format string exploits.
Chris Lattnere65acc12007-08-25 05:36:18 +0000202 StringLiteral *FExpr = dyn_cast<StringLiteral>(OrigFormatExpr);
Chris Lattner2e64c072007-08-10 20:18:51 +0000203
Ted Kremenek081ed872007-08-14 17:39:48 +0000204 if (FExpr == NULL) {
205 Diag(Args[format_idx]->getLocStart(),
206 diag::warn_printf_not_string_constant, Fn->getSourceRange());
207 return;
208 }
209
210 // CHECK: is the format string a wide literal?
211 if (FExpr->isWide()) {
212 Diag(Args[format_idx]->getLocStart(),
213 diag::warn_printf_format_string_is_wide_literal,
214 Fn->getSourceRange());
215 return;
216 }
217
218 // Str - The format string. NOTE: this is NOT null-terminated!
219 const char * const Str = FExpr->getStrData();
220
221 // CHECK: empty format string?
222 const unsigned StrLen = FExpr->getByteLength();
223
224 if (StrLen == 0) {
225 Diag(Args[format_idx]->getLocStart(),
226 diag::warn_printf_empty_format_string, Fn->getSourceRange());
227 return;
228 }
229
230 // We process the format string using a binary state machine. The
231 // current state is stored in CurrentState.
232 enum {
233 state_OrdChr,
234 state_Conversion
235 } CurrentState = state_OrdChr;
236
237 // numConversions - The number of conversions seen so far. This is
238 // incremented as we traverse the format string.
239 unsigned numConversions = 0;
240
241 // numDataArgs - The number of data arguments after the format
242 // string. This can only be determined for non vprintf-like
243 // functions. For those functions, this value is 1 (the sole
244 // va_arg argument).
245 unsigned numDataArgs = NumArgsInCall-(format_idx+1);
246
247 // Inspect the format string.
248 unsigned StrIdx = 0;
249
250 // LastConversionIdx - Index within the format string where we last saw
251 // a '%' character that starts a new format conversion.
252 unsigned LastConversionIdx = 0;
253
254 for ( ; StrIdx < StrLen ; ++StrIdx ) {
255
256 // Is the number of detected conversion conversions greater than
257 // the number of matching data arguments? If so, stop.
258 if (!HasVAListArg && numConversions > numDataArgs) break;
259
260 // Handle "\0"
261 if(Str[StrIdx] == '\0' ) {
262 // The string returned by getStrData() is not null-terminated,
263 // so the presence of a null character is likely an error.
264
265 SourceLocation Loc =
266 PP.AdvanceToTokenCharacter(Args[format_idx]->getLocStart(),StrIdx+1);
267
268 Diag(Loc, diag::warn_printf_format_string_contains_null_char,
269 Fn->getSourceRange());
270
271 return;
272 }
273
274 // Ordinary characters (not processing a format conversion).
275 if (CurrentState == state_OrdChr) {
276 if (Str[StrIdx] == '%') {
277 CurrentState = state_Conversion;
278 LastConversionIdx = StrIdx;
279 }
280 continue;
281 }
282
283 // Seen '%'. Now processing a format conversion.
284 switch (Str[StrIdx]) {
285 // Characters which can terminate a format conversion
286 // (e.g. "%d"). Characters that specify length modifiers or
287 // other flags are handled by the default case below.
288 //
289 // TODO: additional checks will go into the following cases.
290 case 'i':
291 case 'd':
292 case 'o':
293 case 'u':
294 case 'x':
295 case 'X':
296 case 'D':
297 case 'O':
298 case 'U':
299 case 'e':
300 case 'E':
301 case 'f':
302 case 'F':
303 case 'g':
304 case 'G':
305 case 'a':
306 case 'A':
307 case 'c':
308 case 'C':
309 case 'S':
310 case 's':
Chris Lattner04e04642007-08-26 17:39:38 +0000311 case 'p':
Ted Kremenek081ed872007-08-14 17:39:48 +0000312 ++numConversions;
313 CurrentState = state_OrdChr;
314 break;
315
316 // CHECK: Are we using "%n"? Issue a warning.
317 case 'n': {
318 ++numConversions;
319 CurrentState = state_OrdChr;
320 SourceLocation Loc =
321 PP.AdvanceToTokenCharacter(Args[format_idx]->getLocStart(),
322 LastConversionIdx+1);
323
324 Diag(Loc, diag::warn_printf_write_back, Fn->getSourceRange());
325 break;
326 }
327
328 // Handle "%%"
329 case '%':
330 // Sanity check: Was the first "%" character the previous one?
331 // If not, we will assume that we have a malformed format
332 // conversion, and that the current "%" character is the start
333 // of a new conversion.
334 if (StrIdx - LastConversionIdx == 1)
335 CurrentState = state_OrdChr;
336 else {
337 // Issue a warning: invalid format conversion.
338 SourceLocation Loc =
339 PP.AdvanceToTokenCharacter(Args[format_idx]->getLocStart(),
340 LastConversionIdx+1);
341
342 Diag(Loc, diag::warn_printf_invalid_conversion,
343 std::string(Str+LastConversionIdx, Str+StrIdx),
344 Fn->getSourceRange());
345
346 // This conversion is broken. Advance to the next format
347 // conversion.
348 LastConversionIdx = StrIdx;
349 ++numConversions;
350 }
351
352 break;
353
354 default:
355 // This case catches all other characters: flags, widths, etc.
356 // We should eventually process those as well.
357 break;
358 }
359 }
360
361 if (CurrentState == state_Conversion) {
362 // Issue a warning: invalid format conversion.
363 SourceLocation Loc =
364 PP.AdvanceToTokenCharacter(Args[format_idx]->getLocStart(),
365 LastConversionIdx+1);
366
367 Diag(Loc, diag::warn_printf_invalid_conversion,
Chris Lattner6f65d202007-08-26 17:38:22 +0000368 std::string(Str+LastConversionIdx,
369 Str+std::min(LastConversionIdx+2, StrLen)),
Ted Kremenek081ed872007-08-14 17:39:48 +0000370 Fn->getSourceRange());
371 return;
372 }
373
374 if (!HasVAListArg) {
375 // CHECK: Does the number of format conversions exceed the number
376 // of data arguments?
377 if (numConversions > numDataArgs) {
378 SourceLocation Loc =
379 PP.AdvanceToTokenCharacter(Args[format_idx]->getLocStart(),
380 LastConversionIdx);
381
382 Diag(Loc, diag::warn_printf_insufficient_data_args,
383 Fn->getSourceRange());
384 }
385 // CHECK: Does the number of data arguments exceed the number of
386 // format conversions in the format string?
387 else if (numConversions < numDataArgs)
388 Diag(Args[format_idx+numConversions+1]->getLocStart(),
389 diag::warn_printf_too_many_data_args, Fn->getSourceRange());
390 }
391}
Ted Kremenek45925ab2007-08-17 16:46:58 +0000392
393//===--- CHECK: Return Address of Stack Variable --------------------------===//
394
395static DeclRefExpr* EvalVal(Expr *E);
396static DeclRefExpr* EvalAddr(Expr* E);
397
398/// CheckReturnStackAddr - Check if a return statement returns the address
399/// of a stack variable.
400void
401Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
402 SourceLocation ReturnLoc) {
403
404 // Perform checking for returned stack addresses.
405 if (lhsType->isPointerType()) {
406 if (DeclRefExpr *DR = EvalAddr(RetValExp))
407 Diag(DR->getLocStart(), diag::warn_ret_stack_addr,
408 DR->getDecl()->getIdentifier()->getName(),
409 RetValExp->getSourceRange());
410 }
411 // Perform checking for stack values returned by reference.
412 else if (lhsType->isReferenceType()) {
413 if (DeclRefExpr *DR = EvalVal(RetValExp))
414 Diag(DR->getLocStart(), diag::warn_ret_stack_ref,
415 DR->getDecl()->getIdentifier()->getName(),
416 RetValExp->getSourceRange());
417 }
418}
419
420/// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that
421/// check if the expression in a return statement evaluates to an address
422/// to a location on the stack. The recursion is used to traverse the
423/// AST of the return expression, with recursion backtracking when we
424/// encounter a subexpression that (1) clearly does not lead to the address
425/// of a stack variable or (2) is something we cannot determine leads to
426/// the address of a stack variable based on such local checking.
427///
428/// EvalAddr processes expressions that are pointers, and EvalVal handles
429/// expressions that are rvalues or variable references.
430/// At the base case of the recursion is a check for a DeclRefExpr* in
431/// the refers to a stack variable.
432///
433/// This implementation handles:
434///
435/// * pointer-to-pointer casts
436/// * implicit conversions from array references to pointers
437/// * taking the address of fields
438/// * arbitrary interplay between "&" and "*" operators
439/// * pointer arithmetic from an address of a stack variable
440/// * taking the address of an array element where the array is on the stack
441static DeclRefExpr* EvalAddr(Expr *E) {
442
443 // We should only be called for evaluating pointer expressions.
444 assert (E->getType()->isPointerType() && "EvalAddr only works on pointers");
445
446 // Our "symbolic interpreter" is just a dispatch off the currently
447 // viewed AST node. We then recursively traverse the AST by calling
448 // EvalAddr and EvalVal appropriately.
449 switch (E->getStmtClass()) {
450
451 case Stmt::ParenExprClass:
452 // Ignore parentheses.
453 return EvalAddr(cast<ParenExpr>(E)->getSubExpr());
454
455 case Stmt::UnaryOperatorClass: {
456 // The only unary operator that make sense to handle here
457 // is AddrOf. All others don't make sense as pointers.
458 UnaryOperator *U = cast<UnaryOperator>(E);
459
460 if (U->getOpcode() == UnaryOperator::AddrOf)
461 return EvalVal(U->getSubExpr());
462 else
463 return NULL;
464 }
465
466 case Stmt::BinaryOperatorClass: {
467 // Handle pointer arithmetic. All other binary operators are not valid
468 // in this context.
469 BinaryOperator *B = cast<BinaryOperator>(E);
470 BinaryOperator::Opcode op = B->getOpcode();
471
472 if (op != BinaryOperator::Add && op != BinaryOperator::Sub)
473 return NULL;
474
475 Expr *Base = B->getLHS();
476
477 // Determine which argument is the real pointer base. It could be
478 // the RHS argument instead of the LHS.
479 if (!Base->getType()->isPointerType()) Base = B->getRHS();
480
481 assert (Base->getType()->isPointerType());
482 return EvalAddr(Base);
483 }
484
485 // For conditional operators we need to see if either the LHS or RHS are
486 // valid DeclRefExpr*s. If one of them is valid, we return it.
487 case Stmt::ConditionalOperatorClass: {
488 ConditionalOperator *C = cast<ConditionalOperator>(E);
489
490 if (DeclRefExpr* LHS = EvalAddr(C->getLHS()))
491 return LHS;
492 else
493 return EvalAddr(C->getRHS());
494 }
495
496 // For implicit casts, we need to handle conversions from arrays to
497 // pointer values, and implicit pointer-to-pointer conversions.
498 case Stmt::ImplicitCastExprClass: {
499 ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E);
500 Expr* SubExpr = IE->getSubExpr();
501
502 if (SubExpr->getType()->isPointerType())
503 return EvalAddr(SubExpr);
504 else
505 return EvalVal(SubExpr);
506 }
507
508 // For casts, we handle pointer-to-pointer conversions (which
509 // is essentially a no-op from our mini-interpreter's standpoint).
510 // For other casts we abort.
511 case Stmt::CastExprClass: {
512 CastExpr *C = cast<CastExpr>(E);
513 Expr *SubExpr = C->getSubExpr();
514
515 if (SubExpr->getType()->isPointerType())
516 return EvalAddr(SubExpr);
517 else
518 return NULL;
519 }
520
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000521 // C++ casts. For dynamic casts, static casts, and const casts, we
522 // are always converting from a pointer-to-pointer, so we just blow
523 // through the cast. In the case the dynamic cast doesn't fail
524 // (and return NULL), we take the conservative route and report cases
525 // where we return the address of a stack variable. For Reinterpre
526 case Stmt::CXXCastExprClass: {
527 CXXCastExpr *C = cast<CXXCastExpr>(E);
528
529 if (C->getOpcode() == CXXCastExpr::ReinterpretCast) {
530 Expr *S = C->getSubExpr();
531 if (S->getType()->isPointerType())
532 return EvalAddr(S);
533 else
534 return NULL;
535 }
536 else
537 return EvalAddr(C->getSubExpr());
538 }
Ted Kremenek45925ab2007-08-17 16:46:58 +0000539
540 // Everything else: we simply don't reason about them.
541 default:
542 return NULL;
543 }
544}
545
546
547/// EvalVal - This function is complements EvalAddr in the mutual recursion.
548/// See the comments for EvalAddr for more details.
549static DeclRefExpr* EvalVal(Expr *E) {
550
551 // We should only be called for evaluating non-pointer expressions.
552 assert (!E->getType()->isPointerType() && "EvalVal doesn't work on pointers");
553
554 // Our "symbolic interpreter" is just a dispatch off the currently
555 // viewed AST node. We then recursively traverse the AST by calling
556 // EvalAddr and EvalVal appropriately.
557 switch (E->getStmtClass()) {
558
559 case Stmt::DeclRefExprClass: {
560 // DeclRefExpr: the base case. When we hit a DeclRefExpr we are looking
561 // at code that refers to a variable's name. We check if it has local
562 // storage within the function, and if so, return the expression.
563 DeclRefExpr *DR = cast<DeclRefExpr>(E);
564
565 if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
566 if(V->hasLocalStorage()) return DR;
567
568 return NULL;
569 }
570
571 case Stmt::ParenExprClass:
572 // Ignore parentheses.
573 return EvalVal(cast<ParenExpr>(E)->getSubExpr());
574
575 case Stmt::UnaryOperatorClass: {
576 // The only unary operator that make sense to handle here
577 // is Deref. All others don't resolve to a "name." This includes
578 // handling all sorts of rvalues passed to a unary operator.
579 UnaryOperator *U = cast<UnaryOperator>(E);
580
581 if (U->getOpcode() == UnaryOperator::Deref)
582 return EvalAddr(U->getSubExpr());
583
584 return NULL;
585 }
586
587 case Stmt::ArraySubscriptExprClass: {
588 // Array subscripts are potential references to data on the stack. We
589 // retrieve the DeclRefExpr* for the array variable if it indeed
590 // has local storage.
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000591 return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase());
Ted Kremenek45925ab2007-08-17 16:46:58 +0000592 }
593
594 case Stmt::ConditionalOperatorClass: {
595 // For conditional operators we need to see if either the LHS or RHS are
596 // non-NULL DeclRefExpr's. If one is non-NULL, we return it.
597 ConditionalOperator *C = cast<ConditionalOperator>(E);
598
599 if (DeclRefExpr *LHS = EvalVal(C->getLHS()))
600 return LHS;
601 else
602 return EvalVal(C->getRHS());
603 }
604
605 // Accesses to members are potential references to data on the stack.
606 case Stmt::MemberExprClass: {
607 MemberExpr *M = cast<MemberExpr>(E);
608
609 // Check for indirect access. We only want direct field accesses.
610 if (!M->isArrow())
611 return EvalVal(M->getBase());
612 else
613 return NULL;
614 }
615
616 // Everything else: we simply don't reason about them.
617 default:
618 return NULL;
619 }
620}