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