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