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