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); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame^] | 504 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame^] | 515 | if (OrigArg0->isTypeDependent() || OrigArg1->isTypeDependent()) |
| 516 | return false; |
| 517 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 518 | // If the common type isn't a real floating type, then the arguments were |
| 519 | // invalid for this operation. |
| 520 | if (!Res->isRealFloatingType()) |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 521 | return Diag(OrigArg0->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 522 | diag::err_typecheck_call_invalid_ordered_compare) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 523 | << OrigArg0->getType() << OrigArg1->getType() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 524 | << SourceRange(OrigArg0->getLocStart(), OrigArg1->getLocEnd()); |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 525 | |
| 526 | return false; |
| 527 | } |
| 528 | |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 529 | bool Sema::SemaBuiltinStackAddress(CallExpr *TheCall) { |
| 530 | // The signature for these builtins is exact; the only thing we need |
| 531 | // to check is that the argument is a constant. |
| 532 | SourceLocation Loc; |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame^] | 533 | if (!TheCall->getArg(0)->isTypeDependent() && |
| 534 | !TheCall->getArg(0)->isValueDependent() && |
| 535 | !TheCall->getArg(0)->isIntegerConstantExpr(Context, &Loc)) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 536 | return Diag(Loc, diag::err_stack_const_level) << TheCall->getSourceRange(); |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 537 | |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 538 | return false; |
| 539 | } |
| 540 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 541 | /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. |
| 542 | // This is declared to take (...), so we have to check everything. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 543 | Action::OwningExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 544 | if (TheCall->getNumArgs() < 3) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 545 | return ExprError(Diag(TheCall->getLocEnd(), |
| 546 | diag::err_typecheck_call_too_few_args) |
| 547 | << 0 /*function call*/ << TheCall->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 548 | |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame^] | 549 | unsigned numElements = std::numeric_limits<unsigned>::max(); |
| 550 | if (!TheCall->getArg(0)->isTypeDependent() && |
| 551 | !TheCall->getArg(1)->isTypeDependent()) { |
| 552 | QualType FAType = TheCall->getArg(0)->getType(); |
| 553 | QualType SAType = TheCall->getArg(1)->getType(); |
| 554 | |
| 555 | if (!FAType->isVectorType() || !SAType->isVectorType()) { |
| 556 | Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector) |
| 557 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
| 558 | TheCall->getArg(1)->getLocEnd()); |
| 559 | return ExprError(); |
| 560 | } |
| 561 | |
| 562 | if (Context.getCanonicalType(FAType).getUnqualifiedType() != |
| 563 | Context.getCanonicalType(SAType).getUnqualifiedType()) { |
| 564 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
| 565 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
| 566 | TheCall->getArg(1)->getLocEnd()); |
| 567 | return ExprError(); |
| 568 | } |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 569 | |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame^] | 570 | numElements = FAType->getAsVectorType()->getNumElements(); |
| 571 | if (TheCall->getNumArgs() != numElements+2) { |
| 572 | if (TheCall->getNumArgs() < numElements+2) |
| 573 | return ExprError(Diag(TheCall->getLocEnd(), |
| 574 | diag::err_typecheck_call_too_few_args) |
| 575 | << 0 /*function call*/ << TheCall->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 576 | return ExprError(Diag(TheCall->getLocEnd(), |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame^] | 577 | diag::err_typecheck_call_too_many_args) |
| 578 | << 0 /*function call*/ << TheCall->getSourceRange()); |
| 579 | } |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame^] | 583 | if (TheCall->getArg(i)->isTypeDependent() || |
| 584 | TheCall->getArg(i)->isValueDependent()) |
| 585 | continue; |
| 586 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 587 | llvm::APSInt Result(32); |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 588 | if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 589 | return ExprError(Diag(TheCall->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 590 | diag::err_shufflevector_nonconstant_argument) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 591 | << TheCall->getArg(i)->getSourceRange()); |
| 592 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 593 | if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 594 | return ExprError(Diag(TheCall->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 595 | diag::err_shufflevector_argument_too_large) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 596 | << TheCall->getArg(i)->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | llvm::SmallVector<Expr*, 32> exprs; |
| 600 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 601 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 602 | exprs.push_back(TheCall->getArg(i)); |
| 603 | TheCall->setArg(i, 0); |
| 604 | } |
| 605 | |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame^] | 606 | return Owned(new (Context) ShuffleVectorExpr(exprs.begin(), exprs.size(), |
| 607 | exprs[0]->getType(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 608 | TheCall->getCallee()->getLocStart(), |
| 609 | TheCall->getRParenLoc())); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 610 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 611 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 612 | /// SemaBuiltinPrefetch - Handle __builtin_prefetch. |
| 613 | // This is declared to take (const void*, ...) and can take two |
| 614 | // optional constant int args. |
| 615 | bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 616 | unsigned NumArgs = TheCall->getNumArgs(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 617 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 618 | if (NumArgs > 3) |
| 619 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_many_args) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 620 | << 0 /*function call*/ << TheCall->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 621 | |
| 622 | // Argument 0 is checked for us and the remaining arguments must be |
| 623 | // constant integers. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 624 | for (unsigned i = 1; i != NumArgs; ++i) { |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 625 | Expr *Arg = TheCall->getArg(i); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame^] | 626 | if (Arg->isTypeDependent()) |
| 627 | continue; |
| 628 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 629 | QualType RWType = Arg->getType(); |
| 630 | |
| 631 | const BuiltinType *BT = RWType->getAsBuiltinType(); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 632 | llvm::APSInt Result; |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame^] | 633 | if (!BT || BT->getKind() != BuiltinType::Int) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 634 | return Diag(TheCall->getLocStart(), diag::err_prefetch_invalid_argument) |
| 635 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame^] | 636 | |
| 637 | if (Arg->isValueDependent()) |
| 638 | continue; |
| 639 | |
| 640 | if (!Arg->isIntegerConstantExpr(Result, Context)) |
| 641 | return Diag(TheCall->getLocStart(), diag::err_prefetch_invalid_argument) |
| 642 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 643 | |
| 644 | // FIXME: gcc issues a warning and rewrites these to 0. These |
| 645 | // seems especially odd for the third argument since the default |
| 646 | // is 3. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 647 | if (i == 1) { |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 648 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 1) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 649 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 650 | << "0" << "1" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 651 | } else { |
| 652 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 653 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 654 | << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 658 | return false; |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 661 | /// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr, |
| 662 | /// int type). This simply type checks that type is one of the defined |
| 663 | /// constants (0-3). |
| 664 | bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) { |
| 665 | Expr *Arg = TheCall->getArg(1); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame^] | 666 | if (Arg->isTypeDependent()) |
| 667 | return false; |
| 668 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 669 | QualType ArgType = Arg->getType(); |
| 670 | const BuiltinType *BT = ArgType->getAsBuiltinType(); |
| 671 | llvm::APSInt Result(32); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame^] | 672 | if (!BT || BT->getKind() != BuiltinType::Int) |
| 673 | return Diag(TheCall->getLocStart(), diag::err_object_size_invalid_argument) |
| 674 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
| 675 | |
| 676 | if (Arg->isValueDependent()) |
| 677 | return false; |
| 678 | |
| 679 | if (!Arg->isIntegerConstantExpr(Result, Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 680 | return Diag(TheCall->getLocStart(), diag::err_object_size_invalid_argument) |
| 681 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 685 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 686 | << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | return false; |
| 690 | } |
| 691 | |
Eli Friedman | 586d6a8 | 2009-05-03 06:04:26 +0000 | [diff] [blame] | 692 | /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 693 | /// This checks that val is a constant 1. |
| 694 | bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { |
| 695 | Expr *Arg = TheCall->getArg(1); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame^] | 696 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 697 | return false; |
| 698 | |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 699 | llvm::APSInt Result(32); |
| 700 | if (!Arg->isIntegerConstantExpr(Result, Context) || Result != 1) |
| 701 | return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val) |
| 702 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
| 703 | |
| 704 | return false; |
| 705 | } |
| 706 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 707 | // Handle i > 1 ? "x" : "y", recursivelly |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 708 | bool Sema::SemaCheckStringLiteral(const Expr *E, const CallExpr *TheCall, |
| 709 | bool HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 710 | unsigned format_idx, unsigned firstDataArg) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame^] | 711 | if (E->isTypeDependent() || E->isValueDependent()) |
| 712 | return false; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 713 | |
| 714 | switch (E->getStmtClass()) { |
| 715 | case Stmt::ConditionalOperatorClass: { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 716 | const ConditionalOperator *C = cast<ConditionalOperator>(E); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 717 | return SemaCheckStringLiteral(C->getLHS(), TheCall, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 718 | HasVAListArg, format_idx, firstDataArg) |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 719 | && SemaCheckStringLiteral(C->getRHS(), TheCall, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 720 | HasVAListArg, format_idx, firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | case Stmt::ImplicitCastExprClass: { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 724 | const ImplicitCastExpr *Expr = cast<ImplicitCastExpr>(E); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 725 | return SemaCheckStringLiteral(Expr->getSubExpr(), TheCall, HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 726 | format_idx, firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | case Stmt::ParenExprClass: { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 730 | const ParenExpr *Expr = cast<ParenExpr>(E); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 731 | return SemaCheckStringLiteral(Expr->getSubExpr(), TheCall, HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 732 | format_idx, firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 733 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 734 | |
| 735 | case Stmt::DeclRefExprClass: { |
| 736 | const DeclRefExpr *DR = cast<DeclRefExpr>(E); |
| 737 | |
| 738 | // As an exception, do not flag errors for variables binding to |
| 739 | // const string literals. |
| 740 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 741 | bool isConstant = false; |
| 742 | QualType T = DR->getType(); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 743 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 744 | if (const ArrayType *AT = Context.getAsArrayType(T)) { |
| 745 | isConstant = AT->getElementType().isConstant(Context); |
| 746 | } |
| 747 | else if (const PointerType *PT = T->getAsPointerType()) { |
| 748 | isConstant = T.isConstant(Context) && |
| 749 | PT->getPointeeType().isConstant(Context); |
| 750 | } |
| 751 | |
| 752 | if (isConstant) { |
| 753 | const VarDecl *Def = 0; |
| 754 | if (const Expr *Init = VD->getDefinition(Def)) |
| 755 | return SemaCheckStringLiteral(Init, TheCall, |
| 756 | HasVAListArg, format_idx, firstDataArg); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | return false; |
| 761 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 762 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 763 | case Stmt::ObjCStringLiteralClass: |
| 764 | case Stmt::StringLiteralClass: { |
| 765 | const StringLiteral *StrE = NULL; |
| 766 | |
| 767 | if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 768 | StrE = ObjCFExpr->getString(); |
| 769 | else |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 770 | StrE = cast<StringLiteral>(E); |
| 771 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 772 | if (StrE) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 773 | CheckPrintfString(StrE, E, TheCall, HasVAListArg, format_idx, |
| 774 | firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 775 | return true; |
| 776 | } |
| 777 | |
| 778 | return false; |
| 779 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 780 | |
| 781 | default: |
| 782 | return false; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | |
| 786 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 787 | /// CheckPrintfArguments - Check calls to printf (and similar functions) for |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 788 | /// correct use of format strings. |
| 789 | /// |
| 790 | /// HasVAListArg - A predicate indicating whether the printf-like |
| 791 | /// function is passed an explicit va_arg argument (e.g., vprintf) |
| 792 | /// |
| 793 | /// format_idx - The index into Args for the format string. |
| 794 | /// |
| 795 | /// Improper format strings to functions in the printf family can be |
| 796 | /// the source of bizarre bugs and very serious security holes. A |
| 797 | /// good source of information is available in the following paper |
| 798 | /// (which includes additional references): |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 799 | /// |
| 800 | /// FormatGuard: Automatic Protection From printf Format String |
| 801 | /// Vulnerabilities, Proceedings of the 10th USENIX Security Symposium, 2001. |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 802 | /// |
| 803 | /// Functionality implemented: |
| 804 | /// |
| 805 | /// We can statically check the following properties for string |
| 806 | /// literal format strings for non v.*printf functions (where the |
| 807 | /// arguments are passed directly): |
| 808 | // |
| 809 | /// (1) Are the number of format conversions equal to the number of |
| 810 | /// data arguments? |
| 811 | /// |
| 812 | /// (2) Does each format conversion correctly match the type of the |
| 813 | /// corresponding data argument? (TODO) |
| 814 | /// |
| 815 | /// Moreover, for all printf functions we can: |
| 816 | /// |
| 817 | /// (3) Check for a missing format string (when not caught by type checking). |
| 818 | /// |
| 819 | /// (4) Check for no-operation flags; e.g. using "#" with format |
| 820 | /// conversion 'c' (TODO) |
| 821 | /// |
| 822 | /// (5) Check the use of '%n', a major source of security holes. |
| 823 | /// |
| 824 | /// (6) Check for malformed format conversions that don't specify anything. |
| 825 | /// |
| 826 | /// (7) Check for empty format strings. e.g: printf(""); |
| 827 | /// |
| 828 | /// (8) Check that the format string is a wide literal. |
| 829 | /// |
Ted Kremenek | 6d43959 | 2008-03-03 16:50:00 +0000 | [diff] [blame] | 830 | /// (9) Also check the arguments of functions with the __format__ attribute. |
| 831 | /// (TODO). |
| 832 | /// |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 833 | /// All of these checks can be done by parsing the format string. |
| 834 | /// |
| 835 | /// For now, we ONLY do (1), (3), (5), (6), (7), and (8). |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 836 | void |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 837 | Sema::CheckPrintfArguments(const CallExpr *TheCall, bool HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 838 | unsigned format_idx, unsigned firstDataArg) { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 839 | const Expr *Fn = TheCall->getCallee(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 840 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 841 | // CHECK: printf-like function is called with no format string. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 842 | if (format_idx >= TheCall->getNumArgs()) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 843 | Diag(TheCall->getRParenLoc(), diag::warn_printf_missing_format_string) |
| 844 | << Fn->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 845 | return; |
| 846 | } |
| 847 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 848 | const Expr *OrigFormatExpr = TheCall->getArg(format_idx)->IgnoreParenCasts(); |
Chris Lattner | 459e848 | 2007-08-25 05:36:18 +0000 | [diff] [blame] | 849 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 850 | // CHECK: format string is not a string literal. |
| 851 | // |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 852 | // Dynamically generated format strings are difficult to |
| 853 | // automatically vet at compile time. Requiring that format strings |
| 854 | // are string literals: (1) permits the checking of format strings by |
| 855 | // the compiler and thereby (2) can practically remove the source of |
| 856 | // many format string exploits. |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 857 | |
| 858 | // Format string can be either ObjC string (e.g. @"%d") or |
| 859 | // C string (e.g. "%d") |
| 860 | // ObjC string uses the same format specifiers as C string, so we can use |
| 861 | // the same format string checking logic for both ObjC and C strings. |
Chris Lattner | 1cd3e1f | 2009-04-29 04:49:34 +0000 | [diff] [blame] | 862 | if (SemaCheckStringLiteral(OrigFormatExpr, TheCall, HasVAListArg, format_idx, |
| 863 | firstDataArg)) |
| 864 | return; // Literal format string found, check done! |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 865 | |
Chris Lattner | 1cd3e1f | 2009-04-29 04:49:34 +0000 | [diff] [blame] | 866 | // For vprintf* functions (i.e., HasVAListArg==true), we add a |
| 867 | // special check to see if the format string is a function parameter |
| 868 | // of the function calling the printf function. If the function |
| 869 | // has an attribute indicating it is a printf-like function, then we |
| 870 | // should suppress warnings concerning non-literals being used in a call |
| 871 | // to a vprintf function. For example: |
| 872 | // |
| 873 | // void |
| 874 | // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...) { |
| 875 | // va_list ap; |
| 876 | // va_start(ap, fmt); |
| 877 | // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". |
| 878 | // ... |
| 879 | // |
| 880 | // |
| 881 | // FIXME: We don't have full attribute support yet, so just check to see |
| 882 | // if the argument is a DeclRefExpr that references a parameter. We'll |
| 883 | // add proper support for checking the attribute later. |
| 884 | if (HasVAListArg) |
| 885 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(OrigFormatExpr)) |
| 886 | if (isa<ParmVarDecl>(DR->getDecl())) |
| 887 | return; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 888 | |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 889 | // If there are no arguments specified, warn with -Wformat-security, otherwise |
| 890 | // warn only with -Wformat-nonliteral. |
| 891 | if (TheCall->getNumArgs() == format_idx+1) |
| 892 | Diag(TheCall->getArg(format_idx)->getLocStart(), |
| 893 | diag::warn_printf_nonliteral_noargs) |
| 894 | << OrigFormatExpr->getSourceRange(); |
| 895 | else |
| 896 | Diag(TheCall->getArg(format_idx)->getLocStart(), |
| 897 | diag::warn_printf_nonliteral) |
| 898 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 899 | } |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 900 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 901 | void Sema::CheckPrintfString(const StringLiteral *FExpr, |
| 902 | const Expr *OrigFormatExpr, |
| 903 | const CallExpr *TheCall, bool HasVAListArg, |
| 904 | unsigned format_idx, unsigned firstDataArg) { |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 905 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 906 | const ObjCStringLiteral *ObjCFExpr = |
| 907 | dyn_cast<ObjCStringLiteral>(OrigFormatExpr); |
| 908 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 909 | // CHECK: is the format string a wide literal? |
| 910 | if (FExpr->isWide()) { |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 911 | Diag(FExpr->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 912 | diag::warn_printf_format_string_is_wide_literal) |
| 913 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 914 | return; |
| 915 | } |
| 916 | |
| 917 | // Str - The format string. NOTE: this is NOT null-terminated! |
Chris Lattner | b9fc856 | 2009-04-29 04:12:34 +0000 | [diff] [blame] | 918 | const char *Str = FExpr->getStrData(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 919 | |
| 920 | // CHECK: empty format string? |
Chris Lattner | b9fc856 | 2009-04-29 04:12:34 +0000 | [diff] [blame] | 921 | unsigned StrLen = FExpr->getByteLength(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 922 | |
| 923 | if (StrLen == 0) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 924 | Diag(FExpr->getLocStart(), diag::warn_printf_empty_format_string) |
| 925 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 926 | return; |
| 927 | } |
| 928 | |
| 929 | // We process the format string using a binary state machine. The |
| 930 | // current state is stored in CurrentState. |
| 931 | enum { |
| 932 | state_OrdChr, |
| 933 | state_Conversion |
| 934 | } CurrentState = state_OrdChr; |
| 935 | |
| 936 | // numConversions - The number of conversions seen so far. This is |
| 937 | // incremented as we traverse the format string. |
| 938 | unsigned numConversions = 0; |
| 939 | |
| 940 | // numDataArgs - The number of data arguments after the format |
| 941 | // string. This can only be determined for non vprintf-like |
| 942 | // functions. For those functions, this value is 1 (the sole |
| 943 | // va_arg argument). |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 944 | unsigned numDataArgs = TheCall->getNumArgs()-firstDataArg; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 945 | |
| 946 | // Inspect the format string. |
| 947 | unsigned StrIdx = 0; |
| 948 | |
| 949 | // LastConversionIdx - Index within the format string where we last saw |
| 950 | // a '%' character that starts a new format conversion. |
| 951 | unsigned LastConversionIdx = 0; |
| 952 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 953 | for (; StrIdx < StrLen; ++StrIdx) { |
Chris Lattner | 998568f | 2007-12-28 05:38:24 +0000 | [diff] [blame] | 954 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 955 | // Is the number of detected conversion conversions greater than |
| 956 | // the number of matching data arguments? If so, stop. |
| 957 | if (!HasVAListArg && numConversions > numDataArgs) break; |
| 958 | |
| 959 | // Handle "\0" |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 960 | if (Str[StrIdx] == '\0') { |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 961 | // The string returned by getStrData() is not null-terminated, |
| 962 | // so the presence of a null character is likely an error. |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 963 | Diag(getLocationOfStringLiteralByte(FExpr, StrIdx), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 964 | diag::warn_printf_format_string_contains_null_char) |
| 965 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 966 | return; |
| 967 | } |
| 968 | |
| 969 | // Ordinary characters (not processing a format conversion). |
| 970 | if (CurrentState == state_OrdChr) { |
| 971 | if (Str[StrIdx] == '%') { |
| 972 | CurrentState = state_Conversion; |
| 973 | LastConversionIdx = StrIdx; |
| 974 | } |
| 975 | continue; |
| 976 | } |
| 977 | |
| 978 | // Seen '%'. Now processing a format conversion. |
| 979 | switch (Str[StrIdx]) { |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 980 | // Handle dynamic precision or width specifier. |
| 981 | case '*': { |
| 982 | ++numConversions; |
| 983 | |
Ted Kremenek | 42ae3e8 | 2009-05-13 16:06:05 +0000 | [diff] [blame] | 984 | if (!HasVAListArg) { |
| 985 | if (numConversions > numDataArgs) { |
| 986 | SourceLocation Loc = getLocationOfStringLiteralByte(FExpr, StrIdx); |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 987 | |
Ted Kremenek | 42ae3e8 | 2009-05-13 16:06:05 +0000 | [diff] [blame] | 988 | if (Str[StrIdx-1] == '.') |
| 989 | Diag(Loc, diag::warn_printf_asterisk_precision_missing_arg) |
| 990 | << OrigFormatExpr->getSourceRange(); |
| 991 | else |
| 992 | Diag(Loc, diag::warn_printf_asterisk_width_missing_arg) |
| 993 | << OrigFormatExpr->getSourceRange(); |
| 994 | |
| 995 | // Don't do any more checking. We'll just emit spurious errors. |
| 996 | return; |
| 997 | } |
| 998 | |
| 999 | // Perform type checking on width/precision specifier. |
| 1000 | const Expr *E = TheCall->getArg(format_idx+numConversions); |
| 1001 | if (const BuiltinType *BT = E->getType()->getAsBuiltinType()) |
| 1002 | if (BT->getKind() == BuiltinType::Int) |
| 1003 | break; |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 1004 | |
Ted Kremenek | 42ae3e8 | 2009-05-13 16:06:05 +0000 | [diff] [blame] | 1005 | SourceLocation Loc = getLocationOfStringLiteralByte(FExpr, StrIdx); |
| 1006 | |
| 1007 | if (Str[StrIdx-1] == '.') |
| 1008 | Diag(Loc, diag::warn_printf_asterisk_precision_wrong_type) |
| 1009 | << E->getType() << E->getSourceRange(); |
| 1010 | else |
| 1011 | Diag(Loc, diag::warn_printf_asterisk_width_wrong_type) |
| 1012 | << E->getType() << E->getSourceRange(); |
| 1013 | |
| 1014 | break; |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 1015 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | // Characters which can terminate a format conversion |
| 1019 | // (e.g. "%d"). Characters that specify length modifiers or |
| 1020 | // other flags are handled by the default case below. |
| 1021 | // |
| 1022 | // FIXME: additional checks will go into the following cases. |
| 1023 | case 'i': |
| 1024 | case 'd': |
| 1025 | case 'o': |
| 1026 | case 'u': |
| 1027 | case 'x': |
| 1028 | case 'X': |
| 1029 | case 'D': |
| 1030 | case 'O': |
| 1031 | case 'U': |
| 1032 | case 'e': |
| 1033 | case 'E': |
| 1034 | case 'f': |
| 1035 | case 'F': |
| 1036 | case 'g': |
| 1037 | case 'G': |
| 1038 | case 'a': |
| 1039 | case 'A': |
| 1040 | case 'c': |
| 1041 | case 'C': |
| 1042 | case 'S': |
| 1043 | case 's': |
| 1044 | case 'p': |
| 1045 | ++numConversions; |
| 1046 | CurrentState = state_OrdChr; |
| 1047 | break; |
| 1048 | |
| 1049 | // CHECK: Are we using "%n"? Issue a warning. |
| 1050 | case 'n': { |
| 1051 | ++numConversions; |
| 1052 | CurrentState = state_OrdChr; |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 1053 | SourceLocation Loc = getLocationOfStringLiteralByte(FExpr, |
| 1054 | LastConversionIdx); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1055 | |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1056 | Diag(Loc, diag::warn_printf_write_back)<<OrigFormatExpr->getSourceRange(); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1057 | break; |
| 1058 | } |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1059 | |
| 1060 | // Handle "%@" |
| 1061 | case '@': |
| 1062 | // %@ is allowed in ObjC format strings only. |
| 1063 | if(ObjCFExpr != NULL) |
| 1064 | CurrentState = state_OrdChr; |
| 1065 | else { |
| 1066 | // Issue a warning: invalid format conversion. |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 1067 | SourceLocation Loc = |
| 1068 | getLocationOfStringLiteralByte(FExpr, LastConversionIdx); |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1069 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 1070 | Diag(Loc, diag::warn_printf_invalid_conversion) |
| 1071 | << std::string(Str+LastConversionIdx, |
| 1072 | Str+std::min(LastConversionIdx+2, StrLen)) |
| 1073 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1074 | } |
| 1075 | ++numConversions; |
| 1076 | break; |
| 1077 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1078 | // Handle "%%" |
| 1079 | case '%': |
| 1080 | // Sanity check: Was the first "%" character the previous one? |
| 1081 | // If not, we will assume that we have a malformed format |
| 1082 | // conversion, and that the current "%" character is the start |
| 1083 | // of a new conversion. |
| 1084 | if (StrIdx - LastConversionIdx == 1) |
| 1085 | CurrentState = state_OrdChr; |
| 1086 | else { |
| 1087 | // Issue a warning: invalid format conversion. |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 1088 | SourceLocation Loc = |
| 1089 | getLocationOfStringLiteralByte(FExpr, LastConversionIdx); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1090 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 1091 | Diag(Loc, diag::warn_printf_invalid_conversion) |
| 1092 | << std::string(Str+LastConversionIdx, Str+StrIdx) |
| 1093 | << OrigFormatExpr->getSourceRange(); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1094 | |
| 1095 | // This conversion is broken. Advance to the next format |
| 1096 | // conversion. |
| 1097 | LastConversionIdx = StrIdx; |
| 1098 | ++numConversions; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1099 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1100 | break; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1101 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1102 | default: |
| 1103 | // This case catches all other characters: flags, widths, etc. |
| 1104 | // We should eventually process those as well. |
| 1105 | break; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | if (CurrentState == state_Conversion) { |
| 1110 | // Issue a warning: invalid format conversion. |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 1111 | SourceLocation Loc = |
| 1112 | getLocationOfStringLiteralByte(FExpr, LastConversionIdx); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1113 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 1114 | Diag(Loc, diag::warn_printf_invalid_conversion) |
| 1115 | << std::string(Str+LastConversionIdx, |
| 1116 | Str+std::min(LastConversionIdx+2, StrLen)) |
| 1117 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1118 | return; |
| 1119 | } |
| 1120 | |
| 1121 | if (!HasVAListArg) { |
| 1122 | // CHECK: Does the number of format conversions exceed the number |
| 1123 | // of data arguments? |
| 1124 | if (numConversions > numDataArgs) { |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 1125 | SourceLocation Loc = |
| 1126 | getLocationOfStringLiteralByte(FExpr, LastConversionIdx); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1127 | |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1128 | Diag(Loc, diag::warn_printf_insufficient_data_args) |
| 1129 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1130 | } |
| 1131 | // CHECK: Does the number of data arguments exceed the number of |
| 1132 | // format conversions in the format string? |
| 1133 | else if (numConversions < numDataArgs) |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1134 | Diag(TheCall->getArg(format_idx+numConversions+1)->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1135 | diag::warn_printf_too_many_data_args) |
| 1136 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1137 | } |
| 1138 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1139 | |
| 1140 | //===--- CHECK: Return Address of Stack Variable --------------------------===// |
| 1141 | |
| 1142 | static DeclRefExpr* EvalVal(Expr *E); |
| 1143 | static DeclRefExpr* EvalAddr(Expr* E); |
| 1144 | |
| 1145 | /// CheckReturnStackAddr - Check if a return statement returns the address |
| 1146 | /// of a stack variable. |
| 1147 | void |
| 1148 | Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, |
| 1149 | SourceLocation ReturnLoc) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1150 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1151 | // Perform checking for returned stack addresses. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1152 | if (lhsType->isPointerType() || lhsType->isBlockPointerType()) { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1153 | if (DeclRefExpr *DR = EvalAddr(RetValExp)) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1154 | Diag(DR->getLocStart(), diag::warn_ret_stack_addr) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1155 | << DR->getDecl()->getDeclName() << RetValExp->getSourceRange(); |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 1156 | |
| 1157 | // Skip over implicit cast expressions when checking for block expressions. |
| 1158 | if (ImplicitCastExpr *IcExpr = |
| 1159 | dyn_cast_or_null<ImplicitCastExpr>(RetValExp)) |
| 1160 | RetValExp = IcExpr->getSubExpr(); |
| 1161 | |
Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 1162 | if (BlockExpr *C = dyn_cast_or_null<BlockExpr>(RetValExp)) |
Mike Stump | 397195b | 2009-04-17 00:09:41 +0000 | [diff] [blame] | 1163 | if (C->hasBlockDeclRefExprs()) |
| 1164 | Diag(C->getLocStart(), diag::err_ret_local_block) |
| 1165 | << C->getSourceRange(); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1166 | } |
| 1167 | // Perform checking for stack values returned by reference. |
| 1168 | else if (lhsType->isReferenceType()) { |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1169 | // Check for a reference to the stack |
| 1170 | if (DeclRefExpr *DR = EvalVal(RetValExp)) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1171 | Diag(DR->getLocStart(), diag::warn_ret_stack_ref) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1172 | << DR->getDecl()->getDeclName() << RetValExp->getSourceRange(); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that |
| 1177 | /// check if the expression in a return statement evaluates to an address |
| 1178 | /// to a location on the stack. The recursion is used to traverse the |
| 1179 | /// AST of the return expression, with recursion backtracking when we |
| 1180 | /// encounter a subexpression that (1) clearly does not lead to the address |
| 1181 | /// of a stack variable or (2) is something we cannot determine leads to |
| 1182 | /// the address of a stack variable based on such local checking. |
| 1183 | /// |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 1184 | /// EvalAddr processes expressions that are pointers that are used as |
| 1185 | /// references (and not L-values). EvalVal handles all other values. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1186 | /// At the base case of the recursion is a check for a DeclRefExpr* in |
| 1187 | /// the refers to a stack variable. |
| 1188 | /// |
| 1189 | /// This implementation handles: |
| 1190 | /// |
| 1191 | /// * pointer-to-pointer casts |
| 1192 | /// * implicit conversions from array references to pointers |
| 1193 | /// * taking the address of fields |
| 1194 | /// * arbitrary interplay between "&" and "*" operators |
| 1195 | /// * pointer arithmetic from an address of a stack variable |
| 1196 | /// * taking the address of an array element where the array is on the stack |
| 1197 | static DeclRefExpr* EvalAddr(Expr *E) { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1198 | // We should only be called for evaluating pointer expressions. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1199 | assert((E->getType()->isPointerType() || |
| 1200 | E->getType()->isBlockPointerType() || |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1201 | E->getType()->isObjCQualifiedIdType()) && |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1202 | "EvalAddr only works on pointers"); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1203 | |
| 1204 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 1205 | // viewed AST node. We then recursively traverse the AST by calling |
| 1206 | // EvalAddr and EvalVal appropriately. |
| 1207 | switch (E->getStmtClass()) { |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1208 | case Stmt::ParenExprClass: |
| 1209 | // Ignore parentheses. |
| 1210 | return EvalAddr(cast<ParenExpr>(E)->getSubExpr()); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1211 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1212 | case Stmt::UnaryOperatorClass: { |
| 1213 | // The only unary operator that make sense to handle here |
| 1214 | // is AddrOf. All others don't make sense as pointers. |
| 1215 | UnaryOperator *U = cast<UnaryOperator>(E); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1216 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1217 | if (U->getOpcode() == UnaryOperator::AddrOf) |
| 1218 | return EvalVal(U->getSubExpr()); |
| 1219 | else |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1220 | return NULL; |
| 1221 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1222 | |
| 1223 | case Stmt::BinaryOperatorClass: { |
| 1224 | // Handle pointer arithmetic. All other binary operators are not valid |
| 1225 | // in this context. |
| 1226 | BinaryOperator *B = cast<BinaryOperator>(E); |
| 1227 | BinaryOperator::Opcode op = B->getOpcode(); |
| 1228 | |
| 1229 | if (op != BinaryOperator::Add && op != BinaryOperator::Sub) |
| 1230 | return NULL; |
| 1231 | |
| 1232 | Expr *Base = B->getLHS(); |
| 1233 | |
| 1234 | // Determine which argument is the real pointer base. It could be |
| 1235 | // the RHS argument instead of the LHS. |
| 1236 | if (!Base->getType()->isPointerType()) Base = B->getRHS(); |
| 1237 | |
| 1238 | assert (Base->getType()->isPointerType()); |
| 1239 | return EvalAddr(Base); |
| 1240 | } |
Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 1241 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1242 | // For conditional operators we need to see if either the LHS or RHS are |
| 1243 | // valid DeclRefExpr*s. If one of them is valid, we return it. |
| 1244 | case Stmt::ConditionalOperatorClass: { |
| 1245 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 1246 | |
| 1247 | // Handle the GNU extension for missing LHS. |
| 1248 | if (Expr *lhsExpr = C->getLHS()) |
| 1249 | if (DeclRefExpr* LHS = EvalAddr(lhsExpr)) |
| 1250 | return LHS; |
| 1251 | |
| 1252 | return EvalAddr(C->getRHS()); |
| 1253 | } |
| 1254 | |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 1255 | // For casts, we need to handle conversions from arrays to |
| 1256 | // pointer values, and pointer-to-pointer conversions. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1257 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1258 | case Stmt::CStyleCastExprClass: |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1259 | case Stmt::CXXFunctionalCastExprClass: { |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1260 | Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 1261 | QualType T = SubExpr->getType(); |
| 1262 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1263 | if (SubExpr->getType()->isPointerType() || |
| 1264 | SubExpr->getType()->isBlockPointerType() || |
| 1265 | SubExpr->getType()->isObjCQualifiedIdType()) |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 1266 | return EvalAddr(SubExpr); |
| 1267 | else if (T->isArrayType()) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1268 | return EvalVal(SubExpr); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1269 | else |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 1270 | return 0; |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | // C++ casts. For dynamic casts, static casts, and const casts, we |
| 1274 | // are always converting from a pointer-to-pointer, so we just blow |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1275 | // through the cast. In the case the dynamic cast doesn't fail (and |
| 1276 | // return NULL), we take the conservative route and report cases |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1277 | // where we return the address of a stack variable. For Reinterpre |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1278 | // FIXME: The comment about is wrong; we're not always converting |
| 1279 | // from pointer to pointer. I'm guessing that this code should also |
| 1280 | // handle references to objects. |
| 1281 | case Stmt::CXXStaticCastExprClass: |
| 1282 | case Stmt::CXXDynamicCastExprClass: |
| 1283 | case Stmt::CXXConstCastExprClass: |
| 1284 | case Stmt::CXXReinterpretCastExprClass: { |
| 1285 | Expr *S = cast<CXXNamedCastExpr>(E)->getSubExpr(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1286 | if (S->getType()->isPointerType() || S->getType()->isBlockPointerType()) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1287 | return EvalAddr(S); |
| 1288 | else |
| 1289 | return NULL; |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | // Everything else: we simply don't reason about them. |
| 1293 | default: |
| 1294 | return NULL; |
| 1295 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | |
| 1299 | /// EvalVal - This function is complements EvalAddr in the mutual recursion. |
| 1300 | /// See the comments for EvalAddr for more details. |
| 1301 | static DeclRefExpr* EvalVal(Expr *E) { |
| 1302 | |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 1303 | // We should only be called for evaluating non-pointer expressions, or |
| 1304 | // expressions with a pointer type that are not used as references but instead |
| 1305 | // are l-values (e.g., DeclRefExpr with a pointer type). |
| 1306 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1307 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 1308 | // viewed AST node. We then recursively traverse the AST by calling |
| 1309 | // EvalAddr and EvalVal appropriately. |
| 1310 | switch (E->getStmtClass()) { |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1311 | case Stmt::DeclRefExprClass: |
| 1312 | case Stmt::QualifiedDeclRefExprClass: { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1313 | // DeclRefExpr: the base case. When we hit a DeclRefExpr we are looking |
| 1314 | // at code that refers to a variable's name. We check if it has local |
| 1315 | // storage within the function, and if so, return the expression. |
| 1316 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
| 1317 | |
| 1318 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1319 | if(V->hasLocalStorage() && !V->getType()->isReferenceType()) return DR; |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1320 | |
| 1321 | return NULL; |
| 1322 | } |
| 1323 | |
| 1324 | case Stmt::ParenExprClass: |
| 1325 | // Ignore parentheses. |
| 1326 | return EvalVal(cast<ParenExpr>(E)->getSubExpr()); |
| 1327 | |
| 1328 | case Stmt::UnaryOperatorClass: { |
| 1329 | // The only unary operator that make sense to handle here |
| 1330 | // is Deref. All others don't resolve to a "name." This includes |
| 1331 | // handling all sorts of rvalues passed to a unary operator. |
| 1332 | UnaryOperator *U = cast<UnaryOperator>(E); |
| 1333 | |
| 1334 | if (U->getOpcode() == UnaryOperator::Deref) |
| 1335 | return EvalAddr(U->getSubExpr()); |
| 1336 | |
| 1337 | return NULL; |
| 1338 | } |
| 1339 | |
| 1340 | case Stmt::ArraySubscriptExprClass: { |
| 1341 | // Array subscripts are potential references to data on the stack. We |
| 1342 | // retrieve the DeclRefExpr* for the array variable if it indeed |
| 1343 | // has local storage. |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 1344 | return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase()); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | case Stmt::ConditionalOperatorClass: { |
| 1348 | // For conditional operators we need to see if either the LHS or RHS are |
| 1349 | // non-NULL DeclRefExpr's. If one is non-NULL, we return it. |
| 1350 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 1351 | |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 1352 | // Handle the GNU extension for missing LHS. |
| 1353 | if (Expr *lhsExpr = C->getLHS()) |
| 1354 | if (DeclRefExpr *LHS = EvalVal(lhsExpr)) |
| 1355 | return LHS; |
| 1356 | |
| 1357 | return EvalVal(C->getRHS()); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | // Accesses to members are potential references to data on the stack. |
| 1361 | case Stmt::MemberExprClass: { |
| 1362 | MemberExpr *M = cast<MemberExpr>(E); |
| 1363 | |
| 1364 | // Check for indirect access. We only want direct field accesses. |
| 1365 | if (!M->isArrow()) |
| 1366 | return EvalVal(M->getBase()); |
| 1367 | else |
| 1368 | return NULL; |
| 1369 | } |
| 1370 | |
| 1371 | // Everything else: we simply don't reason about them. |
| 1372 | default: |
| 1373 | return NULL; |
| 1374 | } |
| 1375 | } |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1376 | |
| 1377 | //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// |
| 1378 | |
| 1379 | /// Check for comparisons of floating point operands using != and ==. |
| 1380 | /// Issue a warning if these are no self-comparisons, as they are not likely |
| 1381 | /// to do what the programmer intended. |
| 1382 | void Sema::CheckFloatComparison(SourceLocation loc, Expr* lex, Expr *rex) { |
| 1383 | bool EmitWarning = true; |
| 1384 | |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 1385 | Expr* LeftExprSansParen = lex->IgnoreParens(); |
Ted Kremenek | 32e97b6 | 2008-01-17 17:55:13 +0000 | [diff] [blame] | 1386 | Expr* RightExprSansParen = rex->IgnoreParens(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1387 | |
| 1388 | // Special case: check for x == x (which is OK). |
| 1389 | // Do not emit warnings for such cases. |
| 1390 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) |
| 1391 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) |
| 1392 | if (DRL->getDecl() == DRR->getDecl()) |
| 1393 | EmitWarning = false; |
| 1394 | |
Ted Kremenek | 1b500bb | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 1395 | |
| 1396 | // Special case: check for comparisons against literals that can be exactly |
| 1397 | // represented by APFloat. In such cases, do not emit a warning. This |
| 1398 | // is a heuristic: often comparison against such literals are used to |
| 1399 | // detect if a value in a variable has not changed. This clearly can |
| 1400 | // lead to false negatives. |
| 1401 | if (EmitWarning) { |
| 1402 | if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { |
| 1403 | if (FLL->isExact()) |
| 1404 | EmitWarning = false; |
| 1405 | } |
| 1406 | else |
| 1407 | if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)){ |
| 1408 | if (FLR->isExact()) |
| 1409 | EmitWarning = false; |
| 1410 | } |
| 1411 | } |
| 1412 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1413 | // Check for comparisons with builtin types. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1414 | if (EmitWarning) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1415 | if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1416 | if (CL->isBuiltinCall(Context)) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1417 | EmitWarning = false; |
| 1418 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1419 | if (EmitWarning) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1420 | if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1421 | if (CR->isBuiltinCall(Context)) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1422 | EmitWarning = false; |
| 1423 | |
| 1424 | // Emit the diagnostic. |
| 1425 | if (EmitWarning) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1426 | Diag(loc, diag::warn_floatingpoint_eq) |
| 1427 | << lex->getSourceRange() << rex->getSourceRange(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1428 | } |