Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1 | //===--- SemaChecking.cpp - Extra Semantic Checking -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10 | // This file implements extra semantic analysis beyond what is enforced |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 11 | // by the C type system. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Initialization.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Sema.h" |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaInternal.h" |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Initialization.h" |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 19 | #include "clang/Sema/Lookup.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 20 | #include "clang/Sema/ScopeInfo.h" |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/Analyses/FormatString.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 23 | #include "clang/AST/CharUnits.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 25 | #include "clang/AST/DeclObjC.h" |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 26 | #include "clang/AST/Expr.h" |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 27 | #include "clang/AST/ExprCXX.h" |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 28 | #include "clang/AST/ExprObjC.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 29 | #include "clang/AST/EvaluatedExprVisitor.h" |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 30 | #include "clang/AST/DeclObjC.h" |
| 31 | #include "clang/AST/StmtCXX.h" |
| 32 | #include "clang/AST/StmtObjC.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 33 | #include "clang/Lex/Preprocessor.h" |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/BitVector.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallString.h" |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/STLExtras.h" |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 38 | #include "clang/Basic/TargetBuiltins.h" |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 39 | #include "clang/Basic/TargetInfo.h" |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 40 | #include "clang/Basic/ConvertUTF.h" |
Zhongxing Xu | a1f3dba | 2009-05-20 01:55:10 +0000 | [diff] [blame] | 41 | #include <limits> |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 42 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 43 | using namespace sema; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 45 | SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, |
| 46 | unsigned ByteNo) const { |
Chris Lattner | 08f92e3 | 2010-11-17 07:37:15 +0000 | [diff] [blame] | 47 | return SL->getLocationOfByte(ByteNo, PP.getSourceManager(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 48 | PP.getLangOpts(), PP.getTargetInfo()); |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 49 | } |
| 50 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 51 | /// Checks that a call expression's argument count is the desired number. |
| 52 | /// This is useful when doing custom type-checking. Returns true on error. |
| 53 | static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) { |
| 54 | unsigned argCount = call->getNumArgs(); |
| 55 | if (argCount == desiredArgCount) return false; |
| 56 | |
| 57 | if (argCount < desiredArgCount) |
| 58 | return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 59 | << 0 /*function call*/ << desiredArgCount << argCount |
| 60 | << call->getSourceRange(); |
| 61 | |
| 62 | // Highlight all the excess arguments. |
| 63 | SourceRange range(call->getArg(desiredArgCount)->getLocStart(), |
| 64 | call->getArg(argCount - 1)->getLocEnd()); |
| 65 | |
| 66 | return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args) |
| 67 | << 0 /*function call*/ << desiredArgCount << argCount |
| 68 | << call->getArg(1)->getSourceRange(); |
| 69 | } |
| 70 | |
Julien Lerouge | e593921 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 71 | /// Check that the first argument to __builtin_annotation is an integer |
| 72 | /// and the second argument is a non-wide string literal. |
| 73 | static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) { |
| 74 | if (checkArgCount(S, TheCall, 2)) |
| 75 | return true; |
| 76 | |
| 77 | // First argument should be an integer. |
| 78 | Expr *ValArg = TheCall->getArg(0); |
| 79 | QualType Ty = ValArg->getType(); |
| 80 | if (!Ty->isIntegerType()) { |
| 81 | S.Diag(ValArg->getLocStart(), diag::err_builtin_annotation_first_arg) |
| 82 | << ValArg->getSourceRange(); |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 83 | return true; |
| 84 | } |
Julien Lerouge | e593921 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 85 | |
| 86 | // Second argument should be a constant string. |
| 87 | Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts(); |
| 88 | StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg); |
| 89 | if (!Literal || !Literal->isAscii()) { |
| 90 | S.Diag(StrArg->getLocStart(), diag::err_builtin_annotation_second_arg) |
| 91 | << StrArg->getSourceRange(); |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | TheCall->setType(Ty); |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 99 | ExprResult |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 100 | Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 101 | ExprResult TheCallResult(Owned(TheCall)); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 946928f | 2010-10-01 23:23:24 +0000 | [diff] [blame] | 103 | // Find out if any arguments are required to be integer constant expressions. |
| 104 | unsigned ICEArguments = 0; |
| 105 | ASTContext::GetBuiltinTypeError Error; |
| 106 | Context.GetBuiltinType(BuiltinID, Error, &ICEArguments); |
| 107 | if (Error != ASTContext::GE_None) |
| 108 | ICEArguments = 0; // Don't diagnose previously diagnosed errors. |
| 109 | |
| 110 | // If any arguments are required to be ICE's, check and diagnose. |
| 111 | for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) { |
| 112 | // Skip arguments not required to be ICE's. |
| 113 | if ((ICEArguments & (1 << ArgNo)) == 0) continue; |
| 114 | |
| 115 | llvm::APSInt Result; |
| 116 | if (SemaBuiltinConstantArg(TheCall, ArgNo, Result)) |
| 117 | return true; |
| 118 | ICEArguments &= ~(1 << ArgNo); |
| 119 | } |
| 120 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 121 | switch (BuiltinID) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 122 | case Builtin::BI__builtin___CFStringMakeConstantString: |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 123 | assert(TheCall->getNumArgs() == 1 && |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 124 | "Wrong # arguments to builtin CFStringMakeConstantString"); |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 125 | if (CheckObjCString(TheCall->getArg(0))) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 126 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 127 | break; |
Ted Kremenek | 49ff7a1 | 2008-07-09 17:58:53 +0000 | [diff] [blame] | 128 | case Builtin::BI__builtin_stdarg_start: |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 129 | case Builtin::BI__builtin_va_start: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 130 | if (SemaBuiltinVAStart(TheCall)) |
| 131 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 132 | break; |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 133 | case Builtin::BI__builtin_isgreater: |
| 134 | case Builtin::BI__builtin_isgreaterequal: |
| 135 | case Builtin::BI__builtin_isless: |
| 136 | case Builtin::BI__builtin_islessequal: |
| 137 | case Builtin::BI__builtin_islessgreater: |
| 138 | case Builtin::BI__builtin_isunordered: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 139 | if (SemaBuiltinUnorderedCompare(TheCall)) |
| 140 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 141 | break; |
Benjamin Kramer | e771a7a | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 142 | case Builtin::BI__builtin_fpclassify: |
| 143 | if (SemaBuiltinFPClassification(TheCall, 6)) |
| 144 | return ExprError(); |
| 145 | break; |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 146 | case Builtin::BI__builtin_isfinite: |
| 147 | case Builtin::BI__builtin_isinf: |
| 148 | case Builtin::BI__builtin_isinf_sign: |
| 149 | case Builtin::BI__builtin_isnan: |
| 150 | case Builtin::BI__builtin_isnormal: |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 151 | if (SemaBuiltinFPClassification(TheCall, 1)) |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 152 | return ExprError(); |
| 153 | break; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 154 | case Builtin::BI__builtin_shufflevector: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 155 | return SemaBuiltinShuffleVector(TheCall); |
| 156 | // TheCall will be freed by the smart pointer here, but that's fine, since |
| 157 | // SemaBuiltinShuffleVector guts it, but then doesn't release it. |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 158 | case Builtin::BI__builtin_prefetch: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 159 | if (SemaBuiltinPrefetch(TheCall)) |
| 160 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 161 | break; |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 162 | case Builtin::BI__builtin_object_size: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 163 | if (SemaBuiltinObjectSize(TheCall)) |
| 164 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 165 | break; |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 166 | case Builtin::BI__builtin_longjmp: |
| 167 | if (SemaBuiltinLongjmp(TheCall)) |
| 168 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 169 | break; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 170 | |
| 171 | case Builtin::BI__builtin_classify_type: |
| 172 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 173 | TheCall->setType(Context.IntTy); |
| 174 | break; |
Chris Lattner | 75c29a0 | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 175 | case Builtin::BI__builtin_constant_p: |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 176 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 177 | TheCall->setType(Context.IntTy); |
Chris Lattner | 75c29a0 | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 178 | break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 179 | case Builtin::BI__sync_fetch_and_add: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 180 | case Builtin::BI__sync_fetch_and_add_1: |
| 181 | case Builtin::BI__sync_fetch_and_add_2: |
| 182 | case Builtin::BI__sync_fetch_and_add_4: |
| 183 | case Builtin::BI__sync_fetch_and_add_8: |
| 184 | case Builtin::BI__sync_fetch_and_add_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 185 | case Builtin::BI__sync_fetch_and_sub: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 186 | case Builtin::BI__sync_fetch_and_sub_1: |
| 187 | case Builtin::BI__sync_fetch_and_sub_2: |
| 188 | case Builtin::BI__sync_fetch_and_sub_4: |
| 189 | case Builtin::BI__sync_fetch_and_sub_8: |
| 190 | case Builtin::BI__sync_fetch_and_sub_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 191 | case Builtin::BI__sync_fetch_and_or: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 192 | case Builtin::BI__sync_fetch_and_or_1: |
| 193 | case Builtin::BI__sync_fetch_and_or_2: |
| 194 | case Builtin::BI__sync_fetch_and_or_4: |
| 195 | case Builtin::BI__sync_fetch_and_or_8: |
| 196 | case Builtin::BI__sync_fetch_and_or_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 197 | case Builtin::BI__sync_fetch_and_and: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 198 | case Builtin::BI__sync_fetch_and_and_1: |
| 199 | case Builtin::BI__sync_fetch_and_and_2: |
| 200 | case Builtin::BI__sync_fetch_and_and_4: |
| 201 | case Builtin::BI__sync_fetch_and_and_8: |
| 202 | case Builtin::BI__sync_fetch_and_and_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 203 | case Builtin::BI__sync_fetch_and_xor: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 204 | case Builtin::BI__sync_fetch_and_xor_1: |
| 205 | case Builtin::BI__sync_fetch_and_xor_2: |
| 206 | case Builtin::BI__sync_fetch_and_xor_4: |
| 207 | case Builtin::BI__sync_fetch_and_xor_8: |
| 208 | case Builtin::BI__sync_fetch_and_xor_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 209 | case Builtin::BI__sync_add_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 210 | case Builtin::BI__sync_add_and_fetch_1: |
| 211 | case Builtin::BI__sync_add_and_fetch_2: |
| 212 | case Builtin::BI__sync_add_and_fetch_4: |
| 213 | case Builtin::BI__sync_add_and_fetch_8: |
| 214 | case Builtin::BI__sync_add_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 215 | case Builtin::BI__sync_sub_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 216 | case Builtin::BI__sync_sub_and_fetch_1: |
| 217 | case Builtin::BI__sync_sub_and_fetch_2: |
| 218 | case Builtin::BI__sync_sub_and_fetch_4: |
| 219 | case Builtin::BI__sync_sub_and_fetch_8: |
| 220 | case Builtin::BI__sync_sub_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 221 | case Builtin::BI__sync_and_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 222 | case Builtin::BI__sync_and_and_fetch_1: |
| 223 | case Builtin::BI__sync_and_and_fetch_2: |
| 224 | case Builtin::BI__sync_and_and_fetch_4: |
| 225 | case Builtin::BI__sync_and_and_fetch_8: |
| 226 | case Builtin::BI__sync_and_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 227 | case Builtin::BI__sync_or_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 228 | case Builtin::BI__sync_or_and_fetch_1: |
| 229 | case Builtin::BI__sync_or_and_fetch_2: |
| 230 | case Builtin::BI__sync_or_and_fetch_4: |
| 231 | case Builtin::BI__sync_or_and_fetch_8: |
| 232 | case Builtin::BI__sync_or_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 233 | case Builtin::BI__sync_xor_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 234 | case Builtin::BI__sync_xor_and_fetch_1: |
| 235 | case Builtin::BI__sync_xor_and_fetch_2: |
| 236 | case Builtin::BI__sync_xor_and_fetch_4: |
| 237 | case Builtin::BI__sync_xor_and_fetch_8: |
| 238 | case Builtin::BI__sync_xor_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 239 | case Builtin::BI__sync_val_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 240 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 241 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 242 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 243 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 244 | case Builtin::BI__sync_val_compare_and_swap_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 245 | case Builtin::BI__sync_bool_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 246 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 247 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 248 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 249 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 250 | case Builtin::BI__sync_bool_compare_and_swap_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 251 | case Builtin::BI__sync_lock_test_and_set: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 252 | case Builtin::BI__sync_lock_test_and_set_1: |
| 253 | case Builtin::BI__sync_lock_test_and_set_2: |
| 254 | case Builtin::BI__sync_lock_test_and_set_4: |
| 255 | case Builtin::BI__sync_lock_test_and_set_8: |
| 256 | case Builtin::BI__sync_lock_test_and_set_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 257 | case Builtin::BI__sync_lock_release: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 258 | case Builtin::BI__sync_lock_release_1: |
| 259 | case Builtin::BI__sync_lock_release_2: |
| 260 | case Builtin::BI__sync_lock_release_4: |
| 261 | case Builtin::BI__sync_lock_release_8: |
| 262 | case Builtin::BI__sync_lock_release_16: |
Chris Lattner | 23aa9c8 | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 263 | case Builtin::BI__sync_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 264 | case Builtin::BI__sync_swap_1: |
| 265 | case Builtin::BI__sync_swap_2: |
| 266 | case Builtin::BI__sync_swap_4: |
| 267 | case Builtin::BI__sync_swap_8: |
| 268 | case Builtin::BI__sync_swap_16: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame^] | 269 | return SemaBuiltinAtomicOverloaded(TheCallResult); |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 270 | #define BUILTIN(ID, TYPE, ATTRS) |
| 271 | #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ |
| 272 | case Builtin::BI##ID: \ |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame^] | 273 | return SemaAtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID); |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 274 | #include "clang/Basic/Builtins.def" |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 275 | case Builtin::BI__builtin_annotation: |
Julien Lerouge | e593921 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 276 | if (SemaBuiltinAnnotation(*this, TheCall)) |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 277 | return ExprError(); |
| 278 | break; |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | // Since the target specific builtins for each arch overlap, only check those |
| 282 | // of the arch we are compiling for. |
| 283 | if (BuiltinID >= Builtin::FirstTSBuiltin) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 284 | switch (Context.getTargetInfo().getTriple().getArch()) { |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 285 | case llvm::Triple::arm: |
| 286 | case llvm::Triple::thumb: |
| 287 | if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall)) |
| 288 | return ExprError(); |
| 289 | break; |
Simon Atanasyan | fad0a32 | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 290 | case llvm::Triple::mips: |
| 291 | case llvm::Triple::mipsel: |
| 292 | case llvm::Triple::mips64: |
| 293 | case llvm::Triple::mips64el: |
| 294 | if (CheckMipsBuiltinFunctionCall(BuiltinID, TheCall)) |
| 295 | return ExprError(); |
| 296 | break; |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 297 | default: |
| 298 | break; |
| 299 | } |
| 300 | } |
| 301 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame^] | 302 | return TheCallResult; |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 305 | // Get the valid immediate range for the specified NEON type code. |
| 306 | static unsigned RFT(unsigned t, bool shift = false) { |
Bob Wilson | da95f73 | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 307 | NeonTypeFlags Type(t); |
| 308 | int IsQuad = Type.isQuad(); |
| 309 | switch (Type.getEltType()) { |
| 310 | case NeonTypeFlags::Int8: |
| 311 | case NeonTypeFlags::Poly8: |
| 312 | return shift ? 7 : (8 << IsQuad) - 1; |
| 313 | case NeonTypeFlags::Int16: |
| 314 | case NeonTypeFlags::Poly16: |
| 315 | return shift ? 15 : (4 << IsQuad) - 1; |
| 316 | case NeonTypeFlags::Int32: |
| 317 | return shift ? 31 : (2 << IsQuad) - 1; |
| 318 | case NeonTypeFlags::Int64: |
| 319 | return shift ? 63 : (1 << IsQuad) - 1; |
| 320 | case NeonTypeFlags::Float16: |
| 321 | assert(!shift && "cannot shift float types!"); |
| 322 | return (4 << IsQuad) - 1; |
| 323 | case NeonTypeFlags::Float32: |
| 324 | assert(!shift && "cannot shift float types!"); |
| 325 | return (2 << IsQuad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 326 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 327 | llvm_unreachable("Invalid NeonTypeFlag!"); |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 330 | /// getNeonEltType - Return the QualType corresponding to the elements of |
| 331 | /// the vector type specified by the NeonTypeFlags. This is used to check |
| 332 | /// the pointer arguments for Neon load/store intrinsics. |
| 333 | static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context) { |
| 334 | switch (Flags.getEltType()) { |
| 335 | case NeonTypeFlags::Int8: |
| 336 | return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy; |
| 337 | case NeonTypeFlags::Int16: |
| 338 | return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy; |
| 339 | case NeonTypeFlags::Int32: |
| 340 | return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy; |
| 341 | case NeonTypeFlags::Int64: |
| 342 | return Flags.isUnsigned() ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 343 | case NeonTypeFlags::Poly8: |
| 344 | return Context.SignedCharTy; |
| 345 | case NeonTypeFlags::Poly16: |
| 346 | return Context.ShortTy; |
| 347 | case NeonTypeFlags::Float16: |
| 348 | return Context.UnsignedShortTy; |
| 349 | case NeonTypeFlags::Float32: |
| 350 | return Context.FloatTy; |
| 351 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 352 | llvm_unreachable("Invalid NeonTypeFlag!"); |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 355 | bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 356 | llvm::APSInt Result; |
| 357 | |
Richard Smith | f8ee6bc | 2012-08-14 01:28:02 +0000 | [diff] [blame] | 358 | uint64_t mask = 0; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 359 | unsigned TV = 0; |
Bob Wilson | 4648255 | 2011-11-16 21:32:23 +0000 | [diff] [blame] | 360 | int PtrArgNum = -1; |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 361 | bool HasConstPtr = false; |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 362 | switch (BuiltinID) { |
Nate Begeman | a23326b | 2010-06-17 04:17:01 +0000 | [diff] [blame] | 363 | #define GET_NEON_OVERLOAD_CHECK |
| 364 | #include "clang/Basic/arm_neon.inc" |
| 365 | #undef GET_NEON_OVERLOAD_CHECK |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 368 | // For NEON intrinsics which are overloaded on vector element type, validate |
| 369 | // the immediate which specifies which variant to emit. |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 370 | unsigned ImmArg = TheCall->getNumArgs()-1; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 371 | if (mask) { |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 372 | if (SemaBuiltinConstantArg(TheCall, ImmArg, Result)) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 373 | return true; |
| 374 | |
Bob Wilson | da95f73 | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 375 | TV = Result.getLimitedValue(64); |
Richard Smith | f8ee6bc | 2012-08-14 01:28:02 +0000 | [diff] [blame] | 376 | if ((TV > 63) || (mask & (1ULL << TV)) == 0) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 377 | return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code) |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 378 | << TheCall->getArg(ImmArg)->getSourceRange(); |
| 379 | } |
| 380 | |
Bob Wilson | 4648255 | 2011-11-16 21:32:23 +0000 | [diff] [blame] | 381 | if (PtrArgNum >= 0) { |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 382 | // Check that pointer arguments have the specified type. |
Bob Wilson | 4648255 | 2011-11-16 21:32:23 +0000 | [diff] [blame] | 383 | Expr *Arg = TheCall->getArg(PtrArgNum); |
| 384 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) |
| 385 | Arg = ICE->getSubExpr(); |
| 386 | ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg); |
| 387 | QualType RHSTy = RHS.get()->getType(); |
| 388 | QualType EltTy = getNeonEltType(NeonTypeFlags(TV), Context); |
| 389 | if (HasConstPtr) |
| 390 | EltTy = EltTy.withConst(); |
| 391 | QualType LHSTy = Context.getPointerType(EltTy); |
| 392 | AssignConvertType ConvTy; |
| 393 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
| 394 | if (RHS.isInvalid()) |
| 395 | return true; |
| 396 | if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), LHSTy, RHSTy, |
| 397 | RHS.get(), AA_Assigning)) |
| 398 | return true; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 399 | } |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 400 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 401 | // For NEON intrinsics which take an immediate value as part of the |
| 402 | // instruction, range check them here. |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 403 | unsigned i = 0, l = 0, u = 0; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 404 | switch (BuiltinID) { |
| 405 | default: return false; |
Nate Begeman | bb37f50 | 2010-07-29 22:48:34 +0000 | [diff] [blame] | 406 | case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break; |
| 407 | case ARM::BI__builtin_arm_usat: i = 1; u = 31; break; |
Nate Begeman | 99c40bb | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 408 | case ARM::BI__builtin_arm_vcvtr_f: |
| 409 | case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break; |
Nate Begeman | a23326b | 2010-06-17 04:17:01 +0000 | [diff] [blame] | 410 | #define GET_NEON_IMMEDIATE_CHECK |
| 411 | #include "clang/Basic/arm_neon.inc" |
| 412 | #undef GET_NEON_IMMEDIATE_CHECK |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 413 | }; |
| 414 | |
Douglas Gregor | 592a423 | 2012-06-29 01:05:22 +0000 | [diff] [blame] | 415 | // We can't check the value of a dependent argument. |
| 416 | if (TheCall->getArg(i)->isTypeDependent() || |
| 417 | TheCall->getArg(i)->isValueDependent()) |
| 418 | return false; |
| 419 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 420 | // Check that the immediate argument is actually a constant. |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 421 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 422 | return true; |
| 423 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 424 | // Range check against the upper/lower values for this isntruction. |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 425 | unsigned Val = Result.getZExtValue(); |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 426 | if (Val < l || Val > (u + l)) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 427 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 428 | << l << u+l << TheCall->getArg(i)->getSourceRange(); |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 429 | |
Nate Begeman | 99c40bb | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 430 | // FIXME: VFP Intrinsics should error if VFP not present. |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 431 | return false; |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 432 | } |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 433 | |
Simon Atanasyan | fad0a32 | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 434 | bool Sema::CheckMipsBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
| 435 | unsigned i = 0, l = 0, u = 0; |
| 436 | switch (BuiltinID) { |
| 437 | default: return false; |
| 438 | case Mips::BI__builtin_mips_wrdsp: i = 1; l = 0; u = 63; break; |
| 439 | case Mips::BI__builtin_mips_rddsp: i = 0; l = 0; u = 63; break; |
| 440 | }; |
| 441 | |
| 442 | // We can't check the value of a dependent argument. |
| 443 | if (TheCall->getArg(i)->isTypeDependent() || |
| 444 | TheCall->getArg(i)->isValueDependent()) |
| 445 | return false; |
| 446 | |
| 447 | // Check that the immediate argument is actually a constant. |
| 448 | llvm::APSInt Result; |
| 449 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 450 | return true; |
| 451 | |
| 452 | // Range check against the upper/lower values for this instruction. |
| 453 | unsigned Val = Result.getZExtValue(); |
| 454 | if (Val < l || Val > u) |
| 455 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 456 | << l << u << TheCall->getArg(i)->getSourceRange(); |
| 457 | |
| 458 | return false; |
| 459 | } |
| 460 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 461 | /// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo |
| 462 | /// parameter with the FormatAttr's correct format_idx and firstDataArg. |
| 463 | /// Returns true when the format fits the function and the FormatStringInfo has |
| 464 | /// been populated. |
| 465 | bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember, |
| 466 | FormatStringInfo *FSI) { |
| 467 | FSI->HasVAListArg = Format->getFirstArg() == 0; |
| 468 | FSI->FormatIdx = Format->getFormatIdx() - 1; |
| 469 | FSI->FirstDataArg = FSI->HasVAListArg ? 0 : Format->getFirstArg() - 1; |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 470 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 471 | // The way the format attribute works in GCC, the implicit this argument |
| 472 | // of member functions is counted. However, it doesn't appear in our own |
| 473 | // lists, so decrement format_idx in that case. |
| 474 | if (IsCXXMember) { |
| 475 | if(FSI->FormatIdx == 0) |
| 476 | return false; |
| 477 | --FSI->FormatIdx; |
| 478 | if (FSI->FirstDataArg != 0) |
| 479 | --FSI->FirstDataArg; |
| 480 | } |
| 481 | return true; |
| 482 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 484 | /// Handles the checks for format strings, non-POD arguments to vararg |
| 485 | /// functions, and NULL arguments passed to non-NULL parameters. |
| 486 | void Sema::checkCall(NamedDecl *FDecl, Expr **Args, |
| 487 | unsigned NumArgs, |
| 488 | unsigned NumProtoArgs, |
| 489 | bool IsMemberFunction, |
| 490 | SourceLocation Loc, |
| 491 | SourceRange Range, |
| 492 | VariadicCallType CallType) { |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 493 | // FIXME: This mechanism should be abstracted to be less fragile and |
| 494 | // more efficient. For example, just map function ids to custom |
| 495 | // handlers. |
| 496 | |
Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 497 | // Printf and scanf checking. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 498 | bool HandledFormatString = false; |
Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 499 | for (specific_attr_iterator<FormatAttr> |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 500 | I = FDecl->specific_attr_begin<FormatAttr>(), |
| 501 | E = FDecl->specific_attr_end<FormatAttr>(); I != E ; ++I) |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 502 | if (CheckFormatArguments(*I, Args, NumArgs, IsMemberFunction, CallType, |
| 503 | Loc, Range)) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 504 | HandledFormatString = true; |
| 505 | |
| 506 | // Refuse POD arguments that weren't caught by the format string |
| 507 | // checks above. |
| 508 | if (!HandledFormatString && CallType != VariadicDoesNotApply) |
| 509 | for (unsigned ArgIdx = NumProtoArgs; ArgIdx < NumArgs; ++ArgIdx) |
| 510 | variadicArgumentPODCheck(Args[ArgIdx], CallType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 511 | |
Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 512 | for (specific_attr_iterator<NonNullAttr> |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 513 | I = FDecl->specific_attr_begin<NonNullAttr>(), |
| 514 | E = FDecl->specific_attr_end<NonNullAttr>(); I != E; ++I) |
| 515 | CheckNonNullArguments(*I, Args, Loc); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 516 | |
| 517 | // Type safety checking. |
| 518 | for (specific_attr_iterator<ArgumentWithTypeTagAttr> |
| 519 | i = FDecl->specific_attr_begin<ArgumentWithTypeTagAttr>(), |
| 520 | e = FDecl->specific_attr_end<ArgumentWithTypeTagAttr>(); i != e; ++i) { |
| 521 | CheckArgumentWithTypeTag(*i, Args); |
| 522 | } |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /// CheckConstructorCall - Check a constructor call for correctness and safety |
| 526 | /// properties not enforced by the C type system. |
| 527 | void Sema::CheckConstructorCall(FunctionDecl *FDecl, Expr **Args, |
| 528 | unsigned NumArgs, |
| 529 | const FunctionProtoType *Proto, |
| 530 | SourceLocation Loc) { |
| 531 | VariadicCallType CallType = |
| 532 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
| 533 | checkCall(FDecl, Args, NumArgs, Proto->getNumArgs(), |
| 534 | /*IsMemberFunction=*/true, Loc, SourceRange(), CallType); |
| 535 | } |
| 536 | |
| 537 | /// CheckFunctionCall - Check a direct function call for various correctness |
| 538 | /// and safety properties not strictly enforced by the C type system. |
| 539 | bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall, |
| 540 | const FunctionProtoType *Proto) { |
| 541 | bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall); |
| 542 | VariadicCallType CallType = getVariadicCallType(FDecl, Proto, |
| 543 | TheCall->getCallee()); |
| 544 | unsigned NumProtoArgs = Proto ? Proto->getNumArgs() : 0; |
| 545 | checkCall(FDecl, TheCall->getArgs(), TheCall->getNumArgs(), NumProtoArgs, |
| 546 | IsMemberFunction, TheCall->getRParenLoc(), |
| 547 | TheCall->getCallee()->getSourceRange(), CallType); |
| 548 | |
| 549 | IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
| 550 | // None of the checks below are needed for functions that don't have |
| 551 | // simple names (e.g., C++ conversion functions). |
| 552 | if (!FnInfo) |
| 553 | return false; |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 554 | |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 555 | unsigned CMId = FDecl->getMemoryFunctionKind(); |
| 556 | if (CMId == 0) |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 557 | return false; |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 558 | |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 559 | // Handle memory setting and copying functions. |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 560 | if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat) |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 561 | CheckStrlcpycatArguments(TheCall, FnInfo); |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 562 | else if (CMId == Builtin::BIstrncat) |
| 563 | CheckStrncatArguments(TheCall, FnInfo); |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 564 | else |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 565 | CheckMemaccessArguments(TheCall, CMId, FnInfo); |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 566 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 567 | return false; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 570 | bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac, |
| 571 | Expr **Args, unsigned NumArgs) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 572 | VariadicCallType CallType = |
| 573 | Method->isVariadic() ? VariadicMethod : VariadicDoesNotApply; |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 574 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 575 | checkCall(Method, Args, NumArgs, Method->param_size(), |
| 576 | /*IsMemberFunction=*/false, |
| 577 | lbrac, Method->getSourceRange(), CallType); |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 578 | |
| 579 | return false; |
| 580 | } |
| 581 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 582 | bool Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall, |
| 583 | const FunctionProtoType *Proto) { |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 584 | const VarDecl *V = dyn_cast<VarDecl>(NDecl); |
| 585 | if (!V) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 586 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 587 | |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 588 | QualType Ty = V->getType(); |
| 589 | if (!Ty->isBlockPointerType()) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 590 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 591 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 592 | VariadicCallType CallType = |
| 593 | Proto && Proto->isVariadic() ? VariadicBlock : VariadicDoesNotApply ; |
| 594 | unsigned NumProtoArgs = Proto ? Proto->getNumArgs() : 0; |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 595 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 596 | checkCall(NDecl, TheCall->getArgs(), TheCall->getNumArgs(), |
| 597 | NumProtoArgs, /*IsMemberFunction=*/false, |
| 598 | TheCall->getRParenLoc(), |
| 599 | TheCall->getCallee()->getSourceRange(), CallType); |
| 600 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 601 | return false; |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 604 | ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult, |
| 605 | AtomicExpr::AtomicOp Op) { |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 606 | CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); |
| 607 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 608 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 609 | // All these operations take one of the following forms: |
| 610 | enum { |
| 611 | // C __c11_atomic_init(A *, C) |
| 612 | Init, |
| 613 | // C __c11_atomic_load(A *, int) |
| 614 | Load, |
| 615 | // void __atomic_load(A *, CP, int) |
| 616 | Copy, |
| 617 | // C __c11_atomic_add(A *, M, int) |
| 618 | Arithmetic, |
| 619 | // C __atomic_exchange_n(A *, CP, int) |
| 620 | Xchg, |
| 621 | // void __atomic_exchange(A *, C *, CP, int) |
| 622 | GNUXchg, |
| 623 | // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int) |
| 624 | C11CmpXchg, |
| 625 | // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int) |
| 626 | GNUCmpXchg |
| 627 | } Form = Init; |
| 628 | const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 4, 5, 6 }; |
| 629 | const unsigned NumVals[] = { 1, 0, 1, 1, 1, 2, 2, 3 }; |
| 630 | // where: |
| 631 | // C is an appropriate type, |
| 632 | // A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins, |
| 633 | // CP is C for __c11 builtins and GNU _n builtins and is C * otherwise, |
| 634 | // M is C if C is an integer, and ptrdiff_t if C is a pointer, and |
| 635 | // the int parameters are for orderings. |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 636 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 637 | assert(AtomicExpr::AO__c11_atomic_init == 0 && |
| 638 | AtomicExpr::AO__c11_atomic_fetch_xor + 1 == AtomicExpr::AO__atomic_load |
| 639 | && "need to update code for modified C11 atomics"); |
| 640 | bool IsC11 = Op >= AtomicExpr::AO__c11_atomic_init && |
| 641 | Op <= AtomicExpr::AO__c11_atomic_fetch_xor; |
| 642 | bool IsN = Op == AtomicExpr::AO__atomic_load_n || |
| 643 | Op == AtomicExpr::AO__atomic_store_n || |
| 644 | Op == AtomicExpr::AO__atomic_exchange_n || |
| 645 | Op == AtomicExpr::AO__atomic_compare_exchange_n; |
| 646 | bool IsAddSub = false; |
| 647 | |
| 648 | switch (Op) { |
| 649 | case AtomicExpr::AO__c11_atomic_init: |
| 650 | Form = Init; |
| 651 | break; |
| 652 | |
| 653 | case AtomicExpr::AO__c11_atomic_load: |
| 654 | case AtomicExpr::AO__atomic_load_n: |
| 655 | Form = Load; |
| 656 | break; |
| 657 | |
| 658 | case AtomicExpr::AO__c11_atomic_store: |
| 659 | case AtomicExpr::AO__atomic_load: |
| 660 | case AtomicExpr::AO__atomic_store: |
| 661 | case AtomicExpr::AO__atomic_store_n: |
| 662 | Form = Copy; |
| 663 | break; |
| 664 | |
| 665 | case AtomicExpr::AO__c11_atomic_fetch_add: |
| 666 | case AtomicExpr::AO__c11_atomic_fetch_sub: |
| 667 | case AtomicExpr::AO__atomic_fetch_add: |
| 668 | case AtomicExpr::AO__atomic_fetch_sub: |
| 669 | case AtomicExpr::AO__atomic_add_fetch: |
| 670 | case AtomicExpr::AO__atomic_sub_fetch: |
| 671 | IsAddSub = true; |
| 672 | // Fall through. |
| 673 | case AtomicExpr::AO__c11_atomic_fetch_and: |
| 674 | case AtomicExpr::AO__c11_atomic_fetch_or: |
| 675 | case AtomicExpr::AO__c11_atomic_fetch_xor: |
| 676 | case AtomicExpr::AO__atomic_fetch_and: |
| 677 | case AtomicExpr::AO__atomic_fetch_or: |
| 678 | case AtomicExpr::AO__atomic_fetch_xor: |
Richard Smith | 51b9240 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 679 | case AtomicExpr::AO__atomic_fetch_nand: |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 680 | case AtomicExpr::AO__atomic_and_fetch: |
| 681 | case AtomicExpr::AO__atomic_or_fetch: |
| 682 | case AtomicExpr::AO__atomic_xor_fetch: |
Richard Smith | 51b9240 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 683 | case AtomicExpr::AO__atomic_nand_fetch: |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 684 | Form = Arithmetic; |
| 685 | break; |
| 686 | |
| 687 | case AtomicExpr::AO__c11_atomic_exchange: |
| 688 | case AtomicExpr::AO__atomic_exchange_n: |
| 689 | Form = Xchg; |
| 690 | break; |
| 691 | |
| 692 | case AtomicExpr::AO__atomic_exchange: |
| 693 | Form = GNUXchg; |
| 694 | break; |
| 695 | |
| 696 | case AtomicExpr::AO__c11_atomic_compare_exchange_strong: |
| 697 | case AtomicExpr::AO__c11_atomic_compare_exchange_weak: |
| 698 | Form = C11CmpXchg; |
| 699 | break; |
| 700 | |
| 701 | case AtomicExpr::AO__atomic_compare_exchange: |
| 702 | case AtomicExpr::AO__atomic_compare_exchange_n: |
| 703 | Form = GNUCmpXchg; |
| 704 | break; |
| 705 | } |
| 706 | |
| 707 | // Check we have the right number of arguments. |
| 708 | if (TheCall->getNumArgs() < NumArgs[Form]) { |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 709 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 710 | << 0 << NumArgs[Form] << TheCall->getNumArgs() |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 711 | << TheCall->getCallee()->getSourceRange(); |
| 712 | return ExprError(); |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 713 | } else if (TheCall->getNumArgs() > NumArgs[Form]) { |
| 714 | Diag(TheCall->getArg(NumArgs[Form])->getLocStart(), |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 715 | diag::err_typecheck_call_too_many_args) |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 716 | << 0 << NumArgs[Form] << TheCall->getNumArgs() |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 717 | << TheCall->getCallee()->getSourceRange(); |
| 718 | return ExprError(); |
| 719 | } |
| 720 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 721 | // Inspect the first argument of the atomic operation. |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 722 | Expr *Ptr = TheCall->getArg(0); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 723 | Ptr = DefaultFunctionArrayLvalueConversion(Ptr).get(); |
| 724 | const PointerType *pointerType = Ptr->getType()->getAs<PointerType>(); |
| 725 | if (!pointerType) { |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 726 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 727 | << Ptr->getType() << Ptr->getSourceRange(); |
| 728 | return ExprError(); |
| 729 | } |
| 730 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 731 | // For a __c11 builtin, this should be a pointer to an _Atomic type. |
| 732 | QualType AtomTy = pointerType->getPointeeType(); // 'A' |
| 733 | QualType ValType = AtomTy; // 'C' |
| 734 | if (IsC11) { |
| 735 | if (!AtomTy->isAtomicType()) { |
| 736 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic) |
| 737 | << Ptr->getType() << Ptr->getSourceRange(); |
| 738 | return ExprError(); |
| 739 | } |
| 740 | ValType = AtomTy->getAs<AtomicType>()->getValueType(); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 741 | } |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 742 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 743 | // For an arithmetic operation, the implied arithmetic must be well-formed. |
| 744 | if (Form == Arithmetic) { |
| 745 | // gcc does not enforce these rules for GNU atomics, but we do so for sanity. |
| 746 | if (IsAddSub && !ValType->isIntegerType() && !ValType->isPointerType()) { |
| 747 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) |
| 748 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 749 | return ExprError(); |
| 750 | } |
| 751 | if (!IsAddSub && !ValType->isIntegerType()) { |
| 752 | Diag(DRE->getLocStart(), diag::err_atomic_op_bitwise_needs_atomic_int) |
| 753 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 754 | return ExprError(); |
| 755 | } |
| 756 | } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) { |
| 757 | // For __atomic_*_n operations, the value type must be a scalar integral or |
| 758 | // 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] | 759 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 760 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 761 | return ExprError(); |
| 762 | } |
| 763 | |
| 764 | if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context)) { |
| 765 | // For GNU atomics, require a trivially-copyable type. This is not part of |
| 766 | // the GNU atomics specification, but we enforce it for sanity. |
| 767 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_trivial_copy) |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 768 | << Ptr->getType() << Ptr->getSourceRange(); |
| 769 | return ExprError(); |
| 770 | } |
| 771 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 772 | // FIXME: For any builtin other than a load, the ValType must not be |
| 773 | // const-qualified. |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 774 | |
| 775 | switch (ValType.getObjCLifetime()) { |
| 776 | case Qualifiers::OCL_None: |
| 777 | case Qualifiers::OCL_ExplicitNone: |
| 778 | // okay |
| 779 | break; |
| 780 | |
| 781 | case Qualifiers::OCL_Weak: |
| 782 | case Qualifiers::OCL_Strong: |
| 783 | case Qualifiers::OCL_Autoreleasing: |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 784 | // FIXME: Can this happen? By this point, ValType should be known |
| 785 | // to be trivially copyable. |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 786 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
| 787 | << ValType << Ptr->getSourceRange(); |
| 788 | return ExprError(); |
| 789 | } |
| 790 | |
| 791 | QualType ResultType = ValType; |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 792 | if (Form == Copy || Form == GNUXchg || Form == Init) |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 793 | ResultType = Context.VoidTy; |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 794 | else if (Form == C11CmpXchg || Form == GNUCmpXchg) |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 795 | ResultType = Context.BoolTy; |
| 796 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 797 | // The type of a parameter passed 'by value'. In the GNU atomics, such |
| 798 | // arguments are actually passed as pointers. |
| 799 | QualType ByValType = ValType; // 'CP' |
| 800 | if (!IsC11 && !IsN) |
| 801 | ByValType = Ptr->getType(); |
| 802 | |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 803 | // The first argument --- the pointer --- has a fixed type; we |
| 804 | // deduce the types of the rest of the arguments accordingly. Walk |
| 805 | // the remaining arguments, converting them to the deduced value type. |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 806 | for (unsigned i = 1; i != NumArgs[Form]; ++i) { |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 807 | QualType Ty; |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 808 | if (i < NumVals[Form] + 1) { |
| 809 | switch (i) { |
| 810 | case 1: |
| 811 | // The second argument is the non-atomic operand. For arithmetic, this |
| 812 | // is always passed by value, and for a compare_exchange it is always |
| 813 | // passed by address. For the rest, GNU uses by-address and C11 uses |
| 814 | // by-value. |
| 815 | assert(Form != Load); |
| 816 | if (Form == Init || (Form == Arithmetic && ValType->isIntegerType())) |
| 817 | Ty = ValType; |
| 818 | else if (Form == Copy || Form == Xchg) |
| 819 | Ty = ByValType; |
| 820 | else if (Form == Arithmetic) |
| 821 | Ty = Context.getPointerDiffType(); |
| 822 | else |
| 823 | Ty = Context.getPointerType(ValType.getUnqualifiedType()); |
| 824 | break; |
| 825 | case 2: |
| 826 | // The third argument to compare_exchange / GNU exchange is a |
| 827 | // (pointer to a) desired value. |
| 828 | Ty = ByValType; |
| 829 | break; |
| 830 | case 3: |
| 831 | // The fourth argument to GNU compare_exchange is a 'weak' flag. |
| 832 | Ty = Context.BoolTy; |
| 833 | break; |
| 834 | } |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 835 | } else { |
| 836 | // The order(s) are always converted to int. |
| 837 | Ty = Context.IntTy; |
| 838 | } |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 839 | |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 840 | InitializedEntity Entity = |
| 841 | InitializedEntity::InitializeParameter(Context, Ty, false); |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 842 | ExprResult Arg = TheCall->getArg(i); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 843 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 844 | if (Arg.isInvalid()) |
| 845 | return true; |
| 846 | TheCall->setArg(i, Arg.get()); |
| 847 | } |
| 848 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 849 | // Permute the arguments into a 'consistent' order. |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 850 | SmallVector<Expr*, 5> SubExprs; |
| 851 | SubExprs.push_back(Ptr); |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 852 | switch (Form) { |
| 853 | case Init: |
| 854 | // Note, AtomicExpr::getVal1() has a special case for this atomic. |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 855 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 856 | break; |
| 857 | case Load: |
| 858 | SubExprs.push_back(TheCall->getArg(1)); // Order |
| 859 | break; |
| 860 | case Copy: |
| 861 | case Arithmetic: |
| 862 | case Xchg: |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 863 | SubExprs.push_back(TheCall->getArg(2)); // Order |
| 864 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 865 | break; |
| 866 | case GNUXchg: |
| 867 | // Note, AtomicExpr::getVal2() has a special case for this atomic. |
| 868 | SubExprs.push_back(TheCall->getArg(3)); // Order |
| 869 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
| 870 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
| 871 | break; |
| 872 | case C11CmpXchg: |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 873 | SubExprs.push_back(TheCall->getArg(3)); // Order |
| 874 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 875 | SubExprs.push_back(TheCall->getArg(4)); // OrderFail |
David Chisnall | 2ebb98a | 2012-03-29 17:58:59 +0000 | [diff] [blame] | 876 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 877 | break; |
| 878 | case GNUCmpXchg: |
| 879 | SubExprs.push_back(TheCall->getArg(4)); // Order |
| 880 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
| 881 | SubExprs.push_back(TheCall->getArg(5)); // OrderFail |
| 882 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
| 883 | SubExprs.push_back(TheCall->getArg(3)); // Weak |
| 884 | break; |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 885 | } |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 886 | |
| 887 | return Owned(new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(), |
| 888 | SubExprs.data(), SubExprs.size(), |
| 889 | ResultType, Op, |
| 890 | TheCall->getRParenLoc())); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 894 | /// checkBuiltinArgument - Given a call to a builtin function, perform |
| 895 | /// normal type-checking on the given argument, updating the call in |
| 896 | /// place. This is useful when a builtin function requires custom |
| 897 | /// type-checking for some of its arguments but not necessarily all of |
| 898 | /// them. |
| 899 | /// |
| 900 | /// Returns true on error. |
| 901 | static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) { |
| 902 | FunctionDecl *Fn = E->getDirectCallee(); |
| 903 | assert(Fn && "builtin call without direct callee!"); |
| 904 | |
| 905 | ParmVarDecl *Param = Fn->getParamDecl(ArgIndex); |
| 906 | InitializedEntity Entity = |
| 907 | InitializedEntity::InitializeParameter(S.Context, Param); |
| 908 | |
| 909 | ExprResult Arg = E->getArg(0); |
| 910 | Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 911 | if (Arg.isInvalid()) |
| 912 | return true; |
| 913 | |
| 914 | E->setArg(ArgIndex, Arg.take()); |
| 915 | return false; |
| 916 | } |
| 917 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 918 | /// SemaBuiltinAtomicOverloaded - We have a call to a function like |
| 919 | /// __sync_fetch_and_add, which is an overloaded function based on the pointer |
| 920 | /// type of its first argument. The main ActOnCallExpr routines have already |
| 921 | /// promoted the types of arguments because all of these calls are prototyped as |
| 922 | /// void(...). |
| 923 | /// |
| 924 | /// This function goes through and does final semantic checking for these |
| 925 | /// builtins, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 926 | ExprResult |
| 927 | Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) { |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 928 | CallExpr *TheCall = (CallExpr *)TheCallResult.get(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 929 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 930 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 931 | |
| 932 | // 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] | 933 | if (TheCall->getNumArgs() < 1) { |
| 934 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 935 | << 0 << 1 << TheCall->getNumArgs() |
| 936 | << TheCall->getCallee()->getSourceRange(); |
| 937 | return ExprError(); |
| 938 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 939 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 940 | // Inspect the first argument of the atomic builtin. This should always be |
| 941 | // a pointer type, whose element is an integral scalar or pointer type. |
| 942 | // Because it is a pointer type, we don't have to worry about any implicit |
| 943 | // casts here. |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 944 | // FIXME: We don't allow floating point scalars as input. |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 945 | Expr *FirstArg = TheCall->getArg(0); |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 946 | ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg); |
| 947 | if (FirstArgResult.isInvalid()) |
| 948 | return ExprError(); |
| 949 | FirstArg = FirstArgResult.take(); |
| 950 | TheCall->setArg(0, FirstArg); |
| 951 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 952 | const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>(); |
| 953 | if (!pointerType) { |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 954 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
| 955 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 956 | return ExprError(); |
| 957 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 958 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 959 | QualType ValType = pointerType->getPointeeType(); |
Chris Lattner | dd5fa7a | 2010-09-17 21:12:38 +0000 | [diff] [blame] | 960 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 961 | !ValType->isBlockPointerType()) { |
| 962 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr) |
| 963 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 964 | return ExprError(); |
| 965 | } |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 966 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 967 | switch (ValType.getObjCLifetime()) { |
| 968 | case Qualifiers::OCL_None: |
| 969 | case Qualifiers::OCL_ExplicitNone: |
| 970 | // okay |
| 971 | break; |
| 972 | |
| 973 | case Qualifiers::OCL_Weak: |
| 974 | case Qualifiers::OCL_Strong: |
| 975 | case Qualifiers::OCL_Autoreleasing: |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 976 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 977 | << ValType << FirstArg->getSourceRange(); |
| 978 | return ExprError(); |
| 979 | } |
| 980 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 981 | // Strip any qualifiers off ValType. |
| 982 | ValType = ValType.getUnqualifiedType(); |
| 983 | |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 984 | // The majority of builtins return a value, but a few have special return |
| 985 | // types, so allow them to override appropriately below. |
| 986 | QualType ResultType = ValType; |
| 987 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 988 | // We need to figure out which concrete builtin this maps onto. For example, |
| 989 | // __sync_fetch_and_add with a 2 byte object turns into |
| 990 | // __sync_fetch_and_add_2. |
| 991 | #define BUILTIN_ROW(x) \ |
| 992 | { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ |
| 993 | Builtin::BI##x##_8, Builtin::BI##x##_16 } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 994 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 995 | static const unsigned BuiltinIndices[][5] = { |
| 996 | BUILTIN_ROW(__sync_fetch_and_add), |
| 997 | BUILTIN_ROW(__sync_fetch_and_sub), |
| 998 | BUILTIN_ROW(__sync_fetch_and_or), |
| 999 | BUILTIN_ROW(__sync_fetch_and_and), |
| 1000 | BUILTIN_ROW(__sync_fetch_and_xor), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1002 | BUILTIN_ROW(__sync_add_and_fetch), |
| 1003 | BUILTIN_ROW(__sync_sub_and_fetch), |
| 1004 | BUILTIN_ROW(__sync_and_and_fetch), |
| 1005 | BUILTIN_ROW(__sync_or_and_fetch), |
| 1006 | BUILTIN_ROW(__sync_xor_and_fetch), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1007 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1008 | BUILTIN_ROW(__sync_val_compare_and_swap), |
| 1009 | BUILTIN_ROW(__sync_bool_compare_and_swap), |
| 1010 | BUILTIN_ROW(__sync_lock_test_and_set), |
Chris Lattner | 23aa9c8 | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 1011 | BUILTIN_ROW(__sync_lock_release), |
| 1012 | BUILTIN_ROW(__sync_swap) |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1013 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1014 | #undef BUILTIN_ROW |
| 1015 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1016 | // Determine the index of the size. |
| 1017 | unsigned SizeIndex; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1018 | switch (Context.getTypeSizeInChars(ValType).getQuantity()) { |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1019 | case 1: SizeIndex = 0; break; |
| 1020 | case 2: SizeIndex = 1; break; |
| 1021 | case 4: SizeIndex = 2; break; |
| 1022 | case 8: SizeIndex = 3; break; |
| 1023 | case 16: SizeIndex = 4; break; |
| 1024 | default: |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1025 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size) |
| 1026 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 1027 | return ExprError(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1028 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1029 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1030 | // Each of these builtins has one pointer argument, followed by some number of |
| 1031 | // values (0, 1 or 2) followed by a potentially empty varags list of stuff |
| 1032 | // that we ignore. Find out which row of BuiltinIndices to read from as well |
| 1033 | // as the number of fixed args. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 1034 | unsigned BuiltinID = FDecl->getBuiltinID(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1035 | unsigned BuiltinIndex, NumFixed = 1; |
| 1036 | switch (BuiltinID) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1037 | default: llvm_unreachable("Unknown overloaded atomic builtin!"); |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1038 | case Builtin::BI__sync_fetch_and_add: |
| 1039 | case Builtin::BI__sync_fetch_and_add_1: |
| 1040 | case Builtin::BI__sync_fetch_and_add_2: |
| 1041 | case Builtin::BI__sync_fetch_and_add_4: |
| 1042 | case Builtin::BI__sync_fetch_and_add_8: |
| 1043 | case Builtin::BI__sync_fetch_and_add_16: |
| 1044 | BuiltinIndex = 0; |
| 1045 | break; |
| 1046 | |
| 1047 | case Builtin::BI__sync_fetch_and_sub: |
| 1048 | case Builtin::BI__sync_fetch_and_sub_1: |
| 1049 | case Builtin::BI__sync_fetch_and_sub_2: |
| 1050 | case Builtin::BI__sync_fetch_and_sub_4: |
| 1051 | case Builtin::BI__sync_fetch_and_sub_8: |
| 1052 | case Builtin::BI__sync_fetch_and_sub_16: |
| 1053 | BuiltinIndex = 1; |
| 1054 | break; |
| 1055 | |
| 1056 | case Builtin::BI__sync_fetch_and_or: |
| 1057 | case Builtin::BI__sync_fetch_and_or_1: |
| 1058 | case Builtin::BI__sync_fetch_and_or_2: |
| 1059 | case Builtin::BI__sync_fetch_and_or_4: |
| 1060 | case Builtin::BI__sync_fetch_and_or_8: |
| 1061 | case Builtin::BI__sync_fetch_and_or_16: |
| 1062 | BuiltinIndex = 2; |
| 1063 | break; |
| 1064 | |
| 1065 | case Builtin::BI__sync_fetch_and_and: |
| 1066 | case Builtin::BI__sync_fetch_and_and_1: |
| 1067 | case Builtin::BI__sync_fetch_and_and_2: |
| 1068 | case Builtin::BI__sync_fetch_and_and_4: |
| 1069 | case Builtin::BI__sync_fetch_and_and_8: |
| 1070 | case Builtin::BI__sync_fetch_and_and_16: |
| 1071 | BuiltinIndex = 3; |
| 1072 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1073 | |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1074 | case Builtin::BI__sync_fetch_and_xor: |
| 1075 | case Builtin::BI__sync_fetch_and_xor_1: |
| 1076 | case Builtin::BI__sync_fetch_and_xor_2: |
| 1077 | case Builtin::BI__sync_fetch_and_xor_4: |
| 1078 | case Builtin::BI__sync_fetch_and_xor_8: |
| 1079 | case Builtin::BI__sync_fetch_and_xor_16: |
| 1080 | BuiltinIndex = 4; |
| 1081 | break; |
| 1082 | |
| 1083 | case Builtin::BI__sync_add_and_fetch: |
| 1084 | case Builtin::BI__sync_add_and_fetch_1: |
| 1085 | case Builtin::BI__sync_add_and_fetch_2: |
| 1086 | case Builtin::BI__sync_add_and_fetch_4: |
| 1087 | case Builtin::BI__sync_add_and_fetch_8: |
| 1088 | case Builtin::BI__sync_add_and_fetch_16: |
| 1089 | BuiltinIndex = 5; |
| 1090 | break; |
| 1091 | |
| 1092 | case Builtin::BI__sync_sub_and_fetch: |
| 1093 | case Builtin::BI__sync_sub_and_fetch_1: |
| 1094 | case Builtin::BI__sync_sub_and_fetch_2: |
| 1095 | case Builtin::BI__sync_sub_and_fetch_4: |
| 1096 | case Builtin::BI__sync_sub_and_fetch_8: |
| 1097 | case Builtin::BI__sync_sub_and_fetch_16: |
| 1098 | BuiltinIndex = 6; |
| 1099 | break; |
| 1100 | |
| 1101 | case Builtin::BI__sync_and_and_fetch: |
| 1102 | case Builtin::BI__sync_and_and_fetch_1: |
| 1103 | case Builtin::BI__sync_and_and_fetch_2: |
| 1104 | case Builtin::BI__sync_and_and_fetch_4: |
| 1105 | case Builtin::BI__sync_and_and_fetch_8: |
| 1106 | case Builtin::BI__sync_and_and_fetch_16: |
| 1107 | BuiltinIndex = 7; |
| 1108 | break; |
| 1109 | |
| 1110 | case Builtin::BI__sync_or_and_fetch: |
| 1111 | case Builtin::BI__sync_or_and_fetch_1: |
| 1112 | case Builtin::BI__sync_or_and_fetch_2: |
| 1113 | case Builtin::BI__sync_or_and_fetch_4: |
| 1114 | case Builtin::BI__sync_or_and_fetch_8: |
| 1115 | case Builtin::BI__sync_or_and_fetch_16: |
| 1116 | BuiltinIndex = 8; |
| 1117 | break; |
| 1118 | |
| 1119 | case Builtin::BI__sync_xor_and_fetch: |
| 1120 | case Builtin::BI__sync_xor_and_fetch_1: |
| 1121 | case Builtin::BI__sync_xor_and_fetch_2: |
| 1122 | case Builtin::BI__sync_xor_and_fetch_4: |
| 1123 | case Builtin::BI__sync_xor_and_fetch_8: |
| 1124 | case Builtin::BI__sync_xor_and_fetch_16: |
| 1125 | BuiltinIndex = 9; |
| 1126 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1127 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1128 | case Builtin::BI__sync_val_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1129 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 1130 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 1131 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 1132 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 1133 | case Builtin::BI__sync_val_compare_and_swap_16: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 1134 | BuiltinIndex = 10; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1135 | NumFixed = 2; |
| 1136 | break; |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1137 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1138 | case Builtin::BI__sync_bool_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1139 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 1140 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 1141 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 1142 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 1143 | case Builtin::BI__sync_bool_compare_and_swap_16: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 1144 | BuiltinIndex = 11; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1145 | NumFixed = 2; |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1146 | ResultType = Context.BoolTy; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1147 | break; |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1148 | |
| 1149 | case Builtin::BI__sync_lock_test_and_set: |
| 1150 | case Builtin::BI__sync_lock_test_and_set_1: |
| 1151 | case Builtin::BI__sync_lock_test_and_set_2: |
| 1152 | case Builtin::BI__sync_lock_test_and_set_4: |
| 1153 | case Builtin::BI__sync_lock_test_and_set_8: |
| 1154 | case Builtin::BI__sync_lock_test_and_set_16: |
| 1155 | BuiltinIndex = 12; |
| 1156 | break; |
| 1157 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1158 | case Builtin::BI__sync_lock_release: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1159 | case Builtin::BI__sync_lock_release_1: |
| 1160 | case Builtin::BI__sync_lock_release_2: |
| 1161 | case Builtin::BI__sync_lock_release_4: |
| 1162 | case Builtin::BI__sync_lock_release_8: |
| 1163 | case Builtin::BI__sync_lock_release_16: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 1164 | BuiltinIndex = 13; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1165 | NumFixed = 0; |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1166 | ResultType = Context.VoidTy; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1167 | break; |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1168 | |
| 1169 | case Builtin::BI__sync_swap: |
| 1170 | case Builtin::BI__sync_swap_1: |
| 1171 | case Builtin::BI__sync_swap_2: |
| 1172 | case Builtin::BI__sync_swap_4: |
| 1173 | case Builtin::BI__sync_swap_8: |
| 1174 | case Builtin::BI__sync_swap_16: |
| 1175 | BuiltinIndex = 14; |
| 1176 | break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1177 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1178 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1179 | // Now that we know how many fixed arguments we expect, first check that we |
| 1180 | // have at least that many. |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1181 | if (TheCall->getNumArgs() < 1+NumFixed) { |
| 1182 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 1183 | << 0 << 1+NumFixed << TheCall->getNumArgs() |
| 1184 | << TheCall->getCallee()->getSourceRange(); |
| 1185 | return ExprError(); |
| 1186 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1187 | |
Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 1188 | // Get the decl for the concrete builtin from this, we can tell what the |
| 1189 | // concrete integer type we should convert to is. |
| 1190 | unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; |
| 1191 | const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID); |
| 1192 | IdentifierInfo *NewBuiltinII = PP.getIdentifierInfo(NewBuiltinName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | FunctionDecl *NewBuiltinDecl = |
Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 1194 | cast<FunctionDecl>(LazilyCreateBuiltin(NewBuiltinII, NewBuiltinID, |
| 1195 | TUScope, false, DRE->getLocStart())); |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1196 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1197 | // The first argument --- the pointer --- has a fixed type; we |
| 1198 | // deduce the types of the rest of the arguments accordingly. Walk |
| 1199 | // the remaining arguments, converting them to the deduced value type. |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1200 | for (unsigned i = 0; i != NumFixed; ++i) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1201 | ExprResult Arg = TheCall->getArg(i+1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1202 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1203 | // GCC does an implicit conversion to the pointer or integer ValType. This |
| 1204 | // 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] | 1205 | // Initialize the argument. |
| 1206 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 1207 | ValType, /*consume*/ false); |
| 1208 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1209 | if (Arg.isInvalid()) |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1210 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1211 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1212 | // Okay, we have something that *can* be converted to the right type. Check |
| 1213 | // to see if there is a potentially weird extension going on here. This can |
| 1214 | // happen when you do an atomic operation on something like an char* and |
| 1215 | // pass in 42. The 42 gets converted to char. This is even more strange |
| 1216 | // for things like 45.123 -> char, etc. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1217 | // FIXME: Do this check. |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1218 | TheCall->setArg(i+1, Arg.take()); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1219 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1220 | |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1221 | ASTContext& Context = this->getASTContext(); |
| 1222 | |
| 1223 | // Create a new DeclRefExpr to refer to the new decl. |
| 1224 | DeclRefExpr* NewDRE = DeclRefExpr::Create( |
| 1225 | Context, |
| 1226 | DRE->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1227 | SourceLocation(), |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1228 | NewBuiltinDecl, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1229 | /*enclosing*/ false, |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1230 | DRE->getLocation(), |
| 1231 | NewBuiltinDecl->getType(), |
| 1232 | DRE->getValueKind()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1233 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1234 | // Set the callee in the CallExpr. |
| 1235 | // FIXME: This leaks the original parens and implicit casts. |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1236 | ExprResult PromotedCall = UsualUnaryConversions(NewDRE); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1237 | if (PromotedCall.isInvalid()) |
| 1238 | return ExprError(); |
| 1239 | TheCall->setCallee(PromotedCall.take()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1240 | |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 1241 | // Change the result type of the call to match the original value type. This |
| 1242 | // is arbitrary, but the codegen for these builtins ins design to handle it |
| 1243 | // gracefully. |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1244 | TheCall->setType(ResultType); |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1245 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame^] | 1246 | return TheCallResult; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 1249 | /// CheckObjCString - Checks that the argument to the builtin |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1250 | /// CFString constructor is correct |
Steve Naroff | fd94262 | 2009-04-13 20:26:29 +0000 | [diff] [blame] | 1251 | /// Note: It might also make sense to do the UTF-16 conversion here (would |
| 1252 | /// simplify the backend). |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 1253 | bool Sema::CheckObjCString(Expr *Arg) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1254 | Arg = Arg->IgnoreParenCasts(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1255 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); |
| 1256 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1257 | if (!Literal || !Literal->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1258 | Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 1259 | << Arg->getSourceRange(); |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 1260 | return true; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1261 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1262 | |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1263 | if (Literal->containsNonAsciiOrNull()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1264 | StringRef String = Literal->getString(); |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1265 | unsigned NumBytes = String.size(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1266 | SmallVector<UTF16, 128> ToBuf(NumBytes); |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1267 | const UTF8 *FromPtr = (UTF8 *)String.data(); |
| 1268 | UTF16 *ToPtr = &ToBuf[0]; |
| 1269 | |
| 1270 | ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, |
| 1271 | &ToPtr, ToPtr + NumBytes, |
| 1272 | strictConversion); |
| 1273 | // Check for conversion failure. |
| 1274 | if (Result != conversionOK) |
| 1275 | Diag(Arg->getLocStart(), |
| 1276 | diag::warn_cfstring_truncated) << Arg->getSourceRange(); |
| 1277 | } |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 1278 | return false; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1281 | /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity. |
| 1282 | /// Emit an error and return true on failure, return false on success. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1283 | bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) { |
| 1284 | Expr *Fn = TheCall->getCallee(); |
| 1285 | if (TheCall->getNumArgs() > 2) { |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 1286 | Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1287 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1288 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
| 1289 | << Fn->getSourceRange() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1290 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1291 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1292 | return true; |
| 1293 | } |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 1294 | |
| 1295 | if (TheCall->getNumArgs() < 2) { |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1296 | return Diag(TheCall->getLocEnd(), |
| 1297 | diag::err_typecheck_call_too_few_args_at_least) |
| 1298 | << 0 /*function call*/ << 2 << TheCall->getNumArgs(); |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 1301 | // Type-check the first argument normally. |
| 1302 | if (checkBuiltinArgument(*this, TheCall, 0)) |
| 1303 | return true; |
| 1304 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1305 | // Determine whether the current function is variadic or not. |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1306 | BlockScopeInfo *CurBlock = getCurBlock(); |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1307 | bool isVariadic; |
Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 1308 | if (CurBlock) |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 1309 | isVariadic = CurBlock->TheDecl->isVariadic(); |
Ted Kremenek | 9498d38 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 1310 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
| 1311 | isVariadic = FD->isVariadic(); |
| 1312 | else |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1313 | isVariadic = getCurMethodDecl()->isVariadic(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1314 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1315 | if (!isVariadic) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1316 | Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function); |
| 1317 | return true; |
| 1318 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1319 | |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1320 | // Verify that the second argument to the builtin is the last argument of the |
| 1321 | // current function or method. |
| 1322 | bool SecondArgIsLastNamedArgument = false; |
Anders Carlsson | e2c1410 | 2008-02-13 01:22:59 +0000 | [diff] [blame] | 1323 | const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1324 | |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 1325 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { |
| 1326 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1327 | // FIXME: This isn't correct for methods (results in bogus warning). |
| 1328 | // Get the last formal in the current function. |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 1329 | const ParmVarDecl *LastArg; |
Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 1330 | if (CurBlock) |
| 1331 | LastArg = *(CurBlock->TheDecl->param_end()-1); |
| 1332 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1333 | LastArg = *(FD->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1334 | else |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1335 | LastArg = *(getCurMethodDecl()->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1336 | SecondArgIsLastNamedArgument = PV == LastArg; |
| 1337 | } |
| 1338 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1339 | |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1340 | if (!SecondArgIsLastNamedArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | Diag(TheCall->getArg(1)->getLocStart(), |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1342 | diag::warn_second_parameter_of_va_start_not_last_named_argument); |
| 1343 | return false; |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 1344 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1345 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1346 | /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and |
| 1347 | /// friends. This is declared to take (...), so we have to check everything. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1348 | bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { |
| 1349 | if (TheCall->getNumArgs() < 2) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 1350 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1351 | << 0 << 2 << TheCall->getNumArgs()/*function call*/; |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1352 | if (TheCall->getNumArgs() > 2) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1353 | return Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1354 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1355 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1356 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
| 1357 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1358 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1359 | ExprResult OrigArg0 = TheCall->getArg(0); |
| 1360 | ExprResult OrigArg1 = TheCall->getArg(1); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1361 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1362 | // Do standard promotions between the two arguments, returning their common |
| 1363 | // type. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1364 | QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1365 | if (OrigArg0.isInvalid() || OrigArg1.isInvalid()) |
| 1366 | return true; |
Daniel Dunbar | 403bc2b | 2009-02-19 19:28:43 +0000 | [diff] [blame] | 1367 | |
| 1368 | // Make sure any conversions are pushed back into the call; this is |
| 1369 | // type safe since unordered compare builtins are declared as "_Bool |
| 1370 | // foo(...)". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1371 | TheCall->setArg(0, OrigArg0.get()); |
| 1372 | TheCall->setArg(1, OrigArg1.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1373 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1374 | if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent()) |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1375 | return false; |
| 1376 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1377 | // If the common type isn't a real floating type, then the arguments were |
| 1378 | // invalid for this operation. |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 1379 | if (Res.isNull() || !Res->isRealFloatingType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1380 | return Diag(OrigArg0.get()->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1381 | diag::err_typecheck_call_invalid_ordered_compare) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1382 | << OrigArg0.get()->getType() << OrigArg1.get()->getType() |
| 1383 | << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1384 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1385 | return false; |
| 1386 | } |
| 1387 | |
Benjamin Kramer | e771a7a | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 1388 | /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like |
| 1389 | /// __builtin_isnan and friends. This is declared to take (...), so we have |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1390 | /// to check everything. We expect the last argument to be a floating point |
| 1391 | /// value. |
| 1392 | bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) { |
| 1393 | if (TheCall->getNumArgs() < NumArgs) |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1394 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1395 | << 0 << NumArgs << TheCall->getNumArgs()/*function call*/; |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1396 | if (TheCall->getNumArgs() > NumArgs) |
| 1397 | return Diag(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1398 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1399 | << 0 /*function call*/ << NumArgs << TheCall->getNumArgs() |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1400 | << SourceRange(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1401 | (*(TheCall->arg_end()-1))->getLocEnd()); |
| 1402 | |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1403 | Expr *OrigArg = TheCall->getArg(NumArgs-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1405 | if (OrigArg->isTypeDependent()) |
| 1406 | return false; |
| 1407 | |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1408 | // This operation requires a non-_Complex floating-point number. |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1409 | if (!OrigArg->getType()->isRealFloatingType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | return Diag(OrigArg->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1411 | diag::err_typecheck_call_invalid_unary_fp) |
| 1412 | << OrigArg->getType() << OrigArg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1413 | |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1414 | // If this is an implicit conversion from float -> double, remove it. |
| 1415 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) { |
| 1416 | Expr *CastArg = Cast->getSubExpr(); |
| 1417 | if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) { |
| 1418 | assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) && |
| 1419 | "promotion from float to double is the only expected cast here"); |
| 1420 | Cast->setSubExpr(0); |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1421 | TheCall->setArg(NumArgs-1, CastArg); |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1422 | } |
| 1423 | } |
| 1424 | |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1425 | return false; |
| 1426 | } |
| 1427 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1428 | /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. |
| 1429 | // This is declared to take (...), so we have to check everything. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1430 | ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1431 | if (TheCall->getNumArgs() < 2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1432 | return ExprError(Diag(TheCall->getLocEnd(), |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1433 | diag::err_typecheck_call_too_few_args_at_least) |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1434 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1435 | << TheCall->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1436 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1437 | // Determine which of the following types of shufflevector we're checking: |
| 1438 | // 1) unary, vector mask: (lhs, mask) |
| 1439 | // 2) binary, vector mask: (lhs, rhs, mask) |
| 1440 | // 3) binary, scalar mask: (lhs, rhs, index, ..., index) |
| 1441 | QualType resType = TheCall->getArg(0)->getType(); |
| 1442 | unsigned numElements = 0; |
| 1443 | |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1444 | if (!TheCall->getArg(0)->isTypeDependent() && |
| 1445 | !TheCall->getArg(1)->isTypeDependent()) { |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1446 | QualType LHSType = TheCall->getArg(0)->getType(); |
| 1447 | QualType RHSType = TheCall->getArg(1)->getType(); |
| 1448 | |
| 1449 | if (!LHSType->isVectorType() || !RHSType->isVectorType()) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1450 | Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1451 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1452 | TheCall->getArg(1)->getLocEnd()); |
| 1453 | return ExprError(); |
| 1454 | } |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1455 | |
| 1456 | numElements = LHSType->getAs<VectorType>()->getNumElements(); |
| 1457 | unsigned numResElements = TheCall->getNumArgs() - 2; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1459 | // Check to see if we have a call with 2 vector arguments, the unary shuffle |
| 1460 | // with mask. If so, verify that RHS is an integer vector type with the |
| 1461 | // same number of elts as lhs. |
| 1462 | if (TheCall->getNumArgs() == 2) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 1463 | if (!RHSType->hasIntegerRepresentation() || |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1464 | RHSType->getAs<VectorType>()->getNumElements() != numElements) |
| 1465 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
| 1466 | << SourceRange(TheCall->getArg(1)->getLocStart(), |
| 1467 | TheCall->getArg(1)->getLocEnd()); |
| 1468 | numResElements = numElements; |
| 1469 | } |
| 1470 | else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1471 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1473 | TheCall->getArg(1)->getLocEnd()); |
| 1474 | return ExprError(); |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1475 | } else if (numElements != numResElements) { |
| 1476 | QualType eltType = LHSType->getAs<VectorType>()->getElementType(); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1477 | resType = Context.getVectorType(eltType, numResElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1478 | VectorType::GenericVector); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1479 | } |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1483 | if (TheCall->getArg(i)->isTypeDependent() || |
| 1484 | TheCall->getArg(i)->isValueDependent()) |
| 1485 | continue; |
| 1486 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1487 | llvm::APSInt Result(32); |
| 1488 | if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) |
| 1489 | return ExprError(Diag(TheCall->getLocStart(), |
| 1490 | diag::err_shufflevector_nonconstant_argument) |
| 1491 | << TheCall->getArg(i)->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1492 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 1493 | if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1494 | return ExprError(Diag(TheCall->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1495 | diag::err_shufflevector_argument_too_large) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1496 | << TheCall->getArg(i)->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1499 | SmallVector<Expr*, 32> exprs; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1500 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 1501 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1502 | exprs.push_back(TheCall->getArg(i)); |
| 1503 | TheCall->setArg(i, 0); |
| 1504 | } |
| 1505 | |
Nate Begeman | a88dc30 | 2009-08-12 02:10:25 +0000 | [diff] [blame] | 1506 | return Owned(new (Context) ShuffleVectorExpr(Context, exprs.begin(), |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1507 | exprs.size(), resType, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1508 | TheCall->getCallee()->getLocStart(), |
| 1509 | TheCall->getRParenLoc())); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1510 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1511 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1512 | /// SemaBuiltinPrefetch - Handle __builtin_prefetch. |
| 1513 | // This is declared to take (const void*, ...) and can take two |
| 1514 | // optional constant int args. |
| 1515 | bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1516 | unsigned NumArgs = TheCall->getNumArgs(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1517 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1518 | if (NumArgs > 3) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1519 | return Diag(TheCall->getLocEnd(), |
| 1520 | diag::err_typecheck_call_too_many_args_at_most) |
| 1521 | << 0 /*function call*/ << 3 << NumArgs |
| 1522 | << TheCall->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1523 | |
| 1524 | // Argument 0 is checked for us and the remaining arguments must be |
| 1525 | // constant integers. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1526 | for (unsigned i = 1; i != NumArgs; ++i) { |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1527 | Expr *Arg = TheCall->getArg(i); |
Douglas Gregor | 592a423 | 2012-06-29 01:05:22 +0000 | [diff] [blame] | 1528 | |
| 1529 | // We can't check the value of a dependent argument. |
| 1530 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 1531 | continue; |
| 1532 | |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1533 | llvm::APSInt Result; |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1534 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 1535 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1537 | // FIXME: gcc issues a warning and rewrites these to 0. These |
| 1538 | // seems especially odd for the third argument since the default |
| 1539 | // is 3. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1540 | if (i == 1) { |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1541 | if (Result.getLimitedValue() > 1) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1542 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1543 | << "0" << "1" << Arg->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1544 | } else { |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1545 | if (Result.getLimitedValue() > 3) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1546 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1547 | << "0" << "3" << Arg->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1548 | } |
| 1549 | } |
| 1550 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1551 | return false; |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1554 | /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr |
| 1555 | /// TheCall is a constant expression. |
| 1556 | bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum, |
| 1557 | llvm::APSInt &Result) { |
| 1558 | Expr *Arg = TheCall->getArg(ArgNum); |
| 1559 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 1560 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 1561 | |
| 1562 | if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; |
| 1563 | |
| 1564 | if (!Arg->isIntegerConstantExpr(Result, Context)) |
| 1565 | return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type) |
Eric Christopher | 5e89655 | 2010-04-19 18:23:02 +0000 | [diff] [blame] | 1566 | << FDecl->getDeclName() << Arg->getSourceRange(); |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1567 | |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1568 | return false; |
| 1569 | } |
| 1570 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1571 | /// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr, |
| 1572 | /// int type). This simply type checks that type is one of the defined |
| 1573 | /// constants (0-3). |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1574 | // For compatibility check 0-3, llvm only handles 0 and 2. |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1575 | bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) { |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1576 | llvm::APSInt Result; |
Douglas Gregor | 592a423 | 2012-06-29 01:05:22 +0000 | [diff] [blame] | 1577 | |
| 1578 | // We can't check the value of a dependent argument. |
| 1579 | if (TheCall->getArg(1)->isTypeDependent() || |
| 1580 | TheCall->getArg(1)->isValueDependent()) |
| 1581 | return false; |
| 1582 | |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1583 | // Check constant-ness first. |
| 1584 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 1585 | return true; |
| 1586 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1587 | Expr *Arg = TheCall->getArg(1); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1588 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1589 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 1590 | << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | return false; |
| 1594 | } |
| 1595 | |
Eli Friedman | 586d6a8 | 2009-05-03 06:04:26 +0000 | [diff] [blame] | 1596 | /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 1597 | /// This checks that val is a constant 1. |
| 1598 | bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { |
| 1599 | Expr *Arg = TheCall->getArg(1); |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1600 | llvm::APSInt Result; |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1601 | |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1602 | // TODO: This is less than ideal. Overload this to take a value. |
| 1603 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 1604 | return true; |
| 1605 | |
| 1606 | if (Result != 1) |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 1607 | return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val) |
| 1608 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
| 1609 | |
| 1610 | return false; |
| 1611 | } |
| 1612 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1613 | // Determine if an expression is a string literal or constant string. |
| 1614 | // If this function returns false on the arguments to a function expecting a |
| 1615 | // format string, we will usually need to emit a warning. |
| 1616 | // True string literals are then checked by CheckFormatString. |
| 1617 | Sema::StringLiteralCheckType |
| 1618 | Sema::checkFormatStringExpr(const Expr *E, Expr **Args, |
| 1619 | unsigned NumArgs, bool HasVAListArg, |
| 1620 | unsigned format_idx, unsigned firstDataArg, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1621 | FormatStringType Type, VariadicCallType CallType, |
| 1622 | bool inFunctionCall) { |
Ted Kremenek | 4fe6441 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 1623 | tryAgain: |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1624 | if (E->isTypeDependent() || E->isValueDependent()) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1625 | return SLCT_NotALiteral; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1626 | |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1627 | E = E->IgnoreParenCasts(); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1628 | |
David Blaikie | a73cdcb | 2012-02-10 21:07:25 +0000 | [diff] [blame] | 1629 | if (E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 1630 | // Technically -Wformat-nonliteral does not warn about this case. |
| 1631 | // The behavior of printf and friends in this case is implementation |
| 1632 | // dependent. Ideally if the format string cannot be null then |
| 1633 | // it should have a 'nonnull' attribute in the function prototype. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1634 | return SLCT_CheckedLiteral; |
David Blaikie | a73cdcb | 2012-02-10 21:07:25 +0000 | [diff] [blame] | 1635 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1636 | switch (E->getStmtClass()) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1637 | case Stmt::BinaryConditionalOperatorClass: |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1638 | case Stmt::ConditionalOperatorClass: { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1639 | // The expression is a literal if both sub-expressions were, and it was |
| 1640 | // completely checked only if both sub-expressions were checked. |
| 1641 | const AbstractConditionalOperator *C = |
| 1642 | cast<AbstractConditionalOperator>(E); |
| 1643 | StringLiteralCheckType Left = |
| 1644 | checkFormatStringExpr(C->getTrueExpr(), Args, NumArgs, |
| 1645 | HasVAListArg, format_idx, firstDataArg, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1646 | Type, CallType, inFunctionCall); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1647 | if (Left == SLCT_NotALiteral) |
| 1648 | return SLCT_NotALiteral; |
| 1649 | StringLiteralCheckType Right = |
| 1650 | checkFormatStringExpr(C->getFalseExpr(), Args, NumArgs, |
| 1651 | HasVAListArg, format_idx, firstDataArg, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1652 | Type, CallType, inFunctionCall); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1653 | return Left < Right ? Left : Right; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | case Stmt::ImplicitCastExprClass: { |
Ted Kremenek | 4fe6441 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 1657 | E = cast<ImplicitCastExpr>(E)->getSubExpr(); |
| 1658 | goto tryAgain; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1661 | case Stmt::OpaqueValueExprClass: |
| 1662 | if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) { |
| 1663 | E = src; |
| 1664 | goto tryAgain; |
| 1665 | } |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1666 | return SLCT_NotALiteral; |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1667 | |
Ted Kremenek | b43e8ad | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 1668 | case Stmt::PredefinedExprClass: |
| 1669 | // While __func__, etc., are technically not string literals, they |
| 1670 | // cannot contain format specifiers and thus are not a security |
| 1671 | // liability. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1672 | return SLCT_UncheckedLiteral; |
Ted Kremenek | b43e8ad | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 1673 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1674 | case Stmt::DeclRefExprClass: { |
| 1675 | const DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1676 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1677 | // As an exception, do not flag errors for variables binding to |
| 1678 | // const string literals. |
| 1679 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 1680 | bool isConstant = false; |
| 1681 | QualType T = DR->getType(); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1682 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1683 | if (const ArrayType *AT = Context.getAsArrayType(T)) { |
| 1684 | isConstant = AT->getElementType().isConstant(Context); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1685 | } else if (const PointerType *PT = T->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1686 | isConstant = T.isConstant(Context) && |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1687 | PT->getPointeeType().isConstant(Context); |
Jean-Daniel Dupas | e98e5b5 | 2012-01-25 10:35:33 +0000 | [diff] [blame] | 1688 | } else if (T->isObjCObjectPointerType()) { |
| 1689 | // In ObjC, there is usually no "const ObjectPointer" type, |
| 1690 | // so don't check if the pointee type is constant. |
| 1691 | isConstant = T.isConstant(Context); |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1692 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1693 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1694 | if (isConstant) { |
Matt Beaumont-Gay | e2c6066 | 2012-05-11 22:10:59 +0000 | [diff] [blame] | 1695 | if (const Expr *Init = VD->getAnyInitializer()) { |
| 1696 | // Look through initializers like const char c[] = { "foo" } |
| 1697 | if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { |
| 1698 | if (InitList->isStringLiteralInit()) |
| 1699 | Init = InitList->getInit(0)->IgnoreParenImpCasts(); |
| 1700 | } |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1701 | return checkFormatStringExpr(Init, Args, NumArgs, |
| 1702 | HasVAListArg, format_idx, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1703 | firstDataArg, Type, CallType, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1704 | /*inFunctionCall*/false); |
Matt Beaumont-Gay | e2c6066 | 2012-05-11 22:10:59 +0000 | [diff] [blame] | 1705 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1706 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1707 | |
Anders Carlsson | d966a55 | 2009-06-28 19:55:58 +0000 | [diff] [blame] | 1708 | // For vprintf* functions (i.e., HasVAListArg==true), we add a |
| 1709 | // special check to see if the format string is a function parameter |
| 1710 | // of the function calling the printf function. If the function |
| 1711 | // has an attribute indicating it is a printf-like function, then we |
| 1712 | // should suppress warnings concerning non-literals being used in a call |
| 1713 | // to a vprintf function. For example: |
| 1714 | // |
| 1715 | // void |
| 1716 | // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){ |
| 1717 | // va_list ap; |
| 1718 | // va_start(ap, fmt); |
| 1719 | // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". |
| 1720 | // ... |
| 1721 | // |
Jean-Daniel Dupas | f57c413 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 1722 | if (HasVAListArg) { |
| 1723 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) { |
| 1724 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) { |
| 1725 | int PVIndex = PV->getFunctionScopeIndex() + 1; |
| 1726 | for (specific_attr_iterator<FormatAttr> |
| 1727 | i = ND->specific_attr_begin<FormatAttr>(), |
| 1728 | e = ND->specific_attr_end<FormatAttr>(); i != e ; ++i) { |
| 1729 | FormatAttr *PVFormat = *i; |
| 1730 | // adjust for implicit parameter |
| 1731 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 1732 | if (MD->isInstance()) |
| 1733 | ++PVIndex; |
| 1734 | // We also check if the formats are compatible. |
| 1735 | // We can't pass a 'scanf' string to a 'printf' function. |
| 1736 | if (PVIndex == PVFormat->getFormatIdx() && |
| 1737 | Type == GetFormatStringType(PVFormat)) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1738 | return SLCT_UncheckedLiteral; |
Jean-Daniel Dupas | f57c413 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 1739 | } |
| 1740 | } |
| 1741 | } |
| 1742 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1743 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1744 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1745 | return SLCT_NotALiteral; |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1746 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1747 | |
Jean-Daniel Dupas | 52aabaf | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 1748 | case Stmt::CallExprClass: |
| 1749 | case Stmt::CXXMemberCallExprClass: { |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1750 | const CallExpr *CE = cast<CallExpr>(E); |
Jean-Daniel Dupas | 52aabaf | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 1751 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) { |
| 1752 | if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) { |
| 1753 | unsigned ArgIndex = FA->getFormatIdx(); |
| 1754 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 1755 | if (MD->isInstance()) |
| 1756 | --ArgIndex; |
| 1757 | const Expr *Arg = CE->getArg(ArgIndex - 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1758 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1759 | return checkFormatStringExpr(Arg, Args, NumArgs, |
| 1760 | HasVAListArg, format_idx, firstDataArg, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1761 | Type, CallType, inFunctionCall); |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 1762 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 1763 | unsigned BuiltinID = FD->getBuiltinID(); |
| 1764 | if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString || |
| 1765 | BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) { |
| 1766 | const Expr *Arg = CE->getArg(0); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1767 | return checkFormatStringExpr(Arg, Args, NumArgs, |
| 1768 | HasVAListArg, format_idx, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1769 | firstDataArg, Type, CallType, |
| 1770 | inFunctionCall); |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 1771 | } |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1772 | } |
| 1773 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1774 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1775 | return SLCT_NotALiteral; |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1776 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1777 | case Stmt::ObjCStringLiteralClass: |
| 1778 | case Stmt::StringLiteralClass: { |
| 1779 | const StringLiteral *StrE = NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1780 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1781 | if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1782 | StrE = ObjCFExpr->getString(); |
| 1783 | else |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1784 | StrE = cast<StringLiteral>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1786 | if (StrE) { |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1787 | CheckFormatString(StrE, E, Args, NumArgs, HasVAListArg, format_idx, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1788 | firstDataArg, Type, inFunctionCall, CallType); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1789 | return SLCT_CheckedLiteral; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1790 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1792 | return SLCT_NotALiteral; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1793 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1794 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1795 | default: |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1796 | return SLCT_NotALiteral; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1797 | } |
| 1798 | } |
| 1799 | |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1800 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1801 | Sema::CheckNonNullArguments(const NonNullAttr *NonNull, |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1802 | const Expr * const *ExprArgs, |
| 1803 | SourceLocation CallSiteLoc) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1804 | for (NonNullAttr::args_iterator i = NonNull->args_begin(), |
| 1805 | e = NonNull->args_end(); |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1806 | i != e; ++i) { |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1807 | const Expr *ArgExpr = ExprArgs[*i]; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1808 | if (ArgExpr->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1809 | Expr::NPC_ValueDependentIsNotNull)) |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1810 | Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1811 | } |
| 1812 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1813 | |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1814 | Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) { |
| 1815 | return llvm::StringSwitch<FormatStringType>(Format->getType()) |
| 1816 | .Case("scanf", FST_Scanf) |
| 1817 | .Cases("printf", "printf0", FST_Printf) |
| 1818 | .Cases("NSString", "CFString", FST_NSString) |
| 1819 | .Case("strftime", FST_Strftime) |
| 1820 | .Case("strfmon", FST_Strfmon) |
| 1821 | .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf) |
| 1822 | .Default(FST_Unknown); |
| 1823 | } |
| 1824 | |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1825 | /// CheckFormatArguments - Check calls to printf and scanf (and similar |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1826 | /// functions) for correct use of format strings. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1827 | /// Returns true if a format string has been fully checked. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1828 | bool Sema::CheckFormatArguments(const FormatAttr *Format, Expr **Args, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1829 | unsigned NumArgs, bool IsCXXMember, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1830 | VariadicCallType CallType, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1831 | SourceLocation Loc, SourceRange Range) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1832 | FormatStringInfo FSI; |
| 1833 | if (getFormatStringInfo(Format, IsCXXMember, &FSI)) |
| 1834 | return CheckFormatArguments(Args, NumArgs, FSI.HasVAListArg, FSI.FormatIdx, |
| 1835 | FSI.FirstDataArg, GetFormatStringType(Format), |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1836 | CallType, Loc, Range); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1837 | return false; |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1838 | } |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1839 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1840 | bool Sema::CheckFormatArguments(Expr **Args, unsigned NumArgs, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1841 | bool HasVAListArg, unsigned format_idx, |
| 1842 | unsigned firstDataArg, FormatStringType Type, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1843 | VariadicCallType CallType, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1844 | SourceLocation Loc, SourceRange Range) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1845 | // CHECK: printf/scanf-like function is called with no format string. |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1846 | if (format_idx >= NumArgs) { |
| 1847 | Diag(Loc, diag::warn_missing_format_string) << Range; |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1848 | return false; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1849 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1850 | |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1851 | const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1852 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1853 | // CHECK: format string is not a string literal. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | // |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1855 | // Dynamically generated format strings are difficult to |
| 1856 | // automatically vet at compile time. Requiring that format strings |
| 1857 | // are string literals: (1) permits the checking of format strings by |
| 1858 | // the compiler and thereby (2) can practically remove the source of |
| 1859 | // many format string exploits. |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1860 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1861 | // Format string can be either ObjC string (e.g. @"%d") or |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1862 | // C string (e.g. "%d") |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1863 | // 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] | 1864 | // the same format string checking logic for both ObjC and C strings. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1865 | StringLiteralCheckType CT = |
| 1866 | checkFormatStringExpr(OrigFormatExpr, Args, NumArgs, HasVAListArg, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1867 | format_idx, firstDataArg, Type, CallType); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1868 | if (CT != SLCT_NotALiteral) |
| 1869 | // Literal format string found, check done! |
| 1870 | return CT == SLCT_CheckedLiteral; |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1871 | |
Jean-Daniel Dupas | 2837a2f | 2012-02-07 23:10:53 +0000 | [diff] [blame] | 1872 | // Strftime is particular as it always uses a single 'time' argument, |
| 1873 | // so it is safe to pass a non-literal string. |
| 1874 | if (Type == FST_Strftime) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1875 | return false; |
Jean-Daniel Dupas | 2837a2f | 2012-02-07 23:10:53 +0000 | [diff] [blame] | 1876 | |
Jean-Daniel Dupas | ce3aa39 | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 1877 | // Do not emit diag when the string param is a macro expansion and the |
| 1878 | // format is either NSString or CFString. This is a hack to prevent |
| 1879 | // diag when using the NSLocalizedString and CFCopyLocalizedString macros |
| 1880 | // 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] | 1881 | if (Type == FST_NSString && |
| 1882 | SourceMgr.isInSystemMacro(Args[format_idx]->getLocStart())) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1883 | return false; |
Jean-Daniel Dupas | ce3aa39 | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 1884 | |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1885 | // If there are no arguments specified, warn with -Wformat-security, otherwise |
| 1886 | // warn only with -Wformat-nonliteral. |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1887 | if (NumArgs == format_idx+1) |
| 1888 | Diag(Args[format_idx]->getLocStart(), |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1889 | diag::warn_format_nonliteral_noargs) |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1890 | << OrigFormatExpr->getSourceRange(); |
| 1891 | else |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1892 | Diag(Args[format_idx]->getLocStart(), |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1893 | diag::warn_format_nonliteral) |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1894 | << OrigFormatExpr->getSourceRange(); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1895 | return false; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1896 | } |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1897 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1898 | namespace { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1899 | class CheckFormatHandler : public analyze_format_string::FormatStringHandler { |
| 1900 | protected: |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1901 | Sema &S; |
| 1902 | const StringLiteral *FExpr; |
| 1903 | const Expr *OrigFormatExpr; |
Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1904 | const unsigned FirstDataArg; |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1905 | const unsigned NumDataArgs; |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1906 | const char *Beg; // Start of format string. |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1907 | const bool HasVAListArg; |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1908 | const Expr * const *Args; |
| 1909 | const unsigned NumArgs; |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1910 | unsigned FormatIdx; |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1911 | llvm::BitVector CoveredArgs; |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1912 | bool usesPositionalArgs; |
| 1913 | bool atFirstArg; |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1914 | bool inFunctionCall; |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1915 | Sema::VariadicCallType CallType; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1916 | public: |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1917 | CheckFormatHandler(Sema &s, const StringLiteral *fexpr, |
Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1918 | const Expr *origFormatExpr, unsigned firstDataArg, |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 1919 | unsigned numDataArgs, const char *beg, bool hasVAListArg, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1920 | Expr **args, unsigned numArgs, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1921 | unsigned formatIdx, bool inFunctionCall, |
| 1922 | Sema::VariadicCallType callType) |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1923 | : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 1924 | FirstDataArg(firstDataArg), NumDataArgs(numDataArgs), |
| 1925 | Beg(beg), HasVAListArg(hasVAListArg), |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1926 | Args(args), NumArgs(numArgs), FormatIdx(formatIdx), |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1927 | usesPositionalArgs(false), atFirstArg(true), |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1928 | inFunctionCall(inFunctionCall), CallType(callType) { |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1929 | CoveredArgs.resize(numDataArgs); |
| 1930 | CoveredArgs.reset(); |
| 1931 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1932 | |
Ted Kremenek | 07d161f | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 1933 | void DoneProcessing(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1934 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1935 | void HandleIncompleteSpecifier(const char *startSpecifier, |
| 1936 | unsigned specifierLen); |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 1937 | |
| 1938 | void HandleNonStandardLengthModifier( |
| 1939 | const analyze_format_string::LengthModifier &LM, |
| 1940 | const char *startSpecifier, unsigned specifierLen); |
| 1941 | |
| 1942 | void HandleNonStandardConversionSpecifier( |
| 1943 | const analyze_format_string::ConversionSpecifier &CS, |
| 1944 | const char *startSpecifier, unsigned specifierLen); |
| 1945 | |
| 1946 | void HandleNonStandardConversionSpecification( |
| 1947 | const analyze_format_string::LengthModifier &LM, |
| 1948 | const analyze_format_string::ConversionSpecifier &CS, |
| 1949 | const char *startSpecifier, unsigned specifierLen); |
| 1950 | |
Hans Wennborg | f856264 | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 1951 | virtual void HandlePosition(const char *startPos, unsigned posLen); |
| 1952 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1953 | virtual void HandleInvalidPosition(const char *startSpecifier, |
| 1954 | unsigned specifierLen, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1955 | analyze_format_string::PositionContext p); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1956 | |
| 1957 | virtual void HandleZeroPosition(const char *startPos, unsigned posLen); |
| 1958 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1959 | void HandleNullChar(const char *nullCharacter); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1960 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1961 | template <typename Range> |
| 1962 | static void EmitFormatDiagnostic(Sema &S, bool inFunctionCall, |
| 1963 | const Expr *ArgumentExpr, |
| 1964 | PartialDiagnostic PDiag, |
| 1965 | SourceLocation StringLoc, |
| 1966 | bool IsStringLocation, Range StringRange, |
| 1967 | FixItHint Fixit = FixItHint()); |
| 1968 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1969 | protected: |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1970 | bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, |
| 1971 | const char *startSpec, |
| 1972 | unsigned specifierLen, |
| 1973 | const char *csStart, unsigned csLen); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1974 | |
| 1975 | void HandlePositionalNonpositionalArgs(SourceLocation Loc, |
| 1976 | const char *startSpec, |
| 1977 | unsigned specifierLen); |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1978 | |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 1979 | SourceRange getFormatStringRange(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1980 | CharSourceRange getSpecifierRange(const char *startSpecifier, |
| 1981 | unsigned specifierLen); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1982 | SourceLocation getLocationOfByte(const char *x); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1983 | |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1984 | const Expr *getDataArg(unsigned i) const; |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 1985 | |
| 1986 | bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, |
| 1987 | const analyze_format_string::ConversionSpecifier &CS, |
| 1988 | const char *startSpecifier, unsigned specifierLen, |
| 1989 | unsigned argIndex); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1990 | |
| 1991 | template <typename Range> |
| 1992 | void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc, |
| 1993 | bool IsStringLocation, Range StringRange, |
| 1994 | FixItHint Fixit = FixItHint()); |
| 1995 | |
| 1996 | void CheckPositionalAndNonpositionalArgs( |
| 1997 | const analyze_format_string::FormatSpecifier *FS); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1998 | }; |
| 1999 | } |
| 2000 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2001 | SourceRange CheckFormatHandler::getFormatStringRange() { |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2002 | return OrigFormatExpr->getSourceRange(); |
| 2003 | } |
| 2004 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2005 | CharSourceRange CheckFormatHandler:: |
| 2006 | getSpecifierRange(const char *startSpecifier, unsigned specifierLen) { |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2007 | SourceLocation Start = getLocationOfByte(startSpecifier); |
| 2008 | SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1); |
| 2009 | |
| 2010 | // Advance the end SourceLocation by one due to half-open ranges. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2011 | End = End.getLocWithOffset(1); |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2012 | |
| 2013 | return CharSourceRange::getCharRange(Start, End); |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2016 | SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2017 | return S.getLocationOfStringLiteralByte(FExpr, x - Beg); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2020 | void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, |
| 2021 | unsigned specifierLen){ |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2022 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier), |
| 2023 | getLocationOfByte(startSpecifier), |
| 2024 | /*IsStringLocation*/true, |
| 2025 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2028 | void CheckFormatHandler::HandleNonStandardLengthModifier( |
| 2029 | const analyze_format_string::LengthModifier &LM, |
| 2030 | const char *startSpecifier, unsigned specifierLen) { |
| 2031 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) << LM.toString() |
Hans Wennborg | f856264 | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 2032 | << 0, |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2033 | getLocationOfByte(LM.getStart()), |
| 2034 | /*IsStringLocation*/true, |
| 2035 | getSpecifierRange(startSpecifier, specifierLen)); |
| 2036 | } |
| 2037 | |
| 2038 | void CheckFormatHandler::HandleNonStandardConversionSpecifier( |
| 2039 | const analyze_format_string::ConversionSpecifier &CS, |
| 2040 | const char *startSpecifier, unsigned specifierLen) { |
| 2041 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) << CS.toString() |
Hans Wennborg | f856264 | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 2042 | << 1, |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2043 | getLocationOfByte(CS.getStart()), |
| 2044 | /*IsStringLocation*/true, |
| 2045 | getSpecifierRange(startSpecifier, specifierLen)); |
| 2046 | } |
| 2047 | |
| 2048 | void CheckFormatHandler::HandleNonStandardConversionSpecification( |
| 2049 | const analyze_format_string::LengthModifier &LM, |
| 2050 | const analyze_format_string::ConversionSpecifier &CS, |
| 2051 | const char *startSpecifier, unsigned specifierLen) { |
| 2052 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_conversion_spec) |
| 2053 | << LM.toString() << CS.toString(), |
| 2054 | getLocationOfByte(LM.getStart()), |
| 2055 | /*IsStringLocation*/true, |
| 2056 | getSpecifierRange(startSpecifier, specifierLen)); |
| 2057 | } |
| 2058 | |
Hans Wennborg | f856264 | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 2059 | void CheckFormatHandler::HandlePosition(const char *startPos, |
| 2060 | unsigned posLen) { |
| 2061 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg), |
| 2062 | getLocationOfByte(startPos), |
| 2063 | /*IsStringLocation*/true, |
| 2064 | getSpecifierRange(startPos, posLen)); |
| 2065 | } |
| 2066 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2067 | void |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2068 | CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen, |
| 2069 | analyze_format_string::PositionContext p) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2070 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier) |
| 2071 | << (unsigned) p, |
| 2072 | getLocationOfByte(startPos), /*IsStringLocation*/true, |
| 2073 | getSpecifierRange(startPos, posLen)); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2076 | void CheckFormatHandler::HandleZeroPosition(const char *startPos, |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2077 | unsigned posLen) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2078 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier), |
| 2079 | getLocationOfByte(startPos), |
| 2080 | /*IsStringLocation*/true, |
| 2081 | getSpecifierRange(startPos, posLen)); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2084 | void CheckFormatHandler::HandleNullChar(const char *nullCharacter) { |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2085 | if (!isa<ObjCStringLiteral>(OrigFormatExpr)) { |
Ted Kremenek | 0c06944 | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 2086 | // The presence of a null character is likely an error. |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2087 | EmitFormatDiagnostic( |
| 2088 | S.PDiag(diag::warn_printf_format_string_contains_null_char), |
| 2089 | getLocationOfByte(nullCharacter), /*IsStringLocation*/true, |
| 2090 | getFormatStringRange()); |
Ted Kremenek | 0c06944 | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 2091 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2092 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2093 | |
Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2094 | // Note that this may return NULL if there was an error parsing or building |
| 2095 | // one of the argument expressions. |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2096 | const Expr *CheckFormatHandler::getDataArg(unsigned i) const { |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2097 | return Args[FirstDataArg + i]; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | void CheckFormatHandler::DoneProcessing() { |
| 2101 | // Does the number of data arguments exceed the number of |
| 2102 | // format conversions in the format string? |
| 2103 | if (!HasVAListArg) { |
| 2104 | // Find any arguments that weren't covered. |
| 2105 | CoveredArgs.flip(); |
| 2106 | signed notCoveredArg = CoveredArgs.find_first(); |
| 2107 | if (notCoveredArg >= 0) { |
| 2108 | assert((unsigned)notCoveredArg < NumDataArgs); |
Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2109 | if (const Expr *E = getDataArg((unsigned) notCoveredArg)) { |
| 2110 | SourceLocation Loc = E->getLocStart(); |
| 2111 | if (!S.getSourceManager().isInSystemMacro(Loc)) { |
| 2112 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_data_arg_not_used), |
| 2113 | Loc, /*IsStringLocation*/false, |
| 2114 | getFormatStringRange()); |
| 2115 | } |
Bob Wilson | c03f2df | 2012-05-03 19:47:19 +0000 | [diff] [blame] | 2116 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2117 | } |
| 2118 | } |
| 2119 | } |
| 2120 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2121 | bool |
| 2122 | CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, |
| 2123 | SourceLocation Loc, |
| 2124 | const char *startSpec, |
| 2125 | unsigned specifierLen, |
| 2126 | const char *csStart, |
| 2127 | unsigned csLen) { |
| 2128 | |
| 2129 | bool keepGoing = true; |
| 2130 | if (argIndex < NumDataArgs) { |
| 2131 | // Consider the argument coverered, even though the specifier doesn't |
| 2132 | // make sense. |
| 2133 | CoveredArgs.set(argIndex); |
| 2134 | } |
| 2135 | else { |
| 2136 | // If argIndex exceeds the number of data arguments we |
| 2137 | // don't issue a warning because that is just a cascade of warnings (and |
| 2138 | // they may have intended '%%' anyway). We don't want to continue processing |
| 2139 | // the format string after this point, however, as we will like just get |
| 2140 | // gibberish when trying to match arguments. |
| 2141 | keepGoing = false; |
| 2142 | } |
| 2143 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2144 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_conversion) |
| 2145 | << StringRef(csStart, csLen), |
| 2146 | Loc, /*IsStringLocation*/true, |
| 2147 | getSpecifierRange(startSpec, specifierLen)); |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2148 | |
| 2149 | return keepGoing; |
| 2150 | } |
| 2151 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2152 | void |
| 2153 | CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc, |
| 2154 | const char *startSpec, |
| 2155 | unsigned specifierLen) { |
| 2156 | EmitFormatDiagnostic( |
| 2157 | S.PDiag(diag::warn_format_mix_positional_nonpositional_args), |
| 2158 | Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen)); |
| 2159 | } |
| 2160 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2161 | bool |
| 2162 | CheckFormatHandler::CheckNumArgs( |
| 2163 | const analyze_format_string::FormatSpecifier &FS, |
| 2164 | const analyze_format_string::ConversionSpecifier &CS, |
| 2165 | const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { |
| 2166 | |
| 2167 | if (argIndex >= NumDataArgs) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2168 | PartialDiagnostic PDiag = FS.usesPositionalArg() |
| 2169 | ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args) |
| 2170 | << (argIndex+1) << NumDataArgs) |
| 2171 | : S.PDiag(diag::warn_printf_insufficient_data_args); |
| 2172 | EmitFormatDiagnostic( |
| 2173 | PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true, |
| 2174 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2175 | return false; |
| 2176 | } |
| 2177 | return true; |
| 2178 | } |
| 2179 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2180 | template<typename Range> |
| 2181 | void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag, |
| 2182 | SourceLocation Loc, |
| 2183 | bool IsStringLocation, |
| 2184 | Range StringRange, |
| 2185 | FixItHint FixIt) { |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2186 | EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag, |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2187 | Loc, IsStringLocation, StringRange, FixIt); |
| 2188 | } |
| 2189 | |
| 2190 | /// \brief If the format string is not within the funcion call, emit a note |
| 2191 | /// so that the function call and string are in diagnostic messages. |
| 2192 | /// |
Dmitri Gribenko | 70517ca | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 2193 | /// \param InFunctionCall if true, the format string is within the function |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2194 | /// call and only one diagnostic message will be produced. Otherwise, an |
| 2195 | /// extra note will be emitted pointing to location of the format string. |
| 2196 | /// |
| 2197 | /// \param ArgumentExpr the expression that is passed as the format string |
| 2198 | /// argument in the function call. Used for getting locations when two |
| 2199 | /// diagnostics are emitted. |
| 2200 | /// |
| 2201 | /// \param PDiag the callee should already have provided any strings for the |
| 2202 | /// diagnostic message. This function only adds locations and fixits |
| 2203 | /// to diagnostics. |
| 2204 | /// |
| 2205 | /// \param Loc primary location for diagnostic. If two diagnostics are |
| 2206 | /// required, one will be at Loc and a new SourceLocation will be created for |
| 2207 | /// the other one. |
| 2208 | /// |
| 2209 | /// \param IsStringLocation if true, Loc points to the format string should be |
| 2210 | /// used for the note. Otherwise, Loc points to the argument list and will |
| 2211 | /// be used with PDiag. |
| 2212 | /// |
| 2213 | /// \param StringRange some or all of the string to highlight. This is |
| 2214 | /// templated so it can accept either a CharSourceRange or a SourceRange. |
| 2215 | /// |
Dmitri Gribenko | 70517ca | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 2216 | /// \param FixIt optional fix it hint for the format string. |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2217 | template<typename Range> |
| 2218 | void CheckFormatHandler::EmitFormatDiagnostic(Sema &S, bool InFunctionCall, |
| 2219 | const Expr *ArgumentExpr, |
| 2220 | PartialDiagnostic PDiag, |
| 2221 | SourceLocation Loc, |
| 2222 | bool IsStringLocation, |
| 2223 | Range StringRange, |
| 2224 | FixItHint FixIt) { |
| 2225 | if (InFunctionCall) |
| 2226 | S.Diag(Loc, PDiag) << StringRange << FixIt; |
| 2227 | else { |
| 2228 | S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag) |
| 2229 | << ArgumentExpr->getSourceRange(); |
| 2230 | S.Diag(IsStringLocation ? Loc : StringRange.getBegin(), |
| 2231 | diag::note_format_string_defined) |
| 2232 | << StringRange << FixIt; |
| 2233 | } |
| 2234 | } |
| 2235 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2236 | //===--- CHECK: Printf format string checking ------------------------------===// |
| 2237 | |
| 2238 | namespace { |
| 2239 | class CheckPrintfHandler : public CheckFormatHandler { |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2240 | bool ObjCContext; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2241 | public: |
| 2242 | CheckPrintfHandler(Sema &s, const StringLiteral *fexpr, |
| 2243 | const Expr *origFormatExpr, unsigned firstDataArg, |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2244 | unsigned numDataArgs, bool isObjC, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2245 | const char *beg, bool hasVAListArg, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2246 | Expr **Args, unsigned NumArgs, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2247 | unsigned formatIdx, bool inFunctionCall, |
| 2248 | Sema::VariadicCallType CallType) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2249 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2250 | numDataArgs, beg, hasVAListArg, Args, NumArgs, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2251 | formatIdx, inFunctionCall, CallType), ObjCContext(isObjC) |
| 2252 | {} |
| 2253 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2254 | |
| 2255 | bool HandleInvalidPrintfConversionSpecifier( |
| 2256 | const analyze_printf::PrintfSpecifier &FS, |
| 2257 | const char *startSpecifier, |
| 2258 | unsigned specifierLen); |
| 2259 | |
| 2260 | bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, |
| 2261 | const char *startSpecifier, |
| 2262 | unsigned specifierLen); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2263 | bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, |
| 2264 | const char *StartSpecifier, |
| 2265 | unsigned SpecifierLen, |
| 2266 | const Expr *E); |
| 2267 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2268 | bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, |
| 2269 | const char *startSpecifier, unsigned specifierLen); |
| 2270 | void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, |
| 2271 | const analyze_printf::OptionalAmount &Amt, |
| 2272 | unsigned type, |
| 2273 | const char *startSpecifier, unsigned specifierLen); |
| 2274 | void HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
| 2275 | const analyze_printf::OptionalFlag &flag, |
| 2276 | const char *startSpecifier, unsigned specifierLen); |
| 2277 | void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, |
| 2278 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 2279 | const analyze_printf::OptionalFlag &flag, |
| 2280 | const char *startSpecifier, unsigned specifierLen); |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2281 | bool checkForCStrMembers(const analyze_printf::ArgType &AT, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2282 | const Expr *E, const CharSourceRange &CSR); |
| 2283 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2284 | }; |
| 2285 | } |
| 2286 | |
| 2287 | bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( |
| 2288 | const analyze_printf::PrintfSpecifier &FS, |
| 2289 | const char *startSpecifier, |
| 2290 | unsigned specifierLen) { |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2291 | const analyze_printf::PrintfConversionSpecifier &CS = |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2292 | FS.getConversionSpecifier(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2293 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2294 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 2295 | getLocationOfByte(CS.getStart()), |
| 2296 | startSpecifier, specifierLen, |
| 2297 | CS.getStart(), CS.getLength()); |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2300 | bool CheckPrintfHandler::HandleAmount( |
| 2301 | const analyze_format_string::OptionalAmount &Amt, |
| 2302 | unsigned k, const char *startSpecifier, |
| 2303 | unsigned specifierLen) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2304 | |
| 2305 | if (Amt.hasDataArgument()) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2306 | if (!HasVAListArg) { |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2307 | unsigned argIndex = Amt.getArgIndex(); |
| 2308 | if (argIndex >= NumDataArgs) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2309 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg) |
| 2310 | << k, |
| 2311 | getLocationOfByte(Amt.getStart()), |
| 2312 | /*IsStringLocation*/true, |
| 2313 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2314 | // Don't do any more checking. We will just emit |
| 2315 | // spurious errors. |
| 2316 | return false; |
| 2317 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2318 | |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2319 | // Type check the data argument. It should be an 'int'. |
Ted Kremenek | 31f8e32 | 2010-01-29 23:32:22 +0000 | [diff] [blame] | 2320 | // Although not in conformance with C99, we also allow the argument to be |
| 2321 | // an 'unsigned int' as that is a reasonably safe case. GCC also |
| 2322 | // doesn't emit a warning for that case. |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2323 | CoveredArgs.set(argIndex); |
| 2324 | const Expr *Arg = getDataArg(argIndex); |
Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2325 | if (!Arg) |
| 2326 | return false; |
| 2327 | |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2328 | QualType T = Arg->getType(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2329 | |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2330 | const analyze_printf::ArgType &AT = Amt.getArgType(S.Context); |
| 2331 | assert(AT.isValid()); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2332 | |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2333 | if (!AT.matchesType(S.Context, T)) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2334 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type) |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2335 | << k << AT.getRepresentativeTypeName(S.Context) |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2336 | << T << Arg->getSourceRange(), |
| 2337 | getLocationOfByte(Amt.getStart()), |
| 2338 | /*IsStringLocation*/true, |
| 2339 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2340 | // Don't do any more checking. We will just emit |
| 2341 | // spurious errors. |
| 2342 | return false; |
| 2343 | } |
| 2344 | } |
| 2345 | } |
| 2346 | return true; |
| 2347 | } |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2348 | |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2349 | void CheckPrintfHandler::HandleInvalidAmount( |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2350 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2351 | const analyze_printf::OptionalAmount &Amt, |
| 2352 | unsigned type, |
| 2353 | const char *startSpecifier, |
| 2354 | unsigned specifierLen) { |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2355 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 2356 | FS.getConversionSpecifier(); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2357 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2358 | FixItHint fixit = |
| 2359 | Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant |
| 2360 | ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), |
| 2361 | Amt.getConstantLength())) |
| 2362 | : FixItHint(); |
| 2363 | |
| 2364 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount) |
| 2365 | << type << CS.toString(), |
| 2366 | getLocationOfByte(Amt.getStart()), |
| 2367 | /*IsStringLocation*/true, |
| 2368 | getSpecifierRange(startSpecifier, specifierLen), |
| 2369 | fixit); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2372 | void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2373 | const analyze_printf::OptionalFlag &flag, |
| 2374 | const char *startSpecifier, |
| 2375 | unsigned specifierLen) { |
| 2376 | // Warn about pointless flag with a fixit removal. |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2377 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 2378 | FS.getConversionSpecifier(); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2379 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag) |
| 2380 | << flag.toString() << CS.toString(), |
| 2381 | getLocationOfByte(flag.getPosition()), |
| 2382 | /*IsStringLocation*/true, |
| 2383 | getSpecifierRange(startSpecifier, specifierLen), |
| 2384 | FixItHint::CreateRemoval( |
| 2385 | getSpecifierRange(flag.getPosition(), 1))); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | void CheckPrintfHandler::HandleIgnoredFlag( |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2389 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2390 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 2391 | const analyze_printf::OptionalFlag &flag, |
| 2392 | const char *startSpecifier, |
| 2393 | unsigned specifierLen) { |
| 2394 | // Warn about ignored flag with a fixit removal. |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2395 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag) |
| 2396 | << ignoredFlag.toString() << flag.toString(), |
| 2397 | getLocationOfByte(ignoredFlag.getPosition()), |
| 2398 | /*IsStringLocation*/true, |
| 2399 | getSpecifierRange(startSpecifier, specifierLen), |
| 2400 | FixItHint::CreateRemoval( |
| 2401 | getSpecifierRange(ignoredFlag.getPosition(), 1))); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2404 | // Determines if the specified is a C++ class or struct containing |
| 2405 | // a member with the specified name and kind (e.g. a CXXMethodDecl named |
| 2406 | // "c_str()"). |
| 2407 | template<typename MemberKind> |
| 2408 | static llvm::SmallPtrSet<MemberKind*, 1> |
| 2409 | CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) { |
| 2410 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 2411 | llvm::SmallPtrSet<MemberKind*, 1> Results; |
| 2412 | |
| 2413 | if (!RT) |
| 2414 | return Results; |
| 2415 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 2416 | if (!RD) |
| 2417 | return Results; |
| 2418 | |
| 2419 | LookupResult R(S, &S.PP.getIdentifierTable().get(Name), SourceLocation(), |
| 2420 | Sema::LookupMemberName); |
| 2421 | |
| 2422 | // We just need to include all members of the right kind turned up by the |
| 2423 | // filter, at this point. |
| 2424 | if (S.LookupQualifiedName(R, RT->getDecl())) |
| 2425 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2426 | NamedDecl *decl = (*I)->getUnderlyingDecl(); |
| 2427 | if (MemberKind *FK = dyn_cast<MemberKind>(decl)) |
| 2428 | Results.insert(FK); |
| 2429 | } |
| 2430 | return Results; |
| 2431 | } |
| 2432 | |
| 2433 | // 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] | 2434 | // better diagnostic if so. AT is assumed to be valid. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2435 | // Returns true when a c_str() conversion method is found. |
| 2436 | bool CheckPrintfHandler::checkForCStrMembers( |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2437 | const analyze_printf::ArgType &AT, const Expr *E, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2438 | const CharSourceRange &CSR) { |
| 2439 | typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet; |
| 2440 | |
| 2441 | MethodSet Results = |
| 2442 | CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType()); |
| 2443 | |
| 2444 | for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); |
| 2445 | MI != ME; ++MI) { |
| 2446 | const CXXMethodDecl *Method = *MI; |
| 2447 | if (Method->getNumParams() == 0 && |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2448 | AT.matchesType(S.Context, Method->getResultType())) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2449 | // FIXME: Suggest parens if the expression needs them. |
| 2450 | SourceLocation EndLoc = |
| 2451 | S.getPreprocessor().getLocForEndOfToken(E->getLocEnd()); |
| 2452 | S.Diag(E->getLocStart(), diag::note_printf_c_str) |
| 2453 | << "c_str()" |
| 2454 | << FixItHint::CreateInsertion(EndLoc, ".c_str()"); |
| 2455 | return true; |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | return false; |
| 2460 | } |
| 2461 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2462 | bool |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2463 | CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier |
Ted Kremenek | 5c41ee8 | 2010-02-11 09:27:41 +0000 | [diff] [blame] | 2464 | &FS, |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2465 | const char *startSpecifier, |
| 2466 | unsigned specifierLen) { |
| 2467 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2468 | using namespace analyze_format_string; |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2469 | using namespace analyze_printf; |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2470 | const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2471 | |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2472 | if (FS.consumesDataArgument()) { |
| 2473 | if (atFirstArg) { |
| 2474 | atFirstArg = false; |
| 2475 | usesPositionalArgs = FS.usesPositionalArg(); |
| 2476 | } |
| 2477 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2478 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), |
| 2479 | startSpecifier, specifierLen); |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2480 | return false; |
| 2481 | } |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2482 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2483 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2484 | // First check if the field width, precision, and conversion specifier |
| 2485 | // have matching data arguments. |
| 2486 | if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, |
| 2487 | startSpecifier, specifierLen)) { |
| 2488 | return false; |
| 2489 | } |
| 2490 | |
| 2491 | if (!HandleAmount(FS.getPrecision(), /* precision */ 1, |
| 2492 | startSpecifier, specifierLen)) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2493 | return false; |
| 2494 | } |
| 2495 | |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2496 | if (!CS.consumesDataArgument()) { |
| 2497 | // FIXME: Technically specifying a precision or field width here |
| 2498 | // makes no sense. Worth issuing a warning at some point. |
Ted Kremenek | 0e5675d | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 2499 | return true; |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2500 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2501 | |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2502 | // Consume the argument. |
| 2503 | unsigned argIndex = FS.getArgIndex(); |
Ted Kremenek | e3fc547 | 2010-02-27 08:34:51 +0000 | [diff] [blame] | 2504 | if (argIndex < NumDataArgs) { |
| 2505 | // The check to see if the argIndex is valid will come later. |
| 2506 | // We set the bit here because we may exit early from this |
| 2507 | // function if we encounter some other error. |
| 2508 | CoveredArgs.set(argIndex); |
| 2509 | } |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2510 | |
| 2511 | // Check for using an Objective-C specific conversion specifier |
| 2512 | // in a non-ObjC literal. |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2513 | if (!ObjCContext && CS.isObjCArg()) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2514 | return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, |
| 2515 | specifierLen); |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2516 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2517 | |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2518 | // Check for invalid use of field width |
| 2519 | if (!FS.hasValidFieldWidth()) { |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2520 | HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2521 | startSpecifier, specifierLen); |
| 2522 | } |
| 2523 | |
| 2524 | // Check for invalid use of precision |
| 2525 | if (!FS.hasValidPrecision()) { |
| 2526 | HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, |
| 2527 | startSpecifier, specifierLen); |
| 2528 | } |
| 2529 | |
| 2530 | // Check each flag does not conflict with any other component. |
Ted Kremenek | 65197b4 | 2011-01-08 05:28:46 +0000 | [diff] [blame] | 2531 | if (!FS.hasValidThousandsGroupingPrefix()) |
| 2532 | HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2533 | if (!FS.hasValidLeadingZeros()) |
| 2534 | HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); |
| 2535 | if (!FS.hasValidPlusPrefix()) |
| 2536 | HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2537 | if (!FS.hasValidSpacePrefix()) |
| 2538 | HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2539 | if (!FS.hasValidAlternativeForm()) |
| 2540 | HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); |
| 2541 | if (!FS.hasValidLeftJustified()) |
| 2542 | HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); |
| 2543 | |
| 2544 | // Check that flags are not ignored by another flag |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2545 | if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' |
| 2546 | HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), |
| 2547 | startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2548 | if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' |
| 2549 | HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), |
| 2550 | startSpecifier, specifierLen); |
| 2551 | |
| 2552 | // Check the length modifier is valid with the given conversion specifier. |
| 2553 | const LengthModifier &LM = FS.getLengthModifier(); |
| 2554 | if (!FS.hasValidLengthModifier()) |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2555 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_nonsensical_length) |
| 2556 | << LM.toString() << CS.toString(), |
| 2557 | getLocationOfByte(LM.getStart()), |
| 2558 | /*IsStringLocation*/true, |
| 2559 | getSpecifierRange(startSpecifier, specifierLen), |
| 2560 | FixItHint::CreateRemoval( |
| 2561 | getSpecifierRange(LM.getStart(), |
| 2562 | LM.getLength()))); |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2563 | if (!FS.hasStandardLengthModifier()) |
| 2564 | HandleNonStandardLengthModifier(LM, startSpecifier, specifierLen); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2565 | if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2566 | HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); |
| 2567 | if (!FS.hasStandardLengthConversionCombination()) |
| 2568 | HandleNonStandardConversionSpecification(LM, CS, startSpecifier, |
| 2569 | specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2570 | |
Ted Kremenek | da51f0d | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 2571 | // The remaining checks depend on the data arguments. |
| 2572 | if (HasVAListArg) |
| 2573 | return true; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2574 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2575 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | da51f0d | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 2576 | return false; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2577 | |
Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2578 | const Expr *Arg = getDataArg(argIndex); |
| 2579 | if (!Arg) |
| 2580 | return true; |
| 2581 | |
| 2582 | return checkFormatExpr(FS, startSpecifier, specifierLen, Arg); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | bool |
| 2586 | CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, |
| 2587 | const char *StartSpecifier, |
| 2588 | unsigned SpecifierLen, |
| 2589 | const Expr *E) { |
| 2590 | using namespace analyze_format_string; |
| 2591 | using namespace analyze_printf; |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2592 | // Now type check the data expression that matches the |
| 2593 | // format specifier. |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2594 | const analyze_printf::ArgType &AT = FS.getArgType(S.Context, |
| 2595 | ObjCContext); |
| 2596 | if (AT.isValid() && !AT.matchesType(S.Context, E->getType())) { |
Jordan Rose | ee0259d | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 2597 | // Look through argument promotions for our error message's reported type. |
| 2598 | // This includes the integral and floating promotions, but excludes array |
| 2599 | // and function pointer decay; seeing that an argument intended to be a |
| 2600 | // string has type 'char [6]' is probably more confusing than 'char *'. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2601 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
Jordan Rose | ee0259d | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 2602 | if (ICE->getCastKind() == CK_IntegralCast || |
| 2603 | ICE->getCastKind() == CK_FloatingCast) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2604 | E = ICE->getSubExpr(); |
Jordan Rose | ee0259d | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 2605 | |
| 2606 | // Check if we didn't match because of an implicit cast from a 'char' |
| 2607 | // or 'short' to an 'int'. This is done because printf is a varargs |
| 2608 | // function. |
| 2609 | if (ICE->getType() == S.Context.IntTy || |
| 2610 | ICE->getType() == S.Context.UnsignedIntTy) { |
| 2611 | // All further checking is done on the subexpression. |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2612 | if (AT.matchesType(S.Context, E->getType())) |
Jordan Rose | ee0259d | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 2613 | return true; |
| 2614 | } |
Ted Kremenek | 4d8ae4d | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 2615 | } |
Jordan Rose | ee0259d | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 2616 | } |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2617 | |
| 2618 | // We may be able to offer a FixItHint if it is a supported type. |
| 2619 | PrintfSpecifier fixedFS = FS; |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2620 | bool success = fixedFS.fixType(E->getType(), S.getLangOpts(), |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2621 | S.Context, ObjCContext); |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2622 | |
| 2623 | if (success) { |
| 2624 | // Get the fix string from the fixed format specifier |
Jordan Rose | ee0259d | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 2625 | SmallString<16> buf; |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2626 | llvm::raw_svector_ostream os(buf); |
| 2627 | fixedFS.toString(os); |
| 2628 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2629 | EmitFormatDiagnostic( |
| 2630 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2631 | << AT.getRepresentativeTypeName(S.Context) << E->getType() |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2632 | << E->getSourceRange(), |
| 2633 | E->getLocStart(), |
Matt Beaumont-Gay | abf145a | 2012-05-17 00:03:16 +0000 | [diff] [blame] | 2634 | /*IsStringLocation*/false, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2635 | getSpecifierRange(StartSpecifier, SpecifierLen), |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2636 | FixItHint::CreateReplacement( |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2637 | getSpecifierRange(StartSpecifier, SpecifierLen), |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2638 | os.str())); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2639 | } else { |
| 2640 | const CharSourceRange &CSR = getSpecifierRange(StartSpecifier, |
| 2641 | SpecifierLen); |
| 2642 | // Since the warning for passing non-POD types to variadic functions |
| 2643 | // was deferred until now, we emit a warning for non-POD |
| 2644 | // arguments here. |
| 2645 | if (S.isValidVarArgType(E->getType()) == Sema::VAK_Invalid) { |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2646 | unsigned DiagKind; |
| 2647 | if (E->getType()->isObjCObjectType()) |
| 2648 | DiagKind = diag::err_cannot_pass_objc_interface_to_vararg_format; |
| 2649 | else |
| 2650 | DiagKind = diag::warn_non_pod_vararg_with_format_string; |
| 2651 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2652 | EmitFormatDiagnostic( |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2653 | S.PDiag(DiagKind) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2654 | << S.getLangOpts().CPlusPlus0x |
| 2655 | << E->getType() |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2656 | << CallType |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2657 | << AT.getRepresentativeTypeName(S.Context) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2658 | << CSR |
| 2659 | << E->getSourceRange(), |
| 2660 | E->getLocStart(), /*IsStringLocation*/false, CSR); |
| 2661 | |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2662 | checkForCStrMembers(AT, E, CSR); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2663 | } else |
| 2664 | EmitFormatDiagnostic( |
| 2665 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2666 | << AT.getRepresentativeTypeName(S.Context) << E->getType() |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2667 | << CSR |
| 2668 | << E->getSourceRange(), |
| 2669 | E->getLocStart(), /*IsStringLocation*/false, CSR); |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2670 | } |
| 2671 | } |
| 2672 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2673 | return true; |
| 2674 | } |
| 2675 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2676 | //===--- CHECK: Scanf format string checking ------------------------------===// |
| 2677 | |
| 2678 | namespace { |
| 2679 | class CheckScanfHandler : public CheckFormatHandler { |
| 2680 | public: |
| 2681 | CheckScanfHandler(Sema &s, const StringLiteral *fexpr, |
| 2682 | const Expr *origFormatExpr, unsigned firstDataArg, |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2683 | unsigned numDataArgs, const char *beg, bool hasVAListArg, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2684 | Expr **Args, unsigned NumArgs, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2685 | unsigned formatIdx, bool inFunctionCall, |
| 2686 | Sema::VariadicCallType CallType) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2687 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2688 | numDataArgs, beg, hasVAListArg, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2689 | Args, NumArgs, formatIdx, inFunctionCall, CallType) |
| 2690 | {} |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2691 | |
| 2692 | bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, |
| 2693 | const char *startSpecifier, |
| 2694 | unsigned specifierLen); |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2695 | |
| 2696 | bool HandleInvalidScanfConversionSpecifier( |
| 2697 | const analyze_scanf::ScanfSpecifier &FS, |
| 2698 | const char *startSpecifier, |
| 2699 | unsigned specifierLen); |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2700 | |
| 2701 | void HandleIncompleteScanList(const char *start, const char *end); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2702 | }; |
Ted Kremenek | 07d161f | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 2703 | } |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2704 | |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2705 | void CheckScanfHandler::HandleIncompleteScanList(const char *start, |
| 2706 | const char *end) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2707 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete), |
| 2708 | getLocationOfByte(end), /*IsStringLocation*/true, |
| 2709 | getSpecifierRange(start, end - start)); |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2712 | bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( |
| 2713 | const analyze_scanf::ScanfSpecifier &FS, |
| 2714 | const char *startSpecifier, |
| 2715 | unsigned specifierLen) { |
| 2716 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2717 | const analyze_scanf::ScanfConversionSpecifier &CS = |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2718 | FS.getConversionSpecifier(); |
| 2719 | |
| 2720 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 2721 | getLocationOfByte(CS.getStart()), |
| 2722 | startSpecifier, specifierLen, |
| 2723 | CS.getStart(), CS.getLength()); |
| 2724 | } |
| 2725 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2726 | bool CheckScanfHandler::HandleScanfSpecifier( |
| 2727 | const analyze_scanf::ScanfSpecifier &FS, |
| 2728 | const char *startSpecifier, |
| 2729 | unsigned specifierLen) { |
| 2730 | |
| 2731 | using namespace analyze_scanf; |
| 2732 | using namespace analyze_format_string; |
| 2733 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2734 | const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2735 | |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2736 | // Handle case where '%' and '*' don't consume an argument. These shouldn't |
| 2737 | // be used to decide if we are using positional arguments consistently. |
| 2738 | if (FS.consumesDataArgument()) { |
| 2739 | if (atFirstArg) { |
| 2740 | atFirstArg = false; |
| 2741 | usesPositionalArgs = FS.usesPositionalArg(); |
| 2742 | } |
| 2743 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2744 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), |
| 2745 | startSpecifier, specifierLen); |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2746 | return false; |
| 2747 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2748 | } |
| 2749 | |
| 2750 | // Check if the field with is non-zero. |
| 2751 | const OptionalAmount &Amt = FS.getFieldWidth(); |
| 2752 | if (Amt.getHowSpecified() == OptionalAmount::Constant) { |
| 2753 | if (Amt.getConstantAmount() == 0) { |
| 2754 | const CharSourceRange &R = getSpecifierRange(Amt.getStart(), |
| 2755 | Amt.getConstantLength()); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2756 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width), |
| 2757 | getLocationOfByte(Amt.getStart()), |
| 2758 | /*IsStringLocation*/true, R, |
| 2759 | FixItHint::CreateRemoval(R)); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2760 | } |
| 2761 | } |
| 2762 | |
| 2763 | if (!FS.consumesDataArgument()) { |
| 2764 | // FIXME: Technically specifying a precision or field width here |
| 2765 | // makes no sense. Worth issuing a warning at some point. |
| 2766 | return true; |
| 2767 | } |
| 2768 | |
| 2769 | // Consume the argument. |
| 2770 | unsigned argIndex = FS.getArgIndex(); |
| 2771 | if (argIndex < NumDataArgs) { |
| 2772 | // The check to see if the argIndex is valid will come later. |
| 2773 | // We set the bit here because we may exit early from this |
| 2774 | // function if we encounter some other error. |
| 2775 | CoveredArgs.set(argIndex); |
| 2776 | } |
| 2777 | |
Ted Kremenek | 1e51c20 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 2778 | // Check the length modifier is valid with the given conversion specifier. |
| 2779 | const LengthModifier &LM = FS.getLengthModifier(); |
| 2780 | if (!FS.hasValidLengthModifier()) { |
Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 2781 | const CharSourceRange &R = getSpecifierRange(LM.getStart(), LM.getLength()); |
| 2782 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_nonsensical_length) |
| 2783 | << LM.toString() << CS.toString() |
| 2784 | << getSpecifierRange(startSpecifier, specifierLen), |
| 2785 | getLocationOfByte(LM.getStart()), |
| 2786 | /*IsStringLocation*/true, R, |
| 2787 | FixItHint::CreateRemoval(R)); |
Ted Kremenek | 1e51c20 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2790 | if (!FS.hasStandardLengthModifier()) |
| 2791 | HandleNonStandardLengthModifier(LM, startSpecifier, specifierLen); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2792 | if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2793 | HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); |
| 2794 | if (!FS.hasStandardLengthConversionCombination()) |
| 2795 | HandleNonStandardConversionSpecification(LM, CS, startSpecifier, |
| 2796 | specifierLen); |
| 2797 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2798 | // The remaining checks depend on the data arguments. |
| 2799 | if (HasVAListArg) |
| 2800 | return true; |
| 2801 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2802 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2803 | return false; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2804 | |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2805 | // Check that the argument type matches the format specifier. |
| 2806 | const Expr *Ex = getDataArg(argIndex); |
Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2807 | if (!Ex) |
| 2808 | return true; |
| 2809 | |
Hans Wennborg | 58e1e54 | 2012-08-07 08:59:46 +0000 | [diff] [blame] | 2810 | const analyze_format_string::ArgType &AT = FS.getArgType(S.Context); |
| 2811 | if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType())) { |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2812 | ScanfSpecifier fixedFS = FS; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2813 | bool success = fixedFS.fixType(Ex->getType(), S.getLangOpts(), |
Hans Wennborg | be6126a | 2012-02-15 09:59:46 +0000 | [diff] [blame] | 2814 | S.Context); |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2815 | |
| 2816 | if (success) { |
| 2817 | // Get the fix string from the fixed format specifier. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2818 | SmallString<128> buf; |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2819 | llvm::raw_svector_ostream os(buf); |
| 2820 | fixedFS.toString(os); |
| 2821 | |
| 2822 | EmitFormatDiagnostic( |
| 2823 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
Hans Wennborg | 58e1e54 | 2012-08-07 08:59:46 +0000 | [diff] [blame] | 2824 | << AT.getRepresentativeTypeName(S.Context) << Ex->getType() |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2825 | << Ex->getSourceRange(), |
Matt Beaumont-Gay | abf145a | 2012-05-17 00:03:16 +0000 | [diff] [blame] | 2826 | Ex->getLocStart(), |
| 2827 | /*IsStringLocation*/false, |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2828 | getSpecifierRange(startSpecifier, specifierLen), |
| 2829 | FixItHint::CreateReplacement( |
| 2830 | getSpecifierRange(startSpecifier, specifierLen), |
| 2831 | os.str())); |
| 2832 | } else { |
Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 2833 | EmitFormatDiagnostic( |
| 2834 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
Hans Wennborg | 58e1e54 | 2012-08-07 08:59:46 +0000 | [diff] [blame] | 2835 | << AT.getRepresentativeTypeName(S.Context) << Ex->getType() |
Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 2836 | << Ex->getSourceRange(), |
Matt Beaumont-Gay | abf145a | 2012-05-17 00:03:16 +0000 | [diff] [blame] | 2837 | Ex->getLocStart(), |
| 2838 | /*IsStringLocation*/false, |
Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 2839 | getSpecifierRange(startSpecifier, specifierLen)); |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2840 | } |
| 2841 | } |
| 2842 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2843 | return true; |
| 2844 | } |
| 2845 | |
| 2846 | void Sema::CheckFormatString(const StringLiteral *FExpr, |
Ted Kremenek | 0e5675d | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 2847 | const Expr *OrigFormatExpr, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2848 | Expr **Args, unsigned NumArgs, |
| 2849 | bool HasVAListArg, unsigned format_idx, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2850 | unsigned firstDataArg, FormatStringType Type, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2851 | bool inFunctionCall, VariadicCallType CallType) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2852 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2853 | // CHECK: is the format string a wide literal? |
Richard Smith | df9ef1b | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 2854 | if (!FExpr->isAscii() && !FExpr->isUTF8()) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2855 | CheckFormatHandler::EmitFormatDiagnostic( |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2856 | *this, inFunctionCall, Args[format_idx], |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2857 | PDiag(diag::warn_format_string_is_wide_literal), FExpr->getLocStart(), |
| 2858 | /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2859 | return; |
| 2860 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2861 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2862 | // Str - The format string. NOTE: this is NOT null-terminated! |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2863 | StringRef StrRef = FExpr->getString(); |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 2864 | const char *Str = StrRef.data(); |
| 2865 | unsigned StrLen = StrRef.size(); |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2866 | const unsigned numDataArgs = NumArgs - firstDataArg; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2867 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2868 | // CHECK: empty format string? |
Ted Kremenek | 4cd5791 | 2011-09-29 05:52:16 +0000 | [diff] [blame] | 2869 | if (StrLen == 0 && numDataArgs > 0) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2870 | CheckFormatHandler::EmitFormatDiagnostic( |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2871 | *this, inFunctionCall, Args[format_idx], |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2872 | PDiag(diag::warn_empty_format_string), FExpr->getLocStart(), |
| 2873 | /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2874 | return; |
| 2875 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2876 | |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2877 | if (Type == FST_Printf || Type == FST_NSString) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2878 | CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2879 | numDataArgs, (Type == FST_NSString), |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2880 | Str, HasVAListArg, Args, NumArgs, format_idx, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2881 | inFunctionCall, CallType); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2882 | |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 2883 | if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2884 | getLangOpts())) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2885 | H.DoneProcessing(); |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2886 | } else if (Type == FST_Scanf) { |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2887 | CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, numDataArgs, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2888 | Str, HasVAListArg, Args, NumArgs, format_idx, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2889 | inFunctionCall, CallType); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2890 | |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 2891 | if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2892 | getLangOpts())) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2893 | H.DoneProcessing(); |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2894 | } // TODO: handle other formats |
Ted Kremenek | ce7024e | 2010-01-28 01:18:22 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2897 | //===--- CHECK: Standard memory functions ---------------------------------===// |
| 2898 | |
Douglas Gregor | 2a053a3 | 2011-05-03 20:05:22 +0000 | [diff] [blame] | 2899 | /// \brief Determine whether the given type is a dynamic class type (e.g., |
| 2900 | /// whether it has a vtable). |
| 2901 | static bool isDynamicClassType(QualType T) { |
| 2902 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
| 2903 | if (CXXRecordDecl *Definition = Record->getDefinition()) |
| 2904 | if (Definition->isDynamicClass()) |
| 2905 | return true; |
| 2906 | |
| 2907 | return false; |
| 2908 | } |
| 2909 | |
Chandler Carruth | a72a12f | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 2910 | /// \brief If E is a sizeof expression, returns its argument expression, |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2911 | /// otherwise returns NULL. |
| 2912 | static const Expr *getSizeOfExprArg(const Expr* E) { |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2913 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2914 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
| 2915 | if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType()) |
| 2916 | return SizeOf->getArgumentExpr()->IgnoreParenImpCasts(); |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2917 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2918 | return 0; |
| 2919 | } |
| 2920 | |
Chandler Carruth | a72a12f | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 2921 | /// \brief If E is a sizeof expression, returns its argument type. |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2922 | static QualType getSizeOfArgType(const Expr* E) { |
| 2923 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
| 2924 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
| 2925 | if (SizeOf->getKind() == clang::UETT_SizeOf) |
| 2926 | return SizeOf->getTypeOfArgument(); |
| 2927 | |
| 2928 | return QualType(); |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2931 | /// \brief Check for dangerous or invalid arguments to memset(). |
| 2932 | /// |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 2933 | /// This issues warnings on known problematic, dangerous or unspecified |
Matt Beaumont-Gay | cc2f30c | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 2934 | /// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp' |
| 2935 | /// function calls. |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2936 | /// |
| 2937 | /// \param Call The call expression to diagnose. |
Matt Beaumont-Gay | cc2f30c | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 2938 | void Sema::CheckMemaccessArguments(const CallExpr *Call, |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2939 | unsigned BId, |
Matt Beaumont-Gay | cc2f30c | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 2940 | IdentifierInfo *FnName) { |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2941 | assert(BId != 0); |
| 2942 | |
Ted Kremenek | 1d59f7f | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 2943 | // It is possible to have a non-standard definition of memset. Validate |
Douglas Gregor | 707a23e | 2011-06-16 17:56:04 +0000 | [diff] [blame] | 2944 | // we have enough arguments, and if not, abort further checking. |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2945 | unsigned ExpectedNumArgs = (BId == Builtin::BIstrndup ? 2 : 3); |
Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2946 | if (Call->getNumArgs() < ExpectedNumArgs) |
Ted Kremenek | 1d59f7f | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 2947 | return; |
| 2948 | |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2949 | unsigned LastArg = (BId == Builtin::BImemset || |
| 2950 | BId == Builtin::BIstrndup ? 1 : 2); |
| 2951 | unsigned LenArg = (BId == Builtin::BIstrndup ? 1 : 2); |
Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2952 | const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts(); |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2953 | |
| 2954 | // We have special checking when the length is a sizeof expression. |
| 2955 | QualType SizeOfArgTy = getSizeOfArgType(LenExpr); |
| 2956 | const Expr *SizeOfArg = getSizeOfExprArg(LenExpr); |
| 2957 | llvm::FoldingSetNodeID SizeOfArgID; |
| 2958 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2959 | for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) { |
| 2960 | const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts(); |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2961 | SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange(); |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2962 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2963 | QualType DestTy = Dest->getType(); |
| 2964 | if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) { |
| 2965 | QualType PointeeTy = DestPtrTy->getPointeeType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2966 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2967 | // Never warn about void type pointers. This can be used to suppress |
| 2968 | // false positives. |
| 2969 | if (PointeeTy->isVoidType()) |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2970 | continue; |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2971 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2972 | // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by |
| 2973 | // actually comparing the expressions for equality. Because computing the |
| 2974 | // expression IDs can be expensive, we only do this if the diagnostic is |
| 2975 | // enabled. |
| 2976 | if (SizeOfArg && |
| 2977 | Diags.getDiagnosticLevel(diag::warn_sizeof_pointer_expr_memaccess, |
| 2978 | SizeOfArg->getExprLoc())) { |
| 2979 | // We only compute IDs for expressions if the warning is enabled, and |
| 2980 | // cache the sizeof arg's ID. |
| 2981 | if (SizeOfArgID == llvm::FoldingSetNodeID()) |
| 2982 | SizeOfArg->Profile(SizeOfArgID, Context, true); |
| 2983 | llvm::FoldingSetNodeID DestID; |
| 2984 | Dest->Profile(DestID, Context, true); |
| 2985 | if (DestID == SizeOfArgID) { |
Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2986 | // TODO: For strncpy() and friends, this could suggest sizeof(dst) |
| 2987 | // over sizeof(src) as well. |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2988 | unsigned ActionIdx = 0; // Default is to suggest dereferencing. |
Anna Zaks | 6fcb372 | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 2989 | StringRef ReadableName = FnName->getName(); |
| 2990 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2991 | if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest)) |
Anna Zaks | 90c7832 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 2992 | if (UnaryOp->getOpcode() == UO_AddrOf) |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2993 | ActionIdx = 1; // If its an address-of operator, just remove it. |
| 2994 | if (Context.getTypeSize(PointeeTy) == Context.getCharWidth()) |
| 2995 | ActionIdx = 2; // If the pointee's size is sizeof(char), |
| 2996 | // suggest an explicit length. |
Anna Zaks | 6fcb372 | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 2997 | |
| 2998 | // If the function is defined as a builtin macro, do not show macro |
| 2999 | // expansion. |
| 3000 | SourceLocation SL = SizeOfArg->getExprLoc(); |
| 3001 | SourceRange DSR = Dest->getSourceRange(); |
| 3002 | SourceRange SSR = SizeOfArg->getSourceRange(); |
| 3003 | SourceManager &SM = PP.getSourceManager(); |
| 3004 | |
| 3005 | if (SM.isMacroArgExpansion(SL)) { |
| 3006 | ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts); |
| 3007 | SL = SM.getSpellingLoc(SL); |
| 3008 | DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()), |
| 3009 | SM.getSpellingLoc(DSR.getEnd())); |
| 3010 | SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()), |
| 3011 | SM.getSpellingLoc(SSR.getEnd())); |
| 3012 | } |
| 3013 | |
Anna Zaks | 90c7832 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 3014 | DiagRuntimeBehavior(SL, SizeOfArg, |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3015 | PDiag(diag::warn_sizeof_pointer_expr_memaccess) |
Anna Zaks | 6fcb372 | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 3016 | << ReadableName |
Anna Zaks | 90c7832 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 3017 | << PointeeTy |
| 3018 | << DestTy |
Anna Zaks | 6fcb372 | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 3019 | << DSR |
Anna Zaks | 90c7832 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 3020 | << SSR); |
| 3021 | DiagRuntimeBehavior(SL, SizeOfArg, |
| 3022 | PDiag(diag::warn_sizeof_pointer_expr_memaccess_note) |
| 3023 | << ActionIdx |
| 3024 | << SSR); |
| 3025 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3026 | break; |
| 3027 | } |
| 3028 | } |
| 3029 | |
| 3030 | // Also check for cases where the sizeof argument is the exact same |
| 3031 | // type as the memory argument, and where it points to a user-defined |
| 3032 | // record type. |
| 3033 | if (SizeOfArgTy != QualType()) { |
| 3034 | if (PointeeTy->isRecordType() && |
| 3035 | Context.typesAreCompatible(SizeOfArgTy, DestTy)) { |
| 3036 | DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest, |
| 3037 | PDiag(diag::warn_sizeof_pointer_type_memaccess) |
| 3038 | << FnName << SizeOfArgTy << ArgIdx |
| 3039 | << PointeeTy << Dest->getSourceRange() |
| 3040 | << LenExpr->getSourceRange()); |
| 3041 | break; |
| 3042 | } |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 3043 | } |
| 3044 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 3045 | // Always complain about dynamic classes. |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3046 | if (isDynamicClassType(PointeeTy)) { |
| 3047 | |
| 3048 | unsigned OperationType = 0; |
| 3049 | // "overwritten" if we're warning about the destination for any call |
| 3050 | // but memcmp; otherwise a verb appropriate to the call. |
| 3051 | if (ArgIdx != 0 || BId == Builtin::BImemcmp) { |
| 3052 | if (BId == Builtin::BImemcpy) |
| 3053 | OperationType = 1; |
| 3054 | else if(BId == Builtin::BImemmove) |
| 3055 | OperationType = 2; |
| 3056 | else if (BId == Builtin::BImemcmp) |
| 3057 | OperationType = 3; |
| 3058 | } |
| 3059 | |
Matt Beaumont-Gay | 5c5218e | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 3060 | DiagRuntimeBehavior( |
| 3061 | Dest->getExprLoc(), Dest, |
| 3062 | PDiag(diag::warn_dyn_class_memaccess) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3063 | << (BId == Builtin::BImemcmp ? ArgIdx + 2 : ArgIdx) |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3064 | << FnName << PointeeTy |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3065 | << OperationType |
Matt Beaumont-Gay | 5c5218e | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 3066 | << Call->getCallee()->getSourceRange()); |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3067 | } else if (PointeeTy.hasNonTrivialObjCLifetime() && |
| 3068 | BId != Builtin::BImemset) |
Matt Beaumont-Gay | 5c5218e | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 3069 | DiagRuntimeBehavior( |
| 3070 | Dest->getExprLoc(), Dest, |
| 3071 | PDiag(diag::warn_arc_object_memaccess) |
| 3072 | << ArgIdx << FnName << PointeeTy |
| 3073 | << Call->getCallee()->getSourceRange()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3074 | else |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 3075 | continue; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3076 | |
| 3077 | DiagRuntimeBehavior( |
| 3078 | Dest->getExprLoc(), Dest, |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 3079 | PDiag(diag::note_bad_memaccess_silence) |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 3080 | << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)")); |
| 3081 | break; |
| 3082 | } |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 3083 | } |
| 3084 | } |
| 3085 | |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 3086 | // A little helper routine: ignore addition and subtraction of integer literals. |
| 3087 | // This intentionally does not ignore all integer constant expressions because |
| 3088 | // we don't want to remove sizeof(). |
| 3089 | static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) { |
| 3090 | Ex = Ex->IgnoreParenCasts(); |
| 3091 | |
| 3092 | for (;;) { |
| 3093 | const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex); |
| 3094 | if (!BO || !BO->isAdditiveOp()) |
| 3095 | break; |
| 3096 | |
| 3097 | const Expr *RHS = BO->getRHS()->IgnoreParenCasts(); |
| 3098 | const Expr *LHS = BO->getLHS()->IgnoreParenCasts(); |
| 3099 | |
| 3100 | if (isa<IntegerLiteral>(RHS)) |
| 3101 | Ex = LHS; |
| 3102 | else if (isa<IntegerLiteral>(LHS)) |
| 3103 | Ex = RHS; |
| 3104 | else |
| 3105 | break; |
| 3106 | } |
| 3107 | |
| 3108 | return Ex; |
| 3109 | } |
| 3110 | |
Anna Zaks | 0f38ace | 2012-08-08 21:42:23 +0000 | [diff] [blame] | 3111 | static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty, |
| 3112 | ASTContext &Context) { |
| 3113 | // Only handle constant-sized or VLAs, but not flexible members. |
| 3114 | if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) { |
| 3115 | // Only issue the FIXIT for arrays of size > 1. |
| 3116 | if (CAT->getSize().getSExtValue() <= 1) |
| 3117 | return false; |
| 3118 | } else if (!Ty->isVariableArrayType()) { |
| 3119 | return false; |
| 3120 | } |
| 3121 | return true; |
| 3122 | } |
| 3123 | |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 3124 | // Warn if the user has made the 'size' argument to strlcpy or strlcat |
| 3125 | // be the size of the source, instead of the destination. |
| 3126 | void Sema::CheckStrlcpycatArguments(const CallExpr *Call, |
| 3127 | IdentifierInfo *FnName) { |
| 3128 | |
| 3129 | // Don't crash if the user has the wrong number of arguments |
| 3130 | if (Call->getNumArgs() != 3) |
| 3131 | return; |
| 3132 | |
| 3133 | const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context); |
| 3134 | const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context); |
| 3135 | const Expr *CompareWithSrc = NULL; |
| 3136 | |
| 3137 | // Look for 'strlcpy(dst, x, sizeof(x))' |
| 3138 | if (const Expr *Ex = getSizeOfExprArg(SizeArg)) |
| 3139 | CompareWithSrc = Ex; |
| 3140 | else { |
| 3141 | // Look for 'strlcpy(dst, x, strlen(x))' |
| 3142 | if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3143 | if (SizeCall->isBuiltinCall() == Builtin::BIstrlen |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 3144 | && SizeCall->getNumArgs() == 1) |
| 3145 | CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context); |
| 3146 | } |
| 3147 | } |
| 3148 | |
| 3149 | if (!CompareWithSrc) |
| 3150 | return; |
| 3151 | |
| 3152 | // Determine if the argument to sizeof/strlen is equal to the source |
| 3153 | // argument. In principle there's all kinds of things you could do |
| 3154 | // here, for instance creating an == expression and evaluating it with |
| 3155 | // EvaluateAsBooleanCondition, but this uses a more direct technique: |
| 3156 | const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg); |
| 3157 | if (!SrcArgDRE) |
| 3158 | return; |
| 3159 | |
| 3160 | const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc); |
| 3161 | if (!CompareWithSrcDRE || |
| 3162 | SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl()) |
| 3163 | return; |
| 3164 | |
| 3165 | const Expr *OriginalSizeArg = Call->getArg(2); |
| 3166 | Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size) |
| 3167 | << OriginalSizeArg->getSourceRange() << FnName; |
| 3168 | |
| 3169 | // Output a FIXIT hint if the destination is an array (rather than a |
| 3170 | // pointer to an array). This could be enhanced to handle some |
| 3171 | // pointers if we know the actual size, like if DstArg is 'array+2' |
| 3172 | // we could say 'sizeof(array)-2'. |
| 3173 | const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts(); |
Anna Zaks | 0f38ace | 2012-08-08 21:42:23 +0000 | [diff] [blame] | 3174 | if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context)) |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 3175 | return; |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 3176 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3177 | SmallString<128> sizeString; |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 3178 | llvm::raw_svector_ostream OS(sizeString); |
| 3179 | OS << "sizeof("; |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 3180 | DstArg->printPretty(OS, 0, getPrintingPolicy()); |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 3181 | OS << ")"; |
| 3182 | |
| 3183 | Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size) |
| 3184 | << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(), |
| 3185 | OS.str()); |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 3186 | } |
| 3187 | |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3188 | /// Check if two expressions refer to the same declaration. |
| 3189 | static bool referToTheSameDecl(const Expr *E1, const Expr *E2) { |
| 3190 | if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1)) |
| 3191 | if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2)) |
| 3192 | return D1->getDecl() == D2->getDecl(); |
| 3193 | return false; |
| 3194 | } |
| 3195 | |
| 3196 | static const Expr *getStrlenExprArg(const Expr *E) { |
| 3197 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { |
| 3198 | const FunctionDecl *FD = CE->getDirectCallee(); |
| 3199 | if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen) |
| 3200 | return 0; |
| 3201 | return CE->getArg(0)->IgnoreParenCasts(); |
| 3202 | } |
| 3203 | return 0; |
| 3204 | } |
| 3205 | |
| 3206 | // Warn on anti-patterns as the 'size' argument to strncat. |
| 3207 | // The correct size argument should look like following: |
| 3208 | // strncat(dst, src, sizeof(dst) - strlen(dest) - 1); |
| 3209 | void Sema::CheckStrncatArguments(const CallExpr *CE, |
| 3210 | IdentifierInfo *FnName) { |
| 3211 | // Don't crash if the user has the wrong number of arguments. |
| 3212 | if (CE->getNumArgs() < 3) |
| 3213 | return; |
| 3214 | const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts(); |
| 3215 | const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts(); |
| 3216 | const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts(); |
| 3217 | |
| 3218 | // Identify common expressions, which are wrongly used as the size argument |
| 3219 | // to strncat and may lead to buffer overflows. |
| 3220 | unsigned PatternType = 0; |
| 3221 | if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) { |
| 3222 | // - sizeof(dst) |
| 3223 | if (referToTheSameDecl(SizeOfArg, DstArg)) |
| 3224 | PatternType = 1; |
| 3225 | // - sizeof(src) |
| 3226 | else if (referToTheSameDecl(SizeOfArg, SrcArg)) |
| 3227 | PatternType = 2; |
| 3228 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) { |
| 3229 | if (BE->getOpcode() == BO_Sub) { |
| 3230 | const Expr *L = BE->getLHS()->IgnoreParenCasts(); |
| 3231 | const Expr *R = BE->getRHS()->IgnoreParenCasts(); |
| 3232 | // - sizeof(dst) - strlen(dst) |
| 3233 | if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) && |
| 3234 | referToTheSameDecl(DstArg, getStrlenExprArg(R))) |
| 3235 | PatternType = 1; |
| 3236 | // - sizeof(src) - (anything) |
| 3237 | else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L))) |
| 3238 | PatternType = 2; |
| 3239 | } |
| 3240 | } |
| 3241 | |
| 3242 | if (PatternType == 0) |
| 3243 | return; |
| 3244 | |
Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 3245 | // Generate the diagnostic. |
| 3246 | SourceLocation SL = LenArg->getLocStart(); |
| 3247 | SourceRange SR = LenArg->getSourceRange(); |
| 3248 | SourceManager &SM = PP.getSourceManager(); |
| 3249 | |
| 3250 | // If the function is defined as a builtin macro, do not show macro expansion. |
| 3251 | if (SM.isMacroArgExpansion(SL)) { |
| 3252 | SL = SM.getSpellingLoc(SL); |
| 3253 | SR = SourceRange(SM.getSpellingLoc(SR.getBegin()), |
| 3254 | SM.getSpellingLoc(SR.getEnd())); |
| 3255 | } |
| 3256 | |
Anna Zaks | 0f38ace | 2012-08-08 21:42:23 +0000 | [diff] [blame] | 3257 | // Check if the destination is an array (rather than a pointer to an array). |
| 3258 | QualType DstTy = DstArg->getType(); |
| 3259 | bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy, |
| 3260 | Context); |
| 3261 | if (!isKnownSizeArray) { |
| 3262 | if (PatternType == 1) |
| 3263 | Diag(SL, diag::warn_strncat_wrong_size) << SR; |
| 3264 | else |
| 3265 | Diag(SL, diag::warn_strncat_src_size) << SR; |
| 3266 | return; |
| 3267 | } |
| 3268 | |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3269 | if (PatternType == 1) |
Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 3270 | Diag(SL, diag::warn_strncat_large_size) << SR; |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3271 | else |
Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 3272 | Diag(SL, diag::warn_strncat_src_size) << SR; |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3273 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3274 | SmallString<128> sizeString; |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3275 | llvm::raw_svector_ostream OS(sizeString); |
| 3276 | OS << "sizeof("; |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 3277 | DstArg->printPretty(OS, 0, getPrintingPolicy()); |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3278 | OS << ") - "; |
| 3279 | OS << "strlen("; |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 3280 | DstArg->printPretty(OS, 0, getPrintingPolicy()); |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3281 | OS << ") - 1"; |
| 3282 | |
Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 3283 | Diag(SL, diag::note_strncat_wrong_size) |
| 3284 | << FixItHint::CreateReplacement(SR, OS.str()); |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3287 | //===--- CHECK: Return Address of Stack Variable --------------------------===// |
| 3288 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3289 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 3290 | Decl *ParentDecl); |
| 3291 | static Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 3292 | Decl *ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3293 | |
| 3294 | /// CheckReturnStackAddr - Check if a return statement returns the address |
| 3295 | /// of a stack variable. |
| 3296 | void |
| 3297 | Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, |
| 3298 | SourceLocation ReturnLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3299 | |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3300 | Expr *stackE = 0; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3301 | SmallVector<DeclRefExpr *, 8> refVars; |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3302 | |
| 3303 | // Perform checking for returned stack addresses, local blocks, |
| 3304 | // label addresses or references to temporaries. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3305 | if (lhsType->isPointerType() || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3306 | (!getLangOpts().ObjCAutoRefCount && lhsType->isBlockPointerType())) { |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3307 | stackE = EvalAddr(RetValExp, refVars, /*ParentDecl=*/0); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3308 | } else if (lhsType->isReferenceType()) { |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3309 | stackE = EvalVal(RetValExp, refVars, /*ParentDecl=*/0); |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | if (stackE == 0) |
| 3313 | return; // Nothing suspicious was found. |
| 3314 | |
| 3315 | SourceLocation diagLoc; |
| 3316 | SourceRange diagRange; |
| 3317 | if (refVars.empty()) { |
| 3318 | diagLoc = stackE->getLocStart(); |
| 3319 | diagRange = stackE->getSourceRange(); |
| 3320 | } else { |
| 3321 | // We followed through a reference variable. 'stackE' contains the |
| 3322 | // problematic expression but we will warn at the return statement pointing |
| 3323 | // at the reference variable. We will later display the "trail" of |
| 3324 | // reference variables using notes. |
| 3325 | diagLoc = refVars[0]->getLocStart(); |
| 3326 | diagRange = refVars[0]->getSourceRange(); |
| 3327 | } |
| 3328 | |
| 3329 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var. |
| 3330 | Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref |
| 3331 | : diag::warn_ret_stack_addr) |
| 3332 | << DR->getDecl()->getDeclName() << diagRange; |
| 3333 | } else if (isa<BlockExpr>(stackE)) { // local block. |
| 3334 | Diag(diagLoc, diag::err_ret_local_block) << diagRange; |
| 3335 | } else if (isa<AddrLabelExpr>(stackE)) { // address of label. |
| 3336 | Diag(diagLoc, diag::warn_ret_addr_label) << diagRange; |
| 3337 | } else { // local temporary. |
| 3338 | Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref |
| 3339 | : diag::warn_ret_local_temp_addr) |
| 3340 | << diagRange; |
| 3341 | } |
| 3342 | |
| 3343 | // Display the "trail" of reference variables that we followed until we |
| 3344 | // found the problematic expression using notes. |
| 3345 | for (unsigned i = 0, e = refVars.size(); i != e; ++i) { |
| 3346 | VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl()); |
| 3347 | // If this var binds to another reference var, show the range of the next |
| 3348 | // var, otherwise the var binds to the problematic expression, in which case |
| 3349 | // show the range of the expression. |
| 3350 | SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange() |
| 3351 | : stackE->getSourceRange(); |
| 3352 | Diag(VD->getLocation(), diag::note_ref_var_local_bind) |
| 3353 | << VD->getDeclName() << range; |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3354 | } |
| 3355 | } |
| 3356 | |
| 3357 | /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that |
| 3358 | /// check if the expression in a return statement evaluates to an address |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3359 | /// to a location on the stack, a local block, an address of a label, or a |
| 3360 | /// reference to local temporary. The recursion is used to traverse the |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3361 | /// AST of the return expression, with recursion backtracking when we |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3362 | /// encounter a subexpression that (1) clearly does not lead to one of the |
| 3363 | /// above problematic expressions (2) is something we cannot determine leads to |
| 3364 | /// a problematic expression based on such local checking. |
| 3365 | /// |
| 3366 | /// Both EvalAddr and EvalVal follow through reference variables to evaluate |
| 3367 | /// the expression that they point to. Such variables are added to the |
| 3368 | /// 'refVars' vector so that we know what the reference variable "trail" was. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3369 | /// |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 3370 | /// EvalAddr processes expressions that are pointers that are used as |
| 3371 | /// references (and not L-values). EvalVal handles all other values. |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3372 | /// At the base case of the recursion is a check for the above problematic |
| 3373 | /// expressions. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3374 | /// |
| 3375 | /// This implementation handles: |
| 3376 | /// |
| 3377 | /// * pointer-to-pointer casts |
| 3378 | /// * implicit conversions from array references to pointers |
| 3379 | /// * taking the address of fields |
| 3380 | /// * arbitrary interplay between "&" and "*" operators |
| 3381 | /// * pointer arithmetic from an address of a stack variable |
| 3382 | /// * 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] | 3383 | static Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 3384 | Decl *ParentDecl) { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3385 | if (E->isTypeDependent()) |
| 3386 | return NULL; |
| 3387 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3388 | // We should only be called for evaluating pointer expressions. |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3389 | assert((E->getType()->isAnyPointerType() || |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3390 | E->getType()->isBlockPointerType() || |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3391 | E->getType()->isObjCQualifiedIdType()) && |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3392 | "EvalAddr only works on pointers"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3393 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3394 | E = E->IgnoreParens(); |
| 3395 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3396 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 3397 | // viewed AST node. We then recursively traverse the AST by calling |
| 3398 | // EvalAddr and EvalVal appropriately. |
| 3399 | switch (E->getStmtClass()) { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3400 | case Stmt::DeclRefExprClass: { |
| 3401 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
| 3402 | |
| 3403 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
| 3404 | // If this is a reference variable, follow through to the expression that |
| 3405 | // it points to. |
| 3406 | if (V->hasLocalStorage() && |
| 3407 | V->getType()->isReferenceType() && V->hasInit()) { |
| 3408 | // Add the reference variable to the "trail". |
| 3409 | refVars.push_back(DR); |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3410 | return EvalAddr(V->getInit(), refVars, ParentDecl); |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
| 3413 | return NULL; |
| 3414 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3415 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3416 | case Stmt::UnaryOperatorClass: { |
| 3417 | // The only unary operator that make sense to handle here |
| 3418 | // is AddrOf. All others don't make sense as pointers. |
| 3419 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3420 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3421 | if (U->getOpcode() == UO_AddrOf) |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3422 | return EvalVal(U->getSubExpr(), refVars, ParentDecl); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3423 | else |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3424 | return NULL; |
| 3425 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3426 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3427 | case Stmt::BinaryOperatorClass: { |
| 3428 | // Handle pointer arithmetic. All other binary operators are not valid |
| 3429 | // in this context. |
| 3430 | BinaryOperator *B = cast<BinaryOperator>(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3431 | BinaryOperatorKind op = B->getOpcode(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3432 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3433 | if (op != BO_Add && op != BO_Sub) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3434 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3435 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3436 | Expr *Base = B->getLHS(); |
| 3437 | |
| 3438 | // Determine which argument is the real pointer base. It could be |
| 3439 | // the RHS argument instead of the LHS. |
| 3440 | if (!Base->getType()->isPointerType()) Base = B->getRHS(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3441 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3442 | assert (Base->getType()->isPointerType()); |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3443 | return EvalAddr(Base, refVars, ParentDecl); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3444 | } |
Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 3445 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3446 | // For conditional operators we need to see if either the LHS or RHS are |
| 3447 | // valid DeclRefExpr*s. If one of them is valid, we return it. |
| 3448 | case Stmt::ConditionalOperatorClass: { |
| 3449 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3450 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3451 | // Handle the GNU extension for missing LHS. |
Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 3452 | if (Expr *lhsExpr = C->getLHS()) { |
| 3453 | // In C++, we can have a throw-expression, which has 'void' type. |
| 3454 | if (!lhsExpr->getType()->isVoidType()) |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3455 | if (Expr* LHS = EvalAddr(lhsExpr, refVars, ParentDecl)) |
Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 3456 | return LHS; |
| 3457 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3458 | |
Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 3459 | // In C++, we can have a throw-expression, which has 'void' type. |
| 3460 | if (C->getRHS()->getType()->isVoidType()) |
| 3461 | return NULL; |
| 3462 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3463 | return EvalAddr(C->getRHS(), refVars, ParentDecl); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3464 | } |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3465 | |
| 3466 | case Stmt::BlockExprClass: |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 3467 | if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures()) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3468 | return E; // local block. |
| 3469 | return NULL; |
| 3470 | |
| 3471 | case Stmt::AddrLabelExprClass: |
| 3472 | return E; // address of label. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3473 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3474 | case Stmt::ExprWithCleanupsClass: |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3475 | return EvalAddr(cast<ExprWithCleanups>(E)->getSubExpr(), refVars, |
| 3476 | ParentDecl); |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3477 | |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 3478 | // For casts, we need to handle conversions from arrays to |
| 3479 | // pointer values, and pointer-to-pointer conversions. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 3480 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 3481 | case Stmt::CStyleCastExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3482 | case Stmt::CXXFunctionalCastExprClass: |
Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3483 | case Stmt::ObjCBridgedCastExprClass: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3484 | case Stmt::CXXStaticCastExprClass: |
| 3485 | case Stmt::CXXDynamicCastExprClass: |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 3486 | case Stmt::CXXConstCastExprClass: |
| 3487 | case Stmt::CXXReinterpretCastExprClass: { |
Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3488 | Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
| 3489 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 3490 | case CK_BitCast: |
| 3491 | case CK_LValueToRValue: |
| 3492 | case CK_NoOp: |
| 3493 | case CK_BaseToDerived: |
| 3494 | case CK_DerivedToBase: |
| 3495 | case CK_UncheckedDerivedToBase: |
| 3496 | case CK_Dynamic: |
| 3497 | case CK_CPointerToObjCPointerCast: |
| 3498 | case CK_BlockPointerToObjCPointerCast: |
| 3499 | case CK_AnyPointerToBlockPointerCast: |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3500 | return EvalAddr(SubExpr, refVars, ParentDecl); |
Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3501 | |
| 3502 | case CK_ArrayToPointerDecay: |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3503 | return EvalVal(SubExpr, refVars, ParentDecl); |
Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3504 | |
| 3505 | default: |
| 3506 | return 0; |
| 3507 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3508 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3509 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3510 | case Stmt::MaterializeTemporaryExprClass: |
| 3511 | if (Expr *Result = EvalAddr( |
| 3512 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3513 | refVars, ParentDecl)) |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3514 | return Result; |
| 3515 | |
| 3516 | return E; |
| 3517 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3518 | // Everything else: we simply don't reason about them. |
| 3519 | default: |
| 3520 | return NULL; |
| 3521 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3522 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3523 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3524 | |
| 3525 | /// EvalVal - This function is complements EvalAddr in the mutual recursion. |
| 3526 | /// See the comments for EvalAddr for more details. |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3527 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 3528 | Decl *ParentDecl) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3529 | do { |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 3530 | // We should only be called for evaluating non-pointer expressions, or |
| 3531 | // expressions with a pointer type that are not used as references but instead |
| 3532 | // are l-values (e.g., DeclRefExpr with a pointer type). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3533 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3534 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 3535 | // viewed AST node. We then recursively traverse the AST by calling |
| 3536 | // EvalAddr and EvalVal appropriately. |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3537 | |
| 3538 | E = E->IgnoreParens(); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3539 | switch (E->getStmtClass()) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3540 | case Stmt::ImplicitCastExprClass: { |
| 3541 | ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E); |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3542 | if (IE->getValueKind() == VK_LValue) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3543 | E = IE->getSubExpr(); |
| 3544 | continue; |
| 3545 | } |
| 3546 | return NULL; |
| 3547 | } |
| 3548 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3549 | case Stmt::ExprWithCleanupsClass: |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3550 | return EvalVal(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,ParentDecl); |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3551 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3552 | case Stmt::DeclRefExprClass: { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3553 | // When we hit a DeclRefExpr we are looking at code that refers to a |
| 3554 | // variable's name. If it's not a reference variable we check if it has |
| 3555 | // local storage within the function, and if so, return the expression. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3556 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3557 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3558 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) { |
| 3559 | // Check if it refers to itself, e.g. "int& i = i;". |
| 3560 | if (V == ParentDecl) |
| 3561 | return DR; |
| 3562 | |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3563 | if (V->hasLocalStorage()) { |
| 3564 | if (!V->getType()->isReferenceType()) |
| 3565 | return DR; |
| 3566 | |
| 3567 | // Reference variable, follow through to the expression that |
| 3568 | // it points to. |
| 3569 | if (V->hasInit()) { |
| 3570 | // Add the reference variable to the "trail". |
| 3571 | refVars.push_back(DR); |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3572 | return EvalVal(V->getInit(), refVars, V); |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3573 | } |
| 3574 | } |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3575 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3576 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3577 | return NULL; |
| 3578 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3579 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3580 | case Stmt::UnaryOperatorClass: { |
| 3581 | // The only unary operator that make sense to handle here |
| 3582 | // is Deref. All others don't resolve to a "name." This includes |
| 3583 | // handling all sorts of rvalues passed to a unary operator. |
| 3584 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3585 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3586 | if (U->getOpcode() == UO_Deref) |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3587 | return EvalAddr(U->getSubExpr(), refVars, ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3588 | |
| 3589 | return NULL; |
| 3590 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3591 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3592 | case Stmt::ArraySubscriptExprClass: { |
| 3593 | // Array subscripts are potential references to data on the stack. We |
| 3594 | // retrieve the DeclRefExpr* for the array variable if it indeed |
| 3595 | // has local storage. |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3596 | return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars,ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3597 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3598 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3599 | case Stmt::ConditionalOperatorClass: { |
| 3600 | // 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] | 3601 | // non-NULL Expr's. If one is non-NULL, we return it. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3602 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 3603 | |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 3604 | // Handle the GNU extension for missing LHS. |
| 3605 | if (Expr *lhsExpr = C->getLHS()) |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3606 | if (Expr *LHS = EvalVal(lhsExpr, refVars, ParentDecl)) |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 3607 | return LHS; |
| 3608 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3609 | return EvalVal(C->getRHS(), refVars, ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3610 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3611 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3612 | // Accesses to members are potential references to data on the stack. |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 3613 | case Stmt::MemberExprClass: { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3614 | MemberExpr *M = cast<MemberExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3615 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3616 | // Check for indirect access. We only want direct field accesses. |
Ted Kremenek | a423e81 | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 3617 | if (M->isArrow()) |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3618 | return NULL; |
Ted Kremenek | a423e81 | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 3619 | |
| 3620 | // Check whether the member type is itself a reference, in which case |
| 3621 | // we're not going to refer to the member, but to what the member refers to. |
| 3622 | if (M->getMemberDecl()->getType()->isReferenceType()) |
| 3623 | return NULL; |
| 3624 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3625 | return EvalVal(M->getBase(), refVars, ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3626 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3627 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3628 | case Stmt::MaterializeTemporaryExprClass: |
| 3629 | if (Expr *Result = EvalVal( |
| 3630 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3631 | refVars, ParentDecl)) |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3632 | return Result; |
| 3633 | |
| 3634 | return E; |
| 3635 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3636 | default: |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3637 | // Check that we don't return or take the address of a reference to a |
| 3638 | // temporary. This is only useful in C++. |
| 3639 | if (!E->isTypeDependent() && E->isRValue()) |
| 3640 | return E; |
| 3641 | |
| 3642 | // Everything else: we simply don't reason about them. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3643 | return NULL; |
| 3644 | } |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3645 | } while (true); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3646 | } |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3647 | |
| 3648 | //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// |
| 3649 | |
| 3650 | /// Check for comparisons of floating point operands using != and ==. |
| 3651 | /// Issue a warning if these are no self-comparisons, as they are not likely |
| 3652 | /// to do what the programmer intended. |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3653 | void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) { |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3654 | Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts(); |
| 3655 | Expr* RightExprSansParen = RHS->IgnoreParenImpCasts(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3656 | |
| 3657 | // Special case: check for x == x (which is OK). |
| 3658 | // Do not emit warnings for such cases. |
| 3659 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) |
| 3660 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) |
| 3661 | if (DRL->getDecl() == DRR->getDecl()) |
David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3662 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3663 | |
| 3664 | |
Ted Kremenek | 1b500bb | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 3665 | // Special case: check for comparisons against literals that can be exactly |
| 3666 | // represented by APFloat. In such cases, do not emit a warning. This |
| 3667 | // is a heuristic: often comparison against such literals are used to |
| 3668 | // detect if a value in a variable has not changed. This clearly can |
| 3669 | // lead to false negatives. |
David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3670 | if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { |
| 3671 | if (FLL->isExact()) |
| 3672 | return; |
| 3673 | } else |
| 3674 | if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)) |
| 3675 | if (FLR->isExact()) |
| 3676 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3677 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3678 | // Check for comparisons with builtin types. |
David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3679 | if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) |
| 3680 | if (CL->isBuiltinCall()) |
| 3681 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3682 | |
David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3683 | if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) |
| 3684 | if (CR->isBuiltinCall()) |
| 3685 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3686 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3687 | // Emit the diagnostic. |
David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3688 | Diag(Loc, diag::warn_floatingpoint_eq) |
| 3689 | << LHS->getSourceRange() << RHS->getSourceRange(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3690 | } |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3691 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3692 | //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// |
| 3693 | //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3694 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3695 | namespace { |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3696 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3697 | /// Structure recording the 'active' range of an integer-valued |
| 3698 | /// expression. |
| 3699 | struct IntRange { |
| 3700 | /// The number of bits active in the int. |
| 3701 | unsigned Width; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3702 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3703 | /// True if the int is known not to have negative values. |
| 3704 | bool NonNegative; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3705 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3706 | IntRange(unsigned Width, bool NonNegative) |
| 3707 | : Width(Width), NonNegative(NonNegative) |
| 3708 | {} |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3709 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3710 | /// Returns the range of the bool type. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3711 | static IntRange forBoolType() { |
| 3712 | return IntRange(1, true); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3713 | } |
| 3714 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3715 | /// Returns the range of an opaque value of the given integral type. |
| 3716 | static IntRange forValueOfType(ASTContext &C, QualType T) { |
| 3717 | return forValueOfCanonicalType(C, |
| 3718 | T->getCanonicalTypeInternal().getTypePtr()); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3719 | } |
| 3720 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3721 | /// Returns the range of an opaque value of a canonical integral type. |
| 3722 | static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3723 | assert(T->isCanonicalUnqualified()); |
| 3724 | |
| 3725 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 3726 | T = VT->getElementType().getTypePtr(); |
| 3727 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 3728 | T = CT->getElementType().getTypePtr(); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3729 | |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3730 | // For enum types, use the known bit width of the enumerators. |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3731 | if (const EnumType *ET = dyn_cast<EnumType>(T)) { |
| 3732 | EnumDecl *Enum = ET->getDecl(); |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3733 | if (!Enum->isCompleteDefinition()) |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3734 | return IntRange(C.getIntWidth(QualType(T, 0)), false); |
| 3735 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3736 | unsigned NumPositive = Enum->getNumPositiveBits(); |
| 3737 | unsigned NumNegative = Enum->getNumNegativeBits(); |
| 3738 | |
| 3739 | return IntRange(std::max(NumPositive, NumNegative), NumNegative == 0); |
| 3740 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3741 | |
| 3742 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 3743 | assert(BT->isInteger()); |
| 3744 | |
| 3745 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 3746 | } |
| 3747 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3748 | /// Returns the "target" range of a canonical integral type, i.e. |
| 3749 | /// the range of values expressible in the type. |
| 3750 | /// |
| 3751 | /// This matches forValueOfCanonicalType except that enums have the |
| 3752 | /// full range of their type, not the range of their enumerators. |
| 3753 | static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) { |
| 3754 | assert(T->isCanonicalUnqualified()); |
| 3755 | |
| 3756 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 3757 | T = VT->getElementType().getTypePtr(); |
| 3758 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 3759 | T = CT->getElementType().getTypePtr(); |
| 3760 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Douglas Gregor | 69ff26b | 2011-09-08 23:29:05 +0000 | [diff] [blame] | 3761 | T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr(); |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3762 | |
| 3763 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 3764 | assert(BT->isInteger()); |
| 3765 | |
| 3766 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 3767 | } |
| 3768 | |
| 3769 | /// Returns the supremum of two ranges: i.e. their conservative merge. |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 3770 | static IntRange join(IntRange L, IntRange R) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3771 | return IntRange(std::max(L.Width, R.Width), |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3772 | L.NonNegative && R.NonNegative); |
| 3773 | } |
| 3774 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3775 | /// Returns the infinum of two ranges: i.e. their aggressive merge. |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 3776 | static IntRange meet(IntRange L, IntRange R) { |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3777 | return IntRange(std::min(L.Width, R.Width), |
| 3778 | L.NonNegative || R.NonNegative); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3779 | } |
| 3780 | }; |
| 3781 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3782 | static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, |
| 3783 | unsigned MaxWidth) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3784 | if (value.isSigned() && value.isNegative()) |
| 3785 | return IntRange(value.getMinSignedBits(), false); |
| 3786 | |
| 3787 | if (value.getBitWidth() > MaxWidth) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3788 | value = value.trunc(MaxWidth); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3789 | |
| 3790 | // isNonNegative() just checks the sign bit without considering |
| 3791 | // signedness. |
| 3792 | return IntRange(value.getActiveBits(), true); |
| 3793 | } |
| 3794 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3795 | static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, |
| 3796 | unsigned MaxWidth) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3797 | if (result.isInt()) |
| 3798 | return GetValueRange(C, result.getInt(), MaxWidth); |
| 3799 | |
| 3800 | if (result.isVector()) { |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 3801 | IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); |
| 3802 | for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { |
| 3803 | IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); |
| 3804 | R = IntRange::join(R, El); |
| 3805 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3806 | return R; |
| 3807 | } |
| 3808 | |
| 3809 | if (result.isComplexInt()) { |
| 3810 | IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); |
| 3811 | IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); |
| 3812 | return IntRange::join(R, I); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
| 3815 | // This can happen with lossless casts to intptr_t of "based" lvalues. |
| 3816 | // Assume it might use arbitrary bits. |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 3817 | // FIXME: The only reason we need to pass the type in here is to get |
| 3818 | // the sign right on this one case. It would be nice if APValue |
| 3819 | // preserved this. |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 3820 | assert(result.isLValue() || result.isAddrLabelDiff()); |
Douglas Gregor | 5e9ebb3 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 3821 | return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType()); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3822 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3823 | |
| 3824 | /// Pseudo-evaluate the given integer expression, estimating the |
| 3825 | /// range of values it might take. |
| 3826 | /// |
| 3827 | /// \param MaxWidth - the width to which the value will be truncated |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3828 | static IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3829 | E = E->IgnoreParens(); |
| 3830 | |
| 3831 | // Try a full evaluation first. |
| 3832 | Expr::EvalResult result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3833 | if (E->EvaluateAsRValue(result, C)) |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 3834 | return GetValueRange(C, result.Val, E->getType(), MaxWidth); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3835 | |
| 3836 | // I think we only want to look through implicit casts here; if the |
| 3837 | // user has an explicit widening cast, we should treat the value as |
| 3838 | // being of the new, wider type. |
| 3839 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) { |
Eli Friedman | b17ee5b | 2011-12-15 02:41:52 +0000 | [diff] [blame] | 3840 | if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue) |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3841 | return GetExprRange(C, CE->getSubExpr(), MaxWidth); |
| 3842 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3843 | IntRange OutputTypeRange = IntRange::forValueOfType(C, CE->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3844 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3845 | bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast); |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3846 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3847 | // 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] | 3848 | if (!isIntegerCast) |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3849 | return OutputTypeRange; |
| 3850 | |
| 3851 | IntRange SubRange |
| 3852 | = GetExprRange(C, CE->getSubExpr(), |
| 3853 | std::min(MaxWidth, OutputTypeRange.Width)); |
| 3854 | |
| 3855 | // Bail out if the subexpr's range is as wide as the cast type. |
| 3856 | if (SubRange.Width >= OutputTypeRange.Width) |
| 3857 | return OutputTypeRange; |
| 3858 | |
| 3859 | // Otherwise, we take the smaller width, and we're non-negative if |
| 3860 | // either the output type or the subexpr is. |
| 3861 | return IntRange(SubRange.Width, |
| 3862 | SubRange.NonNegative || OutputTypeRange.NonNegative); |
| 3863 | } |
| 3864 | |
| 3865 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 3866 | // If we can fold the condition, just take that operand. |
| 3867 | bool CondResult; |
| 3868 | if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) |
| 3869 | return GetExprRange(C, CondResult ? CO->getTrueExpr() |
| 3870 | : CO->getFalseExpr(), |
| 3871 | MaxWidth); |
| 3872 | |
| 3873 | // Otherwise, conservatively merge. |
| 3874 | IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth); |
| 3875 | IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth); |
| 3876 | return IntRange::join(L, R); |
| 3877 | } |
| 3878 | |
| 3879 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 3880 | switch (BO->getOpcode()) { |
| 3881 | |
| 3882 | // Boolean-valued operations are single-bit and positive. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3883 | case BO_LAnd: |
| 3884 | case BO_LOr: |
| 3885 | case BO_LT: |
| 3886 | case BO_GT: |
| 3887 | case BO_LE: |
| 3888 | case BO_GE: |
| 3889 | case BO_EQ: |
| 3890 | case BO_NE: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3891 | return IntRange::forBoolType(); |
| 3892 | |
John McCall | 862ff87 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 3893 | // The type of the assignments is the type of the LHS, so the RHS |
| 3894 | // is not necessarily the same type. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3895 | case BO_MulAssign: |
| 3896 | case BO_DivAssign: |
| 3897 | case BO_RemAssign: |
| 3898 | case BO_AddAssign: |
| 3899 | case BO_SubAssign: |
John McCall | 862ff87 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 3900 | case BO_XorAssign: |
| 3901 | case BO_OrAssign: |
| 3902 | // TODO: bitfields? |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3903 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 3904 | |
John McCall | 862ff87 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 3905 | // Simple assignments just pass through the RHS, which will have |
| 3906 | // been coerced to the LHS type. |
| 3907 | case BO_Assign: |
| 3908 | // TODO: bitfields? |
| 3909 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 3910 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3911 | // Operations with opaque sources are black-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3912 | case BO_PtrMemD: |
| 3913 | case BO_PtrMemI: |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3914 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3915 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3916 | // Bitwise-and uses the *infinum* of the two source ranges. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3917 | case BO_And: |
| 3918 | case BO_AndAssign: |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3919 | return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth), |
| 3920 | GetExprRange(C, BO->getRHS(), MaxWidth)); |
| 3921 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3922 | // Left shift gets black-listed based on a judgement call. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3923 | case BO_Shl: |
John McCall | 3aae609 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 3924 | // ...except that we want to treat '1 << (blah)' as logically |
| 3925 | // positive. It's an important idiom. |
| 3926 | if (IntegerLiteral *I |
| 3927 | = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { |
| 3928 | if (I->getValue() == 1) { |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3929 | IntRange R = IntRange::forValueOfType(C, E->getType()); |
John McCall | 3aae609 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 3930 | return IntRange(R.Width, /*NonNegative*/ true); |
| 3931 | } |
| 3932 | } |
| 3933 | // fallthrough |
| 3934 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3935 | case BO_ShlAssign: |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3936 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3937 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3938 | // Right shift by a constant can narrow its left argument. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3939 | case BO_Shr: |
| 3940 | case BO_ShrAssign: { |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3941 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 3942 | |
| 3943 | // If the shift amount is a positive constant, drop the width by |
| 3944 | // that much. |
| 3945 | llvm::APSInt shift; |
| 3946 | if (BO->getRHS()->isIntegerConstantExpr(shift, C) && |
| 3947 | shift.isNonNegative()) { |
| 3948 | unsigned zext = shift.getZExtValue(); |
| 3949 | if (zext >= L.Width) |
| 3950 | L.Width = (L.NonNegative ? 0 : 1); |
| 3951 | else |
| 3952 | L.Width -= zext; |
| 3953 | } |
| 3954 | |
| 3955 | return L; |
| 3956 | } |
| 3957 | |
| 3958 | // Comma acts as its right operand. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3959 | case BO_Comma: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3960 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 3961 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3962 | // Black-list pointer subtractions. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3963 | case BO_Sub: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3964 | if (BO->getLHS()->getType()->isPointerType()) |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3965 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | 00fe761 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 3966 | break; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3967 | |
John McCall | 00fe761 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 3968 | // The width of a division result is mostly determined by the size |
| 3969 | // of the LHS. |
| 3970 | case BO_Div: { |
| 3971 | // Don't 'pre-truncate' the operands. |
| 3972 | unsigned opWidth = C.getIntWidth(E->getType()); |
| 3973 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); |
| 3974 | |
| 3975 | // If the divisor is constant, use that. |
| 3976 | llvm::APSInt divisor; |
| 3977 | if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) { |
| 3978 | unsigned log2 = divisor.logBase2(); // floor(log_2(divisor)) |
| 3979 | if (log2 >= L.Width) |
| 3980 | L.Width = (L.NonNegative ? 0 : 1); |
| 3981 | else |
| 3982 | L.Width = std::min(L.Width - log2, MaxWidth); |
| 3983 | return L; |
| 3984 | } |
| 3985 | |
| 3986 | // Otherwise, just use the LHS's width. |
| 3987 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); |
| 3988 | return IntRange(L.Width, L.NonNegative && R.NonNegative); |
| 3989 | } |
| 3990 | |
| 3991 | // The result of a remainder can't be larger than the result of |
| 3992 | // either side. |
| 3993 | case BO_Rem: { |
| 3994 | // Don't 'pre-truncate' the operands. |
| 3995 | unsigned opWidth = C.getIntWidth(E->getType()); |
| 3996 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); |
| 3997 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); |
| 3998 | |
| 3999 | IntRange meet = IntRange::meet(L, R); |
| 4000 | meet.Width = std::min(meet.Width, MaxWidth); |
| 4001 | return meet; |
| 4002 | } |
| 4003 | |
| 4004 | // The default behavior is okay for these. |
| 4005 | case BO_Mul: |
| 4006 | case BO_Add: |
| 4007 | case BO_Xor: |
| 4008 | case BO_Or: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4009 | break; |
| 4010 | } |
| 4011 | |
John McCall | 00fe761 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 4012 | // The default case is to treat the operation as if it were closed |
| 4013 | // on the narrowest type that encompasses both operands. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4014 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 4015 | IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth); |
| 4016 | return IntRange::join(L, R); |
| 4017 | } |
| 4018 | |
| 4019 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 4020 | switch (UO->getOpcode()) { |
| 4021 | // Boolean-valued operations are white-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4022 | case UO_LNot: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4023 | return IntRange::forBoolType(); |
| 4024 | |
| 4025 | // Operations with opaque sources are black-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4026 | case UO_Deref: |
| 4027 | case UO_AddrOf: // should be impossible |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4028 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4029 | |
| 4030 | default: |
| 4031 | return GetExprRange(C, UO->getSubExpr(), MaxWidth); |
| 4032 | } |
| 4033 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4034 | |
| 4035 | if (dyn_cast<OffsetOfExpr>(E)) { |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4036 | IntRange::forValueOfType(C, E->getType()); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4037 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4038 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4039 | if (FieldDecl *BitField = E->getBitField()) |
| 4040 | return IntRange(BitField->getBitWidthValue(C), |
Douglas Gregor | 5e9ebb3 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 4041 | BitField->getType()->isUnsignedIntegerOrEnumerationType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4042 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4043 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4044 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4045 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4046 | static IntRange GetExprRange(ASTContext &C, Expr *E) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4047 | return GetExprRange(C, E, C.getIntWidth(E->getType())); |
| 4048 | } |
| 4049 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4050 | /// Checks whether the given value, which currently has the given |
| 4051 | /// source semantics, has the same value when coerced through the |
| 4052 | /// target semantics. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4053 | static bool IsSameFloatAfterCast(const llvm::APFloat &value, |
| 4054 | const llvm::fltSemantics &Src, |
| 4055 | const llvm::fltSemantics &Tgt) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4056 | llvm::APFloat truncated = value; |
| 4057 | |
| 4058 | bool ignored; |
| 4059 | truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 4060 | truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 4061 | |
| 4062 | return truncated.bitwiseIsEqual(value); |
| 4063 | } |
| 4064 | |
| 4065 | /// Checks whether the given value, which currently has the given |
| 4066 | /// source semantics, has the same value when coerced through the |
| 4067 | /// target semantics. |
| 4068 | /// |
| 4069 | /// The value might be a vector of floats (or a complex number). |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4070 | static bool IsSameFloatAfterCast(const APValue &value, |
| 4071 | const llvm::fltSemantics &Src, |
| 4072 | const llvm::fltSemantics &Tgt) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4073 | if (value.isFloat()) |
| 4074 | return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); |
| 4075 | |
| 4076 | if (value.isVector()) { |
| 4077 | for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) |
| 4078 | if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) |
| 4079 | return false; |
| 4080 | return true; |
| 4081 | } |
| 4082 | |
| 4083 | assert(value.isComplexFloat()); |
| 4084 | return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && |
| 4085 | IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); |
| 4086 | } |
| 4087 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4088 | static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4089 | |
Ted Kremenek | e3b159c | 2010-09-23 21:43:44 +0000 | [diff] [blame] | 4090 | static bool IsZero(Sema &S, Expr *E) { |
| 4091 | // Suppress cases where we are comparing against an enum constant. |
| 4092 | if (const DeclRefExpr *DR = |
| 4093 | dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) |
| 4094 | if (isa<EnumConstantDecl>(DR->getDecl())) |
| 4095 | return false; |
| 4096 | |
| 4097 | // Suppress cases where the '0' value is expanded from a macro. |
| 4098 | if (E->getLocStart().isMacroID()) |
| 4099 | return false; |
| 4100 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4101 | llvm::APSInt Value; |
| 4102 | return E->isIntegerConstantExpr(Value, S.Context) && Value == 0; |
| 4103 | } |
| 4104 | |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4105 | static bool HasEnumType(Expr *E) { |
| 4106 | // Strip off implicit integral promotions. |
| 4107 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
Argyrios Kyrtzidis | 63b57ae | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 4108 | if (ICE->getCastKind() != CK_IntegralCast && |
| 4109 | ICE->getCastKind() != CK_NoOp) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4110 | break; |
Argyrios Kyrtzidis | 63b57ae | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 4111 | E = ICE->getSubExpr(); |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
| 4114 | return E->getType()->isEnumeralType(); |
| 4115 | } |
| 4116 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4117 | static void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4118 | BinaryOperatorKind op = E->getOpcode(); |
Douglas Gregor | 14af91a | 2010-12-21 07:22:56 +0000 | [diff] [blame] | 4119 | if (E->isValueDependent()) |
| 4120 | return; |
| 4121 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4122 | if (op == BO_LT && IsZero(S, E->getRHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4123 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4124 | << "< 0" << "false" << HasEnumType(E->getLHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4125 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4126 | } else if (op == BO_GE && IsZero(S, E->getRHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4127 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4128 | << ">= 0" << "true" << HasEnumType(E->getLHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4129 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4130 | } else if (op == BO_GT && IsZero(S, E->getLHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4131 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4132 | << "0 >" << "false" << HasEnumType(E->getRHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4133 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4134 | } else if (op == BO_LE && IsZero(S, E->getLHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4135 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4136 | << "0 <=" << "true" << HasEnumType(E->getRHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4137 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
| 4138 | } |
| 4139 | } |
| 4140 | |
| 4141 | /// Analyze the operands of the given comparison. Implements the |
| 4142 | /// fallback case from AnalyzeComparison. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4143 | static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4144 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 4145 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4146 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4147 | |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4148 | /// \brief Implements -Wsign-compare. |
| 4149 | /// |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4150 | /// \param E the binary operator to check for warnings |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4151 | static void AnalyzeComparison(Sema &S, BinaryOperator *E) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4152 | // The type the comparison is being performed in. |
| 4153 | QualType T = E->getLHS()->getType(); |
| 4154 | assert(S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType()) |
| 4155 | && "comparison with mismatched types"); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4156 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4157 | // We don't do anything special if this isn't an unsigned integral |
| 4158 | // comparison: we're only interested in integral comparisons, and |
| 4159 | // 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] | 4160 | // |
| 4161 | // We also don't care about value-dependent expressions or expressions |
| 4162 | // whose result is a constant. |
| 4163 | if (!T->hasUnsignedIntegerRepresentation() |
| 4164 | || E->isValueDependent() || E->isIntegerConstantExpr(S.Context)) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4165 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4166 | |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4167 | Expr *LHS = E->getLHS()->IgnoreParenImpCasts(); |
| 4168 | Expr *RHS = E->getRHS()->IgnoreParenImpCasts(); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4169 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4170 | // Check to see if one of the (unmodified) operands is of different |
| 4171 | // signedness. |
| 4172 | Expr *signedOperand, *unsignedOperand; |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4173 | if (LHS->getType()->hasSignedIntegerRepresentation()) { |
| 4174 | assert(!RHS->getType()->hasSignedIntegerRepresentation() && |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4175 | "unsigned comparison between two signed integer expressions?"); |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4176 | signedOperand = LHS; |
| 4177 | unsignedOperand = RHS; |
| 4178 | } else if (RHS->getType()->hasSignedIntegerRepresentation()) { |
| 4179 | signedOperand = RHS; |
| 4180 | unsignedOperand = LHS; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4181 | } else { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4182 | CheckTrivialUnsignedComparison(S, E); |
| 4183 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4184 | } |
| 4185 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4186 | // Otherwise, calculate the effective range of the signed operand. |
| 4187 | IntRange signedRange = GetExprRange(S.Context, signedOperand); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4188 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4189 | // Go ahead and analyze implicit conversions in the operands. Note |
| 4190 | // that we skip the implicit conversions on both sides. |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4191 | AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc()); |
| 4192 | AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc()); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4193 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4194 | // If the signed range is non-negative, -Wsign-compare won't fire, |
| 4195 | // but we should still check for comparisons which are always true |
| 4196 | // or false. |
| 4197 | if (signedRange.NonNegative) |
| 4198 | return CheckTrivialUnsignedComparison(S, E); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4199 | |
| 4200 | // For (in)equality comparisons, if the unsigned operand is a |
| 4201 | // constant which cannot collide with a overflowed signed operand, |
| 4202 | // then reinterpreting the signed operand as unsigned will not |
| 4203 | // change the result of the comparison. |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4204 | if (E->isEqualityOp()) { |
| 4205 | unsigned comparisonWidth = S.Context.getIntWidth(T); |
| 4206 | IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4207 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4208 | // We should never be unable to prove that the unsigned operand is |
| 4209 | // non-negative. |
| 4210 | assert(unsignedRange.NonNegative && "unsigned range includes negative?"); |
| 4211 | |
| 4212 | if (unsignedRange.Width < comparisonWidth) |
| 4213 | return; |
| 4214 | } |
| 4215 | |
Douglas Gregor | 6d3b93d | 2012-05-01 01:53:49 +0000 | [diff] [blame] | 4216 | S.DiagRuntimeBehavior(E->getOperatorLoc(), E, |
| 4217 | S.PDiag(diag::warn_mixed_sign_comparison) |
| 4218 | << LHS->getType() << RHS->getType() |
| 4219 | << LHS->getSourceRange() << RHS->getSourceRange()); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4220 | } |
| 4221 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4222 | /// Analyzes an attempt to assign the given value to a bitfield. |
| 4223 | /// |
| 4224 | /// Returns true if there was something fishy about the attempt. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4225 | static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, |
| 4226 | SourceLocation InitLoc) { |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4227 | assert(Bitfield->isBitField()); |
| 4228 | if (Bitfield->isInvalidDecl()) |
| 4229 | return false; |
| 4230 | |
John McCall | 91b6014 | 2010-11-11 05:33:51 +0000 | [diff] [blame] | 4231 | // White-list bool bitfields. |
| 4232 | if (Bitfield->getType()->isBooleanType()) |
| 4233 | return false; |
| 4234 | |
Douglas Gregor | 46ff303 | 2011-02-04 13:09:01 +0000 | [diff] [blame] | 4235 | // Ignore value- or type-dependent expressions. |
| 4236 | if (Bitfield->getBitWidth()->isValueDependent() || |
| 4237 | Bitfield->getBitWidth()->isTypeDependent() || |
| 4238 | Init->isValueDependent() || |
| 4239 | Init->isTypeDependent()) |
| 4240 | return false; |
| 4241 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4242 | Expr *OriginalInit = Init->IgnoreParenImpCasts(); |
| 4243 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 4244 | llvm::APSInt Value; |
| 4245 | if (!OriginalInit->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects)) |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4246 | return false; |
| 4247 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4248 | unsigned OriginalWidth = Value.getBitWidth(); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4249 | unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context); |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4250 | |
| 4251 | if (OriginalWidth <= FieldWidth) |
| 4252 | return false; |
| 4253 | |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4254 | // Compute the value which the bitfield will contain. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 4255 | llvm::APSInt TruncatedValue = Value.trunc(FieldWidth); |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4256 | TruncatedValue.setIsSigned(Bitfield->getType()->isSignedIntegerType()); |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4257 | |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4258 | // Check whether the stored value is equal to the original value. |
| 4259 | TruncatedValue = TruncatedValue.extend(OriginalWidth); |
Richard Trieu | e1ecdc1 | 2012-07-23 20:21:35 +0000 | [diff] [blame] | 4260 | if (llvm::APSInt::isSameValue(Value, TruncatedValue)) |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4261 | return false; |
| 4262 | |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4263 | // Special-case bitfields of width 1: booleans are naturally 0/1, and |
Eli Friedman | 34ff062 | 2012-02-02 00:40:20 +0000 | [diff] [blame] | 4264 | // therefore don't strictly fit into a signed bitfield of width 1. |
| 4265 | if (FieldWidth == 1 && Value == 1) |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4266 | return false; |
| 4267 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4268 | std::string PrettyValue = Value.toString(10); |
| 4269 | std::string PrettyTrunc = TruncatedValue.toString(10); |
| 4270 | |
| 4271 | S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant) |
| 4272 | << PrettyValue << PrettyTrunc << OriginalInit->getType() |
| 4273 | << Init->getSourceRange(); |
| 4274 | |
| 4275 | return true; |
| 4276 | } |
| 4277 | |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4278 | /// Analyze the given simple or compound assignment for warning-worthy |
| 4279 | /// operations. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4280 | static void AnalyzeAssignment(Sema &S, BinaryOperator *E) { |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4281 | // Just recurse on the LHS. |
| 4282 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 4283 | |
| 4284 | // We want to recurse on the RHS as normal unless we're assigning to |
| 4285 | // a bitfield. |
| 4286 | if (FieldDecl *Bitfield = E->getLHS()->getBitField()) { |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4287 | if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(), |
| 4288 | E->getOperatorLoc())) { |
| 4289 | // Recurse, ignoring any implicit conversions on the RHS. |
| 4290 | return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(), |
| 4291 | E->getOperatorLoc()); |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4292 | } |
| 4293 | } |
| 4294 | |
| 4295 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
| 4296 | } |
| 4297 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4298 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4299 | static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4300 | SourceLocation CContext, unsigned diag, |
| 4301 | bool pruneControlFlow = false) { |
| 4302 | if (pruneControlFlow) { |
| 4303 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
| 4304 | S.PDiag(diag) |
| 4305 | << SourceType << T << E->getSourceRange() |
| 4306 | << SourceRange(CContext)); |
| 4307 | return; |
| 4308 | } |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 4309 | S.Diag(E->getExprLoc(), diag) |
| 4310 | << SourceType << T << E->getSourceRange() << SourceRange(CContext); |
| 4311 | } |
| 4312 | |
Chandler Carruth | e1b02e0 | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 4313 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4314 | static void DiagnoseImpCast(Sema &S, Expr *E, QualType T, |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4315 | SourceLocation CContext, unsigned diag, |
| 4316 | bool pruneControlFlow = false) { |
| 4317 | DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow); |
Chandler Carruth | e1b02e0 | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 4318 | } |
| 4319 | |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4320 | /// Diagnose an implicit cast from a literal expression. Does not warn when the |
| 4321 | /// cast wouldn't lose information. |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4322 | void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T, |
| 4323 | SourceLocation CContext) { |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4324 | // 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] | 4325 | bool isExact = false; |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4326 | const llvm::APFloat &Value = FL->getValue(); |
Jeffrey Yasskin | 3e1ef78 | 2011-07-15 17:03:07 +0000 | [diff] [blame] | 4327 | llvm::APSInt IntegerValue(S.Context.getIntWidth(T), |
| 4328 | T->hasUnsignedIntegerRepresentation()); |
| 4329 | if (Value.convertToInteger(IntegerValue, |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4330 | llvm::APFloat::rmTowardZero, &isExact) |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4331 | == llvm::APFloat::opOK && isExact) |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4332 | return; |
| 4333 | |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4334 | SmallString<16> PrettySourceValue; |
| 4335 | Value.toString(PrettySourceValue); |
David Blaikie | de7e7b8 | 2012-05-15 17:18:27 +0000 | [diff] [blame] | 4336 | SmallString<16> PrettyTargetValue; |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4337 | if (T->isSpecificBuiltinType(BuiltinType::Bool)) |
| 4338 | PrettyTargetValue = IntegerValue == 0 ? "false" : "true"; |
| 4339 | else |
David Blaikie | de7e7b8 | 2012-05-15 17:18:27 +0000 | [diff] [blame] | 4340 | IntegerValue.toString(PrettyTargetValue); |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4341 | |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4342 | S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer) |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4343 | << FL->getType() << T.getUnqualifiedType() << PrettySourceValue |
| 4344 | << PrettyTargetValue << FL->getSourceRange() << SourceRange(CContext); |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4345 | } |
| 4346 | |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4347 | std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) { |
| 4348 | if (!Range.Width) return "0"; |
| 4349 | |
| 4350 | llvm::APSInt ValueInRange = Value; |
| 4351 | ValueInRange.setIsSigned(!Range.NonNegative); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 4352 | ValueInRange = ValueInRange.trunc(Range.Width); |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4353 | return ValueInRange.toString(10); |
| 4354 | } |
| 4355 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4356 | void CheckImplicitConversion(Sema &S, Expr *E, QualType T, |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4357 | SourceLocation CC, bool *ICContext = 0) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4358 | if (E->isTypeDependent() || E->isValueDependent()) return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4359 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4360 | const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr(); |
| 4361 | const Type *Target = S.Context.getCanonicalType(T).getTypePtr(); |
| 4362 | if (Source == Target) return; |
| 4363 | if (Target->isDependentType()) return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4364 | |
Chandler Carruth | 108f756 | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 4365 | // If the conversion context location is invalid don't complain. We also |
| 4366 | // don't want to emit a warning if the issue occurs from the expansion of |
| 4367 | // a system macro. The problem is that 'getSpellingLoc()' is slow, so we |
| 4368 | // delay this check as long as possible. Once we detect we are in that |
| 4369 | // scenario, we just return. |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4370 | if (CC.isInvalid()) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4371 | return; |
| 4372 | |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4373 | // Diagnose implicit casts to bool. |
| 4374 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) { |
| 4375 | if (isa<StringLiteral>(E)) |
| 4376 | // Warn on string literal to bool. Checks for string literals in logical |
| 4377 | // expressions, for instances, assert(0 && "error here"), is prevented |
| 4378 | // by a check in AnalyzeImplicitConversions(). |
| 4379 | return DiagnoseImpCast(S, E, T, CC, |
| 4380 | diag::warn_impcast_string_literal_to_bool); |
Lang Hames | e14ca9f | 2011-12-05 20:49:50 +0000 | [diff] [blame] | 4381 | if (Source->isFunctionType()) { |
| 4382 | // Warn on function to bool. Checks free functions and static member |
| 4383 | // functions. Weakly imported functions are excluded from the check, |
| 4384 | // since it's common to test their value to check whether the linker |
| 4385 | // found a definition for them. |
| 4386 | ValueDecl *D = 0; |
| 4387 | if (DeclRefExpr* R = dyn_cast<DeclRefExpr>(E)) { |
| 4388 | D = R->getDecl(); |
| 4389 | } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) { |
| 4390 | D = M->getMemberDecl(); |
| 4391 | } |
| 4392 | |
| 4393 | if (D && !D->isWeak()) { |
Richard Trieu | 26b45d8 | 2011-12-06 04:48:01 +0000 | [diff] [blame] | 4394 | if (FunctionDecl* F = dyn_cast<FunctionDecl>(D)) { |
| 4395 | S.Diag(E->getExprLoc(), diag::warn_impcast_function_to_bool) |
| 4396 | << F << E->getSourceRange() << SourceRange(CC); |
David Blaikie | 2def773 | 2011-12-09 21:42:37 +0000 | [diff] [blame] | 4397 | S.Diag(E->getExprLoc(), diag::note_function_to_bool_silence) |
| 4398 | << FixItHint::CreateInsertion(E->getExprLoc(), "&"); |
| 4399 | QualType ReturnType; |
| 4400 | UnresolvedSet<4> NonTemplateOverloads; |
| 4401 | S.isExprCallable(*E, ReturnType, NonTemplateOverloads); |
| 4402 | if (!ReturnType.isNull() |
| 4403 | && ReturnType->isSpecificBuiltinType(BuiltinType::Bool)) |
| 4404 | S.Diag(E->getExprLoc(), diag::note_function_to_bool_call) |
| 4405 | << FixItHint::CreateInsertion( |
| 4406 | S.getPreprocessor().getLocForEndOfToken(E->getLocEnd()), "()"); |
Richard Trieu | 26b45d8 | 2011-12-06 04:48:01 +0000 | [diff] [blame] | 4407 | return; |
| 4408 | } |
Lang Hames | e14ca9f | 2011-12-05 20:49:50 +0000 | [diff] [blame] | 4409 | } |
| 4410 | } |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4411 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4412 | |
| 4413 | // Strip vector types. |
| 4414 | if (isa<VectorType>(Source)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4415 | if (!isa<VectorType>(Target)) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4416 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4417 | return; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4418 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar); |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4419 | } |
Chris Lattner | b792b30 | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 4420 | |
| 4421 | // If the vector cast is cast between two vectors of the same size, it is |
| 4422 | // a bitcast, not a conversion. |
| 4423 | if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target)) |
| 4424 | return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4425 | |
| 4426 | Source = cast<VectorType>(Source)->getElementType().getTypePtr(); |
| 4427 | Target = cast<VectorType>(Target)->getElementType().getTypePtr(); |
| 4428 | } |
| 4429 | |
| 4430 | // Strip complex types. |
| 4431 | if (isa<ComplexType>(Source)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4432 | if (!isa<ComplexType>(Target)) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4433 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4434 | return; |
| 4435 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4436 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar); |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4437 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4438 | |
| 4439 | Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); |
| 4440 | Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); |
| 4441 | } |
| 4442 | |
| 4443 | const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); |
| 4444 | const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); |
| 4445 | |
| 4446 | // If the source is floating point... |
| 4447 | if (SourceBT && SourceBT->isFloatingPoint()) { |
| 4448 | // ...and the target is floating point... |
| 4449 | if (TargetBT && TargetBT->isFloatingPoint()) { |
| 4450 | // ...then warn if we're dropping FP rank. |
| 4451 | |
| 4452 | // Builtin FP kinds are ordered by increasing FP rank. |
| 4453 | if (SourceBT->getKind() > TargetBT->getKind()) { |
| 4454 | // Don't warn about float constants that are precisely |
| 4455 | // representable in the target type. |
| 4456 | Expr::EvalResult result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4457 | if (E->EvaluateAsRValue(result, S.Context)) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4458 | // Value might be a float, a float vector, or a float complex. |
| 4459 | if (IsSameFloatAfterCast(result.Val, |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4460 | S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)), |
| 4461 | S.Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4462 | return; |
| 4463 | } |
| 4464 | |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4465 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4466 | return; |
| 4467 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4468 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4469 | } |
| 4470 | return; |
| 4471 | } |
| 4472 | |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4473 | // If the target is integral, always warn. |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4474 | if (TargetBT && TargetBT->isInteger()) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4475 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4476 | return; |
| 4477 | |
Chandler Carruth | a5b9332 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 4478 | Expr *InnerE = E->IgnoreParenImpCasts(); |
Matt Beaumont-Gay | 634c8af | 2011-09-08 22:30:47 +0000 | [diff] [blame] | 4479 | // We also want to warn on, e.g., "int i = -1.234" |
| 4480 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE)) |
| 4481 | if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus) |
| 4482 | InnerE = UOp->getSubExpr()->IgnoreParenImpCasts(); |
| 4483 | |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4484 | if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) { |
| 4485 | DiagnoseFloatingLiteralImpCast(S, FL, T, CC); |
Chandler Carruth | a5b9332 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 4486 | } else { |
| 4487 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer); |
| 4488 | } |
| 4489 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4490 | |
| 4491 | return; |
| 4492 | } |
| 4493 | |
Richard Trieu | 1838ca5 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 4494 | if ((E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) |
David Blaikie | b26331b | 2012-06-19 21:19:06 +0000 | [diff] [blame] | 4495 | == Expr::NPCK_GNUNull) && !Target->isAnyPointerType() |
David Blaikie | 28a5f0c | 2012-06-21 18:51:10 +0000 | [diff] [blame] | 4496 | && !Target->isBlockPointerType() && !Target->isMemberPointerType()) { |
David Blaikie | b136049 | 2012-03-16 20:30:12 +0000 | [diff] [blame] | 4497 | SourceLocation Loc = E->getSourceRange().getBegin(); |
| 4498 | if (Loc.isMacroID()) |
| 4499 | Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first; |
David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 4500 | if (!Loc.isMacroID() || CC.isMacroID()) |
| 4501 | S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer) |
| 4502 | << T << clang::SourceRange(CC) |
| 4503 | << FixItHint::CreateReplacement(Loc, S.getFixItZeroLiteralForType(T)); |
Richard Trieu | 1838ca5 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 4504 | } |
| 4505 | |
David Blaikie | b26331b | 2012-06-19 21:19:06 +0000 | [diff] [blame] | 4506 | if (!Source->isIntegerType() || !Target->isIntegerType()) |
| 4507 | return; |
| 4508 | |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4509 | // TODO: remove this early return once the false positives for constant->bool |
| 4510 | // in templates, macros, etc, are reduced or removed. |
| 4511 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) |
| 4512 | return; |
| 4513 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4514 | IntRange SourceRange = GetExprRange(S.Context, E); |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4515 | IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4516 | |
| 4517 | if (SourceRange.Width > TargetRange.Width) { |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4518 | // If the source is a constant, use a default-on diagnostic. |
| 4519 | // TODO: this should happen for bitfield stores, too. |
| 4520 | llvm::APSInt Value(32); |
| 4521 | if (E->isIntegerConstantExpr(Value, S.Context)) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4522 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4523 | return; |
| 4524 | |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4525 | std::string PrettySourceValue = Value.toString(10); |
| 4526 | std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); |
| 4527 | |
Ted Kremenek | 5e745da | 2011-10-22 02:37:33 +0000 | [diff] [blame] | 4528 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
| 4529 | S.PDiag(diag::warn_impcast_integer_precision_constant) |
| 4530 | << PrettySourceValue << PrettyTargetValue |
| 4531 | << E->getType() << T << E->getSourceRange() |
| 4532 | << clang::SourceRange(CC)); |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4533 | return; |
| 4534 | } |
| 4535 | |
Chris Lattner | b792b30 | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 4536 | // 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] | 4537 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4538 | return; |
| 4539 | |
David Blaikie | 3705084 | 2012-04-12 22:40:54 +0000 | [diff] [blame] | 4540 | if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64) |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4541 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32, |
| 4542 | /* pruneControlFlow */ true); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4543 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4544 | } |
| 4545 | |
| 4546 | if ((TargetRange.NonNegative && !SourceRange.NonNegative) || |
| 4547 | (!TargetRange.NonNegative && SourceRange.NonNegative && |
| 4548 | SourceRange.Width == TargetRange.Width)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4549 | |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4550 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4551 | return; |
| 4552 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4553 | unsigned DiagID = diag::warn_impcast_integer_sign; |
| 4554 | |
| 4555 | // Traditionally, gcc has warned about this under -Wsign-compare. |
| 4556 | // We also want to warn about it in -Wconversion. |
| 4557 | // So if -Wconversion is off, use a completely identical diagnostic |
| 4558 | // in the sign-compare group. |
| 4559 | // The conditional-checking code will |
| 4560 | if (ICContext) { |
| 4561 | DiagID = diag::warn_impcast_integer_sign_conditional; |
| 4562 | *ICContext = true; |
| 4563 | } |
| 4564 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4565 | return DiagnoseImpCast(S, E, T, CC, DiagID); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4566 | } |
| 4567 | |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4568 | // Diagnose conversions between different enumeration types. |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 4569 | // In C, we pretend that the type of an EnumConstantDecl is its enumeration |
| 4570 | // type, to give us better diagnostics. |
| 4571 | QualType SourceType = E->getType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4572 | if (!S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 4573 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 4574 | if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 4575 | EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext()); |
| 4576 | SourceType = S.Context.getTypeDeclType(Enum); |
| 4577 | Source = S.Context.getCanonicalType(SourceType).getTypePtr(); |
| 4578 | } |
| 4579 | } |
| 4580 | |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4581 | if (const EnumType *SourceEnum = Source->getAs<EnumType>()) |
| 4582 | if (const EnumType *TargetEnum = Target->getAs<EnumType>()) |
| 4583 | if ((SourceEnum->getDecl()->getIdentifier() || |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4584 | SourceEnum->getDecl()->getTypedefNameForAnonDecl()) && |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4585 | (TargetEnum->getDecl()->getIdentifier() || |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4586 | TargetEnum->getDecl()->getTypedefNameForAnonDecl()) && |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4587 | SourceEnum != TargetEnum) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4588 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4589 | return; |
| 4590 | |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 4591 | return DiagnoseImpCast(S, E, SourceType, T, CC, |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4592 | diag::warn_impcast_different_enum_types); |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4593 | } |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4594 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4595 | return; |
| 4596 | } |
| 4597 | |
David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 4598 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, |
| 4599 | SourceLocation CC, QualType T); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4600 | |
| 4601 | void CheckConditionalOperand(Sema &S, Expr *E, QualType T, |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4602 | SourceLocation CC, bool &ICContext) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4603 | E = E->IgnoreParenImpCasts(); |
| 4604 | |
| 4605 | if (isa<ConditionalOperator>(E)) |
David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 4606 | return CheckConditionalOperator(S, cast<ConditionalOperator>(E), CC, T); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4607 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4608 | AnalyzeImplicitConversions(S, E, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4609 | if (E->getType() != T) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4610 | return CheckImplicitConversion(S, E, T, CC, &ICContext); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4611 | return; |
| 4612 | } |
| 4613 | |
David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 4614 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, |
| 4615 | SourceLocation CC, QualType T) { |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4616 | AnalyzeImplicitConversions(S, E->getCond(), CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4617 | |
| 4618 | bool Suspicious = false; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4619 | CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious); |
| 4620 | CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4621 | |
| 4622 | // If -Wconversion would have warned about either of the candidates |
| 4623 | // for a signedness conversion to the context type... |
| 4624 | if (!Suspicious) return; |
| 4625 | |
| 4626 | // ...but it's currently ignored... |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 4627 | if (S.Diags.getDiagnosticLevel(diag::warn_impcast_integer_sign_conditional, |
| 4628 | CC)) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4629 | return; |
| 4630 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4631 | // ...then check whether it would have warned about either of the |
| 4632 | // candidates for a signedness conversion to the condition type. |
Richard Trieu | 5254161 | 2011-07-21 02:46:28 +0000 | [diff] [blame] | 4633 | if (E->getType() == T) return; |
| 4634 | |
| 4635 | Suspicious = false; |
| 4636 | CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(), |
| 4637 | E->getType(), CC, &Suspicious); |
| 4638 | if (!Suspicious) |
| 4639 | CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(), |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4640 | E->getType(), CC, &Suspicious); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4641 | } |
| 4642 | |
| 4643 | /// AnalyzeImplicitConversions - Find and report any interesting |
| 4644 | /// implicit conversions in the given expression. There are a couple |
| 4645 | /// of competing diagnostics here, -Wconversion and -Wsign-compare. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4646 | void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4647 | QualType T = OrigE->getType(); |
| 4648 | Expr *E = OrigE->IgnoreParenImpCasts(); |
| 4649 | |
Douglas Gregor | f8b6e15 | 2011-10-10 17:38:18 +0000 | [diff] [blame] | 4650 | if (E->isTypeDependent() || E->isValueDependent()) |
| 4651 | return; |
| 4652 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4653 | // For conditional operators, we analyze the arguments as if they |
| 4654 | // were being fed directly into the output. |
| 4655 | if (isa<ConditionalOperator>(E)) { |
| 4656 | ConditionalOperator *CO = cast<ConditionalOperator>(E); |
David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 4657 | CheckConditionalOperator(S, CO, CC, T); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4658 | return; |
| 4659 | } |
| 4660 | |
| 4661 | // Go ahead and check any implicit conversions we might have skipped. |
| 4662 | // The non-canonical typecheck is just an optimization; |
| 4663 | // CheckImplicitConversion will filter out dead implicit conversions. |
| 4664 | if (E->getType() != T) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4665 | CheckImplicitConversion(S, E, T, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4666 | |
| 4667 | // Now continue drilling into this expression. |
| 4668 | |
| 4669 | // Skip past explicit casts. |
| 4670 | if (isa<ExplicitCastExpr>(E)) { |
| 4671 | E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts(); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4672 | return AnalyzeImplicitConversions(S, E, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4673 | } |
| 4674 | |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4675 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 4676 | // Do a somewhat different check with comparison operators. |
| 4677 | if (BO->isComparisonOp()) |
| 4678 | return AnalyzeComparison(S, BO); |
| 4679 | |
Eli Friedman | 0fa0638 | 2012-01-26 23:34:06 +0000 | [diff] [blame] | 4680 | // And with simple assignments. |
| 4681 | if (BO->getOpcode() == BO_Assign) |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4682 | return AnalyzeAssignment(S, BO); |
| 4683 | } |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4684 | |
| 4685 | // These break the otherwise-useful invariant below. Fortunately, |
| 4686 | // we don't really need to recurse into them, because any internal |
| 4687 | // expressions should have been analyzed already when they were |
| 4688 | // built into statements. |
| 4689 | if (isa<StmtExpr>(E)) return; |
| 4690 | |
| 4691 | // Don't descend into unevaluated contexts. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4692 | if (isa<UnaryExprOrTypeTraitExpr>(E)) return; |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4693 | |
| 4694 | // Now just recurse over the expression's children. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4695 | CC = E->getExprLoc(); |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4696 | BinaryOperator *BO = dyn_cast<BinaryOperator>(E); |
| 4697 | bool IsLogicalOperator = BO && BO->isLogicalOp(); |
| 4698 | for (Stmt::child_range I = E->children(); I; ++I) { |
Douglas Gregor | 54042f1 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 4699 | Expr *ChildExpr = dyn_cast_or_null<Expr>(*I); |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 4700 | if (!ChildExpr) |
| 4701 | continue; |
| 4702 | |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4703 | if (IsLogicalOperator && |
| 4704 | isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts())) |
| 4705 | // Ignore checking string literals that are in logical operators. |
| 4706 | continue; |
| 4707 | AnalyzeImplicitConversions(S, ChildExpr, CC); |
| 4708 | } |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
| 4711 | } // end anonymous namespace |
| 4712 | |
| 4713 | /// Diagnoses "dangerous" implicit conversions within the given |
| 4714 | /// expression (which is a full expression). Implements -Wconversion |
| 4715 | /// and -Wsign-compare. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4716 | /// |
| 4717 | /// \param CC the "context" location of the implicit conversion, i.e. |
| 4718 | /// the most location of the syntactic entity requiring the implicit |
| 4719 | /// conversion |
| 4720 | void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4721 | // Don't diagnose in unevaluated contexts. |
David Blaikie | 71f55f7 | 2012-08-06 22:47:24 +0000 | [diff] [blame] | 4722 | if (isUnevaluatedContext()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4723 | return; |
| 4724 | |
| 4725 | // Don't diagnose for value- or type-dependent expressions. |
| 4726 | if (E->isTypeDependent() || E->isValueDependent()) |
| 4727 | return; |
| 4728 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4729 | // Check for array bounds violations in cases where the check isn't triggered |
| 4730 | // elsewhere for other Expr types (like BinaryOperators), e.g. when an |
| 4731 | // ArraySubscriptExpr is on the RHS of a variable initialization. |
| 4732 | CheckArrayAccess(E); |
| 4733 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4734 | // This is not the right CC for (e.g.) a variable initialization. |
| 4735 | AnalyzeImplicitConversions(*this, E, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4736 | } |
| 4737 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4738 | void Sema::CheckBitFieldInitialization(SourceLocation InitLoc, |
| 4739 | FieldDecl *BitField, |
| 4740 | Expr *Init) { |
| 4741 | (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc); |
| 4742 | } |
| 4743 | |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4744 | /// CheckParmsForFunctionDef - Check that the parameters of the given |
| 4745 | /// function are appropriate for the definition of a function. This |
| 4746 | /// takes care of any checks that cannot be performed on the |
| 4747 | /// declaration itself, e.g., that the types of each of the function |
| 4748 | /// parameters are complete. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 4749 | bool Sema::CheckParmsForFunctionDef(ParmVarDecl **P, ParmVarDecl **PEnd, |
| 4750 | bool CheckParameterNames) { |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4751 | bool HasInvalidParm = false; |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 4752 | for (; P != PEnd; ++P) { |
| 4753 | ParmVarDecl *Param = *P; |
| 4754 | |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4755 | // C99 6.7.5.3p4: the parameters in a parameter type list in a |
| 4756 | // function declarator that is part of a function definition of |
| 4757 | // that function shall not have incomplete type. |
| 4758 | // |
| 4759 | // This is also C++ [dcl.fct]p6. |
| 4760 | if (!Param->isInvalidDecl() && |
| 4761 | RequireCompleteType(Param->getLocation(), Param->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4762 | diag::err_typecheck_decl_incomplete_type)) { |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4763 | Param->setInvalidDecl(); |
| 4764 | HasInvalidParm = true; |
| 4765 | } |
| 4766 | |
| 4767 | // C99 6.9.1p5: If the declarator includes a parameter type list, the |
| 4768 | // declaration of each parameter shall include an identifier. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 4769 | if (CheckParameterNames && |
| 4770 | Param->getIdentifier() == 0 && |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4771 | !Param->isImplicit() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4772 | !getLangOpts().CPlusPlus) |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4773 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
Sam Weinig | d17e340 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 4774 | |
| 4775 | // C99 6.7.5.3p12: |
| 4776 | // If the function declarator is not part of a definition of that |
| 4777 | // function, parameters may have incomplete type and may use the [*] |
| 4778 | // notation in their sequences of declarator specifiers to specify |
| 4779 | // variable length array types. |
| 4780 | QualType PType = Param->getOriginalType(); |
| 4781 | if (const ArrayType *AT = Context.getAsArrayType(PType)) { |
| 4782 | if (AT->getSizeModifier() == ArrayType::Star) { |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 4783 | // FIXME: This diagnosic should point the '[*]' if source-location |
Sam Weinig | d17e340 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 4784 | // information is added for it. |
| 4785 | Diag(Param->getLocation(), diag::err_array_star_in_function_definition); |
| 4786 | } |
| 4787 | } |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
| 4790 | return HasInvalidParm; |
| 4791 | } |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4792 | |
| 4793 | /// CheckCastAlign - Implements -Wcast-align, which warns when a |
| 4794 | /// pointer cast increases the alignment requirements. |
| 4795 | void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { |
| 4796 | // This is actually a lot of work to potentially be doing on every |
| 4797 | // 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] | 4798 | if (getDiagnostics().getDiagnosticLevel(diag::warn_cast_align, |
| 4799 | TRange.getBegin()) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 4800 | == DiagnosticsEngine::Ignored) |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4801 | return; |
| 4802 | |
| 4803 | // Ignore dependent types. |
| 4804 | if (T->isDependentType() || Op->getType()->isDependentType()) |
| 4805 | return; |
| 4806 | |
| 4807 | // Require that the destination be a pointer type. |
| 4808 | const PointerType *DestPtr = T->getAs<PointerType>(); |
| 4809 | if (!DestPtr) return; |
| 4810 | |
| 4811 | // If the destination has alignment 1, we're done. |
| 4812 | QualType DestPointee = DestPtr->getPointeeType(); |
| 4813 | if (DestPointee->isIncompleteType()) return; |
| 4814 | CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); |
| 4815 | if (DestAlign.isOne()) return; |
| 4816 | |
| 4817 | // Require that the source be a pointer type. |
| 4818 | const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); |
| 4819 | if (!SrcPtr) return; |
| 4820 | QualType SrcPointee = SrcPtr->getPointeeType(); |
| 4821 | |
| 4822 | // Whitelist casts from cv void*. We already implicitly |
| 4823 | // whitelisted casts to cv void*, since they have alignment 1. |
| 4824 | // Also whitelist casts involving incomplete types, which implicitly |
| 4825 | // includes 'void'. |
| 4826 | if (SrcPointee->isIncompleteType()) return; |
| 4827 | |
| 4828 | CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee); |
| 4829 | if (SrcAlign >= DestAlign) return; |
| 4830 | |
| 4831 | Diag(TRange.getBegin(), diag::warn_cast_align) |
| 4832 | << Op->getType() << T |
| 4833 | << static_cast<unsigned>(SrcAlign.getQuantity()) |
| 4834 | << static_cast<unsigned>(DestAlign.getQuantity()) |
| 4835 | << TRange << Op->getSourceRange(); |
| 4836 | } |
| 4837 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4838 | static const Type* getElementType(const Expr *BaseExpr) { |
| 4839 | const Type* EltType = BaseExpr->getType().getTypePtr(); |
| 4840 | if (EltType->isAnyPointerType()) |
| 4841 | return EltType->getPointeeType().getTypePtr(); |
| 4842 | else if (EltType->isArrayType()) |
| 4843 | return EltType->getBaseElementTypeUnsafe(); |
| 4844 | return EltType; |
| 4845 | } |
| 4846 | |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 4847 | /// \brief Check whether this array fits the idiom of a size-one tail padded |
| 4848 | /// array member of a struct. |
| 4849 | /// |
| 4850 | /// We avoid emitting out-of-bounds access warnings for such arrays as they are |
| 4851 | /// commonly used to emulate flexible arrays in C89 code. |
| 4852 | static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size, |
| 4853 | const NamedDecl *ND) { |
| 4854 | if (Size != 1 || !ND) return false; |
| 4855 | |
| 4856 | const FieldDecl *FD = dyn_cast<FieldDecl>(ND); |
| 4857 | if (!FD) return false; |
| 4858 | |
| 4859 | // Don't consider sizes resulting from macro expansions or template argument |
| 4860 | // substitution to form C89 tail-padded arrays. |
Sean Callanan | d2cf348 | 2012-05-04 18:22:53 +0000 | [diff] [blame] | 4861 | |
| 4862 | TypeSourceInfo *TInfo = FD->getTypeSourceInfo(); |
Ted Kremenek | 00e1f6f | 2012-05-09 05:35:08 +0000 | [diff] [blame] | 4863 | while (TInfo) { |
| 4864 | TypeLoc TL = TInfo->getTypeLoc(); |
| 4865 | // Look through typedefs. |
| 4866 | const TypedefTypeLoc *TTL = dyn_cast<TypedefTypeLoc>(&TL); |
| 4867 | if (TTL) { |
| 4868 | const TypedefNameDecl *TDL = TTL->getTypedefNameDecl(); |
| 4869 | TInfo = TDL->getTypeSourceInfo(); |
| 4870 | continue; |
| 4871 | } |
| 4872 | ConstantArrayTypeLoc CTL = cast<ConstantArrayTypeLoc>(TL); |
| 4873 | const Expr *SizeExpr = dyn_cast<IntegerLiteral>(CTL.getSizeExpr()); |
Sean Callanan | d2cf348 | 2012-05-04 18:22:53 +0000 | [diff] [blame] | 4874 | if (!SizeExpr || SizeExpr->getExprLoc().isMacroID()) |
| 4875 | return false; |
Ted Kremenek | 00e1f6f | 2012-05-09 05:35:08 +0000 | [diff] [blame] | 4876 | break; |
Sean Callanan | d2cf348 | 2012-05-04 18:22:53 +0000 | [diff] [blame] | 4877 | } |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 4878 | |
| 4879 | const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext()); |
Matt Beaumont-Gay | 381711c | 2011-11-29 22:43:53 +0000 | [diff] [blame] | 4880 | if (!RD) return false; |
| 4881 | if (RD->isUnion()) return false; |
| 4882 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4883 | if (!CRD->isStandardLayout()) return false; |
| 4884 | } |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 4885 | |
Benjamin Kramer | 22d4fed | 2011-08-06 03:04:42 +0000 | [diff] [blame] | 4886 | // See if this is the last field decl in the record. |
| 4887 | const Decl *D = FD; |
| 4888 | while ((D = D->getNextDeclInContext())) |
| 4889 | if (isa<FieldDecl>(D)) |
| 4890 | return false; |
| 4891 | return true; |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4894 | void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4895 | const ArraySubscriptExpr *ASE, |
Richard Smith | 25b009a | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 4896 | bool AllowOnePastEnd, bool IndexNegated) { |
Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 4897 | IndexExpr = IndexExpr->IgnoreParenImpCasts(); |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4898 | if (IndexExpr->isValueDependent()) |
| 4899 | return; |
| 4900 | |
Matt Beaumont-Gay | 8ef8f43 | 2011-12-12 22:35:02 +0000 | [diff] [blame] | 4901 | const Type *EffectiveType = getElementType(BaseExpr); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4902 | BaseExpr = BaseExpr->IgnoreParenCasts(); |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4903 | const ConstantArrayType *ArrayTy = |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4904 | Context.getAsConstantArrayType(BaseExpr->getType()); |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4905 | if (!ArrayTy) |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 4906 | return; |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 4907 | |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4908 | llvm::APSInt index; |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4909 | if (!IndexExpr->EvaluateAsInt(index, Context)) |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 4910 | return; |
Richard Smith | 25b009a | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 4911 | if (IndexNegated) |
| 4912 | index = -index; |
Ted Kremenek | 8fd0a5d | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 4913 | |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4914 | const NamedDecl *ND = NULL; |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4915 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
| 4916 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 4917 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4918 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4919 | |
Ted Kremenek | 9e060ca | 2011-02-23 23:06:04 +0000 | [diff] [blame] | 4920 | if (index.isUnsigned() || !index.isNegative()) { |
Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 4921 | llvm::APInt size = ArrayTy->getSize(); |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 4922 | if (!size.isStrictlyPositive()) |
| 4923 | return; |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4924 | |
| 4925 | const Type* BaseType = getElementType(BaseExpr); |
Nico Weber | de5998f | 2011-09-17 22:59:41 +0000 | [diff] [blame] | 4926 | if (BaseType != EffectiveType) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4927 | // Make sure we're comparing apples to apples when comparing index to size |
| 4928 | uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); |
| 4929 | uint64_t array_typesize = Context.getTypeSize(BaseType); |
Kaelyn Uhrain | d10f4bc | 2011-08-10 19:47:25 +0000 | [diff] [blame] | 4930 | // Handle ptrarith_typesize being zero, such as when casting to void* |
Kaelyn Uhrain | 18f1697 | 2011-08-10 18:49:28 +0000 | [diff] [blame] | 4931 | if (!ptrarith_typesize) ptrarith_typesize = 1; |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4932 | if (ptrarith_typesize != array_typesize) { |
| 4933 | // There's a cast to a different size type involved |
| 4934 | uint64_t ratio = array_typesize / ptrarith_typesize; |
| 4935 | // TODO: Be smarter about handling cases where array_typesize is not a |
| 4936 | // multiple of ptrarith_typesize |
| 4937 | if (ptrarith_typesize * ratio == array_typesize) |
| 4938 | size *= llvm::APInt(size.getBitWidth(), ratio); |
| 4939 | } |
| 4940 | } |
| 4941 | |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4942 | if (size.getBitWidth() > index.getBitWidth()) |
Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 4943 | index = index.zext(size.getBitWidth()); |
Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 4944 | else if (size.getBitWidth() < index.getBitWidth()) |
Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 4945 | size = size.zext(index.getBitWidth()); |
Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 4946 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4947 | // For array subscripting the index must be less than size, but for pointer |
| 4948 | // arithmetic also allow the index (offset) to be equal to size since |
| 4949 | // computing the next address after the end of the array is legal and |
| 4950 | // commonly done e.g. in C++ iterators and range-based for loops. |
Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 4951 | if (AllowOnePastEnd ? index.ule(size) : index.ult(size)) |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4952 | return; |
| 4953 | |
| 4954 | // Also don't warn for arrays of size 1 which are members of some |
| 4955 | // structure. These are often used to approximate flexible arrays in C89 |
| 4956 | // code. |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4957 | if (IsTailPaddedMemberArray(*this, size, ND)) |
Ted Kremenek | 8fd0a5d | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 4958 | return; |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4959 | |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4960 | // Suppress the warning if the subscript expression (as identified by the |
| 4961 | // ']' location) and the index expression are both from macro expansions |
| 4962 | // within a system header. |
| 4963 | if (ASE) { |
| 4964 | SourceLocation RBracketLoc = SourceMgr.getSpellingLoc( |
| 4965 | ASE->getRBracketLoc()); |
| 4966 | if (SourceMgr.isInSystemHeader(RBracketLoc)) { |
| 4967 | SourceLocation IndexLoc = SourceMgr.getSpellingLoc( |
| 4968 | IndexExpr->getLocStart()); |
| 4969 | if (SourceMgr.isFromSameFile(RBracketLoc, IndexLoc)) |
| 4970 | return; |
| 4971 | } |
| 4972 | } |
| 4973 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4974 | unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds; |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4975 | if (ASE) |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4976 | DiagID = diag::warn_array_index_exceeds_bounds; |
| 4977 | |
| 4978 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, |
| 4979 | PDiag(DiagID) << index.toString(10, true) |
| 4980 | << size.toString(10, true) |
| 4981 | << (unsigned)size.getLimitedValue(~0U) |
| 4982 | << IndexExpr->getSourceRange()); |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4983 | } else { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4984 | unsigned DiagID = diag::warn_array_index_precedes_bounds; |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4985 | if (!ASE) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4986 | DiagID = diag::warn_ptr_arith_precedes_bounds; |
| 4987 | if (index.isNegative()) index = -index; |
| 4988 | } |
| 4989 | |
| 4990 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, |
| 4991 | PDiag(DiagID) << index.toString(10, true) |
| 4992 | << IndexExpr->getSourceRange()); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 4993 | } |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 4994 | |
Matt Beaumont-Gay | cfbc5b5 | 2011-11-29 19:27:11 +0000 | [diff] [blame] | 4995 | if (!ND) { |
| 4996 | // Try harder to find a NamedDecl to point at in the note. |
| 4997 | while (const ArraySubscriptExpr *ASE = |
| 4998 | dyn_cast<ArraySubscriptExpr>(BaseExpr)) |
| 4999 | BaseExpr = ASE->getBase()->IgnoreParenCasts(); |
| 5000 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
| 5001 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); |
| 5002 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) |
| 5003 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); |
| 5004 | } |
| 5005 | |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 5006 | if (ND) |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5007 | DiagRuntimeBehavior(ND->getLocStart(), BaseExpr, |
| 5008 | PDiag(diag::note_array_index_out_of_bounds) |
| 5009 | << ND->getDeclName()); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 5010 | } |
| 5011 | |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5012 | void Sema::CheckArrayAccess(const Expr *expr) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5013 | int AllowOnePastEnd = 0; |
| 5014 | while (expr) { |
| 5015 | expr = expr->IgnoreParenImpCasts(); |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5016 | switch (expr->getStmtClass()) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5017 | case Stmt::ArraySubscriptExprClass: { |
| 5018 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr); |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 5019 | CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE, |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5020 | AllowOnePastEnd > 0); |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5021 | return; |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5022 | } |
| 5023 | case Stmt::UnaryOperatorClass: { |
| 5024 | // Only unwrap the * and & unary operators |
| 5025 | const UnaryOperator *UO = cast<UnaryOperator>(expr); |
| 5026 | expr = UO->getSubExpr(); |
| 5027 | switch (UO->getOpcode()) { |
| 5028 | case UO_AddrOf: |
| 5029 | AllowOnePastEnd++; |
| 5030 | break; |
| 5031 | case UO_Deref: |
| 5032 | AllowOnePastEnd--; |
| 5033 | break; |
| 5034 | default: |
| 5035 | return; |
| 5036 | } |
| 5037 | break; |
| 5038 | } |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5039 | case Stmt::ConditionalOperatorClass: { |
| 5040 | const ConditionalOperator *cond = cast<ConditionalOperator>(expr); |
| 5041 | if (const Expr *lhs = cond->getLHS()) |
| 5042 | CheckArrayAccess(lhs); |
| 5043 | if (const Expr *rhs = cond->getRHS()) |
| 5044 | CheckArrayAccess(rhs); |
| 5045 | return; |
| 5046 | } |
| 5047 | default: |
| 5048 | return; |
| 5049 | } |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 5050 | } |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5051 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5052 | |
| 5053 | //===--- CHECK: Objective-C retain cycles ----------------------------------// |
| 5054 | |
| 5055 | namespace { |
| 5056 | struct RetainCycleOwner { |
| 5057 | RetainCycleOwner() : Variable(0), Indirect(false) {} |
| 5058 | VarDecl *Variable; |
| 5059 | SourceRange Range; |
| 5060 | SourceLocation Loc; |
| 5061 | bool Indirect; |
| 5062 | |
| 5063 | void setLocsFrom(Expr *e) { |
| 5064 | Loc = e->getExprLoc(); |
| 5065 | Range = e->getSourceRange(); |
| 5066 | } |
| 5067 | }; |
| 5068 | } |
| 5069 | |
| 5070 | /// Consider whether capturing the given variable can possibly lead to |
| 5071 | /// a retain cycle. |
| 5072 | static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) { |
| 5073 | // In ARC, it's captured strongly iff the variable has __strong |
| 5074 | // lifetime. In MRR, it's captured strongly if the variable is |
| 5075 | // __block and has an appropriate type. |
| 5076 | if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 5077 | return false; |
| 5078 | |
| 5079 | owner.Variable = var; |
| 5080 | owner.setLocsFrom(ref); |
| 5081 | return true; |
| 5082 | } |
| 5083 | |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 5084 | static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5085 | while (true) { |
| 5086 | e = e->IgnoreParens(); |
| 5087 | if (CastExpr *cast = dyn_cast<CastExpr>(e)) { |
| 5088 | switch (cast->getCastKind()) { |
| 5089 | case CK_BitCast: |
| 5090 | case CK_LValueBitCast: |
| 5091 | case CK_LValueToRValue: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5092 | case CK_ARCReclaimReturnedObject: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5093 | e = cast->getSubExpr(); |
| 5094 | continue; |
| 5095 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5096 | default: |
| 5097 | return false; |
| 5098 | } |
| 5099 | } |
| 5100 | |
| 5101 | if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) { |
| 5102 | ObjCIvarDecl *ivar = ref->getDecl(); |
| 5103 | if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 5104 | return false; |
| 5105 | |
| 5106 | // Try to find a retain cycle in the base. |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 5107 | if (!findRetainCycleOwner(S, ref->getBase(), owner)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5108 | return false; |
| 5109 | |
| 5110 | if (ref->isFreeIvar()) owner.setLocsFrom(ref); |
| 5111 | owner.Indirect = true; |
| 5112 | return true; |
| 5113 | } |
| 5114 | |
| 5115 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 5116 | VarDecl *var = dyn_cast<VarDecl>(ref->getDecl()); |
| 5117 | if (!var) return false; |
| 5118 | return considerVariable(var, ref, owner); |
| 5119 | } |
| 5120 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5121 | if (MemberExpr *member = dyn_cast<MemberExpr>(e)) { |
| 5122 | if (member->isArrow()) return false; |
| 5123 | |
| 5124 | // Don't count this as an indirect ownership. |
| 5125 | e = member->getBase(); |
| 5126 | continue; |
| 5127 | } |
| 5128 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 5129 | if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { |
| 5130 | // Only pay attention to pseudo-objects on property references. |
| 5131 | ObjCPropertyRefExpr *pre |
| 5132 | = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm() |
| 5133 | ->IgnoreParens()); |
| 5134 | if (!pre) return false; |
| 5135 | if (pre->isImplicitProperty()) return false; |
| 5136 | ObjCPropertyDecl *property = pre->getExplicitProperty(); |
| 5137 | if (!property->isRetaining() && |
| 5138 | !(property->getPropertyIvarDecl() && |
| 5139 | property->getPropertyIvarDecl()->getType() |
| 5140 | .getObjCLifetime() == Qualifiers::OCL_Strong)) |
| 5141 | return false; |
| 5142 | |
| 5143 | owner.Indirect = true; |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 5144 | if (pre->isSuperReceiver()) { |
| 5145 | owner.Variable = S.getCurMethodDecl()->getSelfDecl(); |
| 5146 | if (!owner.Variable) |
| 5147 | return false; |
| 5148 | owner.Loc = pre->getLocation(); |
| 5149 | owner.Range = pre->getSourceRange(); |
| 5150 | return true; |
| 5151 | } |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 5152 | e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase()) |
| 5153 | ->getSourceExpr()); |
| 5154 | continue; |
| 5155 | } |
| 5156 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5157 | // Array ivars? |
| 5158 | |
| 5159 | return false; |
| 5160 | } |
| 5161 | } |
| 5162 | |
| 5163 | namespace { |
| 5164 | struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> { |
| 5165 | FindCaptureVisitor(ASTContext &Context, VarDecl *variable) |
| 5166 | : EvaluatedExprVisitor<FindCaptureVisitor>(Context), |
| 5167 | Variable(variable), Capturer(0) {} |
| 5168 | |
| 5169 | VarDecl *Variable; |
| 5170 | Expr *Capturer; |
| 5171 | |
| 5172 | void VisitDeclRefExpr(DeclRefExpr *ref) { |
| 5173 | if (ref->getDecl() == Variable && !Capturer) |
| 5174 | Capturer = ref; |
| 5175 | } |
| 5176 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5177 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) { |
| 5178 | if (Capturer) return; |
| 5179 | Visit(ref->getBase()); |
| 5180 | if (Capturer && ref->isFreeIvar()) |
| 5181 | Capturer = ref; |
| 5182 | } |
| 5183 | |
| 5184 | void VisitBlockExpr(BlockExpr *block) { |
| 5185 | // Look inside nested blocks |
| 5186 | if (block->getBlockDecl()->capturesVariable(Variable)) |
| 5187 | Visit(block->getBlockDecl()->getBody()); |
| 5188 | } |
| 5189 | }; |
| 5190 | } |
| 5191 | |
| 5192 | /// Check whether the given argument is a block which captures a |
| 5193 | /// variable. |
| 5194 | static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) { |
| 5195 | assert(owner.Variable && owner.Loc.isValid()); |
| 5196 | |
| 5197 | e = e->IgnoreParenCasts(); |
| 5198 | BlockExpr *block = dyn_cast<BlockExpr>(e); |
| 5199 | if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable)) |
| 5200 | return 0; |
| 5201 | |
| 5202 | FindCaptureVisitor visitor(S.Context, owner.Variable); |
| 5203 | visitor.Visit(block->getBlockDecl()->getBody()); |
| 5204 | return visitor.Capturer; |
| 5205 | } |
| 5206 | |
| 5207 | static void diagnoseRetainCycle(Sema &S, Expr *capturer, |
| 5208 | RetainCycleOwner &owner) { |
| 5209 | assert(capturer); |
| 5210 | assert(owner.Variable && owner.Loc.isValid()); |
| 5211 | |
| 5212 | S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle) |
| 5213 | << owner.Variable << capturer->getSourceRange(); |
| 5214 | S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner) |
| 5215 | << owner.Indirect << owner.Range; |
| 5216 | } |
| 5217 | |
| 5218 | /// Check for a keyword selector that starts with the word 'add' or |
| 5219 | /// 'set'. |
| 5220 | static bool isSetterLikeSelector(Selector sel) { |
| 5221 | if (sel.isUnarySelector()) return false; |
| 5222 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5223 | StringRef str = sel.getNameForSlot(0); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5224 | while (!str.empty() && str.front() == '_') str = str.substr(1); |
Ted Kremenek | 968a0ee | 2011-12-01 00:59:21 +0000 | [diff] [blame] | 5225 | if (str.startswith("set")) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5226 | str = str.substr(3); |
Ted Kremenek | 968a0ee | 2011-12-01 00:59:21 +0000 | [diff] [blame] | 5227 | else if (str.startswith("add")) { |
| 5228 | // Specially whitelist 'addOperationWithBlock:'. |
| 5229 | if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock")) |
| 5230 | return false; |
| 5231 | str = str.substr(3); |
| 5232 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5233 | else |
| 5234 | return false; |
| 5235 | |
| 5236 | if (str.empty()) return true; |
| 5237 | return !islower(str.front()); |
| 5238 | } |
| 5239 | |
| 5240 | /// Check a message send to see if it's likely to cause a retain cycle. |
| 5241 | void Sema::checkRetainCycles(ObjCMessageExpr *msg) { |
| 5242 | // Only check instance methods whose selector looks like a setter. |
| 5243 | if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector())) |
| 5244 | return; |
| 5245 | |
| 5246 | // Try to find a variable that the receiver is strongly owned by. |
| 5247 | RetainCycleOwner owner; |
| 5248 | if (msg->getReceiverKind() == ObjCMessageExpr::Instance) { |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 5249 | if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5250 | return; |
| 5251 | } else { |
| 5252 | assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance); |
| 5253 | owner.Variable = getCurMethodDecl()->getSelfDecl(); |
| 5254 | owner.Loc = msg->getSuperLoc(); |
| 5255 | owner.Range = msg->getSuperLoc(); |
| 5256 | } |
| 5257 | |
| 5258 | // Check whether the receiver is captured by any of the arguments. |
| 5259 | for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) |
| 5260 | if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) |
| 5261 | return diagnoseRetainCycle(*this, capturer, owner); |
| 5262 | } |
| 5263 | |
| 5264 | /// Check a property assign to see if it's likely to cause a retain cycle. |
| 5265 | void Sema::checkRetainCycles(Expr *receiver, Expr *argument) { |
| 5266 | RetainCycleOwner owner; |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 5267 | if (!findRetainCycleOwner(*this, receiver, owner)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5268 | return; |
| 5269 | |
| 5270 | if (Expr *capturer = findCapturingExpr(*this, argument, owner)) |
| 5271 | diagnoseRetainCycle(*this, capturer, owner); |
| 5272 | } |
| 5273 | |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5274 | bool Sema::checkUnsafeAssigns(SourceLocation Loc, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5275 | QualType LHS, Expr *RHS) { |
| 5276 | Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime(); |
| 5277 | if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone) |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5278 | return false; |
| 5279 | // strip off any implicit cast added to get to the one arc-specific |
| 5280 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5281 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5282 | Diag(Loc, diag::warn_arc_retained_assign) |
Fariborz Jahanian | bd2e27e | 2012-07-06 21:09:27 +0000 | [diff] [blame] | 5283 | << (LT == Qualifiers::OCL_ExplicitNone) << 1 |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5284 | << RHS->getSourceRange(); |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5285 | return true; |
| 5286 | } |
| 5287 | RHS = cast->getSubExpr(); |
| 5288 | } |
| 5289 | return false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5290 | } |
| 5291 | |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5292 | void Sema::checkUnsafeExprAssigns(SourceLocation Loc, |
| 5293 | Expr *LHS, Expr *RHS) { |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 5294 | QualType LHSType; |
| 5295 | // PropertyRef on LHS type need be directly obtained from |
| 5296 | // its declaration as it has a PsuedoType. |
| 5297 | ObjCPropertyRefExpr *PRE |
| 5298 | = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens()); |
| 5299 | if (PRE && !PRE->isImplicitProperty()) { |
| 5300 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
| 5301 | if (PD) |
| 5302 | LHSType = PD->getType(); |
| 5303 | } |
| 5304 | |
| 5305 | if (LHSType.isNull()) |
| 5306 | LHSType = LHS->getType(); |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5307 | if (checkUnsafeAssigns(Loc, LHSType, RHS)) |
| 5308 | return; |
| 5309 | Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime(); |
| 5310 | // FIXME. Check for other life times. |
| 5311 | if (LT != Qualifiers::OCL_None) |
| 5312 | return; |
| 5313 | |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 5314 | if (PRE) { |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5315 | if (PRE->isImplicitProperty()) |
| 5316 | return; |
| 5317 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
| 5318 | if (!PD) |
| 5319 | return; |
| 5320 | |
| 5321 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 5322 | if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) { |
| 5323 | // when 'assign' attribute was not explicitly specified |
| 5324 | // by user, ignore it and rely on property type itself |
| 5325 | // for lifetime info. |
| 5326 | unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten(); |
| 5327 | if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) && |
| 5328 | LHSType->isObjCRetainableType()) |
| 5329 | return; |
| 5330 | |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5331 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5332 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5333 | Diag(Loc, diag::warn_arc_retained_property_assign) |
| 5334 | << RHS->getSourceRange(); |
| 5335 | return; |
| 5336 | } |
| 5337 | RHS = cast->getSubExpr(); |
| 5338 | } |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 5339 | } |
Fariborz Jahanian | bd2e27e | 2012-07-06 21:09:27 +0000 | [diff] [blame] | 5340 | else if (Attributes & ObjCPropertyDecl::OBJC_PR_weak) { |
| 5341 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
| 5342 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
| 5343 | Diag(Loc, diag::warn_arc_retained_assign) |
| 5344 | << 0 << 0<< RHS->getSourceRange(); |
| 5345 | return; |
| 5346 | } |
| 5347 | RHS = cast->getSubExpr(); |
| 5348 | } |
| 5349 | } |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5350 | } |
| 5351 | } |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 5352 | |
| 5353 | //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===// |
| 5354 | |
| 5355 | namespace { |
| 5356 | bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr, |
| 5357 | SourceLocation StmtLoc, |
| 5358 | const NullStmt *Body) { |
| 5359 | // Do not warn if the body is a macro that expands to nothing, e.g: |
| 5360 | // |
| 5361 | // #define CALL(x) |
| 5362 | // if (condition) |
| 5363 | // CALL(0); |
| 5364 | // |
| 5365 | if (Body->hasLeadingEmptyMacro()) |
| 5366 | return false; |
| 5367 | |
| 5368 | // Get line numbers of statement and body. |
| 5369 | bool StmtLineInvalid; |
| 5370 | unsigned StmtLine = SourceMgr.getSpellingLineNumber(StmtLoc, |
| 5371 | &StmtLineInvalid); |
| 5372 | if (StmtLineInvalid) |
| 5373 | return false; |
| 5374 | |
| 5375 | bool BodyLineInvalid; |
| 5376 | unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(), |
| 5377 | &BodyLineInvalid); |
| 5378 | if (BodyLineInvalid) |
| 5379 | return false; |
| 5380 | |
| 5381 | // Warn if null statement and body are on the same line. |
| 5382 | if (StmtLine != BodyLine) |
| 5383 | return false; |
| 5384 | |
| 5385 | return true; |
| 5386 | } |
| 5387 | } // Unnamed namespace |
| 5388 | |
| 5389 | void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc, |
| 5390 | const Stmt *Body, |
| 5391 | unsigned DiagID) { |
| 5392 | // Since this is a syntactic check, don't emit diagnostic for template |
| 5393 | // instantiations, this just adds noise. |
| 5394 | if (CurrentInstantiationScope) |
| 5395 | return; |
| 5396 | |
| 5397 | // The body should be a null statement. |
| 5398 | const NullStmt *NBody = dyn_cast<NullStmt>(Body); |
| 5399 | if (!NBody) |
| 5400 | return; |
| 5401 | |
| 5402 | // Do the usual checks. |
| 5403 | if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) |
| 5404 | return; |
| 5405 | |
| 5406 | Diag(NBody->getSemiLoc(), DiagID); |
| 5407 | Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); |
| 5408 | } |
| 5409 | |
| 5410 | void Sema::DiagnoseEmptyLoopBody(const Stmt *S, |
| 5411 | const Stmt *PossibleBody) { |
| 5412 | assert(!CurrentInstantiationScope); // Ensured by caller |
| 5413 | |
| 5414 | SourceLocation StmtLoc; |
| 5415 | const Stmt *Body; |
| 5416 | unsigned DiagID; |
| 5417 | if (const ForStmt *FS = dyn_cast<ForStmt>(S)) { |
| 5418 | StmtLoc = FS->getRParenLoc(); |
| 5419 | Body = FS->getBody(); |
| 5420 | DiagID = diag::warn_empty_for_body; |
| 5421 | } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) { |
| 5422 | StmtLoc = WS->getCond()->getSourceRange().getEnd(); |
| 5423 | Body = WS->getBody(); |
| 5424 | DiagID = diag::warn_empty_while_body; |
| 5425 | } else |
| 5426 | return; // Neither `for' nor `while'. |
| 5427 | |
| 5428 | // The body should be a null statement. |
| 5429 | const NullStmt *NBody = dyn_cast<NullStmt>(Body); |
| 5430 | if (!NBody) |
| 5431 | return; |
| 5432 | |
| 5433 | // Skip expensive checks if diagnostic is disabled. |
| 5434 | if (Diags.getDiagnosticLevel(DiagID, NBody->getSemiLoc()) == |
| 5435 | DiagnosticsEngine::Ignored) |
| 5436 | return; |
| 5437 | |
| 5438 | // Do the usual checks. |
| 5439 | if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) |
| 5440 | return; |
| 5441 | |
| 5442 | // `for(...);' and `while(...);' are popular idioms, so in order to keep |
| 5443 | // noise level low, emit diagnostics only if for/while is followed by a |
| 5444 | // CompoundStmt, e.g.: |
| 5445 | // for (int i = 0; i < n; i++); |
| 5446 | // { |
| 5447 | // a(i); |
| 5448 | // } |
| 5449 | // or if for/while is followed by a statement with more indentation |
| 5450 | // than for/while itself: |
| 5451 | // for (int i = 0; i < n; i++); |
| 5452 | // a(i); |
| 5453 | bool ProbableTypo = isa<CompoundStmt>(PossibleBody); |
| 5454 | if (!ProbableTypo) { |
| 5455 | bool BodyColInvalid; |
| 5456 | unsigned BodyCol = SourceMgr.getPresumedColumnNumber( |
| 5457 | PossibleBody->getLocStart(), |
| 5458 | &BodyColInvalid); |
| 5459 | if (BodyColInvalid) |
| 5460 | return; |
| 5461 | |
| 5462 | bool StmtColInvalid; |
| 5463 | unsigned StmtCol = SourceMgr.getPresumedColumnNumber( |
| 5464 | S->getLocStart(), |
| 5465 | &StmtColInvalid); |
| 5466 | if (StmtColInvalid) |
| 5467 | return; |
| 5468 | |
| 5469 | if (BodyCol > StmtCol) |
| 5470 | ProbableTypo = true; |
| 5471 | } |
| 5472 | |
| 5473 | if (ProbableTypo) { |
| 5474 | Diag(NBody->getSemiLoc(), DiagID); |
| 5475 | Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); |
| 5476 | } |
| 5477 | } |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 5478 | |
| 5479 | //===--- Layout compatibility ----------------------------------------------// |
| 5480 | |
| 5481 | namespace { |
| 5482 | |
| 5483 | bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2); |
| 5484 | |
| 5485 | /// \brief Check if two enumeration types are layout-compatible. |
| 5486 | bool isLayoutCompatible(ASTContext &C, EnumDecl *ED1, EnumDecl *ED2) { |
| 5487 | // C++11 [dcl.enum] p8: |
| 5488 | // Two enumeration types are layout-compatible if they have the same |
| 5489 | // underlying type. |
| 5490 | return ED1->isComplete() && ED2->isComplete() && |
| 5491 | C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType()); |
| 5492 | } |
| 5493 | |
| 5494 | /// \brief Check if two fields are layout-compatible. |
| 5495 | bool isLayoutCompatible(ASTContext &C, FieldDecl *Field1, FieldDecl *Field2) { |
| 5496 | if (!isLayoutCompatible(C, Field1->getType(), Field2->getType())) |
| 5497 | return false; |
| 5498 | |
| 5499 | if (Field1->isBitField() != Field2->isBitField()) |
| 5500 | return false; |
| 5501 | |
| 5502 | if (Field1->isBitField()) { |
| 5503 | // Make sure that the bit-fields are the same length. |
| 5504 | unsigned Bits1 = Field1->getBitWidthValue(C); |
| 5505 | unsigned Bits2 = Field2->getBitWidthValue(C); |
| 5506 | |
| 5507 | if (Bits1 != Bits2) |
| 5508 | return false; |
| 5509 | } |
| 5510 | |
| 5511 | return true; |
| 5512 | } |
| 5513 | |
| 5514 | /// \brief Check if two standard-layout structs are layout-compatible. |
| 5515 | /// (C++11 [class.mem] p17) |
| 5516 | bool isLayoutCompatibleStruct(ASTContext &C, |
| 5517 | RecordDecl *RD1, |
| 5518 | RecordDecl *RD2) { |
| 5519 | // If both records are C++ classes, check that base classes match. |
| 5520 | if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1)) { |
| 5521 | // If one of records is a CXXRecordDecl we are in C++ mode, |
| 5522 | // thus the other one is a CXXRecordDecl, too. |
| 5523 | const CXXRecordDecl *D2CXX = cast<CXXRecordDecl>(RD2); |
| 5524 | // Check number of base classes. |
| 5525 | if (D1CXX->getNumBases() != D2CXX->getNumBases()) |
| 5526 | return false; |
| 5527 | |
| 5528 | // Check the base classes. |
| 5529 | for (CXXRecordDecl::base_class_const_iterator |
| 5530 | Base1 = D1CXX->bases_begin(), |
| 5531 | BaseEnd1 = D1CXX->bases_end(), |
| 5532 | Base2 = D2CXX->bases_begin(); |
| 5533 | Base1 != BaseEnd1; |
| 5534 | ++Base1, ++Base2) { |
| 5535 | if (!isLayoutCompatible(C, Base1->getType(), Base2->getType())) |
| 5536 | return false; |
| 5537 | } |
| 5538 | } else if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2)) { |
| 5539 | // If only RD2 is a C++ class, it should have zero base classes. |
| 5540 | if (D2CXX->getNumBases() > 0) |
| 5541 | return false; |
| 5542 | } |
| 5543 | |
| 5544 | // Check the fields. |
| 5545 | RecordDecl::field_iterator Field2 = RD2->field_begin(), |
| 5546 | Field2End = RD2->field_end(), |
| 5547 | Field1 = RD1->field_begin(), |
| 5548 | Field1End = RD1->field_end(); |
| 5549 | for ( ; Field1 != Field1End && Field2 != Field2End; ++Field1, ++Field2) { |
| 5550 | if (!isLayoutCompatible(C, *Field1, *Field2)) |
| 5551 | return false; |
| 5552 | } |
| 5553 | if (Field1 != Field1End || Field2 != Field2End) |
| 5554 | return false; |
| 5555 | |
| 5556 | return true; |
| 5557 | } |
| 5558 | |
| 5559 | /// \brief Check if two standard-layout unions are layout-compatible. |
| 5560 | /// (C++11 [class.mem] p18) |
| 5561 | bool isLayoutCompatibleUnion(ASTContext &C, |
| 5562 | RecordDecl *RD1, |
| 5563 | RecordDecl *RD2) { |
| 5564 | llvm::SmallPtrSet<FieldDecl *, 8> UnmatchedFields; |
| 5565 | for (RecordDecl::field_iterator Field2 = RD2->field_begin(), |
| 5566 | Field2End = RD2->field_end(); |
| 5567 | Field2 != Field2End; ++Field2) { |
| 5568 | UnmatchedFields.insert(*Field2); |
| 5569 | } |
| 5570 | |
| 5571 | for (RecordDecl::field_iterator Field1 = RD1->field_begin(), |
| 5572 | Field1End = RD1->field_end(); |
| 5573 | Field1 != Field1End; ++Field1) { |
| 5574 | llvm::SmallPtrSet<FieldDecl *, 8>::iterator |
| 5575 | I = UnmatchedFields.begin(), |
| 5576 | E = UnmatchedFields.end(); |
| 5577 | |
| 5578 | for ( ; I != E; ++I) { |
| 5579 | if (isLayoutCompatible(C, *Field1, *I)) { |
| 5580 | bool Result = UnmatchedFields.erase(*I); |
| 5581 | (void) Result; |
| 5582 | assert(Result); |
| 5583 | break; |
| 5584 | } |
| 5585 | } |
| 5586 | if (I == E) |
| 5587 | return false; |
| 5588 | } |
| 5589 | |
| 5590 | return UnmatchedFields.empty(); |
| 5591 | } |
| 5592 | |
| 5593 | bool isLayoutCompatible(ASTContext &C, RecordDecl *RD1, RecordDecl *RD2) { |
| 5594 | if (RD1->isUnion() != RD2->isUnion()) |
| 5595 | return false; |
| 5596 | |
| 5597 | if (RD1->isUnion()) |
| 5598 | return isLayoutCompatibleUnion(C, RD1, RD2); |
| 5599 | else |
| 5600 | return isLayoutCompatibleStruct(C, RD1, RD2); |
| 5601 | } |
| 5602 | |
| 5603 | /// \brief Check if two types are layout-compatible in C++11 sense. |
| 5604 | bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2) { |
| 5605 | if (T1.isNull() || T2.isNull()) |
| 5606 | return false; |
| 5607 | |
| 5608 | // C++11 [basic.types] p11: |
| 5609 | // If two types T1 and T2 are the same type, then T1 and T2 are |
| 5610 | // layout-compatible types. |
| 5611 | if (C.hasSameType(T1, T2)) |
| 5612 | return true; |
| 5613 | |
| 5614 | T1 = T1.getCanonicalType().getUnqualifiedType(); |
| 5615 | T2 = T2.getCanonicalType().getUnqualifiedType(); |
| 5616 | |
| 5617 | const Type::TypeClass TC1 = T1->getTypeClass(); |
| 5618 | const Type::TypeClass TC2 = T2->getTypeClass(); |
| 5619 | |
| 5620 | if (TC1 != TC2) |
| 5621 | return false; |
| 5622 | |
| 5623 | if (TC1 == Type::Enum) { |
| 5624 | return isLayoutCompatible(C, |
| 5625 | cast<EnumType>(T1)->getDecl(), |
| 5626 | cast<EnumType>(T2)->getDecl()); |
| 5627 | } else if (TC1 == Type::Record) { |
| 5628 | if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType()) |
| 5629 | return false; |
| 5630 | |
| 5631 | return isLayoutCompatible(C, |
| 5632 | cast<RecordType>(T1)->getDecl(), |
| 5633 | cast<RecordType>(T2)->getDecl()); |
| 5634 | } |
| 5635 | |
| 5636 | return false; |
| 5637 | } |
| 5638 | } |
| 5639 | |
| 5640 | //===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----// |
| 5641 | |
| 5642 | namespace { |
| 5643 | /// \brief Given a type tag expression find the type tag itself. |
| 5644 | /// |
| 5645 | /// \param TypeExpr Type tag expression, as it appears in user's code. |
| 5646 | /// |
| 5647 | /// \param VD Declaration of an identifier that appears in a type tag. |
| 5648 | /// |
| 5649 | /// \param MagicValue Type tag magic value. |
| 5650 | bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx, |
| 5651 | const ValueDecl **VD, uint64_t *MagicValue) { |
| 5652 | while(true) { |
| 5653 | if (!TypeExpr) |
| 5654 | return false; |
| 5655 | |
| 5656 | TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts(); |
| 5657 | |
| 5658 | switch (TypeExpr->getStmtClass()) { |
| 5659 | case Stmt::UnaryOperatorClass: { |
| 5660 | const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr); |
| 5661 | if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) { |
| 5662 | TypeExpr = UO->getSubExpr(); |
| 5663 | continue; |
| 5664 | } |
| 5665 | return false; |
| 5666 | } |
| 5667 | |
| 5668 | case Stmt::DeclRefExprClass: { |
| 5669 | const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr); |
| 5670 | *VD = DRE->getDecl(); |
| 5671 | return true; |
| 5672 | } |
| 5673 | |
| 5674 | case Stmt::IntegerLiteralClass: { |
| 5675 | const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr); |
| 5676 | llvm::APInt MagicValueAPInt = IL->getValue(); |
| 5677 | if (MagicValueAPInt.getActiveBits() <= 64) { |
| 5678 | *MagicValue = MagicValueAPInt.getZExtValue(); |
| 5679 | return true; |
| 5680 | } else |
| 5681 | return false; |
| 5682 | } |
| 5683 | |
| 5684 | case Stmt::BinaryConditionalOperatorClass: |
| 5685 | case Stmt::ConditionalOperatorClass: { |
| 5686 | const AbstractConditionalOperator *ACO = |
| 5687 | cast<AbstractConditionalOperator>(TypeExpr); |
| 5688 | bool Result; |
| 5689 | if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx)) { |
| 5690 | if (Result) |
| 5691 | TypeExpr = ACO->getTrueExpr(); |
| 5692 | else |
| 5693 | TypeExpr = ACO->getFalseExpr(); |
| 5694 | continue; |
| 5695 | } |
| 5696 | return false; |
| 5697 | } |
| 5698 | |
| 5699 | case Stmt::BinaryOperatorClass: { |
| 5700 | const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr); |
| 5701 | if (BO->getOpcode() == BO_Comma) { |
| 5702 | TypeExpr = BO->getRHS(); |
| 5703 | continue; |
| 5704 | } |
| 5705 | return false; |
| 5706 | } |
| 5707 | |
| 5708 | default: |
| 5709 | return false; |
| 5710 | } |
| 5711 | } |
| 5712 | } |
| 5713 | |
| 5714 | /// \brief Retrieve the C type corresponding to type tag TypeExpr. |
| 5715 | /// |
| 5716 | /// \param TypeExpr Expression that specifies a type tag. |
| 5717 | /// |
| 5718 | /// \param MagicValues Registered magic values. |
| 5719 | /// |
| 5720 | /// \param FoundWrongKind Set to true if a type tag was found, but of a wrong |
| 5721 | /// kind. |
| 5722 | /// |
| 5723 | /// \param TypeInfo Information about the corresponding C type. |
| 5724 | /// |
| 5725 | /// \returns true if the corresponding C type was found. |
| 5726 | bool GetMatchingCType( |
| 5727 | const IdentifierInfo *ArgumentKind, |
| 5728 | const Expr *TypeExpr, const ASTContext &Ctx, |
| 5729 | const llvm::DenseMap<Sema::TypeTagMagicValue, |
| 5730 | Sema::TypeTagData> *MagicValues, |
| 5731 | bool &FoundWrongKind, |
| 5732 | Sema::TypeTagData &TypeInfo) { |
| 5733 | FoundWrongKind = false; |
| 5734 | |
| 5735 | // Variable declaration that has type_tag_for_datatype attribute. |
| 5736 | const ValueDecl *VD = NULL; |
| 5737 | |
| 5738 | uint64_t MagicValue; |
| 5739 | |
| 5740 | if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue)) |
| 5741 | return false; |
| 5742 | |
| 5743 | if (VD) { |
| 5744 | for (specific_attr_iterator<TypeTagForDatatypeAttr> |
| 5745 | I = VD->specific_attr_begin<TypeTagForDatatypeAttr>(), |
| 5746 | E = VD->specific_attr_end<TypeTagForDatatypeAttr>(); |
| 5747 | I != E; ++I) { |
| 5748 | if (I->getArgumentKind() != ArgumentKind) { |
| 5749 | FoundWrongKind = true; |
| 5750 | return false; |
| 5751 | } |
| 5752 | TypeInfo.Type = I->getMatchingCType(); |
| 5753 | TypeInfo.LayoutCompatible = I->getLayoutCompatible(); |
| 5754 | TypeInfo.MustBeNull = I->getMustBeNull(); |
| 5755 | return true; |
| 5756 | } |
| 5757 | return false; |
| 5758 | } |
| 5759 | |
| 5760 | if (!MagicValues) |
| 5761 | return false; |
| 5762 | |
| 5763 | llvm::DenseMap<Sema::TypeTagMagicValue, |
| 5764 | Sema::TypeTagData>::const_iterator I = |
| 5765 | MagicValues->find(std::make_pair(ArgumentKind, MagicValue)); |
| 5766 | if (I == MagicValues->end()) |
| 5767 | return false; |
| 5768 | |
| 5769 | TypeInfo = I->second; |
| 5770 | return true; |
| 5771 | } |
| 5772 | } // unnamed namespace |
| 5773 | |
| 5774 | void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind, |
| 5775 | uint64_t MagicValue, QualType Type, |
| 5776 | bool LayoutCompatible, |
| 5777 | bool MustBeNull) { |
| 5778 | if (!TypeTagForDatatypeMagicValues) |
| 5779 | TypeTagForDatatypeMagicValues.reset( |
| 5780 | new llvm::DenseMap<TypeTagMagicValue, TypeTagData>); |
| 5781 | |
| 5782 | TypeTagMagicValue Magic(ArgumentKind, MagicValue); |
| 5783 | (*TypeTagForDatatypeMagicValues)[Magic] = |
| 5784 | TypeTagData(Type, LayoutCompatible, MustBeNull); |
| 5785 | } |
| 5786 | |
| 5787 | namespace { |
| 5788 | bool IsSameCharType(QualType T1, QualType T2) { |
| 5789 | const BuiltinType *BT1 = T1->getAs<BuiltinType>(); |
| 5790 | if (!BT1) |
| 5791 | return false; |
| 5792 | |
| 5793 | const BuiltinType *BT2 = T2->getAs<BuiltinType>(); |
| 5794 | if (!BT2) |
| 5795 | return false; |
| 5796 | |
| 5797 | BuiltinType::Kind T1Kind = BT1->getKind(); |
| 5798 | BuiltinType::Kind T2Kind = BT2->getKind(); |
| 5799 | |
| 5800 | return (T1Kind == BuiltinType::SChar && T2Kind == BuiltinType::Char_S) || |
| 5801 | (T1Kind == BuiltinType::UChar && T2Kind == BuiltinType::Char_U) || |
| 5802 | (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) || |
| 5803 | (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar); |
| 5804 | } |
| 5805 | } // unnamed namespace |
| 5806 | |
| 5807 | void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr, |
| 5808 | const Expr * const *ExprArgs) { |
| 5809 | const IdentifierInfo *ArgumentKind = Attr->getArgumentKind(); |
| 5810 | bool IsPointerAttr = Attr->getIsPointer(); |
| 5811 | |
| 5812 | const Expr *TypeTagExpr = ExprArgs[Attr->getTypeTagIdx()]; |
| 5813 | bool FoundWrongKind; |
| 5814 | TypeTagData TypeInfo; |
| 5815 | if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context, |
| 5816 | TypeTagForDatatypeMagicValues.get(), |
| 5817 | FoundWrongKind, TypeInfo)) { |
| 5818 | if (FoundWrongKind) |
| 5819 | Diag(TypeTagExpr->getExprLoc(), |
| 5820 | diag::warn_type_tag_for_datatype_wrong_kind) |
| 5821 | << TypeTagExpr->getSourceRange(); |
| 5822 | return; |
| 5823 | } |
| 5824 | |
| 5825 | const Expr *ArgumentExpr = ExprArgs[Attr->getArgumentIdx()]; |
| 5826 | if (IsPointerAttr) { |
| 5827 | // Skip implicit cast of pointer to `void *' (as a function argument). |
| 5828 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr)) |
| 5829 | if (ICE->getType()->isVoidPointerType()) |
| 5830 | ArgumentExpr = ICE->getSubExpr(); |
| 5831 | } |
| 5832 | QualType ArgumentType = ArgumentExpr->getType(); |
| 5833 | |
| 5834 | // Passing a `void*' pointer shouldn't trigger a warning. |
| 5835 | if (IsPointerAttr && ArgumentType->isVoidPointerType()) |
| 5836 | return; |
| 5837 | |
| 5838 | if (TypeInfo.MustBeNull) { |
| 5839 | // Type tag with matching void type requires a null pointer. |
| 5840 | if (!ArgumentExpr->isNullPointerConstant(Context, |
| 5841 | Expr::NPC_ValueDependentIsNotNull)) { |
| 5842 | Diag(ArgumentExpr->getExprLoc(), |
| 5843 | diag::warn_type_safety_null_pointer_required) |
| 5844 | << ArgumentKind->getName() |
| 5845 | << ArgumentExpr->getSourceRange() |
| 5846 | << TypeTagExpr->getSourceRange(); |
| 5847 | } |
| 5848 | return; |
| 5849 | } |
| 5850 | |
| 5851 | QualType RequiredType = TypeInfo.Type; |
| 5852 | if (IsPointerAttr) |
| 5853 | RequiredType = Context.getPointerType(RequiredType); |
| 5854 | |
| 5855 | bool mismatch = false; |
| 5856 | if (!TypeInfo.LayoutCompatible) { |
| 5857 | mismatch = !Context.hasSameType(ArgumentType, RequiredType); |
| 5858 | |
| 5859 | // C++11 [basic.fundamental] p1: |
| 5860 | // Plain char, signed char, and unsigned char are three distinct types. |
| 5861 | // |
| 5862 | // But we treat plain `char' as equivalent to `signed char' or `unsigned |
| 5863 | // char' depending on the current char signedness mode. |
| 5864 | if (mismatch) |
| 5865 | if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(), |
| 5866 | RequiredType->getPointeeType())) || |
| 5867 | (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType))) |
| 5868 | mismatch = false; |
| 5869 | } else |
| 5870 | if (IsPointerAttr) |
| 5871 | mismatch = !isLayoutCompatible(Context, |
| 5872 | ArgumentType->getPointeeType(), |
| 5873 | RequiredType->getPointeeType()); |
| 5874 | else |
| 5875 | mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType); |
| 5876 | |
| 5877 | if (mismatch) |
| 5878 | Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch) |
| 5879 | << ArgumentType << ArgumentKind->getName() |
| 5880 | << TypeInfo.LayoutCompatible << RequiredType |
| 5881 | << ArgumentExpr->getSourceRange() |
| 5882 | << TypeTagExpr->getSourceRange(); |
| 5883 | } |
| 5884 | |