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