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