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