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