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