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 | |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Sema.h" |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame^] | 16 | #include "clang/Sema/SemaInternal.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 17 | #include "clang/Sema/ScopeInfo.h" |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/Analyses/FormatString.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 20 | #include "clang/AST/CharUnits.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclCXX.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" |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 33 | #include "clang/Basic/TargetBuiltins.h" |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 34 | #include "clang/Basic/TargetInfo.h" |
Zhongxing Xu | a1f3dba | 2009-05-20 01:55:10 +0000 | [diff] [blame] | 35 | #include <limits> |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 36 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 37 | using namespace sema; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 39 | /// getLocationOfStringLiteralByte - Return a source location that points to the |
| 40 | /// specified byte of the specified string literal. |
| 41 | /// |
| 42 | /// Strings are amazingly complex. They can be formed from multiple tokens and |
| 43 | /// can have escape sequences in them in addition to the usual trigraph and |
| 44 | /// escaped newline business. This routine handles this complexity. |
| 45 | /// |
| 46 | SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, |
| 47 | unsigned ByteNo) const { |
| 48 | assert(!SL->isWide() && "This doesn't work for wide strings yet"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 50 | // Loop over all of the tokens in this string until we find the one that |
| 51 | // contains the byte we're looking for. |
| 52 | unsigned TokNo = 0; |
| 53 | while (1) { |
| 54 | assert(TokNo < SL->getNumConcatenated() && "Invalid byte number!"); |
| 55 | SourceLocation StrTokLoc = SL->getStrTokenLoc(TokNo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 57 | // Get the spelling of the string so that we can get the data that makes up |
| 58 | // the string literal, not the identifier for the macro it is potentially |
| 59 | // expanded through. |
| 60 | SourceLocation StrTokSpellingLoc = SourceMgr.getSpellingLoc(StrTokLoc); |
| 61 | |
| 62 | // Re-lex the token to get its length and original spelling. |
| 63 | std::pair<FileID, unsigned> LocInfo = |
| 64 | SourceMgr.getDecomposedLoc(StrTokSpellingLoc); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 65 | bool Invalid = false; |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 66 | llvm::StringRef Buffer = SourceMgr.getBufferData(LocInfo.first, &Invalid); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 67 | if (Invalid) |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 68 | return StrTokSpellingLoc; |
| 69 | |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 70 | const char *StrData = Buffer.data()+LocInfo.second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 72 | // Create a langops struct and enable trigraphs. This is sufficient for |
| 73 | // relexing tokens. |
| 74 | LangOptions LangOpts; |
| 75 | LangOpts.Trigraphs = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 77 | // Create a lexer starting at the beginning of this token. |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 78 | Lexer TheLexer(StrTokSpellingLoc, LangOpts, Buffer.begin(), StrData, |
| 79 | Buffer.end()); |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 80 | Token TheTok; |
| 81 | TheLexer.LexFromRawLexer(TheTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 443e53c | 2009-02-18 19:26:42 +0000 | [diff] [blame] | 83 | // Use the StringLiteralParser to compute the length of the string in bytes. |
Douglas Gregor | b90f4b3 | 2010-05-26 05:35:51 +0000 | [diff] [blame] | 84 | StringLiteralParser SLP(&TheTok, 1, PP, /*Complain=*/false); |
Chris Lattner | 443e53c | 2009-02-18 19:26:42 +0000 | [diff] [blame] | 85 | unsigned TokNumBytes = SLP.GetStringLength(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 2197c96 | 2009-02-18 18:52:52 +0000 | [diff] [blame] | 87 | // 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] | 88 | if (ByteNo < TokNumBytes || |
| 89 | (ByteNo == TokNumBytes && TokNo == SL->getNumConcatenated())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | unsigned Offset = |
Douglas Gregor | b90f4b3 | 2010-05-26 05:35:51 +0000 | [diff] [blame] | 91 | StringLiteralParser::getOffsetOfStringByte(TheTok, ByteNo, PP, |
| 92 | /*Complain=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 94 | // Now that we know the offset of the token in the spelling, use the |
| 95 | // preprocessor to get the offset in the original source. |
| 96 | return PP.AdvanceToTokenCharacter(StrTokLoc, Offset); |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 97 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 99 | // Move to the next string token. |
| 100 | ++TokNo; |
| 101 | ByteNo -= TokNumBytes; |
| 102 | } |
| 103 | } |
| 104 | |
Ryan Flynn | 4403a5e | 2009-08-06 03:00:50 +0000 | [diff] [blame] | 105 | /// CheckablePrintfAttr - does a function call have a "printf" attribute |
| 106 | /// and arguments that merit checking? |
| 107 | bool Sema::CheckablePrintfAttr(const FormatAttr *Format, CallExpr *TheCall) { |
| 108 | if (Format->getType() == "printf") return true; |
| 109 | if (Format->getType() == "printf0") { |
| 110 | // printf0 allows null "format" string; if so don't check format/args |
| 111 | unsigned format_idx = Format->getFormatIdx() - 1; |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 112 | // Does the index refer to the implicit object argument? |
| 113 | if (isa<CXXMemberCallExpr>(TheCall)) { |
| 114 | if (format_idx == 0) |
| 115 | return false; |
| 116 | --format_idx; |
| 117 | } |
Ryan Flynn | 4403a5e | 2009-08-06 03:00:50 +0000 | [diff] [blame] | 118 | if (format_idx < TheCall->getNumArgs()) { |
| 119 | Expr *Format = TheCall->getArg(format_idx)->IgnoreParenCasts(); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 120 | if (!Format->isNullPointerConstant(Context, |
| 121 | Expr::NPC_ValueDependentIsNull)) |
Ryan Flynn | 4403a5e | 2009-08-06 03:00:50 +0000 | [diff] [blame] | 122 | return true; |
| 123 | } |
| 124 | } |
| 125 | return false; |
| 126 | } |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 127 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 128 | ExprResult |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 129 | Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 130 | ExprResult TheCallResult(Owned(TheCall)); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 131 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 132 | switch (BuiltinID) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 133 | case Builtin::BI__builtin___CFStringMakeConstantString: |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 134 | assert(TheCall->getNumArgs() == 1 && |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 135 | "Wrong # arguments to builtin CFStringMakeConstantString"); |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 136 | if (CheckObjCString(TheCall->getArg(0))) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 137 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 138 | break; |
Ted Kremenek | 49ff7a1 | 2008-07-09 17:58:53 +0000 | [diff] [blame] | 139 | case Builtin::BI__builtin_stdarg_start: |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 140 | case Builtin::BI__builtin_va_start: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 141 | if (SemaBuiltinVAStart(TheCall)) |
| 142 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 143 | break; |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 144 | case Builtin::BI__builtin_isgreater: |
| 145 | case Builtin::BI__builtin_isgreaterequal: |
| 146 | case Builtin::BI__builtin_isless: |
| 147 | case Builtin::BI__builtin_islessequal: |
| 148 | case Builtin::BI__builtin_islessgreater: |
| 149 | case Builtin::BI__builtin_isunordered: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 150 | if (SemaBuiltinUnorderedCompare(TheCall)) |
| 151 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 152 | break; |
Benjamin Kramer | e771a7a | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 153 | case Builtin::BI__builtin_fpclassify: |
| 154 | if (SemaBuiltinFPClassification(TheCall, 6)) |
| 155 | return ExprError(); |
| 156 | break; |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 157 | case Builtin::BI__builtin_isfinite: |
| 158 | case Builtin::BI__builtin_isinf: |
| 159 | case Builtin::BI__builtin_isinf_sign: |
| 160 | case Builtin::BI__builtin_isnan: |
| 161 | case Builtin::BI__builtin_isnormal: |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 162 | if (SemaBuiltinFPClassification(TheCall, 1)) |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 163 | return ExprError(); |
| 164 | break; |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 165 | case Builtin::BI__builtin_return_address: |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 166 | case Builtin::BI__builtin_frame_address: { |
| 167 | llvm::APSInt Result; |
| 168 | if (SemaBuiltinConstantArg(TheCall, 0, Result)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 169 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 170 | break; |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 171 | } |
| 172 | case Builtin::BI__builtin_eh_return_data_regno: { |
| 173 | llvm::APSInt Result; |
| 174 | if (SemaBuiltinConstantArg(TheCall, 0, Result)) |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 175 | return ExprError(); |
| 176 | break; |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 177 | } |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 178 | case Builtin::BI__builtin_shufflevector: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 179 | return SemaBuiltinShuffleVector(TheCall); |
| 180 | // TheCall will be freed by the smart pointer here, but that's fine, since |
| 181 | // SemaBuiltinShuffleVector guts it, but then doesn't release it. |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 182 | case Builtin::BI__builtin_prefetch: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 183 | if (SemaBuiltinPrefetch(TheCall)) |
| 184 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 185 | break; |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 186 | case Builtin::BI__builtin_object_size: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 187 | if (SemaBuiltinObjectSize(TheCall)) |
| 188 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 189 | break; |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 190 | case Builtin::BI__builtin_longjmp: |
| 191 | if (SemaBuiltinLongjmp(TheCall)) |
| 192 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 193 | break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 194 | case Builtin::BI__sync_fetch_and_add: |
| 195 | case Builtin::BI__sync_fetch_and_sub: |
| 196 | case Builtin::BI__sync_fetch_and_or: |
| 197 | case Builtin::BI__sync_fetch_and_and: |
| 198 | case Builtin::BI__sync_fetch_and_xor: |
| 199 | case Builtin::BI__sync_add_and_fetch: |
| 200 | case Builtin::BI__sync_sub_and_fetch: |
| 201 | case Builtin::BI__sync_and_and_fetch: |
| 202 | case Builtin::BI__sync_or_and_fetch: |
| 203 | case Builtin::BI__sync_xor_and_fetch: |
| 204 | case Builtin::BI__sync_val_compare_and_swap: |
| 205 | case Builtin::BI__sync_bool_compare_and_swap: |
| 206 | case Builtin::BI__sync_lock_test_and_set: |
| 207 | case Builtin::BI__sync_lock_release: |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 208 | return SemaBuiltinAtomicOverloaded(move(TheCallResult)); |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // Since the target specific builtins for each arch overlap, only check those |
| 212 | // of the arch we are compiling for. |
| 213 | if (BuiltinID >= Builtin::FirstTSBuiltin) { |
| 214 | switch (Context.Target.getTriple().getArch()) { |
| 215 | case llvm::Triple::arm: |
| 216 | case llvm::Triple::thumb: |
| 217 | if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall)) |
| 218 | return ExprError(); |
| 219 | break; |
| 220 | case llvm::Triple::x86: |
| 221 | case llvm::Triple::x86_64: |
| 222 | if (CheckX86BuiltinFunctionCall(BuiltinID, TheCall)) |
| 223 | return ExprError(); |
| 224 | break; |
| 225 | default: |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | return move(TheCallResult); |
| 231 | } |
| 232 | |
| 233 | bool Sema::CheckX86BuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
| 234 | switch (BuiltinID) { |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 235 | case X86::BI__builtin_ia32_palignr128: |
| 236 | case X86::BI__builtin_ia32_palignr: { |
| 237 | llvm::APSInt Result; |
| 238 | if (SemaBuiltinConstantArg(TheCall, 2, Result)) |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 239 | return true; |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 240 | break; |
| 241 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 242 | } |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 243 | return false; |
| 244 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 246 | // Get the valid immediate range for the specified NEON type code. |
| 247 | static unsigned RFT(unsigned t, bool shift = false) { |
| 248 | bool quad = t & 0x10; |
| 249 | |
| 250 | switch (t & 0x7) { |
| 251 | case 0: // i8 |
Nate Begeman | d69ec16 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 252 | return shift ? 7 : (8 << (int)quad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 253 | case 1: // i16 |
Nate Begeman | d69ec16 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 254 | return shift ? 15 : (4 << (int)quad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 255 | case 2: // i32 |
Nate Begeman | d69ec16 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 256 | return shift ? 31 : (2 << (int)quad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 257 | case 3: // i64 |
Nate Begeman | d69ec16 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 258 | return shift ? 63 : (1 << (int)quad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 259 | case 4: // f32 |
| 260 | assert(!shift && "cannot shift float types!"); |
Nate Begeman | d69ec16 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 261 | return (2 << (int)quad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 262 | case 5: // poly8 |
| 263 | assert(!shift && "cannot shift polynomial types!"); |
Nate Begeman | d69ec16 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 264 | return (8 << (int)quad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 265 | case 6: // poly16 |
| 266 | assert(!shift && "cannot shift polynomial types!"); |
Nate Begeman | d69ec16 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 267 | return (4 << (int)quad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 268 | case 7: // float16 |
| 269 | assert(!shift && "cannot shift float types!"); |
Nate Begeman | d69ec16 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 270 | return (4 << (int)quad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 271 | } |
| 272 | return 0; |
| 273 | } |
| 274 | |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 275 | bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 276 | llvm::APSInt Result; |
| 277 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 278 | unsigned mask = 0; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 279 | unsigned TV = 0; |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 280 | switch (BuiltinID) { |
Nate Begeman | a23326b | 2010-06-17 04:17:01 +0000 | [diff] [blame] | 281 | #define GET_NEON_OVERLOAD_CHECK |
| 282 | #include "clang/Basic/arm_neon.inc" |
| 283 | #undef GET_NEON_OVERLOAD_CHECK |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 286 | // For NEON intrinsics which are overloaded on vector element type, validate |
| 287 | // the immediate which specifies which variant to emit. |
| 288 | if (mask) { |
| 289 | unsigned ArgNo = TheCall->getNumArgs()-1; |
| 290 | if (SemaBuiltinConstantArg(TheCall, ArgNo, Result)) |
| 291 | return true; |
| 292 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 293 | TV = Result.getLimitedValue(32); |
| 294 | if ((TV > 31) || (mask & (1 << TV)) == 0) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 295 | return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code) |
| 296 | << TheCall->getArg(ArgNo)->getSourceRange(); |
| 297 | } |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 298 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 299 | // For NEON intrinsics which take an immediate value as part of the |
| 300 | // instruction, range check them here. |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 301 | unsigned i = 0, l = 0, u = 0; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 302 | switch (BuiltinID) { |
| 303 | default: return false; |
Nate Begeman | bb37f50 | 2010-07-29 22:48:34 +0000 | [diff] [blame] | 304 | case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break; |
| 305 | case ARM::BI__builtin_arm_usat: i = 1; u = 31; break; |
Nate Begeman | 99c40bb | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 306 | case ARM::BI__builtin_arm_vcvtr_f: |
| 307 | case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break; |
Nate Begeman | a23326b | 2010-06-17 04:17:01 +0000 | [diff] [blame] | 308 | #define GET_NEON_IMMEDIATE_CHECK |
| 309 | #include "clang/Basic/arm_neon.inc" |
| 310 | #undef GET_NEON_IMMEDIATE_CHECK |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 311 | }; |
| 312 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 313 | // Check that the immediate argument is actually a constant. |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 314 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 315 | return true; |
| 316 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 317 | // Range check against the upper/lower values for this isntruction. |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 318 | unsigned Val = Result.getZExtValue(); |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 319 | if (Val < l || Val > (u + l)) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 320 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 321 | << l << u+l << TheCall->getArg(i)->getSourceRange(); |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 322 | |
Nate Begeman | 99c40bb | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 323 | // FIXME: VFP Intrinsics should error if VFP not present. |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 324 | return false; |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 325 | } |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 326 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 327 | /// CheckFunctionCall - Check a direct function call for various correctness |
| 328 | /// and safety properties not strictly enforced by the C type system. |
| 329 | bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall) { |
| 330 | // Get the IdentifierInfo* for the called function. |
| 331 | IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
| 332 | |
| 333 | // None of the checks below are needed for functions that don't have |
| 334 | // simple names (e.g., C++ conversion functions). |
| 335 | if (!FnInfo) |
| 336 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 338 | // FIXME: This mechanism should be abstracted to be less fragile and |
| 339 | // more efficient. For example, just map function ids to custom |
| 340 | // handlers. |
| 341 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 342 | // Printf checking. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 343 | if (const FormatAttr *Format = FDecl->getAttr<FormatAttr>()) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 344 | const bool b = Format->getType() == "scanf"; |
| 345 | if (b || CheckablePrintfAttr(Format, TheCall)) { |
Ted Kremenek | 3d692df | 2009-02-27 17:58:43 +0000 | [diff] [blame] | 346 | bool HasVAListArg = Format->getFirstArg() == 0; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 347 | CheckPrintfScanfArguments(TheCall, HasVAListArg, |
| 348 | Format->getFormatIdx() - 1, |
| 349 | HasVAListArg ? 0 : Format->getFirstArg() - 1, |
| 350 | !b); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 351 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 352 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 354 | specific_attr_iterator<NonNullAttr> |
| 355 | i = FDecl->specific_attr_begin<NonNullAttr>(), |
| 356 | e = FDecl->specific_attr_end<NonNullAttr>(); |
| 357 | |
| 358 | for (; i != e; ++i) |
| 359 | CheckNonNullArguments(*i, TheCall); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 360 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 361 | return false; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 364 | bool Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall) { |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 365 | // Printf checking. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 366 | const FormatAttr *Format = NDecl->getAttr<FormatAttr>(); |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 367 | if (!Format) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 368 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 370 | const VarDecl *V = dyn_cast<VarDecl>(NDecl); |
| 371 | if (!V) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 372 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 373 | |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 374 | QualType Ty = V->getType(); |
| 375 | if (!Ty->isBlockPointerType()) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 376 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 378 | const bool b = Format->getType() == "scanf"; |
| 379 | if (!b && !CheckablePrintfAttr(Format, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 380 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 381 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 382 | bool HasVAListArg = Format->getFirstArg() == 0; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 383 | CheckPrintfScanfArguments(TheCall, HasVAListArg, Format->getFormatIdx() - 1, |
| 384 | HasVAListArg ? 0 : Format->getFirstArg() - 1, !b); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 385 | |
| 386 | return false; |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 389 | /// SemaBuiltinAtomicOverloaded - We have a call to a function like |
| 390 | /// __sync_fetch_and_add, which is an overloaded function based on the pointer |
| 391 | /// type of its first argument. The main ActOnCallExpr routines have already |
| 392 | /// promoted the types of arguments because all of these calls are prototyped as |
| 393 | /// void(...). |
| 394 | /// |
| 395 | /// This function goes through and does final semantic checking for these |
| 396 | /// builtins, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 397 | ExprResult |
| 398 | Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) { |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 399 | CallExpr *TheCall = (CallExpr *)TheCallResult.get(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 400 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 401 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 402 | |
| 403 | // Ensure that we have at least one argument to do type inference from. |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 404 | if (TheCall->getNumArgs() < 1) { |
| 405 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 406 | << 0 << 1 << TheCall->getNumArgs() |
| 407 | << TheCall->getCallee()->getSourceRange(); |
| 408 | return ExprError(); |
| 409 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 411 | // Inspect the first argument of the atomic builtin. This should always be |
| 412 | // a pointer type, whose element is an integral scalar or pointer type. |
| 413 | // Because it is a pointer type, we don't have to worry about any implicit |
| 414 | // casts here. |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 415 | // FIXME: We don't allow floating point scalars as input. |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 416 | Expr *FirstArg = TheCall->getArg(0); |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 417 | if (!FirstArg->getType()->isPointerType()) { |
| 418 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
| 419 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 420 | return ExprError(); |
| 421 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 423 | QualType ValType = |
| 424 | FirstArg->getType()->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | if (!ValType->isIntegerType() && !ValType->isPointerType() && |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 426 | !ValType->isBlockPointerType()) { |
| 427 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr) |
| 428 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 429 | return ExprError(); |
| 430 | } |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 431 | |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 432 | // The majority of builtins return a value, but a few have special return |
| 433 | // types, so allow them to override appropriately below. |
| 434 | QualType ResultType = ValType; |
| 435 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 436 | // We need to figure out which concrete builtin this maps onto. For example, |
| 437 | // __sync_fetch_and_add with a 2 byte object turns into |
| 438 | // __sync_fetch_and_add_2. |
| 439 | #define BUILTIN_ROW(x) \ |
| 440 | { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ |
| 441 | Builtin::BI##x##_8, Builtin::BI##x##_16 } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 442 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 443 | static const unsigned BuiltinIndices[][5] = { |
| 444 | BUILTIN_ROW(__sync_fetch_and_add), |
| 445 | BUILTIN_ROW(__sync_fetch_and_sub), |
| 446 | BUILTIN_ROW(__sync_fetch_and_or), |
| 447 | BUILTIN_ROW(__sync_fetch_and_and), |
| 448 | BUILTIN_ROW(__sync_fetch_and_xor), |
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 | BUILTIN_ROW(__sync_add_and_fetch), |
| 451 | BUILTIN_ROW(__sync_sub_and_fetch), |
| 452 | BUILTIN_ROW(__sync_and_and_fetch), |
| 453 | BUILTIN_ROW(__sync_or_and_fetch), |
| 454 | BUILTIN_ROW(__sync_xor_and_fetch), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 456 | BUILTIN_ROW(__sync_val_compare_and_swap), |
| 457 | BUILTIN_ROW(__sync_bool_compare_and_swap), |
| 458 | BUILTIN_ROW(__sync_lock_test_and_set), |
| 459 | BUILTIN_ROW(__sync_lock_release) |
| 460 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | #undef BUILTIN_ROW |
| 462 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 463 | // Determine the index of the size. |
| 464 | unsigned SizeIndex; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 465 | switch (Context.getTypeSizeInChars(ValType).getQuantity()) { |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 466 | case 1: SizeIndex = 0; break; |
| 467 | case 2: SizeIndex = 1; break; |
| 468 | case 4: SizeIndex = 2; break; |
| 469 | case 8: SizeIndex = 3; break; |
| 470 | case 16: SizeIndex = 4; break; |
| 471 | default: |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 472 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size) |
| 473 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 474 | return ExprError(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 475 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 476 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 477 | // Each of these builtins has one pointer argument, followed by some number of |
| 478 | // values (0, 1 or 2) followed by a potentially empty varags list of stuff |
| 479 | // that we ignore. Find out which row of BuiltinIndices to read from as well |
| 480 | // as the number of fixed args. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 481 | unsigned BuiltinID = FDecl->getBuiltinID(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 482 | unsigned BuiltinIndex, NumFixed = 1; |
| 483 | switch (BuiltinID) { |
| 484 | default: assert(0 && "Unknown overloaded atomic builtin!"); |
| 485 | case Builtin::BI__sync_fetch_and_add: BuiltinIndex = 0; break; |
| 486 | case Builtin::BI__sync_fetch_and_sub: BuiltinIndex = 1; break; |
| 487 | case Builtin::BI__sync_fetch_and_or: BuiltinIndex = 2; break; |
| 488 | case Builtin::BI__sync_fetch_and_and: BuiltinIndex = 3; break; |
| 489 | case Builtin::BI__sync_fetch_and_xor: BuiltinIndex = 4; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 491 | case Builtin::BI__sync_add_and_fetch: BuiltinIndex = 5; break; |
| 492 | case Builtin::BI__sync_sub_and_fetch: BuiltinIndex = 6; break; |
| 493 | case Builtin::BI__sync_and_and_fetch: BuiltinIndex = 7; break; |
| 494 | case Builtin::BI__sync_or_and_fetch: BuiltinIndex = 8; break; |
| 495 | case Builtin::BI__sync_xor_and_fetch: BuiltinIndex = 9; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 497 | case Builtin::BI__sync_val_compare_and_swap: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 498 | BuiltinIndex = 10; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 499 | NumFixed = 2; |
| 500 | break; |
| 501 | case Builtin::BI__sync_bool_compare_and_swap: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 502 | BuiltinIndex = 11; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 503 | NumFixed = 2; |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 504 | ResultType = Context.BoolTy; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 505 | break; |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 506 | case Builtin::BI__sync_lock_test_and_set: BuiltinIndex = 12; break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 507 | case Builtin::BI__sync_lock_release: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 508 | BuiltinIndex = 13; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 509 | NumFixed = 0; |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 510 | ResultType = Context.VoidTy; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 511 | break; |
| 512 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 514 | // Now that we know how many fixed arguments we expect, first check that we |
| 515 | // have at least that many. |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 516 | if (TheCall->getNumArgs() < 1+NumFixed) { |
| 517 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 518 | << 0 << 1+NumFixed << TheCall->getNumArgs() |
| 519 | << TheCall->getCallee()->getSourceRange(); |
| 520 | return ExprError(); |
| 521 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | |
Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 523 | // Get the decl for the concrete builtin from this, we can tell what the |
| 524 | // concrete integer type we should convert to is. |
| 525 | unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; |
| 526 | const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID); |
| 527 | IdentifierInfo *NewBuiltinII = PP.getIdentifierInfo(NewBuiltinName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | FunctionDecl *NewBuiltinDecl = |
Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 529 | cast<FunctionDecl>(LazilyCreateBuiltin(NewBuiltinII, NewBuiltinID, |
| 530 | TUScope, false, DRE->getLocStart())); |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 531 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 532 | // The first argument --- the pointer --- has a fixed type; we |
| 533 | // deduce the types of the rest of the arguments accordingly. Walk |
| 534 | // the remaining arguments, converting them to the deduced value type. |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 535 | for (unsigned i = 0; i != NumFixed; ++i) { |
| 536 | Expr *Arg = TheCall->getArg(i+1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 538 | // If the argument is an implicit cast, then there was a promotion due to |
| 539 | // "...", just remove it now. |
| 540 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) { |
| 541 | Arg = ICE->getSubExpr(); |
| 542 | ICE->setSubExpr(0); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 543 | TheCall->setArg(i+1, Arg); |
| 544 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 546 | // GCC does an implicit conversion to the pointer or integer ValType. This |
| 547 | // can fail in some cases (1i -> int**), check for this error case now. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 548 | CastKind Kind = CK_Unknown; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 549 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 550 | if (CheckCastTypes(Arg->getSourceRange(), ValType, Arg, Kind, BasePath)) |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 551 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 553 | // Okay, we have something that *can* be converted to the right type. Check |
| 554 | // to see if there is a potentially weird extension going on here. This can |
| 555 | // happen when you do an atomic operation on something like an char* and |
| 556 | // pass in 42. The 42 gets converted to char. This is even more strange |
| 557 | // for things like 45.123 -> char, etc. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | // FIXME: Do this check. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 559 | ImpCastExprToType(Arg, ValType, Kind, VK_RValue, &BasePath); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 560 | TheCall->setArg(i+1, Arg); |
| 561 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 563 | // Switch the DeclRefExpr to refer to the new decl. |
| 564 | DRE->setDecl(NewBuiltinDecl); |
| 565 | DRE->setType(NewBuiltinDecl->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 566 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 567 | // Set the callee in the CallExpr. |
| 568 | // FIXME: This leaks the original parens and implicit casts. |
| 569 | Expr *PromotedCall = DRE; |
| 570 | UsualUnaryConversions(PromotedCall); |
| 571 | TheCall->setCallee(PromotedCall); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 573 | // Change the result type of the call to match the original value type. This |
| 574 | // is arbitrary, but the codegen for these builtins ins design to handle it |
| 575 | // gracefully. |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 576 | TheCall->setType(ResultType); |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 577 | |
| 578 | return move(TheCallResult); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 582 | /// CheckObjCString - Checks that the argument to the builtin |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 583 | /// CFString constructor is correct |
Steve Naroff | fd94262 | 2009-04-13 20:26:29 +0000 | [diff] [blame] | 584 | /// FIXME: GCC currently emits the following warning: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 585 | /// "warning: input conversion stopped due to an input byte that does not |
Steve Naroff | fd94262 | 2009-04-13 20:26:29 +0000 | [diff] [blame] | 586 | /// belong to the input codeset UTF-8" |
| 587 | /// Note: It might also make sense to do the UTF-16 conversion here (would |
| 588 | /// simplify the backend). |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 589 | bool Sema::CheckObjCString(Expr *Arg) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 590 | Arg = Arg->IgnoreParenCasts(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 591 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); |
| 592 | |
| 593 | if (!Literal || Literal->isWide()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 594 | Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 595 | << Arg->getSourceRange(); |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 596 | return true; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 597 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 598 | |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 599 | size_t NulPos = Literal->getString().find('\0'); |
| 600 | if (NulPos != llvm::StringRef::npos) { |
| 601 | Diag(getLocationOfStringLiteralByte(Literal, NulPos), |
| 602 | diag::warn_cfstring_literal_contains_nul_character) |
| 603 | << Arg->getSourceRange(); |
Daniel Dunbar | f015b03 | 2009-09-22 10:03:52 +0000 | [diff] [blame] | 604 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 605 | |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 606 | return false; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 609 | /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity. |
| 610 | /// Emit an error and return true on failure, return false on success. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 611 | bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) { |
| 612 | Expr *Fn = TheCall->getCallee(); |
| 613 | if (TheCall->getNumArgs() > 2) { |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 614 | Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 615 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 616 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
| 617 | << Fn->getSourceRange() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 618 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 619 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 620 | return true; |
| 621 | } |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 622 | |
| 623 | if (TheCall->getNumArgs() < 2) { |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 624 | return Diag(TheCall->getLocEnd(), |
| 625 | diag::err_typecheck_call_too_few_args_at_least) |
| 626 | << 0 /*function call*/ << 2 << TheCall->getNumArgs(); |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 629 | // Determine whether the current function is variadic or not. |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 630 | BlockScopeInfo *CurBlock = getCurBlock(); |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 631 | bool isVariadic; |
Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 632 | if (CurBlock) |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 633 | isVariadic = CurBlock->TheDecl->isVariadic(); |
Ted Kremenek | 9498d38 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 634 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
| 635 | isVariadic = FD->isVariadic(); |
| 636 | else |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 637 | isVariadic = getCurMethodDecl()->isVariadic(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 639 | if (!isVariadic) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 640 | Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function); |
| 641 | return true; |
| 642 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 643 | |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 644 | // Verify that the second argument to the builtin is the last argument of the |
| 645 | // current function or method. |
| 646 | bool SecondArgIsLastNamedArgument = false; |
Anders Carlsson | e2c1410 | 2008-02-13 01:22:59 +0000 | [diff] [blame] | 647 | const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 648 | |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 649 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { |
| 650 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 651 | // FIXME: This isn't correct for methods (results in bogus warning). |
| 652 | // Get the last formal in the current function. |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 653 | const ParmVarDecl *LastArg; |
Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 654 | if (CurBlock) |
| 655 | LastArg = *(CurBlock->TheDecl->param_end()-1); |
| 656 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 657 | LastArg = *(FD->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 658 | else |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 659 | LastArg = *(getCurMethodDecl()->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 660 | SecondArgIsLastNamedArgument = PV == LastArg; |
| 661 | } |
| 662 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 663 | |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 664 | if (!SecondArgIsLastNamedArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 665 | Diag(TheCall->getArg(1)->getLocStart(), |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 666 | diag::warn_second_parameter_of_va_start_not_last_named_argument); |
| 667 | return false; |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 668 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 669 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 670 | /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and |
| 671 | /// friends. This is declared to take (...), so we have to check everything. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 672 | bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { |
| 673 | if (TheCall->getNumArgs() < 2) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 674 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 675 | << 0 << 2 << TheCall->getNumArgs()/*function call*/; |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 676 | if (TheCall->getNumArgs() > 2) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 677 | return Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 678 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 679 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 680 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
| 681 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 682 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 683 | Expr *OrigArg0 = TheCall->getArg(0); |
| 684 | Expr *OrigArg1 = TheCall->getArg(1); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 685 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 686 | // Do standard promotions between the two arguments, returning their common |
| 687 | // type. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 688 | QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); |
Daniel Dunbar | 403bc2b | 2009-02-19 19:28:43 +0000 | [diff] [blame] | 689 | |
| 690 | // Make sure any conversions are pushed back into the call; this is |
| 691 | // type safe since unordered compare builtins are declared as "_Bool |
| 692 | // foo(...)". |
| 693 | TheCall->setArg(0, OrigArg0); |
| 694 | TheCall->setArg(1, OrigArg1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 695 | |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 696 | if (OrigArg0->isTypeDependent() || OrigArg1->isTypeDependent()) |
| 697 | return false; |
| 698 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 699 | // If the common type isn't a real floating type, then the arguments were |
| 700 | // invalid for this operation. |
| 701 | if (!Res->isRealFloatingType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 702 | return Diag(OrigArg0->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 703 | diag::err_typecheck_call_invalid_ordered_compare) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 704 | << OrigArg0->getType() << OrigArg1->getType() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 705 | << SourceRange(OrigArg0->getLocStart(), OrigArg1->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 706 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 707 | return false; |
| 708 | } |
| 709 | |
Benjamin Kramer | e771a7a | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 710 | /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like |
| 711 | /// __builtin_isnan and friends. This is declared to take (...), so we have |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 712 | /// to check everything. We expect the last argument to be a floating point |
| 713 | /// value. |
| 714 | bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) { |
| 715 | if (TheCall->getNumArgs() < NumArgs) |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 716 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 717 | << 0 << NumArgs << TheCall->getNumArgs()/*function call*/; |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 718 | if (TheCall->getNumArgs() > NumArgs) |
| 719 | return Diag(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 720 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 721 | << 0 /*function call*/ << NumArgs << TheCall->getNumArgs() |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 722 | << SourceRange(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 723 | (*(TheCall->arg_end()-1))->getLocEnd()); |
| 724 | |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 725 | Expr *OrigArg = TheCall->getArg(NumArgs-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 726 | |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 727 | if (OrigArg->isTypeDependent()) |
| 728 | return false; |
| 729 | |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 730 | // This operation requires a non-_Complex floating-point number. |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 731 | if (!OrigArg->getType()->isRealFloatingType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 732 | return Diag(OrigArg->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 733 | diag::err_typecheck_call_invalid_unary_fp) |
| 734 | << OrigArg->getType() << OrigArg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 735 | |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 736 | // If this is an implicit conversion from float -> double, remove it. |
| 737 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) { |
| 738 | Expr *CastArg = Cast->getSubExpr(); |
| 739 | if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) { |
| 740 | assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) && |
| 741 | "promotion from float to double is the only expected cast here"); |
| 742 | Cast->setSubExpr(0); |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 743 | TheCall->setArg(NumArgs-1, CastArg); |
| 744 | OrigArg = CastArg; |
| 745 | } |
| 746 | } |
| 747 | |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 748 | return false; |
| 749 | } |
| 750 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 751 | /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. |
| 752 | // This is declared to take (...), so we have to check everything. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 753 | ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 754 | if (TheCall->getNumArgs() < 2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 755 | return ExprError(Diag(TheCall->getLocEnd(), |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 756 | diag::err_typecheck_call_too_few_args_at_least) |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 757 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 758 | << TheCall->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 759 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 760 | // Determine which of the following types of shufflevector we're checking: |
| 761 | // 1) unary, vector mask: (lhs, mask) |
| 762 | // 2) binary, vector mask: (lhs, rhs, mask) |
| 763 | // 3) binary, scalar mask: (lhs, rhs, index, ..., index) |
| 764 | QualType resType = TheCall->getArg(0)->getType(); |
| 765 | unsigned numElements = 0; |
| 766 | |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 767 | if (!TheCall->getArg(0)->isTypeDependent() && |
| 768 | !TheCall->getArg(1)->isTypeDependent()) { |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 769 | QualType LHSType = TheCall->getArg(0)->getType(); |
| 770 | QualType RHSType = TheCall->getArg(1)->getType(); |
| 771 | |
| 772 | if (!LHSType->isVectorType() || !RHSType->isVectorType()) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 773 | Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 775 | TheCall->getArg(1)->getLocEnd()); |
| 776 | return ExprError(); |
| 777 | } |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 778 | |
| 779 | numElements = LHSType->getAs<VectorType>()->getNumElements(); |
| 780 | unsigned numResElements = TheCall->getNumArgs() - 2; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 781 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 782 | // Check to see if we have a call with 2 vector arguments, the unary shuffle |
| 783 | // with mask. If so, verify that RHS is an integer vector type with the |
| 784 | // same number of elts as lhs. |
| 785 | if (TheCall->getNumArgs() == 2) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 786 | if (!RHSType->hasIntegerRepresentation() || |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 787 | RHSType->getAs<VectorType>()->getNumElements() != numElements) |
| 788 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
| 789 | << SourceRange(TheCall->getArg(1)->getLocStart(), |
| 790 | TheCall->getArg(1)->getLocEnd()); |
| 791 | numResElements = numElements; |
| 792 | } |
| 793 | else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 794 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 795 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 796 | TheCall->getArg(1)->getLocEnd()); |
| 797 | return ExprError(); |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 798 | } else if (numElements != numResElements) { |
| 799 | QualType eltType = LHSType->getAs<VectorType>()->getElementType(); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 800 | resType = Context.getVectorType(eltType, numResElements, |
| 801 | VectorType::NotAltiVec); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 802 | } |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 806 | if (TheCall->getArg(i)->isTypeDependent() || |
| 807 | TheCall->getArg(i)->isValueDependent()) |
| 808 | continue; |
| 809 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 810 | llvm::APSInt Result(32); |
| 811 | if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) |
| 812 | return ExprError(Diag(TheCall->getLocStart(), |
| 813 | diag::err_shufflevector_nonconstant_argument) |
| 814 | << TheCall->getArg(i)->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 815 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 816 | if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 817 | return ExprError(Diag(TheCall->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 818 | diag::err_shufflevector_argument_too_large) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 819 | << TheCall->getArg(i)->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | llvm::SmallVector<Expr*, 32> exprs; |
| 823 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 824 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 825 | exprs.push_back(TheCall->getArg(i)); |
| 826 | TheCall->setArg(i, 0); |
| 827 | } |
| 828 | |
Nate Begeman | a88dc30 | 2009-08-12 02:10:25 +0000 | [diff] [blame] | 829 | return Owned(new (Context) ShuffleVectorExpr(Context, exprs.begin(), |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 830 | exprs.size(), resType, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 831 | TheCall->getCallee()->getLocStart(), |
| 832 | TheCall->getRParenLoc())); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 833 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 834 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 835 | /// SemaBuiltinPrefetch - Handle __builtin_prefetch. |
| 836 | // This is declared to take (const void*, ...) and can take two |
| 837 | // optional constant int args. |
| 838 | bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 839 | unsigned NumArgs = TheCall->getNumArgs(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 840 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 841 | if (NumArgs > 3) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 842 | return Diag(TheCall->getLocEnd(), |
| 843 | diag::err_typecheck_call_too_many_args_at_most) |
| 844 | << 0 /*function call*/ << 3 << NumArgs |
| 845 | << TheCall->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 846 | |
| 847 | // Argument 0 is checked for us and the remaining arguments must be |
| 848 | // constant integers. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 849 | for (unsigned i = 1; i != NumArgs; ++i) { |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 850 | Expr *Arg = TheCall->getArg(i); |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 851 | |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 852 | llvm::APSInt Result; |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 853 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 854 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 855 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 856 | // FIXME: gcc issues a warning and rewrites these to 0. These |
| 857 | // seems especially odd for the third argument since the default |
| 858 | // is 3. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 859 | if (i == 1) { |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 860 | if (Result.getLimitedValue() > 1) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 861 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 862 | << "0" << "1" << Arg->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 863 | } else { |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 864 | if (Result.getLimitedValue() > 3) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 865 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 866 | << "0" << "3" << Arg->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 870 | return false; |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 873 | /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr |
| 874 | /// TheCall is a constant expression. |
| 875 | bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum, |
| 876 | llvm::APSInt &Result) { |
| 877 | Expr *Arg = TheCall->getArg(ArgNum); |
| 878 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 879 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 880 | |
| 881 | if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; |
| 882 | |
| 883 | if (!Arg->isIntegerConstantExpr(Result, Context)) |
| 884 | return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type) |
Eric Christopher | 5e89655 | 2010-04-19 18:23:02 +0000 | [diff] [blame] | 885 | << FDecl->getDeclName() << Arg->getSourceRange(); |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 886 | |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 887 | return false; |
| 888 | } |
| 889 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 890 | /// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr, |
| 891 | /// int type). This simply type checks that type is one of the defined |
| 892 | /// constants (0-3). |
Eric Christopher | fee667f | 2009-12-23 03:49:37 +0000 | [diff] [blame] | 893 | // For compatability check 0-3, llvm only handles 0 and 2. |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 894 | bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) { |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 895 | llvm::APSInt Result; |
| 896 | |
| 897 | // Check constant-ness first. |
| 898 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 899 | return true; |
| 900 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 901 | Expr *Arg = TheCall->getArg(1); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 902 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 903 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 904 | << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | return false; |
| 908 | } |
| 909 | |
Eli Friedman | 586d6a8 | 2009-05-03 06:04:26 +0000 | [diff] [blame] | 910 | /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 911 | /// This checks that val is a constant 1. |
| 912 | bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { |
| 913 | Expr *Arg = TheCall->getArg(1); |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 914 | llvm::APSInt Result; |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 915 | |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 916 | // TODO: This is less than ideal. Overload this to take a value. |
| 917 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 918 | return true; |
| 919 | |
| 920 | if (Result != 1) |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 921 | return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val) |
| 922 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
| 923 | |
| 924 | return false; |
| 925 | } |
| 926 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 927 | // Handle i > 1 ? "x" : "y", recursivelly |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 928 | bool Sema::SemaCheckStringLiteral(const Expr *E, const CallExpr *TheCall, |
| 929 | bool HasVAListArg, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 930 | unsigned format_idx, unsigned firstDataArg, |
| 931 | bool isPrintf) { |
| 932 | |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 933 | if (E->isTypeDependent() || E->isValueDependent()) |
| 934 | return false; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 935 | |
| 936 | switch (E->getStmtClass()) { |
| 937 | case Stmt::ConditionalOperatorClass: { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 938 | const ConditionalOperator *C = cast<ConditionalOperator>(E); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 939 | return SemaCheckStringLiteral(C->getTrueExpr(), TheCall, HasVAListArg, |
| 940 | format_idx, firstDataArg, isPrintf) |
| 941 | && SemaCheckStringLiteral(C->getRHS(), TheCall, HasVAListArg, |
| 942 | format_idx, firstDataArg, isPrintf); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | case Stmt::ImplicitCastExprClass: { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 946 | const ImplicitCastExpr *Expr = cast<ImplicitCastExpr>(E); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 947 | return SemaCheckStringLiteral(Expr->getSubExpr(), TheCall, HasVAListArg, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 948 | format_idx, firstDataArg, isPrintf); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | case Stmt::ParenExprClass: { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 952 | const ParenExpr *Expr = cast<ParenExpr>(E); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 953 | return SemaCheckStringLiteral(Expr->getSubExpr(), TheCall, HasVAListArg, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 954 | format_idx, firstDataArg, isPrintf); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 955 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 956 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 957 | case Stmt::DeclRefExprClass: { |
| 958 | const DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 959 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 960 | // As an exception, do not flag errors for variables binding to |
| 961 | // const string literals. |
| 962 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 963 | bool isConstant = false; |
| 964 | QualType T = DR->getType(); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 965 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 966 | if (const ArrayType *AT = Context.getAsArrayType(T)) { |
| 967 | isConstant = AT->getElementType().isConstant(Context); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 968 | } else if (const PointerType *PT = T->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | isConstant = T.isConstant(Context) && |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 970 | PT->getPointeeType().isConstant(Context); |
| 971 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 973 | if (isConstant) { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 974 | if (const Expr *Init = VD->getAnyInitializer()) |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 975 | return SemaCheckStringLiteral(Init, TheCall, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 976 | HasVAListArg, format_idx, firstDataArg, |
| 977 | isPrintf); |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 978 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | |
Anders Carlsson | d966a55 | 2009-06-28 19:55:58 +0000 | [diff] [blame] | 980 | // For vprintf* functions (i.e., HasVAListArg==true), we add a |
| 981 | // special check to see if the format string is a function parameter |
| 982 | // of the function calling the printf function. If the function |
| 983 | // has an attribute indicating it is a printf-like function, then we |
| 984 | // should suppress warnings concerning non-literals being used in a call |
| 985 | // to a vprintf function. For example: |
| 986 | // |
| 987 | // void |
| 988 | // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){ |
| 989 | // va_list ap; |
| 990 | // va_start(ap, fmt); |
| 991 | // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". |
| 992 | // ... |
| 993 | // |
| 994 | // |
| 995 | // FIXME: We don't have full attribute support yet, so just check to see |
| 996 | // if the argument is a DeclRefExpr that references a parameter. We'll |
| 997 | // add proper support for checking the attribute later. |
| 998 | if (HasVAListArg) |
| 999 | if (isa<ParmVarDecl>(VD)) |
| 1000 | return true; |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1001 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1002 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1003 | return false; |
| 1004 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1005 | |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1006 | case Stmt::CallExprClass: { |
| 1007 | const CallExpr *CE = cast<CallExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1008 | if (const ImplicitCastExpr *ICE |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1009 | = dyn_cast<ImplicitCastExpr>(CE->getCallee())) { |
| 1010 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) { |
| 1011 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1012 | if (const FormatArgAttr *FA = FD->getAttr<FormatArgAttr>()) { |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1013 | unsigned ArgIndex = FA->getFormatIdx(); |
| 1014 | const Expr *Arg = CE->getArg(ArgIndex - 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1015 | |
| 1016 | return SemaCheckStringLiteral(Arg, TheCall, HasVAListArg, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1017 | format_idx, firstDataArg, isPrintf); |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1023 | return false; |
| 1024 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1025 | case Stmt::ObjCStringLiteralClass: |
| 1026 | case Stmt::StringLiteralClass: { |
| 1027 | const StringLiteral *StrE = NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1029 | if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1030 | StrE = ObjCFExpr->getString(); |
| 1031 | else |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1032 | StrE = cast<StringLiteral>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1033 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1034 | if (StrE) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1035 | CheckFormatString(StrE, E, TheCall, HasVAListArg, format_idx, |
| 1036 | firstDataArg, isPrintf); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1037 | return true; |
| 1038 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1039 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1040 | return false; |
| 1041 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1042 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1043 | default: |
| 1044 | return false; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1045 | } |
| 1046 | } |
| 1047 | |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1048 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1049 | Sema::CheckNonNullArguments(const NonNullAttr *NonNull, |
| 1050 | const CallExpr *TheCall) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1051 | for (NonNullAttr::args_iterator i = NonNull->args_begin(), |
| 1052 | e = NonNull->args_end(); |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1053 | i != e; ++i) { |
Chris Lattner | 12b97ff | 2009-05-25 18:23:36 +0000 | [diff] [blame] | 1054 | const Expr *ArgExpr = TheCall->getArg(*i); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1055 | if (ArgExpr->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1056 | Expr::NPC_ValueDependentIsNotNull)) |
Chris Lattner | 12b97ff | 2009-05-25 18:23:36 +0000 | [diff] [blame] | 1057 | Diag(TheCall->getCallee()->getLocStart(), diag::warn_null_arg) |
| 1058 | << ArgExpr->getSourceRange(); |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1059 | } |
| 1060 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1061 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1062 | /// CheckPrintfScanfArguments - Check calls to printf and scanf (and similar |
| 1063 | /// functions) for correct use of format strings. |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1064 | void |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1065 | Sema::CheckPrintfScanfArguments(const CallExpr *TheCall, bool HasVAListArg, |
| 1066 | unsigned format_idx, unsigned firstDataArg, |
| 1067 | bool isPrintf) { |
| 1068 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1069 | const Expr *Fn = TheCall->getCallee(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1070 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1071 | // The way the format attribute works in GCC, the implicit this argument |
| 1072 | // of member functions is counted. However, it doesn't appear in our own |
| 1073 | // lists, so decrement format_idx in that case. |
| 1074 | if (isa<CXXMemberCallExpr>(TheCall)) { |
| 1075 | // Catch a format attribute mistakenly referring to the object argument. |
| 1076 | if (format_idx == 0) |
| 1077 | return; |
| 1078 | --format_idx; |
| 1079 | if(firstDataArg != 0) |
| 1080 | --firstDataArg; |
| 1081 | } |
| 1082 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1083 | // CHECK: printf/scanf-like function is called with no format string. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1084 | if (format_idx >= TheCall->getNumArgs()) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1085 | Diag(TheCall->getRParenLoc(), diag::warn_missing_format_string) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1086 | << Fn->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1087 | return; |
| 1088 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1089 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1090 | const Expr *OrigFormatExpr = TheCall->getArg(format_idx)->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1091 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1092 | // CHECK: format string is not a string literal. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1093 | // |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1094 | // Dynamically generated format strings are difficult to |
| 1095 | // automatically vet at compile time. Requiring that format strings |
| 1096 | // are string literals: (1) permits the checking of format strings by |
| 1097 | // the compiler and thereby (2) can practically remove the source of |
| 1098 | // many format string exploits. |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1099 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1100 | // Format string can be either ObjC string (e.g. @"%d") or |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1101 | // C string (e.g. "%d") |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1102 | // 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] | 1103 | // the same format string checking logic for both ObjC and C strings. |
Chris Lattner | 1cd3e1f | 2009-04-29 04:49:34 +0000 | [diff] [blame] | 1104 | if (SemaCheckStringLiteral(OrigFormatExpr, TheCall, HasVAListArg, format_idx, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1105 | firstDataArg, isPrintf)) |
Chris Lattner | 1cd3e1f | 2009-04-29 04:49:34 +0000 | [diff] [blame] | 1106 | return; // Literal format string found, check done! |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1107 | |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1108 | // If there are no arguments specified, warn with -Wformat-security, otherwise |
| 1109 | // warn only with -Wformat-nonliteral. |
| 1110 | if (TheCall->getNumArgs() == format_idx+1) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | Diag(TheCall->getArg(format_idx)->getLocStart(), |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1112 | diag::warn_format_nonliteral_noargs) |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1113 | << OrigFormatExpr->getSourceRange(); |
| 1114 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1115 | Diag(TheCall->getArg(format_idx)->getLocStart(), |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1116 | diag::warn_format_nonliteral) |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1117 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1118 | } |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1119 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1120 | namespace { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1121 | class CheckFormatHandler : public analyze_format_string::FormatStringHandler { |
| 1122 | protected: |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1123 | Sema &S; |
| 1124 | const StringLiteral *FExpr; |
| 1125 | const Expr *OrigFormatExpr; |
Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1126 | const unsigned FirstDataArg; |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1127 | const unsigned NumDataArgs; |
| 1128 | const bool IsObjCLiteral; |
| 1129 | const char *Beg; // Start of format string. |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1130 | const bool HasVAListArg; |
| 1131 | const CallExpr *TheCall; |
| 1132 | unsigned FormatIdx; |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1133 | llvm::BitVector CoveredArgs; |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1134 | bool usesPositionalArgs; |
| 1135 | bool atFirstArg; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1136 | public: |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1137 | CheckFormatHandler(Sema &s, const StringLiteral *fexpr, |
Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1138 | const Expr *origFormatExpr, unsigned firstDataArg, |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1139 | unsigned numDataArgs, bool isObjCLiteral, |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1140 | const char *beg, bool hasVAListArg, |
| 1141 | const CallExpr *theCall, unsigned formatIdx) |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1142 | : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), |
Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1143 | FirstDataArg(firstDataArg), |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1144 | NumDataArgs(numDataArgs), |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1145 | IsObjCLiteral(isObjCLiteral), Beg(beg), |
| 1146 | HasVAListArg(hasVAListArg), |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1147 | TheCall(theCall), FormatIdx(formatIdx), |
| 1148 | usesPositionalArgs(false), atFirstArg(true) { |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1149 | CoveredArgs.resize(numDataArgs); |
| 1150 | CoveredArgs.reset(); |
| 1151 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1152 | |
Ted Kremenek | 07d161f | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 1153 | void DoneProcessing(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1154 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1155 | void HandleIncompleteSpecifier(const char *startSpecifier, |
| 1156 | unsigned specifierLen); |
| 1157 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1158 | virtual void HandleInvalidPosition(const char *startSpecifier, |
| 1159 | unsigned specifierLen, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1160 | analyze_format_string::PositionContext p); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1161 | |
| 1162 | virtual void HandleZeroPosition(const char *startPos, unsigned posLen); |
| 1163 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1164 | void HandleNullChar(const char *nullCharacter); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1165 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1166 | protected: |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1167 | bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, |
| 1168 | const char *startSpec, |
| 1169 | unsigned specifierLen, |
| 1170 | const char *csStart, unsigned csLen); |
| 1171 | |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 1172 | SourceRange getFormatStringRange(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1173 | CharSourceRange getSpecifierRange(const char *startSpecifier, |
| 1174 | unsigned specifierLen); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1175 | SourceLocation getLocationOfByte(const char *x); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1176 | |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1177 | const Expr *getDataArg(unsigned i) const; |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 1178 | |
| 1179 | bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, |
| 1180 | const analyze_format_string::ConversionSpecifier &CS, |
| 1181 | const char *startSpecifier, unsigned specifierLen, |
| 1182 | unsigned argIndex); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1183 | }; |
| 1184 | } |
| 1185 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1186 | SourceRange CheckFormatHandler::getFormatStringRange() { |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1187 | return OrigFormatExpr->getSourceRange(); |
| 1188 | } |
| 1189 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1190 | CharSourceRange CheckFormatHandler:: |
| 1191 | getSpecifierRange(const char *startSpecifier, unsigned specifierLen) { |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 1192 | SourceLocation Start = getLocationOfByte(startSpecifier); |
| 1193 | SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1); |
| 1194 | |
| 1195 | // Advance the end SourceLocation by one due to half-open ranges. |
| 1196 | End = End.getFileLocWithOffset(1); |
| 1197 | |
| 1198 | return CharSourceRange::getCharRange(Start, End); |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1201 | SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1202 | return S.getLocationOfStringLiteralByte(FExpr, x - Beg); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1205 | void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, |
| 1206 | unsigned specifierLen){ |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 1207 | SourceLocation Loc = getLocationOfByte(startSpecifier); |
| 1208 | S.Diag(Loc, diag::warn_printf_incomplete_specifier) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1209 | << getSpecifierRange(startSpecifier, specifierLen); |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1212 | void |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1213 | CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen, |
| 1214 | analyze_format_string::PositionContext p) { |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1215 | SourceLocation Loc = getLocationOfByte(startPos); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1216 | S.Diag(Loc, diag::warn_format_invalid_positional_specifier) |
| 1217 | << (unsigned) p << getSpecifierRange(startPos, posLen); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1220 | void CheckFormatHandler::HandleZeroPosition(const char *startPos, |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1221 | unsigned posLen) { |
| 1222 | SourceLocation Loc = getLocationOfByte(startPos); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1223 | S.Diag(Loc, diag::warn_format_zero_positional_specifier) |
| 1224 | << getSpecifierRange(startPos, posLen); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1227 | void CheckFormatHandler::HandleNullChar(const char *nullCharacter) { |
| 1228 | // The presence of a null character is likely an error. |
| 1229 | S.Diag(getLocationOfByte(nullCharacter), |
| 1230 | diag::warn_printf_format_string_contains_null_char) |
| 1231 | << getFormatStringRange(); |
| 1232 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1233 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1234 | const Expr *CheckFormatHandler::getDataArg(unsigned i) const { |
| 1235 | return TheCall->getArg(FirstDataArg + i); |
| 1236 | } |
| 1237 | |
| 1238 | void CheckFormatHandler::DoneProcessing() { |
| 1239 | // Does the number of data arguments exceed the number of |
| 1240 | // format conversions in the format string? |
| 1241 | if (!HasVAListArg) { |
| 1242 | // Find any arguments that weren't covered. |
| 1243 | CoveredArgs.flip(); |
| 1244 | signed notCoveredArg = CoveredArgs.find_first(); |
| 1245 | if (notCoveredArg >= 0) { |
| 1246 | assert((unsigned)notCoveredArg < NumDataArgs); |
| 1247 | S.Diag(getDataArg((unsigned) notCoveredArg)->getLocStart(), |
| 1248 | diag::warn_printf_data_arg_not_used) |
| 1249 | << getFormatStringRange(); |
| 1250 | } |
| 1251 | } |
| 1252 | } |
| 1253 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1254 | bool |
| 1255 | CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, |
| 1256 | SourceLocation Loc, |
| 1257 | const char *startSpec, |
| 1258 | unsigned specifierLen, |
| 1259 | const char *csStart, |
| 1260 | unsigned csLen) { |
| 1261 | |
| 1262 | bool keepGoing = true; |
| 1263 | if (argIndex < NumDataArgs) { |
| 1264 | // Consider the argument coverered, even though the specifier doesn't |
| 1265 | // make sense. |
| 1266 | CoveredArgs.set(argIndex); |
| 1267 | } |
| 1268 | else { |
| 1269 | // If argIndex exceeds the number of data arguments we |
| 1270 | // don't issue a warning because that is just a cascade of warnings (and |
| 1271 | // they may have intended '%%' anyway). We don't want to continue processing |
| 1272 | // the format string after this point, however, as we will like just get |
| 1273 | // gibberish when trying to match arguments. |
| 1274 | keepGoing = false; |
| 1275 | } |
| 1276 | |
| 1277 | S.Diag(Loc, diag::warn_format_invalid_conversion) |
| 1278 | << llvm::StringRef(csStart, csLen) |
| 1279 | << getSpecifierRange(startSpec, specifierLen); |
| 1280 | |
| 1281 | return keepGoing; |
| 1282 | } |
| 1283 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 1284 | bool |
| 1285 | CheckFormatHandler::CheckNumArgs( |
| 1286 | const analyze_format_string::FormatSpecifier &FS, |
| 1287 | const analyze_format_string::ConversionSpecifier &CS, |
| 1288 | const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { |
| 1289 | |
| 1290 | if (argIndex >= NumDataArgs) { |
| 1291 | if (FS.usesPositionalArg()) { |
| 1292 | S.Diag(getLocationOfByte(CS.getStart()), |
| 1293 | diag::warn_printf_positional_arg_exceeds_data_args) |
| 1294 | << (argIndex+1) << NumDataArgs |
| 1295 | << getSpecifierRange(startSpecifier, specifierLen); |
| 1296 | } |
| 1297 | else { |
| 1298 | S.Diag(getLocationOfByte(CS.getStart()), |
| 1299 | diag::warn_printf_insufficient_data_args) |
| 1300 | << getSpecifierRange(startSpecifier, specifierLen); |
| 1301 | } |
| 1302 | |
| 1303 | return false; |
| 1304 | } |
| 1305 | return true; |
| 1306 | } |
| 1307 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1308 | //===--- CHECK: Printf format string checking ------------------------------===// |
| 1309 | |
| 1310 | namespace { |
| 1311 | class CheckPrintfHandler : public CheckFormatHandler { |
| 1312 | public: |
| 1313 | CheckPrintfHandler(Sema &s, const StringLiteral *fexpr, |
| 1314 | const Expr *origFormatExpr, unsigned firstDataArg, |
| 1315 | unsigned numDataArgs, bool isObjCLiteral, |
| 1316 | const char *beg, bool hasVAListArg, |
| 1317 | const CallExpr *theCall, unsigned formatIdx) |
| 1318 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
| 1319 | numDataArgs, isObjCLiteral, beg, hasVAListArg, |
| 1320 | theCall, formatIdx) {} |
| 1321 | |
| 1322 | |
| 1323 | bool HandleInvalidPrintfConversionSpecifier( |
| 1324 | const analyze_printf::PrintfSpecifier &FS, |
| 1325 | const char *startSpecifier, |
| 1326 | unsigned specifierLen); |
| 1327 | |
| 1328 | bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, |
| 1329 | const char *startSpecifier, |
| 1330 | unsigned specifierLen); |
| 1331 | |
| 1332 | bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, |
| 1333 | const char *startSpecifier, unsigned specifierLen); |
| 1334 | void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, |
| 1335 | const analyze_printf::OptionalAmount &Amt, |
| 1336 | unsigned type, |
| 1337 | const char *startSpecifier, unsigned specifierLen); |
| 1338 | void HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
| 1339 | const analyze_printf::OptionalFlag &flag, |
| 1340 | const char *startSpecifier, unsigned specifierLen); |
| 1341 | void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, |
| 1342 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 1343 | const analyze_printf::OptionalFlag &flag, |
| 1344 | const char *startSpecifier, unsigned specifierLen); |
| 1345 | }; |
| 1346 | } |
| 1347 | |
| 1348 | bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( |
| 1349 | const analyze_printf::PrintfSpecifier &FS, |
| 1350 | const char *startSpecifier, |
| 1351 | unsigned specifierLen) { |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1352 | const analyze_printf::PrintfConversionSpecifier &CS = |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1353 | FS.getConversionSpecifier(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1354 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1355 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 1356 | getLocationOfByte(CS.getStart()), |
| 1357 | startSpecifier, specifierLen, |
| 1358 | CS.getStart(), CS.getLength()); |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1361 | bool CheckPrintfHandler::HandleAmount( |
| 1362 | const analyze_format_string::OptionalAmount &Amt, |
| 1363 | unsigned k, const char *startSpecifier, |
| 1364 | unsigned specifierLen) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1365 | |
| 1366 | if (Amt.hasDataArgument()) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1367 | if (!HasVAListArg) { |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1368 | unsigned argIndex = Amt.getArgIndex(); |
| 1369 | if (argIndex >= NumDataArgs) { |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1370 | S.Diag(getLocationOfByte(Amt.getStart()), |
| 1371 | diag::warn_printf_asterisk_missing_arg) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1372 | << k << getSpecifierRange(startSpecifier, specifierLen); |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1373 | // Don't do any more checking. We will just emit |
| 1374 | // spurious errors. |
| 1375 | return false; |
| 1376 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1377 | |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1378 | // Type check the data argument. It should be an 'int'. |
Ted Kremenek | 31f8e32 | 2010-01-29 23:32:22 +0000 | [diff] [blame] | 1379 | // Although not in conformance with C99, we also allow the argument to be |
| 1380 | // an 'unsigned int' as that is a reasonably safe case. GCC also |
| 1381 | // doesn't emit a warning for that case. |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1382 | CoveredArgs.set(argIndex); |
| 1383 | const Expr *Arg = getDataArg(argIndex); |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1384 | QualType T = Arg->getType(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1385 | |
| 1386 | const analyze_printf::ArgTypeResult &ATR = Amt.getArgType(S.Context); |
| 1387 | assert(ATR.isValid()); |
| 1388 | |
| 1389 | if (!ATR.matchesType(S.Context, T)) { |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1390 | S.Diag(getLocationOfByte(Amt.getStart()), |
| 1391 | diag::warn_printf_asterisk_wrong_type) |
| 1392 | << k |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1393 | << ATR.getRepresentativeType(S.Context) << T |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1394 | << getSpecifierRange(startSpecifier, specifierLen) |
Ted Kremenek | d635c5f | 2010-01-30 00:49:51 +0000 | [diff] [blame] | 1395 | << Arg->getSourceRange(); |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1396 | // Don't do any more checking. We will just emit |
| 1397 | // spurious errors. |
| 1398 | return false; |
| 1399 | } |
| 1400 | } |
| 1401 | } |
| 1402 | return true; |
| 1403 | } |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1404 | |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1405 | void CheckPrintfHandler::HandleInvalidAmount( |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1406 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1407 | const analyze_printf::OptionalAmount &Amt, |
| 1408 | unsigned type, |
| 1409 | const char *startSpecifier, |
| 1410 | unsigned specifierLen) { |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1411 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 1412 | FS.getConversionSpecifier(); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1413 | switch (Amt.getHowSpecified()) { |
| 1414 | case analyze_printf::OptionalAmount::Constant: |
| 1415 | S.Diag(getLocationOfByte(Amt.getStart()), |
| 1416 | diag::warn_printf_nonsensical_optional_amount) |
| 1417 | << type |
| 1418 | << CS.toString() |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1419 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1420 | << FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1421 | Amt.getConstantLength())); |
| 1422 | break; |
| 1423 | |
| 1424 | default: |
| 1425 | S.Diag(getLocationOfByte(Amt.getStart()), |
| 1426 | diag::warn_printf_nonsensical_optional_amount) |
| 1427 | << type |
| 1428 | << CS.toString() |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1429 | << getSpecifierRange(startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1430 | break; |
| 1431 | } |
| 1432 | } |
| 1433 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1434 | void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1435 | const analyze_printf::OptionalFlag &flag, |
| 1436 | const char *startSpecifier, |
| 1437 | unsigned specifierLen) { |
| 1438 | // Warn about pointless flag with a fixit removal. |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1439 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 1440 | FS.getConversionSpecifier(); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1441 | S.Diag(getLocationOfByte(flag.getPosition()), |
| 1442 | diag::warn_printf_nonsensical_flag) |
| 1443 | << flag.toString() << CS.toString() |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1444 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1445 | << FixItHint::CreateRemoval(getSpecifierRange(flag.getPosition(), 1)); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | void CheckPrintfHandler::HandleIgnoredFlag( |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1449 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1450 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 1451 | const analyze_printf::OptionalFlag &flag, |
| 1452 | const char *startSpecifier, |
| 1453 | unsigned specifierLen) { |
| 1454 | // Warn about ignored flag with a fixit removal. |
| 1455 | S.Diag(getLocationOfByte(ignoredFlag.getPosition()), |
| 1456 | diag::warn_printf_ignored_flag) |
| 1457 | << ignoredFlag.toString() << flag.toString() |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1458 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1459 | << FixItHint::CreateRemoval(getSpecifierRange( |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1460 | ignoredFlag.getPosition(), 1)); |
| 1461 | } |
| 1462 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1463 | bool |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1464 | CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier |
Ted Kremenek | 5c41ee8 | 2010-02-11 09:27:41 +0000 | [diff] [blame] | 1465 | &FS, |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1466 | const char *startSpecifier, |
| 1467 | unsigned specifierLen) { |
| 1468 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1469 | using namespace analyze_format_string; |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1470 | using namespace analyze_printf; |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1471 | const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1472 | |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 1473 | if (FS.consumesDataArgument()) { |
| 1474 | if (atFirstArg) { |
| 1475 | atFirstArg = false; |
| 1476 | usesPositionalArgs = FS.usesPositionalArg(); |
| 1477 | } |
| 1478 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
| 1479 | // Cannot mix-and-match positional and non-positional arguments. |
| 1480 | S.Diag(getLocationOfByte(CS.getStart()), |
| 1481 | diag::warn_format_mix_positional_nonpositional_args) |
| 1482 | << getSpecifierRange(startSpecifier, specifierLen); |
| 1483 | return false; |
| 1484 | } |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1485 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1486 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1487 | // First check if the field width, precision, and conversion specifier |
| 1488 | // have matching data arguments. |
| 1489 | if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, |
| 1490 | startSpecifier, specifierLen)) { |
| 1491 | return false; |
| 1492 | } |
| 1493 | |
| 1494 | if (!HandleAmount(FS.getPrecision(), /* precision */ 1, |
| 1495 | startSpecifier, specifierLen)) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1496 | return false; |
| 1497 | } |
| 1498 | |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 1499 | if (!CS.consumesDataArgument()) { |
| 1500 | // FIXME: Technically specifying a precision or field width here |
| 1501 | // makes no sense. Worth issuing a warning at some point. |
Ted Kremenek | 0e5675d | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 1502 | return true; |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 1503 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1504 | |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1505 | // Consume the argument. |
| 1506 | unsigned argIndex = FS.getArgIndex(); |
Ted Kremenek | e3fc547 | 2010-02-27 08:34:51 +0000 | [diff] [blame] | 1507 | if (argIndex < NumDataArgs) { |
| 1508 | // The check to see if the argIndex is valid will come later. |
| 1509 | // We set the bit here because we may exit early from this |
| 1510 | // function if we encounter some other error. |
| 1511 | CoveredArgs.set(argIndex); |
| 1512 | } |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1513 | |
| 1514 | // Check for using an Objective-C specific conversion specifier |
| 1515 | // in a non-ObjC literal. |
| 1516 | if (!IsObjCLiteral && CS.isObjCArg()) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1517 | return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, |
| 1518 | specifierLen); |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1519 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1520 | |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1521 | // Check for invalid use of field width |
| 1522 | if (!FS.hasValidFieldWidth()) { |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 1523 | HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1524 | startSpecifier, specifierLen); |
| 1525 | } |
| 1526 | |
| 1527 | // Check for invalid use of precision |
| 1528 | if (!FS.hasValidPrecision()) { |
| 1529 | HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, |
| 1530 | startSpecifier, specifierLen); |
| 1531 | } |
| 1532 | |
| 1533 | // Check each flag does not conflict with any other component. |
| 1534 | if (!FS.hasValidLeadingZeros()) |
| 1535 | HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); |
| 1536 | if (!FS.hasValidPlusPrefix()) |
| 1537 | HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 1538 | if (!FS.hasValidSpacePrefix()) |
| 1539 | HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1540 | if (!FS.hasValidAlternativeForm()) |
| 1541 | HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); |
| 1542 | if (!FS.hasValidLeftJustified()) |
| 1543 | HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); |
| 1544 | |
| 1545 | // Check that flags are not ignored by another flag |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 1546 | if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' |
| 1547 | HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), |
| 1548 | startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1549 | if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' |
| 1550 | HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), |
| 1551 | startSpecifier, specifierLen); |
| 1552 | |
| 1553 | // Check the length modifier is valid with the given conversion specifier. |
| 1554 | const LengthModifier &LM = FS.getLengthModifier(); |
| 1555 | if (!FS.hasValidLengthModifier()) |
| 1556 | S.Diag(getLocationOfByte(LM.getStart()), |
Ted Kremenek | 649aecf | 2010-07-20 20:03:43 +0000 | [diff] [blame] | 1557 | diag::warn_format_nonsensical_length) |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1558 | << LM.toString() << CS.toString() |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1559 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1560 | << FixItHint::CreateRemoval(getSpecifierRange(LM.getStart(), |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1561 | LM.getLength())); |
| 1562 | |
| 1563 | // Are we using '%n'? |
Ted Kremenek | 35d353b | 2010-07-20 20:04:10 +0000 | [diff] [blame] | 1564 | if (CS.getKind() == ConversionSpecifier::nArg) { |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1565 | // Issue a warning about this being a possible security issue. |
Ted Kremenek | e82d804 | 2010-01-29 01:35:25 +0000 | [diff] [blame] | 1566 | S.Diag(getLocationOfByte(CS.getStart()), diag::warn_printf_write_back) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1567 | << getSpecifierRange(startSpecifier, specifierLen); |
Ted Kremenek | e82d804 | 2010-01-29 01:35:25 +0000 | [diff] [blame] | 1568 | // Continue checking the other format specifiers. |
| 1569 | return true; |
| 1570 | } |
Ted Kremenek | 5c41ee8 | 2010-02-11 09:27:41 +0000 | [diff] [blame] | 1571 | |
Ted Kremenek | da51f0d | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 1572 | // The remaining checks depend on the data arguments. |
| 1573 | if (HasVAListArg) |
| 1574 | return true; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1575 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 1576 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | da51f0d | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 1577 | return false; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1578 | |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 1579 | // Now type check the data expression that matches the |
| 1580 | // format specifier. |
| 1581 | const Expr *Ex = getDataArg(argIndex); |
| 1582 | const analyze_printf::ArgTypeResult &ATR = FS.getArgType(S.Context); |
| 1583 | if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) { |
| 1584 | // Check if we didn't match because of an implicit cast from a 'char' |
| 1585 | // or 'short' to an 'int'. This is done because printf is a varargs |
| 1586 | // function. |
| 1587 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Ex)) |
| 1588 | if (ICE->getType() == S.Context.IntTy) |
| 1589 | if (ATR.matchesType(S.Context, ICE->getSubExpr()->getType())) |
| 1590 | return true; |
| 1591 | |
| 1592 | // We may be able to offer a FixItHint if it is a supported type. |
| 1593 | PrintfSpecifier fixedFS = FS; |
| 1594 | bool success = fixedFS.fixType(Ex->getType()); |
| 1595 | |
| 1596 | if (success) { |
| 1597 | // Get the fix string from the fixed format specifier |
| 1598 | llvm::SmallString<128> buf; |
| 1599 | llvm::raw_svector_ostream os(buf); |
| 1600 | fixedFS.toString(os); |
| 1601 | |
Ted Kremenek | 9325eaf | 2010-08-24 22:24:51 +0000 | [diff] [blame] | 1602 | // FIXME: getRepresentativeType() perhaps should return a string |
| 1603 | // instead of a QualType to better handle when the representative |
| 1604 | // type is 'wint_t' (which is defined in the system headers). |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 1605 | S.Diag(getLocationOfByte(CS.getStart()), |
| 1606 | diag::warn_printf_conversion_argument_type_mismatch) |
| 1607 | << ATR.getRepresentativeType(S.Context) << Ex->getType() |
| 1608 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1609 | << Ex->getSourceRange() |
| 1610 | << FixItHint::CreateReplacement( |
| 1611 | getSpecifierRange(startSpecifier, specifierLen), |
| 1612 | os.str()); |
| 1613 | } |
| 1614 | else { |
| 1615 | S.Diag(getLocationOfByte(CS.getStart()), |
| 1616 | diag::warn_printf_conversion_argument_type_mismatch) |
| 1617 | << ATR.getRepresentativeType(S.Context) << Ex->getType() |
| 1618 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1619 | << Ex->getSourceRange(); |
| 1620 | } |
| 1621 | } |
| 1622 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1623 | return true; |
| 1624 | } |
| 1625 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1626 | //===--- CHECK: Scanf format string checking ------------------------------===// |
| 1627 | |
| 1628 | namespace { |
| 1629 | class CheckScanfHandler : public CheckFormatHandler { |
| 1630 | public: |
| 1631 | CheckScanfHandler(Sema &s, const StringLiteral *fexpr, |
| 1632 | const Expr *origFormatExpr, unsigned firstDataArg, |
| 1633 | unsigned numDataArgs, bool isObjCLiteral, |
| 1634 | const char *beg, bool hasVAListArg, |
| 1635 | const CallExpr *theCall, unsigned formatIdx) |
| 1636 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
| 1637 | numDataArgs, isObjCLiteral, beg, hasVAListArg, |
| 1638 | theCall, formatIdx) {} |
| 1639 | |
| 1640 | bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, |
| 1641 | const char *startSpecifier, |
| 1642 | unsigned specifierLen); |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1643 | |
| 1644 | bool HandleInvalidScanfConversionSpecifier( |
| 1645 | const analyze_scanf::ScanfSpecifier &FS, |
| 1646 | const char *startSpecifier, |
| 1647 | unsigned specifierLen); |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 1648 | |
| 1649 | void HandleIncompleteScanList(const char *start, const char *end); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1650 | }; |
Ted Kremenek | 07d161f | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 1651 | } |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1652 | |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 1653 | void CheckScanfHandler::HandleIncompleteScanList(const char *start, |
| 1654 | const char *end) { |
| 1655 | S.Diag(getLocationOfByte(end), diag::warn_scanf_scanlist_incomplete) |
| 1656 | << getSpecifierRange(start, end - start); |
| 1657 | } |
| 1658 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1659 | bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( |
| 1660 | const analyze_scanf::ScanfSpecifier &FS, |
| 1661 | const char *startSpecifier, |
| 1662 | unsigned specifierLen) { |
| 1663 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1664 | const analyze_scanf::ScanfConversionSpecifier &CS = |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1665 | FS.getConversionSpecifier(); |
| 1666 | |
| 1667 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 1668 | getLocationOfByte(CS.getStart()), |
| 1669 | startSpecifier, specifierLen, |
| 1670 | CS.getStart(), CS.getLength()); |
| 1671 | } |
| 1672 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1673 | bool CheckScanfHandler::HandleScanfSpecifier( |
| 1674 | const analyze_scanf::ScanfSpecifier &FS, |
| 1675 | const char *startSpecifier, |
| 1676 | unsigned specifierLen) { |
| 1677 | |
| 1678 | using namespace analyze_scanf; |
| 1679 | using namespace analyze_format_string; |
| 1680 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1681 | const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1682 | |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 1683 | // Handle case where '%' and '*' don't consume an argument. These shouldn't |
| 1684 | // be used to decide if we are using positional arguments consistently. |
| 1685 | if (FS.consumesDataArgument()) { |
| 1686 | if (atFirstArg) { |
| 1687 | atFirstArg = false; |
| 1688 | usesPositionalArgs = FS.usesPositionalArg(); |
| 1689 | } |
| 1690 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
| 1691 | // Cannot mix-and-match positional and non-positional arguments. |
| 1692 | S.Diag(getLocationOfByte(CS.getStart()), |
| 1693 | diag::warn_format_mix_positional_nonpositional_args) |
| 1694 | << getSpecifierRange(startSpecifier, specifierLen); |
| 1695 | return false; |
| 1696 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | // Check if the field with is non-zero. |
| 1700 | const OptionalAmount &Amt = FS.getFieldWidth(); |
| 1701 | if (Amt.getHowSpecified() == OptionalAmount::Constant) { |
| 1702 | if (Amt.getConstantAmount() == 0) { |
| 1703 | const CharSourceRange &R = getSpecifierRange(Amt.getStart(), |
| 1704 | Amt.getConstantLength()); |
| 1705 | S.Diag(getLocationOfByte(Amt.getStart()), |
| 1706 | diag::warn_scanf_nonzero_width) |
| 1707 | << R << FixItHint::CreateRemoval(R); |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | if (!FS.consumesDataArgument()) { |
| 1712 | // FIXME: Technically specifying a precision or field width here |
| 1713 | // makes no sense. Worth issuing a warning at some point. |
| 1714 | return true; |
| 1715 | } |
| 1716 | |
| 1717 | // Consume the argument. |
| 1718 | unsigned argIndex = FS.getArgIndex(); |
| 1719 | if (argIndex < NumDataArgs) { |
| 1720 | // The check to see if the argIndex is valid will come later. |
| 1721 | // We set the bit here because we may exit early from this |
| 1722 | // function if we encounter some other error. |
| 1723 | CoveredArgs.set(argIndex); |
| 1724 | } |
| 1725 | |
Ted Kremenek | 1e51c20 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 1726 | // Check the length modifier is valid with the given conversion specifier. |
| 1727 | const LengthModifier &LM = FS.getLengthModifier(); |
| 1728 | if (!FS.hasValidLengthModifier()) { |
| 1729 | S.Diag(getLocationOfByte(LM.getStart()), |
| 1730 | diag::warn_format_nonsensical_length) |
| 1731 | << LM.toString() << CS.toString() |
| 1732 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1733 | << FixItHint::CreateRemoval(getSpecifierRange(LM.getStart(), |
| 1734 | LM.getLength())); |
| 1735 | } |
| 1736 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1737 | // The remaining checks depend on the data arguments. |
| 1738 | if (HasVAListArg) |
| 1739 | return true; |
| 1740 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 1741 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1742 | return false; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1743 | |
| 1744 | // FIXME: Check that the argument type matches the format specifier. |
| 1745 | |
| 1746 | return true; |
| 1747 | } |
| 1748 | |
| 1749 | void Sema::CheckFormatString(const StringLiteral *FExpr, |
Ted Kremenek | 0e5675d | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 1750 | const Expr *OrigFormatExpr, |
| 1751 | const CallExpr *TheCall, bool HasVAListArg, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1752 | unsigned format_idx, unsigned firstDataArg, |
| 1753 | bool isPrintf) { |
| 1754 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1755 | // CHECK: is the format string a wide literal? |
| 1756 | if (FExpr->isWide()) { |
| 1757 | Diag(FExpr->getLocStart(), |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1758 | diag::warn_format_string_is_wide_literal) |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1759 | << OrigFormatExpr->getSourceRange(); |
| 1760 | return; |
| 1761 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1762 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1763 | // Str - The format string. NOTE: this is NOT null-terminated! |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 1764 | llvm::StringRef StrRef = FExpr->getString(); |
| 1765 | const char *Str = StrRef.data(); |
| 1766 | unsigned StrLen = StrRef.size(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1767 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1768 | // CHECK: empty format string? |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1769 | if (StrLen == 0) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1770 | Diag(FExpr->getLocStart(), diag::warn_empty_format_string) |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1771 | << OrigFormatExpr->getSourceRange(); |
| 1772 | return; |
| 1773 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1774 | |
| 1775 | if (isPrintf) { |
| 1776 | CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, |
| 1777 | TheCall->getNumArgs() - firstDataArg, |
| 1778 | isa<ObjCStringLiteral>(OrigFormatExpr), Str, |
| 1779 | HasVAListArg, TheCall, format_idx); |
| 1780 | |
| 1781 | if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen)) |
| 1782 | H.DoneProcessing(); |
| 1783 | } |
| 1784 | else { |
| 1785 | CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, |
| 1786 | TheCall->getNumArgs() - firstDataArg, |
| 1787 | isa<ObjCStringLiteral>(OrigFormatExpr), Str, |
| 1788 | HasVAListArg, TheCall, format_idx); |
| 1789 | |
| 1790 | if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen)) |
| 1791 | H.DoneProcessing(); |
| 1792 | } |
Ted Kremenek | ce7024e | 2010-01-28 01:18:22 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1795 | //===--- CHECK: Return Address of Stack Variable --------------------------===// |
| 1796 | |
| 1797 | static DeclRefExpr* EvalVal(Expr *E); |
| 1798 | static DeclRefExpr* EvalAddr(Expr* E); |
| 1799 | |
| 1800 | /// CheckReturnStackAddr - Check if a return statement returns the address |
| 1801 | /// of a stack variable. |
| 1802 | void |
| 1803 | Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, |
| 1804 | SourceLocation ReturnLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1805 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1806 | // Perform checking for returned stack addresses. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1807 | if (lhsType->isPointerType() || lhsType->isBlockPointerType()) { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1808 | if (DeclRefExpr *DR = EvalAddr(RetValExp)) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1809 | Diag(DR->getLocStart(), diag::warn_ret_stack_addr) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1810 | << DR->getDecl()->getDeclName() << RetValExp->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 1812 | // Skip over implicit cast expressions when checking for block expressions. |
Chris Lattner | 4ca606e | 2009-09-08 00:36:37 +0000 | [diff] [blame] | 1813 | RetValExp = RetValExp->IgnoreParenCasts(); |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 1814 | |
Chris Lattner | 9e6b37a | 2009-10-30 04:01:58 +0000 | [diff] [blame] | 1815 | if (BlockExpr *C = dyn_cast<BlockExpr>(RetValExp)) |
Mike Stump | 397195b | 2009-04-17 00:09:41 +0000 | [diff] [blame] | 1816 | if (C->hasBlockDeclRefExprs()) |
| 1817 | Diag(C->getLocStart(), diag::err_ret_local_block) |
| 1818 | << C->getSourceRange(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1819 | |
Chris Lattner | 9e6b37a | 2009-10-30 04:01:58 +0000 | [diff] [blame] | 1820 | if (AddrLabelExpr *ALE = dyn_cast<AddrLabelExpr>(RetValExp)) |
| 1821 | Diag(ALE->getLocStart(), diag::warn_ret_addr_label) |
| 1822 | << ALE->getSourceRange(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1823 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1824 | } else if (lhsType->isReferenceType()) { |
| 1825 | // Perform checking for stack values returned by reference. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1826 | // Check for a reference to the stack |
| 1827 | if (DeclRefExpr *DR = EvalVal(RetValExp)) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1828 | Diag(DR->getLocStart(), diag::warn_ret_stack_ref) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1829 | << DR->getDecl()->getDeclName() << RetValExp->getSourceRange(); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that |
| 1834 | /// check if the expression in a return statement evaluates to an address |
| 1835 | /// to a location on the stack. The recursion is used to traverse the |
| 1836 | /// AST of the return expression, with recursion backtracking when we |
| 1837 | /// encounter a subexpression that (1) clearly does not lead to the address |
| 1838 | /// of a stack variable or (2) is something we cannot determine leads to |
| 1839 | /// the address of a stack variable based on such local checking. |
| 1840 | /// |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 1841 | /// EvalAddr processes expressions that are pointers that are used as |
| 1842 | /// references (and not L-values). EvalVal handles all other values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1843 | /// 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] | 1844 | /// the refers to a stack variable. |
| 1845 | /// |
| 1846 | /// This implementation handles: |
| 1847 | /// |
| 1848 | /// * pointer-to-pointer casts |
| 1849 | /// * implicit conversions from array references to pointers |
| 1850 | /// * taking the address of fields |
| 1851 | /// * arbitrary interplay between "&" and "*" operators |
| 1852 | /// * pointer arithmetic from an address of a stack variable |
| 1853 | /// * taking the address of an array element where the array is on the stack |
| 1854 | static DeclRefExpr* EvalAddr(Expr *E) { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1855 | // We should only be called for evaluating pointer expressions. |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 1856 | assert((E->getType()->isAnyPointerType() || |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1857 | E->getType()->isBlockPointerType() || |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1858 | E->getType()->isObjCQualifiedIdType()) && |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1859 | "EvalAddr only works on pointers"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1860 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1861 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 1862 | // viewed AST node. We then recursively traverse the AST by calling |
| 1863 | // EvalAddr and EvalVal appropriately. |
| 1864 | switch (E->getStmtClass()) { |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1865 | case Stmt::ParenExprClass: |
| 1866 | // Ignore parentheses. |
| 1867 | return EvalAddr(cast<ParenExpr>(E)->getSubExpr()); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1868 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1869 | case Stmt::UnaryOperatorClass: { |
| 1870 | // The only unary operator that make sense to handle here |
| 1871 | // is AddrOf. All others don't make sense as pointers. |
| 1872 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1873 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1874 | if (U->getOpcode() == UO_AddrOf) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1875 | return EvalVal(U->getSubExpr()); |
| 1876 | else |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1877 | return NULL; |
| 1878 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1880 | case Stmt::BinaryOperatorClass: { |
| 1881 | // Handle pointer arithmetic. All other binary operators are not valid |
| 1882 | // in this context. |
| 1883 | BinaryOperator *B = cast<BinaryOperator>(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1884 | BinaryOperatorKind op = B->getOpcode(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1885 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1886 | if (op != BO_Add && op != BO_Sub) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1887 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1888 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1889 | Expr *Base = B->getLHS(); |
| 1890 | |
| 1891 | // Determine which argument is the real pointer base. It could be |
| 1892 | // the RHS argument instead of the LHS. |
| 1893 | if (!Base->getType()->isPointerType()) Base = B->getRHS(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1894 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1895 | assert (Base->getType()->isPointerType()); |
| 1896 | return EvalAddr(Base); |
| 1897 | } |
Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 1898 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1899 | // For conditional operators we need to see if either the LHS or RHS are |
| 1900 | // valid DeclRefExpr*s. If one of them is valid, we return it. |
| 1901 | case Stmt::ConditionalOperatorClass: { |
| 1902 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1903 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1904 | // Handle the GNU extension for missing LHS. |
| 1905 | if (Expr *lhsExpr = C->getLHS()) |
| 1906 | if (DeclRefExpr* LHS = EvalAddr(lhsExpr)) |
| 1907 | return LHS; |
| 1908 | |
| 1909 | return EvalAddr(C->getRHS()); |
| 1910 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 | |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 1912 | // For casts, we need to handle conversions from arrays to |
| 1913 | // pointer values, and pointer-to-pointer conversions. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1914 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1915 | case Stmt::CStyleCastExprClass: |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1916 | case Stmt::CXXFunctionalCastExprClass: { |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1917 | Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 1918 | QualType T = SubExpr->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1919 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1920 | if (SubExpr->getType()->isPointerType() || |
| 1921 | SubExpr->getType()->isBlockPointerType() || |
| 1922 | SubExpr->getType()->isObjCQualifiedIdType()) |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 1923 | return EvalAddr(SubExpr); |
| 1924 | else if (T->isArrayType()) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1925 | return EvalVal(SubExpr); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1926 | else |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 1927 | return 0; |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1928 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1930 | // C++ casts. For dynamic casts, static casts, and const casts, we |
| 1931 | // are always converting from a pointer-to-pointer, so we just blow |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1932 | // through the cast. In the case the dynamic cast doesn't fail (and |
| 1933 | // return NULL), we take the conservative route and report cases |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1934 | // where we return the address of a stack variable. For Reinterpre |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1935 | // FIXME: The comment about is wrong; we're not always converting |
| 1936 | // from pointer to pointer. I'm guessing that this code should also |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1937 | // handle references to objects. |
| 1938 | case Stmt::CXXStaticCastExprClass: |
| 1939 | case Stmt::CXXDynamicCastExprClass: |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1940 | case Stmt::CXXConstCastExprClass: |
| 1941 | case Stmt::CXXReinterpretCastExprClass: { |
| 1942 | Expr *S = cast<CXXNamedCastExpr>(E)->getSubExpr(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1943 | if (S->getType()->isPointerType() || S->getType()->isBlockPointerType()) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1944 | return EvalAddr(S); |
| 1945 | else |
| 1946 | return NULL; |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1947 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1948 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 1949 | // Everything else: we simply don't reason about them. |
| 1950 | default: |
| 1951 | return NULL; |
| 1952 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1953 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1954 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1955 | |
| 1956 | /// EvalVal - This function is complements EvalAddr in the mutual recursion. |
| 1957 | /// See the comments for EvalAddr for more details. |
| 1958 | static DeclRefExpr* EvalVal(Expr *E) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 1959 | do { |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 1960 | // We should only be called for evaluating non-pointer expressions, or |
| 1961 | // expressions with a pointer type that are not used as references but instead |
| 1962 | // are l-values (e.g., DeclRefExpr with a pointer type). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1963 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1964 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 1965 | // viewed AST node. We then recursively traverse the AST by calling |
| 1966 | // EvalAddr and EvalVal appropriately. |
| 1967 | switch (E->getStmtClass()) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 1968 | case Stmt::ImplicitCastExprClass: { |
| 1969 | ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E); |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1970 | if (IE->getValueKind() == VK_LValue) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 1971 | E = IE->getSubExpr(); |
| 1972 | continue; |
| 1973 | } |
| 1974 | return NULL; |
| 1975 | } |
| 1976 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 1977 | case Stmt::DeclRefExprClass: { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1978 | // DeclRefExpr: the base case. When we hit a DeclRefExpr we are looking |
| 1979 | // at code that refers to a variable's name. We check if it has local |
| 1980 | // storage within the function, and if so, return the expression. |
| 1981 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1982 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1983 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1984 | if (V->hasLocalStorage() && !V->getType()->isReferenceType()) return DR; |
| 1985 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1986 | return NULL; |
| 1987 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 1989 | case Stmt::ParenExprClass: { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1990 | // Ignore parentheses. |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 1991 | E = cast<ParenExpr>(E)->getSubExpr(); |
| 1992 | continue; |
| 1993 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1994 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1995 | case Stmt::UnaryOperatorClass: { |
| 1996 | // The only unary operator that make sense to handle here |
| 1997 | // is Deref. All others don't resolve to a "name." This includes |
| 1998 | // handling all sorts of rvalues passed to a unary operator. |
| 1999 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2000 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2001 | if (U->getOpcode() == UO_Deref) |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2002 | return EvalAddr(U->getSubExpr()); |
| 2003 | |
| 2004 | return NULL; |
| 2005 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2006 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2007 | case Stmt::ArraySubscriptExprClass: { |
| 2008 | // Array subscripts are potential references to data on the stack. We |
| 2009 | // retrieve the DeclRefExpr* for the array variable if it indeed |
| 2010 | // has local storage. |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 2011 | return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase()); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2012 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2013 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2014 | case Stmt::ConditionalOperatorClass: { |
| 2015 | // For conditional operators we need to see if either the LHS or RHS are |
| 2016 | // non-NULL DeclRefExpr's. If one is non-NULL, we return it. |
| 2017 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 2018 | |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 2019 | // Handle the GNU extension for missing LHS. |
| 2020 | if (Expr *lhsExpr = C->getLHS()) |
| 2021 | if (DeclRefExpr *LHS = EvalVal(lhsExpr)) |
| 2022 | return LHS; |
| 2023 | |
| 2024 | return EvalVal(C->getRHS()); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2025 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2026 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2027 | // Accesses to members are potential references to data on the stack. |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 2028 | case Stmt::MemberExprClass: { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2029 | MemberExpr *M = cast<MemberExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2030 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2031 | // Check for indirect access. We only want direct field accesses. |
| 2032 | if (!M->isArrow()) |
| 2033 | return EvalVal(M->getBase()); |
| 2034 | else |
| 2035 | return NULL; |
| 2036 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2037 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2038 | // Everything else: we simply don't reason about them. |
| 2039 | default: |
| 2040 | return NULL; |
| 2041 | } |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 2042 | } while (true); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2043 | } |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2044 | |
| 2045 | //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// |
| 2046 | |
| 2047 | /// Check for comparisons of floating point operands using != and ==. |
| 2048 | /// Issue a warning if these are no self-comparisons, as they are not likely |
| 2049 | /// to do what the programmer intended. |
| 2050 | void Sema::CheckFloatComparison(SourceLocation loc, Expr* lex, Expr *rex) { |
| 2051 | bool EmitWarning = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2052 | |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 2053 | Expr* LeftExprSansParen = lex->IgnoreParens(); |
Ted Kremenek | 32e97b6 | 2008-01-17 17:55:13 +0000 | [diff] [blame] | 2054 | Expr* RightExprSansParen = rex->IgnoreParens(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2055 | |
| 2056 | // Special case: check for x == x (which is OK). |
| 2057 | // Do not emit warnings for such cases. |
| 2058 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) |
| 2059 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) |
| 2060 | if (DRL->getDecl() == DRR->getDecl()) |
| 2061 | EmitWarning = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2062 | |
| 2063 | |
Ted Kremenek | 1b500bb | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 2064 | // Special case: check for comparisons against literals that can be exactly |
| 2065 | // represented by APFloat. In such cases, do not emit a warning. This |
| 2066 | // is a heuristic: often comparison against such literals are used to |
| 2067 | // detect if a value in a variable has not changed. This clearly can |
| 2068 | // lead to false negatives. |
| 2069 | if (EmitWarning) { |
| 2070 | if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { |
| 2071 | if (FLL->isExact()) |
| 2072 | EmitWarning = false; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2073 | } else |
Ted Kremenek | 1b500bb | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 2074 | if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)){ |
| 2075 | if (FLR->isExact()) |
| 2076 | EmitWarning = false; |
| 2077 | } |
| 2078 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2079 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2080 | // Check for comparisons with builtin types. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2081 | if (EmitWarning) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2082 | if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2083 | if (CL->isBuiltinCall(Context)) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2084 | EmitWarning = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2086 | if (EmitWarning) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2087 | if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2088 | if (CR->isBuiltinCall(Context)) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2089 | EmitWarning = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2090 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2091 | // Emit the diagnostic. |
| 2092 | if (EmitWarning) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2093 | Diag(loc, diag::warn_floatingpoint_eq) |
| 2094 | << lex->getSourceRange() << rex->getSourceRange(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2095 | } |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2096 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2097 | //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// |
| 2098 | //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2099 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2100 | namespace { |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2101 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2102 | /// Structure recording the 'active' range of an integer-valued |
| 2103 | /// expression. |
| 2104 | struct IntRange { |
| 2105 | /// The number of bits active in the int. |
| 2106 | unsigned Width; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2107 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2108 | /// True if the int is known not to have negative values. |
| 2109 | bool NonNegative; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2110 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2111 | IntRange(unsigned Width, bool NonNegative) |
| 2112 | : Width(Width), NonNegative(NonNegative) |
| 2113 | {} |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2114 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2115 | // Returns the range of the bool type. |
| 2116 | static IntRange forBoolType() { |
| 2117 | return IntRange(1, true); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2120 | // Returns the range of an integral type. |
| 2121 | static IntRange forType(ASTContext &C, QualType T) { |
| 2122 | return forCanonicalType(C, T->getCanonicalTypeInternal().getTypePtr()); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2125 | // Returns the range of an integeral type based on its canonical |
| 2126 | // representation. |
| 2127 | static IntRange forCanonicalType(ASTContext &C, const Type *T) { |
| 2128 | assert(T->isCanonicalUnqualified()); |
| 2129 | |
| 2130 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 2131 | T = VT->getElementType().getTypePtr(); |
| 2132 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 2133 | T = CT->getElementType().getTypePtr(); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2134 | |
| 2135 | if (const EnumType *ET = dyn_cast<EnumType>(T)) { |
| 2136 | EnumDecl *Enum = ET->getDecl(); |
| 2137 | unsigned NumPositive = Enum->getNumPositiveBits(); |
| 2138 | unsigned NumNegative = Enum->getNumNegativeBits(); |
| 2139 | |
| 2140 | return IntRange(std::max(NumPositive, NumNegative), NumNegative == 0); |
| 2141 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2142 | |
| 2143 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 2144 | assert(BT->isInteger()); |
| 2145 | |
| 2146 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 2147 | } |
| 2148 | |
| 2149 | // Returns the supremum of two ranges: i.e. their conservative merge. |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 2150 | static IntRange join(IntRange L, IntRange R) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2151 | return IntRange(std::max(L.Width, R.Width), |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2152 | L.NonNegative && R.NonNegative); |
| 2153 | } |
| 2154 | |
| 2155 | // Returns the infinum of two ranges: i.e. their aggressive merge. |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 2156 | static IntRange meet(IntRange L, IntRange R) { |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2157 | return IntRange(std::min(L.Width, R.Width), |
| 2158 | L.NonNegative || R.NonNegative); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2159 | } |
| 2160 | }; |
| 2161 | |
| 2162 | IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, unsigned MaxWidth) { |
| 2163 | if (value.isSigned() && value.isNegative()) |
| 2164 | return IntRange(value.getMinSignedBits(), false); |
| 2165 | |
| 2166 | if (value.getBitWidth() > MaxWidth) |
| 2167 | value.trunc(MaxWidth); |
| 2168 | |
| 2169 | // isNonNegative() just checks the sign bit without considering |
| 2170 | // signedness. |
| 2171 | return IntRange(value.getActiveBits(), true); |
| 2172 | } |
| 2173 | |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 2174 | IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2175 | unsigned MaxWidth) { |
| 2176 | if (result.isInt()) |
| 2177 | return GetValueRange(C, result.getInt(), MaxWidth); |
| 2178 | |
| 2179 | if (result.isVector()) { |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 2180 | IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); |
| 2181 | for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { |
| 2182 | IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); |
| 2183 | R = IntRange::join(R, El); |
| 2184 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2185 | return R; |
| 2186 | } |
| 2187 | |
| 2188 | if (result.isComplexInt()) { |
| 2189 | IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); |
| 2190 | IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); |
| 2191 | return IntRange::join(R, I); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | // This can happen with lossless casts to intptr_t of "based" lvalues. |
| 2195 | // Assume it might use arbitrary bits. |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 2196 | // FIXME: The only reason we need to pass the type in here is to get |
| 2197 | // the sign right on this one case. It would be nice if APValue |
| 2198 | // preserved this. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2199 | assert(result.isLValue()); |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 2200 | return IntRange(MaxWidth, Ty->isUnsignedIntegerType()); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2201 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2202 | |
| 2203 | /// Pseudo-evaluate the given integer expression, estimating the |
| 2204 | /// range of values it might take. |
| 2205 | /// |
| 2206 | /// \param MaxWidth - the width to which the value will be truncated |
| 2207 | IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) { |
| 2208 | E = E->IgnoreParens(); |
| 2209 | |
| 2210 | // Try a full evaluation first. |
| 2211 | Expr::EvalResult result; |
| 2212 | if (E->Evaluate(result, C)) |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 2213 | return GetValueRange(C, result.Val, E->getType(), MaxWidth); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2214 | |
| 2215 | // I think we only want to look through implicit casts here; if the |
| 2216 | // user has an explicit widening cast, we should treat the value as |
| 2217 | // being of the new, wider type. |
| 2218 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2219 | if (CE->getCastKind() == CK_NoOp) |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2220 | return GetExprRange(C, CE->getSubExpr(), MaxWidth); |
| 2221 | |
| 2222 | IntRange OutputTypeRange = IntRange::forType(C, CE->getType()); |
| 2223 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2224 | bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast); |
| 2225 | if (!isIntegerCast && CE->getCastKind() == CK_Unknown) |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2226 | isIntegerCast = CE->getSubExpr()->getType()->isIntegerType(); |
| 2227 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2228 | // 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] | 2229 | if (!isIntegerCast) |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2230 | return OutputTypeRange; |
| 2231 | |
| 2232 | IntRange SubRange |
| 2233 | = GetExprRange(C, CE->getSubExpr(), |
| 2234 | std::min(MaxWidth, OutputTypeRange.Width)); |
| 2235 | |
| 2236 | // Bail out if the subexpr's range is as wide as the cast type. |
| 2237 | if (SubRange.Width >= OutputTypeRange.Width) |
| 2238 | return OutputTypeRange; |
| 2239 | |
| 2240 | // Otherwise, we take the smaller width, and we're non-negative if |
| 2241 | // either the output type or the subexpr is. |
| 2242 | return IntRange(SubRange.Width, |
| 2243 | SubRange.NonNegative || OutputTypeRange.NonNegative); |
| 2244 | } |
| 2245 | |
| 2246 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 2247 | // If we can fold the condition, just take that operand. |
| 2248 | bool CondResult; |
| 2249 | if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) |
| 2250 | return GetExprRange(C, CondResult ? CO->getTrueExpr() |
| 2251 | : CO->getFalseExpr(), |
| 2252 | MaxWidth); |
| 2253 | |
| 2254 | // Otherwise, conservatively merge. |
| 2255 | IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth); |
| 2256 | IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth); |
| 2257 | return IntRange::join(L, R); |
| 2258 | } |
| 2259 | |
| 2260 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 2261 | switch (BO->getOpcode()) { |
| 2262 | |
| 2263 | // Boolean-valued operations are single-bit and positive. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2264 | case BO_LAnd: |
| 2265 | case BO_LOr: |
| 2266 | case BO_LT: |
| 2267 | case BO_GT: |
| 2268 | case BO_LE: |
| 2269 | case BO_GE: |
| 2270 | case BO_EQ: |
| 2271 | case BO_NE: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2272 | return IntRange::forBoolType(); |
| 2273 | |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 2274 | // The type of these compound assignments is the type of the LHS, |
| 2275 | // so the RHS is not necessarily an integer. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2276 | case BO_MulAssign: |
| 2277 | case BO_DivAssign: |
| 2278 | case BO_RemAssign: |
| 2279 | case BO_AddAssign: |
| 2280 | case BO_SubAssign: |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 2281 | return IntRange::forType(C, E->getType()); |
| 2282 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2283 | // Operations with opaque sources are black-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2284 | case BO_PtrMemD: |
| 2285 | case BO_PtrMemI: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2286 | return IntRange::forType(C, E->getType()); |
| 2287 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2288 | // Bitwise-and uses the *infinum* of the two source ranges. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2289 | case BO_And: |
| 2290 | case BO_AndAssign: |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2291 | return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth), |
| 2292 | GetExprRange(C, BO->getRHS(), MaxWidth)); |
| 2293 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2294 | // Left shift gets black-listed based on a judgement call. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2295 | case BO_Shl: |
John McCall | 3aae609 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 2296 | // ...except that we want to treat '1 << (blah)' as logically |
| 2297 | // positive. It's an important idiom. |
| 2298 | if (IntegerLiteral *I |
| 2299 | = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { |
| 2300 | if (I->getValue() == 1) { |
| 2301 | IntRange R = IntRange::forType(C, E->getType()); |
| 2302 | return IntRange(R.Width, /*NonNegative*/ true); |
| 2303 | } |
| 2304 | } |
| 2305 | // fallthrough |
| 2306 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2307 | case BO_ShlAssign: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2308 | return IntRange::forType(C, E->getType()); |
| 2309 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2310 | // Right shift by a constant can narrow its left argument. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2311 | case BO_Shr: |
| 2312 | case BO_ShrAssign: { |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2313 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 2314 | |
| 2315 | // If the shift amount is a positive constant, drop the width by |
| 2316 | // that much. |
| 2317 | llvm::APSInt shift; |
| 2318 | if (BO->getRHS()->isIntegerConstantExpr(shift, C) && |
| 2319 | shift.isNonNegative()) { |
| 2320 | unsigned zext = shift.getZExtValue(); |
| 2321 | if (zext >= L.Width) |
| 2322 | L.Width = (L.NonNegative ? 0 : 1); |
| 2323 | else |
| 2324 | L.Width -= zext; |
| 2325 | } |
| 2326 | |
| 2327 | return L; |
| 2328 | } |
| 2329 | |
| 2330 | // Comma acts as its right operand. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2331 | case BO_Comma: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2332 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 2333 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2334 | // Black-list pointer subtractions. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2335 | case BO_Sub: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2336 | if (BO->getLHS()->getType()->isPointerType()) |
| 2337 | return IntRange::forType(C, E->getType()); |
| 2338 | // fallthrough |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2339 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2340 | default: |
| 2341 | break; |
| 2342 | } |
| 2343 | |
| 2344 | // Treat every other operator as if it were closed on the |
| 2345 | // narrowest type that encompasses both operands. |
| 2346 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 2347 | IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth); |
| 2348 | return IntRange::join(L, R); |
| 2349 | } |
| 2350 | |
| 2351 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 2352 | switch (UO->getOpcode()) { |
| 2353 | // Boolean-valued operations are white-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2354 | case UO_LNot: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2355 | return IntRange::forBoolType(); |
| 2356 | |
| 2357 | // Operations with opaque sources are black-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2358 | case UO_Deref: |
| 2359 | case UO_AddrOf: // should be impossible |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2360 | return IntRange::forType(C, E->getType()); |
| 2361 | |
| 2362 | default: |
| 2363 | return GetExprRange(C, UO->getSubExpr(), MaxWidth); |
| 2364 | } |
| 2365 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 2366 | |
| 2367 | if (dyn_cast<OffsetOfExpr>(E)) { |
| 2368 | IntRange::forType(C, E->getType()); |
| 2369 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2370 | |
| 2371 | FieldDecl *BitField = E->getBitField(); |
| 2372 | if (BitField) { |
| 2373 | llvm::APSInt BitWidthAP = BitField->getBitWidth()->EvaluateAsInt(C); |
| 2374 | unsigned BitWidth = BitWidthAP.getZExtValue(); |
| 2375 | |
| 2376 | return IntRange(BitWidth, BitField->getType()->isUnsignedIntegerType()); |
| 2377 | } |
| 2378 | |
| 2379 | return IntRange::forType(C, E->getType()); |
| 2380 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2381 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2382 | IntRange GetExprRange(ASTContext &C, Expr *E) { |
| 2383 | return GetExprRange(C, E, C.getIntWidth(E->getType())); |
| 2384 | } |
| 2385 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2386 | /// Checks whether the given value, which currently has the given |
| 2387 | /// source semantics, has the same value when coerced through the |
| 2388 | /// target semantics. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2389 | bool IsSameFloatAfterCast(const llvm::APFloat &value, |
| 2390 | const llvm::fltSemantics &Src, |
| 2391 | const llvm::fltSemantics &Tgt) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2392 | llvm::APFloat truncated = value; |
| 2393 | |
| 2394 | bool ignored; |
| 2395 | truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 2396 | truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 2397 | |
| 2398 | return truncated.bitwiseIsEqual(value); |
| 2399 | } |
| 2400 | |
| 2401 | /// Checks whether the given value, which currently has the given |
| 2402 | /// source semantics, has the same value when coerced through the |
| 2403 | /// target semantics. |
| 2404 | /// |
| 2405 | /// The value might be a vector of floats (or a complex number). |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2406 | bool IsSameFloatAfterCast(const APValue &value, |
| 2407 | const llvm::fltSemantics &Src, |
| 2408 | const llvm::fltSemantics &Tgt) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2409 | if (value.isFloat()) |
| 2410 | return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); |
| 2411 | |
| 2412 | if (value.isVector()) { |
| 2413 | for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) |
| 2414 | if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) |
| 2415 | return false; |
| 2416 | return true; |
| 2417 | } |
| 2418 | |
| 2419 | assert(value.isComplexFloat()); |
| 2420 | return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && |
| 2421 | IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); |
| 2422 | } |
| 2423 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2424 | void AnalyzeImplicitConversions(Sema &S, Expr *E); |
| 2425 | |
| 2426 | bool IsZero(Sema &S, Expr *E) { |
| 2427 | llvm::APSInt Value; |
| 2428 | return E->isIntegerConstantExpr(Value, S.Context) && Value == 0; |
| 2429 | } |
| 2430 | |
| 2431 | void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2432 | BinaryOperatorKind op = E->getOpcode(); |
| 2433 | if (op == BO_LT && IsZero(S, E->getRHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2434 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
| 2435 | << "< 0" << "false" |
| 2436 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2437 | } else if (op == BO_GE && IsZero(S, E->getRHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2438 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
| 2439 | << ">= 0" << "true" |
| 2440 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2441 | } else if (op == BO_GT && IsZero(S, E->getLHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2442 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
| 2443 | << "0 >" << "false" |
| 2444 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2445 | } else if (op == BO_LE && IsZero(S, E->getLHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2446 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
| 2447 | << "0 <=" << "true" |
| 2448 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | /// Analyze the operands of the given comparison. Implements the |
| 2453 | /// fallback case from AnalyzeComparison. |
| 2454 | void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { |
| 2455 | AnalyzeImplicitConversions(S, E->getLHS()); |
| 2456 | AnalyzeImplicitConversions(S, E->getRHS()); |
| 2457 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2458 | |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2459 | /// \brief Implements -Wsign-compare. |
| 2460 | /// |
| 2461 | /// \param lex the left-hand expression |
| 2462 | /// \param rex the right-hand expression |
| 2463 | /// \param OpLoc the location of the joining operator |
John McCall | d1b47bf | 2010-03-11 19:43:18 +0000 | [diff] [blame] | 2464 | /// \param BinOpc binary opcode or 0 |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2465 | void AnalyzeComparison(Sema &S, BinaryOperator *E) { |
| 2466 | // The type the comparison is being performed in. |
| 2467 | QualType T = E->getLHS()->getType(); |
| 2468 | assert(S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType()) |
| 2469 | && "comparison with mismatched types"); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2470 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2471 | // We don't do anything special if this isn't an unsigned integral |
| 2472 | // comparison: we're only interested in integral comparisons, and |
| 2473 | // signed comparisons only happen in cases we don't care to warn about. |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 2474 | if (!T->hasUnsignedIntegerRepresentation()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2475 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2476 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2477 | Expr *lex = E->getLHS()->IgnoreParenImpCasts(); |
| 2478 | Expr *rex = E->getRHS()->IgnoreParenImpCasts(); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2479 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2480 | // Check to see if one of the (unmodified) operands is of different |
| 2481 | // signedness. |
| 2482 | Expr *signedOperand, *unsignedOperand; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 2483 | if (lex->getType()->hasSignedIntegerRepresentation()) { |
| 2484 | assert(!rex->getType()->hasSignedIntegerRepresentation() && |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2485 | "unsigned comparison between two signed integer expressions?"); |
| 2486 | signedOperand = lex; |
| 2487 | unsignedOperand = rex; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 2488 | } else if (rex->getType()->hasSignedIntegerRepresentation()) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2489 | signedOperand = rex; |
| 2490 | unsignedOperand = lex; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2491 | } else { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2492 | CheckTrivialUnsignedComparison(S, E); |
| 2493 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2496 | // Otherwise, calculate the effective range of the signed operand. |
| 2497 | IntRange signedRange = GetExprRange(S.Context, signedOperand); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2498 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2499 | // Go ahead and analyze implicit conversions in the operands. Note |
| 2500 | // that we skip the implicit conversions on both sides. |
| 2501 | AnalyzeImplicitConversions(S, lex); |
| 2502 | AnalyzeImplicitConversions(S, rex); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2503 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2504 | // If the signed range is non-negative, -Wsign-compare won't fire, |
| 2505 | // but we should still check for comparisons which are always true |
| 2506 | // or false. |
| 2507 | if (signedRange.NonNegative) |
| 2508 | return CheckTrivialUnsignedComparison(S, E); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2509 | |
| 2510 | // For (in)equality comparisons, if the unsigned operand is a |
| 2511 | // constant which cannot collide with a overflowed signed operand, |
| 2512 | // then reinterpreting the signed operand as unsigned will not |
| 2513 | // change the result of the comparison. |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2514 | if (E->isEqualityOp()) { |
| 2515 | unsigned comparisonWidth = S.Context.getIntWidth(T); |
| 2516 | IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2517 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2518 | // We should never be unable to prove that the unsigned operand is |
| 2519 | // non-negative. |
| 2520 | assert(unsignedRange.NonNegative && "unsigned range includes negative?"); |
| 2521 | |
| 2522 | if (unsignedRange.Width < comparisonWidth) |
| 2523 | return; |
| 2524 | } |
| 2525 | |
| 2526 | S.Diag(E->getOperatorLoc(), diag::warn_mixed_sign_comparison) |
| 2527 | << lex->getType() << rex->getType() |
| 2528 | << lex->getSourceRange() << rex->getSourceRange(); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2529 | } |
| 2530 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2531 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2532 | void DiagnoseImpCast(Sema &S, Expr *E, QualType T, unsigned diag) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2533 | S.Diag(E->getExprLoc(), diag) << E->getType() << T << E->getSourceRange(); |
| 2534 | } |
| 2535 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2536 | void CheckImplicitConversion(Sema &S, Expr *E, QualType T, |
| 2537 | bool *ICContext = 0) { |
| 2538 | if (E->isTypeDependent() || E->isValueDependent()) return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2539 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2540 | const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr(); |
| 2541 | const Type *Target = S.Context.getCanonicalType(T).getTypePtr(); |
| 2542 | if (Source == Target) return; |
| 2543 | if (Target->isDependentType()) return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2544 | |
| 2545 | // Never diagnose implicit casts to bool. |
| 2546 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) |
| 2547 | return; |
| 2548 | |
| 2549 | // Strip vector types. |
| 2550 | if (isa<VectorType>(Source)) { |
| 2551 | if (!isa<VectorType>(Target)) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2552 | return DiagnoseImpCast(S, E, T, diag::warn_impcast_vector_scalar); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2553 | |
| 2554 | Source = cast<VectorType>(Source)->getElementType().getTypePtr(); |
| 2555 | Target = cast<VectorType>(Target)->getElementType().getTypePtr(); |
| 2556 | } |
| 2557 | |
| 2558 | // Strip complex types. |
| 2559 | if (isa<ComplexType>(Source)) { |
| 2560 | if (!isa<ComplexType>(Target)) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2561 | return DiagnoseImpCast(S, E, T, diag::warn_impcast_complex_scalar); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2562 | |
| 2563 | Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); |
| 2564 | Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); |
| 2565 | } |
| 2566 | |
| 2567 | const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); |
| 2568 | const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); |
| 2569 | |
| 2570 | // If the source is floating point... |
| 2571 | if (SourceBT && SourceBT->isFloatingPoint()) { |
| 2572 | // ...and the target is floating point... |
| 2573 | if (TargetBT && TargetBT->isFloatingPoint()) { |
| 2574 | // ...then warn if we're dropping FP rank. |
| 2575 | |
| 2576 | // Builtin FP kinds are ordered by increasing FP rank. |
| 2577 | if (SourceBT->getKind() > TargetBT->getKind()) { |
| 2578 | // Don't warn about float constants that are precisely |
| 2579 | // representable in the target type. |
| 2580 | Expr::EvalResult result; |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2581 | if (E->Evaluate(result, S.Context)) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2582 | // Value might be a float, a float vector, or a float complex. |
| 2583 | if (IsSameFloatAfterCast(result.Val, |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2584 | S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)), |
| 2585 | S.Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2586 | return; |
| 2587 | } |
| 2588 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2589 | DiagnoseImpCast(S, E, T, diag::warn_impcast_float_precision); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2590 | } |
| 2591 | return; |
| 2592 | } |
| 2593 | |
| 2594 | // If the target is integral, always warn. |
| 2595 | if ((TargetBT && TargetBT->isInteger())) |
| 2596 | // TODO: don't warn for integer values? |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2597 | DiagnoseImpCast(S, E, T, diag::warn_impcast_float_integer); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2598 | |
| 2599 | return; |
| 2600 | } |
| 2601 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2602 | if (!Source->isIntegerType() || !Target->isIntegerType()) |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2603 | return; |
| 2604 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2605 | IntRange SourceRange = GetExprRange(S.Context, E); |
| 2606 | IntRange TargetRange = IntRange::forCanonicalType(S.Context, Target); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2607 | |
| 2608 | if (SourceRange.Width > TargetRange.Width) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2609 | // People want to build with -Wshorten-64-to-32 and not -Wconversion |
| 2610 | // and by god we'll let them. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2611 | if (SourceRange.Width == 64 && TargetRange.Width == 32) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2612 | return DiagnoseImpCast(S, E, T, diag::warn_impcast_integer_64_32); |
| 2613 | return DiagnoseImpCast(S, E, T, diag::warn_impcast_integer_precision); |
| 2614 | } |
| 2615 | |
| 2616 | if ((TargetRange.NonNegative && !SourceRange.NonNegative) || |
| 2617 | (!TargetRange.NonNegative && SourceRange.NonNegative && |
| 2618 | SourceRange.Width == TargetRange.Width)) { |
| 2619 | unsigned DiagID = diag::warn_impcast_integer_sign; |
| 2620 | |
| 2621 | // Traditionally, gcc has warned about this under -Wsign-compare. |
| 2622 | // We also want to warn about it in -Wconversion. |
| 2623 | // So if -Wconversion is off, use a completely identical diagnostic |
| 2624 | // in the sign-compare group. |
| 2625 | // The conditional-checking code will |
| 2626 | if (ICContext) { |
| 2627 | DiagID = diag::warn_impcast_integer_sign_conditional; |
| 2628 | *ICContext = true; |
| 2629 | } |
| 2630 | |
| 2631 | return DiagnoseImpCast(S, E, T, DiagID); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | return; |
| 2635 | } |
| 2636 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2637 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T); |
| 2638 | |
| 2639 | void CheckConditionalOperand(Sema &S, Expr *E, QualType T, |
| 2640 | bool &ICContext) { |
| 2641 | E = E->IgnoreParenImpCasts(); |
| 2642 | |
| 2643 | if (isa<ConditionalOperator>(E)) |
| 2644 | return CheckConditionalOperator(S, cast<ConditionalOperator>(E), T); |
| 2645 | |
| 2646 | AnalyzeImplicitConversions(S, E); |
| 2647 | if (E->getType() != T) |
| 2648 | return CheckImplicitConversion(S, E, T, &ICContext); |
| 2649 | return; |
| 2650 | } |
| 2651 | |
| 2652 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T) { |
| 2653 | AnalyzeImplicitConversions(S, E->getCond()); |
| 2654 | |
| 2655 | bool Suspicious = false; |
| 2656 | CheckConditionalOperand(S, E->getTrueExpr(), T, Suspicious); |
| 2657 | CheckConditionalOperand(S, E->getFalseExpr(), T, Suspicious); |
| 2658 | |
| 2659 | // If -Wconversion would have warned about either of the candidates |
| 2660 | // for a signedness conversion to the context type... |
| 2661 | if (!Suspicious) return; |
| 2662 | |
| 2663 | // ...but it's currently ignored... |
| 2664 | if (S.Diags.getDiagnosticLevel(diag::warn_impcast_integer_sign_conditional)) |
| 2665 | return; |
| 2666 | |
| 2667 | // ...and -Wsign-compare isn't... |
| 2668 | if (!S.Diags.getDiagnosticLevel(diag::warn_mixed_sign_conditional)) |
| 2669 | return; |
| 2670 | |
| 2671 | // ...then check whether it would have warned about either of the |
| 2672 | // candidates for a signedness conversion to the condition type. |
| 2673 | if (E->getType() != T) { |
| 2674 | Suspicious = false; |
| 2675 | CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(), |
| 2676 | E->getType(), &Suspicious); |
| 2677 | if (!Suspicious) |
| 2678 | CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(), |
| 2679 | E->getType(), &Suspicious); |
| 2680 | if (!Suspicious) |
| 2681 | return; |
| 2682 | } |
| 2683 | |
| 2684 | // If so, emit a diagnostic under -Wsign-compare. |
| 2685 | Expr *lex = E->getTrueExpr()->IgnoreParenImpCasts(); |
| 2686 | Expr *rex = E->getFalseExpr()->IgnoreParenImpCasts(); |
| 2687 | S.Diag(E->getQuestionLoc(), diag::warn_mixed_sign_conditional) |
| 2688 | << lex->getType() << rex->getType() |
| 2689 | << lex->getSourceRange() << rex->getSourceRange(); |
| 2690 | } |
| 2691 | |
| 2692 | /// AnalyzeImplicitConversions - Find and report any interesting |
| 2693 | /// implicit conversions in the given expression. There are a couple |
| 2694 | /// of competing diagnostics here, -Wconversion and -Wsign-compare. |
| 2695 | void AnalyzeImplicitConversions(Sema &S, Expr *OrigE) { |
| 2696 | QualType T = OrigE->getType(); |
| 2697 | Expr *E = OrigE->IgnoreParenImpCasts(); |
| 2698 | |
| 2699 | // For conditional operators, we analyze the arguments as if they |
| 2700 | // were being fed directly into the output. |
| 2701 | if (isa<ConditionalOperator>(E)) { |
| 2702 | ConditionalOperator *CO = cast<ConditionalOperator>(E); |
| 2703 | CheckConditionalOperator(S, CO, T); |
| 2704 | return; |
| 2705 | } |
| 2706 | |
| 2707 | // Go ahead and check any implicit conversions we might have skipped. |
| 2708 | // The non-canonical typecheck is just an optimization; |
| 2709 | // CheckImplicitConversion will filter out dead implicit conversions. |
| 2710 | if (E->getType() != T) |
| 2711 | CheckImplicitConversion(S, E, T); |
| 2712 | |
| 2713 | // Now continue drilling into this expression. |
| 2714 | |
| 2715 | // Skip past explicit casts. |
| 2716 | if (isa<ExplicitCastExpr>(E)) { |
| 2717 | E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts(); |
| 2718 | return AnalyzeImplicitConversions(S, E); |
| 2719 | } |
| 2720 | |
| 2721 | // Do a somewhat different check with comparison operators. |
| 2722 | if (isa<BinaryOperator>(E) && cast<BinaryOperator>(E)->isComparisonOp()) |
| 2723 | return AnalyzeComparison(S, cast<BinaryOperator>(E)); |
| 2724 | |
| 2725 | // These break the otherwise-useful invariant below. Fortunately, |
| 2726 | // we don't really need to recurse into them, because any internal |
| 2727 | // expressions should have been analyzed already when they were |
| 2728 | // built into statements. |
| 2729 | if (isa<StmtExpr>(E)) return; |
| 2730 | |
| 2731 | // Don't descend into unevaluated contexts. |
| 2732 | if (isa<SizeOfAlignOfExpr>(E)) return; |
| 2733 | |
| 2734 | // Now just recurse over the expression's children. |
| 2735 | for (Stmt::child_iterator I = E->child_begin(), IE = E->child_end(); |
| 2736 | I != IE; ++I) |
| 2737 | AnalyzeImplicitConversions(S, cast<Expr>(*I)); |
| 2738 | } |
| 2739 | |
| 2740 | } // end anonymous namespace |
| 2741 | |
| 2742 | /// Diagnoses "dangerous" implicit conversions within the given |
| 2743 | /// expression (which is a full expression). Implements -Wconversion |
| 2744 | /// and -Wsign-compare. |
| 2745 | void Sema::CheckImplicitConversions(Expr *E) { |
| 2746 | // Don't diagnose in unevaluated contexts. |
| 2747 | if (ExprEvalContexts.back().Context == Sema::Unevaluated) |
| 2748 | return; |
| 2749 | |
| 2750 | // Don't diagnose for value- or type-dependent expressions. |
| 2751 | if (E->isTypeDependent() || E->isValueDependent()) |
| 2752 | return; |
| 2753 | |
| 2754 | AnalyzeImplicitConversions(*this, E); |
| 2755 | } |
| 2756 | |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 2757 | /// CheckParmsForFunctionDef - Check that the parameters of the given |
| 2758 | /// function are appropriate for the definition of a function. This |
| 2759 | /// takes care of any checks that cannot be performed on the |
| 2760 | /// declaration itself, e.g., that the types of each of the function |
| 2761 | /// parameters are complete. |
| 2762 | bool Sema::CheckParmsForFunctionDef(FunctionDecl *FD) { |
| 2763 | bool HasInvalidParm = false; |
| 2764 | for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) { |
| 2765 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 2766 | |
| 2767 | // C99 6.7.5.3p4: the parameters in a parameter type list in a |
| 2768 | // function declarator that is part of a function definition of |
| 2769 | // that function shall not have incomplete type. |
| 2770 | // |
| 2771 | // This is also C++ [dcl.fct]p6. |
| 2772 | if (!Param->isInvalidDecl() && |
| 2773 | RequireCompleteType(Param->getLocation(), Param->getType(), |
| 2774 | diag::err_typecheck_decl_incomplete_type)) { |
| 2775 | Param->setInvalidDecl(); |
| 2776 | HasInvalidParm = true; |
| 2777 | } |
| 2778 | |
| 2779 | // C99 6.9.1p5: If the declarator includes a parameter type list, the |
| 2780 | // declaration of each parameter shall include an identifier. |
| 2781 | if (Param->getIdentifier() == 0 && |
| 2782 | !Param->isImplicit() && |
| 2783 | !getLangOptions().CPlusPlus) |
| 2784 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
Sam Weinig | d17e340 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 2785 | |
| 2786 | // C99 6.7.5.3p12: |
| 2787 | // If the function declarator is not part of a definition of that |
| 2788 | // function, parameters may have incomplete type and may use the [*] |
| 2789 | // notation in their sequences of declarator specifiers to specify |
| 2790 | // variable length array types. |
| 2791 | QualType PType = Param->getOriginalType(); |
| 2792 | if (const ArrayType *AT = Context.getAsArrayType(PType)) { |
| 2793 | if (AT->getSizeModifier() == ArrayType::Star) { |
| 2794 | // FIXME: This diagnosic should point the the '[*]' if source-location |
| 2795 | // information is added for it. |
| 2796 | Diag(Param->getLocation(), diag::err_array_star_in_function_definition); |
| 2797 | } |
| 2798 | } |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
| 2801 | return HasInvalidParm; |
| 2802 | } |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 2803 | |
| 2804 | /// CheckCastAlign - Implements -Wcast-align, which warns when a |
| 2805 | /// pointer cast increases the alignment requirements. |
| 2806 | void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { |
| 2807 | // This is actually a lot of work to potentially be doing on every |
| 2808 | // cast; don't do it if we're ignoring -Wcast_align (as is the default). |
| 2809 | if (getDiagnostics().getDiagnosticLevel(diag::warn_cast_align) |
| 2810 | == Diagnostic::Ignored) |
| 2811 | return; |
| 2812 | |
| 2813 | // Ignore dependent types. |
| 2814 | if (T->isDependentType() || Op->getType()->isDependentType()) |
| 2815 | return; |
| 2816 | |
| 2817 | // Require that the destination be a pointer type. |
| 2818 | const PointerType *DestPtr = T->getAs<PointerType>(); |
| 2819 | if (!DestPtr) return; |
| 2820 | |
| 2821 | // If the destination has alignment 1, we're done. |
| 2822 | QualType DestPointee = DestPtr->getPointeeType(); |
| 2823 | if (DestPointee->isIncompleteType()) return; |
| 2824 | CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); |
| 2825 | if (DestAlign.isOne()) return; |
| 2826 | |
| 2827 | // Require that the source be a pointer type. |
| 2828 | const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); |
| 2829 | if (!SrcPtr) return; |
| 2830 | QualType SrcPointee = SrcPtr->getPointeeType(); |
| 2831 | |
| 2832 | // Whitelist casts from cv void*. We already implicitly |
| 2833 | // whitelisted casts to cv void*, since they have alignment 1. |
| 2834 | // Also whitelist casts involving incomplete types, which implicitly |
| 2835 | // includes 'void'. |
| 2836 | if (SrcPointee->isIncompleteType()) return; |
| 2837 | |
| 2838 | CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee); |
| 2839 | if (SrcAlign >= DestAlign) return; |
| 2840 | |
| 2841 | Diag(TRange.getBegin(), diag::warn_cast_align) |
| 2842 | << Op->getType() << T |
| 2843 | << static_cast<unsigned>(SrcAlign.getQuantity()) |
| 2844 | << static_cast<unsigned>(DestAlign.getQuantity()) |
| 2845 | << TRange << Op->getSourceRange(); |
| 2846 | } |
| 2847 | |