Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1 | //===--- SemaChecking.cpp - Extra Semantic Checking -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 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" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprCXX.h" |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprObjC.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
| 23 | /// CheckFunctionCall - Check a direct function call for various correctness |
| 24 | /// and safety properties not strictly enforced by the C type system. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 25 | Action::OwningExprResult |
| 26 | Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall) { |
| 27 | OwningExprResult TheCallResult(Owned(TheCall)); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 28 | // Get the IdentifierInfo* for the called function. |
| 29 | IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 30 | |
| 31 | // None of the checks below are needed for functions that don't have |
| 32 | // simple names (e.g., C++ conversion functions). |
| 33 | if (!FnInfo) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 34 | return move(TheCallResult); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 35 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 36 | switch (FDecl->getBuiltinID(Context)) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 37 | case Builtin::BI__builtin___CFStringMakeConstantString: |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 38 | assert(TheCall->getNumArgs() == 1 && |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 39 | "Wrong # arguments to builtin CFStringMakeConstantString"); |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame^] | 40 | if (CheckObjCString(TheCall->getArg(0))) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 41 | return ExprError(); |
| 42 | return move(TheCallResult); |
Ted Kremenek | 49ff7a1 | 2008-07-09 17:58:53 +0000 | [diff] [blame] | 43 | case Builtin::BI__builtin_stdarg_start: |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 44 | case Builtin::BI__builtin_va_start: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 45 | if (SemaBuiltinVAStart(TheCall)) |
| 46 | return ExprError(); |
| 47 | return move(TheCallResult); |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 48 | case Builtin::BI__builtin_isgreater: |
| 49 | case Builtin::BI__builtin_isgreaterequal: |
| 50 | case Builtin::BI__builtin_isless: |
| 51 | case Builtin::BI__builtin_islessequal: |
| 52 | case Builtin::BI__builtin_islessgreater: |
| 53 | case Builtin::BI__builtin_isunordered: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 54 | if (SemaBuiltinUnorderedCompare(TheCall)) |
| 55 | return ExprError(); |
| 56 | return move(TheCallResult); |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 57 | case Builtin::BI__builtin_return_address: |
| 58 | case Builtin::BI__builtin_frame_address: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 59 | if (SemaBuiltinStackAddress(TheCall)) |
| 60 | return ExprError(); |
| 61 | return move(TheCallResult); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 62 | case Builtin::BI__builtin_shufflevector: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 63 | return SemaBuiltinShuffleVector(TheCall); |
| 64 | // TheCall will be freed by the smart pointer here, but that's fine, since |
| 65 | // SemaBuiltinShuffleVector guts it, but then doesn't release it. |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 66 | case Builtin::BI__builtin_prefetch: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 67 | if (SemaBuiltinPrefetch(TheCall)) |
| 68 | return ExprError(); |
| 69 | return move(TheCallResult); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 70 | case Builtin::BI__builtin_object_size: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 71 | if (SemaBuiltinObjectSize(TheCall)) |
| 72 | return ExprError(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 73 | } |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 74 | |
| 75 | // FIXME: This mechanism should be abstracted to be less fragile and |
| 76 | // more efficient. For example, just map function ids to custom |
| 77 | // handlers. |
| 78 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 79 | // Printf checking. |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 80 | if (const FormatAttr *Format = FDecl->getAttr<FormatAttr>()) { |
| 81 | if (Format->getType() == "printf") { |
| 82 | bool HasVAListArg = false; |
| 83 | if (const FunctionTypeProto *Proto |
| 84 | = FDecl->getType()->getAsFunctionTypeProto()) |
| 85 | HasVAListArg = !Proto->isVariadic(); |
| 86 | CheckPrintfArguments(TheCall, HasVAListArg, Format->getFormatIdx() - 1, |
| 87 | Format->getFirstArg() - 1); |
| 88 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 89 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 90 | |
| 91 | return move(TheCallResult); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame^] | 94 | /// CheckObjCString - Checks that the argument to the builtin |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 95 | /// CFString constructor is correct |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame^] | 96 | bool Sema::CheckObjCString(Expr *Arg) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 97 | Arg = Arg->IgnoreParenCasts(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 98 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); |
| 99 | |
| 100 | if (!Literal || Literal->isWide()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 101 | Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 102 | << Arg->getSourceRange(); |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 103 | return true; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | const char *Data = Literal->getStrData(); |
| 107 | unsigned Length = Literal->getByteLength(); |
| 108 | |
| 109 | for (unsigned i = 0; i < Length; ++i) { |
| 110 | if (!isascii(Data[i])) { |
| 111 | Diag(PP.AdvanceToTokenCharacter(Arg->getLocStart(), i + 1), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 112 | diag::warn_cfstring_literal_contains_non_ascii_character) |
| 113 | << Arg->getSourceRange(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 114 | break; |
| 115 | } |
| 116 | |
| 117 | if (!Data[i]) { |
| 118 | Diag(PP.AdvanceToTokenCharacter(Arg->getLocStart(), i + 1), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 119 | diag::warn_cfstring_literal_contains_nul_character) |
| 120 | << Arg->getSourceRange(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 125 | return false; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 128 | /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity. |
| 129 | /// Emit an error and return true on failure, return false on success. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 130 | bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) { |
| 131 | Expr *Fn = TheCall->getCallee(); |
| 132 | if (TheCall->getNumArgs() > 2) { |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 133 | Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 134 | diag::err_typecheck_call_too_many_args) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 135 | << 0 /*function call*/ << Fn->getSourceRange() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 136 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
| 137 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 138 | return true; |
| 139 | } |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 140 | |
| 141 | if (TheCall->getNumArgs() < 2) { |
| 142 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 143 | << 0 /*function call*/; |
| 144 | } |
| 145 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 146 | // Determine whether the current function is variadic or not. |
| 147 | bool isVariadic; |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 148 | if (getCurFunctionDecl()) { |
| 149 | if (FunctionTypeProto* FTP = |
| 150 | dyn_cast<FunctionTypeProto>(getCurFunctionDecl()->getType())) |
| 151 | isVariadic = FTP->isVariadic(); |
| 152 | else |
| 153 | isVariadic = false; |
| 154 | } else { |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 155 | isVariadic = getCurMethodDecl()->isVariadic(); |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 156 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 157 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 158 | if (!isVariadic) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 159 | Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function); |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | // Verify that the second argument to the builtin is the last argument of the |
| 164 | // current function or method. |
| 165 | bool SecondArgIsLastNamedArgument = false; |
Anders Carlsson | e2c1410 | 2008-02-13 01:22:59 +0000 | [diff] [blame] | 166 | const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 167 | |
| 168 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { |
| 169 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 170 | // FIXME: This isn't correct for methods (results in bogus warning). |
| 171 | // Get the last formal in the current function. |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 172 | const ParmVarDecl *LastArg; |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 173 | if (FunctionDecl *FD = getCurFunctionDecl()) |
| 174 | LastArg = *(FD->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 175 | else |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 176 | LastArg = *(getCurMethodDecl()->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 177 | SecondArgIsLastNamedArgument = PV == LastArg; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (!SecondArgIsLastNamedArgument) |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 182 | Diag(TheCall->getArg(1)->getLocStart(), |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 183 | diag::warn_second_parameter_of_va_start_not_last_named_argument); |
| 184 | return false; |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 185 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 187 | /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and |
| 188 | /// friends. This is declared to take (...), so we have to check everything. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 189 | bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { |
| 190 | if (TheCall->getNumArgs() < 2) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 191 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 192 | << 0 /*function call*/; |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 193 | if (TheCall->getNumArgs() > 2) |
| 194 | return Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 195 | diag::err_typecheck_call_too_many_args) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 196 | << 0 /*function call*/ |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 197 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
| 198 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 200 | Expr *OrigArg0 = TheCall->getArg(0); |
| 201 | Expr *OrigArg1 = TheCall->getArg(1); |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 202 | |
| 203 | // Do standard promotions between the two arguments, returning their common |
| 204 | // type. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 205 | QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 206 | |
| 207 | // If the common type isn't a real floating type, then the arguments were |
| 208 | // invalid for this operation. |
| 209 | if (!Res->isRealFloatingType()) |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 210 | return Diag(OrigArg0->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 211 | diag::err_typecheck_call_invalid_ordered_compare) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 212 | << OrigArg0->getType() << OrigArg1->getType() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 213 | << SourceRange(OrigArg0->getLocStart(), OrigArg1->getLocEnd()); |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 214 | |
| 215 | return false; |
| 216 | } |
| 217 | |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 218 | bool Sema::SemaBuiltinStackAddress(CallExpr *TheCall) { |
| 219 | // The signature for these builtins is exact; the only thing we need |
| 220 | // to check is that the argument is a constant. |
| 221 | SourceLocation Loc; |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 222 | if (!TheCall->getArg(0)->isIntegerConstantExpr(Context, &Loc)) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 223 | return Diag(Loc, diag::err_stack_const_level) << TheCall->getSourceRange(); |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 224 | |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 225 | return false; |
| 226 | } |
| 227 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 228 | /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. |
| 229 | // This is declared to take (...), so we have to check everything. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 230 | Action::OwningExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 231 | if (TheCall->getNumArgs() < 3) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 232 | return ExprError(Diag(TheCall->getLocEnd(), |
| 233 | diag::err_typecheck_call_too_few_args) |
| 234 | << 0 /*function call*/ << TheCall->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 235 | |
| 236 | QualType FAType = TheCall->getArg(0)->getType(); |
| 237 | QualType SAType = TheCall->getArg(1)->getType(); |
| 238 | |
| 239 | if (!FAType->isVectorType() || !SAType->isVectorType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 240 | Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector) |
| 241 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
| 242 | TheCall->getArg(1)->getLocEnd()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 243 | return ExprError(); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 246 | if (Context.getCanonicalType(FAType).getUnqualifiedType() != |
| 247 | Context.getCanonicalType(SAType).getUnqualifiedType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 248 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
| 249 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
| 250 | TheCall->getArg(1)->getLocEnd()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 251 | return ExprError(); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | unsigned numElements = FAType->getAsVectorType()->getNumElements(); |
| 255 | if (TheCall->getNumArgs() != numElements+2) { |
| 256 | if (TheCall->getNumArgs() < numElements+2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 257 | return ExprError(Diag(TheCall->getLocEnd(), |
| 258 | diag::err_typecheck_call_too_few_args) |
| 259 | << 0 /*function call*/ << TheCall->getSourceRange()); |
| 260 | return ExprError(Diag(TheCall->getLocEnd(), |
| 261 | diag::err_typecheck_call_too_many_args) |
| 262 | << 0 /*function call*/ << TheCall->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { |
| 266 | llvm::APSInt Result(32); |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 267 | if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 268 | return ExprError(Diag(TheCall->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 269 | diag::err_shufflevector_nonconstant_argument) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 270 | << TheCall->getArg(i)->getSourceRange()); |
| 271 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 272 | if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 273 | return ExprError(Diag(TheCall->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 274 | diag::err_shufflevector_argument_too_large) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 275 | << TheCall->getArg(i)->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | llvm::SmallVector<Expr*, 32> exprs; |
| 279 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 280 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 281 | exprs.push_back(TheCall->getArg(i)); |
| 282 | TheCall->setArg(i, 0); |
| 283 | } |
| 284 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 285 | return Owned(new (Context) ShuffleVectorExpr(exprs.begin(), numElements+2, |
| 286 | FAType, |
| 287 | TheCall->getCallee()->getLocStart(), |
| 288 | TheCall->getRParenLoc())); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 289 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 290 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 291 | /// SemaBuiltinPrefetch - Handle __builtin_prefetch. |
| 292 | // This is declared to take (const void*, ...) and can take two |
| 293 | // optional constant int args. |
| 294 | bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 295 | unsigned NumArgs = TheCall->getNumArgs(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 296 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 297 | if (NumArgs > 3) |
| 298 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_many_args) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 299 | << 0 /*function call*/ << TheCall->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 300 | |
| 301 | // Argument 0 is checked for us and the remaining arguments must be |
| 302 | // constant integers. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 303 | for (unsigned i = 1; i != NumArgs; ++i) { |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 304 | Expr *Arg = TheCall->getArg(i); |
| 305 | QualType RWType = Arg->getType(); |
| 306 | |
| 307 | const BuiltinType *BT = RWType->getAsBuiltinType(); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 308 | llvm::APSInt Result; |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 309 | if (!BT || BT->getKind() != BuiltinType::Int || |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 310 | !Arg->isIntegerConstantExpr(Result, Context)) |
| 311 | return Diag(TheCall->getLocStart(), diag::err_prefetch_invalid_argument) |
| 312 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 313 | |
| 314 | // FIXME: gcc issues a warning and rewrites these to 0. These |
| 315 | // seems especially odd for the third argument since the default |
| 316 | // is 3. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 317 | if (i == 1) { |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 318 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 1) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 319 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 320 | << "0" << "1" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 321 | } else { |
| 322 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 323 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 324 | << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 328 | return false; |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 331 | /// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr, |
| 332 | /// int type). This simply type checks that type is one of the defined |
| 333 | /// constants (0-3). |
| 334 | bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) { |
| 335 | Expr *Arg = TheCall->getArg(1); |
| 336 | QualType ArgType = Arg->getType(); |
| 337 | const BuiltinType *BT = ArgType->getAsBuiltinType(); |
| 338 | llvm::APSInt Result(32); |
| 339 | if (!BT || BT->getKind() != BuiltinType::Int || |
| 340 | !Arg->isIntegerConstantExpr(Result, Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 341 | return Diag(TheCall->getLocStart(), diag::err_object_size_invalid_argument) |
| 342 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 346 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 347 | << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | return false; |
| 351 | } |
| 352 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 353 | // Handle i > 1 ? "x" : "y", recursivelly |
| 354 | bool Sema::SemaCheckStringLiteral(Expr *E, CallExpr *TheCall, bool HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 355 | unsigned format_idx, unsigned firstDataArg) { |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 356 | |
| 357 | switch (E->getStmtClass()) { |
| 358 | case Stmt::ConditionalOperatorClass: { |
| 359 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 360 | return SemaCheckStringLiteral(C->getLHS(), TheCall, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 361 | HasVAListArg, format_idx, firstDataArg) |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 362 | && SemaCheckStringLiteral(C->getRHS(), TheCall, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 363 | HasVAListArg, format_idx, firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | case Stmt::ImplicitCastExprClass: { |
| 367 | ImplicitCastExpr *Expr = dyn_cast<ImplicitCastExpr>(E); |
| 368 | return SemaCheckStringLiteral(Expr->getSubExpr(), TheCall, HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 369 | format_idx, firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | case Stmt::ParenExprClass: { |
| 373 | ParenExpr *Expr = dyn_cast<ParenExpr>(E); |
| 374 | return SemaCheckStringLiteral(Expr->getSubExpr(), TheCall, HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 375 | format_idx, firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | default: { |
| 379 | ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E); |
| 380 | StringLiteral *StrE = NULL; |
| 381 | |
| 382 | if (ObjCFExpr) |
| 383 | StrE = ObjCFExpr->getString(); |
| 384 | else |
| 385 | StrE = dyn_cast<StringLiteral>(E); |
| 386 | |
| 387 | if (StrE) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 388 | CheckPrintfString(StrE, E, TheCall, HasVAListArg, format_idx, |
| 389 | firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 390 | return true; |
| 391 | } |
| 392 | |
| 393 | return false; |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 399 | /// CheckPrintfArguments - Check calls to printf (and similar functions) for |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 400 | /// correct use of format strings. |
| 401 | /// |
| 402 | /// HasVAListArg - A predicate indicating whether the printf-like |
| 403 | /// function is passed an explicit va_arg argument (e.g., vprintf) |
| 404 | /// |
| 405 | /// format_idx - The index into Args for the format string. |
| 406 | /// |
| 407 | /// Improper format strings to functions in the printf family can be |
| 408 | /// the source of bizarre bugs and very serious security holes. A |
| 409 | /// good source of information is available in the following paper |
| 410 | /// (which includes additional references): |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 411 | /// |
| 412 | /// FormatGuard: Automatic Protection From printf Format String |
| 413 | /// Vulnerabilities, Proceedings of the 10th USENIX Security Symposium, 2001. |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 414 | /// |
| 415 | /// Functionality implemented: |
| 416 | /// |
| 417 | /// We can statically check the following properties for string |
| 418 | /// literal format strings for non v.*printf functions (where the |
| 419 | /// arguments are passed directly): |
| 420 | // |
| 421 | /// (1) Are the number of format conversions equal to the number of |
| 422 | /// data arguments? |
| 423 | /// |
| 424 | /// (2) Does each format conversion correctly match the type of the |
| 425 | /// corresponding data argument? (TODO) |
| 426 | /// |
| 427 | /// Moreover, for all printf functions we can: |
| 428 | /// |
| 429 | /// (3) Check for a missing format string (when not caught by type checking). |
| 430 | /// |
| 431 | /// (4) Check for no-operation flags; e.g. using "#" with format |
| 432 | /// conversion 'c' (TODO) |
| 433 | /// |
| 434 | /// (5) Check the use of '%n', a major source of security holes. |
| 435 | /// |
| 436 | /// (6) Check for malformed format conversions that don't specify anything. |
| 437 | /// |
| 438 | /// (7) Check for empty format strings. e.g: printf(""); |
| 439 | /// |
| 440 | /// (8) Check that the format string is a wide literal. |
| 441 | /// |
Ted Kremenek | 6d43959 | 2008-03-03 16:50:00 +0000 | [diff] [blame] | 442 | /// (9) Also check the arguments of functions with the __format__ attribute. |
| 443 | /// (TODO). |
| 444 | /// |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 445 | /// All of these checks can be done by parsing the format string. |
| 446 | /// |
| 447 | /// For now, we ONLY do (1), (3), (5), (6), (7), and (8). |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 448 | void |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 449 | Sema::CheckPrintfArguments(CallExpr *TheCall, bool HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 450 | unsigned format_idx, unsigned firstDataArg) { |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 451 | Expr *Fn = TheCall->getCallee(); |
| 452 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 453 | // CHECK: printf-like function is called with no format string. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 454 | if (format_idx >= TheCall->getNumArgs()) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 455 | Diag(TheCall->getRParenLoc(), diag::warn_printf_missing_format_string) |
| 456 | << Fn->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 457 | return; |
| 458 | } |
| 459 | |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 460 | Expr *OrigFormatExpr = TheCall->getArg(format_idx)->IgnoreParenCasts(); |
Chris Lattner | 459e848 | 2007-08-25 05:36:18 +0000 | [diff] [blame] | 461 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 462 | // CHECK: format string is not a string literal. |
| 463 | // |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 464 | // Dynamically generated format strings are difficult to |
| 465 | // automatically vet at compile time. Requiring that format strings |
| 466 | // are string literals: (1) permits the checking of format strings by |
| 467 | // the compiler and thereby (2) can practically remove the source of |
| 468 | // many format string exploits. |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 469 | |
| 470 | // Format string can be either ObjC string (e.g. @"%d") or |
| 471 | // C string (e.g. "%d") |
| 472 | // ObjC string uses the same format specifiers as C string, so we can use |
| 473 | // the same format string checking logic for both ObjC and C strings. |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 474 | bool isFExpr = SemaCheckStringLiteral(OrigFormatExpr, TheCall, |
| 475 | HasVAListArg, format_idx, |
| 476 | firstDataArg); |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 477 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 478 | if (!isFExpr) { |
Ted Kremenek | 4a33646 | 2007-12-17 19:03:13 +0000 | [diff] [blame] | 479 | // For vprintf* functions (i.e., HasVAListArg==true), we add a |
| 480 | // special check to see if the format string is a function parameter |
| 481 | // of the function calling the printf function. If the function |
| 482 | // has an attribute indicating it is a printf-like function, then we |
| 483 | // should suppress warnings concerning non-literals being used in a call |
| 484 | // to a vprintf function. For example: |
| 485 | // |
| 486 | // void |
| 487 | // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...) { |
| 488 | // va_list ap; |
| 489 | // va_start(ap, fmt); |
| 490 | // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". |
| 491 | // ... |
| 492 | // |
| 493 | // |
| 494 | // FIXME: We don't have full attribute support yet, so just check to see |
| 495 | // if the argument is a DeclRefExpr that references a parameter. We'll |
| 496 | // add proper support for checking the attribute later. |
| 497 | if (HasVAListArg) |
Chris Lattner | 998568f | 2007-12-28 05:38:24 +0000 | [diff] [blame] | 498 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(OrigFormatExpr)) |
| 499 | if (isa<ParmVarDecl>(DR->getDecl())) |
Ted Kremenek | 4a33646 | 2007-12-17 19:03:13 +0000 | [diff] [blame] | 500 | return; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 501 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 502 | Diag(TheCall->getArg(format_idx)->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 503 | diag::warn_printf_not_string_constant) |
| 504 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 505 | return; |
| 506 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 507 | } |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 508 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 509 | void Sema::CheckPrintfString(StringLiteral *FExpr, Expr *OrigFormatExpr, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 510 | CallExpr *TheCall, bool HasVAListArg, unsigned format_idx, |
| 511 | unsigned firstDataArg) { |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 512 | |
| 513 | ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(OrigFormatExpr); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 514 | // CHECK: is the format string a wide literal? |
| 515 | if (FExpr->isWide()) { |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 516 | Diag(FExpr->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 517 | diag::warn_printf_format_string_is_wide_literal) |
| 518 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 519 | return; |
| 520 | } |
| 521 | |
| 522 | // Str - The format string. NOTE: this is NOT null-terminated! |
| 523 | const char * const Str = FExpr->getStrData(); |
| 524 | |
| 525 | // CHECK: empty format string? |
| 526 | const unsigned StrLen = FExpr->getByteLength(); |
| 527 | |
| 528 | if (StrLen == 0) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 529 | Diag(FExpr->getLocStart(), diag::warn_printf_empty_format_string) |
| 530 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 531 | return; |
| 532 | } |
| 533 | |
| 534 | // We process the format string using a binary state machine. The |
| 535 | // current state is stored in CurrentState. |
| 536 | enum { |
| 537 | state_OrdChr, |
| 538 | state_Conversion |
| 539 | } CurrentState = state_OrdChr; |
| 540 | |
| 541 | // numConversions - The number of conversions seen so far. This is |
| 542 | // incremented as we traverse the format string. |
| 543 | unsigned numConversions = 0; |
| 544 | |
| 545 | // numDataArgs - The number of data arguments after the format |
| 546 | // string. This can only be determined for non vprintf-like |
| 547 | // functions. For those functions, this value is 1 (the sole |
| 548 | // va_arg argument). |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 549 | unsigned numDataArgs = TheCall->getNumArgs()-firstDataArg; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 550 | |
| 551 | // Inspect the format string. |
| 552 | unsigned StrIdx = 0; |
| 553 | |
| 554 | // LastConversionIdx - Index within the format string where we last saw |
| 555 | // a '%' character that starts a new format conversion. |
| 556 | unsigned LastConversionIdx = 0; |
| 557 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 558 | for (; StrIdx < StrLen; ++StrIdx) { |
Chris Lattner | 998568f | 2007-12-28 05:38:24 +0000 | [diff] [blame] | 559 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 560 | // Is the number of detected conversion conversions greater than |
| 561 | // the number of matching data arguments? If so, stop. |
| 562 | if (!HasVAListArg && numConversions > numDataArgs) break; |
| 563 | |
| 564 | // Handle "\0" |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 565 | if (Str[StrIdx] == '\0') { |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 566 | // The string returned by getStrData() is not null-terminated, |
| 567 | // so the presence of a null character is likely an error. |
Chris Lattner | 998568f | 2007-12-28 05:38:24 +0000 | [diff] [blame] | 568 | Diag(PP.AdvanceToTokenCharacter(FExpr->getLocStart(), StrIdx+1), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 569 | diag::warn_printf_format_string_contains_null_char) |
| 570 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 571 | return; |
| 572 | } |
| 573 | |
| 574 | // Ordinary characters (not processing a format conversion). |
| 575 | if (CurrentState == state_OrdChr) { |
| 576 | if (Str[StrIdx] == '%') { |
| 577 | CurrentState = state_Conversion; |
| 578 | LastConversionIdx = StrIdx; |
| 579 | } |
| 580 | continue; |
| 581 | } |
| 582 | |
| 583 | // Seen '%'. Now processing a format conversion. |
| 584 | switch (Str[StrIdx]) { |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 585 | // Handle dynamic precision or width specifier. |
| 586 | case '*': { |
| 587 | ++numConversions; |
| 588 | |
| 589 | if (!HasVAListArg && numConversions > numDataArgs) { |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 590 | SourceLocation Loc = FExpr->getLocStart(); |
| 591 | Loc = PP.AdvanceToTokenCharacter(Loc, StrIdx+1); |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 592 | |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 593 | if (Str[StrIdx-1] == '.') |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 594 | Diag(Loc, diag::warn_printf_asterisk_precision_missing_arg) |
| 595 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 596 | else |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 597 | Diag(Loc, diag::warn_printf_asterisk_width_missing_arg) |
| 598 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 599 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 600 | // Don't do any more checking. We'll just emit spurious errors. |
| 601 | return; |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 602 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 603 | |
| 604 | // Perform type checking on width/precision specifier. |
| 605 | Expr *E = TheCall->getArg(format_idx+numConversions); |
| 606 | if (const BuiltinType *BT = E->getType()->getAsBuiltinType()) |
| 607 | if (BT->getKind() == BuiltinType::Int) |
| 608 | break; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 609 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 610 | SourceLocation Loc = |
| 611 | PP.AdvanceToTokenCharacter(FExpr->getLocStart(), StrIdx+1); |
| 612 | |
| 613 | if (Str[StrIdx-1] == '.') |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 614 | Diag(Loc, diag::warn_printf_asterisk_precision_wrong_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 615 | << E->getType() << E->getSourceRange(); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 616 | else |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 617 | Diag(Loc, diag::warn_printf_asterisk_width_wrong_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 618 | << E->getType() << E->getSourceRange(); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 619 | |
| 620 | break; |
| 621 | } |
| 622 | |
| 623 | // Characters which can terminate a format conversion |
| 624 | // (e.g. "%d"). Characters that specify length modifiers or |
| 625 | // other flags are handled by the default case below. |
| 626 | // |
| 627 | // FIXME: additional checks will go into the following cases. |
| 628 | case 'i': |
| 629 | case 'd': |
| 630 | case 'o': |
| 631 | case 'u': |
| 632 | case 'x': |
| 633 | case 'X': |
| 634 | case 'D': |
| 635 | case 'O': |
| 636 | case 'U': |
| 637 | case 'e': |
| 638 | case 'E': |
| 639 | case 'f': |
| 640 | case 'F': |
| 641 | case 'g': |
| 642 | case 'G': |
| 643 | case 'a': |
| 644 | case 'A': |
| 645 | case 'c': |
| 646 | case 'C': |
| 647 | case 'S': |
| 648 | case 's': |
| 649 | case 'p': |
| 650 | ++numConversions; |
| 651 | CurrentState = state_OrdChr; |
| 652 | break; |
| 653 | |
| 654 | // CHECK: Are we using "%n"? Issue a warning. |
| 655 | case 'n': { |
| 656 | ++numConversions; |
| 657 | CurrentState = state_OrdChr; |
| 658 | SourceLocation Loc = PP.AdvanceToTokenCharacter(FExpr->getLocStart(), |
| 659 | LastConversionIdx+1); |
| 660 | |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 661 | Diag(Loc, diag::warn_printf_write_back)<<OrigFormatExpr->getSourceRange(); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 662 | break; |
| 663 | } |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 664 | |
| 665 | // Handle "%@" |
| 666 | case '@': |
| 667 | // %@ is allowed in ObjC format strings only. |
| 668 | if(ObjCFExpr != NULL) |
| 669 | CurrentState = state_OrdChr; |
| 670 | else { |
| 671 | // Issue a warning: invalid format conversion. |
| 672 | SourceLocation Loc = PP.AdvanceToTokenCharacter(FExpr->getLocStart(), |
| 673 | LastConversionIdx+1); |
| 674 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 675 | Diag(Loc, diag::warn_printf_invalid_conversion) |
| 676 | << std::string(Str+LastConversionIdx, |
| 677 | Str+std::min(LastConversionIdx+2, StrLen)) |
| 678 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 679 | } |
| 680 | ++numConversions; |
| 681 | break; |
| 682 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 683 | // Handle "%%" |
| 684 | case '%': |
| 685 | // Sanity check: Was the first "%" character the previous one? |
| 686 | // If not, we will assume that we have a malformed format |
| 687 | // conversion, and that the current "%" character is the start |
| 688 | // of a new conversion. |
| 689 | if (StrIdx - LastConversionIdx == 1) |
| 690 | CurrentState = state_OrdChr; |
| 691 | else { |
| 692 | // Issue a warning: invalid format conversion. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 693 | SourceLocation Loc = PP.AdvanceToTokenCharacter(FExpr->getLocStart(), |
| 694 | LastConversionIdx+1); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 695 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 696 | Diag(Loc, diag::warn_printf_invalid_conversion) |
| 697 | << std::string(Str+LastConversionIdx, Str+StrIdx) |
| 698 | << OrigFormatExpr->getSourceRange(); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 699 | |
| 700 | // This conversion is broken. Advance to the next format |
| 701 | // conversion. |
| 702 | LastConversionIdx = StrIdx; |
| 703 | ++numConversions; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 704 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 705 | break; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 706 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 707 | default: |
| 708 | // This case catches all other characters: flags, widths, etc. |
| 709 | // We should eventually process those as well. |
| 710 | break; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
| 714 | if (CurrentState == state_Conversion) { |
| 715 | // Issue a warning: invalid format conversion. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 716 | SourceLocation Loc = PP.AdvanceToTokenCharacter(FExpr->getLocStart(), |
| 717 | LastConversionIdx+1); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 718 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 719 | Diag(Loc, diag::warn_printf_invalid_conversion) |
| 720 | << std::string(Str+LastConversionIdx, |
| 721 | Str+std::min(LastConversionIdx+2, StrLen)) |
| 722 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 723 | return; |
| 724 | } |
| 725 | |
| 726 | if (!HasVAListArg) { |
| 727 | // CHECK: Does the number of format conversions exceed the number |
| 728 | // of data arguments? |
| 729 | if (numConversions > numDataArgs) { |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 730 | SourceLocation Loc = PP.AdvanceToTokenCharacter(FExpr->getLocStart(), |
| 731 | LastConversionIdx); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 732 | |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 733 | Diag(Loc, diag::warn_printf_insufficient_data_args) |
| 734 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 735 | } |
| 736 | // CHECK: Does the number of data arguments exceed the number of |
| 737 | // format conversions in the format string? |
| 738 | else if (numConversions < numDataArgs) |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 739 | Diag(TheCall->getArg(format_idx+numConversions+1)->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 740 | diag::warn_printf_too_many_data_args) |
| 741 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 742 | } |
| 743 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 744 | |
| 745 | //===--- CHECK: Return Address of Stack Variable --------------------------===// |
| 746 | |
| 747 | static DeclRefExpr* EvalVal(Expr *E); |
| 748 | static DeclRefExpr* EvalAddr(Expr* E); |
| 749 | |
| 750 | /// CheckReturnStackAddr - Check if a return statement returns the address |
| 751 | /// of a stack variable. |
| 752 | void |
| 753 | Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, |
| 754 | SourceLocation ReturnLoc) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 755 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 756 | // Perform checking for returned stack addresses. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 757 | if (lhsType->isPointerType() || lhsType->isBlockPointerType()) { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 758 | if (DeclRefExpr *DR = EvalAddr(RetValExp)) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 759 | Diag(DR->getLocStart(), diag::warn_ret_stack_addr) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 760 | << DR->getDecl()->getDeclName() << RetValExp->getSourceRange(); |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 761 | |
| 762 | // Skip over implicit cast expressions when checking for block expressions. |
| 763 | if (ImplicitCastExpr *IcExpr = |
| 764 | dyn_cast_or_null<ImplicitCastExpr>(RetValExp)) |
| 765 | RetValExp = IcExpr->getSubExpr(); |
| 766 | |
Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 767 | if (BlockExpr *C = dyn_cast_or_null<BlockExpr>(RetValExp)) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 768 | Diag(C->getLocStart(), diag::err_ret_local_block) |
| 769 | << C->getSourceRange(); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 770 | } |
| 771 | // Perform checking for stack values returned by reference. |
| 772 | else if (lhsType->isReferenceType()) { |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 773 | // Check for a reference to the stack |
| 774 | if (DeclRefExpr *DR = EvalVal(RetValExp)) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 775 | Diag(DR->getLocStart(), diag::warn_ret_stack_ref) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 776 | << DR->getDecl()->getDeclName() << RetValExp->getSourceRange(); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
| 780 | /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that |
| 781 | /// check if the expression in a return statement evaluates to an address |
| 782 | /// to a location on the stack. The recursion is used to traverse the |
| 783 | /// AST of the return expression, with recursion backtracking when we |
| 784 | /// encounter a subexpression that (1) clearly does not lead to the address |
| 785 | /// of a stack variable or (2) is something we cannot determine leads to |
| 786 | /// the address of a stack variable based on such local checking. |
| 787 | /// |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 788 | /// EvalAddr processes expressions that are pointers that are used as |
| 789 | /// references (and not L-values). EvalVal handles all other values. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 790 | /// At the base case of the recursion is a check for a DeclRefExpr* in |
| 791 | /// the refers to a stack variable. |
| 792 | /// |
| 793 | /// This implementation handles: |
| 794 | /// |
| 795 | /// * pointer-to-pointer casts |
| 796 | /// * implicit conversions from array references to pointers |
| 797 | /// * taking the address of fields |
| 798 | /// * arbitrary interplay between "&" and "*" operators |
| 799 | /// * pointer arithmetic from an address of a stack variable |
| 800 | /// * taking the address of an array element where the array is on the stack |
| 801 | static DeclRefExpr* EvalAddr(Expr *E) { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 802 | // We should only be called for evaluating pointer expressions. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 803 | assert((E->getType()->isPointerType() || |
| 804 | E->getType()->isBlockPointerType() || |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 805 | E->getType()->isObjCQualifiedIdType()) && |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 806 | "EvalAddr only works on pointers"); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 807 | |
| 808 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 809 | // viewed AST node. We then recursively traverse the AST by calling |
| 810 | // EvalAddr and EvalVal appropriately. |
| 811 | switch (E->getStmtClass()) { |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 812 | case Stmt::ParenExprClass: |
| 813 | // Ignore parentheses. |
| 814 | return EvalAddr(cast<ParenExpr>(E)->getSubExpr()); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 815 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 816 | case Stmt::UnaryOperatorClass: { |
| 817 | // The only unary operator that make sense to handle here |
| 818 | // is AddrOf. All others don't make sense as pointers. |
| 819 | UnaryOperator *U = cast<UnaryOperator>(E); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 820 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 821 | if (U->getOpcode() == UnaryOperator::AddrOf) |
| 822 | return EvalVal(U->getSubExpr()); |
| 823 | else |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 824 | return NULL; |
| 825 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 826 | |
| 827 | case Stmt::BinaryOperatorClass: { |
| 828 | // Handle pointer arithmetic. All other binary operators are not valid |
| 829 | // in this context. |
| 830 | BinaryOperator *B = cast<BinaryOperator>(E); |
| 831 | BinaryOperator::Opcode op = B->getOpcode(); |
| 832 | |
| 833 | if (op != BinaryOperator::Add && op != BinaryOperator::Sub) |
| 834 | return NULL; |
| 835 | |
| 836 | Expr *Base = B->getLHS(); |
| 837 | |
| 838 | // Determine which argument is the real pointer base. It could be |
| 839 | // the RHS argument instead of the LHS. |
| 840 | if (!Base->getType()->isPointerType()) Base = B->getRHS(); |
| 841 | |
| 842 | assert (Base->getType()->isPointerType()); |
| 843 | return EvalAddr(Base); |
| 844 | } |
Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 845 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 846 | // For conditional operators we need to see if either the LHS or RHS are |
| 847 | // valid DeclRefExpr*s. If one of them is valid, we return it. |
| 848 | case Stmt::ConditionalOperatorClass: { |
| 849 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 850 | |
| 851 | // Handle the GNU extension for missing LHS. |
| 852 | if (Expr *lhsExpr = C->getLHS()) |
| 853 | if (DeclRefExpr* LHS = EvalAddr(lhsExpr)) |
| 854 | return LHS; |
| 855 | |
| 856 | return EvalAddr(C->getRHS()); |
| 857 | } |
| 858 | |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 859 | // For casts, we need to handle conversions from arrays to |
| 860 | // pointer values, and pointer-to-pointer conversions. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 861 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 862 | case Stmt::CStyleCastExprClass: |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 863 | case Stmt::CXXFunctionalCastExprClass: { |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 864 | Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 865 | QualType T = SubExpr->getType(); |
| 866 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 867 | if (SubExpr->getType()->isPointerType() || |
| 868 | SubExpr->getType()->isBlockPointerType() || |
| 869 | SubExpr->getType()->isObjCQualifiedIdType()) |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 870 | return EvalAddr(SubExpr); |
| 871 | else if (T->isArrayType()) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 872 | return EvalVal(SubExpr); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 873 | else |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 874 | return 0; |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | // C++ casts. For dynamic casts, static casts, and const casts, we |
| 878 | // are always converting from a pointer-to-pointer, so we just blow |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 879 | // through the cast. In the case the dynamic cast doesn't fail (and |
| 880 | // return NULL), we take the conservative route and report cases |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 881 | // where we return the address of a stack variable. For Reinterpre |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 882 | // FIXME: The comment about is wrong; we're not always converting |
| 883 | // from pointer to pointer. I'm guessing that this code should also |
| 884 | // handle references to objects. |
| 885 | case Stmt::CXXStaticCastExprClass: |
| 886 | case Stmt::CXXDynamicCastExprClass: |
| 887 | case Stmt::CXXConstCastExprClass: |
| 888 | case Stmt::CXXReinterpretCastExprClass: { |
| 889 | Expr *S = cast<CXXNamedCastExpr>(E)->getSubExpr(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 890 | if (S->getType()->isPointerType() || S->getType()->isBlockPointerType()) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 891 | return EvalAddr(S); |
| 892 | else |
| 893 | return NULL; |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | // Everything else: we simply don't reason about them. |
| 897 | default: |
| 898 | return NULL; |
| 899 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | |
| 903 | /// EvalVal - This function is complements EvalAddr in the mutual recursion. |
| 904 | /// See the comments for EvalAddr for more details. |
| 905 | static DeclRefExpr* EvalVal(Expr *E) { |
| 906 | |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 907 | // We should only be called for evaluating non-pointer expressions, or |
| 908 | // expressions with a pointer type that are not used as references but instead |
| 909 | // are l-values (e.g., DeclRefExpr with a pointer type). |
| 910 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 911 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 912 | // viewed AST node. We then recursively traverse the AST by calling |
| 913 | // EvalAddr and EvalVal appropriately. |
| 914 | switch (E->getStmtClass()) { |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 915 | case Stmt::DeclRefExprClass: |
| 916 | case Stmt::QualifiedDeclRefExprClass: { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 917 | // DeclRefExpr: the base case. When we hit a DeclRefExpr we are looking |
| 918 | // at code that refers to a variable's name. We check if it has local |
| 919 | // storage within the function, and if so, return the expression. |
| 920 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
| 921 | |
| 922 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 923 | if(V->hasLocalStorage() && !V->getType()->isReferenceType()) return DR; |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 924 | |
| 925 | return NULL; |
| 926 | } |
| 927 | |
| 928 | case Stmt::ParenExprClass: |
| 929 | // Ignore parentheses. |
| 930 | return EvalVal(cast<ParenExpr>(E)->getSubExpr()); |
| 931 | |
| 932 | case Stmt::UnaryOperatorClass: { |
| 933 | // The only unary operator that make sense to handle here |
| 934 | // is Deref. All others don't resolve to a "name." This includes |
| 935 | // handling all sorts of rvalues passed to a unary operator. |
| 936 | UnaryOperator *U = cast<UnaryOperator>(E); |
| 937 | |
| 938 | if (U->getOpcode() == UnaryOperator::Deref) |
| 939 | return EvalAddr(U->getSubExpr()); |
| 940 | |
| 941 | return NULL; |
| 942 | } |
| 943 | |
| 944 | case Stmt::ArraySubscriptExprClass: { |
| 945 | // Array subscripts are potential references to data on the stack. We |
| 946 | // retrieve the DeclRefExpr* for the array variable if it indeed |
| 947 | // has local storage. |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 948 | return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase()); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | case Stmt::ConditionalOperatorClass: { |
| 952 | // For conditional operators we need to see if either the LHS or RHS are |
| 953 | // non-NULL DeclRefExpr's. If one is non-NULL, we return it. |
| 954 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 955 | |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 956 | // Handle the GNU extension for missing LHS. |
| 957 | if (Expr *lhsExpr = C->getLHS()) |
| 958 | if (DeclRefExpr *LHS = EvalVal(lhsExpr)) |
| 959 | return LHS; |
| 960 | |
| 961 | return EvalVal(C->getRHS()); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | // Accesses to members are potential references to data on the stack. |
| 965 | case Stmt::MemberExprClass: { |
| 966 | MemberExpr *M = cast<MemberExpr>(E); |
| 967 | |
| 968 | // Check for indirect access. We only want direct field accesses. |
| 969 | if (!M->isArrow()) |
| 970 | return EvalVal(M->getBase()); |
| 971 | else |
| 972 | return NULL; |
| 973 | } |
| 974 | |
| 975 | // Everything else: we simply don't reason about them. |
| 976 | default: |
| 977 | return NULL; |
| 978 | } |
| 979 | } |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 980 | |
| 981 | //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// |
| 982 | |
| 983 | /// Check for comparisons of floating point operands using != and ==. |
| 984 | /// Issue a warning if these are no self-comparisons, as they are not likely |
| 985 | /// to do what the programmer intended. |
| 986 | void Sema::CheckFloatComparison(SourceLocation loc, Expr* lex, Expr *rex) { |
| 987 | bool EmitWarning = true; |
| 988 | |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 989 | Expr* LeftExprSansParen = lex->IgnoreParens(); |
Ted Kremenek | 32e97b6 | 2008-01-17 17:55:13 +0000 | [diff] [blame] | 990 | Expr* RightExprSansParen = rex->IgnoreParens(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 991 | |
| 992 | // Special case: check for x == x (which is OK). |
| 993 | // Do not emit warnings for such cases. |
| 994 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) |
| 995 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) |
| 996 | if (DRL->getDecl() == DRR->getDecl()) |
| 997 | EmitWarning = false; |
| 998 | |
Ted Kremenek | 1b500bb | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 999 | |
| 1000 | // Special case: check for comparisons against literals that can be exactly |
| 1001 | // represented by APFloat. In such cases, do not emit a warning. This |
| 1002 | // is a heuristic: often comparison against such literals are used to |
| 1003 | // detect if a value in a variable has not changed. This clearly can |
| 1004 | // lead to false negatives. |
| 1005 | if (EmitWarning) { |
| 1006 | if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { |
| 1007 | if (FLL->isExact()) |
| 1008 | EmitWarning = false; |
| 1009 | } |
| 1010 | else |
| 1011 | if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)){ |
| 1012 | if (FLR->isExact()) |
| 1013 | EmitWarning = false; |
| 1014 | } |
| 1015 | } |
| 1016 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1017 | // Check for comparisons with builtin types. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1018 | if (EmitWarning) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1019 | if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1020 | if (CL->isBuiltinCall(Context)) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1021 | EmitWarning = false; |
| 1022 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1023 | if (EmitWarning) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1024 | if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1025 | if (CR->isBuiltinCall(Context)) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1026 | EmitWarning = false; |
| 1027 | |
| 1028 | // Emit the diagnostic. |
| 1029 | if (EmitWarning) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1030 | Diag(loc, diag::warn_floatingpoint_eq) |
| 1031 | << lex->getSourceRange() << rex->getSourceRange(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1032 | } |