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