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