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 | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 15 | #include "clang/Sema/SemaInternal.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 17 | #include "clang/AST/CharUnits.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/EvaluatedExprVisitor.h" |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprObjC.h" |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 24 | #include "clang/AST/StmtCXX.h" |
| 25 | #include "clang/AST/StmtObjC.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 26 | #include "clang/Analysis/Analyses/FormatString.h" |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 27 | #include "clang/Basic/CharInfo.h" |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 28 | #include "clang/Basic/TargetBuiltins.h" |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 29 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 30 | #include "clang/Lex/Preprocessor.h" |
| 31 | #include "clang/Sema/Initialization.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 32 | #include "clang/Sema/Lookup.h" |
| 33 | #include "clang/Sema/ScopeInfo.h" |
| 34 | #include "clang/Sema/Sema.h" |
| 35 | #include "llvm/ADT/BitVector.h" |
| 36 | #include "llvm/ADT/STLExtras.h" |
| 37 | #include "llvm/ADT/SmallString.h" |
Dmitri Gribenko | cb5620c | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 38 | #include "llvm/Support/ConvertUTF.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 39 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | a1f3dba | 2009-05-20 01:55:10 +0000 | [diff] [blame] | 40 | #include <limits> |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 41 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 42 | using namespace sema; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 44 | SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, |
| 45 | unsigned ByteNo) const { |
Chris Lattner | 08f92e3 | 2010-11-17 07:37:15 +0000 | [diff] [blame] | 46 | return SL->getLocationOfByte(ByteNo, PP.getSourceManager(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 47 | PP.getLangOpts(), PP.getTargetInfo()); |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 48 | } |
| 49 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 50 | /// Checks that a call expression's argument count is the desired number. |
| 51 | /// This is useful when doing custom type-checking. Returns true on error. |
| 52 | static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) { |
| 53 | unsigned argCount = call->getNumArgs(); |
| 54 | if (argCount == desiredArgCount) return false; |
| 55 | |
| 56 | if (argCount < desiredArgCount) |
| 57 | return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 58 | << 0 /*function call*/ << desiredArgCount << argCount |
| 59 | << call->getSourceRange(); |
| 60 | |
| 61 | // Highlight all the excess arguments. |
| 62 | SourceRange range(call->getArg(desiredArgCount)->getLocStart(), |
| 63 | call->getArg(argCount - 1)->getLocEnd()); |
| 64 | |
| 65 | return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args) |
| 66 | << 0 /*function call*/ << desiredArgCount << argCount |
| 67 | << call->getArg(1)->getSourceRange(); |
| 68 | } |
| 69 | |
Julien Lerouge | e593921 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 70 | /// Check that the first argument to __builtin_annotation is an integer |
| 71 | /// and the second argument is a non-wide string literal. |
| 72 | static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) { |
| 73 | if (checkArgCount(S, TheCall, 2)) |
| 74 | return true; |
| 75 | |
| 76 | // First argument should be an integer. |
| 77 | Expr *ValArg = TheCall->getArg(0); |
| 78 | QualType Ty = ValArg->getType(); |
| 79 | if (!Ty->isIntegerType()) { |
| 80 | S.Diag(ValArg->getLocStart(), diag::err_builtin_annotation_first_arg) |
| 81 | << ValArg->getSourceRange(); |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 82 | return true; |
| 83 | } |
Julien Lerouge | e593921 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 84 | |
| 85 | // Second argument should be a constant string. |
| 86 | Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts(); |
| 87 | StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg); |
| 88 | if (!Literal || !Literal->isAscii()) { |
| 89 | S.Diag(StrArg->getLocStart(), diag::err_builtin_annotation_second_arg) |
| 90 | << StrArg->getSourceRange(); |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | TheCall->setType(Ty); |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 98 | ExprResult |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 99 | Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 100 | ExprResult TheCallResult(Owned(TheCall)); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 946928f | 2010-10-01 23:23:24 +0000 | [diff] [blame] | 102 | // Find out if any arguments are required to be integer constant expressions. |
| 103 | unsigned ICEArguments = 0; |
| 104 | ASTContext::GetBuiltinTypeError Error; |
| 105 | Context.GetBuiltinType(BuiltinID, Error, &ICEArguments); |
| 106 | if (Error != ASTContext::GE_None) |
| 107 | ICEArguments = 0; // Don't diagnose previously diagnosed errors. |
| 108 | |
| 109 | // If any arguments are required to be ICE's, check and diagnose. |
| 110 | for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) { |
| 111 | // Skip arguments not required to be ICE's. |
| 112 | if ((ICEArguments & (1 << ArgNo)) == 0) continue; |
| 113 | |
| 114 | llvm::APSInt Result; |
| 115 | if (SemaBuiltinConstantArg(TheCall, ArgNo, Result)) |
| 116 | return true; |
| 117 | ICEArguments &= ~(1 << ArgNo); |
| 118 | } |
| 119 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 120 | switch (BuiltinID) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 121 | case Builtin::BI__builtin___CFStringMakeConstantString: |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 122 | assert(TheCall->getNumArgs() == 1 && |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 123 | "Wrong # arguments to builtin CFStringMakeConstantString"); |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 124 | if (CheckObjCString(TheCall->getArg(0))) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 125 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 126 | break; |
Ted Kremenek | 49ff7a1 | 2008-07-09 17:58:53 +0000 | [diff] [blame] | 127 | case Builtin::BI__builtin_stdarg_start: |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 128 | case Builtin::BI__builtin_va_start: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 129 | if (SemaBuiltinVAStart(TheCall)) |
| 130 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 131 | break; |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 132 | case Builtin::BI__builtin_isgreater: |
| 133 | case Builtin::BI__builtin_isgreaterequal: |
| 134 | case Builtin::BI__builtin_isless: |
| 135 | case Builtin::BI__builtin_islessequal: |
| 136 | case Builtin::BI__builtin_islessgreater: |
| 137 | case Builtin::BI__builtin_isunordered: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 138 | if (SemaBuiltinUnorderedCompare(TheCall)) |
| 139 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 140 | break; |
Benjamin Kramer | e771a7a | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 141 | case Builtin::BI__builtin_fpclassify: |
| 142 | if (SemaBuiltinFPClassification(TheCall, 6)) |
| 143 | return ExprError(); |
| 144 | break; |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 145 | case Builtin::BI__builtin_isfinite: |
| 146 | case Builtin::BI__builtin_isinf: |
| 147 | case Builtin::BI__builtin_isinf_sign: |
| 148 | case Builtin::BI__builtin_isnan: |
| 149 | case Builtin::BI__builtin_isnormal: |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 150 | if (SemaBuiltinFPClassification(TheCall, 1)) |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 151 | return ExprError(); |
| 152 | break; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 153 | case Builtin::BI__builtin_shufflevector: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 154 | return SemaBuiltinShuffleVector(TheCall); |
| 155 | // TheCall will be freed by the smart pointer here, but that's fine, since |
| 156 | // SemaBuiltinShuffleVector guts it, but then doesn't release it. |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 157 | case Builtin::BI__builtin_prefetch: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 158 | if (SemaBuiltinPrefetch(TheCall)) |
| 159 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 160 | break; |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 161 | case Builtin::BI__builtin_object_size: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 162 | if (SemaBuiltinObjectSize(TheCall)) |
| 163 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 164 | break; |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 165 | case Builtin::BI__builtin_longjmp: |
| 166 | if (SemaBuiltinLongjmp(TheCall)) |
| 167 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 168 | break; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 169 | |
| 170 | case Builtin::BI__builtin_classify_type: |
| 171 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 172 | TheCall->setType(Context.IntTy); |
| 173 | break; |
Chris Lattner | 75c29a0 | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 174 | case Builtin::BI__builtin_constant_p: |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 175 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 176 | TheCall->setType(Context.IntTy); |
Chris Lattner | 75c29a0 | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 177 | break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 178 | case Builtin::BI__sync_fetch_and_add: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 179 | case Builtin::BI__sync_fetch_and_add_1: |
| 180 | case Builtin::BI__sync_fetch_and_add_2: |
| 181 | case Builtin::BI__sync_fetch_and_add_4: |
| 182 | case Builtin::BI__sync_fetch_and_add_8: |
| 183 | case Builtin::BI__sync_fetch_and_add_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 184 | case Builtin::BI__sync_fetch_and_sub: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 185 | case Builtin::BI__sync_fetch_and_sub_1: |
| 186 | case Builtin::BI__sync_fetch_and_sub_2: |
| 187 | case Builtin::BI__sync_fetch_and_sub_4: |
| 188 | case Builtin::BI__sync_fetch_and_sub_8: |
| 189 | case Builtin::BI__sync_fetch_and_sub_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 190 | case Builtin::BI__sync_fetch_and_or: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 191 | case Builtin::BI__sync_fetch_and_or_1: |
| 192 | case Builtin::BI__sync_fetch_and_or_2: |
| 193 | case Builtin::BI__sync_fetch_and_or_4: |
| 194 | case Builtin::BI__sync_fetch_and_or_8: |
| 195 | case Builtin::BI__sync_fetch_and_or_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 196 | case Builtin::BI__sync_fetch_and_and: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 197 | case Builtin::BI__sync_fetch_and_and_1: |
| 198 | case Builtin::BI__sync_fetch_and_and_2: |
| 199 | case Builtin::BI__sync_fetch_and_and_4: |
| 200 | case Builtin::BI__sync_fetch_and_and_8: |
| 201 | case Builtin::BI__sync_fetch_and_and_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 202 | case Builtin::BI__sync_fetch_and_xor: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 203 | case Builtin::BI__sync_fetch_and_xor_1: |
| 204 | case Builtin::BI__sync_fetch_and_xor_2: |
| 205 | case Builtin::BI__sync_fetch_and_xor_4: |
| 206 | case Builtin::BI__sync_fetch_and_xor_8: |
| 207 | case Builtin::BI__sync_fetch_and_xor_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 208 | case Builtin::BI__sync_add_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 209 | case Builtin::BI__sync_add_and_fetch_1: |
| 210 | case Builtin::BI__sync_add_and_fetch_2: |
| 211 | case Builtin::BI__sync_add_and_fetch_4: |
| 212 | case Builtin::BI__sync_add_and_fetch_8: |
| 213 | case Builtin::BI__sync_add_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 214 | case Builtin::BI__sync_sub_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 215 | case Builtin::BI__sync_sub_and_fetch_1: |
| 216 | case Builtin::BI__sync_sub_and_fetch_2: |
| 217 | case Builtin::BI__sync_sub_and_fetch_4: |
| 218 | case Builtin::BI__sync_sub_and_fetch_8: |
| 219 | case Builtin::BI__sync_sub_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 220 | case Builtin::BI__sync_and_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 221 | case Builtin::BI__sync_and_and_fetch_1: |
| 222 | case Builtin::BI__sync_and_and_fetch_2: |
| 223 | case Builtin::BI__sync_and_and_fetch_4: |
| 224 | case Builtin::BI__sync_and_and_fetch_8: |
| 225 | case Builtin::BI__sync_and_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 226 | case Builtin::BI__sync_or_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 227 | case Builtin::BI__sync_or_and_fetch_1: |
| 228 | case Builtin::BI__sync_or_and_fetch_2: |
| 229 | case Builtin::BI__sync_or_and_fetch_4: |
| 230 | case Builtin::BI__sync_or_and_fetch_8: |
| 231 | case Builtin::BI__sync_or_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 232 | case Builtin::BI__sync_xor_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 233 | case Builtin::BI__sync_xor_and_fetch_1: |
| 234 | case Builtin::BI__sync_xor_and_fetch_2: |
| 235 | case Builtin::BI__sync_xor_and_fetch_4: |
| 236 | case Builtin::BI__sync_xor_and_fetch_8: |
| 237 | case Builtin::BI__sync_xor_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 238 | case Builtin::BI__sync_val_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 239 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 240 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 241 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 242 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 243 | case Builtin::BI__sync_val_compare_and_swap_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 244 | case Builtin::BI__sync_bool_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 245 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 246 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 247 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 248 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 249 | case Builtin::BI__sync_bool_compare_and_swap_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 250 | case Builtin::BI__sync_lock_test_and_set: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 251 | case Builtin::BI__sync_lock_test_and_set_1: |
| 252 | case Builtin::BI__sync_lock_test_and_set_2: |
| 253 | case Builtin::BI__sync_lock_test_and_set_4: |
| 254 | case Builtin::BI__sync_lock_test_and_set_8: |
| 255 | case Builtin::BI__sync_lock_test_and_set_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 256 | case Builtin::BI__sync_lock_release: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 257 | case Builtin::BI__sync_lock_release_1: |
| 258 | case Builtin::BI__sync_lock_release_2: |
| 259 | case Builtin::BI__sync_lock_release_4: |
| 260 | case Builtin::BI__sync_lock_release_8: |
| 261 | case Builtin::BI__sync_lock_release_16: |
Chris Lattner | 23aa9c8 | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 262 | case Builtin::BI__sync_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 263 | case Builtin::BI__sync_swap_1: |
| 264 | case Builtin::BI__sync_swap_2: |
| 265 | case Builtin::BI__sync_swap_4: |
| 266 | case Builtin::BI__sync_swap_8: |
| 267 | case Builtin::BI__sync_swap_16: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 268 | return SemaBuiltinAtomicOverloaded(TheCallResult); |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 269 | #define BUILTIN(ID, TYPE, ATTRS) |
| 270 | #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ |
| 271 | case Builtin::BI##ID: \ |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 272 | return SemaAtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID); |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 273 | #include "clang/Basic/Builtins.def" |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 274 | case Builtin::BI__builtin_annotation: |
Julien Lerouge | e593921 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 275 | if (SemaBuiltinAnnotation(*this, TheCall)) |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 276 | return ExprError(); |
| 277 | break; |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // Since the target specific builtins for each arch overlap, only check those |
| 281 | // of the arch we are compiling for. |
| 282 | if (BuiltinID >= Builtin::FirstTSBuiltin) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 283 | switch (Context.getTargetInfo().getTriple().getArch()) { |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 284 | case llvm::Triple::arm: |
| 285 | case llvm::Triple::thumb: |
| 286 | if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall)) |
| 287 | return ExprError(); |
| 288 | break; |
Simon Atanasyan | fad0a32 | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 289 | case llvm::Triple::mips: |
| 290 | case llvm::Triple::mipsel: |
| 291 | case llvm::Triple::mips64: |
| 292 | case llvm::Triple::mips64el: |
| 293 | if (CheckMipsBuiltinFunctionCall(BuiltinID, TheCall)) |
| 294 | return ExprError(); |
| 295 | break; |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 296 | default: |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 301 | return TheCallResult; |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 304 | // Get the valid immediate range for the specified NEON type code. |
| 305 | static unsigned RFT(unsigned t, bool shift = false) { |
Bob Wilson | da95f73 | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 306 | NeonTypeFlags Type(t); |
| 307 | int IsQuad = Type.isQuad(); |
| 308 | switch (Type.getEltType()) { |
| 309 | case NeonTypeFlags::Int8: |
| 310 | case NeonTypeFlags::Poly8: |
| 311 | return shift ? 7 : (8 << IsQuad) - 1; |
| 312 | case NeonTypeFlags::Int16: |
| 313 | case NeonTypeFlags::Poly16: |
| 314 | return shift ? 15 : (4 << IsQuad) - 1; |
| 315 | case NeonTypeFlags::Int32: |
| 316 | return shift ? 31 : (2 << IsQuad) - 1; |
| 317 | case NeonTypeFlags::Int64: |
| 318 | return shift ? 63 : (1 << IsQuad) - 1; |
| 319 | case NeonTypeFlags::Float16: |
| 320 | assert(!shift && "cannot shift float types!"); |
| 321 | return (4 << IsQuad) - 1; |
| 322 | case NeonTypeFlags::Float32: |
| 323 | assert(!shift && "cannot shift float types!"); |
| 324 | return (2 << IsQuad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 325 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 326 | llvm_unreachable("Invalid NeonTypeFlag!"); |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 329 | /// getNeonEltType - Return the QualType corresponding to the elements of |
| 330 | /// the vector type specified by the NeonTypeFlags. This is used to check |
| 331 | /// the pointer arguments for Neon load/store intrinsics. |
| 332 | static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context) { |
| 333 | switch (Flags.getEltType()) { |
| 334 | case NeonTypeFlags::Int8: |
| 335 | return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy; |
| 336 | case NeonTypeFlags::Int16: |
| 337 | return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy; |
| 338 | case NeonTypeFlags::Int32: |
| 339 | return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy; |
| 340 | case NeonTypeFlags::Int64: |
| 341 | return Flags.isUnsigned() ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 342 | case NeonTypeFlags::Poly8: |
| 343 | return Context.SignedCharTy; |
| 344 | case NeonTypeFlags::Poly16: |
| 345 | return Context.ShortTy; |
| 346 | case NeonTypeFlags::Float16: |
| 347 | return Context.UnsignedShortTy; |
| 348 | case NeonTypeFlags::Float32: |
| 349 | return Context.FloatTy; |
| 350 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 351 | llvm_unreachable("Invalid NeonTypeFlag!"); |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 354 | bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 355 | llvm::APSInt Result; |
| 356 | |
Richard Smith | f8ee6bc | 2012-08-14 01:28:02 +0000 | [diff] [blame] | 357 | uint64_t mask = 0; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 358 | unsigned TV = 0; |
Bob Wilson | 4648255 | 2011-11-16 21:32:23 +0000 | [diff] [blame] | 359 | int PtrArgNum = -1; |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 360 | bool HasConstPtr = false; |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 361 | switch (BuiltinID) { |
Nate Begeman | a23326b | 2010-06-17 04:17:01 +0000 | [diff] [blame] | 362 | #define GET_NEON_OVERLOAD_CHECK |
| 363 | #include "clang/Basic/arm_neon.inc" |
| 364 | #undef GET_NEON_OVERLOAD_CHECK |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 367 | // For NEON intrinsics which are overloaded on vector element type, validate |
| 368 | // the immediate which specifies which variant to emit. |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 369 | unsigned ImmArg = TheCall->getNumArgs()-1; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 370 | if (mask) { |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 371 | if (SemaBuiltinConstantArg(TheCall, ImmArg, Result)) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 372 | return true; |
| 373 | |
Bob Wilson | da95f73 | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 374 | TV = Result.getLimitedValue(64); |
Richard Smith | f8ee6bc | 2012-08-14 01:28:02 +0000 | [diff] [blame] | 375 | if ((TV > 63) || (mask & (1ULL << TV)) == 0) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 376 | return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code) |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 377 | << TheCall->getArg(ImmArg)->getSourceRange(); |
| 378 | } |
| 379 | |
Bob Wilson | 4648255 | 2011-11-16 21:32:23 +0000 | [diff] [blame] | 380 | if (PtrArgNum >= 0) { |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 381 | // Check that pointer arguments have the specified type. |
Bob Wilson | 4648255 | 2011-11-16 21:32:23 +0000 | [diff] [blame] | 382 | Expr *Arg = TheCall->getArg(PtrArgNum); |
| 383 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) |
| 384 | Arg = ICE->getSubExpr(); |
| 385 | ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg); |
| 386 | QualType RHSTy = RHS.get()->getType(); |
| 387 | QualType EltTy = getNeonEltType(NeonTypeFlags(TV), Context); |
| 388 | if (HasConstPtr) |
| 389 | EltTy = EltTy.withConst(); |
| 390 | QualType LHSTy = Context.getPointerType(EltTy); |
| 391 | AssignConvertType ConvTy; |
| 392 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
| 393 | if (RHS.isInvalid()) |
| 394 | return true; |
| 395 | if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), LHSTy, RHSTy, |
| 396 | RHS.get(), AA_Assigning)) |
| 397 | return true; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 398 | } |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 399 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 400 | // For NEON intrinsics which take an immediate value as part of the |
| 401 | // instruction, range check them here. |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 402 | unsigned i = 0, l = 0, u = 0; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 403 | switch (BuiltinID) { |
| 404 | default: return false; |
Nate Begeman | bb37f50 | 2010-07-29 22:48:34 +0000 | [diff] [blame] | 405 | case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break; |
| 406 | case ARM::BI__builtin_arm_usat: i = 1; u = 31; break; |
Nate Begeman | 99c40bb | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 407 | case ARM::BI__builtin_arm_vcvtr_f: |
| 408 | case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break; |
Nate Begeman | a23326b | 2010-06-17 04:17:01 +0000 | [diff] [blame] | 409 | #define GET_NEON_IMMEDIATE_CHECK |
| 410 | #include "clang/Basic/arm_neon.inc" |
| 411 | #undef GET_NEON_IMMEDIATE_CHECK |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 412 | }; |
| 413 | |
Douglas Gregor | 592a423 | 2012-06-29 01:05:22 +0000 | [diff] [blame] | 414 | // We can't check the value of a dependent argument. |
| 415 | if (TheCall->getArg(i)->isTypeDependent() || |
| 416 | TheCall->getArg(i)->isValueDependent()) |
| 417 | return false; |
| 418 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 419 | // Check that the immediate argument is actually a constant. |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 420 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 421 | return true; |
| 422 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 423 | // Range check against the upper/lower values for this isntruction. |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 424 | unsigned Val = Result.getZExtValue(); |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 425 | if (Val < l || Val > (u + l)) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 426 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 427 | << l << u+l << TheCall->getArg(i)->getSourceRange(); |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 428 | |
Nate Begeman | 99c40bb | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 429 | // FIXME: VFP Intrinsics should error if VFP not present. |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 430 | return false; |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 431 | } |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 432 | |
Simon Atanasyan | fad0a32 | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 433 | bool Sema::CheckMipsBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
| 434 | unsigned i = 0, l = 0, u = 0; |
| 435 | switch (BuiltinID) { |
| 436 | default: return false; |
| 437 | case Mips::BI__builtin_mips_wrdsp: i = 1; l = 0; u = 63; break; |
| 438 | case Mips::BI__builtin_mips_rddsp: i = 0; l = 0; u = 63; break; |
Simon Atanasyan | be22cb8 | 2012-08-27 12:29:20 +0000 | [diff] [blame] | 439 | case Mips::BI__builtin_mips_append: i = 2; l = 0; u = 31; break; |
| 440 | case Mips::BI__builtin_mips_balign: i = 2; l = 0; u = 3; break; |
| 441 | case Mips::BI__builtin_mips_precr_sra_ph_w: i = 2; l = 0; u = 31; break; |
| 442 | case Mips::BI__builtin_mips_precr_sra_r_ph_w: i = 2; l = 0; u = 31; break; |
| 443 | case Mips::BI__builtin_mips_prepend: i = 2; l = 0; u = 31; break; |
Simon Atanasyan | fad0a32 | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 444 | }; |
| 445 | |
| 446 | // We can't check the value of a dependent argument. |
| 447 | if (TheCall->getArg(i)->isTypeDependent() || |
| 448 | TheCall->getArg(i)->isValueDependent()) |
| 449 | return false; |
| 450 | |
| 451 | // Check that the immediate argument is actually a constant. |
| 452 | llvm::APSInt Result; |
| 453 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 454 | return true; |
| 455 | |
| 456 | // Range check against the upper/lower values for this instruction. |
| 457 | unsigned Val = Result.getZExtValue(); |
| 458 | if (Val < l || Val > u) |
| 459 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 460 | << l << u << TheCall->getArg(i)->getSourceRange(); |
| 461 | |
| 462 | return false; |
| 463 | } |
| 464 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 465 | /// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo |
| 466 | /// parameter with the FormatAttr's correct format_idx and firstDataArg. |
| 467 | /// Returns true when the format fits the function and the FormatStringInfo has |
| 468 | /// been populated. |
| 469 | bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember, |
| 470 | FormatStringInfo *FSI) { |
| 471 | FSI->HasVAListArg = Format->getFirstArg() == 0; |
| 472 | FSI->FormatIdx = Format->getFormatIdx() - 1; |
| 473 | FSI->FirstDataArg = FSI->HasVAListArg ? 0 : Format->getFirstArg() - 1; |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 474 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 475 | // The way the format attribute works in GCC, the implicit this argument |
| 476 | // of member functions is counted. However, it doesn't appear in our own |
| 477 | // lists, so decrement format_idx in that case. |
| 478 | if (IsCXXMember) { |
| 479 | if(FSI->FormatIdx == 0) |
| 480 | return false; |
| 481 | --FSI->FormatIdx; |
| 482 | if (FSI->FirstDataArg != 0) |
| 483 | --FSI->FirstDataArg; |
| 484 | } |
| 485 | return true; |
| 486 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 488 | /// Handles the checks for format strings, non-POD arguments to vararg |
| 489 | /// functions, and NULL arguments passed to non-NULL parameters. |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 490 | void Sema::checkCall(NamedDecl *FDecl, |
| 491 | ArrayRef<const Expr *> Args, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 492 | unsigned NumProtoArgs, |
| 493 | bool IsMemberFunction, |
| 494 | SourceLocation Loc, |
| 495 | SourceRange Range, |
| 496 | VariadicCallType CallType) { |
Jordan Rose | 66360e2 | 2012-10-02 01:49:54 +0000 | [diff] [blame] | 497 | if (CurContext->isDependentContext()) |
| 498 | return; |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 499 | |
Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 500 | // Printf and scanf checking. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 501 | bool HandledFormatString = false; |
Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 502 | for (specific_attr_iterator<FormatAttr> |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 503 | I = FDecl->specific_attr_begin<FormatAttr>(), |
| 504 | E = FDecl->specific_attr_end<FormatAttr>(); I != E ; ++I) |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 505 | if (CheckFormatArguments(*I, Args, IsMemberFunction, CallType, Loc, Range)) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 506 | HandledFormatString = true; |
| 507 | |
| 508 | // Refuse POD arguments that weren't caught by the format string |
| 509 | // checks above. |
| 510 | if (!HandledFormatString && CallType != VariadicDoesNotApply) |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 511 | for (unsigned ArgIdx = NumProtoArgs; ArgIdx < Args.size(); ++ArgIdx) { |
Ted Kremenek | 0234bfa | 2012-10-11 19:06:43 +0000 | [diff] [blame] | 512 | // Args[ArgIdx] can be null in malformed code. |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 513 | if (const Expr *Arg = Args[ArgIdx]) |
Ted Kremenek | 0234bfa | 2012-10-11 19:06:43 +0000 | [diff] [blame] | 514 | variadicArgumentPODCheck(Arg, CallType); |
| 515 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 516 | |
Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 517 | for (specific_attr_iterator<NonNullAttr> |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 518 | I = FDecl->specific_attr_begin<NonNullAttr>(), |
| 519 | E = FDecl->specific_attr_end<NonNullAttr>(); I != E; ++I) |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 520 | CheckNonNullArguments(*I, Args.data(), Loc); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 521 | |
| 522 | // Type safety checking. |
| 523 | for (specific_attr_iterator<ArgumentWithTypeTagAttr> |
| 524 | i = FDecl->specific_attr_begin<ArgumentWithTypeTagAttr>(), |
| 525 | e = FDecl->specific_attr_end<ArgumentWithTypeTagAttr>(); i != e; ++i) { |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 526 | CheckArgumentWithTypeTag(*i, Args.data()); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 527 | } |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /// CheckConstructorCall - Check a constructor call for correctness and safety |
| 531 | /// properties not enforced by the C type system. |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 532 | void Sema::CheckConstructorCall(FunctionDecl *FDecl, |
| 533 | ArrayRef<const Expr *> Args, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 534 | const FunctionProtoType *Proto, |
| 535 | SourceLocation Loc) { |
| 536 | VariadicCallType CallType = |
| 537 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 538 | checkCall(FDecl, Args, Proto->getNumArgs(), |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 539 | /*IsMemberFunction=*/true, Loc, SourceRange(), CallType); |
| 540 | } |
| 541 | |
| 542 | /// CheckFunctionCall - Check a direct function call for various correctness |
| 543 | /// and safety properties not strictly enforced by the C type system. |
| 544 | bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall, |
| 545 | const FunctionProtoType *Proto) { |
Eli Friedman | 2edcde8 | 2012-10-11 00:30:58 +0000 | [diff] [blame] | 546 | bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) && |
| 547 | isa<CXXMethodDecl>(FDecl); |
| 548 | bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) || |
| 549 | IsMemberOperatorCall; |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 550 | VariadicCallType CallType = getVariadicCallType(FDecl, Proto, |
| 551 | TheCall->getCallee()); |
| 552 | unsigned NumProtoArgs = Proto ? Proto->getNumArgs() : 0; |
Eli Friedman | 2edcde8 | 2012-10-11 00:30:58 +0000 | [diff] [blame] | 553 | Expr** Args = TheCall->getArgs(); |
| 554 | unsigned NumArgs = TheCall->getNumArgs(); |
Eli Friedman | df75b0c | 2012-10-11 00:34:15 +0000 | [diff] [blame] | 555 | if (IsMemberOperatorCall) { |
Eli Friedman | 2edcde8 | 2012-10-11 00:30:58 +0000 | [diff] [blame] | 556 | // If this is a call to a member operator, hide the first argument |
| 557 | // from checkCall. |
| 558 | // FIXME: Our choice of AST representation here is less than ideal. |
| 559 | ++Args; |
| 560 | --NumArgs; |
| 561 | } |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 562 | checkCall(FDecl, llvm::makeArrayRef<const Expr *>(Args, NumArgs), |
| 563 | NumProtoArgs, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 564 | IsMemberFunction, TheCall->getRParenLoc(), |
| 565 | TheCall->getCallee()->getSourceRange(), CallType); |
| 566 | |
| 567 | IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
| 568 | // None of the checks below are needed for functions that don't have |
| 569 | // simple names (e.g., C++ conversion functions). |
| 570 | if (!FnInfo) |
| 571 | return false; |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 572 | |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 573 | unsigned CMId = FDecl->getMemoryFunctionKind(); |
| 574 | if (CMId == 0) |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 575 | return false; |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 576 | |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 577 | // Handle memory setting and copying functions. |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 578 | if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat) |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 579 | CheckStrlcpycatArguments(TheCall, FnInfo); |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 580 | else if (CMId == Builtin::BIstrncat) |
| 581 | CheckStrncatArguments(TheCall, FnInfo); |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 582 | else |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 583 | CheckMemaccessArguments(TheCall, CMId, FnInfo); |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 584 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 585 | return false; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 588 | bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac, |
| 589 | Expr **Args, unsigned NumArgs) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 590 | VariadicCallType CallType = |
| 591 | Method->isVariadic() ? VariadicMethod : VariadicDoesNotApply; |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 592 | |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 593 | checkCall(Method, llvm::makeArrayRef<const Expr *>(Args, NumArgs), |
| 594 | Method->param_size(), |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 595 | /*IsMemberFunction=*/false, |
| 596 | lbrac, Method->getSourceRange(), CallType); |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 597 | |
| 598 | return false; |
| 599 | } |
| 600 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 601 | bool Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall, |
| 602 | const FunctionProtoType *Proto) { |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 603 | const VarDecl *V = dyn_cast<VarDecl>(NDecl); |
| 604 | if (!V) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 605 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 606 | |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 607 | QualType Ty = V->getType(); |
| 608 | if (!Ty->isBlockPointerType()) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 609 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 611 | VariadicCallType CallType = |
| 612 | Proto && Proto->isVariadic() ? VariadicBlock : VariadicDoesNotApply ; |
| 613 | unsigned NumProtoArgs = Proto ? Proto->getNumArgs() : 0; |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 614 | |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 615 | checkCall(NDecl, |
| 616 | llvm::makeArrayRef<const Expr *>(TheCall->getArgs(), |
| 617 | TheCall->getNumArgs()), |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 618 | NumProtoArgs, /*IsMemberFunction=*/false, |
| 619 | TheCall->getRParenLoc(), |
| 620 | TheCall->getCallee()->getSourceRange(), CallType); |
| 621 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 622 | return false; |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 625 | ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult, |
| 626 | AtomicExpr::AtomicOp Op) { |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 627 | CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); |
| 628 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 629 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 630 | // All these operations take one of the following forms: |
| 631 | enum { |
| 632 | // C __c11_atomic_init(A *, C) |
| 633 | Init, |
| 634 | // C __c11_atomic_load(A *, int) |
| 635 | Load, |
| 636 | // void __atomic_load(A *, CP, int) |
| 637 | Copy, |
| 638 | // C __c11_atomic_add(A *, M, int) |
| 639 | Arithmetic, |
| 640 | // C __atomic_exchange_n(A *, CP, int) |
| 641 | Xchg, |
| 642 | // void __atomic_exchange(A *, C *, CP, int) |
| 643 | GNUXchg, |
| 644 | // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int) |
| 645 | C11CmpXchg, |
| 646 | // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int) |
| 647 | GNUCmpXchg |
| 648 | } Form = Init; |
| 649 | const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 4, 5, 6 }; |
| 650 | const unsigned NumVals[] = { 1, 0, 1, 1, 1, 2, 2, 3 }; |
| 651 | // where: |
| 652 | // C is an appropriate type, |
| 653 | // A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins, |
| 654 | // CP is C for __c11 builtins and GNU _n builtins and is C * otherwise, |
| 655 | // M is C if C is an integer, and ptrdiff_t if C is a pointer, and |
| 656 | // the int parameters are for orderings. |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 657 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 658 | assert(AtomicExpr::AO__c11_atomic_init == 0 && |
| 659 | AtomicExpr::AO__c11_atomic_fetch_xor + 1 == AtomicExpr::AO__atomic_load |
| 660 | && "need to update code for modified C11 atomics"); |
| 661 | bool IsC11 = Op >= AtomicExpr::AO__c11_atomic_init && |
| 662 | Op <= AtomicExpr::AO__c11_atomic_fetch_xor; |
| 663 | bool IsN = Op == AtomicExpr::AO__atomic_load_n || |
| 664 | Op == AtomicExpr::AO__atomic_store_n || |
| 665 | Op == AtomicExpr::AO__atomic_exchange_n || |
| 666 | Op == AtomicExpr::AO__atomic_compare_exchange_n; |
| 667 | bool IsAddSub = false; |
| 668 | |
| 669 | switch (Op) { |
| 670 | case AtomicExpr::AO__c11_atomic_init: |
| 671 | Form = Init; |
| 672 | break; |
| 673 | |
| 674 | case AtomicExpr::AO__c11_atomic_load: |
| 675 | case AtomicExpr::AO__atomic_load_n: |
| 676 | Form = Load; |
| 677 | break; |
| 678 | |
| 679 | case AtomicExpr::AO__c11_atomic_store: |
| 680 | case AtomicExpr::AO__atomic_load: |
| 681 | case AtomicExpr::AO__atomic_store: |
| 682 | case AtomicExpr::AO__atomic_store_n: |
| 683 | Form = Copy; |
| 684 | break; |
| 685 | |
| 686 | case AtomicExpr::AO__c11_atomic_fetch_add: |
| 687 | case AtomicExpr::AO__c11_atomic_fetch_sub: |
| 688 | case AtomicExpr::AO__atomic_fetch_add: |
| 689 | case AtomicExpr::AO__atomic_fetch_sub: |
| 690 | case AtomicExpr::AO__atomic_add_fetch: |
| 691 | case AtomicExpr::AO__atomic_sub_fetch: |
| 692 | IsAddSub = true; |
| 693 | // Fall through. |
| 694 | case AtomicExpr::AO__c11_atomic_fetch_and: |
| 695 | case AtomicExpr::AO__c11_atomic_fetch_or: |
| 696 | case AtomicExpr::AO__c11_atomic_fetch_xor: |
| 697 | case AtomicExpr::AO__atomic_fetch_and: |
| 698 | case AtomicExpr::AO__atomic_fetch_or: |
| 699 | case AtomicExpr::AO__atomic_fetch_xor: |
Richard Smith | 51b9240 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 700 | case AtomicExpr::AO__atomic_fetch_nand: |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 701 | case AtomicExpr::AO__atomic_and_fetch: |
| 702 | case AtomicExpr::AO__atomic_or_fetch: |
| 703 | case AtomicExpr::AO__atomic_xor_fetch: |
Richard Smith | 51b9240 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 704 | case AtomicExpr::AO__atomic_nand_fetch: |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 705 | Form = Arithmetic; |
| 706 | break; |
| 707 | |
| 708 | case AtomicExpr::AO__c11_atomic_exchange: |
| 709 | case AtomicExpr::AO__atomic_exchange_n: |
| 710 | Form = Xchg; |
| 711 | break; |
| 712 | |
| 713 | case AtomicExpr::AO__atomic_exchange: |
| 714 | Form = GNUXchg; |
| 715 | break; |
| 716 | |
| 717 | case AtomicExpr::AO__c11_atomic_compare_exchange_strong: |
| 718 | case AtomicExpr::AO__c11_atomic_compare_exchange_weak: |
| 719 | Form = C11CmpXchg; |
| 720 | break; |
| 721 | |
| 722 | case AtomicExpr::AO__atomic_compare_exchange: |
| 723 | case AtomicExpr::AO__atomic_compare_exchange_n: |
| 724 | Form = GNUCmpXchg; |
| 725 | break; |
| 726 | } |
| 727 | |
| 728 | // Check we have the right number of arguments. |
| 729 | if (TheCall->getNumArgs() < NumArgs[Form]) { |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 730 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 731 | << 0 << NumArgs[Form] << TheCall->getNumArgs() |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 732 | << TheCall->getCallee()->getSourceRange(); |
| 733 | return ExprError(); |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 734 | } else if (TheCall->getNumArgs() > NumArgs[Form]) { |
| 735 | Diag(TheCall->getArg(NumArgs[Form])->getLocStart(), |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 736 | diag::err_typecheck_call_too_many_args) |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 737 | << 0 << NumArgs[Form] << TheCall->getNumArgs() |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 738 | << TheCall->getCallee()->getSourceRange(); |
| 739 | return ExprError(); |
| 740 | } |
| 741 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 742 | // Inspect the first argument of the atomic operation. |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 743 | Expr *Ptr = TheCall->getArg(0); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 744 | Ptr = DefaultFunctionArrayLvalueConversion(Ptr).get(); |
| 745 | const PointerType *pointerType = Ptr->getType()->getAs<PointerType>(); |
| 746 | if (!pointerType) { |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 747 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 748 | << Ptr->getType() << Ptr->getSourceRange(); |
| 749 | return ExprError(); |
| 750 | } |
| 751 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 752 | // For a __c11 builtin, this should be a pointer to an _Atomic type. |
| 753 | QualType AtomTy = pointerType->getPointeeType(); // 'A' |
| 754 | QualType ValType = AtomTy; // 'C' |
| 755 | if (IsC11) { |
| 756 | if (!AtomTy->isAtomicType()) { |
| 757 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic) |
| 758 | << Ptr->getType() << Ptr->getSourceRange(); |
| 759 | return ExprError(); |
| 760 | } |
Richard Smith | bc57b10 | 2012-09-15 06:09:58 +0000 | [diff] [blame] | 761 | if (AtomTy.isConstQualified()) { |
| 762 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_non_const_atomic) |
| 763 | << Ptr->getType() << Ptr->getSourceRange(); |
| 764 | return ExprError(); |
| 765 | } |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 766 | ValType = AtomTy->getAs<AtomicType>()->getValueType(); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 767 | } |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 768 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 769 | // For an arithmetic operation, the implied arithmetic must be well-formed. |
| 770 | if (Form == Arithmetic) { |
| 771 | // gcc does not enforce these rules for GNU atomics, but we do so for sanity. |
| 772 | if (IsAddSub && !ValType->isIntegerType() && !ValType->isPointerType()) { |
| 773 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) |
| 774 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 775 | return ExprError(); |
| 776 | } |
| 777 | if (!IsAddSub && !ValType->isIntegerType()) { |
| 778 | Diag(DRE->getLocStart(), diag::err_atomic_op_bitwise_needs_atomic_int) |
| 779 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 780 | return ExprError(); |
| 781 | } |
| 782 | } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) { |
| 783 | // For __atomic_*_n operations, the value type must be a scalar integral or |
| 784 | // pointer type which is 1, 2, 4, 8 or 16 bytes in length. |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 785 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 786 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 787 | return ExprError(); |
| 788 | } |
| 789 | |
| 790 | if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context)) { |
| 791 | // For GNU atomics, require a trivially-copyable type. This is not part of |
| 792 | // the GNU atomics specification, but we enforce it for sanity. |
| 793 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_trivial_copy) |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 794 | << Ptr->getType() << Ptr->getSourceRange(); |
| 795 | return ExprError(); |
| 796 | } |
| 797 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 798 | // FIXME: For any builtin other than a load, the ValType must not be |
| 799 | // const-qualified. |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 800 | |
| 801 | switch (ValType.getObjCLifetime()) { |
| 802 | case Qualifiers::OCL_None: |
| 803 | case Qualifiers::OCL_ExplicitNone: |
| 804 | // okay |
| 805 | break; |
| 806 | |
| 807 | case Qualifiers::OCL_Weak: |
| 808 | case Qualifiers::OCL_Strong: |
| 809 | case Qualifiers::OCL_Autoreleasing: |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 810 | // FIXME: Can this happen? By this point, ValType should be known |
| 811 | // to be trivially copyable. |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 812 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
| 813 | << ValType << Ptr->getSourceRange(); |
| 814 | return ExprError(); |
| 815 | } |
| 816 | |
| 817 | QualType ResultType = ValType; |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 818 | if (Form == Copy || Form == GNUXchg || Form == Init) |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 819 | ResultType = Context.VoidTy; |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 820 | else if (Form == C11CmpXchg || Form == GNUCmpXchg) |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 821 | ResultType = Context.BoolTy; |
| 822 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 823 | // The type of a parameter passed 'by value'. In the GNU atomics, such |
| 824 | // arguments are actually passed as pointers. |
| 825 | QualType ByValType = ValType; // 'CP' |
| 826 | if (!IsC11 && !IsN) |
| 827 | ByValType = Ptr->getType(); |
| 828 | |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 829 | // The first argument --- the pointer --- has a fixed type; we |
| 830 | // deduce the types of the rest of the arguments accordingly. Walk |
| 831 | // the remaining arguments, converting them to the deduced value type. |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 832 | for (unsigned i = 1; i != NumArgs[Form]; ++i) { |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 833 | QualType Ty; |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 834 | if (i < NumVals[Form] + 1) { |
| 835 | switch (i) { |
| 836 | case 1: |
| 837 | // The second argument is the non-atomic operand. For arithmetic, this |
| 838 | // is always passed by value, and for a compare_exchange it is always |
| 839 | // passed by address. For the rest, GNU uses by-address and C11 uses |
| 840 | // by-value. |
| 841 | assert(Form != Load); |
| 842 | if (Form == Init || (Form == Arithmetic && ValType->isIntegerType())) |
| 843 | Ty = ValType; |
| 844 | else if (Form == Copy || Form == Xchg) |
| 845 | Ty = ByValType; |
| 846 | else if (Form == Arithmetic) |
| 847 | Ty = Context.getPointerDiffType(); |
| 848 | else |
| 849 | Ty = Context.getPointerType(ValType.getUnqualifiedType()); |
| 850 | break; |
| 851 | case 2: |
| 852 | // The third argument to compare_exchange / GNU exchange is a |
| 853 | // (pointer to a) desired value. |
| 854 | Ty = ByValType; |
| 855 | break; |
| 856 | case 3: |
| 857 | // The fourth argument to GNU compare_exchange is a 'weak' flag. |
| 858 | Ty = Context.BoolTy; |
| 859 | break; |
| 860 | } |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 861 | } else { |
| 862 | // The order(s) are always converted to int. |
| 863 | Ty = Context.IntTy; |
| 864 | } |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 865 | |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 866 | InitializedEntity Entity = |
| 867 | InitializedEntity::InitializeParameter(Context, Ty, false); |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 868 | ExprResult Arg = TheCall->getArg(i); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 869 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 870 | if (Arg.isInvalid()) |
| 871 | return true; |
| 872 | TheCall->setArg(i, Arg.get()); |
| 873 | } |
| 874 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 875 | // Permute the arguments into a 'consistent' order. |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 876 | SmallVector<Expr*, 5> SubExprs; |
| 877 | SubExprs.push_back(Ptr); |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 878 | switch (Form) { |
| 879 | case Init: |
| 880 | // Note, AtomicExpr::getVal1() has a special case for this atomic. |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 881 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 882 | break; |
| 883 | case Load: |
| 884 | SubExprs.push_back(TheCall->getArg(1)); // Order |
| 885 | break; |
| 886 | case Copy: |
| 887 | case Arithmetic: |
| 888 | case Xchg: |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 889 | SubExprs.push_back(TheCall->getArg(2)); // Order |
| 890 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 891 | break; |
| 892 | case GNUXchg: |
| 893 | // Note, AtomicExpr::getVal2() has a special case for this atomic. |
| 894 | SubExprs.push_back(TheCall->getArg(3)); // Order |
| 895 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
| 896 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
| 897 | break; |
| 898 | case C11CmpXchg: |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 899 | SubExprs.push_back(TheCall->getArg(3)); // Order |
| 900 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 901 | SubExprs.push_back(TheCall->getArg(4)); // OrderFail |
David Chisnall | 2ebb98a | 2012-03-29 17:58:59 +0000 | [diff] [blame] | 902 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 903 | break; |
| 904 | case GNUCmpXchg: |
| 905 | SubExprs.push_back(TheCall->getArg(4)); // Order |
| 906 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
| 907 | SubExprs.push_back(TheCall->getArg(5)); // OrderFail |
| 908 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
| 909 | SubExprs.push_back(TheCall->getArg(3)); // Weak |
| 910 | break; |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 911 | } |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 912 | |
| 913 | return Owned(new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 914 | SubExprs, ResultType, Op, |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 915 | TheCall->getRParenLoc())); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 919 | /// checkBuiltinArgument - Given a call to a builtin function, perform |
| 920 | /// normal type-checking on the given argument, updating the call in |
| 921 | /// place. This is useful when a builtin function requires custom |
| 922 | /// type-checking for some of its arguments but not necessarily all of |
| 923 | /// them. |
| 924 | /// |
| 925 | /// Returns true on error. |
| 926 | static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) { |
| 927 | FunctionDecl *Fn = E->getDirectCallee(); |
| 928 | assert(Fn && "builtin call without direct callee!"); |
| 929 | |
| 930 | ParmVarDecl *Param = Fn->getParamDecl(ArgIndex); |
| 931 | InitializedEntity Entity = |
| 932 | InitializedEntity::InitializeParameter(S.Context, Param); |
| 933 | |
| 934 | ExprResult Arg = E->getArg(0); |
| 935 | Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 936 | if (Arg.isInvalid()) |
| 937 | return true; |
| 938 | |
| 939 | E->setArg(ArgIndex, Arg.take()); |
| 940 | return false; |
| 941 | } |
| 942 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 943 | /// SemaBuiltinAtomicOverloaded - We have a call to a function like |
| 944 | /// __sync_fetch_and_add, which is an overloaded function based on the pointer |
| 945 | /// type of its first argument. The main ActOnCallExpr routines have already |
| 946 | /// promoted the types of arguments because all of these calls are prototyped as |
| 947 | /// void(...). |
| 948 | /// |
| 949 | /// This function goes through and does final semantic checking for these |
| 950 | /// builtins, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 951 | ExprResult |
| 952 | Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) { |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 953 | CallExpr *TheCall = (CallExpr *)TheCallResult.get(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 954 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 955 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 956 | |
| 957 | // 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] | 958 | if (TheCall->getNumArgs() < 1) { |
| 959 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 960 | << 0 << 1 << TheCall->getNumArgs() |
| 961 | << TheCall->getCallee()->getSourceRange(); |
| 962 | return ExprError(); |
| 963 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 964 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 965 | // Inspect the first argument of the atomic builtin. This should always be |
| 966 | // a pointer type, whose element is an integral scalar or pointer type. |
| 967 | // Because it is a pointer type, we don't have to worry about any implicit |
| 968 | // casts here. |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 969 | // FIXME: We don't allow floating point scalars as input. |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 970 | Expr *FirstArg = TheCall->getArg(0); |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 971 | ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg); |
| 972 | if (FirstArgResult.isInvalid()) |
| 973 | return ExprError(); |
| 974 | FirstArg = FirstArgResult.take(); |
| 975 | TheCall->setArg(0, FirstArg); |
| 976 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 977 | const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>(); |
| 978 | if (!pointerType) { |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 979 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
| 980 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 981 | return ExprError(); |
| 982 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 983 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 984 | QualType ValType = pointerType->getPointeeType(); |
Chris Lattner | dd5fa7a | 2010-09-17 21:12:38 +0000 | [diff] [blame] | 985 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 986 | !ValType->isBlockPointerType()) { |
| 987 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr) |
| 988 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 989 | return ExprError(); |
| 990 | } |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 991 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 992 | switch (ValType.getObjCLifetime()) { |
| 993 | case Qualifiers::OCL_None: |
| 994 | case Qualifiers::OCL_ExplicitNone: |
| 995 | // okay |
| 996 | break; |
| 997 | |
| 998 | case Qualifiers::OCL_Weak: |
| 999 | case Qualifiers::OCL_Strong: |
| 1000 | case Qualifiers::OCL_Autoreleasing: |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 1001 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1002 | << ValType << FirstArg->getSourceRange(); |
| 1003 | return ExprError(); |
| 1004 | } |
| 1005 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1006 | // Strip any qualifiers off ValType. |
| 1007 | ValType = ValType.getUnqualifiedType(); |
| 1008 | |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1009 | // The majority of builtins return a value, but a few have special return |
| 1010 | // types, so allow them to override appropriately below. |
| 1011 | QualType ResultType = ValType; |
| 1012 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1013 | // We need to figure out which concrete builtin this maps onto. For example, |
| 1014 | // __sync_fetch_and_add with a 2 byte object turns into |
| 1015 | // __sync_fetch_and_add_2. |
| 1016 | #define BUILTIN_ROW(x) \ |
| 1017 | { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ |
| 1018 | Builtin::BI##x##_8, Builtin::BI##x##_16 } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1019 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1020 | static const unsigned BuiltinIndices[][5] = { |
| 1021 | BUILTIN_ROW(__sync_fetch_and_add), |
| 1022 | BUILTIN_ROW(__sync_fetch_and_sub), |
| 1023 | BUILTIN_ROW(__sync_fetch_and_or), |
| 1024 | BUILTIN_ROW(__sync_fetch_and_and), |
| 1025 | BUILTIN_ROW(__sync_fetch_and_xor), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1026 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1027 | BUILTIN_ROW(__sync_add_and_fetch), |
| 1028 | BUILTIN_ROW(__sync_sub_and_fetch), |
| 1029 | BUILTIN_ROW(__sync_and_and_fetch), |
| 1030 | BUILTIN_ROW(__sync_or_and_fetch), |
| 1031 | BUILTIN_ROW(__sync_xor_and_fetch), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1033 | BUILTIN_ROW(__sync_val_compare_and_swap), |
| 1034 | BUILTIN_ROW(__sync_bool_compare_and_swap), |
| 1035 | BUILTIN_ROW(__sync_lock_test_and_set), |
Chris Lattner | 23aa9c8 | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 1036 | BUILTIN_ROW(__sync_lock_release), |
| 1037 | BUILTIN_ROW(__sync_swap) |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1038 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1039 | #undef BUILTIN_ROW |
| 1040 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1041 | // Determine the index of the size. |
| 1042 | unsigned SizeIndex; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1043 | switch (Context.getTypeSizeInChars(ValType).getQuantity()) { |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1044 | case 1: SizeIndex = 0; break; |
| 1045 | case 2: SizeIndex = 1; break; |
| 1046 | case 4: SizeIndex = 2; break; |
| 1047 | case 8: SizeIndex = 3; break; |
| 1048 | case 16: SizeIndex = 4; break; |
| 1049 | default: |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1050 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size) |
| 1051 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 1052 | return ExprError(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1053 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1054 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1055 | // Each of these builtins has one pointer argument, followed by some number of |
| 1056 | // values (0, 1 or 2) followed by a potentially empty varags list of stuff |
| 1057 | // that we ignore. Find out which row of BuiltinIndices to read from as well |
| 1058 | // as the number of fixed args. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 1059 | unsigned BuiltinID = FDecl->getBuiltinID(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1060 | unsigned BuiltinIndex, NumFixed = 1; |
| 1061 | switch (BuiltinID) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1062 | default: llvm_unreachable("Unknown overloaded atomic builtin!"); |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1063 | case Builtin::BI__sync_fetch_and_add: |
| 1064 | case Builtin::BI__sync_fetch_and_add_1: |
| 1065 | case Builtin::BI__sync_fetch_and_add_2: |
| 1066 | case Builtin::BI__sync_fetch_and_add_4: |
| 1067 | case Builtin::BI__sync_fetch_and_add_8: |
| 1068 | case Builtin::BI__sync_fetch_and_add_16: |
| 1069 | BuiltinIndex = 0; |
| 1070 | break; |
| 1071 | |
| 1072 | case Builtin::BI__sync_fetch_and_sub: |
| 1073 | case Builtin::BI__sync_fetch_and_sub_1: |
| 1074 | case Builtin::BI__sync_fetch_and_sub_2: |
| 1075 | case Builtin::BI__sync_fetch_and_sub_4: |
| 1076 | case Builtin::BI__sync_fetch_and_sub_8: |
| 1077 | case Builtin::BI__sync_fetch_and_sub_16: |
| 1078 | BuiltinIndex = 1; |
| 1079 | break; |
| 1080 | |
| 1081 | case Builtin::BI__sync_fetch_and_or: |
| 1082 | case Builtin::BI__sync_fetch_and_or_1: |
| 1083 | case Builtin::BI__sync_fetch_and_or_2: |
| 1084 | case Builtin::BI__sync_fetch_and_or_4: |
| 1085 | case Builtin::BI__sync_fetch_and_or_8: |
| 1086 | case Builtin::BI__sync_fetch_and_or_16: |
| 1087 | BuiltinIndex = 2; |
| 1088 | break; |
| 1089 | |
| 1090 | case Builtin::BI__sync_fetch_and_and: |
| 1091 | case Builtin::BI__sync_fetch_and_and_1: |
| 1092 | case Builtin::BI__sync_fetch_and_and_2: |
| 1093 | case Builtin::BI__sync_fetch_and_and_4: |
| 1094 | case Builtin::BI__sync_fetch_and_and_8: |
| 1095 | case Builtin::BI__sync_fetch_and_and_16: |
| 1096 | BuiltinIndex = 3; |
| 1097 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1098 | |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1099 | case Builtin::BI__sync_fetch_and_xor: |
| 1100 | case Builtin::BI__sync_fetch_and_xor_1: |
| 1101 | case Builtin::BI__sync_fetch_and_xor_2: |
| 1102 | case Builtin::BI__sync_fetch_and_xor_4: |
| 1103 | case Builtin::BI__sync_fetch_and_xor_8: |
| 1104 | case Builtin::BI__sync_fetch_and_xor_16: |
| 1105 | BuiltinIndex = 4; |
| 1106 | break; |
| 1107 | |
| 1108 | case Builtin::BI__sync_add_and_fetch: |
| 1109 | case Builtin::BI__sync_add_and_fetch_1: |
| 1110 | case Builtin::BI__sync_add_and_fetch_2: |
| 1111 | case Builtin::BI__sync_add_and_fetch_4: |
| 1112 | case Builtin::BI__sync_add_and_fetch_8: |
| 1113 | case Builtin::BI__sync_add_and_fetch_16: |
| 1114 | BuiltinIndex = 5; |
| 1115 | break; |
| 1116 | |
| 1117 | case Builtin::BI__sync_sub_and_fetch: |
| 1118 | case Builtin::BI__sync_sub_and_fetch_1: |
| 1119 | case Builtin::BI__sync_sub_and_fetch_2: |
| 1120 | case Builtin::BI__sync_sub_and_fetch_4: |
| 1121 | case Builtin::BI__sync_sub_and_fetch_8: |
| 1122 | case Builtin::BI__sync_sub_and_fetch_16: |
| 1123 | BuiltinIndex = 6; |
| 1124 | break; |
| 1125 | |
| 1126 | case Builtin::BI__sync_and_and_fetch: |
| 1127 | case Builtin::BI__sync_and_and_fetch_1: |
| 1128 | case Builtin::BI__sync_and_and_fetch_2: |
| 1129 | case Builtin::BI__sync_and_and_fetch_4: |
| 1130 | case Builtin::BI__sync_and_and_fetch_8: |
| 1131 | case Builtin::BI__sync_and_and_fetch_16: |
| 1132 | BuiltinIndex = 7; |
| 1133 | break; |
| 1134 | |
| 1135 | case Builtin::BI__sync_or_and_fetch: |
| 1136 | case Builtin::BI__sync_or_and_fetch_1: |
| 1137 | case Builtin::BI__sync_or_and_fetch_2: |
| 1138 | case Builtin::BI__sync_or_and_fetch_4: |
| 1139 | case Builtin::BI__sync_or_and_fetch_8: |
| 1140 | case Builtin::BI__sync_or_and_fetch_16: |
| 1141 | BuiltinIndex = 8; |
| 1142 | break; |
| 1143 | |
| 1144 | case Builtin::BI__sync_xor_and_fetch: |
| 1145 | case Builtin::BI__sync_xor_and_fetch_1: |
| 1146 | case Builtin::BI__sync_xor_and_fetch_2: |
| 1147 | case Builtin::BI__sync_xor_and_fetch_4: |
| 1148 | case Builtin::BI__sync_xor_and_fetch_8: |
| 1149 | case Builtin::BI__sync_xor_and_fetch_16: |
| 1150 | BuiltinIndex = 9; |
| 1151 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1152 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1153 | case Builtin::BI__sync_val_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1154 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 1155 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 1156 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 1157 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 1158 | case Builtin::BI__sync_val_compare_and_swap_16: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 1159 | BuiltinIndex = 10; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1160 | NumFixed = 2; |
| 1161 | break; |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1162 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1163 | case Builtin::BI__sync_bool_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1164 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 1165 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 1166 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 1167 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 1168 | case Builtin::BI__sync_bool_compare_and_swap_16: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 1169 | BuiltinIndex = 11; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1170 | NumFixed = 2; |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1171 | ResultType = Context.BoolTy; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1172 | break; |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1173 | |
| 1174 | case Builtin::BI__sync_lock_test_and_set: |
| 1175 | case Builtin::BI__sync_lock_test_and_set_1: |
| 1176 | case Builtin::BI__sync_lock_test_and_set_2: |
| 1177 | case Builtin::BI__sync_lock_test_and_set_4: |
| 1178 | case Builtin::BI__sync_lock_test_and_set_8: |
| 1179 | case Builtin::BI__sync_lock_test_and_set_16: |
| 1180 | BuiltinIndex = 12; |
| 1181 | break; |
| 1182 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1183 | case Builtin::BI__sync_lock_release: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1184 | case Builtin::BI__sync_lock_release_1: |
| 1185 | case Builtin::BI__sync_lock_release_2: |
| 1186 | case Builtin::BI__sync_lock_release_4: |
| 1187 | case Builtin::BI__sync_lock_release_8: |
| 1188 | case Builtin::BI__sync_lock_release_16: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 1189 | BuiltinIndex = 13; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1190 | NumFixed = 0; |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1191 | ResultType = Context.VoidTy; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1192 | break; |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1193 | |
| 1194 | case Builtin::BI__sync_swap: |
| 1195 | case Builtin::BI__sync_swap_1: |
| 1196 | case Builtin::BI__sync_swap_2: |
| 1197 | case Builtin::BI__sync_swap_4: |
| 1198 | case Builtin::BI__sync_swap_8: |
| 1199 | case Builtin::BI__sync_swap_16: |
| 1200 | BuiltinIndex = 14; |
| 1201 | break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1202 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1203 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1204 | // Now that we know how many fixed arguments we expect, first check that we |
| 1205 | // have at least that many. |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1206 | if (TheCall->getNumArgs() < 1+NumFixed) { |
| 1207 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 1208 | << 0 << 1+NumFixed << TheCall->getNumArgs() |
| 1209 | << TheCall->getCallee()->getSourceRange(); |
| 1210 | return ExprError(); |
| 1211 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1212 | |
Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 1213 | // Get the decl for the concrete builtin from this, we can tell what the |
| 1214 | // concrete integer type we should convert to is. |
| 1215 | unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; |
| 1216 | const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID); |
Abramo Bagnara | 2ad11cd | 2012-09-22 09:05:22 +0000 | [diff] [blame] | 1217 | FunctionDecl *NewBuiltinDecl; |
| 1218 | if (NewBuiltinID == BuiltinID) |
| 1219 | NewBuiltinDecl = FDecl; |
| 1220 | else { |
| 1221 | // Perform builtin lookup to avoid redeclaring it. |
| 1222 | DeclarationName DN(&Context.Idents.get(NewBuiltinName)); |
| 1223 | LookupResult Res(*this, DN, DRE->getLocStart(), LookupOrdinaryName); |
| 1224 | LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true); |
| 1225 | assert(Res.getFoundDecl()); |
| 1226 | NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl()); |
| 1227 | if (NewBuiltinDecl == 0) |
| 1228 | return ExprError(); |
| 1229 | } |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1230 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1231 | // The first argument --- the pointer --- has a fixed type; we |
| 1232 | // deduce the types of the rest of the arguments accordingly. Walk |
| 1233 | // the remaining arguments, converting them to the deduced value type. |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1234 | for (unsigned i = 0; i != NumFixed; ++i) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1235 | ExprResult Arg = TheCall->getArg(i+1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1236 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1237 | // GCC does an implicit conversion to the pointer or integer ValType. This |
| 1238 | // 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] | 1239 | // Initialize the argument. |
| 1240 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 1241 | ValType, /*consume*/ false); |
| 1242 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1243 | if (Arg.isInvalid()) |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1244 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1245 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1246 | // Okay, we have something that *can* be converted to the right type. Check |
| 1247 | // to see if there is a potentially weird extension going on here. This can |
| 1248 | // happen when you do an atomic operation on something like an char* and |
| 1249 | // pass in 42. The 42 gets converted to char. This is even more strange |
| 1250 | // for things like 45.123 -> char, etc. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1251 | // FIXME: Do this check. |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1252 | TheCall->setArg(i+1, Arg.take()); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1253 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1254 | |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1255 | ASTContext& Context = this->getASTContext(); |
| 1256 | |
| 1257 | // Create a new DeclRefExpr to refer to the new decl. |
| 1258 | DeclRefExpr* NewDRE = DeclRefExpr::Create( |
| 1259 | Context, |
| 1260 | DRE->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1261 | SourceLocation(), |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1262 | NewBuiltinDecl, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1263 | /*enclosing*/ false, |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1264 | DRE->getLocation(), |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 1265 | Context.BuiltinFnTy, |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1266 | DRE->getValueKind()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1268 | // Set the callee in the CallExpr. |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 1269 | // FIXME: This loses syntactic information. |
| 1270 | QualType CalleePtrTy = Context.getPointerType(NewBuiltinDecl->getType()); |
| 1271 | ExprResult PromotedCall = ImpCastExprToType(NewDRE, CalleePtrTy, |
| 1272 | CK_BuiltinFnToFnPtr); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1273 | TheCall->setCallee(PromotedCall.take()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1274 | |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 1275 | // Change the result type of the call to match the original value type. This |
| 1276 | // is arbitrary, but the codegen for these builtins ins design to handle it |
| 1277 | // gracefully. |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1278 | TheCall->setType(ResultType); |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1279 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1280 | return TheCallResult; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 1283 | /// CheckObjCString - Checks that the argument to the builtin |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1284 | /// CFString constructor is correct |
Steve Naroff | fd94262 | 2009-04-13 20:26:29 +0000 | [diff] [blame] | 1285 | /// Note: It might also make sense to do the UTF-16 conversion here (would |
| 1286 | /// simplify the backend). |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 1287 | bool Sema::CheckObjCString(Expr *Arg) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1288 | Arg = Arg->IgnoreParenCasts(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1289 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); |
| 1290 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1291 | if (!Literal || !Literal->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1292 | Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 1293 | << Arg->getSourceRange(); |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 1294 | return true; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1295 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1296 | |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1297 | if (Literal->containsNonAsciiOrNull()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1298 | StringRef String = Literal->getString(); |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1299 | unsigned NumBytes = String.size(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1300 | SmallVector<UTF16, 128> ToBuf(NumBytes); |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 1301 | const UTF8 *FromPtr = (const UTF8 *)String.data(); |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1302 | UTF16 *ToPtr = &ToBuf[0]; |
| 1303 | |
| 1304 | ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, |
| 1305 | &ToPtr, ToPtr + NumBytes, |
| 1306 | strictConversion); |
| 1307 | // Check for conversion failure. |
| 1308 | if (Result != conversionOK) |
| 1309 | Diag(Arg->getLocStart(), |
| 1310 | diag::warn_cfstring_truncated) << Arg->getSourceRange(); |
| 1311 | } |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 1312 | return false; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1315 | /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity. |
| 1316 | /// Emit an error and return true on failure, return false on success. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1317 | bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) { |
| 1318 | Expr *Fn = TheCall->getCallee(); |
| 1319 | if (TheCall->getNumArgs() > 2) { |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 1320 | Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1321 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1322 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
| 1323 | << Fn->getSourceRange() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1324 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1325 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1326 | return true; |
| 1327 | } |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 1328 | |
| 1329 | if (TheCall->getNumArgs() < 2) { |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1330 | return Diag(TheCall->getLocEnd(), |
| 1331 | diag::err_typecheck_call_too_few_args_at_least) |
| 1332 | << 0 /*function call*/ << 2 << TheCall->getNumArgs(); |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 1335 | // Type-check the first argument normally. |
| 1336 | if (checkBuiltinArgument(*this, TheCall, 0)) |
| 1337 | return true; |
| 1338 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1339 | // Determine whether the current function is variadic or not. |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1340 | BlockScopeInfo *CurBlock = getCurBlock(); |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1341 | bool isVariadic; |
Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 1342 | if (CurBlock) |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 1343 | isVariadic = CurBlock->TheDecl->isVariadic(); |
Ted Kremenek | 9498d38 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 1344 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
| 1345 | isVariadic = FD->isVariadic(); |
| 1346 | else |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1347 | isVariadic = getCurMethodDecl()->isVariadic(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1348 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1349 | if (!isVariadic) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1350 | Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function); |
| 1351 | return true; |
| 1352 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1353 | |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1354 | // Verify that the second argument to the builtin is the last argument of the |
| 1355 | // current function or method. |
| 1356 | bool SecondArgIsLastNamedArgument = false; |
Anders Carlsson | e2c1410 | 2008-02-13 01:22:59 +0000 | [diff] [blame] | 1357 | const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1358 | |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 1359 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { |
| 1360 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1361 | // FIXME: This isn't correct for methods (results in bogus warning). |
| 1362 | // Get the last formal in the current function. |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 1363 | const ParmVarDecl *LastArg; |
Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 1364 | if (CurBlock) |
| 1365 | LastArg = *(CurBlock->TheDecl->param_end()-1); |
| 1366 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1367 | LastArg = *(FD->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1368 | else |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1369 | LastArg = *(getCurMethodDecl()->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1370 | SecondArgIsLastNamedArgument = PV == LastArg; |
| 1371 | } |
| 1372 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1373 | |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1374 | if (!SecondArgIsLastNamedArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1375 | Diag(TheCall->getArg(1)->getLocStart(), |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1376 | diag::warn_second_parameter_of_va_start_not_last_named_argument); |
| 1377 | return false; |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 1378 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1379 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1380 | /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and |
| 1381 | /// friends. This is declared to take (...), so we have to check everything. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1382 | bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { |
| 1383 | if (TheCall->getNumArgs() < 2) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 1384 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1385 | << 0 << 2 << TheCall->getNumArgs()/*function call*/; |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1386 | if (TheCall->getNumArgs() > 2) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1387 | return Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1388 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1389 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1390 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
| 1391 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1392 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1393 | ExprResult OrigArg0 = TheCall->getArg(0); |
| 1394 | ExprResult OrigArg1 = TheCall->getArg(1); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1395 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1396 | // Do standard promotions between the two arguments, returning their common |
| 1397 | // type. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1398 | QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1399 | if (OrigArg0.isInvalid() || OrigArg1.isInvalid()) |
| 1400 | return true; |
Daniel Dunbar | 403bc2b | 2009-02-19 19:28:43 +0000 | [diff] [blame] | 1401 | |
| 1402 | // Make sure any conversions are pushed back into the call; this is |
| 1403 | // type safe since unordered compare builtins are declared as "_Bool |
| 1404 | // foo(...)". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1405 | TheCall->setArg(0, OrigArg0.get()); |
| 1406 | TheCall->setArg(1, OrigArg1.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1407 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1408 | if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent()) |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1409 | return false; |
| 1410 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1411 | // If the common type isn't a real floating type, then the arguments were |
| 1412 | // invalid for this operation. |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 1413 | if (Res.isNull() || !Res->isRealFloatingType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1414 | return Diag(OrigArg0.get()->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1415 | diag::err_typecheck_call_invalid_ordered_compare) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1416 | << OrigArg0.get()->getType() << OrigArg1.get()->getType() |
| 1417 | << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1419 | return false; |
| 1420 | } |
| 1421 | |
Benjamin Kramer | e771a7a | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 1422 | /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like |
| 1423 | /// __builtin_isnan and friends. This is declared to take (...), so we have |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1424 | /// to check everything. We expect the last argument to be a floating point |
| 1425 | /// value. |
| 1426 | bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) { |
| 1427 | if (TheCall->getNumArgs() < NumArgs) |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1428 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1429 | << 0 << NumArgs << TheCall->getNumArgs()/*function call*/; |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1430 | if (TheCall->getNumArgs() > NumArgs) |
| 1431 | return Diag(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1432 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1433 | << 0 /*function call*/ << NumArgs << TheCall->getNumArgs() |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1434 | << SourceRange(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1435 | (*(TheCall->arg_end()-1))->getLocEnd()); |
| 1436 | |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1437 | Expr *OrigArg = TheCall->getArg(NumArgs-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1438 | |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1439 | if (OrigArg->isTypeDependent()) |
| 1440 | return false; |
| 1441 | |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1442 | // This operation requires a non-_Complex floating-point number. |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1443 | if (!OrigArg->getType()->isRealFloatingType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1444 | return Diag(OrigArg->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1445 | diag::err_typecheck_call_invalid_unary_fp) |
| 1446 | << OrigArg->getType() << OrigArg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1448 | // If this is an implicit conversion from float -> double, remove it. |
| 1449 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) { |
| 1450 | Expr *CastArg = Cast->getSubExpr(); |
| 1451 | if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) { |
| 1452 | assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) && |
| 1453 | "promotion from float to double is the only expected cast here"); |
| 1454 | Cast->setSubExpr(0); |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1455 | TheCall->setArg(NumArgs-1, CastArg); |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1456 | } |
| 1457 | } |
| 1458 | |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1459 | return false; |
| 1460 | } |
| 1461 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1462 | /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. |
| 1463 | // This is declared to take (...), so we have to check everything. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1464 | ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1465 | if (TheCall->getNumArgs() < 2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1466 | return ExprError(Diag(TheCall->getLocEnd(), |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1467 | diag::err_typecheck_call_too_few_args_at_least) |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1468 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1469 | << TheCall->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1470 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1471 | // Determine which of the following types of shufflevector we're checking: |
| 1472 | // 1) unary, vector mask: (lhs, mask) |
| 1473 | // 2) binary, vector mask: (lhs, rhs, mask) |
| 1474 | // 3) binary, scalar mask: (lhs, rhs, index, ..., index) |
| 1475 | QualType resType = TheCall->getArg(0)->getType(); |
| 1476 | unsigned numElements = 0; |
| 1477 | |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1478 | if (!TheCall->getArg(0)->isTypeDependent() && |
| 1479 | !TheCall->getArg(1)->isTypeDependent()) { |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1480 | QualType LHSType = TheCall->getArg(0)->getType(); |
| 1481 | QualType RHSType = TheCall->getArg(1)->getType(); |
| 1482 | |
| 1483 | if (!LHSType->isVectorType() || !RHSType->isVectorType()) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1484 | Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1485 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1486 | TheCall->getArg(1)->getLocEnd()); |
| 1487 | return ExprError(); |
| 1488 | } |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1489 | |
| 1490 | numElements = LHSType->getAs<VectorType>()->getNumElements(); |
| 1491 | unsigned numResElements = TheCall->getNumArgs() - 2; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1493 | // Check to see if we have a call with 2 vector arguments, the unary shuffle |
| 1494 | // with mask. If so, verify that RHS is an integer vector type with the |
| 1495 | // same number of elts as lhs. |
| 1496 | if (TheCall->getNumArgs() == 2) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 1497 | if (!RHSType->hasIntegerRepresentation() || |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1498 | RHSType->getAs<VectorType>()->getNumElements() != numElements) |
| 1499 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
| 1500 | << SourceRange(TheCall->getArg(1)->getLocStart(), |
| 1501 | TheCall->getArg(1)->getLocEnd()); |
| 1502 | numResElements = numElements; |
| 1503 | } |
| 1504 | else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1505 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1506 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1507 | TheCall->getArg(1)->getLocEnd()); |
| 1508 | return ExprError(); |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1509 | } else if (numElements != numResElements) { |
| 1510 | QualType eltType = LHSType->getAs<VectorType>()->getElementType(); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1511 | resType = Context.getVectorType(eltType, numResElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1512 | VectorType::GenericVector); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1513 | } |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1517 | if (TheCall->getArg(i)->isTypeDependent() || |
| 1518 | TheCall->getArg(i)->isValueDependent()) |
| 1519 | continue; |
| 1520 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1521 | llvm::APSInt Result(32); |
| 1522 | if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) |
| 1523 | return ExprError(Diag(TheCall->getLocStart(), |
| 1524 | diag::err_shufflevector_nonconstant_argument) |
| 1525 | << TheCall->getArg(i)->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1526 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 1527 | if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1528 | return ExprError(Diag(TheCall->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1529 | diag::err_shufflevector_argument_too_large) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1530 | << TheCall->getArg(i)->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1533 | SmallVector<Expr*, 32> exprs; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1534 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 1535 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1536 | exprs.push_back(TheCall->getArg(i)); |
| 1537 | TheCall->setArg(i, 0); |
| 1538 | } |
| 1539 | |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1540 | return Owned(new (Context) ShuffleVectorExpr(Context, exprs, resType, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1541 | TheCall->getCallee()->getLocStart(), |
| 1542 | TheCall->getRParenLoc())); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1543 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1544 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1545 | /// SemaBuiltinPrefetch - Handle __builtin_prefetch. |
| 1546 | // This is declared to take (const void*, ...) and can take two |
| 1547 | // optional constant int args. |
| 1548 | bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1549 | unsigned NumArgs = TheCall->getNumArgs(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1550 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1551 | if (NumArgs > 3) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1552 | return Diag(TheCall->getLocEnd(), |
| 1553 | diag::err_typecheck_call_too_many_args_at_most) |
| 1554 | << 0 /*function call*/ << 3 << NumArgs |
| 1555 | << TheCall->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1556 | |
| 1557 | // Argument 0 is checked for us and the remaining arguments must be |
| 1558 | // constant integers. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1559 | for (unsigned i = 1; i != NumArgs; ++i) { |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1560 | Expr *Arg = TheCall->getArg(i); |
Douglas Gregor | 592a423 | 2012-06-29 01:05:22 +0000 | [diff] [blame] | 1561 | |
| 1562 | // We can't check the value of a dependent argument. |
| 1563 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 1564 | continue; |
| 1565 | |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1566 | llvm::APSInt Result; |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1567 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 1568 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1569 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1570 | // FIXME: gcc issues a warning and rewrites these to 0. These |
| 1571 | // seems especially odd for the third argument since the default |
| 1572 | // is 3. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1573 | if (i == 1) { |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1574 | if (Result.getLimitedValue() > 1) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1575 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1576 | << "0" << "1" << Arg->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1577 | } else { |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1578 | if (Result.getLimitedValue() > 3) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1579 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1580 | << "0" << "3" << Arg->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1581 | } |
| 1582 | } |
| 1583 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1584 | return false; |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1587 | /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr |
| 1588 | /// TheCall is a constant expression. |
| 1589 | bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum, |
| 1590 | llvm::APSInt &Result) { |
| 1591 | Expr *Arg = TheCall->getArg(ArgNum); |
| 1592 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 1593 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 1594 | |
| 1595 | if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; |
| 1596 | |
| 1597 | if (!Arg->isIntegerConstantExpr(Result, Context)) |
| 1598 | return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type) |
Eric Christopher | 5e89655 | 2010-04-19 18:23:02 +0000 | [diff] [blame] | 1599 | << FDecl->getDeclName() << Arg->getSourceRange(); |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1600 | |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1601 | return false; |
| 1602 | } |
| 1603 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1604 | /// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr, |
| 1605 | /// int type). This simply type checks that type is one of the defined |
| 1606 | /// constants (0-3). |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1607 | // For compatibility check 0-3, llvm only handles 0 and 2. |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1608 | bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) { |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1609 | llvm::APSInt Result; |
Douglas Gregor | 592a423 | 2012-06-29 01:05:22 +0000 | [diff] [blame] | 1610 | |
| 1611 | // We can't check the value of a dependent argument. |
| 1612 | if (TheCall->getArg(1)->isTypeDependent() || |
| 1613 | TheCall->getArg(1)->isValueDependent()) |
| 1614 | return false; |
| 1615 | |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1616 | // Check constant-ness first. |
| 1617 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 1618 | return true; |
| 1619 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1620 | Expr *Arg = TheCall->getArg(1); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1621 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1622 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 1623 | << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | return false; |
| 1627 | } |
| 1628 | |
Eli Friedman | 586d6a8 | 2009-05-03 06:04:26 +0000 | [diff] [blame] | 1629 | /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 1630 | /// This checks that val is a constant 1. |
| 1631 | bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { |
| 1632 | Expr *Arg = TheCall->getArg(1); |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1633 | llvm::APSInt Result; |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1634 | |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1635 | // TODO: This is less than ideal. Overload this to take a value. |
| 1636 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 1637 | return true; |
| 1638 | |
| 1639 | if (Result != 1) |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 1640 | return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val) |
| 1641 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
| 1642 | |
| 1643 | return false; |
| 1644 | } |
| 1645 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1646 | // Determine if an expression is a string literal or constant string. |
| 1647 | // If this function returns false on the arguments to a function expecting a |
| 1648 | // format string, we will usually need to emit a warning. |
| 1649 | // True string literals are then checked by CheckFormatString. |
| 1650 | Sema::StringLiteralCheckType |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 1651 | Sema::checkFormatStringExpr(const Expr *E, ArrayRef<const Expr *> Args, |
| 1652 | bool HasVAListArg, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1653 | unsigned format_idx, unsigned firstDataArg, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1654 | FormatStringType Type, VariadicCallType CallType, |
| 1655 | bool inFunctionCall) { |
Ted Kremenek | 4fe6441 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 1656 | tryAgain: |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1657 | if (E->isTypeDependent() || E->isValueDependent()) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1658 | return SLCT_NotALiteral; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1659 | |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1660 | E = E->IgnoreParenCasts(); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1661 | |
David Blaikie | a73cdcb | 2012-02-10 21:07:25 +0000 | [diff] [blame] | 1662 | if (E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 1663 | // Technically -Wformat-nonliteral does not warn about this case. |
| 1664 | // The behavior of printf and friends in this case is implementation |
| 1665 | // dependent. Ideally if the format string cannot be null then |
| 1666 | // it should have a 'nonnull' attribute in the function prototype. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1667 | return SLCT_CheckedLiteral; |
David Blaikie | a73cdcb | 2012-02-10 21:07:25 +0000 | [diff] [blame] | 1668 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1669 | switch (E->getStmtClass()) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1670 | case Stmt::BinaryConditionalOperatorClass: |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1671 | case Stmt::ConditionalOperatorClass: { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1672 | // The expression is a literal if both sub-expressions were, and it was |
| 1673 | // completely checked only if both sub-expressions were checked. |
| 1674 | const AbstractConditionalOperator *C = |
| 1675 | cast<AbstractConditionalOperator>(E); |
| 1676 | StringLiteralCheckType Left = |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 1677 | checkFormatStringExpr(C->getTrueExpr(), Args, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1678 | HasVAListArg, format_idx, firstDataArg, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1679 | Type, CallType, inFunctionCall); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1680 | if (Left == SLCT_NotALiteral) |
| 1681 | return SLCT_NotALiteral; |
| 1682 | StringLiteralCheckType Right = |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 1683 | checkFormatStringExpr(C->getFalseExpr(), Args, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1684 | HasVAListArg, format_idx, firstDataArg, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1685 | Type, CallType, inFunctionCall); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1686 | return Left < Right ? Left : Right; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | case Stmt::ImplicitCastExprClass: { |
Ted Kremenek | 4fe6441 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 1690 | E = cast<ImplicitCastExpr>(E)->getSubExpr(); |
| 1691 | goto tryAgain; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1694 | case Stmt::OpaqueValueExprClass: |
| 1695 | if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) { |
| 1696 | E = src; |
| 1697 | goto tryAgain; |
| 1698 | } |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1699 | return SLCT_NotALiteral; |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1700 | |
Ted Kremenek | b43e8ad | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 1701 | case Stmt::PredefinedExprClass: |
| 1702 | // While __func__, etc., are technically not string literals, they |
| 1703 | // cannot contain format specifiers and thus are not a security |
| 1704 | // liability. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1705 | return SLCT_UncheckedLiteral; |
Ted Kremenek | b43e8ad | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 1706 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1707 | case Stmt::DeclRefExprClass: { |
| 1708 | const DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1709 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1710 | // As an exception, do not flag errors for variables binding to |
| 1711 | // const string literals. |
| 1712 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 1713 | bool isConstant = false; |
| 1714 | QualType T = DR->getType(); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1715 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1716 | if (const ArrayType *AT = Context.getAsArrayType(T)) { |
| 1717 | isConstant = AT->getElementType().isConstant(Context); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1718 | } else if (const PointerType *PT = T->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1719 | isConstant = T.isConstant(Context) && |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1720 | PT->getPointeeType().isConstant(Context); |
Jean-Daniel Dupas | e98e5b5 | 2012-01-25 10:35:33 +0000 | [diff] [blame] | 1721 | } else if (T->isObjCObjectPointerType()) { |
| 1722 | // In ObjC, there is usually no "const ObjectPointer" type, |
| 1723 | // so don't check if the pointee type is constant. |
| 1724 | isConstant = T.isConstant(Context); |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1725 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1726 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1727 | if (isConstant) { |
Matt Beaumont-Gay | e2c6066 | 2012-05-11 22:10:59 +0000 | [diff] [blame] | 1728 | if (const Expr *Init = VD->getAnyInitializer()) { |
| 1729 | // Look through initializers like const char c[] = { "foo" } |
| 1730 | if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { |
| 1731 | if (InitList->isStringLiteralInit()) |
| 1732 | Init = InitList->getInit(0)->IgnoreParenImpCasts(); |
| 1733 | } |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 1734 | return checkFormatStringExpr(Init, Args, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1735 | HasVAListArg, format_idx, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1736 | firstDataArg, Type, CallType, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1737 | /*inFunctionCall*/false); |
Matt Beaumont-Gay | e2c6066 | 2012-05-11 22:10:59 +0000 | [diff] [blame] | 1738 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1739 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1740 | |
Anders Carlsson | d966a55 | 2009-06-28 19:55:58 +0000 | [diff] [blame] | 1741 | // For vprintf* functions (i.e., HasVAListArg==true), we add a |
| 1742 | // special check to see if the format string is a function parameter |
| 1743 | // of the function calling the printf function. If the function |
| 1744 | // has an attribute indicating it is a printf-like function, then we |
| 1745 | // should suppress warnings concerning non-literals being used in a call |
| 1746 | // to a vprintf function. For example: |
| 1747 | // |
| 1748 | // void |
| 1749 | // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){ |
| 1750 | // va_list ap; |
| 1751 | // va_start(ap, fmt); |
| 1752 | // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". |
| 1753 | // ... |
| 1754 | // |
Jean-Daniel Dupas | f57c413 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 1755 | if (HasVAListArg) { |
| 1756 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) { |
| 1757 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) { |
| 1758 | int PVIndex = PV->getFunctionScopeIndex() + 1; |
| 1759 | for (specific_attr_iterator<FormatAttr> |
| 1760 | i = ND->specific_attr_begin<FormatAttr>(), |
| 1761 | e = ND->specific_attr_end<FormatAttr>(); i != e ; ++i) { |
| 1762 | FormatAttr *PVFormat = *i; |
| 1763 | // adjust for implicit parameter |
| 1764 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 1765 | if (MD->isInstance()) |
| 1766 | ++PVIndex; |
| 1767 | // We also check if the formats are compatible. |
| 1768 | // We can't pass a 'scanf' string to a 'printf' function. |
| 1769 | if (PVIndex == PVFormat->getFormatIdx() && |
| 1770 | Type == GetFormatStringType(PVFormat)) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1771 | return SLCT_UncheckedLiteral; |
Jean-Daniel Dupas | f57c413 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 1772 | } |
| 1773 | } |
| 1774 | } |
| 1775 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1776 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1777 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1778 | return SLCT_NotALiteral; |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1779 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1780 | |
Jean-Daniel Dupas | 52aabaf | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 1781 | case Stmt::CallExprClass: |
| 1782 | case Stmt::CXXMemberCallExprClass: { |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1783 | const CallExpr *CE = cast<CallExpr>(E); |
Jean-Daniel Dupas | 52aabaf | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 1784 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) { |
| 1785 | if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) { |
| 1786 | unsigned ArgIndex = FA->getFormatIdx(); |
| 1787 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 1788 | if (MD->isInstance()) |
| 1789 | --ArgIndex; |
| 1790 | const Expr *Arg = CE->getArg(ArgIndex - 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 1792 | return checkFormatStringExpr(Arg, Args, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1793 | HasVAListArg, format_idx, firstDataArg, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1794 | Type, CallType, inFunctionCall); |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 1795 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 1796 | unsigned BuiltinID = FD->getBuiltinID(); |
| 1797 | if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString || |
| 1798 | BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) { |
| 1799 | const Expr *Arg = CE->getArg(0); |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 1800 | return checkFormatStringExpr(Arg, Args, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1801 | HasVAListArg, format_idx, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1802 | firstDataArg, Type, CallType, |
| 1803 | inFunctionCall); |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 1804 | } |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1805 | } |
| 1806 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1807 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1808 | return SLCT_NotALiteral; |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1809 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1810 | case Stmt::ObjCStringLiteralClass: |
| 1811 | case Stmt::StringLiteralClass: { |
| 1812 | const StringLiteral *StrE = NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1814 | if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1815 | StrE = ObjCFExpr->getString(); |
| 1816 | else |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1817 | StrE = cast<StringLiteral>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1819 | if (StrE) { |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 1820 | CheckFormatString(StrE, E, Args, HasVAListArg, format_idx, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1821 | firstDataArg, Type, inFunctionCall, CallType); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1822 | return SLCT_CheckedLiteral; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1823 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1824 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1825 | return SLCT_NotALiteral; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1826 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1827 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1828 | default: |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1829 | return SLCT_NotALiteral; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1830 | } |
| 1831 | } |
| 1832 | |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1833 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1834 | Sema::CheckNonNullArguments(const NonNullAttr *NonNull, |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1835 | const Expr * const *ExprArgs, |
| 1836 | SourceLocation CallSiteLoc) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1837 | for (NonNullAttr::args_iterator i = NonNull->args_begin(), |
| 1838 | e = NonNull->args_end(); |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1839 | i != e; ++i) { |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1840 | const Expr *ArgExpr = ExprArgs[*i]; |
Nick Lewycky | 3edf387 | 2013-01-23 05:08:29 +0000 | [diff] [blame] | 1841 | |
| 1842 | // As a special case, transparent unions initialized with zero are |
| 1843 | // considered null for the purposes of the nonnull attribute. |
| 1844 | if (const RecordType *UT = ArgExpr->getType()->getAsUnionType()) { |
| 1845 | if (UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1846 | if (const CompoundLiteralExpr *CLE = |
| 1847 | dyn_cast<CompoundLiteralExpr>(ArgExpr)) |
| 1848 | if (const InitListExpr *ILE = |
| 1849 | dyn_cast<InitListExpr>(CLE->getInitializer())) |
| 1850 | ArgExpr = ILE->getInit(0); |
| 1851 | } |
| 1852 | |
| 1853 | bool Result; |
| 1854 | if (ArgExpr->EvaluateAsBooleanCondition(Result, Context) && !Result) |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1855 | Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1856 | } |
| 1857 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1858 | |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1859 | Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) { |
| 1860 | return llvm::StringSwitch<FormatStringType>(Format->getType()) |
| 1861 | .Case("scanf", FST_Scanf) |
| 1862 | .Cases("printf", "printf0", FST_Printf) |
| 1863 | .Cases("NSString", "CFString", FST_NSString) |
| 1864 | .Case("strftime", FST_Strftime) |
| 1865 | .Case("strfmon", FST_Strfmon) |
| 1866 | .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf) |
| 1867 | .Default(FST_Unknown); |
| 1868 | } |
| 1869 | |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1870 | /// CheckFormatArguments - Check calls to printf and scanf (and similar |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1871 | /// functions) for correct use of format strings. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1872 | /// Returns true if a format string has been fully checked. |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 1873 | bool Sema::CheckFormatArguments(const FormatAttr *Format, |
| 1874 | ArrayRef<const Expr *> Args, |
| 1875 | bool IsCXXMember, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1876 | VariadicCallType CallType, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1877 | SourceLocation Loc, SourceRange Range) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1878 | FormatStringInfo FSI; |
| 1879 | if (getFormatStringInfo(Format, IsCXXMember, &FSI)) |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 1880 | return CheckFormatArguments(Args, FSI.HasVAListArg, FSI.FormatIdx, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1881 | FSI.FirstDataArg, GetFormatStringType(Format), |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1882 | CallType, Loc, Range); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1883 | return false; |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1884 | } |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1885 | |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 1886 | bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1887 | bool HasVAListArg, unsigned format_idx, |
| 1888 | unsigned firstDataArg, FormatStringType Type, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1889 | VariadicCallType CallType, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1890 | SourceLocation Loc, SourceRange Range) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1891 | // CHECK: printf/scanf-like function is called with no format string. |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 1892 | if (format_idx >= Args.size()) { |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1893 | Diag(Loc, diag::warn_missing_format_string) << Range; |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1894 | return false; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1895 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1896 | |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1897 | const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1898 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1899 | // CHECK: format string is not a string literal. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1900 | // |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1901 | // Dynamically generated format strings are difficult to |
| 1902 | // automatically vet at compile time. Requiring that format strings |
| 1903 | // are string literals: (1) permits the checking of format strings by |
| 1904 | // the compiler and thereby (2) can practically remove the source of |
| 1905 | // many format string exploits. |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1906 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1907 | // Format string can be either ObjC string (e.g. @"%d") or |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1908 | // C string (e.g. "%d") |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1909 | // 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] | 1910 | // the same format string checking logic for both ObjC and C strings. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1911 | StringLiteralCheckType CT = |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 1912 | checkFormatStringExpr(OrigFormatExpr, Args, HasVAListArg, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1913 | format_idx, firstDataArg, Type, CallType); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1914 | if (CT != SLCT_NotALiteral) |
| 1915 | // Literal format string found, check done! |
| 1916 | return CT == SLCT_CheckedLiteral; |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1917 | |
Jean-Daniel Dupas | 2837a2f | 2012-02-07 23:10:53 +0000 | [diff] [blame] | 1918 | // Strftime is particular as it always uses a single 'time' argument, |
| 1919 | // so it is safe to pass a non-literal string. |
| 1920 | if (Type == FST_Strftime) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1921 | return false; |
Jean-Daniel Dupas | 2837a2f | 2012-02-07 23:10:53 +0000 | [diff] [blame] | 1922 | |
Jean-Daniel Dupas | ce3aa39 | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 1923 | // Do not emit diag when the string param is a macro expansion and the |
| 1924 | // format is either NSString or CFString. This is a hack to prevent |
| 1925 | // diag when using the NSLocalizedString and CFCopyLocalizedString macros |
| 1926 | // which are usually used in place of NS and CF string literals. |
Jean-Daniel Dupas | dc17020 | 2012-05-04 21:08:08 +0000 | [diff] [blame] | 1927 | if (Type == FST_NSString && |
| 1928 | SourceMgr.isInSystemMacro(Args[format_idx]->getLocStart())) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1929 | return false; |
Jean-Daniel Dupas | ce3aa39 | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 1930 | |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1931 | // If there are no arguments specified, warn with -Wformat-security, otherwise |
| 1932 | // warn only with -Wformat-nonliteral. |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 1933 | if (Args.size() == format_idx+1) |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1934 | Diag(Args[format_idx]->getLocStart(), |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1935 | diag::warn_format_nonliteral_noargs) |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1936 | << OrigFormatExpr->getSourceRange(); |
| 1937 | else |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1938 | Diag(Args[format_idx]->getLocStart(), |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1939 | diag::warn_format_nonliteral) |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1940 | << OrigFormatExpr->getSourceRange(); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1941 | return false; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1942 | } |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1943 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1944 | namespace { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1945 | class CheckFormatHandler : public analyze_format_string::FormatStringHandler { |
| 1946 | protected: |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1947 | Sema &S; |
| 1948 | const StringLiteral *FExpr; |
| 1949 | const Expr *OrigFormatExpr; |
Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1950 | const unsigned FirstDataArg; |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1951 | const unsigned NumDataArgs; |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1952 | const char *Beg; // Start of format string. |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1953 | const bool HasVAListArg; |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 1954 | ArrayRef<const Expr *> Args; |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1955 | unsigned FormatIdx; |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1956 | llvm::BitVector CoveredArgs; |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1957 | bool usesPositionalArgs; |
| 1958 | bool atFirstArg; |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1959 | bool inFunctionCall; |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1960 | Sema::VariadicCallType CallType; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1961 | public: |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1962 | CheckFormatHandler(Sema &s, const StringLiteral *fexpr, |
Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1963 | const Expr *origFormatExpr, unsigned firstDataArg, |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 1964 | unsigned numDataArgs, const char *beg, bool hasVAListArg, |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 1965 | ArrayRef<const Expr *> Args, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1966 | unsigned formatIdx, bool inFunctionCall, |
| 1967 | Sema::VariadicCallType callType) |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1968 | : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 1969 | FirstDataArg(firstDataArg), NumDataArgs(numDataArgs), |
| 1970 | Beg(beg), HasVAListArg(hasVAListArg), |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 1971 | Args(Args), FormatIdx(formatIdx), |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1972 | usesPositionalArgs(false), atFirstArg(true), |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1973 | inFunctionCall(inFunctionCall), CallType(callType) { |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1974 | CoveredArgs.resize(numDataArgs); |
| 1975 | CoveredArgs.reset(); |
| 1976 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1977 | |
Ted Kremenek | 07d161f | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 1978 | void DoneProcessing(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1979 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1980 | void HandleIncompleteSpecifier(const char *startSpecifier, |
| 1981 | unsigned specifierLen); |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 1982 | |
Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 1983 | void HandleInvalidLengthModifier( |
| 1984 | const analyze_format_string::FormatSpecifier &FS, |
| 1985 | const analyze_format_string::ConversionSpecifier &CS, |
Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 1986 | const char *startSpecifier, unsigned specifierLen, unsigned DiagID); |
Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 1987 | |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 1988 | void HandleNonStandardLengthModifier( |
Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 1989 | const analyze_format_string::FormatSpecifier &FS, |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 1990 | const char *startSpecifier, unsigned specifierLen); |
| 1991 | |
| 1992 | void HandleNonStandardConversionSpecifier( |
| 1993 | const analyze_format_string::ConversionSpecifier &CS, |
| 1994 | const char *startSpecifier, unsigned specifierLen); |
| 1995 | |
Hans Wennborg | f856264 | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 1996 | virtual void HandlePosition(const char *startPos, unsigned posLen); |
| 1997 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1998 | virtual void HandleInvalidPosition(const char *startSpecifier, |
| 1999 | unsigned specifierLen, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2000 | analyze_format_string::PositionContext p); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2001 | |
| 2002 | virtual void HandleZeroPosition(const char *startPos, unsigned posLen); |
| 2003 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2004 | void HandleNullChar(const char *nullCharacter); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2005 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2006 | template <typename Range> |
| 2007 | static void EmitFormatDiagnostic(Sema &S, bool inFunctionCall, |
| 2008 | const Expr *ArgumentExpr, |
| 2009 | PartialDiagnostic PDiag, |
| 2010 | SourceLocation StringLoc, |
| 2011 | bool IsStringLocation, Range StringRange, |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2012 | ArrayRef<FixItHint> Fixit = ArrayRef<FixItHint>()); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2013 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2014 | protected: |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2015 | bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, |
| 2016 | const char *startSpec, |
| 2017 | unsigned specifierLen, |
| 2018 | const char *csStart, unsigned csLen); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2019 | |
| 2020 | void HandlePositionalNonpositionalArgs(SourceLocation Loc, |
| 2021 | const char *startSpec, |
| 2022 | unsigned specifierLen); |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2023 | |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2024 | SourceRange getFormatStringRange(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2025 | CharSourceRange getSpecifierRange(const char *startSpecifier, |
| 2026 | unsigned specifierLen); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2027 | SourceLocation getLocationOfByte(const char *x); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2028 | |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2029 | const Expr *getDataArg(unsigned i) const; |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2030 | |
| 2031 | bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, |
| 2032 | const analyze_format_string::ConversionSpecifier &CS, |
| 2033 | const char *startSpecifier, unsigned specifierLen, |
| 2034 | unsigned argIndex); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2035 | |
| 2036 | template <typename Range> |
| 2037 | void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc, |
| 2038 | bool IsStringLocation, Range StringRange, |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2039 | ArrayRef<FixItHint> Fixit = ArrayRef<FixItHint>()); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2040 | |
| 2041 | void CheckPositionalAndNonpositionalArgs( |
| 2042 | const analyze_format_string::FormatSpecifier *FS); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2043 | }; |
| 2044 | } |
| 2045 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2046 | SourceRange CheckFormatHandler::getFormatStringRange() { |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2047 | return OrigFormatExpr->getSourceRange(); |
| 2048 | } |
| 2049 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2050 | CharSourceRange CheckFormatHandler:: |
| 2051 | getSpecifierRange(const char *startSpecifier, unsigned specifierLen) { |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2052 | SourceLocation Start = getLocationOfByte(startSpecifier); |
| 2053 | SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1); |
| 2054 | |
| 2055 | // Advance the end SourceLocation by one due to half-open ranges. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2056 | End = End.getLocWithOffset(1); |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2057 | |
| 2058 | return CharSourceRange::getCharRange(Start, End); |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2061 | SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2062 | return S.getLocationOfStringLiteralByte(FExpr, x - Beg); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2065 | void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, |
| 2066 | unsigned specifierLen){ |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2067 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier), |
| 2068 | getLocationOfByte(startSpecifier), |
| 2069 | /*IsStringLocation*/true, |
| 2070 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2073 | void CheckFormatHandler::HandleInvalidLengthModifier( |
| 2074 | const analyze_format_string::FormatSpecifier &FS, |
| 2075 | const analyze_format_string::ConversionSpecifier &CS, |
Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2076 | const char *startSpecifier, unsigned specifierLen, unsigned DiagID) { |
Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2077 | using namespace analyze_format_string; |
| 2078 | |
| 2079 | const LengthModifier &LM = FS.getLengthModifier(); |
| 2080 | CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); |
| 2081 | |
| 2082 | // See if we know how to fix this length modifier. |
| 2083 | llvm::Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); |
| 2084 | if (FixedLM) { |
Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2085 | EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), |
Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2086 | getLocationOfByte(LM.getStart()), |
| 2087 | /*IsStringLocation*/true, |
| 2088 | getSpecifierRange(startSpecifier, specifierLen)); |
| 2089 | |
| 2090 | S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) |
| 2091 | << FixedLM->toString() |
| 2092 | << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); |
| 2093 | |
| 2094 | } else { |
Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2095 | FixItHint Hint; |
| 2096 | if (DiagID == diag::warn_format_nonsensical_length) |
| 2097 | Hint = FixItHint::CreateRemoval(LMRange); |
| 2098 | |
| 2099 | EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), |
Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2100 | getLocationOfByte(LM.getStart()), |
| 2101 | /*IsStringLocation*/true, |
| 2102 | getSpecifierRange(startSpecifier, specifierLen), |
Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2103 | Hint); |
Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2104 | } |
| 2105 | } |
| 2106 | |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2107 | void CheckFormatHandler::HandleNonStandardLengthModifier( |
Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2108 | const analyze_format_string::FormatSpecifier &FS, |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2109 | const char *startSpecifier, unsigned specifierLen) { |
Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2110 | using namespace analyze_format_string; |
| 2111 | |
| 2112 | const LengthModifier &LM = FS.getLengthModifier(); |
| 2113 | CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); |
| 2114 | |
| 2115 | // See if we know how to fix this length modifier. |
| 2116 | llvm::Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); |
| 2117 | if (FixedLM) { |
| 2118 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
| 2119 | << LM.toString() << 0, |
| 2120 | getLocationOfByte(LM.getStart()), |
| 2121 | /*IsStringLocation*/true, |
| 2122 | getSpecifierRange(startSpecifier, specifierLen)); |
| 2123 | |
| 2124 | S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) |
| 2125 | << FixedLM->toString() |
| 2126 | << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); |
| 2127 | |
| 2128 | } else { |
| 2129 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
| 2130 | << LM.toString() << 0, |
| 2131 | getLocationOfByte(LM.getStart()), |
| 2132 | /*IsStringLocation*/true, |
| 2133 | getSpecifierRange(startSpecifier, specifierLen)); |
| 2134 | } |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | void CheckFormatHandler::HandleNonStandardConversionSpecifier( |
| 2138 | const analyze_format_string::ConversionSpecifier &CS, |
| 2139 | const char *startSpecifier, unsigned specifierLen) { |
Jordan Rose | 670941c | 2012-09-13 02:11:15 +0000 | [diff] [blame] | 2140 | using namespace analyze_format_string; |
| 2141 | |
| 2142 | // See if we know how to fix this conversion specifier. |
| 2143 | llvm::Optional<ConversionSpecifier> FixedCS = CS.getStandardSpecifier(); |
| 2144 | if (FixedCS) { |
| 2145 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
| 2146 | << CS.toString() << /*conversion specifier*/1, |
| 2147 | getLocationOfByte(CS.getStart()), |
| 2148 | /*IsStringLocation*/true, |
| 2149 | getSpecifierRange(startSpecifier, specifierLen)); |
| 2150 | |
| 2151 | CharSourceRange CSRange = getSpecifierRange(CS.getStart(), CS.getLength()); |
| 2152 | S.Diag(getLocationOfByte(CS.getStart()), diag::note_format_fix_specifier) |
| 2153 | << FixedCS->toString() |
| 2154 | << FixItHint::CreateReplacement(CSRange, FixedCS->toString()); |
| 2155 | } else { |
| 2156 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
| 2157 | << CS.toString() << /*conversion specifier*/1, |
| 2158 | getLocationOfByte(CS.getStart()), |
| 2159 | /*IsStringLocation*/true, |
| 2160 | getSpecifierRange(startSpecifier, specifierLen)); |
| 2161 | } |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2162 | } |
| 2163 | |
Hans Wennborg | f856264 | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 2164 | void CheckFormatHandler::HandlePosition(const char *startPos, |
| 2165 | unsigned posLen) { |
| 2166 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg), |
| 2167 | getLocationOfByte(startPos), |
| 2168 | /*IsStringLocation*/true, |
| 2169 | getSpecifierRange(startPos, posLen)); |
| 2170 | } |
| 2171 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2172 | void |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2173 | CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen, |
| 2174 | analyze_format_string::PositionContext p) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2175 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier) |
| 2176 | << (unsigned) p, |
| 2177 | getLocationOfByte(startPos), /*IsStringLocation*/true, |
| 2178 | getSpecifierRange(startPos, posLen)); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2181 | void CheckFormatHandler::HandleZeroPosition(const char *startPos, |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2182 | unsigned posLen) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2183 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier), |
| 2184 | getLocationOfByte(startPos), |
| 2185 | /*IsStringLocation*/true, |
| 2186 | getSpecifierRange(startPos, posLen)); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2189 | void CheckFormatHandler::HandleNullChar(const char *nullCharacter) { |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2190 | if (!isa<ObjCStringLiteral>(OrigFormatExpr)) { |
Ted Kremenek | 0c06944 | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 2191 | // The presence of a null character is likely an error. |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2192 | EmitFormatDiagnostic( |
| 2193 | S.PDiag(diag::warn_printf_format_string_contains_null_char), |
| 2194 | getLocationOfByte(nullCharacter), /*IsStringLocation*/true, |
| 2195 | getFormatStringRange()); |
Ted Kremenek | 0c06944 | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 2196 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2197 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2198 | |
Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2199 | // Note that this may return NULL if there was an error parsing or building |
| 2200 | // one of the argument expressions. |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2201 | const Expr *CheckFormatHandler::getDataArg(unsigned i) const { |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2202 | return Args[FirstDataArg + i]; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
| 2205 | void CheckFormatHandler::DoneProcessing() { |
| 2206 | // Does the number of data arguments exceed the number of |
| 2207 | // format conversions in the format string? |
| 2208 | if (!HasVAListArg) { |
| 2209 | // Find any arguments that weren't covered. |
| 2210 | CoveredArgs.flip(); |
| 2211 | signed notCoveredArg = CoveredArgs.find_first(); |
| 2212 | if (notCoveredArg >= 0) { |
| 2213 | assert((unsigned)notCoveredArg < NumDataArgs); |
Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2214 | if (const Expr *E = getDataArg((unsigned) notCoveredArg)) { |
| 2215 | SourceLocation Loc = E->getLocStart(); |
| 2216 | if (!S.getSourceManager().isInSystemMacro(Loc)) { |
| 2217 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_data_arg_not_used), |
| 2218 | Loc, /*IsStringLocation*/false, |
| 2219 | getFormatStringRange()); |
| 2220 | } |
Bob Wilson | c03f2df | 2012-05-03 19:47:19 +0000 | [diff] [blame] | 2221 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2222 | } |
| 2223 | } |
| 2224 | } |
| 2225 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2226 | bool |
| 2227 | CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, |
| 2228 | SourceLocation Loc, |
| 2229 | const char *startSpec, |
| 2230 | unsigned specifierLen, |
| 2231 | const char *csStart, |
| 2232 | unsigned csLen) { |
| 2233 | |
| 2234 | bool keepGoing = true; |
| 2235 | if (argIndex < NumDataArgs) { |
| 2236 | // Consider the argument coverered, even though the specifier doesn't |
| 2237 | // make sense. |
| 2238 | CoveredArgs.set(argIndex); |
| 2239 | } |
| 2240 | else { |
| 2241 | // If argIndex exceeds the number of data arguments we |
| 2242 | // don't issue a warning because that is just a cascade of warnings (and |
| 2243 | // they may have intended '%%' anyway). We don't want to continue processing |
| 2244 | // the format string after this point, however, as we will like just get |
| 2245 | // gibberish when trying to match arguments. |
| 2246 | keepGoing = false; |
| 2247 | } |
| 2248 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2249 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_conversion) |
| 2250 | << StringRef(csStart, csLen), |
| 2251 | Loc, /*IsStringLocation*/true, |
| 2252 | getSpecifierRange(startSpec, specifierLen)); |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2253 | |
| 2254 | return keepGoing; |
| 2255 | } |
| 2256 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2257 | void |
| 2258 | CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc, |
| 2259 | const char *startSpec, |
| 2260 | unsigned specifierLen) { |
| 2261 | EmitFormatDiagnostic( |
| 2262 | S.PDiag(diag::warn_format_mix_positional_nonpositional_args), |
| 2263 | Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen)); |
| 2264 | } |
| 2265 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2266 | bool |
| 2267 | CheckFormatHandler::CheckNumArgs( |
| 2268 | const analyze_format_string::FormatSpecifier &FS, |
| 2269 | const analyze_format_string::ConversionSpecifier &CS, |
| 2270 | const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { |
| 2271 | |
| 2272 | if (argIndex >= NumDataArgs) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2273 | PartialDiagnostic PDiag = FS.usesPositionalArg() |
| 2274 | ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args) |
| 2275 | << (argIndex+1) << NumDataArgs) |
| 2276 | : S.PDiag(diag::warn_printf_insufficient_data_args); |
| 2277 | EmitFormatDiagnostic( |
| 2278 | PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true, |
| 2279 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2280 | return false; |
| 2281 | } |
| 2282 | return true; |
| 2283 | } |
| 2284 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2285 | template<typename Range> |
| 2286 | void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag, |
| 2287 | SourceLocation Loc, |
| 2288 | bool IsStringLocation, |
| 2289 | Range StringRange, |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2290 | ArrayRef<FixItHint> FixIt) { |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2291 | EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag, |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2292 | Loc, IsStringLocation, StringRange, FixIt); |
| 2293 | } |
| 2294 | |
| 2295 | /// \brief If the format string is not within the funcion call, emit a note |
| 2296 | /// so that the function call and string are in diagnostic messages. |
| 2297 | /// |
Dmitri Gribenko | 70517ca | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 2298 | /// \param InFunctionCall if true, the format string is within the function |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2299 | /// call and only one diagnostic message will be produced. Otherwise, an |
| 2300 | /// extra note will be emitted pointing to location of the format string. |
| 2301 | /// |
| 2302 | /// \param ArgumentExpr the expression that is passed as the format string |
| 2303 | /// argument in the function call. Used for getting locations when two |
| 2304 | /// diagnostics are emitted. |
| 2305 | /// |
| 2306 | /// \param PDiag the callee should already have provided any strings for the |
| 2307 | /// diagnostic message. This function only adds locations and fixits |
| 2308 | /// to diagnostics. |
| 2309 | /// |
| 2310 | /// \param Loc primary location for diagnostic. If two diagnostics are |
| 2311 | /// required, one will be at Loc and a new SourceLocation will be created for |
| 2312 | /// the other one. |
| 2313 | /// |
| 2314 | /// \param IsStringLocation if true, Loc points to the format string should be |
| 2315 | /// used for the note. Otherwise, Loc points to the argument list and will |
| 2316 | /// be used with PDiag. |
| 2317 | /// |
| 2318 | /// \param StringRange some or all of the string to highlight. This is |
| 2319 | /// templated so it can accept either a CharSourceRange or a SourceRange. |
| 2320 | /// |
Dmitri Gribenko | 70517ca | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 2321 | /// \param FixIt optional fix it hint for the format string. |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2322 | template<typename Range> |
| 2323 | void CheckFormatHandler::EmitFormatDiagnostic(Sema &S, bool InFunctionCall, |
| 2324 | const Expr *ArgumentExpr, |
| 2325 | PartialDiagnostic PDiag, |
| 2326 | SourceLocation Loc, |
| 2327 | bool IsStringLocation, |
| 2328 | Range StringRange, |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2329 | ArrayRef<FixItHint> FixIt) { |
| 2330 | if (InFunctionCall) { |
| 2331 | const Sema::SemaDiagnosticBuilder &D = S.Diag(Loc, PDiag); |
| 2332 | D << StringRange; |
| 2333 | for (ArrayRef<FixItHint>::iterator I = FixIt.begin(), E = FixIt.end(); |
| 2334 | I != E; ++I) { |
| 2335 | D << *I; |
| 2336 | } |
| 2337 | } else { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2338 | S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag) |
| 2339 | << ArgumentExpr->getSourceRange(); |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2340 | |
| 2341 | const Sema::SemaDiagnosticBuilder &Note = |
| 2342 | S.Diag(IsStringLocation ? Loc : StringRange.getBegin(), |
| 2343 | diag::note_format_string_defined); |
| 2344 | |
| 2345 | Note << StringRange; |
| 2346 | for (ArrayRef<FixItHint>::iterator I = FixIt.begin(), E = FixIt.end(); |
| 2347 | I != E; ++I) { |
| 2348 | Note << *I; |
| 2349 | } |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2350 | } |
| 2351 | } |
| 2352 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2353 | //===--- CHECK: Printf format string checking ------------------------------===// |
| 2354 | |
| 2355 | namespace { |
| 2356 | class CheckPrintfHandler : public CheckFormatHandler { |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2357 | bool ObjCContext; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2358 | public: |
| 2359 | CheckPrintfHandler(Sema &s, const StringLiteral *fexpr, |
| 2360 | const Expr *origFormatExpr, unsigned firstDataArg, |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2361 | unsigned numDataArgs, bool isObjC, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2362 | const char *beg, bool hasVAListArg, |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2363 | ArrayRef<const Expr *> Args, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2364 | unsigned formatIdx, bool inFunctionCall, |
| 2365 | Sema::VariadicCallType CallType) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2366 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2367 | numDataArgs, beg, hasVAListArg, Args, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2368 | formatIdx, inFunctionCall, CallType), ObjCContext(isObjC) |
| 2369 | {} |
| 2370 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2371 | |
| 2372 | bool HandleInvalidPrintfConversionSpecifier( |
| 2373 | const analyze_printf::PrintfSpecifier &FS, |
| 2374 | const char *startSpecifier, |
| 2375 | unsigned specifierLen); |
| 2376 | |
| 2377 | bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, |
| 2378 | const char *startSpecifier, |
| 2379 | unsigned specifierLen); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2380 | bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, |
| 2381 | const char *StartSpecifier, |
| 2382 | unsigned SpecifierLen, |
| 2383 | const Expr *E); |
| 2384 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2385 | bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, |
| 2386 | const char *startSpecifier, unsigned specifierLen); |
| 2387 | void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, |
| 2388 | const analyze_printf::OptionalAmount &Amt, |
| 2389 | unsigned type, |
| 2390 | const char *startSpecifier, unsigned specifierLen); |
| 2391 | void HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
| 2392 | const analyze_printf::OptionalFlag &flag, |
| 2393 | const char *startSpecifier, unsigned specifierLen); |
| 2394 | void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, |
| 2395 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 2396 | const analyze_printf::OptionalFlag &flag, |
| 2397 | const char *startSpecifier, unsigned specifierLen); |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2398 | bool checkForCStrMembers(const analyze_printf::ArgType &AT, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2399 | const Expr *E, const CharSourceRange &CSR); |
| 2400 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2401 | }; |
| 2402 | } |
| 2403 | |
| 2404 | bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( |
| 2405 | const analyze_printf::PrintfSpecifier &FS, |
| 2406 | const char *startSpecifier, |
| 2407 | unsigned specifierLen) { |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2408 | const analyze_printf::PrintfConversionSpecifier &CS = |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2409 | FS.getConversionSpecifier(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2410 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2411 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 2412 | getLocationOfByte(CS.getStart()), |
| 2413 | startSpecifier, specifierLen, |
| 2414 | CS.getStart(), CS.getLength()); |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2417 | bool CheckPrintfHandler::HandleAmount( |
| 2418 | const analyze_format_string::OptionalAmount &Amt, |
| 2419 | unsigned k, const char *startSpecifier, |
| 2420 | unsigned specifierLen) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2421 | |
| 2422 | if (Amt.hasDataArgument()) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2423 | if (!HasVAListArg) { |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2424 | unsigned argIndex = Amt.getArgIndex(); |
| 2425 | if (argIndex >= NumDataArgs) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2426 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg) |
| 2427 | << k, |
| 2428 | getLocationOfByte(Amt.getStart()), |
| 2429 | /*IsStringLocation*/true, |
| 2430 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2431 | // Don't do any more checking. We will just emit |
| 2432 | // spurious errors. |
| 2433 | return false; |
| 2434 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2435 | |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2436 | // Type check the data argument. It should be an 'int'. |
Ted Kremenek | 31f8e32 | 2010-01-29 23:32:22 +0000 | [diff] [blame] | 2437 | // Although not in conformance with C99, we also allow the argument to be |
| 2438 | // an 'unsigned int' as that is a reasonably safe case. GCC also |
| 2439 | // doesn't emit a warning for that case. |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2440 | CoveredArgs.set(argIndex); |
| 2441 | const Expr *Arg = getDataArg(argIndex); |
Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2442 | if (!Arg) |
| 2443 | return false; |
| 2444 | |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2445 | QualType T = Arg->getType(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2446 | |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2447 | const analyze_printf::ArgType &AT = Amt.getArgType(S.Context); |
| 2448 | assert(AT.isValid()); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2449 | |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2450 | if (!AT.matchesType(S.Context, T)) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2451 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type) |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2452 | << k << AT.getRepresentativeTypeName(S.Context) |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2453 | << T << Arg->getSourceRange(), |
| 2454 | getLocationOfByte(Amt.getStart()), |
| 2455 | /*IsStringLocation*/true, |
| 2456 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2457 | // Don't do any more checking. We will just emit |
| 2458 | // spurious errors. |
| 2459 | return false; |
| 2460 | } |
| 2461 | } |
| 2462 | } |
| 2463 | return true; |
| 2464 | } |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2465 | |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2466 | void CheckPrintfHandler::HandleInvalidAmount( |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2467 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2468 | const analyze_printf::OptionalAmount &Amt, |
| 2469 | unsigned type, |
| 2470 | const char *startSpecifier, |
| 2471 | unsigned specifierLen) { |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2472 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 2473 | FS.getConversionSpecifier(); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2474 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2475 | FixItHint fixit = |
| 2476 | Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant |
| 2477 | ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), |
| 2478 | Amt.getConstantLength())) |
| 2479 | : FixItHint(); |
| 2480 | |
| 2481 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount) |
| 2482 | << type << CS.toString(), |
| 2483 | getLocationOfByte(Amt.getStart()), |
| 2484 | /*IsStringLocation*/true, |
| 2485 | getSpecifierRange(startSpecifier, specifierLen), |
| 2486 | fixit); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2489 | void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2490 | const analyze_printf::OptionalFlag &flag, |
| 2491 | const char *startSpecifier, |
| 2492 | unsigned specifierLen) { |
| 2493 | // Warn about pointless flag with a fixit removal. |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2494 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 2495 | FS.getConversionSpecifier(); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2496 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag) |
| 2497 | << flag.toString() << CS.toString(), |
| 2498 | getLocationOfByte(flag.getPosition()), |
| 2499 | /*IsStringLocation*/true, |
| 2500 | getSpecifierRange(startSpecifier, specifierLen), |
| 2501 | FixItHint::CreateRemoval( |
| 2502 | getSpecifierRange(flag.getPosition(), 1))); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | void CheckPrintfHandler::HandleIgnoredFlag( |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2506 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2507 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 2508 | const analyze_printf::OptionalFlag &flag, |
| 2509 | const char *startSpecifier, |
| 2510 | unsigned specifierLen) { |
| 2511 | // Warn about ignored flag with a fixit removal. |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2512 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag) |
| 2513 | << ignoredFlag.toString() << flag.toString(), |
| 2514 | getLocationOfByte(ignoredFlag.getPosition()), |
| 2515 | /*IsStringLocation*/true, |
| 2516 | getSpecifierRange(startSpecifier, specifierLen), |
| 2517 | FixItHint::CreateRemoval( |
| 2518 | getSpecifierRange(ignoredFlag.getPosition(), 1))); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2521 | // Determines if the specified is a C++ class or struct containing |
| 2522 | // a member with the specified name and kind (e.g. a CXXMethodDecl named |
| 2523 | // "c_str()"). |
| 2524 | template<typename MemberKind> |
| 2525 | static llvm::SmallPtrSet<MemberKind*, 1> |
| 2526 | CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) { |
| 2527 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 2528 | llvm::SmallPtrSet<MemberKind*, 1> Results; |
| 2529 | |
| 2530 | if (!RT) |
| 2531 | return Results; |
| 2532 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 2533 | if (!RD) |
| 2534 | return Results; |
| 2535 | |
| 2536 | LookupResult R(S, &S.PP.getIdentifierTable().get(Name), SourceLocation(), |
| 2537 | Sema::LookupMemberName); |
| 2538 | |
| 2539 | // We just need to include all members of the right kind turned up by the |
| 2540 | // filter, at this point. |
| 2541 | if (S.LookupQualifiedName(R, RT->getDecl())) |
| 2542 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2543 | NamedDecl *decl = (*I)->getUnderlyingDecl(); |
| 2544 | if (MemberKind *FK = dyn_cast<MemberKind>(decl)) |
| 2545 | Results.insert(FK); |
| 2546 | } |
| 2547 | return Results; |
| 2548 | } |
| 2549 | |
| 2550 | // Check if a (w)string was passed when a (w)char* was needed, and offer a |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2551 | // better diagnostic if so. AT is assumed to be valid. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2552 | // Returns true when a c_str() conversion method is found. |
| 2553 | bool CheckPrintfHandler::checkForCStrMembers( |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2554 | const analyze_printf::ArgType &AT, const Expr *E, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2555 | const CharSourceRange &CSR) { |
| 2556 | typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet; |
| 2557 | |
| 2558 | MethodSet Results = |
| 2559 | CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType()); |
| 2560 | |
| 2561 | for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); |
| 2562 | MI != ME; ++MI) { |
| 2563 | const CXXMethodDecl *Method = *MI; |
| 2564 | if (Method->getNumParams() == 0 && |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2565 | AT.matchesType(S.Context, Method->getResultType())) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2566 | // FIXME: Suggest parens if the expression needs them. |
| 2567 | SourceLocation EndLoc = |
| 2568 | S.getPreprocessor().getLocForEndOfToken(E->getLocEnd()); |
| 2569 | S.Diag(E->getLocStart(), diag::note_printf_c_str) |
| 2570 | << "c_str()" |
| 2571 | << FixItHint::CreateInsertion(EndLoc, ".c_str()"); |
| 2572 | return true; |
| 2573 | } |
| 2574 | } |
| 2575 | |
| 2576 | return false; |
| 2577 | } |
| 2578 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2579 | bool |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2580 | CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier |
Ted Kremenek | 5c41ee8 | 2010-02-11 09:27:41 +0000 | [diff] [blame] | 2581 | &FS, |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2582 | const char *startSpecifier, |
| 2583 | unsigned specifierLen) { |
| 2584 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2585 | using namespace analyze_format_string; |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2586 | using namespace analyze_printf; |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2587 | const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2588 | |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2589 | if (FS.consumesDataArgument()) { |
| 2590 | if (atFirstArg) { |
| 2591 | atFirstArg = false; |
| 2592 | usesPositionalArgs = FS.usesPositionalArg(); |
| 2593 | } |
| 2594 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2595 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), |
| 2596 | startSpecifier, specifierLen); |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2597 | return false; |
| 2598 | } |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2599 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2600 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2601 | // First check if the field width, precision, and conversion specifier |
| 2602 | // have matching data arguments. |
| 2603 | if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, |
| 2604 | startSpecifier, specifierLen)) { |
| 2605 | return false; |
| 2606 | } |
| 2607 | |
| 2608 | if (!HandleAmount(FS.getPrecision(), /* precision */ 1, |
| 2609 | startSpecifier, specifierLen)) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2610 | return false; |
| 2611 | } |
| 2612 | |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2613 | if (!CS.consumesDataArgument()) { |
| 2614 | // FIXME: Technically specifying a precision or field width here |
| 2615 | // makes no sense. Worth issuing a warning at some point. |
Ted Kremenek | 0e5675d | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 2616 | return true; |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2617 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2618 | |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2619 | // Consume the argument. |
| 2620 | unsigned argIndex = FS.getArgIndex(); |
Ted Kremenek | e3fc547 | 2010-02-27 08:34:51 +0000 | [diff] [blame] | 2621 | if (argIndex < NumDataArgs) { |
| 2622 | // The check to see if the argIndex is valid will come later. |
| 2623 | // We set the bit here because we may exit early from this |
| 2624 | // function if we encounter some other error. |
| 2625 | CoveredArgs.set(argIndex); |
| 2626 | } |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2627 | |
| 2628 | // Check for using an Objective-C specific conversion specifier |
| 2629 | // in a non-ObjC literal. |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2630 | if (!ObjCContext && CS.isObjCArg()) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2631 | return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, |
| 2632 | specifierLen); |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2633 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2634 | |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2635 | // Check for invalid use of field width |
| 2636 | if (!FS.hasValidFieldWidth()) { |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2637 | HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2638 | startSpecifier, specifierLen); |
| 2639 | } |
| 2640 | |
| 2641 | // Check for invalid use of precision |
| 2642 | if (!FS.hasValidPrecision()) { |
| 2643 | HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, |
| 2644 | startSpecifier, specifierLen); |
| 2645 | } |
| 2646 | |
| 2647 | // Check each flag does not conflict with any other component. |
Ted Kremenek | 65197b4 | 2011-01-08 05:28:46 +0000 | [diff] [blame] | 2648 | if (!FS.hasValidThousandsGroupingPrefix()) |
| 2649 | HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2650 | if (!FS.hasValidLeadingZeros()) |
| 2651 | HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); |
| 2652 | if (!FS.hasValidPlusPrefix()) |
| 2653 | HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2654 | if (!FS.hasValidSpacePrefix()) |
| 2655 | HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2656 | if (!FS.hasValidAlternativeForm()) |
| 2657 | HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); |
| 2658 | if (!FS.hasValidLeftJustified()) |
| 2659 | HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); |
| 2660 | |
| 2661 | // Check that flags are not ignored by another flag |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2662 | if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' |
| 2663 | HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), |
| 2664 | startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2665 | if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' |
| 2666 | HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), |
| 2667 | startSpecifier, specifierLen); |
| 2668 | |
| 2669 | // Check the length modifier is valid with the given conversion specifier. |
Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2670 | if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo())) |
Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2671 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
| 2672 | diag::warn_format_nonsensical_length); |
Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2673 | else if (!FS.hasStandardLengthModifier()) |
Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2674 | HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); |
Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2675 | else if (!FS.hasStandardLengthConversionCombination()) |
Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2676 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
| 2677 | diag::warn_format_non_standard_conversion_spec); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2678 | |
Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2679 | if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) |
| 2680 | HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); |
| 2681 | |
Ted Kremenek | da51f0d | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 2682 | // The remaining checks depend on the data arguments. |
| 2683 | if (HasVAListArg) |
| 2684 | return true; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2685 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2686 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | da51f0d | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 2687 | return false; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2688 | |
Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2689 | const Expr *Arg = getDataArg(argIndex); |
| 2690 | if (!Arg) |
| 2691 | return true; |
| 2692 | |
| 2693 | return checkFormatExpr(FS, startSpecifier, specifierLen, Arg); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2696 | static bool requiresParensToAddCast(const Expr *E) { |
| 2697 | // FIXME: We should have a general way to reason about operator |
| 2698 | // precedence and whether parens are actually needed here. |
| 2699 | // Take care of a few common cases where they aren't. |
| 2700 | const Expr *Inside = E->IgnoreImpCasts(); |
| 2701 | if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Inside)) |
| 2702 | Inside = POE->getSyntacticForm()->IgnoreImpCasts(); |
| 2703 | |
| 2704 | switch (Inside->getStmtClass()) { |
| 2705 | case Stmt::ArraySubscriptExprClass: |
| 2706 | case Stmt::CallExprClass: |
Jordan Rose | 17ddc54 | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 2707 | case Stmt::CharacterLiteralClass: |
| 2708 | case Stmt::CXXBoolLiteralExprClass: |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2709 | case Stmt::DeclRefExprClass: |
Jordan Rose | 17ddc54 | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 2710 | case Stmt::FloatingLiteralClass: |
| 2711 | case Stmt::IntegerLiteralClass: |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2712 | case Stmt::MemberExprClass: |
Jordan Rose | 17ddc54 | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 2713 | case Stmt::ObjCArrayLiteralClass: |
| 2714 | case Stmt::ObjCBoolLiteralExprClass: |
| 2715 | case Stmt::ObjCBoxedExprClass: |
| 2716 | case Stmt::ObjCDictionaryLiteralClass: |
| 2717 | case Stmt::ObjCEncodeExprClass: |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2718 | case Stmt::ObjCIvarRefExprClass: |
| 2719 | case Stmt::ObjCMessageExprClass: |
| 2720 | case Stmt::ObjCPropertyRefExprClass: |
Jordan Rose | 17ddc54 | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 2721 | case Stmt::ObjCStringLiteralClass: |
| 2722 | case Stmt::ObjCSubscriptRefExprClass: |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2723 | case Stmt::ParenExprClass: |
Jordan Rose | 17ddc54 | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 2724 | case Stmt::StringLiteralClass: |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2725 | case Stmt::UnaryOperatorClass: |
| 2726 | return false; |
| 2727 | default: |
| 2728 | return true; |
| 2729 | } |
| 2730 | } |
| 2731 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2732 | bool |
| 2733 | CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, |
| 2734 | const char *StartSpecifier, |
| 2735 | unsigned SpecifierLen, |
| 2736 | const Expr *E) { |
| 2737 | using namespace analyze_format_string; |
| 2738 | using namespace analyze_printf; |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2739 | // Now type check the data expression that matches the |
| 2740 | // format specifier. |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2741 | const analyze_printf::ArgType &AT = FS.getArgType(S.Context, |
| 2742 | ObjCContext); |
Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2743 | if (!AT.isValid()) |
| 2744 | return true; |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2745 | |
Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 2746 | QualType ExprTy = E->getType(); |
| 2747 | if (AT.matchesType(S.Context, ExprTy)) |
Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2748 | return true; |
Jordan Rose | ee0259d | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 2749 | |
Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2750 | // Look through argument promotions for our error message's reported type. |
| 2751 | // This includes the integral and floating promotions, but excludes array |
| 2752 | // and function pointer decay; seeing that an argument intended to be a |
| 2753 | // string has type 'char [6]' is probably more confusing than 'char *'. |
| 2754 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
| 2755 | if (ICE->getCastKind() == CK_IntegralCast || |
| 2756 | ICE->getCastKind() == CK_FloatingCast) { |
| 2757 | E = ICE->getSubExpr(); |
Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 2758 | ExprTy = E->getType(); |
Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2759 | |
| 2760 | // Check if we didn't match because of an implicit cast from a 'char' |
| 2761 | // or 'short' to an 'int'. This is done because printf is a varargs |
| 2762 | // function. |
| 2763 | if (ICE->getType() == S.Context.IntTy || |
| 2764 | ICE->getType() == S.Context.UnsignedIntTy) { |
| 2765 | // All further checking is done on the subexpression. |
Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 2766 | if (AT.matchesType(S.Context, ExprTy)) |
Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2767 | return true; |
Ted Kremenek | 4d8ae4d | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 2768 | } |
Jordan Rose | ee0259d | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 2769 | } |
Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 2770 | } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) { |
| 2771 | // Special case for 'a', which has type 'int' in C. |
| 2772 | // Note, however, that we do /not/ want to treat multibyte constants like |
| 2773 | // 'MooV' as characters! This form is deprecated but still exists. |
| 2774 | if (ExprTy == S.Context.IntTy) |
| 2775 | if (llvm::isUIntN(S.Context.getCharWidth(), CL->getValue())) |
| 2776 | ExprTy = S.Context.CharTy; |
Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2777 | } |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2778 | |
Jordan Rose | 2cd3440 | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 2779 | // %C in an Objective-C context prints a unichar, not a wchar_t. |
| 2780 | // If the argument is an integer of some kind, believe the %C and suggest |
| 2781 | // a cast instead of changing the conversion specifier. |
Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 2782 | QualType IntendedTy = ExprTy; |
Jordan Rose | 2cd3440 | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 2783 | if (ObjCContext && |
| 2784 | FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) { |
| 2785 | if (ExprTy->isIntegralOrUnscopedEnumerationType() && |
| 2786 | !ExprTy->isCharType()) { |
| 2787 | // 'unichar' is defined as a typedef of unsigned short, but we should |
| 2788 | // prefer using the typedef if it is visible. |
| 2789 | IntendedTy = S.Context.UnsignedShortTy; |
| 2790 | |
| 2791 | LookupResult Result(S, &S.Context.Idents.get("unichar"), E->getLocStart(), |
| 2792 | Sema::LookupOrdinaryName); |
| 2793 | if (S.LookupName(Result, S.getCurScope())) { |
| 2794 | NamedDecl *ND = Result.getFoundDecl(); |
| 2795 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND)) |
| 2796 | if (TD->getUnderlyingType() == IntendedTy) |
| 2797 | IntendedTy = S.Context.getTypedefType(TD); |
| 2798 | } |
| 2799 | } |
| 2800 | } |
| 2801 | |
| 2802 | // Special-case some of Darwin's platform-independence types by suggesting |
| 2803 | // casts to primitive types that are known to be large enough. |
| 2804 | bool ShouldNotPrintDirectly = false; |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2805 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2806 | if (const TypedefType *UserTy = IntendedTy->getAs<TypedefType>()) { |
| 2807 | StringRef Name = UserTy->getDecl()->getName(); |
Jordan Rose | 2cd3440 | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 2808 | QualType CastTy = llvm::StringSwitch<QualType>(Name) |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2809 | .Case("NSInteger", S.Context.LongTy) |
| 2810 | .Case("NSUInteger", S.Context.UnsignedLongTy) |
| 2811 | .Case("SInt32", S.Context.IntTy) |
| 2812 | .Case("UInt32", S.Context.UnsignedIntTy) |
Jordan Rose | 2cd3440 | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 2813 | .Default(QualType()); |
| 2814 | |
| 2815 | if (!CastTy.isNull()) { |
| 2816 | ShouldNotPrintDirectly = true; |
| 2817 | IntendedTy = CastTy; |
| 2818 | } |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2819 | } |
| 2820 | } |
| 2821 | |
Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2822 | // We may be able to offer a FixItHint if it is a supported type. |
| 2823 | PrintfSpecifier fixedFS = FS; |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2824 | bool success = fixedFS.fixType(IntendedTy, S.getLangOpts(), |
Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2825 | S.Context, ObjCContext); |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2826 | |
Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2827 | if (success) { |
| 2828 | // Get the fix string from the fixed format specifier |
| 2829 | SmallString<16> buf; |
| 2830 | llvm::raw_svector_ostream os(buf); |
| 2831 | fixedFS.toString(os); |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2832 | |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2833 | CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen); |
| 2834 | |
Jordan Rose | 2cd3440 | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 2835 | if (IntendedTy == ExprTy) { |
| 2836 | // In this case, the specifier is wrong and should be changed to match |
| 2837 | // the argument. |
| 2838 | EmitFormatDiagnostic( |
| 2839 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
| 2840 | << AT.getRepresentativeTypeName(S.Context) << IntendedTy |
| 2841 | << E->getSourceRange(), |
| 2842 | E->getLocStart(), |
| 2843 | /*IsStringLocation*/false, |
| 2844 | SpecRange, |
| 2845 | FixItHint::CreateReplacement(SpecRange, os.str())); |
| 2846 | |
| 2847 | } else { |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2848 | // The canonical type for formatting this value is different from the |
| 2849 | // actual type of the expression. (This occurs, for example, with Darwin's |
| 2850 | // NSInteger on 32-bit platforms, where it is typedef'd as 'int', but |
| 2851 | // should be printed as 'long' for 64-bit compatibility.) |
| 2852 | // Rather than emitting a normal format/argument mismatch, we want to |
| 2853 | // add a cast to the recommended type (and correct the format string |
| 2854 | // if necessary). |
| 2855 | SmallString<16> CastBuf; |
| 2856 | llvm::raw_svector_ostream CastFix(CastBuf); |
| 2857 | CastFix << "("; |
| 2858 | IntendedTy.print(CastFix, S.Context.getPrintingPolicy()); |
| 2859 | CastFix << ")"; |
| 2860 | |
| 2861 | SmallVector<FixItHint,4> Hints; |
| 2862 | if (!AT.matchesType(S.Context, IntendedTy)) |
| 2863 | Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str())); |
| 2864 | |
| 2865 | if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) { |
| 2866 | // If there's already a cast present, just replace it. |
| 2867 | SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc()); |
| 2868 | Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str())); |
| 2869 | |
| 2870 | } else if (!requiresParensToAddCast(E)) { |
| 2871 | // If the expression has high enough precedence, |
| 2872 | // just write the C-style cast. |
| 2873 | Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(), |
| 2874 | CastFix.str())); |
| 2875 | } else { |
| 2876 | // Otherwise, add parens around the expression as well as the cast. |
| 2877 | CastFix << "("; |
| 2878 | Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(), |
| 2879 | CastFix.str())); |
| 2880 | |
| 2881 | SourceLocation After = S.PP.getLocForEndOfToken(E->getLocEnd()); |
| 2882 | Hints.push_back(FixItHint::CreateInsertion(After, ")")); |
| 2883 | } |
| 2884 | |
Jordan Rose | 2cd3440 | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 2885 | if (ShouldNotPrintDirectly) { |
| 2886 | // The expression has a type that should not be printed directly. |
| 2887 | // We extract the name from the typedef because we don't want to show |
| 2888 | // the underlying type in the diagnostic. |
| 2889 | StringRef Name = cast<TypedefType>(ExprTy)->getDecl()->getName(); |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2890 | |
Jordan Rose | 2cd3440 | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 2891 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_argument_needs_cast) |
| 2892 | << Name << IntendedTy |
| 2893 | << E->getSourceRange(), |
| 2894 | E->getLocStart(), /*IsStringLocation=*/false, |
| 2895 | SpecRange, Hints); |
| 2896 | } else { |
| 2897 | // In this case, the expression could be printed using a different |
| 2898 | // specifier, but we've decided that the specifier is probably correct |
| 2899 | // and we should cast instead. Just use the normal warning message. |
| 2900 | EmitFormatDiagnostic( |
| 2901 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
| 2902 | << AT.getRepresentativeTypeName(S.Context) << ExprTy |
| 2903 | << E->getSourceRange(), |
| 2904 | E->getLocStart(), /*IsStringLocation*/false, |
| 2905 | SpecRange, Hints); |
| 2906 | } |
Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2907 | } |
Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2908 | } else { |
| 2909 | const CharSourceRange &CSR = getSpecifierRange(StartSpecifier, |
| 2910 | SpecifierLen); |
| 2911 | // Since the warning for passing non-POD types to variadic functions |
| 2912 | // was deferred until now, we emit a warning for non-POD |
| 2913 | // arguments here. |
Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 2914 | if (S.isValidVarArgType(ExprTy) == Sema::VAK_Invalid) { |
Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2915 | unsigned DiagKind; |
Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 2916 | if (ExprTy->isObjCObjectType()) |
Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2917 | DiagKind = diag::err_cannot_pass_objc_interface_to_vararg_format; |
| 2918 | else |
| 2919 | DiagKind = diag::warn_non_pod_vararg_with_format_string; |
| 2920 | |
| 2921 | EmitFormatDiagnostic( |
| 2922 | S.PDiag(DiagKind) |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2923 | << S.getLangOpts().CPlusPlus11 |
Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 2924 | << ExprTy |
Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2925 | << CallType |
| 2926 | << AT.getRepresentativeTypeName(S.Context) |
| 2927 | << CSR |
| 2928 | << E->getSourceRange(), |
| 2929 | E->getLocStart(), /*IsStringLocation*/false, CSR); |
| 2930 | |
| 2931 | checkForCStrMembers(AT, E, CSR); |
| 2932 | } else |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2933 | EmitFormatDiagnostic( |
| 2934 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 2935 | << AT.getRepresentativeTypeName(S.Context) << ExprTy |
Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2936 | << CSR |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2937 | << E->getSourceRange(), |
Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2938 | E->getLocStart(), /*IsStringLocation*/false, CSR); |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2941 | return true; |
| 2942 | } |
| 2943 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2944 | //===--- CHECK: Scanf format string checking ------------------------------===// |
| 2945 | |
| 2946 | namespace { |
| 2947 | class CheckScanfHandler : public CheckFormatHandler { |
| 2948 | public: |
| 2949 | CheckScanfHandler(Sema &s, const StringLiteral *fexpr, |
| 2950 | const Expr *origFormatExpr, unsigned firstDataArg, |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2951 | unsigned numDataArgs, const char *beg, bool hasVAListArg, |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2952 | ArrayRef<const Expr *> Args, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2953 | unsigned formatIdx, bool inFunctionCall, |
| 2954 | Sema::VariadicCallType CallType) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2955 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2956 | numDataArgs, beg, hasVAListArg, |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2957 | Args, formatIdx, inFunctionCall, CallType) |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2958 | {} |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2959 | |
| 2960 | bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, |
| 2961 | const char *startSpecifier, |
| 2962 | unsigned specifierLen); |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2963 | |
| 2964 | bool HandleInvalidScanfConversionSpecifier( |
| 2965 | const analyze_scanf::ScanfSpecifier &FS, |
| 2966 | const char *startSpecifier, |
| 2967 | unsigned specifierLen); |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2968 | |
| 2969 | void HandleIncompleteScanList(const char *start, const char *end); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2970 | }; |
Ted Kremenek | 07d161f | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 2971 | } |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2972 | |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2973 | void CheckScanfHandler::HandleIncompleteScanList(const char *start, |
| 2974 | const char *end) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2975 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete), |
| 2976 | getLocationOfByte(end), /*IsStringLocation*/true, |
| 2977 | getSpecifierRange(start, end - start)); |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2978 | } |
| 2979 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2980 | bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( |
| 2981 | const analyze_scanf::ScanfSpecifier &FS, |
| 2982 | const char *startSpecifier, |
| 2983 | unsigned specifierLen) { |
| 2984 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2985 | const analyze_scanf::ScanfConversionSpecifier &CS = |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2986 | FS.getConversionSpecifier(); |
| 2987 | |
| 2988 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 2989 | getLocationOfByte(CS.getStart()), |
| 2990 | startSpecifier, specifierLen, |
| 2991 | CS.getStart(), CS.getLength()); |
| 2992 | } |
| 2993 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2994 | bool CheckScanfHandler::HandleScanfSpecifier( |
| 2995 | const analyze_scanf::ScanfSpecifier &FS, |
| 2996 | const char *startSpecifier, |
| 2997 | unsigned specifierLen) { |
| 2998 | |
| 2999 | using namespace analyze_scanf; |
| 3000 | using namespace analyze_format_string; |
| 3001 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 3002 | const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3003 | |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 3004 | // Handle case where '%' and '*' don't consume an argument. These shouldn't |
| 3005 | // be used to decide if we are using positional arguments consistently. |
| 3006 | if (FS.consumesDataArgument()) { |
| 3007 | if (atFirstArg) { |
| 3008 | atFirstArg = false; |
| 3009 | usesPositionalArgs = FS.usesPositionalArg(); |
| 3010 | } |
| 3011 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3012 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), |
| 3013 | startSpecifier, specifierLen); |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 3014 | return false; |
| 3015 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
| 3018 | // Check if the field with is non-zero. |
| 3019 | const OptionalAmount &Amt = FS.getFieldWidth(); |
| 3020 | if (Amt.getHowSpecified() == OptionalAmount::Constant) { |
| 3021 | if (Amt.getConstantAmount() == 0) { |
| 3022 | const CharSourceRange &R = getSpecifierRange(Amt.getStart(), |
| 3023 | Amt.getConstantLength()); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3024 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width), |
| 3025 | getLocationOfByte(Amt.getStart()), |
| 3026 | /*IsStringLocation*/true, R, |
| 3027 | FixItHint::CreateRemoval(R)); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3028 | } |
| 3029 | } |
| 3030 | |
| 3031 | if (!FS.consumesDataArgument()) { |
| 3032 | // FIXME: Technically specifying a precision or field width here |
| 3033 | // makes no sense. Worth issuing a warning at some point. |
| 3034 | return true; |
| 3035 | } |
| 3036 | |
| 3037 | // Consume the argument. |
| 3038 | unsigned argIndex = FS.getArgIndex(); |
| 3039 | if (argIndex < NumDataArgs) { |
| 3040 | // The check to see if the argIndex is valid will come later. |
| 3041 | // We set the bit here because we may exit early from this |
| 3042 | // function if we encounter some other error. |
| 3043 | CoveredArgs.set(argIndex); |
| 3044 | } |
| 3045 | |
Ted Kremenek | 1e51c20 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 3046 | // Check the length modifier is valid with the given conversion specifier. |
Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3047 | if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo())) |
Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3048 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
| 3049 | diag::warn_format_nonsensical_length); |
Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3050 | else if (!FS.hasStandardLengthModifier()) |
Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3051 | HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); |
Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3052 | else if (!FS.hasStandardLengthConversionCombination()) |
Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3053 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
| 3054 | diag::warn_format_non_standard_conversion_spec); |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 3055 | |
Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3056 | if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) |
| 3057 | HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); |
| 3058 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3059 | // The remaining checks depend on the data arguments. |
| 3060 | if (HasVAListArg) |
| 3061 | return true; |
| 3062 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 3063 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3064 | return false; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3065 | |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3066 | // Check that the argument type matches the format specifier. |
| 3067 | const Expr *Ex = getDataArg(argIndex); |
Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 3068 | if (!Ex) |
| 3069 | return true; |
| 3070 | |
Hans Wennborg | 58e1e54 | 2012-08-07 08:59:46 +0000 | [diff] [blame] | 3071 | const analyze_format_string::ArgType &AT = FS.getArgType(S.Context); |
| 3072 | if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType())) { |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3073 | ScanfSpecifier fixedFS = FS; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3074 | bool success = fixedFS.fixType(Ex->getType(), S.getLangOpts(), |
Hans Wennborg | be6126a | 2012-02-15 09:59:46 +0000 | [diff] [blame] | 3075 | S.Context); |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3076 | |
| 3077 | if (success) { |
| 3078 | // Get the fix string from the fixed format specifier. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3079 | SmallString<128> buf; |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3080 | llvm::raw_svector_ostream os(buf); |
| 3081 | fixedFS.toString(os); |
| 3082 | |
| 3083 | EmitFormatDiagnostic( |
| 3084 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
Hans Wennborg | 58e1e54 | 2012-08-07 08:59:46 +0000 | [diff] [blame] | 3085 | << AT.getRepresentativeTypeName(S.Context) << Ex->getType() |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3086 | << Ex->getSourceRange(), |
Matt Beaumont-Gay | abf145a | 2012-05-17 00:03:16 +0000 | [diff] [blame] | 3087 | Ex->getLocStart(), |
| 3088 | /*IsStringLocation*/false, |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3089 | getSpecifierRange(startSpecifier, specifierLen), |
| 3090 | FixItHint::CreateReplacement( |
| 3091 | getSpecifierRange(startSpecifier, specifierLen), |
| 3092 | os.str())); |
| 3093 | } else { |
Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 3094 | EmitFormatDiagnostic( |
| 3095 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
Hans Wennborg | 58e1e54 | 2012-08-07 08:59:46 +0000 | [diff] [blame] | 3096 | << AT.getRepresentativeTypeName(S.Context) << Ex->getType() |
Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 3097 | << Ex->getSourceRange(), |
Matt Beaumont-Gay | abf145a | 2012-05-17 00:03:16 +0000 | [diff] [blame] | 3098 | Ex->getLocStart(), |
| 3099 | /*IsStringLocation*/false, |
Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 3100 | getSpecifierRange(startSpecifier, specifierLen)); |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3101 | } |
| 3102 | } |
| 3103 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3104 | return true; |
| 3105 | } |
| 3106 | |
| 3107 | void Sema::CheckFormatString(const StringLiteral *FExpr, |
Ted Kremenek | 0e5675d | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 3108 | const Expr *OrigFormatExpr, |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 3109 | ArrayRef<const Expr *> Args, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3110 | bool HasVAListArg, unsigned format_idx, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 3111 | unsigned firstDataArg, FormatStringType Type, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3112 | bool inFunctionCall, VariadicCallType CallType) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3113 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3114 | // CHECK: is the format string a wide literal? |
Richard Smith | df9ef1b | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 3115 | if (!FExpr->isAscii() && !FExpr->isUTF8()) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3116 | CheckFormatHandler::EmitFormatDiagnostic( |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3117 | *this, inFunctionCall, Args[format_idx], |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3118 | PDiag(diag::warn_format_string_is_wide_literal), FExpr->getLocStart(), |
| 3119 | /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3120 | return; |
| 3121 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3122 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3123 | // Str - The format string. NOTE: this is NOT null-terminated! |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3124 | StringRef StrRef = FExpr->getString(); |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 3125 | const char *Str = StrRef.data(); |
| 3126 | unsigned StrLen = StrRef.size(); |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 3127 | const unsigned numDataArgs = Args.size() - firstDataArg; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3128 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3129 | // CHECK: empty format string? |
Ted Kremenek | 4cd5791 | 2011-09-29 05:52:16 +0000 | [diff] [blame] | 3130 | if (StrLen == 0 && numDataArgs > 0) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3131 | CheckFormatHandler::EmitFormatDiagnostic( |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3132 | *this, inFunctionCall, Args[format_idx], |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3133 | PDiag(diag::warn_empty_format_string), FExpr->getLocStart(), |
| 3134 | /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3135 | return; |
| 3136 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3137 | |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 3138 | if (Type == FST_Printf || Type == FST_NSString) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3139 | CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 3140 | numDataArgs, (Type == FST_NSString), |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 3141 | Str, HasVAListArg, Args, format_idx, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3142 | inFunctionCall, CallType); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3143 | |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 3144 | if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen, |
Jordan Rose | 275b6f5 | 2012-09-13 02:11:03 +0000 | [diff] [blame] | 3145 | getLangOpts(), |
| 3146 | Context.getTargetInfo())) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3147 | H.DoneProcessing(); |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 3148 | } else if (Type == FST_Scanf) { |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 3149 | CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, numDataArgs, |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 3150 | Str, HasVAListArg, Args, format_idx, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3151 | inFunctionCall, CallType); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3152 | |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 3153 | if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen, |
Jordan Rose | 275b6f5 | 2012-09-13 02:11:03 +0000 | [diff] [blame] | 3154 | getLangOpts(), |
| 3155 | Context.getTargetInfo())) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3156 | H.DoneProcessing(); |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 3157 | } // TODO: handle other formats |
Ted Kremenek | ce7024e | 2010-01-28 01:18:22 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 3160 | //===--- CHECK: Standard memory functions ---------------------------------===// |
| 3161 | |
Douglas Gregor | 2a053a3 | 2011-05-03 20:05:22 +0000 | [diff] [blame] | 3162 | /// \brief Determine whether the given type is a dynamic class type (e.g., |
| 3163 | /// whether it has a vtable). |
| 3164 | static bool isDynamicClassType(QualType T) { |
| 3165 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
| 3166 | if (CXXRecordDecl *Definition = Record->getDefinition()) |
| 3167 | if (Definition->isDynamicClass()) |
| 3168 | return true; |
| 3169 | |
| 3170 | return false; |
| 3171 | } |
| 3172 | |
Chandler Carruth | a72a12f | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 3173 | /// \brief If E is a sizeof expression, returns its argument expression, |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3174 | /// otherwise returns NULL. |
| 3175 | static const Expr *getSizeOfExprArg(const Expr* E) { |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 3176 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3177 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
| 3178 | if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType()) |
| 3179 | return SizeOf->getArgumentExpr()->IgnoreParenImpCasts(); |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 3180 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3181 | return 0; |
| 3182 | } |
| 3183 | |
Chandler Carruth | a72a12f | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 3184 | /// \brief If E is a sizeof expression, returns its argument type. |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3185 | static QualType getSizeOfArgType(const Expr* E) { |
| 3186 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
| 3187 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
| 3188 | if (SizeOf->getKind() == clang::UETT_SizeOf) |
| 3189 | return SizeOf->getTypeOfArgument(); |
| 3190 | |
| 3191 | return QualType(); |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 3192 | } |
| 3193 | |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 3194 | /// \brief Check for dangerous or invalid arguments to memset(). |
| 3195 | /// |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 3196 | /// This issues warnings on known problematic, dangerous or unspecified |
Matt Beaumont-Gay | cc2f30c | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 3197 | /// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp' |
| 3198 | /// function calls. |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 3199 | /// |
| 3200 | /// \param Call The call expression to diagnose. |
Matt Beaumont-Gay | cc2f30c | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 3201 | void Sema::CheckMemaccessArguments(const CallExpr *Call, |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3202 | unsigned BId, |
Matt Beaumont-Gay | cc2f30c | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 3203 | IdentifierInfo *FnName) { |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3204 | assert(BId != 0); |
| 3205 | |
Ted Kremenek | 1d59f7f | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 3206 | // It is possible to have a non-standard definition of memset. Validate |
Douglas Gregor | 707a23e | 2011-06-16 17:56:04 +0000 | [diff] [blame] | 3207 | // we have enough arguments, and if not, abort further checking. |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3208 | unsigned ExpectedNumArgs = (BId == Builtin::BIstrndup ? 2 : 3); |
Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 3209 | if (Call->getNumArgs() < ExpectedNumArgs) |
Ted Kremenek | 1d59f7f | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 3210 | return; |
| 3211 | |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3212 | unsigned LastArg = (BId == Builtin::BImemset || |
| 3213 | BId == Builtin::BIstrndup ? 1 : 2); |
| 3214 | unsigned LenArg = (BId == Builtin::BIstrndup ? 1 : 2); |
Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 3215 | const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts(); |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3216 | |
| 3217 | // We have special checking when the length is a sizeof expression. |
| 3218 | QualType SizeOfArgTy = getSizeOfArgType(LenExpr); |
| 3219 | const Expr *SizeOfArg = getSizeOfExprArg(LenExpr); |
| 3220 | llvm::FoldingSetNodeID SizeOfArgID; |
| 3221 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 3222 | for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) { |
| 3223 | const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts(); |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 3224 | SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange(); |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 3225 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 3226 | QualType DestTy = Dest->getType(); |
| 3227 | if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) { |
| 3228 | QualType PointeeTy = DestPtrTy->getPointeeType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3229 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3230 | // Never warn about void type pointers. This can be used to suppress |
| 3231 | // false positives. |
| 3232 | if (PointeeTy->isVoidType()) |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 3233 | continue; |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 3234 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3235 | // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by |
| 3236 | // actually comparing the expressions for equality. Because computing the |
| 3237 | // expression IDs can be expensive, we only do this if the diagnostic is |
| 3238 | // enabled. |
| 3239 | if (SizeOfArg && |
| 3240 | Diags.getDiagnosticLevel(diag::warn_sizeof_pointer_expr_memaccess, |
| 3241 | SizeOfArg->getExprLoc())) { |
| 3242 | // We only compute IDs for expressions if the warning is enabled, and |
| 3243 | // cache the sizeof arg's ID. |
| 3244 | if (SizeOfArgID == llvm::FoldingSetNodeID()) |
| 3245 | SizeOfArg->Profile(SizeOfArgID, Context, true); |
| 3246 | llvm::FoldingSetNodeID DestID; |
| 3247 | Dest->Profile(DestID, Context, true); |
| 3248 | if (DestID == SizeOfArgID) { |
Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 3249 | // TODO: For strncpy() and friends, this could suggest sizeof(dst) |
| 3250 | // over sizeof(src) as well. |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3251 | unsigned ActionIdx = 0; // Default is to suggest dereferencing. |
Anna Zaks | 6fcb372 | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 3252 | StringRef ReadableName = FnName->getName(); |
| 3253 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3254 | if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest)) |
Anna Zaks | 90c7832 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 3255 | if (UnaryOp->getOpcode() == UO_AddrOf) |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3256 | ActionIdx = 1; // If its an address-of operator, just remove it. |
Fariborz Jahanian | 7adf417 | 2013-01-30 01:12:44 +0000 | [diff] [blame] | 3257 | if (!PointeeTy->isIncompleteType() && |
| 3258 | (Context.getTypeSize(PointeeTy) == Context.getCharWidth())) |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3259 | ActionIdx = 2; // If the pointee's size is sizeof(char), |
| 3260 | // suggest an explicit length. |
Anna Zaks | 6fcb372 | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 3261 | |
| 3262 | // If the function is defined as a builtin macro, do not show macro |
| 3263 | // expansion. |
| 3264 | SourceLocation SL = SizeOfArg->getExprLoc(); |
| 3265 | SourceRange DSR = Dest->getSourceRange(); |
| 3266 | SourceRange SSR = SizeOfArg->getSourceRange(); |
| 3267 | SourceManager &SM = PP.getSourceManager(); |
| 3268 | |
| 3269 | if (SM.isMacroArgExpansion(SL)) { |
| 3270 | ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts); |
| 3271 | SL = SM.getSpellingLoc(SL); |
| 3272 | DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()), |
| 3273 | SM.getSpellingLoc(DSR.getEnd())); |
| 3274 | SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()), |
| 3275 | SM.getSpellingLoc(SSR.getEnd())); |
| 3276 | } |
| 3277 | |
Anna Zaks | 90c7832 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 3278 | DiagRuntimeBehavior(SL, SizeOfArg, |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3279 | PDiag(diag::warn_sizeof_pointer_expr_memaccess) |
Anna Zaks | 6fcb372 | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 3280 | << ReadableName |
Anna Zaks | 90c7832 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 3281 | << PointeeTy |
| 3282 | << DestTy |
Anna Zaks | 6fcb372 | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 3283 | << DSR |
Anna Zaks | 90c7832 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 3284 | << SSR); |
| 3285 | DiagRuntimeBehavior(SL, SizeOfArg, |
| 3286 | PDiag(diag::warn_sizeof_pointer_expr_memaccess_note) |
| 3287 | << ActionIdx |
| 3288 | << SSR); |
| 3289 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3290 | break; |
| 3291 | } |
| 3292 | } |
| 3293 | |
| 3294 | // Also check for cases where the sizeof argument is the exact same |
| 3295 | // type as the memory argument, and where it points to a user-defined |
| 3296 | // record type. |
| 3297 | if (SizeOfArgTy != QualType()) { |
| 3298 | if (PointeeTy->isRecordType() && |
| 3299 | Context.typesAreCompatible(SizeOfArgTy, DestTy)) { |
| 3300 | DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest, |
| 3301 | PDiag(diag::warn_sizeof_pointer_type_memaccess) |
| 3302 | << FnName << SizeOfArgTy << ArgIdx |
| 3303 | << PointeeTy << Dest->getSourceRange() |
| 3304 | << LenExpr->getSourceRange()); |
| 3305 | break; |
| 3306 | } |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 3309 | // Always complain about dynamic classes. |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3310 | if (isDynamicClassType(PointeeTy)) { |
| 3311 | |
| 3312 | unsigned OperationType = 0; |
| 3313 | // "overwritten" if we're warning about the destination for any call |
| 3314 | // but memcmp; otherwise a verb appropriate to the call. |
| 3315 | if (ArgIdx != 0 || BId == Builtin::BImemcmp) { |
| 3316 | if (BId == Builtin::BImemcpy) |
| 3317 | OperationType = 1; |
| 3318 | else if(BId == Builtin::BImemmove) |
| 3319 | OperationType = 2; |
| 3320 | else if (BId == Builtin::BImemcmp) |
| 3321 | OperationType = 3; |
| 3322 | } |
| 3323 | |
Matt Beaumont-Gay | 5c5218e | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 3324 | DiagRuntimeBehavior( |
| 3325 | Dest->getExprLoc(), Dest, |
| 3326 | PDiag(diag::warn_dyn_class_memaccess) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3327 | << (BId == Builtin::BImemcmp ? ArgIdx + 2 : ArgIdx) |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3328 | << FnName << PointeeTy |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3329 | << OperationType |
Matt Beaumont-Gay | 5c5218e | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 3330 | << Call->getCallee()->getSourceRange()); |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3331 | } else if (PointeeTy.hasNonTrivialObjCLifetime() && |
| 3332 | BId != Builtin::BImemset) |
Matt Beaumont-Gay | 5c5218e | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 3333 | DiagRuntimeBehavior( |
| 3334 | Dest->getExprLoc(), Dest, |
| 3335 | PDiag(diag::warn_arc_object_memaccess) |
| 3336 | << ArgIdx << FnName << PointeeTy |
| 3337 | << Call->getCallee()->getSourceRange()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3338 | else |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 3339 | continue; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3340 | |
| 3341 | DiagRuntimeBehavior( |
| 3342 | Dest->getExprLoc(), Dest, |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 3343 | PDiag(diag::note_bad_memaccess_silence) |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 3344 | << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)")); |
| 3345 | break; |
| 3346 | } |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 3347 | } |
| 3348 | } |
| 3349 | |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 3350 | // A little helper routine: ignore addition and subtraction of integer literals. |
| 3351 | // This intentionally does not ignore all integer constant expressions because |
| 3352 | // we don't want to remove sizeof(). |
| 3353 | static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) { |
| 3354 | Ex = Ex->IgnoreParenCasts(); |
| 3355 | |
| 3356 | for (;;) { |
| 3357 | const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex); |
| 3358 | if (!BO || !BO->isAdditiveOp()) |
| 3359 | break; |
| 3360 | |
| 3361 | const Expr *RHS = BO->getRHS()->IgnoreParenCasts(); |
| 3362 | const Expr *LHS = BO->getLHS()->IgnoreParenCasts(); |
| 3363 | |
| 3364 | if (isa<IntegerLiteral>(RHS)) |
| 3365 | Ex = LHS; |
| 3366 | else if (isa<IntegerLiteral>(LHS)) |
| 3367 | Ex = RHS; |
| 3368 | else |
| 3369 | break; |
| 3370 | } |
| 3371 | |
| 3372 | return Ex; |
| 3373 | } |
| 3374 | |
Anna Zaks | 0f38ace | 2012-08-08 21:42:23 +0000 | [diff] [blame] | 3375 | static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty, |
| 3376 | ASTContext &Context) { |
| 3377 | // Only handle constant-sized or VLAs, but not flexible members. |
| 3378 | if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) { |
| 3379 | // Only issue the FIXIT for arrays of size > 1. |
| 3380 | if (CAT->getSize().getSExtValue() <= 1) |
| 3381 | return false; |
| 3382 | } else if (!Ty->isVariableArrayType()) { |
| 3383 | return false; |
| 3384 | } |
| 3385 | return true; |
| 3386 | } |
| 3387 | |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 3388 | // Warn if the user has made the 'size' argument to strlcpy or strlcat |
| 3389 | // be the size of the source, instead of the destination. |
| 3390 | void Sema::CheckStrlcpycatArguments(const CallExpr *Call, |
| 3391 | IdentifierInfo *FnName) { |
| 3392 | |
| 3393 | // Don't crash if the user has the wrong number of arguments |
| 3394 | if (Call->getNumArgs() != 3) |
| 3395 | return; |
| 3396 | |
| 3397 | const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context); |
| 3398 | const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context); |
| 3399 | const Expr *CompareWithSrc = NULL; |
| 3400 | |
| 3401 | // Look for 'strlcpy(dst, x, sizeof(x))' |
| 3402 | if (const Expr *Ex = getSizeOfExprArg(SizeArg)) |
| 3403 | CompareWithSrc = Ex; |
| 3404 | else { |
| 3405 | // Look for 'strlcpy(dst, x, strlen(x))' |
| 3406 | if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3407 | if (SizeCall->isBuiltinCall() == Builtin::BIstrlen |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 3408 | && SizeCall->getNumArgs() == 1) |
| 3409 | CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context); |
| 3410 | } |
| 3411 | } |
| 3412 | |
| 3413 | if (!CompareWithSrc) |
| 3414 | return; |
| 3415 | |
| 3416 | // Determine if the argument to sizeof/strlen is equal to the source |
| 3417 | // argument. In principle there's all kinds of things you could do |
| 3418 | // here, for instance creating an == expression and evaluating it with |
| 3419 | // EvaluateAsBooleanCondition, but this uses a more direct technique: |
| 3420 | const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg); |
| 3421 | if (!SrcArgDRE) |
| 3422 | return; |
| 3423 | |
| 3424 | const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc); |
| 3425 | if (!CompareWithSrcDRE || |
| 3426 | SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl()) |
| 3427 | return; |
| 3428 | |
| 3429 | const Expr *OriginalSizeArg = Call->getArg(2); |
| 3430 | Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size) |
| 3431 | << OriginalSizeArg->getSourceRange() << FnName; |
| 3432 | |
| 3433 | // Output a FIXIT hint if the destination is an array (rather than a |
| 3434 | // pointer to an array). This could be enhanced to handle some |
| 3435 | // pointers if we know the actual size, like if DstArg is 'array+2' |
| 3436 | // we could say 'sizeof(array)-2'. |
| 3437 | const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts(); |
Anna Zaks | 0f38ace | 2012-08-08 21:42:23 +0000 | [diff] [blame] | 3438 | if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context)) |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 3439 | return; |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 3440 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3441 | SmallString<128> sizeString; |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 3442 | llvm::raw_svector_ostream OS(sizeString); |
| 3443 | OS << "sizeof("; |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 3444 | DstArg->printPretty(OS, 0, getPrintingPolicy()); |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 3445 | OS << ")"; |
| 3446 | |
| 3447 | Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size) |
| 3448 | << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(), |
| 3449 | OS.str()); |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3452 | /// Check if two expressions refer to the same declaration. |
| 3453 | static bool referToTheSameDecl(const Expr *E1, const Expr *E2) { |
| 3454 | if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1)) |
| 3455 | if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2)) |
| 3456 | return D1->getDecl() == D2->getDecl(); |
| 3457 | return false; |
| 3458 | } |
| 3459 | |
| 3460 | static const Expr *getStrlenExprArg(const Expr *E) { |
| 3461 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { |
| 3462 | const FunctionDecl *FD = CE->getDirectCallee(); |
| 3463 | if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen) |
| 3464 | return 0; |
| 3465 | return CE->getArg(0)->IgnoreParenCasts(); |
| 3466 | } |
| 3467 | return 0; |
| 3468 | } |
| 3469 | |
| 3470 | // Warn on anti-patterns as the 'size' argument to strncat. |
| 3471 | // The correct size argument should look like following: |
| 3472 | // strncat(dst, src, sizeof(dst) - strlen(dest) - 1); |
| 3473 | void Sema::CheckStrncatArguments(const CallExpr *CE, |
| 3474 | IdentifierInfo *FnName) { |
| 3475 | // Don't crash if the user has the wrong number of arguments. |
| 3476 | if (CE->getNumArgs() < 3) |
| 3477 | return; |
| 3478 | const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts(); |
| 3479 | const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts(); |
| 3480 | const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts(); |
| 3481 | |
| 3482 | // Identify common expressions, which are wrongly used as the size argument |
| 3483 | // to strncat and may lead to buffer overflows. |
| 3484 | unsigned PatternType = 0; |
| 3485 | if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) { |
| 3486 | // - sizeof(dst) |
| 3487 | if (referToTheSameDecl(SizeOfArg, DstArg)) |
| 3488 | PatternType = 1; |
| 3489 | // - sizeof(src) |
| 3490 | else if (referToTheSameDecl(SizeOfArg, SrcArg)) |
| 3491 | PatternType = 2; |
| 3492 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) { |
| 3493 | if (BE->getOpcode() == BO_Sub) { |
| 3494 | const Expr *L = BE->getLHS()->IgnoreParenCasts(); |
| 3495 | const Expr *R = BE->getRHS()->IgnoreParenCasts(); |
| 3496 | // - sizeof(dst) - strlen(dst) |
| 3497 | if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) && |
| 3498 | referToTheSameDecl(DstArg, getStrlenExprArg(R))) |
| 3499 | PatternType = 1; |
| 3500 | // - sizeof(src) - (anything) |
| 3501 | else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L))) |
| 3502 | PatternType = 2; |
| 3503 | } |
| 3504 | } |
| 3505 | |
| 3506 | if (PatternType == 0) |
| 3507 | return; |
| 3508 | |
Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 3509 | // Generate the diagnostic. |
| 3510 | SourceLocation SL = LenArg->getLocStart(); |
| 3511 | SourceRange SR = LenArg->getSourceRange(); |
| 3512 | SourceManager &SM = PP.getSourceManager(); |
| 3513 | |
| 3514 | // If the function is defined as a builtin macro, do not show macro expansion. |
| 3515 | if (SM.isMacroArgExpansion(SL)) { |
| 3516 | SL = SM.getSpellingLoc(SL); |
| 3517 | SR = SourceRange(SM.getSpellingLoc(SR.getBegin()), |
| 3518 | SM.getSpellingLoc(SR.getEnd())); |
| 3519 | } |
| 3520 | |
Anna Zaks | 0f38ace | 2012-08-08 21:42:23 +0000 | [diff] [blame] | 3521 | // Check if the destination is an array (rather than a pointer to an array). |
| 3522 | QualType DstTy = DstArg->getType(); |
| 3523 | bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy, |
| 3524 | Context); |
| 3525 | if (!isKnownSizeArray) { |
| 3526 | if (PatternType == 1) |
| 3527 | Diag(SL, diag::warn_strncat_wrong_size) << SR; |
| 3528 | else |
| 3529 | Diag(SL, diag::warn_strncat_src_size) << SR; |
| 3530 | return; |
| 3531 | } |
| 3532 | |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3533 | if (PatternType == 1) |
Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 3534 | Diag(SL, diag::warn_strncat_large_size) << SR; |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3535 | else |
Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 3536 | Diag(SL, diag::warn_strncat_src_size) << SR; |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3537 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3538 | SmallString<128> sizeString; |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3539 | llvm::raw_svector_ostream OS(sizeString); |
| 3540 | OS << "sizeof("; |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 3541 | DstArg->printPretty(OS, 0, getPrintingPolicy()); |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3542 | OS << ") - "; |
| 3543 | OS << "strlen("; |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 3544 | DstArg->printPretty(OS, 0, getPrintingPolicy()); |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3545 | OS << ") - 1"; |
| 3546 | |
Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 3547 | Diag(SL, diag::note_strncat_wrong_size) |
| 3548 | << FixItHint::CreateReplacement(SR, OS.str()); |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3549 | } |
| 3550 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3551 | //===--- CHECK: Return Address of Stack Variable --------------------------===// |
| 3552 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3553 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 3554 | Decl *ParentDecl); |
| 3555 | static Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 3556 | Decl *ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3557 | |
| 3558 | /// CheckReturnStackAddr - Check if a return statement returns the address |
| 3559 | /// of a stack variable. |
| 3560 | void |
| 3561 | Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, |
| 3562 | SourceLocation ReturnLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3563 | |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3564 | Expr *stackE = 0; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3565 | SmallVector<DeclRefExpr *, 8> refVars; |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3566 | |
| 3567 | // Perform checking for returned stack addresses, local blocks, |
| 3568 | // label addresses or references to temporaries. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3569 | if (lhsType->isPointerType() || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3570 | (!getLangOpts().ObjCAutoRefCount && lhsType->isBlockPointerType())) { |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3571 | stackE = EvalAddr(RetValExp, refVars, /*ParentDecl=*/0); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3572 | } else if (lhsType->isReferenceType()) { |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3573 | stackE = EvalVal(RetValExp, refVars, /*ParentDecl=*/0); |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
| 3576 | if (stackE == 0) |
| 3577 | return; // Nothing suspicious was found. |
| 3578 | |
| 3579 | SourceLocation diagLoc; |
| 3580 | SourceRange diagRange; |
| 3581 | if (refVars.empty()) { |
| 3582 | diagLoc = stackE->getLocStart(); |
| 3583 | diagRange = stackE->getSourceRange(); |
| 3584 | } else { |
| 3585 | // We followed through a reference variable. 'stackE' contains the |
| 3586 | // problematic expression but we will warn at the return statement pointing |
| 3587 | // at the reference variable. We will later display the "trail" of |
| 3588 | // reference variables using notes. |
| 3589 | diagLoc = refVars[0]->getLocStart(); |
| 3590 | diagRange = refVars[0]->getSourceRange(); |
| 3591 | } |
| 3592 | |
| 3593 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var. |
| 3594 | Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref |
| 3595 | : diag::warn_ret_stack_addr) |
| 3596 | << DR->getDecl()->getDeclName() << diagRange; |
| 3597 | } else if (isa<BlockExpr>(stackE)) { // local block. |
| 3598 | Diag(diagLoc, diag::err_ret_local_block) << diagRange; |
| 3599 | } else if (isa<AddrLabelExpr>(stackE)) { // address of label. |
| 3600 | Diag(diagLoc, diag::warn_ret_addr_label) << diagRange; |
| 3601 | } else { // local temporary. |
| 3602 | Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref |
| 3603 | : diag::warn_ret_local_temp_addr) |
| 3604 | << diagRange; |
| 3605 | } |
| 3606 | |
| 3607 | // Display the "trail" of reference variables that we followed until we |
| 3608 | // found the problematic expression using notes. |
| 3609 | for (unsigned i = 0, e = refVars.size(); i != e; ++i) { |
| 3610 | VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl()); |
| 3611 | // If this var binds to another reference var, show the range of the next |
| 3612 | // var, otherwise the var binds to the problematic expression, in which case |
| 3613 | // show the range of the expression. |
| 3614 | SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange() |
| 3615 | : stackE->getSourceRange(); |
| 3616 | Diag(VD->getLocation(), diag::note_ref_var_local_bind) |
| 3617 | << VD->getDeclName() << range; |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3618 | } |
| 3619 | } |
| 3620 | |
| 3621 | /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that |
| 3622 | /// check if the expression in a return statement evaluates to an address |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3623 | /// to a location on the stack, a local block, an address of a label, or a |
| 3624 | /// reference to local temporary. The recursion is used to traverse the |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3625 | /// AST of the return expression, with recursion backtracking when we |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3626 | /// encounter a subexpression that (1) clearly does not lead to one of the |
| 3627 | /// above problematic expressions (2) is something we cannot determine leads to |
| 3628 | /// a problematic expression based on such local checking. |
| 3629 | /// |
| 3630 | /// Both EvalAddr and EvalVal follow through reference variables to evaluate |
| 3631 | /// the expression that they point to. Such variables are added to the |
| 3632 | /// 'refVars' vector so that we know what the reference variable "trail" was. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3633 | /// |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 3634 | /// EvalAddr processes expressions that are pointers that are used as |
| 3635 | /// references (and not L-values). EvalVal handles all other values. |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3636 | /// At the base case of the recursion is a check for the above problematic |
| 3637 | /// expressions. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3638 | /// |
| 3639 | /// This implementation handles: |
| 3640 | /// |
| 3641 | /// * pointer-to-pointer casts |
| 3642 | /// * implicit conversions from array references to pointers |
| 3643 | /// * taking the address of fields |
| 3644 | /// * arbitrary interplay between "&" and "*" operators |
| 3645 | /// * pointer arithmetic from an address of a stack variable |
| 3646 | /// * taking the address of an array element where the array is on the stack |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3647 | static Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 3648 | Decl *ParentDecl) { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3649 | if (E->isTypeDependent()) |
| 3650 | return NULL; |
| 3651 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3652 | // We should only be called for evaluating pointer expressions. |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3653 | assert((E->getType()->isAnyPointerType() || |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3654 | E->getType()->isBlockPointerType() || |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3655 | E->getType()->isObjCQualifiedIdType()) && |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3656 | "EvalAddr only works on pointers"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3657 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3658 | E = E->IgnoreParens(); |
| 3659 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3660 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 3661 | // viewed AST node. We then recursively traverse the AST by calling |
| 3662 | // EvalAddr and EvalVal appropriately. |
| 3663 | switch (E->getStmtClass()) { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3664 | case Stmt::DeclRefExprClass: { |
| 3665 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
| 3666 | |
| 3667 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
| 3668 | // If this is a reference variable, follow through to the expression that |
| 3669 | // it points to. |
| 3670 | if (V->hasLocalStorage() && |
| 3671 | V->getType()->isReferenceType() && V->hasInit()) { |
| 3672 | // Add the reference variable to the "trail". |
| 3673 | refVars.push_back(DR); |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3674 | return EvalAddr(V->getInit(), refVars, ParentDecl); |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | return NULL; |
| 3678 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3679 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3680 | case Stmt::UnaryOperatorClass: { |
| 3681 | // The only unary operator that make sense to handle here |
| 3682 | // is AddrOf. All others don't make sense as pointers. |
| 3683 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3684 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3685 | if (U->getOpcode() == UO_AddrOf) |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3686 | return EvalVal(U->getSubExpr(), refVars, ParentDecl); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3687 | else |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3688 | return NULL; |
| 3689 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3690 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3691 | case Stmt::BinaryOperatorClass: { |
| 3692 | // Handle pointer arithmetic. All other binary operators are not valid |
| 3693 | // in this context. |
| 3694 | BinaryOperator *B = cast<BinaryOperator>(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3695 | BinaryOperatorKind op = B->getOpcode(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3696 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3697 | if (op != BO_Add && op != BO_Sub) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3698 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3699 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3700 | Expr *Base = B->getLHS(); |
| 3701 | |
| 3702 | // Determine which argument is the real pointer base. It could be |
| 3703 | // the RHS argument instead of the LHS. |
| 3704 | if (!Base->getType()->isPointerType()) Base = B->getRHS(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3705 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3706 | assert (Base->getType()->isPointerType()); |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3707 | return EvalAddr(Base, refVars, ParentDecl); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3708 | } |
Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 3709 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3710 | // For conditional operators we need to see if either the LHS or RHS are |
| 3711 | // valid DeclRefExpr*s. If one of them is valid, we return it. |
| 3712 | case Stmt::ConditionalOperatorClass: { |
| 3713 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3714 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3715 | // Handle the GNU extension for missing LHS. |
Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 3716 | if (Expr *lhsExpr = C->getLHS()) { |
| 3717 | // In C++, we can have a throw-expression, which has 'void' type. |
| 3718 | if (!lhsExpr->getType()->isVoidType()) |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3719 | if (Expr* LHS = EvalAddr(lhsExpr, refVars, ParentDecl)) |
Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 3720 | return LHS; |
| 3721 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3722 | |
Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 3723 | // In C++, we can have a throw-expression, which has 'void' type. |
| 3724 | if (C->getRHS()->getType()->isVoidType()) |
| 3725 | return NULL; |
| 3726 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3727 | return EvalAddr(C->getRHS(), refVars, ParentDecl); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3728 | } |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3729 | |
| 3730 | case Stmt::BlockExprClass: |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 3731 | if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures()) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3732 | return E; // local block. |
| 3733 | return NULL; |
| 3734 | |
| 3735 | case Stmt::AddrLabelExprClass: |
| 3736 | return E; // address of label. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3737 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3738 | case Stmt::ExprWithCleanupsClass: |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3739 | return EvalAddr(cast<ExprWithCleanups>(E)->getSubExpr(), refVars, |
| 3740 | ParentDecl); |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3741 | |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 3742 | // For casts, we need to handle conversions from arrays to |
| 3743 | // pointer values, and pointer-to-pointer conversions. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 3744 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 3745 | case Stmt::CStyleCastExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3746 | case Stmt::CXXFunctionalCastExprClass: |
Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3747 | case Stmt::ObjCBridgedCastExprClass: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3748 | case Stmt::CXXStaticCastExprClass: |
| 3749 | case Stmt::CXXDynamicCastExprClass: |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 3750 | case Stmt::CXXConstCastExprClass: |
| 3751 | case Stmt::CXXReinterpretCastExprClass: { |
Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3752 | Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
| 3753 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 3754 | case CK_BitCast: |
| 3755 | case CK_LValueToRValue: |
| 3756 | case CK_NoOp: |
| 3757 | case CK_BaseToDerived: |
| 3758 | case CK_DerivedToBase: |
| 3759 | case CK_UncheckedDerivedToBase: |
| 3760 | case CK_Dynamic: |
| 3761 | case CK_CPointerToObjCPointerCast: |
| 3762 | case CK_BlockPointerToObjCPointerCast: |
| 3763 | case CK_AnyPointerToBlockPointerCast: |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3764 | return EvalAddr(SubExpr, refVars, ParentDecl); |
Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3765 | |
| 3766 | case CK_ArrayToPointerDecay: |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3767 | return EvalVal(SubExpr, refVars, ParentDecl); |
Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3768 | |
| 3769 | default: |
| 3770 | return 0; |
| 3771 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3772 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3773 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3774 | case Stmt::MaterializeTemporaryExprClass: |
| 3775 | if (Expr *Result = EvalAddr( |
| 3776 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3777 | refVars, ParentDecl)) |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3778 | return Result; |
| 3779 | |
| 3780 | return E; |
| 3781 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3782 | // Everything else: we simply don't reason about them. |
| 3783 | default: |
| 3784 | return NULL; |
| 3785 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3786 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3787 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3788 | |
| 3789 | /// EvalVal - This function is complements EvalAddr in the mutual recursion. |
| 3790 | /// See the comments for EvalAddr for more details. |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3791 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 3792 | Decl *ParentDecl) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3793 | do { |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 3794 | // We should only be called for evaluating non-pointer expressions, or |
| 3795 | // expressions with a pointer type that are not used as references but instead |
| 3796 | // are l-values (e.g., DeclRefExpr with a pointer type). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3797 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3798 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 3799 | // viewed AST node. We then recursively traverse the AST by calling |
| 3800 | // EvalAddr and EvalVal appropriately. |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3801 | |
| 3802 | E = E->IgnoreParens(); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3803 | switch (E->getStmtClass()) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3804 | case Stmt::ImplicitCastExprClass: { |
| 3805 | ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E); |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3806 | if (IE->getValueKind() == VK_LValue) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3807 | E = IE->getSubExpr(); |
| 3808 | continue; |
| 3809 | } |
| 3810 | return NULL; |
| 3811 | } |
| 3812 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3813 | case Stmt::ExprWithCleanupsClass: |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3814 | return EvalVal(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,ParentDecl); |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3815 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3816 | case Stmt::DeclRefExprClass: { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3817 | // When we hit a DeclRefExpr we are looking at code that refers to a |
| 3818 | // variable's name. If it's not a reference variable we check if it has |
| 3819 | // local storage within the function, and if so, return the expression. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3820 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3821 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3822 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) { |
| 3823 | // Check if it refers to itself, e.g. "int& i = i;". |
| 3824 | if (V == ParentDecl) |
| 3825 | return DR; |
| 3826 | |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3827 | if (V->hasLocalStorage()) { |
| 3828 | if (!V->getType()->isReferenceType()) |
| 3829 | return DR; |
| 3830 | |
| 3831 | // Reference variable, follow through to the expression that |
| 3832 | // it points to. |
| 3833 | if (V->hasInit()) { |
| 3834 | // Add the reference variable to the "trail". |
| 3835 | refVars.push_back(DR); |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3836 | return EvalVal(V->getInit(), refVars, V); |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3837 | } |
| 3838 | } |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3839 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3840 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3841 | return NULL; |
| 3842 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3843 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3844 | case Stmt::UnaryOperatorClass: { |
| 3845 | // The only unary operator that make sense to handle here |
| 3846 | // is Deref. All others don't resolve to a "name." This includes |
| 3847 | // handling all sorts of rvalues passed to a unary operator. |
| 3848 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3849 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3850 | if (U->getOpcode() == UO_Deref) |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3851 | return EvalAddr(U->getSubExpr(), refVars, ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3852 | |
| 3853 | return NULL; |
| 3854 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3855 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3856 | case Stmt::ArraySubscriptExprClass: { |
| 3857 | // Array subscripts are potential references to data on the stack. We |
| 3858 | // retrieve the DeclRefExpr* for the array variable if it indeed |
| 3859 | // has local storage. |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3860 | return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars,ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3861 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3862 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3863 | case Stmt::ConditionalOperatorClass: { |
| 3864 | // 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] | 3865 | // non-NULL Expr's. If one is non-NULL, we return it. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3866 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 3867 | |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 3868 | // Handle the GNU extension for missing LHS. |
| 3869 | if (Expr *lhsExpr = C->getLHS()) |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3870 | if (Expr *LHS = EvalVal(lhsExpr, refVars, ParentDecl)) |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 3871 | return LHS; |
| 3872 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3873 | return EvalVal(C->getRHS(), refVars, ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3874 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3875 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3876 | // Accesses to members are potential references to data on the stack. |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 3877 | case Stmt::MemberExprClass: { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3878 | MemberExpr *M = cast<MemberExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3879 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3880 | // Check for indirect access. We only want direct field accesses. |
Ted Kremenek | a423e81 | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 3881 | if (M->isArrow()) |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3882 | return NULL; |
Ted Kremenek | a423e81 | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 3883 | |
| 3884 | // Check whether the member type is itself a reference, in which case |
| 3885 | // we're not going to refer to the member, but to what the member refers to. |
| 3886 | if (M->getMemberDecl()->getType()->isReferenceType()) |
| 3887 | return NULL; |
| 3888 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3889 | return EvalVal(M->getBase(), refVars, ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3890 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3891 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3892 | case Stmt::MaterializeTemporaryExprClass: |
| 3893 | if (Expr *Result = EvalVal( |
| 3894 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3895 | refVars, ParentDecl)) |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3896 | return Result; |
| 3897 | |
| 3898 | return E; |
| 3899 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3900 | default: |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3901 | // Check that we don't return or take the address of a reference to a |
| 3902 | // temporary. This is only useful in C++. |
| 3903 | if (!E->isTypeDependent() && E->isRValue()) |
| 3904 | return E; |
| 3905 | |
| 3906 | // Everything else: we simply don't reason about them. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3907 | return NULL; |
| 3908 | } |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3909 | } while (true); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3910 | } |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3911 | |
| 3912 | //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// |
| 3913 | |
| 3914 | /// Check for comparisons of floating point operands using != and ==. |
| 3915 | /// Issue a warning if these are no self-comparisons, as they are not likely |
| 3916 | /// to do what the programmer intended. |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3917 | void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) { |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3918 | Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts(); |
| 3919 | Expr* RightExprSansParen = RHS->IgnoreParenImpCasts(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3920 | |
| 3921 | // Special case: check for x == x (which is OK). |
| 3922 | // Do not emit warnings for such cases. |
| 3923 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) |
| 3924 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) |
| 3925 | if (DRL->getDecl() == DRR->getDecl()) |
David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3926 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3927 | |
| 3928 | |
Ted Kremenek | 1b500bb | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 3929 | // Special case: check for comparisons against literals that can be exactly |
| 3930 | // represented by APFloat. In such cases, do not emit a warning. This |
| 3931 | // is a heuristic: often comparison against such literals are used to |
| 3932 | // detect if a value in a variable has not changed. This clearly can |
| 3933 | // lead to false negatives. |
David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3934 | if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { |
| 3935 | if (FLL->isExact()) |
| 3936 | return; |
| 3937 | } else |
| 3938 | if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)) |
| 3939 | if (FLR->isExact()) |
| 3940 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3941 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3942 | // Check for comparisons with builtin types. |
David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3943 | if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) |
| 3944 | if (CL->isBuiltinCall()) |
| 3945 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3946 | |
David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3947 | if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) |
| 3948 | if (CR->isBuiltinCall()) |
| 3949 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3950 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3951 | // Emit the diagnostic. |
David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3952 | Diag(Loc, diag::warn_floatingpoint_eq) |
| 3953 | << LHS->getSourceRange() << RHS->getSourceRange(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3954 | } |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3955 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3956 | //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// |
| 3957 | //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3958 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3959 | namespace { |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3960 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3961 | /// Structure recording the 'active' range of an integer-valued |
| 3962 | /// expression. |
| 3963 | struct IntRange { |
| 3964 | /// The number of bits active in the int. |
| 3965 | unsigned Width; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3966 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3967 | /// True if the int is known not to have negative values. |
| 3968 | bool NonNegative; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3969 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3970 | IntRange(unsigned Width, bool NonNegative) |
| 3971 | : Width(Width), NonNegative(NonNegative) |
| 3972 | {} |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3973 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3974 | /// Returns the range of the bool type. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3975 | static IntRange forBoolType() { |
| 3976 | return IntRange(1, true); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3977 | } |
| 3978 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3979 | /// Returns the range of an opaque value of the given integral type. |
| 3980 | static IntRange forValueOfType(ASTContext &C, QualType T) { |
| 3981 | return forValueOfCanonicalType(C, |
| 3982 | T->getCanonicalTypeInternal().getTypePtr()); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3985 | /// Returns the range of an opaque value of a canonical integral type. |
| 3986 | static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3987 | assert(T->isCanonicalUnqualified()); |
| 3988 | |
| 3989 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 3990 | T = VT->getElementType().getTypePtr(); |
| 3991 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 3992 | T = CT->getElementType().getTypePtr(); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3993 | |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3994 | // For enum types, use the known bit width of the enumerators. |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3995 | if (const EnumType *ET = dyn_cast<EnumType>(T)) { |
| 3996 | EnumDecl *Enum = ET->getDecl(); |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3997 | if (!Enum->isCompleteDefinition()) |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3998 | return IntRange(C.getIntWidth(QualType(T, 0)), false); |
| 3999 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4000 | unsigned NumPositive = Enum->getNumPositiveBits(); |
| 4001 | unsigned NumNegative = Enum->getNumNegativeBits(); |
| 4002 | |
Richard Trieu | 8f50b24 | 2012-11-16 01:32:40 +0000 | [diff] [blame] | 4003 | if (NumNegative == 0) |
| 4004 | return IntRange(NumPositive, true/*NonNegative*/); |
| 4005 | else |
| 4006 | return IntRange(std::max(NumPositive + 1, NumNegative), |
| 4007 | false/*NonNegative*/); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4008 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4009 | |
| 4010 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 4011 | assert(BT->isInteger()); |
| 4012 | |
| 4013 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 4014 | } |
| 4015 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4016 | /// Returns the "target" range of a canonical integral type, i.e. |
| 4017 | /// the range of values expressible in the type. |
| 4018 | /// |
| 4019 | /// This matches forValueOfCanonicalType except that enums have the |
| 4020 | /// full range of their type, not the range of their enumerators. |
| 4021 | static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) { |
| 4022 | assert(T->isCanonicalUnqualified()); |
| 4023 | |
| 4024 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 4025 | T = VT->getElementType().getTypePtr(); |
| 4026 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 4027 | T = CT->getElementType().getTypePtr(); |
| 4028 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Douglas Gregor | 69ff26b | 2011-09-08 23:29:05 +0000 | [diff] [blame] | 4029 | T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr(); |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4030 | |
| 4031 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 4032 | assert(BT->isInteger()); |
| 4033 | |
| 4034 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 4035 | } |
| 4036 | |
| 4037 | /// Returns the supremum of two ranges: i.e. their conservative merge. |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 4038 | static IntRange join(IntRange L, IntRange R) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4039 | return IntRange(std::max(L.Width, R.Width), |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 4040 | L.NonNegative && R.NonNegative); |
| 4041 | } |
| 4042 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4043 | /// Returns the infinum of two ranges: i.e. their aggressive merge. |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 4044 | static IntRange meet(IntRange L, IntRange R) { |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 4045 | return IntRange(std::min(L.Width, R.Width), |
| 4046 | L.NonNegative || R.NonNegative); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4047 | } |
| 4048 | }; |
| 4049 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4050 | static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, |
| 4051 | unsigned MaxWidth) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4052 | if (value.isSigned() && value.isNegative()) |
| 4053 | return IntRange(value.getMinSignedBits(), false); |
| 4054 | |
| 4055 | if (value.getBitWidth() > MaxWidth) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 4056 | value = value.trunc(MaxWidth); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4057 | |
| 4058 | // isNonNegative() just checks the sign bit without considering |
| 4059 | // signedness. |
| 4060 | return IntRange(value.getActiveBits(), true); |
| 4061 | } |
| 4062 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4063 | static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, |
| 4064 | unsigned MaxWidth) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4065 | if (result.isInt()) |
| 4066 | return GetValueRange(C, result.getInt(), MaxWidth); |
| 4067 | |
| 4068 | if (result.isVector()) { |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 4069 | IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); |
| 4070 | for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { |
| 4071 | IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); |
| 4072 | R = IntRange::join(R, El); |
| 4073 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4074 | return R; |
| 4075 | } |
| 4076 | |
| 4077 | if (result.isComplexInt()) { |
| 4078 | IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); |
| 4079 | IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); |
| 4080 | return IntRange::join(R, I); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | // This can happen with lossless casts to intptr_t of "based" lvalues. |
| 4084 | // Assume it might use arbitrary bits. |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 4085 | // FIXME: The only reason we need to pass the type in here is to get |
| 4086 | // the sign right on this one case. It would be nice if APValue |
| 4087 | // preserved this. |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4088 | assert(result.isLValue() || result.isAddrLabelDiff()); |
Douglas Gregor | 5e9ebb3 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 4089 | return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType()); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4090 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4091 | |
| 4092 | /// Pseudo-evaluate the given integer expression, estimating the |
| 4093 | /// range of values it might take. |
| 4094 | /// |
| 4095 | /// \param MaxWidth - the width to which the value will be truncated |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4096 | static IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4097 | E = E->IgnoreParens(); |
| 4098 | |
| 4099 | // Try a full evaluation first. |
| 4100 | Expr::EvalResult result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4101 | if (E->EvaluateAsRValue(result, C)) |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 4102 | return GetValueRange(C, result.Val, E->getType(), MaxWidth); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4103 | |
| 4104 | // I think we only want to look through implicit casts here; if the |
| 4105 | // user has an explicit widening cast, we should treat the value as |
| 4106 | // being of the new, wider type. |
| 4107 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) { |
Eli Friedman | b17ee5b | 2011-12-15 02:41:52 +0000 | [diff] [blame] | 4108 | if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue) |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4109 | return GetExprRange(C, CE->getSubExpr(), MaxWidth); |
| 4110 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4111 | IntRange OutputTypeRange = IntRange::forValueOfType(C, CE->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4112 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4113 | bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast); |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 4114 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4115 | // 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] | 4116 | if (!isIntegerCast) |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4117 | return OutputTypeRange; |
| 4118 | |
| 4119 | IntRange SubRange |
| 4120 | = GetExprRange(C, CE->getSubExpr(), |
| 4121 | std::min(MaxWidth, OutputTypeRange.Width)); |
| 4122 | |
| 4123 | // Bail out if the subexpr's range is as wide as the cast type. |
| 4124 | if (SubRange.Width >= OutputTypeRange.Width) |
| 4125 | return OutputTypeRange; |
| 4126 | |
| 4127 | // Otherwise, we take the smaller width, and we're non-negative if |
| 4128 | // either the output type or the subexpr is. |
| 4129 | return IntRange(SubRange.Width, |
| 4130 | SubRange.NonNegative || OutputTypeRange.NonNegative); |
| 4131 | } |
| 4132 | |
| 4133 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 4134 | // If we can fold the condition, just take that operand. |
| 4135 | bool CondResult; |
| 4136 | if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) |
| 4137 | return GetExprRange(C, CondResult ? CO->getTrueExpr() |
| 4138 | : CO->getFalseExpr(), |
| 4139 | MaxWidth); |
| 4140 | |
| 4141 | // Otherwise, conservatively merge. |
| 4142 | IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth); |
| 4143 | IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth); |
| 4144 | return IntRange::join(L, R); |
| 4145 | } |
| 4146 | |
| 4147 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 4148 | switch (BO->getOpcode()) { |
| 4149 | |
| 4150 | // Boolean-valued operations are single-bit and positive. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4151 | case BO_LAnd: |
| 4152 | case BO_LOr: |
| 4153 | case BO_LT: |
| 4154 | case BO_GT: |
| 4155 | case BO_LE: |
| 4156 | case BO_GE: |
| 4157 | case BO_EQ: |
| 4158 | case BO_NE: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4159 | return IntRange::forBoolType(); |
| 4160 | |
John McCall | 862ff87 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 4161 | // The type of the assignments is the type of the LHS, so the RHS |
| 4162 | // is not necessarily the same type. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4163 | case BO_MulAssign: |
| 4164 | case BO_DivAssign: |
| 4165 | case BO_RemAssign: |
| 4166 | case BO_AddAssign: |
| 4167 | case BO_SubAssign: |
John McCall | 862ff87 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 4168 | case BO_XorAssign: |
| 4169 | case BO_OrAssign: |
| 4170 | // TODO: bitfields? |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4171 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 4172 | |
John McCall | 862ff87 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 4173 | // Simple assignments just pass through the RHS, which will have |
| 4174 | // been coerced to the LHS type. |
| 4175 | case BO_Assign: |
| 4176 | // TODO: bitfields? |
| 4177 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 4178 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4179 | // Operations with opaque sources are black-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4180 | case BO_PtrMemD: |
| 4181 | case BO_PtrMemI: |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4182 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4183 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 4184 | // Bitwise-and uses the *infinum* of the two source ranges. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4185 | case BO_And: |
| 4186 | case BO_AndAssign: |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 4187 | return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth), |
| 4188 | GetExprRange(C, BO->getRHS(), MaxWidth)); |
| 4189 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4190 | // Left shift gets black-listed based on a judgement call. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4191 | case BO_Shl: |
John McCall | 3aae609 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 4192 | // ...except that we want to treat '1 << (blah)' as logically |
| 4193 | // positive. It's an important idiom. |
| 4194 | if (IntegerLiteral *I |
| 4195 | = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { |
| 4196 | if (I->getValue() == 1) { |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4197 | IntRange R = IntRange::forValueOfType(C, E->getType()); |
John McCall | 3aae609 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 4198 | return IntRange(R.Width, /*NonNegative*/ true); |
| 4199 | } |
| 4200 | } |
| 4201 | // fallthrough |
| 4202 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4203 | case BO_ShlAssign: |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4204 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4205 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 4206 | // Right shift by a constant can narrow its left argument. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4207 | case BO_Shr: |
| 4208 | case BO_ShrAssign: { |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 4209 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 4210 | |
| 4211 | // If the shift amount is a positive constant, drop the width by |
| 4212 | // that much. |
| 4213 | llvm::APSInt shift; |
| 4214 | if (BO->getRHS()->isIntegerConstantExpr(shift, C) && |
| 4215 | shift.isNonNegative()) { |
| 4216 | unsigned zext = shift.getZExtValue(); |
| 4217 | if (zext >= L.Width) |
| 4218 | L.Width = (L.NonNegative ? 0 : 1); |
| 4219 | else |
| 4220 | L.Width -= zext; |
| 4221 | } |
| 4222 | |
| 4223 | return L; |
| 4224 | } |
| 4225 | |
| 4226 | // Comma acts as its right operand. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4227 | case BO_Comma: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4228 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 4229 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 4230 | // Black-list pointer subtractions. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4231 | case BO_Sub: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4232 | if (BO->getLHS()->getType()->isPointerType()) |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4233 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | 00fe761 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 4234 | break; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 4235 | |
John McCall | 00fe761 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 4236 | // The width of a division result is mostly determined by the size |
| 4237 | // of the LHS. |
| 4238 | case BO_Div: { |
| 4239 | // Don't 'pre-truncate' the operands. |
| 4240 | unsigned opWidth = C.getIntWidth(E->getType()); |
| 4241 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); |
| 4242 | |
| 4243 | // If the divisor is constant, use that. |
| 4244 | llvm::APSInt divisor; |
| 4245 | if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) { |
| 4246 | unsigned log2 = divisor.logBase2(); // floor(log_2(divisor)) |
| 4247 | if (log2 >= L.Width) |
| 4248 | L.Width = (L.NonNegative ? 0 : 1); |
| 4249 | else |
| 4250 | L.Width = std::min(L.Width - log2, MaxWidth); |
| 4251 | return L; |
| 4252 | } |
| 4253 | |
| 4254 | // Otherwise, just use the LHS's width. |
| 4255 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); |
| 4256 | return IntRange(L.Width, L.NonNegative && R.NonNegative); |
| 4257 | } |
| 4258 | |
| 4259 | // The result of a remainder can't be larger than the result of |
| 4260 | // either side. |
| 4261 | case BO_Rem: { |
| 4262 | // Don't 'pre-truncate' the operands. |
| 4263 | unsigned opWidth = C.getIntWidth(E->getType()); |
| 4264 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); |
| 4265 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); |
| 4266 | |
| 4267 | IntRange meet = IntRange::meet(L, R); |
| 4268 | meet.Width = std::min(meet.Width, MaxWidth); |
| 4269 | return meet; |
| 4270 | } |
| 4271 | |
| 4272 | // The default behavior is okay for these. |
| 4273 | case BO_Mul: |
| 4274 | case BO_Add: |
| 4275 | case BO_Xor: |
| 4276 | case BO_Or: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4277 | break; |
| 4278 | } |
| 4279 | |
John McCall | 00fe761 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 4280 | // The default case is to treat the operation as if it were closed |
| 4281 | // on the narrowest type that encompasses both operands. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4282 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 4283 | IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth); |
| 4284 | return IntRange::join(L, R); |
| 4285 | } |
| 4286 | |
| 4287 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 4288 | switch (UO->getOpcode()) { |
| 4289 | // Boolean-valued operations are white-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4290 | case UO_LNot: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4291 | return IntRange::forBoolType(); |
| 4292 | |
| 4293 | // Operations with opaque sources are black-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4294 | case UO_Deref: |
| 4295 | case UO_AddrOf: // should be impossible |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4296 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4297 | |
| 4298 | default: |
| 4299 | return GetExprRange(C, UO->getSubExpr(), MaxWidth); |
| 4300 | } |
| 4301 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4302 | |
| 4303 | if (dyn_cast<OffsetOfExpr>(E)) { |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4304 | IntRange::forValueOfType(C, E->getType()); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4305 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4306 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4307 | if (FieldDecl *BitField = E->getBitField()) |
| 4308 | return IntRange(BitField->getBitWidthValue(C), |
Douglas Gregor | 5e9ebb3 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 4309 | BitField->getType()->isUnsignedIntegerOrEnumerationType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4310 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4311 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4312 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4313 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4314 | static IntRange GetExprRange(ASTContext &C, Expr *E) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4315 | return GetExprRange(C, E, C.getIntWidth(E->getType())); |
| 4316 | } |
| 4317 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4318 | /// Checks whether the given value, which currently has the given |
| 4319 | /// source semantics, has the same value when coerced through the |
| 4320 | /// target semantics. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4321 | static bool IsSameFloatAfterCast(const llvm::APFloat &value, |
| 4322 | const llvm::fltSemantics &Src, |
| 4323 | const llvm::fltSemantics &Tgt) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4324 | llvm::APFloat truncated = value; |
| 4325 | |
| 4326 | bool ignored; |
| 4327 | truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 4328 | truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 4329 | |
| 4330 | return truncated.bitwiseIsEqual(value); |
| 4331 | } |
| 4332 | |
| 4333 | /// Checks whether the given value, which currently has the given |
| 4334 | /// source semantics, has the same value when coerced through the |
| 4335 | /// target semantics. |
| 4336 | /// |
| 4337 | /// The value might be a vector of floats (or a complex number). |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4338 | static bool IsSameFloatAfterCast(const APValue &value, |
| 4339 | const llvm::fltSemantics &Src, |
| 4340 | const llvm::fltSemantics &Tgt) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4341 | if (value.isFloat()) |
| 4342 | return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); |
| 4343 | |
| 4344 | if (value.isVector()) { |
| 4345 | for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) |
| 4346 | if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) |
| 4347 | return false; |
| 4348 | return true; |
| 4349 | } |
| 4350 | |
| 4351 | assert(value.isComplexFloat()); |
| 4352 | return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && |
| 4353 | IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); |
| 4354 | } |
| 4355 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4356 | static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4357 | |
Ted Kremenek | e3b159c | 2010-09-23 21:43:44 +0000 | [diff] [blame] | 4358 | static bool IsZero(Sema &S, Expr *E) { |
| 4359 | // Suppress cases where we are comparing against an enum constant. |
| 4360 | if (const DeclRefExpr *DR = |
| 4361 | dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) |
| 4362 | if (isa<EnumConstantDecl>(DR->getDecl())) |
| 4363 | return false; |
| 4364 | |
| 4365 | // Suppress cases where the '0' value is expanded from a macro. |
| 4366 | if (E->getLocStart().isMacroID()) |
| 4367 | return false; |
| 4368 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4369 | llvm::APSInt Value; |
| 4370 | return E->isIntegerConstantExpr(Value, S.Context) && Value == 0; |
| 4371 | } |
| 4372 | |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4373 | static bool HasEnumType(Expr *E) { |
| 4374 | // Strip off implicit integral promotions. |
| 4375 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
Argyrios Kyrtzidis | 63b57ae | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 4376 | if (ICE->getCastKind() != CK_IntegralCast && |
| 4377 | ICE->getCastKind() != CK_NoOp) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4378 | break; |
Argyrios Kyrtzidis | 63b57ae | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 4379 | E = ICE->getSubExpr(); |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | return E->getType()->isEnumeralType(); |
| 4383 | } |
| 4384 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4385 | static void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4386 | BinaryOperatorKind op = E->getOpcode(); |
Douglas Gregor | 14af91a | 2010-12-21 07:22:56 +0000 | [diff] [blame] | 4387 | if (E->isValueDependent()) |
| 4388 | return; |
| 4389 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4390 | if (op == BO_LT && IsZero(S, E->getRHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4391 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4392 | << "< 0" << "false" << HasEnumType(E->getLHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4393 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4394 | } else if (op == BO_GE && IsZero(S, E->getRHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4395 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4396 | << ">= 0" << "true" << HasEnumType(E->getLHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4397 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4398 | } else if (op == BO_GT && IsZero(S, E->getLHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4399 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4400 | << "0 >" << "false" << HasEnumType(E->getRHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4401 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4402 | } else if (op == BO_LE && IsZero(S, E->getLHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4403 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4404 | << "0 <=" << "true" << HasEnumType(E->getRHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4405 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
| 4406 | } |
| 4407 | } |
| 4408 | |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4409 | static void DiagnoseOutOfRangeComparison(Sema &S, BinaryOperator *E, |
Fariborz Jahanian | a193f20 | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 4410 | Expr *Constant, Expr *Other, |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4411 | llvm::APSInt Value, |
Fariborz Jahanian | a193f20 | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 4412 | bool RhsConstant) { |
Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4413 | // 0 values are handled later by CheckTrivialUnsignedComparison(). |
| 4414 | if (Value == 0) |
| 4415 | return; |
| 4416 | |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4417 | BinaryOperatorKind op = E->getOpcode(); |
Fariborz Jahanian | a193f20 | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 4418 | QualType OtherT = Other->getType(); |
| 4419 | QualType ConstantT = Constant->getType(); |
Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4420 | QualType CommonT = E->getLHS()->getType(); |
Fariborz Jahanian | a193f20 | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 4421 | if (S.Context.hasSameUnqualifiedType(OtherT, ConstantT)) |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4422 | return; |
Fariborz Jahanian | a193f20 | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 4423 | assert((OtherT->isIntegerType() && ConstantT->isIntegerType()) |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4424 | && "comparison with non-integer type"); |
Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4425 | |
| 4426 | bool ConstantSigned = ConstantT->isSignedIntegerType(); |
Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4427 | bool CommonSigned = CommonT->isSignedIntegerType(); |
| 4428 | |
| 4429 | bool EqualityOnly = false; |
| 4430 | |
| 4431 | // TODO: Investigate using GetExprRange() to get tighter bounds on |
| 4432 | // on the bit ranges. |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4433 | IntRange OtherRange = IntRange::forValueOfType(S.Context, OtherT); |
Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4434 | unsigned OtherWidth = OtherRange.Width; |
| 4435 | |
| 4436 | if (CommonSigned) { |
| 4437 | // The common type is signed, therefore no signed to unsigned conversion. |
Eli Friedman | d87de7b | 2012-11-30 23:09:29 +0000 | [diff] [blame] | 4438 | if (!OtherRange.NonNegative) { |
Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4439 | // Check that the constant is representable in type OtherT. |
| 4440 | if (ConstantSigned) { |
| 4441 | if (OtherWidth >= Value.getMinSignedBits()) |
| 4442 | return; |
| 4443 | } else { // !ConstantSigned |
| 4444 | if (OtherWidth >= Value.getActiveBits() + 1) |
| 4445 | return; |
| 4446 | } |
| 4447 | } else { // !OtherSigned |
| 4448 | // Check that the constant is representable in type OtherT. |
| 4449 | // Negative values are out of range. |
| 4450 | if (ConstantSigned) { |
| 4451 | if (Value.isNonNegative() && OtherWidth >= Value.getActiveBits()) |
| 4452 | return; |
| 4453 | } else { // !ConstantSigned |
| 4454 | if (OtherWidth >= Value.getActiveBits()) |
| 4455 | return; |
| 4456 | } |
| 4457 | } |
| 4458 | } else { // !CommonSigned |
Eli Friedman | d87de7b | 2012-11-30 23:09:29 +0000 | [diff] [blame] | 4459 | if (OtherRange.NonNegative) { |
Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4460 | if (OtherWidth >= Value.getActiveBits()) |
| 4461 | return; |
Eli Friedman | d87de7b | 2012-11-30 23:09:29 +0000 | [diff] [blame] | 4462 | } else if (!OtherRange.NonNegative && !ConstantSigned) { |
Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4463 | // Check to see if the constant is representable in OtherT. |
| 4464 | if (OtherWidth > Value.getActiveBits()) |
| 4465 | return; |
| 4466 | // Check to see if the constant is equivalent to a negative value |
| 4467 | // cast to CommonT. |
| 4468 | if (S.Context.getIntWidth(ConstantT) == S.Context.getIntWidth(CommonT) && |
Richard Trieu | 5d1cf4f | 2012-11-15 03:43:50 +0000 | [diff] [blame] | 4469 | Value.isNegative() && Value.getMinSignedBits() <= OtherWidth) |
Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4470 | return; |
| 4471 | // The constant value rests between values that OtherT can represent after |
| 4472 | // conversion. Relational comparison still works, but equality |
| 4473 | // comparisons will be tautological. |
| 4474 | EqualityOnly = true; |
| 4475 | } else { // OtherSigned && ConstantSigned |
| 4476 | assert(0 && "Two signed types converted to unsigned types."); |
| 4477 | } |
| 4478 | } |
| 4479 | |
| 4480 | bool PositiveConstant = !ConstantSigned || Value.isNonNegative(); |
| 4481 | |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4482 | bool IsTrue = true; |
Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4483 | if (op == BO_EQ || op == BO_NE) { |
| 4484 | IsTrue = op == BO_NE; |
| 4485 | } else if (EqualityOnly) { |
| 4486 | return; |
| 4487 | } else if (RhsConstant) { |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4488 | if (op == BO_GT || op == BO_GE) |
Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4489 | IsTrue = !PositiveConstant; |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4490 | else // op == BO_LT || op == BO_LE |
Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4491 | IsTrue = PositiveConstant; |
Fariborz Jahanian | a193f20 | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 4492 | } else { |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4493 | if (op == BO_LT || op == BO_LE) |
Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4494 | IsTrue = !PositiveConstant; |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4495 | else // op == BO_GT || op == BO_GE |
Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4496 | IsTrue = PositiveConstant; |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4497 | } |
Fariborz Jahanian | a193f20 | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 4498 | SmallString<16> PrettySourceValue(Value.toString(10)); |
| 4499 | S.Diag(E->getOperatorLoc(), diag::warn_out_of_range_compare) |
Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4500 | << PrettySourceValue << OtherT << IsTrue |
| 4501 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4502 | } |
| 4503 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4504 | /// Analyze the operands of the given comparison. Implements the |
| 4505 | /// fallback case from AnalyzeComparison. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4506 | static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4507 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 4508 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4509 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4510 | |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4511 | /// \brief Implements -Wsign-compare. |
| 4512 | /// |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4513 | /// \param E the binary operator to check for warnings |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4514 | static void AnalyzeComparison(Sema &S, BinaryOperator *E) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4515 | // The type the comparison is being performed in. |
| 4516 | QualType T = E->getLHS()->getType(); |
| 4517 | assert(S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType()) |
| 4518 | && "comparison with mismatched types"); |
Fariborz Jahanian | ab4702f | 2012-09-18 17:46:26 +0000 | [diff] [blame] | 4519 | if (E->isValueDependent()) |
| 4520 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4521 | |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4522 | Expr *LHS = E->getLHS()->IgnoreParenImpCasts(); |
| 4523 | Expr *RHS = E->getRHS()->IgnoreParenImpCasts(); |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4524 | |
| 4525 | bool IsComparisonConstant = false; |
| 4526 | |
Fariborz Jahanian | a193f20 | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 4527 | // Check whether an integer constant comparison results in a value |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4528 | // of 'true' or 'false'. |
| 4529 | if (T->isIntegralType(S.Context)) { |
| 4530 | llvm::APSInt RHSValue; |
| 4531 | bool IsRHSIntegralLiteral = |
| 4532 | RHS->isIntegerConstantExpr(RHSValue, S.Context); |
| 4533 | llvm::APSInt LHSValue; |
| 4534 | bool IsLHSIntegralLiteral = |
| 4535 | LHS->isIntegerConstantExpr(LHSValue, S.Context); |
| 4536 | if (IsRHSIntegralLiteral && !IsLHSIntegralLiteral) |
| 4537 | DiagnoseOutOfRangeComparison(S, E, RHS, LHS, RHSValue, true); |
| 4538 | else if (!IsRHSIntegralLiteral && IsLHSIntegralLiteral) |
| 4539 | DiagnoseOutOfRangeComparison(S, E, LHS, RHS, LHSValue, false); |
| 4540 | else |
| 4541 | IsComparisonConstant = |
| 4542 | (IsRHSIntegralLiteral && IsLHSIntegralLiteral); |
Fariborz Jahanian | a193f20 | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 4543 | } else if (!T->hasUnsignedIntegerRepresentation()) |
| 4544 | IsComparisonConstant = E->isIntegerConstantExpr(S.Context); |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4545 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4546 | // We don't do anything special if this isn't an unsigned integral |
| 4547 | // comparison: we're only interested in integral comparisons, and |
| 4548 | // 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] | 4549 | // |
| 4550 | // We also don't care about value-dependent expressions or expressions |
| 4551 | // whose result is a constant. |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4552 | if (!T->hasUnsignedIntegerRepresentation() || IsComparisonConstant) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4553 | return AnalyzeImpConvsInComparison(S, E); |
Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4554 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4555 | // Check to see if one of the (unmodified) operands is of different |
| 4556 | // signedness. |
| 4557 | Expr *signedOperand, *unsignedOperand; |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4558 | if (LHS->getType()->hasSignedIntegerRepresentation()) { |
| 4559 | assert(!RHS->getType()->hasSignedIntegerRepresentation() && |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4560 | "unsigned comparison between two signed integer expressions?"); |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4561 | signedOperand = LHS; |
| 4562 | unsignedOperand = RHS; |
| 4563 | } else if (RHS->getType()->hasSignedIntegerRepresentation()) { |
| 4564 | signedOperand = RHS; |
| 4565 | unsignedOperand = LHS; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4566 | } else { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4567 | CheckTrivialUnsignedComparison(S, E); |
| 4568 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4569 | } |
| 4570 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4571 | // Otherwise, calculate the effective range of the signed operand. |
| 4572 | IntRange signedRange = GetExprRange(S.Context, signedOperand); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4573 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4574 | // Go ahead and analyze implicit conversions in the operands. Note |
| 4575 | // that we skip the implicit conversions on both sides. |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4576 | AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc()); |
| 4577 | AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc()); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4578 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4579 | // If the signed range is non-negative, -Wsign-compare won't fire, |
| 4580 | // but we should still check for comparisons which are always true |
| 4581 | // or false. |
| 4582 | if (signedRange.NonNegative) |
| 4583 | return CheckTrivialUnsignedComparison(S, E); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4584 | |
| 4585 | // For (in)equality comparisons, if the unsigned operand is a |
| 4586 | // constant which cannot collide with a overflowed signed operand, |
| 4587 | // then reinterpreting the signed operand as unsigned will not |
| 4588 | // change the result of the comparison. |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4589 | if (E->isEqualityOp()) { |
| 4590 | unsigned comparisonWidth = S.Context.getIntWidth(T); |
| 4591 | IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4592 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4593 | // We should never be unable to prove that the unsigned operand is |
| 4594 | // non-negative. |
| 4595 | assert(unsignedRange.NonNegative && "unsigned range includes negative?"); |
| 4596 | |
| 4597 | if (unsignedRange.Width < comparisonWidth) |
| 4598 | return; |
| 4599 | } |
| 4600 | |
Douglas Gregor | 6d3b93d | 2012-05-01 01:53:49 +0000 | [diff] [blame] | 4601 | S.DiagRuntimeBehavior(E->getOperatorLoc(), E, |
| 4602 | S.PDiag(diag::warn_mixed_sign_comparison) |
| 4603 | << LHS->getType() << RHS->getType() |
| 4604 | << LHS->getSourceRange() << RHS->getSourceRange()); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4605 | } |
| 4606 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4607 | /// Analyzes an attempt to assign the given value to a bitfield. |
| 4608 | /// |
| 4609 | /// Returns true if there was something fishy about the attempt. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4610 | static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, |
| 4611 | SourceLocation InitLoc) { |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4612 | assert(Bitfield->isBitField()); |
| 4613 | if (Bitfield->isInvalidDecl()) |
| 4614 | return false; |
| 4615 | |
John McCall | 91b6014 | 2010-11-11 05:33:51 +0000 | [diff] [blame] | 4616 | // White-list bool bitfields. |
| 4617 | if (Bitfield->getType()->isBooleanType()) |
| 4618 | return false; |
| 4619 | |
Douglas Gregor | 46ff303 | 2011-02-04 13:09:01 +0000 | [diff] [blame] | 4620 | // Ignore value- or type-dependent expressions. |
| 4621 | if (Bitfield->getBitWidth()->isValueDependent() || |
| 4622 | Bitfield->getBitWidth()->isTypeDependent() || |
| 4623 | Init->isValueDependent() || |
| 4624 | Init->isTypeDependent()) |
| 4625 | return false; |
| 4626 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4627 | Expr *OriginalInit = Init->IgnoreParenImpCasts(); |
| 4628 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 4629 | llvm::APSInt Value; |
| 4630 | if (!OriginalInit->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects)) |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4631 | return false; |
| 4632 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4633 | unsigned OriginalWidth = Value.getBitWidth(); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4634 | unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context); |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4635 | |
| 4636 | if (OriginalWidth <= FieldWidth) |
| 4637 | return false; |
| 4638 | |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4639 | // Compute the value which the bitfield will contain. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 4640 | llvm::APSInt TruncatedValue = Value.trunc(FieldWidth); |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4641 | TruncatedValue.setIsSigned(Bitfield->getType()->isSignedIntegerType()); |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4642 | |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4643 | // Check whether the stored value is equal to the original value. |
| 4644 | TruncatedValue = TruncatedValue.extend(OriginalWidth); |
Richard Trieu | e1ecdc1 | 2012-07-23 20:21:35 +0000 | [diff] [blame] | 4645 | if (llvm::APSInt::isSameValue(Value, TruncatedValue)) |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4646 | return false; |
| 4647 | |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4648 | // Special-case bitfields of width 1: booleans are naturally 0/1, and |
Eli Friedman | 34ff062 | 2012-02-02 00:40:20 +0000 | [diff] [blame] | 4649 | // therefore don't strictly fit into a signed bitfield of width 1. |
| 4650 | if (FieldWidth == 1 && Value == 1) |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4651 | return false; |
| 4652 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4653 | std::string PrettyValue = Value.toString(10); |
| 4654 | std::string PrettyTrunc = TruncatedValue.toString(10); |
| 4655 | |
| 4656 | S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant) |
| 4657 | << PrettyValue << PrettyTrunc << OriginalInit->getType() |
| 4658 | << Init->getSourceRange(); |
| 4659 | |
| 4660 | return true; |
| 4661 | } |
| 4662 | |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4663 | /// Analyze the given simple or compound assignment for warning-worthy |
| 4664 | /// operations. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4665 | static void AnalyzeAssignment(Sema &S, BinaryOperator *E) { |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4666 | // Just recurse on the LHS. |
| 4667 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 4668 | |
| 4669 | // We want to recurse on the RHS as normal unless we're assigning to |
| 4670 | // a bitfield. |
| 4671 | if (FieldDecl *Bitfield = E->getLHS()->getBitField()) { |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4672 | if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(), |
| 4673 | E->getOperatorLoc())) { |
| 4674 | // Recurse, ignoring any implicit conversions on the RHS. |
| 4675 | return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(), |
| 4676 | E->getOperatorLoc()); |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4677 | } |
| 4678 | } |
| 4679 | |
| 4680 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
| 4681 | } |
| 4682 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4683 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4684 | static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4685 | SourceLocation CContext, unsigned diag, |
| 4686 | bool pruneControlFlow = false) { |
| 4687 | if (pruneControlFlow) { |
| 4688 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
| 4689 | S.PDiag(diag) |
| 4690 | << SourceType << T << E->getSourceRange() |
| 4691 | << SourceRange(CContext)); |
| 4692 | return; |
| 4693 | } |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 4694 | S.Diag(E->getExprLoc(), diag) |
| 4695 | << SourceType << T << E->getSourceRange() << SourceRange(CContext); |
| 4696 | } |
| 4697 | |
Chandler Carruth | e1b02e0 | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 4698 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4699 | static void DiagnoseImpCast(Sema &S, Expr *E, QualType T, |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4700 | SourceLocation CContext, unsigned diag, |
| 4701 | bool pruneControlFlow = false) { |
| 4702 | DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow); |
Chandler Carruth | e1b02e0 | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 4703 | } |
| 4704 | |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4705 | /// Diagnose an implicit cast from a literal expression. Does not warn when the |
| 4706 | /// cast wouldn't lose information. |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4707 | void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T, |
| 4708 | SourceLocation CContext) { |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4709 | // 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] | 4710 | bool isExact = false; |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4711 | const llvm::APFloat &Value = FL->getValue(); |
Jeffrey Yasskin | 3e1ef78 | 2011-07-15 17:03:07 +0000 | [diff] [blame] | 4712 | llvm::APSInt IntegerValue(S.Context.getIntWidth(T), |
| 4713 | T->hasUnsignedIntegerRepresentation()); |
| 4714 | if (Value.convertToInteger(IntegerValue, |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4715 | llvm::APFloat::rmTowardZero, &isExact) |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4716 | == llvm::APFloat::opOK && isExact) |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4717 | return; |
| 4718 | |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4719 | SmallString<16> PrettySourceValue; |
| 4720 | Value.toString(PrettySourceValue); |
David Blaikie | de7e7b8 | 2012-05-15 17:18:27 +0000 | [diff] [blame] | 4721 | SmallString<16> PrettyTargetValue; |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4722 | if (T->isSpecificBuiltinType(BuiltinType::Bool)) |
| 4723 | PrettyTargetValue = IntegerValue == 0 ? "false" : "true"; |
| 4724 | else |
David Blaikie | de7e7b8 | 2012-05-15 17:18:27 +0000 | [diff] [blame] | 4725 | IntegerValue.toString(PrettyTargetValue); |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4726 | |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4727 | S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer) |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4728 | << FL->getType() << T.getUnqualifiedType() << PrettySourceValue |
| 4729 | << PrettyTargetValue << FL->getSourceRange() << SourceRange(CContext); |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4730 | } |
| 4731 | |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4732 | std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) { |
| 4733 | if (!Range.Width) return "0"; |
| 4734 | |
| 4735 | llvm::APSInt ValueInRange = Value; |
| 4736 | ValueInRange.setIsSigned(!Range.NonNegative); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 4737 | ValueInRange = ValueInRange.trunc(Range.Width); |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4738 | return ValueInRange.toString(10); |
| 4739 | } |
| 4740 | |
Hans Wennborg | 88617a2 | 2012-08-28 15:44:30 +0000 | [diff] [blame] | 4741 | static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) { |
| 4742 | if (!isa<ImplicitCastExpr>(Ex)) |
| 4743 | return false; |
| 4744 | |
| 4745 | Expr *InnerE = Ex->IgnoreParenImpCasts(); |
| 4746 | const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr(); |
| 4747 | const Type *Source = |
| 4748 | S.Context.getCanonicalType(InnerE->getType()).getTypePtr(); |
| 4749 | if (Target->isDependentType()) |
| 4750 | return false; |
| 4751 | |
| 4752 | const BuiltinType *FloatCandidateBT = |
| 4753 | dyn_cast<BuiltinType>(ToBool ? Source : Target); |
| 4754 | const Type *BoolCandidateType = ToBool ? Target : Source; |
| 4755 | |
| 4756 | return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) && |
| 4757 | FloatCandidateBT && (FloatCandidateBT->isFloatingPoint())); |
| 4758 | } |
| 4759 | |
| 4760 | void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall, |
| 4761 | SourceLocation CC) { |
| 4762 | unsigned NumArgs = TheCall->getNumArgs(); |
| 4763 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 4764 | Expr *CurrA = TheCall->getArg(i); |
| 4765 | if (!IsImplicitBoolFloatConversion(S, CurrA, true)) |
| 4766 | continue; |
| 4767 | |
| 4768 | bool IsSwapped = ((i > 0) && |
| 4769 | IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false)); |
| 4770 | IsSwapped |= ((i < (NumArgs - 1)) && |
| 4771 | IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false)); |
| 4772 | if (IsSwapped) { |
| 4773 | // Warn on this floating-point to bool conversion. |
| 4774 | DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(), |
| 4775 | CurrA->getType(), CC, |
| 4776 | diag::warn_impcast_floating_point_to_bool); |
| 4777 | } |
| 4778 | } |
| 4779 | } |
| 4780 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4781 | void CheckImplicitConversion(Sema &S, Expr *E, QualType T, |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4782 | SourceLocation CC, bool *ICContext = 0) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4783 | if (E->isTypeDependent() || E->isValueDependent()) return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4784 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4785 | const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr(); |
| 4786 | const Type *Target = S.Context.getCanonicalType(T).getTypePtr(); |
| 4787 | if (Source == Target) return; |
| 4788 | if (Target->isDependentType()) return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4789 | |
Chandler Carruth | 108f756 | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 4790 | // If the conversion context location is invalid don't complain. We also |
| 4791 | // don't want to emit a warning if the issue occurs from the expansion of |
| 4792 | // a system macro. The problem is that 'getSpellingLoc()' is slow, so we |
| 4793 | // delay this check as long as possible. Once we detect we are in that |
| 4794 | // scenario, we just return. |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4795 | if (CC.isInvalid()) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4796 | return; |
| 4797 | |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4798 | // Diagnose implicit casts to bool. |
| 4799 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) { |
| 4800 | if (isa<StringLiteral>(E)) |
| 4801 | // Warn on string literal to bool. Checks for string literals in logical |
| 4802 | // expressions, for instances, assert(0 && "error here"), is prevented |
| 4803 | // by a check in AnalyzeImplicitConversions(). |
| 4804 | return DiagnoseImpCast(S, E, T, CC, |
| 4805 | diag::warn_impcast_string_literal_to_bool); |
Lang Hames | e14ca9f | 2011-12-05 20:49:50 +0000 | [diff] [blame] | 4806 | if (Source->isFunctionType()) { |
| 4807 | // Warn on function to bool. Checks free functions and static member |
| 4808 | // functions. Weakly imported functions are excluded from the check, |
| 4809 | // since it's common to test their value to check whether the linker |
| 4810 | // found a definition for them. |
| 4811 | ValueDecl *D = 0; |
| 4812 | if (DeclRefExpr* R = dyn_cast<DeclRefExpr>(E)) { |
| 4813 | D = R->getDecl(); |
| 4814 | } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) { |
| 4815 | D = M->getMemberDecl(); |
| 4816 | } |
| 4817 | |
| 4818 | if (D && !D->isWeak()) { |
Richard Trieu | 26b45d8 | 2011-12-06 04:48:01 +0000 | [diff] [blame] | 4819 | if (FunctionDecl* F = dyn_cast<FunctionDecl>(D)) { |
| 4820 | S.Diag(E->getExprLoc(), diag::warn_impcast_function_to_bool) |
| 4821 | << F << E->getSourceRange() << SourceRange(CC); |
David Blaikie | 2def773 | 2011-12-09 21:42:37 +0000 | [diff] [blame] | 4822 | S.Diag(E->getExprLoc(), diag::note_function_to_bool_silence) |
| 4823 | << FixItHint::CreateInsertion(E->getExprLoc(), "&"); |
| 4824 | QualType ReturnType; |
| 4825 | UnresolvedSet<4> NonTemplateOverloads; |
| 4826 | S.isExprCallable(*E, ReturnType, NonTemplateOverloads); |
| 4827 | if (!ReturnType.isNull() |
| 4828 | && ReturnType->isSpecificBuiltinType(BuiltinType::Bool)) |
| 4829 | S.Diag(E->getExprLoc(), diag::note_function_to_bool_call) |
| 4830 | << FixItHint::CreateInsertion( |
| 4831 | S.getPreprocessor().getLocForEndOfToken(E->getLocEnd()), "()"); |
Richard Trieu | 26b45d8 | 2011-12-06 04:48:01 +0000 | [diff] [blame] | 4832 | return; |
| 4833 | } |
Lang Hames | e14ca9f | 2011-12-05 20:49:50 +0000 | [diff] [blame] | 4834 | } |
| 4835 | } |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4836 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4837 | |
| 4838 | // Strip vector types. |
| 4839 | if (isa<VectorType>(Source)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4840 | if (!isa<VectorType>(Target)) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4841 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4842 | return; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4843 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar); |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4844 | } |
Chris Lattner | b792b30 | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 4845 | |
| 4846 | // If the vector cast is cast between two vectors of the same size, it is |
| 4847 | // a bitcast, not a conversion. |
| 4848 | if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target)) |
| 4849 | return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4850 | |
| 4851 | Source = cast<VectorType>(Source)->getElementType().getTypePtr(); |
| 4852 | Target = cast<VectorType>(Target)->getElementType().getTypePtr(); |
| 4853 | } |
| 4854 | |
| 4855 | // Strip complex types. |
| 4856 | if (isa<ComplexType>(Source)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4857 | if (!isa<ComplexType>(Target)) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4858 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4859 | return; |
| 4860 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4861 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar); |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4862 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4863 | |
| 4864 | Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); |
| 4865 | Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); |
| 4866 | } |
| 4867 | |
| 4868 | const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); |
| 4869 | const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); |
| 4870 | |
| 4871 | // If the source is floating point... |
| 4872 | if (SourceBT && SourceBT->isFloatingPoint()) { |
| 4873 | // ...and the target is floating point... |
| 4874 | if (TargetBT && TargetBT->isFloatingPoint()) { |
| 4875 | // ...then warn if we're dropping FP rank. |
| 4876 | |
| 4877 | // Builtin FP kinds are ordered by increasing FP rank. |
| 4878 | if (SourceBT->getKind() > TargetBT->getKind()) { |
| 4879 | // Don't warn about float constants that are precisely |
| 4880 | // representable in the target type. |
| 4881 | Expr::EvalResult result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4882 | if (E->EvaluateAsRValue(result, S.Context)) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4883 | // Value might be a float, a float vector, or a float complex. |
| 4884 | if (IsSameFloatAfterCast(result.Val, |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4885 | S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)), |
| 4886 | S.Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4887 | return; |
| 4888 | } |
| 4889 | |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4890 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4891 | return; |
| 4892 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4893 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4894 | } |
| 4895 | return; |
| 4896 | } |
| 4897 | |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4898 | // If the target is integral, always warn. |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4899 | if (TargetBT && TargetBT->isInteger()) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4900 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4901 | return; |
| 4902 | |
Chandler Carruth | a5b9332 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 4903 | Expr *InnerE = E->IgnoreParenImpCasts(); |
Matt Beaumont-Gay | 634c8af | 2011-09-08 22:30:47 +0000 | [diff] [blame] | 4904 | // We also want to warn on, e.g., "int i = -1.234" |
| 4905 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE)) |
| 4906 | if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus) |
| 4907 | InnerE = UOp->getSubExpr()->IgnoreParenImpCasts(); |
| 4908 | |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4909 | if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) { |
| 4910 | DiagnoseFloatingLiteralImpCast(S, FL, T, CC); |
Chandler Carruth | a5b9332 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 4911 | } else { |
| 4912 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer); |
| 4913 | } |
| 4914 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4915 | |
Hans Wennborg | 88617a2 | 2012-08-28 15:44:30 +0000 | [diff] [blame] | 4916 | // If the target is bool, warn if expr is a function or method call. |
| 4917 | if (Target->isSpecificBuiltinType(BuiltinType::Bool) && |
| 4918 | isa<CallExpr>(E)) { |
| 4919 | // Check last argument of function call to see if it is an |
| 4920 | // implicit cast from a type matching the type the result |
| 4921 | // is being cast to. |
| 4922 | CallExpr *CEx = cast<CallExpr>(E); |
| 4923 | unsigned NumArgs = CEx->getNumArgs(); |
| 4924 | if (NumArgs > 0) { |
| 4925 | Expr *LastA = CEx->getArg(NumArgs - 1); |
| 4926 | Expr *InnerE = LastA->IgnoreParenImpCasts(); |
| 4927 | const Type *InnerType = |
| 4928 | S.Context.getCanonicalType(InnerE->getType()).getTypePtr(); |
| 4929 | if (isa<ImplicitCastExpr>(LastA) && (InnerType == Target)) { |
| 4930 | // Warn on this floating-point to bool conversion |
| 4931 | DiagnoseImpCast(S, E, T, CC, |
| 4932 | diag::warn_impcast_floating_point_to_bool); |
| 4933 | } |
| 4934 | } |
| 4935 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4936 | return; |
| 4937 | } |
| 4938 | |
Richard Trieu | 1838ca5 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 4939 | if ((E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) |
David Blaikie | b26331b | 2012-06-19 21:19:06 +0000 | [diff] [blame] | 4940 | == Expr::NPCK_GNUNull) && !Target->isAnyPointerType() |
David Blaikie | e81b43b | 2012-11-08 00:41:20 +0000 | [diff] [blame] | 4941 | && !Target->isBlockPointerType() && !Target->isMemberPointerType() |
David Blaikie | 896c7dd | 2013-02-16 00:56:22 +0000 | [diff] [blame] | 4942 | && Target->isScalarType() && !Target->isNullPtrType()) { |
David Blaikie | b136049 | 2012-03-16 20:30:12 +0000 | [diff] [blame] | 4943 | SourceLocation Loc = E->getSourceRange().getBegin(); |
| 4944 | if (Loc.isMacroID()) |
| 4945 | Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first; |
David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 4946 | if (!Loc.isMacroID() || CC.isMacroID()) |
| 4947 | S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer) |
| 4948 | << T << clang::SourceRange(CC) |
| 4949 | << FixItHint::CreateReplacement(Loc, S.getFixItZeroLiteralForType(T)); |
Richard Trieu | 1838ca5 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 4950 | } |
| 4951 | |
David Blaikie | b26331b | 2012-06-19 21:19:06 +0000 | [diff] [blame] | 4952 | if (!Source->isIntegerType() || !Target->isIntegerType()) |
| 4953 | return; |
| 4954 | |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4955 | // TODO: remove this early return once the false positives for constant->bool |
| 4956 | // in templates, macros, etc, are reduced or removed. |
| 4957 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) |
| 4958 | return; |
| 4959 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4960 | IntRange SourceRange = GetExprRange(S.Context, E); |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4961 | IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4962 | |
| 4963 | if (SourceRange.Width > TargetRange.Width) { |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4964 | // If the source is a constant, use a default-on diagnostic. |
| 4965 | // TODO: this should happen for bitfield stores, too. |
| 4966 | llvm::APSInt Value(32); |
| 4967 | if (E->isIntegerConstantExpr(Value, S.Context)) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4968 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4969 | return; |
| 4970 | |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4971 | std::string PrettySourceValue = Value.toString(10); |
| 4972 | std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); |
| 4973 | |
Ted Kremenek | 5e745da | 2011-10-22 02:37:33 +0000 | [diff] [blame] | 4974 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
| 4975 | S.PDiag(diag::warn_impcast_integer_precision_constant) |
| 4976 | << PrettySourceValue << PrettyTargetValue |
| 4977 | << E->getType() << T << E->getSourceRange() |
| 4978 | << clang::SourceRange(CC)); |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4979 | return; |
| 4980 | } |
| 4981 | |
Chris Lattner | b792b30 | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 4982 | // 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] | 4983 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4984 | return; |
David Blaikie | 0c5d005 | 2012-11-19 23:12:51 +0000 | [diff] [blame] | 4985 | |
David Blaikie | 3705084 | 2012-04-12 22:40:54 +0000 | [diff] [blame] | 4986 | if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64) |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4987 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32, |
| 4988 | /* pruneControlFlow */ true); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4989 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4990 | } |
| 4991 | |
| 4992 | if ((TargetRange.NonNegative && !SourceRange.NonNegative) || |
| 4993 | (!TargetRange.NonNegative && SourceRange.NonNegative && |
| 4994 | SourceRange.Width == TargetRange.Width)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4995 | |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4996 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4997 | return; |
| 4998 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4999 | unsigned DiagID = diag::warn_impcast_integer_sign; |
| 5000 | |
| 5001 | // Traditionally, gcc has warned about this under -Wsign-compare. |
| 5002 | // We also want to warn about it in -Wconversion. |
| 5003 | // So if -Wconversion is off, use a completely identical diagnostic |
| 5004 | // in the sign-compare group. |
| 5005 | // The conditional-checking code will |
| 5006 | if (ICContext) { |
| 5007 | DiagID = diag::warn_impcast_integer_sign_conditional; |
| 5008 | *ICContext = true; |
| 5009 | } |
| 5010 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5011 | return DiagnoseImpCast(S, E, T, CC, DiagID); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5012 | } |
| 5013 | |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 5014 | // Diagnose conversions between different enumeration types. |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 5015 | // In C, we pretend that the type of an EnumConstantDecl is its enumeration |
| 5016 | // type, to give us better diagnostics. |
| 5017 | QualType SourceType = E->getType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5018 | if (!S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 5019 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 5020 | if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 5021 | EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext()); |
| 5022 | SourceType = S.Context.getTypeDeclType(Enum); |
| 5023 | Source = S.Context.getCanonicalType(SourceType).getTypePtr(); |
| 5024 | } |
| 5025 | } |
| 5026 | |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 5027 | if (const EnumType *SourceEnum = Source->getAs<EnumType>()) |
| 5028 | if (const EnumType *TargetEnum = Target->getAs<EnumType>()) |
| 5029 | if ((SourceEnum->getDecl()->getIdentifier() || |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5030 | SourceEnum->getDecl()->getTypedefNameForAnonDecl()) && |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 5031 | (TargetEnum->getDecl()->getIdentifier() || |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5032 | TargetEnum->getDecl()->getTypedefNameForAnonDecl()) && |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 5033 | SourceEnum != TargetEnum) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 5034 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 5035 | return; |
| 5036 | |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 5037 | return DiagnoseImpCast(S, E, SourceType, T, CC, |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 5038 | diag::warn_impcast_different_enum_types); |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 5039 | } |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 5040 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5041 | return; |
| 5042 | } |
| 5043 | |
David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 5044 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, |
| 5045 | SourceLocation CC, QualType T); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5046 | |
| 5047 | void CheckConditionalOperand(Sema &S, Expr *E, QualType T, |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5048 | SourceLocation CC, bool &ICContext) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5049 | E = E->IgnoreParenImpCasts(); |
| 5050 | |
| 5051 | if (isa<ConditionalOperator>(E)) |
David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 5052 | return CheckConditionalOperator(S, cast<ConditionalOperator>(E), CC, T); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5053 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5054 | AnalyzeImplicitConversions(S, E, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5055 | if (E->getType() != T) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5056 | return CheckImplicitConversion(S, E, T, CC, &ICContext); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5057 | return; |
| 5058 | } |
| 5059 | |
David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 5060 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, |
| 5061 | SourceLocation CC, QualType T) { |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5062 | AnalyzeImplicitConversions(S, E->getCond(), CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5063 | |
| 5064 | bool Suspicious = false; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5065 | CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious); |
| 5066 | CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5067 | |
| 5068 | // If -Wconversion would have warned about either of the candidates |
| 5069 | // for a signedness conversion to the context type... |
| 5070 | if (!Suspicious) return; |
| 5071 | |
| 5072 | // ...but it's currently ignored... |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 5073 | if (S.Diags.getDiagnosticLevel(diag::warn_impcast_integer_sign_conditional, |
| 5074 | CC)) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5075 | return; |
| 5076 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5077 | // ...then check whether it would have warned about either of the |
| 5078 | // candidates for a signedness conversion to the condition type. |
Richard Trieu | 5254161 | 2011-07-21 02:46:28 +0000 | [diff] [blame] | 5079 | if (E->getType() == T) return; |
| 5080 | |
| 5081 | Suspicious = false; |
| 5082 | CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(), |
| 5083 | E->getType(), CC, &Suspicious); |
| 5084 | if (!Suspicious) |
| 5085 | CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(), |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5086 | E->getType(), CC, &Suspicious); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5087 | } |
| 5088 | |
| 5089 | /// AnalyzeImplicitConversions - Find and report any interesting |
| 5090 | /// implicit conversions in the given expression. There are a couple |
| 5091 | /// of competing diagnostics here, -Wconversion and -Wsign-compare. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5092 | void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5093 | QualType T = OrigE->getType(); |
| 5094 | Expr *E = OrigE->IgnoreParenImpCasts(); |
| 5095 | |
Douglas Gregor | f8b6e15 | 2011-10-10 17:38:18 +0000 | [diff] [blame] | 5096 | if (E->isTypeDependent() || E->isValueDependent()) |
| 5097 | return; |
| 5098 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5099 | // For conditional operators, we analyze the arguments as if they |
| 5100 | // were being fed directly into the output. |
| 5101 | if (isa<ConditionalOperator>(E)) { |
| 5102 | ConditionalOperator *CO = cast<ConditionalOperator>(E); |
David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 5103 | CheckConditionalOperator(S, CO, CC, T); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5104 | return; |
| 5105 | } |
| 5106 | |
Hans Wennborg | 88617a2 | 2012-08-28 15:44:30 +0000 | [diff] [blame] | 5107 | // Check implicit argument conversions for function calls. |
| 5108 | if (CallExpr *Call = dyn_cast<CallExpr>(E)) |
| 5109 | CheckImplicitArgumentConversions(S, Call, CC); |
| 5110 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5111 | // Go ahead and check any implicit conversions we might have skipped. |
| 5112 | // The non-canonical typecheck is just an optimization; |
| 5113 | // CheckImplicitConversion will filter out dead implicit conversions. |
| 5114 | if (E->getType() != T) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5115 | CheckImplicitConversion(S, E, T, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5116 | |
| 5117 | // Now continue drilling into this expression. |
| 5118 | |
| 5119 | // Skip past explicit casts. |
| 5120 | if (isa<ExplicitCastExpr>(E)) { |
| 5121 | E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts(); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5122 | return AnalyzeImplicitConversions(S, E, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5123 | } |
| 5124 | |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 5125 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 5126 | // Do a somewhat different check with comparison operators. |
| 5127 | if (BO->isComparisonOp()) |
| 5128 | return AnalyzeComparison(S, BO); |
| 5129 | |
Eli Friedman | 0fa0638 | 2012-01-26 23:34:06 +0000 | [diff] [blame] | 5130 | // And with simple assignments. |
| 5131 | if (BO->getOpcode() == BO_Assign) |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 5132 | return AnalyzeAssignment(S, BO); |
| 5133 | } |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5134 | |
| 5135 | // These break the otherwise-useful invariant below. Fortunately, |
| 5136 | // we don't really need to recurse into them, because any internal |
| 5137 | // expressions should have been analyzed already when they were |
| 5138 | // built into statements. |
| 5139 | if (isa<StmtExpr>(E)) return; |
| 5140 | |
| 5141 | // Don't descend into unevaluated contexts. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5142 | if (isa<UnaryExprOrTypeTraitExpr>(E)) return; |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5143 | |
| 5144 | // Now just recurse over the expression's children. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5145 | CC = E->getExprLoc(); |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 5146 | BinaryOperator *BO = dyn_cast<BinaryOperator>(E); |
| 5147 | bool IsLogicalOperator = BO && BO->isLogicalOp(); |
| 5148 | for (Stmt::child_range I = E->children(); I; ++I) { |
Douglas Gregor | 54042f1 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 5149 | Expr *ChildExpr = dyn_cast_or_null<Expr>(*I); |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 5150 | if (!ChildExpr) |
| 5151 | continue; |
| 5152 | |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 5153 | if (IsLogicalOperator && |
| 5154 | isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts())) |
| 5155 | // Ignore checking string literals that are in logical operators. |
| 5156 | continue; |
| 5157 | AnalyzeImplicitConversions(S, ChildExpr, CC); |
| 5158 | } |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5159 | } |
| 5160 | |
| 5161 | } // end anonymous namespace |
| 5162 | |
| 5163 | /// Diagnoses "dangerous" implicit conversions within the given |
| 5164 | /// expression (which is a full expression). Implements -Wconversion |
| 5165 | /// and -Wsign-compare. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5166 | /// |
| 5167 | /// \param CC the "context" location of the implicit conversion, i.e. |
| 5168 | /// the most location of the syntactic entity requiring the implicit |
| 5169 | /// conversion |
| 5170 | void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5171 | // Don't diagnose in unevaluated contexts. |
David Blaikie | 71f55f7 | 2012-08-06 22:47:24 +0000 | [diff] [blame] | 5172 | if (isUnevaluatedContext()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5173 | return; |
| 5174 | |
| 5175 | // Don't diagnose for value- or type-dependent expressions. |
| 5176 | if (E->isTypeDependent() || E->isValueDependent()) |
| 5177 | return; |
| 5178 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5179 | // Check for array bounds violations in cases where the check isn't triggered |
| 5180 | // elsewhere for other Expr types (like BinaryOperators), e.g. when an |
| 5181 | // ArraySubscriptExpr is on the RHS of a variable initialization. |
| 5182 | CheckArrayAccess(E); |
| 5183 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5184 | // This is not the right CC for (e.g.) a variable initialization. |
| 5185 | AnalyzeImplicitConversions(*this, E, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5186 | } |
| 5187 | |
Fariborz Jahanian | ad48a50 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 5188 | /// Diagnose when expression is an integer constant expression and its evaluation |
| 5189 | /// results in integer overflow |
| 5190 | void Sema::CheckForIntOverflow (Expr *E) { |
| 5191 | if (const BinaryOperator *BExpr = dyn_cast<BinaryOperator>(E->IgnoreParens())) { |
| 5192 | unsigned Opc = BExpr->getOpcode(); |
| 5193 | if (Opc != BO_Add && Opc != BO_Sub && Opc != BO_Mul) |
| 5194 | return; |
| 5195 | llvm::SmallVector<PartialDiagnosticAt, 4> Diags; |
| 5196 | E->EvaluateForOverflow(Context, &Diags); |
| 5197 | } |
| 5198 | } |
| 5199 | |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 5200 | namespace { |
| 5201 | /// \brief Visitor for expressions which looks for unsequenced operations on the |
| 5202 | /// same object. |
| 5203 | class SequenceChecker : public EvaluatedExprVisitor<SequenceChecker> { |
| 5204 | /// \brief A tree of sequenced regions within an expression. Two regions are |
| 5205 | /// unsequenced if one is an ancestor or a descendent of the other. When we |
| 5206 | /// finish processing an expression with sequencing, such as a comma |
| 5207 | /// expression, we fold its tree nodes into its parent, since they are |
| 5208 | /// unsequenced with respect to nodes we will visit later. |
| 5209 | class SequenceTree { |
| 5210 | struct Value { |
| 5211 | explicit Value(unsigned Parent) : Parent(Parent), Merged(false) {} |
| 5212 | unsigned Parent : 31; |
| 5213 | bool Merged : 1; |
| 5214 | }; |
| 5215 | llvm::SmallVector<Value, 8> Values; |
| 5216 | |
| 5217 | public: |
| 5218 | /// \brief A region within an expression which may be sequenced with respect |
| 5219 | /// to some other region. |
| 5220 | class Seq { |
| 5221 | explicit Seq(unsigned N) : Index(N) {} |
| 5222 | unsigned Index; |
| 5223 | friend class SequenceTree; |
| 5224 | public: |
| 5225 | Seq() : Index(0) {} |
| 5226 | }; |
| 5227 | |
| 5228 | SequenceTree() { Values.push_back(Value(0)); } |
| 5229 | Seq root() const { return Seq(0); } |
| 5230 | |
| 5231 | /// \brief Create a new sequence of operations, which is an unsequenced |
| 5232 | /// subset of \p Parent. This sequence of operations is sequenced with |
| 5233 | /// respect to other children of \p Parent. |
| 5234 | Seq allocate(Seq Parent) { |
| 5235 | Values.push_back(Value(Parent.Index)); |
| 5236 | return Seq(Values.size() - 1); |
| 5237 | } |
| 5238 | |
| 5239 | /// \brief Merge a sequence of operations into its parent. |
| 5240 | void merge(Seq S) { |
| 5241 | Values[S.Index].Merged = true; |
| 5242 | } |
| 5243 | |
| 5244 | /// \brief Determine whether two operations are unsequenced. This operation |
| 5245 | /// is asymmetric: \p Cur should be the more recent sequence, and \p Old |
| 5246 | /// should have been merged into its parent as appropriate. |
| 5247 | bool isUnsequenced(Seq Cur, Seq Old) { |
| 5248 | unsigned C = representative(Cur.Index); |
| 5249 | unsigned Target = representative(Old.Index); |
| 5250 | while (C >= Target) { |
| 5251 | if (C == Target) |
| 5252 | return true; |
| 5253 | C = Values[C].Parent; |
| 5254 | } |
| 5255 | return false; |
| 5256 | } |
| 5257 | |
| 5258 | private: |
| 5259 | /// \brief Pick a representative for a sequence. |
| 5260 | unsigned representative(unsigned K) { |
| 5261 | if (Values[K].Merged) |
| 5262 | // Perform path compression as we go. |
| 5263 | return Values[K].Parent = representative(Values[K].Parent); |
| 5264 | return K; |
| 5265 | } |
| 5266 | }; |
| 5267 | |
| 5268 | /// An object for which we can track unsequenced uses. |
| 5269 | typedef NamedDecl *Object; |
| 5270 | |
| 5271 | /// Different flavors of object usage which we track. We only track the |
| 5272 | /// least-sequenced usage of each kind. |
| 5273 | enum UsageKind { |
| 5274 | /// A read of an object. Multiple unsequenced reads are OK. |
| 5275 | UK_Use, |
| 5276 | /// A modification of an object which is sequenced before the value |
| 5277 | /// computation of the expression, such as ++n. |
| 5278 | UK_ModAsValue, |
| 5279 | /// A modification of an object which is not sequenced before the value |
| 5280 | /// computation of the expression, such as n++. |
| 5281 | UK_ModAsSideEffect, |
| 5282 | |
| 5283 | UK_Count = UK_ModAsSideEffect + 1 |
| 5284 | }; |
| 5285 | |
| 5286 | struct Usage { |
| 5287 | Usage() : Use(0), Seq() {} |
| 5288 | Expr *Use; |
| 5289 | SequenceTree::Seq Seq; |
| 5290 | }; |
| 5291 | |
| 5292 | struct UsageInfo { |
| 5293 | UsageInfo() : Diagnosed(false) {} |
| 5294 | Usage Uses[UK_Count]; |
| 5295 | /// Have we issued a diagnostic for this variable already? |
| 5296 | bool Diagnosed; |
| 5297 | }; |
| 5298 | typedef llvm::SmallDenseMap<Object, UsageInfo, 16> UsageInfoMap; |
| 5299 | |
| 5300 | Sema &SemaRef; |
| 5301 | /// Sequenced regions within the expression. |
| 5302 | SequenceTree Tree; |
| 5303 | /// Declaration modifications and references which we have seen. |
| 5304 | UsageInfoMap UsageMap; |
| 5305 | /// The region we are currently within. |
| 5306 | SequenceTree::Seq Region; |
| 5307 | /// Filled in with declarations which were modified as a side-effect |
| 5308 | /// (that is, post-increment operations). |
| 5309 | llvm::SmallVectorImpl<std::pair<Object, Usage> > *ModAsSideEffect; |
Richard Smith | 1a2dcd5 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 5310 | /// Expressions to check later. We defer checking these to reduce |
| 5311 | /// stack usage. |
| 5312 | llvm::SmallVectorImpl<Expr*> &WorkList; |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 5313 | |
| 5314 | /// RAII object wrapping the visitation of a sequenced subexpression of an |
| 5315 | /// expression. At the end of this process, the side-effects of the evaluation |
| 5316 | /// become sequenced with respect to the value computation of the result, so |
| 5317 | /// we downgrade any UK_ModAsSideEffect within the evaluation to |
| 5318 | /// UK_ModAsValue. |
| 5319 | struct SequencedSubexpression { |
| 5320 | SequencedSubexpression(SequenceChecker &Self) |
| 5321 | : Self(Self), OldModAsSideEffect(Self.ModAsSideEffect) { |
| 5322 | Self.ModAsSideEffect = &ModAsSideEffect; |
| 5323 | } |
| 5324 | ~SequencedSubexpression() { |
| 5325 | for (unsigned I = 0, E = ModAsSideEffect.size(); I != E; ++I) { |
| 5326 | UsageInfo &U = Self.UsageMap[ModAsSideEffect[I].first]; |
| 5327 | U.Uses[UK_ModAsSideEffect] = ModAsSideEffect[I].second; |
| 5328 | Self.addUsage(U, ModAsSideEffect[I].first, |
| 5329 | ModAsSideEffect[I].second.Use, UK_ModAsValue); |
| 5330 | } |
| 5331 | Self.ModAsSideEffect = OldModAsSideEffect; |
| 5332 | } |
| 5333 | |
| 5334 | SequenceChecker &Self; |
| 5335 | llvm::SmallVector<std::pair<Object, Usage>, 4> ModAsSideEffect; |
| 5336 | llvm::SmallVectorImpl<std::pair<Object, Usage> > *OldModAsSideEffect; |
| 5337 | }; |
| 5338 | |
| 5339 | /// \brief Find the object which is produced by the specified expression, |
| 5340 | /// if any. |
| 5341 | Object getObject(Expr *E, bool Mod) const { |
| 5342 | E = E->IgnoreParenCasts(); |
| 5343 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 5344 | if (Mod && (UO->getOpcode() == UO_PreInc || UO->getOpcode() == UO_PreDec)) |
| 5345 | return getObject(UO->getSubExpr(), Mod); |
| 5346 | } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 5347 | if (BO->getOpcode() == BO_Comma) |
| 5348 | return getObject(BO->getRHS(), Mod); |
| 5349 | if (Mod && BO->isAssignmentOp()) |
| 5350 | return getObject(BO->getLHS(), Mod); |
| 5351 | } else if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 5352 | // FIXME: Check for more interesting cases, like "x.n = ++x.n". |
| 5353 | if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenCasts())) |
| 5354 | return ME->getMemberDecl(); |
| 5355 | } else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 5356 | // FIXME: If this is a reference, map through to its value. |
| 5357 | return DRE->getDecl(); |
| 5358 | return 0; |
| 5359 | } |
| 5360 | |
| 5361 | /// \brief Note that an object was modified or used by an expression. |
| 5362 | void addUsage(UsageInfo &UI, Object O, Expr *Ref, UsageKind UK) { |
| 5363 | Usage &U = UI.Uses[UK]; |
| 5364 | if (!U.Use || !Tree.isUnsequenced(Region, U.Seq)) { |
| 5365 | if (UK == UK_ModAsSideEffect && ModAsSideEffect) |
| 5366 | ModAsSideEffect->push_back(std::make_pair(O, U)); |
| 5367 | U.Use = Ref; |
| 5368 | U.Seq = Region; |
| 5369 | } |
| 5370 | } |
| 5371 | /// \brief Check whether a modification or use conflicts with a prior usage. |
| 5372 | void checkUsage(Object O, UsageInfo &UI, Expr *Ref, UsageKind OtherKind, |
| 5373 | bool IsModMod) { |
| 5374 | if (UI.Diagnosed) |
| 5375 | return; |
| 5376 | |
| 5377 | const Usage &U = UI.Uses[OtherKind]; |
| 5378 | if (!U.Use || !Tree.isUnsequenced(Region, U.Seq)) |
| 5379 | return; |
| 5380 | |
| 5381 | Expr *Mod = U.Use; |
| 5382 | Expr *ModOrUse = Ref; |
| 5383 | if (OtherKind == UK_Use) |
| 5384 | std::swap(Mod, ModOrUse); |
| 5385 | |
| 5386 | SemaRef.Diag(Mod->getExprLoc(), |
| 5387 | IsModMod ? diag::warn_unsequenced_mod_mod |
| 5388 | : diag::warn_unsequenced_mod_use) |
| 5389 | << O << SourceRange(ModOrUse->getExprLoc()); |
| 5390 | UI.Diagnosed = true; |
| 5391 | } |
| 5392 | |
| 5393 | void notePreUse(Object O, Expr *Use) { |
| 5394 | UsageInfo &U = UsageMap[O]; |
| 5395 | // Uses conflict with other modifications. |
| 5396 | checkUsage(O, U, Use, UK_ModAsValue, false); |
| 5397 | } |
| 5398 | void notePostUse(Object O, Expr *Use) { |
| 5399 | UsageInfo &U = UsageMap[O]; |
| 5400 | checkUsage(O, U, Use, UK_ModAsSideEffect, false); |
| 5401 | addUsage(U, O, Use, UK_Use); |
| 5402 | } |
| 5403 | |
| 5404 | void notePreMod(Object O, Expr *Mod) { |
| 5405 | UsageInfo &U = UsageMap[O]; |
| 5406 | // Modifications conflict with other modifications and with uses. |
| 5407 | checkUsage(O, U, Mod, UK_ModAsValue, true); |
| 5408 | checkUsage(O, U, Mod, UK_Use, false); |
| 5409 | } |
| 5410 | void notePostMod(Object O, Expr *Use, UsageKind UK) { |
| 5411 | UsageInfo &U = UsageMap[O]; |
| 5412 | checkUsage(O, U, Use, UK_ModAsSideEffect, true); |
| 5413 | addUsage(U, O, Use, UK); |
| 5414 | } |
| 5415 | |
| 5416 | public: |
Richard Smith | 1a2dcd5 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 5417 | SequenceChecker(Sema &S, Expr *E, |
| 5418 | llvm::SmallVectorImpl<Expr*> &WorkList) |
Richard Smith | e5096c8 | 2013-01-17 01:40:50 +0000 | [diff] [blame] | 5419 | : EvaluatedExprVisitor<SequenceChecker>(S.Context), SemaRef(S), |
Richard Smith | 1a2dcd5 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 5420 | Region(Tree.root()), ModAsSideEffect(0), WorkList(WorkList) { |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 5421 | Visit(E); |
| 5422 | } |
| 5423 | |
| 5424 | void VisitStmt(Stmt *S) { |
| 5425 | // Skip all statements which aren't expressions for now. |
| 5426 | } |
| 5427 | |
| 5428 | void VisitExpr(Expr *E) { |
| 5429 | // By default, just recurse to evaluated subexpressions. |
Richard Smith | e5096c8 | 2013-01-17 01:40:50 +0000 | [diff] [blame] | 5430 | EvaluatedExprVisitor<SequenceChecker>::VisitStmt(E); |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 5431 | } |
| 5432 | |
| 5433 | void VisitCastExpr(CastExpr *E) { |
| 5434 | Object O = Object(); |
| 5435 | if (E->getCastKind() == CK_LValueToRValue) |
| 5436 | O = getObject(E->getSubExpr(), false); |
| 5437 | |
| 5438 | if (O) |
| 5439 | notePreUse(O, E); |
| 5440 | VisitExpr(E); |
| 5441 | if (O) |
| 5442 | notePostUse(O, E); |
| 5443 | } |
| 5444 | |
| 5445 | void VisitBinComma(BinaryOperator *BO) { |
| 5446 | // C++11 [expr.comma]p1: |
| 5447 | // Every value computation and side effect associated with the left |
| 5448 | // expression is sequenced before every value computation and side |
| 5449 | // effect associated with the right expression. |
| 5450 | SequenceTree::Seq LHS = Tree.allocate(Region); |
| 5451 | SequenceTree::Seq RHS = Tree.allocate(Region); |
| 5452 | SequenceTree::Seq OldRegion = Region; |
| 5453 | |
| 5454 | { |
| 5455 | SequencedSubexpression SeqLHS(*this); |
| 5456 | Region = LHS; |
| 5457 | Visit(BO->getLHS()); |
| 5458 | } |
| 5459 | |
| 5460 | Region = RHS; |
| 5461 | Visit(BO->getRHS()); |
| 5462 | |
| 5463 | Region = OldRegion; |
| 5464 | |
| 5465 | // Forget that LHS and RHS are sequenced. They are both unsequenced |
| 5466 | // with respect to other stuff. |
| 5467 | Tree.merge(LHS); |
| 5468 | Tree.merge(RHS); |
| 5469 | } |
| 5470 | |
| 5471 | void VisitBinAssign(BinaryOperator *BO) { |
| 5472 | // The modification is sequenced after the value computation of the LHS |
| 5473 | // and RHS, so check it before inspecting the operands and update the |
| 5474 | // map afterwards. |
| 5475 | Object O = getObject(BO->getLHS(), true); |
| 5476 | if (!O) |
| 5477 | return VisitExpr(BO); |
| 5478 | |
| 5479 | notePreMod(O, BO); |
| 5480 | |
| 5481 | // C++11 [expr.ass]p7: |
| 5482 | // E1 op= E2 is equivalent to E1 = E1 op E2, except that E1 is evaluated |
| 5483 | // only once. |
| 5484 | // |
| 5485 | // Therefore, for a compound assignment operator, O is considered used |
| 5486 | // everywhere except within the evaluation of E1 itself. |
| 5487 | if (isa<CompoundAssignOperator>(BO)) |
| 5488 | notePreUse(O, BO); |
| 5489 | |
| 5490 | Visit(BO->getLHS()); |
| 5491 | |
| 5492 | if (isa<CompoundAssignOperator>(BO)) |
| 5493 | notePostUse(O, BO); |
| 5494 | |
| 5495 | Visit(BO->getRHS()); |
| 5496 | |
| 5497 | notePostMod(O, BO, UK_ModAsValue); |
| 5498 | } |
| 5499 | void VisitCompoundAssignOperator(CompoundAssignOperator *CAO) { |
| 5500 | VisitBinAssign(CAO); |
| 5501 | } |
| 5502 | |
| 5503 | void VisitUnaryPreInc(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } |
| 5504 | void VisitUnaryPreDec(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } |
| 5505 | void VisitUnaryPreIncDec(UnaryOperator *UO) { |
| 5506 | Object O = getObject(UO->getSubExpr(), true); |
| 5507 | if (!O) |
| 5508 | return VisitExpr(UO); |
| 5509 | |
| 5510 | notePreMod(O, UO); |
| 5511 | Visit(UO->getSubExpr()); |
| 5512 | notePostMod(O, UO, UK_ModAsValue); |
| 5513 | } |
| 5514 | |
| 5515 | void VisitUnaryPostInc(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } |
| 5516 | void VisitUnaryPostDec(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } |
| 5517 | void VisitUnaryPostIncDec(UnaryOperator *UO) { |
| 5518 | Object O = getObject(UO->getSubExpr(), true); |
| 5519 | if (!O) |
| 5520 | return VisitExpr(UO); |
| 5521 | |
| 5522 | notePreMod(O, UO); |
| 5523 | Visit(UO->getSubExpr()); |
| 5524 | notePostMod(O, UO, UK_ModAsSideEffect); |
| 5525 | } |
| 5526 | |
| 5527 | /// Don't visit the RHS of '&&' or '||' if it might not be evaluated. |
| 5528 | void VisitBinLOr(BinaryOperator *BO) { |
| 5529 | // The side-effects of the LHS of an '&&' are sequenced before the |
| 5530 | // value computation of the RHS, and hence before the value computation |
| 5531 | // of the '&&' itself, unless the LHS evaluates to zero. We treat them |
| 5532 | // as if they were unconditionally sequenced. |
| 5533 | { |
| 5534 | SequencedSubexpression Sequenced(*this); |
| 5535 | Visit(BO->getLHS()); |
| 5536 | } |
| 5537 | |
| 5538 | bool Result; |
| 5539 | if (!BO->getLHS()->isValueDependent() && |
Richard Smith | 995e4a7 | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 5540 | BO->getLHS()->EvaluateAsBooleanCondition(Result, SemaRef.Context)) { |
| 5541 | if (!Result) |
| 5542 | Visit(BO->getRHS()); |
| 5543 | } else { |
| 5544 | // Check for unsequenced operations in the RHS, treating it as an |
| 5545 | // entirely separate evaluation. |
| 5546 | // |
| 5547 | // FIXME: If there are operations in the RHS which are unsequenced |
| 5548 | // with respect to operations outside the RHS, and those operations |
| 5549 | // are unconditionally evaluated, diagnose them. |
Richard Smith | 1a2dcd5 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 5550 | WorkList.push_back(BO->getRHS()); |
Richard Smith | 995e4a7 | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 5551 | } |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 5552 | } |
| 5553 | void VisitBinLAnd(BinaryOperator *BO) { |
| 5554 | { |
| 5555 | SequencedSubexpression Sequenced(*this); |
| 5556 | Visit(BO->getLHS()); |
| 5557 | } |
| 5558 | |
| 5559 | bool Result; |
| 5560 | if (!BO->getLHS()->isValueDependent() && |
Richard Smith | 995e4a7 | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 5561 | BO->getLHS()->EvaluateAsBooleanCondition(Result, SemaRef.Context)) { |
| 5562 | if (Result) |
| 5563 | Visit(BO->getRHS()); |
| 5564 | } else { |
Richard Smith | 1a2dcd5 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 5565 | WorkList.push_back(BO->getRHS()); |
Richard Smith | 995e4a7 | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 5566 | } |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 5567 | } |
| 5568 | |
| 5569 | // Only visit the condition, unless we can be sure which subexpression will |
| 5570 | // be chosen. |
| 5571 | void VisitAbstractConditionalOperator(AbstractConditionalOperator *CO) { |
| 5572 | SequencedSubexpression Sequenced(*this); |
| 5573 | Visit(CO->getCond()); |
| 5574 | |
| 5575 | bool Result; |
| 5576 | if (!CO->getCond()->isValueDependent() && |
| 5577 | CO->getCond()->EvaluateAsBooleanCondition(Result, SemaRef.Context)) |
| 5578 | Visit(Result ? CO->getTrueExpr() : CO->getFalseExpr()); |
Richard Smith | 995e4a7 | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 5579 | else { |
Richard Smith | 1a2dcd5 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 5580 | WorkList.push_back(CO->getTrueExpr()); |
| 5581 | WorkList.push_back(CO->getFalseExpr()); |
Richard Smith | 995e4a7 | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 5582 | } |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 5583 | } |
| 5584 | |
| 5585 | void VisitCXXConstructExpr(CXXConstructExpr *CCE) { |
| 5586 | if (!CCE->isListInitialization()) |
| 5587 | return VisitExpr(CCE); |
| 5588 | |
| 5589 | // In C++11, list initializations are sequenced. |
| 5590 | llvm::SmallVector<SequenceTree::Seq, 32> Elts; |
| 5591 | SequenceTree::Seq Parent = Region; |
| 5592 | for (CXXConstructExpr::arg_iterator I = CCE->arg_begin(), |
| 5593 | E = CCE->arg_end(); |
| 5594 | I != E; ++I) { |
| 5595 | Region = Tree.allocate(Parent); |
| 5596 | Elts.push_back(Region); |
| 5597 | Visit(*I); |
| 5598 | } |
| 5599 | |
| 5600 | // Forget that the initializers are sequenced. |
| 5601 | Region = Parent; |
| 5602 | for (unsigned I = 0; I < Elts.size(); ++I) |
| 5603 | Tree.merge(Elts[I]); |
| 5604 | } |
| 5605 | |
| 5606 | void VisitInitListExpr(InitListExpr *ILE) { |
| 5607 | if (!SemaRef.getLangOpts().CPlusPlus11) |
| 5608 | return VisitExpr(ILE); |
| 5609 | |
| 5610 | // In C++11, list initializations are sequenced. |
| 5611 | llvm::SmallVector<SequenceTree::Seq, 32> Elts; |
| 5612 | SequenceTree::Seq Parent = Region; |
| 5613 | for (unsigned I = 0; I < ILE->getNumInits(); ++I) { |
| 5614 | Expr *E = ILE->getInit(I); |
| 5615 | if (!E) continue; |
| 5616 | Region = Tree.allocate(Parent); |
| 5617 | Elts.push_back(Region); |
| 5618 | Visit(E); |
| 5619 | } |
| 5620 | |
| 5621 | // Forget that the initializers are sequenced. |
| 5622 | Region = Parent; |
| 5623 | for (unsigned I = 0; I < Elts.size(); ++I) |
| 5624 | Tree.merge(Elts[I]); |
| 5625 | } |
| 5626 | }; |
| 5627 | } |
| 5628 | |
| 5629 | void Sema::CheckUnsequencedOperations(Expr *E) { |
Richard Smith | 1a2dcd5 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 5630 | llvm::SmallVector<Expr*, 8> WorkList; |
| 5631 | WorkList.push_back(E); |
| 5632 | while (!WorkList.empty()) { |
| 5633 | Expr *Item = WorkList.back(); |
| 5634 | WorkList.pop_back(); |
| 5635 | SequenceChecker(*this, Item, WorkList); |
| 5636 | } |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 5637 | } |
| 5638 | |
Fariborz Jahanian | ad48a50 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 5639 | void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc, |
| 5640 | bool IsConstexpr) { |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 5641 | CheckImplicitConversions(E, CheckLoc); |
| 5642 | CheckUnsequencedOperations(E); |
Fariborz Jahanian | ad48a50 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 5643 | if (!IsConstexpr && !E->isValueDependent()) |
| 5644 | CheckForIntOverflow(E); |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 5645 | } |
| 5646 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5647 | void Sema::CheckBitFieldInitialization(SourceLocation InitLoc, |
| 5648 | FieldDecl *BitField, |
| 5649 | Expr *Init) { |
| 5650 | (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc); |
| 5651 | } |
| 5652 | |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 5653 | /// CheckParmsForFunctionDef - Check that the parameters of the given |
| 5654 | /// function are appropriate for the definition of a function. This |
| 5655 | /// takes care of any checks that cannot be performed on the |
| 5656 | /// declaration itself, e.g., that the types of each of the function |
| 5657 | /// parameters are complete. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 5658 | bool Sema::CheckParmsForFunctionDef(ParmVarDecl **P, ParmVarDecl **PEnd, |
| 5659 | bool CheckParameterNames) { |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 5660 | bool HasInvalidParm = false; |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 5661 | for (; P != PEnd; ++P) { |
| 5662 | ParmVarDecl *Param = *P; |
| 5663 | |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 5664 | // C99 6.7.5.3p4: the parameters in a parameter type list in a |
| 5665 | // function declarator that is part of a function definition of |
| 5666 | // that function shall not have incomplete type. |
| 5667 | // |
| 5668 | // This is also C++ [dcl.fct]p6. |
| 5669 | if (!Param->isInvalidDecl() && |
| 5670 | RequireCompleteType(Param->getLocation(), Param->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5671 | diag::err_typecheck_decl_incomplete_type)) { |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 5672 | Param->setInvalidDecl(); |
| 5673 | HasInvalidParm = true; |
| 5674 | } |
| 5675 | |
| 5676 | // C99 6.9.1p5: If the declarator includes a parameter type list, the |
| 5677 | // declaration of each parameter shall include an identifier. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 5678 | if (CheckParameterNames && |
| 5679 | Param->getIdentifier() == 0 && |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 5680 | !Param->isImplicit() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5681 | !getLangOpts().CPlusPlus) |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 5682 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
Sam Weinig | d17e340 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 5683 | |
| 5684 | // C99 6.7.5.3p12: |
| 5685 | // If the function declarator is not part of a definition of that |
| 5686 | // function, parameters may have incomplete type and may use the [*] |
| 5687 | // notation in their sequences of declarator specifiers to specify |
| 5688 | // variable length array types. |
| 5689 | QualType PType = Param->getOriginalType(); |
| 5690 | if (const ArrayType *AT = Context.getAsArrayType(PType)) { |
| 5691 | if (AT->getSizeModifier() == ArrayType::Star) { |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 5692 | // FIXME: This diagnosic should point the '[*]' if source-location |
Sam Weinig | d17e340 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 5693 | // information is added for it. |
| 5694 | Diag(Param->getLocation(), diag::err_array_star_in_function_definition); |
| 5695 | } |
| 5696 | } |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 5697 | } |
| 5698 | |
| 5699 | return HasInvalidParm; |
| 5700 | } |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5701 | |
| 5702 | /// CheckCastAlign - Implements -Wcast-align, which warns when a |
| 5703 | /// pointer cast increases the alignment requirements. |
| 5704 | void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { |
| 5705 | // This is actually a lot of work to potentially be doing on every |
| 5706 | // 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] | 5707 | if (getDiagnostics().getDiagnosticLevel(diag::warn_cast_align, |
| 5708 | TRange.getBegin()) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 5709 | == DiagnosticsEngine::Ignored) |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5710 | return; |
| 5711 | |
| 5712 | // Ignore dependent types. |
| 5713 | if (T->isDependentType() || Op->getType()->isDependentType()) |
| 5714 | return; |
| 5715 | |
| 5716 | // Require that the destination be a pointer type. |
| 5717 | const PointerType *DestPtr = T->getAs<PointerType>(); |
| 5718 | if (!DestPtr) return; |
| 5719 | |
| 5720 | // If the destination has alignment 1, we're done. |
| 5721 | QualType DestPointee = DestPtr->getPointeeType(); |
| 5722 | if (DestPointee->isIncompleteType()) return; |
| 5723 | CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); |
| 5724 | if (DestAlign.isOne()) return; |
| 5725 | |
| 5726 | // Require that the source be a pointer type. |
| 5727 | const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); |
| 5728 | if (!SrcPtr) return; |
| 5729 | QualType SrcPointee = SrcPtr->getPointeeType(); |
| 5730 | |
| 5731 | // Whitelist casts from cv void*. We already implicitly |
| 5732 | // whitelisted casts to cv void*, since they have alignment 1. |
| 5733 | // Also whitelist casts involving incomplete types, which implicitly |
| 5734 | // includes 'void'. |
| 5735 | if (SrcPointee->isIncompleteType()) return; |
| 5736 | |
| 5737 | CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee); |
| 5738 | if (SrcAlign >= DestAlign) return; |
| 5739 | |
| 5740 | Diag(TRange.getBegin(), diag::warn_cast_align) |
| 5741 | << Op->getType() << T |
| 5742 | << static_cast<unsigned>(SrcAlign.getQuantity()) |
| 5743 | << static_cast<unsigned>(DestAlign.getQuantity()) |
| 5744 | << TRange << Op->getSourceRange(); |
| 5745 | } |
| 5746 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5747 | static const Type* getElementType(const Expr *BaseExpr) { |
| 5748 | const Type* EltType = BaseExpr->getType().getTypePtr(); |
| 5749 | if (EltType->isAnyPointerType()) |
| 5750 | return EltType->getPointeeType().getTypePtr(); |
| 5751 | else if (EltType->isArrayType()) |
| 5752 | return EltType->getBaseElementTypeUnsafe(); |
| 5753 | return EltType; |
| 5754 | } |
| 5755 | |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 5756 | /// \brief Check whether this array fits the idiom of a size-one tail padded |
| 5757 | /// array member of a struct. |
| 5758 | /// |
| 5759 | /// We avoid emitting out-of-bounds access warnings for such arrays as they are |
| 5760 | /// commonly used to emulate flexible arrays in C89 code. |
| 5761 | static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size, |
| 5762 | const NamedDecl *ND) { |
| 5763 | if (Size != 1 || !ND) return false; |
| 5764 | |
| 5765 | const FieldDecl *FD = dyn_cast<FieldDecl>(ND); |
| 5766 | if (!FD) return false; |
| 5767 | |
| 5768 | // Don't consider sizes resulting from macro expansions or template argument |
| 5769 | // substitution to form C89 tail-padded arrays. |
Sean Callanan | d2cf348 | 2012-05-04 18:22:53 +0000 | [diff] [blame] | 5770 | |
| 5771 | TypeSourceInfo *TInfo = FD->getTypeSourceInfo(); |
Ted Kremenek | 00e1f6f | 2012-05-09 05:35:08 +0000 | [diff] [blame] | 5772 | while (TInfo) { |
| 5773 | TypeLoc TL = TInfo->getTypeLoc(); |
| 5774 | // Look through typedefs. |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 5775 | if (TypedefTypeLoc TTL = TL.getAs<TypedefTypeLoc>()) { |
| 5776 | const TypedefNameDecl *TDL = TTL.getTypedefNameDecl(); |
Ted Kremenek | 00e1f6f | 2012-05-09 05:35:08 +0000 | [diff] [blame] | 5777 | TInfo = TDL->getTypeSourceInfo(); |
| 5778 | continue; |
| 5779 | } |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 5780 | if (ConstantArrayTypeLoc CTL = TL.getAs<ConstantArrayTypeLoc>()) { |
| 5781 | const Expr *SizeExpr = dyn_cast<IntegerLiteral>(CTL.getSizeExpr()); |
Chad Rosier | 5e25301 | 2013-02-06 00:58:34 +0000 | [diff] [blame] | 5782 | if (!SizeExpr || SizeExpr->getExprLoc().isMacroID()) |
| 5783 | return false; |
| 5784 | } |
Ted Kremenek | 00e1f6f | 2012-05-09 05:35:08 +0000 | [diff] [blame] | 5785 | break; |
Sean Callanan | d2cf348 | 2012-05-04 18:22:53 +0000 | [diff] [blame] | 5786 | } |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 5787 | |
| 5788 | const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext()); |
Matt Beaumont-Gay | 381711c | 2011-11-29 22:43:53 +0000 | [diff] [blame] | 5789 | if (!RD) return false; |
| 5790 | if (RD->isUnion()) return false; |
| 5791 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 5792 | if (!CRD->isStandardLayout()) return false; |
| 5793 | } |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 5794 | |
Benjamin Kramer | 22d4fed | 2011-08-06 03:04:42 +0000 | [diff] [blame] | 5795 | // See if this is the last field decl in the record. |
| 5796 | const Decl *D = FD; |
| 5797 | while ((D = D->getNextDeclInContext())) |
| 5798 | if (isa<FieldDecl>(D)) |
| 5799 | return false; |
| 5800 | return true; |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 5801 | } |
| 5802 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5803 | void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 5804 | const ArraySubscriptExpr *ASE, |
Richard Smith | 25b009a | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 5805 | bool AllowOnePastEnd, bool IndexNegated) { |
Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 5806 | IndexExpr = IndexExpr->IgnoreParenImpCasts(); |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 5807 | if (IndexExpr->isValueDependent()) |
| 5808 | return; |
| 5809 | |
Matt Beaumont-Gay | 8ef8f43 | 2011-12-12 22:35:02 +0000 | [diff] [blame] | 5810 | const Type *EffectiveType = getElementType(BaseExpr); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5811 | BaseExpr = BaseExpr->IgnoreParenCasts(); |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 5812 | const ConstantArrayType *ArrayTy = |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5813 | Context.getAsConstantArrayType(BaseExpr->getType()); |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 5814 | if (!ArrayTy) |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 5815 | return; |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 5816 | |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 5817 | llvm::APSInt index; |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 5818 | if (!IndexExpr->EvaluateAsInt(index, Context)) |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 5819 | return; |
Richard Smith | 25b009a | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 5820 | if (IndexNegated) |
| 5821 | index = -index; |
Ted Kremenek | 8fd0a5d | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 5822 | |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 5823 | const NamedDecl *ND = NULL; |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 5824 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
| 5825 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 5826 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 5827 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 5828 | |
Ted Kremenek | 9e060ca | 2011-02-23 23:06:04 +0000 | [diff] [blame] | 5829 | if (index.isUnsigned() || !index.isNegative()) { |
Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 5830 | llvm::APInt size = ArrayTy->getSize(); |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 5831 | if (!size.isStrictlyPositive()) |
| 5832 | return; |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5833 | |
| 5834 | const Type* BaseType = getElementType(BaseExpr); |
Nico Weber | de5998f | 2011-09-17 22:59:41 +0000 | [diff] [blame] | 5835 | if (BaseType != EffectiveType) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5836 | // Make sure we're comparing apples to apples when comparing index to size |
| 5837 | uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); |
| 5838 | uint64_t array_typesize = Context.getTypeSize(BaseType); |
Kaelyn Uhrain | d10f4bc | 2011-08-10 19:47:25 +0000 | [diff] [blame] | 5839 | // Handle ptrarith_typesize being zero, such as when casting to void* |
Kaelyn Uhrain | 18f1697 | 2011-08-10 18:49:28 +0000 | [diff] [blame] | 5840 | if (!ptrarith_typesize) ptrarith_typesize = 1; |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5841 | if (ptrarith_typesize != array_typesize) { |
| 5842 | // There's a cast to a different size type involved |
| 5843 | uint64_t ratio = array_typesize / ptrarith_typesize; |
| 5844 | // TODO: Be smarter about handling cases where array_typesize is not a |
| 5845 | // multiple of ptrarith_typesize |
| 5846 | if (ptrarith_typesize * ratio == array_typesize) |
| 5847 | size *= llvm::APInt(size.getBitWidth(), ratio); |
| 5848 | } |
| 5849 | } |
| 5850 | |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 5851 | if (size.getBitWidth() > index.getBitWidth()) |
Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 5852 | index = index.zext(size.getBitWidth()); |
Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 5853 | else if (size.getBitWidth() < index.getBitWidth()) |
Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 5854 | size = size.zext(index.getBitWidth()); |
Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 5855 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5856 | // For array subscripting the index must be less than size, but for pointer |
| 5857 | // arithmetic also allow the index (offset) to be equal to size since |
| 5858 | // computing the next address after the end of the array is legal and |
| 5859 | // commonly done e.g. in C++ iterators and range-based for loops. |
Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 5860 | if (AllowOnePastEnd ? index.ule(size) : index.ult(size)) |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 5861 | return; |
| 5862 | |
| 5863 | // Also don't warn for arrays of size 1 which are members of some |
| 5864 | // structure. These are often used to approximate flexible arrays in C89 |
| 5865 | // code. |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5866 | if (IsTailPaddedMemberArray(*this, size, ND)) |
Ted Kremenek | 8fd0a5d | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 5867 | return; |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 5868 | |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 5869 | // Suppress the warning if the subscript expression (as identified by the |
| 5870 | // ']' location) and the index expression are both from macro expansions |
| 5871 | // within a system header. |
| 5872 | if (ASE) { |
| 5873 | SourceLocation RBracketLoc = SourceMgr.getSpellingLoc( |
| 5874 | ASE->getRBracketLoc()); |
| 5875 | if (SourceMgr.isInSystemHeader(RBracketLoc)) { |
| 5876 | SourceLocation IndexLoc = SourceMgr.getSpellingLoc( |
| 5877 | IndexExpr->getLocStart()); |
| 5878 | if (SourceMgr.isFromSameFile(RBracketLoc, IndexLoc)) |
| 5879 | return; |
| 5880 | } |
| 5881 | } |
| 5882 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5883 | unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds; |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 5884 | if (ASE) |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5885 | DiagID = diag::warn_array_index_exceeds_bounds; |
| 5886 | |
| 5887 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, |
| 5888 | PDiag(DiagID) << index.toString(10, true) |
| 5889 | << size.toString(10, true) |
| 5890 | << (unsigned)size.getLimitedValue(~0U) |
| 5891 | << IndexExpr->getSourceRange()); |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 5892 | } else { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5893 | unsigned DiagID = diag::warn_array_index_precedes_bounds; |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 5894 | if (!ASE) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5895 | DiagID = diag::warn_ptr_arith_precedes_bounds; |
| 5896 | if (index.isNegative()) index = -index; |
| 5897 | } |
| 5898 | |
| 5899 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, |
| 5900 | PDiag(DiagID) << index.toString(10, true) |
| 5901 | << IndexExpr->getSourceRange()); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 5902 | } |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 5903 | |
Matt Beaumont-Gay | cfbc5b5 | 2011-11-29 19:27:11 +0000 | [diff] [blame] | 5904 | if (!ND) { |
| 5905 | // Try harder to find a NamedDecl to point at in the note. |
| 5906 | while (const ArraySubscriptExpr *ASE = |
| 5907 | dyn_cast<ArraySubscriptExpr>(BaseExpr)) |
| 5908 | BaseExpr = ASE->getBase()->IgnoreParenCasts(); |
| 5909 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
| 5910 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); |
| 5911 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) |
| 5912 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); |
| 5913 | } |
| 5914 | |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 5915 | if (ND) |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5916 | DiagRuntimeBehavior(ND->getLocStart(), BaseExpr, |
| 5917 | PDiag(diag::note_array_index_out_of_bounds) |
| 5918 | << ND->getDeclName()); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 5919 | } |
| 5920 | |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5921 | void Sema::CheckArrayAccess(const Expr *expr) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5922 | int AllowOnePastEnd = 0; |
| 5923 | while (expr) { |
| 5924 | expr = expr->IgnoreParenImpCasts(); |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5925 | switch (expr->getStmtClass()) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5926 | case Stmt::ArraySubscriptExprClass: { |
| 5927 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr); |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 5928 | CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE, |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5929 | AllowOnePastEnd > 0); |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5930 | return; |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5931 | } |
| 5932 | case Stmt::UnaryOperatorClass: { |
| 5933 | // Only unwrap the * and & unary operators |
| 5934 | const UnaryOperator *UO = cast<UnaryOperator>(expr); |
| 5935 | expr = UO->getSubExpr(); |
| 5936 | switch (UO->getOpcode()) { |
| 5937 | case UO_AddrOf: |
| 5938 | AllowOnePastEnd++; |
| 5939 | break; |
| 5940 | case UO_Deref: |
| 5941 | AllowOnePastEnd--; |
| 5942 | break; |
| 5943 | default: |
| 5944 | return; |
| 5945 | } |
| 5946 | break; |
| 5947 | } |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5948 | case Stmt::ConditionalOperatorClass: { |
| 5949 | const ConditionalOperator *cond = cast<ConditionalOperator>(expr); |
| 5950 | if (const Expr *lhs = cond->getLHS()) |
| 5951 | CheckArrayAccess(lhs); |
| 5952 | if (const Expr *rhs = cond->getRHS()) |
| 5953 | CheckArrayAccess(rhs); |
| 5954 | return; |
| 5955 | } |
| 5956 | default: |
| 5957 | return; |
| 5958 | } |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 5959 | } |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5960 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5961 | |
| 5962 | //===--- CHECK: Objective-C retain cycles ----------------------------------// |
| 5963 | |
| 5964 | namespace { |
| 5965 | struct RetainCycleOwner { |
| 5966 | RetainCycleOwner() : Variable(0), Indirect(false) {} |
| 5967 | VarDecl *Variable; |
| 5968 | SourceRange Range; |
| 5969 | SourceLocation Loc; |
| 5970 | bool Indirect; |
| 5971 | |
| 5972 | void setLocsFrom(Expr *e) { |
| 5973 | Loc = e->getExprLoc(); |
| 5974 | Range = e->getSourceRange(); |
| 5975 | } |
| 5976 | }; |
| 5977 | } |
| 5978 | |
| 5979 | /// Consider whether capturing the given variable can possibly lead to |
| 5980 | /// a retain cycle. |
| 5981 | static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) { |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 5982 | // In ARC, it's captured strongly iff the variable has __strong |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5983 | // lifetime. In MRR, it's captured strongly if the variable is |
| 5984 | // __block and has an appropriate type. |
| 5985 | if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 5986 | return false; |
| 5987 | |
| 5988 | owner.Variable = var; |
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 5989 | if (ref) |
| 5990 | owner.setLocsFrom(ref); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5991 | return true; |
| 5992 | } |
| 5993 | |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 5994 | static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5995 | while (true) { |
| 5996 | e = e->IgnoreParens(); |
| 5997 | if (CastExpr *cast = dyn_cast<CastExpr>(e)) { |
| 5998 | switch (cast->getCastKind()) { |
| 5999 | case CK_BitCast: |
| 6000 | case CK_LValueBitCast: |
| 6001 | case CK_LValueToRValue: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 6002 | case CK_ARCReclaimReturnedObject: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6003 | e = cast->getSubExpr(); |
| 6004 | continue; |
| 6005 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6006 | default: |
| 6007 | return false; |
| 6008 | } |
| 6009 | } |
| 6010 | |
| 6011 | if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) { |
| 6012 | ObjCIvarDecl *ivar = ref->getDecl(); |
| 6013 | if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 6014 | return false; |
| 6015 | |
| 6016 | // Try to find a retain cycle in the base. |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 6017 | if (!findRetainCycleOwner(S, ref->getBase(), owner)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6018 | return false; |
| 6019 | |
| 6020 | if (ref->isFreeIvar()) owner.setLocsFrom(ref); |
| 6021 | owner.Indirect = true; |
| 6022 | return true; |
| 6023 | } |
| 6024 | |
| 6025 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 6026 | VarDecl *var = dyn_cast<VarDecl>(ref->getDecl()); |
| 6027 | if (!var) return false; |
| 6028 | return considerVariable(var, ref, owner); |
| 6029 | } |
| 6030 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6031 | if (MemberExpr *member = dyn_cast<MemberExpr>(e)) { |
| 6032 | if (member->isArrow()) return false; |
| 6033 | |
| 6034 | // Don't count this as an indirect ownership. |
| 6035 | e = member->getBase(); |
| 6036 | continue; |
| 6037 | } |
| 6038 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 6039 | if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { |
| 6040 | // Only pay attention to pseudo-objects on property references. |
| 6041 | ObjCPropertyRefExpr *pre |
| 6042 | = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm() |
| 6043 | ->IgnoreParens()); |
| 6044 | if (!pre) return false; |
| 6045 | if (pre->isImplicitProperty()) return false; |
| 6046 | ObjCPropertyDecl *property = pre->getExplicitProperty(); |
| 6047 | if (!property->isRetaining() && |
| 6048 | !(property->getPropertyIvarDecl() && |
| 6049 | property->getPropertyIvarDecl()->getType() |
| 6050 | .getObjCLifetime() == Qualifiers::OCL_Strong)) |
| 6051 | return false; |
| 6052 | |
| 6053 | owner.Indirect = true; |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 6054 | if (pre->isSuperReceiver()) { |
| 6055 | owner.Variable = S.getCurMethodDecl()->getSelfDecl(); |
| 6056 | if (!owner.Variable) |
| 6057 | return false; |
| 6058 | owner.Loc = pre->getLocation(); |
| 6059 | owner.Range = pre->getSourceRange(); |
| 6060 | return true; |
| 6061 | } |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 6062 | e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase()) |
| 6063 | ->getSourceExpr()); |
| 6064 | continue; |
| 6065 | } |
| 6066 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6067 | // Array ivars? |
| 6068 | |
| 6069 | return false; |
| 6070 | } |
| 6071 | } |
| 6072 | |
| 6073 | namespace { |
| 6074 | struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> { |
| 6075 | FindCaptureVisitor(ASTContext &Context, VarDecl *variable) |
| 6076 | : EvaluatedExprVisitor<FindCaptureVisitor>(Context), |
| 6077 | Variable(variable), Capturer(0) {} |
| 6078 | |
| 6079 | VarDecl *Variable; |
| 6080 | Expr *Capturer; |
| 6081 | |
| 6082 | void VisitDeclRefExpr(DeclRefExpr *ref) { |
| 6083 | if (ref->getDecl() == Variable && !Capturer) |
| 6084 | Capturer = ref; |
| 6085 | } |
| 6086 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6087 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) { |
| 6088 | if (Capturer) return; |
| 6089 | Visit(ref->getBase()); |
| 6090 | if (Capturer && ref->isFreeIvar()) |
| 6091 | Capturer = ref; |
| 6092 | } |
| 6093 | |
| 6094 | void VisitBlockExpr(BlockExpr *block) { |
| 6095 | // Look inside nested blocks |
| 6096 | if (block->getBlockDecl()->capturesVariable(Variable)) |
| 6097 | Visit(block->getBlockDecl()->getBody()); |
| 6098 | } |
Fariborz Jahanian | 7e2e4c3 | 2012-08-31 20:04:47 +0000 | [diff] [blame] | 6099 | |
| 6100 | void VisitOpaqueValueExpr(OpaqueValueExpr *OVE) { |
| 6101 | if (Capturer) return; |
| 6102 | if (OVE->getSourceExpr()) |
| 6103 | Visit(OVE->getSourceExpr()); |
| 6104 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6105 | }; |
| 6106 | } |
| 6107 | |
| 6108 | /// Check whether the given argument is a block which captures a |
| 6109 | /// variable. |
| 6110 | static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) { |
| 6111 | assert(owner.Variable && owner.Loc.isValid()); |
| 6112 | |
| 6113 | e = e->IgnoreParenCasts(); |
Jordan Rose | 1fac58a | 2012-09-17 17:54:30 +0000 | [diff] [blame] | 6114 | |
| 6115 | // Look through [^{...} copy] and Block_copy(^{...}). |
| 6116 | if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(e)) { |
| 6117 | Selector Cmd = ME->getSelector(); |
| 6118 | if (Cmd.isUnarySelector() && Cmd.getNameForSlot(0) == "copy") { |
| 6119 | e = ME->getInstanceReceiver(); |
| 6120 | if (!e) |
| 6121 | return 0; |
| 6122 | e = e->IgnoreParenCasts(); |
| 6123 | } |
| 6124 | } else if (CallExpr *CE = dyn_cast<CallExpr>(e)) { |
| 6125 | if (CE->getNumArgs() == 1) { |
| 6126 | FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(CE->getCalleeDecl()); |
Ted Kremenek | d13eff6 | 2012-10-02 04:36:54 +0000 | [diff] [blame] | 6127 | if (Fn) { |
| 6128 | const IdentifierInfo *FnI = Fn->getIdentifier(); |
| 6129 | if (FnI && FnI->isStr("_Block_copy")) { |
| 6130 | e = CE->getArg(0)->IgnoreParenCasts(); |
| 6131 | } |
| 6132 | } |
Jordan Rose | 1fac58a | 2012-09-17 17:54:30 +0000 | [diff] [blame] | 6133 | } |
| 6134 | } |
| 6135 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6136 | BlockExpr *block = dyn_cast<BlockExpr>(e); |
| 6137 | if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable)) |
| 6138 | return 0; |
| 6139 | |
| 6140 | FindCaptureVisitor visitor(S.Context, owner.Variable); |
| 6141 | visitor.Visit(block->getBlockDecl()->getBody()); |
| 6142 | return visitor.Capturer; |
| 6143 | } |
| 6144 | |
| 6145 | static void diagnoseRetainCycle(Sema &S, Expr *capturer, |
| 6146 | RetainCycleOwner &owner) { |
| 6147 | assert(capturer); |
| 6148 | assert(owner.Variable && owner.Loc.isValid()); |
| 6149 | |
| 6150 | S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle) |
| 6151 | << owner.Variable << capturer->getSourceRange(); |
| 6152 | S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner) |
| 6153 | << owner.Indirect << owner.Range; |
| 6154 | } |
| 6155 | |
| 6156 | /// Check for a keyword selector that starts with the word 'add' or |
| 6157 | /// 'set'. |
| 6158 | static bool isSetterLikeSelector(Selector sel) { |
| 6159 | if (sel.isUnarySelector()) return false; |
| 6160 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6161 | StringRef str = sel.getNameForSlot(0); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6162 | while (!str.empty() && str.front() == '_') str = str.substr(1); |
Ted Kremenek | 968a0ee | 2011-12-01 00:59:21 +0000 | [diff] [blame] | 6163 | if (str.startswith("set")) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6164 | str = str.substr(3); |
Ted Kremenek | 968a0ee | 2011-12-01 00:59:21 +0000 | [diff] [blame] | 6165 | else if (str.startswith("add")) { |
| 6166 | // Specially whitelist 'addOperationWithBlock:'. |
| 6167 | if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock")) |
| 6168 | return false; |
| 6169 | str = str.substr(3); |
| 6170 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6171 | else |
| 6172 | return false; |
| 6173 | |
| 6174 | if (str.empty()) return true; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 6175 | return !isLowercase(str.front()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6176 | } |
| 6177 | |
| 6178 | /// Check a message send to see if it's likely to cause a retain cycle. |
| 6179 | void Sema::checkRetainCycles(ObjCMessageExpr *msg) { |
| 6180 | // Only check instance methods whose selector looks like a setter. |
| 6181 | if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector())) |
| 6182 | return; |
| 6183 | |
| 6184 | // Try to find a variable that the receiver is strongly owned by. |
| 6185 | RetainCycleOwner owner; |
| 6186 | if (msg->getReceiverKind() == ObjCMessageExpr::Instance) { |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 6187 | if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6188 | return; |
| 6189 | } else { |
| 6190 | assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance); |
| 6191 | owner.Variable = getCurMethodDecl()->getSelfDecl(); |
| 6192 | owner.Loc = msg->getSuperLoc(); |
| 6193 | owner.Range = msg->getSuperLoc(); |
| 6194 | } |
| 6195 | |
| 6196 | // Check whether the receiver is captured by any of the arguments. |
| 6197 | for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) |
| 6198 | if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) |
| 6199 | return diagnoseRetainCycle(*this, capturer, owner); |
| 6200 | } |
| 6201 | |
| 6202 | /// Check a property assign to see if it's likely to cause a retain cycle. |
| 6203 | void Sema::checkRetainCycles(Expr *receiver, Expr *argument) { |
| 6204 | RetainCycleOwner owner; |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 6205 | if (!findRetainCycleOwner(*this, receiver, owner)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6206 | return; |
| 6207 | |
| 6208 | if (Expr *capturer = findCapturingExpr(*this, argument, owner)) |
| 6209 | diagnoseRetainCycle(*this, capturer, owner); |
| 6210 | } |
| 6211 | |
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 6212 | void Sema::checkRetainCycles(VarDecl *Var, Expr *Init) { |
| 6213 | RetainCycleOwner Owner; |
| 6214 | if (!considerVariable(Var, /*DeclRefExpr=*/0, Owner)) |
| 6215 | return; |
| 6216 | |
| 6217 | // Because we don't have an expression for the variable, we have to set the |
| 6218 | // location explicitly here. |
| 6219 | Owner.Loc = Var->getLocation(); |
| 6220 | Owner.Range = Var->getSourceRange(); |
| 6221 | |
| 6222 | if (Expr *Capturer = findCapturingExpr(*this, Init, Owner)) |
| 6223 | diagnoseRetainCycle(*this, Capturer, Owner); |
| 6224 | } |
| 6225 | |
Ted Kremenek | 9d08401 | 2012-12-21 08:04:28 +0000 | [diff] [blame] | 6226 | static bool checkUnsafeAssignLiteral(Sema &S, SourceLocation Loc, |
| 6227 | Expr *RHS, bool isProperty) { |
| 6228 | // Check if RHS is an Objective-C object literal, which also can get |
| 6229 | // immediately zapped in a weak reference. Note that we explicitly |
| 6230 | // allow ObjCStringLiterals, since those are designed to never really die. |
| 6231 | RHS = RHS->IgnoreParenImpCasts(); |
Ted Kremenek | f530ff7 | 2012-12-21 21:59:39 +0000 | [diff] [blame] | 6232 | |
Ted Kremenek | d3292c8 | 2012-12-21 22:46:35 +0000 | [diff] [blame] | 6233 | // This enum needs to match with the 'select' in |
| 6234 | // warn_objc_arc_literal_assign (off-by-1). |
| 6235 | Sema::ObjCLiteralKind Kind = S.CheckLiteralKind(RHS); |
| 6236 | if (Kind == Sema::LK_String || Kind == Sema::LK_None) |
| 6237 | return false; |
Ted Kremenek | f530ff7 | 2012-12-21 21:59:39 +0000 | [diff] [blame] | 6238 | |
| 6239 | S.Diag(Loc, diag::warn_arc_literal_assign) |
Ted Kremenek | d3292c8 | 2012-12-21 22:46:35 +0000 | [diff] [blame] | 6240 | << (unsigned) Kind |
Ted Kremenek | 9d08401 | 2012-12-21 08:04:28 +0000 | [diff] [blame] | 6241 | << (isProperty ? 0 : 1) |
| 6242 | << RHS->getSourceRange(); |
Ted Kremenek | f530ff7 | 2012-12-21 21:59:39 +0000 | [diff] [blame] | 6243 | |
| 6244 | return true; |
Ted Kremenek | 9d08401 | 2012-12-21 08:04:28 +0000 | [diff] [blame] | 6245 | } |
| 6246 | |
Ted Kremenek | b29b30f | 2012-12-21 19:45:30 +0000 | [diff] [blame] | 6247 | static bool checkUnsafeAssignObject(Sema &S, SourceLocation Loc, |
| 6248 | Qualifiers::ObjCLifetime LT, |
| 6249 | Expr *RHS, bool isProperty) { |
| 6250 | // Strip off any implicit cast added to get to the one ARC-specific. |
| 6251 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
| 6252 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
| 6253 | S.Diag(Loc, diag::warn_arc_retained_assign) |
| 6254 | << (LT == Qualifiers::OCL_ExplicitNone) |
| 6255 | << (isProperty ? 0 : 1) |
| 6256 | << RHS->getSourceRange(); |
| 6257 | return true; |
| 6258 | } |
| 6259 | RHS = cast->getSubExpr(); |
| 6260 | } |
| 6261 | |
| 6262 | if (LT == Qualifiers::OCL_Weak && |
| 6263 | checkUnsafeAssignLiteral(S, Loc, RHS, isProperty)) |
| 6264 | return true; |
| 6265 | |
| 6266 | return false; |
| 6267 | } |
| 6268 | |
Ted Kremenek | b1ea510 | 2012-12-21 08:04:20 +0000 | [diff] [blame] | 6269 | bool Sema::checkUnsafeAssigns(SourceLocation Loc, |
| 6270 | QualType LHS, Expr *RHS) { |
| 6271 | Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime(); |
| 6272 | |
| 6273 | if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone) |
| 6274 | return false; |
| 6275 | |
| 6276 | if (checkUnsafeAssignObject(*this, Loc, LT, RHS, false)) |
| 6277 | return true; |
| 6278 | |
| 6279 | return false; |
| 6280 | } |
| 6281 | |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 6282 | void Sema::checkUnsafeExprAssigns(SourceLocation Loc, |
| 6283 | Expr *LHS, Expr *RHS) { |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 6284 | QualType LHSType; |
| 6285 | // PropertyRef on LHS type need be directly obtained from |
| 6286 | // its declaration as it has a PsuedoType. |
| 6287 | ObjCPropertyRefExpr *PRE |
| 6288 | = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens()); |
| 6289 | if (PRE && !PRE->isImplicitProperty()) { |
| 6290 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
| 6291 | if (PD) |
| 6292 | LHSType = PD->getType(); |
| 6293 | } |
| 6294 | |
| 6295 | if (LHSType.isNull()) |
| 6296 | LHSType = LHS->getType(); |
Jordan Rose | 7a27048 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 6297 | |
| 6298 | Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime(); |
| 6299 | |
| 6300 | if (LT == Qualifiers::OCL_Weak) { |
| 6301 | DiagnosticsEngine::Level Level = |
| 6302 | Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, Loc); |
| 6303 | if (Level != DiagnosticsEngine::Ignored) |
| 6304 | getCurFunction()->markSafeWeakUse(LHS); |
| 6305 | } |
| 6306 | |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 6307 | if (checkUnsafeAssigns(Loc, LHSType, RHS)) |
| 6308 | return; |
Jordan Rose | 7a27048 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 6309 | |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 6310 | // FIXME. Check for other life times. |
| 6311 | if (LT != Qualifiers::OCL_None) |
| 6312 | return; |
| 6313 | |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 6314 | if (PRE) { |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 6315 | if (PRE->isImplicitProperty()) |
| 6316 | return; |
| 6317 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
| 6318 | if (!PD) |
| 6319 | return; |
| 6320 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 6321 | unsigned Attributes = PD->getPropertyAttributes(); |
| 6322 | if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) { |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 6323 | // when 'assign' attribute was not explicitly specified |
| 6324 | // by user, ignore it and rely on property type itself |
| 6325 | // for lifetime info. |
| 6326 | unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten(); |
| 6327 | if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) && |
| 6328 | LHSType->isObjCRetainableType()) |
| 6329 | return; |
| 6330 | |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 6331 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 6332 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 6333 | Diag(Loc, diag::warn_arc_retained_property_assign) |
| 6334 | << RHS->getSourceRange(); |
| 6335 | return; |
| 6336 | } |
| 6337 | RHS = cast->getSubExpr(); |
| 6338 | } |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 6339 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 6340 | else if (Attributes & ObjCPropertyDecl::OBJC_PR_weak) { |
Ted Kremenek | b1ea510 | 2012-12-21 08:04:20 +0000 | [diff] [blame] | 6341 | if (checkUnsafeAssignObject(*this, Loc, Qualifiers::OCL_Weak, RHS, true)) |
| 6342 | return; |
Fariborz Jahanian | bd2e27e | 2012-07-06 21:09:27 +0000 | [diff] [blame] | 6343 | } |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 6344 | } |
| 6345 | } |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 6346 | |
| 6347 | //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===// |
| 6348 | |
| 6349 | namespace { |
| 6350 | bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr, |
| 6351 | SourceLocation StmtLoc, |
| 6352 | const NullStmt *Body) { |
| 6353 | // Do not warn if the body is a macro that expands to nothing, e.g: |
| 6354 | // |
| 6355 | // #define CALL(x) |
| 6356 | // if (condition) |
| 6357 | // CALL(0); |
| 6358 | // |
| 6359 | if (Body->hasLeadingEmptyMacro()) |
| 6360 | return false; |
| 6361 | |
| 6362 | // Get line numbers of statement and body. |
| 6363 | bool StmtLineInvalid; |
| 6364 | unsigned StmtLine = SourceMgr.getSpellingLineNumber(StmtLoc, |
| 6365 | &StmtLineInvalid); |
| 6366 | if (StmtLineInvalid) |
| 6367 | return false; |
| 6368 | |
| 6369 | bool BodyLineInvalid; |
| 6370 | unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(), |
| 6371 | &BodyLineInvalid); |
| 6372 | if (BodyLineInvalid) |
| 6373 | return false; |
| 6374 | |
| 6375 | // Warn if null statement and body are on the same line. |
| 6376 | if (StmtLine != BodyLine) |
| 6377 | return false; |
| 6378 | |
| 6379 | return true; |
| 6380 | } |
| 6381 | } // Unnamed namespace |
| 6382 | |
| 6383 | void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc, |
| 6384 | const Stmt *Body, |
| 6385 | unsigned DiagID) { |
| 6386 | // Since this is a syntactic check, don't emit diagnostic for template |
| 6387 | // instantiations, this just adds noise. |
| 6388 | if (CurrentInstantiationScope) |
| 6389 | return; |
| 6390 | |
| 6391 | // The body should be a null statement. |
| 6392 | const NullStmt *NBody = dyn_cast<NullStmt>(Body); |
| 6393 | if (!NBody) |
| 6394 | return; |
| 6395 | |
| 6396 | // Do the usual checks. |
| 6397 | if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) |
| 6398 | return; |
| 6399 | |
| 6400 | Diag(NBody->getSemiLoc(), DiagID); |
| 6401 | Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); |
| 6402 | } |
| 6403 | |
| 6404 | void Sema::DiagnoseEmptyLoopBody(const Stmt *S, |
| 6405 | const Stmt *PossibleBody) { |
| 6406 | assert(!CurrentInstantiationScope); // Ensured by caller |
| 6407 | |
| 6408 | SourceLocation StmtLoc; |
| 6409 | const Stmt *Body; |
| 6410 | unsigned DiagID; |
| 6411 | if (const ForStmt *FS = dyn_cast<ForStmt>(S)) { |
| 6412 | StmtLoc = FS->getRParenLoc(); |
| 6413 | Body = FS->getBody(); |
| 6414 | DiagID = diag::warn_empty_for_body; |
| 6415 | } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) { |
| 6416 | StmtLoc = WS->getCond()->getSourceRange().getEnd(); |
| 6417 | Body = WS->getBody(); |
| 6418 | DiagID = diag::warn_empty_while_body; |
| 6419 | } else |
| 6420 | return; // Neither `for' nor `while'. |
| 6421 | |
| 6422 | // The body should be a null statement. |
| 6423 | const NullStmt *NBody = dyn_cast<NullStmt>(Body); |
| 6424 | if (!NBody) |
| 6425 | return; |
| 6426 | |
| 6427 | // Skip expensive checks if diagnostic is disabled. |
| 6428 | if (Diags.getDiagnosticLevel(DiagID, NBody->getSemiLoc()) == |
| 6429 | DiagnosticsEngine::Ignored) |
| 6430 | return; |
| 6431 | |
| 6432 | // Do the usual checks. |
| 6433 | if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) |
| 6434 | return; |
| 6435 | |
| 6436 | // `for(...);' and `while(...);' are popular idioms, so in order to keep |
| 6437 | // noise level low, emit diagnostics only if for/while is followed by a |
| 6438 | // CompoundStmt, e.g.: |
| 6439 | // for (int i = 0; i < n; i++); |
| 6440 | // { |
| 6441 | // a(i); |
| 6442 | // } |
| 6443 | // or if for/while is followed by a statement with more indentation |
| 6444 | // than for/while itself: |
| 6445 | // for (int i = 0; i < n; i++); |
| 6446 | // a(i); |
| 6447 | bool ProbableTypo = isa<CompoundStmt>(PossibleBody); |
| 6448 | if (!ProbableTypo) { |
| 6449 | bool BodyColInvalid; |
| 6450 | unsigned BodyCol = SourceMgr.getPresumedColumnNumber( |
| 6451 | PossibleBody->getLocStart(), |
| 6452 | &BodyColInvalid); |
| 6453 | if (BodyColInvalid) |
| 6454 | return; |
| 6455 | |
| 6456 | bool StmtColInvalid; |
| 6457 | unsigned StmtCol = SourceMgr.getPresumedColumnNumber( |
| 6458 | S->getLocStart(), |
| 6459 | &StmtColInvalid); |
| 6460 | if (StmtColInvalid) |
| 6461 | return; |
| 6462 | |
| 6463 | if (BodyCol > StmtCol) |
| 6464 | ProbableTypo = true; |
| 6465 | } |
| 6466 | |
| 6467 | if (ProbableTypo) { |
| 6468 | Diag(NBody->getSemiLoc(), DiagID); |
| 6469 | Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); |
| 6470 | } |
| 6471 | } |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 6472 | |
| 6473 | //===--- Layout compatibility ----------------------------------------------// |
| 6474 | |
| 6475 | namespace { |
| 6476 | |
| 6477 | bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2); |
| 6478 | |
| 6479 | /// \brief Check if two enumeration types are layout-compatible. |
| 6480 | bool isLayoutCompatible(ASTContext &C, EnumDecl *ED1, EnumDecl *ED2) { |
| 6481 | // C++11 [dcl.enum] p8: |
| 6482 | // Two enumeration types are layout-compatible if they have the same |
| 6483 | // underlying type. |
| 6484 | return ED1->isComplete() && ED2->isComplete() && |
| 6485 | C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType()); |
| 6486 | } |
| 6487 | |
| 6488 | /// \brief Check if two fields are layout-compatible. |
| 6489 | bool isLayoutCompatible(ASTContext &C, FieldDecl *Field1, FieldDecl *Field2) { |
| 6490 | if (!isLayoutCompatible(C, Field1->getType(), Field2->getType())) |
| 6491 | return false; |
| 6492 | |
| 6493 | if (Field1->isBitField() != Field2->isBitField()) |
| 6494 | return false; |
| 6495 | |
| 6496 | if (Field1->isBitField()) { |
| 6497 | // Make sure that the bit-fields are the same length. |
| 6498 | unsigned Bits1 = Field1->getBitWidthValue(C); |
| 6499 | unsigned Bits2 = Field2->getBitWidthValue(C); |
| 6500 | |
| 6501 | if (Bits1 != Bits2) |
| 6502 | return false; |
| 6503 | } |
| 6504 | |
| 6505 | return true; |
| 6506 | } |
| 6507 | |
| 6508 | /// \brief Check if two standard-layout structs are layout-compatible. |
| 6509 | /// (C++11 [class.mem] p17) |
| 6510 | bool isLayoutCompatibleStruct(ASTContext &C, |
| 6511 | RecordDecl *RD1, |
| 6512 | RecordDecl *RD2) { |
| 6513 | // If both records are C++ classes, check that base classes match. |
| 6514 | if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1)) { |
| 6515 | // If one of records is a CXXRecordDecl we are in C++ mode, |
| 6516 | // thus the other one is a CXXRecordDecl, too. |
| 6517 | const CXXRecordDecl *D2CXX = cast<CXXRecordDecl>(RD2); |
| 6518 | // Check number of base classes. |
| 6519 | if (D1CXX->getNumBases() != D2CXX->getNumBases()) |
| 6520 | return false; |
| 6521 | |
| 6522 | // Check the base classes. |
| 6523 | for (CXXRecordDecl::base_class_const_iterator |
| 6524 | Base1 = D1CXX->bases_begin(), |
| 6525 | BaseEnd1 = D1CXX->bases_end(), |
| 6526 | Base2 = D2CXX->bases_begin(); |
| 6527 | Base1 != BaseEnd1; |
| 6528 | ++Base1, ++Base2) { |
| 6529 | if (!isLayoutCompatible(C, Base1->getType(), Base2->getType())) |
| 6530 | return false; |
| 6531 | } |
| 6532 | } else if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2)) { |
| 6533 | // If only RD2 is a C++ class, it should have zero base classes. |
| 6534 | if (D2CXX->getNumBases() > 0) |
| 6535 | return false; |
| 6536 | } |
| 6537 | |
| 6538 | // Check the fields. |
| 6539 | RecordDecl::field_iterator Field2 = RD2->field_begin(), |
| 6540 | Field2End = RD2->field_end(), |
| 6541 | Field1 = RD1->field_begin(), |
| 6542 | Field1End = RD1->field_end(); |
| 6543 | for ( ; Field1 != Field1End && Field2 != Field2End; ++Field1, ++Field2) { |
| 6544 | if (!isLayoutCompatible(C, *Field1, *Field2)) |
| 6545 | return false; |
| 6546 | } |
| 6547 | if (Field1 != Field1End || Field2 != Field2End) |
| 6548 | return false; |
| 6549 | |
| 6550 | return true; |
| 6551 | } |
| 6552 | |
| 6553 | /// \brief Check if two standard-layout unions are layout-compatible. |
| 6554 | /// (C++11 [class.mem] p18) |
| 6555 | bool isLayoutCompatibleUnion(ASTContext &C, |
| 6556 | RecordDecl *RD1, |
| 6557 | RecordDecl *RD2) { |
| 6558 | llvm::SmallPtrSet<FieldDecl *, 8> UnmatchedFields; |
| 6559 | for (RecordDecl::field_iterator Field2 = RD2->field_begin(), |
| 6560 | Field2End = RD2->field_end(); |
| 6561 | Field2 != Field2End; ++Field2) { |
| 6562 | UnmatchedFields.insert(*Field2); |
| 6563 | } |
| 6564 | |
| 6565 | for (RecordDecl::field_iterator Field1 = RD1->field_begin(), |
| 6566 | Field1End = RD1->field_end(); |
| 6567 | Field1 != Field1End; ++Field1) { |
| 6568 | llvm::SmallPtrSet<FieldDecl *, 8>::iterator |
| 6569 | I = UnmatchedFields.begin(), |
| 6570 | E = UnmatchedFields.end(); |
| 6571 | |
| 6572 | for ( ; I != E; ++I) { |
| 6573 | if (isLayoutCompatible(C, *Field1, *I)) { |
| 6574 | bool Result = UnmatchedFields.erase(*I); |
| 6575 | (void) Result; |
| 6576 | assert(Result); |
| 6577 | break; |
| 6578 | } |
| 6579 | } |
| 6580 | if (I == E) |
| 6581 | return false; |
| 6582 | } |
| 6583 | |
| 6584 | return UnmatchedFields.empty(); |
| 6585 | } |
| 6586 | |
| 6587 | bool isLayoutCompatible(ASTContext &C, RecordDecl *RD1, RecordDecl *RD2) { |
| 6588 | if (RD1->isUnion() != RD2->isUnion()) |
| 6589 | return false; |
| 6590 | |
| 6591 | if (RD1->isUnion()) |
| 6592 | return isLayoutCompatibleUnion(C, RD1, RD2); |
| 6593 | else |
| 6594 | return isLayoutCompatibleStruct(C, RD1, RD2); |
| 6595 | } |
| 6596 | |
| 6597 | /// \brief Check if two types are layout-compatible in C++11 sense. |
| 6598 | bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2) { |
| 6599 | if (T1.isNull() || T2.isNull()) |
| 6600 | return false; |
| 6601 | |
| 6602 | // C++11 [basic.types] p11: |
| 6603 | // If two types T1 and T2 are the same type, then T1 and T2 are |
| 6604 | // layout-compatible types. |
| 6605 | if (C.hasSameType(T1, T2)) |
| 6606 | return true; |
| 6607 | |
| 6608 | T1 = T1.getCanonicalType().getUnqualifiedType(); |
| 6609 | T2 = T2.getCanonicalType().getUnqualifiedType(); |
| 6610 | |
| 6611 | const Type::TypeClass TC1 = T1->getTypeClass(); |
| 6612 | const Type::TypeClass TC2 = T2->getTypeClass(); |
| 6613 | |
| 6614 | if (TC1 != TC2) |
| 6615 | return false; |
| 6616 | |
| 6617 | if (TC1 == Type::Enum) { |
| 6618 | return isLayoutCompatible(C, |
| 6619 | cast<EnumType>(T1)->getDecl(), |
| 6620 | cast<EnumType>(T2)->getDecl()); |
| 6621 | } else if (TC1 == Type::Record) { |
| 6622 | if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType()) |
| 6623 | return false; |
| 6624 | |
| 6625 | return isLayoutCompatible(C, |
| 6626 | cast<RecordType>(T1)->getDecl(), |
| 6627 | cast<RecordType>(T2)->getDecl()); |
| 6628 | } |
| 6629 | |
| 6630 | return false; |
| 6631 | } |
| 6632 | } |
| 6633 | |
| 6634 | //===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----// |
| 6635 | |
| 6636 | namespace { |
| 6637 | /// \brief Given a type tag expression find the type tag itself. |
| 6638 | /// |
| 6639 | /// \param TypeExpr Type tag expression, as it appears in user's code. |
| 6640 | /// |
| 6641 | /// \param VD Declaration of an identifier that appears in a type tag. |
| 6642 | /// |
| 6643 | /// \param MagicValue Type tag magic value. |
| 6644 | bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx, |
| 6645 | const ValueDecl **VD, uint64_t *MagicValue) { |
| 6646 | while(true) { |
| 6647 | if (!TypeExpr) |
| 6648 | return false; |
| 6649 | |
| 6650 | TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts(); |
| 6651 | |
| 6652 | switch (TypeExpr->getStmtClass()) { |
| 6653 | case Stmt::UnaryOperatorClass: { |
| 6654 | const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr); |
| 6655 | if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) { |
| 6656 | TypeExpr = UO->getSubExpr(); |
| 6657 | continue; |
| 6658 | } |
| 6659 | return false; |
| 6660 | } |
| 6661 | |
| 6662 | case Stmt::DeclRefExprClass: { |
| 6663 | const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr); |
| 6664 | *VD = DRE->getDecl(); |
| 6665 | return true; |
| 6666 | } |
| 6667 | |
| 6668 | case Stmt::IntegerLiteralClass: { |
| 6669 | const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr); |
| 6670 | llvm::APInt MagicValueAPInt = IL->getValue(); |
| 6671 | if (MagicValueAPInt.getActiveBits() <= 64) { |
| 6672 | *MagicValue = MagicValueAPInt.getZExtValue(); |
| 6673 | return true; |
| 6674 | } else |
| 6675 | return false; |
| 6676 | } |
| 6677 | |
| 6678 | case Stmt::BinaryConditionalOperatorClass: |
| 6679 | case Stmt::ConditionalOperatorClass: { |
| 6680 | const AbstractConditionalOperator *ACO = |
| 6681 | cast<AbstractConditionalOperator>(TypeExpr); |
| 6682 | bool Result; |
| 6683 | if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx)) { |
| 6684 | if (Result) |
| 6685 | TypeExpr = ACO->getTrueExpr(); |
| 6686 | else |
| 6687 | TypeExpr = ACO->getFalseExpr(); |
| 6688 | continue; |
| 6689 | } |
| 6690 | return false; |
| 6691 | } |
| 6692 | |
| 6693 | case Stmt::BinaryOperatorClass: { |
| 6694 | const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr); |
| 6695 | if (BO->getOpcode() == BO_Comma) { |
| 6696 | TypeExpr = BO->getRHS(); |
| 6697 | continue; |
| 6698 | } |
| 6699 | return false; |
| 6700 | } |
| 6701 | |
| 6702 | default: |
| 6703 | return false; |
| 6704 | } |
| 6705 | } |
| 6706 | } |
| 6707 | |
| 6708 | /// \brief Retrieve the C type corresponding to type tag TypeExpr. |
| 6709 | /// |
| 6710 | /// \param TypeExpr Expression that specifies a type tag. |
| 6711 | /// |
| 6712 | /// \param MagicValues Registered magic values. |
| 6713 | /// |
| 6714 | /// \param FoundWrongKind Set to true if a type tag was found, but of a wrong |
| 6715 | /// kind. |
| 6716 | /// |
| 6717 | /// \param TypeInfo Information about the corresponding C type. |
| 6718 | /// |
| 6719 | /// \returns true if the corresponding C type was found. |
| 6720 | bool GetMatchingCType( |
| 6721 | const IdentifierInfo *ArgumentKind, |
| 6722 | const Expr *TypeExpr, const ASTContext &Ctx, |
| 6723 | const llvm::DenseMap<Sema::TypeTagMagicValue, |
| 6724 | Sema::TypeTagData> *MagicValues, |
| 6725 | bool &FoundWrongKind, |
| 6726 | Sema::TypeTagData &TypeInfo) { |
| 6727 | FoundWrongKind = false; |
| 6728 | |
| 6729 | // Variable declaration that has type_tag_for_datatype attribute. |
| 6730 | const ValueDecl *VD = NULL; |
| 6731 | |
| 6732 | uint64_t MagicValue; |
| 6733 | |
| 6734 | if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue)) |
| 6735 | return false; |
| 6736 | |
| 6737 | if (VD) { |
| 6738 | for (specific_attr_iterator<TypeTagForDatatypeAttr> |
| 6739 | I = VD->specific_attr_begin<TypeTagForDatatypeAttr>(), |
| 6740 | E = VD->specific_attr_end<TypeTagForDatatypeAttr>(); |
| 6741 | I != E; ++I) { |
| 6742 | if (I->getArgumentKind() != ArgumentKind) { |
| 6743 | FoundWrongKind = true; |
| 6744 | return false; |
| 6745 | } |
| 6746 | TypeInfo.Type = I->getMatchingCType(); |
| 6747 | TypeInfo.LayoutCompatible = I->getLayoutCompatible(); |
| 6748 | TypeInfo.MustBeNull = I->getMustBeNull(); |
| 6749 | return true; |
| 6750 | } |
| 6751 | return false; |
| 6752 | } |
| 6753 | |
| 6754 | if (!MagicValues) |
| 6755 | return false; |
| 6756 | |
| 6757 | llvm::DenseMap<Sema::TypeTagMagicValue, |
| 6758 | Sema::TypeTagData>::const_iterator I = |
| 6759 | MagicValues->find(std::make_pair(ArgumentKind, MagicValue)); |
| 6760 | if (I == MagicValues->end()) |
| 6761 | return false; |
| 6762 | |
| 6763 | TypeInfo = I->second; |
| 6764 | return true; |
| 6765 | } |
| 6766 | } // unnamed namespace |
| 6767 | |
| 6768 | void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind, |
| 6769 | uint64_t MagicValue, QualType Type, |
| 6770 | bool LayoutCompatible, |
| 6771 | bool MustBeNull) { |
| 6772 | if (!TypeTagForDatatypeMagicValues) |
| 6773 | TypeTagForDatatypeMagicValues.reset( |
| 6774 | new llvm::DenseMap<TypeTagMagicValue, TypeTagData>); |
| 6775 | |
| 6776 | TypeTagMagicValue Magic(ArgumentKind, MagicValue); |
| 6777 | (*TypeTagForDatatypeMagicValues)[Magic] = |
| 6778 | TypeTagData(Type, LayoutCompatible, MustBeNull); |
| 6779 | } |
| 6780 | |
| 6781 | namespace { |
| 6782 | bool IsSameCharType(QualType T1, QualType T2) { |
| 6783 | const BuiltinType *BT1 = T1->getAs<BuiltinType>(); |
| 6784 | if (!BT1) |
| 6785 | return false; |
| 6786 | |
| 6787 | const BuiltinType *BT2 = T2->getAs<BuiltinType>(); |
| 6788 | if (!BT2) |
| 6789 | return false; |
| 6790 | |
| 6791 | BuiltinType::Kind T1Kind = BT1->getKind(); |
| 6792 | BuiltinType::Kind T2Kind = BT2->getKind(); |
| 6793 | |
| 6794 | return (T1Kind == BuiltinType::SChar && T2Kind == BuiltinType::Char_S) || |
| 6795 | (T1Kind == BuiltinType::UChar && T2Kind == BuiltinType::Char_U) || |
| 6796 | (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) || |
| 6797 | (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar); |
| 6798 | } |
| 6799 | } // unnamed namespace |
| 6800 | |
| 6801 | void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr, |
| 6802 | const Expr * const *ExprArgs) { |
| 6803 | const IdentifierInfo *ArgumentKind = Attr->getArgumentKind(); |
| 6804 | bool IsPointerAttr = Attr->getIsPointer(); |
| 6805 | |
| 6806 | const Expr *TypeTagExpr = ExprArgs[Attr->getTypeTagIdx()]; |
| 6807 | bool FoundWrongKind; |
| 6808 | TypeTagData TypeInfo; |
| 6809 | if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context, |
| 6810 | TypeTagForDatatypeMagicValues.get(), |
| 6811 | FoundWrongKind, TypeInfo)) { |
| 6812 | if (FoundWrongKind) |
| 6813 | Diag(TypeTagExpr->getExprLoc(), |
| 6814 | diag::warn_type_tag_for_datatype_wrong_kind) |
| 6815 | << TypeTagExpr->getSourceRange(); |
| 6816 | return; |
| 6817 | } |
| 6818 | |
| 6819 | const Expr *ArgumentExpr = ExprArgs[Attr->getArgumentIdx()]; |
| 6820 | if (IsPointerAttr) { |
| 6821 | // Skip implicit cast of pointer to `void *' (as a function argument). |
| 6822 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr)) |
Dmitri Gribenko | 5a24980 | 2012-11-03 16:07:49 +0000 | [diff] [blame] | 6823 | if (ICE->getType()->isVoidPointerType() && |
Dmitri Gribenko | b57ce4e | 2012-11-03 22:10:18 +0000 | [diff] [blame] | 6824 | ICE->getCastKind() == CK_BitCast) |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 6825 | ArgumentExpr = ICE->getSubExpr(); |
| 6826 | } |
| 6827 | QualType ArgumentType = ArgumentExpr->getType(); |
| 6828 | |
| 6829 | // Passing a `void*' pointer shouldn't trigger a warning. |
| 6830 | if (IsPointerAttr && ArgumentType->isVoidPointerType()) |
| 6831 | return; |
| 6832 | |
| 6833 | if (TypeInfo.MustBeNull) { |
| 6834 | // Type tag with matching void type requires a null pointer. |
| 6835 | if (!ArgumentExpr->isNullPointerConstant(Context, |
| 6836 | Expr::NPC_ValueDependentIsNotNull)) { |
| 6837 | Diag(ArgumentExpr->getExprLoc(), |
| 6838 | diag::warn_type_safety_null_pointer_required) |
| 6839 | << ArgumentKind->getName() |
| 6840 | << ArgumentExpr->getSourceRange() |
| 6841 | << TypeTagExpr->getSourceRange(); |
| 6842 | } |
| 6843 | return; |
| 6844 | } |
| 6845 | |
| 6846 | QualType RequiredType = TypeInfo.Type; |
| 6847 | if (IsPointerAttr) |
| 6848 | RequiredType = Context.getPointerType(RequiredType); |
| 6849 | |
| 6850 | bool mismatch = false; |
| 6851 | if (!TypeInfo.LayoutCompatible) { |
| 6852 | mismatch = !Context.hasSameType(ArgumentType, RequiredType); |
| 6853 | |
| 6854 | // C++11 [basic.fundamental] p1: |
| 6855 | // Plain char, signed char, and unsigned char are three distinct types. |
| 6856 | // |
| 6857 | // But we treat plain `char' as equivalent to `signed char' or `unsigned |
| 6858 | // char' depending on the current char signedness mode. |
| 6859 | if (mismatch) |
| 6860 | if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(), |
| 6861 | RequiredType->getPointeeType())) || |
| 6862 | (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType))) |
| 6863 | mismatch = false; |
| 6864 | } else |
| 6865 | if (IsPointerAttr) |
| 6866 | mismatch = !isLayoutCompatible(Context, |
| 6867 | ArgumentType->getPointeeType(), |
| 6868 | RequiredType->getPointeeType()); |
| 6869 | else |
| 6870 | mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType); |
| 6871 | |
| 6872 | if (mismatch) |
| 6873 | Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch) |
| 6874 | << ArgumentType << ArgumentKind->getName() |
| 6875 | << TypeInfo.LayoutCompatible << RequiredType |
| 6876 | << ArgumentExpr->getSourceRange() |
| 6877 | << TypeTagExpr->getSourceRange(); |
| 6878 | } |