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