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