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 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10 | // This file implements extra semantic analysis beyond what is enforced |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 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"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 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); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 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; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 55 | // Create a langops struct and enable trigraphs. This is sufficient for |
| 56 | // relexing tokens. |
| 57 | LangOptions LangOpts; |
| 58 | LangOpts.Trigraphs = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 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); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 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(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08: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())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | unsigned Offset = |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 74 | StringLiteralParser::getOffsetOfStringByte(TheTok, ByteNo, PP); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 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 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 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; |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 94 | // Does the index refer to the implicit object argument? |
| 95 | if (isa<CXXMemberCallExpr>(TheCall)) { |
| 96 | if (format_idx == 0) |
| 97 | return false; |
| 98 | --format_idx; |
| 99 | } |
Ryan Flynn | 4403a5e | 2009-08-06 03:00:50 +0000 | [diff] [blame] | 100 | if (format_idx < TheCall->getNumArgs()) { |
| 101 | Expr *Format = TheCall->getArg(format_idx)->IgnoreParenCasts(); |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 102 | if (!Format->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
Ryan Flynn | 4403a5e | 2009-08-06 03:00:50 +0000 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | } |
| 106 | return false; |
| 107 | } |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 108 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 109 | Action::OwningExprResult |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 110 | Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 111 | OwningExprResult TheCallResult(Owned(TheCall)); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 112 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 113 | switch (BuiltinID) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 114 | case Builtin::BI__builtin___CFStringMakeConstantString: |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 115 | assert(TheCall->getNumArgs() == 1 && |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 116 | "Wrong # arguments to builtin CFStringMakeConstantString"); |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 117 | if (CheckObjCString(TheCall->getArg(0))) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 118 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 119 | break; |
Ted Kremenek | 49ff7a1 | 2008-07-09 17:58:53 +0000 | [diff] [blame] | 120 | case Builtin::BI__builtin_stdarg_start: |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 121 | case Builtin::BI__builtin_va_start: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 122 | if (SemaBuiltinVAStart(TheCall)) |
| 123 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 124 | break; |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 125 | case Builtin::BI__builtin_isgreater: |
| 126 | case Builtin::BI__builtin_isgreaterequal: |
| 127 | case Builtin::BI__builtin_isless: |
| 128 | case Builtin::BI__builtin_islessequal: |
| 129 | case Builtin::BI__builtin_islessgreater: |
| 130 | case Builtin::BI__builtin_isunordered: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 131 | if (SemaBuiltinUnorderedCompare(TheCall)) |
| 132 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 133 | break; |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 134 | case Builtin::BI__builtin_isfinite: |
| 135 | case Builtin::BI__builtin_isinf: |
| 136 | case Builtin::BI__builtin_isinf_sign: |
| 137 | case Builtin::BI__builtin_isnan: |
| 138 | case Builtin::BI__builtin_isnormal: |
| 139 | if (SemaBuiltinUnaryFP(TheCall)) |
| 140 | return ExprError(); |
| 141 | break; |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 142 | case Builtin::BI__builtin_return_address: |
| 143 | case Builtin::BI__builtin_frame_address: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 144 | if (SemaBuiltinStackAddress(TheCall)) |
| 145 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 146 | break; |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 147 | case Builtin::BI__builtin_eh_return_data_regno: |
| 148 | if (SemaBuiltinEHReturnDataRegNo(TheCall)) |
| 149 | return ExprError(); |
| 150 | break; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 151 | case Builtin::BI__builtin_shufflevector: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 152 | return SemaBuiltinShuffleVector(TheCall); |
| 153 | // TheCall will be freed by the smart pointer here, but that's fine, since |
| 154 | // SemaBuiltinShuffleVector guts it, but then doesn't release it. |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 155 | case Builtin::BI__builtin_prefetch: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 156 | if (SemaBuiltinPrefetch(TheCall)) |
| 157 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 158 | break; |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 159 | case Builtin::BI__builtin_object_size: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 160 | if (SemaBuiltinObjectSize(TheCall)) |
| 161 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 162 | break; |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 163 | case Builtin::BI__builtin_longjmp: |
| 164 | if (SemaBuiltinLongjmp(TheCall)) |
| 165 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 166 | break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 167 | case Builtin::BI__sync_fetch_and_add: |
| 168 | case Builtin::BI__sync_fetch_and_sub: |
| 169 | case Builtin::BI__sync_fetch_and_or: |
| 170 | case Builtin::BI__sync_fetch_and_and: |
| 171 | case Builtin::BI__sync_fetch_and_xor: |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 172 | case Builtin::BI__sync_fetch_and_nand: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 173 | case Builtin::BI__sync_add_and_fetch: |
| 174 | case Builtin::BI__sync_sub_and_fetch: |
| 175 | case Builtin::BI__sync_and_and_fetch: |
| 176 | case Builtin::BI__sync_or_and_fetch: |
| 177 | case Builtin::BI__sync_xor_and_fetch: |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 178 | case Builtin::BI__sync_nand_and_fetch: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 179 | case Builtin::BI__sync_val_compare_and_swap: |
| 180 | case Builtin::BI__sync_bool_compare_and_swap: |
| 181 | case Builtin::BI__sync_lock_test_and_set: |
| 182 | case Builtin::BI__sync_lock_release: |
| 183 | if (SemaBuiltinAtomicOverloaded(TheCall)) |
| 184 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 185 | break; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 186 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 188 | return move(TheCallResult); |
| 189 | } |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 190 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 191 | /// CheckFunctionCall - Check a direct function call for various correctness |
| 192 | /// and safety properties not strictly enforced by the C type system. |
| 193 | bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall) { |
| 194 | // Get the IdentifierInfo* for the called function. |
| 195 | IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
| 196 | |
| 197 | // None of the checks below are needed for functions that don't have |
| 198 | // simple names (e.g., C++ conversion functions). |
| 199 | if (!FnInfo) |
| 200 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 202 | // FIXME: This mechanism should be abstracted to be less fragile and |
| 203 | // more efficient. For example, just map function ids to custom |
| 204 | // handlers. |
| 205 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 206 | // Printf checking. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 207 | if (const FormatAttr *Format = FDecl->getAttr<FormatAttr>()) { |
Ryan Flynn | 4403a5e | 2009-08-06 03:00:50 +0000 | [diff] [blame] | 208 | if (CheckablePrintfAttr(Format, TheCall)) { |
Ted Kremenek | 3d692df | 2009-02-27 17:58:43 +0000 | [diff] [blame] | 209 | bool HasVAListArg = Format->getFirstArg() == 0; |
| 210 | if (!HasVAListArg) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | if (const FunctionProtoType *Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 212 | = FDecl->getType()->getAs<FunctionProtoType>()) |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 213 | HasVAListArg = !Proto->isVariadic(); |
Ted Kremenek | 3d692df | 2009-02-27 17:58:43 +0000 | [diff] [blame] | 214 | } |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 215 | CheckPrintfArguments(TheCall, HasVAListArg, Format->getFormatIdx() - 1, |
Ted Kremenek | 3d692df | 2009-02-27 17:58:43 +0000 | [diff] [blame] | 216 | HasVAListArg ? 0 : Format->getFirstArg() - 1); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 217 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 218 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | |
| 220 | for (const NonNullAttr *NonNull = FDecl->getAttr<NonNullAttr>(); NonNull; |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 221 | NonNull = NonNull->getNext<NonNullAttr>()) |
| 222 | CheckNonNullArguments(NonNull, TheCall); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 223 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 224 | return false; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 227 | bool Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall) { |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 228 | // Printf checking. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 229 | const FormatAttr *Format = NDecl->getAttr<FormatAttr>(); |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 230 | if (!Format) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 231 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 233 | const VarDecl *V = dyn_cast<VarDecl>(NDecl); |
| 234 | if (!V) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 235 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 237 | QualType Ty = V->getType(); |
| 238 | if (!Ty->isBlockPointerType()) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 239 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 241 | if (!CheckablePrintfAttr(Format, TheCall)) |
| 242 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 244 | bool HasVAListArg = Format->getFirstArg() == 0; |
| 245 | if (!HasVAListArg) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | const FunctionType *FT = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 247 | Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 248 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) |
| 249 | HasVAListArg = !Proto->isVariadic(); |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 250 | } |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 251 | CheckPrintfArguments(TheCall, HasVAListArg, Format->getFormatIdx() - 1, |
| 252 | HasVAListArg ? 0 : Format->getFirstArg() - 1); |
| 253 | |
| 254 | return false; |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 257 | /// SemaBuiltinAtomicOverloaded - We have a call to a function like |
| 258 | /// __sync_fetch_and_add, which is an overloaded function based on the pointer |
| 259 | /// type of its first argument. The main ActOnCallExpr routines have already |
| 260 | /// promoted the types of arguments because all of these calls are prototyped as |
| 261 | /// void(...). |
| 262 | /// |
| 263 | /// This function goes through and does final semantic checking for these |
| 264 | /// builtins, |
| 265 | bool Sema::SemaBuiltinAtomicOverloaded(CallExpr *TheCall) { |
| 266 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 267 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 268 | |
| 269 | // Ensure that we have at least one argument to do type inference from. |
| 270 | if (TheCall->getNumArgs() < 1) |
| 271 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 272 | << 0 << TheCall->getCallee()->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 274 | // Inspect the first argument of the atomic builtin. This should always be |
| 275 | // a pointer type, whose element is an integral scalar or pointer type. |
| 276 | // Because it is a pointer type, we don't have to worry about any implicit |
| 277 | // casts here. |
| 278 | Expr *FirstArg = TheCall->getArg(0); |
| 279 | if (!FirstArg->getType()->isPointerType()) |
| 280 | return Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
| 281 | << FirstArg->getType() << FirstArg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 283 | QualType ValType = FirstArg->getType()->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | if (!ValType->isIntegerType() && !ValType->isPointerType() && |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 285 | !ValType->isBlockPointerType()) |
| 286 | return Diag(DRE->getLocStart(), |
| 287 | diag::err_atomic_builtin_must_be_pointer_intptr) |
| 288 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 289 | |
| 290 | // We need to figure out which concrete builtin this maps onto. For example, |
| 291 | // __sync_fetch_and_add with a 2 byte object turns into |
| 292 | // __sync_fetch_and_add_2. |
| 293 | #define BUILTIN_ROW(x) \ |
| 294 | { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ |
| 295 | Builtin::BI##x##_8, Builtin::BI##x##_16 } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 297 | static const unsigned BuiltinIndices[][5] = { |
| 298 | BUILTIN_ROW(__sync_fetch_and_add), |
| 299 | BUILTIN_ROW(__sync_fetch_and_sub), |
| 300 | BUILTIN_ROW(__sync_fetch_and_or), |
| 301 | BUILTIN_ROW(__sync_fetch_and_and), |
| 302 | BUILTIN_ROW(__sync_fetch_and_xor), |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 303 | BUILTIN_ROW(__sync_fetch_and_nand), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 305 | BUILTIN_ROW(__sync_add_and_fetch), |
| 306 | BUILTIN_ROW(__sync_sub_and_fetch), |
| 307 | BUILTIN_ROW(__sync_and_and_fetch), |
| 308 | BUILTIN_ROW(__sync_or_and_fetch), |
| 309 | BUILTIN_ROW(__sync_xor_and_fetch), |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 310 | BUILTIN_ROW(__sync_nand_and_fetch), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 312 | BUILTIN_ROW(__sync_val_compare_and_swap), |
| 313 | BUILTIN_ROW(__sync_bool_compare_and_swap), |
| 314 | BUILTIN_ROW(__sync_lock_test_and_set), |
| 315 | BUILTIN_ROW(__sync_lock_release) |
| 316 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | #undef BUILTIN_ROW |
| 318 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 319 | // Determine the index of the size. |
| 320 | unsigned SizeIndex; |
| 321 | switch (Context.getTypeSize(ValType)/8) { |
| 322 | case 1: SizeIndex = 0; break; |
| 323 | case 2: SizeIndex = 1; break; |
| 324 | case 4: SizeIndex = 2; break; |
| 325 | case 8: SizeIndex = 3; break; |
| 326 | case 16: SizeIndex = 4; break; |
| 327 | default: |
| 328 | return Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size) |
| 329 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 330 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 332 | // Each of these builtins has one pointer argument, followed by some number of |
| 333 | // values (0, 1 or 2) followed by a potentially empty varags list of stuff |
| 334 | // that we ignore. Find out which row of BuiltinIndices to read from as well |
| 335 | // as the number of fixed args. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 336 | unsigned BuiltinID = FDecl->getBuiltinID(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 337 | unsigned BuiltinIndex, NumFixed = 1; |
| 338 | switch (BuiltinID) { |
| 339 | default: assert(0 && "Unknown overloaded atomic builtin!"); |
| 340 | case Builtin::BI__sync_fetch_and_add: BuiltinIndex = 0; break; |
| 341 | case Builtin::BI__sync_fetch_and_sub: BuiltinIndex = 1; break; |
| 342 | case Builtin::BI__sync_fetch_and_or: BuiltinIndex = 2; break; |
| 343 | case Builtin::BI__sync_fetch_and_and: BuiltinIndex = 3; break; |
| 344 | case Builtin::BI__sync_fetch_and_xor: BuiltinIndex = 4; break; |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 345 | case Builtin::BI__sync_fetch_and_nand:BuiltinIndex = 5; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 347 | case Builtin::BI__sync_add_and_fetch: BuiltinIndex = 6; break; |
| 348 | case Builtin::BI__sync_sub_and_fetch: BuiltinIndex = 7; break; |
| 349 | case Builtin::BI__sync_and_and_fetch: BuiltinIndex = 8; break; |
| 350 | case Builtin::BI__sync_or_and_fetch: BuiltinIndex = 9; break; |
| 351 | case Builtin::BI__sync_xor_and_fetch: BuiltinIndex =10; break; |
| 352 | case Builtin::BI__sync_nand_and_fetch:BuiltinIndex =11; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 354 | case Builtin::BI__sync_val_compare_and_swap: |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 355 | BuiltinIndex = 12; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 356 | NumFixed = 2; |
| 357 | break; |
| 358 | case Builtin::BI__sync_bool_compare_and_swap: |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 359 | BuiltinIndex = 13; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 360 | NumFixed = 2; |
| 361 | break; |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 362 | case Builtin::BI__sync_lock_test_and_set: BuiltinIndex = 14; break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 363 | case Builtin::BI__sync_lock_release: |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 364 | BuiltinIndex = 15; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 365 | NumFixed = 0; |
| 366 | break; |
| 367 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 369 | // Now that we know how many fixed arguments we expect, first check that we |
| 370 | // have at least that many. |
| 371 | if (TheCall->getNumArgs() < 1+NumFixed) |
| 372 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 373 | << 0 << TheCall->getCallee()->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
| 375 | |
Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 376 | // Get the decl for the concrete builtin from this, we can tell what the |
| 377 | // concrete integer type we should convert to is. |
| 378 | unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; |
| 379 | const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID); |
| 380 | IdentifierInfo *NewBuiltinII = PP.getIdentifierInfo(NewBuiltinName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 381 | FunctionDecl *NewBuiltinDecl = |
Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 382 | cast<FunctionDecl>(LazilyCreateBuiltin(NewBuiltinII, NewBuiltinID, |
| 383 | TUScope, false, DRE->getLocStart())); |
| 384 | const FunctionProtoType *BuiltinFT = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 385 | NewBuiltinDecl->getType()->getAs<FunctionProtoType>(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 386 | ValType = BuiltinFT->getArgType(0)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | |
Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 388 | // If the first type needs to be converted (e.g. void** -> int*), do it now. |
| 389 | if (BuiltinFT->getArgType(0) != FirstArg->getType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 390 | ImpCastExprToType(FirstArg, BuiltinFT->getArgType(0), CastExpr::CK_BitCast); |
Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 391 | TheCall->setArg(0, FirstArg); |
| 392 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 394 | // Next, walk the valid ones promoting to the right type. |
| 395 | for (unsigned i = 0; i != NumFixed; ++i) { |
| 396 | Expr *Arg = TheCall->getArg(i+1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 398 | // If the argument is an implicit cast, then there was a promotion due to |
| 399 | // "...", just remove it now. |
| 400 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) { |
| 401 | Arg = ICE->getSubExpr(); |
| 402 | ICE->setSubExpr(0); |
| 403 | ICE->Destroy(Context); |
| 404 | TheCall->setArg(i+1, Arg); |
| 405 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 407 | // GCC does an implicit conversion to the pointer or integer ValType. This |
| 408 | // 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] | 409 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; |
Fariborz Jahanian | e9f4208 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 410 | CXXMethodDecl *ConversionDecl = 0; |
| 411 | if (CheckCastTypes(Arg->getSourceRange(), ValType, Arg, Kind, |
| 412 | ConversionDecl)) |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 413 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 414 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 415 | // Okay, we have something that *can* be converted to the right type. Check |
| 416 | // to see if there is a potentially weird extension going on here. This can |
| 417 | // happen when you do an atomic operation on something like an char* and |
| 418 | // pass in 42. The 42 gets converted to char. This is even more strange |
| 419 | // for things like 45.123 -> char, etc. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 420 | // FIXME: Do this check. |
Anders Carlsson | cdb6197 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 421 | ImpCastExprToType(Arg, ValType, Kind, /*isLvalue=*/false); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 422 | TheCall->setArg(i+1, Arg); |
| 423 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 425 | // Switch the DeclRefExpr to refer to the new decl. |
| 426 | DRE->setDecl(NewBuiltinDecl); |
| 427 | DRE->setType(NewBuiltinDecl->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 429 | // Set the callee in the CallExpr. |
| 430 | // FIXME: This leaks the original parens and implicit casts. |
| 431 | Expr *PromotedCall = DRE; |
| 432 | UsualUnaryConversions(PromotedCall); |
| 433 | TheCall->setCallee(PromotedCall); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 435 | |
| 436 | // Change the result type of the call to match the result type of the decl. |
| 437 | TheCall->setType(NewBuiltinDecl->getResultType()); |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 442 | /// CheckObjCString - Checks that the argument to the builtin |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 443 | /// CFString constructor is correct |
Steve Naroff | fd94262 | 2009-04-13 20:26:29 +0000 | [diff] [blame] | 444 | /// FIXME: GCC currently emits the following warning: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | /// "warning: input conversion stopped due to an input byte that does not |
Steve Naroff | fd94262 | 2009-04-13 20:26:29 +0000 | [diff] [blame] | 446 | /// belong to the input codeset UTF-8" |
| 447 | /// Note: It might also make sense to do the UTF-16 conversion here (would |
| 448 | /// simplify the backend). |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 449 | bool Sema::CheckObjCString(Expr *Arg) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 450 | Arg = Arg->IgnoreParenCasts(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 451 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); |
| 452 | |
| 453 | if (!Literal || Literal->isWide()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 454 | Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 455 | << Arg->getSourceRange(); |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 456 | return true; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 457 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 458 | |
Daniel Dunbar | f015b03 | 2009-09-22 10:03:52 +0000 | [diff] [blame] | 459 | const char *Data = Literal->getStrData(); |
| 460 | unsigned Length = Literal->getByteLength(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | |
Daniel Dunbar | f015b03 | 2009-09-22 10:03:52 +0000 | [diff] [blame] | 462 | for (unsigned i = 0; i < Length; ++i) { |
| 463 | if (!Data[i]) { |
| 464 | Diag(getLocationOfStringLiteralByte(Literal, i), |
| 465 | diag::warn_cfstring_literal_contains_nul_character) |
| 466 | << Arg->getSourceRange(); |
| 467 | break; |
| 468 | } |
| 469 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 470 | |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 471 | return false; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 474 | /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity. |
| 475 | /// Emit an error and return true on failure, return false on success. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 476 | bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) { |
| 477 | Expr *Fn = TheCall->getCallee(); |
| 478 | if (TheCall->getNumArgs() > 2) { |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 479 | Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 480 | diag::err_typecheck_call_too_many_args) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 481 | << 0 /*function call*/ << Fn->getSourceRange() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 483 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 484 | return true; |
| 485 | } |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 486 | |
| 487 | if (TheCall->getNumArgs() < 2) { |
| 488 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 489 | << 0 /*function call*/; |
| 490 | } |
| 491 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 492 | // Determine whether the current function is variadic or not. |
| 493 | bool isVariadic; |
Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 494 | if (CurBlock) |
| 495 | isVariadic = CurBlock->isVariadic; |
| 496 | else if (getCurFunctionDecl()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 497 | if (FunctionProtoType* FTP = |
| 498 | dyn_cast<FunctionProtoType>(getCurFunctionDecl()->getType())) |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 499 | isVariadic = FTP->isVariadic(); |
| 500 | else |
| 501 | isVariadic = false; |
| 502 | } else { |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 503 | isVariadic = getCurMethodDecl()->isVariadic(); |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 504 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 505 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 506 | if (!isVariadic) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 507 | Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function); |
| 508 | return true; |
| 509 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 511 | // Verify that the second argument to the builtin is the last argument of the |
| 512 | // current function or method. |
| 513 | bool SecondArgIsLastNamedArgument = false; |
Anders Carlsson | e2c1410 | 2008-02-13 01:22:59 +0000 | [diff] [blame] | 514 | const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 515 | |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 516 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { |
| 517 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 518 | // FIXME: This isn't correct for methods (results in bogus warning). |
| 519 | // Get the last formal in the current function. |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 520 | const ParmVarDecl *LastArg; |
Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 521 | if (CurBlock) |
| 522 | LastArg = *(CurBlock->TheDecl->param_end()-1); |
| 523 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 524 | LastArg = *(FD->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 525 | else |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 526 | LastArg = *(getCurMethodDecl()->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 527 | SecondArgIsLastNamedArgument = PV == LastArg; |
| 528 | } |
| 529 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 530 | |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 531 | if (!SecondArgIsLastNamedArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 532 | Diag(TheCall->getArg(1)->getLocStart(), |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 533 | diag::warn_second_parameter_of_va_start_not_last_named_argument); |
| 534 | return false; |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 535 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 536 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 537 | /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and |
| 538 | /// friends. This is declared to take (...), so we have to check everything. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 539 | bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { |
| 540 | if (TheCall->getNumArgs() < 2) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 541 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 542 | << 0 /*function call*/; |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 543 | if (TheCall->getNumArgs() > 2) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | return Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 545 | diag::err_typecheck_call_too_many_args) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 546 | << 0 /*function call*/ |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 547 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
| 548 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 550 | Expr *OrigArg0 = TheCall->getArg(0); |
| 551 | Expr *OrigArg1 = TheCall->getArg(1); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 552 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 553 | // Do standard promotions between the two arguments, returning their common |
| 554 | // type. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 555 | QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); |
Daniel Dunbar | 403bc2b | 2009-02-19 19:28:43 +0000 | [diff] [blame] | 556 | |
| 557 | // Make sure any conversions are pushed back into the call; this is |
| 558 | // type safe since unordered compare builtins are declared as "_Bool |
| 559 | // foo(...)". |
| 560 | TheCall->setArg(0, OrigArg0); |
| 561 | TheCall->setArg(1, OrigArg1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 563 | if (OrigArg0->isTypeDependent() || OrigArg1->isTypeDependent()) |
| 564 | return false; |
| 565 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 566 | // If the common type isn't a real floating type, then the arguments were |
| 567 | // invalid for this operation. |
| 568 | if (!Res->isRealFloatingType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | return Diag(OrigArg0->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 570 | diag::err_typecheck_call_invalid_ordered_compare) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 571 | << OrigArg0->getType() << OrigArg1->getType() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 572 | << SourceRange(OrigArg0->getLocStart(), OrigArg1->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 573 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 574 | return false; |
| 575 | } |
| 576 | |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 577 | /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isnan and |
| 578 | /// friends. This is declared to take (...), so we have to check everything. |
| 579 | bool Sema::SemaBuiltinUnaryFP(CallExpr *TheCall) { |
| 580 | if (TheCall->getNumArgs() < 1) |
| 581 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 582 | << 0 /*function call*/; |
| 583 | if (TheCall->getNumArgs() > 1) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | return Diag(TheCall->getArg(1)->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 585 | diag::err_typecheck_call_too_many_args) |
| 586 | << 0 /*function call*/ |
| 587 | << SourceRange(TheCall->getArg(1)->getLocStart(), |
| 588 | (*(TheCall->arg_end()-1))->getLocEnd()); |
| 589 | |
| 590 | Expr *OrigArg = TheCall->getArg(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 591 | |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 592 | if (OrigArg->isTypeDependent()) |
| 593 | return false; |
| 594 | |
| 595 | // This operation requires a floating-point number |
| 596 | if (!OrigArg->getType()->isRealFloatingType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 597 | return Diag(OrigArg->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 598 | diag::err_typecheck_call_invalid_unary_fp) |
| 599 | << OrigArg->getType() << OrigArg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 600 | |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 601 | return false; |
| 602 | } |
| 603 | |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 604 | bool Sema::SemaBuiltinStackAddress(CallExpr *TheCall) { |
| 605 | // The signature for these builtins is exact; the only thing we need |
| 606 | // to check is that the argument is a constant. |
| 607 | SourceLocation Loc; |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 608 | if (!TheCall->getArg(0)->isTypeDependent() && |
| 609 | !TheCall->getArg(0)->isValueDependent() && |
| 610 | !TheCall->getArg(0)->isIntegerConstantExpr(Context, &Loc)) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 611 | return Diag(Loc, diag::err_stack_const_level) << TheCall->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 612 | |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 613 | return false; |
| 614 | } |
| 615 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 616 | /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. |
| 617 | // This is declared to take (...), so we have to check everything. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 618 | Action::OwningExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 619 | if (TheCall->getNumArgs() < 3) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 620 | return ExprError(Diag(TheCall->getLocEnd(), |
| 621 | diag::err_typecheck_call_too_few_args) |
| 622 | << 0 /*function call*/ << TheCall->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 623 | |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 624 | unsigned numElements = std::numeric_limits<unsigned>::max(); |
| 625 | if (!TheCall->getArg(0)->isTypeDependent() && |
| 626 | !TheCall->getArg(1)->isTypeDependent()) { |
| 627 | QualType FAType = TheCall->getArg(0)->getType(); |
| 628 | QualType SAType = TheCall->getArg(1)->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 629 | |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 630 | if (!FAType->isVectorType() || !SAType->isVectorType()) { |
| 631 | Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 633 | TheCall->getArg(1)->getLocEnd()); |
| 634 | return ExprError(); |
| 635 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 636 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 637 | if (!Context.hasSameUnqualifiedType(FAType, SAType)) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 638 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 640 | TheCall->getArg(1)->getLocEnd()); |
| 641 | return ExprError(); |
| 642 | } |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 643 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 644 | numElements = FAType->getAs<VectorType>()->getNumElements(); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 645 | if (TheCall->getNumArgs() != numElements+2) { |
| 646 | if (TheCall->getNumArgs() < numElements+2) |
| 647 | return ExprError(Diag(TheCall->getLocEnd(), |
| 648 | diag::err_typecheck_call_too_few_args) |
| 649 | << 0 /*function call*/ << TheCall->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 650 | return ExprError(Diag(TheCall->getLocEnd(), |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 651 | diag::err_typecheck_call_too_many_args) |
| 652 | << 0 /*function call*/ << TheCall->getSourceRange()); |
| 653 | } |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 657 | if (TheCall->getArg(i)->isTypeDependent() || |
| 658 | TheCall->getArg(i)->isValueDependent()) |
| 659 | continue; |
| 660 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 661 | llvm::APSInt Result(32); |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 662 | if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 663 | return ExprError(Diag(TheCall->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 664 | diag::err_shufflevector_nonconstant_argument) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 665 | << TheCall->getArg(i)->getSourceRange()); |
| 666 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 667 | if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 668 | return ExprError(Diag(TheCall->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 669 | diag::err_shufflevector_argument_too_large) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 670 | << TheCall->getArg(i)->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | llvm::SmallVector<Expr*, 32> exprs; |
| 674 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 675 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 676 | exprs.push_back(TheCall->getArg(i)); |
| 677 | TheCall->setArg(i, 0); |
| 678 | } |
| 679 | |
Nate Begeman | a88dc30 | 2009-08-12 02:10:25 +0000 | [diff] [blame] | 680 | return Owned(new (Context) ShuffleVectorExpr(Context, exprs.begin(), |
| 681 | exprs.size(), exprs[0]->getType(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 682 | TheCall->getCallee()->getLocStart(), |
| 683 | TheCall->getRParenLoc())); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 684 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 685 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 686 | /// SemaBuiltinPrefetch - Handle __builtin_prefetch. |
| 687 | // This is declared to take (const void*, ...) and can take two |
| 688 | // optional constant int args. |
| 689 | bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 690 | unsigned NumArgs = TheCall->getNumArgs(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 691 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 692 | if (NumArgs > 3) |
| 693 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_many_args) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 694 | << 0 /*function call*/ << TheCall->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 695 | |
| 696 | // Argument 0 is checked for us and the remaining arguments must be |
| 697 | // constant integers. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 698 | for (unsigned i = 1; i != NumArgs; ++i) { |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 699 | Expr *Arg = TheCall->getArg(i); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 700 | if (Arg->isTypeDependent()) |
| 701 | continue; |
| 702 | |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 703 | if (!Arg->getType()->isIntegralType()) |
| 704 | return Diag(TheCall->getLocStart(), diag::err_prefetch_invalid_arg_type) |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 705 | << Arg->getSourceRange(); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 706 | |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 707 | ImpCastExprToType(Arg, Context.IntTy, CastExpr::CK_IntegralCast); |
| 708 | TheCall->setArg(i, Arg); |
| 709 | |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 710 | if (Arg->isValueDependent()) |
| 711 | continue; |
| 712 | |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 713 | llvm::APSInt Result; |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 714 | if (!Arg->isIntegerConstantExpr(Result, Context)) |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 715 | return Diag(TheCall->getLocStart(), diag::err_prefetch_invalid_arg_ice) |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 716 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 718 | // FIXME: gcc issues a warning and rewrites these to 0. These |
| 719 | // seems especially odd for the third argument since the default |
| 720 | // is 3. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 721 | if (i == 1) { |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 722 | if (Result.getLimitedValue() > 1) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 723 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 724 | << "0" << "1" << Arg->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 725 | } else { |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 726 | if (Result.getLimitedValue() > 3) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 727 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 728 | << "0" << "3" << Arg->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 732 | return false; |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 735 | /// SemaBuiltinEHReturnDataRegNo - Handle __builtin_eh_return_data_regno, the |
| 736 | /// operand must be an integer constant. |
| 737 | bool Sema::SemaBuiltinEHReturnDataRegNo(CallExpr *TheCall) { |
| 738 | llvm::APSInt Result; |
| 739 | if (!TheCall->getArg(0)->isIntegerConstantExpr(Result, Context)) |
| 740 | return Diag(TheCall->getLocStart(), diag::err_expr_not_ice) |
| 741 | << TheCall->getArg(0)->getSourceRange(); |
| 742 | |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 747 | /// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr, |
| 748 | /// int type). This simply type checks that type is one of the defined |
| 749 | /// constants (0-3). |
Eric Christopher | fee667f | 2009-12-23 03:49:37 +0000 | [diff] [blame] | 750 | // For compatability check 0-3, llvm only handles 0 and 2. |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 751 | bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) { |
| 752 | Expr *Arg = TheCall->getArg(1); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 753 | if (Arg->isTypeDependent()) |
| 754 | return false; |
| 755 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 756 | QualType ArgType = Arg->getType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 757 | const BuiltinType *BT = ArgType->getAs<BuiltinType>(); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 758 | llvm::APSInt Result(32); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 759 | if (!BT || BT->getKind() != BuiltinType::Int) |
| 760 | return Diag(TheCall->getLocStart(), diag::err_object_size_invalid_argument) |
| 761 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
| 762 | |
| 763 | if (Arg->isValueDependent()) |
| 764 | return false; |
| 765 | |
| 766 | if (!Arg->isIntegerConstantExpr(Result, Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 767 | return Diag(TheCall->getLocStart(), diag::err_object_size_invalid_argument) |
| 768 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 772 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 773 | << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | return false; |
| 777 | } |
| 778 | |
Eli Friedman | 586d6a8 | 2009-05-03 06:04:26 +0000 | [diff] [blame] | 779 | /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 780 | /// This checks that val is a constant 1. |
| 781 | bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { |
| 782 | Expr *Arg = TheCall->getArg(1); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 783 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 784 | return false; |
| 785 | |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 786 | llvm::APSInt Result(32); |
| 787 | if (!Arg->isIntegerConstantExpr(Result, Context) || Result != 1) |
| 788 | return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val) |
| 789 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
| 790 | |
| 791 | return false; |
| 792 | } |
| 793 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 794 | // Handle i > 1 ? "x" : "y", recursivelly |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 795 | bool Sema::SemaCheckStringLiteral(const Expr *E, const CallExpr *TheCall, |
| 796 | bool HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 797 | unsigned format_idx, unsigned firstDataArg) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 798 | if (E->isTypeDependent() || E->isValueDependent()) |
| 799 | return false; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 800 | |
| 801 | switch (E->getStmtClass()) { |
| 802 | case Stmt::ConditionalOperatorClass: { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 803 | const ConditionalOperator *C = cast<ConditionalOperator>(E); |
Chris Lattner | 813b70d | 2009-12-22 06:00:13 +0000 | [diff] [blame] | 804 | return SemaCheckStringLiteral(C->getTrueExpr(), TheCall, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 805 | HasVAListArg, format_idx, firstDataArg) |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 806 | && SemaCheckStringLiteral(C->getRHS(), TheCall, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 807 | HasVAListArg, format_idx, firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | case Stmt::ImplicitCastExprClass: { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 811 | const ImplicitCastExpr *Expr = cast<ImplicitCastExpr>(E); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 812 | return SemaCheckStringLiteral(Expr->getSubExpr(), TheCall, HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 813 | format_idx, firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | case Stmt::ParenExprClass: { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 817 | const ParenExpr *Expr = cast<ParenExpr>(E); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 818 | return SemaCheckStringLiteral(Expr->getSubExpr(), TheCall, HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 819 | format_idx, firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 820 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 821 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 822 | case Stmt::DeclRefExprClass: { |
| 823 | const DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 824 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 825 | // As an exception, do not flag errors for variables binding to |
| 826 | // const string literals. |
| 827 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 828 | bool isConstant = false; |
| 829 | QualType T = DR->getType(); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 830 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 831 | if (const ArrayType *AT = Context.getAsArrayType(T)) { |
| 832 | isConstant = AT->getElementType().isConstant(Context); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 833 | } else if (const PointerType *PT = T->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | isConstant = T.isConstant(Context) && |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 835 | PT->getPointeeType().isConstant(Context); |
| 836 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 837 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 838 | if (isConstant) { |
| 839 | const VarDecl *Def = 0; |
| 840 | if (const Expr *Init = VD->getDefinition(Def)) |
| 841 | return SemaCheckStringLiteral(Init, TheCall, |
| 842 | HasVAListArg, format_idx, firstDataArg); |
| 843 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 844 | |
Anders Carlsson | d966a55 | 2009-06-28 19:55:58 +0000 | [diff] [blame] | 845 | // For vprintf* functions (i.e., HasVAListArg==true), we add a |
| 846 | // special check to see if the format string is a function parameter |
| 847 | // of the function calling the printf function. If the function |
| 848 | // has an attribute indicating it is a printf-like function, then we |
| 849 | // should suppress warnings concerning non-literals being used in a call |
| 850 | // to a vprintf function. For example: |
| 851 | // |
| 852 | // void |
| 853 | // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){ |
| 854 | // va_list ap; |
| 855 | // va_start(ap, fmt); |
| 856 | // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". |
| 857 | // ... |
| 858 | // |
| 859 | // |
| 860 | // FIXME: We don't have full attribute support yet, so just check to see |
| 861 | // if the argument is a DeclRefExpr that references a parameter. We'll |
| 862 | // add proper support for checking the attribute later. |
| 863 | if (HasVAListArg) |
| 864 | if (isa<ParmVarDecl>(VD)) |
| 865 | return true; |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 866 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 867 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 868 | return false; |
| 869 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 870 | |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 871 | case Stmt::CallExprClass: { |
| 872 | const CallExpr *CE = cast<CallExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | if (const ImplicitCastExpr *ICE |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 874 | = dyn_cast<ImplicitCastExpr>(CE->getCallee())) { |
| 875 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) { |
| 876 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 877 | if (const FormatArgAttr *FA = FD->getAttr<FormatArgAttr>()) { |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 878 | unsigned ArgIndex = FA->getFormatIdx(); |
| 879 | const Expr *Arg = CE->getArg(ArgIndex - 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 880 | |
| 881 | return SemaCheckStringLiteral(Arg, TheCall, HasVAListArg, |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 882 | format_idx, firstDataArg); |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 887 | |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 888 | return false; |
| 889 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 890 | case Stmt::ObjCStringLiteralClass: |
| 891 | case Stmt::StringLiteralClass: { |
| 892 | const StringLiteral *StrE = NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 893 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 894 | if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 895 | StrE = ObjCFExpr->getString(); |
| 896 | else |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 897 | StrE = cast<StringLiteral>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 898 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 899 | if (StrE) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 900 | CheckPrintfString(StrE, E, TheCall, HasVAListArg, format_idx, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 901 | firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 902 | return true; |
| 903 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 904 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 905 | return false; |
| 906 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 907 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 908 | default: |
| 909 | return false; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 913 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 914 | Sema::CheckNonNullArguments(const NonNullAttr *NonNull, |
| 915 | const CallExpr *TheCall) { |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 916 | for (NonNullAttr::iterator i = NonNull->begin(), e = NonNull->end(); |
| 917 | i != e; ++i) { |
Chris Lattner | 12b97ff | 2009-05-25 18:23:36 +0000 | [diff] [blame] | 918 | const Expr *ArgExpr = TheCall->getArg(*i); |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 919 | if (ArgExpr->isNullPointerConstant(Context, |
| 920 | Expr::NPC_ValueDependentIsNotNull)) |
Chris Lattner | 12b97ff | 2009-05-25 18:23:36 +0000 | [diff] [blame] | 921 | Diag(TheCall->getCallee()->getLocStart(), diag::warn_null_arg) |
| 922 | << ArgExpr->getSourceRange(); |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 923 | } |
| 924 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 925 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 926 | /// CheckPrintfArguments - Check calls to printf (and similar functions) for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 927 | /// correct use of format strings. |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 928 | /// |
| 929 | /// HasVAListArg - A predicate indicating whether the printf-like |
| 930 | /// function is passed an explicit va_arg argument (e.g., vprintf) |
| 931 | /// |
| 932 | /// format_idx - The index into Args for the format string. |
| 933 | /// |
| 934 | /// Improper format strings to functions in the printf family can be |
| 935 | /// the source of bizarre bugs and very serious security holes. A |
| 936 | /// good source of information is available in the following paper |
| 937 | /// (which includes additional references): |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 938 | /// |
| 939 | /// FormatGuard: Automatic Protection From printf Format String |
| 940 | /// Vulnerabilities, Proceedings of the 10th USENIX Security Symposium, 2001. |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 941 | /// |
| 942 | /// Functionality implemented: |
| 943 | /// |
| 944 | /// We can statically check the following properties for string |
| 945 | /// literal format strings for non v.*printf functions (where the |
| 946 | /// arguments are passed directly): |
| 947 | // |
| 948 | /// (1) Are the number of format conversions equal to the number of |
| 949 | /// data arguments? |
| 950 | /// |
| 951 | /// (2) Does each format conversion correctly match the type of the |
| 952 | /// corresponding data argument? (TODO) |
| 953 | /// |
| 954 | /// Moreover, for all printf functions we can: |
| 955 | /// |
| 956 | /// (3) Check for a missing format string (when not caught by type checking). |
| 957 | /// |
| 958 | /// (4) Check for no-operation flags; e.g. using "#" with format |
| 959 | /// conversion 'c' (TODO) |
| 960 | /// |
| 961 | /// (5) Check the use of '%n', a major source of security holes. |
| 962 | /// |
| 963 | /// (6) Check for malformed format conversions that don't specify anything. |
| 964 | /// |
| 965 | /// (7) Check for empty format strings. e.g: printf(""); |
| 966 | /// |
| 967 | /// (8) Check that the format string is a wide literal. |
| 968 | /// |
| 969 | /// All of these checks can be done by parsing the format string. |
| 970 | /// |
| 971 | /// For now, we ONLY do (1), (3), (5), (6), (7), and (8). |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 972 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 973 | Sema::CheckPrintfArguments(const CallExpr *TheCall, bool HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 974 | unsigned format_idx, unsigned firstDataArg) { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 975 | const Expr *Fn = TheCall->getCallee(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 976 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 977 | // The way the format attribute works in GCC, the implicit this argument |
| 978 | // of member functions is counted. However, it doesn't appear in our own |
| 979 | // lists, so decrement format_idx in that case. |
| 980 | if (isa<CXXMemberCallExpr>(TheCall)) { |
| 981 | // Catch a format attribute mistakenly referring to the object argument. |
| 982 | if (format_idx == 0) |
| 983 | return; |
| 984 | --format_idx; |
| 985 | if(firstDataArg != 0) |
| 986 | --firstDataArg; |
| 987 | } |
| 988 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | // CHECK: printf-like function is called with no format string. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 990 | if (format_idx >= TheCall->getNumArgs()) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 991 | Diag(TheCall->getRParenLoc(), diag::warn_printf_missing_format_string) |
| 992 | << Fn->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 993 | return; |
| 994 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 995 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 996 | const Expr *OrigFormatExpr = TheCall->getArg(format_idx)->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 998 | // CHECK: format string is not a string literal. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | // |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1000 | // Dynamically generated format strings are difficult to |
| 1001 | // automatically vet at compile time. Requiring that format strings |
| 1002 | // are string literals: (1) permits the checking of format strings by |
| 1003 | // the compiler and thereby (2) can practically remove the source of |
| 1004 | // many format string exploits. |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1005 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1006 | // Format string can be either ObjC string (e.g. @"%d") or |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1007 | // C string (e.g. "%d") |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1008 | // ObjC string uses the same format specifiers as C string, so we can use |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1009 | // the same format string checking logic for both ObjC and C strings. |
Chris Lattner | 1cd3e1f | 2009-04-29 04:49:34 +0000 | [diff] [blame] | 1010 | if (SemaCheckStringLiteral(OrigFormatExpr, TheCall, HasVAListArg, format_idx, |
| 1011 | firstDataArg)) |
| 1012 | return; // Literal format string found, check done! |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1013 | |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1014 | // If there are no arguments specified, warn with -Wformat-security, otherwise |
| 1015 | // warn only with -Wformat-nonliteral. |
| 1016 | if (TheCall->getNumArgs() == format_idx+1) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1017 | Diag(TheCall->getArg(format_idx)->getLocStart(), |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1018 | diag::warn_printf_nonliteral_noargs) |
| 1019 | << OrigFormatExpr->getSourceRange(); |
| 1020 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1021 | Diag(TheCall->getArg(format_idx)->getLocStart(), |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1022 | diag::warn_printf_nonliteral) |
| 1023 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1024 | } |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1025 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1026 | void Sema::CheckPrintfString(const StringLiteral *FExpr, |
| 1027 | const Expr *OrigFormatExpr, |
| 1028 | const CallExpr *TheCall, bool HasVAListArg, |
| 1029 | unsigned format_idx, unsigned firstDataArg) { |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1030 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1031 | const ObjCStringLiteral *ObjCFExpr = |
| 1032 | dyn_cast<ObjCStringLiteral>(OrigFormatExpr); |
| 1033 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1034 | // CHECK: is the format string a wide literal? |
| 1035 | if (FExpr->isWide()) { |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1036 | Diag(FExpr->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1037 | diag::warn_printf_format_string_is_wide_literal) |
| 1038 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1039 | return; |
| 1040 | } |
| 1041 | |
| 1042 | // Str - The format string. NOTE: this is NOT null-terminated! |
Chris Lattner | b9fc856 | 2009-04-29 04:12:34 +0000 | [diff] [blame] | 1043 | const char *Str = FExpr->getStrData(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1044 | |
| 1045 | // CHECK: empty format string? |
Chris Lattner | b9fc856 | 2009-04-29 04:12:34 +0000 | [diff] [blame] | 1046 | unsigned StrLen = FExpr->getByteLength(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1047 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1048 | if (StrLen == 0) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1049 | Diag(FExpr->getLocStart(), diag::warn_printf_empty_format_string) |
| 1050 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1051 | return; |
| 1052 | } |
| 1053 | |
| 1054 | // We process the format string using a binary state machine. The |
| 1055 | // current state is stored in CurrentState. |
| 1056 | enum { |
| 1057 | state_OrdChr, |
| 1058 | state_Conversion |
| 1059 | } CurrentState = state_OrdChr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1061 | // numConversions - The number of conversions seen so far. This is |
| 1062 | // incremented as we traverse the format string. |
| 1063 | unsigned numConversions = 0; |
| 1064 | |
| 1065 | // numDataArgs - The number of data arguments after the format |
| 1066 | // string. This can only be determined for non vprintf-like |
| 1067 | // functions. For those functions, this value is 1 (the sole |
| 1068 | // va_arg argument). |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1069 | unsigned numDataArgs = TheCall->getNumArgs()-firstDataArg; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1070 | |
| 1071 | // Inspect the format string. |
| 1072 | unsigned StrIdx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1073 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1074 | // LastConversionIdx - Index within the format string where we last saw |
| 1075 | // a '%' character that starts a new format conversion. |
| 1076 | unsigned LastConversionIdx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1078 | for (; StrIdx < StrLen; ++StrIdx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1079 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1080 | // Is the number of detected conversion conversions greater than |
| 1081 | // the number of matching data arguments? If so, stop. |
| 1082 | if (!HasVAListArg && numConversions > numDataArgs) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1084 | // Handle "\0" |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1085 | if (Str[StrIdx] == '\0') { |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1086 | // The string returned by getStrData() is not null-terminated, |
| 1087 | // so the presence of a null character is likely an error. |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 1088 | Diag(getLocationOfStringLiteralByte(FExpr, StrIdx), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1089 | diag::warn_printf_format_string_contains_null_char) |
| 1090 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1091 | return; |
| 1092 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1093 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1094 | // Ordinary characters (not processing a format conversion). |
| 1095 | if (CurrentState == state_OrdChr) { |
| 1096 | if (Str[StrIdx] == '%') { |
| 1097 | CurrentState = state_Conversion; |
| 1098 | LastConversionIdx = StrIdx; |
| 1099 | } |
| 1100 | continue; |
| 1101 | } |
| 1102 | |
| 1103 | // Seen '%'. Now processing a format conversion. |
| 1104 | switch (Str[StrIdx]) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | // Handle dynamic precision or width specifier. |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1106 | case '*': { |
| 1107 | ++numConversions; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1108 | |
Ted Kremenek | 42ae3e8 | 2009-05-13 16:06:05 +0000 | [diff] [blame] | 1109 | if (!HasVAListArg) { |
| 1110 | if (numConversions > numDataArgs) { |
| 1111 | SourceLocation Loc = getLocationOfStringLiteralByte(FExpr, StrIdx); |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 1112 | |
Ted Kremenek | 42ae3e8 | 2009-05-13 16:06:05 +0000 | [diff] [blame] | 1113 | if (Str[StrIdx-1] == '.') |
| 1114 | Diag(Loc, diag::warn_printf_asterisk_precision_missing_arg) |
| 1115 | << OrigFormatExpr->getSourceRange(); |
| 1116 | else |
| 1117 | Diag(Loc, diag::warn_printf_asterisk_width_missing_arg) |
| 1118 | << OrigFormatExpr->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1119 | |
Ted Kremenek | 42ae3e8 | 2009-05-13 16:06:05 +0000 | [diff] [blame] | 1120 | // Don't do any more checking. We'll just emit spurious errors. |
| 1121 | return; |
| 1122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | |
Ted Kremenek | 42ae3e8 | 2009-05-13 16:06:05 +0000 | [diff] [blame] | 1124 | // Perform type checking on width/precision specifier. |
| 1125 | const Expr *E = TheCall->getArg(format_idx+numConversions); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1126 | if (const BuiltinType *BT = E->getType()->getAs<BuiltinType>()) |
Ted Kremenek | 42ae3e8 | 2009-05-13 16:06:05 +0000 | [diff] [blame] | 1127 | if (BT->getKind() == BuiltinType::Int) |
| 1128 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | |
Ted Kremenek | 42ae3e8 | 2009-05-13 16:06:05 +0000 | [diff] [blame] | 1130 | SourceLocation Loc = getLocationOfStringLiteralByte(FExpr, StrIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1131 | |
Ted Kremenek | 42ae3e8 | 2009-05-13 16:06:05 +0000 | [diff] [blame] | 1132 | if (Str[StrIdx-1] == '.') |
| 1133 | Diag(Loc, diag::warn_printf_asterisk_precision_wrong_type) |
| 1134 | << E->getType() << E->getSourceRange(); |
| 1135 | else |
| 1136 | Diag(Loc, diag::warn_printf_asterisk_width_wrong_type) |
| 1137 | << E->getType() << E->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | |
| 1139 | break; |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 1140 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1141 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1142 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1143 | // Characters which can terminate a format conversion |
| 1144 | // (e.g. "%d"). Characters that specify length modifiers or |
| 1145 | // other flags are handled by the default case below. |
| 1146 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1147 | // FIXME: additional checks will go into the following cases. |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1148 | case 'i': |
| 1149 | case 'd': |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1150 | case 'o': |
| 1151 | case 'u': |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1152 | case 'x': |
| 1153 | case 'X': |
| 1154 | case 'D': |
| 1155 | case 'O': |
| 1156 | case 'U': |
| 1157 | case 'e': |
| 1158 | case 'E': |
| 1159 | case 'f': |
| 1160 | case 'F': |
| 1161 | case 'g': |
| 1162 | case 'G': |
| 1163 | case 'a': |
| 1164 | case 'A': |
| 1165 | case 'c': |
| 1166 | case 'C': |
| 1167 | case 'S': |
| 1168 | case 's': |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1169 | case 'p': |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1170 | ++numConversions; |
| 1171 | CurrentState = state_OrdChr; |
| 1172 | break; |
| 1173 | |
Eli Friedman | b92abb4 | 2009-06-02 08:36:19 +0000 | [diff] [blame] | 1174 | case 'm': |
| 1175 | // FIXME: Warn in situations where this isn't supported! |
| 1176 | CurrentState = state_OrdChr; |
| 1177 | break; |
| 1178 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1179 | // CHECK: Are we using "%n"? Issue a warning. |
| 1180 | case 'n': { |
| 1181 | ++numConversions; |
| 1182 | CurrentState = state_OrdChr; |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 1183 | SourceLocation Loc = getLocationOfStringLiteralByte(FExpr, |
| 1184 | LastConversionIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1185 | |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1186 | Diag(Loc, diag::warn_printf_write_back)<<OrigFormatExpr->getSourceRange(); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1187 | break; |
| 1188 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1189 | |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1190 | // Handle "%@" |
| 1191 | case '@': |
| 1192 | // %@ is allowed in ObjC format strings only. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | if (ObjCFExpr != NULL) |
| 1194 | CurrentState = state_OrdChr; |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1195 | else { |
| 1196 | // Issue a warning: invalid format conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1197 | SourceLocation Loc = |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 1198 | getLocationOfStringLiteralByte(FExpr, LastConversionIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 1200 | Diag(Loc, diag::warn_printf_invalid_conversion) |
| 1201 | << std::string(Str+LastConversionIdx, |
| 1202 | Str+std::min(LastConversionIdx+2, StrLen)) |
| 1203 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1204 | } |
| 1205 | ++numConversions; |
| 1206 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1208 | // Handle "%%" |
| 1209 | case '%': |
| 1210 | // Sanity check: Was the first "%" character the previous one? |
| 1211 | // If not, we will assume that we have a malformed format |
| 1212 | // conversion, and that the current "%" character is the start |
| 1213 | // of a new conversion. |
| 1214 | if (StrIdx - LastConversionIdx == 1) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1215 | CurrentState = state_OrdChr; |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1216 | else { |
| 1217 | // Issue a warning: invalid format conversion. |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 1218 | SourceLocation Loc = |
| 1219 | getLocationOfStringLiteralByte(FExpr, LastConversionIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1220 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 1221 | Diag(Loc, diag::warn_printf_invalid_conversion) |
| 1222 | << std::string(Str+LastConversionIdx, Str+StrIdx) |
| 1223 | << OrigFormatExpr->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1224 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1225 | // This conversion is broken. Advance to the next format |
| 1226 | // conversion. |
| 1227 | LastConversionIdx = StrIdx; |
| 1228 | ++numConversions; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1229 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1230 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1231 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1232 | default: |
| 1233 | // This case catches all other characters: flags, widths, etc. |
| 1234 | // We should eventually process those as well. |
| 1235 | break; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | if (CurrentState == state_Conversion) { |
| 1240 | // Issue a warning: invalid format conversion. |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 1241 | SourceLocation Loc = |
| 1242 | getLocationOfStringLiteralByte(FExpr, LastConversionIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1243 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 1244 | Diag(Loc, diag::warn_printf_invalid_conversion) |
| 1245 | << std::string(Str+LastConversionIdx, |
| 1246 | Str+std::min(LastConversionIdx+2, StrLen)) |
| 1247 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1248 | return; |
| 1249 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1250 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1251 | if (!HasVAListArg) { |
| 1252 | // CHECK: Does the number of format conversions exceed the number |
| 1253 | // of data arguments? |
| 1254 | if (numConversions > numDataArgs) { |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 1255 | SourceLocation Loc = |
| 1256 | getLocationOfStringLiteralByte(FExpr, LastConversionIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1257 | |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1258 | Diag(Loc, diag::warn_printf_insufficient_data_args) |
| 1259 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1260 | } |
| 1261 | // CHECK: Does the number of data arguments exceed the number of |
| 1262 | // format conversions in the format string? |
| 1263 | else if (numConversions < numDataArgs) |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1264 | Diag(TheCall->getArg(format_idx+numConversions+1)->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1265 | diag::warn_printf_too_many_data_args) |
| 1266 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1267 | } |
| 1268 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1269 | |
| 1270 | //===--- CHECK: Return Address of Stack Variable --------------------------===// |
| 1271 | |
| 1272 | static DeclRefExpr* EvalVal(Expr *E); |
| 1273 | static DeclRefExpr* EvalAddr(Expr* E); |
| 1274 | |
| 1275 | /// CheckReturnStackAddr - Check if a return statement returns the address |
| 1276 | /// of a stack variable. |
| 1277 | void |
| 1278 | Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, |
| 1279 | SourceLocation ReturnLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1280 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1281 | // Perform checking for returned stack addresses. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1282 | if (lhsType->isPointerType() || lhsType->isBlockPointerType()) { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1283 | if (DeclRefExpr *DR = EvalAddr(RetValExp)) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1284 | Diag(DR->getLocStart(), diag::warn_ret_stack_addr) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1285 | << DR->getDecl()->getDeclName() << RetValExp->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1286 | |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 1287 | // Skip over implicit cast expressions when checking for block expressions. |
Chris Lattner | 4ca606e | 2009-09-08 00:36:37 +0000 | [diff] [blame] | 1288 | RetValExp = RetValExp->IgnoreParenCasts(); |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 1289 | |
Chris Lattner | 9e6b37a | 2009-10-30 04:01:58 +0000 | [diff] [blame] | 1290 | if (BlockExpr *C = dyn_cast<BlockExpr>(RetValExp)) |
Mike Stump | 397195b | 2009-04-17 00:09:41 +0000 | [diff] [blame] | 1291 | if (C->hasBlockDeclRefExprs()) |
| 1292 | Diag(C->getLocStart(), diag::err_ret_local_block) |
| 1293 | << C->getSourceRange(); |
Chris Lattner | 9e6b37a | 2009-10-30 04:01:58 +0000 | [diff] [blame] | 1294 | |
| 1295 | if (AddrLabelExpr *ALE = dyn_cast<AddrLabelExpr>(RetValExp)) |
| 1296 | Diag(ALE->getLocStart(), diag::warn_ret_addr_label) |
| 1297 | << ALE->getSourceRange(); |
| 1298 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1299 | } else if (lhsType->isReferenceType()) { |
| 1300 | // Perform checking for stack values returned by reference. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1301 | // Check for a reference to the stack |
| 1302 | if (DeclRefExpr *DR = EvalVal(RetValExp)) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1303 | Diag(DR->getLocStart(), diag::warn_ret_stack_ref) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1304 | << DR->getDecl()->getDeclName() << RetValExp->getSourceRange(); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that |
| 1309 | /// check if the expression in a return statement evaluates to an address |
| 1310 | /// to a location on the stack. The recursion is used to traverse the |
| 1311 | /// AST of the return expression, with recursion backtracking when we |
| 1312 | /// encounter a subexpression that (1) clearly does not lead to the address |
| 1313 | /// of a stack variable or (2) is something we cannot determine leads to |
| 1314 | /// the address of a stack variable based on such local checking. |
| 1315 | /// |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 1316 | /// EvalAddr processes expressions that are pointers that are used as |
| 1317 | /// references (and not L-values). EvalVal handles all other values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1318 | /// At the base case of the recursion is a check for a DeclRefExpr* in |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1319 | /// the refers to a stack variable. |
| 1320 | /// |
| 1321 | /// This implementation handles: |
| 1322 | /// |
| 1323 | /// * pointer-to-pointer casts |
| 1324 | /// * implicit conversions from array references to pointers |
| 1325 | /// * taking the address of fields |
| 1326 | /// * arbitrary interplay between "&" and "*" operators |
| 1327 | /// * pointer arithmetic from an address of a stack variable |
| 1328 | /// * taking the address of an array element where the array is on the stack |
| 1329 | static DeclRefExpr* EvalAddr(Expr *E) { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1330 | // We should only be called for evaluating pointer expressions. |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 1331 | assert((E->getType()->isAnyPointerType() || |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1332 | E->getType()->isBlockPointerType() || |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1333 | E->getType()->isObjCQualifiedIdType()) && |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1334 | "EvalAddr only works on pointers"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1335 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1336 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 1337 | // viewed AST node. We then recursively traverse the AST by calling |
| 1338 | // EvalAddr and EvalVal appropriately. |
| 1339 | switch (E->getStmtClass()) { |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1340 | case Stmt::ParenExprClass: |
| 1341 | // Ignore parentheses. |
| 1342 | return EvalAddr(cast<ParenExpr>(E)->getSubExpr()); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1343 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1344 | case Stmt::UnaryOperatorClass: { |
| 1345 | // The only unary operator that make sense to handle here |
| 1346 | // is AddrOf. All others don't make sense as pointers. |
| 1347 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1348 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1349 | if (U->getOpcode() == UnaryOperator::AddrOf) |
| 1350 | return EvalVal(U->getSubExpr()); |
| 1351 | else |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1352 | return NULL; |
| 1353 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1354 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1355 | case Stmt::BinaryOperatorClass: { |
| 1356 | // Handle pointer arithmetic. All other binary operators are not valid |
| 1357 | // in this context. |
| 1358 | BinaryOperator *B = cast<BinaryOperator>(E); |
| 1359 | BinaryOperator::Opcode op = B->getOpcode(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1360 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1361 | if (op != BinaryOperator::Add && op != BinaryOperator::Sub) |
| 1362 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1364 | Expr *Base = B->getLHS(); |
| 1365 | |
| 1366 | // Determine which argument is the real pointer base. It could be |
| 1367 | // the RHS argument instead of the LHS. |
| 1368 | if (!Base->getType()->isPointerType()) Base = B->getRHS(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1370 | assert (Base->getType()->isPointerType()); |
| 1371 | return EvalAddr(Base); |
| 1372 | } |
Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 1373 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1374 | // For conditional operators we need to see if either the LHS or RHS are |
| 1375 | // valid DeclRefExpr*s. If one of them is valid, we return it. |
| 1376 | case Stmt::ConditionalOperatorClass: { |
| 1377 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1378 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1379 | // Handle the GNU extension for missing LHS. |
| 1380 | if (Expr *lhsExpr = C->getLHS()) |
| 1381 | if (DeclRefExpr* LHS = EvalAddr(lhsExpr)) |
| 1382 | return LHS; |
| 1383 | |
| 1384 | return EvalAddr(C->getRHS()); |
| 1385 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1386 | |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 1387 | // For casts, we need to handle conversions from arrays to |
| 1388 | // pointer values, and pointer-to-pointer conversions. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1389 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1390 | case Stmt::CStyleCastExprClass: |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1391 | case Stmt::CXXFunctionalCastExprClass: { |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1392 | Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 1393 | QualType T = SubExpr->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1394 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1395 | if (SubExpr->getType()->isPointerType() || |
| 1396 | SubExpr->getType()->isBlockPointerType() || |
| 1397 | SubExpr->getType()->isObjCQualifiedIdType()) |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 1398 | return EvalAddr(SubExpr); |
| 1399 | else if (T->isArrayType()) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1400 | return EvalVal(SubExpr); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1401 | else |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 1402 | return 0; |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1403 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1405 | // C++ casts. For dynamic casts, static casts, and const casts, we |
| 1406 | // are always converting from a pointer-to-pointer, so we just blow |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1407 | // through the cast. In the case the dynamic cast doesn't fail (and |
| 1408 | // return NULL), we take the conservative route and report cases |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1409 | // where we return the address of a stack variable. For Reinterpre |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1410 | // FIXME: The comment about is wrong; we're not always converting |
| 1411 | // from pointer to pointer. I'm guessing that this code should also |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | // handle references to objects. |
| 1413 | case Stmt::CXXStaticCastExprClass: |
| 1414 | case Stmt::CXXDynamicCastExprClass: |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1415 | case Stmt::CXXConstCastExprClass: |
| 1416 | case Stmt::CXXReinterpretCastExprClass: { |
| 1417 | Expr *S = cast<CXXNamedCastExpr>(E)->getSubExpr(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1418 | if (S->getType()->isPointerType() || S->getType()->isBlockPointerType()) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1419 | return EvalAddr(S); |
| 1420 | else |
| 1421 | return NULL; |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1422 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1423 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1424 | // Everything else: we simply don't reason about them. |
| 1425 | default: |
| 1426 | return NULL; |
| 1427 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1428 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1430 | |
| 1431 | /// EvalVal - This function is complements EvalAddr in the mutual recursion. |
| 1432 | /// See the comments for EvalAddr for more details. |
| 1433 | static DeclRefExpr* EvalVal(Expr *E) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1434 | |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 1435 | // We should only be called for evaluating non-pointer expressions, or |
| 1436 | // expressions with a pointer type that are not used as references but instead |
| 1437 | // are l-values (e.g., DeclRefExpr with a pointer type). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1438 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1439 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 1440 | // viewed AST node. We then recursively traverse the AST by calling |
| 1441 | // EvalAddr and EvalVal appropriately. |
| 1442 | switch (E->getStmtClass()) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 1443 | case Stmt::DeclRefExprClass: { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1444 | // DeclRefExpr: the base case. When we hit a DeclRefExpr we are looking |
| 1445 | // at code that refers to a variable's name. We check if it has local |
| 1446 | // storage within the function, and if so, return the expression. |
| 1447 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1449 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1450 | if (V->hasLocalStorage() && !V->getType()->isReferenceType()) return DR; |
| 1451 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1452 | return NULL; |
| 1453 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1454 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1455 | case Stmt::ParenExprClass: |
| 1456 | // Ignore parentheses. |
| 1457 | return EvalVal(cast<ParenExpr>(E)->getSubExpr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1459 | case Stmt::UnaryOperatorClass: { |
| 1460 | // The only unary operator that make sense to handle here |
| 1461 | // is Deref. All others don't resolve to a "name." This includes |
| 1462 | // handling all sorts of rvalues passed to a unary operator. |
| 1463 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1465 | if (U->getOpcode() == UnaryOperator::Deref) |
| 1466 | return EvalAddr(U->getSubExpr()); |
| 1467 | |
| 1468 | return NULL; |
| 1469 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1470 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1471 | case Stmt::ArraySubscriptExprClass: { |
| 1472 | // Array subscripts are potential references to data on the stack. We |
| 1473 | // retrieve the DeclRefExpr* for the array variable if it indeed |
| 1474 | // has local storage. |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 1475 | return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase()); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1476 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1477 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1478 | case Stmt::ConditionalOperatorClass: { |
| 1479 | // For conditional operators we need to see if either the LHS or RHS are |
| 1480 | // non-NULL DeclRefExpr's. If one is non-NULL, we return it. |
| 1481 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 1482 | |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 1483 | // Handle the GNU extension for missing LHS. |
| 1484 | if (Expr *lhsExpr = C->getLHS()) |
| 1485 | if (DeclRefExpr *LHS = EvalVal(lhsExpr)) |
| 1486 | return LHS; |
| 1487 | |
| 1488 | return EvalVal(C->getRHS()); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1489 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1491 | // Accesses to members are potential references to data on the stack. |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 1492 | case Stmt::MemberExprClass: { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1493 | MemberExpr *M = cast<MemberExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1494 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1495 | // Check for indirect access. We only want direct field accesses. |
| 1496 | if (!M->isArrow()) |
| 1497 | return EvalVal(M->getBase()); |
| 1498 | else |
| 1499 | return NULL; |
| 1500 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1502 | // Everything else: we simply don't reason about them. |
| 1503 | default: |
| 1504 | return NULL; |
| 1505 | } |
| 1506 | } |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1507 | |
| 1508 | //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// |
| 1509 | |
| 1510 | /// Check for comparisons of floating point operands using != and ==. |
| 1511 | /// Issue a warning if these are no self-comparisons, as they are not likely |
| 1512 | /// to do what the programmer intended. |
| 1513 | void Sema::CheckFloatComparison(SourceLocation loc, Expr* lex, Expr *rex) { |
| 1514 | bool EmitWarning = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1515 | |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 1516 | Expr* LeftExprSansParen = lex->IgnoreParens(); |
Ted Kremenek | 32e97b6 | 2008-01-17 17:55:13 +0000 | [diff] [blame] | 1517 | Expr* RightExprSansParen = rex->IgnoreParens(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1518 | |
| 1519 | // Special case: check for x == x (which is OK). |
| 1520 | // Do not emit warnings for such cases. |
| 1521 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) |
| 1522 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) |
| 1523 | if (DRL->getDecl() == DRR->getDecl()) |
| 1524 | EmitWarning = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1525 | |
| 1526 | |
Ted Kremenek | 1b500bb | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 1527 | // Special case: check for comparisons against literals that can be exactly |
| 1528 | // represented by APFloat. In such cases, do not emit a warning. This |
| 1529 | // is a heuristic: often comparison against such literals are used to |
| 1530 | // detect if a value in a variable has not changed. This clearly can |
| 1531 | // lead to false negatives. |
| 1532 | if (EmitWarning) { |
| 1533 | if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { |
| 1534 | if (FLL->isExact()) |
| 1535 | EmitWarning = false; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1536 | } else |
Ted Kremenek | 1b500bb | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 1537 | if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)){ |
| 1538 | if (FLR->isExact()) |
| 1539 | EmitWarning = false; |
| 1540 | } |
| 1541 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1542 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1543 | // Check for comparisons with builtin types. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1544 | if (EmitWarning) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1545 | if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1546 | if (CL->isBuiltinCall(Context)) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1547 | EmitWarning = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1548 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1549 | if (EmitWarning) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1550 | if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1551 | if (CR->isBuiltinCall(Context)) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1552 | EmitWarning = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1553 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1554 | // Emit the diagnostic. |
| 1555 | if (EmitWarning) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1556 | Diag(loc, diag::warn_floatingpoint_eq) |
| 1557 | << lex->getSourceRange() << rex->getSourceRange(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1558 | } |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 1559 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1560 | //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// |
| 1561 | //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 1562 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1563 | namespace { |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 1564 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1565 | /// Structure recording the 'active' range of an integer-valued |
| 1566 | /// expression. |
| 1567 | struct IntRange { |
| 1568 | /// The number of bits active in the int. |
| 1569 | unsigned Width; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 1570 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1571 | /// True if the int is known not to have negative values. |
| 1572 | bool NonNegative; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 1573 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1574 | IntRange() {} |
| 1575 | IntRange(unsigned Width, bool NonNegative) |
| 1576 | : Width(Width), NonNegative(NonNegative) |
| 1577 | {} |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 1578 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1579 | // Returns the range of the bool type. |
| 1580 | static IntRange forBoolType() { |
| 1581 | return IntRange(1, true); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1584 | // Returns the range of an integral type. |
| 1585 | static IntRange forType(ASTContext &C, QualType T) { |
| 1586 | return forCanonicalType(C, T->getCanonicalTypeInternal().getTypePtr()); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1589 | // Returns the range of an integeral type based on its canonical |
| 1590 | // representation. |
| 1591 | static IntRange forCanonicalType(ASTContext &C, const Type *T) { |
| 1592 | assert(T->isCanonicalUnqualified()); |
| 1593 | |
| 1594 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 1595 | T = VT->getElementType().getTypePtr(); |
| 1596 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 1597 | T = CT->getElementType().getTypePtr(); |
| 1598 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 1599 | T = ET->getDecl()->getIntegerType().getTypePtr(); |
| 1600 | |
| 1601 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 1602 | assert(BT->isInteger()); |
| 1603 | |
| 1604 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 1605 | } |
| 1606 | |
| 1607 | // Returns the supremum of two ranges: i.e. their conservative merge. |
| 1608 | static IntRange join(const IntRange &L, const IntRange &R) { |
| 1609 | return IntRange(std::max(L.Width, R.Width), |
| 1610 | L.NonNegative && R.NonNegative); |
| 1611 | } |
| 1612 | }; |
| 1613 | |
| 1614 | IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, unsigned MaxWidth) { |
| 1615 | if (value.isSigned() && value.isNegative()) |
| 1616 | return IntRange(value.getMinSignedBits(), false); |
| 1617 | |
| 1618 | if (value.getBitWidth() > MaxWidth) |
| 1619 | value.trunc(MaxWidth); |
| 1620 | |
| 1621 | // isNonNegative() just checks the sign bit without considering |
| 1622 | // signedness. |
| 1623 | return IntRange(value.getActiveBits(), true); |
| 1624 | } |
| 1625 | |
| 1626 | IntRange GetValueRange(ASTContext &C, APValue &result, |
| 1627 | unsigned MaxWidth) { |
| 1628 | if (result.isInt()) |
| 1629 | return GetValueRange(C, result.getInt(), MaxWidth); |
| 1630 | |
| 1631 | if (result.isVector()) { |
| 1632 | IntRange R = GetValueRange(C, result.getVectorElt(0), MaxWidth); |
| 1633 | for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) |
| 1634 | R = IntRange::join(R, GetValueRange(C, result.getVectorElt(i), MaxWidth)); |
| 1635 | return R; |
| 1636 | } |
| 1637 | |
| 1638 | if (result.isComplexInt()) { |
| 1639 | IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); |
| 1640 | IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); |
| 1641 | return IntRange::join(R, I); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | // This can happen with lossless casts to intptr_t of "based" lvalues. |
| 1645 | // Assume it might use arbitrary bits. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1646 | assert(result.isLValue()); |
| 1647 | return IntRange(MaxWidth, false); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 1648 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1649 | |
| 1650 | /// Pseudo-evaluate the given integer expression, estimating the |
| 1651 | /// range of values it might take. |
| 1652 | /// |
| 1653 | /// \param MaxWidth - the width to which the value will be truncated |
| 1654 | IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) { |
| 1655 | E = E->IgnoreParens(); |
| 1656 | |
| 1657 | // Try a full evaluation first. |
| 1658 | Expr::EvalResult result; |
| 1659 | if (E->Evaluate(result, C)) |
| 1660 | return GetValueRange(C, result.Val, MaxWidth); |
| 1661 | |
| 1662 | // I think we only want to look through implicit casts here; if the |
| 1663 | // user has an explicit widening cast, we should treat the value as |
| 1664 | // being of the new, wider type. |
| 1665 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) { |
| 1666 | if (CE->getCastKind() == CastExpr::CK_NoOp) |
| 1667 | return GetExprRange(C, CE->getSubExpr(), MaxWidth); |
| 1668 | |
| 1669 | IntRange OutputTypeRange = IntRange::forType(C, CE->getType()); |
| 1670 | |
| 1671 | // Assume that non-integer casts can span the full range of the type. |
| 1672 | if (CE->getCastKind() != CastExpr::CK_IntegralCast) |
| 1673 | return OutputTypeRange; |
| 1674 | |
| 1675 | IntRange SubRange |
| 1676 | = GetExprRange(C, CE->getSubExpr(), |
| 1677 | std::min(MaxWidth, OutputTypeRange.Width)); |
| 1678 | |
| 1679 | // Bail out if the subexpr's range is as wide as the cast type. |
| 1680 | if (SubRange.Width >= OutputTypeRange.Width) |
| 1681 | return OutputTypeRange; |
| 1682 | |
| 1683 | // Otherwise, we take the smaller width, and we're non-negative if |
| 1684 | // either the output type or the subexpr is. |
| 1685 | return IntRange(SubRange.Width, |
| 1686 | SubRange.NonNegative || OutputTypeRange.NonNegative); |
| 1687 | } |
| 1688 | |
| 1689 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 1690 | // If we can fold the condition, just take that operand. |
| 1691 | bool CondResult; |
| 1692 | if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) |
| 1693 | return GetExprRange(C, CondResult ? CO->getTrueExpr() |
| 1694 | : CO->getFalseExpr(), |
| 1695 | MaxWidth); |
| 1696 | |
| 1697 | // Otherwise, conservatively merge. |
| 1698 | IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth); |
| 1699 | IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth); |
| 1700 | return IntRange::join(L, R); |
| 1701 | } |
| 1702 | |
| 1703 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 1704 | switch (BO->getOpcode()) { |
| 1705 | |
| 1706 | // Boolean-valued operations are single-bit and positive. |
| 1707 | case BinaryOperator::LAnd: |
| 1708 | case BinaryOperator::LOr: |
| 1709 | case BinaryOperator::LT: |
| 1710 | case BinaryOperator::GT: |
| 1711 | case BinaryOperator::LE: |
| 1712 | case BinaryOperator::GE: |
| 1713 | case BinaryOperator::EQ: |
| 1714 | case BinaryOperator::NE: |
| 1715 | return IntRange::forBoolType(); |
| 1716 | |
| 1717 | // Operations with opaque sources are black-listed. |
| 1718 | case BinaryOperator::PtrMemD: |
| 1719 | case BinaryOperator::PtrMemI: |
| 1720 | return IntRange::forType(C, E->getType()); |
| 1721 | |
| 1722 | // Left shift gets black-listed based on a judgement call. |
| 1723 | case BinaryOperator::Shl: |
| 1724 | return IntRange::forType(C, E->getType()); |
| 1725 | |
| 1726 | // Various special cases. |
| 1727 | case BinaryOperator::Shr: |
| 1728 | // TODO: if the RHS is constant, change the width as appropriate. |
| 1729 | return GetExprRange(C, BO->getLHS(), MaxWidth); |
| 1730 | case BinaryOperator::Comma: |
| 1731 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 1732 | |
| 1733 | case BinaryOperator::Sub: |
| 1734 | if (BO->getLHS()->getType()->isPointerType()) |
| 1735 | return IntRange::forType(C, E->getType()); |
| 1736 | // fallthrough |
| 1737 | |
| 1738 | default: |
| 1739 | break; |
| 1740 | } |
| 1741 | |
| 1742 | // Treat every other operator as if it were closed on the |
| 1743 | // narrowest type that encompasses both operands. |
| 1744 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 1745 | IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth); |
| 1746 | return IntRange::join(L, R); |
| 1747 | } |
| 1748 | |
| 1749 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 1750 | switch (UO->getOpcode()) { |
| 1751 | // Boolean-valued operations are white-listed. |
| 1752 | case UnaryOperator::LNot: |
| 1753 | return IntRange::forBoolType(); |
| 1754 | |
| 1755 | // Operations with opaque sources are black-listed. |
| 1756 | case UnaryOperator::Deref: |
| 1757 | case UnaryOperator::AddrOf: // should be impossible |
| 1758 | case UnaryOperator::OffsetOf: |
| 1759 | return IntRange::forType(C, E->getType()); |
| 1760 | |
| 1761 | default: |
| 1762 | return GetExprRange(C, UO->getSubExpr(), MaxWidth); |
| 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | FieldDecl *BitField = E->getBitField(); |
| 1767 | if (BitField) { |
| 1768 | llvm::APSInt BitWidthAP = BitField->getBitWidth()->EvaluateAsInt(C); |
| 1769 | unsigned BitWidth = BitWidthAP.getZExtValue(); |
| 1770 | |
| 1771 | return IntRange(BitWidth, BitField->getType()->isUnsignedIntegerType()); |
| 1772 | } |
| 1773 | |
| 1774 | return IntRange::forType(C, E->getType()); |
| 1775 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 1776 | |
| 1777 | /// Checks whether the given value, which currently has the given |
| 1778 | /// source semantics, has the same value when coerced through the |
| 1779 | /// target semantics. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1780 | bool IsSameFloatAfterCast(const llvm::APFloat &value, |
| 1781 | const llvm::fltSemantics &Src, |
| 1782 | const llvm::fltSemantics &Tgt) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 1783 | llvm::APFloat truncated = value; |
| 1784 | |
| 1785 | bool ignored; |
| 1786 | truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 1787 | truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 1788 | |
| 1789 | return truncated.bitwiseIsEqual(value); |
| 1790 | } |
| 1791 | |
| 1792 | /// Checks whether the given value, which currently has the given |
| 1793 | /// source semantics, has the same value when coerced through the |
| 1794 | /// target semantics. |
| 1795 | /// |
| 1796 | /// The value might be a vector of floats (or a complex number). |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1797 | bool IsSameFloatAfterCast(const APValue &value, |
| 1798 | const llvm::fltSemantics &Src, |
| 1799 | const llvm::fltSemantics &Tgt) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 1800 | if (value.isFloat()) |
| 1801 | return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); |
| 1802 | |
| 1803 | if (value.isVector()) { |
| 1804 | for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) |
| 1805 | if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) |
| 1806 | return false; |
| 1807 | return true; |
| 1808 | } |
| 1809 | |
| 1810 | assert(value.isComplexFloat()); |
| 1811 | return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && |
| 1812 | IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); |
| 1813 | } |
| 1814 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1815 | } // end anonymous namespace |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 1816 | |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 1817 | /// \brief Implements -Wsign-compare. |
| 1818 | /// |
| 1819 | /// \param lex the left-hand expression |
| 1820 | /// \param rex the right-hand expression |
| 1821 | /// \param OpLoc the location of the joining operator |
| 1822 | /// \param Equality whether this is an "equality-like" join, which |
| 1823 | /// suppresses the warning in some cases |
| 1824 | void Sema::CheckSignCompare(Expr *lex, Expr *rex, SourceLocation OpLoc, |
| 1825 | const PartialDiagnostic &PD, bool Equality) { |
| 1826 | // Don't warn if we're in an unevaluated context. |
| 1827 | if (ExprEvalContexts.back().Context == Unevaluated) |
| 1828 | return; |
| 1829 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1830 | // If either expression is value-dependent, don't warn. We'll get another |
| 1831 | // chance at instantiation time. |
| 1832 | if (lex->isValueDependent() || rex->isValueDependent()) |
| 1833 | return; |
| 1834 | |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 1835 | QualType lt = lex->getType(), rt = rex->getType(); |
| 1836 | |
| 1837 | // Only warn if both operands are integral. |
| 1838 | if (!lt->isIntegerType() || !rt->isIntegerType()) |
| 1839 | return; |
| 1840 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1841 | // In C, the width of a bitfield determines its type, and the |
| 1842 | // declared type only contributes the signedness. This duplicates |
| 1843 | // the work that will later be done by UsualUnaryConversions. |
| 1844 | // Eventually, this check will be reorganized in a way that avoids |
| 1845 | // this duplication. |
| 1846 | if (!getLangOptions().CPlusPlus) { |
| 1847 | QualType tmp; |
| 1848 | tmp = Context.isPromotableBitField(lex); |
| 1849 | if (!tmp.isNull()) lt = tmp; |
| 1850 | tmp = Context.isPromotableBitField(rex); |
| 1851 | if (!tmp.isNull()) rt = tmp; |
| 1852 | } |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 1853 | |
| 1854 | // The rule is that the signed operand becomes unsigned, so isolate the |
| 1855 | // signed operand. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1856 | Expr *signedOperand = lex, *unsignedOperand = rex; |
| 1857 | QualType signedType = lt, unsignedType = rt; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 1858 | if (lt->isSignedIntegerType()) { |
| 1859 | if (rt->isSignedIntegerType()) return; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 1860 | } else { |
| 1861 | if (!rt->isSignedIntegerType()) return; |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1862 | std::swap(signedOperand, unsignedOperand); |
| 1863 | std::swap(signedType, unsignedType); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1866 | unsigned unsignedWidth = Context.getIntWidth(unsignedType); |
| 1867 | unsigned signedWidth = Context.getIntWidth(signedType); |
| 1868 | |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 1869 | // If the unsigned type is strictly smaller than the signed type, |
| 1870 | // then (1) the result type will be signed and (2) the unsigned |
| 1871 | // value will fit fully within the signed type, and thus the result |
| 1872 | // of the comparison will be exact. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1873 | if (signedWidth > unsignedWidth) |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 1874 | return; |
| 1875 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1876 | // Otherwise, calculate the effective ranges. |
| 1877 | IntRange signedRange = GetExprRange(Context, signedOperand, signedWidth); |
| 1878 | IntRange unsignedRange = GetExprRange(Context, unsignedOperand, unsignedWidth); |
| 1879 | |
| 1880 | // We should never be unable to prove that the unsigned operand is |
| 1881 | // non-negative. |
| 1882 | assert(unsignedRange.NonNegative && "unsigned range includes negative?"); |
| 1883 | |
| 1884 | // If the signed operand is non-negative, then the signed->unsigned |
| 1885 | // conversion won't change it. |
| 1886 | if (signedRange.NonNegative) |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 1887 | return; |
| 1888 | |
| 1889 | // For (in)equality comparisons, if the unsigned operand is a |
| 1890 | // constant which cannot collide with a overflowed signed operand, |
| 1891 | // then reinterpreting the signed operand as unsigned will not |
| 1892 | // change the result of the comparison. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1893 | if (Equality && unsignedRange.Width < unsignedWidth) |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 1894 | return; |
| 1895 | |
| 1896 | Diag(OpLoc, PD) |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1897 | << lt << rt << lex->getSourceRange() << rex->getSourceRange(); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 1900 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
| 1901 | static void DiagnoseImpCast(Sema &S, Expr *E, QualType T, unsigned diag) { |
| 1902 | S.Diag(E->getExprLoc(), diag) << E->getType() << T << E->getSourceRange(); |
| 1903 | } |
| 1904 | |
| 1905 | /// Implements -Wconversion. |
| 1906 | void Sema::CheckImplicitConversion(Expr *E, QualType T) { |
| 1907 | // Don't diagnose in unevaluated contexts. |
| 1908 | if (ExprEvalContexts.back().Context == Sema::Unevaluated) |
| 1909 | return; |
| 1910 | |
| 1911 | // Don't diagnose for value-dependent expressions. |
| 1912 | if (E->isValueDependent()) |
| 1913 | return; |
| 1914 | |
| 1915 | const Type *Source = Context.getCanonicalType(E->getType()).getTypePtr(); |
| 1916 | const Type *Target = Context.getCanonicalType(T).getTypePtr(); |
| 1917 | |
| 1918 | // Never diagnose implicit casts to bool. |
| 1919 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) |
| 1920 | return; |
| 1921 | |
| 1922 | // Strip vector types. |
| 1923 | if (isa<VectorType>(Source)) { |
| 1924 | if (!isa<VectorType>(Target)) |
| 1925 | return DiagnoseImpCast(*this, E, T, diag::warn_impcast_vector_scalar); |
| 1926 | |
| 1927 | Source = cast<VectorType>(Source)->getElementType().getTypePtr(); |
| 1928 | Target = cast<VectorType>(Target)->getElementType().getTypePtr(); |
| 1929 | } |
| 1930 | |
| 1931 | // Strip complex types. |
| 1932 | if (isa<ComplexType>(Source)) { |
| 1933 | if (!isa<ComplexType>(Target)) |
| 1934 | return DiagnoseImpCast(*this, E, T, diag::warn_impcast_complex_scalar); |
| 1935 | |
| 1936 | Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); |
| 1937 | Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); |
| 1938 | } |
| 1939 | |
| 1940 | const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); |
| 1941 | const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); |
| 1942 | |
| 1943 | // If the source is floating point... |
| 1944 | if (SourceBT && SourceBT->isFloatingPoint()) { |
| 1945 | // ...and the target is floating point... |
| 1946 | if (TargetBT && TargetBT->isFloatingPoint()) { |
| 1947 | // ...then warn if we're dropping FP rank. |
| 1948 | |
| 1949 | // Builtin FP kinds are ordered by increasing FP rank. |
| 1950 | if (SourceBT->getKind() > TargetBT->getKind()) { |
| 1951 | // Don't warn about float constants that are precisely |
| 1952 | // representable in the target type. |
| 1953 | Expr::EvalResult result; |
| 1954 | if (E->Evaluate(result, Context)) { |
| 1955 | // Value might be a float, a float vector, or a float complex. |
| 1956 | if (IsSameFloatAfterCast(result.Val, |
| 1957 | Context.getFloatTypeSemantics(QualType(TargetBT, 0)), |
| 1958 | Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) |
| 1959 | return; |
| 1960 | } |
| 1961 | |
| 1962 | DiagnoseImpCast(*this, E, T, diag::warn_impcast_float_precision); |
| 1963 | } |
| 1964 | return; |
| 1965 | } |
| 1966 | |
| 1967 | // If the target is integral, always warn. |
| 1968 | if ((TargetBT && TargetBT->isInteger())) |
| 1969 | // TODO: don't warn for integer values? |
| 1970 | return DiagnoseImpCast(*this, E, T, diag::warn_impcast_float_integer); |
| 1971 | |
| 1972 | return; |
| 1973 | } |
| 1974 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1975 | if (!Source->isIntegerType() || !Target->isIntegerType()) |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 1976 | return; |
| 1977 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1978 | IntRange SourceRange = GetExprRange(Context, E, Context.getIntWidth(E->getType())); |
| 1979 | IntRange TargetRange = IntRange::forCanonicalType(Context, Target); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 1980 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1981 | // FIXME: also signed<->unsigned? |
| 1982 | |
| 1983 | if (SourceRange.Width > TargetRange.Width) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 1984 | // People want to build with -Wshorten-64-to-32 and not -Wconversion |
| 1985 | // and by god we'll let them. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame^] | 1986 | if (SourceRange.Width == 64 && TargetRange.Width == 32) |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 1987 | return DiagnoseImpCast(*this, E, T, diag::warn_impcast_integer_64_32); |
| 1988 | return DiagnoseImpCast(*this, E, T, diag::warn_impcast_integer_precision); |
| 1989 | } |
| 1990 | |
| 1991 | return; |
| 1992 | } |
| 1993 | |