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