blob: cc682af99f070e99534019e997db105c64310119 [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':
311 case 'P':
312 ++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,
Ted Kremenek45925ab2007-08-17 16:46:58 +0000368 std::string(Str+LastConversionIdx, Str+StrIdx),
Ted Kremenek081ed872007-08-14 17:39:48 +0000369 Fn->getSourceRange());
370 return;
371 }
372
373 if (!HasVAListArg) {
374 // CHECK: Does the number of format conversions exceed the number
375 // of data arguments?
376 if (numConversions > numDataArgs) {
377 SourceLocation Loc =
378 PP.AdvanceToTokenCharacter(Args[format_idx]->getLocStart(),
379 LastConversionIdx);
380
381 Diag(Loc, diag::warn_printf_insufficient_data_args,
382 Fn->getSourceRange());
383 }
384 // CHECK: Does the number of data arguments exceed the number of
385 // format conversions in the format string?
386 else if (numConversions < numDataArgs)
387 Diag(Args[format_idx+numConversions+1]->getLocStart(),
388 diag::warn_printf_too_many_data_args, Fn->getSourceRange());
389 }
390}
Ted Kremenek45925ab2007-08-17 16:46:58 +0000391
392//===--- CHECK: Return Address of Stack Variable --------------------------===//
393
394static DeclRefExpr* EvalVal(Expr *E);
395static DeclRefExpr* EvalAddr(Expr* E);
396
397/// CheckReturnStackAddr - Check if a return statement returns the address
398/// of a stack variable.
399void
400Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
401 SourceLocation ReturnLoc) {
402
403 // Perform checking for returned stack addresses.
404 if (lhsType->isPointerType()) {
405 if (DeclRefExpr *DR = EvalAddr(RetValExp))
406 Diag(DR->getLocStart(), diag::warn_ret_stack_addr,
407 DR->getDecl()->getIdentifier()->getName(),
408 RetValExp->getSourceRange());
409 }
410 // Perform checking for stack values returned by reference.
411 else if (lhsType->isReferenceType()) {
412 if (DeclRefExpr *DR = EvalVal(RetValExp))
413 Diag(DR->getLocStart(), diag::warn_ret_stack_ref,
414 DR->getDecl()->getIdentifier()->getName(),
415 RetValExp->getSourceRange());
416 }
417}
418
419/// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that
420/// check if the expression in a return statement evaluates to an address
421/// to a location on the stack. The recursion is used to traverse the
422/// AST of the return expression, with recursion backtracking when we
423/// encounter a subexpression that (1) clearly does not lead to the address
424/// of a stack variable or (2) is something we cannot determine leads to
425/// the address of a stack variable based on such local checking.
426///
427/// EvalAddr processes expressions that are pointers, and EvalVal handles
428/// expressions that are rvalues or variable references.
429/// At the base case of the recursion is a check for a DeclRefExpr* in
430/// the refers to a stack variable.
431///
432/// This implementation handles:
433///
434/// * pointer-to-pointer casts
435/// * implicit conversions from array references to pointers
436/// * taking the address of fields
437/// * arbitrary interplay between "&" and "*" operators
438/// * pointer arithmetic from an address of a stack variable
439/// * taking the address of an array element where the array is on the stack
440static DeclRefExpr* EvalAddr(Expr *E) {
441
442 // We should only be called for evaluating pointer expressions.
443 assert (E->getType()->isPointerType() && "EvalAddr only works on pointers");
444
445 // Our "symbolic interpreter" is just a dispatch off the currently
446 // viewed AST node. We then recursively traverse the AST by calling
447 // EvalAddr and EvalVal appropriately.
448 switch (E->getStmtClass()) {
449
450 case Stmt::ParenExprClass:
451 // Ignore parentheses.
452 return EvalAddr(cast<ParenExpr>(E)->getSubExpr());
453
454 case Stmt::UnaryOperatorClass: {
455 // The only unary operator that make sense to handle here
456 // is AddrOf. All others don't make sense as pointers.
457 UnaryOperator *U = cast<UnaryOperator>(E);
458
459 if (U->getOpcode() == UnaryOperator::AddrOf)
460 return EvalVal(U->getSubExpr());
461 else
462 return NULL;
463 }
464
465 case Stmt::BinaryOperatorClass: {
466 // Handle pointer arithmetic. All other binary operators are not valid
467 // in this context.
468 BinaryOperator *B = cast<BinaryOperator>(E);
469 BinaryOperator::Opcode op = B->getOpcode();
470
471 if (op != BinaryOperator::Add && op != BinaryOperator::Sub)
472 return NULL;
473
474 Expr *Base = B->getLHS();
475
476 // Determine which argument is the real pointer base. It could be
477 // the RHS argument instead of the LHS.
478 if (!Base->getType()->isPointerType()) Base = B->getRHS();
479
480 assert (Base->getType()->isPointerType());
481 return EvalAddr(Base);
482 }
483
484 // For conditional operators we need to see if either the LHS or RHS are
485 // valid DeclRefExpr*s. If one of them is valid, we return it.
486 case Stmt::ConditionalOperatorClass: {
487 ConditionalOperator *C = cast<ConditionalOperator>(E);
488
489 if (DeclRefExpr* LHS = EvalAddr(C->getLHS()))
490 return LHS;
491 else
492 return EvalAddr(C->getRHS());
493 }
494
495 // For implicit casts, we need to handle conversions from arrays to
496 // pointer values, and implicit pointer-to-pointer conversions.
497 case Stmt::ImplicitCastExprClass: {
498 ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E);
499 Expr* SubExpr = IE->getSubExpr();
500
501 if (SubExpr->getType()->isPointerType())
502 return EvalAddr(SubExpr);
503 else
504 return EvalVal(SubExpr);
505 }
506
507 // For casts, we handle pointer-to-pointer conversions (which
508 // is essentially a no-op from our mini-interpreter's standpoint).
509 // For other casts we abort.
510 case Stmt::CastExprClass: {
511 CastExpr *C = cast<CastExpr>(E);
512 Expr *SubExpr = C->getSubExpr();
513
514 if (SubExpr->getType()->isPointerType())
515 return EvalAddr(SubExpr);
516 else
517 return NULL;
518 }
519
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000520 // C++ casts. For dynamic casts, static casts, and const casts, we
521 // are always converting from a pointer-to-pointer, so we just blow
522 // through the cast. In the case the dynamic cast doesn't fail
523 // (and return NULL), we take the conservative route and report cases
524 // where we return the address of a stack variable. For Reinterpre
525 case Stmt::CXXCastExprClass: {
526 CXXCastExpr *C = cast<CXXCastExpr>(E);
527
528 if (C->getOpcode() == CXXCastExpr::ReinterpretCast) {
529 Expr *S = C->getSubExpr();
530 if (S->getType()->isPointerType())
531 return EvalAddr(S);
532 else
533 return NULL;
534 }
535 else
536 return EvalAddr(C->getSubExpr());
537 }
Ted Kremenek45925ab2007-08-17 16:46:58 +0000538
539 // Everything else: we simply don't reason about them.
540 default:
541 return NULL;
542 }
543}
544
545
546/// EvalVal - This function is complements EvalAddr in the mutual recursion.
547/// See the comments for EvalAddr for more details.
548static DeclRefExpr* EvalVal(Expr *E) {
549
550 // We should only be called for evaluating non-pointer expressions.
551 assert (!E->getType()->isPointerType() && "EvalVal doesn't work on pointers");
552
553 // Our "symbolic interpreter" is just a dispatch off the currently
554 // viewed AST node. We then recursively traverse the AST by calling
555 // EvalAddr and EvalVal appropriately.
556 switch (E->getStmtClass()) {
557
558 case Stmt::DeclRefExprClass: {
559 // DeclRefExpr: the base case. When we hit a DeclRefExpr we are looking
560 // at code that refers to a variable's name. We check if it has local
561 // storage within the function, and if so, return the expression.
562 DeclRefExpr *DR = cast<DeclRefExpr>(E);
563
564 if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
565 if(V->hasLocalStorage()) return DR;
566
567 return NULL;
568 }
569
570 case Stmt::ParenExprClass:
571 // Ignore parentheses.
572 return EvalVal(cast<ParenExpr>(E)->getSubExpr());
573
574 case Stmt::UnaryOperatorClass: {
575 // The only unary operator that make sense to handle here
576 // is Deref. All others don't resolve to a "name." This includes
577 // handling all sorts of rvalues passed to a unary operator.
578 UnaryOperator *U = cast<UnaryOperator>(E);
579
580 if (U->getOpcode() == UnaryOperator::Deref)
581 return EvalAddr(U->getSubExpr());
582
583 return NULL;
584 }
585
586 case Stmt::ArraySubscriptExprClass: {
587 // Array subscripts are potential references to data on the stack. We
588 // retrieve the DeclRefExpr* for the array variable if it indeed
589 // has local storage.
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000590 return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase());
Ted Kremenek45925ab2007-08-17 16:46:58 +0000591 }
592
593 case Stmt::ConditionalOperatorClass: {
594 // For conditional operators we need to see if either the LHS or RHS are
595 // non-NULL DeclRefExpr's. If one is non-NULL, we return it.
596 ConditionalOperator *C = cast<ConditionalOperator>(E);
597
598 if (DeclRefExpr *LHS = EvalVal(C->getLHS()))
599 return LHS;
600 else
601 return EvalVal(C->getRHS());
602 }
603
604 // Accesses to members are potential references to data on the stack.
605 case Stmt::MemberExprClass: {
606 MemberExpr *M = cast<MemberExpr>(E);
607
608 // Check for indirect access. We only want direct field accesses.
609 if (!M->isArrow())
610 return EvalVal(M->getBase());
611 else
612 return NULL;
613 }
614
615 // Everything else: we simply don't reason about them.
616 default:
617 return NULL;
618 }
619}