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