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