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 | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 20 | #include "clang/Lex/LiteralSupport.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 24 | /// getLocationOfStringLiteralByte - Return a source location that points to the |
| 25 | /// specified byte of the specified string literal. |
| 26 | /// |
| 27 | /// Strings are amazingly complex. They can be formed from multiple tokens and |
| 28 | /// can have escape sequences in them in addition to the usual trigraph and |
| 29 | /// escaped newline business. This routine handles this complexity. |
| 30 | /// |
| 31 | SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, |
| 32 | unsigned ByteNo) const { |
| 33 | assert(!SL->isWide() && "This doesn't work for wide strings yet"); |
| 34 | |
| 35 | // Loop over all of the tokens in this string until we find the one that |
| 36 | // contains the byte we're looking for. |
| 37 | unsigned TokNo = 0; |
| 38 | while (1) { |
| 39 | assert(TokNo < SL->getNumConcatenated() && "Invalid byte number!"); |
| 40 | SourceLocation StrTokLoc = SL->getStrTokenLoc(TokNo); |
| 41 | |
| 42 | // Get the spelling of the string so that we can get the data that makes up |
| 43 | // the string literal, not the identifier for the macro it is potentially |
| 44 | // expanded through. |
| 45 | SourceLocation StrTokSpellingLoc = SourceMgr.getSpellingLoc(StrTokLoc); |
| 46 | |
| 47 | // Re-lex the token to get its length and original spelling. |
| 48 | std::pair<FileID, unsigned> LocInfo = |
| 49 | SourceMgr.getDecomposedLoc(StrTokSpellingLoc); |
| 50 | std::pair<const char *,const char *> Buffer = |
| 51 | SourceMgr.getBufferData(LocInfo.first); |
| 52 | const char *StrData = Buffer.first+LocInfo.second; |
| 53 | |
| 54 | // Create a langops struct and enable trigraphs. This is sufficient for |
| 55 | // relexing tokens. |
| 56 | LangOptions LangOpts; |
| 57 | LangOpts.Trigraphs = true; |
| 58 | |
| 59 | // Create a lexer starting at the beginning of this token. |
| 60 | Lexer TheLexer(StrTokSpellingLoc, LangOpts, Buffer.first, StrData, |
| 61 | Buffer.second); |
| 62 | Token TheTok; |
| 63 | TheLexer.LexFromRawLexer(TheTok); |
| 64 | |
Chris Lattner | 443e53c | 2009-02-18 19:26:42 +0000 | [diff] [blame] | 65 | // Use the StringLiteralParser to compute the length of the string in bytes. |
| 66 | StringLiteralParser SLP(&TheTok, 1, PP); |
| 67 | unsigned TokNumBytes = SLP.GetStringLength(); |
Chris Lattner | d0d082f | 2009-02-18 18:34:12 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 2197c96 | 2009-02-18 18:52:52 +0000 | [diff] [blame] | 69 | // If the byte is in this token, return the location of the byte. |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 70 | if (ByteNo < TokNumBytes || |
| 71 | (ByteNo == TokNumBytes && TokNo == SL->getNumConcatenated())) { |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 72 | unsigned Offset = |
| 73 | StringLiteralParser::getOffsetOfStringByte(TheTok, ByteNo, PP); |
| 74 | |
| 75 | // Now that we know the offset of the token in the spelling, use the |
| 76 | // preprocessor to get the offset in the original source. |
| 77 | return PP.AdvanceToTokenCharacter(StrTokLoc, Offset); |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // Move to the next string token. |
| 81 | ++TokNo; |
| 82 | ByteNo -= TokNumBytes; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 87 | /// CheckFunctionCall - Check a direct function call for various correctness |
| 88 | /// and safety properties not strictly enforced by the C type system. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 89 | Action::OwningExprResult |
| 90 | Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall) { |
| 91 | OwningExprResult TheCallResult(Owned(TheCall)); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 92 | // Get the IdentifierInfo* for the called function. |
| 93 | IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 94 | |
| 95 | // None of the checks below are needed for functions that don't have |
| 96 | // simple names (e.g., C++ conversion functions). |
| 97 | if (!FnInfo) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 98 | return move(TheCallResult); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 99 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 100 | switch (FDecl->getBuiltinID(Context)) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 101 | case Builtin::BI__builtin___CFStringMakeConstantString: |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 102 | assert(TheCall->getNumArgs() == 1 && |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 103 | "Wrong # arguments to builtin CFStringMakeConstantString"); |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 104 | if (CheckObjCString(TheCall->getArg(0))) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 105 | return ExprError(); |
| 106 | return move(TheCallResult); |
Ted Kremenek | 49ff7a1 | 2008-07-09 17:58:53 +0000 | [diff] [blame] | 107 | case Builtin::BI__builtin_stdarg_start: |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 108 | case Builtin::BI__builtin_va_start: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 109 | if (SemaBuiltinVAStart(TheCall)) |
| 110 | return ExprError(); |
| 111 | return move(TheCallResult); |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 112 | case Builtin::BI__builtin_isgreater: |
| 113 | case Builtin::BI__builtin_isgreaterequal: |
| 114 | case Builtin::BI__builtin_isless: |
| 115 | case Builtin::BI__builtin_islessequal: |
| 116 | case Builtin::BI__builtin_islessgreater: |
| 117 | case Builtin::BI__builtin_isunordered: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 118 | if (SemaBuiltinUnorderedCompare(TheCall)) |
| 119 | return ExprError(); |
| 120 | return move(TheCallResult); |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 121 | case Builtin::BI__builtin_return_address: |
| 122 | case Builtin::BI__builtin_frame_address: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 123 | if (SemaBuiltinStackAddress(TheCall)) |
| 124 | return ExprError(); |
| 125 | return move(TheCallResult); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 126 | case Builtin::BI__builtin_shufflevector: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 127 | return SemaBuiltinShuffleVector(TheCall); |
| 128 | // TheCall will be freed by the smart pointer here, but that's fine, since |
| 129 | // SemaBuiltinShuffleVector guts it, but then doesn't release it. |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 130 | case Builtin::BI__builtin_prefetch: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 131 | if (SemaBuiltinPrefetch(TheCall)) |
| 132 | return ExprError(); |
| 133 | return move(TheCallResult); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 134 | case Builtin::BI__builtin_object_size: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 135 | if (SemaBuiltinObjectSize(TheCall)) |
| 136 | return ExprError(); |
Eli Friedman | 586d6a8 | 2009-05-03 06:04:26 +0000 | [diff] [blame] | 137 | return move(TheCallResult); |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 138 | case Builtin::BI__builtin_longjmp: |
| 139 | if (SemaBuiltinLongjmp(TheCall)) |
| 140 | return ExprError(); |
Eli Friedman | 586d6a8 | 2009-05-03 06:04:26 +0000 | [diff] [blame] | 141 | return move(TheCallResult); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 142 | case Builtin::BI__sync_fetch_and_add: |
| 143 | case Builtin::BI__sync_fetch_and_sub: |
| 144 | case Builtin::BI__sync_fetch_and_or: |
| 145 | case Builtin::BI__sync_fetch_and_and: |
| 146 | case Builtin::BI__sync_fetch_and_xor: |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 147 | case Builtin::BI__sync_fetch_and_nand: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 148 | case Builtin::BI__sync_add_and_fetch: |
| 149 | case Builtin::BI__sync_sub_and_fetch: |
| 150 | case Builtin::BI__sync_and_and_fetch: |
| 151 | case Builtin::BI__sync_or_and_fetch: |
| 152 | case Builtin::BI__sync_xor_and_fetch: |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 153 | case Builtin::BI__sync_nand_and_fetch: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 154 | case Builtin::BI__sync_val_compare_and_swap: |
| 155 | case Builtin::BI__sync_bool_compare_and_swap: |
| 156 | case Builtin::BI__sync_lock_test_and_set: |
| 157 | case Builtin::BI__sync_lock_release: |
| 158 | if (SemaBuiltinAtomicOverloaded(TheCall)) |
| 159 | return ExprError(); |
| 160 | return move(TheCallResult); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 161 | } |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 162 | |
| 163 | // FIXME: This mechanism should be abstracted to be less fragile and |
| 164 | // more efficient. For example, just map function ids to custom |
| 165 | // handlers. |
| 166 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 167 | // Printf checking. |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 168 | if (const FormatAttr *Format = FDecl->getAttr<FormatAttr>()) { |
| 169 | if (Format->getType() == "printf") { |
Ted Kremenek | 3d692df | 2009-02-27 17:58:43 +0000 | [diff] [blame] | 170 | bool HasVAListArg = Format->getFirstArg() == 0; |
| 171 | if (!HasVAListArg) { |
| 172 | if (const FunctionProtoType *Proto |
| 173 | = FDecl->getType()->getAsFunctionProtoType()) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 174 | HasVAListArg = !Proto->isVariadic(); |
Ted Kremenek | 3d692df | 2009-02-27 17:58:43 +0000 | [diff] [blame] | 175 | } |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 176 | CheckPrintfArguments(TheCall, HasVAListArg, Format->getFormatIdx() - 1, |
Ted Kremenek | 3d692df | 2009-02-27 17:58:43 +0000 | [diff] [blame] | 177 | HasVAListArg ? 0 : Format->getFirstArg() - 1); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 178 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 179 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 180 | |
| 181 | return move(TheCallResult); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 184 | /// SemaBuiltinAtomicOverloaded - We have a call to a function like |
| 185 | /// __sync_fetch_and_add, which is an overloaded function based on the pointer |
| 186 | /// type of its first argument. The main ActOnCallExpr routines have already |
| 187 | /// promoted the types of arguments because all of these calls are prototyped as |
| 188 | /// void(...). |
| 189 | /// |
| 190 | /// This function goes through and does final semantic checking for these |
| 191 | /// builtins, |
| 192 | bool Sema::SemaBuiltinAtomicOverloaded(CallExpr *TheCall) { |
| 193 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 194 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 195 | |
| 196 | // Ensure that we have at least one argument to do type inference from. |
| 197 | if (TheCall->getNumArgs() < 1) |
| 198 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 199 | << 0 << TheCall->getCallee()->getSourceRange(); |
| 200 | |
| 201 | // Inspect the first argument of the atomic builtin. This should always be |
| 202 | // a pointer type, whose element is an integral scalar or pointer type. |
| 203 | // Because it is a pointer type, we don't have to worry about any implicit |
| 204 | // casts here. |
| 205 | Expr *FirstArg = TheCall->getArg(0); |
| 206 | if (!FirstArg->getType()->isPointerType()) |
| 207 | return Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
| 208 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 209 | |
| 210 | QualType ValType = FirstArg->getType()->getAsPointerType()->getPointeeType(); |
| 211 | if (!ValType->isIntegerType() && !ValType->isPointerType() && |
| 212 | !ValType->isBlockPointerType()) |
| 213 | return Diag(DRE->getLocStart(), |
| 214 | diag::err_atomic_builtin_must_be_pointer_intptr) |
| 215 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 216 | |
| 217 | // We need to figure out which concrete builtin this maps onto. For example, |
| 218 | // __sync_fetch_and_add with a 2 byte object turns into |
| 219 | // __sync_fetch_and_add_2. |
| 220 | #define BUILTIN_ROW(x) \ |
| 221 | { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ |
| 222 | Builtin::BI##x##_8, Builtin::BI##x##_16 } |
| 223 | |
| 224 | static const unsigned BuiltinIndices[][5] = { |
| 225 | BUILTIN_ROW(__sync_fetch_and_add), |
| 226 | BUILTIN_ROW(__sync_fetch_and_sub), |
| 227 | BUILTIN_ROW(__sync_fetch_and_or), |
| 228 | BUILTIN_ROW(__sync_fetch_and_and), |
| 229 | BUILTIN_ROW(__sync_fetch_and_xor), |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 230 | BUILTIN_ROW(__sync_fetch_and_nand), |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 231 | |
| 232 | BUILTIN_ROW(__sync_add_and_fetch), |
| 233 | BUILTIN_ROW(__sync_sub_and_fetch), |
| 234 | BUILTIN_ROW(__sync_and_and_fetch), |
| 235 | BUILTIN_ROW(__sync_or_and_fetch), |
| 236 | BUILTIN_ROW(__sync_xor_and_fetch), |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 237 | BUILTIN_ROW(__sync_nand_and_fetch), |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 238 | |
| 239 | BUILTIN_ROW(__sync_val_compare_and_swap), |
| 240 | BUILTIN_ROW(__sync_bool_compare_and_swap), |
| 241 | BUILTIN_ROW(__sync_lock_test_and_set), |
| 242 | BUILTIN_ROW(__sync_lock_release) |
| 243 | }; |
| 244 | #undef BUILTIN_ROW |
| 245 | |
| 246 | // Determine the index of the size. |
| 247 | unsigned SizeIndex; |
| 248 | switch (Context.getTypeSize(ValType)/8) { |
| 249 | case 1: SizeIndex = 0; break; |
| 250 | case 2: SizeIndex = 1; break; |
| 251 | case 4: SizeIndex = 2; break; |
| 252 | case 8: SizeIndex = 3; break; |
| 253 | case 16: SizeIndex = 4; break; |
| 254 | default: |
| 255 | return Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size) |
| 256 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 257 | } |
| 258 | |
| 259 | // Each of these builtins has one pointer argument, followed by some number of |
| 260 | // values (0, 1 or 2) followed by a potentially empty varags list of stuff |
| 261 | // that we ignore. Find out which row of BuiltinIndices to read from as well |
| 262 | // as the number of fixed args. |
| 263 | unsigned BuiltinID = FDecl->getBuiltinID(Context); |
| 264 | unsigned BuiltinIndex, NumFixed = 1; |
| 265 | switch (BuiltinID) { |
| 266 | default: assert(0 && "Unknown overloaded atomic builtin!"); |
| 267 | case Builtin::BI__sync_fetch_and_add: BuiltinIndex = 0; break; |
| 268 | case Builtin::BI__sync_fetch_and_sub: BuiltinIndex = 1; break; |
| 269 | case Builtin::BI__sync_fetch_and_or: BuiltinIndex = 2; break; |
| 270 | case Builtin::BI__sync_fetch_and_and: BuiltinIndex = 3; break; |
| 271 | case Builtin::BI__sync_fetch_and_xor: BuiltinIndex = 4; break; |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 272 | case Builtin::BI__sync_fetch_and_nand:BuiltinIndex = 5; break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 273 | |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 274 | case Builtin::BI__sync_add_and_fetch: BuiltinIndex = 6; break; |
| 275 | case Builtin::BI__sync_sub_and_fetch: BuiltinIndex = 7; break; |
| 276 | case Builtin::BI__sync_and_and_fetch: BuiltinIndex = 8; break; |
| 277 | case Builtin::BI__sync_or_and_fetch: BuiltinIndex = 9; break; |
| 278 | case Builtin::BI__sync_xor_and_fetch: BuiltinIndex =10; break; |
| 279 | case Builtin::BI__sync_nand_and_fetch:BuiltinIndex =11; break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 280 | |
| 281 | case Builtin::BI__sync_val_compare_and_swap: |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 282 | BuiltinIndex = 12; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 283 | NumFixed = 2; |
| 284 | break; |
| 285 | case Builtin::BI__sync_bool_compare_and_swap: |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 286 | BuiltinIndex = 13; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 287 | NumFixed = 2; |
| 288 | break; |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 289 | case Builtin::BI__sync_lock_test_and_set: BuiltinIndex = 14; break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 290 | case Builtin::BI__sync_lock_release: |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 291 | BuiltinIndex = 15; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 292 | NumFixed = 0; |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | // Now that we know how many fixed arguments we expect, first check that we |
| 297 | // have at least that many. |
| 298 | if (TheCall->getNumArgs() < 1+NumFixed) |
| 299 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 300 | << 0 << TheCall->getCallee()->getSourceRange(); |
| 301 | |
Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 302 | |
| 303 | // Get the decl for the concrete builtin from this, we can tell what the |
| 304 | // concrete integer type we should convert to is. |
| 305 | unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; |
| 306 | const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID); |
| 307 | IdentifierInfo *NewBuiltinII = PP.getIdentifierInfo(NewBuiltinName); |
| 308 | FunctionDecl *NewBuiltinDecl = |
| 309 | cast<FunctionDecl>(LazilyCreateBuiltin(NewBuiltinII, NewBuiltinID, |
| 310 | TUScope, false, DRE->getLocStart())); |
| 311 | const FunctionProtoType *BuiltinFT = |
| 312 | NewBuiltinDecl->getType()->getAsFunctionProtoType(); |
| 313 | ValType = BuiltinFT->getArgType(0)->getAsPointerType()->getPointeeType(); |
| 314 | |
| 315 | // If the first type needs to be converted (e.g. void** -> int*), do it now. |
| 316 | if (BuiltinFT->getArgType(0) != FirstArg->getType()) { |
| 317 | ImpCastExprToType(FirstArg, BuiltinFT->getArgType(0), false); |
| 318 | TheCall->setArg(0, FirstArg); |
| 319 | } |
| 320 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 321 | // Next, walk the valid ones promoting to the right type. |
| 322 | for (unsigned i = 0; i != NumFixed; ++i) { |
| 323 | Expr *Arg = TheCall->getArg(i+1); |
| 324 | |
| 325 | // If the argument is an implicit cast, then there was a promotion due to |
| 326 | // "...", just remove it now. |
| 327 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) { |
| 328 | Arg = ICE->getSubExpr(); |
| 329 | ICE->setSubExpr(0); |
| 330 | ICE->Destroy(Context); |
| 331 | TheCall->setArg(i+1, Arg); |
| 332 | } |
| 333 | |
| 334 | // GCC does an implicit conversion to the pointer or integer ValType. This |
| 335 | // can fail in some cases (1i -> int**), check for this error case now. |
| 336 | if (CheckCastTypes(Arg->getSourceRange(), ValType, Arg)) |
| 337 | return true; |
| 338 | |
| 339 | // Okay, we have something that *can* be converted to the right type. Check |
| 340 | // to see if there is a potentially weird extension going on here. This can |
| 341 | // happen when you do an atomic operation on something like an char* and |
| 342 | // pass in 42. The 42 gets converted to char. This is even more strange |
| 343 | // for things like 45.123 -> char, etc. |
| 344 | // FIXME: Do this check. |
| 345 | ImpCastExprToType(Arg, ValType, false); |
| 346 | TheCall->setArg(i+1, Arg); |
| 347 | } |
| 348 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 349 | // Switch the DeclRefExpr to refer to the new decl. |
| 350 | DRE->setDecl(NewBuiltinDecl); |
| 351 | DRE->setType(NewBuiltinDecl->getType()); |
| 352 | |
| 353 | // Set the callee in the CallExpr. |
| 354 | // FIXME: This leaks the original parens and implicit casts. |
| 355 | Expr *PromotedCall = DRE; |
| 356 | UsualUnaryConversions(PromotedCall); |
| 357 | TheCall->setCallee(PromotedCall); |
| 358 | |
| 359 | |
| 360 | // Change the result type of the call to match the result type of the decl. |
| 361 | TheCall->setType(NewBuiltinDecl->getResultType()); |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 366 | /// CheckObjCString - Checks that the argument to the builtin |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 367 | /// CFString constructor is correct |
Steve Naroff | fd94262 | 2009-04-13 20:26:29 +0000 | [diff] [blame] | 368 | /// FIXME: GCC currently emits the following warning: |
| 369 | /// "warning: input conversion stopped due to an input byte that does not |
| 370 | /// belong to the input codeset UTF-8" |
| 371 | /// Note: It might also make sense to do the UTF-16 conversion here (would |
| 372 | /// simplify the backend). |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 373 | bool Sema::CheckObjCString(Expr *Arg) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 374 | Arg = Arg->IgnoreParenCasts(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 375 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); |
| 376 | |
| 377 | if (!Literal || Literal->isWide()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 378 | Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 379 | << Arg->getSourceRange(); |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 380 | return true; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | const char *Data = Literal->getStrData(); |
| 384 | unsigned Length = Literal->getByteLength(); |
| 385 | |
| 386 | for (unsigned i = 0; i < Length; ++i) { |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 387 | if (!Data[i]) { |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 388 | Diag(getLocationOfStringLiteralByte(Literal, i), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 389 | diag::warn_cfstring_literal_contains_nul_character) |
| 390 | << Arg->getSourceRange(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 391 | break; |
| 392 | } |
| 393 | } |
| 394 | |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 395 | return false; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 398 | /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity. |
| 399 | /// Emit an error and return true on failure, return false on success. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 400 | bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) { |
| 401 | Expr *Fn = TheCall->getCallee(); |
| 402 | if (TheCall->getNumArgs() > 2) { |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 403 | Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 404 | diag::err_typecheck_call_too_many_args) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 405 | << 0 /*function call*/ << Fn->getSourceRange() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 406 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
| 407 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 408 | return true; |
| 409 | } |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 410 | |
| 411 | if (TheCall->getNumArgs() < 2) { |
| 412 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 413 | << 0 /*function call*/; |
| 414 | } |
| 415 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 416 | // Determine whether the current function is variadic or not. |
| 417 | bool isVariadic; |
Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 418 | if (CurBlock) |
| 419 | isVariadic = CurBlock->isVariadic; |
| 420 | else if (getCurFunctionDecl()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 421 | if (FunctionProtoType* FTP = |
| 422 | dyn_cast<FunctionProtoType>(getCurFunctionDecl()->getType())) |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 423 | isVariadic = FTP->isVariadic(); |
| 424 | else |
| 425 | isVariadic = false; |
| 426 | } else { |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 427 | isVariadic = getCurMethodDecl()->isVariadic(); |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 428 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 429 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 430 | if (!isVariadic) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 431 | Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function); |
| 432 | return true; |
| 433 | } |
| 434 | |
| 435 | // Verify that the second argument to the builtin is the last argument of the |
| 436 | // current function or method. |
| 437 | bool SecondArgIsLastNamedArgument = false; |
Anders Carlsson | e2c1410 | 2008-02-13 01:22:59 +0000 | [diff] [blame] | 438 | const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 439 | |
| 440 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { |
| 441 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 442 | // FIXME: This isn't correct for methods (results in bogus warning). |
| 443 | // Get the last formal in the current function. |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 444 | const ParmVarDecl *LastArg; |
Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 445 | if (CurBlock) |
| 446 | LastArg = *(CurBlock->TheDecl->param_end()-1); |
| 447 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 448 | LastArg = *(FD->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 449 | else |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 450 | LastArg = *(getCurMethodDecl()->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 451 | SecondArgIsLastNamedArgument = PV == LastArg; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | if (!SecondArgIsLastNamedArgument) |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 456 | Diag(TheCall->getArg(1)->getLocStart(), |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 457 | diag::warn_second_parameter_of_va_start_not_last_named_argument); |
| 458 | return false; |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 459 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 460 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 461 | /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and |
| 462 | /// friends. This is declared to take (...), so we have to check everything. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 463 | bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { |
| 464 | if (TheCall->getNumArgs() < 2) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 465 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 466 | << 0 /*function call*/; |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 467 | if (TheCall->getNumArgs() > 2) |
| 468 | return Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 469 | diag::err_typecheck_call_too_many_args) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 470 | << 0 /*function call*/ |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 471 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
| 472 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 473 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 474 | Expr *OrigArg0 = TheCall->getArg(0); |
| 475 | Expr *OrigArg1 = TheCall->getArg(1); |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 476 | |
| 477 | // Do standard promotions between the two arguments, returning their common |
| 478 | // type. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 479 | QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); |
Daniel Dunbar | 403bc2b | 2009-02-19 19:28:43 +0000 | [diff] [blame] | 480 | |
| 481 | // Make sure any conversions are pushed back into the call; this is |
| 482 | // type safe since unordered compare builtins are declared as "_Bool |
| 483 | // foo(...)". |
| 484 | TheCall->setArg(0, OrigArg0); |
| 485 | TheCall->setArg(1, OrigArg1); |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 486 | |
| 487 | // If the common type isn't a real floating type, then the arguments were |
| 488 | // invalid for this operation. |
| 489 | if (!Res->isRealFloatingType()) |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 490 | return Diag(OrigArg0->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 491 | diag::err_typecheck_call_invalid_ordered_compare) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 492 | << OrigArg0->getType() << OrigArg1->getType() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 493 | << SourceRange(OrigArg0->getLocStart(), OrigArg1->getLocEnd()); |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 494 | |
| 495 | return false; |
| 496 | } |
| 497 | |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 498 | bool Sema::SemaBuiltinStackAddress(CallExpr *TheCall) { |
| 499 | // The signature for these builtins is exact; the only thing we need |
| 500 | // to check is that the argument is a constant. |
| 501 | SourceLocation Loc; |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 502 | if (!TheCall->getArg(0)->isIntegerConstantExpr(Context, &Loc)) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 503 | return Diag(Loc, diag::err_stack_const_level) << TheCall->getSourceRange(); |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 504 | |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 505 | return false; |
| 506 | } |
| 507 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 508 | /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. |
| 509 | // This is declared to take (...), so we have to check everything. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 510 | Action::OwningExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 511 | if (TheCall->getNumArgs() < 3) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 512 | return ExprError(Diag(TheCall->getLocEnd(), |
| 513 | diag::err_typecheck_call_too_few_args) |
| 514 | << 0 /*function call*/ << TheCall->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 515 | |
| 516 | QualType FAType = TheCall->getArg(0)->getType(); |
| 517 | QualType SAType = TheCall->getArg(1)->getType(); |
| 518 | |
| 519 | if (!FAType->isVectorType() || !SAType->isVectorType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 520 | Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector) |
| 521 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
| 522 | TheCall->getArg(1)->getLocEnd()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 523 | return ExprError(); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 526 | if (Context.getCanonicalType(FAType).getUnqualifiedType() != |
| 527 | Context.getCanonicalType(SAType).getUnqualifiedType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 528 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
| 529 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
| 530 | TheCall->getArg(1)->getLocEnd()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 531 | return ExprError(); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | unsigned numElements = FAType->getAsVectorType()->getNumElements(); |
| 535 | if (TheCall->getNumArgs() != numElements+2) { |
| 536 | if (TheCall->getNumArgs() < numElements+2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 537 | return ExprError(Diag(TheCall->getLocEnd(), |
| 538 | diag::err_typecheck_call_too_few_args) |
| 539 | << 0 /*function call*/ << TheCall->getSourceRange()); |
| 540 | return ExprError(Diag(TheCall->getLocEnd(), |
| 541 | diag::err_typecheck_call_too_many_args) |
| 542 | << 0 /*function call*/ << TheCall->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { |
| 546 | llvm::APSInt Result(32); |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 547 | if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 548 | return ExprError(Diag(TheCall->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 549 | diag::err_shufflevector_nonconstant_argument) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 550 | << TheCall->getArg(i)->getSourceRange()); |
| 551 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 552 | if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 553 | return ExprError(Diag(TheCall->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 554 | diag::err_shufflevector_argument_too_large) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 555 | << TheCall->getArg(i)->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | llvm::SmallVector<Expr*, 32> exprs; |
| 559 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 560 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 561 | exprs.push_back(TheCall->getArg(i)); |
| 562 | TheCall->setArg(i, 0); |
| 563 | } |
| 564 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 565 | return Owned(new (Context) ShuffleVectorExpr(exprs.begin(), numElements+2, |
| 566 | FAType, |
| 567 | TheCall->getCallee()->getLocStart(), |
| 568 | TheCall->getRParenLoc())); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 569 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 570 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 571 | /// SemaBuiltinPrefetch - Handle __builtin_prefetch. |
| 572 | // This is declared to take (const void*, ...) and can take two |
| 573 | // optional constant int args. |
| 574 | bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 575 | unsigned NumArgs = TheCall->getNumArgs(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 576 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 577 | if (NumArgs > 3) |
| 578 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_many_args) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 579 | << 0 /*function call*/ << TheCall->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 580 | |
| 581 | // Argument 0 is checked for us and the remaining arguments must be |
| 582 | // constant integers. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 583 | for (unsigned i = 1; i != NumArgs; ++i) { |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 584 | Expr *Arg = TheCall->getArg(i); |
| 585 | QualType RWType = Arg->getType(); |
| 586 | |
| 587 | const BuiltinType *BT = RWType->getAsBuiltinType(); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 588 | llvm::APSInt Result; |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 589 | if (!BT || BT->getKind() != BuiltinType::Int || |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 590 | !Arg->isIntegerConstantExpr(Result, Context)) |
| 591 | return Diag(TheCall->getLocStart(), diag::err_prefetch_invalid_argument) |
| 592 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 593 | |
| 594 | // FIXME: gcc issues a warning and rewrites these to 0. These |
| 595 | // seems especially odd for the third argument since the default |
| 596 | // is 3. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 597 | if (i == 1) { |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 598 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 1) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 599 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 600 | << "0" << "1" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 601 | } else { |
| 602 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 603 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 604 | << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 608 | return false; |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 611 | /// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr, |
| 612 | /// int type). This simply type checks that type is one of the defined |
| 613 | /// constants (0-3). |
| 614 | bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) { |
| 615 | Expr *Arg = TheCall->getArg(1); |
| 616 | QualType ArgType = Arg->getType(); |
| 617 | const BuiltinType *BT = ArgType->getAsBuiltinType(); |
| 618 | llvm::APSInt Result(32); |
| 619 | if (!BT || BT->getKind() != BuiltinType::Int || |
| 620 | !Arg->isIntegerConstantExpr(Result, Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 621 | return Diag(TheCall->getLocStart(), diag::err_object_size_invalid_argument) |
| 622 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 626 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 627 | << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | return false; |
| 631 | } |
| 632 | |
Eli Friedman | 586d6a8 | 2009-05-03 06:04:26 +0000 | [diff] [blame] | 633 | /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 634 | /// This checks that val is a constant 1. |
| 635 | bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { |
| 636 | Expr *Arg = TheCall->getArg(1); |
| 637 | llvm::APSInt Result(32); |
| 638 | if (!Arg->isIntegerConstantExpr(Result, Context) || Result != 1) |
| 639 | return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val) |
| 640 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
| 641 | |
| 642 | return false; |
| 643 | } |
| 644 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 645 | // Handle i > 1 ? "x" : "y", recursivelly |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 646 | bool Sema::SemaCheckStringLiteral(const Expr *E, const CallExpr *TheCall, |
| 647 | bool HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 648 | unsigned format_idx, unsigned firstDataArg) { |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 649 | |
| 650 | switch (E->getStmtClass()) { |
| 651 | case Stmt::ConditionalOperatorClass: { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 652 | const ConditionalOperator *C = cast<ConditionalOperator>(E); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 653 | return SemaCheckStringLiteral(C->getLHS(), TheCall, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 654 | HasVAListArg, format_idx, firstDataArg) |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 655 | && SemaCheckStringLiteral(C->getRHS(), TheCall, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 656 | HasVAListArg, format_idx, firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | case Stmt::ImplicitCastExprClass: { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 660 | const ImplicitCastExpr *Expr = cast<ImplicitCastExpr>(E); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 661 | return SemaCheckStringLiteral(Expr->getSubExpr(), TheCall, HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 662 | format_idx, firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | case Stmt::ParenExprClass: { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 666 | const ParenExpr *Expr = cast<ParenExpr>(E); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 667 | return SemaCheckStringLiteral(Expr->getSubExpr(), TheCall, HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 668 | format_idx, firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 669 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 670 | |
| 671 | case Stmt::DeclRefExprClass: { |
| 672 | const DeclRefExpr *DR = cast<DeclRefExpr>(E); |
| 673 | |
| 674 | // As an exception, do not flag errors for variables binding to |
| 675 | // const string literals. |
| 676 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 677 | bool isConstant = false; |
| 678 | QualType T = DR->getType(); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 679 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 680 | if (const ArrayType *AT = Context.getAsArrayType(T)) { |
| 681 | isConstant = AT->getElementType().isConstant(Context); |
| 682 | } |
| 683 | else if (const PointerType *PT = T->getAsPointerType()) { |
| 684 | isConstant = T.isConstant(Context) && |
| 685 | PT->getPointeeType().isConstant(Context); |
| 686 | } |
| 687 | |
| 688 | if (isConstant) { |
| 689 | const VarDecl *Def = 0; |
| 690 | if (const Expr *Init = VD->getDefinition(Def)) |
| 691 | return SemaCheckStringLiteral(Init, TheCall, |
| 692 | HasVAListArg, format_idx, firstDataArg); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | return false; |
| 697 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 698 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 699 | case Stmt::ObjCStringLiteralClass: |
| 700 | case Stmt::StringLiteralClass: { |
| 701 | const StringLiteral *StrE = NULL; |
| 702 | |
| 703 | if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 704 | StrE = ObjCFExpr->getString(); |
| 705 | else |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 706 | StrE = cast<StringLiteral>(E); |
| 707 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 708 | if (StrE) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 709 | CheckPrintfString(StrE, E, TheCall, HasVAListArg, format_idx, |
| 710 | firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 711 | return true; |
| 712 | } |
| 713 | |
| 714 | return false; |
| 715 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 716 | |
| 717 | default: |
| 718 | return false; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
| 722 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 723 | /// CheckPrintfArguments - Check calls to printf (and similar functions) for |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 724 | /// correct use of format strings. |
| 725 | /// |
| 726 | /// HasVAListArg - A predicate indicating whether the printf-like |
| 727 | /// function is passed an explicit va_arg argument (e.g., vprintf) |
| 728 | /// |
| 729 | /// format_idx - The index into Args for the format string. |
| 730 | /// |
| 731 | /// Improper format strings to functions in the printf family can be |
| 732 | /// the source of bizarre bugs and very serious security holes. A |
| 733 | /// good source of information is available in the following paper |
| 734 | /// (which includes additional references): |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 735 | /// |
| 736 | /// FormatGuard: Automatic Protection From printf Format String |
| 737 | /// Vulnerabilities, Proceedings of the 10th USENIX Security Symposium, 2001. |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 738 | /// |
| 739 | /// Functionality implemented: |
| 740 | /// |
| 741 | /// We can statically check the following properties for string |
| 742 | /// literal format strings for non v.*printf functions (where the |
| 743 | /// arguments are passed directly): |
| 744 | // |
| 745 | /// (1) Are the number of format conversions equal to the number of |
| 746 | /// data arguments? |
| 747 | /// |
| 748 | /// (2) Does each format conversion correctly match the type of the |
| 749 | /// corresponding data argument? (TODO) |
| 750 | /// |
| 751 | /// Moreover, for all printf functions we can: |
| 752 | /// |
| 753 | /// (3) Check for a missing format string (when not caught by type checking). |
| 754 | /// |
| 755 | /// (4) Check for no-operation flags; e.g. using "#" with format |
| 756 | /// conversion 'c' (TODO) |
| 757 | /// |
| 758 | /// (5) Check the use of '%n', a major source of security holes. |
| 759 | /// |
| 760 | /// (6) Check for malformed format conversions that don't specify anything. |
| 761 | /// |
| 762 | /// (7) Check for empty format strings. e.g: printf(""); |
| 763 | /// |
| 764 | /// (8) Check that the format string is a wide literal. |
| 765 | /// |
Ted Kremenek | 6d43959 | 2008-03-03 16:50:00 +0000 | [diff] [blame] | 766 | /// (9) Also check the arguments of functions with the __format__ attribute. |
| 767 | /// (TODO). |
| 768 | /// |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 769 | /// All of these checks can be done by parsing the format string. |
| 770 | /// |
| 771 | /// For now, we ONLY do (1), (3), (5), (6), (7), and (8). |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 772 | void |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 773 | Sema::CheckPrintfArguments(const CallExpr *TheCall, bool HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 774 | unsigned format_idx, unsigned firstDataArg) { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 775 | const Expr *Fn = TheCall->getCallee(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 776 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 777 | // CHECK: printf-like function is called with no format string. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 778 | if (format_idx >= TheCall->getNumArgs()) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 779 | Diag(TheCall->getRParenLoc(), diag::warn_printf_missing_format_string) |
| 780 | << Fn->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 781 | return; |
| 782 | } |
| 783 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 784 | const Expr *OrigFormatExpr = TheCall->getArg(format_idx)->IgnoreParenCasts(); |
Chris Lattner | 459e848 | 2007-08-25 05:36:18 +0000 | [diff] [blame] | 785 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 786 | // CHECK: format string is not a string literal. |
| 787 | // |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 788 | // Dynamically generated format strings are difficult to |
| 789 | // automatically vet at compile time. Requiring that format strings |
| 790 | // are string literals: (1) permits the checking of format strings by |
| 791 | // the compiler and thereby (2) can practically remove the source of |
| 792 | // many format string exploits. |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 793 | |
| 794 | // Format string can be either ObjC string (e.g. @"%d") or |
| 795 | // C string (e.g. "%d") |
| 796 | // ObjC string uses the same format specifiers as C string, so we can use |
| 797 | // the same format string checking logic for both ObjC and C strings. |
Chris Lattner | 1cd3e1f | 2009-04-29 04:49:34 +0000 | [diff] [blame] | 798 | if (SemaCheckStringLiteral(OrigFormatExpr, TheCall, HasVAListArg, format_idx, |
| 799 | firstDataArg)) |
| 800 | return; // Literal format string found, check done! |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 801 | |
Chris Lattner | 1cd3e1f | 2009-04-29 04:49:34 +0000 | [diff] [blame] | 802 | // For vprintf* functions (i.e., HasVAListArg==true), we add a |
| 803 | // special check to see if the format string is a function parameter |
| 804 | // of the function calling the printf function. If the function |
| 805 | // has an attribute indicating it is a printf-like function, then we |
| 806 | // should suppress warnings concerning non-literals being used in a call |
| 807 | // to a vprintf function. For example: |
| 808 | // |
| 809 | // void |
| 810 | // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...) { |
| 811 | // va_list ap; |
| 812 | // va_start(ap, fmt); |
| 813 | // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". |
| 814 | // ... |
| 815 | // |
| 816 | // |
| 817 | // FIXME: We don't have full attribute support yet, so just check to see |
| 818 | // if the argument is a DeclRefExpr that references a parameter. We'll |
| 819 | // add proper support for checking the attribute later. |
| 820 | if (HasVAListArg) |
| 821 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(OrigFormatExpr)) |
| 822 | if (isa<ParmVarDecl>(DR->getDecl())) |
| 823 | return; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 824 | |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 825 | // If there are no arguments specified, warn with -Wformat-security, otherwise |
| 826 | // warn only with -Wformat-nonliteral. |
| 827 | if (TheCall->getNumArgs() == format_idx+1) |
| 828 | Diag(TheCall->getArg(format_idx)->getLocStart(), |
| 829 | diag::warn_printf_nonliteral_noargs) |
| 830 | << OrigFormatExpr->getSourceRange(); |
| 831 | else |
| 832 | Diag(TheCall->getArg(format_idx)->getLocStart(), |
| 833 | diag::warn_printf_nonliteral) |
| 834 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 835 | } |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 836 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 837 | void Sema::CheckPrintfString(const StringLiteral *FExpr, |
| 838 | const Expr *OrigFormatExpr, |
| 839 | const CallExpr *TheCall, bool HasVAListArg, |
| 840 | unsigned format_idx, unsigned firstDataArg) { |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 841 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 842 | const ObjCStringLiteral *ObjCFExpr = |
| 843 | dyn_cast<ObjCStringLiteral>(OrigFormatExpr); |
| 844 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 845 | // CHECK: is the format string a wide literal? |
| 846 | if (FExpr->isWide()) { |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 847 | Diag(FExpr->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 848 | diag::warn_printf_format_string_is_wide_literal) |
| 849 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 850 | return; |
| 851 | } |
| 852 | |
| 853 | // Str - The format string. NOTE: this is NOT null-terminated! |
Chris Lattner | b9fc856 | 2009-04-29 04:12:34 +0000 | [diff] [blame] | 854 | const char *Str = FExpr->getStrData(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 855 | |
| 856 | // CHECK: empty format string? |
Chris Lattner | b9fc856 | 2009-04-29 04:12:34 +0000 | [diff] [blame] | 857 | unsigned StrLen = FExpr->getByteLength(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 858 | |
| 859 | if (StrLen == 0) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 860 | Diag(FExpr->getLocStart(), diag::warn_printf_empty_format_string) |
| 861 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 862 | return; |
| 863 | } |
| 864 | |
| 865 | // We process the format string using a binary state machine. The |
| 866 | // current state is stored in CurrentState. |
| 867 | enum { |
| 868 | state_OrdChr, |
| 869 | state_Conversion |
| 870 | } CurrentState = state_OrdChr; |
| 871 | |
| 872 | // numConversions - The number of conversions seen so far. This is |
| 873 | // incremented as we traverse the format string. |
| 874 | unsigned numConversions = 0; |
| 875 | |
| 876 | // numDataArgs - The number of data arguments after the format |
| 877 | // string. This can only be determined for non vprintf-like |
| 878 | // functions. For those functions, this value is 1 (the sole |
| 879 | // va_arg argument). |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 880 | unsigned numDataArgs = TheCall->getNumArgs()-firstDataArg; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 881 | |
| 882 | // Inspect the format string. |
| 883 | unsigned StrIdx = 0; |
| 884 | |
| 885 | // LastConversionIdx - Index within the format string where we last saw |
| 886 | // a '%' character that starts a new format conversion. |
| 887 | unsigned LastConversionIdx = 0; |
| 888 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 889 | for (; StrIdx < StrLen; ++StrIdx) { |
Chris Lattner | 998568f | 2007-12-28 05:38:24 +0000 | [diff] [blame] | 890 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 891 | // Is the number of detected conversion conversions greater than |
| 892 | // the number of matching data arguments? If so, stop. |
| 893 | if (!HasVAListArg && numConversions > numDataArgs) break; |
| 894 | |
| 895 | // Handle "\0" |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 896 | if (Str[StrIdx] == '\0') { |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 897 | // The string returned by getStrData() is not null-terminated, |
| 898 | // so the presence of a null character is likely an error. |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 899 | Diag(getLocationOfStringLiteralByte(FExpr, StrIdx), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 900 | diag::warn_printf_format_string_contains_null_char) |
| 901 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 902 | return; |
| 903 | } |
| 904 | |
| 905 | // Ordinary characters (not processing a format conversion). |
| 906 | if (CurrentState == state_OrdChr) { |
| 907 | if (Str[StrIdx] == '%') { |
| 908 | CurrentState = state_Conversion; |
| 909 | LastConversionIdx = StrIdx; |
| 910 | } |
| 911 | continue; |
| 912 | } |
| 913 | |
| 914 | // Seen '%'. Now processing a format conversion. |
| 915 | switch (Str[StrIdx]) { |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 916 | // Handle dynamic precision or width specifier. |
| 917 | case '*': { |
| 918 | ++numConversions; |
| 919 | |
Ted Kremenek | 42ae3e8 | 2009-05-13 16:06:05 +0000 | [diff] [blame] | 920 | if (!HasVAListArg) { |
| 921 | if (numConversions > numDataArgs) { |
| 922 | SourceLocation Loc = getLocationOfStringLiteralByte(FExpr, StrIdx); |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 923 | |
Ted Kremenek | 42ae3e8 | 2009-05-13 16:06:05 +0000 | [diff] [blame] | 924 | if (Str[StrIdx-1] == '.') |
| 925 | Diag(Loc, diag::warn_printf_asterisk_precision_missing_arg) |
| 926 | << OrigFormatExpr->getSourceRange(); |
| 927 | else |
| 928 | Diag(Loc, diag::warn_printf_asterisk_width_missing_arg) |
| 929 | << OrigFormatExpr->getSourceRange(); |
| 930 | |
| 931 | // Don't do any more checking. We'll just emit spurious errors. |
| 932 | return; |
| 933 | } |
| 934 | |
| 935 | // Perform type checking on width/precision specifier. |
| 936 | const Expr *E = TheCall->getArg(format_idx+numConversions); |
| 937 | if (const BuiltinType *BT = E->getType()->getAsBuiltinType()) |
| 938 | if (BT->getKind() == BuiltinType::Int) |
| 939 | break; |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 940 | |
Ted Kremenek | 42ae3e8 | 2009-05-13 16:06:05 +0000 | [diff] [blame] | 941 | SourceLocation Loc = getLocationOfStringLiteralByte(FExpr, StrIdx); |
| 942 | |
| 943 | if (Str[StrIdx-1] == '.') |
| 944 | Diag(Loc, diag::warn_printf_asterisk_precision_wrong_type) |
| 945 | << E->getType() << E->getSourceRange(); |
| 946 | else |
| 947 | Diag(Loc, diag::warn_printf_asterisk_width_wrong_type) |
| 948 | << E->getType() << E->getSourceRange(); |
| 949 | |
| 950 | break; |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 951 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | // Characters which can terminate a format conversion |
| 955 | // (e.g. "%d"). Characters that specify length modifiers or |
| 956 | // other flags are handled by the default case below. |
| 957 | // |
| 958 | // FIXME: additional checks will go into the following cases. |
| 959 | case 'i': |
| 960 | case 'd': |
| 961 | case 'o': |
| 962 | case 'u': |
| 963 | case 'x': |
| 964 | case 'X': |
| 965 | case 'D': |
| 966 | case 'O': |
| 967 | case 'U': |
| 968 | case 'e': |
| 969 | case 'E': |
| 970 | case 'f': |
| 971 | case 'F': |
| 972 | case 'g': |
| 973 | case 'G': |
| 974 | case 'a': |
| 975 | case 'A': |
| 976 | case 'c': |
| 977 | case 'C': |
| 978 | case 'S': |
| 979 | case 's': |
| 980 | case 'p': |
| 981 | ++numConversions; |
| 982 | CurrentState = state_OrdChr; |
| 983 | break; |
| 984 | |
| 985 | // CHECK: Are we using "%n"? Issue a warning. |
| 986 | case 'n': { |
| 987 | ++numConversions; |
| 988 | CurrentState = state_OrdChr; |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 989 | SourceLocation Loc = getLocationOfStringLiteralByte(FExpr, |
| 990 | LastConversionIdx); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 991 | |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 992 | Diag(Loc, diag::warn_printf_write_back)<<OrigFormatExpr->getSourceRange(); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 993 | break; |
| 994 | } |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 995 | |
| 996 | // Handle "%@" |
| 997 | case '@': |
| 998 | // %@ is allowed in ObjC format strings only. |
| 999 | if(ObjCFExpr != NULL) |
| 1000 | CurrentState = state_OrdChr; |
| 1001 | else { |
| 1002 | // Issue a warning: invalid format conversion. |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 1003 | SourceLocation Loc = |
| 1004 | getLocationOfStringLiteralByte(FExpr, LastConversionIdx); |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1005 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 1006 | Diag(Loc, diag::warn_printf_invalid_conversion) |
| 1007 | << std::string(Str+LastConversionIdx, |
| 1008 | Str+std::min(LastConversionIdx+2, StrLen)) |
| 1009 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1010 | } |
| 1011 | ++numConversions; |
| 1012 | break; |
| 1013 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1014 | // Handle "%%" |
| 1015 | case '%': |
| 1016 | // Sanity check: Was the first "%" character the previous one? |
| 1017 | // If not, we will assume that we have a malformed format |
| 1018 | // conversion, and that the current "%" character is the start |
| 1019 | // of a new conversion. |
| 1020 | if (StrIdx - LastConversionIdx == 1) |
| 1021 | CurrentState = state_OrdChr; |
| 1022 | else { |
| 1023 | // Issue a warning: invalid format conversion. |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 1024 | SourceLocation Loc = |
| 1025 | getLocationOfStringLiteralByte(FExpr, LastConversionIdx); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1026 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 1027 | Diag(Loc, diag::warn_printf_invalid_conversion) |
| 1028 | << std::string(Str+LastConversionIdx, Str+StrIdx) |
| 1029 | << OrigFormatExpr->getSourceRange(); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1030 | |
| 1031 | // This conversion is broken. Advance to the next format |
| 1032 | // conversion. |
| 1033 | LastConversionIdx = StrIdx; |
| 1034 | ++numConversions; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1035 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1036 | break; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1037 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1038 | default: |
| 1039 | // This case catches all other characters: flags, widths, etc. |
| 1040 | // We should eventually process those as well. |
| 1041 | break; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | if (CurrentState == state_Conversion) { |
| 1046 | // Issue a warning: invalid format conversion. |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 1047 | SourceLocation Loc = |
| 1048 | getLocationOfStringLiteralByte(FExpr, LastConversionIdx); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1049 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 1050 | Diag(Loc, diag::warn_printf_invalid_conversion) |
| 1051 | << std::string(Str+LastConversionIdx, |
| 1052 | Str+std::min(LastConversionIdx+2, StrLen)) |
| 1053 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1054 | return; |
| 1055 | } |
| 1056 | |
| 1057 | if (!HasVAListArg) { |
| 1058 | // CHECK: Does the number of format conversions exceed the number |
| 1059 | // of data arguments? |
| 1060 | if (numConversions > numDataArgs) { |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 1061 | SourceLocation Loc = |
| 1062 | getLocationOfStringLiteralByte(FExpr, LastConversionIdx); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1063 | |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1064 | Diag(Loc, diag::warn_printf_insufficient_data_args) |
| 1065 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1066 | } |
| 1067 | // CHECK: Does the number of data arguments exceed the number of |
| 1068 | // format conversions in the format string? |
| 1069 | else if (numConversions < numDataArgs) |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1070 | Diag(TheCall->getArg(format_idx+numConversions+1)->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1071 | diag::warn_printf_too_many_data_args) |
| 1072 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1073 | } |
| 1074 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1075 | |
| 1076 | //===--- CHECK: Return Address of Stack Variable --------------------------===// |
| 1077 | |
| 1078 | static DeclRefExpr* EvalVal(Expr *E); |
| 1079 | static DeclRefExpr* EvalAddr(Expr* E); |
| 1080 | |
| 1081 | /// CheckReturnStackAddr - Check if a return statement returns the address |
| 1082 | /// of a stack variable. |
| 1083 | void |
| 1084 | Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, |
| 1085 | SourceLocation ReturnLoc) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1086 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1087 | // Perform checking for returned stack addresses. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1088 | if (lhsType->isPointerType() || lhsType->isBlockPointerType()) { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1089 | if (DeclRefExpr *DR = EvalAddr(RetValExp)) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1090 | Diag(DR->getLocStart(), diag::warn_ret_stack_addr) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1091 | << DR->getDecl()->getDeclName() << RetValExp->getSourceRange(); |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 1092 | |
| 1093 | // Skip over implicit cast expressions when checking for block expressions. |
| 1094 | if (ImplicitCastExpr *IcExpr = |
| 1095 | dyn_cast_or_null<ImplicitCastExpr>(RetValExp)) |
| 1096 | RetValExp = IcExpr->getSubExpr(); |
| 1097 | |
Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 1098 | if (BlockExpr *C = dyn_cast_or_null<BlockExpr>(RetValExp)) |
Mike Stump | 397195b | 2009-04-17 00:09:41 +0000 | [diff] [blame] | 1099 | if (C->hasBlockDeclRefExprs()) |
| 1100 | Diag(C->getLocStart(), diag::err_ret_local_block) |
| 1101 | << C->getSourceRange(); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1102 | } |
| 1103 | // Perform checking for stack values returned by reference. |
| 1104 | else if (lhsType->isReferenceType()) { |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1105 | // Check for a reference to the stack |
| 1106 | if (DeclRefExpr *DR = EvalVal(RetValExp)) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1107 | Diag(DR->getLocStart(), diag::warn_ret_stack_ref) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1108 | << DR->getDecl()->getDeclName() << RetValExp->getSourceRange(); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that |
| 1113 | /// check if the expression in a return statement evaluates to an address |
| 1114 | /// to a location on the stack. The recursion is used to traverse the |
| 1115 | /// AST of the return expression, with recursion backtracking when we |
| 1116 | /// encounter a subexpression that (1) clearly does not lead to the address |
| 1117 | /// of a stack variable or (2) is something we cannot determine leads to |
| 1118 | /// the address of a stack variable based on such local checking. |
| 1119 | /// |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 1120 | /// EvalAddr processes expressions that are pointers that are used as |
| 1121 | /// references (and not L-values). EvalVal handles all other values. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1122 | /// At the base case of the recursion is a check for a DeclRefExpr* in |
| 1123 | /// the refers to a stack variable. |
| 1124 | /// |
| 1125 | /// This implementation handles: |
| 1126 | /// |
| 1127 | /// * pointer-to-pointer casts |
| 1128 | /// * implicit conversions from array references to pointers |
| 1129 | /// * taking the address of fields |
| 1130 | /// * arbitrary interplay between "&" and "*" operators |
| 1131 | /// * pointer arithmetic from an address of a stack variable |
| 1132 | /// * taking the address of an array element where the array is on the stack |
| 1133 | static DeclRefExpr* EvalAddr(Expr *E) { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1134 | // We should only be called for evaluating pointer expressions. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1135 | assert((E->getType()->isPointerType() || |
| 1136 | E->getType()->isBlockPointerType() || |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1137 | E->getType()->isObjCQualifiedIdType()) && |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1138 | "EvalAddr only works on pointers"); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1139 | |
| 1140 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 1141 | // viewed AST node. We then recursively traverse the AST by calling |
| 1142 | // EvalAddr and EvalVal appropriately. |
| 1143 | switch (E->getStmtClass()) { |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1144 | case Stmt::ParenExprClass: |
| 1145 | // Ignore parentheses. |
| 1146 | return EvalAddr(cast<ParenExpr>(E)->getSubExpr()); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1147 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1148 | case Stmt::UnaryOperatorClass: { |
| 1149 | // The only unary operator that make sense to handle here |
| 1150 | // is AddrOf. All others don't make sense as pointers. |
| 1151 | UnaryOperator *U = cast<UnaryOperator>(E); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1152 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1153 | if (U->getOpcode() == UnaryOperator::AddrOf) |
| 1154 | return EvalVal(U->getSubExpr()); |
| 1155 | else |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1156 | return NULL; |
| 1157 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1158 | |
| 1159 | case Stmt::BinaryOperatorClass: { |
| 1160 | // Handle pointer arithmetic. All other binary operators are not valid |
| 1161 | // in this context. |
| 1162 | BinaryOperator *B = cast<BinaryOperator>(E); |
| 1163 | BinaryOperator::Opcode op = B->getOpcode(); |
| 1164 | |
| 1165 | if (op != BinaryOperator::Add && op != BinaryOperator::Sub) |
| 1166 | return NULL; |
| 1167 | |
| 1168 | Expr *Base = B->getLHS(); |
| 1169 | |
| 1170 | // Determine which argument is the real pointer base. It could be |
| 1171 | // the RHS argument instead of the LHS. |
| 1172 | if (!Base->getType()->isPointerType()) Base = B->getRHS(); |
| 1173 | |
| 1174 | assert (Base->getType()->isPointerType()); |
| 1175 | return EvalAddr(Base); |
| 1176 | } |
Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 1177 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1178 | // For conditional operators we need to see if either the LHS or RHS are |
| 1179 | // valid DeclRefExpr*s. If one of them is valid, we return it. |
| 1180 | case Stmt::ConditionalOperatorClass: { |
| 1181 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 1182 | |
| 1183 | // Handle the GNU extension for missing LHS. |
| 1184 | if (Expr *lhsExpr = C->getLHS()) |
| 1185 | if (DeclRefExpr* LHS = EvalAddr(lhsExpr)) |
| 1186 | return LHS; |
| 1187 | |
| 1188 | return EvalAddr(C->getRHS()); |
| 1189 | } |
| 1190 | |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 1191 | // For casts, we need to handle conversions from arrays to |
| 1192 | // pointer values, and pointer-to-pointer conversions. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1193 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1194 | case Stmt::CStyleCastExprClass: |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1195 | case Stmt::CXXFunctionalCastExprClass: { |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1196 | Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 1197 | QualType T = SubExpr->getType(); |
| 1198 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1199 | if (SubExpr->getType()->isPointerType() || |
| 1200 | SubExpr->getType()->isBlockPointerType() || |
| 1201 | SubExpr->getType()->isObjCQualifiedIdType()) |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 1202 | return EvalAddr(SubExpr); |
| 1203 | else if (T->isArrayType()) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1204 | return EvalVal(SubExpr); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1205 | else |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 1206 | return 0; |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | // C++ casts. For dynamic casts, static casts, and const casts, we |
| 1210 | // are always converting from a pointer-to-pointer, so we just blow |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1211 | // through the cast. In the case the dynamic cast doesn't fail (and |
| 1212 | // return NULL), we take the conservative route and report cases |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1213 | // where we return the address of a stack variable. For Reinterpre |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1214 | // FIXME: The comment about is wrong; we're not always converting |
| 1215 | // from pointer to pointer. I'm guessing that this code should also |
| 1216 | // handle references to objects. |
| 1217 | case Stmt::CXXStaticCastExprClass: |
| 1218 | case Stmt::CXXDynamicCastExprClass: |
| 1219 | case Stmt::CXXConstCastExprClass: |
| 1220 | case Stmt::CXXReinterpretCastExprClass: { |
| 1221 | Expr *S = cast<CXXNamedCastExpr>(E)->getSubExpr(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1222 | if (S->getType()->isPointerType() || S->getType()->isBlockPointerType()) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1223 | return EvalAddr(S); |
| 1224 | else |
| 1225 | return NULL; |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | // Everything else: we simply don't reason about them. |
| 1229 | default: |
| 1230 | return NULL; |
| 1231 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | |
| 1235 | /// EvalVal - This function is complements EvalAddr in the mutual recursion. |
| 1236 | /// See the comments for EvalAddr for more details. |
| 1237 | static DeclRefExpr* EvalVal(Expr *E) { |
| 1238 | |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 1239 | // We should only be called for evaluating non-pointer expressions, or |
| 1240 | // expressions with a pointer type that are not used as references but instead |
| 1241 | // are l-values (e.g., DeclRefExpr with a pointer type). |
| 1242 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1243 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 1244 | // viewed AST node. We then recursively traverse the AST by calling |
| 1245 | // EvalAddr and EvalVal appropriately. |
| 1246 | switch (E->getStmtClass()) { |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1247 | case Stmt::DeclRefExprClass: |
| 1248 | case Stmt::QualifiedDeclRefExprClass: { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1249 | // DeclRefExpr: the base case. When we hit a DeclRefExpr we are looking |
| 1250 | // at code that refers to a variable's name. We check if it has local |
| 1251 | // storage within the function, and if so, return the expression. |
| 1252 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
| 1253 | |
| 1254 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1255 | if(V->hasLocalStorage() && !V->getType()->isReferenceType()) return DR; |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1256 | |
| 1257 | return NULL; |
| 1258 | } |
| 1259 | |
| 1260 | case Stmt::ParenExprClass: |
| 1261 | // Ignore parentheses. |
| 1262 | return EvalVal(cast<ParenExpr>(E)->getSubExpr()); |
| 1263 | |
| 1264 | case Stmt::UnaryOperatorClass: { |
| 1265 | // The only unary operator that make sense to handle here |
| 1266 | // is Deref. All others don't resolve to a "name." This includes |
| 1267 | // handling all sorts of rvalues passed to a unary operator. |
| 1268 | UnaryOperator *U = cast<UnaryOperator>(E); |
| 1269 | |
| 1270 | if (U->getOpcode() == UnaryOperator::Deref) |
| 1271 | return EvalAddr(U->getSubExpr()); |
| 1272 | |
| 1273 | return NULL; |
| 1274 | } |
| 1275 | |
| 1276 | case Stmt::ArraySubscriptExprClass: { |
| 1277 | // Array subscripts are potential references to data on the stack. We |
| 1278 | // retrieve the DeclRefExpr* for the array variable if it indeed |
| 1279 | // has local storage. |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 1280 | return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase()); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | case Stmt::ConditionalOperatorClass: { |
| 1284 | // For conditional operators we need to see if either the LHS or RHS are |
| 1285 | // non-NULL DeclRefExpr's. If one is non-NULL, we return it. |
| 1286 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 1287 | |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 1288 | // Handle the GNU extension for missing LHS. |
| 1289 | if (Expr *lhsExpr = C->getLHS()) |
| 1290 | if (DeclRefExpr *LHS = EvalVal(lhsExpr)) |
| 1291 | return LHS; |
| 1292 | |
| 1293 | return EvalVal(C->getRHS()); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | // Accesses to members are potential references to data on the stack. |
| 1297 | case Stmt::MemberExprClass: { |
| 1298 | MemberExpr *M = cast<MemberExpr>(E); |
| 1299 | |
| 1300 | // Check for indirect access. We only want direct field accesses. |
| 1301 | if (!M->isArrow()) |
| 1302 | return EvalVal(M->getBase()); |
| 1303 | else |
| 1304 | return NULL; |
| 1305 | } |
| 1306 | |
| 1307 | // Everything else: we simply don't reason about them. |
| 1308 | default: |
| 1309 | return NULL; |
| 1310 | } |
| 1311 | } |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1312 | |
| 1313 | //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// |
| 1314 | |
| 1315 | /// Check for comparisons of floating point operands using != and ==. |
| 1316 | /// Issue a warning if these are no self-comparisons, as they are not likely |
| 1317 | /// to do what the programmer intended. |
| 1318 | void Sema::CheckFloatComparison(SourceLocation loc, Expr* lex, Expr *rex) { |
| 1319 | bool EmitWarning = true; |
| 1320 | |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 1321 | Expr* LeftExprSansParen = lex->IgnoreParens(); |
Ted Kremenek | 32e97b6 | 2008-01-17 17:55:13 +0000 | [diff] [blame] | 1322 | Expr* RightExprSansParen = rex->IgnoreParens(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1323 | |
| 1324 | // Special case: check for x == x (which is OK). |
| 1325 | // Do not emit warnings for such cases. |
| 1326 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) |
| 1327 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) |
| 1328 | if (DRL->getDecl() == DRR->getDecl()) |
| 1329 | EmitWarning = false; |
| 1330 | |
Ted Kremenek | 1b500bb | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 1331 | |
| 1332 | // Special case: check for comparisons against literals that can be exactly |
| 1333 | // represented by APFloat. In such cases, do not emit a warning. This |
| 1334 | // is a heuristic: often comparison against such literals are used to |
| 1335 | // detect if a value in a variable has not changed. This clearly can |
| 1336 | // lead to false negatives. |
| 1337 | if (EmitWarning) { |
| 1338 | if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { |
| 1339 | if (FLL->isExact()) |
| 1340 | EmitWarning = false; |
| 1341 | } |
| 1342 | else |
| 1343 | if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)){ |
| 1344 | if (FLR->isExact()) |
| 1345 | EmitWarning = false; |
| 1346 | } |
| 1347 | } |
| 1348 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1349 | // Check for comparisons with builtin types. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1350 | if (EmitWarning) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1351 | if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1352 | if (CL->isBuiltinCall(Context)) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1353 | EmitWarning = false; |
| 1354 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1355 | if (EmitWarning) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1356 | if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1357 | if (CR->isBuiltinCall(Context)) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1358 | EmitWarning = false; |
| 1359 | |
| 1360 | // Emit the diagnostic. |
| 1361 | if (EmitWarning) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1362 | Diag(loc, diag::warn_floatingpoint_eq) |
| 1363 | << lex->getSourceRange() << rex->getSourceRange(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1364 | } |