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" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 25 | #include "clang/AST/EvaluatedExprVisitor.h" |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 26 | #include "clang/AST/DeclObjC.h" |
| 27 | #include "clang/AST/StmtCXX.h" |
| 28 | #include "clang/AST/StmtObjC.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" |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 35 | #include "clang/Basic/ConvertUTF.h" |
Zhongxing Xu | a1f3dba | 2009-05-20 01:55:10 +0000 | [diff] [blame] | 36 | #include <limits> |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 37 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 38 | using namespace sema; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 40 | SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, |
| 41 | unsigned ByteNo) const { |
Chris Lattner | 08f92e3 | 2010-11-17 07:37:15 +0000 | [diff] [blame] | 42 | return SL->getLocationOfByte(ByteNo, PP.getSourceManager(), |
| 43 | PP.getLangOptions(), PP.getTargetInfo()); |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 44 | } |
Chris Lattner | 08f92e3 | 2010-11-17 07:37:15 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 46 | |
Ryan Flynn | 4403a5e | 2009-08-06 03:00:50 +0000 | [diff] [blame] | 47 | /// CheckablePrintfAttr - does a function call have a "printf" attribute |
| 48 | /// and arguments that merit checking? |
| 49 | bool Sema::CheckablePrintfAttr(const FormatAttr *Format, CallExpr *TheCall) { |
| 50 | if (Format->getType() == "printf") return true; |
| 51 | if (Format->getType() == "printf0") { |
| 52 | // printf0 allows null "format" string; if so don't check format/args |
| 53 | unsigned format_idx = Format->getFormatIdx() - 1; |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 54 | // Does the index refer to the implicit object argument? |
| 55 | if (isa<CXXMemberCallExpr>(TheCall)) { |
| 56 | if (format_idx == 0) |
| 57 | return false; |
| 58 | --format_idx; |
| 59 | } |
Ryan Flynn | 4403a5e | 2009-08-06 03:00:50 +0000 | [diff] [blame] | 60 | if (format_idx < TheCall->getNumArgs()) { |
| 61 | Expr *Format = TheCall->getArg(format_idx)->IgnoreParenCasts(); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 62 | if (!Format->isNullPointerConstant(Context, |
| 63 | Expr::NPC_ValueDependentIsNull)) |
Ryan Flynn | 4403a5e | 2009-08-06 03:00:50 +0000 | [diff] [blame] | 64 | return true; |
| 65 | } |
| 66 | } |
| 67 | return false; |
| 68 | } |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 69 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 70 | /// Checks that a call expression's argument count is the desired number. |
| 71 | /// This is useful when doing custom type-checking. Returns true on error. |
| 72 | static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) { |
| 73 | unsigned argCount = call->getNumArgs(); |
| 74 | if (argCount == desiredArgCount) return false; |
| 75 | |
| 76 | if (argCount < desiredArgCount) |
| 77 | return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 78 | << 0 /*function call*/ << desiredArgCount << argCount |
| 79 | << call->getSourceRange(); |
| 80 | |
| 81 | // Highlight all the excess arguments. |
| 82 | SourceRange range(call->getArg(desiredArgCount)->getLocStart(), |
| 83 | call->getArg(argCount - 1)->getLocEnd()); |
| 84 | |
| 85 | return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args) |
| 86 | << 0 /*function call*/ << desiredArgCount << argCount |
| 87 | << call->getArg(1)->getSourceRange(); |
| 88 | } |
| 89 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 90 | ExprResult |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 91 | Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 92 | ExprResult TheCallResult(Owned(TheCall)); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 946928f | 2010-10-01 23:23:24 +0000 | [diff] [blame] | 94 | // Find out if any arguments are required to be integer constant expressions. |
| 95 | unsigned ICEArguments = 0; |
| 96 | ASTContext::GetBuiltinTypeError Error; |
| 97 | Context.GetBuiltinType(BuiltinID, Error, &ICEArguments); |
| 98 | if (Error != ASTContext::GE_None) |
| 99 | ICEArguments = 0; // Don't diagnose previously diagnosed errors. |
| 100 | |
| 101 | // If any arguments are required to be ICE's, check and diagnose. |
| 102 | for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) { |
| 103 | // Skip arguments not required to be ICE's. |
| 104 | if ((ICEArguments & (1 << ArgNo)) == 0) continue; |
| 105 | |
| 106 | llvm::APSInt Result; |
| 107 | if (SemaBuiltinConstantArg(TheCall, ArgNo, Result)) |
| 108 | return true; |
| 109 | ICEArguments &= ~(1 << ArgNo); |
| 110 | } |
| 111 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 112 | switch (BuiltinID) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 113 | case Builtin::BI__builtin___CFStringMakeConstantString: |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 114 | assert(TheCall->getNumArgs() == 1 && |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 115 | "Wrong # arguments to builtin CFStringMakeConstantString"); |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 116 | if (CheckObjCString(TheCall->getArg(0))) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 117 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 118 | break; |
Ted Kremenek | 49ff7a1 | 2008-07-09 17:58:53 +0000 | [diff] [blame] | 119 | case Builtin::BI__builtin_stdarg_start: |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 120 | case Builtin::BI__builtin_va_start: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 121 | if (SemaBuiltinVAStart(TheCall)) |
| 122 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 123 | break; |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 124 | case Builtin::BI__builtin_isgreater: |
| 125 | case Builtin::BI__builtin_isgreaterequal: |
| 126 | case Builtin::BI__builtin_isless: |
| 127 | case Builtin::BI__builtin_islessequal: |
| 128 | case Builtin::BI__builtin_islessgreater: |
| 129 | case Builtin::BI__builtin_isunordered: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 130 | if (SemaBuiltinUnorderedCompare(TheCall)) |
| 131 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 132 | break; |
Benjamin Kramer | e771a7a | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 133 | case Builtin::BI__builtin_fpclassify: |
| 134 | if (SemaBuiltinFPClassification(TheCall, 6)) |
| 135 | return ExprError(); |
| 136 | break; |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 137 | case Builtin::BI__builtin_isfinite: |
| 138 | case Builtin::BI__builtin_isinf: |
| 139 | case Builtin::BI__builtin_isinf_sign: |
| 140 | case Builtin::BI__builtin_isnan: |
| 141 | case Builtin::BI__builtin_isnormal: |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 142 | if (SemaBuiltinFPClassification(TheCall, 1)) |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 143 | return ExprError(); |
| 144 | break; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 145 | case Builtin::BI__builtin_shufflevector: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 146 | return SemaBuiltinShuffleVector(TheCall); |
| 147 | // TheCall will be freed by the smart pointer here, but that's fine, since |
| 148 | // SemaBuiltinShuffleVector guts it, but then doesn't release it. |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 149 | case Builtin::BI__builtin_prefetch: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 150 | if (SemaBuiltinPrefetch(TheCall)) |
| 151 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 152 | break; |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 153 | case Builtin::BI__builtin_object_size: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 154 | if (SemaBuiltinObjectSize(TheCall)) |
| 155 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 156 | break; |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 157 | case Builtin::BI__builtin_longjmp: |
| 158 | if (SemaBuiltinLongjmp(TheCall)) |
| 159 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 160 | break; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 161 | |
| 162 | case Builtin::BI__builtin_classify_type: |
| 163 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 164 | TheCall->setType(Context.IntTy); |
| 165 | break; |
Chris Lattner | 75c29a0 | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 166 | case Builtin::BI__builtin_constant_p: |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 167 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 168 | TheCall->setType(Context.IntTy); |
Chris Lattner | 75c29a0 | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 169 | break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 170 | case Builtin::BI__sync_fetch_and_add: |
| 171 | case Builtin::BI__sync_fetch_and_sub: |
| 172 | case Builtin::BI__sync_fetch_and_or: |
| 173 | case Builtin::BI__sync_fetch_and_and: |
| 174 | case Builtin::BI__sync_fetch_and_xor: |
| 175 | case Builtin::BI__sync_add_and_fetch: |
| 176 | case Builtin::BI__sync_sub_and_fetch: |
| 177 | case Builtin::BI__sync_and_and_fetch: |
| 178 | case Builtin::BI__sync_or_and_fetch: |
| 179 | case Builtin::BI__sync_xor_and_fetch: |
| 180 | case Builtin::BI__sync_val_compare_and_swap: |
| 181 | case Builtin::BI__sync_bool_compare_and_swap: |
| 182 | case Builtin::BI__sync_lock_test_and_set: |
| 183 | case Builtin::BI__sync_lock_release: |
Chris Lattner | 23aa9c8 | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 184 | case Builtin::BI__sync_swap: |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 185 | return SemaBuiltinAtomicOverloaded(move(TheCallResult)); |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // Since the target specific builtins for each arch overlap, only check those |
| 189 | // of the arch we are compiling for. |
| 190 | if (BuiltinID >= Builtin::FirstTSBuiltin) { |
| 191 | switch (Context.Target.getTriple().getArch()) { |
| 192 | case llvm::Triple::arm: |
| 193 | case llvm::Triple::thumb: |
| 194 | if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall)) |
| 195 | return ExprError(); |
| 196 | break; |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 197 | default: |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return move(TheCallResult); |
| 203 | } |
| 204 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 205 | // Get the valid immediate range for the specified NEON type code. |
| 206 | static unsigned RFT(unsigned t, bool shift = false) { |
| 207 | bool quad = t & 0x10; |
| 208 | |
| 209 | switch (t & 0x7) { |
| 210 | case 0: // i8 |
Nate Begeman | d69ec16 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 211 | return shift ? 7 : (8 << (int)quad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 212 | case 1: // i16 |
Nate Begeman | d69ec16 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 213 | return shift ? 15 : (4 << (int)quad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 214 | case 2: // i32 |
Nate Begeman | d69ec16 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 215 | return shift ? 31 : (2 << (int)quad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 216 | case 3: // i64 |
Nate Begeman | d69ec16 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 217 | return shift ? 63 : (1 << (int)quad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 218 | case 4: // f32 |
| 219 | assert(!shift && "cannot shift float types!"); |
Nate Begeman | d69ec16 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 220 | return (2 << (int)quad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 221 | case 5: // poly8 |
Bob Wilson | 42499f9 | 2010-12-10 19:45:06 +0000 | [diff] [blame] | 222 | return shift ? 7 : (8 << (int)quad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 223 | case 6: // poly16 |
Bob Wilson | 42499f9 | 2010-12-10 19:45:06 +0000 | [diff] [blame] | 224 | return shift ? 15 : (4 << (int)quad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 225 | case 7: // float16 |
| 226 | assert(!shift && "cannot shift float types!"); |
Nate Begeman | d69ec16 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 227 | return (4 << (int)quad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 228 | } |
| 229 | return 0; |
| 230 | } |
| 231 | |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 232 | bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 233 | llvm::APSInt Result; |
| 234 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 235 | unsigned mask = 0; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 236 | unsigned TV = 0; |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 237 | switch (BuiltinID) { |
Nate Begeman | a23326b | 2010-06-17 04:17:01 +0000 | [diff] [blame] | 238 | #define GET_NEON_OVERLOAD_CHECK |
| 239 | #include "clang/Basic/arm_neon.inc" |
| 240 | #undef GET_NEON_OVERLOAD_CHECK |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 243 | // For NEON intrinsics which are overloaded on vector element type, validate |
| 244 | // the immediate which specifies which variant to emit. |
| 245 | if (mask) { |
| 246 | unsigned ArgNo = TheCall->getNumArgs()-1; |
| 247 | if (SemaBuiltinConstantArg(TheCall, ArgNo, Result)) |
| 248 | return true; |
| 249 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 250 | TV = Result.getLimitedValue(32); |
| 251 | if ((TV > 31) || (mask & (1 << TV)) == 0) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 252 | return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code) |
| 253 | << TheCall->getArg(ArgNo)->getSourceRange(); |
| 254 | } |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 255 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 256 | // For NEON intrinsics which take an immediate value as part of the |
| 257 | // instruction, range check them here. |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 258 | unsigned i = 0, l = 0, u = 0; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 259 | switch (BuiltinID) { |
| 260 | default: return false; |
Nate Begeman | bb37f50 | 2010-07-29 22:48:34 +0000 | [diff] [blame] | 261 | case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break; |
| 262 | case ARM::BI__builtin_arm_usat: i = 1; u = 31; break; |
Nate Begeman | 99c40bb | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 263 | case ARM::BI__builtin_arm_vcvtr_f: |
| 264 | case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break; |
Nate Begeman | a23326b | 2010-06-17 04:17:01 +0000 | [diff] [blame] | 265 | #define GET_NEON_IMMEDIATE_CHECK |
| 266 | #include "clang/Basic/arm_neon.inc" |
| 267 | #undef GET_NEON_IMMEDIATE_CHECK |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 270 | // Check that the immediate argument is actually a constant. |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 271 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 272 | return true; |
| 273 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 274 | // Range check against the upper/lower values for this isntruction. |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 275 | unsigned Val = Result.getZExtValue(); |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 276 | if (Val < l || Val > (u + l)) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 277 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 278 | << l << u+l << TheCall->getArg(i)->getSourceRange(); |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 279 | |
Nate Begeman | 99c40bb | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 280 | // FIXME: VFP Intrinsics should error if VFP not present. |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 281 | return false; |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 282 | } |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 283 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 284 | /// CheckFunctionCall - Check a direct function call for various correctness |
| 285 | /// and safety properties not strictly enforced by the C type system. |
| 286 | bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall) { |
| 287 | // Get the IdentifierInfo* for the called function. |
| 288 | IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
| 289 | |
| 290 | // None of the checks below are needed for functions that don't have |
| 291 | // simple names (e.g., C++ conversion functions). |
| 292 | if (!FnInfo) |
| 293 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 295 | // FIXME: This mechanism should be abstracted to be less fragile and |
| 296 | // more efficient. For example, just map function ids to custom |
| 297 | // handlers. |
| 298 | |
Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 299 | // Printf and scanf checking. |
| 300 | for (specific_attr_iterator<FormatAttr> |
| 301 | i = FDecl->specific_attr_begin<FormatAttr>(), |
| 302 | e = FDecl->specific_attr_end<FormatAttr>(); i != e ; ++i) { |
| 303 | |
| 304 | const FormatAttr *Format = *i; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 305 | const bool b = Format->getType() == "scanf"; |
| 306 | if (b || CheckablePrintfAttr(Format, TheCall)) { |
Ted Kremenek | 3d692df | 2009-02-27 17:58:43 +0000 | [diff] [blame] | 307 | bool HasVAListArg = Format->getFirstArg() == 0; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 308 | CheckPrintfScanfArguments(TheCall, HasVAListArg, |
| 309 | Format->getFormatIdx() - 1, |
| 310 | HasVAListArg ? 0 : Format->getFirstArg() - 1, |
| 311 | !b); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 312 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 313 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | |
Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 315 | for (specific_attr_iterator<NonNullAttr> |
| 316 | i = FDecl->specific_attr_begin<NonNullAttr>(), |
| 317 | e = FDecl->specific_attr_end<NonNullAttr>(); i != e; ++i) { |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 318 | CheckNonNullArguments(*i, TheCall->getArgs(), |
| 319 | TheCall->getCallee()->getLocStart()); |
Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 320 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 321 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 322 | // Memset/memcpy/memmove handling |
| 323 | if (FDecl->getLinkage() == ExternalLinkage && |
| 324 | (!getLangOptions().CPlusPlus || FDecl->isExternC())) { |
| 325 | if (FnInfo->isStr("memset") || FnInfo->isStr("memcpy") || |
| 326 | FnInfo->isStr("memmove")) |
| 327 | CheckMemsetcpymoveArguments(TheCall, FnInfo); |
| 328 | } |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 329 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 330 | return false; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 333 | bool Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall) { |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 334 | // Printf checking. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 335 | const FormatAttr *Format = NDecl->getAttr<FormatAttr>(); |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 336 | if (!Format) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 337 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 339 | const VarDecl *V = dyn_cast<VarDecl>(NDecl); |
| 340 | if (!V) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 341 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 343 | QualType Ty = V->getType(); |
| 344 | if (!Ty->isBlockPointerType()) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 345 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 347 | const bool b = Format->getType() == "scanf"; |
| 348 | if (!b && !CheckablePrintfAttr(Format, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 349 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 351 | bool HasVAListArg = Format->getFirstArg() == 0; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 352 | CheckPrintfScanfArguments(TheCall, HasVAListArg, Format->getFormatIdx() - 1, |
| 353 | HasVAListArg ? 0 : Format->getFirstArg() - 1, !b); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 354 | |
| 355 | return false; |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 358 | /// SemaBuiltinAtomicOverloaded - We have a call to a function like |
| 359 | /// __sync_fetch_and_add, which is an overloaded function based on the pointer |
| 360 | /// type of its first argument. The main ActOnCallExpr routines have already |
| 361 | /// promoted the types of arguments because all of these calls are prototyped as |
| 362 | /// void(...). |
| 363 | /// |
| 364 | /// This function goes through and does final semantic checking for these |
| 365 | /// builtins, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 366 | ExprResult |
| 367 | Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) { |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 368 | CallExpr *TheCall = (CallExpr *)TheCallResult.get(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 369 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 370 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 371 | |
| 372 | // 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] | 373 | if (TheCall->getNumArgs() < 1) { |
| 374 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 375 | << 0 << 1 << TheCall->getNumArgs() |
| 376 | << TheCall->getCallee()->getSourceRange(); |
| 377 | return ExprError(); |
| 378 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 380 | // Inspect the first argument of the atomic builtin. This should always be |
| 381 | // a pointer type, whose element is an integral scalar or pointer type. |
| 382 | // Because it is a pointer type, we don't have to worry about any implicit |
| 383 | // casts here. |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 384 | // FIXME: We don't allow floating point scalars as input. |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 385 | Expr *FirstArg = TheCall->getArg(0); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 386 | const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>(); |
| 387 | if (!pointerType) { |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 388 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
| 389 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 390 | return ExprError(); |
| 391 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 393 | QualType ValType = pointerType->getPointeeType(); |
Chris Lattner | dd5fa7a | 2010-09-17 21:12:38 +0000 | [diff] [blame] | 394 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 395 | !ValType->isBlockPointerType()) { |
| 396 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr) |
| 397 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 398 | return ExprError(); |
| 399 | } |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 400 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 401 | switch (ValType.getObjCLifetime()) { |
| 402 | case Qualifiers::OCL_None: |
| 403 | case Qualifiers::OCL_ExplicitNone: |
| 404 | // okay |
| 405 | break; |
| 406 | |
| 407 | case Qualifiers::OCL_Weak: |
| 408 | case Qualifiers::OCL_Strong: |
| 409 | case Qualifiers::OCL_Autoreleasing: |
| 410 | Diag(DRE->getLocStart(), diag::err_arc_atomic_lifetime) |
| 411 | << ValType << FirstArg->getSourceRange(); |
| 412 | return ExprError(); |
| 413 | } |
| 414 | |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 415 | // The majority of builtins return a value, but a few have special return |
| 416 | // types, so allow them to override appropriately below. |
| 417 | QualType ResultType = ValType; |
| 418 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 419 | // We need to figure out which concrete builtin this maps onto. For example, |
| 420 | // __sync_fetch_and_add with a 2 byte object turns into |
| 421 | // __sync_fetch_and_add_2. |
| 422 | #define BUILTIN_ROW(x) \ |
| 423 | { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ |
| 424 | Builtin::BI##x##_8, Builtin::BI##x##_16 } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 426 | static const unsigned BuiltinIndices[][5] = { |
| 427 | BUILTIN_ROW(__sync_fetch_and_add), |
| 428 | BUILTIN_ROW(__sync_fetch_and_sub), |
| 429 | BUILTIN_ROW(__sync_fetch_and_or), |
| 430 | BUILTIN_ROW(__sync_fetch_and_and), |
| 431 | BUILTIN_ROW(__sync_fetch_and_xor), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 433 | BUILTIN_ROW(__sync_add_and_fetch), |
| 434 | BUILTIN_ROW(__sync_sub_and_fetch), |
| 435 | BUILTIN_ROW(__sync_and_and_fetch), |
| 436 | BUILTIN_ROW(__sync_or_and_fetch), |
| 437 | BUILTIN_ROW(__sync_xor_and_fetch), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 439 | BUILTIN_ROW(__sync_val_compare_and_swap), |
| 440 | BUILTIN_ROW(__sync_bool_compare_and_swap), |
| 441 | BUILTIN_ROW(__sync_lock_test_and_set), |
Chris Lattner | 23aa9c8 | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 442 | BUILTIN_ROW(__sync_lock_release), |
| 443 | BUILTIN_ROW(__sync_swap) |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 444 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | #undef BUILTIN_ROW |
| 446 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 447 | // Determine the index of the size. |
| 448 | unsigned SizeIndex; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 449 | switch (Context.getTypeSizeInChars(ValType).getQuantity()) { |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 450 | case 1: SizeIndex = 0; break; |
| 451 | case 2: SizeIndex = 1; break; |
| 452 | case 4: SizeIndex = 2; break; |
| 453 | case 8: SizeIndex = 3; break; |
| 454 | case 16: SizeIndex = 4; break; |
| 455 | default: |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 456 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size) |
| 457 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 458 | return ExprError(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 459 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 461 | // Each of these builtins has one pointer argument, followed by some number of |
| 462 | // values (0, 1 or 2) followed by a potentially empty varags list of stuff |
| 463 | // that we ignore. Find out which row of BuiltinIndices to read from as well |
| 464 | // as the number of fixed args. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 465 | unsigned BuiltinID = FDecl->getBuiltinID(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 466 | unsigned BuiltinIndex, NumFixed = 1; |
| 467 | switch (BuiltinID) { |
| 468 | default: assert(0 && "Unknown overloaded atomic builtin!"); |
| 469 | case Builtin::BI__sync_fetch_and_add: BuiltinIndex = 0; break; |
| 470 | case Builtin::BI__sync_fetch_and_sub: BuiltinIndex = 1; break; |
| 471 | case Builtin::BI__sync_fetch_and_or: BuiltinIndex = 2; break; |
| 472 | case Builtin::BI__sync_fetch_and_and: BuiltinIndex = 3; break; |
| 473 | case Builtin::BI__sync_fetch_and_xor: BuiltinIndex = 4; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 475 | case Builtin::BI__sync_add_and_fetch: BuiltinIndex = 5; break; |
| 476 | case Builtin::BI__sync_sub_and_fetch: BuiltinIndex = 6; break; |
| 477 | case Builtin::BI__sync_and_and_fetch: BuiltinIndex = 7; break; |
| 478 | case Builtin::BI__sync_or_and_fetch: BuiltinIndex = 8; break; |
| 479 | case Builtin::BI__sync_xor_and_fetch: BuiltinIndex = 9; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 481 | case Builtin::BI__sync_val_compare_and_swap: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 482 | BuiltinIndex = 10; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 483 | NumFixed = 2; |
| 484 | break; |
| 485 | case Builtin::BI__sync_bool_compare_and_swap: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 486 | BuiltinIndex = 11; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 487 | NumFixed = 2; |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 488 | ResultType = Context.BoolTy; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 489 | break; |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 490 | case Builtin::BI__sync_lock_test_and_set: BuiltinIndex = 12; break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 491 | case Builtin::BI__sync_lock_release: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 492 | BuiltinIndex = 13; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 493 | NumFixed = 0; |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 494 | ResultType = Context.VoidTy; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 495 | break; |
Chris Lattner | 23aa9c8 | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 496 | case Builtin::BI__sync_swap: BuiltinIndex = 14; break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 497 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 499 | // Now that we know how many fixed arguments we expect, first check that we |
| 500 | // have at least that many. |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 501 | if (TheCall->getNumArgs() < 1+NumFixed) { |
| 502 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 503 | << 0 << 1+NumFixed << TheCall->getNumArgs() |
| 504 | << TheCall->getCallee()->getSourceRange(); |
| 505 | return ExprError(); |
| 506 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | |
Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 508 | // Get the decl for the concrete builtin from this, we can tell what the |
| 509 | // concrete integer type we should convert to is. |
| 510 | unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; |
| 511 | const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID); |
| 512 | IdentifierInfo *NewBuiltinII = PP.getIdentifierInfo(NewBuiltinName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | FunctionDecl *NewBuiltinDecl = |
Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 514 | cast<FunctionDecl>(LazilyCreateBuiltin(NewBuiltinII, NewBuiltinID, |
| 515 | TUScope, false, DRE->getLocStart())); |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 516 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 517 | // The first argument --- the pointer --- has a fixed type; we |
| 518 | // deduce the types of the rest of the arguments accordingly. Walk |
| 519 | // the remaining arguments, converting them to the deduced value type. |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 520 | for (unsigned i = 0; i != NumFixed; ++i) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 521 | ExprResult Arg = TheCall->getArg(i+1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 523 | // If the argument is an implicit cast, then there was a promotion due to |
| 524 | // "...", just remove it now. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 525 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg.get())) { |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 526 | Arg = ICE->getSubExpr(); |
| 527 | ICE->setSubExpr(0); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 528 | TheCall->setArg(i+1, Arg.get()); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 529 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 530 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 531 | // GCC does an implicit conversion to the pointer or integer ValType. This |
| 532 | // can fail in some cases (1i -> int**), check for this error case now. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 533 | CastKind Kind = CK_Invalid; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 534 | ExprValueKind VK = VK_RValue; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 535 | CXXCastPath BasePath; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 536 | Arg = CheckCastTypes(Arg.get()->getLocStart(), Arg.get()->getSourceRange(), |
| 537 | ValType, Arg.take(), Kind, VK, BasePath); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 538 | if (Arg.isInvalid()) |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 539 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 540 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 541 | // Okay, we have something that *can* be converted to the right type. Check |
| 542 | // to see if there is a potentially weird extension going on here. This can |
| 543 | // happen when you do an atomic operation on something like an char* and |
| 544 | // pass in 42. The 42 gets converted to char. This is even more strange |
| 545 | // for things like 45.123 -> char, etc. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | // FIXME: Do this check. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 547 | Arg = ImpCastExprToType(Arg.take(), ValType, Kind, VK, &BasePath); |
| 548 | TheCall->setArg(i+1, Arg.get()); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 549 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 551 | // Switch the DeclRefExpr to refer to the new decl. |
| 552 | DRE->setDecl(NewBuiltinDecl); |
| 553 | DRE->setType(NewBuiltinDecl->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 555 | // Set the callee in the CallExpr. |
| 556 | // FIXME: This leaks the original parens and implicit casts. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 557 | ExprResult PromotedCall = UsualUnaryConversions(DRE); |
| 558 | if (PromotedCall.isInvalid()) |
| 559 | return ExprError(); |
| 560 | TheCall->setCallee(PromotedCall.take()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 561 | |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 562 | // Change the result type of the call to match the original value type. This |
| 563 | // is arbitrary, but the codegen for these builtins ins design to handle it |
| 564 | // gracefully. |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 565 | TheCall->setType(ResultType); |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 566 | |
| 567 | return move(TheCallResult); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 571 | /// CheckObjCString - Checks that the argument to the builtin |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 572 | /// CFString constructor is correct |
Steve Naroff | fd94262 | 2009-04-13 20:26:29 +0000 | [diff] [blame] | 573 | /// Note: It might also make sense to do the UTF-16 conversion here (would |
| 574 | /// simplify the backend). |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 575 | bool Sema::CheckObjCString(Expr *Arg) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 576 | Arg = Arg->IgnoreParenCasts(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 577 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); |
| 578 | |
| 579 | if (!Literal || Literal->isWide()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 580 | Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 581 | << Arg->getSourceRange(); |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 582 | return true; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 583 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 585 | if (Literal->containsNonAsciiOrNull()) { |
| 586 | llvm::StringRef String = Literal->getString(); |
| 587 | unsigned NumBytes = String.size(); |
| 588 | llvm::SmallVector<UTF16, 128> ToBuf(NumBytes); |
| 589 | const UTF8 *FromPtr = (UTF8 *)String.data(); |
| 590 | UTF16 *ToPtr = &ToBuf[0]; |
| 591 | |
| 592 | ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, |
| 593 | &ToPtr, ToPtr + NumBytes, |
| 594 | strictConversion); |
| 595 | // Check for conversion failure. |
| 596 | if (Result != conversionOK) |
| 597 | Diag(Arg->getLocStart(), |
| 598 | diag::warn_cfstring_truncated) << Arg->getSourceRange(); |
| 599 | } |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 600 | return false; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 603 | /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity. |
| 604 | /// Emit an error and return true on failure, return false on success. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 605 | bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) { |
| 606 | Expr *Fn = TheCall->getCallee(); |
| 607 | if (TheCall->getNumArgs() > 2) { |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 608 | Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 609 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 610 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
| 611 | << Fn->getSourceRange() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 612 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 613 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 614 | return true; |
| 615 | } |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 616 | |
| 617 | if (TheCall->getNumArgs() < 2) { |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 618 | return Diag(TheCall->getLocEnd(), |
| 619 | diag::err_typecheck_call_too_few_args_at_least) |
| 620 | << 0 /*function call*/ << 2 << TheCall->getNumArgs(); |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 623 | // Determine whether the current function is variadic or not. |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 624 | BlockScopeInfo *CurBlock = getCurBlock(); |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 625 | bool isVariadic; |
Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 626 | if (CurBlock) |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 627 | isVariadic = CurBlock->TheDecl->isVariadic(); |
Ted Kremenek | 9498d38 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 628 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
| 629 | isVariadic = FD->isVariadic(); |
| 630 | else |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 631 | isVariadic = getCurMethodDecl()->isVariadic(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 633 | if (!isVariadic) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 634 | Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function); |
| 635 | return true; |
| 636 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 637 | |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 638 | // Verify that the second argument to the builtin is the last argument of the |
| 639 | // current function or method. |
| 640 | bool SecondArgIsLastNamedArgument = false; |
Anders Carlsson | e2c1410 | 2008-02-13 01:22:59 +0000 | [diff] [blame] | 641 | const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 642 | |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 643 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { |
| 644 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 645 | // FIXME: This isn't correct for methods (results in bogus warning). |
| 646 | // Get the last formal in the current function. |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 647 | const ParmVarDecl *LastArg; |
Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 648 | if (CurBlock) |
| 649 | LastArg = *(CurBlock->TheDecl->param_end()-1); |
| 650 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 651 | LastArg = *(FD->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 652 | else |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 653 | LastArg = *(getCurMethodDecl()->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 654 | SecondArgIsLastNamedArgument = PV == LastArg; |
| 655 | } |
| 656 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 658 | if (!SecondArgIsLastNamedArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 659 | Diag(TheCall->getArg(1)->getLocStart(), |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 660 | diag::warn_second_parameter_of_va_start_not_last_named_argument); |
| 661 | return false; |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 662 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 663 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 664 | /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and |
| 665 | /// friends. This is declared to take (...), so we have to check everything. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 666 | bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { |
| 667 | if (TheCall->getNumArgs() < 2) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 668 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 669 | << 0 << 2 << TheCall->getNumArgs()/*function call*/; |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 670 | if (TheCall->getNumArgs() > 2) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | return Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 672 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 673 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 674 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
| 675 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 677 | ExprResult OrigArg0 = TheCall->getArg(0); |
| 678 | ExprResult OrigArg1 = TheCall->getArg(1); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 679 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 680 | // Do standard promotions between the two arguments, returning their common |
| 681 | // type. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 682 | QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 683 | if (OrigArg0.isInvalid() || OrigArg1.isInvalid()) |
| 684 | return true; |
Daniel Dunbar | 403bc2b | 2009-02-19 19:28:43 +0000 | [diff] [blame] | 685 | |
| 686 | // Make sure any conversions are pushed back into the call; this is |
| 687 | // type safe since unordered compare builtins are declared as "_Bool |
| 688 | // foo(...)". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 689 | TheCall->setArg(0, OrigArg0.get()); |
| 690 | TheCall->setArg(1, OrigArg1.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 691 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 692 | if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent()) |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 693 | return false; |
| 694 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 695 | // If the common type isn't a real floating type, then the arguments were |
| 696 | // invalid for this operation. |
| 697 | if (!Res->isRealFloatingType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 698 | return Diag(OrigArg0.get()->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 699 | diag::err_typecheck_call_invalid_ordered_compare) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 700 | << OrigArg0.get()->getType() << OrigArg1.get()->getType() |
| 701 | << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 702 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 703 | return false; |
| 704 | } |
| 705 | |
Benjamin Kramer | e771a7a | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 706 | /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like |
| 707 | /// __builtin_isnan and friends. This is declared to take (...), so we have |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 708 | /// to check everything. We expect the last argument to be a floating point |
| 709 | /// value. |
| 710 | bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) { |
| 711 | if (TheCall->getNumArgs() < NumArgs) |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 712 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 713 | << 0 << NumArgs << TheCall->getNumArgs()/*function call*/; |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 714 | if (TheCall->getNumArgs() > NumArgs) |
| 715 | return Diag(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 716 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 717 | << 0 /*function call*/ << NumArgs << TheCall->getNumArgs() |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 718 | << SourceRange(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 719 | (*(TheCall->arg_end()-1))->getLocEnd()); |
| 720 | |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 721 | Expr *OrigArg = TheCall->getArg(NumArgs-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 723 | if (OrigArg->isTypeDependent()) |
| 724 | return false; |
| 725 | |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 726 | // This operation requires a non-_Complex floating-point number. |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 727 | if (!OrigArg->getType()->isRealFloatingType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 728 | return Diag(OrigArg->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 729 | diag::err_typecheck_call_invalid_unary_fp) |
| 730 | << OrigArg->getType() << OrigArg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 731 | |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 732 | // If this is an implicit conversion from float -> double, remove it. |
| 733 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) { |
| 734 | Expr *CastArg = Cast->getSubExpr(); |
| 735 | if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) { |
| 736 | assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) && |
| 737 | "promotion from float to double is the only expected cast here"); |
| 738 | Cast->setSubExpr(0); |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 739 | TheCall->setArg(NumArgs-1, CastArg); |
| 740 | OrigArg = CastArg; |
| 741 | } |
| 742 | } |
| 743 | |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 744 | return false; |
| 745 | } |
| 746 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 747 | /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. |
| 748 | // This is declared to take (...), so we have to check everything. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 749 | ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 750 | if (TheCall->getNumArgs() < 2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 751 | return ExprError(Diag(TheCall->getLocEnd(), |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 752 | diag::err_typecheck_call_too_few_args_at_least) |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 753 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 754 | << TheCall->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 755 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 756 | // Determine which of the following types of shufflevector we're checking: |
| 757 | // 1) unary, vector mask: (lhs, mask) |
| 758 | // 2) binary, vector mask: (lhs, rhs, mask) |
| 759 | // 3) binary, scalar mask: (lhs, rhs, index, ..., index) |
| 760 | QualType resType = TheCall->getArg(0)->getType(); |
| 761 | unsigned numElements = 0; |
| 762 | |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 763 | if (!TheCall->getArg(0)->isTypeDependent() && |
| 764 | !TheCall->getArg(1)->isTypeDependent()) { |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 765 | QualType LHSType = TheCall->getArg(0)->getType(); |
| 766 | QualType RHSType = TheCall->getArg(1)->getType(); |
| 767 | |
| 768 | if (!LHSType->isVectorType() || !RHSType->isVectorType()) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 769 | Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 770 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 771 | TheCall->getArg(1)->getLocEnd()); |
| 772 | return ExprError(); |
| 773 | } |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 774 | |
| 775 | numElements = LHSType->getAs<VectorType>()->getNumElements(); |
| 776 | unsigned numResElements = TheCall->getNumArgs() - 2; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 777 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 778 | // Check to see if we have a call with 2 vector arguments, the unary shuffle |
| 779 | // with mask. If so, verify that RHS is an integer vector type with the |
| 780 | // same number of elts as lhs. |
| 781 | if (TheCall->getNumArgs() == 2) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 782 | if (!RHSType->hasIntegerRepresentation() || |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 783 | RHSType->getAs<VectorType>()->getNumElements() != numElements) |
| 784 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
| 785 | << SourceRange(TheCall->getArg(1)->getLocStart(), |
| 786 | TheCall->getArg(1)->getLocEnd()); |
| 787 | numResElements = numElements; |
| 788 | } |
| 789 | else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 790 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 791 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 792 | TheCall->getArg(1)->getLocEnd()); |
| 793 | return ExprError(); |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 794 | } else if (numElements != numResElements) { |
| 795 | QualType eltType = LHSType->getAs<VectorType>()->getElementType(); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 796 | resType = Context.getVectorType(eltType, numResElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 797 | VectorType::GenericVector); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 798 | } |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 802 | if (TheCall->getArg(i)->isTypeDependent() || |
| 803 | TheCall->getArg(i)->isValueDependent()) |
| 804 | continue; |
| 805 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 806 | llvm::APSInt Result(32); |
| 807 | if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) |
| 808 | return ExprError(Diag(TheCall->getLocStart(), |
| 809 | diag::err_shufflevector_nonconstant_argument) |
| 810 | << TheCall->getArg(i)->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 811 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 812 | if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 813 | return ExprError(Diag(TheCall->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 814 | diag::err_shufflevector_argument_too_large) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 815 | << TheCall->getArg(i)->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | llvm::SmallVector<Expr*, 32> exprs; |
| 819 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 820 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 821 | exprs.push_back(TheCall->getArg(i)); |
| 822 | TheCall->setArg(i, 0); |
| 823 | } |
| 824 | |
Nate Begeman | a88dc30 | 2009-08-12 02:10:25 +0000 | [diff] [blame] | 825 | return Owned(new (Context) ShuffleVectorExpr(Context, exprs.begin(), |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 826 | exprs.size(), resType, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 827 | TheCall->getCallee()->getLocStart(), |
| 828 | TheCall->getRParenLoc())); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 829 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 830 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 831 | /// SemaBuiltinPrefetch - Handle __builtin_prefetch. |
| 832 | // This is declared to take (const void*, ...) and can take two |
| 833 | // optional constant int args. |
| 834 | bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 835 | unsigned NumArgs = TheCall->getNumArgs(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 836 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 837 | if (NumArgs > 3) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 838 | return Diag(TheCall->getLocEnd(), |
| 839 | diag::err_typecheck_call_too_many_args_at_most) |
| 840 | << 0 /*function call*/ << 3 << NumArgs |
| 841 | << TheCall->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 842 | |
| 843 | // Argument 0 is checked for us and the remaining arguments must be |
| 844 | // constant integers. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 845 | for (unsigned i = 1; i != NumArgs; ++i) { |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 846 | Expr *Arg = TheCall->getArg(i); |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 847 | |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 848 | llvm::APSInt Result; |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 849 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 850 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 851 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 852 | // FIXME: gcc issues a warning and rewrites these to 0. These |
| 853 | // seems especially odd for the third argument since the default |
| 854 | // is 3. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 855 | if (i == 1) { |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 856 | if (Result.getLimitedValue() > 1) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 857 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 858 | << "0" << "1" << Arg->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 859 | } else { |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 860 | if (Result.getLimitedValue() > 3) |
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" << "3" << Arg->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 866 | return false; |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 869 | /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr |
| 870 | /// TheCall is a constant expression. |
| 871 | bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum, |
| 872 | llvm::APSInt &Result) { |
| 873 | Expr *Arg = TheCall->getArg(ArgNum); |
| 874 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 875 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 876 | |
| 877 | if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; |
| 878 | |
| 879 | if (!Arg->isIntegerConstantExpr(Result, Context)) |
| 880 | return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type) |
Eric Christopher | 5e89655 | 2010-04-19 18:23:02 +0000 | [diff] [blame] | 881 | << FDecl->getDeclName() << Arg->getSourceRange(); |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 882 | |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 883 | return false; |
| 884 | } |
| 885 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 886 | /// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr, |
| 887 | /// int type). This simply type checks that type is one of the defined |
| 888 | /// constants (0-3). |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 889 | // For compatibility check 0-3, llvm only handles 0 and 2. |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 890 | bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) { |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 891 | llvm::APSInt Result; |
| 892 | |
| 893 | // Check constant-ness first. |
| 894 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 895 | return true; |
| 896 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 897 | Expr *Arg = TheCall->getArg(1); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 898 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 899 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 900 | << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | return false; |
| 904 | } |
| 905 | |
Eli Friedman | 586d6a8 | 2009-05-03 06:04:26 +0000 | [diff] [blame] | 906 | /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 907 | /// This checks that val is a constant 1. |
| 908 | bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { |
| 909 | Expr *Arg = TheCall->getArg(1); |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 910 | llvm::APSInt Result; |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 911 | |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 912 | // TODO: This is less than ideal. Overload this to take a value. |
| 913 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 914 | return true; |
| 915 | |
| 916 | if (Result != 1) |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 917 | return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val) |
| 918 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
| 919 | |
| 920 | return false; |
| 921 | } |
| 922 | |
Ted Kremenek | b43e8ad | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 923 | // Handle i > 1 ? "x" : "y", recursively. |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 924 | bool Sema::SemaCheckStringLiteral(const Expr *E, const CallExpr *TheCall, |
| 925 | bool HasVAListArg, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 926 | unsigned format_idx, unsigned firstDataArg, |
| 927 | bool isPrintf) { |
Ted Kremenek | 4fe6441 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 928 | tryAgain: |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 929 | if (E->isTypeDependent() || E->isValueDependent()) |
| 930 | return false; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 931 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 932 | E = E->IgnoreParens(); |
| 933 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 934 | switch (E->getStmtClass()) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 935 | case Stmt::BinaryConditionalOperatorClass: |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 936 | case Stmt::ConditionalOperatorClass: { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 937 | const AbstractConditionalOperator *C = cast<AbstractConditionalOperator>(E); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 938 | return SemaCheckStringLiteral(C->getTrueExpr(), TheCall, HasVAListArg, |
| 939 | format_idx, firstDataArg, isPrintf) |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 940 | && SemaCheckStringLiteral(C->getFalseExpr(), TheCall, HasVAListArg, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 941 | format_idx, firstDataArg, isPrintf); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Ted Kremenek | 95355bb | 2010-09-09 03:51:42 +0000 | [diff] [blame] | 944 | case Stmt::IntegerLiteralClass: |
| 945 | // Technically -Wformat-nonliteral does not warn about this case. |
| 946 | // The behavior of printf and friends in this case is implementation |
| 947 | // dependent. Ideally if the format string cannot be null then |
| 948 | // it should have a 'nonnull' attribute in the function prototype. |
| 949 | return true; |
| 950 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 951 | case Stmt::ImplicitCastExprClass: { |
Ted Kremenek | 4fe6441 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 952 | E = cast<ImplicitCastExpr>(E)->getSubExpr(); |
| 953 | goto tryAgain; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 954 | } |
| 955 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 956 | case Stmt::OpaqueValueExprClass: |
| 957 | if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) { |
| 958 | E = src; |
| 959 | goto tryAgain; |
| 960 | } |
| 961 | return false; |
| 962 | |
Ted Kremenek | b43e8ad | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 963 | case Stmt::PredefinedExprClass: |
| 964 | // While __func__, etc., are technically not string literals, they |
| 965 | // cannot contain format specifiers and thus are not a security |
| 966 | // liability. |
| 967 | return true; |
| 968 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 969 | case Stmt::DeclRefExprClass: { |
| 970 | const DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 971 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 972 | // As an exception, do not flag errors for variables binding to |
| 973 | // const string literals. |
| 974 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 975 | bool isConstant = false; |
| 976 | QualType T = DR->getType(); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 977 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 978 | if (const ArrayType *AT = Context.getAsArrayType(T)) { |
| 979 | isConstant = AT->getElementType().isConstant(Context); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 980 | } else if (const PointerType *PT = T->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 981 | isConstant = T.isConstant(Context) && |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 982 | PT->getPointeeType().isConstant(Context); |
| 983 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 985 | if (isConstant) { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 986 | if (const Expr *Init = VD->getAnyInitializer()) |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 987 | return SemaCheckStringLiteral(Init, TheCall, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 988 | HasVAListArg, format_idx, firstDataArg, |
| 989 | isPrintf); |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 990 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 991 | |
Anders Carlsson | d966a55 | 2009-06-28 19:55:58 +0000 | [diff] [blame] | 992 | // For vprintf* functions (i.e., HasVAListArg==true), we add a |
| 993 | // special check to see if the format string is a function parameter |
| 994 | // of the function calling the printf function. If the function |
| 995 | // has an attribute indicating it is a printf-like function, then we |
| 996 | // should suppress warnings concerning non-literals being used in a call |
| 997 | // to a vprintf function. For example: |
| 998 | // |
| 999 | // void |
| 1000 | // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){ |
| 1001 | // va_list ap; |
| 1002 | // va_start(ap, fmt); |
| 1003 | // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". |
| 1004 | // ... |
| 1005 | // |
| 1006 | // |
| 1007 | // FIXME: We don't have full attribute support yet, so just check to see |
| 1008 | // if the argument is a DeclRefExpr that references a parameter. We'll |
| 1009 | // add proper support for checking the attribute later. |
| 1010 | if (HasVAListArg) |
| 1011 | if (isa<ParmVarDecl>(VD)) |
| 1012 | return true; |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1013 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1014 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1015 | return false; |
| 1016 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1017 | |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1018 | case Stmt::CallExprClass: { |
| 1019 | const CallExpr *CE = cast<CallExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1020 | if (const ImplicitCastExpr *ICE |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1021 | = dyn_cast<ImplicitCastExpr>(CE->getCallee())) { |
| 1022 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) { |
| 1023 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1024 | if (const FormatArgAttr *FA = FD->getAttr<FormatArgAttr>()) { |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1025 | unsigned ArgIndex = FA->getFormatIdx(); |
| 1026 | const Expr *Arg = CE->getArg(ArgIndex - 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1027 | |
| 1028 | return SemaCheckStringLiteral(Arg, TheCall, HasVAListArg, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1029 | format_idx, firstDataArg, isPrintf); |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1034 | |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1035 | return false; |
| 1036 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1037 | case Stmt::ObjCStringLiteralClass: |
| 1038 | case Stmt::StringLiteralClass: { |
| 1039 | const StringLiteral *StrE = NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1040 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1041 | if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1042 | StrE = ObjCFExpr->getString(); |
| 1043 | else |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1044 | StrE = cast<StringLiteral>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1046 | if (StrE) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1047 | CheckFormatString(StrE, E, TheCall, HasVAListArg, format_idx, |
| 1048 | firstDataArg, isPrintf); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1049 | return true; |
| 1050 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1051 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1052 | return false; |
| 1053 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1054 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1055 | default: |
| 1056 | return false; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1057 | } |
| 1058 | } |
| 1059 | |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1060 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1061 | Sema::CheckNonNullArguments(const NonNullAttr *NonNull, |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1062 | const Expr * const *ExprArgs, |
| 1063 | SourceLocation CallSiteLoc) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1064 | for (NonNullAttr::args_iterator i = NonNull->args_begin(), |
| 1065 | e = NonNull->args_end(); |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1066 | i != e; ++i) { |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1067 | const Expr *ArgExpr = ExprArgs[*i]; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1068 | if (ArgExpr->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1069 | Expr::NPC_ValueDependentIsNotNull)) |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1070 | Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1071 | } |
| 1072 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1073 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1074 | /// CheckPrintfScanfArguments - Check calls to printf and scanf (and similar |
| 1075 | /// functions) for correct use of format strings. |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1076 | void |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1077 | Sema::CheckPrintfScanfArguments(const CallExpr *TheCall, bool HasVAListArg, |
| 1078 | unsigned format_idx, unsigned firstDataArg, |
| 1079 | bool isPrintf) { |
| 1080 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1081 | const Expr *Fn = TheCall->getCallee(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1082 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1083 | // The way the format attribute works in GCC, the implicit this argument |
| 1084 | // of member functions is counted. However, it doesn't appear in our own |
| 1085 | // lists, so decrement format_idx in that case. |
| 1086 | if (isa<CXXMemberCallExpr>(TheCall)) { |
Chandler Carruth | 9263a30 | 2010-11-16 08:49:43 +0000 | [diff] [blame] | 1087 | const CXXMethodDecl *method_decl = |
| 1088 | dyn_cast<CXXMethodDecl>(TheCall->getCalleeDecl()); |
| 1089 | if (method_decl && method_decl->isInstance()) { |
| 1090 | // Catch a format attribute mistakenly referring to the object argument. |
| 1091 | if (format_idx == 0) |
| 1092 | return; |
| 1093 | --format_idx; |
| 1094 | if(firstDataArg != 0) |
| 1095 | --firstDataArg; |
| 1096 | } |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1099 | // CHECK: printf/scanf-like function is called with no format string. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1100 | if (format_idx >= TheCall->getNumArgs()) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1101 | Diag(TheCall->getRParenLoc(), diag::warn_missing_format_string) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1102 | << Fn->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1103 | return; |
| 1104 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1106 | const Expr *OrigFormatExpr = TheCall->getArg(format_idx)->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1108 | // CHECK: format string is not a string literal. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1109 | // |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1110 | // Dynamically generated format strings are difficult to |
| 1111 | // automatically vet at compile time. Requiring that format strings |
| 1112 | // are string literals: (1) permits the checking of format strings by |
| 1113 | // the compiler and thereby (2) can practically remove the source of |
| 1114 | // many format string exploits. |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1115 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1116 | // Format string can be either ObjC string (e.g. @"%d") or |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1117 | // C string (e.g. "%d") |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1118 | // 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] | 1119 | // the same format string checking logic for both ObjC and C strings. |
Chris Lattner | 1cd3e1f | 2009-04-29 04:49:34 +0000 | [diff] [blame] | 1120 | if (SemaCheckStringLiteral(OrigFormatExpr, TheCall, HasVAListArg, format_idx, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1121 | firstDataArg, isPrintf)) |
Chris Lattner | 1cd3e1f | 2009-04-29 04:49:34 +0000 | [diff] [blame] | 1122 | return; // Literal format string found, check done! |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1123 | |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1124 | // If there are no arguments specified, warn with -Wformat-security, otherwise |
| 1125 | // warn only with -Wformat-nonliteral. |
| 1126 | if (TheCall->getNumArgs() == format_idx+1) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1127 | Diag(TheCall->getArg(format_idx)->getLocStart(), |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1128 | diag::warn_format_nonliteral_noargs) |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1129 | << OrigFormatExpr->getSourceRange(); |
| 1130 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1131 | Diag(TheCall->getArg(format_idx)->getLocStart(), |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1132 | diag::warn_format_nonliteral) |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1133 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1134 | } |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1135 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1136 | namespace { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1137 | class CheckFormatHandler : public analyze_format_string::FormatStringHandler { |
| 1138 | protected: |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1139 | Sema &S; |
| 1140 | const StringLiteral *FExpr; |
| 1141 | const Expr *OrigFormatExpr; |
Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1142 | const unsigned FirstDataArg; |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1143 | const unsigned NumDataArgs; |
| 1144 | const bool IsObjCLiteral; |
| 1145 | const char *Beg; // Start of format string. |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1146 | const bool HasVAListArg; |
| 1147 | const CallExpr *TheCall; |
| 1148 | unsigned FormatIdx; |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1149 | llvm::BitVector CoveredArgs; |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1150 | bool usesPositionalArgs; |
| 1151 | bool atFirstArg; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1152 | public: |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1153 | CheckFormatHandler(Sema &s, const StringLiteral *fexpr, |
Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1154 | const Expr *origFormatExpr, unsigned firstDataArg, |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1155 | unsigned numDataArgs, bool isObjCLiteral, |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1156 | const char *beg, bool hasVAListArg, |
| 1157 | const CallExpr *theCall, unsigned formatIdx) |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1158 | : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), |
Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1159 | FirstDataArg(firstDataArg), |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1160 | NumDataArgs(numDataArgs), |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1161 | IsObjCLiteral(isObjCLiteral), Beg(beg), |
| 1162 | HasVAListArg(hasVAListArg), |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1163 | TheCall(theCall), FormatIdx(formatIdx), |
| 1164 | usesPositionalArgs(false), atFirstArg(true) { |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1165 | CoveredArgs.resize(numDataArgs); |
| 1166 | CoveredArgs.reset(); |
| 1167 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1168 | |
Ted Kremenek | 07d161f | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 1169 | void DoneProcessing(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1170 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1171 | void HandleIncompleteSpecifier(const char *startSpecifier, |
| 1172 | unsigned specifierLen); |
| 1173 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1174 | virtual void HandleInvalidPosition(const char *startSpecifier, |
| 1175 | unsigned specifierLen, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1176 | analyze_format_string::PositionContext p); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1177 | |
| 1178 | virtual void HandleZeroPosition(const char *startPos, unsigned posLen); |
| 1179 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1180 | void HandleNullChar(const char *nullCharacter); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1181 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1182 | protected: |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1183 | bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, |
| 1184 | const char *startSpec, |
| 1185 | unsigned specifierLen, |
| 1186 | const char *csStart, unsigned csLen); |
| 1187 | |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 1188 | SourceRange getFormatStringRange(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1189 | CharSourceRange getSpecifierRange(const char *startSpecifier, |
| 1190 | unsigned specifierLen); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1191 | SourceLocation getLocationOfByte(const char *x); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1192 | |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1193 | const Expr *getDataArg(unsigned i) const; |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 1194 | |
| 1195 | bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, |
| 1196 | const analyze_format_string::ConversionSpecifier &CS, |
| 1197 | const char *startSpecifier, unsigned specifierLen, |
| 1198 | unsigned argIndex); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1199 | }; |
| 1200 | } |
| 1201 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1202 | SourceRange CheckFormatHandler::getFormatStringRange() { |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1203 | return OrigFormatExpr->getSourceRange(); |
| 1204 | } |
| 1205 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1206 | CharSourceRange CheckFormatHandler:: |
| 1207 | getSpecifierRange(const char *startSpecifier, unsigned specifierLen) { |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 1208 | SourceLocation Start = getLocationOfByte(startSpecifier); |
| 1209 | SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1); |
| 1210 | |
| 1211 | // Advance the end SourceLocation by one due to half-open ranges. |
| 1212 | End = End.getFileLocWithOffset(1); |
| 1213 | |
| 1214 | return CharSourceRange::getCharRange(Start, End); |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1217 | SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1218 | return S.getLocationOfStringLiteralByte(FExpr, x - Beg); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1221 | void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, |
| 1222 | unsigned specifierLen){ |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 1223 | SourceLocation Loc = getLocationOfByte(startSpecifier); |
| 1224 | S.Diag(Loc, diag::warn_printf_incomplete_specifier) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1225 | << getSpecifierRange(startSpecifier, specifierLen); |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1228 | void |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1229 | CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen, |
| 1230 | analyze_format_string::PositionContext p) { |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1231 | SourceLocation Loc = getLocationOfByte(startPos); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1232 | S.Diag(Loc, diag::warn_format_invalid_positional_specifier) |
| 1233 | << (unsigned) p << getSpecifierRange(startPos, posLen); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1236 | void CheckFormatHandler::HandleZeroPosition(const char *startPos, |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1237 | unsigned posLen) { |
| 1238 | SourceLocation Loc = getLocationOfByte(startPos); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1239 | S.Diag(Loc, diag::warn_format_zero_positional_specifier) |
| 1240 | << getSpecifierRange(startPos, posLen); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1243 | void CheckFormatHandler::HandleNullChar(const char *nullCharacter) { |
Ted Kremenek | 0c06944 | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 1244 | if (!IsObjCLiteral) { |
| 1245 | // The presence of a null character is likely an error. |
| 1246 | S.Diag(getLocationOfByte(nullCharacter), |
| 1247 | diag::warn_printf_format_string_contains_null_char) |
| 1248 | << getFormatStringRange(); |
| 1249 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1250 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1251 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1252 | const Expr *CheckFormatHandler::getDataArg(unsigned i) const { |
| 1253 | return TheCall->getArg(FirstDataArg + i); |
| 1254 | } |
| 1255 | |
| 1256 | void CheckFormatHandler::DoneProcessing() { |
| 1257 | // Does the number of data arguments exceed the number of |
| 1258 | // format conversions in the format string? |
| 1259 | if (!HasVAListArg) { |
| 1260 | // Find any arguments that weren't covered. |
| 1261 | CoveredArgs.flip(); |
| 1262 | signed notCoveredArg = CoveredArgs.find_first(); |
| 1263 | if (notCoveredArg >= 0) { |
| 1264 | assert((unsigned)notCoveredArg < NumDataArgs); |
| 1265 | S.Diag(getDataArg((unsigned) notCoveredArg)->getLocStart(), |
| 1266 | diag::warn_printf_data_arg_not_used) |
| 1267 | << getFormatStringRange(); |
| 1268 | } |
| 1269 | } |
| 1270 | } |
| 1271 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1272 | bool |
| 1273 | CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, |
| 1274 | SourceLocation Loc, |
| 1275 | const char *startSpec, |
| 1276 | unsigned specifierLen, |
| 1277 | const char *csStart, |
| 1278 | unsigned csLen) { |
| 1279 | |
| 1280 | bool keepGoing = true; |
| 1281 | if (argIndex < NumDataArgs) { |
| 1282 | // Consider the argument coverered, even though the specifier doesn't |
| 1283 | // make sense. |
| 1284 | CoveredArgs.set(argIndex); |
| 1285 | } |
| 1286 | else { |
| 1287 | // If argIndex exceeds the number of data arguments we |
| 1288 | // don't issue a warning because that is just a cascade of warnings (and |
| 1289 | // they may have intended '%%' anyway). We don't want to continue processing |
| 1290 | // the format string after this point, however, as we will like just get |
| 1291 | // gibberish when trying to match arguments. |
| 1292 | keepGoing = false; |
| 1293 | } |
| 1294 | |
| 1295 | S.Diag(Loc, diag::warn_format_invalid_conversion) |
| 1296 | << llvm::StringRef(csStart, csLen) |
| 1297 | << getSpecifierRange(startSpec, specifierLen); |
| 1298 | |
| 1299 | return keepGoing; |
| 1300 | } |
| 1301 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 1302 | bool |
| 1303 | CheckFormatHandler::CheckNumArgs( |
| 1304 | const analyze_format_string::FormatSpecifier &FS, |
| 1305 | const analyze_format_string::ConversionSpecifier &CS, |
| 1306 | const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { |
| 1307 | |
| 1308 | if (argIndex >= NumDataArgs) { |
| 1309 | if (FS.usesPositionalArg()) { |
| 1310 | S.Diag(getLocationOfByte(CS.getStart()), |
| 1311 | diag::warn_printf_positional_arg_exceeds_data_args) |
| 1312 | << (argIndex+1) << NumDataArgs |
| 1313 | << getSpecifierRange(startSpecifier, specifierLen); |
| 1314 | } |
| 1315 | else { |
| 1316 | S.Diag(getLocationOfByte(CS.getStart()), |
| 1317 | diag::warn_printf_insufficient_data_args) |
| 1318 | << getSpecifierRange(startSpecifier, specifierLen); |
| 1319 | } |
| 1320 | |
| 1321 | return false; |
| 1322 | } |
| 1323 | return true; |
| 1324 | } |
| 1325 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1326 | //===--- CHECK: Printf format string checking ------------------------------===// |
| 1327 | |
| 1328 | namespace { |
| 1329 | class CheckPrintfHandler : public CheckFormatHandler { |
| 1330 | public: |
| 1331 | CheckPrintfHandler(Sema &s, const StringLiteral *fexpr, |
| 1332 | const Expr *origFormatExpr, unsigned firstDataArg, |
| 1333 | unsigned numDataArgs, bool isObjCLiteral, |
| 1334 | const char *beg, bool hasVAListArg, |
| 1335 | const CallExpr *theCall, unsigned formatIdx) |
| 1336 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
| 1337 | numDataArgs, isObjCLiteral, beg, hasVAListArg, |
| 1338 | theCall, formatIdx) {} |
| 1339 | |
| 1340 | |
| 1341 | bool HandleInvalidPrintfConversionSpecifier( |
| 1342 | const analyze_printf::PrintfSpecifier &FS, |
| 1343 | const char *startSpecifier, |
| 1344 | unsigned specifierLen); |
| 1345 | |
| 1346 | bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, |
| 1347 | const char *startSpecifier, |
| 1348 | unsigned specifierLen); |
| 1349 | |
| 1350 | bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, |
| 1351 | const char *startSpecifier, unsigned specifierLen); |
| 1352 | void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, |
| 1353 | const analyze_printf::OptionalAmount &Amt, |
| 1354 | unsigned type, |
| 1355 | const char *startSpecifier, unsigned specifierLen); |
| 1356 | void HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
| 1357 | const analyze_printf::OptionalFlag &flag, |
| 1358 | const char *startSpecifier, unsigned specifierLen); |
| 1359 | void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, |
| 1360 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 1361 | const analyze_printf::OptionalFlag &flag, |
| 1362 | const char *startSpecifier, unsigned specifierLen); |
| 1363 | }; |
| 1364 | } |
| 1365 | |
| 1366 | bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( |
| 1367 | const analyze_printf::PrintfSpecifier &FS, |
| 1368 | const char *startSpecifier, |
| 1369 | unsigned specifierLen) { |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1370 | const analyze_printf::PrintfConversionSpecifier &CS = |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1371 | FS.getConversionSpecifier(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1372 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1373 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 1374 | getLocationOfByte(CS.getStart()), |
| 1375 | startSpecifier, specifierLen, |
| 1376 | CS.getStart(), CS.getLength()); |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1379 | bool CheckPrintfHandler::HandleAmount( |
| 1380 | const analyze_format_string::OptionalAmount &Amt, |
| 1381 | unsigned k, const char *startSpecifier, |
| 1382 | unsigned specifierLen) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1383 | |
| 1384 | if (Amt.hasDataArgument()) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1385 | if (!HasVAListArg) { |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1386 | unsigned argIndex = Amt.getArgIndex(); |
| 1387 | if (argIndex >= NumDataArgs) { |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1388 | S.Diag(getLocationOfByte(Amt.getStart()), |
| 1389 | diag::warn_printf_asterisk_missing_arg) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1390 | << k << getSpecifierRange(startSpecifier, specifierLen); |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1391 | // Don't do any more checking. We will just emit |
| 1392 | // spurious errors. |
| 1393 | return false; |
| 1394 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1395 | |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1396 | // Type check the data argument. It should be an 'int'. |
Ted Kremenek | 31f8e32 | 2010-01-29 23:32:22 +0000 | [diff] [blame] | 1397 | // Although not in conformance with C99, we also allow the argument to be |
| 1398 | // an 'unsigned int' as that is a reasonably safe case. GCC also |
| 1399 | // doesn't emit a warning for that case. |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1400 | CoveredArgs.set(argIndex); |
| 1401 | const Expr *Arg = getDataArg(argIndex); |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1402 | QualType T = Arg->getType(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1403 | |
| 1404 | const analyze_printf::ArgTypeResult &ATR = Amt.getArgType(S.Context); |
| 1405 | assert(ATR.isValid()); |
| 1406 | |
| 1407 | if (!ATR.matchesType(S.Context, T)) { |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1408 | S.Diag(getLocationOfByte(Amt.getStart()), |
| 1409 | diag::warn_printf_asterisk_wrong_type) |
| 1410 | << k |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1411 | << ATR.getRepresentativeType(S.Context) << T |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1412 | << getSpecifierRange(startSpecifier, specifierLen) |
Ted Kremenek | d635c5f | 2010-01-30 00:49:51 +0000 | [diff] [blame] | 1413 | << Arg->getSourceRange(); |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1414 | // Don't do any more checking. We will just emit |
| 1415 | // spurious errors. |
| 1416 | return false; |
| 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | return true; |
| 1421 | } |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1422 | |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1423 | void CheckPrintfHandler::HandleInvalidAmount( |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1424 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1425 | const analyze_printf::OptionalAmount &Amt, |
| 1426 | unsigned type, |
| 1427 | const char *startSpecifier, |
| 1428 | unsigned specifierLen) { |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1429 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 1430 | FS.getConversionSpecifier(); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1431 | switch (Amt.getHowSpecified()) { |
| 1432 | case analyze_printf::OptionalAmount::Constant: |
| 1433 | S.Diag(getLocationOfByte(Amt.getStart()), |
| 1434 | diag::warn_printf_nonsensical_optional_amount) |
| 1435 | << type |
| 1436 | << CS.toString() |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1437 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1438 | << FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1439 | Amt.getConstantLength())); |
| 1440 | break; |
| 1441 | |
| 1442 | default: |
| 1443 | S.Diag(getLocationOfByte(Amt.getStart()), |
| 1444 | diag::warn_printf_nonsensical_optional_amount) |
| 1445 | << type |
| 1446 | << CS.toString() |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1447 | << getSpecifierRange(startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1448 | break; |
| 1449 | } |
| 1450 | } |
| 1451 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1452 | void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1453 | const analyze_printf::OptionalFlag &flag, |
| 1454 | const char *startSpecifier, |
| 1455 | unsigned specifierLen) { |
| 1456 | // Warn about pointless flag with a fixit removal. |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1457 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 1458 | FS.getConversionSpecifier(); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1459 | S.Diag(getLocationOfByte(flag.getPosition()), |
| 1460 | diag::warn_printf_nonsensical_flag) |
| 1461 | << flag.toString() << CS.toString() |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1462 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1463 | << FixItHint::CreateRemoval(getSpecifierRange(flag.getPosition(), 1)); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | void CheckPrintfHandler::HandleIgnoredFlag( |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1467 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1468 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 1469 | const analyze_printf::OptionalFlag &flag, |
| 1470 | const char *startSpecifier, |
| 1471 | unsigned specifierLen) { |
| 1472 | // Warn about ignored flag with a fixit removal. |
| 1473 | S.Diag(getLocationOfByte(ignoredFlag.getPosition()), |
| 1474 | diag::warn_printf_ignored_flag) |
| 1475 | << ignoredFlag.toString() << flag.toString() |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1476 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1477 | << FixItHint::CreateRemoval(getSpecifierRange( |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1478 | ignoredFlag.getPosition(), 1)); |
| 1479 | } |
| 1480 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1481 | bool |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1482 | CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier |
Ted Kremenek | 5c41ee8 | 2010-02-11 09:27:41 +0000 | [diff] [blame] | 1483 | &FS, |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1484 | const char *startSpecifier, |
| 1485 | unsigned specifierLen) { |
| 1486 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1487 | using namespace analyze_format_string; |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1488 | using namespace analyze_printf; |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1489 | const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1490 | |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 1491 | if (FS.consumesDataArgument()) { |
| 1492 | if (atFirstArg) { |
| 1493 | atFirstArg = false; |
| 1494 | usesPositionalArgs = FS.usesPositionalArg(); |
| 1495 | } |
| 1496 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
| 1497 | // Cannot mix-and-match positional and non-positional arguments. |
| 1498 | S.Diag(getLocationOfByte(CS.getStart()), |
| 1499 | diag::warn_format_mix_positional_nonpositional_args) |
| 1500 | << getSpecifierRange(startSpecifier, specifierLen); |
| 1501 | return false; |
| 1502 | } |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1503 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1504 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1505 | // First check if the field width, precision, and conversion specifier |
| 1506 | // have matching data arguments. |
| 1507 | if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, |
| 1508 | startSpecifier, specifierLen)) { |
| 1509 | return false; |
| 1510 | } |
| 1511 | |
| 1512 | if (!HandleAmount(FS.getPrecision(), /* precision */ 1, |
| 1513 | startSpecifier, specifierLen)) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1514 | return false; |
| 1515 | } |
| 1516 | |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 1517 | if (!CS.consumesDataArgument()) { |
| 1518 | // FIXME: Technically specifying a precision or field width here |
| 1519 | // makes no sense. Worth issuing a warning at some point. |
Ted Kremenek | 0e5675d | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 1520 | return true; |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 1521 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1522 | |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1523 | // Consume the argument. |
| 1524 | unsigned argIndex = FS.getArgIndex(); |
Ted Kremenek | e3fc547 | 2010-02-27 08:34:51 +0000 | [diff] [blame] | 1525 | if (argIndex < NumDataArgs) { |
| 1526 | // The check to see if the argIndex is valid will come later. |
| 1527 | // We set the bit here because we may exit early from this |
| 1528 | // function if we encounter some other error. |
| 1529 | CoveredArgs.set(argIndex); |
| 1530 | } |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1531 | |
| 1532 | // Check for using an Objective-C specific conversion specifier |
| 1533 | // in a non-ObjC literal. |
| 1534 | if (!IsObjCLiteral && CS.isObjCArg()) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1535 | return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, |
| 1536 | specifierLen); |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1537 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1538 | |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1539 | // Check for invalid use of field width |
| 1540 | if (!FS.hasValidFieldWidth()) { |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 1541 | HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1542 | startSpecifier, specifierLen); |
| 1543 | } |
| 1544 | |
| 1545 | // Check for invalid use of precision |
| 1546 | if (!FS.hasValidPrecision()) { |
| 1547 | HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, |
| 1548 | startSpecifier, specifierLen); |
| 1549 | } |
| 1550 | |
| 1551 | // Check each flag does not conflict with any other component. |
Ted Kremenek | 65197b4 | 2011-01-08 05:28:46 +0000 | [diff] [blame] | 1552 | if (!FS.hasValidThousandsGroupingPrefix()) |
| 1553 | HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1554 | if (!FS.hasValidLeadingZeros()) |
| 1555 | HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); |
| 1556 | if (!FS.hasValidPlusPrefix()) |
| 1557 | HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 1558 | if (!FS.hasValidSpacePrefix()) |
| 1559 | HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1560 | if (!FS.hasValidAlternativeForm()) |
| 1561 | HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); |
| 1562 | if (!FS.hasValidLeftJustified()) |
| 1563 | HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); |
| 1564 | |
| 1565 | // Check that flags are not ignored by another flag |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 1566 | if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' |
| 1567 | HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), |
| 1568 | startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1569 | if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' |
| 1570 | HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), |
| 1571 | startSpecifier, specifierLen); |
| 1572 | |
| 1573 | // Check the length modifier is valid with the given conversion specifier. |
| 1574 | const LengthModifier &LM = FS.getLengthModifier(); |
| 1575 | if (!FS.hasValidLengthModifier()) |
| 1576 | S.Diag(getLocationOfByte(LM.getStart()), |
Ted Kremenek | 649aecf | 2010-07-20 20:03:43 +0000 | [diff] [blame] | 1577 | diag::warn_format_nonsensical_length) |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1578 | << LM.toString() << CS.toString() |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1579 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1580 | << FixItHint::CreateRemoval(getSpecifierRange(LM.getStart(), |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1581 | LM.getLength())); |
| 1582 | |
| 1583 | // Are we using '%n'? |
Ted Kremenek | 35d353b | 2010-07-20 20:04:10 +0000 | [diff] [blame] | 1584 | if (CS.getKind() == ConversionSpecifier::nArg) { |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1585 | // Issue a warning about this being a possible security issue. |
Ted Kremenek | e82d804 | 2010-01-29 01:35:25 +0000 | [diff] [blame] | 1586 | S.Diag(getLocationOfByte(CS.getStart()), diag::warn_printf_write_back) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1587 | << getSpecifierRange(startSpecifier, specifierLen); |
Ted Kremenek | e82d804 | 2010-01-29 01:35:25 +0000 | [diff] [blame] | 1588 | // Continue checking the other format specifiers. |
| 1589 | return true; |
| 1590 | } |
Ted Kremenek | 5c41ee8 | 2010-02-11 09:27:41 +0000 | [diff] [blame] | 1591 | |
Ted Kremenek | da51f0d | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 1592 | // The remaining checks depend on the data arguments. |
| 1593 | if (HasVAListArg) |
| 1594 | return true; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1595 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 1596 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | da51f0d | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 1597 | return false; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1598 | |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 1599 | // Now type check the data expression that matches the |
| 1600 | // format specifier. |
| 1601 | const Expr *Ex = getDataArg(argIndex); |
| 1602 | const analyze_printf::ArgTypeResult &ATR = FS.getArgType(S.Context); |
| 1603 | if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) { |
| 1604 | // Check if we didn't match because of an implicit cast from a 'char' |
| 1605 | // or 'short' to an 'int'. This is done because printf is a varargs |
| 1606 | // function. |
| 1607 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Ex)) |
Ted Kremenek | 4d8ae4d | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 1608 | if (ICE->getType() == S.Context.IntTy) { |
| 1609 | // All further checking is done on the subexpression. |
| 1610 | Ex = ICE->getSubExpr(); |
| 1611 | if (ATR.matchesType(S.Context, Ex->getType())) |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 1612 | return true; |
Ted Kremenek | 4d8ae4d | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 1613 | } |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 1614 | |
| 1615 | // We may be able to offer a FixItHint if it is a supported type. |
| 1616 | PrintfSpecifier fixedFS = FS; |
| 1617 | bool success = fixedFS.fixType(Ex->getType()); |
| 1618 | |
| 1619 | if (success) { |
| 1620 | // Get the fix string from the fixed format specifier |
| 1621 | llvm::SmallString<128> buf; |
| 1622 | llvm::raw_svector_ostream os(buf); |
| 1623 | fixedFS.toString(os); |
| 1624 | |
Ted Kremenek | 9325eaf | 2010-08-24 22:24:51 +0000 | [diff] [blame] | 1625 | // FIXME: getRepresentativeType() perhaps should return a string |
| 1626 | // instead of a QualType to better handle when the representative |
| 1627 | // type is 'wint_t' (which is defined in the system headers). |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 1628 | S.Diag(getLocationOfByte(CS.getStart()), |
| 1629 | diag::warn_printf_conversion_argument_type_mismatch) |
| 1630 | << ATR.getRepresentativeType(S.Context) << Ex->getType() |
| 1631 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1632 | << Ex->getSourceRange() |
| 1633 | << FixItHint::CreateReplacement( |
| 1634 | getSpecifierRange(startSpecifier, specifierLen), |
| 1635 | os.str()); |
| 1636 | } |
| 1637 | else { |
| 1638 | S.Diag(getLocationOfByte(CS.getStart()), |
| 1639 | diag::warn_printf_conversion_argument_type_mismatch) |
| 1640 | << ATR.getRepresentativeType(S.Context) << Ex->getType() |
| 1641 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1642 | << Ex->getSourceRange(); |
| 1643 | } |
| 1644 | } |
| 1645 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1646 | return true; |
| 1647 | } |
| 1648 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1649 | //===--- CHECK: Scanf format string checking ------------------------------===// |
| 1650 | |
| 1651 | namespace { |
| 1652 | class CheckScanfHandler : public CheckFormatHandler { |
| 1653 | public: |
| 1654 | CheckScanfHandler(Sema &s, const StringLiteral *fexpr, |
| 1655 | const Expr *origFormatExpr, unsigned firstDataArg, |
| 1656 | unsigned numDataArgs, bool isObjCLiteral, |
| 1657 | const char *beg, bool hasVAListArg, |
| 1658 | const CallExpr *theCall, unsigned formatIdx) |
| 1659 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
| 1660 | numDataArgs, isObjCLiteral, beg, hasVAListArg, |
| 1661 | theCall, formatIdx) {} |
| 1662 | |
| 1663 | bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, |
| 1664 | const char *startSpecifier, |
| 1665 | unsigned specifierLen); |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1666 | |
| 1667 | bool HandleInvalidScanfConversionSpecifier( |
| 1668 | const analyze_scanf::ScanfSpecifier &FS, |
| 1669 | const char *startSpecifier, |
| 1670 | unsigned specifierLen); |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 1671 | |
| 1672 | void HandleIncompleteScanList(const char *start, const char *end); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1673 | }; |
Ted Kremenek | 07d161f | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 1674 | } |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1675 | |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 1676 | void CheckScanfHandler::HandleIncompleteScanList(const char *start, |
| 1677 | const char *end) { |
| 1678 | S.Diag(getLocationOfByte(end), diag::warn_scanf_scanlist_incomplete) |
| 1679 | << getSpecifierRange(start, end - start); |
| 1680 | } |
| 1681 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1682 | bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( |
| 1683 | const analyze_scanf::ScanfSpecifier &FS, |
| 1684 | const char *startSpecifier, |
| 1685 | unsigned specifierLen) { |
| 1686 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1687 | const analyze_scanf::ScanfConversionSpecifier &CS = |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1688 | FS.getConversionSpecifier(); |
| 1689 | |
| 1690 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 1691 | getLocationOfByte(CS.getStart()), |
| 1692 | startSpecifier, specifierLen, |
| 1693 | CS.getStart(), CS.getLength()); |
| 1694 | } |
| 1695 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1696 | bool CheckScanfHandler::HandleScanfSpecifier( |
| 1697 | const analyze_scanf::ScanfSpecifier &FS, |
| 1698 | const char *startSpecifier, |
| 1699 | unsigned specifierLen) { |
| 1700 | |
| 1701 | using namespace analyze_scanf; |
| 1702 | using namespace analyze_format_string; |
| 1703 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1704 | const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1705 | |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 1706 | // Handle case where '%' and '*' don't consume an argument. These shouldn't |
| 1707 | // be used to decide if we are using positional arguments consistently. |
| 1708 | if (FS.consumesDataArgument()) { |
| 1709 | if (atFirstArg) { |
| 1710 | atFirstArg = false; |
| 1711 | usesPositionalArgs = FS.usesPositionalArg(); |
| 1712 | } |
| 1713 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
| 1714 | // Cannot mix-and-match positional and non-positional arguments. |
| 1715 | S.Diag(getLocationOfByte(CS.getStart()), |
| 1716 | diag::warn_format_mix_positional_nonpositional_args) |
| 1717 | << getSpecifierRange(startSpecifier, specifierLen); |
| 1718 | return false; |
| 1719 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | // Check if the field with is non-zero. |
| 1723 | const OptionalAmount &Amt = FS.getFieldWidth(); |
| 1724 | if (Amt.getHowSpecified() == OptionalAmount::Constant) { |
| 1725 | if (Amt.getConstantAmount() == 0) { |
| 1726 | const CharSourceRange &R = getSpecifierRange(Amt.getStart(), |
| 1727 | Amt.getConstantLength()); |
| 1728 | S.Diag(getLocationOfByte(Amt.getStart()), |
| 1729 | diag::warn_scanf_nonzero_width) |
| 1730 | << R << FixItHint::CreateRemoval(R); |
| 1731 | } |
| 1732 | } |
| 1733 | |
| 1734 | if (!FS.consumesDataArgument()) { |
| 1735 | // FIXME: Technically specifying a precision or field width here |
| 1736 | // makes no sense. Worth issuing a warning at some point. |
| 1737 | return true; |
| 1738 | } |
| 1739 | |
| 1740 | // Consume the argument. |
| 1741 | unsigned argIndex = FS.getArgIndex(); |
| 1742 | if (argIndex < NumDataArgs) { |
| 1743 | // The check to see if the argIndex is valid will come later. |
| 1744 | // We set the bit here because we may exit early from this |
| 1745 | // function if we encounter some other error. |
| 1746 | CoveredArgs.set(argIndex); |
| 1747 | } |
| 1748 | |
Ted Kremenek | 1e51c20 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 1749 | // Check the length modifier is valid with the given conversion specifier. |
| 1750 | const LengthModifier &LM = FS.getLengthModifier(); |
| 1751 | if (!FS.hasValidLengthModifier()) { |
| 1752 | S.Diag(getLocationOfByte(LM.getStart()), |
| 1753 | diag::warn_format_nonsensical_length) |
| 1754 | << LM.toString() << CS.toString() |
| 1755 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1756 | << FixItHint::CreateRemoval(getSpecifierRange(LM.getStart(), |
| 1757 | LM.getLength())); |
| 1758 | } |
| 1759 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1760 | // The remaining checks depend on the data arguments. |
| 1761 | if (HasVAListArg) |
| 1762 | return true; |
| 1763 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 1764 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1765 | return false; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1766 | |
| 1767 | // FIXME: Check that the argument type matches the format specifier. |
| 1768 | |
| 1769 | return true; |
| 1770 | } |
| 1771 | |
| 1772 | void Sema::CheckFormatString(const StringLiteral *FExpr, |
Ted Kremenek | 0e5675d | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 1773 | const Expr *OrigFormatExpr, |
| 1774 | const CallExpr *TheCall, bool HasVAListArg, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1775 | unsigned format_idx, unsigned firstDataArg, |
| 1776 | bool isPrintf) { |
| 1777 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1778 | // CHECK: is the format string a wide literal? |
| 1779 | if (FExpr->isWide()) { |
| 1780 | Diag(FExpr->getLocStart(), |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1781 | diag::warn_format_string_is_wide_literal) |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1782 | << OrigFormatExpr->getSourceRange(); |
| 1783 | return; |
| 1784 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1785 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1786 | // Str - The format string. NOTE: this is NOT null-terminated! |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 1787 | llvm::StringRef StrRef = FExpr->getString(); |
| 1788 | const char *Str = StrRef.data(); |
| 1789 | unsigned StrLen = StrRef.size(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1790 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1791 | // CHECK: empty format string? |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1792 | if (StrLen == 0) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1793 | Diag(FExpr->getLocStart(), diag::warn_empty_format_string) |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1794 | << OrigFormatExpr->getSourceRange(); |
| 1795 | return; |
| 1796 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1797 | |
| 1798 | if (isPrintf) { |
| 1799 | CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, |
| 1800 | TheCall->getNumArgs() - firstDataArg, |
| 1801 | isa<ObjCStringLiteral>(OrigFormatExpr), Str, |
| 1802 | HasVAListArg, TheCall, format_idx); |
| 1803 | |
| 1804 | if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen)) |
| 1805 | H.DoneProcessing(); |
| 1806 | } |
| 1807 | else { |
| 1808 | CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, |
| 1809 | TheCall->getNumArgs() - firstDataArg, |
| 1810 | isa<ObjCStringLiteral>(OrigFormatExpr), Str, |
| 1811 | HasVAListArg, TheCall, format_idx); |
| 1812 | |
| 1813 | if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen)) |
| 1814 | H.DoneProcessing(); |
| 1815 | } |
Ted Kremenek | ce7024e | 2010-01-28 01:18:22 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 1818 | //===--- CHECK: Standard memory functions ---------------------------------===// |
| 1819 | |
Douglas Gregor | 2a053a3 | 2011-05-03 20:05:22 +0000 | [diff] [blame] | 1820 | /// \brief Determine whether the given type is a dynamic class type (e.g., |
| 1821 | /// whether it has a vtable). |
| 1822 | static bool isDynamicClassType(QualType T) { |
| 1823 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
| 1824 | if (CXXRecordDecl *Definition = Record->getDefinition()) |
| 1825 | if (Definition->isDynamicClass()) |
| 1826 | return true; |
| 1827 | |
| 1828 | return false; |
| 1829 | } |
| 1830 | |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 1831 | /// \brief If E is a sizeof expression, returns the expression's type in |
| 1832 | /// OutType. |
| 1833 | static bool sizeofExprType(const Expr* E, QualType *OutType) { |
| 1834 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
| 1835 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) { |
| 1836 | if (SizeOf->getKind() != clang::UETT_SizeOf) |
| 1837 | return false; |
| 1838 | |
| 1839 | *OutType = SizeOf->getTypeOfArgument(); |
| 1840 | return true; |
| 1841 | } |
| 1842 | return false; |
| 1843 | } |
| 1844 | |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 1845 | /// \brief Check for dangerous or invalid arguments to memset(). |
| 1846 | /// |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 1847 | /// This issues warnings on known problematic, dangerous or unspecified |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 1848 | /// arguments to the standard 'memset', 'memcpy', and 'memmove' function calls. |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 1849 | /// |
| 1850 | /// \param Call The call expression to diagnose. |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 1851 | void Sema::CheckMemsetcpymoveArguments(const CallExpr *Call, |
| 1852 | const IdentifierInfo *FnName) { |
Ted Kremenek | 1d59f7f | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 1853 | // It is possible to have a non-standard definition of memset. Validate |
| 1854 | // we have the proper number of arguments, and if not, abort further |
| 1855 | // checking. |
| 1856 | if (Call->getNumArgs() != 3) |
| 1857 | return; |
| 1858 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 1859 | unsigned LastArg = FnName->isStr("memset")? 1 : 2; |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 1860 | const Expr *LenExpr = Call->getArg(2)->IgnoreParenImpCasts(); |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 1861 | for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) { |
| 1862 | const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts(); |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 1863 | SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange(); |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 1864 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 1865 | QualType DestTy = Dest->getType(); |
| 1866 | if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) { |
| 1867 | QualType PointeeTy = DestPtrTy->getPointeeType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 1868 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 1869 | if (PointeeTy->isVoidType()) |
| 1870 | continue; |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 1871 | |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 1872 | // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). |
| 1873 | QualType SizeofTy; |
| 1874 | if (sizeofExprType(LenExpr, &SizeofTy) && |
| 1875 | Context.typesAreCompatible(SizeofTy, DestTy)) { |
| 1876 | // Note: This complains about sizeof(typeof(p)) as well. |
| 1877 | SourceLocation loc = LenExpr->getSourceRange().getBegin(); |
| 1878 | Diag(loc, diag::warn_sizeof_pointer) |
| 1879 | << SizeofTy << PointeeTy << ArgIdx << FnName; |
| 1880 | break; |
| 1881 | } |
| 1882 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 1883 | unsigned DiagID; |
| 1884 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 1885 | // Always complain about dynamic classes. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 1886 | if (isDynamicClassType(PointeeTy)) |
| 1887 | DiagID = diag::warn_dyn_class_memaccess; |
| 1888 | else if (PointeeTy.hasNonTrivialObjCLifetime() && |
| 1889 | !FnName->isStr("memset")) |
| 1890 | DiagID = diag::warn_arc_object_memaccess; |
| 1891 | else |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 1892 | continue; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 1893 | |
| 1894 | DiagRuntimeBehavior( |
| 1895 | Dest->getExprLoc(), Dest, |
| 1896 | PDiag(DiagID) |
| 1897 | << ArgIdx << FnName << PointeeTy |
| 1898 | << Call->getCallee()->getSourceRange()); |
Chandler Carruth | 43fa33b | 2011-04-29 09:46:08 +0000 | [diff] [blame] | 1899 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 1900 | DiagRuntimeBehavior( |
| 1901 | Dest->getExprLoc(), Dest, |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 1902 | PDiag(diag::note_bad_memaccess_silence) |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 1903 | << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)")); |
| 1904 | break; |
| 1905 | } |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 1906 | } |
| 1907 | } |
| 1908 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1909 | //===--- CHECK: Return Address of Stack Variable --------------------------===// |
| 1910 | |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 1911 | static Expr *EvalVal(Expr *E, llvm::SmallVectorImpl<DeclRefExpr *> &refVars); |
| 1912 | static Expr *EvalAddr(Expr* E, llvm::SmallVectorImpl<DeclRefExpr *> &refVars); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1913 | |
| 1914 | /// CheckReturnStackAddr - Check if a return statement returns the address |
| 1915 | /// of a stack variable. |
| 1916 | void |
| 1917 | Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, |
| 1918 | SourceLocation ReturnLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1919 | |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 1920 | Expr *stackE = 0; |
| 1921 | llvm::SmallVector<DeclRefExpr *, 8> refVars; |
| 1922 | |
| 1923 | // Perform checking for returned stack addresses, local blocks, |
| 1924 | // label addresses or references to temporaries. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 1925 | if (lhsType->isPointerType() || |
| 1926 | (!getLangOptions().ObjCAutoRefCount && lhsType->isBlockPointerType())) { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 1927 | stackE = EvalAddr(RetValExp, refVars); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1928 | } else if (lhsType->isReferenceType()) { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 1929 | stackE = EvalVal(RetValExp, refVars); |
| 1930 | } |
| 1931 | |
| 1932 | if (stackE == 0) |
| 1933 | return; // Nothing suspicious was found. |
| 1934 | |
| 1935 | SourceLocation diagLoc; |
| 1936 | SourceRange diagRange; |
| 1937 | if (refVars.empty()) { |
| 1938 | diagLoc = stackE->getLocStart(); |
| 1939 | diagRange = stackE->getSourceRange(); |
| 1940 | } else { |
| 1941 | // We followed through a reference variable. 'stackE' contains the |
| 1942 | // problematic expression but we will warn at the return statement pointing |
| 1943 | // at the reference variable. We will later display the "trail" of |
| 1944 | // reference variables using notes. |
| 1945 | diagLoc = refVars[0]->getLocStart(); |
| 1946 | diagRange = refVars[0]->getSourceRange(); |
| 1947 | } |
| 1948 | |
| 1949 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var. |
| 1950 | Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref |
| 1951 | : diag::warn_ret_stack_addr) |
| 1952 | << DR->getDecl()->getDeclName() << diagRange; |
| 1953 | } else if (isa<BlockExpr>(stackE)) { // local block. |
| 1954 | Diag(diagLoc, diag::err_ret_local_block) << diagRange; |
| 1955 | } else if (isa<AddrLabelExpr>(stackE)) { // address of label. |
| 1956 | Diag(diagLoc, diag::warn_ret_addr_label) << diagRange; |
| 1957 | } else { // local temporary. |
| 1958 | Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref |
| 1959 | : diag::warn_ret_local_temp_addr) |
| 1960 | << diagRange; |
| 1961 | } |
| 1962 | |
| 1963 | // Display the "trail" of reference variables that we followed until we |
| 1964 | // found the problematic expression using notes. |
| 1965 | for (unsigned i = 0, e = refVars.size(); i != e; ++i) { |
| 1966 | VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl()); |
| 1967 | // If this var binds to another reference var, show the range of the next |
| 1968 | // var, otherwise the var binds to the problematic expression, in which case |
| 1969 | // show the range of the expression. |
| 1970 | SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange() |
| 1971 | : stackE->getSourceRange(); |
| 1972 | Diag(VD->getLocation(), diag::note_ref_var_local_bind) |
| 1973 | << VD->getDeclName() << range; |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that |
| 1978 | /// check if the expression in a return statement evaluates to an address |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 1979 | /// to a location on the stack, a local block, an address of a label, or a |
| 1980 | /// reference to local temporary. The recursion is used to traverse the |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1981 | /// AST of the return expression, with recursion backtracking when we |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 1982 | /// encounter a subexpression that (1) clearly does not lead to one of the |
| 1983 | /// above problematic expressions (2) is something we cannot determine leads to |
| 1984 | /// a problematic expression based on such local checking. |
| 1985 | /// |
| 1986 | /// Both EvalAddr and EvalVal follow through reference variables to evaluate |
| 1987 | /// the expression that they point to. Such variables are added to the |
| 1988 | /// 'refVars' vector so that we know what the reference variable "trail" was. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1989 | /// |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 1990 | /// EvalAddr processes expressions that are pointers that are used as |
| 1991 | /// references (and not L-values). EvalVal handles all other values. |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 1992 | /// At the base case of the recursion is a check for the above problematic |
| 1993 | /// expressions. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1994 | /// |
| 1995 | /// This implementation handles: |
| 1996 | /// |
| 1997 | /// * pointer-to-pointer casts |
| 1998 | /// * implicit conversions from array references to pointers |
| 1999 | /// * taking the address of fields |
| 2000 | /// * arbitrary interplay between "&" and "*" operators |
| 2001 | /// * pointer arithmetic from an address of a stack variable |
| 2002 | /// * taking the address of an array element where the array is on the stack |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2003 | static Expr *EvalAddr(Expr *E, llvm::SmallVectorImpl<DeclRefExpr *> &refVars) { |
| 2004 | if (E->isTypeDependent()) |
| 2005 | return NULL; |
| 2006 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2007 | // We should only be called for evaluating pointer expressions. |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2008 | assert((E->getType()->isAnyPointerType() || |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2009 | E->getType()->isBlockPointerType() || |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2010 | E->getType()->isObjCQualifiedIdType()) && |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2011 | "EvalAddr only works on pointers"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2012 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2013 | E = E->IgnoreParens(); |
| 2014 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2015 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 2016 | // viewed AST node. We then recursively traverse the AST by calling |
| 2017 | // EvalAddr and EvalVal appropriately. |
| 2018 | switch (E->getStmtClass()) { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2019 | case Stmt::DeclRefExprClass: { |
| 2020 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
| 2021 | |
| 2022 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
| 2023 | // If this is a reference variable, follow through to the expression that |
| 2024 | // it points to. |
| 2025 | if (V->hasLocalStorage() && |
| 2026 | V->getType()->isReferenceType() && V->hasInit()) { |
| 2027 | // Add the reference variable to the "trail". |
| 2028 | refVars.push_back(DR); |
| 2029 | return EvalAddr(V->getInit(), refVars); |
| 2030 | } |
| 2031 | |
| 2032 | return NULL; |
| 2033 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2034 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2035 | case Stmt::UnaryOperatorClass: { |
| 2036 | // The only unary operator that make sense to handle here |
| 2037 | // is AddrOf. All others don't make sense as pointers. |
| 2038 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2039 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2040 | if (U->getOpcode() == UO_AddrOf) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2041 | return EvalVal(U->getSubExpr(), refVars); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2042 | else |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2043 | return NULL; |
| 2044 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2045 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2046 | case Stmt::BinaryOperatorClass: { |
| 2047 | // Handle pointer arithmetic. All other binary operators are not valid |
| 2048 | // in this context. |
| 2049 | BinaryOperator *B = cast<BinaryOperator>(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2050 | BinaryOperatorKind op = B->getOpcode(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2051 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2052 | if (op != BO_Add && op != BO_Sub) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2053 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2054 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2055 | Expr *Base = B->getLHS(); |
| 2056 | |
| 2057 | // Determine which argument is the real pointer base. It could be |
| 2058 | // the RHS argument instead of the LHS. |
| 2059 | if (!Base->getType()->isPointerType()) Base = B->getRHS(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2060 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2061 | assert (Base->getType()->isPointerType()); |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2062 | return EvalAddr(Base, refVars); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2063 | } |
Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 2064 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2065 | // For conditional operators we need to see if either the LHS or RHS are |
| 2066 | // valid DeclRefExpr*s. If one of them is valid, we return it. |
| 2067 | case Stmt::ConditionalOperatorClass: { |
| 2068 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2069 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2070 | // Handle the GNU extension for missing LHS. |
Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 2071 | if (Expr *lhsExpr = C->getLHS()) { |
| 2072 | // In C++, we can have a throw-expression, which has 'void' type. |
| 2073 | if (!lhsExpr->getType()->isVoidType()) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2074 | if (Expr* LHS = EvalAddr(lhsExpr, refVars)) |
Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 2075 | return LHS; |
| 2076 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2077 | |
Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 2078 | // In C++, we can have a throw-expression, which has 'void' type. |
| 2079 | if (C->getRHS()->getType()->isVoidType()) |
| 2080 | return NULL; |
| 2081 | |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2082 | return EvalAddr(C->getRHS(), refVars); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2083 | } |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2084 | |
| 2085 | case Stmt::BlockExprClass: |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2086 | if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures()) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2087 | return E; // local block. |
| 2088 | return NULL; |
| 2089 | |
| 2090 | case Stmt::AddrLabelExprClass: |
| 2091 | return E; // address of label. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 2093 | // For casts, we need to handle conversions from arrays to |
| 2094 | // pointer values, and pointer-to-pointer conversions. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2095 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2096 | case Stmt::CStyleCastExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 2097 | case Stmt::CXXFunctionalCastExprClass: |
| 2098 | case Stmt::ObjCBridgedCastExprClass: { |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 2099 | Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 2100 | QualType T = SubExpr->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2102 | if (SubExpr->getType()->isPointerType() || |
| 2103 | SubExpr->getType()->isBlockPointerType() || |
| 2104 | SubExpr->getType()->isObjCQualifiedIdType()) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2105 | return EvalAddr(SubExpr, refVars); |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 2106 | else if (T->isArrayType()) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2107 | return EvalVal(SubExpr, refVars); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2108 | else |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 2109 | return 0; |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2110 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2111 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2112 | // C++ casts. For dynamic casts, static casts, and const casts, we |
| 2113 | // are always converting from a pointer-to-pointer, so we just blow |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2114 | // through the cast. In the case the dynamic cast doesn't fail (and |
| 2115 | // return NULL), we take the conservative route and report cases |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2116 | // where we return the address of a stack variable. For Reinterpre |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2117 | // FIXME: The comment about is wrong; we're not always converting |
| 2118 | // from pointer to pointer. I'm guessing that this code should also |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2119 | // handle references to objects. |
| 2120 | case Stmt::CXXStaticCastExprClass: |
| 2121 | case Stmt::CXXDynamicCastExprClass: |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2122 | case Stmt::CXXConstCastExprClass: |
| 2123 | case Stmt::CXXReinterpretCastExprClass: { |
| 2124 | Expr *S = cast<CXXNamedCastExpr>(E)->getSubExpr(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2125 | if (S->getType()->isPointerType() || S->getType()->isBlockPointerType()) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2126 | return EvalAddr(S, refVars); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2127 | else |
| 2128 | return NULL; |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2129 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2130 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2131 | // Everything else: we simply don't reason about them. |
| 2132 | default: |
| 2133 | return NULL; |
| 2134 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2135 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2136 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2137 | |
| 2138 | /// EvalVal - This function is complements EvalAddr in the mutual recursion. |
| 2139 | /// See the comments for EvalAddr for more details. |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2140 | static Expr *EvalVal(Expr *E, llvm::SmallVectorImpl<DeclRefExpr *> &refVars) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 2141 | do { |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 2142 | // We should only be called for evaluating non-pointer expressions, or |
| 2143 | // expressions with a pointer type that are not used as references but instead |
| 2144 | // are l-values (e.g., DeclRefExpr with a pointer type). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2145 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2146 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 2147 | // viewed AST node. We then recursively traverse the AST by calling |
| 2148 | // EvalAddr and EvalVal appropriately. |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2149 | |
| 2150 | E = E->IgnoreParens(); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2151 | switch (E->getStmtClass()) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 2152 | case Stmt::ImplicitCastExprClass: { |
| 2153 | ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E); |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2154 | if (IE->getValueKind() == VK_LValue) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 2155 | E = IE->getSubExpr(); |
| 2156 | continue; |
| 2157 | } |
| 2158 | return NULL; |
| 2159 | } |
| 2160 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2161 | case Stmt::DeclRefExprClass: { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2162 | // When we hit a DeclRefExpr we are looking at code that refers to a |
| 2163 | // variable's name. If it's not a reference variable we check if it has |
| 2164 | // local storage within the function, and if so, return the expression. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2165 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2166 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2167 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2168 | if (V->hasLocalStorage()) { |
| 2169 | if (!V->getType()->isReferenceType()) |
| 2170 | return DR; |
| 2171 | |
| 2172 | // Reference variable, follow through to the expression that |
| 2173 | // it points to. |
| 2174 | if (V->hasInit()) { |
| 2175 | // Add the reference variable to the "trail". |
| 2176 | refVars.push_back(DR); |
| 2177 | return EvalVal(V->getInit(), refVars); |
| 2178 | } |
| 2179 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2180 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2181 | return NULL; |
| 2182 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2183 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2184 | case Stmt::UnaryOperatorClass: { |
| 2185 | // The only unary operator that make sense to handle here |
| 2186 | // is Deref. All others don't resolve to a "name." This includes |
| 2187 | // handling all sorts of rvalues passed to a unary operator. |
| 2188 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2189 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2190 | if (U->getOpcode() == UO_Deref) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2191 | return EvalAddr(U->getSubExpr(), refVars); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2192 | |
| 2193 | return NULL; |
| 2194 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2195 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2196 | case Stmt::ArraySubscriptExprClass: { |
| 2197 | // Array subscripts are potential references to data on the stack. We |
| 2198 | // retrieve the DeclRefExpr* for the array variable if it indeed |
| 2199 | // has local storage. |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2200 | return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2201 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2202 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2203 | case Stmt::ConditionalOperatorClass: { |
| 2204 | // For conditional operators we need to see if either the LHS or RHS are |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2205 | // non-NULL Expr's. If one is non-NULL, we return it. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2206 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 2207 | |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 2208 | // Handle the GNU extension for missing LHS. |
| 2209 | if (Expr *lhsExpr = C->getLHS()) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2210 | if (Expr *LHS = EvalVal(lhsExpr, refVars)) |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 2211 | return LHS; |
| 2212 | |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2213 | return EvalVal(C->getRHS(), refVars); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2214 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2215 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2216 | // Accesses to members are potential references to data on the stack. |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 2217 | case Stmt::MemberExprClass: { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2218 | MemberExpr *M = cast<MemberExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2219 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2220 | // Check for indirect access. We only want direct field accesses. |
Ted Kremenek | a423e81 | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 2221 | if (M->isArrow()) |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2222 | return NULL; |
Ted Kremenek | a423e81 | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 2223 | |
| 2224 | // Check whether the member type is itself a reference, in which case |
| 2225 | // we're not going to refer to the member, but to what the member refers to. |
| 2226 | if (M->getMemberDecl()->getType()->isReferenceType()) |
| 2227 | return NULL; |
| 2228 | |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2229 | return EvalVal(M->getBase(), refVars); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2230 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2231 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2232 | default: |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2233 | // Check that we don't return or take the address of a reference to a |
| 2234 | // temporary. This is only useful in C++. |
| 2235 | if (!E->isTypeDependent() && E->isRValue()) |
| 2236 | return E; |
| 2237 | |
| 2238 | // Everything else: we simply don't reason about them. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2239 | return NULL; |
| 2240 | } |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 2241 | } while (true); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2242 | } |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2243 | |
| 2244 | //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// |
| 2245 | |
| 2246 | /// Check for comparisons of floating point operands using != and ==. |
| 2247 | /// Issue a warning if these are no self-comparisons, as they are not likely |
| 2248 | /// to do what the programmer intended. |
| 2249 | void Sema::CheckFloatComparison(SourceLocation loc, Expr* lex, Expr *rex) { |
| 2250 | bool EmitWarning = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2251 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2252 | Expr* LeftExprSansParen = lex->IgnoreParenImpCasts(); |
| 2253 | Expr* RightExprSansParen = rex->IgnoreParenImpCasts(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2254 | |
| 2255 | // Special case: check for x == x (which is OK). |
| 2256 | // Do not emit warnings for such cases. |
| 2257 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) |
| 2258 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) |
| 2259 | if (DRL->getDecl() == DRR->getDecl()) |
| 2260 | EmitWarning = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2261 | |
| 2262 | |
Ted Kremenek | 1b500bb | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 2263 | // Special case: check for comparisons against literals that can be exactly |
| 2264 | // represented by APFloat. In such cases, do not emit a warning. This |
| 2265 | // is a heuristic: often comparison against such literals are used to |
| 2266 | // detect if a value in a variable has not changed. This clearly can |
| 2267 | // lead to false negatives. |
| 2268 | if (EmitWarning) { |
| 2269 | if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { |
| 2270 | if (FLL->isExact()) |
| 2271 | EmitWarning = false; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2272 | } else |
Ted Kremenek | 1b500bb | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 2273 | if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)){ |
| 2274 | if (FLR->isExact()) |
| 2275 | EmitWarning = false; |
| 2276 | } |
| 2277 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2278 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2279 | // Check for comparisons with builtin types. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2280 | if (EmitWarning) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2281 | if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2282 | if (CL->isBuiltinCall(Context)) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2283 | EmitWarning = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2284 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2285 | if (EmitWarning) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2286 | if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2287 | if (CR->isBuiltinCall(Context)) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2288 | EmitWarning = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2289 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2290 | // Emit the diagnostic. |
| 2291 | if (EmitWarning) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2292 | Diag(loc, diag::warn_floatingpoint_eq) |
| 2293 | << lex->getSourceRange() << rex->getSourceRange(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2294 | } |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2295 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2296 | //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// |
| 2297 | //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2298 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2299 | namespace { |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2300 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2301 | /// Structure recording the 'active' range of an integer-valued |
| 2302 | /// expression. |
| 2303 | struct IntRange { |
| 2304 | /// The number of bits active in the int. |
| 2305 | unsigned Width; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2306 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2307 | /// True if the int is known not to have negative values. |
| 2308 | bool NonNegative; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2309 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2310 | IntRange(unsigned Width, bool NonNegative) |
| 2311 | : Width(Width), NonNegative(NonNegative) |
| 2312 | {} |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2313 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2314 | /// Returns the range of the bool type. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2315 | static IntRange forBoolType() { |
| 2316 | return IntRange(1, true); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2319 | /// Returns the range of an opaque value of the given integral type. |
| 2320 | static IntRange forValueOfType(ASTContext &C, QualType T) { |
| 2321 | return forValueOfCanonicalType(C, |
| 2322 | T->getCanonicalTypeInternal().getTypePtr()); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2325 | /// Returns the range of an opaque value of a canonical integral type. |
| 2326 | static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2327 | assert(T->isCanonicalUnqualified()); |
| 2328 | |
| 2329 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 2330 | T = VT->getElementType().getTypePtr(); |
| 2331 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 2332 | T = CT->getElementType().getTypePtr(); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2333 | |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 2334 | // For enum types, use the known bit width of the enumerators. |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2335 | if (const EnumType *ET = dyn_cast<EnumType>(T)) { |
| 2336 | EnumDecl *Enum = ET->getDecl(); |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 2337 | if (!Enum->isDefinition()) |
| 2338 | return IntRange(C.getIntWidth(QualType(T, 0)), false); |
| 2339 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2340 | unsigned NumPositive = Enum->getNumPositiveBits(); |
| 2341 | unsigned NumNegative = Enum->getNumNegativeBits(); |
| 2342 | |
| 2343 | return IntRange(std::max(NumPositive, NumNegative), NumNegative == 0); |
| 2344 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2345 | |
| 2346 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 2347 | assert(BT->isInteger()); |
| 2348 | |
| 2349 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 2350 | } |
| 2351 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2352 | /// Returns the "target" range of a canonical integral type, i.e. |
| 2353 | /// the range of values expressible in the type. |
| 2354 | /// |
| 2355 | /// This matches forValueOfCanonicalType except that enums have the |
| 2356 | /// full range of their type, not the range of their enumerators. |
| 2357 | static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) { |
| 2358 | assert(T->isCanonicalUnqualified()); |
| 2359 | |
| 2360 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 2361 | T = VT->getElementType().getTypePtr(); |
| 2362 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 2363 | T = CT->getElementType().getTypePtr(); |
| 2364 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 2365 | T = ET->getDecl()->getIntegerType().getTypePtr(); |
| 2366 | |
| 2367 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 2368 | assert(BT->isInteger()); |
| 2369 | |
| 2370 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 2371 | } |
| 2372 | |
| 2373 | /// Returns the supremum of two ranges: i.e. their conservative merge. |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 2374 | static IntRange join(IntRange L, IntRange R) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2375 | return IntRange(std::max(L.Width, R.Width), |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2376 | L.NonNegative && R.NonNegative); |
| 2377 | } |
| 2378 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2379 | /// Returns the infinum of two ranges: i.e. their aggressive merge. |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 2380 | static IntRange meet(IntRange L, IntRange R) { |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2381 | return IntRange(std::min(L.Width, R.Width), |
| 2382 | L.NonNegative || R.NonNegative); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2383 | } |
| 2384 | }; |
| 2385 | |
| 2386 | IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, unsigned MaxWidth) { |
| 2387 | if (value.isSigned() && value.isNegative()) |
| 2388 | return IntRange(value.getMinSignedBits(), false); |
| 2389 | |
| 2390 | if (value.getBitWidth() > MaxWidth) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2391 | value = value.trunc(MaxWidth); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2392 | |
| 2393 | // isNonNegative() just checks the sign bit without considering |
| 2394 | // signedness. |
| 2395 | return IntRange(value.getActiveBits(), true); |
| 2396 | } |
| 2397 | |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 2398 | IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2399 | unsigned MaxWidth) { |
| 2400 | if (result.isInt()) |
| 2401 | return GetValueRange(C, result.getInt(), MaxWidth); |
| 2402 | |
| 2403 | if (result.isVector()) { |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 2404 | IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); |
| 2405 | for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { |
| 2406 | IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); |
| 2407 | R = IntRange::join(R, El); |
| 2408 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2409 | return R; |
| 2410 | } |
| 2411 | |
| 2412 | if (result.isComplexInt()) { |
| 2413 | IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); |
| 2414 | IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); |
| 2415 | return IntRange::join(R, I); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | // This can happen with lossless casts to intptr_t of "based" lvalues. |
| 2419 | // Assume it might use arbitrary bits. |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 2420 | // FIXME: The only reason we need to pass the type in here is to get |
| 2421 | // the sign right on this one case. It would be nice if APValue |
| 2422 | // preserved this. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2423 | assert(result.isLValue()); |
Douglas Gregor | 5e9ebb3 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 2424 | return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType()); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2425 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2426 | |
| 2427 | /// Pseudo-evaluate the given integer expression, estimating the |
| 2428 | /// range of values it might take. |
| 2429 | /// |
| 2430 | /// \param MaxWidth - the width to which the value will be truncated |
| 2431 | IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) { |
| 2432 | E = E->IgnoreParens(); |
| 2433 | |
| 2434 | // Try a full evaluation first. |
| 2435 | Expr::EvalResult result; |
| 2436 | if (E->Evaluate(result, C)) |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 2437 | return GetValueRange(C, result.Val, E->getType(), MaxWidth); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2438 | |
| 2439 | // I think we only want to look through implicit casts here; if the |
| 2440 | // user has an explicit widening cast, we should treat the value as |
| 2441 | // being of the new, wider type. |
| 2442 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2443 | if (CE->getCastKind() == CK_NoOp) |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2444 | return GetExprRange(C, CE->getSubExpr(), MaxWidth); |
| 2445 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2446 | IntRange OutputTypeRange = IntRange::forValueOfType(C, CE->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2447 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2448 | bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast); |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2449 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2450 | // 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] | 2451 | if (!isIntegerCast) |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2452 | return OutputTypeRange; |
| 2453 | |
| 2454 | IntRange SubRange |
| 2455 | = GetExprRange(C, CE->getSubExpr(), |
| 2456 | std::min(MaxWidth, OutputTypeRange.Width)); |
| 2457 | |
| 2458 | // Bail out if the subexpr's range is as wide as the cast type. |
| 2459 | if (SubRange.Width >= OutputTypeRange.Width) |
| 2460 | return OutputTypeRange; |
| 2461 | |
| 2462 | // Otherwise, we take the smaller width, and we're non-negative if |
| 2463 | // either the output type or the subexpr is. |
| 2464 | return IntRange(SubRange.Width, |
| 2465 | SubRange.NonNegative || OutputTypeRange.NonNegative); |
| 2466 | } |
| 2467 | |
| 2468 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 2469 | // If we can fold the condition, just take that operand. |
| 2470 | bool CondResult; |
| 2471 | if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) |
| 2472 | return GetExprRange(C, CondResult ? CO->getTrueExpr() |
| 2473 | : CO->getFalseExpr(), |
| 2474 | MaxWidth); |
| 2475 | |
| 2476 | // Otherwise, conservatively merge. |
| 2477 | IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth); |
| 2478 | IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth); |
| 2479 | return IntRange::join(L, R); |
| 2480 | } |
| 2481 | |
| 2482 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 2483 | switch (BO->getOpcode()) { |
| 2484 | |
| 2485 | // Boolean-valued operations are single-bit and positive. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2486 | case BO_LAnd: |
| 2487 | case BO_LOr: |
| 2488 | case BO_LT: |
| 2489 | case BO_GT: |
| 2490 | case BO_LE: |
| 2491 | case BO_GE: |
| 2492 | case BO_EQ: |
| 2493 | case BO_NE: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2494 | return IntRange::forBoolType(); |
| 2495 | |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 2496 | // The type of these compound assignments is the type of the LHS, |
| 2497 | // so the RHS is not necessarily an integer. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2498 | case BO_MulAssign: |
| 2499 | case BO_DivAssign: |
| 2500 | case BO_RemAssign: |
| 2501 | case BO_AddAssign: |
| 2502 | case BO_SubAssign: |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2503 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 2504 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2505 | // Operations with opaque sources are black-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2506 | case BO_PtrMemD: |
| 2507 | case BO_PtrMemI: |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2508 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2509 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2510 | // Bitwise-and uses the *infinum* of the two source ranges. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2511 | case BO_And: |
| 2512 | case BO_AndAssign: |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2513 | return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth), |
| 2514 | GetExprRange(C, BO->getRHS(), MaxWidth)); |
| 2515 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2516 | // Left shift gets black-listed based on a judgement call. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2517 | case BO_Shl: |
John McCall | 3aae609 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 2518 | // ...except that we want to treat '1 << (blah)' as logically |
| 2519 | // positive. It's an important idiom. |
| 2520 | if (IntegerLiteral *I |
| 2521 | = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { |
| 2522 | if (I->getValue() == 1) { |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2523 | IntRange R = IntRange::forValueOfType(C, E->getType()); |
John McCall | 3aae609 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 2524 | return IntRange(R.Width, /*NonNegative*/ true); |
| 2525 | } |
| 2526 | } |
| 2527 | // fallthrough |
| 2528 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2529 | case BO_ShlAssign: |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2530 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2531 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2532 | // Right shift by a constant can narrow its left argument. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2533 | case BO_Shr: |
| 2534 | case BO_ShrAssign: { |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2535 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 2536 | |
| 2537 | // If the shift amount is a positive constant, drop the width by |
| 2538 | // that much. |
| 2539 | llvm::APSInt shift; |
| 2540 | if (BO->getRHS()->isIntegerConstantExpr(shift, C) && |
| 2541 | shift.isNonNegative()) { |
| 2542 | unsigned zext = shift.getZExtValue(); |
| 2543 | if (zext >= L.Width) |
| 2544 | L.Width = (L.NonNegative ? 0 : 1); |
| 2545 | else |
| 2546 | L.Width -= zext; |
| 2547 | } |
| 2548 | |
| 2549 | return L; |
| 2550 | } |
| 2551 | |
| 2552 | // Comma acts as its right operand. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2553 | case BO_Comma: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2554 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 2555 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2556 | // Black-list pointer subtractions. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2557 | case BO_Sub: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2558 | if (BO->getLHS()->getType()->isPointerType()) |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2559 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2560 | // fallthrough |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2561 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2562 | default: |
| 2563 | break; |
| 2564 | } |
| 2565 | |
| 2566 | // Treat every other operator as if it were closed on the |
| 2567 | // narrowest type that encompasses both operands. |
| 2568 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 2569 | IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth); |
| 2570 | return IntRange::join(L, R); |
| 2571 | } |
| 2572 | |
| 2573 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 2574 | switch (UO->getOpcode()) { |
| 2575 | // Boolean-valued operations are white-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2576 | case UO_LNot: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2577 | return IntRange::forBoolType(); |
| 2578 | |
| 2579 | // Operations with opaque sources are black-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2580 | case UO_Deref: |
| 2581 | case UO_AddrOf: // should be impossible |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2582 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2583 | |
| 2584 | default: |
| 2585 | return GetExprRange(C, UO->getSubExpr(), MaxWidth); |
| 2586 | } |
| 2587 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 2588 | |
| 2589 | if (dyn_cast<OffsetOfExpr>(E)) { |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2590 | IntRange::forValueOfType(C, E->getType()); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 2591 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2592 | |
| 2593 | FieldDecl *BitField = E->getBitField(); |
| 2594 | if (BitField) { |
| 2595 | llvm::APSInt BitWidthAP = BitField->getBitWidth()->EvaluateAsInt(C); |
| 2596 | unsigned BitWidth = BitWidthAP.getZExtValue(); |
| 2597 | |
Douglas Gregor | 5e9ebb3 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 2598 | return IntRange(BitWidth, |
| 2599 | BitField->getType()->isUnsignedIntegerOrEnumerationType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2602 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2603 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2604 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2605 | IntRange GetExprRange(ASTContext &C, Expr *E) { |
| 2606 | return GetExprRange(C, E, C.getIntWidth(E->getType())); |
| 2607 | } |
| 2608 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2609 | /// Checks whether the given value, which currently has the given |
| 2610 | /// source semantics, has the same value when coerced through the |
| 2611 | /// target semantics. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2612 | bool IsSameFloatAfterCast(const llvm::APFloat &value, |
| 2613 | const llvm::fltSemantics &Src, |
| 2614 | const llvm::fltSemantics &Tgt) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2615 | llvm::APFloat truncated = value; |
| 2616 | |
| 2617 | bool ignored; |
| 2618 | truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 2619 | truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 2620 | |
| 2621 | return truncated.bitwiseIsEqual(value); |
| 2622 | } |
| 2623 | |
| 2624 | /// Checks whether the given value, which currently has the given |
| 2625 | /// source semantics, has the same value when coerced through the |
| 2626 | /// target semantics. |
| 2627 | /// |
| 2628 | /// The value might be a vector of floats (or a complex number). |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2629 | bool IsSameFloatAfterCast(const APValue &value, |
| 2630 | const llvm::fltSemantics &Src, |
| 2631 | const llvm::fltSemantics &Tgt) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2632 | if (value.isFloat()) |
| 2633 | return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); |
| 2634 | |
| 2635 | if (value.isVector()) { |
| 2636 | for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) |
| 2637 | if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) |
| 2638 | return false; |
| 2639 | return true; |
| 2640 | } |
| 2641 | |
| 2642 | assert(value.isComplexFloat()); |
| 2643 | return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && |
| 2644 | IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); |
| 2645 | } |
| 2646 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2647 | void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2648 | |
Ted Kremenek | e3b159c | 2010-09-23 21:43:44 +0000 | [diff] [blame] | 2649 | static bool IsZero(Sema &S, Expr *E) { |
| 2650 | // Suppress cases where we are comparing against an enum constant. |
| 2651 | if (const DeclRefExpr *DR = |
| 2652 | dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) |
| 2653 | if (isa<EnumConstantDecl>(DR->getDecl())) |
| 2654 | return false; |
| 2655 | |
| 2656 | // Suppress cases where the '0' value is expanded from a macro. |
| 2657 | if (E->getLocStart().isMacroID()) |
| 2658 | return false; |
| 2659 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2660 | llvm::APSInt Value; |
| 2661 | return E->isIntegerConstantExpr(Value, S.Context) && Value == 0; |
| 2662 | } |
| 2663 | |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 2664 | static bool HasEnumType(Expr *E) { |
| 2665 | // Strip off implicit integral promotions. |
| 2666 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
Argyrios Kyrtzidis | 63b57ae | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 2667 | if (ICE->getCastKind() != CK_IntegralCast && |
| 2668 | ICE->getCastKind() != CK_NoOp) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 2669 | break; |
Argyrios Kyrtzidis | 63b57ae | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 2670 | E = ICE->getSubExpr(); |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | return E->getType()->isEnumeralType(); |
| 2674 | } |
| 2675 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2676 | void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2677 | BinaryOperatorKind op = E->getOpcode(); |
Douglas Gregor | 14af91a | 2010-12-21 07:22:56 +0000 | [diff] [blame] | 2678 | if (E->isValueDependent()) |
| 2679 | return; |
| 2680 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2681 | if (op == BO_LT && IsZero(S, E->getRHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2682 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 2683 | << "< 0" << "false" << HasEnumType(E->getLHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2684 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2685 | } else if (op == BO_GE && IsZero(S, E->getRHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2686 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 2687 | << ">= 0" << "true" << HasEnumType(E->getLHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2688 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2689 | } else if (op == BO_GT && IsZero(S, E->getLHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2690 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 2691 | << "0 >" << "false" << HasEnumType(E->getRHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2692 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2693 | } else if (op == BO_LE && IsZero(S, E->getLHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2694 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 2695 | << "0 <=" << "true" << HasEnumType(E->getRHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2696 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
| 2697 | } |
| 2698 | } |
| 2699 | |
| 2700 | /// Analyze the operands of the given comparison. Implements the |
| 2701 | /// fallback case from AnalyzeComparison. |
| 2702 | void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2703 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 2704 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2705 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2706 | |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2707 | /// \brief Implements -Wsign-compare. |
| 2708 | /// |
| 2709 | /// \param lex the left-hand expression |
| 2710 | /// \param rex the right-hand expression |
| 2711 | /// \param OpLoc the location of the joining operator |
John McCall | d1b47bf | 2010-03-11 19:43:18 +0000 | [diff] [blame] | 2712 | /// \param BinOpc binary opcode or 0 |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2713 | void AnalyzeComparison(Sema &S, BinaryOperator *E) { |
| 2714 | // The type the comparison is being performed in. |
| 2715 | QualType T = E->getLHS()->getType(); |
| 2716 | assert(S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType()) |
| 2717 | && "comparison with mismatched types"); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2718 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2719 | // We don't do anything special if this isn't an unsigned integral |
| 2720 | // comparison: we're only interested in integral comparisons, and |
| 2721 | // signed comparisons only happen in cases we don't care to warn about. |
Douglas Gregor | 3e026e3 | 2011-02-19 22:34:59 +0000 | [diff] [blame] | 2722 | // |
| 2723 | // We also don't care about value-dependent expressions or expressions |
| 2724 | // whose result is a constant. |
| 2725 | if (!T->hasUnsignedIntegerRepresentation() |
| 2726 | || E->isValueDependent() || E->isIntegerConstantExpr(S.Context)) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2727 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2728 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2729 | Expr *lex = E->getLHS()->IgnoreParenImpCasts(); |
| 2730 | Expr *rex = E->getRHS()->IgnoreParenImpCasts(); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2731 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2732 | // Check to see if one of the (unmodified) operands is of different |
| 2733 | // signedness. |
| 2734 | Expr *signedOperand, *unsignedOperand; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 2735 | if (lex->getType()->hasSignedIntegerRepresentation()) { |
| 2736 | assert(!rex->getType()->hasSignedIntegerRepresentation() && |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2737 | "unsigned comparison between two signed integer expressions?"); |
| 2738 | signedOperand = lex; |
| 2739 | unsignedOperand = rex; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 2740 | } else if (rex->getType()->hasSignedIntegerRepresentation()) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2741 | signedOperand = rex; |
| 2742 | unsignedOperand = lex; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2743 | } else { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2744 | CheckTrivialUnsignedComparison(S, E); |
| 2745 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2748 | // Otherwise, calculate the effective range of the signed operand. |
| 2749 | IntRange signedRange = GetExprRange(S.Context, signedOperand); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2750 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2751 | // Go ahead and analyze implicit conversions in the operands. Note |
| 2752 | // that we skip the implicit conversions on both sides. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2753 | AnalyzeImplicitConversions(S, lex, E->getOperatorLoc()); |
| 2754 | AnalyzeImplicitConversions(S, rex, E->getOperatorLoc()); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2755 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2756 | // If the signed range is non-negative, -Wsign-compare won't fire, |
| 2757 | // but we should still check for comparisons which are always true |
| 2758 | // or false. |
| 2759 | if (signedRange.NonNegative) |
| 2760 | return CheckTrivialUnsignedComparison(S, E); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2761 | |
| 2762 | // For (in)equality comparisons, if the unsigned operand is a |
| 2763 | // constant which cannot collide with a overflowed signed operand, |
| 2764 | // then reinterpreting the signed operand as unsigned will not |
| 2765 | // change the result of the comparison. |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2766 | if (E->isEqualityOp()) { |
| 2767 | unsigned comparisonWidth = S.Context.getIntWidth(T); |
| 2768 | IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2769 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2770 | // We should never be unable to prove that the unsigned operand is |
| 2771 | // non-negative. |
| 2772 | assert(unsignedRange.NonNegative && "unsigned range includes negative?"); |
| 2773 | |
| 2774 | if (unsignedRange.Width < comparisonWidth) |
| 2775 | return; |
| 2776 | } |
| 2777 | |
| 2778 | S.Diag(E->getOperatorLoc(), diag::warn_mixed_sign_comparison) |
| 2779 | << lex->getType() << rex->getType() |
| 2780 | << lex->getSourceRange() << rex->getSourceRange(); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 2783 | /// Analyzes an attempt to assign the given value to a bitfield. |
| 2784 | /// |
| 2785 | /// Returns true if there was something fishy about the attempt. |
| 2786 | bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, |
| 2787 | SourceLocation InitLoc) { |
| 2788 | assert(Bitfield->isBitField()); |
| 2789 | if (Bitfield->isInvalidDecl()) |
| 2790 | return false; |
| 2791 | |
John McCall | 91b6014 | 2010-11-11 05:33:51 +0000 | [diff] [blame] | 2792 | // White-list bool bitfields. |
| 2793 | if (Bitfield->getType()->isBooleanType()) |
| 2794 | return false; |
| 2795 | |
Douglas Gregor | 46ff303 | 2011-02-04 13:09:01 +0000 | [diff] [blame] | 2796 | // Ignore value- or type-dependent expressions. |
| 2797 | if (Bitfield->getBitWidth()->isValueDependent() || |
| 2798 | Bitfield->getBitWidth()->isTypeDependent() || |
| 2799 | Init->isValueDependent() || |
| 2800 | Init->isTypeDependent()) |
| 2801 | return false; |
| 2802 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 2803 | Expr *OriginalInit = Init->IgnoreParenImpCasts(); |
| 2804 | |
| 2805 | llvm::APSInt Width(32); |
| 2806 | Expr::EvalResult InitValue; |
| 2807 | if (!Bitfield->getBitWidth()->isIntegerConstantExpr(Width, S.Context) || |
John McCall | 91b6014 | 2010-11-11 05:33:51 +0000 | [diff] [blame] | 2808 | !OriginalInit->Evaluate(InitValue, S.Context) || |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 2809 | !InitValue.Val.isInt()) |
| 2810 | return false; |
| 2811 | |
| 2812 | const llvm::APSInt &Value = InitValue.Val.getInt(); |
| 2813 | unsigned OriginalWidth = Value.getBitWidth(); |
| 2814 | unsigned FieldWidth = Width.getZExtValue(); |
| 2815 | |
| 2816 | if (OriginalWidth <= FieldWidth) |
| 2817 | return false; |
| 2818 | |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2819 | llvm::APSInt TruncatedValue = Value.trunc(FieldWidth); |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 2820 | |
| 2821 | // It's fairly common to write values into signed bitfields |
| 2822 | // that, if sign-extended, would end up becoming a different |
| 2823 | // value. We don't want to warn about that. |
| 2824 | if (Value.isSigned() && Value.isNegative()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2825 | TruncatedValue = TruncatedValue.sext(OriginalWidth); |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 2826 | else |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2827 | TruncatedValue = TruncatedValue.zext(OriginalWidth); |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 2828 | |
| 2829 | if (Value == TruncatedValue) |
| 2830 | return false; |
| 2831 | |
| 2832 | std::string PrettyValue = Value.toString(10); |
| 2833 | std::string PrettyTrunc = TruncatedValue.toString(10); |
| 2834 | |
| 2835 | S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant) |
| 2836 | << PrettyValue << PrettyTrunc << OriginalInit->getType() |
| 2837 | << Init->getSourceRange(); |
| 2838 | |
| 2839 | return true; |
| 2840 | } |
| 2841 | |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 2842 | /// Analyze the given simple or compound assignment for warning-worthy |
| 2843 | /// operations. |
| 2844 | void AnalyzeAssignment(Sema &S, BinaryOperator *E) { |
| 2845 | // Just recurse on the LHS. |
| 2846 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 2847 | |
| 2848 | // We want to recurse on the RHS as normal unless we're assigning to |
| 2849 | // a bitfield. |
| 2850 | if (FieldDecl *Bitfield = E->getLHS()->getBitField()) { |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 2851 | if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(), |
| 2852 | E->getOperatorLoc())) { |
| 2853 | // Recurse, ignoring any implicit conversions on the RHS. |
| 2854 | return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(), |
| 2855 | E->getOperatorLoc()); |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 2856 | } |
| 2857 | } |
| 2858 | |
| 2859 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
| 2860 | } |
| 2861 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2862 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 2863 | void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, |
| 2864 | SourceLocation CContext, unsigned diag) { |
| 2865 | S.Diag(E->getExprLoc(), diag) |
| 2866 | << SourceType << T << E->getSourceRange() << SourceRange(CContext); |
| 2867 | } |
| 2868 | |
Chandler Carruth | e1b02e0 | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 2869 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
| 2870 | void DiagnoseImpCast(Sema &S, Expr *E, QualType T, SourceLocation CContext, |
| 2871 | unsigned diag) { |
| 2872 | DiagnoseImpCast(S, E, E->getType(), T, CContext, diag); |
| 2873 | } |
| 2874 | |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 2875 | /// Diagnose an implicit cast from a literal expression. Also attemps to supply |
| 2876 | /// fixit hints when the cast wouldn't lose information to simply write the |
| 2877 | /// expression with the expected type. |
| 2878 | void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T, |
| 2879 | SourceLocation CContext) { |
| 2880 | // Emit the primary warning first, then try to emit a fixit hint note if |
| 2881 | // reasonable. |
| 2882 | S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer) |
| 2883 | << FL->getType() << T << FL->getSourceRange() << SourceRange(CContext); |
| 2884 | |
| 2885 | const llvm::APFloat &Value = FL->getValue(); |
| 2886 | |
| 2887 | // Don't attempt to fix PPC double double literals. |
| 2888 | if (&Value.getSemantics() == &llvm::APFloat::PPCDoubleDouble) |
| 2889 | return; |
| 2890 | |
| 2891 | // Try to convert this exactly to an 64-bit integer. FIXME: It would be |
| 2892 | // nice to support arbitrarily large integers here. |
| 2893 | bool isExact = false; |
| 2894 | uint64_t IntegerPart; |
| 2895 | if (Value.convertToInteger(&IntegerPart, 64, /*isSigned=*/true, |
| 2896 | llvm::APFloat::rmTowardZero, &isExact) |
| 2897 | != llvm::APFloat::opOK || !isExact) |
| 2898 | return; |
| 2899 | |
| 2900 | llvm::APInt IntegerValue(64, IntegerPart, /*isSigned=*/true); |
| 2901 | |
| 2902 | std::string LiteralValue = IntegerValue.toString(10, /*isSigned=*/true); |
| 2903 | S.Diag(FL->getExprLoc(), diag::note_fix_integral_float_as_integer) |
| 2904 | << FixItHint::CreateReplacement(FL->getSourceRange(), LiteralValue); |
| 2905 | } |
| 2906 | |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 2907 | std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) { |
| 2908 | if (!Range.Width) return "0"; |
| 2909 | |
| 2910 | llvm::APSInt ValueInRange = Value; |
| 2911 | ValueInRange.setIsSigned(!Range.NonNegative); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2912 | ValueInRange = ValueInRange.trunc(Range.Width); |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 2913 | return ValueInRange.toString(10); |
| 2914 | } |
| 2915 | |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 2916 | static bool isFromSystemMacro(Sema &S, SourceLocation loc) { |
| 2917 | SourceManager &smgr = S.Context.getSourceManager(); |
| 2918 | return loc.isMacroID() && smgr.isInSystemHeader(smgr.getSpellingLoc(loc)); |
| 2919 | } |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 2920 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2921 | void CheckImplicitConversion(Sema &S, Expr *E, QualType T, |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2922 | SourceLocation CC, bool *ICContext = 0) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2923 | if (E->isTypeDependent() || E->isValueDependent()) return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2924 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2925 | const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr(); |
| 2926 | const Type *Target = S.Context.getCanonicalType(T).getTypePtr(); |
| 2927 | if (Source == Target) return; |
| 2928 | if (Target->isDependentType()) return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2929 | |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 2930 | // If the conversion context location is invalid don't complain. |
| 2931 | // We also don't want to emit a warning if the issue occurs from the |
| 2932 | // instantiation of a system macro. The problem is that 'getSpellingLoc()' |
| 2933 | // is slow, so we delay this check as long as possible. Once we detect |
| 2934 | // we are in that scenario, we just return. |
| 2935 | if (CC.isInvalid()) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2936 | return; |
| 2937 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2938 | // Never diagnose implicit casts to bool. |
| 2939 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) |
| 2940 | return; |
| 2941 | |
| 2942 | // Strip vector types. |
| 2943 | if (isa<VectorType>(Source)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 2944 | if (!isa<VectorType>(Target)) { |
| 2945 | if (isFromSystemMacro(S, CC)) |
| 2946 | return; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2947 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar); |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 2948 | } |
Chris Lattner | b792b30 | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 2949 | |
| 2950 | // If the vector cast is cast between two vectors of the same size, it is |
| 2951 | // a bitcast, not a conversion. |
| 2952 | if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target)) |
| 2953 | return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2954 | |
| 2955 | Source = cast<VectorType>(Source)->getElementType().getTypePtr(); |
| 2956 | Target = cast<VectorType>(Target)->getElementType().getTypePtr(); |
| 2957 | } |
| 2958 | |
| 2959 | // Strip complex types. |
| 2960 | if (isa<ComplexType>(Source)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 2961 | if (!isa<ComplexType>(Target)) { |
| 2962 | if (isFromSystemMacro(S, CC)) |
| 2963 | return; |
| 2964 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2965 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar); |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 2966 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2967 | |
| 2968 | Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); |
| 2969 | Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); |
| 2970 | } |
| 2971 | |
| 2972 | const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); |
| 2973 | const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); |
| 2974 | |
| 2975 | // If the source is floating point... |
| 2976 | if (SourceBT && SourceBT->isFloatingPoint()) { |
| 2977 | // ...and the target is floating point... |
| 2978 | if (TargetBT && TargetBT->isFloatingPoint()) { |
| 2979 | // ...then warn if we're dropping FP rank. |
| 2980 | |
| 2981 | // Builtin FP kinds are ordered by increasing FP rank. |
| 2982 | if (SourceBT->getKind() > TargetBT->getKind()) { |
| 2983 | // Don't warn about float constants that are precisely |
| 2984 | // representable in the target type. |
| 2985 | Expr::EvalResult result; |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2986 | if (E->Evaluate(result, S.Context)) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2987 | // Value might be a float, a float vector, or a float complex. |
| 2988 | if (IsSameFloatAfterCast(result.Val, |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2989 | S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)), |
| 2990 | S.Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2991 | return; |
| 2992 | } |
| 2993 | |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 2994 | if (isFromSystemMacro(S, CC)) |
| 2995 | return; |
| 2996 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2997 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2998 | } |
| 2999 | return; |
| 3000 | } |
| 3001 | |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3002 | // If the target is integral, always warn. |
Chandler Carruth | a5b9332 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 3003 | if ((TargetBT && TargetBT->isInteger())) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3004 | if (isFromSystemMacro(S, CC)) |
| 3005 | return; |
| 3006 | |
Chandler Carruth | a5b9332 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 3007 | Expr *InnerE = E->IgnoreParenImpCasts(); |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 3008 | if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) { |
| 3009 | DiagnoseFloatingLiteralImpCast(S, FL, T, CC); |
Chandler Carruth | a5b9332 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 3010 | } else { |
| 3011 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer); |
| 3012 | } |
| 3013 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3014 | |
| 3015 | return; |
| 3016 | } |
| 3017 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3018 | if (!Source->isIntegerType() || !Target->isIntegerType()) |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3019 | return; |
| 3020 | |
Richard Trieu | 1838ca5 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 3021 | if ((E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) |
| 3022 | == Expr::NPCK_GNUNull) && Target->isIntegerType()) { |
| 3023 | S.Diag(E->getExprLoc(), diag::warn_impcast_null_pointer_to_integer) |
| 3024 | << E->getSourceRange() << clang::SourceRange(CC); |
| 3025 | return; |
| 3026 | } |
| 3027 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3028 | IntRange SourceRange = GetExprRange(S.Context, E); |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3029 | IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3030 | |
| 3031 | if (SourceRange.Width > TargetRange.Width) { |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3032 | // If the source is a constant, use a default-on diagnostic. |
| 3033 | // TODO: this should happen for bitfield stores, too. |
| 3034 | llvm::APSInt Value(32); |
| 3035 | if (E->isIntegerConstantExpr(Value, S.Context)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3036 | if (isFromSystemMacro(S, CC)) |
| 3037 | return; |
| 3038 | |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3039 | std::string PrettySourceValue = Value.toString(10); |
| 3040 | std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); |
| 3041 | |
| 3042 | S.Diag(E->getExprLoc(), diag::warn_impcast_integer_precision_constant) |
| 3043 | << PrettySourceValue << PrettyTargetValue |
| 3044 | << E->getType() << T << E->getSourceRange() << clang::SourceRange(CC); |
| 3045 | return; |
| 3046 | } |
| 3047 | |
Chris Lattner | b792b30 | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 3048 | // People want to build with -Wshorten-64-to-32 and not -Wconversion. |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3049 | if (isFromSystemMacro(S, CC)) |
| 3050 | return; |
| 3051 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3052 | if (SourceRange.Width == 64 && TargetRange.Width == 32) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3053 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32); |
| 3054 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3055 | } |
| 3056 | |
| 3057 | if ((TargetRange.NonNegative && !SourceRange.NonNegative) || |
| 3058 | (!TargetRange.NonNegative && SourceRange.NonNegative && |
| 3059 | SourceRange.Width == TargetRange.Width)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3060 | |
| 3061 | if (isFromSystemMacro(S, CC)) |
| 3062 | return; |
| 3063 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3064 | unsigned DiagID = diag::warn_impcast_integer_sign; |
| 3065 | |
| 3066 | // Traditionally, gcc has warned about this under -Wsign-compare. |
| 3067 | // We also want to warn about it in -Wconversion. |
| 3068 | // So if -Wconversion is off, use a completely identical diagnostic |
| 3069 | // in the sign-compare group. |
| 3070 | // The conditional-checking code will |
| 3071 | if (ICContext) { |
| 3072 | DiagID = diag::warn_impcast_integer_sign_conditional; |
| 3073 | *ICContext = true; |
| 3074 | } |
| 3075 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3076 | return DiagnoseImpCast(S, E, T, CC, DiagID); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 3079 | // Diagnose conversions between different enumeration types. |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 3080 | // In C, we pretend that the type of an EnumConstantDecl is its enumeration |
| 3081 | // type, to give us better diagnostics. |
| 3082 | QualType SourceType = E->getType(); |
| 3083 | if (!S.getLangOptions().CPlusPlus) { |
| 3084 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 3085 | if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 3086 | EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext()); |
| 3087 | SourceType = S.Context.getTypeDeclType(Enum); |
| 3088 | Source = S.Context.getCanonicalType(SourceType).getTypePtr(); |
| 3089 | } |
| 3090 | } |
| 3091 | |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 3092 | if (const EnumType *SourceEnum = Source->getAs<EnumType>()) |
| 3093 | if (const EnumType *TargetEnum = Target->getAs<EnumType>()) |
| 3094 | if ((SourceEnum->getDecl()->getIdentifier() || |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3095 | SourceEnum->getDecl()->getTypedefNameForAnonDecl()) && |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 3096 | (TargetEnum->getDecl()->getIdentifier() || |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3097 | TargetEnum->getDecl()->getTypedefNameForAnonDecl()) && |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3098 | SourceEnum != TargetEnum) { |
| 3099 | if (isFromSystemMacro(S, CC)) |
| 3100 | return; |
| 3101 | |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 3102 | return DiagnoseImpCast(S, E, SourceType, T, CC, |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 3103 | diag::warn_impcast_different_enum_types); |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3104 | } |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 3105 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3106 | return; |
| 3107 | } |
| 3108 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3109 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T); |
| 3110 | |
| 3111 | void CheckConditionalOperand(Sema &S, Expr *E, QualType T, |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3112 | SourceLocation CC, bool &ICContext) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3113 | E = E->IgnoreParenImpCasts(); |
| 3114 | |
| 3115 | if (isa<ConditionalOperator>(E)) |
| 3116 | return CheckConditionalOperator(S, cast<ConditionalOperator>(E), T); |
| 3117 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3118 | AnalyzeImplicitConversions(S, E, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3119 | if (E->getType() != T) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3120 | return CheckImplicitConversion(S, E, T, CC, &ICContext); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3121 | return; |
| 3122 | } |
| 3123 | |
| 3124 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T) { |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3125 | SourceLocation CC = E->getQuestionLoc(); |
| 3126 | |
| 3127 | AnalyzeImplicitConversions(S, E->getCond(), CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3128 | |
| 3129 | bool Suspicious = false; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3130 | CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious); |
| 3131 | CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3132 | |
| 3133 | // If -Wconversion would have warned about either of the candidates |
| 3134 | // for a signedness conversion to the context type... |
| 3135 | if (!Suspicious) return; |
| 3136 | |
| 3137 | // ...but it's currently ignored... |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3138 | if (S.Diags.getDiagnosticLevel(diag::warn_impcast_integer_sign_conditional, |
| 3139 | CC)) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3140 | return; |
| 3141 | |
| 3142 | // ...and -Wsign-compare isn't... |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3143 | if (!S.Diags.getDiagnosticLevel(diag::warn_mixed_sign_conditional, CC)) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3144 | return; |
| 3145 | |
| 3146 | // ...then check whether it would have warned about either of the |
| 3147 | // candidates for a signedness conversion to the condition type. |
| 3148 | if (E->getType() != T) { |
| 3149 | Suspicious = false; |
| 3150 | CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(), |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3151 | E->getType(), CC, &Suspicious); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3152 | if (!Suspicious) |
| 3153 | CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(), |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3154 | E->getType(), CC, &Suspicious); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3155 | if (!Suspicious) |
| 3156 | return; |
| 3157 | } |
| 3158 | |
| 3159 | // If so, emit a diagnostic under -Wsign-compare. |
| 3160 | Expr *lex = E->getTrueExpr()->IgnoreParenImpCasts(); |
| 3161 | Expr *rex = E->getFalseExpr()->IgnoreParenImpCasts(); |
| 3162 | S.Diag(E->getQuestionLoc(), diag::warn_mixed_sign_conditional) |
| 3163 | << lex->getType() << rex->getType() |
| 3164 | << lex->getSourceRange() << rex->getSourceRange(); |
| 3165 | } |
| 3166 | |
| 3167 | /// AnalyzeImplicitConversions - Find and report any interesting |
| 3168 | /// implicit conversions in the given expression. There are a couple |
| 3169 | /// of competing diagnostics here, -Wconversion and -Wsign-compare. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3170 | void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3171 | QualType T = OrigE->getType(); |
| 3172 | Expr *E = OrigE->IgnoreParenImpCasts(); |
| 3173 | |
| 3174 | // For conditional operators, we analyze the arguments as if they |
| 3175 | // were being fed directly into the output. |
| 3176 | if (isa<ConditionalOperator>(E)) { |
| 3177 | ConditionalOperator *CO = cast<ConditionalOperator>(E); |
| 3178 | CheckConditionalOperator(S, CO, T); |
| 3179 | return; |
| 3180 | } |
| 3181 | |
| 3182 | // Go ahead and check any implicit conversions we might have skipped. |
| 3183 | // The non-canonical typecheck is just an optimization; |
| 3184 | // CheckImplicitConversion will filter out dead implicit conversions. |
| 3185 | if (E->getType() != T) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3186 | CheckImplicitConversion(S, E, T, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3187 | |
| 3188 | // Now continue drilling into this expression. |
| 3189 | |
| 3190 | // Skip past explicit casts. |
| 3191 | if (isa<ExplicitCastExpr>(E)) { |
| 3192 | E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts(); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3193 | return AnalyzeImplicitConversions(S, E, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 3196 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 3197 | // Do a somewhat different check with comparison operators. |
| 3198 | if (BO->isComparisonOp()) |
| 3199 | return AnalyzeComparison(S, BO); |
| 3200 | |
| 3201 | // And with assignments and compound assignments. |
| 3202 | if (BO->isAssignmentOp()) |
| 3203 | return AnalyzeAssignment(S, BO); |
| 3204 | } |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3205 | |
| 3206 | // These break the otherwise-useful invariant below. Fortunately, |
| 3207 | // we don't really need to recurse into them, because any internal |
| 3208 | // expressions should have been analyzed already when they were |
| 3209 | // built into statements. |
| 3210 | if (isa<StmtExpr>(E)) return; |
| 3211 | |
| 3212 | // Don't descend into unevaluated contexts. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3213 | if (isa<UnaryExprOrTypeTraitExpr>(E)) return; |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3214 | |
| 3215 | // Now just recurse over the expression's children. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3216 | CC = E->getExprLoc(); |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 3217 | for (Stmt::child_range I = E->children(); I; ++I) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3218 | AnalyzeImplicitConversions(S, cast<Expr>(*I), CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
| 3221 | } // end anonymous namespace |
| 3222 | |
| 3223 | /// Diagnoses "dangerous" implicit conversions within the given |
| 3224 | /// expression (which is a full expression). Implements -Wconversion |
| 3225 | /// and -Wsign-compare. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3226 | /// |
| 3227 | /// \param CC the "context" location of the implicit conversion, i.e. |
| 3228 | /// the most location of the syntactic entity requiring the implicit |
| 3229 | /// conversion |
| 3230 | void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3231 | // Don't diagnose in unevaluated contexts. |
| 3232 | if (ExprEvalContexts.back().Context == Sema::Unevaluated) |
| 3233 | return; |
| 3234 | |
| 3235 | // Don't diagnose for value- or type-dependent expressions. |
| 3236 | if (E->isTypeDependent() || E->isValueDependent()) |
| 3237 | return; |
| 3238 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3239 | // This is not the right CC for (e.g.) a variable initialization. |
| 3240 | AnalyzeImplicitConversions(*this, E, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3241 | } |
| 3242 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3243 | void Sema::CheckBitFieldInitialization(SourceLocation InitLoc, |
| 3244 | FieldDecl *BitField, |
| 3245 | Expr *Init) { |
| 3246 | (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc); |
| 3247 | } |
| 3248 | |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 3249 | /// CheckParmsForFunctionDef - Check that the parameters of the given |
| 3250 | /// function are appropriate for the definition of a function. This |
| 3251 | /// takes care of any checks that cannot be performed on the |
| 3252 | /// declaration itself, e.g., that the types of each of the function |
| 3253 | /// parameters are complete. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 3254 | bool Sema::CheckParmsForFunctionDef(ParmVarDecl **P, ParmVarDecl **PEnd, |
| 3255 | bool CheckParameterNames) { |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 3256 | bool HasInvalidParm = false; |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 3257 | for (; P != PEnd; ++P) { |
| 3258 | ParmVarDecl *Param = *P; |
| 3259 | |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 3260 | // C99 6.7.5.3p4: the parameters in a parameter type list in a |
| 3261 | // function declarator that is part of a function definition of |
| 3262 | // that function shall not have incomplete type. |
| 3263 | // |
| 3264 | // This is also C++ [dcl.fct]p6. |
| 3265 | if (!Param->isInvalidDecl() && |
| 3266 | RequireCompleteType(Param->getLocation(), Param->getType(), |
| 3267 | diag::err_typecheck_decl_incomplete_type)) { |
| 3268 | Param->setInvalidDecl(); |
| 3269 | HasInvalidParm = true; |
| 3270 | } |
| 3271 | |
| 3272 | // C99 6.9.1p5: If the declarator includes a parameter type list, the |
| 3273 | // declaration of each parameter shall include an identifier. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 3274 | if (CheckParameterNames && |
| 3275 | Param->getIdentifier() == 0 && |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 3276 | !Param->isImplicit() && |
| 3277 | !getLangOptions().CPlusPlus) |
| 3278 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
Sam Weinig | d17e340 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 3279 | |
| 3280 | // C99 6.7.5.3p12: |
| 3281 | // If the function declarator is not part of a definition of that |
| 3282 | // function, parameters may have incomplete type and may use the [*] |
| 3283 | // notation in their sequences of declarator specifiers to specify |
| 3284 | // variable length array types. |
| 3285 | QualType PType = Param->getOriginalType(); |
| 3286 | if (const ArrayType *AT = Context.getAsArrayType(PType)) { |
| 3287 | if (AT->getSizeModifier() == ArrayType::Star) { |
| 3288 | // FIXME: This diagnosic should point the the '[*]' if source-location |
| 3289 | // information is added for it. |
| 3290 | Diag(Param->getLocation(), diag::err_array_star_in_function_definition); |
| 3291 | } |
| 3292 | } |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | return HasInvalidParm; |
| 3296 | } |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 3297 | |
| 3298 | /// CheckCastAlign - Implements -Wcast-align, which warns when a |
| 3299 | /// pointer cast increases the alignment requirements. |
| 3300 | void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { |
| 3301 | // This is actually a lot of work to potentially be doing on every |
| 3302 | // cast; don't do it if we're ignoring -Wcast_align (as is the default). |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3303 | if (getDiagnostics().getDiagnosticLevel(diag::warn_cast_align, |
| 3304 | TRange.getBegin()) |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 3305 | == Diagnostic::Ignored) |
| 3306 | return; |
| 3307 | |
| 3308 | // Ignore dependent types. |
| 3309 | if (T->isDependentType() || Op->getType()->isDependentType()) |
| 3310 | return; |
| 3311 | |
| 3312 | // Require that the destination be a pointer type. |
| 3313 | const PointerType *DestPtr = T->getAs<PointerType>(); |
| 3314 | if (!DestPtr) return; |
| 3315 | |
| 3316 | // If the destination has alignment 1, we're done. |
| 3317 | QualType DestPointee = DestPtr->getPointeeType(); |
| 3318 | if (DestPointee->isIncompleteType()) return; |
| 3319 | CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); |
| 3320 | if (DestAlign.isOne()) return; |
| 3321 | |
| 3322 | // Require that the source be a pointer type. |
| 3323 | const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); |
| 3324 | if (!SrcPtr) return; |
| 3325 | QualType SrcPointee = SrcPtr->getPointeeType(); |
| 3326 | |
| 3327 | // Whitelist casts from cv void*. We already implicitly |
| 3328 | // whitelisted casts to cv void*, since they have alignment 1. |
| 3329 | // Also whitelist casts involving incomplete types, which implicitly |
| 3330 | // includes 'void'. |
| 3331 | if (SrcPointee->isIncompleteType()) return; |
| 3332 | |
| 3333 | CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee); |
| 3334 | if (SrcAlign >= DestAlign) return; |
| 3335 | |
| 3336 | Diag(TRange.getBegin(), diag::warn_cast_align) |
| 3337 | << Op->getType() << T |
| 3338 | << static_cast<unsigned>(SrcAlign.getQuantity()) |
| 3339 | << static_cast<unsigned>(DestAlign.getQuantity()) |
| 3340 | << TRange << Op->getSourceRange(); |
| 3341 | } |
| 3342 | |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 3343 | static void CheckArrayAccess_Check(Sema &S, |
| 3344 | const clang::ArraySubscriptExpr *E) { |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 3345 | const Expr *BaseExpr = E->getBase()->IgnoreParenImpCasts(); |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 3346 | const ConstantArrayType *ArrayTy = |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 3347 | S.Context.getAsConstantArrayType(BaseExpr->getType()); |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 3348 | if (!ArrayTy) |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 3349 | return; |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 3350 | |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 3351 | const Expr *IndexExpr = E->getIdx(); |
| 3352 | if (IndexExpr->isValueDependent()) |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 3353 | return; |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 3354 | llvm::APSInt index; |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 3355 | if (!IndexExpr->isIntegerConstantExpr(index, S.Context)) |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 3356 | return; |
Ted Kremenek | 8fd0a5d | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 3357 | |
Ted Kremenek | 9e060ca | 2011-02-23 23:06:04 +0000 | [diff] [blame] | 3358 | if (index.isUnsigned() || !index.isNegative()) { |
Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 3359 | llvm::APInt size = ArrayTy->getSize(); |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 3360 | if (!size.isStrictlyPositive()) |
| 3361 | return; |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 3362 | if (size.getBitWidth() > index.getBitWidth()) |
| 3363 | index = index.sext(size.getBitWidth()); |
Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 3364 | else if (size.getBitWidth() < index.getBitWidth()) |
| 3365 | size = size.sext(index.getBitWidth()); |
| 3366 | |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 3367 | if (index.slt(size)) |
Ted Kremenek | 8fd0a5d | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 3368 | return; |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 3369 | |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 3370 | S.DiagRuntimeBehavior(E->getBase()->getLocStart(), BaseExpr, |
| 3371 | S.PDiag(diag::warn_array_index_exceeds_bounds) |
| 3372 | << index.toString(10, true) |
| 3373 | << size.toString(10, true) |
| 3374 | << IndexExpr->getSourceRange()); |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 3375 | } else { |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 3376 | S.DiagRuntimeBehavior(E->getBase()->getLocStart(), BaseExpr, |
| 3377 | S.PDiag(diag::warn_array_index_precedes_bounds) |
| 3378 | << index.toString(10, true) |
| 3379 | << IndexExpr->getSourceRange()); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 3380 | } |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 3381 | |
| 3382 | const NamedDecl *ND = NULL; |
| 3383 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
| 3384 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); |
| 3385 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) |
| 3386 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); |
| 3387 | if (ND) |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 3388 | S.DiagRuntimeBehavior(ND->getLocStart(), BaseExpr, |
| 3389 | S.PDiag(diag::note_array_index_out_of_bounds) |
| 3390 | << ND->getDeclName()); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 3393 | void Sema::CheckArrayAccess(const Expr *expr) { |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3394 | while (true) { |
| 3395 | expr = expr->IgnoreParens(); |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 3396 | switch (expr->getStmtClass()) { |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 3397 | case Stmt::ArraySubscriptExprClass: |
| 3398 | CheckArrayAccess_Check(*this, cast<ArraySubscriptExpr>(expr)); |
| 3399 | return; |
| 3400 | case Stmt::ConditionalOperatorClass: { |
| 3401 | const ConditionalOperator *cond = cast<ConditionalOperator>(expr); |
| 3402 | if (const Expr *lhs = cond->getLHS()) |
| 3403 | CheckArrayAccess(lhs); |
| 3404 | if (const Expr *rhs = cond->getRHS()) |
| 3405 | CheckArrayAccess(rhs); |
| 3406 | return; |
| 3407 | } |
| 3408 | default: |
| 3409 | return; |
| 3410 | } |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3411 | } |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 3412 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 3413 | |
| 3414 | //===--- CHECK: Objective-C retain cycles ----------------------------------// |
| 3415 | |
| 3416 | namespace { |
| 3417 | struct RetainCycleOwner { |
| 3418 | RetainCycleOwner() : Variable(0), Indirect(false) {} |
| 3419 | VarDecl *Variable; |
| 3420 | SourceRange Range; |
| 3421 | SourceLocation Loc; |
| 3422 | bool Indirect; |
| 3423 | |
| 3424 | void setLocsFrom(Expr *e) { |
| 3425 | Loc = e->getExprLoc(); |
| 3426 | Range = e->getSourceRange(); |
| 3427 | } |
| 3428 | }; |
| 3429 | } |
| 3430 | |
| 3431 | /// Consider whether capturing the given variable can possibly lead to |
| 3432 | /// a retain cycle. |
| 3433 | static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) { |
| 3434 | // In ARC, it's captured strongly iff the variable has __strong |
| 3435 | // lifetime. In MRR, it's captured strongly if the variable is |
| 3436 | // __block and has an appropriate type. |
| 3437 | if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 3438 | return false; |
| 3439 | |
| 3440 | owner.Variable = var; |
| 3441 | owner.setLocsFrom(ref); |
| 3442 | return true; |
| 3443 | } |
| 3444 | |
| 3445 | static bool findRetainCycleOwner(Expr *e, RetainCycleOwner &owner) { |
| 3446 | while (true) { |
| 3447 | e = e->IgnoreParens(); |
| 3448 | if (CastExpr *cast = dyn_cast<CastExpr>(e)) { |
| 3449 | switch (cast->getCastKind()) { |
| 3450 | case CK_BitCast: |
| 3451 | case CK_LValueBitCast: |
| 3452 | case CK_LValueToRValue: |
| 3453 | e = cast->getSubExpr(); |
| 3454 | continue; |
| 3455 | |
| 3456 | case CK_GetObjCProperty: { |
| 3457 | // Bail out if this isn't a strong explicit property. |
| 3458 | const ObjCPropertyRefExpr *pre = cast->getSubExpr()->getObjCProperty(); |
| 3459 | if (pre->isImplicitProperty()) return false; |
| 3460 | ObjCPropertyDecl *property = pre->getExplicitProperty(); |
| 3461 | if (!(property->getPropertyAttributes() & |
| 3462 | (ObjCPropertyDecl::OBJC_PR_retain | |
| 3463 | ObjCPropertyDecl::OBJC_PR_copy | |
| 3464 | ObjCPropertyDecl::OBJC_PR_strong)) && |
| 3465 | !(property->getPropertyIvarDecl() && |
| 3466 | property->getPropertyIvarDecl()->getType() |
| 3467 | .getObjCLifetime() == Qualifiers::OCL_Strong)) |
| 3468 | return false; |
| 3469 | |
| 3470 | owner.Indirect = true; |
| 3471 | e = const_cast<Expr*>(pre->getBase()); |
| 3472 | continue; |
| 3473 | } |
| 3474 | |
| 3475 | default: |
| 3476 | return false; |
| 3477 | } |
| 3478 | } |
| 3479 | |
| 3480 | if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) { |
| 3481 | ObjCIvarDecl *ivar = ref->getDecl(); |
| 3482 | if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 3483 | return false; |
| 3484 | |
| 3485 | // Try to find a retain cycle in the base. |
| 3486 | if (!findRetainCycleOwner(ref->getBase(), owner)) |
| 3487 | return false; |
| 3488 | |
| 3489 | if (ref->isFreeIvar()) owner.setLocsFrom(ref); |
| 3490 | owner.Indirect = true; |
| 3491 | return true; |
| 3492 | } |
| 3493 | |
| 3494 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 3495 | VarDecl *var = dyn_cast<VarDecl>(ref->getDecl()); |
| 3496 | if (!var) return false; |
| 3497 | return considerVariable(var, ref, owner); |
| 3498 | } |
| 3499 | |
| 3500 | if (BlockDeclRefExpr *ref = dyn_cast<BlockDeclRefExpr>(e)) { |
| 3501 | owner.Variable = ref->getDecl(); |
| 3502 | owner.setLocsFrom(ref); |
| 3503 | return true; |
| 3504 | } |
| 3505 | |
| 3506 | if (MemberExpr *member = dyn_cast<MemberExpr>(e)) { |
| 3507 | if (member->isArrow()) return false; |
| 3508 | |
| 3509 | // Don't count this as an indirect ownership. |
| 3510 | e = member->getBase(); |
| 3511 | continue; |
| 3512 | } |
| 3513 | |
| 3514 | // Array ivars? |
| 3515 | |
| 3516 | return false; |
| 3517 | } |
| 3518 | } |
| 3519 | |
| 3520 | namespace { |
| 3521 | struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> { |
| 3522 | FindCaptureVisitor(ASTContext &Context, VarDecl *variable) |
| 3523 | : EvaluatedExprVisitor<FindCaptureVisitor>(Context), |
| 3524 | Variable(variable), Capturer(0) {} |
| 3525 | |
| 3526 | VarDecl *Variable; |
| 3527 | Expr *Capturer; |
| 3528 | |
| 3529 | void VisitDeclRefExpr(DeclRefExpr *ref) { |
| 3530 | if (ref->getDecl() == Variable && !Capturer) |
| 3531 | Capturer = ref; |
| 3532 | } |
| 3533 | |
| 3534 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *ref) { |
| 3535 | if (ref->getDecl() == Variable && !Capturer) |
| 3536 | Capturer = ref; |
| 3537 | } |
| 3538 | |
| 3539 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) { |
| 3540 | if (Capturer) return; |
| 3541 | Visit(ref->getBase()); |
| 3542 | if (Capturer && ref->isFreeIvar()) |
| 3543 | Capturer = ref; |
| 3544 | } |
| 3545 | |
| 3546 | void VisitBlockExpr(BlockExpr *block) { |
| 3547 | // Look inside nested blocks |
| 3548 | if (block->getBlockDecl()->capturesVariable(Variable)) |
| 3549 | Visit(block->getBlockDecl()->getBody()); |
| 3550 | } |
| 3551 | }; |
| 3552 | } |
| 3553 | |
| 3554 | /// Check whether the given argument is a block which captures a |
| 3555 | /// variable. |
| 3556 | static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) { |
| 3557 | assert(owner.Variable && owner.Loc.isValid()); |
| 3558 | |
| 3559 | e = e->IgnoreParenCasts(); |
| 3560 | BlockExpr *block = dyn_cast<BlockExpr>(e); |
| 3561 | if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable)) |
| 3562 | return 0; |
| 3563 | |
| 3564 | FindCaptureVisitor visitor(S.Context, owner.Variable); |
| 3565 | visitor.Visit(block->getBlockDecl()->getBody()); |
| 3566 | return visitor.Capturer; |
| 3567 | } |
| 3568 | |
| 3569 | static void diagnoseRetainCycle(Sema &S, Expr *capturer, |
| 3570 | RetainCycleOwner &owner) { |
| 3571 | assert(capturer); |
| 3572 | assert(owner.Variable && owner.Loc.isValid()); |
| 3573 | |
| 3574 | S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle) |
| 3575 | << owner.Variable << capturer->getSourceRange(); |
| 3576 | S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner) |
| 3577 | << owner.Indirect << owner.Range; |
| 3578 | } |
| 3579 | |
| 3580 | /// Check for a keyword selector that starts with the word 'add' or |
| 3581 | /// 'set'. |
| 3582 | static bool isSetterLikeSelector(Selector sel) { |
| 3583 | if (sel.isUnarySelector()) return false; |
| 3584 | |
| 3585 | llvm::StringRef str = sel.getNameForSlot(0); |
| 3586 | while (!str.empty() && str.front() == '_') str = str.substr(1); |
| 3587 | if (str.startswith("set") || str.startswith("add")) |
| 3588 | str = str.substr(3); |
| 3589 | else |
| 3590 | return false; |
| 3591 | |
| 3592 | if (str.empty()) return true; |
| 3593 | return !islower(str.front()); |
| 3594 | } |
| 3595 | |
| 3596 | /// Check a message send to see if it's likely to cause a retain cycle. |
| 3597 | void Sema::checkRetainCycles(ObjCMessageExpr *msg) { |
| 3598 | // Only check instance methods whose selector looks like a setter. |
| 3599 | if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector())) |
| 3600 | return; |
| 3601 | |
| 3602 | // Try to find a variable that the receiver is strongly owned by. |
| 3603 | RetainCycleOwner owner; |
| 3604 | if (msg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 3605 | if (!findRetainCycleOwner(msg->getInstanceReceiver(), owner)) |
| 3606 | return; |
| 3607 | } else { |
| 3608 | assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance); |
| 3609 | owner.Variable = getCurMethodDecl()->getSelfDecl(); |
| 3610 | owner.Loc = msg->getSuperLoc(); |
| 3611 | owner.Range = msg->getSuperLoc(); |
| 3612 | } |
| 3613 | |
| 3614 | // Check whether the receiver is captured by any of the arguments. |
| 3615 | for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) |
| 3616 | if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) |
| 3617 | return diagnoseRetainCycle(*this, capturer, owner); |
| 3618 | } |
| 3619 | |
| 3620 | /// Check a property assign to see if it's likely to cause a retain cycle. |
| 3621 | void Sema::checkRetainCycles(Expr *receiver, Expr *argument) { |
| 3622 | RetainCycleOwner owner; |
| 3623 | if (!findRetainCycleOwner(receiver, owner)) |
| 3624 | return; |
| 3625 | |
| 3626 | if (Expr *capturer = findCapturingExpr(*this, argument, owner)) |
| 3627 | diagnoseRetainCycle(*this, capturer, owner); |
| 3628 | } |
| 3629 | |
| 3630 | void Sema::checkUnsafeAssigns(SourceLocation Loc, |
| 3631 | QualType LHS, Expr *RHS) { |
| 3632 | Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime(); |
| 3633 | if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone) |
| 3634 | return; |
| 3635 | if (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) |
| 3636 | if (cast->getCastKind() == CK_ObjCConsumeObject) |
| 3637 | Diag(Loc, diag::warn_arc_retained_assign) |
| 3638 | << (LT == Qualifiers::OCL_ExplicitNone) |
| 3639 | << RHS->getSourceRange(); |
| 3640 | } |
| 3641 | |