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