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