blob: 863882ec98609fda45c30b9732bdbce994bd0973 [file] [log] [blame]
Chris Lattner59907c42007-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 Kremenek23245122007-08-20 16:18:38 +000019#include "clang/AST/ExprCXX.h"
Chris Lattner59907c42007-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 Carlsson71993dd2007-08-17 05:31:46 +000032bool
Ted Kremenek71895b92007-08-14 17:39:48 +000033Sema::CheckFunctionCall(Expr *Fn,
34 SourceLocation LParenLoc, SourceLocation RParenLoc,
35 FunctionDecl *FDecl,
Chris Lattner59907c42007-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 Carlsson71993dd2007-08-17 05:31:46 +000041 if (FnInfo->getBuiltinID() ==
42 Builtin::BI__builtin___CFStringMakeConstantString) {
43 assert(NumArgsInCall == 1 &&
Chris Lattner6f4b92c2007-08-30 17:08:17 +000044 "Wrong number of arguments to builtin CFStringMakeConstantString");
Anders Carlsson71993dd2007-08-17 05:31:46 +000045 return CheckBuiltinCFStringArgument(Args[0]);
46 }
47
Chris Lattner59907c42007-08-10 20:18:51 +000048 // Search the KnownFunctionIDs for the identifier.
49 unsigned i = 0, e = id_num_known_functions;
Ted Kremenek71895b92007-08-14 17:39:48 +000050 for (; i != e; ++i) { if (KnownFunctionIDs[i] == FnInfo) break; }
Anders Carlsson9cdc4d32007-08-17 15:44:17 +000051 if (i == e) return false;
Chris Lattner59907c42007-08-10 20:18:51 +000052
53 // Printf checking.
54 if (i <= id_vprintf) {
Ted Kremenek71895b92007-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 Lattner59907c42007-08-10 20:18:51 +000057 unsigned format_idx = 0;
Ted Kremenek71895b92007-08-14 17:39:48 +000058 bool HasVAListArg = false;
59
Chris Lattner59907c42007-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 Kremenek71895b92007-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 Kremenek06de2762007-08-17 16:46:58 +000075 FDecl, format_idx, Args, NumArgsInCall);
Chris Lattner59907c42007-08-10 20:18:51 +000076 }
Anders Carlsson71993dd2007-08-17 05:31:46 +000077
Anders Carlsson9cdc4d32007-08-17 15:44:17 +000078 return false;
Anders Carlsson71993dd2007-08-17 05:31:46 +000079}
80
81/// CheckBuiltinCFStringArgument - Checks that the argument to the builtin
82/// CFString constructor is correct
Chris Lattnercc6f65d2007-08-25 05:30:33 +000083bool Sema::CheckBuiltinCFStringArgument(Expr* Arg) {
Chris Lattner459e8482007-08-25 05:36:18 +000084 // FIXME: This should go in a helper.
Chris Lattnercc6f65d2007-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 Carlsson71993dd2007-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 Carlsson9cdc4d32007-08-17 15:44:17 +0000100 return true;
Anders Carlsson71993dd2007-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 Carlsson9cdc4d32007-08-17 15:44:17 +0000122 return false;
Chris Lattner59907c42007-08-10 20:18:51 +0000123}
124
125/// CheckPrintfArguments - Check calls to printf (and similar functions) for
Ted Kremenek71895b92007-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 Lattner59907c42007-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 Kremenek71895b92007-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 Lattner59907c42007-08-10 20:18:51 +0000171void
Ted Kremenek71895b92007-08-14 17:39:48 +0000172Sema::CheckPrintfArguments(Expr *Fn,
173 SourceLocation LParenLoc, SourceLocation RParenLoc,
174 bool HasVAListArg, FunctionDecl *FDecl,
Ted Kremenek82077102007-08-10 21:21:05 +0000175 unsigned format_idx, Expr** Args,
176 unsigned NumArgsInCall) {
Ted Kremenek71895b92007-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 Lattner459e8482007-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 Lattner59907c42007-08-10 20:18:51 +0000195 // CHECK: format string is not a string literal.
196 //
Ted Kremenek71895b92007-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 Lattner459e8482007-08-25 05:36:18 +0000202 StringLiteral *FExpr = dyn_cast<StringLiteral>(OrigFormatExpr);
Chris Lattner59907c42007-08-10 20:18:51 +0000203
Ted Kremenek71895b92007-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 Lattner5e9885d2007-08-26 17:39:38 +0000311 case 'p':
Ted Kremenek71895b92007-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 Lattnera9e2ea12007-08-26 17:38:22 +0000368 std::string(Str+LastConversionIdx,
369 Str+std::min(LastConversionIdx+2, StrLen)),
Ted Kremenek71895b92007-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 Kremenek06de2762007-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()) {
Ted Kremenek96eabe02007-08-27 16:39:17 +0000413 // Check for an implicit cast to a reference.
414 if (ImplicitCastExpr *I = dyn_cast<ImplicitCastExpr>(RetValExp))
415 if (DeclRefExpr *DR = EvalVal(I->getSubExpr()))
416 Diag(DR->getLocStart(), diag::warn_ret_stack_ref,
417 DR->getDecl()->getIdentifier()->getName(),
418 RetValExp->getSourceRange());
Ted Kremenek06de2762007-08-17 16:46:58 +0000419 }
420}
421
422/// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that
423/// check if the expression in a return statement evaluates to an address
424/// to a location on the stack. The recursion is used to traverse the
425/// AST of the return expression, with recursion backtracking when we
426/// encounter a subexpression that (1) clearly does not lead to the address
427/// of a stack variable or (2) is something we cannot determine leads to
428/// the address of a stack variable based on such local checking.
429///
Ted Kremeneke8c600f2007-08-28 17:02:55 +0000430/// EvalAddr processes expressions that are pointers that are used as
431/// references (and not L-values). EvalVal handles all other values.
Ted Kremenek06de2762007-08-17 16:46:58 +0000432/// At the base case of the recursion is a check for a DeclRefExpr* in
433/// the refers to a stack variable.
434///
435/// This implementation handles:
436///
437/// * pointer-to-pointer casts
438/// * implicit conversions from array references to pointers
439/// * taking the address of fields
440/// * arbitrary interplay between "&" and "*" operators
441/// * pointer arithmetic from an address of a stack variable
442/// * taking the address of an array element where the array is on the stack
443static DeclRefExpr* EvalAddr(Expr *E) {
444
445 // We should only be called for evaluating pointer expressions.
446 assert (E->getType()->isPointerType() && "EvalAddr only works on pointers");
447
448 // Our "symbolic interpreter" is just a dispatch off the currently
449 // viewed AST node. We then recursively traverse the AST by calling
450 // EvalAddr and EvalVal appropriately.
451 switch (E->getStmtClass()) {
452
453 case Stmt::ParenExprClass:
454 // Ignore parentheses.
455 return EvalAddr(cast<ParenExpr>(E)->getSubExpr());
456
457 case Stmt::UnaryOperatorClass: {
458 // The only unary operator that make sense to handle here
459 // is AddrOf. All others don't make sense as pointers.
460 UnaryOperator *U = cast<UnaryOperator>(E);
461
462 if (U->getOpcode() == UnaryOperator::AddrOf)
463 return EvalVal(U->getSubExpr());
464 else
465 return NULL;
466 }
467
468 case Stmt::BinaryOperatorClass: {
469 // Handle pointer arithmetic. All other binary operators are not valid
470 // in this context.
471 BinaryOperator *B = cast<BinaryOperator>(E);
472 BinaryOperator::Opcode op = B->getOpcode();
473
474 if (op != BinaryOperator::Add && op != BinaryOperator::Sub)
475 return NULL;
476
477 Expr *Base = B->getLHS();
478
479 // Determine which argument is the real pointer base. It could be
480 // the RHS argument instead of the LHS.
481 if (!Base->getType()->isPointerType()) Base = B->getRHS();
482
483 assert (Base->getType()->isPointerType());
484 return EvalAddr(Base);
485 }
486
487 // For conditional operators we need to see if either the LHS or RHS are
488 // valid DeclRefExpr*s. If one of them is valid, we return it.
489 case Stmt::ConditionalOperatorClass: {
490 ConditionalOperator *C = cast<ConditionalOperator>(E);
491
492 if (DeclRefExpr* LHS = EvalAddr(C->getLHS()))
493 return LHS;
494 else
495 return EvalAddr(C->getRHS());
496 }
497
498 // For implicit casts, we need to handle conversions from arrays to
499 // pointer values, and implicit pointer-to-pointer conversions.
500 case Stmt::ImplicitCastExprClass: {
501 ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E);
502 Expr* SubExpr = IE->getSubExpr();
503
504 if (SubExpr->getType()->isPointerType())
505 return EvalAddr(SubExpr);
506 else
507 return EvalVal(SubExpr);
508 }
509
510 // For casts, we handle pointer-to-pointer conversions (which
511 // is essentially a no-op from our mini-interpreter's standpoint).
512 // For other casts we abort.
513 case Stmt::CastExprClass: {
514 CastExpr *C = cast<CastExpr>(E);
515 Expr *SubExpr = C->getSubExpr();
516
517 if (SubExpr->getType()->isPointerType())
518 return EvalAddr(SubExpr);
519 else
520 return NULL;
521 }
522
Ted Kremenek23245122007-08-20 16:18:38 +0000523 // C++ casts. For dynamic casts, static casts, and const casts, we
524 // are always converting from a pointer-to-pointer, so we just blow
525 // through the cast. In the case the dynamic cast doesn't fail
526 // (and return NULL), we take the conservative route and report cases
527 // where we return the address of a stack variable. For Reinterpre
528 case Stmt::CXXCastExprClass: {
529 CXXCastExpr *C = cast<CXXCastExpr>(E);
530
531 if (C->getOpcode() == CXXCastExpr::ReinterpretCast) {
532 Expr *S = C->getSubExpr();
533 if (S->getType()->isPointerType())
534 return EvalAddr(S);
535 else
536 return NULL;
537 }
538 else
539 return EvalAddr(C->getSubExpr());
540 }
Ted Kremenek06de2762007-08-17 16:46:58 +0000541
542 // Everything else: we simply don't reason about them.
543 default:
544 return NULL;
545 }
546}
547
548
549/// EvalVal - This function is complements EvalAddr in the mutual recursion.
550/// See the comments for EvalAddr for more details.
551static DeclRefExpr* EvalVal(Expr *E) {
552
Ted Kremeneke8c600f2007-08-28 17:02:55 +0000553 // We should only be called for evaluating non-pointer expressions, or
554 // expressions with a pointer type that are not used as references but instead
555 // are l-values (e.g., DeclRefExpr with a pointer type).
556
Ted Kremenek06de2762007-08-17 16:46:58 +0000557 // Our "symbolic interpreter" is just a dispatch off the currently
558 // viewed AST node. We then recursively traverse the AST by calling
559 // EvalAddr and EvalVal appropriately.
560 switch (E->getStmtClass()) {
561
562 case Stmt::DeclRefExprClass: {
563 // DeclRefExpr: the base case. When we hit a DeclRefExpr we are looking
564 // at code that refers to a variable's name. We check if it has local
565 // storage within the function, and if so, return the expression.
566 DeclRefExpr *DR = cast<DeclRefExpr>(E);
567
568 if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
569 if(V->hasLocalStorage()) return DR;
570
571 return NULL;
572 }
573
574 case Stmt::ParenExprClass:
575 // Ignore parentheses.
576 return EvalVal(cast<ParenExpr>(E)->getSubExpr());
577
578 case Stmt::UnaryOperatorClass: {
579 // The only unary operator that make sense to handle here
580 // is Deref. All others don't resolve to a "name." This includes
581 // handling all sorts of rvalues passed to a unary operator.
582 UnaryOperator *U = cast<UnaryOperator>(E);
583
584 if (U->getOpcode() == UnaryOperator::Deref)
585 return EvalAddr(U->getSubExpr());
586
587 return NULL;
588 }
589
590 case Stmt::ArraySubscriptExprClass: {
591 // Array subscripts are potential references to data on the stack. We
592 // retrieve the DeclRefExpr* for the array variable if it indeed
593 // has local storage.
Ted Kremenek23245122007-08-20 16:18:38 +0000594 return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase());
Ted Kremenek06de2762007-08-17 16:46:58 +0000595 }
596
597 case Stmt::ConditionalOperatorClass: {
598 // For conditional operators we need to see if either the LHS or RHS are
599 // non-NULL DeclRefExpr's. If one is non-NULL, we return it.
600 ConditionalOperator *C = cast<ConditionalOperator>(E);
601
602 if (DeclRefExpr *LHS = EvalVal(C->getLHS()))
603 return LHS;
604 else
605 return EvalVal(C->getRHS());
606 }
607
608 // Accesses to members are potential references to data on the stack.
609 case Stmt::MemberExprClass: {
610 MemberExpr *M = cast<MemberExpr>(E);
611
612 // Check for indirect access. We only want direct field accesses.
613 if (!M->isArrow())
614 return EvalVal(M->getBase());
615 else
616 return NULL;
617 }
618
619 // Everything else: we simply don't reason about them.
620 default:
621 return NULL;
622 }
623}