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 | |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Initialization.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Sema.h" |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaInternal.h" |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Initialization.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 19 | #include "clang/Sema/ScopeInfo.h" |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/Analyses/FormatString.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 22 | #include "clang/AST/CharUnits.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprObjC.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 27 | #include "clang/AST/EvaluatedExprVisitor.h" |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 28 | #include "clang/AST/DeclObjC.h" |
| 29 | #include "clang/AST/StmtCXX.h" |
| 30 | #include "clang/AST/StmtObjC.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Preprocessor.h" |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/BitVector.h" |
| 33 | #include "llvm/ADT/STLExtras.h" |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 35 | #include "clang/Basic/TargetBuiltins.h" |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 36 | #include "clang/Basic/TargetInfo.h" |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 37 | #include "clang/Basic/ConvertUTF.h" |
Zhongxing Xu | a1f3dba | 2009-05-20 01:55:10 +0000 | [diff] [blame] | 38 | #include <limits> |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 39 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 40 | using namespace sema; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 42 | SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, |
| 43 | unsigned ByteNo) const { |
Chris Lattner | 08f92e3 | 2010-11-17 07:37:15 +0000 | [diff] [blame] | 44 | return SL->getLocationOfByte(ByteNo, PP.getSourceManager(), |
| 45 | PP.getLangOptions(), PP.getTargetInfo()); |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 46 | } |
| 47 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 48 | /// Checks that a call expression's argument count is the desired number. |
| 49 | /// This is useful when doing custom type-checking. Returns true on error. |
| 50 | static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) { |
| 51 | unsigned argCount = call->getNumArgs(); |
| 52 | if (argCount == desiredArgCount) return false; |
| 53 | |
| 54 | if (argCount < desiredArgCount) |
| 55 | return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 56 | << 0 /*function call*/ << desiredArgCount << argCount |
| 57 | << call->getSourceRange(); |
| 58 | |
| 59 | // Highlight all the excess arguments. |
| 60 | SourceRange range(call->getArg(desiredArgCount)->getLocStart(), |
| 61 | call->getArg(argCount - 1)->getLocEnd()); |
| 62 | |
| 63 | return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args) |
| 64 | << 0 /*function call*/ << desiredArgCount << argCount |
| 65 | << call->getArg(1)->getSourceRange(); |
| 66 | } |
| 67 | |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 68 | /// CheckBuiltinAnnotationString - Checks that string argument to the builtin |
| 69 | /// annotation is a non wide string literal. |
| 70 | static bool CheckBuiltinAnnotationString(Sema &S, Expr *Arg) { |
| 71 | Arg = Arg->IgnoreParenCasts(); |
| 72 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); |
| 73 | if (!Literal || !Literal->isAscii()) { |
| 74 | S.Diag(Arg->getLocStart(), diag::err_builtin_annotation_not_string_constant) |
| 75 | << Arg->getSourceRange(); |
| 76 | return true; |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 81 | ExprResult |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 82 | Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 83 | ExprResult TheCallResult(Owned(TheCall)); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 946928f | 2010-10-01 23:23:24 +0000 | [diff] [blame] | 85 | // Find out if any arguments are required to be integer constant expressions. |
| 86 | unsigned ICEArguments = 0; |
| 87 | ASTContext::GetBuiltinTypeError Error; |
| 88 | Context.GetBuiltinType(BuiltinID, Error, &ICEArguments); |
| 89 | if (Error != ASTContext::GE_None) |
| 90 | ICEArguments = 0; // Don't diagnose previously diagnosed errors. |
| 91 | |
| 92 | // If any arguments are required to be ICE's, check and diagnose. |
| 93 | for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) { |
| 94 | // Skip arguments not required to be ICE's. |
| 95 | if ((ICEArguments & (1 << ArgNo)) == 0) continue; |
| 96 | |
| 97 | llvm::APSInt Result; |
| 98 | if (SemaBuiltinConstantArg(TheCall, ArgNo, Result)) |
| 99 | return true; |
| 100 | ICEArguments &= ~(1 << ArgNo); |
| 101 | } |
| 102 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 103 | switch (BuiltinID) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 104 | case Builtin::BI__builtin___CFStringMakeConstantString: |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 105 | assert(TheCall->getNumArgs() == 1 && |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 106 | "Wrong # arguments to builtin CFStringMakeConstantString"); |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 107 | if (CheckObjCString(TheCall->getArg(0))) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 108 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 109 | break; |
Ted Kremenek | 49ff7a1 | 2008-07-09 17:58:53 +0000 | [diff] [blame] | 110 | case Builtin::BI__builtin_stdarg_start: |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 111 | case Builtin::BI__builtin_va_start: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 112 | if (SemaBuiltinVAStart(TheCall)) |
| 113 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 114 | break; |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 115 | case Builtin::BI__builtin_isgreater: |
| 116 | case Builtin::BI__builtin_isgreaterequal: |
| 117 | case Builtin::BI__builtin_isless: |
| 118 | case Builtin::BI__builtin_islessequal: |
| 119 | case Builtin::BI__builtin_islessgreater: |
| 120 | case Builtin::BI__builtin_isunordered: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 121 | if (SemaBuiltinUnorderedCompare(TheCall)) |
| 122 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 123 | break; |
Benjamin Kramer | e771a7a | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 124 | case Builtin::BI__builtin_fpclassify: |
| 125 | if (SemaBuiltinFPClassification(TheCall, 6)) |
| 126 | return ExprError(); |
| 127 | break; |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 128 | case Builtin::BI__builtin_isfinite: |
| 129 | case Builtin::BI__builtin_isinf: |
| 130 | case Builtin::BI__builtin_isinf_sign: |
| 131 | case Builtin::BI__builtin_isnan: |
| 132 | case Builtin::BI__builtin_isnormal: |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 133 | if (SemaBuiltinFPClassification(TheCall, 1)) |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 134 | return ExprError(); |
| 135 | break; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 136 | case Builtin::BI__builtin_shufflevector: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 137 | return SemaBuiltinShuffleVector(TheCall); |
| 138 | // TheCall will be freed by the smart pointer here, but that's fine, since |
| 139 | // SemaBuiltinShuffleVector guts it, but then doesn't release it. |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 140 | case Builtin::BI__builtin_prefetch: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 141 | if (SemaBuiltinPrefetch(TheCall)) |
| 142 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 143 | break; |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 144 | case Builtin::BI__builtin_object_size: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 145 | if (SemaBuiltinObjectSize(TheCall)) |
| 146 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 147 | break; |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 148 | case Builtin::BI__builtin_longjmp: |
| 149 | if (SemaBuiltinLongjmp(TheCall)) |
| 150 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 151 | break; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 152 | |
| 153 | case Builtin::BI__builtin_classify_type: |
| 154 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 155 | TheCall->setType(Context.IntTy); |
| 156 | break; |
Chris Lattner | 75c29a0 | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 157 | case Builtin::BI__builtin_constant_p: |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 158 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 159 | TheCall->setType(Context.IntTy); |
Chris Lattner | 75c29a0 | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 160 | break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 161 | case Builtin::BI__sync_fetch_and_add: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 162 | case Builtin::BI__sync_fetch_and_add_1: |
| 163 | case Builtin::BI__sync_fetch_and_add_2: |
| 164 | case Builtin::BI__sync_fetch_and_add_4: |
| 165 | case Builtin::BI__sync_fetch_and_add_8: |
| 166 | case Builtin::BI__sync_fetch_and_add_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 167 | case Builtin::BI__sync_fetch_and_sub: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 168 | case Builtin::BI__sync_fetch_and_sub_1: |
| 169 | case Builtin::BI__sync_fetch_and_sub_2: |
| 170 | case Builtin::BI__sync_fetch_and_sub_4: |
| 171 | case Builtin::BI__sync_fetch_and_sub_8: |
| 172 | case Builtin::BI__sync_fetch_and_sub_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 173 | case Builtin::BI__sync_fetch_and_or: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 174 | case Builtin::BI__sync_fetch_and_or_1: |
| 175 | case Builtin::BI__sync_fetch_and_or_2: |
| 176 | case Builtin::BI__sync_fetch_and_or_4: |
| 177 | case Builtin::BI__sync_fetch_and_or_8: |
| 178 | case Builtin::BI__sync_fetch_and_or_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 179 | case Builtin::BI__sync_fetch_and_and: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 180 | case Builtin::BI__sync_fetch_and_and_1: |
| 181 | case Builtin::BI__sync_fetch_and_and_2: |
| 182 | case Builtin::BI__sync_fetch_and_and_4: |
| 183 | case Builtin::BI__sync_fetch_and_and_8: |
| 184 | case Builtin::BI__sync_fetch_and_and_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 185 | case Builtin::BI__sync_fetch_and_xor: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 186 | case Builtin::BI__sync_fetch_and_xor_1: |
| 187 | case Builtin::BI__sync_fetch_and_xor_2: |
| 188 | case Builtin::BI__sync_fetch_and_xor_4: |
| 189 | case Builtin::BI__sync_fetch_and_xor_8: |
| 190 | case Builtin::BI__sync_fetch_and_xor_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 191 | case Builtin::BI__sync_add_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 192 | case Builtin::BI__sync_add_and_fetch_1: |
| 193 | case Builtin::BI__sync_add_and_fetch_2: |
| 194 | case Builtin::BI__sync_add_and_fetch_4: |
| 195 | case Builtin::BI__sync_add_and_fetch_8: |
| 196 | case Builtin::BI__sync_add_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 197 | case Builtin::BI__sync_sub_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 198 | case Builtin::BI__sync_sub_and_fetch_1: |
| 199 | case Builtin::BI__sync_sub_and_fetch_2: |
| 200 | case Builtin::BI__sync_sub_and_fetch_4: |
| 201 | case Builtin::BI__sync_sub_and_fetch_8: |
| 202 | case Builtin::BI__sync_sub_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 203 | case Builtin::BI__sync_and_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 204 | case Builtin::BI__sync_and_and_fetch_1: |
| 205 | case Builtin::BI__sync_and_and_fetch_2: |
| 206 | case Builtin::BI__sync_and_and_fetch_4: |
| 207 | case Builtin::BI__sync_and_and_fetch_8: |
| 208 | case Builtin::BI__sync_and_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 209 | case Builtin::BI__sync_or_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 210 | case Builtin::BI__sync_or_and_fetch_1: |
| 211 | case Builtin::BI__sync_or_and_fetch_2: |
| 212 | case Builtin::BI__sync_or_and_fetch_4: |
| 213 | case Builtin::BI__sync_or_and_fetch_8: |
| 214 | case Builtin::BI__sync_or_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 215 | case Builtin::BI__sync_xor_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 216 | case Builtin::BI__sync_xor_and_fetch_1: |
| 217 | case Builtin::BI__sync_xor_and_fetch_2: |
| 218 | case Builtin::BI__sync_xor_and_fetch_4: |
| 219 | case Builtin::BI__sync_xor_and_fetch_8: |
| 220 | case Builtin::BI__sync_xor_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 221 | case Builtin::BI__sync_val_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 222 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 223 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 224 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 225 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 226 | case Builtin::BI__sync_val_compare_and_swap_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 227 | case Builtin::BI__sync_bool_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 228 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 229 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 230 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 231 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 232 | case Builtin::BI__sync_bool_compare_and_swap_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 233 | case Builtin::BI__sync_lock_test_and_set: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 234 | case Builtin::BI__sync_lock_test_and_set_1: |
| 235 | case Builtin::BI__sync_lock_test_and_set_2: |
| 236 | case Builtin::BI__sync_lock_test_and_set_4: |
| 237 | case Builtin::BI__sync_lock_test_and_set_8: |
| 238 | case Builtin::BI__sync_lock_test_and_set_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 239 | case Builtin::BI__sync_lock_release: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 240 | case Builtin::BI__sync_lock_release_1: |
| 241 | case Builtin::BI__sync_lock_release_2: |
| 242 | case Builtin::BI__sync_lock_release_4: |
| 243 | case Builtin::BI__sync_lock_release_8: |
| 244 | case Builtin::BI__sync_lock_release_16: |
Chris Lattner | 23aa9c8 | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 245 | case Builtin::BI__sync_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 246 | case Builtin::BI__sync_swap_1: |
| 247 | case Builtin::BI__sync_swap_2: |
| 248 | case Builtin::BI__sync_swap_4: |
| 249 | case Builtin::BI__sync_swap_8: |
| 250 | case Builtin::BI__sync_swap_16: |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 251 | return SemaBuiltinAtomicOverloaded(move(TheCallResult)); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 252 | case Builtin::BI__atomic_load: |
| 253 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::Load); |
| 254 | case Builtin::BI__atomic_store: |
| 255 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::Store); |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 256 | case Builtin::BI__atomic_init: |
| 257 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::Init); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 258 | case Builtin::BI__atomic_exchange: |
| 259 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::Xchg); |
| 260 | case Builtin::BI__atomic_compare_exchange_strong: |
| 261 | return SemaAtomicOpsOverloaded(move(TheCallResult), |
| 262 | AtomicExpr::CmpXchgStrong); |
| 263 | case Builtin::BI__atomic_compare_exchange_weak: |
| 264 | return SemaAtomicOpsOverloaded(move(TheCallResult), |
| 265 | AtomicExpr::CmpXchgWeak); |
| 266 | case Builtin::BI__atomic_fetch_add: |
| 267 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::Add); |
| 268 | case Builtin::BI__atomic_fetch_sub: |
| 269 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::Sub); |
| 270 | case Builtin::BI__atomic_fetch_and: |
| 271 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::And); |
| 272 | case Builtin::BI__atomic_fetch_or: |
| 273 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::Or); |
| 274 | case Builtin::BI__atomic_fetch_xor: |
| 275 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::Xor); |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 276 | case Builtin::BI__builtin_annotation: |
| 277 | if (CheckBuiltinAnnotationString(*this, TheCall->getArg(1))) |
| 278 | return ExprError(); |
| 279 | break; |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | // Since the target specific builtins for each arch overlap, only check those |
| 283 | // of the arch we are compiling for. |
| 284 | if (BuiltinID >= Builtin::FirstTSBuiltin) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 285 | switch (Context.getTargetInfo().getTriple().getArch()) { |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 286 | case llvm::Triple::arm: |
| 287 | case llvm::Triple::thumb: |
| 288 | if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall)) |
| 289 | return ExprError(); |
| 290 | break; |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 291 | default: |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | return move(TheCallResult); |
| 297 | } |
| 298 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 299 | // Get the valid immediate range for the specified NEON type code. |
| 300 | static unsigned RFT(unsigned t, bool shift = false) { |
Bob Wilson | da95f73 | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 301 | NeonTypeFlags Type(t); |
| 302 | int IsQuad = Type.isQuad(); |
| 303 | switch (Type.getEltType()) { |
| 304 | case NeonTypeFlags::Int8: |
| 305 | case NeonTypeFlags::Poly8: |
| 306 | return shift ? 7 : (8 << IsQuad) - 1; |
| 307 | case NeonTypeFlags::Int16: |
| 308 | case NeonTypeFlags::Poly16: |
| 309 | return shift ? 15 : (4 << IsQuad) - 1; |
| 310 | case NeonTypeFlags::Int32: |
| 311 | return shift ? 31 : (2 << IsQuad) - 1; |
| 312 | case NeonTypeFlags::Int64: |
| 313 | return shift ? 63 : (1 << IsQuad) - 1; |
| 314 | case NeonTypeFlags::Float16: |
| 315 | assert(!shift && "cannot shift float types!"); |
| 316 | return (4 << IsQuad) - 1; |
| 317 | case NeonTypeFlags::Float32: |
| 318 | assert(!shift && "cannot shift float types!"); |
| 319 | return (2 << IsQuad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 320 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 321 | llvm_unreachable("Invalid NeonTypeFlag!"); |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 324 | /// getNeonEltType - Return the QualType corresponding to the elements of |
| 325 | /// the vector type specified by the NeonTypeFlags. This is used to check |
| 326 | /// the pointer arguments for Neon load/store intrinsics. |
| 327 | static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context) { |
| 328 | switch (Flags.getEltType()) { |
| 329 | case NeonTypeFlags::Int8: |
| 330 | return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy; |
| 331 | case NeonTypeFlags::Int16: |
| 332 | return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy; |
| 333 | case NeonTypeFlags::Int32: |
| 334 | return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy; |
| 335 | case NeonTypeFlags::Int64: |
| 336 | return Flags.isUnsigned() ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 337 | case NeonTypeFlags::Poly8: |
| 338 | return Context.SignedCharTy; |
| 339 | case NeonTypeFlags::Poly16: |
| 340 | return Context.ShortTy; |
| 341 | case NeonTypeFlags::Float16: |
| 342 | return Context.UnsignedShortTy; |
| 343 | case NeonTypeFlags::Float32: |
| 344 | return Context.FloatTy; |
| 345 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 346 | llvm_unreachable("Invalid NeonTypeFlag!"); |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 349 | bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 350 | llvm::APSInt Result; |
| 351 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 352 | unsigned mask = 0; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 353 | unsigned TV = 0; |
Bob Wilson | 4648255 | 2011-11-16 21:32:23 +0000 | [diff] [blame] | 354 | int PtrArgNum = -1; |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 355 | bool HasConstPtr = false; |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 356 | switch (BuiltinID) { |
Nate Begeman | a23326b | 2010-06-17 04:17:01 +0000 | [diff] [blame] | 357 | #define GET_NEON_OVERLOAD_CHECK |
| 358 | #include "clang/Basic/arm_neon.inc" |
| 359 | #undef GET_NEON_OVERLOAD_CHECK |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 362 | // For NEON intrinsics which are overloaded on vector element type, validate |
| 363 | // the immediate which specifies which variant to emit. |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 364 | unsigned ImmArg = TheCall->getNumArgs()-1; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 365 | if (mask) { |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 366 | if (SemaBuiltinConstantArg(TheCall, ImmArg, Result)) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 367 | return true; |
| 368 | |
Bob Wilson | da95f73 | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 369 | TV = Result.getLimitedValue(64); |
| 370 | if ((TV > 63) || (mask & (1 << TV)) == 0) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 371 | return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code) |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 372 | << TheCall->getArg(ImmArg)->getSourceRange(); |
| 373 | } |
| 374 | |
Bob Wilson | 4648255 | 2011-11-16 21:32:23 +0000 | [diff] [blame] | 375 | if (PtrArgNum >= 0) { |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 376 | // Check that pointer arguments have the specified type. |
Bob Wilson | 4648255 | 2011-11-16 21:32:23 +0000 | [diff] [blame] | 377 | Expr *Arg = TheCall->getArg(PtrArgNum); |
| 378 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) |
| 379 | Arg = ICE->getSubExpr(); |
| 380 | ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg); |
| 381 | QualType RHSTy = RHS.get()->getType(); |
| 382 | QualType EltTy = getNeonEltType(NeonTypeFlags(TV), Context); |
| 383 | if (HasConstPtr) |
| 384 | EltTy = EltTy.withConst(); |
| 385 | QualType LHSTy = Context.getPointerType(EltTy); |
| 386 | AssignConvertType ConvTy; |
| 387 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
| 388 | if (RHS.isInvalid()) |
| 389 | return true; |
| 390 | if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), LHSTy, RHSTy, |
| 391 | RHS.get(), AA_Assigning)) |
| 392 | return true; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 393 | } |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 394 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 395 | // For NEON intrinsics which take an immediate value as part of the |
| 396 | // instruction, range check them here. |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 397 | unsigned i = 0, l = 0, u = 0; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 398 | switch (BuiltinID) { |
| 399 | default: return false; |
Nate Begeman | bb37f50 | 2010-07-29 22:48:34 +0000 | [diff] [blame] | 400 | case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break; |
| 401 | case ARM::BI__builtin_arm_usat: i = 1; u = 31; break; |
Nate Begeman | 99c40bb | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 402 | case ARM::BI__builtin_arm_vcvtr_f: |
| 403 | case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break; |
Nate Begeman | a23326b | 2010-06-17 04:17:01 +0000 | [diff] [blame] | 404 | #define GET_NEON_IMMEDIATE_CHECK |
| 405 | #include "clang/Basic/arm_neon.inc" |
| 406 | #undef GET_NEON_IMMEDIATE_CHECK |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 407 | }; |
| 408 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 409 | // Check that the immediate argument is actually a constant. |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 410 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 411 | return true; |
| 412 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 413 | // Range check against the upper/lower values for this isntruction. |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 414 | unsigned Val = Result.getZExtValue(); |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 415 | if (Val < l || Val > (u + l)) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 416 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 417 | << l << u+l << TheCall->getArg(i)->getSourceRange(); |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 418 | |
Nate Begeman | 99c40bb | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 419 | // FIXME: VFP Intrinsics should error if VFP not present. |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 420 | return false; |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 421 | } |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 422 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 423 | /// CheckFunctionCall - Check a direct function call for various correctness |
| 424 | /// and safety properties not strictly enforced by the C type system. |
| 425 | bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall) { |
| 426 | // Get the IdentifierInfo* for the called function. |
| 427 | IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
| 428 | |
| 429 | // None of the checks below are needed for functions that don't have |
| 430 | // simple names (e.g., C++ conversion functions). |
| 431 | if (!FnInfo) |
| 432 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 434 | // FIXME: This mechanism should be abstracted to be less fragile and |
| 435 | // more efficient. For example, just map function ids to custom |
| 436 | // handlers. |
| 437 | |
Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 438 | // Printf and scanf checking. |
| 439 | for (specific_attr_iterator<FormatAttr> |
| 440 | i = FDecl->specific_attr_begin<FormatAttr>(), |
| 441 | e = FDecl->specific_attr_end<FormatAttr>(); i != e ; ++i) { |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 442 | CheckFormatArguments(*i, TheCall); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 443 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | |
Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 445 | for (specific_attr_iterator<NonNullAttr> |
| 446 | i = FDecl->specific_attr_begin<NonNullAttr>(), |
| 447 | e = FDecl->specific_attr_end<NonNullAttr>(); i != e; ++i) { |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 448 | CheckNonNullArguments(*i, TheCall->getArgs(), |
| 449 | TheCall->getCallee()->getLocStart()); |
Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 450 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 451 | |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 452 | unsigned CMId = FDecl->getMemoryFunctionKind(); |
| 453 | if (CMId == 0) |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 454 | return false; |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 455 | |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 456 | // Handle memory setting and copying functions. |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 457 | if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat) |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 458 | CheckStrlcpycatArguments(TheCall, FnInfo); |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 459 | else |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 460 | CheckMemaccessArguments(TheCall, CMId, FnInfo); |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 461 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 462 | return false; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 465 | bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac, |
| 466 | Expr **Args, unsigned NumArgs) { |
| 467 | for (specific_attr_iterator<FormatAttr> |
| 468 | i = Method->specific_attr_begin<FormatAttr>(), |
| 469 | e = Method->specific_attr_end<FormatAttr>(); i != e ; ++i) { |
| 470 | |
| 471 | CheckFormatArguments(*i, Args, NumArgs, false, lbrac, |
| 472 | Method->getSourceRange()); |
| 473 | } |
| 474 | |
| 475 | // diagnose nonnull arguments. |
| 476 | for (specific_attr_iterator<NonNullAttr> |
| 477 | i = Method->specific_attr_begin<NonNullAttr>(), |
| 478 | e = Method->specific_attr_end<NonNullAttr>(); i != e; ++i) { |
| 479 | CheckNonNullArguments(*i, Args, lbrac); |
| 480 | } |
| 481 | |
| 482 | return false; |
| 483 | } |
| 484 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 485 | bool Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall) { |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 486 | const VarDecl *V = dyn_cast<VarDecl>(NDecl); |
| 487 | if (!V) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 488 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 490 | QualType Ty = V->getType(); |
| 491 | if (!Ty->isBlockPointerType()) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 492 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | |
Jean-Daniel Dupas | 43d1251 | 2012-01-25 00:55:11 +0000 | [diff] [blame] | 494 | // format string checking. |
| 495 | for (specific_attr_iterator<FormatAttr> |
| 496 | i = NDecl->specific_attr_begin<FormatAttr>(), |
| 497 | e = NDecl->specific_attr_end<FormatAttr>(); i != e ; ++i) { |
| 498 | CheckFormatArguments(*i, TheCall); |
| 499 | } |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 500 | |
| 501 | return false; |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 504 | ExprResult |
| 505 | Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult, AtomicExpr::AtomicOp Op) { |
| 506 | CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); |
| 507 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 508 | |
| 509 | // All these operations take one of the following four forms: |
| 510 | // T __atomic_load(_Atomic(T)*, int) (loads) |
| 511 | // T* __atomic_add(_Atomic(T*)*, ptrdiff_t, int) (pointer add/sub) |
| 512 | // int __atomic_compare_exchange_strong(_Atomic(T)*, T*, T, int, int) |
| 513 | // (cmpxchg) |
| 514 | // T __atomic_exchange(_Atomic(T)*, T, int) (everything else) |
| 515 | // where T is an appropriate type, and the int paremeterss are for orderings. |
| 516 | unsigned NumVals = 1; |
| 517 | unsigned NumOrders = 1; |
| 518 | if (Op == AtomicExpr::Load) { |
| 519 | NumVals = 0; |
| 520 | } else if (Op == AtomicExpr::CmpXchgWeak || Op == AtomicExpr::CmpXchgStrong) { |
| 521 | NumVals = 2; |
| 522 | NumOrders = 2; |
| 523 | } |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 524 | if (Op == AtomicExpr::Init) |
| 525 | NumOrders = 0; |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 526 | |
| 527 | if (TheCall->getNumArgs() < NumVals+NumOrders+1) { |
| 528 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 529 | << 0 << NumVals+NumOrders+1 << TheCall->getNumArgs() |
| 530 | << TheCall->getCallee()->getSourceRange(); |
| 531 | return ExprError(); |
| 532 | } else if (TheCall->getNumArgs() > NumVals+NumOrders+1) { |
| 533 | Diag(TheCall->getArg(NumVals+NumOrders+1)->getLocStart(), |
| 534 | diag::err_typecheck_call_too_many_args) |
| 535 | << 0 << NumVals+NumOrders+1 << TheCall->getNumArgs() |
| 536 | << TheCall->getCallee()->getSourceRange(); |
| 537 | return ExprError(); |
| 538 | } |
| 539 | |
| 540 | // Inspect the first argument of the atomic operation. This should always be |
| 541 | // a pointer to an _Atomic type. |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 542 | Expr *Ptr = TheCall->getArg(0); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 543 | Ptr = DefaultFunctionArrayLvalueConversion(Ptr).get(); |
| 544 | const PointerType *pointerType = Ptr->getType()->getAs<PointerType>(); |
| 545 | if (!pointerType) { |
| 546 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic) |
| 547 | << Ptr->getType() << Ptr->getSourceRange(); |
| 548 | return ExprError(); |
| 549 | } |
| 550 | |
| 551 | QualType AtomTy = pointerType->getPointeeType(); |
| 552 | if (!AtomTy->isAtomicType()) { |
| 553 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic) |
| 554 | << Ptr->getType() << Ptr->getSourceRange(); |
| 555 | return ExprError(); |
| 556 | } |
| 557 | QualType ValType = AtomTy->getAs<AtomicType>()->getValueType(); |
| 558 | |
| 559 | if ((Op == AtomicExpr::Add || Op == AtomicExpr::Sub) && |
| 560 | !ValType->isIntegerType() && !ValType->isPointerType()) { |
| 561 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) |
| 562 | << Ptr->getType() << Ptr->getSourceRange(); |
| 563 | return ExprError(); |
| 564 | } |
| 565 | |
| 566 | if (!ValType->isIntegerType() && |
| 567 | (Op == AtomicExpr::And || Op == AtomicExpr::Or || Op == AtomicExpr::Xor)){ |
| 568 | Diag(DRE->getLocStart(), diag::err_atomic_op_logical_needs_atomic_int) |
| 569 | << Ptr->getType() << Ptr->getSourceRange(); |
| 570 | return ExprError(); |
| 571 | } |
| 572 | |
| 573 | switch (ValType.getObjCLifetime()) { |
| 574 | case Qualifiers::OCL_None: |
| 575 | case Qualifiers::OCL_ExplicitNone: |
| 576 | // okay |
| 577 | break; |
| 578 | |
| 579 | case Qualifiers::OCL_Weak: |
| 580 | case Qualifiers::OCL_Strong: |
| 581 | case Qualifiers::OCL_Autoreleasing: |
| 582 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
| 583 | << ValType << Ptr->getSourceRange(); |
| 584 | return ExprError(); |
| 585 | } |
| 586 | |
| 587 | QualType ResultType = ValType; |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 588 | if (Op == AtomicExpr::Store || Op == AtomicExpr::Init) |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 589 | ResultType = Context.VoidTy; |
| 590 | else if (Op == AtomicExpr::CmpXchgWeak || Op == AtomicExpr::CmpXchgStrong) |
| 591 | ResultType = Context.BoolTy; |
| 592 | |
| 593 | // The first argument --- the pointer --- has a fixed type; we |
| 594 | // deduce the types of the rest of the arguments accordingly. Walk |
| 595 | // the remaining arguments, converting them to the deduced value type. |
| 596 | for (unsigned i = 1; i != NumVals+NumOrders+1; ++i) { |
| 597 | ExprResult Arg = TheCall->getArg(i); |
| 598 | QualType Ty; |
| 599 | if (i < NumVals+1) { |
| 600 | // The second argument to a cmpxchg is a pointer to the data which will |
| 601 | // be exchanged. The second argument to a pointer add/subtract is the |
| 602 | // amount to add/subtract, which must be a ptrdiff_t. The third |
| 603 | // argument to a cmpxchg and the second argument in all other cases |
| 604 | // is the type of the value. |
| 605 | if (i == 1 && (Op == AtomicExpr::CmpXchgWeak || |
| 606 | Op == AtomicExpr::CmpXchgStrong)) |
| 607 | Ty = Context.getPointerType(ValType.getUnqualifiedType()); |
| 608 | else if (!ValType->isIntegerType() && |
| 609 | (Op == AtomicExpr::Add || Op == AtomicExpr::Sub)) |
| 610 | Ty = Context.getPointerDiffType(); |
| 611 | else |
| 612 | Ty = ValType; |
| 613 | } else { |
| 614 | // The order(s) are always converted to int. |
| 615 | Ty = Context.IntTy; |
| 616 | } |
| 617 | InitializedEntity Entity = |
| 618 | InitializedEntity::InitializeParameter(Context, Ty, false); |
| 619 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 620 | if (Arg.isInvalid()) |
| 621 | return true; |
| 622 | TheCall->setArg(i, Arg.get()); |
| 623 | } |
| 624 | |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 625 | SmallVector<Expr*, 5> SubExprs; |
| 626 | SubExprs.push_back(Ptr); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 627 | if (Op == AtomicExpr::Load) { |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 628 | SubExprs.push_back(TheCall->getArg(1)); // Order |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 629 | } else if (Op == AtomicExpr::Init) { |
| 630 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 631 | } else if (Op != AtomicExpr::CmpXchgWeak && Op != AtomicExpr::CmpXchgStrong) { |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 632 | SubExprs.push_back(TheCall->getArg(2)); // Order |
| 633 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 634 | } else { |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 635 | SubExprs.push_back(TheCall->getArg(3)); // Order |
| 636 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
| 637 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
| 638 | SubExprs.push_back(TheCall->getArg(4)); // OrderFail |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 639 | } |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 640 | |
| 641 | return Owned(new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(), |
| 642 | SubExprs.data(), SubExprs.size(), |
| 643 | ResultType, Op, |
| 644 | TheCall->getRParenLoc())); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 648 | /// checkBuiltinArgument - Given a call to a builtin function, perform |
| 649 | /// normal type-checking on the given argument, updating the call in |
| 650 | /// place. This is useful when a builtin function requires custom |
| 651 | /// type-checking for some of its arguments but not necessarily all of |
| 652 | /// them. |
| 653 | /// |
| 654 | /// Returns true on error. |
| 655 | static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) { |
| 656 | FunctionDecl *Fn = E->getDirectCallee(); |
| 657 | assert(Fn && "builtin call without direct callee!"); |
| 658 | |
| 659 | ParmVarDecl *Param = Fn->getParamDecl(ArgIndex); |
| 660 | InitializedEntity Entity = |
| 661 | InitializedEntity::InitializeParameter(S.Context, Param); |
| 662 | |
| 663 | ExprResult Arg = E->getArg(0); |
| 664 | Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 665 | if (Arg.isInvalid()) |
| 666 | return true; |
| 667 | |
| 668 | E->setArg(ArgIndex, Arg.take()); |
| 669 | return false; |
| 670 | } |
| 671 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 672 | /// SemaBuiltinAtomicOverloaded - We have a call to a function like |
| 673 | /// __sync_fetch_and_add, which is an overloaded function based on the pointer |
| 674 | /// type of its first argument. The main ActOnCallExpr routines have already |
| 675 | /// promoted the types of arguments because all of these calls are prototyped as |
| 676 | /// void(...). |
| 677 | /// |
| 678 | /// This function goes through and does final semantic checking for these |
| 679 | /// builtins, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 680 | ExprResult |
| 681 | Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) { |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 682 | CallExpr *TheCall = (CallExpr *)TheCallResult.get(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 683 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 684 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 685 | |
| 686 | // 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] | 687 | if (TheCall->getNumArgs() < 1) { |
| 688 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 689 | << 0 << 1 << TheCall->getNumArgs() |
| 690 | << TheCall->getCallee()->getSourceRange(); |
| 691 | return ExprError(); |
| 692 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 694 | // Inspect the first argument of the atomic builtin. This should always be |
| 695 | // a pointer type, whose element is an integral scalar or pointer type. |
| 696 | // Because it is a pointer type, we don't have to worry about any implicit |
| 697 | // casts here. |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 698 | // FIXME: We don't allow floating point scalars as input. |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 699 | Expr *FirstArg = TheCall->getArg(0); |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 700 | ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg); |
| 701 | if (FirstArgResult.isInvalid()) |
| 702 | return ExprError(); |
| 703 | FirstArg = FirstArgResult.take(); |
| 704 | TheCall->setArg(0, FirstArg); |
| 705 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 706 | const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>(); |
| 707 | if (!pointerType) { |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 708 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
| 709 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 710 | return ExprError(); |
| 711 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 713 | QualType ValType = pointerType->getPointeeType(); |
Chris Lattner | dd5fa7a | 2010-09-17 21:12:38 +0000 | [diff] [blame] | 714 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 715 | !ValType->isBlockPointerType()) { |
| 716 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr) |
| 717 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 718 | return ExprError(); |
| 719 | } |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 720 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 721 | switch (ValType.getObjCLifetime()) { |
| 722 | case Qualifiers::OCL_None: |
| 723 | case Qualifiers::OCL_ExplicitNone: |
| 724 | // okay |
| 725 | break; |
| 726 | |
| 727 | case Qualifiers::OCL_Weak: |
| 728 | case Qualifiers::OCL_Strong: |
| 729 | case Qualifiers::OCL_Autoreleasing: |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 730 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 731 | << ValType << FirstArg->getSourceRange(); |
| 732 | return ExprError(); |
| 733 | } |
| 734 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 735 | // Strip any qualifiers off ValType. |
| 736 | ValType = ValType.getUnqualifiedType(); |
| 737 | |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 738 | // The majority of builtins return a value, but a few have special return |
| 739 | // types, so allow them to override appropriately below. |
| 740 | QualType ResultType = ValType; |
| 741 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 742 | // We need to figure out which concrete builtin this maps onto. For example, |
| 743 | // __sync_fetch_and_add with a 2 byte object turns into |
| 744 | // __sync_fetch_and_add_2. |
| 745 | #define BUILTIN_ROW(x) \ |
| 746 | { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ |
| 747 | Builtin::BI##x##_8, Builtin::BI##x##_16 } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 748 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 749 | static const unsigned BuiltinIndices[][5] = { |
| 750 | BUILTIN_ROW(__sync_fetch_and_add), |
| 751 | BUILTIN_ROW(__sync_fetch_and_sub), |
| 752 | BUILTIN_ROW(__sync_fetch_and_or), |
| 753 | BUILTIN_ROW(__sync_fetch_and_and), |
| 754 | BUILTIN_ROW(__sync_fetch_and_xor), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 756 | BUILTIN_ROW(__sync_add_and_fetch), |
| 757 | BUILTIN_ROW(__sync_sub_and_fetch), |
| 758 | BUILTIN_ROW(__sync_and_and_fetch), |
| 759 | BUILTIN_ROW(__sync_or_and_fetch), |
| 760 | BUILTIN_ROW(__sync_xor_and_fetch), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 761 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 762 | BUILTIN_ROW(__sync_val_compare_and_swap), |
| 763 | BUILTIN_ROW(__sync_bool_compare_and_swap), |
| 764 | BUILTIN_ROW(__sync_lock_test_and_set), |
Chris Lattner | 23aa9c8 | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 765 | BUILTIN_ROW(__sync_lock_release), |
| 766 | BUILTIN_ROW(__sync_swap) |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 767 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 768 | #undef BUILTIN_ROW |
| 769 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 770 | // Determine the index of the size. |
| 771 | unsigned SizeIndex; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 772 | switch (Context.getTypeSizeInChars(ValType).getQuantity()) { |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 773 | case 1: SizeIndex = 0; break; |
| 774 | case 2: SizeIndex = 1; break; |
| 775 | case 4: SizeIndex = 2; break; |
| 776 | case 8: SizeIndex = 3; break; |
| 777 | case 16: SizeIndex = 4; break; |
| 778 | default: |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 779 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size) |
| 780 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 781 | return ExprError(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 782 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 784 | // Each of these builtins has one pointer argument, followed by some number of |
| 785 | // values (0, 1 or 2) followed by a potentially empty varags list of stuff |
| 786 | // that we ignore. Find out which row of BuiltinIndices to read from as well |
| 787 | // as the number of fixed args. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 788 | unsigned BuiltinID = FDecl->getBuiltinID(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 789 | unsigned BuiltinIndex, NumFixed = 1; |
| 790 | switch (BuiltinID) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 791 | default: llvm_unreachable("Unknown overloaded atomic builtin!"); |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 792 | case Builtin::BI__sync_fetch_and_add: |
| 793 | case Builtin::BI__sync_fetch_and_add_1: |
| 794 | case Builtin::BI__sync_fetch_and_add_2: |
| 795 | case Builtin::BI__sync_fetch_and_add_4: |
| 796 | case Builtin::BI__sync_fetch_and_add_8: |
| 797 | case Builtin::BI__sync_fetch_and_add_16: |
| 798 | BuiltinIndex = 0; |
| 799 | break; |
| 800 | |
| 801 | case Builtin::BI__sync_fetch_and_sub: |
| 802 | case Builtin::BI__sync_fetch_and_sub_1: |
| 803 | case Builtin::BI__sync_fetch_and_sub_2: |
| 804 | case Builtin::BI__sync_fetch_and_sub_4: |
| 805 | case Builtin::BI__sync_fetch_and_sub_8: |
| 806 | case Builtin::BI__sync_fetch_and_sub_16: |
| 807 | BuiltinIndex = 1; |
| 808 | break; |
| 809 | |
| 810 | case Builtin::BI__sync_fetch_and_or: |
| 811 | case Builtin::BI__sync_fetch_and_or_1: |
| 812 | case Builtin::BI__sync_fetch_and_or_2: |
| 813 | case Builtin::BI__sync_fetch_and_or_4: |
| 814 | case Builtin::BI__sync_fetch_and_or_8: |
| 815 | case Builtin::BI__sync_fetch_and_or_16: |
| 816 | BuiltinIndex = 2; |
| 817 | break; |
| 818 | |
| 819 | case Builtin::BI__sync_fetch_and_and: |
| 820 | case Builtin::BI__sync_fetch_and_and_1: |
| 821 | case Builtin::BI__sync_fetch_and_and_2: |
| 822 | case Builtin::BI__sync_fetch_and_and_4: |
| 823 | case Builtin::BI__sync_fetch_and_and_8: |
| 824 | case Builtin::BI__sync_fetch_and_and_16: |
| 825 | BuiltinIndex = 3; |
| 826 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 828 | case Builtin::BI__sync_fetch_and_xor: |
| 829 | case Builtin::BI__sync_fetch_and_xor_1: |
| 830 | case Builtin::BI__sync_fetch_and_xor_2: |
| 831 | case Builtin::BI__sync_fetch_and_xor_4: |
| 832 | case Builtin::BI__sync_fetch_and_xor_8: |
| 833 | case Builtin::BI__sync_fetch_and_xor_16: |
| 834 | BuiltinIndex = 4; |
| 835 | break; |
| 836 | |
| 837 | case Builtin::BI__sync_add_and_fetch: |
| 838 | case Builtin::BI__sync_add_and_fetch_1: |
| 839 | case Builtin::BI__sync_add_and_fetch_2: |
| 840 | case Builtin::BI__sync_add_and_fetch_4: |
| 841 | case Builtin::BI__sync_add_and_fetch_8: |
| 842 | case Builtin::BI__sync_add_and_fetch_16: |
| 843 | BuiltinIndex = 5; |
| 844 | break; |
| 845 | |
| 846 | case Builtin::BI__sync_sub_and_fetch: |
| 847 | case Builtin::BI__sync_sub_and_fetch_1: |
| 848 | case Builtin::BI__sync_sub_and_fetch_2: |
| 849 | case Builtin::BI__sync_sub_and_fetch_4: |
| 850 | case Builtin::BI__sync_sub_and_fetch_8: |
| 851 | case Builtin::BI__sync_sub_and_fetch_16: |
| 852 | BuiltinIndex = 6; |
| 853 | break; |
| 854 | |
| 855 | case Builtin::BI__sync_and_and_fetch: |
| 856 | case Builtin::BI__sync_and_and_fetch_1: |
| 857 | case Builtin::BI__sync_and_and_fetch_2: |
| 858 | case Builtin::BI__sync_and_and_fetch_4: |
| 859 | case Builtin::BI__sync_and_and_fetch_8: |
| 860 | case Builtin::BI__sync_and_and_fetch_16: |
| 861 | BuiltinIndex = 7; |
| 862 | break; |
| 863 | |
| 864 | case Builtin::BI__sync_or_and_fetch: |
| 865 | case Builtin::BI__sync_or_and_fetch_1: |
| 866 | case Builtin::BI__sync_or_and_fetch_2: |
| 867 | case Builtin::BI__sync_or_and_fetch_4: |
| 868 | case Builtin::BI__sync_or_and_fetch_8: |
| 869 | case Builtin::BI__sync_or_and_fetch_16: |
| 870 | BuiltinIndex = 8; |
| 871 | break; |
| 872 | |
| 873 | case Builtin::BI__sync_xor_and_fetch: |
| 874 | case Builtin::BI__sync_xor_and_fetch_1: |
| 875 | case Builtin::BI__sync_xor_and_fetch_2: |
| 876 | case Builtin::BI__sync_xor_and_fetch_4: |
| 877 | case Builtin::BI__sync_xor_and_fetch_8: |
| 878 | case Builtin::BI__sync_xor_and_fetch_16: |
| 879 | BuiltinIndex = 9; |
| 880 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 881 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 882 | case Builtin::BI__sync_val_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 883 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 884 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 885 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 886 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 887 | case Builtin::BI__sync_val_compare_and_swap_16: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 888 | BuiltinIndex = 10; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 889 | NumFixed = 2; |
| 890 | break; |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 891 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 892 | case Builtin::BI__sync_bool_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 893 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 894 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 895 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 896 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 897 | case Builtin::BI__sync_bool_compare_and_swap_16: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 898 | BuiltinIndex = 11; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 899 | NumFixed = 2; |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 900 | ResultType = Context.BoolTy; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 901 | break; |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 902 | |
| 903 | case Builtin::BI__sync_lock_test_and_set: |
| 904 | case Builtin::BI__sync_lock_test_and_set_1: |
| 905 | case Builtin::BI__sync_lock_test_and_set_2: |
| 906 | case Builtin::BI__sync_lock_test_and_set_4: |
| 907 | case Builtin::BI__sync_lock_test_and_set_8: |
| 908 | case Builtin::BI__sync_lock_test_and_set_16: |
| 909 | BuiltinIndex = 12; |
| 910 | break; |
| 911 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 912 | case Builtin::BI__sync_lock_release: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 913 | case Builtin::BI__sync_lock_release_1: |
| 914 | case Builtin::BI__sync_lock_release_2: |
| 915 | case Builtin::BI__sync_lock_release_4: |
| 916 | case Builtin::BI__sync_lock_release_8: |
| 917 | case Builtin::BI__sync_lock_release_16: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 918 | BuiltinIndex = 13; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 919 | NumFixed = 0; |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 920 | ResultType = Context.VoidTy; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 921 | break; |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 922 | |
| 923 | case Builtin::BI__sync_swap: |
| 924 | case Builtin::BI__sync_swap_1: |
| 925 | case Builtin::BI__sync_swap_2: |
| 926 | case Builtin::BI__sync_swap_4: |
| 927 | case Builtin::BI__sync_swap_8: |
| 928 | case Builtin::BI__sync_swap_16: |
| 929 | BuiltinIndex = 14; |
| 930 | break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 931 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 932 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 933 | // Now that we know how many fixed arguments we expect, first check that we |
| 934 | // have at least that many. |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 935 | if (TheCall->getNumArgs() < 1+NumFixed) { |
| 936 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 937 | << 0 << 1+NumFixed << TheCall->getNumArgs() |
| 938 | << TheCall->getCallee()->getSourceRange(); |
| 939 | return ExprError(); |
| 940 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 941 | |
Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 942 | // Get the decl for the concrete builtin from this, we can tell what the |
| 943 | // concrete integer type we should convert to is. |
| 944 | unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; |
| 945 | const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID); |
| 946 | IdentifierInfo *NewBuiltinII = PP.getIdentifierInfo(NewBuiltinName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 947 | FunctionDecl *NewBuiltinDecl = |
Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 948 | cast<FunctionDecl>(LazilyCreateBuiltin(NewBuiltinII, NewBuiltinID, |
| 949 | TUScope, false, DRE->getLocStart())); |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 950 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 951 | // The first argument --- the pointer --- has a fixed type; we |
| 952 | // deduce the types of the rest of the arguments accordingly. Walk |
| 953 | // the remaining arguments, converting them to the deduced value type. |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 954 | for (unsigned i = 0; i != NumFixed; ++i) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 955 | ExprResult Arg = TheCall->getArg(i+1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 956 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 957 | // GCC does an implicit conversion to the pointer or integer ValType. This |
| 958 | // can fail in some cases (1i -> int**), check for this error case now. |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 959 | // Initialize the argument. |
| 960 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 961 | ValType, /*consume*/ false); |
| 962 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 963 | if (Arg.isInvalid()) |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 964 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 966 | // Okay, we have something that *can* be converted to the right type. Check |
| 967 | // to see if there is a potentially weird extension going on here. This can |
| 968 | // happen when you do an atomic operation on something like an char* and |
| 969 | // pass in 42. The 42 gets converted to char. This is even more strange |
| 970 | // for things like 45.123 -> char, etc. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 971 | // FIXME: Do this check. |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 972 | TheCall->setArg(i+1, Arg.take()); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 973 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 974 | |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 975 | ASTContext& Context = this->getASTContext(); |
| 976 | |
| 977 | // Create a new DeclRefExpr to refer to the new decl. |
| 978 | DeclRefExpr* NewDRE = DeclRefExpr::Create( |
| 979 | Context, |
| 980 | DRE->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 981 | SourceLocation(), |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 982 | NewBuiltinDecl, |
| 983 | DRE->getLocation(), |
| 984 | NewBuiltinDecl->getType(), |
| 985 | DRE->getValueKind()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 986 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 987 | // Set the callee in the CallExpr. |
| 988 | // FIXME: This leaks the original parens and implicit casts. |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 989 | ExprResult PromotedCall = UsualUnaryConversions(NewDRE); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 990 | if (PromotedCall.isInvalid()) |
| 991 | return ExprError(); |
| 992 | TheCall->setCallee(PromotedCall.take()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 993 | |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 994 | // Change the result type of the call to match the original value type. This |
| 995 | // is arbitrary, but the codegen for these builtins ins design to handle it |
| 996 | // gracefully. |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 997 | TheCall->setType(ResultType); |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 998 | |
| 999 | return move(TheCallResult); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 1002 | /// CheckObjCString - Checks that the argument to the builtin |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1003 | /// CFString constructor is correct |
Steve Naroff | fd94262 | 2009-04-13 20:26:29 +0000 | [diff] [blame] | 1004 | /// Note: It might also make sense to do the UTF-16 conversion here (would |
| 1005 | /// simplify the backend). |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 1006 | bool Sema::CheckObjCString(Expr *Arg) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1007 | Arg = Arg->IgnoreParenCasts(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1008 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); |
| 1009 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1010 | if (!Literal || !Literal->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1011 | Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 1012 | << Arg->getSourceRange(); |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 1013 | return true; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1014 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1015 | |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1016 | if (Literal->containsNonAsciiOrNull()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1017 | StringRef String = Literal->getString(); |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1018 | unsigned NumBytes = String.size(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1019 | SmallVector<UTF16, 128> ToBuf(NumBytes); |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1020 | const UTF8 *FromPtr = (UTF8 *)String.data(); |
| 1021 | UTF16 *ToPtr = &ToBuf[0]; |
| 1022 | |
| 1023 | ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, |
| 1024 | &ToPtr, ToPtr + NumBytes, |
| 1025 | strictConversion); |
| 1026 | // Check for conversion failure. |
| 1027 | if (Result != conversionOK) |
| 1028 | Diag(Arg->getLocStart(), |
| 1029 | diag::warn_cfstring_truncated) << Arg->getSourceRange(); |
| 1030 | } |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 1031 | return false; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1034 | /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity. |
| 1035 | /// Emit an error and return true on failure, return false on success. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1036 | bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) { |
| 1037 | Expr *Fn = TheCall->getCallee(); |
| 1038 | if (TheCall->getNumArgs() > 2) { |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 1039 | Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1040 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1041 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
| 1042 | << Fn->getSourceRange() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1043 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1044 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1045 | return true; |
| 1046 | } |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 1047 | |
| 1048 | if (TheCall->getNumArgs() < 2) { |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1049 | return Diag(TheCall->getLocEnd(), |
| 1050 | diag::err_typecheck_call_too_few_args_at_least) |
| 1051 | << 0 /*function call*/ << 2 << TheCall->getNumArgs(); |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 1054 | // Type-check the first argument normally. |
| 1055 | if (checkBuiltinArgument(*this, TheCall, 0)) |
| 1056 | return true; |
| 1057 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1058 | // Determine whether the current function is variadic or not. |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1059 | BlockScopeInfo *CurBlock = getCurBlock(); |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1060 | bool isVariadic; |
Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 1061 | if (CurBlock) |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 1062 | isVariadic = CurBlock->TheDecl->isVariadic(); |
Ted Kremenek | 9498d38 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 1063 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
| 1064 | isVariadic = FD->isVariadic(); |
| 1065 | else |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1066 | isVariadic = getCurMethodDecl()->isVariadic(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1068 | if (!isVariadic) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1069 | Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function); |
| 1070 | return true; |
| 1071 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1072 | |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1073 | // Verify that the second argument to the builtin is the last argument of the |
| 1074 | // current function or method. |
| 1075 | bool SecondArgIsLastNamedArgument = false; |
Anders Carlsson | e2c1410 | 2008-02-13 01:22:59 +0000 | [diff] [blame] | 1076 | const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 1078 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { |
| 1079 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1080 | // FIXME: This isn't correct for methods (results in bogus warning). |
| 1081 | // Get the last formal in the current function. |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 1082 | const ParmVarDecl *LastArg; |
Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 1083 | if (CurBlock) |
| 1084 | LastArg = *(CurBlock->TheDecl->param_end()-1); |
| 1085 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1086 | LastArg = *(FD->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1087 | else |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1088 | LastArg = *(getCurMethodDecl()->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1089 | SecondArgIsLastNamedArgument = PV == LastArg; |
| 1090 | } |
| 1091 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1093 | if (!SecondArgIsLastNamedArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | Diag(TheCall->getArg(1)->getLocStart(), |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1095 | diag::warn_second_parameter_of_va_start_not_last_named_argument); |
| 1096 | return false; |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 1097 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1098 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1099 | /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and |
| 1100 | /// friends. This is declared to take (...), so we have to check everything. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1101 | bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { |
| 1102 | if (TheCall->getNumArgs() < 2) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 1103 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1104 | << 0 << 2 << TheCall->getNumArgs()/*function call*/; |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1105 | if (TheCall->getNumArgs() > 2) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1106 | return Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1107 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1108 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1109 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
| 1110 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1112 | ExprResult OrigArg0 = TheCall->getArg(0); |
| 1113 | ExprResult OrigArg1 = TheCall->getArg(1); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1114 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1115 | // Do standard promotions between the two arguments, returning their common |
| 1116 | // type. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1117 | QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1118 | if (OrigArg0.isInvalid() || OrigArg1.isInvalid()) |
| 1119 | return true; |
Daniel Dunbar | 403bc2b | 2009-02-19 19:28:43 +0000 | [diff] [blame] | 1120 | |
| 1121 | // Make sure any conversions are pushed back into the call; this is |
| 1122 | // type safe since unordered compare builtins are declared as "_Bool |
| 1123 | // foo(...)". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1124 | TheCall->setArg(0, OrigArg0.get()); |
| 1125 | TheCall->setArg(1, OrigArg1.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1126 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1127 | if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent()) |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1128 | return false; |
| 1129 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1130 | // If the common type isn't a real floating type, then the arguments were |
| 1131 | // invalid for this operation. |
| 1132 | if (!Res->isRealFloatingType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1133 | return Diag(OrigArg0.get()->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1134 | diag::err_typecheck_call_invalid_ordered_compare) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1135 | << OrigArg0.get()->getType() << OrigArg1.get()->getType() |
| 1136 | << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1138 | return false; |
| 1139 | } |
| 1140 | |
Benjamin Kramer | e771a7a | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 1141 | /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like |
| 1142 | /// __builtin_isnan and friends. This is declared to take (...), so we have |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1143 | /// to check everything. We expect the last argument to be a floating point |
| 1144 | /// value. |
| 1145 | bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) { |
| 1146 | if (TheCall->getNumArgs() < NumArgs) |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1147 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1148 | << 0 << NumArgs << TheCall->getNumArgs()/*function call*/; |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1149 | if (TheCall->getNumArgs() > NumArgs) |
| 1150 | return Diag(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1151 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1152 | << 0 /*function call*/ << NumArgs << TheCall->getNumArgs() |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1153 | << SourceRange(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1154 | (*(TheCall->arg_end()-1))->getLocEnd()); |
| 1155 | |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1156 | Expr *OrigArg = TheCall->getArg(NumArgs-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1157 | |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1158 | if (OrigArg->isTypeDependent()) |
| 1159 | return false; |
| 1160 | |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1161 | // This operation requires a non-_Complex floating-point number. |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1162 | if (!OrigArg->getType()->isRealFloatingType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1163 | return Diag(OrigArg->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1164 | diag::err_typecheck_call_invalid_unary_fp) |
| 1165 | << OrigArg->getType() << OrigArg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1166 | |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1167 | // If this is an implicit conversion from float -> double, remove it. |
| 1168 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) { |
| 1169 | Expr *CastArg = Cast->getSubExpr(); |
| 1170 | if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) { |
| 1171 | assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) && |
| 1172 | "promotion from float to double is the only expected cast here"); |
| 1173 | Cast->setSubExpr(0); |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1174 | TheCall->setArg(NumArgs-1, CastArg); |
| 1175 | OrigArg = CastArg; |
| 1176 | } |
| 1177 | } |
| 1178 | |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1179 | return false; |
| 1180 | } |
| 1181 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1182 | /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. |
| 1183 | // This is declared to take (...), so we have to check everything. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1184 | ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1185 | if (TheCall->getNumArgs() < 2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1186 | return ExprError(Diag(TheCall->getLocEnd(), |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1187 | diag::err_typecheck_call_too_few_args_at_least) |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1188 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1189 | << TheCall->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1190 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1191 | // Determine which of the following types of shufflevector we're checking: |
| 1192 | // 1) unary, vector mask: (lhs, mask) |
| 1193 | // 2) binary, vector mask: (lhs, rhs, mask) |
| 1194 | // 3) binary, scalar mask: (lhs, rhs, index, ..., index) |
| 1195 | QualType resType = TheCall->getArg(0)->getType(); |
| 1196 | unsigned numElements = 0; |
| 1197 | |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1198 | if (!TheCall->getArg(0)->isTypeDependent() && |
| 1199 | !TheCall->getArg(1)->isTypeDependent()) { |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1200 | QualType LHSType = TheCall->getArg(0)->getType(); |
| 1201 | QualType RHSType = TheCall->getArg(1)->getType(); |
| 1202 | |
| 1203 | if (!LHSType->isVectorType() || !RHSType->isVectorType()) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1204 | Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1206 | TheCall->getArg(1)->getLocEnd()); |
| 1207 | return ExprError(); |
| 1208 | } |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1209 | |
| 1210 | numElements = LHSType->getAs<VectorType>()->getNumElements(); |
| 1211 | unsigned numResElements = TheCall->getNumArgs() - 2; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1212 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1213 | // Check to see if we have a call with 2 vector arguments, the unary shuffle |
| 1214 | // with mask. If so, verify that RHS is an integer vector type with the |
| 1215 | // same number of elts as lhs. |
| 1216 | if (TheCall->getNumArgs() == 2) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 1217 | if (!RHSType->hasIntegerRepresentation() || |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1218 | RHSType->getAs<VectorType>()->getNumElements() != numElements) |
| 1219 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
| 1220 | << SourceRange(TheCall->getArg(1)->getLocStart(), |
| 1221 | TheCall->getArg(1)->getLocEnd()); |
| 1222 | numResElements = numElements; |
| 1223 | } |
| 1224 | else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1225 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1226 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1227 | TheCall->getArg(1)->getLocEnd()); |
| 1228 | return ExprError(); |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1229 | } else if (numElements != numResElements) { |
| 1230 | QualType eltType = LHSType->getAs<VectorType>()->getElementType(); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1231 | resType = Context.getVectorType(eltType, numResElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1232 | VectorType::GenericVector); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1233 | } |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1237 | if (TheCall->getArg(i)->isTypeDependent() || |
| 1238 | TheCall->getArg(i)->isValueDependent()) |
| 1239 | continue; |
| 1240 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1241 | llvm::APSInt Result(32); |
| 1242 | if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) |
| 1243 | return ExprError(Diag(TheCall->getLocStart(), |
| 1244 | diag::err_shufflevector_nonconstant_argument) |
| 1245 | << TheCall->getArg(i)->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1246 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 1247 | if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1248 | return ExprError(Diag(TheCall->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1249 | diag::err_shufflevector_argument_too_large) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1250 | << TheCall->getArg(i)->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1253 | SmallVector<Expr*, 32> exprs; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1254 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 1255 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1256 | exprs.push_back(TheCall->getArg(i)); |
| 1257 | TheCall->setArg(i, 0); |
| 1258 | } |
| 1259 | |
Nate Begeman | a88dc30 | 2009-08-12 02:10:25 +0000 | [diff] [blame] | 1260 | return Owned(new (Context) ShuffleVectorExpr(Context, exprs.begin(), |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1261 | exprs.size(), resType, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1262 | TheCall->getCallee()->getLocStart(), |
| 1263 | TheCall->getRParenLoc())); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1264 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1265 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1266 | /// SemaBuiltinPrefetch - Handle __builtin_prefetch. |
| 1267 | // This is declared to take (const void*, ...) and can take two |
| 1268 | // optional constant int args. |
| 1269 | bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1270 | unsigned NumArgs = TheCall->getNumArgs(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1271 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1272 | if (NumArgs > 3) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1273 | return Diag(TheCall->getLocEnd(), |
| 1274 | diag::err_typecheck_call_too_many_args_at_most) |
| 1275 | << 0 /*function call*/ << 3 << NumArgs |
| 1276 | << TheCall->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1277 | |
| 1278 | // Argument 0 is checked for us and the remaining arguments must be |
| 1279 | // constant integers. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1280 | for (unsigned i = 1; i != NumArgs; ++i) { |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1281 | Expr *Arg = TheCall->getArg(i); |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1282 | |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1283 | llvm::APSInt Result; |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1284 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 1285 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1286 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1287 | // FIXME: gcc issues a warning and rewrites these to 0. These |
| 1288 | // seems especially odd for the third argument since the default |
| 1289 | // is 3. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1290 | if (i == 1) { |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1291 | if (Result.getLimitedValue() > 1) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1292 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1293 | << "0" << "1" << Arg->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1294 | } else { |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1295 | if (Result.getLimitedValue() > 3) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1296 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1297 | << "0" << "3" << Arg->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1298 | } |
| 1299 | } |
| 1300 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1301 | return false; |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1304 | /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr |
| 1305 | /// TheCall is a constant expression. |
| 1306 | bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum, |
| 1307 | llvm::APSInt &Result) { |
| 1308 | Expr *Arg = TheCall->getArg(ArgNum); |
| 1309 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 1310 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 1311 | |
| 1312 | if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; |
| 1313 | |
| 1314 | if (!Arg->isIntegerConstantExpr(Result, Context)) |
| 1315 | return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type) |
Eric Christopher | 5e89655 | 2010-04-19 18:23:02 +0000 | [diff] [blame] | 1316 | << FDecl->getDeclName() << Arg->getSourceRange(); |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1317 | |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1318 | return false; |
| 1319 | } |
| 1320 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1321 | /// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr, |
| 1322 | /// int type). This simply type checks that type is one of the defined |
| 1323 | /// constants (0-3). |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1324 | // For compatibility check 0-3, llvm only handles 0 and 2. |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1325 | bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) { |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1326 | llvm::APSInt Result; |
| 1327 | |
| 1328 | // Check constant-ness first. |
| 1329 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 1330 | return true; |
| 1331 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1332 | Expr *Arg = TheCall->getArg(1); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1333 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1334 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 1335 | << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | return false; |
| 1339 | } |
| 1340 | |
Eli Friedman | 586d6a8 | 2009-05-03 06:04:26 +0000 | [diff] [blame] | 1341 | /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 1342 | /// This checks that val is a constant 1. |
| 1343 | bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { |
| 1344 | Expr *Arg = TheCall->getArg(1); |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1345 | llvm::APSInt Result; |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1346 | |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1347 | // TODO: This is less than ideal. Overload this to take a value. |
| 1348 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 1349 | return true; |
| 1350 | |
| 1351 | if (Result != 1) |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 1352 | return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val) |
| 1353 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
| 1354 | |
| 1355 | return false; |
| 1356 | } |
| 1357 | |
Ted Kremenek | b43e8ad | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 1358 | // Handle i > 1 ? "x" : "y", recursively. |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1359 | bool Sema::SemaCheckStringLiteral(const Expr *E, Expr **Args, |
| 1360 | unsigned NumArgs, bool HasVAListArg, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1361 | unsigned format_idx, unsigned firstDataArg, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1362 | FormatStringType Type, bool inFunctionCall) { |
Ted Kremenek | 4fe6441 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 1363 | tryAgain: |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1364 | if (E->isTypeDependent() || E->isValueDependent()) |
| 1365 | return false; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1366 | |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1367 | E = E->IgnoreParenCasts(); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1368 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1369 | switch (E->getStmtClass()) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1370 | case Stmt::BinaryConditionalOperatorClass: |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1371 | case Stmt::ConditionalOperatorClass: { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1372 | const AbstractConditionalOperator *C = cast<AbstractConditionalOperator>(E); |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1373 | return SemaCheckStringLiteral(C->getTrueExpr(), Args, NumArgs, HasVAListArg, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1374 | format_idx, firstDataArg, Type, |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1375 | inFunctionCall) |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1376 | && SemaCheckStringLiteral(C->getFalseExpr(), Args, NumArgs, HasVAListArg, |
| 1377 | format_idx, firstDataArg, Type, |
| 1378 | inFunctionCall); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
Ted Kremenek | 95355bb | 2010-09-09 03:51:42 +0000 | [diff] [blame] | 1381 | case Stmt::IntegerLiteralClass: |
| 1382 | // Technically -Wformat-nonliteral does not warn about this case. |
| 1383 | // The behavior of printf and friends in this case is implementation |
| 1384 | // dependent. Ideally if the format string cannot be null then |
| 1385 | // it should have a 'nonnull' attribute in the function prototype. |
| 1386 | return true; |
| 1387 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1388 | case Stmt::ImplicitCastExprClass: { |
Ted Kremenek | 4fe6441 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 1389 | E = cast<ImplicitCastExpr>(E)->getSubExpr(); |
| 1390 | goto tryAgain; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1393 | case Stmt::OpaqueValueExprClass: |
| 1394 | if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) { |
| 1395 | E = src; |
| 1396 | goto tryAgain; |
| 1397 | } |
| 1398 | return false; |
| 1399 | |
Ted Kremenek | b43e8ad | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 1400 | case Stmt::PredefinedExprClass: |
| 1401 | // While __func__, etc., are technically not string literals, they |
| 1402 | // cannot contain format specifiers and thus are not a security |
| 1403 | // liability. |
| 1404 | return true; |
| 1405 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1406 | case Stmt::DeclRefExprClass: { |
| 1407 | const DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1408 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1409 | // As an exception, do not flag errors for variables binding to |
| 1410 | // const string literals. |
| 1411 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 1412 | bool isConstant = false; |
| 1413 | QualType T = DR->getType(); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1414 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1415 | if (const ArrayType *AT = Context.getAsArrayType(T)) { |
| 1416 | isConstant = AT->getElementType().isConstant(Context); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1417 | } else if (const PointerType *PT = T->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | isConstant = T.isConstant(Context) && |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1419 | PT->getPointeeType().isConstant(Context); |
Jean-Daniel Dupas | e98e5b5 | 2012-01-25 10:35:33 +0000 | [diff] [blame] | 1420 | } else if (T->isObjCObjectPointerType()) { |
| 1421 | // In ObjC, there is usually no "const ObjectPointer" type, |
| 1422 | // so don't check if the pointee type is constant. |
| 1423 | isConstant = T.isConstant(Context); |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1424 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1425 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1426 | if (isConstant) { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1427 | if (const Expr *Init = VD->getAnyInitializer()) |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1428 | return SemaCheckStringLiteral(Init, Args, NumArgs, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1429 | HasVAListArg, format_idx, firstDataArg, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1430 | Type, /*inFunctionCall*/false); |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1431 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | |
Anders Carlsson | d966a55 | 2009-06-28 19:55:58 +0000 | [diff] [blame] | 1433 | // For vprintf* functions (i.e., HasVAListArg==true), we add a |
| 1434 | // special check to see if the format string is a function parameter |
| 1435 | // of the function calling the printf function. If the function |
| 1436 | // has an attribute indicating it is a printf-like function, then we |
| 1437 | // should suppress warnings concerning non-literals being used in a call |
| 1438 | // to a vprintf function. For example: |
| 1439 | // |
| 1440 | // void |
| 1441 | // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){ |
| 1442 | // va_list ap; |
| 1443 | // va_start(ap, fmt); |
| 1444 | // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". |
| 1445 | // ... |
| 1446 | // |
| 1447 | // |
| 1448 | // FIXME: We don't have full attribute support yet, so just check to see |
| 1449 | // if the argument is a DeclRefExpr that references a parameter. We'll |
| 1450 | // add proper support for checking the attribute later. |
| 1451 | if (HasVAListArg) |
| 1452 | if (isa<ParmVarDecl>(VD)) |
| 1453 | return true; |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1454 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1455 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1456 | return false; |
| 1457 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1458 | |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1459 | case Stmt::CallExprClass: { |
| 1460 | const CallExpr *CE = cast<CallExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | if (const ImplicitCastExpr *ICE |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1462 | = dyn_cast<ImplicitCastExpr>(CE->getCallee())) { |
| 1463 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) { |
| 1464 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1465 | if (const FormatArgAttr *FA = FD->getAttr<FormatArgAttr>()) { |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1466 | unsigned ArgIndex = FA->getFormatIdx(); |
| 1467 | const Expr *Arg = CE->getArg(ArgIndex - 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1469 | return SemaCheckStringLiteral(Arg, Args, NumArgs, HasVAListArg, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1470 | format_idx, firstDataArg, Type, |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1471 | inFunctionCall); |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1472 | } |
| 1473 | } |
| 1474 | } |
| 1475 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1477 | return false; |
| 1478 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1479 | case Stmt::ObjCStringLiteralClass: |
| 1480 | case Stmt::StringLiteralClass: { |
| 1481 | const StringLiteral *StrE = NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1483 | if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1484 | StrE = ObjCFExpr->getString(); |
| 1485 | else |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1486 | StrE = cast<StringLiteral>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1488 | if (StrE) { |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1489 | CheckFormatString(StrE, E, Args, NumArgs, HasVAListArg, format_idx, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1490 | firstDataArg, Type, inFunctionCall); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1491 | return true; |
| 1492 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1493 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1494 | return false; |
| 1495 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1496 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1497 | default: |
| 1498 | return false; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1499 | } |
| 1500 | } |
| 1501 | |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1502 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | Sema::CheckNonNullArguments(const NonNullAttr *NonNull, |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1504 | const Expr * const *ExprArgs, |
| 1505 | SourceLocation CallSiteLoc) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1506 | for (NonNullAttr::args_iterator i = NonNull->args_begin(), |
| 1507 | e = NonNull->args_end(); |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1508 | i != e; ++i) { |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1509 | const Expr *ArgExpr = ExprArgs[*i]; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1510 | if (ArgExpr->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1511 | Expr::NPC_ValueDependentIsNotNull)) |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1512 | Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1513 | } |
| 1514 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1515 | |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1516 | Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) { |
| 1517 | return llvm::StringSwitch<FormatStringType>(Format->getType()) |
| 1518 | .Case("scanf", FST_Scanf) |
| 1519 | .Cases("printf", "printf0", FST_Printf) |
| 1520 | .Cases("NSString", "CFString", FST_NSString) |
| 1521 | .Case("strftime", FST_Strftime) |
| 1522 | .Case("strfmon", FST_Strfmon) |
| 1523 | .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf) |
| 1524 | .Default(FST_Unknown); |
| 1525 | } |
| 1526 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1527 | /// CheckPrintfScanfArguments - Check calls to printf and scanf (and similar |
| 1528 | /// functions) for correct use of format strings. |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1529 | void Sema::CheckFormatArguments(const FormatAttr *Format, CallExpr *TheCall) { |
| 1530 | bool IsCXXMember = false; |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1531 | // The way the format attribute works in GCC, the implicit this argument |
| 1532 | // of member functions is counted. However, it doesn't appear in our own |
| 1533 | // lists, so decrement format_idx in that case. |
| 1534 | if (isa<CXXMemberCallExpr>(TheCall)) { |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1535 | const CXXMethodDecl *method_decl = |
| 1536 | dyn_cast<CXXMethodDecl>(TheCall->getCalleeDecl()); |
| 1537 | IsCXXMember = method_decl && method_decl->isInstance(); |
| 1538 | } |
| 1539 | CheckFormatArguments(Format, TheCall->getArgs(), TheCall->getNumArgs(), |
| 1540 | IsCXXMember, TheCall->getRParenLoc(), |
| 1541 | TheCall->getCallee()->getSourceRange()); |
| 1542 | } |
| 1543 | |
| 1544 | void Sema::CheckFormatArguments(const FormatAttr *Format, Expr **Args, |
| 1545 | unsigned NumArgs, bool IsCXXMember, |
| 1546 | SourceLocation Loc, SourceRange Range) { |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1547 | bool HasVAListArg = Format->getFirstArg() == 0; |
| 1548 | unsigned format_idx = Format->getFormatIdx() - 1; |
| 1549 | unsigned firstDataArg = HasVAListArg ? 0 : Format->getFirstArg() - 1; |
| 1550 | if (IsCXXMember) { |
| 1551 | if (format_idx == 0) |
| 1552 | return; |
| 1553 | --format_idx; |
| 1554 | if(firstDataArg != 0) |
| 1555 | --firstDataArg; |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1556 | } |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1557 | CheckFormatArguments(Args, NumArgs, HasVAListArg, format_idx, |
| 1558 | firstDataArg, GetFormatStringType(Format), Loc, Range); |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1559 | } |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1560 | |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1561 | void Sema::CheckFormatArguments(Expr **Args, unsigned NumArgs, |
| 1562 | bool HasVAListArg, unsigned format_idx, |
| 1563 | unsigned firstDataArg, FormatStringType Type, |
| 1564 | SourceLocation Loc, SourceRange Range) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1565 | // CHECK: printf/scanf-like function is called with no format string. |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1566 | if (format_idx >= NumArgs) { |
| 1567 | Diag(Loc, diag::warn_missing_format_string) << Range; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1568 | return; |
| 1569 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1571 | const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1572 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1573 | // CHECK: format string is not a string literal. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1574 | // |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1575 | // Dynamically generated format strings are difficult to |
| 1576 | // automatically vet at compile time. Requiring that format strings |
| 1577 | // are string literals: (1) permits the checking of format strings by |
| 1578 | // the compiler and thereby (2) can practically remove the source of |
| 1579 | // many format string exploits. |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1580 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1581 | // Format string can be either ObjC string (e.g. @"%d") or |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1582 | // C string (e.g. "%d") |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1583 | // 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] | 1584 | // the same format string checking logic for both ObjC and C strings. |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1585 | if (SemaCheckStringLiteral(OrigFormatExpr, Args, NumArgs, HasVAListArg, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1586 | format_idx, firstDataArg, Type)) |
Chris Lattner | 1cd3e1f | 2009-04-29 04:49:34 +0000 | [diff] [blame] | 1587 | return; // Literal format string found, check done! |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1588 | |
Jean-Daniel Dupas | ce3aa39 | 2012-01-30 19:46:17 +0000 | [diff] [blame^] | 1589 | // Do not emit diag when the string param is a macro expansion and the |
| 1590 | // format is either NSString or CFString. This is a hack to prevent |
| 1591 | // diag when using the NSLocalizedString and CFCopyLocalizedString macros |
| 1592 | // which are usually used in place of NS and CF string literals. |
| 1593 | if (Type == FST_NSString && Args[format_idx]->getLocStart().isMacroID()) |
| 1594 | return; |
| 1595 | |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1596 | // If there are no arguments specified, warn with -Wformat-security, otherwise |
| 1597 | // warn only with -Wformat-nonliteral. |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1598 | if (NumArgs == format_idx+1) |
| 1599 | Diag(Args[format_idx]->getLocStart(), |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1600 | diag::warn_format_nonliteral_noargs) |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1601 | << OrigFormatExpr->getSourceRange(); |
| 1602 | else |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1603 | Diag(Args[format_idx]->getLocStart(), |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1604 | diag::warn_format_nonliteral) |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1605 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1606 | } |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1607 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1608 | namespace { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1609 | class CheckFormatHandler : public analyze_format_string::FormatStringHandler { |
| 1610 | protected: |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1611 | Sema &S; |
| 1612 | const StringLiteral *FExpr; |
| 1613 | const Expr *OrigFormatExpr; |
Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1614 | const unsigned FirstDataArg; |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1615 | const unsigned NumDataArgs; |
| 1616 | const bool IsObjCLiteral; |
| 1617 | const char *Beg; // Start of format string. |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1618 | const bool HasVAListArg; |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1619 | const Expr * const *Args; |
| 1620 | const unsigned NumArgs; |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1621 | unsigned FormatIdx; |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1622 | llvm::BitVector CoveredArgs; |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1623 | bool usesPositionalArgs; |
| 1624 | bool atFirstArg; |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1625 | bool inFunctionCall; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1626 | public: |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1627 | CheckFormatHandler(Sema &s, const StringLiteral *fexpr, |
Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1628 | const Expr *origFormatExpr, unsigned firstDataArg, |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1629 | unsigned numDataArgs, bool isObjCLiteral, |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1630 | const char *beg, bool hasVAListArg, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1631 | Expr **args, unsigned numArgs, |
| 1632 | unsigned formatIdx, bool inFunctionCall) |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1633 | : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), |
Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1634 | FirstDataArg(firstDataArg), |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1635 | NumDataArgs(numDataArgs), |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1636 | IsObjCLiteral(isObjCLiteral), Beg(beg), |
| 1637 | HasVAListArg(hasVAListArg), |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1638 | Args(args), NumArgs(numArgs), FormatIdx(formatIdx), |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1639 | usesPositionalArgs(false), atFirstArg(true), |
| 1640 | inFunctionCall(inFunctionCall) { |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1641 | CoveredArgs.resize(numDataArgs); |
| 1642 | CoveredArgs.reset(); |
| 1643 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1644 | |
Ted Kremenek | 07d161f | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 1645 | void DoneProcessing(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1646 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1647 | void HandleIncompleteSpecifier(const char *startSpecifier, |
| 1648 | unsigned specifierLen); |
| 1649 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1650 | virtual void HandleInvalidPosition(const char *startSpecifier, |
| 1651 | unsigned specifierLen, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1652 | analyze_format_string::PositionContext p); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1653 | |
| 1654 | virtual void HandleZeroPosition(const char *startPos, unsigned posLen); |
| 1655 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1656 | void HandleNullChar(const char *nullCharacter); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1657 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1658 | template <typename Range> |
| 1659 | static void EmitFormatDiagnostic(Sema &S, bool inFunctionCall, |
| 1660 | const Expr *ArgumentExpr, |
| 1661 | PartialDiagnostic PDiag, |
| 1662 | SourceLocation StringLoc, |
| 1663 | bool IsStringLocation, Range StringRange, |
| 1664 | FixItHint Fixit = FixItHint()); |
| 1665 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1666 | protected: |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1667 | bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, |
| 1668 | const char *startSpec, |
| 1669 | unsigned specifierLen, |
| 1670 | const char *csStart, unsigned csLen); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1671 | |
| 1672 | void HandlePositionalNonpositionalArgs(SourceLocation Loc, |
| 1673 | const char *startSpec, |
| 1674 | unsigned specifierLen); |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1675 | |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 1676 | SourceRange getFormatStringRange(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1677 | CharSourceRange getSpecifierRange(const char *startSpecifier, |
| 1678 | unsigned specifierLen); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1679 | SourceLocation getLocationOfByte(const char *x); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1680 | |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1681 | const Expr *getDataArg(unsigned i) const; |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 1682 | |
| 1683 | bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, |
| 1684 | const analyze_format_string::ConversionSpecifier &CS, |
| 1685 | const char *startSpecifier, unsigned specifierLen, |
| 1686 | unsigned argIndex); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1687 | |
| 1688 | template <typename Range> |
| 1689 | void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc, |
| 1690 | bool IsStringLocation, Range StringRange, |
| 1691 | FixItHint Fixit = FixItHint()); |
| 1692 | |
| 1693 | void CheckPositionalAndNonpositionalArgs( |
| 1694 | const analyze_format_string::FormatSpecifier *FS); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1695 | }; |
| 1696 | } |
| 1697 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1698 | SourceRange CheckFormatHandler::getFormatStringRange() { |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1699 | return OrigFormatExpr->getSourceRange(); |
| 1700 | } |
| 1701 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1702 | CharSourceRange CheckFormatHandler:: |
| 1703 | getSpecifierRange(const char *startSpecifier, unsigned specifierLen) { |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 1704 | SourceLocation Start = getLocationOfByte(startSpecifier); |
| 1705 | SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1); |
| 1706 | |
| 1707 | // Advance the end SourceLocation by one due to half-open ranges. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1708 | End = End.getLocWithOffset(1); |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 1709 | |
| 1710 | return CharSourceRange::getCharRange(Start, End); |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1713 | SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1714 | return S.getLocationOfStringLiteralByte(FExpr, x - Beg); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1717 | void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, |
| 1718 | unsigned specifierLen){ |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1719 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier), |
| 1720 | getLocationOfByte(startSpecifier), |
| 1721 | /*IsStringLocation*/true, |
| 1722 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1725 | void |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1726 | CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen, |
| 1727 | analyze_format_string::PositionContext p) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1728 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier) |
| 1729 | << (unsigned) p, |
| 1730 | getLocationOfByte(startPos), /*IsStringLocation*/true, |
| 1731 | getSpecifierRange(startPos, posLen)); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1734 | void CheckFormatHandler::HandleZeroPosition(const char *startPos, |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1735 | unsigned posLen) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1736 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier), |
| 1737 | getLocationOfByte(startPos), |
| 1738 | /*IsStringLocation*/true, |
| 1739 | getSpecifierRange(startPos, posLen)); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1742 | void CheckFormatHandler::HandleNullChar(const char *nullCharacter) { |
Ted Kremenek | 0c06944 | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 1743 | if (!IsObjCLiteral) { |
| 1744 | // The presence of a null character is likely an error. |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1745 | EmitFormatDiagnostic( |
| 1746 | S.PDiag(diag::warn_printf_format_string_contains_null_char), |
| 1747 | getLocationOfByte(nullCharacter), /*IsStringLocation*/true, |
| 1748 | getFormatStringRange()); |
Ted Kremenek | 0c06944 | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 1749 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1750 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1751 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1752 | const Expr *CheckFormatHandler::getDataArg(unsigned i) const { |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1753 | return Args[FirstDataArg + i]; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | void CheckFormatHandler::DoneProcessing() { |
| 1757 | // Does the number of data arguments exceed the number of |
| 1758 | // format conversions in the format string? |
| 1759 | if (!HasVAListArg) { |
| 1760 | // Find any arguments that weren't covered. |
| 1761 | CoveredArgs.flip(); |
| 1762 | signed notCoveredArg = CoveredArgs.find_first(); |
| 1763 | if (notCoveredArg >= 0) { |
| 1764 | assert((unsigned)notCoveredArg < NumDataArgs); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1765 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_data_arg_not_used), |
| 1766 | getDataArg((unsigned) notCoveredArg)->getLocStart(), |
| 1767 | /*IsStringLocation*/false, getFormatStringRange()); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1768 | } |
| 1769 | } |
| 1770 | } |
| 1771 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1772 | bool |
| 1773 | CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, |
| 1774 | SourceLocation Loc, |
| 1775 | const char *startSpec, |
| 1776 | unsigned specifierLen, |
| 1777 | const char *csStart, |
| 1778 | unsigned csLen) { |
| 1779 | |
| 1780 | bool keepGoing = true; |
| 1781 | if (argIndex < NumDataArgs) { |
| 1782 | // Consider the argument coverered, even though the specifier doesn't |
| 1783 | // make sense. |
| 1784 | CoveredArgs.set(argIndex); |
| 1785 | } |
| 1786 | else { |
| 1787 | // If argIndex exceeds the number of data arguments we |
| 1788 | // don't issue a warning because that is just a cascade of warnings (and |
| 1789 | // they may have intended '%%' anyway). We don't want to continue processing |
| 1790 | // the format string after this point, however, as we will like just get |
| 1791 | // gibberish when trying to match arguments. |
| 1792 | keepGoing = false; |
| 1793 | } |
| 1794 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1795 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_conversion) |
| 1796 | << StringRef(csStart, csLen), |
| 1797 | Loc, /*IsStringLocation*/true, |
| 1798 | getSpecifierRange(startSpec, specifierLen)); |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1799 | |
| 1800 | return keepGoing; |
| 1801 | } |
| 1802 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1803 | void |
| 1804 | CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc, |
| 1805 | const char *startSpec, |
| 1806 | unsigned specifierLen) { |
| 1807 | EmitFormatDiagnostic( |
| 1808 | S.PDiag(diag::warn_format_mix_positional_nonpositional_args), |
| 1809 | Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen)); |
| 1810 | } |
| 1811 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 1812 | bool |
| 1813 | CheckFormatHandler::CheckNumArgs( |
| 1814 | const analyze_format_string::FormatSpecifier &FS, |
| 1815 | const analyze_format_string::ConversionSpecifier &CS, |
| 1816 | const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { |
| 1817 | |
| 1818 | if (argIndex >= NumDataArgs) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1819 | PartialDiagnostic PDiag = FS.usesPositionalArg() |
| 1820 | ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args) |
| 1821 | << (argIndex+1) << NumDataArgs) |
| 1822 | : S.PDiag(diag::warn_printf_insufficient_data_args); |
| 1823 | EmitFormatDiagnostic( |
| 1824 | PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true, |
| 1825 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 1826 | return false; |
| 1827 | } |
| 1828 | return true; |
| 1829 | } |
| 1830 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1831 | template<typename Range> |
| 1832 | void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag, |
| 1833 | SourceLocation Loc, |
| 1834 | bool IsStringLocation, |
| 1835 | Range StringRange, |
| 1836 | FixItHint FixIt) { |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1837 | EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag, |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1838 | Loc, IsStringLocation, StringRange, FixIt); |
| 1839 | } |
| 1840 | |
| 1841 | /// \brief If the format string is not within the funcion call, emit a note |
| 1842 | /// so that the function call and string are in diagnostic messages. |
| 1843 | /// |
| 1844 | /// \param inFunctionCall if true, the format string is within the function |
| 1845 | /// call and only one diagnostic message will be produced. Otherwise, an |
| 1846 | /// extra note will be emitted pointing to location of the format string. |
| 1847 | /// |
| 1848 | /// \param ArgumentExpr the expression that is passed as the format string |
| 1849 | /// argument in the function call. Used for getting locations when two |
| 1850 | /// diagnostics are emitted. |
| 1851 | /// |
| 1852 | /// \param PDiag the callee should already have provided any strings for the |
| 1853 | /// diagnostic message. This function only adds locations and fixits |
| 1854 | /// to diagnostics. |
| 1855 | /// |
| 1856 | /// \param Loc primary location for diagnostic. If two diagnostics are |
| 1857 | /// required, one will be at Loc and a new SourceLocation will be created for |
| 1858 | /// the other one. |
| 1859 | /// |
| 1860 | /// \param IsStringLocation if true, Loc points to the format string should be |
| 1861 | /// used for the note. Otherwise, Loc points to the argument list and will |
| 1862 | /// be used with PDiag. |
| 1863 | /// |
| 1864 | /// \param StringRange some or all of the string to highlight. This is |
| 1865 | /// templated so it can accept either a CharSourceRange or a SourceRange. |
| 1866 | /// |
| 1867 | /// \param Fixit optional fix it hint for the format string. |
| 1868 | template<typename Range> |
| 1869 | void CheckFormatHandler::EmitFormatDiagnostic(Sema &S, bool InFunctionCall, |
| 1870 | const Expr *ArgumentExpr, |
| 1871 | PartialDiagnostic PDiag, |
| 1872 | SourceLocation Loc, |
| 1873 | bool IsStringLocation, |
| 1874 | Range StringRange, |
| 1875 | FixItHint FixIt) { |
| 1876 | if (InFunctionCall) |
| 1877 | S.Diag(Loc, PDiag) << StringRange << FixIt; |
| 1878 | else { |
| 1879 | S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag) |
| 1880 | << ArgumentExpr->getSourceRange(); |
| 1881 | S.Diag(IsStringLocation ? Loc : StringRange.getBegin(), |
| 1882 | diag::note_format_string_defined) |
| 1883 | << StringRange << FixIt; |
| 1884 | } |
| 1885 | } |
| 1886 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1887 | //===--- CHECK: Printf format string checking ------------------------------===// |
| 1888 | |
| 1889 | namespace { |
| 1890 | class CheckPrintfHandler : public CheckFormatHandler { |
| 1891 | public: |
| 1892 | CheckPrintfHandler(Sema &s, const StringLiteral *fexpr, |
| 1893 | const Expr *origFormatExpr, unsigned firstDataArg, |
| 1894 | unsigned numDataArgs, bool isObjCLiteral, |
| 1895 | const char *beg, bool hasVAListArg, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1896 | Expr **Args, unsigned NumArgs, |
| 1897 | unsigned formatIdx, bool inFunctionCall) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1898 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
| 1899 | numDataArgs, isObjCLiteral, beg, hasVAListArg, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1900 | Args, NumArgs, formatIdx, inFunctionCall) {} |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1901 | |
| 1902 | |
| 1903 | bool HandleInvalidPrintfConversionSpecifier( |
| 1904 | const analyze_printf::PrintfSpecifier &FS, |
| 1905 | const char *startSpecifier, |
| 1906 | unsigned specifierLen); |
| 1907 | |
| 1908 | bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, |
| 1909 | const char *startSpecifier, |
| 1910 | unsigned specifierLen); |
| 1911 | |
| 1912 | bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, |
| 1913 | const char *startSpecifier, unsigned specifierLen); |
| 1914 | void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, |
| 1915 | const analyze_printf::OptionalAmount &Amt, |
| 1916 | unsigned type, |
| 1917 | const char *startSpecifier, unsigned specifierLen); |
| 1918 | void HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
| 1919 | const analyze_printf::OptionalFlag &flag, |
| 1920 | const char *startSpecifier, unsigned specifierLen); |
| 1921 | void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, |
| 1922 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 1923 | const analyze_printf::OptionalFlag &flag, |
| 1924 | const char *startSpecifier, unsigned specifierLen); |
| 1925 | }; |
| 1926 | } |
| 1927 | |
| 1928 | bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( |
| 1929 | const analyze_printf::PrintfSpecifier &FS, |
| 1930 | const char *startSpecifier, |
| 1931 | unsigned specifierLen) { |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1932 | const analyze_printf::PrintfConversionSpecifier &CS = |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1933 | FS.getConversionSpecifier(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1934 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1935 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 1936 | getLocationOfByte(CS.getStart()), |
| 1937 | startSpecifier, specifierLen, |
| 1938 | CS.getStart(), CS.getLength()); |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1941 | bool CheckPrintfHandler::HandleAmount( |
| 1942 | const analyze_format_string::OptionalAmount &Amt, |
| 1943 | unsigned k, const char *startSpecifier, |
| 1944 | unsigned specifierLen) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1945 | |
| 1946 | if (Amt.hasDataArgument()) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1947 | if (!HasVAListArg) { |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1948 | unsigned argIndex = Amt.getArgIndex(); |
| 1949 | if (argIndex >= NumDataArgs) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1950 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg) |
| 1951 | << k, |
| 1952 | getLocationOfByte(Amt.getStart()), |
| 1953 | /*IsStringLocation*/true, |
| 1954 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1955 | // Don't do any more checking. We will just emit |
| 1956 | // spurious errors. |
| 1957 | return false; |
| 1958 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1959 | |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1960 | // Type check the data argument. It should be an 'int'. |
Ted Kremenek | 31f8e32 | 2010-01-29 23:32:22 +0000 | [diff] [blame] | 1961 | // Although not in conformance with C99, we also allow the argument to be |
| 1962 | // an 'unsigned int' as that is a reasonably safe case. GCC also |
| 1963 | // doesn't emit a warning for that case. |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1964 | CoveredArgs.set(argIndex); |
| 1965 | const Expr *Arg = getDataArg(argIndex); |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1966 | QualType T = Arg->getType(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1967 | |
| 1968 | const analyze_printf::ArgTypeResult &ATR = Amt.getArgType(S.Context); |
| 1969 | assert(ATR.isValid()); |
| 1970 | |
| 1971 | if (!ATR.matchesType(S.Context, T)) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1972 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type) |
Hans Wennborg | a792aff | 2011-12-07 10:33:11 +0000 | [diff] [blame] | 1973 | << k << ATR.getRepresentativeTypeName(S.Context) |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1974 | << T << Arg->getSourceRange(), |
| 1975 | getLocationOfByte(Amt.getStart()), |
| 1976 | /*IsStringLocation*/true, |
| 1977 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1978 | // Don't do any more checking. We will just emit |
| 1979 | // spurious errors. |
| 1980 | return false; |
| 1981 | } |
| 1982 | } |
| 1983 | } |
| 1984 | return true; |
| 1985 | } |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1986 | |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1987 | void CheckPrintfHandler::HandleInvalidAmount( |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1988 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1989 | const analyze_printf::OptionalAmount &Amt, |
| 1990 | unsigned type, |
| 1991 | const char *startSpecifier, |
| 1992 | unsigned specifierLen) { |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1993 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 1994 | FS.getConversionSpecifier(); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1995 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1996 | FixItHint fixit = |
| 1997 | Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant |
| 1998 | ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), |
| 1999 | Amt.getConstantLength())) |
| 2000 | : FixItHint(); |
| 2001 | |
| 2002 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount) |
| 2003 | << type << CS.toString(), |
| 2004 | getLocationOfByte(Amt.getStart()), |
| 2005 | /*IsStringLocation*/true, |
| 2006 | getSpecifierRange(startSpecifier, specifierLen), |
| 2007 | fixit); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2010 | void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2011 | const analyze_printf::OptionalFlag &flag, |
| 2012 | const char *startSpecifier, |
| 2013 | unsigned specifierLen) { |
| 2014 | // Warn about pointless flag with a fixit removal. |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2015 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 2016 | FS.getConversionSpecifier(); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2017 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag) |
| 2018 | << flag.toString() << CS.toString(), |
| 2019 | getLocationOfByte(flag.getPosition()), |
| 2020 | /*IsStringLocation*/true, |
| 2021 | getSpecifierRange(startSpecifier, specifierLen), |
| 2022 | FixItHint::CreateRemoval( |
| 2023 | getSpecifierRange(flag.getPosition(), 1))); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | void CheckPrintfHandler::HandleIgnoredFlag( |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2027 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2028 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 2029 | const analyze_printf::OptionalFlag &flag, |
| 2030 | const char *startSpecifier, |
| 2031 | unsigned specifierLen) { |
| 2032 | // Warn about ignored flag with a fixit removal. |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2033 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag) |
| 2034 | << ignoredFlag.toString() << flag.toString(), |
| 2035 | getLocationOfByte(ignoredFlag.getPosition()), |
| 2036 | /*IsStringLocation*/true, |
| 2037 | getSpecifierRange(startSpecifier, specifierLen), |
| 2038 | FixItHint::CreateRemoval( |
| 2039 | getSpecifierRange(ignoredFlag.getPosition(), 1))); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2042 | bool |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2043 | CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier |
Ted Kremenek | 5c41ee8 | 2010-02-11 09:27:41 +0000 | [diff] [blame] | 2044 | &FS, |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2045 | const char *startSpecifier, |
| 2046 | unsigned specifierLen) { |
| 2047 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2048 | using namespace analyze_format_string; |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2049 | using namespace analyze_printf; |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2050 | const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2051 | |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2052 | if (FS.consumesDataArgument()) { |
| 2053 | if (atFirstArg) { |
| 2054 | atFirstArg = false; |
| 2055 | usesPositionalArgs = FS.usesPositionalArg(); |
| 2056 | } |
| 2057 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2058 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), |
| 2059 | startSpecifier, specifierLen); |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2060 | return false; |
| 2061 | } |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2062 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2063 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2064 | // First check if the field width, precision, and conversion specifier |
| 2065 | // have matching data arguments. |
| 2066 | if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, |
| 2067 | startSpecifier, specifierLen)) { |
| 2068 | return false; |
| 2069 | } |
| 2070 | |
| 2071 | if (!HandleAmount(FS.getPrecision(), /* precision */ 1, |
| 2072 | startSpecifier, specifierLen)) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2073 | return false; |
| 2074 | } |
| 2075 | |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2076 | if (!CS.consumesDataArgument()) { |
| 2077 | // FIXME: Technically specifying a precision or field width here |
| 2078 | // makes no sense. Worth issuing a warning at some point. |
Ted Kremenek | 0e5675d | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 2079 | return true; |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2080 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2081 | |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2082 | // Consume the argument. |
| 2083 | unsigned argIndex = FS.getArgIndex(); |
Ted Kremenek | e3fc547 | 2010-02-27 08:34:51 +0000 | [diff] [blame] | 2084 | if (argIndex < NumDataArgs) { |
| 2085 | // The check to see if the argIndex is valid will come later. |
| 2086 | // We set the bit here because we may exit early from this |
| 2087 | // function if we encounter some other error. |
| 2088 | CoveredArgs.set(argIndex); |
| 2089 | } |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2090 | |
| 2091 | // Check for using an Objective-C specific conversion specifier |
| 2092 | // in a non-ObjC literal. |
| 2093 | if (!IsObjCLiteral && CS.isObjCArg()) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2094 | return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, |
| 2095 | specifierLen); |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2096 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2097 | |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2098 | // Check for invalid use of field width |
| 2099 | if (!FS.hasValidFieldWidth()) { |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2100 | HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2101 | startSpecifier, specifierLen); |
| 2102 | } |
| 2103 | |
| 2104 | // Check for invalid use of precision |
| 2105 | if (!FS.hasValidPrecision()) { |
| 2106 | HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, |
| 2107 | startSpecifier, specifierLen); |
| 2108 | } |
| 2109 | |
| 2110 | // Check each flag does not conflict with any other component. |
Ted Kremenek | 65197b4 | 2011-01-08 05:28:46 +0000 | [diff] [blame] | 2111 | if (!FS.hasValidThousandsGroupingPrefix()) |
| 2112 | HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2113 | if (!FS.hasValidLeadingZeros()) |
| 2114 | HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); |
| 2115 | if (!FS.hasValidPlusPrefix()) |
| 2116 | HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2117 | if (!FS.hasValidSpacePrefix()) |
| 2118 | HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2119 | if (!FS.hasValidAlternativeForm()) |
| 2120 | HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); |
| 2121 | if (!FS.hasValidLeftJustified()) |
| 2122 | HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); |
| 2123 | |
| 2124 | // Check that flags are not ignored by another flag |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2125 | if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' |
| 2126 | HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), |
| 2127 | startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2128 | if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' |
| 2129 | HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), |
| 2130 | startSpecifier, specifierLen); |
| 2131 | |
| 2132 | // Check the length modifier is valid with the given conversion specifier. |
| 2133 | const LengthModifier &LM = FS.getLengthModifier(); |
| 2134 | if (!FS.hasValidLengthModifier()) |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2135 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_nonsensical_length) |
| 2136 | << LM.toString() << CS.toString(), |
| 2137 | getLocationOfByte(LM.getStart()), |
| 2138 | /*IsStringLocation*/true, |
| 2139 | getSpecifierRange(startSpecifier, specifierLen), |
| 2140 | FixItHint::CreateRemoval( |
| 2141 | getSpecifierRange(LM.getStart(), |
| 2142 | LM.getLength()))); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2143 | |
| 2144 | // Are we using '%n'? |
Ted Kremenek | 35d353b | 2010-07-20 20:04:10 +0000 | [diff] [blame] | 2145 | if (CS.getKind() == ConversionSpecifier::nArg) { |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2146 | // Issue a warning about this being a possible security issue. |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2147 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_write_back), |
| 2148 | getLocationOfByte(CS.getStart()), |
| 2149 | /*IsStringLocation*/true, |
| 2150 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | e82d804 | 2010-01-29 01:35:25 +0000 | [diff] [blame] | 2151 | // Continue checking the other format specifiers. |
| 2152 | return true; |
| 2153 | } |
Ted Kremenek | 5c41ee8 | 2010-02-11 09:27:41 +0000 | [diff] [blame] | 2154 | |
Ted Kremenek | da51f0d | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 2155 | // The remaining checks depend on the data arguments. |
| 2156 | if (HasVAListArg) |
| 2157 | return true; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2158 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2159 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | da51f0d | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 2160 | return false; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2161 | |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2162 | // Now type check the data expression that matches the |
| 2163 | // format specifier. |
| 2164 | const Expr *Ex = getDataArg(argIndex); |
Nick Lewycky | 687b5df | 2011-12-02 23:21:43 +0000 | [diff] [blame] | 2165 | const analyze_printf::ArgTypeResult &ATR = FS.getArgType(S.Context); |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2166 | if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) { |
| 2167 | // Check if we didn't match because of an implicit cast from a 'char' |
| 2168 | // or 'short' to an 'int'. This is done because printf is a varargs |
| 2169 | // function. |
| 2170 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Ex)) |
Ted Kremenek | 4d8ae4d | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 2171 | if (ICE->getType() == S.Context.IntTy) { |
| 2172 | // All further checking is done on the subexpression. |
| 2173 | Ex = ICE->getSubExpr(); |
| 2174 | if (ATR.matchesType(S.Context, Ex->getType())) |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2175 | return true; |
Ted Kremenek | 4d8ae4d | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 2176 | } |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2177 | |
| 2178 | // We may be able to offer a FixItHint if it is a supported type. |
| 2179 | PrintfSpecifier fixedFS = FS; |
Hans Wennborg | a7da215 | 2011-10-18 08:10:06 +0000 | [diff] [blame] | 2180 | bool success = fixedFS.fixType(Ex->getType(), S.getLangOptions()); |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2181 | |
| 2182 | if (success) { |
| 2183 | // Get the fix string from the fixed format specifier |
| 2184 | llvm::SmallString<128> buf; |
| 2185 | llvm::raw_svector_ostream os(buf); |
| 2186 | fixedFS.toString(os); |
| 2187 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2188 | EmitFormatDiagnostic( |
| 2189 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
Hans Wennborg | a792aff | 2011-12-07 10:33:11 +0000 | [diff] [blame] | 2190 | << ATR.getRepresentativeTypeName(S.Context) << Ex->getType() |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2191 | << Ex->getSourceRange(), |
| 2192 | getLocationOfByte(CS.getStart()), |
| 2193 | /*IsStringLocation*/true, |
| 2194 | getSpecifierRange(startSpecifier, specifierLen), |
| 2195 | FixItHint::CreateReplacement( |
| 2196 | getSpecifierRange(startSpecifier, specifierLen), |
| 2197 | os.str())); |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2198 | } |
| 2199 | else { |
| 2200 | S.Diag(getLocationOfByte(CS.getStart()), |
| 2201 | diag::warn_printf_conversion_argument_type_mismatch) |
Hans Wennborg | a792aff | 2011-12-07 10:33:11 +0000 | [diff] [blame] | 2202 | << ATR.getRepresentativeTypeName(S.Context) << Ex->getType() |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2203 | << getSpecifierRange(startSpecifier, specifierLen) |
| 2204 | << Ex->getSourceRange(); |
| 2205 | } |
| 2206 | } |
| 2207 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2208 | return true; |
| 2209 | } |
| 2210 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2211 | //===--- CHECK: Scanf format string checking ------------------------------===// |
| 2212 | |
| 2213 | namespace { |
| 2214 | class CheckScanfHandler : public CheckFormatHandler { |
| 2215 | public: |
| 2216 | CheckScanfHandler(Sema &s, const StringLiteral *fexpr, |
| 2217 | const Expr *origFormatExpr, unsigned firstDataArg, |
| 2218 | unsigned numDataArgs, bool isObjCLiteral, |
| 2219 | const char *beg, bool hasVAListArg, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2220 | Expr **Args, unsigned NumArgs, |
| 2221 | unsigned formatIdx, bool inFunctionCall) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2222 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
| 2223 | numDataArgs, isObjCLiteral, beg, hasVAListArg, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2224 | Args, NumArgs, formatIdx, inFunctionCall) {} |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2225 | |
| 2226 | bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, |
| 2227 | const char *startSpecifier, |
| 2228 | unsigned specifierLen); |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2229 | |
| 2230 | bool HandleInvalidScanfConversionSpecifier( |
| 2231 | const analyze_scanf::ScanfSpecifier &FS, |
| 2232 | const char *startSpecifier, |
| 2233 | unsigned specifierLen); |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2234 | |
| 2235 | void HandleIncompleteScanList(const char *start, const char *end); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2236 | }; |
Ted Kremenek | 07d161f | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 2237 | } |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2238 | |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2239 | void CheckScanfHandler::HandleIncompleteScanList(const char *start, |
| 2240 | const char *end) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2241 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete), |
| 2242 | getLocationOfByte(end), /*IsStringLocation*/true, |
| 2243 | getSpecifierRange(start, end - start)); |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2246 | bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( |
| 2247 | const analyze_scanf::ScanfSpecifier &FS, |
| 2248 | const char *startSpecifier, |
| 2249 | unsigned specifierLen) { |
| 2250 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2251 | const analyze_scanf::ScanfConversionSpecifier &CS = |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2252 | FS.getConversionSpecifier(); |
| 2253 | |
| 2254 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 2255 | getLocationOfByte(CS.getStart()), |
| 2256 | startSpecifier, specifierLen, |
| 2257 | CS.getStart(), CS.getLength()); |
| 2258 | } |
| 2259 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2260 | bool CheckScanfHandler::HandleScanfSpecifier( |
| 2261 | const analyze_scanf::ScanfSpecifier &FS, |
| 2262 | const char *startSpecifier, |
| 2263 | unsigned specifierLen) { |
| 2264 | |
| 2265 | using namespace analyze_scanf; |
| 2266 | using namespace analyze_format_string; |
| 2267 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2268 | const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2269 | |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2270 | // Handle case where '%' and '*' don't consume an argument. These shouldn't |
| 2271 | // be used to decide if we are using positional arguments consistently. |
| 2272 | if (FS.consumesDataArgument()) { |
| 2273 | if (atFirstArg) { |
| 2274 | atFirstArg = false; |
| 2275 | usesPositionalArgs = FS.usesPositionalArg(); |
| 2276 | } |
| 2277 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2278 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), |
| 2279 | startSpecifier, specifierLen); |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2280 | return false; |
| 2281 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | // Check if the field with is non-zero. |
| 2285 | const OptionalAmount &Amt = FS.getFieldWidth(); |
| 2286 | if (Amt.getHowSpecified() == OptionalAmount::Constant) { |
| 2287 | if (Amt.getConstantAmount() == 0) { |
| 2288 | const CharSourceRange &R = getSpecifierRange(Amt.getStart(), |
| 2289 | Amt.getConstantLength()); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2290 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width), |
| 2291 | getLocationOfByte(Amt.getStart()), |
| 2292 | /*IsStringLocation*/true, R, |
| 2293 | FixItHint::CreateRemoval(R)); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | if (!FS.consumesDataArgument()) { |
| 2298 | // FIXME: Technically specifying a precision or field width here |
| 2299 | // makes no sense. Worth issuing a warning at some point. |
| 2300 | return true; |
| 2301 | } |
| 2302 | |
| 2303 | // Consume the argument. |
| 2304 | unsigned argIndex = FS.getArgIndex(); |
| 2305 | if (argIndex < NumDataArgs) { |
| 2306 | // The check to see if the argIndex is valid will come later. |
| 2307 | // We set the bit here because we may exit early from this |
| 2308 | // function if we encounter some other error. |
| 2309 | CoveredArgs.set(argIndex); |
| 2310 | } |
| 2311 | |
Ted Kremenek | 1e51c20 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 2312 | // Check the length modifier is valid with the given conversion specifier. |
| 2313 | const LengthModifier &LM = FS.getLengthModifier(); |
| 2314 | if (!FS.hasValidLengthModifier()) { |
| 2315 | S.Diag(getLocationOfByte(LM.getStart()), |
| 2316 | diag::warn_format_nonsensical_length) |
| 2317 | << LM.toString() << CS.toString() |
| 2318 | << getSpecifierRange(startSpecifier, specifierLen) |
| 2319 | << FixItHint::CreateRemoval(getSpecifierRange(LM.getStart(), |
| 2320 | LM.getLength())); |
| 2321 | } |
| 2322 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2323 | // The remaining checks depend on the data arguments. |
| 2324 | if (HasVAListArg) |
| 2325 | return true; |
| 2326 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2327 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2328 | return false; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2329 | |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2330 | // Check that the argument type matches the format specifier. |
| 2331 | const Expr *Ex = getDataArg(argIndex); |
| 2332 | const analyze_scanf::ScanfArgTypeResult &ATR = FS.getArgType(S.Context); |
| 2333 | if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) { |
| 2334 | ScanfSpecifier fixedFS = FS; |
| 2335 | bool success = fixedFS.fixType(Ex->getType(), S.getLangOptions()); |
| 2336 | |
| 2337 | if (success) { |
| 2338 | // Get the fix string from the fixed format specifier. |
| 2339 | llvm::SmallString<128> buf; |
| 2340 | llvm::raw_svector_ostream os(buf); |
| 2341 | fixedFS.toString(os); |
| 2342 | |
| 2343 | EmitFormatDiagnostic( |
| 2344 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
| 2345 | << ATR.getRepresentativeTypeName(S.Context) << Ex->getType() |
| 2346 | << Ex->getSourceRange(), |
| 2347 | getLocationOfByte(CS.getStart()), |
| 2348 | /*IsStringLocation*/true, |
| 2349 | getSpecifierRange(startSpecifier, specifierLen), |
| 2350 | FixItHint::CreateReplacement( |
| 2351 | getSpecifierRange(startSpecifier, specifierLen), |
| 2352 | os.str())); |
| 2353 | } else { |
| 2354 | S.Diag(getLocationOfByte(CS.getStart()), |
| 2355 | diag::warn_printf_conversion_argument_type_mismatch) |
| 2356 | << ATR.getRepresentativeTypeName(S.Context) << Ex->getType() |
| 2357 | << getSpecifierRange(startSpecifier, specifierLen) |
| 2358 | << Ex->getSourceRange(); |
| 2359 | } |
| 2360 | } |
| 2361 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2362 | return true; |
| 2363 | } |
| 2364 | |
| 2365 | void Sema::CheckFormatString(const StringLiteral *FExpr, |
Ted Kremenek | 0e5675d | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 2366 | const Expr *OrigFormatExpr, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2367 | Expr **Args, unsigned NumArgs, |
| 2368 | bool HasVAListArg, unsigned format_idx, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2369 | unsigned firstDataArg, FormatStringType Type, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2370 | bool inFunctionCall) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2371 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2372 | // CHECK: is the format string a wide literal? |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2373 | if (!FExpr->isAscii()) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2374 | CheckFormatHandler::EmitFormatDiagnostic( |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2375 | *this, inFunctionCall, Args[format_idx], |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2376 | PDiag(diag::warn_format_string_is_wide_literal), FExpr->getLocStart(), |
| 2377 | /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2378 | return; |
| 2379 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2380 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2381 | // Str - The format string. NOTE: this is NOT null-terminated! |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2382 | StringRef StrRef = FExpr->getString(); |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 2383 | const char *Str = StrRef.data(); |
| 2384 | unsigned StrLen = StrRef.size(); |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2385 | const unsigned numDataArgs = NumArgs - firstDataArg; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2386 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2387 | // CHECK: empty format string? |
Ted Kremenek | 4cd5791 | 2011-09-29 05:52:16 +0000 | [diff] [blame] | 2388 | if (StrLen == 0 && numDataArgs > 0) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2389 | CheckFormatHandler::EmitFormatDiagnostic( |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2390 | *this, inFunctionCall, Args[format_idx], |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2391 | PDiag(diag::warn_empty_format_string), FExpr->getLocStart(), |
| 2392 | /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2393 | return; |
| 2394 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2395 | |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2396 | if (Type == FST_Printf || Type == FST_NSString) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2397 | CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, |
Ted Kremenek | 4cd5791 | 2011-09-29 05:52:16 +0000 | [diff] [blame] | 2398 | numDataArgs, isa<ObjCStringLiteral>(OrigFormatExpr), |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2399 | Str, HasVAListArg, Args, NumArgs, format_idx, |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2400 | inFunctionCall); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2401 | |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 2402 | if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen, |
| 2403 | getLangOptions())) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2404 | H.DoneProcessing(); |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2405 | } else if (Type == FST_Scanf) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2406 | CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, |
Ted Kremenek | 4cd5791 | 2011-09-29 05:52:16 +0000 | [diff] [blame] | 2407 | numDataArgs, isa<ObjCStringLiteral>(OrigFormatExpr), |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2408 | Str, HasVAListArg, Args, NumArgs, format_idx, |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2409 | inFunctionCall); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2410 | |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 2411 | if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen, |
| 2412 | getLangOptions())) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2413 | H.DoneProcessing(); |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2414 | } // TODO: handle other formats |
Ted Kremenek | ce7024e | 2010-01-28 01:18:22 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2417 | //===--- CHECK: Standard memory functions ---------------------------------===// |
| 2418 | |
Douglas Gregor | 2a053a3 | 2011-05-03 20:05:22 +0000 | [diff] [blame] | 2419 | /// \brief Determine whether the given type is a dynamic class type (e.g., |
| 2420 | /// whether it has a vtable). |
| 2421 | static bool isDynamicClassType(QualType T) { |
| 2422 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
| 2423 | if (CXXRecordDecl *Definition = Record->getDefinition()) |
| 2424 | if (Definition->isDynamicClass()) |
| 2425 | return true; |
| 2426 | |
| 2427 | return false; |
| 2428 | } |
| 2429 | |
Chandler Carruth | a72a12f | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 2430 | /// \brief If E is a sizeof expression, returns its argument expression, |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2431 | /// otherwise returns NULL. |
| 2432 | static const Expr *getSizeOfExprArg(const Expr* E) { |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2433 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2434 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
| 2435 | if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType()) |
| 2436 | return SizeOf->getArgumentExpr()->IgnoreParenImpCasts(); |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2437 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2438 | return 0; |
| 2439 | } |
| 2440 | |
Chandler Carruth | a72a12f | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 2441 | /// \brief If E is a sizeof expression, returns its argument type. |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2442 | static QualType getSizeOfArgType(const Expr* E) { |
| 2443 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
| 2444 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
| 2445 | if (SizeOf->getKind() == clang::UETT_SizeOf) |
| 2446 | return SizeOf->getTypeOfArgument(); |
| 2447 | |
| 2448 | return QualType(); |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2451 | /// \brief Check for dangerous or invalid arguments to memset(). |
| 2452 | /// |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 2453 | /// This issues warnings on known problematic, dangerous or unspecified |
Matt Beaumont-Gay | cc2f30c | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 2454 | /// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp' |
| 2455 | /// function calls. |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2456 | /// |
| 2457 | /// \param Call The call expression to diagnose. |
Matt Beaumont-Gay | cc2f30c | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 2458 | void Sema::CheckMemaccessArguments(const CallExpr *Call, |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2459 | unsigned BId, |
Matt Beaumont-Gay | cc2f30c | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 2460 | IdentifierInfo *FnName) { |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2461 | assert(BId != 0); |
| 2462 | |
Ted Kremenek | 1d59f7f | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 2463 | // It is possible to have a non-standard definition of memset. Validate |
Douglas Gregor | 707a23e | 2011-06-16 17:56:04 +0000 | [diff] [blame] | 2464 | // we have enough arguments, and if not, abort further checking. |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2465 | unsigned ExpectedNumArgs = (BId == Builtin::BIstrndup ? 2 : 3); |
Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2466 | if (Call->getNumArgs() < ExpectedNumArgs) |
Ted Kremenek | 1d59f7f | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 2467 | return; |
| 2468 | |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2469 | unsigned LastArg = (BId == Builtin::BImemset || |
| 2470 | BId == Builtin::BIstrndup ? 1 : 2); |
| 2471 | unsigned LenArg = (BId == Builtin::BIstrndup ? 1 : 2); |
Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2472 | const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts(); |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2473 | |
| 2474 | // We have special checking when the length is a sizeof expression. |
| 2475 | QualType SizeOfArgTy = getSizeOfArgType(LenExpr); |
| 2476 | const Expr *SizeOfArg = getSizeOfExprArg(LenExpr); |
| 2477 | llvm::FoldingSetNodeID SizeOfArgID; |
| 2478 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2479 | for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) { |
| 2480 | const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts(); |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2481 | SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange(); |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2482 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2483 | QualType DestTy = Dest->getType(); |
| 2484 | if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) { |
| 2485 | QualType PointeeTy = DestPtrTy->getPointeeType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2486 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2487 | // Never warn about void type pointers. This can be used to suppress |
| 2488 | // false positives. |
| 2489 | if (PointeeTy->isVoidType()) |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2490 | continue; |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2491 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2492 | // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by |
| 2493 | // actually comparing the expressions for equality. Because computing the |
| 2494 | // expression IDs can be expensive, we only do this if the diagnostic is |
| 2495 | // enabled. |
| 2496 | if (SizeOfArg && |
| 2497 | Diags.getDiagnosticLevel(diag::warn_sizeof_pointer_expr_memaccess, |
| 2498 | SizeOfArg->getExprLoc())) { |
| 2499 | // We only compute IDs for expressions if the warning is enabled, and |
| 2500 | // cache the sizeof arg's ID. |
| 2501 | if (SizeOfArgID == llvm::FoldingSetNodeID()) |
| 2502 | SizeOfArg->Profile(SizeOfArgID, Context, true); |
| 2503 | llvm::FoldingSetNodeID DestID; |
| 2504 | Dest->Profile(DestID, Context, true); |
| 2505 | if (DestID == SizeOfArgID) { |
Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2506 | // TODO: For strncpy() and friends, this could suggest sizeof(dst) |
| 2507 | // over sizeof(src) as well. |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2508 | unsigned ActionIdx = 0; // Default is to suggest dereferencing. |
| 2509 | if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest)) |
| 2510 | if (UnaryOp->getOpcode() == UO_AddrOf) |
| 2511 | ActionIdx = 1; // If its an address-of operator, just remove it. |
| 2512 | if (Context.getTypeSize(PointeeTy) == Context.getCharWidth()) |
| 2513 | ActionIdx = 2; // If the pointee's size is sizeof(char), |
| 2514 | // suggest an explicit length. |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2515 | unsigned DestSrcSelect = |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2516 | (BId == Builtin::BIstrndup ? 1 : ArgIdx); |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2517 | DiagRuntimeBehavior(SizeOfArg->getExprLoc(), Dest, |
| 2518 | PDiag(diag::warn_sizeof_pointer_expr_memaccess) |
Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2519 | << FnName << DestSrcSelect << ActionIdx |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2520 | << Dest->getSourceRange() |
| 2521 | << SizeOfArg->getSourceRange()); |
| 2522 | break; |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | // Also check for cases where the sizeof argument is the exact same |
| 2527 | // type as the memory argument, and where it points to a user-defined |
| 2528 | // record type. |
| 2529 | if (SizeOfArgTy != QualType()) { |
| 2530 | if (PointeeTy->isRecordType() && |
| 2531 | Context.typesAreCompatible(SizeOfArgTy, DestTy)) { |
| 2532 | DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest, |
| 2533 | PDiag(diag::warn_sizeof_pointer_type_memaccess) |
| 2534 | << FnName << SizeOfArgTy << ArgIdx |
| 2535 | << PointeeTy << Dest->getSourceRange() |
| 2536 | << LenExpr->getSourceRange()); |
| 2537 | break; |
| 2538 | } |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2541 | // Always complain about dynamic classes. |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2542 | if (isDynamicClassType(PointeeTy)) { |
| 2543 | |
| 2544 | unsigned OperationType = 0; |
| 2545 | // "overwritten" if we're warning about the destination for any call |
| 2546 | // but memcmp; otherwise a verb appropriate to the call. |
| 2547 | if (ArgIdx != 0 || BId == Builtin::BImemcmp) { |
| 2548 | if (BId == Builtin::BImemcpy) |
| 2549 | OperationType = 1; |
| 2550 | else if(BId == Builtin::BImemmove) |
| 2551 | OperationType = 2; |
| 2552 | else if (BId == Builtin::BImemcmp) |
| 2553 | OperationType = 3; |
| 2554 | } |
| 2555 | |
Matt Beaumont-Gay | 5c5218e | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 2556 | DiagRuntimeBehavior( |
| 2557 | Dest->getExprLoc(), Dest, |
| 2558 | PDiag(diag::warn_dyn_class_memaccess) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2559 | << (BId == Builtin::BImemcmp ? ArgIdx + 2 : ArgIdx) |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2560 | << FnName << PointeeTy |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2561 | << OperationType |
Matt Beaumont-Gay | 5c5218e | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 2562 | << Call->getCallee()->getSourceRange()); |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2563 | } else if (PointeeTy.hasNonTrivialObjCLifetime() && |
| 2564 | BId != Builtin::BImemset) |
Matt Beaumont-Gay | 5c5218e | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 2565 | DiagRuntimeBehavior( |
| 2566 | Dest->getExprLoc(), Dest, |
| 2567 | PDiag(diag::warn_arc_object_memaccess) |
| 2568 | << ArgIdx << FnName << PointeeTy |
| 2569 | << Call->getCallee()->getSourceRange()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2570 | else |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2571 | continue; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2572 | |
| 2573 | DiagRuntimeBehavior( |
| 2574 | Dest->getExprLoc(), Dest, |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 2575 | PDiag(diag::note_bad_memaccess_silence) |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2576 | << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)")); |
| 2577 | break; |
| 2578 | } |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2579 | } |
| 2580 | } |
| 2581 | |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 2582 | // A little helper routine: ignore addition and subtraction of integer literals. |
| 2583 | // This intentionally does not ignore all integer constant expressions because |
| 2584 | // we don't want to remove sizeof(). |
| 2585 | static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) { |
| 2586 | Ex = Ex->IgnoreParenCasts(); |
| 2587 | |
| 2588 | for (;;) { |
| 2589 | const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex); |
| 2590 | if (!BO || !BO->isAdditiveOp()) |
| 2591 | break; |
| 2592 | |
| 2593 | const Expr *RHS = BO->getRHS()->IgnoreParenCasts(); |
| 2594 | const Expr *LHS = BO->getLHS()->IgnoreParenCasts(); |
| 2595 | |
| 2596 | if (isa<IntegerLiteral>(RHS)) |
| 2597 | Ex = LHS; |
| 2598 | else if (isa<IntegerLiteral>(LHS)) |
| 2599 | Ex = RHS; |
| 2600 | else |
| 2601 | break; |
| 2602 | } |
| 2603 | |
| 2604 | return Ex; |
| 2605 | } |
| 2606 | |
| 2607 | // Warn if the user has made the 'size' argument to strlcpy or strlcat |
| 2608 | // be the size of the source, instead of the destination. |
| 2609 | void Sema::CheckStrlcpycatArguments(const CallExpr *Call, |
| 2610 | IdentifierInfo *FnName) { |
| 2611 | |
| 2612 | // Don't crash if the user has the wrong number of arguments |
| 2613 | if (Call->getNumArgs() != 3) |
| 2614 | return; |
| 2615 | |
| 2616 | const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context); |
| 2617 | const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context); |
| 2618 | const Expr *CompareWithSrc = NULL; |
| 2619 | |
| 2620 | // Look for 'strlcpy(dst, x, sizeof(x))' |
| 2621 | if (const Expr *Ex = getSizeOfExprArg(SizeArg)) |
| 2622 | CompareWithSrc = Ex; |
| 2623 | else { |
| 2624 | // Look for 'strlcpy(dst, x, strlen(x))' |
| 2625 | if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2626 | if (SizeCall->isBuiltinCall() == Builtin::BIstrlen |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 2627 | && SizeCall->getNumArgs() == 1) |
| 2628 | CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context); |
| 2629 | } |
| 2630 | } |
| 2631 | |
| 2632 | if (!CompareWithSrc) |
| 2633 | return; |
| 2634 | |
| 2635 | // Determine if the argument to sizeof/strlen is equal to the source |
| 2636 | // argument. In principle there's all kinds of things you could do |
| 2637 | // here, for instance creating an == expression and evaluating it with |
| 2638 | // EvaluateAsBooleanCondition, but this uses a more direct technique: |
| 2639 | const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg); |
| 2640 | if (!SrcArgDRE) |
| 2641 | return; |
| 2642 | |
| 2643 | const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc); |
| 2644 | if (!CompareWithSrcDRE || |
| 2645 | SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl()) |
| 2646 | return; |
| 2647 | |
| 2648 | const Expr *OriginalSizeArg = Call->getArg(2); |
| 2649 | Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size) |
| 2650 | << OriginalSizeArg->getSourceRange() << FnName; |
| 2651 | |
| 2652 | // Output a FIXIT hint if the destination is an array (rather than a |
| 2653 | // pointer to an array). This could be enhanced to handle some |
| 2654 | // pointers if we know the actual size, like if DstArg is 'array+2' |
| 2655 | // we could say 'sizeof(array)-2'. |
| 2656 | const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts(); |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 2657 | QualType DstArgTy = DstArg->getType(); |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 2658 | |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 2659 | // Only handle constant-sized or VLAs, but not flexible members. |
| 2660 | if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(DstArgTy)) { |
| 2661 | // Only issue the FIXIT for arrays of size > 1. |
| 2662 | if (CAT->getSize().getSExtValue() <= 1) |
| 2663 | return; |
| 2664 | } else if (!DstArgTy->isVariableArrayType()) { |
| 2665 | return; |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 2666 | } |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 2667 | |
| 2668 | llvm::SmallString<128> sizeString; |
| 2669 | llvm::raw_svector_ostream OS(sizeString); |
| 2670 | OS << "sizeof("; |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2671 | DstArg->printPretty(OS, Context, 0, getPrintingPolicy()); |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 2672 | OS << ")"; |
| 2673 | |
| 2674 | Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size) |
| 2675 | << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(), |
| 2676 | OS.str()); |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2679 | //===--- CHECK: Return Address of Stack Variable --------------------------===// |
| 2680 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2681 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars); |
| 2682 | static Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2683 | |
| 2684 | /// CheckReturnStackAddr - Check if a return statement returns the address |
| 2685 | /// of a stack variable. |
| 2686 | void |
| 2687 | Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, |
| 2688 | SourceLocation ReturnLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2689 | |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2690 | Expr *stackE = 0; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2691 | SmallVector<DeclRefExpr *, 8> refVars; |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2692 | |
| 2693 | // Perform checking for returned stack addresses, local blocks, |
| 2694 | // label addresses or references to temporaries. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2695 | if (lhsType->isPointerType() || |
| 2696 | (!getLangOptions().ObjCAutoRefCount && lhsType->isBlockPointerType())) { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2697 | stackE = EvalAddr(RetValExp, refVars); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2698 | } else if (lhsType->isReferenceType()) { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2699 | stackE = EvalVal(RetValExp, refVars); |
| 2700 | } |
| 2701 | |
| 2702 | if (stackE == 0) |
| 2703 | return; // Nothing suspicious was found. |
| 2704 | |
| 2705 | SourceLocation diagLoc; |
| 2706 | SourceRange diagRange; |
| 2707 | if (refVars.empty()) { |
| 2708 | diagLoc = stackE->getLocStart(); |
| 2709 | diagRange = stackE->getSourceRange(); |
| 2710 | } else { |
| 2711 | // We followed through a reference variable. 'stackE' contains the |
| 2712 | // problematic expression but we will warn at the return statement pointing |
| 2713 | // at the reference variable. We will later display the "trail" of |
| 2714 | // reference variables using notes. |
| 2715 | diagLoc = refVars[0]->getLocStart(); |
| 2716 | diagRange = refVars[0]->getSourceRange(); |
| 2717 | } |
| 2718 | |
| 2719 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var. |
| 2720 | Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref |
| 2721 | : diag::warn_ret_stack_addr) |
| 2722 | << DR->getDecl()->getDeclName() << diagRange; |
| 2723 | } else if (isa<BlockExpr>(stackE)) { // local block. |
| 2724 | Diag(diagLoc, diag::err_ret_local_block) << diagRange; |
| 2725 | } else if (isa<AddrLabelExpr>(stackE)) { // address of label. |
| 2726 | Diag(diagLoc, diag::warn_ret_addr_label) << diagRange; |
| 2727 | } else { // local temporary. |
| 2728 | Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref |
| 2729 | : diag::warn_ret_local_temp_addr) |
| 2730 | << diagRange; |
| 2731 | } |
| 2732 | |
| 2733 | // Display the "trail" of reference variables that we followed until we |
| 2734 | // found the problematic expression using notes. |
| 2735 | for (unsigned i = 0, e = refVars.size(); i != e; ++i) { |
| 2736 | VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl()); |
| 2737 | // If this var binds to another reference var, show the range of the next |
| 2738 | // var, otherwise the var binds to the problematic expression, in which case |
| 2739 | // show the range of the expression. |
| 2740 | SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange() |
| 2741 | : stackE->getSourceRange(); |
| 2742 | Diag(VD->getLocation(), diag::note_ref_var_local_bind) |
| 2743 | << VD->getDeclName() << range; |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2744 | } |
| 2745 | } |
| 2746 | |
| 2747 | /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that |
| 2748 | /// check if the expression in a return statement evaluates to an address |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2749 | /// to a location on the stack, a local block, an address of a label, or a |
| 2750 | /// reference to local temporary. The recursion is used to traverse the |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2751 | /// AST of the return expression, with recursion backtracking when we |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2752 | /// encounter a subexpression that (1) clearly does not lead to one of the |
| 2753 | /// above problematic expressions (2) is something we cannot determine leads to |
| 2754 | /// a problematic expression based on such local checking. |
| 2755 | /// |
| 2756 | /// Both EvalAddr and EvalVal follow through reference variables to evaluate |
| 2757 | /// the expression that they point to. Such variables are added to the |
| 2758 | /// 'refVars' vector so that we know what the reference variable "trail" was. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2759 | /// |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 2760 | /// EvalAddr processes expressions that are pointers that are used as |
| 2761 | /// references (and not L-values). EvalVal handles all other values. |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2762 | /// At the base case of the recursion is a check for the above problematic |
| 2763 | /// expressions. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2764 | /// |
| 2765 | /// This implementation handles: |
| 2766 | /// |
| 2767 | /// * pointer-to-pointer casts |
| 2768 | /// * implicit conversions from array references to pointers |
| 2769 | /// * taking the address of fields |
| 2770 | /// * arbitrary interplay between "&" and "*" operators |
| 2771 | /// * pointer arithmetic from an address of a stack variable |
| 2772 | /// * taking the address of an array element where the array is on the stack |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2773 | static Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars) { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2774 | if (E->isTypeDependent()) |
| 2775 | return NULL; |
| 2776 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2777 | // We should only be called for evaluating pointer expressions. |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2778 | assert((E->getType()->isAnyPointerType() || |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2779 | E->getType()->isBlockPointerType() || |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2780 | E->getType()->isObjCQualifiedIdType()) && |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2781 | "EvalAddr only works on pointers"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2782 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2783 | E = E->IgnoreParens(); |
| 2784 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2785 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 2786 | // viewed AST node. We then recursively traverse the AST by calling |
| 2787 | // EvalAddr and EvalVal appropriately. |
| 2788 | switch (E->getStmtClass()) { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2789 | case Stmt::DeclRefExprClass: { |
| 2790 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
| 2791 | |
| 2792 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
| 2793 | // If this is a reference variable, follow through to the expression that |
| 2794 | // it points to. |
| 2795 | if (V->hasLocalStorage() && |
| 2796 | V->getType()->isReferenceType() && V->hasInit()) { |
| 2797 | // Add the reference variable to the "trail". |
| 2798 | refVars.push_back(DR); |
| 2799 | return EvalAddr(V->getInit(), refVars); |
| 2800 | } |
| 2801 | |
| 2802 | return NULL; |
| 2803 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2804 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2805 | case Stmt::UnaryOperatorClass: { |
| 2806 | // The only unary operator that make sense to handle here |
| 2807 | // is AddrOf. All others don't make sense as pointers. |
| 2808 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2809 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2810 | if (U->getOpcode() == UO_AddrOf) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2811 | return EvalVal(U->getSubExpr(), refVars); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2812 | else |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2813 | return NULL; |
| 2814 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2815 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2816 | case Stmt::BinaryOperatorClass: { |
| 2817 | // Handle pointer arithmetic. All other binary operators are not valid |
| 2818 | // in this context. |
| 2819 | BinaryOperator *B = cast<BinaryOperator>(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2820 | BinaryOperatorKind op = B->getOpcode(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2821 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2822 | if (op != BO_Add && op != BO_Sub) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2823 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2824 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2825 | Expr *Base = B->getLHS(); |
| 2826 | |
| 2827 | // Determine which argument is the real pointer base. It could be |
| 2828 | // the RHS argument instead of the LHS. |
| 2829 | if (!Base->getType()->isPointerType()) Base = B->getRHS(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2830 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2831 | assert (Base->getType()->isPointerType()); |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2832 | return EvalAddr(Base, refVars); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2833 | } |
Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 2834 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2835 | // For conditional operators we need to see if either the LHS or RHS are |
| 2836 | // valid DeclRefExpr*s. If one of them is valid, we return it. |
| 2837 | case Stmt::ConditionalOperatorClass: { |
| 2838 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2839 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2840 | // Handle the GNU extension for missing LHS. |
Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 2841 | if (Expr *lhsExpr = C->getLHS()) { |
| 2842 | // In C++, we can have a throw-expression, which has 'void' type. |
| 2843 | if (!lhsExpr->getType()->isVoidType()) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2844 | if (Expr* LHS = EvalAddr(lhsExpr, refVars)) |
Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 2845 | return LHS; |
| 2846 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2847 | |
Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 2848 | // In C++, we can have a throw-expression, which has 'void' type. |
| 2849 | if (C->getRHS()->getType()->isVoidType()) |
| 2850 | return NULL; |
| 2851 | |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2852 | return EvalAddr(C->getRHS(), refVars); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2853 | } |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2854 | |
| 2855 | case Stmt::BlockExprClass: |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2856 | if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures()) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2857 | return E; // local block. |
| 2858 | return NULL; |
| 2859 | |
| 2860 | case Stmt::AddrLabelExprClass: |
| 2861 | return E; // address of label. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2862 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 2863 | case Stmt::ExprWithCleanupsClass: |
| 2864 | return EvalAddr(cast<ExprWithCleanups>(E)->getSubExpr(), refVars); |
| 2865 | |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 2866 | // For casts, we need to handle conversions from arrays to |
| 2867 | // pointer values, and pointer-to-pointer conversions. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2868 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2869 | case Stmt::CStyleCastExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2870 | case Stmt::CXXFunctionalCastExprClass: |
| 2871 | case Stmt::ObjCBridgedCastExprClass: { |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 2872 | Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 2873 | QualType T = SubExpr->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2874 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2875 | if (SubExpr->getType()->isPointerType() || |
| 2876 | SubExpr->getType()->isBlockPointerType() || |
| 2877 | SubExpr->getType()->isObjCQualifiedIdType()) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2878 | return EvalAddr(SubExpr, refVars); |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 2879 | else if (T->isArrayType()) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2880 | return EvalVal(SubExpr, refVars); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2881 | else |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 2882 | return 0; |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2883 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2884 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2885 | // C++ casts. For dynamic casts, static casts, and const casts, we |
| 2886 | // are always converting from a pointer-to-pointer, so we just blow |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2887 | // through the cast. In the case the dynamic cast doesn't fail (and |
| 2888 | // return NULL), we take the conservative route and report cases |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2889 | // where we return the address of a stack variable. For Reinterpre |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2890 | // FIXME: The comment about is wrong; we're not always converting |
| 2891 | // from pointer to pointer. I'm guessing that this code should also |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2892 | // handle references to objects. |
| 2893 | case Stmt::CXXStaticCastExprClass: |
| 2894 | case Stmt::CXXDynamicCastExprClass: |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2895 | case Stmt::CXXConstCastExprClass: |
| 2896 | case Stmt::CXXReinterpretCastExprClass: { |
| 2897 | Expr *S = cast<CXXNamedCastExpr>(E)->getSubExpr(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2898 | if (S->getType()->isPointerType() || S->getType()->isBlockPointerType()) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2899 | return EvalAddr(S, refVars); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2900 | else |
| 2901 | return NULL; |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2902 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2903 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2904 | case Stmt::MaterializeTemporaryExprClass: |
| 2905 | if (Expr *Result = EvalAddr( |
| 2906 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), |
| 2907 | refVars)) |
| 2908 | return Result; |
| 2909 | |
| 2910 | return E; |
| 2911 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2912 | // Everything else: we simply don't reason about them. |
| 2913 | default: |
| 2914 | return NULL; |
| 2915 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2916 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2917 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2918 | |
| 2919 | /// EvalVal - This function is complements EvalAddr in the mutual recursion. |
| 2920 | /// See the comments for EvalAddr for more details. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2921 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 2922 | do { |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 2923 | // We should only be called for evaluating non-pointer expressions, or |
| 2924 | // expressions with a pointer type that are not used as references but instead |
| 2925 | // are l-values (e.g., DeclRefExpr with a pointer type). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2926 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2927 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 2928 | // viewed AST node. We then recursively traverse the AST by calling |
| 2929 | // EvalAddr and EvalVal appropriately. |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2930 | |
| 2931 | E = E->IgnoreParens(); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2932 | switch (E->getStmtClass()) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 2933 | case Stmt::ImplicitCastExprClass: { |
| 2934 | ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E); |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2935 | if (IE->getValueKind() == VK_LValue) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 2936 | E = IE->getSubExpr(); |
| 2937 | continue; |
| 2938 | } |
| 2939 | return NULL; |
| 2940 | } |
| 2941 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 2942 | case Stmt::ExprWithCleanupsClass: |
| 2943 | return EvalVal(cast<ExprWithCleanups>(E)->getSubExpr(), refVars); |
| 2944 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2945 | case Stmt::DeclRefExprClass: { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2946 | // When we hit a DeclRefExpr we are looking at code that refers to a |
| 2947 | // variable's name. If it's not a reference variable we check if it has |
| 2948 | // local storage within the function, and if so, return the expression. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2949 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2950 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2951 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2952 | if (V->hasLocalStorage()) { |
| 2953 | if (!V->getType()->isReferenceType()) |
| 2954 | return DR; |
| 2955 | |
| 2956 | // Reference variable, follow through to the expression that |
| 2957 | // it points to. |
| 2958 | if (V->hasInit()) { |
| 2959 | // Add the reference variable to the "trail". |
| 2960 | refVars.push_back(DR); |
| 2961 | return EvalVal(V->getInit(), refVars); |
| 2962 | } |
| 2963 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2964 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2965 | return NULL; |
| 2966 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2967 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2968 | case Stmt::UnaryOperatorClass: { |
| 2969 | // The only unary operator that make sense to handle here |
| 2970 | // is Deref. All others don't resolve to a "name." This includes |
| 2971 | // handling all sorts of rvalues passed to a unary operator. |
| 2972 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2973 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2974 | if (U->getOpcode() == UO_Deref) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2975 | return EvalAddr(U->getSubExpr(), refVars); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2976 | |
| 2977 | return NULL; |
| 2978 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2979 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2980 | case Stmt::ArraySubscriptExprClass: { |
| 2981 | // Array subscripts are potential references to data on the stack. We |
| 2982 | // retrieve the DeclRefExpr* for the array variable if it indeed |
| 2983 | // has local storage. |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2984 | return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2985 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2986 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2987 | case Stmt::ConditionalOperatorClass: { |
| 2988 | // 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] | 2989 | // non-NULL Expr's. If one is non-NULL, we return it. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2990 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 2991 | |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 2992 | // Handle the GNU extension for missing LHS. |
| 2993 | if (Expr *lhsExpr = C->getLHS()) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2994 | if (Expr *LHS = EvalVal(lhsExpr, refVars)) |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 2995 | return LHS; |
| 2996 | |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2997 | return EvalVal(C->getRHS(), refVars); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2998 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2999 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3000 | // Accesses to members are potential references to data on the stack. |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 3001 | case Stmt::MemberExprClass: { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3002 | MemberExpr *M = cast<MemberExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3003 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3004 | // Check for indirect access. We only want direct field accesses. |
Ted Kremenek | a423e81 | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 3005 | if (M->isArrow()) |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3006 | return NULL; |
Ted Kremenek | a423e81 | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 3007 | |
| 3008 | // Check whether the member type is itself a reference, in which case |
| 3009 | // we're not going to refer to the member, but to what the member refers to. |
| 3010 | if (M->getMemberDecl()->getType()->isReferenceType()) |
| 3011 | return NULL; |
| 3012 | |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3013 | return EvalVal(M->getBase(), refVars); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3014 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3015 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3016 | case Stmt::MaterializeTemporaryExprClass: |
| 3017 | if (Expr *Result = EvalVal( |
| 3018 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), |
| 3019 | refVars)) |
| 3020 | return Result; |
| 3021 | |
| 3022 | return E; |
| 3023 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3024 | default: |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3025 | // Check that we don't return or take the address of a reference to a |
| 3026 | // temporary. This is only useful in C++. |
| 3027 | if (!E->isTypeDependent() && E->isRValue()) |
| 3028 | return E; |
| 3029 | |
| 3030 | // Everything else: we simply don't reason about them. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3031 | return NULL; |
| 3032 | } |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3033 | } while (true); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3034 | } |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3035 | |
| 3036 | //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// |
| 3037 | |
| 3038 | /// Check for comparisons of floating point operands using != and ==. |
| 3039 | /// Issue a warning if these are no self-comparisons, as they are not likely |
| 3040 | /// to do what the programmer intended. |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3041 | void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) { |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3042 | bool EmitWarning = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3043 | |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3044 | Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts(); |
| 3045 | Expr* RightExprSansParen = RHS->IgnoreParenImpCasts(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3046 | |
| 3047 | // Special case: check for x == x (which is OK). |
| 3048 | // Do not emit warnings for such cases. |
| 3049 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) |
| 3050 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) |
| 3051 | if (DRL->getDecl() == DRR->getDecl()) |
| 3052 | EmitWarning = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3053 | |
| 3054 | |
Ted Kremenek | 1b500bb | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 3055 | // Special case: check for comparisons against literals that can be exactly |
| 3056 | // represented by APFloat. In such cases, do not emit a warning. This |
| 3057 | // is a heuristic: often comparison against such literals are used to |
| 3058 | // detect if a value in a variable has not changed. This clearly can |
| 3059 | // lead to false negatives. |
| 3060 | if (EmitWarning) { |
| 3061 | if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { |
| 3062 | if (FLL->isExact()) |
| 3063 | EmitWarning = false; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3064 | } else |
Ted Kremenek | 1b500bb | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 3065 | if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)){ |
| 3066 | if (FLR->isExact()) |
| 3067 | EmitWarning = false; |
| 3068 | } |
| 3069 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3070 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3071 | // Check for comparisons with builtin types. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3072 | if (EmitWarning) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3073 | if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3074 | if (CL->isBuiltinCall()) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3075 | EmitWarning = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3076 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3077 | if (EmitWarning) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3078 | if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3079 | if (CR->isBuiltinCall()) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3080 | EmitWarning = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3081 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3082 | // Emit the diagnostic. |
| 3083 | if (EmitWarning) |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3084 | Diag(Loc, diag::warn_floatingpoint_eq) |
| 3085 | << LHS->getSourceRange() << RHS->getSourceRange(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3086 | } |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3087 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3088 | //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// |
| 3089 | //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3090 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3091 | namespace { |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3092 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3093 | /// Structure recording the 'active' range of an integer-valued |
| 3094 | /// expression. |
| 3095 | struct IntRange { |
| 3096 | /// The number of bits active in the int. |
| 3097 | unsigned Width; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3098 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3099 | /// True if the int is known not to have negative values. |
| 3100 | bool NonNegative; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3101 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3102 | IntRange(unsigned Width, bool NonNegative) |
| 3103 | : Width(Width), NonNegative(NonNegative) |
| 3104 | {} |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3105 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3106 | /// Returns the range of the bool type. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3107 | static IntRange forBoolType() { |
| 3108 | return IntRange(1, true); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3111 | /// Returns the range of an opaque value of the given integral type. |
| 3112 | static IntRange forValueOfType(ASTContext &C, QualType T) { |
| 3113 | return forValueOfCanonicalType(C, |
| 3114 | T->getCanonicalTypeInternal().getTypePtr()); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3117 | /// Returns the range of an opaque value of a canonical integral type. |
| 3118 | static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3119 | assert(T->isCanonicalUnqualified()); |
| 3120 | |
| 3121 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 3122 | T = VT->getElementType().getTypePtr(); |
| 3123 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 3124 | T = CT->getElementType().getTypePtr(); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3125 | |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3126 | // For enum types, use the known bit width of the enumerators. |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3127 | if (const EnumType *ET = dyn_cast<EnumType>(T)) { |
| 3128 | EnumDecl *Enum = ET->getDecl(); |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3129 | if (!Enum->isCompleteDefinition()) |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3130 | return IntRange(C.getIntWidth(QualType(T, 0)), false); |
| 3131 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3132 | unsigned NumPositive = Enum->getNumPositiveBits(); |
| 3133 | unsigned NumNegative = Enum->getNumNegativeBits(); |
| 3134 | |
| 3135 | return IntRange(std::max(NumPositive, NumNegative), NumNegative == 0); |
| 3136 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3137 | |
| 3138 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 3139 | assert(BT->isInteger()); |
| 3140 | |
| 3141 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 3142 | } |
| 3143 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3144 | /// Returns the "target" range of a canonical integral type, i.e. |
| 3145 | /// the range of values expressible in the type. |
| 3146 | /// |
| 3147 | /// This matches forValueOfCanonicalType except that enums have the |
| 3148 | /// full range of their type, not the range of their enumerators. |
| 3149 | static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) { |
| 3150 | assert(T->isCanonicalUnqualified()); |
| 3151 | |
| 3152 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 3153 | T = VT->getElementType().getTypePtr(); |
| 3154 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 3155 | T = CT->getElementType().getTypePtr(); |
| 3156 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Douglas Gregor | 69ff26b | 2011-09-08 23:29:05 +0000 | [diff] [blame] | 3157 | T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr(); |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3158 | |
| 3159 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 3160 | assert(BT->isInteger()); |
| 3161 | |
| 3162 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 3163 | } |
| 3164 | |
| 3165 | /// Returns the supremum of two ranges: i.e. their conservative merge. |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 3166 | static IntRange join(IntRange L, IntRange R) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3167 | return IntRange(std::max(L.Width, R.Width), |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3168 | L.NonNegative && R.NonNegative); |
| 3169 | } |
| 3170 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3171 | /// Returns the infinum of two ranges: i.e. their aggressive merge. |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 3172 | static IntRange meet(IntRange L, IntRange R) { |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3173 | return IntRange(std::min(L.Width, R.Width), |
| 3174 | L.NonNegative || R.NonNegative); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3175 | } |
| 3176 | }; |
| 3177 | |
| 3178 | IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, unsigned MaxWidth) { |
| 3179 | if (value.isSigned() && value.isNegative()) |
| 3180 | return IntRange(value.getMinSignedBits(), false); |
| 3181 | |
| 3182 | if (value.getBitWidth() > MaxWidth) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3183 | value = value.trunc(MaxWidth); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3184 | |
| 3185 | // isNonNegative() just checks the sign bit without considering |
| 3186 | // signedness. |
| 3187 | return IntRange(value.getActiveBits(), true); |
| 3188 | } |
| 3189 | |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 3190 | IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3191 | unsigned MaxWidth) { |
| 3192 | if (result.isInt()) |
| 3193 | return GetValueRange(C, result.getInt(), MaxWidth); |
| 3194 | |
| 3195 | if (result.isVector()) { |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 3196 | IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); |
| 3197 | for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { |
| 3198 | IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); |
| 3199 | R = IntRange::join(R, El); |
| 3200 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3201 | return R; |
| 3202 | } |
| 3203 | |
| 3204 | if (result.isComplexInt()) { |
| 3205 | IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); |
| 3206 | IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); |
| 3207 | return IntRange::join(R, I); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3208 | } |
| 3209 | |
| 3210 | // This can happen with lossless casts to intptr_t of "based" lvalues. |
| 3211 | // Assume it might use arbitrary bits. |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 3212 | // FIXME: The only reason we need to pass the type in here is to get |
| 3213 | // the sign right on this one case. It would be nice if APValue |
| 3214 | // preserved this. |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 3215 | assert(result.isLValue() || result.isAddrLabelDiff()); |
Douglas Gregor | 5e9ebb3 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 3216 | return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType()); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3217 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3218 | |
| 3219 | /// Pseudo-evaluate the given integer expression, estimating the |
| 3220 | /// range of values it might take. |
| 3221 | /// |
| 3222 | /// \param MaxWidth - the width to which the value will be truncated |
| 3223 | IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) { |
| 3224 | E = E->IgnoreParens(); |
| 3225 | |
| 3226 | // Try a full evaluation first. |
| 3227 | Expr::EvalResult result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3228 | if (E->EvaluateAsRValue(result, C)) |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 3229 | return GetValueRange(C, result.Val, E->getType(), MaxWidth); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3230 | |
| 3231 | // I think we only want to look through implicit casts here; if the |
| 3232 | // user has an explicit widening cast, we should treat the value as |
| 3233 | // being of the new, wider type. |
| 3234 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) { |
Eli Friedman | b17ee5b | 2011-12-15 02:41:52 +0000 | [diff] [blame] | 3235 | if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue) |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3236 | return GetExprRange(C, CE->getSubExpr(), MaxWidth); |
| 3237 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3238 | IntRange OutputTypeRange = IntRange::forValueOfType(C, CE->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3239 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3240 | bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast); |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3241 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3242 | // 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] | 3243 | if (!isIntegerCast) |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3244 | return OutputTypeRange; |
| 3245 | |
| 3246 | IntRange SubRange |
| 3247 | = GetExprRange(C, CE->getSubExpr(), |
| 3248 | std::min(MaxWidth, OutputTypeRange.Width)); |
| 3249 | |
| 3250 | // Bail out if the subexpr's range is as wide as the cast type. |
| 3251 | if (SubRange.Width >= OutputTypeRange.Width) |
| 3252 | return OutputTypeRange; |
| 3253 | |
| 3254 | // Otherwise, we take the smaller width, and we're non-negative if |
| 3255 | // either the output type or the subexpr is. |
| 3256 | return IntRange(SubRange.Width, |
| 3257 | SubRange.NonNegative || OutputTypeRange.NonNegative); |
| 3258 | } |
| 3259 | |
| 3260 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 3261 | // If we can fold the condition, just take that operand. |
| 3262 | bool CondResult; |
| 3263 | if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) |
| 3264 | return GetExprRange(C, CondResult ? CO->getTrueExpr() |
| 3265 | : CO->getFalseExpr(), |
| 3266 | MaxWidth); |
| 3267 | |
| 3268 | // Otherwise, conservatively merge. |
| 3269 | IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth); |
| 3270 | IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth); |
| 3271 | return IntRange::join(L, R); |
| 3272 | } |
| 3273 | |
| 3274 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 3275 | switch (BO->getOpcode()) { |
| 3276 | |
| 3277 | // Boolean-valued operations are single-bit and positive. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3278 | case BO_LAnd: |
| 3279 | case BO_LOr: |
| 3280 | case BO_LT: |
| 3281 | case BO_GT: |
| 3282 | case BO_LE: |
| 3283 | case BO_GE: |
| 3284 | case BO_EQ: |
| 3285 | case BO_NE: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3286 | return IntRange::forBoolType(); |
| 3287 | |
John McCall | 862ff87 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 3288 | // The type of the assignments is the type of the LHS, so the RHS |
| 3289 | // is not necessarily the same type. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3290 | case BO_MulAssign: |
| 3291 | case BO_DivAssign: |
| 3292 | case BO_RemAssign: |
| 3293 | case BO_AddAssign: |
| 3294 | case BO_SubAssign: |
John McCall | 862ff87 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 3295 | case BO_XorAssign: |
| 3296 | case BO_OrAssign: |
| 3297 | // TODO: bitfields? |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3298 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 3299 | |
John McCall | 862ff87 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 3300 | // Simple assignments just pass through the RHS, which will have |
| 3301 | // been coerced to the LHS type. |
| 3302 | case BO_Assign: |
| 3303 | // TODO: bitfields? |
| 3304 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 3305 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3306 | // Operations with opaque sources are black-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3307 | case BO_PtrMemD: |
| 3308 | case BO_PtrMemI: |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3309 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3310 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3311 | // Bitwise-and uses the *infinum* of the two source ranges. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3312 | case BO_And: |
| 3313 | case BO_AndAssign: |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3314 | return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth), |
| 3315 | GetExprRange(C, BO->getRHS(), MaxWidth)); |
| 3316 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3317 | // Left shift gets black-listed based on a judgement call. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3318 | case BO_Shl: |
John McCall | 3aae609 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 3319 | // ...except that we want to treat '1 << (blah)' as logically |
| 3320 | // positive. It's an important idiom. |
| 3321 | if (IntegerLiteral *I |
| 3322 | = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { |
| 3323 | if (I->getValue() == 1) { |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3324 | IntRange R = IntRange::forValueOfType(C, E->getType()); |
John McCall | 3aae609 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 3325 | return IntRange(R.Width, /*NonNegative*/ true); |
| 3326 | } |
| 3327 | } |
| 3328 | // fallthrough |
| 3329 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3330 | case BO_ShlAssign: |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3331 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3332 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3333 | // Right shift by a constant can narrow its left argument. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3334 | case BO_Shr: |
| 3335 | case BO_ShrAssign: { |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3336 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 3337 | |
| 3338 | // If the shift amount is a positive constant, drop the width by |
| 3339 | // that much. |
| 3340 | llvm::APSInt shift; |
| 3341 | if (BO->getRHS()->isIntegerConstantExpr(shift, C) && |
| 3342 | shift.isNonNegative()) { |
| 3343 | unsigned zext = shift.getZExtValue(); |
| 3344 | if (zext >= L.Width) |
| 3345 | L.Width = (L.NonNegative ? 0 : 1); |
| 3346 | else |
| 3347 | L.Width -= zext; |
| 3348 | } |
| 3349 | |
| 3350 | return L; |
| 3351 | } |
| 3352 | |
| 3353 | // Comma acts as its right operand. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3354 | case BO_Comma: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3355 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 3356 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3357 | // Black-list pointer subtractions. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3358 | case BO_Sub: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3359 | if (BO->getLHS()->getType()->isPointerType()) |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3360 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | 00fe761 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 3361 | break; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3362 | |
John McCall | 00fe761 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 3363 | // The width of a division result is mostly determined by the size |
| 3364 | // of the LHS. |
| 3365 | case BO_Div: { |
| 3366 | // Don't 'pre-truncate' the operands. |
| 3367 | unsigned opWidth = C.getIntWidth(E->getType()); |
| 3368 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); |
| 3369 | |
| 3370 | // If the divisor is constant, use that. |
| 3371 | llvm::APSInt divisor; |
| 3372 | if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) { |
| 3373 | unsigned log2 = divisor.logBase2(); // floor(log_2(divisor)) |
| 3374 | if (log2 >= L.Width) |
| 3375 | L.Width = (L.NonNegative ? 0 : 1); |
| 3376 | else |
| 3377 | L.Width = std::min(L.Width - log2, MaxWidth); |
| 3378 | return L; |
| 3379 | } |
| 3380 | |
| 3381 | // Otherwise, just use the LHS's width. |
| 3382 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); |
| 3383 | return IntRange(L.Width, L.NonNegative && R.NonNegative); |
| 3384 | } |
| 3385 | |
| 3386 | // The result of a remainder can't be larger than the result of |
| 3387 | // either side. |
| 3388 | case BO_Rem: { |
| 3389 | // Don't 'pre-truncate' the operands. |
| 3390 | unsigned opWidth = C.getIntWidth(E->getType()); |
| 3391 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); |
| 3392 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); |
| 3393 | |
| 3394 | IntRange meet = IntRange::meet(L, R); |
| 3395 | meet.Width = std::min(meet.Width, MaxWidth); |
| 3396 | return meet; |
| 3397 | } |
| 3398 | |
| 3399 | // The default behavior is okay for these. |
| 3400 | case BO_Mul: |
| 3401 | case BO_Add: |
| 3402 | case BO_Xor: |
| 3403 | case BO_Or: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3404 | break; |
| 3405 | } |
| 3406 | |
John McCall | 00fe761 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 3407 | // The default case is to treat the operation as if it were closed |
| 3408 | // on the narrowest type that encompasses both operands. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3409 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 3410 | IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth); |
| 3411 | return IntRange::join(L, R); |
| 3412 | } |
| 3413 | |
| 3414 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 3415 | switch (UO->getOpcode()) { |
| 3416 | // Boolean-valued operations are white-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3417 | case UO_LNot: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3418 | return IntRange::forBoolType(); |
| 3419 | |
| 3420 | // Operations with opaque sources are black-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3421 | case UO_Deref: |
| 3422 | case UO_AddrOf: // should be impossible |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3423 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3424 | |
| 3425 | default: |
| 3426 | return GetExprRange(C, UO->getSubExpr(), MaxWidth); |
| 3427 | } |
| 3428 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3429 | |
| 3430 | if (dyn_cast<OffsetOfExpr>(E)) { |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3431 | IntRange::forValueOfType(C, E->getType()); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3432 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3433 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3434 | if (FieldDecl *BitField = E->getBitField()) |
| 3435 | return IntRange(BitField->getBitWidthValue(C), |
Douglas Gregor | 5e9ebb3 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 3436 | BitField->getType()->isUnsignedIntegerOrEnumerationType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3437 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3438 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3439 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3440 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3441 | IntRange GetExprRange(ASTContext &C, Expr *E) { |
| 3442 | return GetExprRange(C, E, C.getIntWidth(E->getType())); |
| 3443 | } |
| 3444 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3445 | /// Checks whether the given value, which currently has the given |
| 3446 | /// source semantics, has the same value when coerced through the |
| 3447 | /// target semantics. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3448 | bool IsSameFloatAfterCast(const llvm::APFloat &value, |
| 3449 | const llvm::fltSemantics &Src, |
| 3450 | const llvm::fltSemantics &Tgt) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3451 | llvm::APFloat truncated = value; |
| 3452 | |
| 3453 | bool ignored; |
| 3454 | truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 3455 | truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 3456 | |
| 3457 | return truncated.bitwiseIsEqual(value); |
| 3458 | } |
| 3459 | |
| 3460 | /// Checks whether the given value, which currently has the given |
| 3461 | /// source semantics, has the same value when coerced through the |
| 3462 | /// target semantics. |
| 3463 | /// |
| 3464 | /// The value might be a vector of floats (or a complex number). |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3465 | bool IsSameFloatAfterCast(const APValue &value, |
| 3466 | const llvm::fltSemantics &Src, |
| 3467 | const llvm::fltSemantics &Tgt) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3468 | if (value.isFloat()) |
| 3469 | return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); |
| 3470 | |
| 3471 | if (value.isVector()) { |
| 3472 | for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) |
| 3473 | if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) |
| 3474 | return false; |
| 3475 | return true; |
| 3476 | } |
| 3477 | |
| 3478 | assert(value.isComplexFloat()); |
| 3479 | return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && |
| 3480 | IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); |
| 3481 | } |
| 3482 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3483 | void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3484 | |
Ted Kremenek | e3b159c | 2010-09-23 21:43:44 +0000 | [diff] [blame] | 3485 | static bool IsZero(Sema &S, Expr *E) { |
| 3486 | // Suppress cases where we are comparing against an enum constant. |
| 3487 | if (const DeclRefExpr *DR = |
| 3488 | dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) |
| 3489 | if (isa<EnumConstantDecl>(DR->getDecl())) |
| 3490 | return false; |
| 3491 | |
| 3492 | // Suppress cases where the '0' value is expanded from a macro. |
| 3493 | if (E->getLocStart().isMacroID()) |
| 3494 | return false; |
| 3495 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3496 | llvm::APSInt Value; |
| 3497 | return E->isIntegerConstantExpr(Value, S.Context) && Value == 0; |
| 3498 | } |
| 3499 | |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3500 | static bool HasEnumType(Expr *E) { |
| 3501 | // Strip off implicit integral promotions. |
| 3502 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
Argyrios Kyrtzidis | 63b57ae | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 3503 | if (ICE->getCastKind() != CK_IntegralCast && |
| 3504 | ICE->getCastKind() != CK_NoOp) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3505 | break; |
Argyrios Kyrtzidis | 63b57ae | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 3506 | E = ICE->getSubExpr(); |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
| 3509 | return E->getType()->isEnumeralType(); |
| 3510 | } |
| 3511 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3512 | void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3513 | BinaryOperatorKind op = E->getOpcode(); |
Douglas Gregor | 14af91a | 2010-12-21 07:22:56 +0000 | [diff] [blame] | 3514 | if (E->isValueDependent()) |
| 3515 | return; |
| 3516 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3517 | if (op == BO_LT && IsZero(S, E->getRHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3518 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3519 | << "< 0" << "false" << HasEnumType(E->getLHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3520 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3521 | } else if (op == BO_GE && IsZero(S, E->getRHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3522 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3523 | << ">= 0" << "true" << HasEnumType(E->getLHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3524 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3525 | } else if (op == BO_GT && IsZero(S, E->getLHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3526 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3527 | << "0 >" << "false" << HasEnumType(E->getRHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3528 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3529 | } else if (op == BO_LE && IsZero(S, E->getLHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3530 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3531 | << "0 <=" << "true" << HasEnumType(E->getRHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3532 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
| 3533 | } |
| 3534 | } |
| 3535 | |
| 3536 | /// Analyze the operands of the given comparison. Implements the |
| 3537 | /// fallback case from AnalyzeComparison. |
| 3538 | void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3539 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 3540 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3541 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3542 | |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3543 | /// \brief Implements -Wsign-compare. |
| 3544 | /// |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3545 | /// \param E the binary operator to check for warnings |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3546 | void AnalyzeComparison(Sema &S, BinaryOperator *E) { |
| 3547 | // The type the comparison is being performed in. |
| 3548 | QualType T = E->getLHS()->getType(); |
| 3549 | assert(S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType()) |
| 3550 | && "comparison with mismatched types"); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3551 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3552 | // We don't do anything special if this isn't an unsigned integral |
| 3553 | // comparison: we're only interested in integral comparisons, and |
| 3554 | // 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] | 3555 | // |
| 3556 | // We also don't care about value-dependent expressions or expressions |
| 3557 | // whose result is a constant. |
| 3558 | if (!T->hasUnsignedIntegerRepresentation() |
| 3559 | || E->isValueDependent() || E->isIntegerConstantExpr(S.Context)) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3560 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3561 | |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3562 | Expr *LHS = E->getLHS()->IgnoreParenImpCasts(); |
| 3563 | Expr *RHS = E->getRHS()->IgnoreParenImpCasts(); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3564 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3565 | // Check to see if one of the (unmodified) operands is of different |
| 3566 | // signedness. |
| 3567 | Expr *signedOperand, *unsignedOperand; |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3568 | if (LHS->getType()->hasSignedIntegerRepresentation()) { |
| 3569 | assert(!RHS->getType()->hasSignedIntegerRepresentation() && |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3570 | "unsigned comparison between two signed integer expressions?"); |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3571 | signedOperand = LHS; |
| 3572 | unsignedOperand = RHS; |
| 3573 | } else if (RHS->getType()->hasSignedIntegerRepresentation()) { |
| 3574 | signedOperand = RHS; |
| 3575 | unsignedOperand = LHS; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3576 | } else { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3577 | CheckTrivialUnsignedComparison(S, E); |
| 3578 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3579 | } |
| 3580 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3581 | // Otherwise, calculate the effective range of the signed operand. |
| 3582 | IntRange signedRange = GetExprRange(S.Context, signedOperand); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3583 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3584 | // Go ahead and analyze implicit conversions in the operands. Note |
| 3585 | // that we skip the implicit conversions on both sides. |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3586 | AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc()); |
| 3587 | AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc()); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3588 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3589 | // If the signed range is non-negative, -Wsign-compare won't fire, |
| 3590 | // but we should still check for comparisons which are always true |
| 3591 | // or false. |
| 3592 | if (signedRange.NonNegative) |
| 3593 | return CheckTrivialUnsignedComparison(S, E); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3594 | |
| 3595 | // For (in)equality comparisons, if the unsigned operand is a |
| 3596 | // constant which cannot collide with a overflowed signed operand, |
| 3597 | // then reinterpreting the signed operand as unsigned will not |
| 3598 | // change the result of the comparison. |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3599 | if (E->isEqualityOp()) { |
| 3600 | unsigned comparisonWidth = S.Context.getIntWidth(T); |
| 3601 | IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3602 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3603 | // We should never be unable to prove that the unsigned operand is |
| 3604 | // non-negative. |
| 3605 | assert(unsignedRange.NonNegative && "unsigned range includes negative?"); |
| 3606 | |
| 3607 | if (unsignedRange.Width < comparisonWidth) |
| 3608 | return; |
| 3609 | } |
| 3610 | |
| 3611 | S.Diag(E->getOperatorLoc(), diag::warn_mixed_sign_comparison) |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3612 | << LHS->getType() << RHS->getType() |
| 3613 | << LHS->getSourceRange() << RHS->getSourceRange(); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3614 | } |
| 3615 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3616 | /// Analyzes an attempt to assign the given value to a bitfield. |
| 3617 | /// |
| 3618 | /// Returns true if there was something fishy about the attempt. |
| 3619 | bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, |
| 3620 | SourceLocation InitLoc) { |
| 3621 | assert(Bitfield->isBitField()); |
| 3622 | if (Bitfield->isInvalidDecl()) |
| 3623 | return false; |
| 3624 | |
John McCall | 91b6014 | 2010-11-11 05:33:51 +0000 | [diff] [blame] | 3625 | // White-list bool bitfields. |
| 3626 | if (Bitfield->getType()->isBooleanType()) |
| 3627 | return false; |
| 3628 | |
Douglas Gregor | 46ff303 | 2011-02-04 13:09:01 +0000 | [diff] [blame] | 3629 | // Ignore value- or type-dependent expressions. |
| 3630 | if (Bitfield->getBitWidth()->isValueDependent() || |
| 3631 | Bitfield->getBitWidth()->isTypeDependent() || |
| 3632 | Init->isValueDependent() || |
| 3633 | Init->isTypeDependent()) |
| 3634 | return false; |
| 3635 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3636 | Expr *OriginalInit = Init->IgnoreParenImpCasts(); |
| 3637 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 3638 | llvm::APSInt Value; |
| 3639 | if (!OriginalInit->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects)) |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3640 | return false; |
| 3641 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3642 | unsigned OriginalWidth = Value.getBitWidth(); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3643 | unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context); |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3644 | |
| 3645 | if (OriginalWidth <= FieldWidth) |
| 3646 | return false; |
| 3647 | |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 3648 | // Compute the value which the bitfield will contain. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3649 | llvm::APSInt TruncatedValue = Value.trunc(FieldWidth); |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 3650 | TruncatedValue.setIsSigned(Bitfield->getType()->isSignedIntegerType()); |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3651 | |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 3652 | // Check whether the stored value is equal to the original value. |
| 3653 | TruncatedValue = TruncatedValue.extend(OriginalWidth); |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3654 | if (Value == TruncatedValue) |
| 3655 | return false; |
| 3656 | |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 3657 | // Special-case bitfields of width 1: booleans are naturally 0/1, and |
| 3658 | // therefore don't strictly fit into a bitfield of width 1. |
| 3659 | if (FieldWidth == 1 && Value.getBoolValue() == TruncatedValue.getBoolValue()) |
| 3660 | return false; |
| 3661 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3662 | std::string PrettyValue = Value.toString(10); |
| 3663 | std::string PrettyTrunc = TruncatedValue.toString(10); |
| 3664 | |
| 3665 | S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant) |
| 3666 | << PrettyValue << PrettyTrunc << OriginalInit->getType() |
| 3667 | << Init->getSourceRange(); |
| 3668 | |
| 3669 | return true; |
| 3670 | } |
| 3671 | |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 3672 | /// Analyze the given simple or compound assignment for warning-worthy |
| 3673 | /// operations. |
| 3674 | void AnalyzeAssignment(Sema &S, BinaryOperator *E) { |
| 3675 | // Just recurse on the LHS. |
| 3676 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 3677 | |
| 3678 | // We want to recurse on the RHS as normal unless we're assigning to |
| 3679 | // a bitfield. |
| 3680 | if (FieldDecl *Bitfield = E->getLHS()->getBitField()) { |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3681 | if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(), |
| 3682 | E->getOperatorLoc())) { |
| 3683 | // Recurse, ignoring any implicit conversions on the RHS. |
| 3684 | return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(), |
| 3685 | E->getOperatorLoc()); |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 3686 | } |
| 3687 | } |
| 3688 | |
| 3689 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
| 3690 | } |
| 3691 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3692 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 3693 | void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, |
| 3694 | SourceLocation CContext, unsigned diag) { |
| 3695 | S.Diag(E->getExprLoc(), diag) |
| 3696 | << SourceType << T << E->getSourceRange() << SourceRange(CContext); |
| 3697 | } |
| 3698 | |
Chandler Carruth | e1b02e0 | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 3699 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
| 3700 | void DiagnoseImpCast(Sema &S, Expr *E, QualType T, SourceLocation CContext, |
| 3701 | unsigned diag) { |
| 3702 | DiagnoseImpCast(S, E, E->getType(), T, CContext, diag); |
| 3703 | } |
| 3704 | |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 3705 | /// Diagnose an implicit cast from a literal expression. Does not warn when the |
| 3706 | /// cast wouldn't lose information. |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 3707 | void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T, |
| 3708 | SourceLocation CContext) { |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 3709 | // Try to convert the literal exactly to an integer. If we can, don't warn. |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 3710 | bool isExact = false; |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 3711 | const llvm::APFloat &Value = FL->getValue(); |
Jeffrey Yasskin | 3e1ef78 | 2011-07-15 17:03:07 +0000 | [diff] [blame] | 3712 | llvm::APSInt IntegerValue(S.Context.getIntWidth(T), |
| 3713 | T->hasUnsignedIntegerRepresentation()); |
| 3714 | if (Value.convertToInteger(IntegerValue, |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 3715 | llvm::APFloat::rmTowardZero, &isExact) |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 3716 | == llvm::APFloat::opOK && isExact) |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 3717 | return; |
| 3718 | |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 3719 | S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer) |
| 3720 | << FL->getType() << T << FL->getSourceRange() << SourceRange(CContext); |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3723 | std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) { |
| 3724 | if (!Range.Width) return "0"; |
| 3725 | |
| 3726 | llvm::APSInt ValueInRange = Value; |
| 3727 | ValueInRange.setIsSigned(!Range.NonNegative); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3728 | ValueInRange = ValueInRange.trunc(Range.Width); |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3729 | return ValueInRange.toString(10); |
| 3730 | } |
| 3731 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3732 | void CheckImplicitConversion(Sema &S, Expr *E, QualType T, |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3733 | SourceLocation CC, bool *ICContext = 0) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3734 | if (E->isTypeDependent() || E->isValueDependent()) return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3735 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3736 | const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr(); |
| 3737 | const Type *Target = S.Context.getCanonicalType(T).getTypePtr(); |
| 3738 | if (Source == Target) return; |
| 3739 | if (Target->isDependentType()) return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3740 | |
Chandler Carruth | 108f756 | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 3741 | // If the conversion context location is invalid don't complain. We also |
| 3742 | // don't want to emit a warning if the issue occurs from the expansion of |
| 3743 | // a system macro. The problem is that 'getSpellingLoc()' is slow, so we |
| 3744 | // delay this check as long as possible. Once we detect we are in that |
| 3745 | // scenario, we just return. |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3746 | if (CC.isInvalid()) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3747 | return; |
| 3748 | |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 3749 | // Diagnose implicit casts to bool. |
| 3750 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) { |
| 3751 | if (isa<StringLiteral>(E)) |
| 3752 | // Warn on string literal to bool. Checks for string literals in logical |
| 3753 | // expressions, for instances, assert(0 && "error here"), is prevented |
| 3754 | // by a check in AnalyzeImplicitConversions(). |
| 3755 | return DiagnoseImpCast(S, E, T, CC, |
| 3756 | diag::warn_impcast_string_literal_to_bool); |
Lang Hames | e14ca9f | 2011-12-05 20:49:50 +0000 | [diff] [blame] | 3757 | if (Source->isFunctionType()) { |
| 3758 | // Warn on function to bool. Checks free functions and static member |
| 3759 | // functions. Weakly imported functions are excluded from the check, |
| 3760 | // since it's common to test their value to check whether the linker |
| 3761 | // found a definition for them. |
| 3762 | ValueDecl *D = 0; |
| 3763 | if (DeclRefExpr* R = dyn_cast<DeclRefExpr>(E)) { |
| 3764 | D = R->getDecl(); |
| 3765 | } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) { |
| 3766 | D = M->getMemberDecl(); |
| 3767 | } |
| 3768 | |
| 3769 | if (D && !D->isWeak()) { |
Richard Trieu | 26b45d8 | 2011-12-06 04:48:01 +0000 | [diff] [blame] | 3770 | if (FunctionDecl* F = dyn_cast<FunctionDecl>(D)) { |
| 3771 | S.Diag(E->getExprLoc(), diag::warn_impcast_function_to_bool) |
| 3772 | << F << E->getSourceRange() << SourceRange(CC); |
David Blaikie | 2def773 | 2011-12-09 21:42:37 +0000 | [diff] [blame] | 3773 | S.Diag(E->getExprLoc(), diag::note_function_to_bool_silence) |
| 3774 | << FixItHint::CreateInsertion(E->getExprLoc(), "&"); |
| 3775 | QualType ReturnType; |
| 3776 | UnresolvedSet<4> NonTemplateOverloads; |
| 3777 | S.isExprCallable(*E, ReturnType, NonTemplateOverloads); |
| 3778 | if (!ReturnType.isNull() |
| 3779 | && ReturnType->isSpecificBuiltinType(BuiltinType::Bool)) |
| 3780 | S.Diag(E->getExprLoc(), diag::note_function_to_bool_call) |
| 3781 | << FixItHint::CreateInsertion( |
| 3782 | S.getPreprocessor().getLocForEndOfToken(E->getLocEnd()), "()"); |
Richard Trieu | 26b45d8 | 2011-12-06 04:48:01 +0000 | [diff] [blame] | 3783 | return; |
| 3784 | } |
Lang Hames | e14ca9f | 2011-12-05 20:49:50 +0000 | [diff] [blame] | 3785 | } |
| 3786 | } |
David Blaikie | e37cdc4 | 2011-09-29 04:06:47 +0000 | [diff] [blame] | 3787 | return; // Other casts to bool are not checked. |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 3788 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3789 | |
| 3790 | // Strip vector types. |
| 3791 | if (isa<VectorType>(Source)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3792 | if (!isa<VectorType>(Target)) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 3793 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3794 | return; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3795 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar); |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3796 | } |
Chris Lattner | b792b30 | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 3797 | |
| 3798 | // If the vector cast is cast between two vectors of the same size, it is |
| 3799 | // a bitcast, not a conversion. |
| 3800 | if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target)) |
| 3801 | return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3802 | |
| 3803 | Source = cast<VectorType>(Source)->getElementType().getTypePtr(); |
| 3804 | Target = cast<VectorType>(Target)->getElementType().getTypePtr(); |
| 3805 | } |
| 3806 | |
| 3807 | // Strip complex types. |
| 3808 | if (isa<ComplexType>(Source)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3809 | if (!isa<ComplexType>(Target)) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 3810 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3811 | return; |
| 3812 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3813 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar); |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3814 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3815 | |
| 3816 | Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); |
| 3817 | Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); |
| 3818 | } |
| 3819 | |
| 3820 | const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); |
| 3821 | const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); |
| 3822 | |
| 3823 | // If the source is floating point... |
| 3824 | if (SourceBT && SourceBT->isFloatingPoint()) { |
| 3825 | // ...and the target is floating point... |
| 3826 | if (TargetBT && TargetBT->isFloatingPoint()) { |
| 3827 | // ...then warn if we're dropping FP rank. |
| 3828 | |
| 3829 | // Builtin FP kinds are ordered by increasing FP rank. |
| 3830 | if (SourceBT->getKind() > TargetBT->getKind()) { |
| 3831 | // Don't warn about float constants that are precisely |
| 3832 | // representable in the target type. |
| 3833 | Expr::EvalResult result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3834 | if (E->EvaluateAsRValue(result, S.Context)) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3835 | // Value might be a float, a float vector, or a float complex. |
| 3836 | if (IsSameFloatAfterCast(result.Val, |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3837 | S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)), |
| 3838 | S.Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3839 | return; |
| 3840 | } |
| 3841 | |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 3842 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3843 | return; |
| 3844 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3845 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3846 | } |
| 3847 | return; |
| 3848 | } |
| 3849 | |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3850 | // If the target is integral, always warn. |
Chandler Carruth | a5b9332 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 3851 | if ((TargetBT && TargetBT->isInteger())) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 3852 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3853 | return; |
| 3854 | |
Chandler Carruth | a5b9332 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 3855 | Expr *InnerE = E->IgnoreParenImpCasts(); |
Matt Beaumont-Gay | 634c8af | 2011-09-08 22:30:47 +0000 | [diff] [blame] | 3856 | // We also want to warn on, e.g., "int i = -1.234" |
| 3857 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE)) |
| 3858 | if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus) |
| 3859 | InnerE = UOp->getSubExpr()->IgnoreParenImpCasts(); |
| 3860 | |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 3861 | if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) { |
| 3862 | DiagnoseFloatingLiteralImpCast(S, FL, T, CC); |
Chandler Carruth | a5b9332 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 3863 | } else { |
| 3864 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer); |
| 3865 | } |
| 3866 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3867 | |
| 3868 | return; |
| 3869 | } |
| 3870 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3871 | if (!Source->isIntegerType() || !Target->isIntegerType()) |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3872 | return; |
| 3873 | |
Richard Trieu | 1838ca5 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 3874 | if ((E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) |
| 3875 | == Expr::NPCK_GNUNull) && Target->isIntegerType()) { |
| 3876 | S.Diag(E->getExprLoc(), diag::warn_impcast_null_pointer_to_integer) |
| 3877 | << E->getSourceRange() << clang::SourceRange(CC); |
| 3878 | return; |
| 3879 | } |
| 3880 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3881 | IntRange SourceRange = GetExprRange(S.Context, E); |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3882 | IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3883 | |
| 3884 | if (SourceRange.Width > TargetRange.Width) { |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3885 | // If the source is a constant, use a default-on diagnostic. |
| 3886 | // TODO: this should happen for bitfield stores, too. |
| 3887 | llvm::APSInt Value(32); |
| 3888 | if (E->isIntegerConstantExpr(Value, S.Context)) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 3889 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3890 | return; |
| 3891 | |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3892 | std::string PrettySourceValue = Value.toString(10); |
| 3893 | std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); |
| 3894 | |
Ted Kremenek | 5e745da | 2011-10-22 02:37:33 +0000 | [diff] [blame] | 3895 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
| 3896 | S.PDiag(diag::warn_impcast_integer_precision_constant) |
| 3897 | << PrettySourceValue << PrettyTargetValue |
| 3898 | << E->getType() << T << E->getSourceRange() |
| 3899 | << clang::SourceRange(CC)); |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3900 | return; |
| 3901 | } |
| 3902 | |
Chris Lattner | b792b30 | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 3903 | // People want to build with -Wshorten-64-to-32 and not -Wconversion. |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 3904 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3905 | return; |
| 3906 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3907 | if (SourceRange.Width == 64 && TargetRange.Width == 32) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3908 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32); |
| 3909 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3910 | } |
| 3911 | |
| 3912 | if ((TargetRange.NonNegative && !SourceRange.NonNegative) || |
| 3913 | (!TargetRange.NonNegative && SourceRange.NonNegative && |
| 3914 | SourceRange.Width == TargetRange.Width)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3915 | |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 3916 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3917 | return; |
| 3918 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3919 | unsigned DiagID = diag::warn_impcast_integer_sign; |
| 3920 | |
| 3921 | // Traditionally, gcc has warned about this under -Wsign-compare. |
| 3922 | // We also want to warn about it in -Wconversion. |
| 3923 | // So if -Wconversion is off, use a completely identical diagnostic |
| 3924 | // in the sign-compare group. |
| 3925 | // The conditional-checking code will |
| 3926 | if (ICContext) { |
| 3927 | DiagID = diag::warn_impcast_integer_sign_conditional; |
| 3928 | *ICContext = true; |
| 3929 | } |
| 3930 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3931 | return DiagnoseImpCast(S, E, T, CC, DiagID); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 3934 | // Diagnose conversions between different enumeration types. |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 3935 | // In C, we pretend that the type of an EnumConstantDecl is its enumeration |
| 3936 | // type, to give us better diagnostics. |
| 3937 | QualType SourceType = E->getType(); |
| 3938 | if (!S.getLangOptions().CPlusPlus) { |
| 3939 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 3940 | if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 3941 | EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext()); |
| 3942 | SourceType = S.Context.getTypeDeclType(Enum); |
| 3943 | Source = S.Context.getCanonicalType(SourceType).getTypePtr(); |
| 3944 | } |
| 3945 | } |
| 3946 | |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 3947 | if (const EnumType *SourceEnum = Source->getAs<EnumType>()) |
| 3948 | if (const EnumType *TargetEnum = Target->getAs<EnumType>()) |
| 3949 | if ((SourceEnum->getDecl()->getIdentifier() || |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3950 | SourceEnum->getDecl()->getTypedefNameForAnonDecl()) && |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 3951 | (TargetEnum->getDecl()->getIdentifier() || |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3952 | TargetEnum->getDecl()->getTypedefNameForAnonDecl()) && |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3953 | SourceEnum != TargetEnum) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 3954 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3955 | return; |
| 3956 | |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 3957 | return DiagnoseImpCast(S, E, SourceType, T, CC, |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 3958 | diag::warn_impcast_different_enum_types); |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3959 | } |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 3960 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3961 | return; |
| 3962 | } |
| 3963 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3964 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T); |
| 3965 | |
| 3966 | void CheckConditionalOperand(Sema &S, Expr *E, QualType T, |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3967 | SourceLocation CC, bool &ICContext) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3968 | E = E->IgnoreParenImpCasts(); |
| 3969 | |
| 3970 | if (isa<ConditionalOperator>(E)) |
| 3971 | return CheckConditionalOperator(S, cast<ConditionalOperator>(E), T); |
| 3972 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3973 | AnalyzeImplicitConversions(S, E, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3974 | if (E->getType() != T) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3975 | return CheckImplicitConversion(S, E, T, CC, &ICContext); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3976 | return; |
| 3977 | } |
| 3978 | |
| 3979 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T) { |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3980 | SourceLocation CC = E->getQuestionLoc(); |
| 3981 | |
| 3982 | AnalyzeImplicitConversions(S, E->getCond(), CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3983 | |
| 3984 | bool Suspicious = false; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3985 | CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious); |
| 3986 | CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3987 | |
| 3988 | // If -Wconversion would have warned about either of the candidates |
| 3989 | // for a signedness conversion to the context type... |
| 3990 | if (!Suspicious) return; |
| 3991 | |
| 3992 | // ...but it's currently ignored... |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3993 | if (S.Diags.getDiagnosticLevel(diag::warn_impcast_integer_sign_conditional, |
| 3994 | CC)) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3995 | return; |
| 3996 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3997 | // ...then check whether it would have warned about either of the |
| 3998 | // candidates for a signedness conversion to the condition type. |
Richard Trieu | 5254161 | 2011-07-21 02:46:28 +0000 | [diff] [blame] | 3999 | if (E->getType() == T) return; |
| 4000 | |
| 4001 | Suspicious = false; |
| 4002 | CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(), |
| 4003 | E->getType(), CC, &Suspicious); |
| 4004 | if (!Suspicious) |
| 4005 | CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(), |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4006 | E->getType(), CC, &Suspicious); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4007 | } |
| 4008 | |
| 4009 | /// AnalyzeImplicitConversions - Find and report any interesting |
| 4010 | /// implicit conversions in the given expression. There are a couple |
| 4011 | /// of competing diagnostics here, -Wconversion and -Wsign-compare. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4012 | void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4013 | QualType T = OrigE->getType(); |
| 4014 | Expr *E = OrigE->IgnoreParenImpCasts(); |
| 4015 | |
Douglas Gregor | f8b6e15 | 2011-10-10 17:38:18 +0000 | [diff] [blame] | 4016 | if (E->isTypeDependent() || E->isValueDependent()) |
| 4017 | return; |
| 4018 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4019 | // For conditional operators, we analyze the arguments as if they |
| 4020 | // were being fed directly into the output. |
| 4021 | if (isa<ConditionalOperator>(E)) { |
| 4022 | ConditionalOperator *CO = cast<ConditionalOperator>(E); |
| 4023 | CheckConditionalOperator(S, CO, T); |
| 4024 | return; |
| 4025 | } |
| 4026 | |
| 4027 | // Go ahead and check any implicit conversions we might have skipped. |
| 4028 | // The non-canonical typecheck is just an optimization; |
| 4029 | // CheckImplicitConversion will filter out dead implicit conversions. |
| 4030 | if (E->getType() != T) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4031 | CheckImplicitConversion(S, E, T, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4032 | |
| 4033 | // Now continue drilling into this expression. |
| 4034 | |
| 4035 | // Skip past explicit casts. |
| 4036 | if (isa<ExplicitCastExpr>(E)) { |
| 4037 | E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts(); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4038 | return AnalyzeImplicitConversions(S, E, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4041 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 4042 | // Do a somewhat different check with comparison operators. |
| 4043 | if (BO->isComparisonOp()) |
| 4044 | return AnalyzeComparison(S, BO); |
| 4045 | |
Eli Friedman | 0fa0638 | 2012-01-26 23:34:06 +0000 | [diff] [blame] | 4046 | // And with simple assignments. |
| 4047 | if (BO->getOpcode() == BO_Assign) |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4048 | return AnalyzeAssignment(S, BO); |
| 4049 | } |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4050 | |
| 4051 | // These break the otherwise-useful invariant below. Fortunately, |
| 4052 | // we don't really need to recurse into them, because any internal |
| 4053 | // expressions should have been analyzed already when they were |
| 4054 | // built into statements. |
| 4055 | if (isa<StmtExpr>(E)) return; |
| 4056 | |
| 4057 | // Don't descend into unevaluated contexts. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4058 | if (isa<UnaryExprOrTypeTraitExpr>(E)) return; |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4059 | |
| 4060 | // Now just recurse over the expression's children. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4061 | CC = E->getExprLoc(); |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4062 | BinaryOperator *BO = dyn_cast<BinaryOperator>(E); |
| 4063 | bool IsLogicalOperator = BO && BO->isLogicalOp(); |
| 4064 | for (Stmt::child_range I = E->children(); I; ++I) { |
| 4065 | Expr *ChildExpr = cast<Expr>(*I); |
| 4066 | if (IsLogicalOperator && |
| 4067 | isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts())) |
| 4068 | // Ignore checking string literals that are in logical operators. |
| 4069 | continue; |
| 4070 | AnalyzeImplicitConversions(S, ChildExpr, CC); |
| 4071 | } |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4072 | } |
| 4073 | |
| 4074 | } // end anonymous namespace |
| 4075 | |
| 4076 | /// Diagnoses "dangerous" implicit conversions within the given |
| 4077 | /// expression (which is a full expression). Implements -Wconversion |
| 4078 | /// and -Wsign-compare. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4079 | /// |
| 4080 | /// \param CC the "context" location of the implicit conversion, i.e. |
| 4081 | /// the most location of the syntactic entity requiring the implicit |
| 4082 | /// conversion |
| 4083 | void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4084 | // Don't diagnose in unevaluated contexts. |
| 4085 | if (ExprEvalContexts.back().Context == Sema::Unevaluated) |
| 4086 | return; |
| 4087 | |
| 4088 | // Don't diagnose for value- or type-dependent expressions. |
| 4089 | if (E->isTypeDependent() || E->isValueDependent()) |
| 4090 | return; |
| 4091 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4092 | // Check for array bounds violations in cases where the check isn't triggered |
| 4093 | // elsewhere for other Expr types (like BinaryOperators), e.g. when an |
| 4094 | // ArraySubscriptExpr is on the RHS of a variable initialization. |
| 4095 | CheckArrayAccess(E); |
| 4096 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4097 | // This is not the right CC for (e.g.) a variable initialization. |
| 4098 | AnalyzeImplicitConversions(*this, E, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4099 | } |
| 4100 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4101 | void Sema::CheckBitFieldInitialization(SourceLocation InitLoc, |
| 4102 | FieldDecl *BitField, |
| 4103 | Expr *Init) { |
| 4104 | (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc); |
| 4105 | } |
| 4106 | |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4107 | /// CheckParmsForFunctionDef - Check that the parameters of the given |
| 4108 | /// function are appropriate for the definition of a function. This |
| 4109 | /// takes care of any checks that cannot be performed on the |
| 4110 | /// declaration itself, e.g., that the types of each of the function |
| 4111 | /// parameters are complete. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 4112 | bool Sema::CheckParmsForFunctionDef(ParmVarDecl **P, ParmVarDecl **PEnd, |
| 4113 | bool CheckParameterNames) { |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4114 | bool HasInvalidParm = false; |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 4115 | for (; P != PEnd; ++P) { |
| 4116 | ParmVarDecl *Param = *P; |
| 4117 | |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4118 | // C99 6.7.5.3p4: the parameters in a parameter type list in a |
| 4119 | // function declarator that is part of a function definition of |
| 4120 | // that function shall not have incomplete type. |
| 4121 | // |
| 4122 | // This is also C++ [dcl.fct]p6. |
| 4123 | if (!Param->isInvalidDecl() && |
| 4124 | RequireCompleteType(Param->getLocation(), Param->getType(), |
| 4125 | diag::err_typecheck_decl_incomplete_type)) { |
| 4126 | Param->setInvalidDecl(); |
| 4127 | HasInvalidParm = true; |
| 4128 | } |
| 4129 | |
| 4130 | // C99 6.9.1p5: If the declarator includes a parameter type list, the |
| 4131 | // declaration of each parameter shall include an identifier. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 4132 | if (CheckParameterNames && |
| 4133 | Param->getIdentifier() == 0 && |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4134 | !Param->isImplicit() && |
| 4135 | !getLangOptions().CPlusPlus) |
| 4136 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
Sam Weinig | d17e340 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 4137 | |
| 4138 | // C99 6.7.5.3p12: |
| 4139 | // If the function declarator is not part of a definition of that |
| 4140 | // function, parameters may have incomplete type and may use the [*] |
| 4141 | // notation in their sequences of declarator specifiers to specify |
| 4142 | // variable length array types. |
| 4143 | QualType PType = Param->getOriginalType(); |
| 4144 | if (const ArrayType *AT = Context.getAsArrayType(PType)) { |
| 4145 | if (AT->getSizeModifier() == ArrayType::Star) { |
| 4146 | // FIXME: This diagnosic should point the the '[*]' if source-location |
| 4147 | // information is added for it. |
| 4148 | Diag(Param->getLocation(), diag::err_array_star_in_function_definition); |
| 4149 | } |
| 4150 | } |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4151 | } |
| 4152 | |
| 4153 | return HasInvalidParm; |
| 4154 | } |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4155 | |
| 4156 | /// CheckCastAlign - Implements -Wcast-align, which warns when a |
| 4157 | /// pointer cast increases the alignment requirements. |
| 4158 | void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { |
| 4159 | // This is actually a lot of work to potentially be doing on every |
| 4160 | // 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] | 4161 | if (getDiagnostics().getDiagnosticLevel(diag::warn_cast_align, |
| 4162 | TRange.getBegin()) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 4163 | == DiagnosticsEngine::Ignored) |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4164 | return; |
| 4165 | |
| 4166 | // Ignore dependent types. |
| 4167 | if (T->isDependentType() || Op->getType()->isDependentType()) |
| 4168 | return; |
| 4169 | |
| 4170 | // Require that the destination be a pointer type. |
| 4171 | const PointerType *DestPtr = T->getAs<PointerType>(); |
| 4172 | if (!DestPtr) return; |
| 4173 | |
| 4174 | // If the destination has alignment 1, we're done. |
| 4175 | QualType DestPointee = DestPtr->getPointeeType(); |
| 4176 | if (DestPointee->isIncompleteType()) return; |
| 4177 | CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); |
| 4178 | if (DestAlign.isOne()) return; |
| 4179 | |
| 4180 | // Require that the source be a pointer type. |
| 4181 | const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); |
| 4182 | if (!SrcPtr) return; |
| 4183 | QualType SrcPointee = SrcPtr->getPointeeType(); |
| 4184 | |
| 4185 | // Whitelist casts from cv void*. We already implicitly |
| 4186 | // whitelisted casts to cv void*, since they have alignment 1. |
| 4187 | // Also whitelist casts involving incomplete types, which implicitly |
| 4188 | // includes 'void'. |
| 4189 | if (SrcPointee->isIncompleteType()) return; |
| 4190 | |
| 4191 | CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee); |
| 4192 | if (SrcAlign >= DestAlign) return; |
| 4193 | |
| 4194 | Diag(TRange.getBegin(), diag::warn_cast_align) |
| 4195 | << Op->getType() << T |
| 4196 | << static_cast<unsigned>(SrcAlign.getQuantity()) |
| 4197 | << static_cast<unsigned>(DestAlign.getQuantity()) |
| 4198 | << TRange << Op->getSourceRange(); |
| 4199 | } |
| 4200 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4201 | static const Type* getElementType(const Expr *BaseExpr) { |
| 4202 | const Type* EltType = BaseExpr->getType().getTypePtr(); |
| 4203 | if (EltType->isAnyPointerType()) |
| 4204 | return EltType->getPointeeType().getTypePtr(); |
| 4205 | else if (EltType->isArrayType()) |
| 4206 | return EltType->getBaseElementTypeUnsafe(); |
| 4207 | return EltType; |
| 4208 | } |
| 4209 | |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 4210 | /// \brief Check whether this array fits the idiom of a size-one tail padded |
| 4211 | /// array member of a struct. |
| 4212 | /// |
| 4213 | /// We avoid emitting out-of-bounds access warnings for such arrays as they are |
| 4214 | /// commonly used to emulate flexible arrays in C89 code. |
| 4215 | static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size, |
| 4216 | const NamedDecl *ND) { |
| 4217 | if (Size != 1 || !ND) return false; |
| 4218 | |
| 4219 | const FieldDecl *FD = dyn_cast<FieldDecl>(ND); |
| 4220 | if (!FD) return false; |
| 4221 | |
| 4222 | // Don't consider sizes resulting from macro expansions or template argument |
| 4223 | // substitution to form C89 tail-padded arrays. |
| 4224 | ConstantArrayTypeLoc TL = |
| 4225 | cast<ConstantArrayTypeLoc>(FD->getTypeSourceInfo()->getTypeLoc()); |
| 4226 | const Expr *SizeExpr = dyn_cast<IntegerLiteral>(TL.getSizeExpr()); |
| 4227 | if (!SizeExpr || SizeExpr->getExprLoc().isMacroID()) |
| 4228 | return false; |
| 4229 | |
| 4230 | const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext()); |
Matt Beaumont-Gay | 381711c | 2011-11-29 22:43:53 +0000 | [diff] [blame] | 4231 | if (!RD) return false; |
| 4232 | if (RD->isUnion()) return false; |
| 4233 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4234 | if (!CRD->isStandardLayout()) return false; |
| 4235 | } |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 4236 | |
Benjamin Kramer | 22d4fed | 2011-08-06 03:04:42 +0000 | [diff] [blame] | 4237 | // See if this is the last field decl in the record. |
| 4238 | const Decl *D = FD; |
| 4239 | while ((D = D->getNextDeclInContext())) |
| 4240 | if (isa<FieldDecl>(D)) |
| 4241 | return false; |
| 4242 | return true; |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 4243 | } |
| 4244 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4245 | void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4246 | const ArraySubscriptExpr *ASE, |
Richard Smith | 25b009a | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 4247 | bool AllowOnePastEnd, bool IndexNegated) { |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4248 | IndexExpr = IndexExpr->IgnoreParenCasts(); |
| 4249 | if (IndexExpr->isValueDependent()) |
| 4250 | return; |
| 4251 | |
Matt Beaumont-Gay | 8ef8f43 | 2011-12-12 22:35:02 +0000 | [diff] [blame] | 4252 | const Type *EffectiveType = getElementType(BaseExpr); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4253 | BaseExpr = BaseExpr->IgnoreParenCasts(); |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4254 | const ConstantArrayType *ArrayTy = |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4255 | Context.getAsConstantArrayType(BaseExpr->getType()); |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4256 | if (!ArrayTy) |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 4257 | return; |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 4258 | |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4259 | llvm::APSInt index; |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4260 | if (!IndexExpr->EvaluateAsInt(index, Context)) |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 4261 | return; |
Richard Smith | 25b009a | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 4262 | if (IndexNegated) |
| 4263 | index = -index; |
Ted Kremenek | 8fd0a5d | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 4264 | |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4265 | const NamedDecl *ND = NULL; |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4266 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
| 4267 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 4268 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4269 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4270 | |
Ted Kremenek | 9e060ca | 2011-02-23 23:06:04 +0000 | [diff] [blame] | 4271 | if (index.isUnsigned() || !index.isNegative()) { |
Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 4272 | llvm::APInt size = ArrayTy->getSize(); |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 4273 | if (!size.isStrictlyPositive()) |
| 4274 | return; |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4275 | |
| 4276 | const Type* BaseType = getElementType(BaseExpr); |
Nico Weber | de5998f | 2011-09-17 22:59:41 +0000 | [diff] [blame] | 4277 | if (BaseType != EffectiveType) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4278 | // Make sure we're comparing apples to apples when comparing index to size |
| 4279 | uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); |
| 4280 | uint64_t array_typesize = Context.getTypeSize(BaseType); |
Kaelyn Uhrain | d10f4bc | 2011-08-10 19:47:25 +0000 | [diff] [blame] | 4281 | // Handle ptrarith_typesize being zero, such as when casting to void* |
Kaelyn Uhrain | 18f1697 | 2011-08-10 18:49:28 +0000 | [diff] [blame] | 4282 | if (!ptrarith_typesize) ptrarith_typesize = 1; |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4283 | if (ptrarith_typesize != array_typesize) { |
| 4284 | // There's a cast to a different size type involved |
| 4285 | uint64_t ratio = array_typesize / ptrarith_typesize; |
| 4286 | // TODO: Be smarter about handling cases where array_typesize is not a |
| 4287 | // multiple of ptrarith_typesize |
| 4288 | if (ptrarith_typesize * ratio == array_typesize) |
| 4289 | size *= llvm::APInt(size.getBitWidth(), ratio); |
| 4290 | } |
| 4291 | } |
| 4292 | |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4293 | if (size.getBitWidth() > index.getBitWidth()) |
| 4294 | index = index.sext(size.getBitWidth()); |
Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 4295 | else if (size.getBitWidth() < index.getBitWidth()) |
| 4296 | size = size.sext(index.getBitWidth()); |
| 4297 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4298 | // For array subscripting the index must be less than size, but for pointer |
| 4299 | // arithmetic also allow the index (offset) to be equal to size since |
| 4300 | // computing the next address after the end of the array is legal and |
| 4301 | // commonly done e.g. in C++ iterators and range-based for loops. |
| 4302 | if (AllowOnePastEnd ? index.sle(size) : index.slt(size)) |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4303 | return; |
| 4304 | |
| 4305 | // Also don't warn for arrays of size 1 which are members of some |
| 4306 | // structure. These are often used to approximate flexible arrays in C89 |
| 4307 | // code. |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4308 | if (IsTailPaddedMemberArray(*this, size, ND)) |
Ted Kremenek | 8fd0a5d | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 4309 | return; |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4310 | |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4311 | // Suppress the warning if the subscript expression (as identified by the |
| 4312 | // ']' location) and the index expression are both from macro expansions |
| 4313 | // within a system header. |
| 4314 | if (ASE) { |
| 4315 | SourceLocation RBracketLoc = SourceMgr.getSpellingLoc( |
| 4316 | ASE->getRBracketLoc()); |
| 4317 | if (SourceMgr.isInSystemHeader(RBracketLoc)) { |
| 4318 | SourceLocation IndexLoc = SourceMgr.getSpellingLoc( |
| 4319 | IndexExpr->getLocStart()); |
| 4320 | if (SourceMgr.isFromSameFile(RBracketLoc, IndexLoc)) |
| 4321 | return; |
| 4322 | } |
| 4323 | } |
| 4324 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4325 | unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds; |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4326 | if (ASE) |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4327 | DiagID = diag::warn_array_index_exceeds_bounds; |
| 4328 | |
| 4329 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, |
| 4330 | PDiag(DiagID) << index.toString(10, true) |
| 4331 | << size.toString(10, true) |
| 4332 | << (unsigned)size.getLimitedValue(~0U) |
| 4333 | << IndexExpr->getSourceRange()); |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4334 | } else { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4335 | unsigned DiagID = diag::warn_array_index_precedes_bounds; |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4336 | if (!ASE) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4337 | DiagID = diag::warn_ptr_arith_precedes_bounds; |
| 4338 | if (index.isNegative()) index = -index; |
| 4339 | } |
| 4340 | |
| 4341 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, |
| 4342 | PDiag(DiagID) << index.toString(10, true) |
| 4343 | << IndexExpr->getSourceRange()); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 4344 | } |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 4345 | |
Matt Beaumont-Gay | cfbc5b5 | 2011-11-29 19:27:11 +0000 | [diff] [blame] | 4346 | if (!ND) { |
| 4347 | // Try harder to find a NamedDecl to point at in the note. |
| 4348 | while (const ArraySubscriptExpr *ASE = |
| 4349 | dyn_cast<ArraySubscriptExpr>(BaseExpr)) |
| 4350 | BaseExpr = ASE->getBase()->IgnoreParenCasts(); |
| 4351 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
| 4352 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); |
| 4353 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) |
| 4354 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); |
| 4355 | } |
| 4356 | |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 4357 | if (ND) |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4358 | DiagRuntimeBehavior(ND->getLocStart(), BaseExpr, |
| 4359 | PDiag(diag::note_array_index_out_of_bounds) |
| 4360 | << ND->getDeclName()); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 4363 | void Sema::CheckArrayAccess(const Expr *expr) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4364 | int AllowOnePastEnd = 0; |
| 4365 | while (expr) { |
| 4366 | expr = expr->IgnoreParenImpCasts(); |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 4367 | switch (expr->getStmtClass()) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4368 | case Stmt::ArraySubscriptExprClass: { |
| 4369 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr); |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4370 | CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE, |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4371 | AllowOnePastEnd > 0); |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 4372 | return; |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4373 | } |
| 4374 | case Stmt::UnaryOperatorClass: { |
| 4375 | // Only unwrap the * and & unary operators |
| 4376 | const UnaryOperator *UO = cast<UnaryOperator>(expr); |
| 4377 | expr = UO->getSubExpr(); |
| 4378 | switch (UO->getOpcode()) { |
| 4379 | case UO_AddrOf: |
| 4380 | AllowOnePastEnd++; |
| 4381 | break; |
| 4382 | case UO_Deref: |
| 4383 | AllowOnePastEnd--; |
| 4384 | break; |
| 4385 | default: |
| 4386 | return; |
| 4387 | } |
| 4388 | break; |
| 4389 | } |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 4390 | case Stmt::ConditionalOperatorClass: { |
| 4391 | const ConditionalOperator *cond = cast<ConditionalOperator>(expr); |
| 4392 | if (const Expr *lhs = cond->getLHS()) |
| 4393 | CheckArrayAccess(lhs); |
| 4394 | if (const Expr *rhs = cond->getRHS()) |
| 4395 | CheckArrayAccess(rhs); |
| 4396 | return; |
| 4397 | } |
| 4398 | default: |
| 4399 | return; |
| 4400 | } |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 4401 | } |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 4402 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4403 | |
| 4404 | //===--- CHECK: Objective-C retain cycles ----------------------------------// |
| 4405 | |
| 4406 | namespace { |
| 4407 | struct RetainCycleOwner { |
| 4408 | RetainCycleOwner() : Variable(0), Indirect(false) {} |
| 4409 | VarDecl *Variable; |
| 4410 | SourceRange Range; |
| 4411 | SourceLocation Loc; |
| 4412 | bool Indirect; |
| 4413 | |
| 4414 | void setLocsFrom(Expr *e) { |
| 4415 | Loc = e->getExprLoc(); |
| 4416 | Range = e->getSourceRange(); |
| 4417 | } |
| 4418 | }; |
| 4419 | } |
| 4420 | |
| 4421 | /// Consider whether capturing the given variable can possibly lead to |
| 4422 | /// a retain cycle. |
| 4423 | static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) { |
| 4424 | // In ARC, it's captured strongly iff the variable has __strong |
| 4425 | // lifetime. In MRR, it's captured strongly if the variable is |
| 4426 | // __block and has an appropriate type. |
| 4427 | if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 4428 | return false; |
| 4429 | |
| 4430 | owner.Variable = var; |
| 4431 | owner.setLocsFrom(ref); |
| 4432 | return true; |
| 4433 | } |
| 4434 | |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 4435 | static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4436 | while (true) { |
| 4437 | e = e->IgnoreParens(); |
| 4438 | if (CastExpr *cast = dyn_cast<CastExpr>(e)) { |
| 4439 | switch (cast->getCastKind()) { |
| 4440 | case CK_BitCast: |
| 4441 | case CK_LValueBitCast: |
| 4442 | case CK_LValueToRValue: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4443 | case CK_ARCReclaimReturnedObject: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4444 | e = cast->getSubExpr(); |
| 4445 | continue; |
| 4446 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4447 | default: |
| 4448 | return false; |
| 4449 | } |
| 4450 | } |
| 4451 | |
| 4452 | if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) { |
| 4453 | ObjCIvarDecl *ivar = ref->getDecl(); |
| 4454 | if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 4455 | return false; |
| 4456 | |
| 4457 | // Try to find a retain cycle in the base. |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 4458 | if (!findRetainCycleOwner(S, ref->getBase(), owner)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4459 | return false; |
| 4460 | |
| 4461 | if (ref->isFreeIvar()) owner.setLocsFrom(ref); |
| 4462 | owner.Indirect = true; |
| 4463 | return true; |
| 4464 | } |
| 4465 | |
| 4466 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 4467 | VarDecl *var = dyn_cast<VarDecl>(ref->getDecl()); |
| 4468 | if (!var) return false; |
| 4469 | return considerVariable(var, ref, owner); |
| 4470 | } |
| 4471 | |
| 4472 | if (BlockDeclRefExpr *ref = dyn_cast<BlockDeclRefExpr>(e)) { |
| 4473 | owner.Variable = ref->getDecl(); |
| 4474 | owner.setLocsFrom(ref); |
| 4475 | return true; |
| 4476 | } |
| 4477 | |
| 4478 | if (MemberExpr *member = dyn_cast<MemberExpr>(e)) { |
| 4479 | if (member->isArrow()) return false; |
| 4480 | |
| 4481 | // Don't count this as an indirect ownership. |
| 4482 | e = member->getBase(); |
| 4483 | continue; |
| 4484 | } |
| 4485 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4486 | if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { |
| 4487 | // Only pay attention to pseudo-objects on property references. |
| 4488 | ObjCPropertyRefExpr *pre |
| 4489 | = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm() |
| 4490 | ->IgnoreParens()); |
| 4491 | if (!pre) return false; |
| 4492 | if (pre->isImplicitProperty()) return false; |
| 4493 | ObjCPropertyDecl *property = pre->getExplicitProperty(); |
| 4494 | if (!property->isRetaining() && |
| 4495 | !(property->getPropertyIvarDecl() && |
| 4496 | property->getPropertyIvarDecl()->getType() |
| 4497 | .getObjCLifetime() == Qualifiers::OCL_Strong)) |
| 4498 | return false; |
| 4499 | |
| 4500 | owner.Indirect = true; |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 4501 | if (pre->isSuperReceiver()) { |
| 4502 | owner.Variable = S.getCurMethodDecl()->getSelfDecl(); |
| 4503 | if (!owner.Variable) |
| 4504 | return false; |
| 4505 | owner.Loc = pre->getLocation(); |
| 4506 | owner.Range = pre->getSourceRange(); |
| 4507 | return true; |
| 4508 | } |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4509 | e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase()) |
| 4510 | ->getSourceExpr()); |
| 4511 | continue; |
| 4512 | } |
| 4513 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4514 | // Array ivars? |
| 4515 | |
| 4516 | return false; |
| 4517 | } |
| 4518 | } |
| 4519 | |
| 4520 | namespace { |
| 4521 | struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> { |
| 4522 | FindCaptureVisitor(ASTContext &Context, VarDecl *variable) |
| 4523 | : EvaluatedExprVisitor<FindCaptureVisitor>(Context), |
| 4524 | Variable(variable), Capturer(0) {} |
| 4525 | |
| 4526 | VarDecl *Variable; |
| 4527 | Expr *Capturer; |
| 4528 | |
| 4529 | void VisitDeclRefExpr(DeclRefExpr *ref) { |
| 4530 | if (ref->getDecl() == Variable && !Capturer) |
| 4531 | Capturer = ref; |
| 4532 | } |
| 4533 | |
| 4534 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *ref) { |
| 4535 | if (ref->getDecl() == Variable && !Capturer) |
| 4536 | Capturer = ref; |
| 4537 | } |
| 4538 | |
| 4539 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) { |
| 4540 | if (Capturer) return; |
| 4541 | Visit(ref->getBase()); |
| 4542 | if (Capturer && ref->isFreeIvar()) |
| 4543 | Capturer = ref; |
| 4544 | } |
| 4545 | |
| 4546 | void VisitBlockExpr(BlockExpr *block) { |
| 4547 | // Look inside nested blocks |
| 4548 | if (block->getBlockDecl()->capturesVariable(Variable)) |
| 4549 | Visit(block->getBlockDecl()->getBody()); |
| 4550 | } |
| 4551 | }; |
| 4552 | } |
| 4553 | |
| 4554 | /// Check whether the given argument is a block which captures a |
| 4555 | /// variable. |
| 4556 | static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) { |
| 4557 | assert(owner.Variable && owner.Loc.isValid()); |
| 4558 | |
| 4559 | e = e->IgnoreParenCasts(); |
| 4560 | BlockExpr *block = dyn_cast<BlockExpr>(e); |
| 4561 | if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable)) |
| 4562 | return 0; |
| 4563 | |
| 4564 | FindCaptureVisitor visitor(S.Context, owner.Variable); |
| 4565 | visitor.Visit(block->getBlockDecl()->getBody()); |
| 4566 | return visitor.Capturer; |
| 4567 | } |
| 4568 | |
| 4569 | static void diagnoseRetainCycle(Sema &S, Expr *capturer, |
| 4570 | RetainCycleOwner &owner) { |
| 4571 | assert(capturer); |
| 4572 | assert(owner.Variable && owner.Loc.isValid()); |
| 4573 | |
| 4574 | S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle) |
| 4575 | << owner.Variable << capturer->getSourceRange(); |
| 4576 | S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner) |
| 4577 | << owner.Indirect << owner.Range; |
| 4578 | } |
| 4579 | |
| 4580 | /// Check for a keyword selector that starts with the word 'add' or |
| 4581 | /// 'set'. |
| 4582 | static bool isSetterLikeSelector(Selector sel) { |
| 4583 | if (sel.isUnarySelector()) return false; |
| 4584 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4585 | StringRef str = sel.getNameForSlot(0); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4586 | while (!str.empty() && str.front() == '_') str = str.substr(1); |
Ted Kremenek | 968a0ee | 2011-12-01 00:59:21 +0000 | [diff] [blame] | 4587 | if (str.startswith("set")) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4588 | str = str.substr(3); |
Ted Kremenek | 968a0ee | 2011-12-01 00:59:21 +0000 | [diff] [blame] | 4589 | else if (str.startswith("add")) { |
| 4590 | // Specially whitelist 'addOperationWithBlock:'. |
| 4591 | if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock")) |
| 4592 | return false; |
| 4593 | str = str.substr(3); |
| 4594 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4595 | else |
| 4596 | return false; |
| 4597 | |
| 4598 | if (str.empty()) return true; |
| 4599 | return !islower(str.front()); |
| 4600 | } |
| 4601 | |
| 4602 | /// Check a message send to see if it's likely to cause a retain cycle. |
| 4603 | void Sema::checkRetainCycles(ObjCMessageExpr *msg) { |
| 4604 | // Only check instance methods whose selector looks like a setter. |
| 4605 | if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector())) |
| 4606 | return; |
| 4607 | |
| 4608 | // Try to find a variable that the receiver is strongly owned by. |
| 4609 | RetainCycleOwner owner; |
| 4610 | if (msg->getReceiverKind() == ObjCMessageExpr::Instance) { |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 4611 | if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4612 | return; |
| 4613 | } else { |
| 4614 | assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance); |
| 4615 | owner.Variable = getCurMethodDecl()->getSelfDecl(); |
| 4616 | owner.Loc = msg->getSuperLoc(); |
| 4617 | owner.Range = msg->getSuperLoc(); |
| 4618 | } |
| 4619 | |
| 4620 | // Check whether the receiver is captured by any of the arguments. |
| 4621 | for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) |
| 4622 | if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) |
| 4623 | return diagnoseRetainCycle(*this, capturer, owner); |
| 4624 | } |
| 4625 | |
| 4626 | /// Check a property assign to see if it's likely to cause a retain cycle. |
| 4627 | void Sema::checkRetainCycles(Expr *receiver, Expr *argument) { |
| 4628 | RetainCycleOwner owner; |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 4629 | if (!findRetainCycleOwner(*this, receiver, owner)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4630 | return; |
| 4631 | |
| 4632 | if (Expr *capturer = findCapturingExpr(*this, argument, owner)) |
| 4633 | diagnoseRetainCycle(*this, capturer, owner); |
| 4634 | } |
| 4635 | |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 4636 | bool Sema::checkUnsafeAssigns(SourceLocation Loc, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4637 | QualType LHS, Expr *RHS) { |
| 4638 | Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime(); |
| 4639 | if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone) |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 4640 | return false; |
| 4641 | // strip off any implicit cast added to get to the one arc-specific |
| 4642 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4643 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4644 | Diag(Loc, diag::warn_arc_retained_assign) |
| 4645 | << (LT == Qualifiers::OCL_ExplicitNone) |
| 4646 | << RHS->getSourceRange(); |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 4647 | return true; |
| 4648 | } |
| 4649 | RHS = cast->getSubExpr(); |
| 4650 | } |
| 4651 | return false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4652 | } |
| 4653 | |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 4654 | void Sema::checkUnsafeExprAssigns(SourceLocation Loc, |
| 4655 | Expr *LHS, Expr *RHS) { |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 4656 | QualType LHSType; |
| 4657 | // PropertyRef on LHS type need be directly obtained from |
| 4658 | // its declaration as it has a PsuedoType. |
| 4659 | ObjCPropertyRefExpr *PRE |
| 4660 | = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens()); |
| 4661 | if (PRE && !PRE->isImplicitProperty()) { |
| 4662 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
| 4663 | if (PD) |
| 4664 | LHSType = PD->getType(); |
| 4665 | } |
| 4666 | |
| 4667 | if (LHSType.isNull()) |
| 4668 | LHSType = LHS->getType(); |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 4669 | if (checkUnsafeAssigns(Loc, LHSType, RHS)) |
| 4670 | return; |
| 4671 | Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime(); |
| 4672 | // FIXME. Check for other life times. |
| 4673 | if (LT != Qualifiers::OCL_None) |
| 4674 | return; |
| 4675 | |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 4676 | if (PRE) { |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 4677 | if (PRE->isImplicitProperty()) |
| 4678 | return; |
| 4679 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
| 4680 | if (!PD) |
| 4681 | return; |
| 4682 | |
| 4683 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 4684 | if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) { |
| 4685 | // when 'assign' attribute was not explicitly specified |
| 4686 | // by user, ignore it and rely on property type itself |
| 4687 | // for lifetime info. |
| 4688 | unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten(); |
| 4689 | if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) && |
| 4690 | LHSType->isObjCRetainableType()) |
| 4691 | return; |
| 4692 | |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 4693 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4694 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 4695 | Diag(Loc, diag::warn_arc_retained_property_assign) |
| 4696 | << RHS->getSourceRange(); |
| 4697 | return; |
| 4698 | } |
| 4699 | RHS = cast->getSubExpr(); |
| 4700 | } |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 4701 | } |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 4702 | } |
| 4703 | } |