Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1 | //===--- SemaChecking.cpp - Extra Semantic Checking -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10 | // This file implements extra semantic analysis beyond what is enforced |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 11 | // by the C type system. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Initialization.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Sema.h" |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaInternal.h" |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Initialization.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 19 | #include "clang/Sema/ScopeInfo.h" |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/Analyses/FormatString.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 22 | #include "clang/AST/CharUnits.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprObjC.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 27 | #include "clang/AST/EvaluatedExprVisitor.h" |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 28 | #include "clang/AST/DeclObjC.h" |
| 29 | #include "clang/AST/StmtCXX.h" |
| 30 | #include "clang/AST/StmtObjC.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Preprocessor.h" |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/BitVector.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallString.h" |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/STLExtras.h" |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 36 | #include "clang/Basic/TargetBuiltins.h" |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 37 | #include "clang/Basic/TargetInfo.h" |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 38 | #include "clang/Basic/ConvertUTF.h" |
Zhongxing Xu | a1f3dba | 2009-05-20 01:55:10 +0000 | [diff] [blame] | 39 | #include <limits> |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 40 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 41 | using namespace sema; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 43 | SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, |
| 44 | unsigned ByteNo) const { |
Chris Lattner | 08f92e3 | 2010-11-17 07:37:15 +0000 | [diff] [blame] | 45 | return SL->getLocationOfByte(ByteNo, PP.getSourceManager(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 46 | PP.getLangOpts(), PP.getTargetInfo()); |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 47 | } |
| 48 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 49 | /// Checks that a call expression's argument count is the desired number. |
| 50 | /// This is useful when doing custom type-checking. Returns true on error. |
| 51 | static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) { |
| 52 | unsigned argCount = call->getNumArgs(); |
| 53 | if (argCount == desiredArgCount) return false; |
| 54 | |
| 55 | if (argCount < desiredArgCount) |
| 56 | return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 57 | << 0 /*function call*/ << desiredArgCount << argCount |
| 58 | << call->getSourceRange(); |
| 59 | |
| 60 | // Highlight all the excess arguments. |
| 61 | SourceRange range(call->getArg(desiredArgCount)->getLocStart(), |
| 62 | call->getArg(argCount - 1)->getLocEnd()); |
| 63 | |
| 64 | return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args) |
| 65 | << 0 /*function call*/ << desiredArgCount << argCount |
| 66 | << call->getArg(1)->getSourceRange(); |
| 67 | } |
| 68 | |
Julien Lerouge | e593921 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 69 | /// Check that the first argument to __builtin_annotation is an integer |
| 70 | /// and the second argument is a non-wide string literal. |
| 71 | static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) { |
| 72 | if (checkArgCount(S, TheCall, 2)) |
| 73 | return true; |
| 74 | |
| 75 | // First argument should be an integer. |
| 76 | Expr *ValArg = TheCall->getArg(0); |
| 77 | QualType Ty = ValArg->getType(); |
| 78 | if (!Ty->isIntegerType()) { |
| 79 | S.Diag(ValArg->getLocStart(), diag::err_builtin_annotation_first_arg) |
| 80 | << ValArg->getSourceRange(); |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 81 | return true; |
| 82 | } |
Julien Lerouge | e593921 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 83 | |
| 84 | // Second argument should be a constant string. |
| 85 | Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts(); |
| 86 | StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg); |
| 87 | if (!Literal || !Literal->isAscii()) { |
| 88 | S.Diag(StrArg->getLocStart(), diag::err_builtin_annotation_second_arg) |
| 89 | << StrArg->getSourceRange(); |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | TheCall->setType(Ty); |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 94 | return false; |
| 95 | } |
| 96 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 97 | ExprResult |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 98 | Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 99 | ExprResult TheCallResult(Owned(TheCall)); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 946928f | 2010-10-01 23:23:24 +0000 | [diff] [blame] | 101 | // Find out if any arguments are required to be integer constant expressions. |
| 102 | unsigned ICEArguments = 0; |
| 103 | ASTContext::GetBuiltinTypeError Error; |
| 104 | Context.GetBuiltinType(BuiltinID, Error, &ICEArguments); |
| 105 | if (Error != ASTContext::GE_None) |
| 106 | ICEArguments = 0; // Don't diagnose previously diagnosed errors. |
| 107 | |
| 108 | // If any arguments are required to be ICE's, check and diagnose. |
| 109 | for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) { |
| 110 | // Skip arguments not required to be ICE's. |
| 111 | if ((ICEArguments & (1 << ArgNo)) == 0) continue; |
| 112 | |
| 113 | llvm::APSInt Result; |
| 114 | if (SemaBuiltinConstantArg(TheCall, ArgNo, Result)) |
| 115 | return true; |
| 116 | ICEArguments &= ~(1 << ArgNo); |
| 117 | } |
| 118 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 119 | switch (BuiltinID) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 120 | case Builtin::BI__builtin___CFStringMakeConstantString: |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 121 | assert(TheCall->getNumArgs() == 1 && |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 122 | "Wrong # arguments to builtin CFStringMakeConstantString"); |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 123 | if (CheckObjCString(TheCall->getArg(0))) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 124 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 125 | break; |
Ted Kremenek | 49ff7a1 | 2008-07-09 17:58:53 +0000 | [diff] [blame] | 126 | case Builtin::BI__builtin_stdarg_start: |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 127 | case Builtin::BI__builtin_va_start: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 128 | if (SemaBuiltinVAStart(TheCall)) |
| 129 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 130 | break; |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 131 | case Builtin::BI__builtin_isgreater: |
| 132 | case Builtin::BI__builtin_isgreaterequal: |
| 133 | case Builtin::BI__builtin_isless: |
| 134 | case Builtin::BI__builtin_islessequal: |
| 135 | case Builtin::BI__builtin_islessgreater: |
| 136 | case Builtin::BI__builtin_isunordered: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 137 | if (SemaBuiltinUnorderedCompare(TheCall)) |
| 138 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 139 | break; |
Benjamin Kramer | e771a7a | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 140 | case Builtin::BI__builtin_fpclassify: |
| 141 | if (SemaBuiltinFPClassification(TheCall, 6)) |
| 142 | return ExprError(); |
| 143 | break; |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 144 | case Builtin::BI__builtin_isfinite: |
| 145 | case Builtin::BI__builtin_isinf: |
| 146 | case Builtin::BI__builtin_isinf_sign: |
| 147 | case Builtin::BI__builtin_isnan: |
| 148 | case Builtin::BI__builtin_isnormal: |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 149 | if (SemaBuiltinFPClassification(TheCall, 1)) |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 150 | return ExprError(); |
| 151 | break; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 152 | case Builtin::BI__builtin_shufflevector: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 153 | return SemaBuiltinShuffleVector(TheCall); |
| 154 | // TheCall will be freed by the smart pointer here, but that's fine, since |
| 155 | // SemaBuiltinShuffleVector guts it, but then doesn't release it. |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 156 | case Builtin::BI__builtin_prefetch: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 157 | if (SemaBuiltinPrefetch(TheCall)) |
| 158 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 159 | break; |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 160 | case Builtin::BI__builtin_object_size: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 161 | if (SemaBuiltinObjectSize(TheCall)) |
| 162 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 163 | break; |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 164 | case Builtin::BI__builtin_longjmp: |
| 165 | if (SemaBuiltinLongjmp(TheCall)) |
| 166 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 167 | break; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 168 | |
| 169 | case Builtin::BI__builtin_classify_type: |
| 170 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 171 | TheCall->setType(Context.IntTy); |
| 172 | break; |
Chris Lattner | 75c29a0 | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 173 | case Builtin::BI__builtin_constant_p: |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 174 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 175 | TheCall->setType(Context.IntTy); |
Chris Lattner | 75c29a0 | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 176 | break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 177 | case Builtin::BI__sync_fetch_and_add: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 178 | case Builtin::BI__sync_fetch_and_add_1: |
| 179 | case Builtin::BI__sync_fetch_and_add_2: |
| 180 | case Builtin::BI__sync_fetch_and_add_4: |
| 181 | case Builtin::BI__sync_fetch_and_add_8: |
| 182 | case Builtin::BI__sync_fetch_and_add_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 183 | case Builtin::BI__sync_fetch_and_sub: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 184 | case Builtin::BI__sync_fetch_and_sub_1: |
| 185 | case Builtin::BI__sync_fetch_and_sub_2: |
| 186 | case Builtin::BI__sync_fetch_and_sub_4: |
| 187 | case Builtin::BI__sync_fetch_and_sub_8: |
| 188 | case Builtin::BI__sync_fetch_and_sub_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 189 | case Builtin::BI__sync_fetch_and_or: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 190 | case Builtin::BI__sync_fetch_and_or_1: |
| 191 | case Builtin::BI__sync_fetch_and_or_2: |
| 192 | case Builtin::BI__sync_fetch_and_or_4: |
| 193 | case Builtin::BI__sync_fetch_and_or_8: |
| 194 | case Builtin::BI__sync_fetch_and_or_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 195 | case Builtin::BI__sync_fetch_and_and: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 196 | case Builtin::BI__sync_fetch_and_and_1: |
| 197 | case Builtin::BI__sync_fetch_and_and_2: |
| 198 | case Builtin::BI__sync_fetch_and_and_4: |
| 199 | case Builtin::BI__sync_fetch_and_and_8: |
| 200 | case Builtin::BI__sync_fetch_and_and_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 201 | case Builtin::BI__sync_fetch_and_xor: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 202 | case Builtin::BI__sync_fetch_and_xor_1: |
| 203 | case Builtin::BI__sync_fetch_and_xor_2: |
| 204 | case Builtin::BI__sync_fetch_and_xor_4: |
| 205 | case Builtin::BI__sync_fetch_and_xor_8: |
| 206 | case Builtin::BI__sync_fetch_and_xor_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 207 | case Builtin::BI__sync_add_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 208 | case Builtin::BI__sync_add_and_fetch_1: |
| 209 | case Builtin::BI__sync_add_and_fetch_2: |
| 210 | case Builtin::BI__sync_add_and_fetch_4: |
| 211 | case Builtin::BI__sync_add_and_fetch_8: |
| 212 | case Builtin::BI__sync_add_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 213 | case Builtin::BI__sync_sub_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 214 | case Builtin::BI__sync_sub_and_fetch_1: |
| 215 | case Builtin::BI__sync_sub_and_fetch_2: |
| 216 | case Builtin::BI__sync_sub_and_fetch_4: |
| 217 | case Builtin::BI__sync_sub_and_fetch_8: |
| 218 | case Builtin::BI__sync_sub_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 219 | case Builtin::BI__sync_and_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 220 | case Builtin::BI__sync_and_and_fetch_1: |
| 221 | case Builtin::BI__sync_and_and_fetch_2: |
| 222 | case Builtin::BI__sync_and_and_fetch_4: |
| 223 | case Builtin::BI__sync_and_and_fetch_8: |
| 224 | case Builtin::BI__sync_and_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 225 | case Builtin::BI__sync_or_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 226 | case Builtin::BI__sync_or_and_fetch_1: |
| 227 | case Builtin::BI__sync_or_and_fetch_2: |
| 228 | case Builtin::BI__sync_or_and_fetch_4: |
| 229 | case Builtin::BI__sync_or_and_fetch_8: |
| 230 | case Builtin::BI__sync_or_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 231 | case Builtin::BI__sync_xor_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 232 | case Builtin::BI__sync_xor_and_fetch_1: |
| 233 | case Builtin::BI__sync_xor_and_fetch_2: |
| 234 | case Builtin::BI__sync_xor_and_fetch_4: |
| 235 | case Builtin::BI__sync_xor_and_fetch_8: |
| 236 | case Builtin::BI__sync_xor_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 237 | case Builtin::BI__sync_val_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 238 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 239 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 240 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 241 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 242 | case Builtin::BI__sync_val_compare_and_swap_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 243 | case Builtin::BI__sync_bool_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 244 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 245 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 246 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 247 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 248 | case Builtin::BI__sync_bool_compare_and_swap_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 249 | case Builtin::BI__sync_lock_test_and_set: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 250 | case Builtin::BI__sync_lock_test_and_set_1: |
| 251 | case Builtin::BI__sync_lock_test_and_set_2: |
| 252 | case Builtin::BI__sync_lock_test_and_set_4: |
| 253 | case Builtin::BI__sync_lock_test_and_set_8: |
| 254 | case Builtin::BI__sync_lock_test_and_set_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 255 | case Builtin::BI__sync_lock_release: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 256 | case Builtin::BI__sync_lock_release_1: |
| 257 | case Builtin::BI__sync_lock_release_2: |
| 258 | case Builtin::BI__sync_lock_release_4: |
| 259 | case Builtin::BI__sync_lock_release_8: |
| 260 | case Builtin::BI__sync_lock_release_16: |
Chris Lattner | 23aa9c8 | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 261 | case Builtin::BI__sync_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 262 | case Builtin::BI__sync_swap_1: |
| 263 | case Builtin::BI__sync_swap_2: |
| 264 | case Builtin::BI__sync_swap_4: |
| 265 | case Builtin::BI__sync_swap_8: |
| 266 | case Builtin::BI__sync_swap_16: |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 267 | return SemaBuiltinAtomicOverloaded(move(TheCallResult)); |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 268 | #define BUILTIN(ID, TYPE, ATTRS) |
| 269 | #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ |
| 270 | case Builtin::BI##ID: \ |
| 271 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::AO##ID); |
| 272 | #include "clang/Basic/Builtins.def" |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 273 | case Builtin::BI__builtin_annotation: |
Julien Lerouge | e593921 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 274 | if (SemaBuiltinAnnotation(*this, TheCall)) |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 275 | return ExprError(); |
| 276 | break; |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | // Since the target specific builtins for each arch overlap, only check those |
| 280 | // of the arch we are compiling for. |
| 281 | if (BuiltinID >= Builtin::FirstTSBuiltin) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 282 | switch (Context.getTargetInfo().getTriple().getArch()) { |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 283 | case llvm::Triple::arm: |
| 284 | case llvm::Triple::thumb: |
| 285 | if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall)) |
| 286 | return ExprError(); |
| 287 | break; |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 288 | default: |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return move(TheCallResult); |
| 294 | } |
| 295 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 296 | // Get the valid immediate range for the specified NEON type code. |
| 297 | static unsigned RFT(unsigned t, bool shift = false) { |
Bob Wilson | da95f73 | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 298 | NeonTypeFlags Type(t); |
| 299 | int IsQuad = Type.isQuad(); |
| 300 | switch (Type.getEltType()) { |
| 301 | case NeonTypeFlags::Int8: |
| 302 | case NeonTypeFlags::Poly8: |
| 303 | return shift ? 7 : (8 << IsQuad) - 1; |
| 304 | case NeonTypeFlags::Int16: |
| 305 | case NeonTypeFlags::Poly16: |
| 306 | return shift ? 15 : (4 << IsQuad) - 1; |
| 307 | case NeonTypeFlags::Int32: |
| 308 | return shift ? 31 : (2 << IsQuad) - 1; |
| 309 | case NeonTypeFlags::Int64: |
| 310 | return shift ? 63 : (1 << IsQuad) - 1; |
| 311 | case NeonTypeFlags::Float16: |
| 312 | assert(!shift && "cannot shift float types!"); |
| 313 | return (4 << IsQuad) - 1; |
| 314 | case NeonTypeFlags::Float32: |
| 315 | assert(!shift && "cannot shift float types!"); |
| 316 | return (2 << IsQuad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 317 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 318 | llvm_unreachable("Invalid NeonTypeFlag!"); |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 321 | /// getNeonEltType - Return the QualType corresponding to the elements of |
| 322 | /// the vector type specified by the NeonTypeFlags. This is used to check |
| 323 | /// the pointer arguments for Neon load/store intrinsics. |
| 324 | static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context) { |
| 325 | switch (Flags.getEltType()) { |
| 326 | case NeonTypeFlags::Int8: |
| 327 | return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy; |
| 328 | case NeonTypeFlags::Int16: |
| 329 | return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy; |
| 330 | case NeonTypeFlags::Int32: |
| 331 | return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy; |
| 332 | case NeonTypeFlags::Int64: |
| 333 | return Flags.isUnsigned() ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 334 | case NeonTypeFlags::Poly8: |
| 335 | return Context.SignedCharTy; |
| 336 | case NeonTypeFlags::Poly16: |
| 337 | return Context.ShortTy; |
| 338 | case NeonTypeFlags::Float16: |
| 339 | return Context.UnsignedShortTy; |
| 340 | case NeonTypeFlags::Float32: |
| 341 | return Context.FloatTy; |
| 342 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 343 | llvm_unreachable("Invalid NeonTypeFlag!"); |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 346 | bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 347 | llvm::APSInt Result; |
| 348 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 349 | unsigned mask = 0; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 350 | unsigned TV = 0; |
Bob Wilson | 4648255 | 2011-11-16 21:32:23 +0000 | [diff] [blame] | 351 | int PtrArgNum = -1; |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 352 | bool HasConstPtr = false; |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 353 | switch (BuiltinID) { |
Nate Begeman | a23326b | 2010-06-17 04:17:01 +0000 | [diff] [blame] | 354 | #define GET_NEON_OVERLOAD_CHECK |
| 355 | #include "clang/Basic/arm_neon.inc" |
| 356 | #undef GET_NEON_OVERLOAD_CHECK |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 359 | // For NEON intrinsics which are overloaded on vector element type, validate |
| 360 | // the immediate which specifies which variant to emit. |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 361 | unsigned ImmArg = TheCall->getNumArgs()-1; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 362 | if (mask) { |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 363 | if (SemaBuiltinConstantArg(TheCall, ImmArg, Result)) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 364 | return true; |
| 365 | |
Bob Wilson | da95f73 | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 366 | TV = Result.getLimitedValue(64); |
| 367 | if ((TV > 63) || (mask & (1 << TV)) == 0) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 368 | return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code) |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 369 | << TheCall->getArg(ImmArg)->getSourceRange(); |
| 370 | } |
| 371 | |
Bob Wilson | 4648255 | 2011-11-16 21:32:23 +0000 | [diff] [blame] | 372 | if (PtrArgNum >= 0) { |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 373 | // Check that pointer arguments have the specified type. |
Bob Wilson | 4648255 | 2011-11-16 21:32:23 +0000 | [diff] [blame] | 374 | Expr *Arg = TheCall->getArg(PtrArgNum); |
| 375 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) |
| 376 | Arg = ICE->getSubExpr(); |
| 377 | ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg); |
| 378 | QualType RHSTy = RHS.get()->getType(); |
| 379 | QualType EltTy = getNeonEltType(NeonTypeFlags(TV), Context); |
| 380 | if (HasConstPtr) |
| 381 | EltTy = EltTy.withConst(); |
| 382 | QualType LHSTy = Context.getPointerType(EltTy); |
| 383 | AssignConvertType ConvTy; |
| 384 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
| 385 | if (RHS.isInvalid()) |
| 386 | return true; |
| 387 | if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), LHSTy, RHSTy, |
| 388 | RHS.get(), AA_Assigning)) |
| 389 | return true; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 390 | } |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 391 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 392 | // For NEON intrinsics which take an immediate value as part of the |
| 393 | // instruction, range check them here. |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 394 | unsigned i = 0, l = 0, u = 0; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 395 | switch (BuiltinID) { |
| 396 | default: return false; |
Nate Begeman | bb37f50 | 2010-07-29 22:48:34 +0000 | [diff] [blame] | 397 | case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break; |
| 398 | case ARM::BI__builtin_arm_usat: i = 1; u = 31; break; |
Nate Begeman | 99c40bb | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 399 | case ARM::BI__builtin_arm_vcvtr_f: |
| 400 | case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break; |
Nate Begeman | a23326b | 2010-06-17 04:17:01 +0000 | [diff] [blame] | 401 | #define GET_NEON_IMMEDIATE_CHECK |
| 402 | #include "clang/Basic/arm_neon.inc" |
| 403 | #undef GET_NEON_IMMEDIATE_CHECK |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 404 | }; |
| 405 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 406 | // Check that the immediate argument is actually a constant. |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 407 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 408 | return true; |
| 409 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 410 | // Range check against the upper/lower values for this isntruction. |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 411 | unsigned Val = Result.getZExtValue(); |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 412 | if (Val < l || Val > (u + l)) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 413 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 414 | << l << u+l << TheCall->getArg(i)->getSourceRange(); |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 415 | |
Nate Begeman | 99c40bb | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 416 | // FIXME: VFP Intrinsics should error if VFP not present. |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 417 | return false; |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 418 | } |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 419 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 420 | /// CheckFunctionCall - Check a direct function call for various correctness |
| 421 | /// and safety properties not strictly enforced by the C type system. |
| 422 | bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall) { |
| 423 | // Get the IdentifierInfo* for the called function. |
| 424 | IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
| 425 | |
| 426 | // None of the checks below are needed for functions that don't have |
| 427 | // simple names (e.g., C++ conversion functions). |
| 428 | if (!FnInfo) |
| 429 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 431 | // FIXME: This mechanism should be abstracted to be less fragile and |
| 432 | // more efficient. For example, just map function ids to custom |
| 433 | // handlers. |
| 434 | |
Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 435 | // Printf and scanf checking. |
| 436 | for (specific_attr_iterator<FormatAttr> |
| 437 | i = FDecl->specific_attr_begin<FormatAttr>(), |
| 438 | e = FDecl->specific_attr_end<FormatAttr>(); i != e ; ++i) { |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 439 | CheckFormatArguments(*i, TheCall); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 440 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | |
Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 442 | for (specific_attr_iterator<NonNullAttr> |
| 443 | i = FDecl->specific_attr_begin<NonNullAttr>(), |
| 444 | e = FDecl->specific_attr_end<NonNullAttr>(); i != e; ++i) { |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 445 | CheckNonNullArguments(*i, TheCall->getArgs(), |
| 446 | TheCall->getCallee()->getLocStart()); |
Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 447 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 448 | |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 449 | unsigned CMId = FDecl->getMemoryFunctionKind(); |
| 450 | if (CMId == 0) |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 451 | return false; |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 452 | |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 453 | // Handle memory setting and copying functions. |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 454 | if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat) |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 455 | CheckStrlcpycatArguments(TheCall, FnInfo); |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 456 | else if (CMId == Builtin::BIstrncat) |
| 457 | CheckStrncatArguments(TheCall, FnInfo); |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 458 | else |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 459 | CheckMemaccessArguments(TheCall, CMId, FnInfo); |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 460 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 461 | return false; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 464 | bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac, |
| 465 | Expr **Args, unsigned NumArgs) { |
| 466 | for (specific_attr_iterator<FormatAttr> |
| 467 | i = Method->specific_attr_begin<FormatAttr>(), |
| 468 | e = Method->specific_attr_end<FormatAttr>(); i != e ; ++i) { |
| 469 | |
| 470 | CheckFormatArguments(*i, Args, NumArgs, false, lbrac, |
| 471 | Method->getSourceRange()); |
| 472 | } |
| 473 | |
| 474 | // diagnose nonnull arguments. |
| 475 | for (specific_attr_iterator<NonNullAttr> |
| 476 | i = Method->specific_attr_begin<NonNullAttr>(), |
| 477 | e = Method->specific_attr_end<NonNullAttr>(); i != e; ++i) { |
| 478 | CheckNonNullArguments(*i, Args, lbrac); |
| 479 | } |
| 480 | |
| 481 | return false; |
| 482 | } |
| 483 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 484 | bool Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall) { |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 485 | const VarDecl *V = dyn_cast<VarDecl>(NDecl); |
| 486 | if (!V) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 487 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 489 | QualType Ty = V->getType(); |
| 490 | if (!Ty->isBlockPointerType()) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 491 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | |
Jean-Daniel Dupas | 43d1251 | 2012-01-25 00:55:11 +0000 | [diff] [blame] | 493 | // format string checking. |
| 494 | for (specific_attr_iterator<FormatAttr> |
| 495 | i = NDecl->specific_attr_begin<FormatAttr>(), |
| 496 | e = NDecl->specific_attr_end<FormatAttr>(); i != e ; ++i) { |
| 497 | CheckFormatArguments(*i, TheCall); |
| 498 | } |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 499 | |
| 500 | return false; |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 503 | ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult, |
| 504 | AtomicExpr::AtomicOp Op) { |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 505 | CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); |
| 506 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 507 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 508 | // All these operations take one of the following forms: |
| 509 | enum { |
| 510 | // C __c11_atomic_init(A *, C) |
| 511 | Init, |
| 512 | // C __c11_atomic_load(A *, int) |
| 513 | Load, |
| 514 | // void __atomic_load(A *, CP, int) |
| 515 | Copy, |
| 516 | // C __c11_atomic_add(A *, M, int) |
| 517 | Arithmetic, |
| 518 | // C __atomic_exchange_n(A *, CP, int) |
| 519 | Xchg, |
| 520 | // void __atomic_exchange(A *, C *, CP, int) |
| 521 | GNUXchg, |
| 522 | // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int) |
| 523 | C11CmpXchg, |
| 524 | // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int) |
| 525 | GNUCmpXchg |
| 526 | } Form = Init; |
| 527 | const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 4, 5, 6 }; |
| 528 | const unsigned NumVals[] = { 1, 0, 1, 1, 1, 2, 2, 3 }; |
| 529 | // where: |
| 530 | // C is an appropriate type, |
| 531 | // A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins, |
| 532 | // CP is C for __c11 builtins and GNU _n builtins and is C * otherwise, |
| 533 | // M is C if C is an integer, and ptrdiff_t if C is a pointer, and |
| 534 | // the int parameters are for orderings. |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 535 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 536 | assert(AtomicExpr::AO__c11_atomic_init == 0 && |
| 537 | AtomicExpr::AO__c11_atomic_fetch_xor + 1 == AtomicExpr::AO__atomic_load |
| 538 | && "need to update code for modified C11 atomics"); |
| 539 | bool IsC11 = Op >= AtomicExpr::AO__c11_atomic_init && |
| 540 | Op <= AtomicExpr::AO__c11_atomic_fetch_xor; |
| 541 | bool IsN = Op == AtomicExpr::AO__atomic_load_n || |
| 542 | Op == AtomicExpr::AO__atomic_store_n || |
| 543 | Op == AtomicExpr::AO__atomic_exchange_n || |
| 544 | Op == AtomicExpr::AO__atomic_compare_exchange_n; |
| 545 | bool IsAddSub = false; |
| 546 | |
| 547 | switch (Op) { |
| 548 | case AtomicExpr::AO__c11_atomic_init: |
| 549 | Form = Init; |
| 550 | break; |
| 551 | |
| 552 | case AtomicExpr::AO__c11_atomic_load: |
| 553 | case AtomicExpr::AO__atomic_load_n: |
| 554 | Form = Load; |
| 555 | break; |
| 556 | |
| 557 | case AtomicExpr::AO__c11_atomic_store: |
| 558 | case AtomicExpr::AO__atomic_load: |
| 559 | case AtomicExpr::AO__atomic_store: |
| 560 | case AtomicExpr::AO__atomic_store_n: |
| 561 | Form = Copy; |
| 562 | break; |
| 563 | |
| 564 | case AtomicExpr::AO__c11_atomic_fetch_add: |
| 565 | case AtomicExpr::AO__c11_atomic_fetch_sub: |
| 566 | case AtomicExpr::AO__atomic_fetch_add: |
| 567 | case AtomicExpr::AO__atomic_fetch_sub: |
| 568 | case AtomicExpr::AO__atomic_add_fetch: |
| 569 | case AtomicExpr::AO__atomic_sub_fetch: |
| 570 | IsAddSub = true; |
| 571 | // Fall through. |
| 572 | case AtomicExpr::AO__c11_atomic_fetch_and: |
| 573 | case AtomicExpr::AO__c11_atomic_fetch_or: |
| 574 | case AtomicExpr::AO__c11_atomic_fetch_xor: |
| 575 | case AtomicExpr::AO__atomic_fetch_and: |
| 576 | case AtomicExpr::AO__atomic_fetch_or: |
| 577 | case AtomicExpr::AO__atomic_fetch_xor: |
Richard Smith | 51b9240 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 578 | case AtomicExpr::AO__atomic_fetch_nand: |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 579 | case AtomicExpr::AO__atomic_and_fetch: |
| 580 | case AtomicExpr::AO__atomic_or_fetch: |
| 581 | case AtomicExpr::AO__atomic_xor_fetch: |
Richard Smith | 51b9240 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 582 | case AtomicExpr::AO__atomic_nand_fetch: |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 583 | Form = Arithmetic; |
| 584 | break; |
| 585 | |
| 586 | case AtomicExpr::AO__c11_atomic_exchange: |
| 587 | case AtomicExpr::AO__atomic_exchange_n: |
| 588 | Form = Xchg; |
| 589 | break; |
| 590 | |
| 591 | case AtomicExpr::AO__atomic_exchange: |
| 592 | Form = GNUXchg; |
| 593 | break; |
| 594 | |
| 595 | case AtomicExpr::AO__c11_atomic_compare_exchange_strong: |
| 596 | case AtomicExpr::AO__c11_atomic_compare_exchange_weak: |
| 597 | Form = C11CmpXchg; |
| 598 | break; |
| 599 | |
| 600 | case AtomicExpr::AO__atomic_compare_exchange: |
| 601 | case AtomicExpr::AO__atomic_compare_exchange_n: |
| 602 | Form = GNUCmpXchg; |
| 603 | break; |
| 604 | } |
| 605 | |
| 606 | // Check we have the right number of arguments. |
| 607 | if (TheCall->getNumArgs() < NumArgs[Form]) { |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 608 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 609 | << 0 << NumArgs[Form] << TheCall->getNumArgs() |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 610 | << TheCall->getCallee()->getSourceRange(); |
| 611 | return ExprError(); |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 612 | } else if (TheCall->getNumArgs() > NumArgs[Form]) { |
| 613 | Diag(TheCall->getArg(NumArgs[Form])->getLocStart(), |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 614 | diag::err_typecheck_call_too_many_args) |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 615 | << 0 << NumArgs[Form] << TheCall->getNumArgs() |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 616 | << TheCall->getCallee()->getSourceRange(); |
| 617 | return ExprError(); |
| 618 | } |
| 619 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 620 | // Inspect the first argument of the atomic operation. |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 621 | Expr *Ptr = TheCall->getArg(0); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 622 | Ptr = DefaultFunctionArrayLvalueConversion(Ptr).get(); |
| 623 | const PointerType *pointerType = Ptr->getType()->getAs<PointerType>(); |
| 624 | if (!pointerType) { |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 625 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 626 | << Ptr->getType() << Ptr->getSourceRange(); |
| 627 | return ExprError(); |
| 628 | } |
| 629 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 630 | // For a __c11 builtin, this should be a pointer to an _Atomic type. |
| 631 | QualType AtomTy = pointerType->getPointeeType(); // 'A' |
| 632 | QualType ValType = AtomTy; // 'C' |
| 633 | if (IsC11) { |
| 634 | if (!AtomTy->isAtomicType()) { |
| 635 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic) |
| 636 | << Ptr->getType() << Ptr->getSourceRange(); |
| 637 | return ExprError(); |
| 638 | } |
| 639 | ValType = AtomTy->getAs<AtomicType>()->getValueType(); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 640 | } |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 641 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 642 | // For an arithmetic operation, the implied arithmetic must be well-formed. |
| 643 | if (Form == Arithmetic) { |
| 644 | // gcc does not enforce these rules for GNU atomics, but we do so for sanity. |
| 645 | if (IsAddSub && !ValType->isIntegerType() && !ValType->isPointerType()) { |
| 646 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) |
| 647 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 648 | return ExprError(); |
| 649 | } |
| 650 | if (!IsAddSub && !ValType->isIntegerType()) { |
| 651 | Diag(DRE->getLocStart(), diag::err_atomic_op_bitwise_needs_atomic_int) |
| 652 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 653 | return ExprError(); |
| 654 | } |
| 655 | } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) { |
| 656 | // For __atomic_*_n operations, the value type must be a scalar integral or |
| 657 | // 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] | 658 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 659 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 660 | return ExprError(); |
| 661 | } |
| 662 | |
| 663 | if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context)) { |
| 664 | // For GNU atomics, require a trivially-copyable type. This is not part of |
| 665 | // the GNU atomics specification, but we enforce it for sanity. |
| 666 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_trivial_copy) |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 667 | << Ptr->getType() << Ptr->getSourceRange(); |
| 668 | return ExprError(); |
| 669 | } |
| 670 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 671 | // FIXME: For any builtin other than a load, the ValType must not be |
| 672 | // const-qualified. |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 673 | |
| 674 | switch (ValType.getObjCLifetime()) { |
| 675 | case Qualifiers::OCL_None: |
| 676 | case Qualifiers::OCL_ExplicitNone: |
| 677 | // okay |
| 678 | break; |
| 679 | |
| 680 | case Qualifiers::OCL_Weak: |
| 681 | case Qualifiers::OCL_Strong: |
| 682 | case Qualifiers::OCL_Autoreleasing: |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 683 | // FIXME: Can this happen? By this point, ValType should be known |
| 684 | // to be trivially copyable. |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 685 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
| 686 | << ValType << Ptr->getSourceRange(); |
| 687 | return ExprError(); |
| 688 | } |
| 689 | |
| 690 | QualType ResultType = ValType; |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 691 | if (Form == Copy || Form == GNUXchg || Form == Init) |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 692 | ResultType = Context.VoidTy; |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 693 | else if (Form == C11CmpXchg || Form == GNUCmpXchg) |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 694 | ResultType = Context.BoolTy; |
| 695 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 696 | // The type of a parameter passed 'by value'. In the GNU atomics, such |
| 697 | // arguments are actually passed as pointers. |
| 698 | QualType ByValType = ValType; // 'CP' |
| 699 | if (!IsC11 && !IsN) |
| 700 | ByValType = Ptr->getType(); |
| 701 | |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 702 | // The first argument --- the pointer --- has a fixed type; we |
| 703 | // deduce the types of the rest of the arguments accordingly. Walk |
| 704 | // the remaining arguments, converting them to the deduced value type. |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 705 | for (unsigned i = 1; i != NumArgs[Form]; ++i) { |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 706 | QualType Ty; |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 707 | if (i < NumVals[Form] + 1) { |
| 708 | switch (i) { |
| 709 | case 1: |
| 710 | // The second argument is the non-atomic operand. For arithmetic, this |
| 711 | // is always passed by value, and for a compare_exchange it is always |
| 712 | // passed by address. For the rest, GNU uses by-address and C11 uses |
| 713 | // by-value. |
| 714 | assert(Form != Load); |
| 715 | if (Form == Init || (Form == Arithmetic && ValType->isIntegerType())) |
| 716 | Ty = ValType; |
| 717 | else if (Form == Copy || Form == Xchg) |
| 718 | Ty = ByValType; |
| 719 | else if (Form == Arithmetic) |
| 720 | Ty = Context.getPointerDiffType(); |
| 721 | else |
| 722 | Ty = Context.getPointerType(ValType.getUnqualifiedType()); |
| 723 | break; |
| 724 | case 2: |
| 725 | // The third argument to compare_exchange / GNU exchange is a |
| 726 | // (pointer to a) desired value. |
| 727 | Ty = ByValType; |
| 728 | break; |
| 729 | case 3: |
| 730 | // The fourth argument to GNU compare_exchange is a 'weak' flag. |
| 731 | Ty = Context.BoolTy; |
| 732 | break; |
| 733 | } |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 734 | } else { |
| 735 | // The order(s) are always converted to int. |
| 736 | Ty = Context.IntTy; |
| 737 | } |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 738 | |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 739 | InitializedEntity Entity = |
| 740 | InitializedEntity::InitializeParameter(Context, Ty, false); |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 741 | ExprResult Arg = TheCall->getArg(i); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 742 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 743 | if (Arg.isInvalid()) |
| 744 | return true; |
| 745 | TheCall->setArg(i, Arg.get()); |
| 746 | } |
| 747 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 748 | // Permute the arguments into a 'consistent' order. |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 749 | SmallVector<Expr*, 5> SubExprs; |
| 750 | SubExprs.push_back(Ptr); |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 751 | switch (Form) { |
| 752 | case Init: |
| 753 | // Note, AtomicExpr::getVal1() has a special case for this atomic. |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 754 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 755 | break; |
| 756 | case Load: |
| 757 | SubExprs.push_back(TheCall->getArg(1)); // Order |
| 758 | break; |
| 759 | case Copy: |
| 760 | case Arithmetic: |
| 761 | case Xchg: |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 762 | SubExprs.push_back(TheCall->getArg(2)); // Order |
| 763 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 764 | break; |
| 765 | case GNUXchg: |
| 766 | // Note, AtomicExpr::getVal2() has a special case for this atomic. |
| 767 | SubExprs.push_back(TheCall->getArg(3)); // Order |
| 768 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
| 769 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
| 770 | break; |
| 771 | case C11CmpXchg: |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 772 | SubExprs.push_back(TheCall->getArg(3)); // Order |
| 773 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 774 | SubExprs.push_back(TheCall->getArg(4)); // OrderFail |
David Chisnall | 2ebb98a | 2012-03-29 17:58:59 +0000 | [diff] [blame] | 775 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 776 | break; |
| 777 | case GNUCmpXchg: |
| 778 | SubExprs.push_back(TheCall->getArg(4)); // Order |
| 779 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
| 780 | SubExprs.push_back(TheCall->getArg(5)); // OrderFail |
| 781 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
| 782 | SubExprs.push_back(TheCall->getArg(3)); // Weak |
| 783 | break; |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 784 | } |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 785 | |
| 786 | return Owned(new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(), |
| 787 | SubExprs.data(), SubExprs.size(), |
| 788 | ResultType, Op, |
| 789 | TheCall->getRParenLoc())); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 793 | /// checkBuiltinArgument - Given a call to a builtin function, perform |
| 794 | /// normal type-checking on the given argument, updating the call in |
| 795 | /// place. This is useful when a builtin function requires custom |
| 796 | /// type-checking for some of its arguments but not necessarily all of |
| 797 | /// them. |
| 798 | /// |
| 799 | /// Returns true on error. |
| 800 | static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) { |
| 801 | FunctionDecl *Fn = E->getDirectCallee(); |
| 802 | assert(Fn && "builtin call without direct callee!"); |
| 803 | |
| 804 | ParmVarDecl *Param = Fn->getParamDecl(ArgIndex); |
| 805 | InitializedEntity Entity = |
| 806 | InitializedEntity::InitializeParameter(S.Context, Param); |
| 807 | |
| 808 | ExprResult Arg = E->getArg(0); |
| 809 | Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 810 | if (Arg.isInvalid()) |
| 811 | return true; |
| 812 | |
| 813 | E->setArg(ArgIndex, Arg.take()); |
| 814 | return false; |
| 815 | } |
| 816 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 817 | /// SemaBuiltinAtomicOverloaded - We have a call to a function like |
| 818 | /// __sync_fetch_and_add, which is an overloaded function based on the pointer |
| 819 | /// type of its first argument. The main ActOnCallExpr routines have already |
| 820 | /// promoted the types of arguments because all of these calls are prototyped as |
| 821 | /// void(...). |
| 822 | /// |
| 823 | /// This function goes through and does final semantic checking for these |
| 824 | /// builtins, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 825 | ExprResult |
| 826 | Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) { |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 827 | CallExpr *TheCall = (CallExpr *)TheCallResult.get(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 828 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 829 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 830 | |
| 831 | // 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] | 832 | if (TheCall->getNumArgs() < 1) { |
| 833 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 834 | << 0 << 1 << TheCall->getNumArgs() |
| 835 | << TheCall->getCallee()->getSourceRange(); |
| 836 | return ExprError(); |
| 837 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 838 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 839 | // Inspect the first argument of the atomic builtin. This should always be |
| 840 | // a pointer type, whose element is an integral scalar or pointer type. |
| 841 | // Because it is a pointer type, we don't have to worry about any implicit |
| 842 | // casts here. |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 843 | // FIXME: We don't allow floating point scalars as input. |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 844 | Expr *FirstArg = TheCall->getArg(0); |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 845 | ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg); |
| 846 | if (FirstArgResult.isInvalid()) |
| 847 | return ExprError(); |
| 848 | FirstArg = FirstArgResult.take(); |
| 849 | TheCall->setArg(0, FirstArg); |
| 850 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 851 | const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>(); |
| 852 | if (!pointerType) { |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 853 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
| 854 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 855 | return ExprError(); |
| 856 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 858 | QualType ValType = pointerType->getPointeeType(); |
Chris Lattner | dd5fa7a | 2010-09-17 21:12:38 +0000 | [diff] [blame] | 859 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 860 | !ValType->isBlockPointerType()) { |
| 861 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr) |
| 862 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 863 | return ExprError(); |
| 864 | } |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 865 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 866 | switch (ValType.getObjCLifetime()) { |
| 867 | case Qualifiers::OCL_None: |
| 868 | case Qualifiers::OCL_ExplicitNone: |
| 869 | // okay |
| 870 | break; |
| 871 | |
| 872 | case Qualifiers::OCL_Weak: |
| 873 | case Qualifiers::OCL_Strong: |
| 874 | case Qualifiers::OCL_Autoreleasing: |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 875 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 876 | << ValType << FirstArg->getSourceRange(); |
| 877 | return ExprError(); |
| 878 | } |
| 879 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 880 | // Strip any qualifiers off ValType. |
| 881 | ValType = ValType.getUnqualifiedType(); |
| 882 | |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 883 | // The majority of builtins return a value, but a few have special return |
| 884 | // types, so allow them to override appropriately below. |
| 885 | QualType ResultType = ValType; |
| 886 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 887 | // We need to figure out which concrete builtin this maps onto. For example, |
| 888 | // __sync_fetch_and_add with a 2 byte object turns into |
| 889 | // __sync_fetch_and_add_2. |
| 890 | #define BUILTIN_ROW(x) \ |
| 891 | { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ |
| 892 | Builtin::BI##x##_8, Builtin::BI##x##_16 } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 893 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 894 | static const unsigned BuiltinIndices[][5] = { |
| 895 | BUILTIN_ROW(__sync_fetch_and_add), |
| 896 | BUILTIN_ROW(__sync_fetch_and_sub), |
| 897 | BUILTIN_ROW(__sync_fetch_and_or), |
| 898 | BUILTIN_ROW(__sync_fetch_and_and), |
| 899 | BUILTIN_ROW(__sync_fetch_and_xor), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 900 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 901 | BUILTIN_ROW(__sync_add_and_fetch), |
| 902 | BUILTIN_ROW(__sync_sub_and_fetch), |
| 903 | BUILTIN_ROW(__sync_and_and_fetch), |
| 904 | BUILTIN_ROW(__sync_or_and_fetch), |
| 905 | BUILTIN_ROW(__sync_xor_and_fetch), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 906 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 907 | BUILTIN_ROW(__sync_val_compare_and_swap), |
| 908 | BUILTIN_ROW(__sync_bool_compare_and_swap), |
| 909 | BUILTIN_ROW(__sync_lock_test_and_set), |
Chris Lattner | 23aa9c8 | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 910 | BUILTIN_ROW(__sync_lock_release), |
| 911 | BUILTIN_ROW(__sync_swap) |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 912 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 | #undef BUILTIN_ROW |
| 914 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 915 | // Determine the index of the size. |
| 916 | unsigned SizeIndex; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 917 | switch (Context.getTypeSizeInChars(ValType).getQuantity()) { |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 918 | case 1: SizeIndex = 0; break; |
| 919 | case 2: SizeIndex = 1; break; |
| 920 | case 4: SizeIndex = 2; break; |
| 921 | case 8: SizeIndex = 3; break; |
| 922 | case 16: SizeIndex = 4; break; |
| 923 | default: |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 924 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size) |
| 925 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 926 | return ExprError(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 927 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 928 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 929 | // Each of these builtins has one pointer argument, followed by some number of |
| 930 | // values (0, 1 or 2) followed by a potentially empty varags list of stuff |
| 931 | // that we ignore. Find out which row of BuiltinIndices to read from as well |
| 932 | // as the number of fixed args. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 933 | unsigned BuiltinID = FDecl->getBuiltinID(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 934 | unsigned BuiltinIndex, NumFixed = 1; |
| 935 | switch (BuiltinID) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 936 | default: llvm_unreachable("Unknown overloaded atomic builtin!"); |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 937 | case Builtin::BI__sync_fetch_and_add: |
| 938 | case Builtin::BI__sync_fetch_and_add_1: |
| 939 | case Builtin::BI__sync_fetch_and_add_2: |
| 940 | case Builtin::BI__sync_fetch_and_add_4: |
| 941 | case Builtin::BI__sync_fetch_and_add_8: |
| 942 | case Builtin::BI__sync_fetch_and_add_16: |
| 943 | BuiltinIndex = 0; |
| 944 | break; |
| 945 | |
| 946 | case Builtin::BI__sync_fetch_and_sub: |
| 947 | case Builtin::BI__sync_fetch_and_sub_1: |
| 948 | case Builtin::BI__sync_fetch_and_sub_2: |
| 949 | case Builtin::BI__sync_fetch_and_sub_4: |
| 950 | case Builtin::BI__sync_fetch_and_sub_8: |
| 951 | case Builtin::BI__sync_fetch_and_sub_16: |
| 952 | BuiltinIndex = 1; |
| 953 | break; |
| 954 | |
| 955 | case Builtin::BI__sync_fetch_and_or: |
| 956 | case Builtin::BI__sync_fetch_and_or_1: |
| 957 | case Builtin::BI__sync_fetch_and_or_2: |
| 958 | case Builtin::BI__sync_fetch_and_or_4: |
| 959 | case Builtin::BI__sync_fetch_and_or_8: |
| 960 | case Builtin::BI__sync_fetch_and_or_16: |
| 961 | BuiltinIndex = 2; |
| 962 | break; |
| 963 | |
| 964 | case Builtin::BI__sync_fetch_and_and: |
| 965 | case Builtin::BI__sync_fetch_and_and_1: |
| 966 | case Builtin::BI__sync_fetch_and_and_2: |
| 967 | case Builtin::BI__sync_fetch_and_and_4: |
| 968 | case Builtin::BI__sync_fetch_and_and_8: |
| 969 | case Builtin::BI__sync_fetch_and_and_16: |
| 970 | BuiltinIndex = 3; |
| 971 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 973 | case Builtin::BI__sync_fetch_and_xor: |
| 974 | case Builtin::BI__sync_fetch_and_xor_1: |
| 975 | case Builtin::BI__sync_fetch_and_xor_2: |
| 976 | case Builtin::BI__sync_fetch_and_xor_4: |
| 977 | case Builtin::BI__sync_fetch_and_xor_8: |
| 978 | case Builtin::BI__sync_fetch_and_xor_16: |
| 979 | BuiltinIndex = 4; |
| 980 | break; |
| 981 | |
| 982 | case Builtin::BI__sync_add_and_fetch: |
| 983 | case Builtin::BI__sync_add_and_fetch_1: |
| 984 | case Builtin::BI__sync_add_and_fetch_2: |
| 985 | case Builtin::BI__sync_add_and_fetch_4: |
| 986 | case Builtin::BI__sync_add_and_fetch_8: |
| 987 | case Builtin::BI__sync_add_and_fetch_16: |
| 988 | BuiltinIndex = 5; |
| 989 | break; |
| 990 | |
| 991 | case Builtin::BI__sync_sub_and_fetch: |
| 992 | case Builtin::BI__sync_sub_and_fetch_1: |
| 993 | case Builtin::BI__sync_sub_and_fetch_2: |
| 994 | case Builtin::BI__sync_sub_and_fetch_4: |
| 995 | case Builtin::BI__sync_sub_and_fetch_8: |
| 996 | case Builtin::BI__sync_sub_and_fetch_16: |
| 997 | BuiltinIndex = 6; |
| 998 | break; |
| 999 | |
| 1000 | case Builtin::BI__sync_and_and_fetch: |
| 1001 | case Builtin::BI__sync_and_and_fetch_1: |
| 1002 | case Builtin::BI__sync_and_and_fetch_2: |
| 1003 | case Builtin::BI__sync_and_and_fetch_4: |
| 1004 | case Builtin::BI__sync_and_and_fetch_8: |
| 1005 | case Builtin::BI__sync_and_and_fetch_16: |
| 1006 | BuiltinIndex = 7; |
| 1007 | break; |
| 1008 | |
| 1009 | case Builtin::BI__sync_or_and_fetch: |
| 1010 | case Builtin::BI__sync_or_and_fetch_1: |
| 1011 | case Builtin::BI__sync_or_and_fetch_2: |
| 1012 | case Builtin::BI__sync_or_and_fetch_4: |
| 1013 | case Builtin::BI__sync_or_and_fetch_8: |
| 1014 | case Builtin::BI__sync_or_and_fetch_16: |
| 1015 | BuiltinIndex = 8; |
| 1016 | break; |
| 1017 | |
| 1018 | case Builtin::BI__sync_xor_and_fetch: |
| 1019 | case Builtin::BI__sync_xor_and_fetch_1: |
| 1020 | case Builtin::BI__sync_xor_and_fetch_2: |
| 1021 | case Builtin::BI__sync_xor_and_fetch_4: |
| 1022 | case Builtin::BI__sync_xor_and_fetch_8: |
| 1023 | case Builtin::BI__sync_xor_and_fetch_16: |
| 1024 | BuiltinIndex = 9; |
| 1025 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1026 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1027 | case Builtin::BI__sync_val_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1028 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 1029 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 1030 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 1031 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 1032 | case Builtin::BI__sync_val_compare_and_swap_16: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 1033 | BuiltinIndex = 10; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1034 | NumFixed = 2; |
| 1035 | break; |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1036 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1037 | case Builtin::BI__sync_bool_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1038 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 1039 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 1040 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 1041 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 1042 | case Builtin::BI__sync_bool_compare_and_swap_16: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 1043 | BuiltinIndex = 11; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1044 | NumFixed = 2; |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1045 | ResultType = Context.BoolTy; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1046 | break; |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1047 | |
| 1048 | case Builtin::BI__sync_lock_test_and_set: |
| 1049 | case Builtin::BI__sync_lock_test_and_set_1: |
| 1050 | case Builtin::BI__sync_lock_test_and_set_2: |
| 1051 | case Builtin::BI__sync_lock_test_and_set_4: |
| 1052 | case Builtin::BI__sync_lock_test_and_set_8: |
| 1053 | case Builtin::BI__sync_lock_test_and_set_16: |
| 1054 | BuiltinIndex = 12; |
| 1055 | break; |
| 1056 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1057 | case Builtin::BI__sync_lock_release: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1058 | case Builtin::BI__sync_lock_release_1: |
| 1059 | case Builtin::BI__sync_lock_release_2: |
| 1060 | case Builtin::BI__sync_lock_release_4: |
| 1061 | case Builtin::BI__sync_lock_release_8: |
| 1062 | case Builtin::BI__sync_lock_release_16: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 1063 | BuiltinIndex = 13; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1064 | NumFixed = 0; |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1065 | ResultType = Context.VoidTy; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1066 | break; |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1067 | |
| 1068 | case Builtin::BI__sync_swap: |
| 1069 | case Builtin::BI__sync_swap_1: |
| 1070 | case Builtin::BI__sync_swap_2: |
| 1071 | case Builtin::BI__sync_swap_4: |
| 1072 | case Builtin::BI__sync_swap_8: |
| 1073 | case Builtin::BI__sync_swap_16: |
| 1074 | BuiltinIndex = 14; |
| 1075 | break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1076 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1078 | // Now that we know how many fixed arguments we expect, first check that we |
| 1079 | // have at least that many. |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1080 | if (TheCall->getNumArgs() < 1+NumFixed) { |
| 1081 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 1082 | << 0 << 1+NumFixed << TheCall->getNumArgs() |
| 1083 | << TheCall->getCallee()->getSourceRange(); |
| 1084 | return ExprError(); |
| 1085 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | |
Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 1087 | // Get the decl for the concrete builtin from this, we can tell what the |
| 1088 | // concrete integer type we should convert to is. |
| 1089 | unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; |
| 1090 | const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID); |
| 1091 | IdentifierInfo *NewBuiltinII = PP.getIdentifierInfo(NewBuiltinName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | FunctionDecl *NewBuiltinDecl = |
Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 1093 | cast<FunctionDecl>(LazilyCreateBuiltin(NewBuiltinII, NewBuiltinID, |
| 1094 | TUScope, false, DRE->getLocStart())); |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1095 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1096 | // The first argument --- the pointer --- has a fixed type; we |
| 1097 | // deduce the types of the rest of the arguments accordingly. Walk |
| 1098 | // the remaining arguments, converting them to the deduced value type. |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1099 | for (unsigned i = 0; i != NumFixed; ++i) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1100 | ExprResult Arg = TheCall->getArg(i+1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1102 | // GCC does an implicit conversion to the pointer or integer ValType. This |
| 1103 | // 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] | 1104 | // Initialize the argument. |
| 1105 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 1106 | ValType, /*consume*/ false); |
| 1107 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1108 | if (Arg.isInvalid()) |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1109 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1110 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1111 | // Okay, we have something that *can* be converted to the right type. Check |
| 1112 | // to see if there is a potentially weird extension going on here. This can |
| 1113 | // happen when you do an atomic operation on something like an char* and |
| 1114 | // pass in 42. The 42 gets converted to char. This is even more strange |
| 1115 | // for things like 45.123 -> char, etc. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1116 | // FIXME: Do this check. |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1117 | TheCall->setArg(i+1, Arg.take()); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1119 | |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1120 | ASTContext& Context = this->getASTContext(); |
| 1121 | |
| 1122 | // Create a new DeclRefExpr to refer to the new decl. |
| 1123 | DeclRefExpr* NewDRE = DeclRefExpr::Create( |
| 1124 | Context, |
| 1125 | DRE->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1126 | SourceLocation(), |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1127 | NewBuiltinDecl, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1128 | /*enclosing*/ false, |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1129 | DRE->getLocation(), |
| 1130 | NewBuiltinDecl->getType(), |
| 1131 | DRE->getValueKind()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1132 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1133 | // Set the callee in the CallExpr. |
| 1134 | // FIXME: This leaks the original parens and implicit casts. |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1135 | ExprResult PromotedCall = UsualUnaryConversions(NewDRE); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1136 | if (PromotedCall.isInvalid()) |
| 1137 | return ExprError(); |
| 1138 | TheCall->setCallee(PromotedCall.take()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1139 | |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 1140 | // Change the result type of the call to match the original value type. This |
| 1141 | // is arbitrary, but the codegen for these builtins ins design to handle it |
| 1142 | // gracefully. |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1143 | TheCall->setType(ResultType); |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1144 | |
| 1145 | return move(TheCallResult); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 1148 | /// CheckObjCString - Checks that the argument to the builtin |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1149 | /// CFString constructor is correct |
Steve Naroff | fd94262 | 2009-04-13 20:26:29 +0000 | [diff] [blame] | 1150 | /// Note: It might also make sense to do the UTF-16 conversion here (would |
| 1151 | /// simplify the backend). |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 1152 | bool Sema::CheckObjCString(Expr *Arg) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1153 | Arg = Arg->IgnoreParenCasts(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1154 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); |
| 1155 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1156 | if (!Literal || !Literal->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1157 | Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 1158 | << Arg->getSourceRange(); |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 1159 | return true; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1160 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1161 | |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1162 | if (Literal->containsNonAsciiOrNull()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1163 | StringRef String = Literal->getString(); |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1164 | unsigned NumBytes = String.size(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1165 | SmallVector<UTF16, 128> ToBuf(NumBytes); |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1166 | const UTF8 *FromPtr = (UTF8 *)String.data(); |
| 1167 | UTF16 *ToPtr = &ToBuf[0]; |
| 1168 | |
| 1169 | ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, |
| 1170 | &ToPtr, ToPtr + NumBytes, |
| 1171 | strictConversion); |
| 1172 | // Check for conversion failure. |
| 1173 | if (Result != conversionOK) |
| 1174 | Diag(Arg->getLocStart(), |
| 1175 | diag::warn_cfstring_truncated) << Arg->getSourceRange(); |
| 1176 | } |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 1177 | return false; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1180 | /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity. |
| 1181 | /// Emit an error and return true on failure, return false on success. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1182 | bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) { |
| 1183 | Expr *Fn = TheCall->getCallee(); |
| 1184 | if (TheCall->getNumArgs() > 2) { |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 1185 | Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1186 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1187 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
| 1188 | << Fn->getSourceRange() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1189 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1190 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1191 | return true; |
| 1192 | } |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 1193 | |
| 1194 | if (TheCall->getNumArgs() < 2) { |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1195 | return Diag(TheCall->getLocEnd(), |
| 1196 | diag::err_typecheck_call_too_few_args_at_least) |
| 1197 | << 0 /*function call*/ << 2 << TheCall->getNumArgs(); |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 1200 | // Type-check the first argument normally. |
| 1201 | if (checkBuiltinArgument(*this, TheCall, 0)) |
| 1202 | return true; |
| 1203 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1204 | // Determine whether the current function is variadic or not. |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1205 | BlockScopeInfo *CurBlock = getCurBlock(); |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1206 | bool isVariadic; |
Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 1207 | if (CurBlock) |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 1208 | isVariadic = CurBlock->TheDecl->isVariadic(); |
Ted Kremenek | 9498d38 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 1209 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
| 1210 | isVariadic = FD->isVariadic(); |
| 1211 | else |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1212 | isVariadic = getCurMethodDecl()->isVariadic(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1214 | if (!isVariadic) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1215 | Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function); |
| 1216 | return true; |
| 1217 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1218 | |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1219 | // Verify that the second argument to the builtin is the last argument of the |
| 1220 | // current function or method. |
| 1221 | bool SecondArgIsLastNamedArgument = false; |
Anders Carlsson | e2c1410 | 2008-02-13 01:22:59 +0000 | [diff] [blame] | 1222 | const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1223 | |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 1224 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { |
| 1225 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1226 | // FIXME: This isn't correct for methods (results in bogus warning). |
| 1227 | // Get the last formal in the current function. |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 1228 | const ParmVarDecl *LastArg; |
Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 1229 | if (CurBlock) |
| 1230 | LastArg = *(CurBlock->TheDecl->param_end()-1); |
| 1231 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1232 | LastArg = *(FD->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1233 | else |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1234 | LastArg = *(getCurMethodDecl()->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1235 | SecondArgIsLastNamedArgument = PV == LastArg; |
| 1236 | } |
| 1237 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1238 | |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1239 | if (!SecondArgIsLastNamedArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1240 | Diag(TheCall->getArg(1)->getLocStart(), |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1241 | diag::warn_second_parameter_of_va_start_not_last_named_argument); |
| 1242 | return false; |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 1243 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1244 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1245 | /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and |
| 1246 | /// friends. This is declared to take (...), so we have to check everything. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1247 | bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { |
| 1248 | if (TheCall->getNumArgs() < 2) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 1249 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1250 | << 0 << 2 << TheCall->getNumArgs()/*function call*/; |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1251 | if (TheCall->getNumArgs() > 2) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1252 | return Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1253 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1254 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1255 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
| 1256 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1257 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1258 | ExprResult OrigArg0 = TheCall->getArg(0); |
| 1259 | ExprResult OrigArg1 = TheCall->getArg(1); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1260 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1261 | // Do standard promotions between the two arguments, returning their common |
| 1262 | // type. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1263 | QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1264 | if (OrigArg0.isInvalid() || OrigArg1.isInvalid()) |
| 1265 | return true; |
Daniel Dunbar | 403bc2b | 2009-02-19 19:28:43 +0000 | [diff] [blame] | 1266 | |
| 1267 | // Make sure any conversions are pushed back into the call; this is |
| 1268 | // type safe since unordered compare builtins are declared as "_Bool |
| 1269 | // foo(...)". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1270 | TheCall->setArg(0, OrigArg0.get()); |
| 1271 | TheCall->setArg(1, OrigArg1.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1272 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1273 | if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent()) |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1274 | return false; |
| 1275 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1276 | // If the common type isn't a real floating type, then the arguments were |
| 1277 | // invalid for this operation. |
| 1278 | if (!Res->isRealFloatingType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1279 | return Diag(OrigArg0.get()->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1280 | diag::err_typecheck_call_invalid_ordered_compare) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1281 | << OrigArg0.get()->getType() << OrigArg1.get()->getType() |
| 1282 | << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1283 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1284 | return false; |
| 1285 | } |
| 1286 | |
Benjamin Kramer | e771a7a | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 1287 | /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like |
| 1288 | /// __builtin_isnan and friends. This is declared to take (...), so we have |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1289 | /// to check everything. We expect the last argument to be a floating point |
| 1290 | /// value. |
| 1291 | bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) { |
| 1292 | if (TheCall->getNumArgs() < NumArgs) |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1293 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1294 | << 0 << NumArgs << TheCall->getNumArgs()/*function call*/; |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1295 | if (TheCall->getNumArgs() > NumArgs) |
| 1296 | return Diag(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1297 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1298 | << 0 /*function call*/ << NumArgs << TheCall->getNumArgs() |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1299 | << SourceRange(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1300 | (*(TheCall->arg_end()-1))->getLocEnd()); |
| 1301 | |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1302 | Expr *OrigArg = TheCall->getArg(NumArgs-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1303 | |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1304 | if (OrigArg->isTypeDependent()) |
| 1305 | return false; |
| 1306 | |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1307 | // This operation requires a non-_Complex floating-point number. |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1308 | if (!OrigArg->getType()->isRealFloatingType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1309 | return Diag(OrigArg->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1310 | diag::err_typecheck_call_invalid_unary_fp) |
| 1311 | << OrigArg->getType() << OrigArg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1313 | // If this is an implicit conversion from float -> double, remove it. |
| 1314 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) { |
| 1315 | Expr *CastArg = Cast->getSubExpr(); |
| 1316 | if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) { |
| 1317 | assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) && |
| 1318 | "promotion from float to double is the only expected cast here"); |
| 1319 | Cast->setSubExpr(0); |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1320 | TheCall->setArg(NumArgs-1, CastArg); |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1321 | } |
| 1322 | } |
| 1323 | |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1324 | return false; |
| 1325 | } |
| 1326 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1327 | /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. |
| 1328 | // This is declared to take (...), so we have to check everything. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1329 | ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1330 | if (TheCall->getNumArgs() < 2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1331 | return ExprError(Diag(TheCall->getLocEnd(), |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1332 | diag::err_typecheck_call_too_few_args_at_least) |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1333 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1334 | << TheCall->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1335 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1336 | // Determine which of the following types of shufflevector we're checking: |
| 1337 | // 1) unary, vector mask: (lhs, mask) |
| 1338 | // 2) binary, vector mask: (lhs, rhs, mask) |
| 1339 | // 3) binary, scalar mask: (lhs, rhs, index, ..., index) |
| 1340 | QualType resType = TheCall->getArg(0)->getType(); |
| 1341 | unsigned numElements = 0; |
| 1342 | |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1343 | if (!TheCall->getArg(0)->isTypeDependent() && |
| 1344 | !TheCall->getArg(1)->isTypeDependent()) { |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1345 | QualType LHSType = TheCall->getArg(0)->getType(); |
| 1346 | QualType RHSType = TheCall->getArg(1)->getType(); |
| 1347 | |
| 1348 | if (!LHSType->isVectorType() || !RHSType->isVectorType()) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1349 | Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1351 | TheCall->getArg(1)->getLocEnd()); |
| 1352 | return ExprError(); |
| 1353 | } |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1354 | |
| 1355 | numElements = LHSType->getAs<VectorType>()->getNumElements(); |
| 1356 | unsigned numResElements = TheCall->getNumArgs() - 2; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1357 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1358 | // Check to see if we have a call with 2 vector arguments, the unary shuffle |
| 1359 | // with mask. If so, verify that RHS is an integer vector type with the |
| 1360 | // same number of elts as lhs. |
| 1361 | if (TheCall->getNumArgs() == 2) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 1362 | if (!RHSType->hasIntegerRepresentation() || |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1363 | RHSType->getAs<VectorType>()->getNumElements() != numElements) |
| 1364 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
| 1365 | << SourceRange(TheCall->getArg(1)->getLocStart(), |
| 1366 | TheCall->getArg(1)->getLocEnd()); |
| 1367 | numResElements = numElements; |
| 1368 | } |
| 1369 | else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1370 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1372 | TheCall->getArg(1)->getLocEnd()); |
| 1373 | return ExprError(); |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1374 | } else if (numElements != numResElements) { |
| 1375 | QualType eltType = LHSType->getAs<VectorType>()->getElementType(); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1376 | resType = Context.getVectorType(eltType, numResElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1377 | VectorType::GenericVector); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1378 | } |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1382 | if (TheCall->getArg(i)->isTypeDependent() || |
| 1383 | TheCall->getArg(i)->isValueDependent()) |
| 1384 | continue; |
| 1385 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1386 | llvm::APSInt Result(32); |
| 1387 | if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) |
| 1388 | return ExprError(Diag(TheCall->getLocStart(), |
| 1389 | diag::err_shufflevector_nonconstant_argument) |
| 1390 | << TheCall->getArg(i)->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1391 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 1392 | if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1393 | return ExprError(Diag(TheCall->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1394 | diag::err_shufflevector_argument_too_large) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1395 | << TheCall->getArg(i)->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1398 | SmallVector<Expr*, 32> exprs; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1399 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 1400 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1401 | exprs.push_back(TheCall->getArg(i)); |
| 1402 | TheCall->setArg(i, 0); |
| 1403 | } |
| 1404 | |
Nate Begeman | a88dc30 | 2009-08-12 02:10:25 +0000 | [diff] [blame] | 1405 | return Owned(new (Context) ShuffleVectorExpr(Context, exprs.begin(), |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1406 | exprs.size(), resType, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1407 | TheCall->getCallee()->getLocStart(), |
| 1408 | TheCall->getRParenLoc())); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1409 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1410 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1411 | /// SemaBuiltinPrefetch - Handle __builtin_prefetch. |
| 1412 | // This is declared to take (const void*, ...) and can take two |
| 1413 | // optional constant int args. |
| 1414 | bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1415 | unsigned NumArgs = TheCall->getNumArgs(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1416 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1417 | if (NumArgs > 3) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1418 | return Diag(TheCall->getLocEnd(), |
| 1419 | diag::err_typecheck_call_too_many_args_at_most) |
| 1420 | << 0 /*function call*/ << 3 << NumArgs |
| 1421 | << TheCall->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1422 | |
| 1423 | // Argument 0 is checked for us and the remaining arguments must be |
| 1424 | // constant integers. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1425 | for (unsigned i = 1; i != NumArgs; ++i) { |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1426 | Expr *Arg = TheCall->getArg(i); |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1427 | |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1428 | llvm::APSInt Result; |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1429 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 1430 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1431 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1432 | // FIXME: gcc issues a warning and rewrites these to 0. These |
| 1433 | // seems especially odd for the third argument since the default |
| 1434 | // is 3. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1435 | if (i == 1) { |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1436 | if (Result.getLimitedValue() > 1) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1437 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1438 | << "0" << "1" << Arg->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1439 | } else { |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1440 | if (Result.getLimitedValue() > 3) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1441 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1442 | << "0" << "3" << Arg->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1443 | } |
| 1444 | } |
| 1445 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1446 | return false; |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1449 | /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr |
| 1450 | /// TheCall is a constant expression. |
| 1451 | bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum, |
| 1452 | llvm::APSInt &Result) { |
| 1453 | Expr *Arg = TheCall->getArg(ArgNum); |
| 1454 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 1455 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 1456 | |
| 1457 | if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; |
| 1458 | |
| 1459 | if (!Arg->isIntegerConstantExpr(Result, Context)) |
| 1460 | return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type) |
Eric Christopher | 5e89655 | 2010-04-19 18:23:02 +0000 | [diff] [blame] | 1461 | << FDecl->getDeclName() << Arg->getSourceRange(); |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1462 | |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1463 | return false; |
| 1464 | } |
| 1465 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1466 | /// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr, |
| 1467 | /// int type). This simply type checks that type is one of the defined |
| 1468 | /// constants (0-3). |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1469 | // For compatibility check 0-3, llvm only handles 0 and 2. |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1470 | bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) { |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1471 | llvm::APSInt Result; |
| 1472 | |
| 1473 | // Check constant-ness first. |
| 1474 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 1475 | return true; |
| 1476 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1477 | Expr *Arg = TheCall->getArg(1); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1478 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1479 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 1480 | << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | return false; |
| 1484 | } |
| 1485 | |
Eli Friedman | 586d6a8 | 2009-05-03 06:04:26 +0000 | [diff] [blame] | 1486 | /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 1487 | /// This checks that val is a constant 1. |
| 1488 | bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { |
| 1489 | Expr *Arg = TheCall->getArg(1); |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1490 | llvm::APSInt Result; |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1491 | |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1492 | // TODO: This is less than ideal. Overload this to take a value. |
| 1493 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 1494 | return true; |
| 1495 | |
| 1496 | if (Result != 1) |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 1497 | return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val) |
| 1498 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
| 1499 | |
| 1500 | return false; |
| 1501 | } |
| 1502 | |
Ted Kremenek | b43e8ad | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 1503 | // Handle i > 1 ? "x" : "y", recursively. |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1504 | bool Sema::SemaCheckStringLiteral(const Expr *E, Expr **Args, |
| 1505 | unsigned NumArgs, bool HasVAListArg, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1506 | unsigned format_idx, unsigned firstDataArg, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1507 | FormatStringType Type, bool inFunctionCall) { |
Ted Kremenek | 4fe6441 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 1508 | tryAgain: |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1509 | if (E->isTypeDependent() || E->isValueDependent()) |
| 1510 | return false; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1511 | |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1512 | E = E->IgnoreParenCasts(); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1513 | |
David Blaikie | a73cdcb | 2012-02-10 21:07:25 +0000 | [diff] [blame] | 1514 | if (E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 1515 | // Technically -Wformat-nonliteral does not warn about this case. |
| 1516 | // The behavior of printf and friends in this case is implementation |
| 1517 | // dependent. Ideally if the format string cannot be null then |
| 1518 | // it should have a 'nonnull' attribute in the function prototype. |
| 1519 | return true; |
| 1520 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1521 | switch (E->getStmtClass()) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1522 | case Stmt::BinaryConditionalOperatorClass: |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1523 | case Stmt::ConditionalOperatorClass: { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1524 | const AbstractConditionalOperator *C = cast<AbstractConditionalOperator>(E); |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1525 | return SemaCheckStringLiteral(C->getTrueExpr(), Args, NumArgs, HasVAListArg, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1526 | format_idx, firstDataArg, Type, |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1527 | inFunctionCall) |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1528 | && SemaCheckStringLiteral(C->getFalseExpr(), Args, NumArgs, HasVAListArg, |
| 1529 | format_idx, firstDataArg, Type, |
| 1530 | inFunctionCall); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | case Stmt::ImplicitCastExprClass: { |
Ted Kremenek | 4fe6441 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 1534 | E = cast<ImplicitCastExpr>(E)->getSubExpr(); |
| 1535 | goto tryAgain; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1538 | case Stmt::OpaqueValueExprClass: |
| 1539 | if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) { |
| 1540 | E = src; |
| 1541 | goto tryAgain; |
| 1542 | } |
| 1543 | return false; |
| 1544 | |
Ted Kremenek | b43e8ad | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 1545 | case Stmt::PredefinedExprClass: |
| 1546 | // While __func__, etc., are technically not string literals, they |
| 1547 | // cannot contain format specifiers and thus are not a security |
| 1548 | // liability. |
| 1549 | return true; |
| 1550 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1551 | case Stmt::DeclRefExprClass: { |
| 1552 | const DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1553 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1554 | // As an exception, do not flag errors for variables binding to |
| 1555 | // const string literals. |
| 1556 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 1557 | bool isConstant = false; |
| 1558 | QualType T = DR->getType(); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1559 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1560 | if (const ArrayType *AT = Context.getAsArrayType(T)) { |
| 1561 | isConstant = AT->getElementType().isConstant(Context); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1562 | } else if (const PointerType *PT = T->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1563 | isConstant = T.isConstant(Context) && |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1564 | PT->getPointeeType().isConstant(Context); |
Jean-Daniel Dupas | e98e5b5 | 2012-01-25 10:35:33 +0000 | [diff] [blame] | 1565 | } else if (T->isObjCObjectPointerType()) { |
| 1566 | // In ObjC, there is usually no "const ObjectPointer" type, |
| 1567 | // so don't check if the pointee type is constant. |
| 1568 | isConstant = T.isConstant(Context); |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1569 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1571 | if (isConstant) { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1572 | if (const Expr *Init = VD->getAnyInitializer()) |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1573 | return SemaCheckStringLiteral(Init, Args, NumArgs, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1574 | HasVAListArg, format_idx, firstDataArg, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1575 | Type, /*inFunctionCall*/false); |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1576 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1577 | |
Anders Carlsson | d966a55 | 2009-06-28 19:55:58 +0000 | [diff] [blame] | 1578 | // For vprintf* functions (i.e., HasVAListArg==true), we add a |
| 1579 | // special check to see if the format string is a function parameter |
| 1580 | // of the function calling the printf function. If the function |
| 1581 | // has an attribute indicating it is a printf-like function, then we |
| 1582 | // should suppress warnings concerning non-literals being used in a call |
| 1583 | // to a vprintf function. For example: |
| 1584 | // |
| 1585 | // void |
| 1586 | // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){ |
| 1587 | // va_list ap; |
| 1588 | // va_start(ap, fmt); |
| 1589 | // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". |
| 1590 | // ... |
| 1591 | // |
Jean-Daniel Dupas | f57c413 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 1592 | if (HasVAListArg) { |
| 1593 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) { |
| 1594 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) { |
| 1595 | int PVIndex = PV->getFunctionScopeIndex() + 1; |
| 1596 | for (specific_attr_iterator<FormatAttr> |
| 1597 | i = ND->specific_attr_begin<FormatAttr>(), |
| 1598 | e = ND->specific_attr_end<FormatAttr>(); i != e ; ++i) { |
| 1599 | FormatAttr *PVFormat = *i; |
| 1600 | // adjust for implicit parameter |
| 1601 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 1602 | if (MD->isInstance()) |
| 1603 | ++PVIndex; |
| 1604 | // We also check if the formats are compatible. |
| 1605 | // We can't pass a 'scanf' string to a 'printf' function. |
| 1606 | if (PVIndex == PVFormat->getFormatIdx() && |
| 1607 | Type == GetFormatStringType(PVFormat)) |
| 1608 | return true; |
| 1609 | } |
| 1610 | } |
| 1611 | } |
| 1612 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1613 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1614 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1615 | return false; |
| 1616 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1617 | |
Jean-Daniel Dupas | 52aabaf | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 1618 | case Stmt::CallExprClass: |
| 1619 | case Stmt::CXXMemberCallExprClass: { |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1620 | const CallExpr *CE = cast<CallExpr>(E); |
Jean-Daniel Dupas | 52aabaf | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 1621 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) { |
| 1622 | if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) { |
| 1623 | unsigned ArgIndex = FA->getFormatIdx(); |
| 1624 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 1625 | if (MD->isInstance()) |
| 1626 | --ArgIndex; |
| 1627 | const Expr *Arg = CE->getArg(ArgIndex - 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | |
Jean-Daniel Dupas | 52aabaf | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 1629 | return SemaCheckStringLiteral(Arg, Args, NumArgs, HasVAListArg, |
| 1630 | format_idx, firstDataArg, Type, |
| 1631 | inFunctionCall); |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1632 | } |
| 1633 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1634 | |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1635 | return false; |
| 1636 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1637 | case Stmt::ObjCStringLiteralClass: |
| 1638 | case Stmt::StringLiteralClass: { |
| 1639 | const StringLiteral *StrE = NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1640 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1641 | if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1642 | StrE = ObjCFExpr->getString(); |
| 1643 | else |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1644 | StrE = cast<StringLiteral>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1645 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1646 | if (StrE) { |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1647 | CheckFormatString(StrE, E, Args, NumArgs, HasVAListArg, format_idx, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1648 | firstDataArg, Type, inFunctionCall); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1649 | return true; |
| 1650 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1651 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1652 | return false; |
| 1653 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1654 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1655 | default: |
| 1656 | return false; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1657 | } |
| 1658 | } |
| 1659 | |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1660 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1661 | Sema::CheckNonNullArguments(const NonNullAttr *NonNull, |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1662 | const Expr * const *ExprArgs, |
| 1663 | SourceLocation CallSiteLoc) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1664 | for (NonNullAttr::args_iterator i = NonNull->args_begin(), |
| 1665 | e = NonNull->args_end(); |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1666 | i != e; ++i) { |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1667 | const Expr *ArgExpr = ExprArgs[*i]; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1668 | if (ArgExpr->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1669 | Expr::NPC_ValueDependentIsNotNull)) |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1670 | Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1671 | } |
| 1672 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1673 | |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1674 | Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) { |
| 1675 | return llvm::StringSwitch<FormatStringType>(Format->getType()) |
| 1676 | .Case("scanf", FST_Scanf) |
| 1677 | .Cases("printf", "printf0", FST_Printf) |
| 1678 | .Cases("NSString", "CFString", FST_NSString) |
| 1679 | .Case("strftime", FST_Strftime) |
| 1680 | .Case("strfmon", FST_Strfmon) |
| 1681 | .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf) |
| 1682 | .Default(FST_Unknown); |
| 1683 | } |
| 1684 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1685 | /// CheckPrintfScanfArguments - Check calls to printf and scanf (and similar |
| 1686 | /// functions) for correct use of format strings. |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1687 | void Sema::CheckFormatArguments(const FormatAttr *Format, CallExpr *TheCall) { |
| 1688 | bool IsCXXMember = false; |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1689 | // The way the format attribute works in GCC, the implicit this argument |
| 1690 | // of member functions is counted. However, it doesn't appear in our own |
| 1691 | // lists, so decrement format_idx in that case. |
Jean-Daniel Dupas | 52aabaf | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 1692 | IsCXXMember = isa<CXXMemberCallExpr>(TheCall); |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1693 | CheckFormatArguments(Format, TheCall->getArgs(), TheCall->getNumArgs(), |
| 1694 | IsCXXMember, TheCall->getRParenLoc(), |
| 1695 | TheCall->getCallee()->getSourceRange()); |
| 1696 | } |
| 1697 | |
| 1698 | void Sema::CheckFormatArguments(const FormatAttr *Format, Expr **Args, |
| 1699 | unsigned NumArgs, bool IsCXXMember, |
| 1700 | SourceLocation Loc, SourceRange Range) { |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1701 | bool HasVAListArg = Format->getFirstArg() == 0; |
| 1702 | unsigned format_idx = Format->getFormatIdx() - 1; |
| 1703 | unsigned firstDataArg = HasVAListArg ? 0 : Format->getFirstArg() - 1; |
| 1704 | if (IsCXXMember) { |
| 1705 | if (format_idx == 0) |
| 1706 | return; |
| 1707 | --format_idx; |
| 1708 | if(firstDataArg != 0) |
| 1709 | --firstDataArg; |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1710 | } |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1711 | CheckFormatArguments(Args, NumArgs, HasVAListArg, format_idx, |
| 1712 | firstDataArg, GetFormatStringType(Format), Loc, Range); |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1713 | } |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1714 | |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1715 | void Sema::CheckFormatArguments(Expr **Args, unsigned NumArgs, |
| 1716 | bool HasVAListArg, unsigned format_idx, |
| 1717 | unsigned firstDataArg, FormatStringType Type, |
| 1718 | SourceLocation Loc, SourceRange Range) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1719 | // CHECK: printf/scanf-like function is called with no format string. |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1720 | if (format_idx >= NumArgs) { |
| 1721 | Diag(Loc, diag::warn_missing_format_string) << Range; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1722 | return; |
| 1723 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1724 | |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1725 | const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1726 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1727 | // CHECK: format string is not a string literal. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1728 | // |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1729 | // Dynamically generated format strings are difficult to |
| 1730 | // automatically vet at compile time. Requiring that format strings |
| 1731 | // are string literals: (1) permits the checking of format strings by |
| 1732 | // the compiler and thereby (2) can practically remove the source of |
| 1733 | // many format string exploits. |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1734 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | // Format string can be either ObjC string (e.g. @"%d") or |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1736 | // C string (e.g. "%d") |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1737 | // 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] | 1738 | // the same format string checking logic for both ObjC and C strings. |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1739 | if (SemaCheckStringLiteral(OrigFormatExpr, Args, NumArgs, HasVAListArg, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1740 | format_idx, firstDataArg, Type)) |
Chris Lattner | 1cd3e1f | 2009-04-29 04:49:34 +0000 | [diff] [blame] | 1741 | return; // Literal format string found, check done! |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1742 | |
Jean-Daniel Dupas | 2837a2f | 2012-02-07 23:10:53 +0000 | [diff] [blame] | 1743 | // Strftime is particular as it always uses a single 'time' argument, |
| 1744 | // so it is safe to pass a non-literal string. |
| 1745 | if (Type == FST_Strftime) |
| 1746 | return; |
| 1747 | |
Jean-Daniel Dupas | ce3aa39 | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 1748 | // Do not emit diag when the string param is a macro expansion and the |
| 1749 | // format is either NSString or CFString. This is a hack to prevent |
| 1750 | // diag when using the NSLocalizedString and CFCopyLocalizedString macros |
| 1751 | // which are usually used in place of NS and CF string literals. |
| 1752 | if (Type == FST_NSString && Args[format_idx]->getLocStart().isMacroID()) |
| 1753 | return; |
| 1754 | |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1755 | // If there are no arguments specified, warn with -Wformat-security, otherwise |
| 1756 | // warn only with -Wformat-nonliteral. |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1757 | if (NumArgs == format_idx+1) |
| 1758 | Diag(Args[format_idx]->getLocStart(), |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1759 | diag::warn_format_nonliteral_noargs) |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1760 | << OrigFormatExpr->getSourceRange(); |
| 1761 | else |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1762 | Diag(Args[format_idx]->getLocStart(), |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1763 | diag::warn_format_nonliteral) |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1764 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1765 | } |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1766 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1767 | namespace { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1768 | class CheckFormatHandler : public analyze_format_string::FormatStringHandler { |
| 1769 | protected: |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1770 | Sema &S; |
| 1771 | const StringLiteral *FExpr; |
| 1772 | const Expr *OrigFormatExpr; |
Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1773 | const unsigned FirstDataArg; |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1774 | const unsigned NumDataArgs; |
| 1775 | const bool IsObjCLiteral; |
| 1776 | const char *Beg; // Start of format string. |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1777 | const bool HasVAListArg; |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1778 | const Expr * const *Args; |
| 1779 | const unsigned NumArgs; |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1780 | unsigned FormatIdx; |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1781 | llvm::BitVector CoveredArgs; |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1782 | bool usesPositionalArgs; |
| 1783 | bool atFirstArg; |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1784 | bool inFunctionCall; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1785 | public: |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1786 | CheckFormatHandler(Sema &s, const StringLiteral *fexpr, |
Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1787 | const Expr *origFormatExpr, unsigned firstDataArg, |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1788 | unsigned numDataArgs, bool isObjCLiteral, |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1789 | const char *beg, bool hasVAListArg, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1790 | Expr **args, unsigned numArgs, |
| 1791 | unsigned formatIdx, bool inFunctionCall) |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1792 | : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), |
Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1793 | FirstDataArg(firstDataArg), |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1794 | NumDataArgs(numDataArgs), |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1795 | IsObjCLiteral(isObjCLiteral), Beg(beg), |
| 1796 | HasVAListArg(hasVAListArg), |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1797 | Args(args), NumArgs(numArgs), FormatIdx(formatIdx), |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1798 | usesPositionalArgs(false), atFirstArg(true), |
| 1799 | inFunctionCall(inFunctionCall) { |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1800 | CoveredArgs.resize(numDataArgs); |
| 1801 | CoveredArgs.reset(); |
| 1802 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1803 | |
Ted Kremenek | 07d161f | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 1804 | void DoneProcessing(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1805 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1806 | void HandleIncompleteSpecifier(const char *startSpecifier, |
| 1807 | unsigned specifierLen); |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 1808 | |
| 1809 | void HandleNonStandardLengthModifier( |
| 1810 | const analyze_format_string::LengthModifier &LM, |
| 1811 | const char *startSpecifier, unsigned specifierLen); |
| 1812 | |
| 1813 | void HandleNonStandardConversionSpecifier( |
| 1814 | const analyze_format_string::ConversionSpecifier &CS, |
| 1815 | const char *startSpecifier, unsigned specifierLen); |
| 1816 | |
| 1817 | void HandleNonStandardConversionSpecification( |
| 1818 | const analyze_format_string::LengthModifier &LM, |
| 1819 | const analyze_format_string::ConversionSpecifier &CS, |
| 1820 | const char *startSpecifier, unsigned specifierLen); |
| 1821 | |
Hans Wennborg | f856264 | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 1822 | virtual void HandlePosition(const char *startPos, unsigned posLen); |
| 1823 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1824 | virtual void HandleInvalidPosition(const char *startSpecifier, |
| 1825 | unsigned specifierLen, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1826 | analyze_format_string::PositionContext p); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1827 | |
| 1828 | virtual void HandleZeroPosition(const char *startPos, unsigned posLen); |
| 1829 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1830 | void HandleNullChar(const char *nullCharacter); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1831 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1832 | template <typename Range> |
| 1833 | static void EmitFormatDiagnostic(Sema &S, bool inFunctionCall, |
| 1834 | const Expr *ArgumentExpr, |
| 1835 | PartialDiagnostic PDiag, |
| 1836 | SourceLocation StringLoc, |
| 1837 | bool IsStringLocation, Range StringRange, |
| 1838 | FixItHint Fixit = FixItHint()); |
| 1839 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1840 | protected: |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1841 | bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, |
| 1842 | const char *startSpec, |
| 1843 | unsigned specifierLen, |
| 1844 | const char *csStart, unsigned csLen); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1845 | |
| 1846 | void HandlePositionalNonpositionalArgs(SourceLocation Loc, |
| 1847 | const char *startSpec, |
| 1848 | unsigned specifierLen); |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1849 | |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 1850 | SourceRange getFormatStringRange(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1851 | CharSourceRange getSpecifierRange(const char *startSpecifier, |
| 1852 | unsigned specifierLen); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1853 | SourceLocation getLocationOfByte(const char *x); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1854 | |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1855 | const Expr *getDataArg(unsigned i) const; |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 1856 | |
| 1857 | bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, |
| 1858 | const analyze_format_string::ConversionSpecifier &CS, |
| 1859 | const char *startSpecifier, unsigned specifierLen, |
| 1860 | unsigned argIndex); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1861 | |
| 1862 | template <typename Range> |
| 1863 | void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc, |
| 1864 | bool IsStringLocation, Range StringRange, |
| 1865 | FixItHint Fixit = FixItHint()); |
| 1866 | |
| 1867 | void CheckPositionalAndNonpositionalArgs( |
| 1868 | const analyze_format_string::FormatSpecifier *FS); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1869 | }; |
| 1870 | } |
| 1871 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1872 | SourceRange CheckFormatHandler::getFormatStringRange() { |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1873 | return OrigFormatExpr->getSourceRange(); |
| 1874 | } |
| 1875 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1876 | CharSourceRange CheckFormatHandler:: |
| 1877 | getSpecifierRange(const char *startSpecifier, unsigned specifierLen) { |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 1878 | SourceLocation Start = getLocationOfByte(startSpecifier); |
| 1879 | SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1); |
| 1880 | |
| 1881 | // Advance the end SourceLocation by one due to half-open ranges. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1882 | End = End.getLocWithOffset(1); |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 1883 | |
| 1884 | return CharSourceRange::getCharRange(Start, End); |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1887 | SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1888 | return S.getLocationOfStringLiteralByte(FExpr, x - Beg); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1891 | void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, |
| 1892 | unsigned specifierLen){ |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1893 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier), |
| 1894 | getLocationOfByte(startSpecifier), |
| 1895 | /*IsStringLocation*/true, |
| 1896 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 1899 | void CheckFormatHandler::HandleNonStandardLengthModifier( |
| 1900 | const analyze_format_string::LengthModifier &LM, |
| 1901 | const char *startSpecifier, unsigned specifierLen) { |
| 1902 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) << LM.toString() |
Hans Wennborg | f856264 | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 1903 | << 0, |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 1904 | getLocationOfByte(LM.getStart()), |
| 1905 | /*IsStringLocation*/true, |
| 1906 | getSpecifierRange(startSpecifier, specifierLen)); |
| 1907 | } |
| 1908 | |
| 1909 | void CheckFormatHandler::HandleNonStandardConversionSpecifier( |
| 1910 | const analyze_format_string::ConversionSpecifier &CS, |
| 1911 | const char *startSpecifier, unsigned specifierLen) { |
| 1912 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) << CS.toString() |
Hans Wennborg | f856264 | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 1913 | << 1, |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 1914 | getLocationOfByte(CS.getStart()), |
| 1915 | /*IsStringLocation*/true, |
| 1916 | getSpecifierRange(startSpecifier, specifierLen)); |
| 1917 | } |
| 1918 | |
| 1919 | void CheckFormatHandler::HandleNonStandardConversionSpecification( |
| 1920 | const analyze_format_string::LengthModifier &LM, |
| 1921 | const analyze_format_string::ConversionSpecifier &CS, |
| 1922 | const char *startSpecifier, unsigned specifierLen) { |
| 1923 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_conversion_spec) |
| 1924 | << LM.toString() << CS.toString(), |
| 1925 | getLocationOfByte(LM.getStart()), |
| 1926 | /*IsStringLocation*/true, |
| 1927 | getSpecifierRange(startSpecifier, specifierLen)); |
| 1928 | } |
| 1929 | |
Hans Wennborg | f856264 | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 1930 | void CheckFormatHandler::HandlePosition(const char *startPos, |
| 1931 | unsigned posLen) { |
| 1932 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg), |
| 1933 | getLocationOfByte(startPos), |
| 1934 | /*IsStringLocation*/true, |
| 1935 | getSpecifierRange(startPos, posLen)); |
| 1936 | } |
| 1937 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1938 | void |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1939 | CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen, |
| 1940 | analyze_format_string::PositionContext p) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1941 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier) |
| 1942 | << (unsigned) p, |
| 1943 | getLocationOfByte(startPos), /*IsStringLocation*/true, |
| 1944 | getSpecifierRange(startPos, posLen)); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1947 | void CheckFormatHandler::HandleZeroPosition(const char *startPos, |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1948 | unsigned posLen) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1949 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier), |
| 1950 | getLocationOfByte(startPos), |
| 1951 | /*IsStringLocation*/true, |
| 1952 | getSpecifierRange(startPos, posLen)); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1955 | void CheckFormatHandler::HandleNullChar(const char *nullCharacter) { |
Ted Kremenek | 0c06944 | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 1956 | if (!IsObjCLiteral) { |
| 1957 | // The presence of a null character is likely an error. |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1958 | EmitFormatDiagnostic( |
| 1959 | S.PDiag(diag::warn_printf_format_string_contains_null_char), |
| 1960 | getLocationOfByte(nullCharacter), /*IsStringLocation*/true, |
| 1961 | getFormatStringRange()); |
Ted Kremenek | 0c06944 | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 1962 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1963 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1964 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1965 | const Expr *CheckFormatHandler::getDataArg(unsigned i) const { |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1966 | return Args[FirstDataArg + i]; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1967 | } |
| 1968 | |
| 1969 | void CheckFormatHandler::DoneProcessing() { |
| 1970 | // Does the number of data arguments exceed the number of |
| 1971 | // format conversions in the format string? |
| 1972 | if (!HasVAListArg) { |
| 1973 | // Find any arguments that weren't covered. |
| 1974 | CoveredArgs.flip(); |
| 1975 | signed notCoveredArg = CoveredArgs.find_first(); |
| 1976 | if (notCoveredArg >= 0) { |
| 1977 | assert((unsigned)notCoveredArg < NumDataArgs); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1978 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_data_arg_not_used), |
| 1979 | getDataArg((unsigned) notCoveredArg)->getLocStart(), |
| 1980 | /*IsStringLocation*/false, getFormatStringRange()); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1981 | } |
| 1982 | } |
| 1983 | } |
| 1984 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1985 | bool |
| 1986 | CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, |
| 1987 | SourceLocation Loc, |
| 1988 | const char *startSpec, |
| 1989 | unsigned specifierLen, |
| 1990 | const char *csStart, |
| 1991 | unsigned csLen) { |
| 1992 | |
| 1993 | bool keepGoing = true; |
| 1994 | if (argIndex < NumDataArgs) { |
| 1995 | // Consider the argument coverered, even though the specifier doesn't |
| 1996 | // make sense. |
| 1997 | CoveredArgs.set(argIndex); |
| 1998 | } |
| 1999 | else { |
| 2000 | // If argIndex exceeds the number of data arguments we |
| 2001 | // don't issue a warning because that is just a cascade of warnings (and |
| 2002 | // they may have intended '%%' anyway). We don't want to continue processing |
| 2003 | // the format string after this point, however, as we will like just get |
| 2004 | // gibberish when trying to match arguments. |
| 2005 | keepGoing = false; |
| 2006 | } |
| 2007 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2008 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_conversion) |
| 2009 | << StringRef(csStart, csLen), |
| 2010 | Loc, /*IsStringLocation*/true, |
| 2011 | getSpecifierRange(startSpec, specifierLen)); |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2012 | |
| 2013 | return keepGoing; |
| 2014 | } |
| 2015 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2016 | void |
| 2017 | CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc, |
| 2018 | const char *startSpec, |
| 2019 | unsigned specifierLen) { |
| 2020 | EmitFormatDiagnostic( |
| 2021 | S.PDiag(diag::warn_format_mix_positional_nonpositional_args), |
| 2022 | Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen)); |
| 2023 | } |
| 2024 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2025 | bool |
| 2026 | CheckFormatHandler::CheckNumArgs( |
| 2027 | const analyze_format_string::FormatSpecifier &FS, |
| 2028 | const analyze_format_string::ConversionSpecifier &CS, |
| 2029 | const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { |
| 2030 | |
| 2031 | if (argIndex >= NumDataArgs) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2032 | PartialDiagnostic PDiag = FS.usesPositionalArg() |
| 2033 | ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args) |
| 2034 | << (argIndex+1) << NumDataArgs) |
| 2035 | : S.PDiag(diag::warn_printf_insufficient_data_args); |
| 2036 | EmitFormatDiagnostic( |
| 2037 | PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true, |
| 2038 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2039 | return false; |
| 2040 | } |
| 2041 | return true; |
| 2042 | } |
| 2043 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2044 | template<typename Range> |
| 2045 | void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag, |
| 2046 | SourceLocation Loc, |
| 2047 | bool IsStringLocation, |
| 2048 | Range StringRange, |
| 2049 | FixItHint FixIt) { |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2050 | EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag, |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2051 | Loc, IsStringLocation, StringRange, FixIt); |
| 2052 | } |
| 2053 | |
| 2054 | /// \brief If the format string is not within the funcion call, emit a note |
| 2055 | /// so that the function call and string are in diagnostic messages. |
| 2056 | /// |
| 2057 | /// \param inFunctionCall if true, the format string is within the function |
| 2058 | /// call and only one diagnostic message will be produced. Otherwise, an |
| 2059 | /// extra note will be emitted pointing to location of the format string. |
| 2060 | /// |
| 2061 | /// \param ArgumentExpr the expression that is passed as the format string |
| 2062 | /// argument in the function call. Used for getting locations when two |
| 2063 | /// diagnostics are emitted. |
| 2064 | /// |
| 2065 | /// \param PDiag the callee should already have provided any strings for the |
| 2066 | /// diagnostic message. This function only adds locations and fixits |
| 2067 | /// to diagnostics. |
| 2068 | /// |
| 2069 | /// \param Loc primary location for diagnostic. If two diagnostics are |
| 2070 | /// required, one will be at Loc and a new SourceLocation will be created for |
| 2071 | /// the other one. |
| 2072 | /// |
| 2073 | /// \param IsStringLocation if true, Loc points to the format string should be |
| 2074 | /// used for the note. Otherwise, Loc points to the argument list and will |
| 2075 | /// be used with PDiag. |
| 2076 | /// |
| 2077 | /// \param StringRange some or all of the string to highlight. This is |
| 2078 | /// templated so it can accept either a CharSourceRange or a SourceRange. |
| 2079 | /// |
| 2080 | /// \param Fixit optional fix it hint for the format string. |
| 2081 | template<typename Range> |
| 2082 | void CheckFormatHandler::EmitFormatDiagnostic(Sema &S, bool InFunctionCall, |
| 2083 | const Expr *ArgumentExpr, |
| 2084 | PartialDiagnostic PDiag, |
| 2085 | SourceLocation Loc, |
| 2086 | bool IsStringLocation, |
| 2087 | Range StringRange, |
| 2088 | FixItHint FixIt) { |
| 2089 | if (InFunctionCall) |
| 2090 | S.Diag(Loc, PDiag) << StringRange << FixIt; |
| 2091 | else { |
| 2092 | S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag) |
| 2093 | << ArgumentExpr->getSourceRange(); |
| 2094 | S.Diag(IsStringLocation ? Loc : StringRange.getBegin(), |
| 2095 | diag::note_format_string_defined) |
| 2096 | << StringRange << FixIt; |
| 2097 | } |
| 2098 | } |
| 2099 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2100 | //===--- CHECK: Printf format string checking ------------------------------===// |
| 2101 | |
| 2102 | namespace { |
| 2103 | class CheckPrintfHandler : public CheckFormatHandler { |
| 2104 | public: |
| 2105 | CheckPrintfHandler(Sema &s, const StringLiteral *fexpr, |
| 2106 | const Expr *origFormatExpr, unsigned firstDataArg, |
| 2107 | unsigned numDataArgs, bool isObjCLiteral, |
| 2108 | const char *beg, bool hasVAListArg, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2109 | Expr **Args, unsigned NumArgs, |
| 2110 | unsigned formatIdx, bool inFunctionCall) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2111 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
| 2112 | numDataArgs, isObjCLiteral, beg, hasVAListArg, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2113 | Args, NumArgs, formatIdx, inFunctionCall) {} |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2114 | |
| 2115 | |
| 2116 | bool HandleInvalidPrintfConversionSpecifier( |
| 2117 | const analyze_printf::PrintfSpecifier &FS, |
| 2118 | const char *startSpecifier, |
| 2119 | unsigned specifierLen); |
| 2120 | |
| 2121 | bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, |
| 2122 | const char *startSpecifier, |
| 2123 | unsigned specifierLen); |
| 2124 | |
| 2125 | bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, |
| 2126 | const char *startSpecifier, unsigned specifierLen); |
| 2127 | void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, |
| 2128 | const analyze_printf::OptionalAmount &Amt, |
| 2129 | unsigned type, |
| 2130 | const char *startSpecifier, unsigned specifierLen); |
| 2131 | void HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
| 2132 | const analyze_printf::OptionalFlag &flag, |
| 2133 | const char *startSpecifier, unsigned specifierLen); |
| 2134 | void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, |
| 2135 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 2136 | const analyze_printf::OptionalFlag &flag, |
| 2137 | const char *startSpecifier, unsigned specifierLen); |
| 2138 | }; |
| 2139 | } |
| 2140 | |
| 2141 | bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( |
| 2142 | const analyze_printf::PrintfSpecifier &FS, |
| 2143 | const char *startSpecifier, |
| 2144 | unsigned specifierLen) { |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2145 | const analyze_printf::PrintfConversionSpecifier &CS = |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2146 | FS.getConversionSpecifier(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2147 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2148 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 2149 | getLocationOfByte(CS.getStart()), |
| 2150 | startSpecifier, specifierLen, |
| 2151 | CS.getStart(), CS.getLength()); |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2154 | bool CheckPrintfHandler::HandleAmount( |
| 2155 | const analyze_format_string::OptionalAmount &Amt, |
| 2156 | unsigned k, const char *startSpecifier, |
| 2157 | unsigned specifierLen) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2158 | |
| 2159 | if (Amt.hasDataArgument()) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2160 | if (!HasVAListArg) { |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2161 | unsigned argIndex = Amt.getArgIndex(); |
| 2162 | if (argIndex >= NumDataArgs) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2163 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg) |
| 2164 | << k, |
| 2165 | getLocationOfByte(Amt.getStart()), |
| 2166 | /*IsStringLocation*/true, |
| 2167 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2168 | // Don't do any more checking. We will just emit |
| 2169 | // spurious errors. |
| 2170 | return false; |
| 2171 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2172 | |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2173 | // Type check the data argument. It should be an 'int'. |
Ted Kremenek | 31f8e32 | 2010-01-29 23:32:22 +0000 | [diff] [blame] | 2174 | // Although not in conformance with C99, we also allow the argument to be |
| 2175 | // an 'unsigned int' as that is a reasonably safe case. GCC also |
| 2176 | // doesn't emit a warning for that case. |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2177 | CoveredArgs.set(argIndex); |
| 2178 | const Expr *Arg = getDataArg(argIndex); |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2179 | QualType T = Arg->getType(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2180 | |
| 2181 | const analyze_printf::ArgTypeResult &ATR = Amt.getArgType(S.Context); |
| 2182 | assert(ATR.isValid()); |
| 2183 | |
| 2184 | if (!ATR.matchesType(S.Context, T)) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2185 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type) |
Hans Wennborg | a792aff | 2011-12-07 10:33:11 +0000 | [diff] [blame] | 2186 | << k << ATR.getRepresentativeTypeName(S.Context) |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2187 | << T << Arg->getSourceRange(), |
| 2188 | getLocationOfByte(Amt.getStart()), |
| 2189 | /*IsStringLocation*/true, |
| 2190 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2191 | // Don't do any more checking. We will just emit |
| 2192 | // spurious errors. |
| 2193 | return false; |
| 2194 | } |
| 2195 | } |
| 2196 | } |
| 2197 | return true; |
| 2198 | } |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2199 | |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2200 | void CheckPrintfHandler::HandleInvalidAmount( |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2201 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2202 | const analyze_printf::OptionalAmount &Amt, |
| 2203 | unsigned type, |
| 2204 | const char *startSpecifier, |
| 2205 | unsigned specifierLen) { |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2206 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 2207 | FS.getConversionSpecifier(); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2208 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2209 | FixItHint fixit = |
| 2210 | Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant |
| 2211 | ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), |
| 2212 | Amt.getConstantLength())) |
| 2213 | : FixItHint(); |
| 2214 | |
| 2215 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount) |
| 2216 | << type << CS.toString(), |
| 2217 | getLocationOfByte(Amt.getStart()), |
| 2218 | /*IsStringLocation*/true, |
| 2219 | getSpecifierRange(startSpecifier, specifierLen), |
| 2220 | fixit); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2223 | void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2224 | const analyze_printf::OptionalFlag &flag, |
| 2225 | const char *startSpecifier, |
| 2226 | unsigned specifierLen) { |
| 2227 | // Warn about pointless flag with a fixit removal. |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2228 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 2229 | FS.getConversionSpecifier(); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2230 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag) |
| 2231 | << flag.toString() << CS.toString(), |
| 2232 | getLocationOfByte(flag.getPosition()), |
| 2233 | /*IsStringLocation*/true, |
| 2234 | getSpecifierRange(startSpecifier, specifierLen), |
| 2235 | FixItHint::CreateRemoval( |
| 2236 | getSpecifierRange(flag.getPosition(), 1))); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | void CheckPrintfHandler::HandleIgnoredFlag( |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2240 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2241 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 2242 | const analyze_printf::OptionalFlag &flag, |
| 2243 | const char *startSpecifier, |
| 2244 | unsigned specifierLen) { |
| 2245 | // Warn about ignored flag with a fixit removal. |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2246 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag) |
| 2247 | << ignoredFlag.toString() << flag.toString(), |
| 2248 | getLocationOfByte(ignoredFlag.getPosition()), |
| 2249 | /*IsStringLocation*/true, |
| 2250 | getSpecifierRange(startSpecifier, specifierLen), |
| 2251 | FixItHint::CreateRemoval( |
| 2252 | getSpecifierRange(ignoredFlag.getPosition(), 1))); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2255 | bool |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2256 | CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier |
Ted Kremenek | 5c41ee8 | 2010-02-11 09:27:41 +0000 | [diff] [blame] | 2257 | &FS, |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2258 | const char *startSpecifier, |
| 2259 | unsigned specifierLen) { |
| 2260 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2261 | using namespace analyze_format_string; |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2262 | using namespace analyze_printf; |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2263 | const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2264 | |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2265 | if (FS.consumesDataArgument()) { |
| 2266 | if (atFirstArg) { |
| 2267 | atFirstArg = false; |
| 2268 | usesPositionalArgs = FS.usesPositionalArg(); |
| 2269 | } |
| 2270 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2271 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), |
| 2272 | startSpecifier, specifierLen); |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2273 | return false; |
| 2274 | } |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2275 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2276 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2277 | // First check if the field width, precision, and conversion specifier |
| 2278 | // have matching data arguments. |
| 2279 | if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, |
| 2280 | startSpecifier, specifierLen)) { |
| 2281 | return false; |
| 2282 | } |
| 2283 | |
| 2284 | if (!HandleAmount(FS.getPrecision(), /* precision */ 1, |
| 2285 | startSpecifier, specifierLen)) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2286 | return false; |
| 2287 | } |
| 2288 | |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2289 | if (!CS.consumesDataArgument()) { |
| 2290 | // FIXME: Technically specifying a precision or field width here |
| 2291 | // makes no sense. Worth issuing a warning at some point. |
Ted Kremenek | 0e5675d | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 2292 | return true; |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2293 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2294 | |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2295 | // Consume the argument. |
| 2296 | unsigned argIndex = FS.getArgIndex(); |
Ted Kremenek | e3fc547 | 2010-02-27 08:34:51 +0000 | [diff] [blame] | 2297 | if (argIndex < NumDataArgs) { |
| 2298 | // The check to see if the argIndex is valid will come later. |
| 2299 | // We set the bit here because we may exit early from this |
| 2300 | // function if we encounter some other error. |
| 2301 | CoveredArgs.set(argIndex); |
| 2302 | } |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2303 | |
| 2304 | // Check for using an Objective-C specific conversion specifier |
| 2305 | // in a non-ObjC literal. |
| 2306 | if (!IsObjCLiteral && CS.isObjCArg()) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2307 | return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, |
| 2308 | specifierLen); |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2309 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2310 | |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2311 | // Check for invalid use of field width |
| 2312 | if (!FS.hasValidFieldWidth()) { |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2313 | HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2314 | startSpecifier, specifierLen); |
| 2315 | } |
| 2316 | |
| 2317 | // Check for invalid use of precision |
| 2318 | if (!FS.hasValidPrecision()) { |
| 2319 | HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, |
| 2320 | startSpecifier, specifierLen); |
| 2321 | } |
| 2322 | |
| 2323 | // Check each flag does not conflict with any other component. |
Ted Kremenek | 65197b4 | 2011-01-08 05:28:46 +0000 | [diff] [blame] | 2324 | if (!FS.hasValidThousandsGroupingPrefix()) |
| 2325 | HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2326 | if (!FS.hasValidLeadingZeros()) |
| 2327 | HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); |
| 2328 | if (!FS.hasValidPlusPrefix()) |
| 2329 | HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2330 | if (!FS.hasValidSpacePrefix()) |
| 2331 | HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2332 | if (!FS.hasValidAlternativeForm()) |
| 2333 | HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); |
| 2334 | if (!FS.hasValidLeftJustified()) |
| 2335 | HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); |
| 2336 | |
| 2337 | // Check that flags are not ignored by another flag |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2338 | if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' |
| 2339 | HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), |
| 2340 | startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2341 | if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' |
| 2342 | HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), |
| 2343 | startSpecifier, specifierLen); |
| 2344 | |
| 2345 | // Check the length modifier is valid with the given conversion specifier. |
| 2346 | const LengthModifier &LM = FS.getLengthModifier(); |
| 2347 | if (!FS.hasValidLengthModifier()) |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2348 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_nonsensical_length) |
| 2349 | << LM.toString() << CS.toString(), |
| 2350 | getLocationOfByte(LM.getStart()), |
| 2351 | /*IsStringLocation*/true, |
| 2352 | getSpecifierRange(startSpecifier, specifierLen), |
| 2353 | FixItHint::CreateRemoval( |
| 2354 | getSpecifierRange(LM.getStart(), |
| 2355 | LM.getLength()))); |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2356 | if (!FS.hasStandardLengthModifier()) |
| 2357 | HandleNonStandardLengthModifier(LM, startSpecifier, specifierLen); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2358 | if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2359 | HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); |
| 2360 | if (!FS.hasStandardLengthConversionCombination()) |
| 2361 | HandleNonStandardConversionSpecification(LM, CS, startSpecifier, |
| 2362 | specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2363 | |
| 2364 | // Are we using '%n'? |
Ted Kremenek | 35d353b | 2010-07-20 20:04:10 +0000 | [diff] [blame] | 2365 | if (CS.getKind() == ConversionSpecifier::nArg) { |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2366 | // Issue a warning about this being a possible security issue. |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2367 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_write_back), |
| 2368 | getLocationOfByte(CS.getStart()), |
| 2369 | /*IsStringLocation*/true, |
| 2370 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | e82d804 | 2010-01-29 01:35:25 +0000 | [diff] [blame] | 2371 | // Continue checking the other format specifiers. |
| 2372 | return true; |
| 2373 | } |
Ted Kremenek | 5c41ee8 | 2010-02-11 09:27:41 +0000 | [diff] [blame] | 2374 | |
Ted Kremenek | da51f0d | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 2375 | // The remaining checks depend on the data arguments. |
| 2376 | if (HasVAListArg) |
| 2377 | return true; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2378 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2379 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | da51f0d | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 2380 | return false; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2381 | |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2382 | // Now type check the data expression that matches the |
| 2383 | // format specifier. |
| 2384 | const Expr *Ex = getDataArg(argIndex); |
Nico Weber | 339b907 | 2012-01-31 01:43:25 +0000 | [diff] [blame] | 2385 | const analyze_printf::ArgTypeResult &ATR = FS.getArgType(S.Context, |
| 2386 | IsObjCLiteral); |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2387 | if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) { |
| 2388 | // Check if we didn't match because of an implicit cast from a 'char' |
| 2389 | // or 'short' to an 'int'. This is done because printf is a varargs |
| 2390 | // function. |
| 2391 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Ex)) |
Ted Kremenek | 4d8ae4d | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 2392 | if (ICE->getType() == S.Context.IntTy) { |
| 2393 | // All further checking is done on the subexpression. |
| 2394 | Ex = ICE->getSubExpr(); |
| 2395 | if (ATR.matchesType(S.Context, Ex->getType())) |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2396 | return true; |
Ted Kremenek | 4d8ae4d | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 2397 | } |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2398 | |
| 2399 | // We may be able to offer a FixItHint if it is a supported type. |
| 2400 | PrintfSpecifier fixedFS = FS; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2401 | bool success = fixedFS.fixType(Ex->getType(), S.getLangOpts(), |
Hans Wennborg | be6126a | 2012-02-15 09:59:46 +0000 | [diff] [blame] | 2402 | S.Context, IsObjCLiteral); |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2403 | |
| 2404 | if (success) { |
| 2405 | // Get the fix string from the fixed format specifier |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2406 | SmallString<128> buf; |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2407 | llvm::raw_svector_ostream os(buf); |
| 2408 | fixedFS.toString(os); |
| 2409 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2410 | EmitFormatDiagnostic( |
| 2411 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
Hans Wennborg | a792aff | 2011-12-07 10:33:11 +0000 | [diff] [blame] | 2412 | << ATR.getRepresentativeTypeName(S.Context) << Ex->getType() |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2413 | << Ex->getSourceRange(), |
| 2414 | getLocationOfByte(CS.getStart()), |
| 2415 | /*IsStringLocation*/true, |
| 2416 | getSpecifierRange(startSpecifier, specifierLen), |
| 2417 | FixItHint::CreateReplacement( |
| 2418 | getSpecifierRange(startSpecifier, specifierLen), |
| 2419 | os.str())); |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2420 | } |
| 2421 | else { |
Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 2422 | EmitFormatDiagnostic( |
| 2423 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
| 2424 | << ATR.getRepresentativeTypeName(S.Context) << Ex->getType() |
| 2425 | << getSpecifierRange(startSpecifier, specifierLen) |
| 2426 | << Ex->getSourceRange(), |
| 2427 | getLocationOfByte(CS.getStart()), |
| 2428 | true, |
| 2429 | getSpecifierRange(startSpecifier, specifierLen)); |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2430 | } |
| 2431 | } |
| 2432 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2433 | return true; |
| 2434 | } |
| 2435 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2436 | //===--- CHECK: Scanf format string checking ------------------------------===// |
| 2437 | |
| 2438 | namespace { |
| 2439 | class CheckScanfHandler : public CheckFormatHandler { |
| 2440 | public: |
| 2441 | CheckScanfHandler(Sema &s, const StringLiteral *fexpr, |
| 2442 | const Expr *origFormatExpr, unsigned firstDataArg, |
| 2443 | unsigned numDataArgs, bool isObjCLiteral, |
| 2444 | const char *beg, bool hasVAListArg, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2445 | Expr **Args, unsigned NumArgs, |
| 2446 | unsigned formatIdx, bool inFunctionCall) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2447 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
| 2448 | numDataArgs, isObjCLiteral, beg, hasVAListArg, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2449 | Args, NumArgs, formatIdx, inFunctionCall) {} |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2450 | |
| 2451 | bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, |
| 2452 | const char *startSpecifier, |
| 2453 | unsigned specifierLen); |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2454 | |
| 2455 | bool HandleInvalidScanfConversionSpecifier( |
| 2456 | const analyze_scanf::ScanfSpecifier &FS, |
| 2457 | const char *startSpecifier, |
| 2458 | unsigned specifierLen); |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2459 | |
| 2460 | void HandleIncompleteScanList(const char *start, const char *end); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2461 | }; |
Ted Kremenek | 07d161f | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 2462 | } |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2463 | |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2464 | void CheckScanfHandler::HandleIncompleteScanList(const char *start, |
| 2465 | const char *end) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2466 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete), |
| 2467 | getLocationOfByte(end), /*IsStringLocation*/true, |
| 2468 | getSpecifierRange(start, end - start)); |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2469 | } |
| 2470 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2471 | bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( |
| 2472 | const analyze_scanf::ScanfSpecifier &FS, |
| 2473 | const char *startSpecifier, |
| 2474 | unsigned specifierLen) { |
| 2475 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2476 | const analyze_scanf::ScanfConversionSpecifier &CS = |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2477 | FS.getConversionSpecifier(); |
| 2478 | |
| 2479 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 2480 | getLocationOfByte(CS.getStart()), |
| 2481 | startSpecifier, specifierLen, |
| 2482 | CS.getStart(), CS.getLength()); |
| 2483 | } |
| 2484 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2485 | bool CheckScanfHandler::HandleScanfSpecifier( |
| 2486 | const analyze_scanf::ScanfSpecifier &FS, |
| 2487 | const char *startSpecifier, |
| 2488 | unsigned specifierLen) { |
| 2489 | |
| 2490 | using namespace analyze_scanf; |
| 2491 | using namespace analyze_format_string; |
| 2492 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2493 | const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2494 | |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2495 | // Handle case where '%' and '*' don't consume an argument. These shouldn't |
| 2496 | // be used to decide if we are using positional arguments consistently. |
| 2497 | if (FS.consumesDataArgument()) { |
| 2498 | if (atFirstArg) { |
| 2499 | atFirstArg = false; |
| 2500 | usesPositionalArgs = FS.usesPositionalArg(); |
| 2501 | } |
| 2502 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2503 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), |
| 2504 | startSpecifier, specifierLen); |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2505 | return false; |
| 2506 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
| 2509 | // Check if the field with is non-zero. |
| 2510 | const OptionalAmount &Amt = FS.getFieldWidth(); |
| 2511 | if (Amt.getHowSpecified() == OptionalAmount::Constant) { |
| 2512 | if (Amt.getConstantAmount() == 0) { |
| 2513 | const CharSourceRange &R = getSpecifierRange(Amt.getStart(), |
| 2514 | Amt.getConstantLength()); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2515 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width), |
| 2516 | getLocationOfByte(Amt.getStart()), |
| 2517 | /*IsStringLocation*/true, R, |
| 2518 | FixItHint::CreateRemoval(R)); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2519 | } |
| 2520 | } |
| 2521 | |
| 2522 | if (!FS.consumesDataArgument()) { |
| 2523 | // FIXME: Technically specifying a precision or field width here |
| 2524 | // makes no sense. Worth issuing a warning at some point. |
| 2525 | return true; |
| 2526 | } |
| 2527 | |
| 2528 | // Consume the argument. |
| 2529 | unsigned argIndex = FS.getArgIndex(); |
| 2530 | if (argIndex < NumDataArgs) { |
| 2531 | // The check to see if the argIndex is valid will come later. |
| 2532 | // We set the bit here because we may exit early from this |
| 2533 | // function if we encounter some other error. |
| 2534 | CoveredArgs.set(argIndex); |
| 2535 | } |
| 2536 | |
Ted Kremenek | 1e51c20 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 2537 | // Check the length modifier is valid with the given conversion specifier. |
| 2538 | const LengthModifier &LM = FS.getLengthModifier(); |
| 2539 | if (!FS.hasValidLengthModifier()) { |
Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 2540 | const CharSourceRange &R = getSpecifierRange(LM.getStart(), LM.getLength()); |
| 2541 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_nonsensical_length) |
| 2542 | << LM.toString() << CS.toString() |
| 2543 | << getSpecifierRange(startSpecifier, specifierLen), |
| 2544 | getLocationOfByte(LM.getStart()), |
| 2545 | /*IsStringLocation*/true, R, |
| 2546 | FixItHint::CreateRemoval(R)); |
Ted Kremenek | 1e51c20 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2549 | if (!FS.hasStandardLengthModifier()) |
| 2550 | HandleNonStandardLengthModifier(LM, startSpecifier, specifierLen); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2551 | if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2552 | HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); |
| 2553 | if (!FS.hasStandardLengthConversionCombination()) |
| 2554 | HandleNonStandardConversionSpecification(LM, CS, startSpecifier, |
| 2555 | specifierLen); |
| 2556 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2557 | // The remaining checks depend on the data arguments. |
| 2558 | if (HasVAListArg) |
| 2559 | return true; |
| 2560 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2561 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2562 | return false; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2563 | |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2564 | // Check that the argument type matches the format specifier. |
| 2565 | const Expr *Ex = getDataArg(argIndex); |
| 2566 | const analyze_scanf::ScanfArgTypeResult &ATR = FS.getArgType(S.Context); |
| 2567 | if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) { |
| 2568 | ScanfSpecifier fixedFS = FS; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2569 | bool success = fixedFS.fixType(Ex->getType(), S.getLangOpts(), |
Hans Wennborg | be6126a | 2012-02-15 09:59:46 +0000 | [diff] [blame] | 2570 | S.Context); |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2571 | |
| 2572 | if (success) { |
| 2573 | // Get the fix string from the fixed format specifier. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2574 | SmallString<128> buf; |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2575 | llvm::raw_svector_ostream os(buf); |
| 2576 | fixedFS.toString(os); |
| 2577 | |
| 2578 | EmitFormatDiagnostic( |
| 2579 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
| 2580 | << ATR.getRepresentativeTypeName(S.Context) << Ex->getType() |
| 2581 | << Ex->getSourceRange(), |
| 2582 | getLocationOfByte(CS.getStart()), |
| 2583 | /*IsStringLocation*/true, |
| 2584 | getSpecifierRange(startSpecifier, specifierLen), |
| 2585 | FixItHint::CreateReplacement( |
| 2586 | getSpecifierRange(startSpecifier, specifierLen), |
| 2587 | os.str())); |
| 2588 | } else { |
Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 2589 | EmitFormatDiagnostic( |
| 2590 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2591 | << ATR.getRepresentativeTypeName(S.Context) << Ex->getType() |
Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 2592 | << Ex->getSourceRange(), |
| 2593 | getLocationOfByte(CS.getStart()), |
| 2594 | /*IsStringLocation*/true, |
| 2595 | getSpecifierRange(startSpecifier, specifierLen)); |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2596 | } |
| 2597 | } |
| 2598 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2599 | return true; |
| 2600 | } |
| 2601 | |
| 2602 | void Sema::CheckFormatString(const StringLiteral *FExpr, |
Ted Kremenek | 0e5675d | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 2603 | const Expr *OrigFormatExpr, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2604 | Expr **Args, unsigned NumArgs, |
| 2605 | bool HasVAListArg, unsigned format_idx, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2606 | unsigned firstDataArg, FormatStringType Type, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2607 | bool inFunctionCall) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2608 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2609 | // CHECK: is the format string a wide literal? |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2610 | if (!FExpr->isAscii()) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2611 | CheckFormatHandler::EmitFormatDiagnostic( |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2612 | *this, inFunctionCall, Args[format_idx], |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2613 | PDiag(diag::warn_format_string_is_wide_literal), FExpr->getLocStart(), |
| 2614 | /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2615 | return; |
| 2616 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2617 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2618 | // Str - The format string. NOTE: this is NOT null-terminated! |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2619 | StringRef StrRef = FExpr->getString(); |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 2620 | const char *Str = StrRef.data(); |
| 2621 | unsigned StrLen = StrRef.size(); |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2622 | const unsigned numDataArgs = NumArgs - firstDataArg; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2623 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2624 | // CHECK: empty format string? |
Ted Kremenek | 4cd5791 | 2011-09-29 05:52:16 +0000 | [diff] [blame] | 2625 | if (StrLen == 0 && numDataArgs > 0) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2626 | CheckFormatHandler::EmitFormatDiagnostic( |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2627 | *this, inFunctionCall, Args[format_idx], |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2628 | PDiag(diag::warn_empty_format_string), FExpr->getLocStart(), |
| 2629 | /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2630 | return; |
| 2631 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2632 | |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2633 | if (Type == FST_Printf || Type == FST_NSString) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2634 | CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, |
Ted Kremenek | 4cd5791 | 2011-09-29 05:52:16 +0000 | [diff] [blame] | 2635 | numDataArgs, isa<ObjCStringLiteral>(OrigFormatExpr), |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2636 | Str, HasVAListArg, Args, NumArgs, format_idx, |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2637 | inFunctionCall); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2638 | |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 2639 | if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2640 | getLangOpts())) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2641 | H.DoneProcessing(); |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2642 | } else if (Type == FST_Scanf) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2643 | CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, |
Ted Kremenek | 4cd5791 | 2011-09-29 05:52:16 +0000 | [diff] [blame] | 2644 | numDataArgs, isa<ObjCStringLiteral>(OrigFormatExpr), |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2645 | Str, HasVAListArg, Args, NumArgs, format_idx, |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2646 | inFunctionCall); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2647 | |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 2648 | if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2649 | getLangOpts())) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2650 | H.DoneProcessing(); |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2651 | } // TODO: handle other formats |
Ted Kremenek | ce7024e | 2010-01-28 01:18:22 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2654 | //===--- CHECK: Standard memory functions ---------------------------------===// |
| 2655 | |
Douglas Gregor | 2a053a3 | 2011-05-03 20:05:22 +0000 | [diff] [blame] | 2656 | /// \brief Determine whether the given type is a dynamic class type (e.g., |
| 2657 | /// whether it has a vtable). |
| 2658 | static bool isDynamicClassType(QualType T) { |
| 2659 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
| 2660 | if (CXXRecordDecl *Definition = Record->getDefinition()) |
| 2661 | if (Definition->isDynamicClass()) |
| 2662 | return true; |
| 2663 | |
| 2664 | return false; |
| 2665 | } |
| 2666 | |
Chandler Carruth | a72a12f | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 2667 | /// \brief If E is a sizeof expression, returns its argument expression, |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2668 | /// otherwise returns NULL. |
| 2669 | static const Expr *getSizeOfExprArg(const Expr* E) { |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2670 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2671 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
| 2672 | if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType()) |
| 2673 | return SizeOf->getArgumentExpr()->IgnoreParenImpCasts(); |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2674 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2675 | return 0; |
| 2676 | } |
| 2677 | |
Chandler Carruth | a72a12f | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 2678 | /// \brief If E is a sizeof expression, returns its argument type. |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2679 | static QualType getSizeOfArgType(const Expr* E) { |
| 2680 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
| 2681 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
| 2682 | if (SizeOf->getKind() == clang::UETT_SizeOf) |
| 2683 | return SizeOf->getTypeOfArgument(); |
| 2684 | |
| 2685 | return QualType(); |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2688 | /// \brief Check for dangerous or invalid arguments to memset(). |
| 2689 | /// |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 2690 | /// This issues warnings on known problematic, dangerous or unspecified |
Matt Beaumont-Gay | cc2f30c | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 2691 | /// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp' |
| 2692 | /// function calls. |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2693 | /// |
| 2694 | /// \param Call The call expression to diagnose. |
Matt Beaumont-Gay | cc2f30c | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 2695 | void Sema::CheckMemaccessArguments(const CallExpr *Call, |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2696 | unsigned BId, |
Matt Beaumont-Gay | cc2f30c | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 2697 | IdentifierInfo *FnName) { |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2698 | assert(BId != 0); |
| 2699 | |
Ted Kremenek | 1d59f7f | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 2700 | // It is possible to have a non-standard definition of memset. Validate |
Douglas Gregor | 707a23e | 2011-06-16 17:56:04 +0000 | [diff] [blame] | 2701 | // we have enough arguments, and if not, abort further checking. |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2702 | unsigned ExpectedNumArgs = (BId == Builtin::BIstrndup ? 2 : 3); |
Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2703 | if (Call->getNumArgs() < ExpectedNumArgs) |
Ted Kremenek | 1d59f7f | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 2704 | return; |
| 2705 | |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2706 | unsigned LastArg = (BId == Builtin::BImemset || |
| 2707 | BId == Builtin::BIstrndup ? 1 : 2); |
| 2708 | unsigned LenArg = (BId == Builtin::BIstrndup ? 1 : 2); |
Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2709 | const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts(); |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2710 | |
| 2711 | // We have special checking when the length is a sizeof expression. |
| 2712 | QualType SizeOfArgTy = getSizeOfArgType(LenExpr); |
| 2713 | const Expr *SizeOfArg = getSizeOfExprArg(LenExpr); |
| 2714 | llvm::FoldingSetNodeID SizeOfArgID; |
| 2715 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2716 | for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) { |
| 2717 | const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts(); |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2718 | SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange(); |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2719 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2720 | QualType DestTy = Dest->getType(); |
| 2721 | if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) { |
| 2722 | QualType PointeeTy = DestPtrTy->getPointeeType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2723 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2724 | // Never warn about void type pointers. This can be used to suppress |
| 2725 | // false positives. |
| 2726 | if (PointeeTy->isVoidType()) |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2727 | continue; |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2728 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2729 | // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by |
| 2730 | // actually comparing the expressions for equality. Because computing the |
| 2731 | // expression IDs can be expensive, we only do this if the diagnostic is |
| 2732 | // enabled. |
| 2733 | if (SizeOfArg && |
| 2734 | Diags.getDiagnosticLevel(diag::warn_sizeof_pointer_expr_memaccess, |
| 2735 | SizeOfArg->getExprLoc())) { |
| 2736 | // We only compute IDs for expressions if the warning is enabled, and |
| 2737 | // cache the sizeof arg's ID. |
| 2738 | if (SizeOfArgID == llvm::FoldingSetNodeID()) |
| 2739 | SizeOfArg->Profile(SizeOfArgID, Context, true); |
| 2740 | llvm::FoldingSetNodeID DestID; |
| 2741 | Dest->Profile(DestID, Context, true); |
| 2742 | if (DestID == SizeOfArgID) { |
Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2743 | // TODO: For strncpy() and friends, this could suggest sizeof(dst) |
| 2744 | // over sizeof(src) as well. |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2745 | unsigned ActionIdx = 0; // Default is to suggest dereferencing. |
| 2746 | if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest)) |
| 2747 | if (UnaryOp->getOpcode() == UO_AddrOf) |
| 2748 | ActionIdx = 1; // If its an address-of operator, just remove it. |
| 2749 | if (Context.getTypeSize(PointeeTy) == Context.getCharWidth()) |
| 2750 | ActionIdx = 2; // If the pointee's size is sizeof(char), |
| 2751 | // suggest an explicit length. |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2752 | unsigned DestSrcSelect = |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2753 | (BId == Builtin::BIstrndup ? 1 : ArgIdx); |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2754 | DiagRuntimeBehavior(SizeOfArg->getExprLoc(), Dest, |
| 2755 | PDiag(diag::warn_sizeof_pointer_expr_memaccess) |
Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2756 | << FnName << DestSrcSelect << ActionIdx |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2757 | << Dest->getSourceRange() |
| 2758 | << SizeOfArg->getSourceRange()); |
| 2759 | break; |
| 2760 | } |
| 2761 | } |
| 2762 | |
| 2763 | // Also check for cases where the sizeof argument is the exact same |
| 2764 | // type as the memory argument, and where it points to a user-defined |
| 2765 | // record type. |
| 2766 | if (SizeOfArgTy != QualType()) { |
| 2767 | if (PointeeTy->isRecordType() && |
| 2768 | Context.typesAreCompatible(SizeOfArgTy, DestTy)) { |
| 2769 | DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest, |
| 2770 | PDiag(diag::warn_sizeof_pointer_type_memaccess) |
| 2771 | << FnName << SizeOfArgTy << ArgIdx |
| 2772 | << PointeeTy << Dest->getSourceRange() |
| 2773 | << LenExpr->getSourceRange()); |
| 2774 | break; |
| 2775 | } |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2778 | // Always complain about dynamic classes. |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2779 | if (isDynamicClassType(PointeeTy)) { |
| 2780 | |
| 2781 | unsigned OperationType = 0; |
| 2782 | // "overwritten" if we're warning about the destination for any call |
| 2783 | // but memcmp; otherwise a verb appropriate to the call. |
| 2784 | if (ArgIdx != 0 || BId == Builtin::BImemcmp) { |
| 2785 | if (BId == Builtin::BImemcpy) |
| 2786 | OperationType = 1; |
| 2787 | else if(BId == Builtin::BImemmove) |
| 2788 | OperationType = 2; |
| 2789 | else if (BId == Builtin::BImemcmp) |
| 2790 | OperationType = 3; |
| 2791 | } |
| 2792 | |
Matt Beaumont-Gay | 5c5218e | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 2793 | DiagRuntimeBehavior( |
| 2794 | Dest->getExprLoc(), Dest, |
| 2795 | PDiag(diag::warn_dyn_class_memaccess) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2796 | << (BId == Builtin::BImemcmp ? ArgIdx + 2 : ArgIdx) |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2797 | << FnName << PointeeTy |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2798 | << OperationType |
Matt Beaumont-Gay | 5c5218e | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 2799 | << Call->getCallee()->getSourceRange()); |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2800 | } else if (PointeeTy.hasNonTrivialObjCLifetime() && |
| 2801 | BId != Builtin::BImemset) |
Matt Beaumont-Gay | 5c5218e | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 2802 | DiagRuntimeBehavior( |
| 2803 | Dest->getExprLoc(), Dest, |
| 2804 | PDiag(diag::warn_arc_object_memaccess) |
| 2805 | << ArgIdx << FnName << PointeeTy |
| 2806 | << Call->getCallee()->getSourceRange()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2807 | else |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2808 | continue; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2809 | |
| 2810 | DiagRuntimeBehavior( |
| 2811 | Dest->getExprLoc(), Dest, |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 2812 | PDiag(diag::note_bad_memaccess_silence) |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2813 | << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)")); |
| 2814 | break; |
| 2815 | } |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2816 | } |
| 2817 | } |
| 2818 | |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 2819 | // A little helper routine: ignore addition and subtraction of integer literals. |
| 2820 | // This intentionally does not ignore all integer constant expressions because |
| 2821 | // we don't want to remove sizeof(). |
| 2822 | static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) { |
| 2823 | Ex = Ex->IgnoreParenCasts(); |
| 2824 | |
| 2825 | for (;;) { |
| 2826 | const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex); |
| 2827 | if (!BO || !BO->isAdditiveOp()) |
| 2828 | break; |
| 2829 | |
| 2830 | const Expr *RHS = BO->getRHS()->IgnoreParenCasts(); |
| 2831 | const Expr *LHS = BO->getLHS()->IgnoreParenCasts(); |
| 2832 | |
| 2833 | if (isa<IntegerLiteral>(RHS)) |
| 2834 | Ex = LHS; |
| 2835 | else if (isa<IntegerLiteral>(LHS)) |
| 2836 | Ex = RHS; |
| 2837 | else |
| 2838 | break; |
| 2839 | } |
| 2840 | |
| 2841 | return Ex; |
| 2842 | } |
| 2843 | |
| 2844 | // Warn if the user has made the 'size' argument to strlcpy or strlcat |
| 2845 | // be the size of the source, instead of the destination. |
| 2846 | void Sema::CheckStrlcpycatArguments(const CallExpr *Call, |
| 2847 | IdentifierInfo *FnName) { |
| 2848 | |
| 2849 | // Don't crash if the user has the wrong number of arguments |
| 2850 | if (Call->getNumArgs() != 3) |
| 2851 | return; |
| 2852 | |
| 2853 | const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context); |
| 2854 | const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context); |
| 2855 | const Expr *CompareWithSrc = NULL; |
| 2856 | |
| 2857 | // Look for 'strlcpy(dst, x, sizeof(x))' |
| 2858 | if (const Expr *Ex = getSizeOfExprArg(SizeArg)) |
| 2859 | CompareWithSrc = Ex; |
| 2860 | else { |
| 2861 | // Look for 'strlcpy(dst, x, strlen(x))' |
| 2862 | if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2863 | if (SizeCall->isBuiltinCall() == Builtin::BIstrlen |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 2864 | && SizeCall->getNumArgs() == 1) |
| 2865 | CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context); |
| 2866 | } |
| 2867 | } |
| 2868 | |
| 2869 | if (!CompareWithSrc) |
| 2870 | return; |
| 2871 | |
| 2872 | // Determine if the argument to sizeof/strlen is equal to the source |
| 2873 | // argument. In principle there's all kinds of things you could do |
| 2874 | // here, for instance creating an == expression and evaluating it with |
| 2875 | // EvaluateAsBooleanCondition, but this uses a more direct technique: |
| 2876 | const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg); |
| 2877 | if (!SrcArgDRE) |
| 2878 | return; |
| 2879 | |
| 2880 | const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc); |
| 2881 | if (!CompareWithSrcDRE || |
| 2882 | SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl()) |
| 2883 | return; |
| 2884 | |
| 2885 | const Expr *OriginalSizeArg = Call->getArg(2); |
| 2886 | Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size) |
| 2887 | << OriginalSizeArg->getSourceRange() << FnName; |
| 2888 | |
| 2889 | // Output a FIXIT hint if the destination is an array (rather than a |
| 2890 | // pointer to an array). This could be enhanced to handle some |
| 2891 | // pointers if we know the actual size, like if DstArg is 'array+2' |
| 2892 | // we could say 'sizeof(array)-2'. |
| 2893 | const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts(); |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 2894 | QualType DstArgTy = DstArg->getType(); |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 2895 | |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 2896 | // Only handle constant-sized or VLAs, but not flexible members. |
| 2897 | if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(DstArgTy)) { |
| 2898 | // Only issue the FIXIT for arrays of size > 1. |
| 2899 | if (CAT->getSize().getSExtValue() <= 1) |
| 2900 | return; |
| 2901 | } else if (!DstArgTy->isVariableArrayType()) { |
| 2902 | return; |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 2903 | } |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 2904 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2905 | SmallString<128> sizeString; |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 2906 | llvm::raw_svector_ostream OS(sizeString); |
| 2907 | OS << "sizeof("; |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2908 | DstArg->printPretty(OS, Context, 0, getPrintingPolicy()); |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 2909 | OS << ")"; |
| 2910 | |
| 2911 | Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size) |
| 2912 | << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(), |
| 2913 | OS.str()); |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 2914 | } |
| 2915 | |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 2916 | /// Check if two expressions refer to the same declaration. |
| 2917 | static bool referToTheSameDecl(const Expr *E1, const Expr *E2) { |
| 2918 | if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1)) |
| 2919 | if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2)) |
| 2920 | return D1->getDecl() == D2->getDecl(); |
| 2921 | return false; |
| 2922 | } |
| 2923 | |
| 2924 | static const Expr *getStrlenExprArg(const Expr *E) { |
| 2925 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { |
| 2926 | const FunctionDecl *FD = CE->getDirectCallee(); |
| 2927 | if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen) |
| 2928 | return 0; |
| 2929 | return CE->getArg(0)->IgnoreParenCasts(); |
| 2930 | } |
| 2931 | return 0; |
| 2932 | } |
| 2933 | |
| 2934 | // Warn on anti-patterns as the 'size' argument to strncat. |
| 2935 | // The correct size argument should look like following: |
| 2936 | // strncat(dst, src, sizeof(dst) - strlen(dest) - 1); |
| 2937 | void Sema::CheckStrncatArguments(const CallExpr *CE, |
| 2938 | IdentifierInfo *FnName) { |
| 2939 | // Don't crash if the user has the wrong number of arguments. |
| 2940 | if (CE->getNumArgs() < 3) |
| 2941 | return; |
| 2942 | const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts(); |
| 2943 | const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts(); |
| 2944 | const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts(); |
| 2945 | |
| 2946 | // Identify common expressions, which are wrongly used as the size argument |
| 2947 | // to strncat and may lead to buffer overflows. |
| 2948 | unsigned PatternType = 0; |
| 2949 | if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) { |
| 2950 | // - sizeof(dst) |
| 2951 | if (referToTheSameDecl(SizeOfArg, DstArg)) |
| 2952 | PatternType = 1; |
| 2953 | // - sizeof(src) |
| 2954 | else if (referToTheSameDecl(SizeOfArg, SrcArg)) |
| 2955 | PatternType = 2; |
| 2956 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) { |
| 2957 | if (BE->getOpcode() == BO_Sub) { |
| 2958 | const Expr *L = BE->getLHS()->IgnoreParenCasts(); |
| 2959 | const Expr *R = BE->getRHS()->IgnoreParenCasts(); |
| 2960 | // - sizeof(dst) - strlen(dst) |
| 2961 | if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) && |
| 2962 | referToTheSameDecl(DstArg, getStrlenExprArg(R))) |
| 2963 | PatternType = 1; |
| 2964 | // - sizeof(src) - (anything) |
| 2965 | else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L))) |
| 2966 | PatternType = 2; |
| 2967 | } |
| 2968 | } |
| 2969 | |
| 2970 | if (PatternType == 0) |
| 2971 | return; |
| 2972 | |
Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 2973 | // Generate the diagnostic. |
| 2974 | SourceLocation SL = LenArg->getLocStart(); |
| 2975 | SourceRange SR = LenArg->getSourceRange(); |
| 2976 | SourceManager &SM = PP.getSourceManager(); |
| 2977 | |
| 2978 | // If the function is defined as a builtin macro, do not show macro expansion. |
| 2979 | if (SM.isMacroArgExpansion(SL)) { |
| 2980 | SL = SM.getSpellingLoc(SL); |
| 2981 | SR = SourceRange(SM.getSpellingLoc(SR.getBegin()), |
| 2982 | SM.getSpellingLoc(SR.getEnd())); |
| 2983 | } |
| 2984 | |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 2985 | if (PatternType == 1) |
Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 2986 | Diag(SL, diag::warn_strncat_large_size) << SR; |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 2987 | else |
Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 2988 | Diag(SL, diag::warn_strncat_src_size) << SR; |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 2989 | |
| 2990 | // Output a FIXIT hint if the destination is an array (rather than a |
| 2991 | // pointer to an array). This could be enhanced to handle some |
| 2992 | // pointers if we know the actual size, like if DstArg is 'array+2' |
| 2993 | // we could say 'sizeof(array)-2'. |
| 2994 | QualType DstArgTy = DstArg->getType(); |
| 2995 | |
| 2996 | // Only handle constant-sized or VLAs, but not flexible members. |
| 2997 | if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(DstArgTy)) { |
| 2998 | // Only issue the FIXIT for arrays of size > 1. |
| 2999 | if (CAT->getSize().getSExtValue() <= 1) |
| 3000 | return; |
| 3001 | } else if (!DstArgTy->isVariableArrayType()) { |
| 3002 | return; |
| 3003 | } |
| 3004 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3005 | SmallString<128> sizeString; |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3006 | llvm::raw_svector_ostream OS(sizeString); |
| 3007 | OS << "sizeof("; |
| 3008 | DstArg->printPretty(OS, Context, 0, getPrintingPolicy()); |
| 3009 | OS << ") - "; |
| 3010 | OS << "strlen("; |
| 3011 | DstArg->printPretty(OS, Context, 0, getPrintingPolicy()); |
| 3012 | OS << ") - 1"; |
| 3013 | |
Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 3014 | Diag(SL, diag::note_strncat_wrong_size) |
| 3015 | << FixItHint::CreateReplacement(SR, OS.str()); |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3018 | //===--- CHECK: Return Address of Stack Variable --------------------------===// |
| 3019 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3020 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 3021 | Decl *ParentDecl); |
| 3022 | static Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 3023 | Decl *ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3024 | |
| 3025 | /// CheckReturnStackAddr - Check if a return statement returns the address |
| 3026 | /// of a stack variable. |
| 3027 | void |
| 3028 | Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, |
| 3029 | SourceLocation ReturnLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3030 | |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3031 | Expr *stackE = 0; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3032 | SmallVector<DeclRefExpr *, 8> refVars; |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3033 | |
| 3034 | // Perform checking for returned stack addresses, local blocks, |
| 3035 | // label addresses or references to temporaries. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3036 | if (lhsType->isPointerType() || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3037 | (!getLangOpts().ObjCAutoRefCount && lhsType->isBlockPointerType())) { |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3038 | stackE = EvalAddr(RetValExp, refVars, /*ParentDecl=*/0); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3039 | } else if (lhsType->isReferenceType()) { |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3040 | stackE = EvalVal(RetValExp, refVars, /*ParentDecl=*/0); |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3041 | } |
| 3042 | |
| 3043 | if (stackE == 0) |
| 3044 | return; // Nothing suspicious was found. |
| 3045 | |
| 3046 | SourceLocation diagLoc; |
| 3047 | SourceRange diagRange; |
| 3048 | if (refVars.empty()) { |
| 3049 | diagLoc = stackE->getLocStart(); |
| 3050 | diagRange = stackE->getSourceRange(); |
| 3051 | } else { |
| 3052 | // We followed through a reference variable. 'stackE' contains the |
| 3053 | // problematic expression but we will warn at the return statement pointing |
| 3054 | // at the reference variable. We will later display the "trail" of |
| 3055 | // reference variables using notes. |
| 3056 | diagLoc = refVars[0]->getLocStart(); |
| 3057 | diagRange = refVars[0]->getSourceRange(); |
| 3058 | } |
| 3059 | |
| 3060 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var. |
| 3061 | Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref |
| 3062 | : diag::warn_ret_stack_addr) |
| 3063 | << DR->getDecl()->getDeclName() << diagRange; |
| 3064 | } else if (isa<BlockExpr>(stackE)) { // local block. |
| 3065 | Diag(diagLoc, diag::err_ret_local_block) << diagRange; |
| 3066 | } else if (isa<AddrLabelExpr>(stackE)) { // address of label. |
| 3067 | Diag(diagLoc, diag::warn_ret_addr_label) << diagRange; |
| 3068 | } else { // local temporary. |
| 3069 | Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref |
| 3070 | : diag::warn_ret_local_temp_addr) |
| 3071 | << diagRange; |
| 3072 | } |
| 3073 | |
| 3074 | // Display the "trail" of reference variables that we followed until we |
| 3075 | // found the problematic expression using notes. |
| 3076 | for (unsigned i = 0, e = refVars.size(); i != e; ++i) { |
| 3077 | VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl()); |
| 3078 | // If this var binds to another reference var, show the range of the next |
| 3079 | // var, otherwise the var binds to the problematic expression, in which case |
| 3080 | // show the range of the expression. |
| 3081 | SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange() |
| 3082 | : stackE->getSourceRange(); |
| 3083 | Diag(VD->getLocation(), diag::note_ref_var_local_bind) |
| 3084 | << VD->getDeclName() << range; |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3085 | } |
| 3086 | } |
| 3087 | |
| 3088 | /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that |
| 3089 | /// check if the expression in a return statement evaluates to an address |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3090 | /// to a location on the stack, a local block, an address of a label, or a |
| 3091 | /// reference to local temporary. The recursion is used to traverse the |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3092 | /// AST of the return expression, with recursion backtracking when we |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3093 | /// encounter a subexpression that (1) clearly does not lead to one of the |
| 3094 | /// above problematic expressions (2) is something we cannot determine leads to |
| 3095 | /// a problematic expression based on such local checking. |
| 3096 | /// |
| 3097 | /// Both EvalAddr and EvalVal follow through reference variables to evaluate |
| 3098 | /// the expression that they point to. Such variables are added to the |
| 3099 | /// 'refVars' vector so that we know what the reference variable "trail" was. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3100 | /// |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 3101 | /// EvalAddr processes expressions that are pointers that are used as |
| 3102 | /// references (and not L-values). EvalVal handles all other values. |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3103 | /// At the base case of the recursion is a check for the above problematic |
| 3104 | /// expressions. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3105 | /// |
| 3106 | /// This implementation handles: |
| 3107 | /// |
| 3108 | /// * pointer-to-pointer casts |
| 3109 | /// * implicit conversions from array references to pointers |
| 3110 | /// * taking the address of fields |
| 3111 | /// * arbitrary interplay between "&" and "*" operators |
| 3112 | /// * pointer arithmetic from an address of a stack variable |
| 3113 | /// * 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^] | 3114 | static Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 3115 | Decl *ParentDecl) { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3116 | if (E->isTypeDependent()) |
| 3117 | return NULL; |
| 3118 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3119 | // We should only be called for evaluating pointer expressions. |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3120 | assert((E->getType()->isAnyPointerType() || |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3121 | E->getType()->isBlockPointerType() || |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3122 | E->getType()->isObjCQualifiedIdType()) && |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3123 | "EvalAddr only works on pointers"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3124 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3125 | E = E->IgnoreParens(); |
| 3126 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3127 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 3128 | // viewed AST node. We then recursively traverse the AST by calling |
| 3129 | // EvalAddr and EvalVal appropriately. |
| 3130 | switch (E->getStmtClass()) { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3131 | case Stmt::DeclRefExprClass: { |
| 3132 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
| 3133 | |
| 3134 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
| 3135 | // If this is a reference variable, follow through to the expression that |
| 3136 | // it points to. |
| 3137 | if (V->hasLocalStorage() && |
| 3138 | V->getType()->isReferenceType() && V->hasInit()) { |
| 3139 | // Add the reference variable to the "trail". |
| 3140 | refVars.push_back(DR); |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3141 | return EvalAddr(V->getInit(), refVars, ParentDecl); |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3142 | } |
| 3143 | |
| 3144 | return NULL; |
| 3145 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3146 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3147 | case Stmt::UnaryOperatorClass: { |
| 3148 | // The only unary operator that make sense to handle here |
| 3149 | // is AddrOf. All others don't make sense as pointers. |
| 3150 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3151 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3152 | if (U->getOpcode() == UO_AddrOf) |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3153 | return EvalVal(U->getSubExpr(), refVars, ParentDecl); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3154 | else |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3155 | return NULL; |
| 3156 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3157 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3158 | case Stmt::BinaryOperatorClass: { |
| 3159 | // Handle pointer arithmetic. All other binary operators are not valid |
| 3160 | // in this context. |
| 3161 | BinaryOperator *B = cast<BinaryOperator>(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3162 | BinaryOperatorKind op = B->getOpcode(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3163 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3164 | if (op != BO_Add && op != BO_Sub) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3165 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3166 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3167 | Expr *Base = B->getLHS(); |
| 3168 | |
| 3169 | // Determine which argument is the real pointer base. It could be |
| 3170 | // the RHS argument instead of the LHS. |
| 3171 | if (!Base->getType()->isPointerType()) Base = B->getRHS(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3172 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3173 | assert (Base->getType()->isPointerType()); |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3174 | return EvalAddr(Base, refVars, ParentDecl); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3175 | } |
Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 3176 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3177 | // For conditional operators we need to see if either the LHS or RHS are |
| 3178 | // valid DeclRefExpr*s. If one of them is valid, we return it. |
| 3179 | case Stmt::ConditionalOperatorClass: { |
| 3180 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3181 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3182 | // Handle the GNU extension for missing LHS. |
Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 3183 | if (Expr *lhsExpr = C->getLHS()) { |
| 3184 | // In C++, we can have a throw-expression, which has 'void' type. |
| 3185 | if (!lhsExpr->getType()->isVoidType()) |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3186 | if (Expr* LHS = EvalAddr(lhsExpr, refVars, ParentDecl)) |
Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 3187 | return LHS; |
| 3188 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3189 | |
Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 3190 | // In C++, we can have a throw-expression, which has 'void' type. |
| 3191 | if (C->getRHS()->getType()->isVoidType()) |
| 3192 | return NULL; |
| 3193 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3194 | return EvalAddr(C->getRHS(), refVars, ParentDecl); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3195 | } |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3196 | |
| 3197 | case Stmt::BlockExprClass: |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 3198 | if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures()) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3199 | return E; // local block. |
| 3200 | return NULL; |
| 3201 | |
| 3202 | case Stmt::AddrLabelExprClass: |
| 3203 | return E; // address of label. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3204 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3205 | case Stmt::ExprWithCleanupsClass: |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3206 | return EvalAddr(cast<ExprWithCleanups>(E)->getSubExpr(), refVars, |
| 3207 | ParentDecl); |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3208 | |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 3209 | // For casts, we need to handle conversions from arrays to |
| 3210 | // pointer values, and pointer-to-pointer conversions. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 3211 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 3212 | case Stmt::CStyleCastExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3213 | case Stmt::CXXFunctionalCastExprClass: |
Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3214 | case Stmt::ObjCBridgedCastExprClass: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3215 | case Stmt::CXXStaticCastExprClass: |
| 3216 | case Stmt::CXXDynamicCastExprClass: |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 3217 | case Stmt::CXXConstCastExprClass: |
| 3218 | case Stmt::CXXReinterpretCastExprClass: { |
Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3219 | Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
| 3220 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 3221 | case CK_BitCast: |
| 3222 | case CK_LValueToRValue: |
| 3223 | case CK_NoOp: |
| 3224 | case CK_BaseToDerived: |
| 3225 | case CK_DerivedToBase: |
| 3226 | case CK_UncheckedDerivedToBase: |
| 3227 | case CK_Dynamic: |
| 3228 | case CK_CPointerToObjCPointerCast: |
| 3229 | case CK_BlockPointerToObjCPointerCast: |
| 3230 | case CK_AnyPointerToBlockPointerCast: |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3231 | return EvalAddr(SubExpr, refVars, ParentDecl); |
Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3232 | |
| 3233 | case CK_ArrayToPointerDecay: |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3234 | return EvalVal(SubExpr, refVars, ParentDecl); |
Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3235 | |
| 3236 | default: |
| 3237 | return 0; |
| 3238 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3239 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3240 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3241 | case Stmt::MaterializeTemporaryExprClass: |
| 3242 | if (Expr *Result = EvalAddr( |
| 3243 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3244 | refVars, ParentDecl)) |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3245 | return Result; |
| 3246 | |
| 3247 | return E; |
| 3248 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3249 | // Everything else: we simply don't reason about them. |
| 3250 | default: |
| 3251 | return NULL; |
| 3252 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3253 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3254 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3255 | |
| 3256 | /// EvalVal - This function is complements EvalAddr in the mutual recursion. |
| 3257 | /// See the comments for EvalAddr for more details. |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3258 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 3259 | Decl *ParentDecl) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3260 | do { |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 3261 | // We should only be called for evaluating non-pointer expressions, or |
| 3262 | // expressions with a pointer type that are not used as references but instead |
| 3263 | // are l-values (e.g., DeclRefExpr with a pointer type). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3264 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3265 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 3266 | // viewed AST node. We then recursively traverse the AST by calling |
| 3267 | // EvalAddr and EvalVal appropriately. |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3268 | |
| 3269 | E = E->IgnoreParens(); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3270 | switch (E->getStmtClass()) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3271 | case Stmt::ImplicitCastExprClass: { |
| 3272 | ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E); |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3273 | if (IE->getValueKind() == VK_LValue) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3274 | E = IE->getSubExpr(); |
| 3275 | continue; |
| 3276 | } |
| 3277 | return NULL; |
| 3278 | } |
| 3279 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3280 | case Stmt::ExprWithCleanupsClass: |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3281 | return EvalVal(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,ParentDecl); |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3282 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3283 | case Stmt::DeclRefExprClass: { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3284 | // When we hit a DeclRefExpr we are looking at code that refers to a |
| 3285 | // variable's name. If it's not a reference variable we check if it has |
| 3286 | // local storage within the function, and if so, return the expression. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3287 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3288 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3289 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) { |
| 3290 | // Check if it refers to itself, e.g. "int& i = i;". |
| 3291 | if (V == ParentDecl) |
| 3292 | return DR; |
| 3293 | |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3294 | if (V->hasLocalStorage()) { |
| 3295 | if (!V->getType()->isReferenceType()) |
| 3296 | return DR; |
| 3297 | |
| 3298 | // Reference variable, follow through to the expression that |
| 3299 | // it points to. |
| 3300 | if (V->hasInit()) { |
| 3301 | // Add the reference variable to the "trail". |
| 3302 | refVars.push_back(DR); |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3303 | return EvalVal(V->getInit(), refVars, V); |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3304 | } |
| 3305 | } |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3306 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3307 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3308 | return NULL; |
| 3309 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3310 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3311 | case Stmt::UnaryOperatorClass: { |
| 3312 | // The only unary operator that make sense to handle here |
| 3313 | // is Deref. All others don't resolve to a "name." This includes |
| 3314 | // handling all sorts of rvalues passed to a unary operator. |
| 3315 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3316 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3317 | if (U->getOpcode() == UO_Deref) |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3318 | return EvalAddr(U->getSubExpr(), refVars, ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3319 | |
| 3320 | return NULL; |
| 3321 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3322 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3323 | case Stmt::ArraySubscriptExprClass: { |
| 3324 | // Array subscripts are potential references to data on the stack. We |
| 3325 | // retrieve the DeclRefExpr* for the array variable if it indeed |
| 3326 | // has local storage. |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3327 | return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars,ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3328 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3329 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3330 | case Stmt::ConditionalOperatorClass: { |
| 3331 | // 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] | 3332 | // non-NULL Expr's. If one is non-NULL, we return it. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3333 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 3334 | |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 3335 | // Handle the GNU extension for missing LHS. |
| 3336 | if (Expr *lhsExpr = C->getLHS()) |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3337 | if (Expr *LHS = EvalVal(lhsExpr, refVars, ParentDecl)) |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 3338 | return LHS; |
| 3339 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3340 | return EvalVal(C->getRHS(), refVars, ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3341 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3342 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3343 | // Accesses to members are potential references to data on the stack. |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 3344 | case Stmt::MemberExprClass: { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3345 | MemberExpr *M = cast<MemberExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3346 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3347 | // Check for indirect access. We only want direct field accesses. |
Ted Kremenek | a423e81 | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 3348 | if (M->isArrow()) |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3349 | return NULL; |
Ted Kremenek | a423e81 | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 3350 | |
| 3351 | // Check whether the member type is itself a reference, in which case |
| 3352 | // we're not going to refer to the member, but to what the member refers to. |
| 3353 | if (M->getMemberDecl()->getType()->isReferenceType()) |
| 3354 | return NULL; |
| 3355 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3356 | return EvalVal(M->getBase(), refVars, ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3357 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3358 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3359 | case Stmt::MaterializeTemporaryExprClass: |
| 3360 | if (Expr *Result = EvalVal( |
| 3361 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame^] | 3362 | refVars, ParentDecl)) |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3363 | return Result; |
| 3364 | |
| 3365 | return E; |
| 3366 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3367 | default: |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3368 | // Check that we don't return or take the address of a reference to a |
| 3369 | // temporary. This is only useful in C++. |
| 3370 | if (!E->isTypeDependent() && E->isRValue()) |
| 3371 | return E; |
| 3372 | |
| 3373 | // Everything else: we simply don't reason about them. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3374 | return NULL; |
| 3375 | } |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3376 | } while (true); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3377 | } |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3378 | |
| 3379 | //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// |
| 3380 | |
| 3381 | /// Check for comparisons of floating point operands using != and ==. |
| 3382 | /// Issue a warning if these are no self-comparisons, as they are not likely |
| 3383 | /// to do what the programmer intended. |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3384 | void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) { |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3385 | bool EmitWarning = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3386 | |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3387 | Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts(); |
| 3388 | Expr* RightExprSansParen = RHS->IgnoreParenImpCasts(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3389 | |
| 3390 | // Special case: check for x == x (which is OK). |
| 3391 | // Do not emit warnings for such cases. |
| 3392 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) |
| 3393 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) |
| 3394 | if (DRL->getDecl() == DRR->getDecl()) |
| 3395 | EmitWarning = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3396 | |
| 3397 | |
Ted Kremenek | 1b500bb | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 3398 | // Special case: check for comparisons against literals that can be exactly |
| 3399 | // represented by APFloat. In such cases, do not emit a warning. This |
| 3400 | // is a heuristic: often comparison against such literals are used to |
| 3401 | // detect if a value in a variable has not changed. This clearly can |
| 3402 | // lead to false negatives. |
| 3403 | if (EmitWarning) { |
| 3404 | if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { |
| 3405 | if (FLL->isExact()) |
| 3406 | EmitWarning = false; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3407 | } else |
Ted Kremenek | 1b500bb | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 3408 | if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)){ |
| 3409 | if (FLR->isExact()) |
| 3410 | EmitWarning = false; |
| 3411 | } |
| 3412 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3413 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3414 | // Check for comparisons with builtin types. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3415 | if (EmitWarning) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3416 | if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3417 | if (CL->isBuiltinCall()) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3418 | EmitWarning = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3419 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3420 | if (EmitWarning) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3421 | if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3422 | if (CR->isBuiltinCall()) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3423 | EmitWarning = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3424 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3425 | // Emit the diagnostic. |
| 3426 | if (EmitWarning) |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3427 | Diag(Loc, diag::warn_floatingpoint_eq) |
| 3428 | << LHS->getSourceRange() << RHS->getSourceRange(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3429 | } |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3430 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3431 | //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// |
| 3432 | //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3433 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3434 | namespace { |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3435 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3436 | /// Structure recording the 'active' range of an integer-valued |
| 3437 | /// expression. |
| 3438 | struct IntRange { |
| 3439 | /// The number of bits active in the int. |
| 3440 | unsigned Width; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3441 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3442 | /// True if the int is known not to have negative values. |
| 3443 | bool NonNegative; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3444 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3445 | IntRange(unsigned Width, bool NonNegative) |
| 3446 | : Width(Width), NonNegative(NonNegative) |
| 3447 | {} |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3448 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3449 | /// Returns the range of the bool type. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3450 | static IntRange forBoolType() { |
| 3451 | return IntRange(1, true); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3452 | } |
| 3453 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3454 | /// Returns the range of an opaque value of the given integral type. |
| 3455 | static IntRange forValueOfType(ASTContext &C, QualType T) { |
| 3456 | return forValueOfCanonicalType(C, |
| 3457 | T->getCanonicalTypeInternal().getTypePtr()); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3458 | } |
| 3459 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3460 | /// Returns the range of an opaque value of a canonical integral type. |
| 3461 | static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3462 | assert(T->isCanonicalUnqualified()); |
| 3463 | |
| 3464 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 3465 | T = VT->getElementType().getTypePtr(); |
| 3466 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 3467 | T = CT->getElementType().getTypePtr(); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3468 | |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3469 | // For enum types, use the known bit width of the enumerators. |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3470 | if (const EnumType *ET = dyn_cast<EnumType>(T)) { |
| 3471 | EnumDecl *Enum = ET->getDecl(); |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3472 | if (!Enum->isCompleteDefinition()) |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3473 | return IntRange(C.getIntWidth(QualType(T, 0)), false); |
| 3474 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3475 | unsigned NumPositive = Enum->getNumPositiveBits(); |
| 3476 | unsigned NumNegative = Enum->getNumNegativeBits(); |
| 3477 | |
| 3478 | return IntRange(std::max(NumPositive, NumNegative), NumNegative == 0); |
| 3479 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3480 | |
| 3481 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 3482 | assert(BT->isInteger()); |
| 3483 | |
| 3484 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 3485 | } |
| 3486 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3487 | /// Returns the "target" range of a canonical integral type, i.e. |
| 3488 | /// the range of values expressible in the type. |
| 3489 | /// |
| 3490 | /// This matches forValueOfCanonicalType except that enums have the |
| 3491 | /// full range of their type, not the range of their enumerators. |
| 3492 | static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) { |
| 3493 | assert(T->isCanonicalUnqualified()); |
| 3494 | |
| 3495 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 3496 | T = VT->getElementType().getTypePtr(); |
| 3497 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 3498 | T = CT->getElementType().getTypePtr(); |
| 3499 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Douglas Gregor | 69ff26b | 2011-09-08 23:29:05 +0000 | [diff] [blame] | 3500 | T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr(); |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3501 | |
| 3502 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 3503 | assert(BT->isInteger()); |
| 3504 | |
| 3505 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 3506 | } |
| 3507 | |
| 3508 | /// Returns the supremum of two ranges: i.e. their conservative merge. |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 3509 | static IntRange join(IntRange L, IntRange R) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3510 | return IntRange(std::max(L.Width, R.Width), |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3511 | L.NonNegative && R.NonNegative); |
| 3512 | } |
| 3513 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3514 | /// Returns the infinum of two ranges: i.e. their aggressive merge. |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 3515 | static IntRange meet(IntRange L, IntRange R) { |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3516 | return IntRange(std::min(L.Width, R.Width), |
| 3517 | L.NonNegative || R.NonNegative); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3518 | } |
| 3519 | }; |
| 3520 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3521 | static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, |
| 3522 | unsigned MaxWidth) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3523 | if (value.isSigned() && value.isNegative()) |
| 3524 | return IntRange(value.getMinSignedBits(), false); |
| 3525 | |
| 3526 | if (value.getBitWidth() > MaxWidth) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3527 | value = value.trunc(MaxWidth); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3528 | |
| 3529 | // isNonNegative() just checks the sign bit without considering |
| 3530 | // signedness. |
| 3531 | return IntRange(value.getActiveBits(), true); |
| 3532 | } |
| 3533 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3534 | static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, |
| 3535 | unsigned MaxWidth) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3536 | if (result.isInt()) |
| 3537 | return GetValueRange(C, result.getInt(), MaxWidth); |
| 3538 | |
| 3539 | if (result.isVector()) { |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 3540 | IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); |
| 3541 | for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { |
| 3542 | IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); |
| 3543 | R = IntRange::join(R, El); |
| 3544 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3545 | return R; |
| 3546 | } |
| 3547 | |
| 3548 | if (result.isComplexInt()) { |
| 3549 | IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); |
| 3550 | IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); |
| 3551 | return IntRange::join(R, I); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | // This can happen with lossless casts to intptr_t of "based" lvalues. |
| 3555 | // Assume it might use arbitrary bits. |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 3556 | // FIXME: The only reason we need to pass the type in here is to get |
| 3557 | // the sign right on this one case. It would be nice if APValue |
| 3558 | // preserved this. |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 3559 | assert(result.isLValue() || result.isAddrLabelDiff()); |
Douglas Gregor | 5e9ebb3 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 3560 | return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType()); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3561 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3562 | |
| 3563 | /// Pseudo-evaluate the given integer expression, estimating the |
| 3564 | /// range of values it might take. |
| 3565 | /// |
| 3566 | /// \param MaxWidth - the width to which the value will be truncated |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3567 | static IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3568 | E = E->IgnoreParens(); |
| 3569 | |
| 3570 | // Try a full evaluation first. |
| 3571 | Expr::EvalResult result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3572 | if (E->EvaluateAsRValue(result, C)) |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 3573 | return GetValueRange(C, result.Val, E->getType(), MaxWidth); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3574 | |
| 3575 | // I think we only want to look through implicit casts here; if the |
| 3576 | // user has an explicit widening cast, we should treat the value as |
| 3577 | // being of the new, wider type. |
| 3578 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) { |
Eli Friedman | b17ee5b | 2011-12-15 02:41:52 +0000 | [diff] [blame] | 3579 | if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue) |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3580 | return GetExprRange(C, CE->getSubExpr(), MaxWidth); |
| 3581 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3582 | IntRange OutputTypeRange = IntRange::forValueOfType(C, CE->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3583 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3584 | bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast); |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3585 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3586 | // 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] | 3587 | if (!isIntegerCast) |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3588 | return OutputTypeRange; |
| 3589 | |
| 3590 | IntRange SubRange |
| 3591 | = GetExprRange(C, CE->getSubExpr(), |
| 3592 | std::min(MaxWidth, OutputTypeRange.Width)); |
| 3593 | |
| 3594 | // Bail out if the subexpr's range is as wide as the cast type. |
| 3595 | if (SubRange.Width >= OutputTypeRange.Width) |
| 3596 | return OutputTypeRange; |
| 3597 | |
| 3598 | // Otherwise, we take the smaller width, and we're non-negative if |
| 3599 | // either the output type or the subexpr is. |
| 3600 | return IntRange(SubRange.Width, |
| 3601 | SubRange.NonNegative || OutputTypeRange.NonNegative); |
| 3602 | } |
| 3603 | |
| 3604 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 3605 | // If we can fold the condition, just take that operand. |
| 3606 | bool CondResult; |
| 3607 | if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) |
| 3608 | return GetExprRange(C, CondResult ? CO->getTrueExpr() |
| 3609 | : CO->getFalseExpr(), |
| 3610 | MaxWidth); |
| 3611 | |
| 3612 | // Otherwise, conservatively merge. |
| 3613 | IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth); |
| 3614 | IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth); |
| 3615 | return IntRange::join(L, R); |
| 3616 | } |
| 3617 | |
| 3618 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 3619 | switch (BO->getOpcode()) { |
| 3620 | |
| 3621 | // Boolean-valued operations are single-bit and positive. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3622 | case BO_LAnd: |
| 3623 | case BO_LOr: |
| 3624 | case BO_LT: |
| 3625 | case BO_GT: |
| 3626 | case BO_LE: |
| 3627 | case BO_GE: |
| 3628 | case BO_EQ: |
| 3629 | case BO_NE: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3630 | return IntRange::forBoolType(); |
| 3631 | |
John McCall | 862ff87 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 3632 | // The type of the assignments is the type of the LHS, so the RHS |
| 3633 | // is not necessarily the same type. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3634 | case BO_MulAssign: |
| 3635 | case BO_DivAssign: |
| 3636 | case BO_RemAssign: |
| 3637 | case BO_AddAssign: |
| 3638 | case BO_SubAssign: |
John McCall | 862ff87 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 3639 | case BO_XorAssign: |
| 3640 | case BO_OrAssign: |
| 3641 | // TODO: bitfields? |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3642 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 3643 | |
John McCall | 862ff87 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 3644 | // Simple assignments just pass through the RHS, which will have |
| 3645 | // been coerced to the LHS type. |
| 3646 | case BO_Assign: |
| 3647 | // TODO: bitfields? |
| 3648 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 3649 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3650 | // Operations with opaque sources are black-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3651 | case BO_PtrMemD: |
| 3652 | case BO_PtrMemI: |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3653 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3654 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3655 | // Bitwise-and uses the *infinum* of the two source ranges. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3656 | case BO_And: |
| 3657 | case BO_AndAssign: |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3658 | return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth), |
| 3659 | GetExprRange(C, BO->getRHS(), MaxWidth)); |
| 3660 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3661 | // Left shift gets black-listed based on a judgement call. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3662 | case BO_Shl: |
John McCall | 3aae609 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 3663 | // ...except that we want to treat '1 << (blah)' as logically |
| 3664 | // positive. It's an important idiom. |
| 3665 | if (IntegerLiteral *I |
| 3666 | = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { |
| 3667 | if (I->getValue() == 1) { |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3668 | IntRange R = IntRange::forValueOfType(C, E->getType()); |
John McCall | 3aae609 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 3669 | return IntRange(R.Width, /*NonNegative*/ true); |
| 3670 | } |
| 3671 | } |
| 3672 | // fallthrough |
| 3673 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3674 | case BO_ShlAssign: |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3675 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3676 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3677 | // Right shift by a constant can narrow its left argument. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3678 | case BO_Shr: |
| 3679 | case BO_ShrAssign: { |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3680 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 3681 | |
| 3682 | // If the shift amount is a positive constant, drop the width by |
| 3683 | // that much. |
| 3684 | llvm::APSInt shift; |
| 3685 | if (BO->getRHS()->isIntegerConstantExpr(shift, C) && |
| 3686 | shift.isNonNegative()) { |
| 3687 | unsigned zext = shift.getZExtValue(); |
| 3688 | if (zext >= L.Width) |
| 3689 | L.Width = (L.NonNegative ? 0 : 1); |
| 3690 | else |
| 3691 | L.Width -= zext; |
| 3692 | } |
| 3693 | |
| 3694 | return L; |
| 3695 | } |
| 3696 | |
| 3697 | // Comma acts as its right operand. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3698 | case BO_Comma: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3699 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 3700 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3701 | // Black-list pointer subtractions. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3702 | case BO_Sub: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3703 | if (BO->getLHS()->getType()->isPointerType()) |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3704 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | 00fe761 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 3705 | break; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3706 | |
John McCall | 00fe761 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 3707 | // The width of a division result is mostly determined by the size |
| 3708 | // of the LHS. |
| 3709 | case BO_Div: { |
| 3710 | // Don't 'pre-truncate' the operands. |
| 3711 | unsigned opWidth = C.getIntWidth(E->getType()); |
| 3712 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); |
| 3713 | |
| 3714 | // If the divisor is constant, use that. |
| 3715 | llvm::APSInt divisor; |
| 3716 | if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) { |
| 3717 | unsigned log2 = divisor.logBase2(); // floor(log_2(divisor)) |
| 3718 | if (log2 >= L.Width) |
| 3719 | L.Width = (L.NonNegative ? 0 : 1); |
| 3720 | else |
| 3721 | L.Width = std::min(L.Width - log2, MaxWidth); |
| 3722 | return L; |
| 3723 | } |
| 3724 | |
| 3725 | // Otherwise, just use the LHS's width. |
| 3726 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); |
| 3727 | return IntRange(L.Width, L.NonNegative && R.NonNegative); |
| 3728 | } |
| 3729 | |
| 3730 | // The result of a remainder can't be larger than the result of |
| 3731 | // either side. |
| 3732 | case BO_Rem: { |
| 3733 | // Don't 'pre-truncate' the operands. |
| 3734 | unsigned opWidth = C.getIntWidth(E->getType()); |
| 3735 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); |
| 3736 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); |
| 3737 | |
| 3738 | IntRange meet = IntRange::meet(L, R); |
| 3739 | meet.Width = std::min(meet.Width, MaxWidth); |
| 3740 | return meet; |
| 3741 | } |
| 3742 | |
| 3743 | // The default behavior is okay for these. |
| 3744 | case BO_Mul: |
| 3745 | case BO_Add: |
| 3746 | case BO_Xor: |
| 3747 | case BO_Or: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3748 | break; |
| 3749 | } |
| 3750 | |
John McCall | 00fe761 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 3751 | // The default case is to treat the operation as if it were closed |
| 3752 | // on the narrowest type that encompasses both operands. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3753 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 3754 | IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth); |
| 3755 | return IntRange::join(L, R); |
| 3756 | } |
| 3757 | |
| 3758 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 3759 | switch (UO->getOpcode()) { |
| 3760 | // Boolean-valued operations are white-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3761 | case UO_LNot: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3762 | return IntRange::forBoolType(); |
| 3763 | |
| 3764 | // Operations with opaque sources are black-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3765 | case UO_Deref: |
| 3766 | case UO_AddrOf: // should be impossible |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3767 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3768 | |
| 3769 | default: |
| 3770 | return GetExprRange(C, UO->getSubExpr(), MaxWidth); |
| 3771 | } |
| 3772 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3773 | |
| 3774 | if (dyn_cast<OffsetOfExpr>(E)) { |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3775 | IntRange::forValueOfType(C, E->getType()); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3776 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3777 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3778 | if (FieldDecl *BitField = E->getBitField()) |
| 3779 | return IntRange(BitField->getBitWidthValue(C), |
Douglas Gregor | 5e9ebb3 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 3780 | BitField->getType()->isUnsignedIntegerOrEnumerationType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3781 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3782 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3783 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3784 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3785 | static IntRange GetExprRange(ASTContext &C, Expr *E) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3786 | return GetExprRange(C, E, C.getIntWidth(E->getType())); |
| 3787 | } |
| 3788 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3789 | /// Checks whether the given value, which currently has the given |
| 3790 | /// source semantics, has the same value when coerced through the |
| 3791 | /// target semantics. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3792 | static bool IsSameFloatAfterCast(const llvm::APFloat &value, |
| 3793 | const llvm::fltSemantics &Src, |
| 3794 | const llvm::fltSemantics &Tgt) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3795 | llvm::APFloat truncated = value; |
| 3796 | |
| 3797 | bool ignored; |
| 3798 | truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 3799 | truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 3800 | |
| 3801 | return truncated.bitwiseIsEqual(value); |
| 3802 | } |
| 3803 | |
| 3804 | /// Checks whether the given value, which currently has the given |
| 3805 | /// source semantics, has the same value when coerced through the |
| 3806 | /// target semantics. |
| 3807 | /// |
| 3808 | /// The value might be a vector of floats (or a complex number). |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3809 | static bool IsSameFloatAfterCast(const APValue &value, |
| 3810 | const llvm::fltSemantics &Src, |
| 3811 | const llvm::fltSemantics &Tgt) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3812 | if (value.isFloat()) |
| 3813 | return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); |
| 3814 | |
| 3815 | if (value.isVector()) { |
| 3816 | for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) |
| 3817 | if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) |
| 3818 | return false; |
| 3819 | return true; |
| 3820 | } |
| 3821 | |
| 3822 | assert(value.isComplexFloat()); |
| 3823 | return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && |
| 3824 | IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); |
| 3825 | } |
| 3826 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3827 | static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3828 | |
Ted Kremenek | e3b159c | 2010-09-23 21:43:44 +0000 | [diff] [blame] | 3829 | static bool IsZero(Sema &S, Expr *E) { |
| 3830 | // Suppress cases where we are comparing against an enum constant. |
| 3831 | if (const DeclRefExpr *DR = |
| 3832 | dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) |
| 3833 | if (isa<EnumConstantDecl>(DR->getDecl())) |
| 3834 | return false; |
| 3835 | |
| 3836 | // Suppress cases where the '0' value is expanded from a macro. |
| 3837 | if (E->getLocStart().isMacroID()) |
| 3838 | return false; |
| 3839 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3840 | llvm::APSInt Value; |
| 3841 | return E->isIntegerConstantExpr(Value, S.Context) && Value == 0; |
| 3842 | } |
| 3843 | |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3844 | static bool HasEnumType(Expr *E) { |
| 3845 | // Strip off implicit integral promotions. |
| 3846 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
Argyrios Kyrtzidis | 63b57ae | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 3847 | if (ICE->getCastKind() != CK_IntegralCast && |
| 3848 | ICE->getCastKind() != CK_NoOp) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3849 | break; |
Argyrios Kyrtzidis | 63b57ae | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 3850 | E = ICE->getSubExpr(); |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
| 3853 | return E->getType()->isEnumeralType(); |
| 3854 | } |
| 3855 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3856 | static void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3857 | BinaryOperatorKind op = E->getOpcode(); |
Douglas Gregor | 14af91a | 2010-12-21 07:22:56 +0000 | [diff] [blame] | 3858 | if (E->isValueDependent()) |
| 3859 | return; |
| 3860 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3861 | if (op == BO_LT && IsZero(S, E->getRHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3862 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3863 | << "< 0" << "false" << HasEnumType(E->getLHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3864 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3865 | } else if (op == BO_GE && IsZero(S, E->getRHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3866 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3867 | << ">= 0" << "true" << HasEnumType(E->getLHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3868 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3869 | } else if (op == BO_GT && IsZero(S, E->getLHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3870 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3871 | << "0 >" << "false" << HasEnumType(E->getRHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3872 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3873 | } else if (op == BO_LE && IsZero(S, E->getLHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3874 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3875 | << "0 <=" << "true" << HasEnumType(E->getRHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3876 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
| 3877 | } |
| 3878 | } |
| 3879 | |
| 3880 | /// Analyze the operands of the given comparison. Implements the |
| 3881 | /// fallback case from AnalyzeComparison. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3882 | static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3883 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 3884 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3885 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3886 | |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3887 | /// \brief Implements -Wsign-compare. |
| 3888 | /// |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3889 | /// \param E the binary operator to check for warnings |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3890 | static void AnalyzeComparison(Sema &S, BinaryOperator *E) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3891 | // The type the comparison is being performed in. |
| 3892 | QualType T = E->getLHS()->getType(); |
| 3893 | assert(S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType()) |
| 3894 | && "comparison with mismatched types"); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3895 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3896 | // We don't do anything special if this isn't an unsigned integral |
| 3897 | // comparison: we're only interested in integral comparisons, and |
| 3898 | // 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] | 3899 | // |
| 3900 | // We also don't care about value-dependent expressions or expressions |
| 3901 | // whose result is a constant. |
| 3902 | if (!T->hasUnsignedIntegerRepresentation() |
| 3903 | || E->isValueDependent() || E->isIntegerConstantExpr(S.Context)) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3904 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3905 | |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3906 | Expr *LHS = E->getLHS()->IgnoreParenImpCasts(); |
| 3907 | Expr *RHS = E->getRHS()->IgnoreParenImpCasts(); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3908 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3909 | // Check to see if one of the (unmodified) operands is of different |
| 3910 | // signedness. |
| 3911 | Expr *signedOperand, *unsignedOperand; |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3912 | if (LHS->getType()->hasSignedIntegerRepresentation()) { |
| 3913 | assert(!RHS->getType()->hasSignedIntegerRepresentation() && |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3914 | "unsigned comparison between two signed integer expressions?"); |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3915 | signedOperand = LHS; |
| 3916 | unsignedOperand = RHS; |
| 3917 | } else if (RHS->getType()->hasSignedIntegerRepresentation()) { |
| 3918 | signedOperand = RHS; |
| 3919 | unsignedOperand = LHS; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3920 | } else { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3921 | CheckTrivialUnsignedComparison(S, E); |
| 3922 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3925 | // Otherwise, calculate the effective range of the signed operand. |
| 3926 | IntRange signedRange = GetExprRange(S.Context, signedOperand); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3927 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3928 | // Go ahead and analyze implicit conversions in the operands. Note |
| 3929 | // that we skip the implicit conversions on both sides. |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3930 | AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc()); |
| 3931 | AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc()); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3932 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3933 | // If the signed range is non-negative, -Wsign-compare won't fire, |
| 3934 | // but we should still check for comparisons which are always true |
| 3935 | // or false. |
| 3936 | if (signedRange.NonNegative) |
| 3937 | return CheckTrivialUnsignedComparison(S, E); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3938 | |
| 3939 | // For (in)equality comparisons, if the unsigned operand is a |
| 3940 | // constant which cannot collide with a overflowed signed operand, |
| 3941 | // then reinterpreting the signed operand as unsigned will not |
| 3942 | // change the result of the comparison. |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3943 | if (E->isEqualityOp()) { |
| 3944 | unsigned comparisonWidth = S.Context.getIntWidth(T); |
| 3945 | IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3946 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3947 | // We should never be unable to prove that the unsigned operand is |
| 3948 | // non-negative. |
| 3949 | assert(unsignedRange.NonNegative && "unsigned range includes negative?"); |
| 3950 | |
| 3951 | if (unsignedRange.Width < comparisonWidth) |
| 3952 | return; |
| 3953 | } |
| 3954 | |
| 3955 | S.Diag(E->getOperatorLoc(), diag::warn_mixed_sign_comparison) |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3956 | << LHS->getType() << RHS->getType() |
| 3957 | << LHS->getSourceRange() << RHS->getSourceRange(); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3960 | /// Analyzes an attempt to assign the given value to a bitfield. |
| 3961 | /// |
| 3962 | /// Returns true if there was something fishy about the attempt. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3963 | static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, |
| 3964 | SourceLocation InitLoc) { |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3965 | assert(Bitfield->isBitField()); |
| 3966 | if (Bitfield->isInvalidDecl()) |
| 3967 | return false; |
| 3968 | |
John McCall | 91b6014 | 2010-11-11 05:33:51 +0000 | [diff] [blame] | 3969 | // White-list bool bitfields. |
| 3970 | if (Bitfield->getType()->isBooleanType()) |
| 3971 | return false; |
| 3972 | |
Douglas Gregor | 46ff303 | 2011-02-04 13:09:01 +0000 | [diff] [blame] | 3973 | // Ignore value- or type-dependent expressions. |
| 3974 | if (Bitfield->getBitWidth()->isValueDependent() || |
| 3975 | Bitfield->getBitWidth()->isTypeDependent() || |
| 3976 | Init->isValueDependent() || |
| 3977 | Init->isTypeDependent()) |
| 3978 | return false; |
| 3979 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3980 | Expr *OriginalInit = Init->IgnoreParenImpCasts(); |
| 3981 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 3982 | llvm::APSInt Value; |
| 3983 | if (!OriginalInit->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects)) |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3984 | return false; |
| 3985 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3986 | unsigned OriginalWidth = Value.getBitWidth(); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3987 | unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context); |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3988 | |
| 3989 | if (OriginalWidth <= FieldWidth) |
| 3990 | return false; |
| 3991 | |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 3992 | // Compute the value which the bitfield will contain. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3993 | llvm::APSInt TruncatedValue = Value.trunc(FieldWidth); |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 3994 | TruncatedValue.setIsSigned(Bitfield->getType()->isSignedIntegerType()); |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3995 | |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 3996 | // Check whether the stored value is equal to the original value. |
| 3997 | TruncatedValue = TruncatedValue.extend(OriginalWidth); |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3998 | if (Value == TruncatedValue) |
| 3999 | return false; |
| 4000 | |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4001 | // Special-case bitfields of width 1: booleans are naturally 0/1, and |
Eli Friedman | 34ff062 | 2012-02-02 00:40:20 +0000 | [diff] [blame] | 4002 | // therefore don't strictly fit into a signed bitfield of width 1. |
| 4003 | if (FieldWidth == 1 && Value == 1) |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4004 | return false; |
| 4005 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4006 | std::string PrettyValue = Value.toString(10); |
| 4007 | std::string PrettyTrunc = TruncatedValue.toString(10); |
| 4008 | |
| 4009 | S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant) |
| 4010 | << PrettyValue << PrettyTrunc << OriginalInit->getType() |
| 4011 | << Init->getSourceRange(); |
| 4012 | |
| 4013 | return true; |
| 4014 | } |
| 4015 | |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4016 | /// Analyze the given simple or compound assignment for warning-worthy |
| 4017 | /// operations. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4018 | static void AnalyzeAssignment(Sema &S, BinaryOperator *E) { |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4019 | // Just recurse on the LHS. |
| 4020 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 4021 | |
| 4022 | // We want to recurse on the RHS as normal unless we're assigning to |
| 4023 | // a bitfield. |
| 4024 | if (FieldDecl *Bitfield = E->getLHS()->getBitField()) { |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4025 | if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(), |
| 4026 | E->getOperatorLoc())) { |
| 4027 | // Recurse, ignoring any implicit conversions on the RHS. |
| 4028 | return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(), |
| 4029 | E->getOperatorLoc()); |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4030 | } |
| 4031 | } |
| 4032 | |
| 4033 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
| 4034 | } |
| 4035 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4036 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4037 | static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4038 | SourceLocation CContext, unsigned diag, |
| 4039 | bool pruneControlFlow = false) { |
| 4040 | if (pruneControlFlow) { |
| 4041 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
| 4042 | S.PDiag(diag) |
| 4043 | << SourceType << T << E->getSourceRange() |
| 4044 | << SourceRange(CContext)); |
| 4045 | return; |
| 4046 | } |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 4047 | S.Diag(E->getExprLoc(), diag) |
| 4048 | << SourceType << T << E->getSourceRange() << SourceRange(CContext); |
| 4049 | } |
| 4050 | |
Chandler Carruth | e1b02e0 | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 4051 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4052 | static void DiagnoseImpCast(Sema &S, Expr *E, QualType T, |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4053 | SourceLocation CContext, unsigned diag, |
| 4054 | bool pruneControlFlow = false) { |
| 4055 | DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow); |
Chandler Carruth | e1b02e0 | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 4056 | } |
| 4057 | |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4058 | /// Diagnose an implicit cast from a literal expression. Does not warn when the |
| 4059 | /// cast wouldn't lose information. |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4060 | void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T, |
| 4061 | SourceLocation CContext) { |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4062 | // 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] | 4063 | bool isExact = false; |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4064 | const llvm::APFloat &Value = FL->getValue(); |
Jeffrey Yasskin | 3e1ef78 | 2011-07-15 17:03:07 +0000 | [diff] [blame] | 4065 | llvm::APSInt IntegerValue(S.Context.getIntWidth(T), |
| 4066 | T->hasUnsignedIntegerRepresentation()); |
| 4067 | if (Value.convertToInteger(IntegerValue, |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4068 | llvm::APFloat::rmTowardZero, &isExact) |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4069 | == llvm::APFloat::opOK && isExact) |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4070 | return; |
| 4071 | |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4072 | S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer) |
| 4073 | << FL->getType() << T << FL->getSourceRange() << SourceRange(CContext); |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4074 | } |
| 4075 | |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4076 | std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) { |
| 4077 | if (!Range.Width) return "0"; |
| 4078 | |
| 4079 | llvm::APSInt ValueInRange = Value; |
| 4080 | ValueInRange.setIsSigned(!Range.NonNegative); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 4081 | ValueInRange = ValueInRange.trunc(Range.Width); |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4082 | return ValueInRange.toString(10); |
| 4083 | } |
| 4084 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4085 | void CheckImplicitConversion(Sema &S, Expr *E, QualType T, |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4086 | SourceLocation CC, bool *ICContext = 0) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4087 | if (E->isTypeDependent() || E->isValueDependent()) return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4088 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4089 | const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr(); |
| 4090 | const Type *Target = S.Context.getCanonicalType(T).getTypePtr(); |
| 4091 | if (Source == Target) return; |
| 4092 | if (Target->isDependentType()) return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4093 | |
Chandler Carruth | 108f756 | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 4094 | // If the conversion context location is invalid don't complain. We also |
| 4095 | // don't want to emit a warning if the issue occurs from the expansion of |
| 4096 | // a system macro. The problem is that 'getSpellingLoc()' is slow, so we |
| 4097 | // delay this check as long as possible. Once we detect we are in that |
| 4098 | // scenario, we just return. |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4099 | if (CC.isInvalid()) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4100 | return; |
| 4101 | |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4102 | // Diagnose implicit casts to bool. |
| 4103 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) { |
| 4104 | if (isa<StringLiteral>(E)) |
| 4105 | // Warn on string literal to bool. Checks for string literals in logical |
| 4106 | // expressions, for instances, assert(0 && "error here"), is prevented |
| 4107 | // by a check in AnalyzeImplicitConversions(). |
| 4108 | return DiagnoseImpCast(S, E, T, CC, |
| 4109 | diag::warn_impcast_string_literal_to_bool); |
Lang Hames | e14ca9f | 2011-12-05 20:49:50 +0000 | [diff] [blame] | 4110 | if (Source->isFunctionType()) { |
| 4111 | // Warn on function to bool. Checks free functions and static member |
| 4112 | // functions. Weakly imported functions are excluded from the check, |
| 4113 | // since it's common to test their value to check whether the linker |
| 4114 | // found a definition for them. |
| 4115 | ValueDecl *D = 0; |
| 4116 | if (DeclRefExpr* R = dyn_cast<DeclRefExpr>(E)) { |
| 4117 | D = R->getDecl(); |
| 4118 | } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) { |
| 4119 | D = M->getMemberDecl(); |
| 4120 | } |
| 4121 | |
| 4122 | if (D && !D->isWeak()) { |
Richard Trieu | 26b45d8 | 2011-12-06 04:48:01 +0000 | [diff] [blame] | 4123 | if (FunctionDecl* F = dyn_cast<FunctionDecl>(D)) { |
| 4124 | S.Diag(E->getExprLoc(), diag::warn_impcast_function_to_bool) |
| 4125 | << F << E->getSourceRange() << SourceRange(CC); |
David Blaikie | 2def773 | 2011-12-09 21:42:37 +0000 | [diff] [blame] | 4126 | S.Diag(E->getExprLoc(), diag::note_function_to_bool_silence) |
| 4127 | << FixItHint::CreateInsertion(E->getExprLoc(), "&"); |
| 4128 | QualType ReturnType; |
| 4129 | UnresolvedSet<4> NonTemplateOverloads; |
| 4130 | S.isExprCallable(*E, ReturnType, NonTemplateOverloads); |
| 4131 | if (!ReturnType.isNull() |
| 4132 | && ReturnType->isSpecificBuiltinType(BuiltinType::Bool)) |
| 4133 | S.Diag(E->getExprLoc(), diag::note_function_to_bool_call) |
| 4134 | << FixItHint::CreateInsertion( |
| 4135 | S.getPreprocessor().getLocForEndOfToken(E->getLocEnd()), "()"); |
Richard Trieu | 26b45d8 | 2011-12-06 04:48:01 +0000 | [diff] [blame] | 4136 | return; |
| 4137 | } |
Lang Hames | e14ca9f | 2011-12-05 20:49:50 +0000 | [diff] [blame] | 4138 | } |
| 4139 | } |
David Blaikie | e37cdc4 | 2011-09-29 04:06:47 +0000 | [diff] [blame] | 4140 | return; // Other casts to bool are not checked. |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4141 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4142 | |
| 4143 | // Strip vector types. |
| 4144 | if (isa<VectorType>(Source)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4145 | if (!isa<VectorType>(Target)) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4146 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4147 | return; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4148 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar); |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4149 | } |
Chris Lattner | b792b30 | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 4150 | |
| 4151 | // If the vector cast is cast between two vectors of the same size, it is |
| 4152 | // a bitcast, not a conversion. |
| 4153 | if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target)) |
| 4154 | return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4155 | |
| 4156 | Source = cast<VectorType>(Source)->getElementType().getTypePtr(); |
| 4157 | Target = cast<VectorType>(Target)->getElementType().getTypePtr(); |
| 4158 | } |
| 4159 | |
| 4160 | // Strip complex types. |
| 4161 | if (isa<ComplexType>(Source)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4162 | if (!isa<ComplexType>(Target)) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4163 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4164 | return; |
| 4165 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4166 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar); |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4167 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4168 | |
| 4169 | Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); |
| 4170 | Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); |
| 4171 | } |
| 4172 | |
| 4173 | const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); |
| 4174 | const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); |
| 4175 | |
| 4176 | // If the source is floating point... |
| 4177 | if (SourceBT && SourceBT->isFloatingPoint()) { |
| 4178 | // ...and the target is floating point... |
| 4179 | if (TargetBT && TargetBT->isFloatingPoint()) { |
| 4180 | // ...then warn if we're dropping FP rank. |
| 4181 | |
| 4182 | // Builtin FP kinds are ordered by increasing FP rank. |
| 4183 | if (SourceBT->getKind() > TargetBT->getKind()) { |
| 4184 | // Don't warn about float constants that are precisely |
| 4185 | // representable in the target type. |
| 4186 | Expr::EvalResult result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4187 | if (E->EvaluateAsRValue(result, S.Context)) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4188 | // Value might be a float, a float vector, or a float complex. |
| 4189 | if (IsSameFloatAfterCast(result.Val, |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4190 | S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)), |
| 4191 | S.Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4192 | return; |
| 4193 | } |
| 4194 | |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4195 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4196 | return; |
| 4197 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4198 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4199 | } |
| 4200 | return; |
| 4201 | } |
| 4202 | |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4203 | // If the target is integral, always warn. |
Chandler Carruth | a5b9332 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 4204 | if ((TargetBT && TargetBT->isInteger())) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4205 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4206 | return; |
| 4207 | |
Chandler Carruth | a5b9332 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 4208 | Expr *InnerE = E->IgnoreParenImpCasts(); |
Matt Beaumont-Gay | 634c8af | 2011-09-08 22:30:47 +0000 | [diff] [blame] | 4209 | // We also want to warn on, e.g., "int i = -1.234" |
| 4210 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE)) |
| 4211 | if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus) |
| 4212 | InnerE = UOp->getSubExpr()->IgnoreParenImpCasts(); |
| 4213 | |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4214 | if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) { |
| 4215 | DiagnoseFloatingLiteralImpCast(S, FL, T, CC); |
Chandler Carruth | a5b9332 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 4216 | } else { |
| 4217 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer); |
| 4218 | } |
| 4219 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4220 | |
| 4221 | return; |
| 4222 | } |
| 4223 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4224 | if (!Source->isIntegerType() || !Target->isIntegerType()) |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4225 | return; |
| 4226 | |
Richard Trieu | 1838ca5 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 4227 | if ((E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) |
| 4228 | == Expr::NPCK_GNUNull) && Target->isIntegerType()) { |
David Blaikie | b136049 | 2012-03-16 20:30:12 +0000 | [diff] [blame] | 4229 | SourceLocation Loc = E->getSourceRange().getBegin(); |
| 4230 | if (Loc.isMacroID()) |
| 4231 | Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first; |
| 4232 | S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer) |
David Blaikie | 2c0abf4 | 2012-04-30 18:27:22 +0000 | [diff] [blame] | 4233 | << T << clang::SourceRange(CC) |
| 4234 | << FixItHint::CreateReplacement(Loc, S.getFixItZeroLiteralForType(T)); |
Richard Trieu | 1838ca5 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 4235 | return; |
| 4236 | } |
| 4237 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4238 | IntRange SourceRange = GetExprRange(S.Context, E); |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4239 | IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4240 | |
| 4241 | if (SourceRange.Width > TargetRange.Width) { |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4242 | // If the source is a constant, use a default-on diagnostic. |
| 4243 | // TODO: this should happen for bitfield stores, too. |
| 4244 | llvm::APSInt Value(32); |
| 4245 | if (E->isIntegerConstantExpr(Value, S.Context)) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4246 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4247 | return; |
| 4248 | |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4249 | std::string PrettySourceValue = Value.toString(10); |
| 4250 | std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); |
| 4251 | |
Ted Kremenek | 5e745da | 2011-10-22 02:37:33 +0000 | [diff] [blame] | 4252 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
| 4253 | S.PDiag(diag::warn_impcast_integer_precision_constant) |
| 4254 | << PrettySourceValue << PrettyTargetValue |
| 4255 | << E->getType() << T << E->getSourceRange() |
| 4256 | << clang::SourceRange(CC)); |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4257 | return; |
| 4258 | } |
| 4259 | |
Chris Lattner | b792b30 | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 4260 | // 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] | 4261 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4262 | return; |
| 4263 | |
David Blaikie | 3705084 | 2012-04-12 22:40:54 +0000 | [diff] [blame] | 4264 | if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64) |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4265 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32, |
| 4266 | /* pruneControlFlow */ true); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4267 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
| 4270 | if ((TargetRange.NonNegative && !SourceRange.NonNegative) || |
| 4271 | (!TargetRange.NonNegative && SourceRange.NonNegative && |
| 4272 | SourceRange.Width == TargetRange.Width)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4273 | |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4274 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4275 | return; |
| 4276 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4277 | unsigned DiagID = diag::warn_impcast_integer_sign; |
| 4278 | |
| 4279 | // Traditionally, gcc has warned about this under -Wsign-compare. |
| 4280 | // We also want to warn about it in -Wconversion. |
| 4281 | // So if -Wconversion is off, use a completely identical diagnostic |
| 4282 | // in the sign-compare group. |
| 4283 | // The conditional-checking code will |
| 4284 | if (ICContext) { |
| 4285 | DiagID = diag::warn_impcast_integer_sign_conditional; |
| 4286 | *ICContext = true; |
| 4287 | } |
| 4288 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4289 | return DiagnoseImpCast(S, E, T, CC, DiagID); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4290 | } |
| 4291 | |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4292 | // Diagnose conversions between different enumeration types. |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 4293 | // In C, we pretend that the type of an EnumConstantDecl is its enumeration |
| 4294 | // type, to give us better diagnostics. |
| 4295 | QualType SourceType = E->getType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4296 | if (!S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 4297 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 4298 | if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 4299 | EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext()); |
| 4300 | SourceType = S.Context.getTypeDeclType(Enum); |
| 4301 | Source = S.Context.getCanonicalType(SourceType).getTypePtr(); |
| 4302 | } |
| 4303 | } |
| 4304 | |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4305 | if (const EnumType *SourceEnum = Source->getAs<EnumType>()) |
| 4306 | if (const EnumType *TargetEnum = Target->getAs<EnumType>()) |
| 4307 | if ((SourceEnum->getDecl()->getIdentifier() || |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4308 | SourceEnum->getDecl()->getTypedefNameForAnonDecl()) && |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4309 | (TargetEnum->getDecl()->getIdentifier() || |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4310 | TargetEnum->getDecl()->getTypedefNameForAnonDecl()) && |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4311 | SourceEnum != TargetEnum) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4312 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4313 | return; |
| 4314 | |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 4315 | return DiagnoseImpCast(S, E, SourceType, T, CC, |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4316 | diag::warn_impcast_different_enum_types); |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4317 | } |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4318 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4319 | return; |
| 4320 | } |
| 4321 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4322 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T); |
| 4323 | |
| 4324 | void CheckConditionalOperand(Sema &S, Expr *E, QualType T, |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4325 | SourceLocation CC, bool &ICContext) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4326 | E = E->IgnoreParenImpCasts(); |
| 4327 | |
| 4328 | if (isa<ConditionalOperator>(E)) |
| 4329 | return CheckConditionalOperator(S, cast<ConditionalOperator>(E), T); |
| 4330 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4331 | AnalyzeImplicitConversions(S, E, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4332 | if (E->getType() != T) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4333 | return CheckImplicitConversion(S, E, T, CC, &ICContext); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4334 | return; |
| 4335 | } |
| 4336 | |
| 4337 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T) { |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4338 | SourceLocation CC = E->getQuestionLoc(); |
| 4339 | |
| 4340 | AnalyzeImplicitConversions(S, E->getCond(), CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4341 | |
| 4342 | bool Suspicious = false; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4343 | CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious); |
| 4344 | CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4345 | |
| 4346 | // If -Wconversion would have warned about either of the candidates |
| 4347 | // for a signedness conversion to the context type... |
| 4348 | if (!Suspicious) return; |
| 4349 | |
| 4350 | // ...but it's currently ignored... |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 4351 | if (S.Diags.getDiagnosticLevel(diag::warn_impcast_integer_sign_conditional, |
| 4352 | CC)) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4353 | return; |
| 4354 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4355 | // ...then check whether it would have warned about either of the |
| 4356 | // candidates for a signedness conversion to the condition type. |
Richard Trieu | 5254161 | 2011-07-21 02:46:28 +0000 | [diff] [blame] | 4357 | if (E->getType() == T) return; |
| 4358 | |
| 4359 | Suspicious = false; |
| 4360 | CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(), |
| 4361 | E->getType(), CC, &Suspicious); |
| 4362 | if (!Suspicious) |
| 4363 | CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(), |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4364 | E->getType(), CC, &Suspicious); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4365 | } |
| 4366 | |
| 4367 | /// AnalyzeImplicitConversions - Find and report any interesting |
| 4368 | /// implicit conversions in the given expression. There are a couple |
| 4369 | /// of competing diagnostics here, -Wconversion and -Wsign-compare. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4370 | void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4371 | QualType T = OrigE->getType(); |
| 4372 | Expr *E = OrigE->IgnoreParenImpCasts(); |
| 4373 | |
Douglas Gregor | f8b6e15 | 2011-10-10 17:38:18 +0000 | [diff] [blame] | 4374 | if (E->isTypeDependent() || E->isValueDependent()) |
| 4375 | return; |
| 4376 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4377 | // For conditional operators, we analyze the arguments as if they |
| 4378 | // were being fed directly into the output. |
| 4379 | if (isa<ConditionalOperator>(E)) { |
| 4380 | ConditionalOperator *CO = cast<ConditionalOperator>(E); |
| 4381 | CheckConditionalOperator(S, CO, T); |
| 4382 | return; |
| 4383 | } |
| 4384 | |
| 4385 | // Go ahead and check any implicit conversions we might have skipped. |
| 4386 | // The non-canonical typecheck is just an optimization; |
| 4387 | // CheckImplicitConversion will filter out dead implicit conversions. |
| 4388 | if (E->getType() != T) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4389 | CheckImplicitConversion(S, E, T, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4390 | |
| 4391 | // Now continue drilling into this expression. |
| 4392 | |
| 4393 | // Skip past explicit casts. |
| 4394 | if (isa<ExplicitCastExpr>(E)) { |
| 4395 | E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts(); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4396 | return AnalyzeImplicitConversions(S, E, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4397 | } |
| 4398 | |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4399 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 4400 | // Do a somewhat different check with comparison operators. |
| 4401 | if (BO->isComparisonOp()) |
| 4402 | return AnalyzeComparison(S, BO); |
| 4403 | |
Eli Friedman | 0fa0638 | 2012-01-26 23:34:06 +0000 | [diff] [blame] | 4404 | // And with simple assignments. |
| 4405 | if (BO->getOpcode() == BO_Assign) |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4406 | return AnalyzeAssignment(S, BO); |
| 4407 | } |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4408 | |
| 4409 | // These break the otherwise-useful invariant below. Fortunately, |
| 4410 | // we don't really need to recurse into them, because any internal |
| 4411 | // expressions should have been analyzed already when they were |
| 4412 | // built into statements. |
| 4413 | if (isa<StmtExpr>(E)) return; |
| 4414 | |
| 4415 | // Don't descend into unevaluated contexts. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4416 | if (isa<UnaryExprOrTypeTraitExpr>(E)) return; |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4417 | |
| 4418 | // Now just recurse over the expression's children. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4419 | CC = E->getExprLoc(); |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4420 | BinaryOperator *BO = dyn_cast<BinaryOperator>(E); |
| 4421 | bool IsLogicalOperator = BO && BO->isLogicalOp(); |
| 4422 | for (Stmt::child_range I = E->children(); I; ++I) { |
Douglas Gregor | 54042f1 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 4423 | Expr *ChildExpr = dyn_cast_or_null<Expr>(*I); |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 4424 | if (!ChildExpr) |
| 4425 | continue; |
| 4426 | |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4427 | if (IsLogicalOperator && |
| 4428 | isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts())) |
| 4429 | // Ignore checking string literals that are in logical operators. |
| 4430 | continue; |
| 4431 | AnalyzeImplicitConversions(S, ChildExpr, CC); |
| 4432 | } |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4433 | } |
| 4434 | |
| 4435 | } // end anonymous namespace |
| 4436 | |
| 4437 | /// Diagnoses "dangerous" implicit conversions within the given |
| 4438 | /// expression (which is a full expression). Implements -Wconversion |
| 4439 | /// and -Wsign-compare. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4440 | /// |
| 4441 | /// \param CC the "context" location of the implicit conversion, i.e. |
| 4442 | /// the most location of the syntactic entity requiring the implicit |
| 4443 | /// conversion |
| 4444 | void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4445 | // Don't diagnose in unevaluated contexts. |
| 4446 | if (ExprEvalContexts.back().Context == Sema::Unevaluated) |
| 4447 | return; |
| 4448 | |
| 4449 | // Don't diagnose for value- or type-dependent expressions. |
| 4450 | if (E->isTypeDependent() || E->isValueDependent()) |
| 4451 | return; |
| 4452 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4453 | // Check for array bounds violations in cases where the check isn't triggered |
| 4454 | // elsewhere for other Expr types (like BinaryOperators), e.g. when an |
| 4455 | // ArraySubscriptExpr is on the RHS of a variable initialization. |
| 4456 | CheckArrayAccess(E); |
| 4457 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4458 | // This is not the right CC for (e.g.) a variable initialization. |
| 4459 | AnalyzeImplicitConversions(*this, E, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4460 | } |
| 4461 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4462 | void Sema::CheckBitFieldInitialization(SourceLocation InitLoc, |
| 4463 | FieldDecl *BitField, |
| 4464 | Expr *Init) { |
| 4465 | (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc); |
| 4466 | } |
| 4467 | |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4468 | /// CheckParmsForFunctionDef - Check that the parameters of the given |
| 4469 | /// function are appropriate for the definition of a function. This |
| 4470 | /// takes care of any checks that cannot be performed on the |
| 4471 | /// declaration itself, e.g., that the types of each of the function |
| 4472 | /// parameters are complete. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 4473 | bool Sema::CheckParmsForFunctionDef(ParmVarDecl **P, ParmVarDecl **PEnd, |
| 4474 | bool CheckParameterNames) { |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4475 | bool HasInvalidParm = false; |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 4476 | for (; P != PEnd; ++P) { |
| 4477 | ParmVarDecl *Param = *P; |
| 4478 | |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4479 | // C99 6.7.5.3p4: the parameters in a parameter type list in a |
| 4480 | // function declarator that is part of a function definition of |
| 4481 | // that function shall not have incomplete type. |
| 4482 | // |
| 4483 | // This is also C++ [dcl.fct]p6. |
| 4484 | if (!Param->isInvalidDecl() && |
| 4485 | RequireCompleteType(Param->getLocation(), Param->getType(), |
| 4486 | diag::err_typecheck_decl_incomplete_type)) { |
| 4487 | Param->setInvalidDecl(); |
| 4488 | HasInvalidParm = true; |
| 4489 | } |
| 4490 | |
| 4491 | // C99 6.9.1p5: If the declarator includes a parameter type list, the |
| 4492 | // declaration of each parameter shall include an identifier. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 4493 | if (CheckParameterNames && |
| 4494 | Param->getIdentifier() == 0 && |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4495 | !Param->isImplicit() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4496 | !getLangOpts().CPlusPlus) |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4497 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
Sam Weinig | d17e340 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 4498 | |
| 4499 | // C99 6.7.5.3p12: |
| 4500 | // If the function declarator is not part of a definition of that |
| 4501 | // function, parameters may have incomplete type and may use the [*] |
| 4502 | // notation in their sequences of declarator specifiers to specify |
| 4503 | // variable length array types. |
| 4504 | QualType PType = Param->getOriginalType(); |
| 4505 | if (const ArrayType *AT = Context.getAsArrayType(PType)) { |
| 4506 | if (AT->getSizeModifier() == ArrayType::Star) { |
| 4507 | // FIXME: This diagnosic should point the the '[*]' if source-location |
| 4508 | // information is added for it. |
| 4509 | Diag(Param->getLocation(), diag::err_array_star_in_function_definition); |
| 4510 | } |
| 4511 | } |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | return HasInvalidParm; |
| 4515 | } |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4516 | |
| 4517 | /// CheckCastAlign - Implements -Wcast-align, which warns when a |
| 4518 | /// pointer cast increases the alignment requirements. |
| 4519 | void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { |
| 4520 | // This is actually a lot of work to potentially be doing on every |
| 4521 | // 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] | 4522 | if (getDiagnostics().getDiagnosticLevel(diag::warn_cast_align, |
| 4523 | TRange.getBegin()) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 4524 | == DiagnosticsEngine::Ignored) |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4525 | return; |
| 4526 | |
| 4527 | // Ignore dependent types. |
| 4528 | if (T->isDependentType() || Op->getType()->isDependentType()) |
| 4529 | return; |
| 4530 | |
| 4531 | // Require that the destination be a pointer type. |
| 4532 | const PointerType *DestPtr = T->getAs<PointerType>(); |
| 4533 | if (!DestPtr) return; |
| 4534 | |
| 4535 | // If the destination has alignment 1, we're done. |
| 4536 | QualType DestPointee = DestPtr->getPointeeType(); |
| 4537 | if (DestPointee->isIncompleteType()) return; |
| 4538 | CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); |
| 4539 | if (DestAlign.isOne()) return; |
| 4540 | |
| 4541 | // Require that the source be a pointer type. |
| 4542 | const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); |
| 4543 | if (!SrcPtr) return; |
| 4544 | QualType SrcPointee = SrcPtr->getPointeeType(); |
| 4545 | |
| 4546 | // Whitelist casts from cv void*. We already implicitly |
| 4547 | // whitelisted casts to cv void*, since they have alignment 1. |
| 4548 | // Also whitelist casts involving incomplete types, which implicitly |
| 4549 | // includes 'void'. |
| 4550 | if (SrcPointee->isIncompleteType()) return; |
| 4551 | |
| 4552 | CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee); |
| 4553 | if (SrcAlign >= DestAlign) return; |
| 4554 | |
| 4555 | Diag(TRange.getBegin(), diag::warn_cast_align) |
| 4556 | << Op->getType() << T |
| 4557 | << static_cast<unsigned>(SrcAlign.getQuantity()) |
| 4558 | << static_cast<unsigned>(DestAlign.getQuantity()) |
| 4559 | << TRange << Op->getSourceRange(); |
| 4560 | } |
| 4561 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4562 | static const Type* getElementType(const Expr *BaseExpr) { |
| 4563 | const Type* EltType = BaseExpr->getType().getTypePtr(); |
| 4564 | if (EltType->isAnyPointerType()) |
| 4565 | return EltType->getPointeeType().getTypePtr(); |
| 4566 | else if (EltType->isArrayType()) |
| 4567 | return EltType->getBaseElementTypeUnsafe(); |
| 4568 | return EltType; |
| 4569 | } |
| 4570 | |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 4571 | /// \brief Check whether this array fits the idiom of a size-one tail padded |
| 4572 | /// array member of a struct. |
| 4573 | /// |
| 4574 | /// We avoid emitting out-of-bounds access warnings for such arrays as they are |
| 4575 | /// commonly used to emulate flexible arrays in C89 code. |
| 4576 | static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size, |
| 4577 | const NamedDecl *ND) { |
| 4578 | if (Size != 1 || !ND) return false; |
| 4579 | |
| 4580 | const FieldDecl *FD = dyn_cast<FieldDecl>(ND); |
| 4581 | if (!FD) return false; |
| 4582 | |
| 4583 | // Don't consider sizes resulting from macro expansions or template argument |
| 4584 | // substitution to form C89 tail-padded arrays. |
| 4585 | ConstantArrayTypeLoc TL = |
| 4586 | cast<ConstantArrayTypeLoc>(FD->getTypeSourceInfo()->getTypeLoc()); |
| 4587 | const Expr *SizeExpr = dyn_cast<IntegerLiteral>(TL.getSizeExpr()); |
| 4588 | if (!SizeExpr || SizeExpr->getExprLoc().isMacroID()) |
| 4589 | return false; |
| 4590 | |
| 4591 | const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext()); |
Matt Beaumont-Gay | 381711c | 2011-11-29 22:43:53 +0000 | [diff] [blame] | 4592 | if (!RD) return false; |
| 4593 | if (RD->isUnion()) return false; |
| 4594 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4595 | if (!CRD->isStandardLayout()) return false; |
| 4596 | } |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 4597 | |
Benjamin Kramer | 22d4fed | 2011-08-06 03:04:42 +0000 | [diff] [blame] | 4598 | // See if this is the last field decl in the record. |
| 4599 | const Decl *D = FD; |
| 4600 | while ((D = D->getNextDeclInContext())) |
| 4601 | if (isa<FieldDecl>(D)) |
| 4602 | return false; |
| 4603 | return true; |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 4604 | } |
| 4605 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4606 | void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4607 | const ArraySubscriptExpr *ASE, |
Richard Smith | 25b009a | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 4608 | bool AllowOnePastEnd, bool IndexNegated) { |
Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 4609 | IndexExpr = IndexExpr->IgnoreParenImpCasts(); |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4610 | if (IndexExpr->isValueDependent()) |
| 4611 | return; |
| 4612 | |
Matt Beaumont-Gay | 8ef8f43 | 2011-12-12 22:35:02 +0000 | [diff] [blame] | 4613 | const Type *EffectiveType = getElementType(BaseExpr); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4614 | BaseExpr = BaseExpr->IgnoreParenCasts(); |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4615 | const ConstantArrayType *ArrayTy = |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4616 | Context.getAsConstantArrayType(BaseExpr->getType()); |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4617 | if (!ArrayTy) |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 4618 | return; |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 4619 | |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4620 | llvm::APSInt index; |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4621 | if (!IndexExpr->EvaluateAsInt(index, Context)) |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 4622 | return; |
Richard Smith | 25b009a | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 4623 | if (IndexNegated) |
| 4624 | index = -index; |
Ted Kremenek | 8fd0a5d | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 4625 | |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4626 | const NamedDecl *ND = NULL; |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4627 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
| 4628 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 4629 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4630 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4631 | |
Ted Kremenek | 9e060ca | 2011-02-23 23:06:04 +0000 | [diff] [blame] | 4632 | if (index.isUnsigned() || !index.isNegative()) { |
Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 4633 | llvm::APInt size = ArrayTy->getSize(); |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 4634 | if (!size.isStrictlyPositive()) |
| 4635 | return; |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4636 | |
| 4637 | const Type* BaseType = getElementType(BaseExpr); |
Nico Weber | de5998f | 2011-09-17 22:59:41 +0000 | [diff] [blame] | 4638 | if (BaseType != EffectiveType) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4639 | // Make sure we're comparing apples to apples when comparing index to size |
| 4640 | uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); |
| 4641 | uint64_t array_typesize = Context.getTypeSize(BaseType); |
Kaelyn Uhrain | d10f4bc | 2011-08-10 19:47:25 +0000 | [diff] [blame] | 4642 | // Handle ptrarith_typesize being zero, such as when casting to void* |
Kaelyn Uhrain | 18f1697 | 2011-08-10 18:49:28 +0000 | [diff] [blame] | 4643 | if (!ptrarith_typesize) ptrarith_typesize = 1; |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4644 | if (ptrarith_typesize != array_typesize) { |
| 4645 | // There's a cast to a different size type involved |
| 4646 | uint64_t ratio = array_typesize / ptrarith_typesize; |
| 4647 | // TODO: Be smarter about handling cases where array_typesize is not a |
| 4648 | // multiple of ptrarith_typesize |
| 4649 | if (ptrarith_typesize * ratio == array_typesize) |
| 4650 | size *= llvm::APInt(size.getBitWidth(), ratio); |
| 4651 | } |
| 4652 | } |
| 4653 | |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4654 | if (size.getBitWidth() > index.getBitWidth()) |
Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 4655 | index = index.zext(size.getBitWidth()); |
Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 4656 | else if (size.getBitWidth() < index.getBitWidth()) |
Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 4657 | size = size.zext(index.getBitWidth()); |
Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 4658 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4659 | // For array subscripting the index must be less than size, but for pointer |
| 4660 | // arithmetic also allow the index (offset) to be equal to size since |
| 4661 | // computing the next address after the end of the array is legal and |
| 4662 | // commonly done e.g. in C++ iterators and range-based for loops. |
Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 4663 | if (AllowOnePastEnd ? index.ule(size) : index.ult(size)) |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4664 | return; |
| 4665 | |
| 4666 | // Also don't warn for arrays of size 1 which are members of some |
| 4667 | // structure. These are often used to approximate flexible arrays in C89 |
| 4668 | // code. |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4669 | if (IsTailPaddedMemberArray(*this, size, ND)) |
Ted Kremenek | 8fd0a5d | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 4670 | return; |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4671 | |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4672 | // Suppress the warning if the subscript expression (as identified by the |
| 4673 | // ']' location) and the index expression are both from macro expansions |
| 4674 | // within a system header. |
| 4675 | if (ASE) { |
| 4676 | SourceLocation RBracketLoc = SourceMgr.getSpellingLoc( |
| 4677 | ASE->getRBracketLoc()); |
| 4678 | if (SourceMgr.isInSystemHeader(RBracketLoc)) { |
| 4679 | SourceLocation IndexLoc = SourceMgr.getSpellingLoc( |
| 4680 | IndexExpr->getLocStart()); |
| 4681 | if (SourceMgr.isFromSameFile(RBracketLoc, IndexLoc)) |
| 4682 | return; |
| 4683 | } |
| 4684 | } |
| 4685 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4686 | unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds; |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4687 | if (ASE) |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4688 | DiagID = diag::warn_array_index_exceeds_bounds; |
| 4689 | |
| 4690 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, |
| 4691 | PDiag(DiagID) << index.toString(10, true) |
| 4692 | << size.toString(10, true) |
| 4693 | << (unsigned)size.getLimitedValue(~0U) |
| 4694 | << IndexExpr->getSourceRange()); |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4695 | } else { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4696 | unsigned DiagID = diag::warn_array_index_precedes_bounds; |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4697 | if (!ASE) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4698 | DiagID = diag::warn_ptr_arith_precedes_bounds; |
| 4699 | if (index.isNegative()) index = -index; |
| 4700 | } |
| 4701 | |
| 4702 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, |
| 4703 | PDiag(DiagID) << index.toString(10, true) |
| 4704 | << IndexExpr->getSourceRange()); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 4705 | } |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 4706 | |
Matt Beaumont-Gay | cfbc5b5 | 2011-11-29 19:27:11 +0000 | [diff] [blame] | 4707 | if (!ND) { |
| 4708 | // Try harder to find a NamedDecl to point at in the note. |
| 4709 | while (const ArraySubscriptExpr *ASE = |
| 4710 | dyn_cast<ArraySubscriptExpr>(BaseExpr)) |
| 4711 | BaseExpr = ASE->getBase()->IgnoreParenCasts(); |
| 4712 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
| 4713 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); |
| 4714 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) |
| 4715 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); |
| 4716 | } |
| 4717 | |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 4718 | if (ND) |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4719 | DiagRuntimeBehavior(ND->getLocStart(), BaseExpr, |
| 4720 | PDiag(diag::note_array_index_out_of_bounds) |
| 4721 | << ND->getDeclName()); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 4722 | } |
| 4723 | |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 4724 | void Sema::CheckArrayAccess(const Expr *expr) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4725 | int AllowOnePastEnd = 0; |
| 4726 | while (expr) { |
| 4727 | expr = expr->IgnoreParenImpCasts(); |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 4728 | switch (expr->getStmtClass()) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4729 | case Stmt::ArraySubscriptExprClass: { |
| 4730 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr); |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4731 | CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE, |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4732 | AllowOnePastEnd > 0); |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 4733 | return; |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4734 | } |
| 4735 | case Stmt::UnaryOperatorClass: { |
| 4736 | // Only unwrap the * and & unary operators |
| 4737 | const UnaryOperator *UO = cast<UnaryOperator>(expr); |
| 4738 | expr = UO->getSubExpr(); |
| 4739 | switch (UO->getOpcode()) { |
| 4740 | case UO_AddrOf: |
| 4741 | AllowOnePastEnd++; |
| 4742 | break; |
| 4743 | case UO_Deref: |
| 4744 | AllowOnePastEnd--; |
| 4745 | break; |
| 4746 | default: |
| 4747 | return; |
| 4748 | } |
| 4749 | break; |
| 4750 | } |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 4751 | case Stmt::ConditionalOperatorClass: { |
| 4752 | const ConditionalOperator *cond = cast<ConditionalOperator>(expr); |
| 4753 | if (const Expr *lhs = cond->getLHS()) |
| 4754 | CheckArrayAccess(lhs); |
| 4755 | if (const Expr *rhs = cond->getRHS()) |
| 4756 | CheckArrayAccess(rhs); |
| 4757 | return; |
| 4758 | } |
| 4759 | default: |
| 4760 | return; |
| 4761 | } |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 4762 | } |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 4763 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4764 | |
| 4765 | //===--- CHECK: Objective-C retain cycles ----------------------------------// |
| 4766 | |
| 4767 | namespace { |
| 4768 | struct RetainCycleOwner { |
| 4769 | RetainCycleOwner() : Variable(0), Indirect(false) {} |
| 4770 | VarDecl *Variable; |
| 4771 | SourceRange Range; |
| 4772 | SourceLocation Loc; |
| 4773 | bool Indirect; |
| 4774 | |
| 4775 | void setLocsFrom(Expr *e) { |
| 4776 | Loc = e->getExprLoc(); |
| 4777 | Range = e->getSourceRange(); |
| 4778 | } |
| 4779 | }; |
| 4780 | } |
| 4781 | |
| 4782 | /// Consider whether capturing the given variable can possibly lead to |
| 4783 | /// a retain cycle. |
| 4784 | static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) { |
| 4785 | // In ARC, it's captured strongly iff the variable has __strong |
| 4786 | // lifetime. In MRR, it's captured strongly if the variable is |
| 4787 | // __block and has an appropriate type. |
| 4788 | if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 4789 | return false; |
| 4790 | |
| 4791 | owner.Variable = var; |
| 4792 | owner.setLocsFrom(ref); |
| 4793 | return true; |
| 4794 | } |
| 4795 | |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 4796 | static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4797 | while (true) { |
| 4798 | e = e->IgnoreParens(); |
| 4799 | if (CastExpr *cast = dyn_cast<CastExpr>(e)) { |
| 4800 | switch (cast->getCastKind()) { |
| 4801 | case CK_BitCast: |
| 4802 | case CK_LValueBitCast: |
| 4803 | case CK_LValueToRValue: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4804 | case CK_ARCReclaimReturnedObject: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4805 | e = cast->getSubExpr(); |
| 4806 | continue; |
| 4807 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4808 | default: |
| 4809 | return false; |
| 4810 | } |
| 4811 | } |
| 4812 | |
| 4813 | if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) { |
| 4814 | ObjCIvarDecl *ivar = ref->getDecl(); |
| 4815 | if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 4816 | return false; |
| 4817 | |
| 4818 | // Try to find a retain cycle in the base. |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 4819 | if (!findRetainCycleOwner(S, ref->getBase(), owner)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4820 | return false; |
| 4821 | |
| 4822 | if (ref->isFreeIvar()) owner.setLocsFrom(ref); |
| 4823 | owner.Indirect = true; |
| 4824 | return true; |
| 4825 | } |
| 4826 | |
| 4827 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 4828 | VarDecl *var = dyn_cast<VarDecl>(ref->getDecl()); |
| 4829 | if (!var) return false; |
| 4830 | return considerVariable(var, ref, owner); |
| 4831 | } |
| 4832 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4833 | if (MemberExpr *member = dyn_cast<MemberExpr>(e)) { |
| 4834 | if (member->isArrow()) return false; |
| 4835 | |
| 4836 | // Don't count this as an indirect ownership. |
| 4837 | e = member->getBase(); |
| 4838 | continue; |
| 4839 | } |
| 4840 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4841 | if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { |
| 4842 | // Only pay attention to pseudo-objects on property references. |
| 4843 | ObjCPropertyRefExpr *pre |
| 4844 | = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm() |
| 4845 | ->IgnoreParens()); |
| 4846 | if (!pre) return false; |
| 4847 | if (pre->isImplicitProperty()) return false; |
| 4848 | ObjCPropertyDecl *property = pre->getExplicitProperty(); |
| 4849 | if (!property->isRetaining() && |
| 4850 | !(property->getPropertyIvarDecl() && |
| 4851 | property->getPropertyIvarDecl()->getType() |
| 4852 | .getObjCLifetime() == Qualifiers::OCL_Strong)) |
| 4853 | return false; |
| 4854 | |
| 4855 | owner.Indirect = true; |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 4856 | if (pre->isSuperReceiver()) { |
| 4857 | owner.Variable = S.getCurMethodDecl()->getSelfDecl(); |
| 4858 | if (!owner.Variable) |
| 4859 | return false; |
| 4860 | owner.Loc = pre->getLocation(); |
| 4861 | owner.Range = pre->getSourceRange(); |
| 4862 | return true; |
| 4863 | } |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4864 | e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase()) |
| 4865 | ->getSourceExpr()); |
| 4866 | continue; |
| 4867 | } |
| 4868 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4869 | // Array ivars? |
| 4870 | |
| 4871 | return false; |
| 4872 | } |
| 4873 | } |
| 4874 | |
| 4875 | namespace { |
| 4876 | struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> { |
| 4877 | FindCaptureVisitor(ASTContext &Context, VarDecl *variable) |
| 4878 | : EvaluatedExprVisitor<FindCaptureVisitor>(Context), |
| 4879 | Variable(variable), Capturer(0) {} |
| 4880 | |
| 4881 | VarDecl *Variable; |
| 4882 | Expr *Capturer; |
| 4883 | |
| 4884 | void VisitDeclRefExpr(DeclRefExpr *ref) { |
| 4885 | if (ref->getDecl() == Variable && !Capturer) |
| 4886 | Capturer = ref; |
| 4887 | } |
| 4888 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4889 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) { |
| 4890 | if (Capturer) return; |
| 4891 | Visit(ref->getBase()); |
| 4892 | if (Capturer && ref->isFreeIvar()) |
| 4893 | Capturer = ref; |
| 4894 | } |
| 4895 | |
| 4896 | void VisitBlockExpr(BlockExpr *block) { |
| 4897 | // Look inside nested blocks |
| 4898 | if (block->getBlockDecl()->capturesVariable(Variable)) |
| 4899 | Visit(block->getBlockDecl()->getBody()); |
| 4900 | } |
| 4901 | }; |
| 4902 | } |
| 4903 | |
| 4904 | /// Check whether the given argument is a block which captures a |
| 4905 | /// variable. |
| 4906 | static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) { |
| 4907 | assert(owner.Variable && owner.Loc.isValid()); |
| 4908 | |
| 4909 | e = e->IgnoreParenCasts(); |
| 4910 | BlockExpr *block = dyn_cast<BlockExpr>(e); |
| 4911 | if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable)) |
| 4912 | return 0; |
| 4913 | |
| 4914 | FindCaptureVisitor visitor(S.Context, owner.Variable); |
| 4915 | visitor.Visit(block->getBlockDecl()->getBody()); |
| 4916 | return visitor.Capturer; |
| 4917 | } |
| 4918 | |
| 4919 | static void diagnoseRetainCycle(Sema &S, Expr *capturer, |
| 4920 | RetainCycleOwner &owner) { |
| 4921 | assert(capturer); |
| 4922 | assert(owner.Variable && owner.Loc.isValid()); |
| 4923 | |
| 4924 | S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle) |
| 4925 | << owner.Variable << capturer->getSourceRange(); |
| 4926 | S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner) |
| 4927 | << owner.Indirect << owner.Range; |
| 4928 | } |
| 4929 | |
| 4930 | /// Check for a keyword selector that starts with the word 'add' or |
| 4931 | /// 'set'. |
| 4932 | static bool isSetterLikeSelector(Selector sel) { |
| 4933 | if (sel.isUnarySelector()) return false; |
| 4934 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4935 | StringRef str = sel.getNameForSlot(0); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4936 | while (!str.empty() && str.front() == '_') str = str.substr(1); |
Ted Kremenek | 968a0ee | 2011-12-01 00:59:21 +0000 | [diff] [blame] | 4937 | if (str.startswith("set")) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4938 | str = str.substr(3); |
Ted Kremenek | 968a0ee | 2011-12-01 00:59:21 +0000 | [diff] [blame] | 4939 | else if (str.startswith("add")) { |
| 4940 | // Specially whitelist 'addOperationWithBlock:'. |
| 4941 | if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock")) |
| 4942 | return false; |
| 4943 | str = str.substr(3); |
| 4944 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4945 | else |
| 4946 | return false; |
| 4947 | |
| 4948 | if (str.empty()) return true; |
| 4949 | return !islower(str.front()); |
| 4950 | } |
| 4951 | |
| 4952 | /// Check a message send to see if it's likely to cause a retain cycle. |
| 4953 | void Sema::checkRetainCycles(ObjCMessageExpr *msg) { |
| 4954 | // Only check instance methods whose selector looks like a setter. |
| 4955 | if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector())) |
| 4956 | return; |
| 4957 | |
| 4958 | // Try to find a variable that the receiver is strongly owned by. |
| 4959 | RetainCycleOwner owner; |
| 4960 | if (msg->getReceiverKind() == ObjCMessageExpr::Instance) { |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 4961 | if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4962 | return; |
| 4963 | } else { |
| 4964 | assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance); |
| 4965 | owner.Variable = getCurMethodDecl()->getSelfDecl(); |
| 4966 | owner.Loc = msg->getSuperLoc(); |
| 4967 | owner.Range = msg->getSuperLoc(); |
| 4968 | } |
| 4969 | |
| 4970 | // Check whether the receiver is captured by any of the arguments. |
| 4971 | for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) |
| 4972 | if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) |
| 4973 | return diagnoseRetainCycle(*this, capturer, owner); |
| 4974 | } |
| 4975 | |
| 4976 | /// Check a property assign to see if it's likely to cause a retain cycle. |
| 4977 | void Sema::checkRetainCycles(Expr *receiver, Expr *argument) { |
| 4978 | RetainCycleOwner owner; |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 4979 | if (!findRetainCycleOwner(*this, receiver, owner)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4980 | return; |
| 4981 | |
| 4982 | if (Expr *capturer = findCapturingExpr(*this, argument, owner)) |
| 4983 | diagnoseRetainCycle(*this, capturer, owner); |
| 4984 | } |
| 4985 | |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 4986 | bool Sema::checkUnsafeAssigns(SourceLocation Loc, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4987 | QualType LHS, Expr *RHS) { |
| 4988 | Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime(); |
| 4989 | if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone) |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 4990 | return false; |
| 4991 | // strip off any implicit cast added to get to the one arc-specific |
| 4992 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4993 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4994 | Diag(Loc, diag::warn_arc_retained_assign) |
| 4995 | << (LT == Qualifiers::OCL_ExplicitNone) |
| 4996 | << RHS->getSourceRange(); |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 4997 | return true; |
| 4998 | } |
| 4999 | RHS = cast->getSubExpr(); |
| 5000 | } |
| 5001 | return false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5002 | } |
| 5003 | |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5004 | void Sema::checkUnsafeExprAssigns(SourceLocation Loc, |
| 5005 | Expr *LHS, Expr *RHS) { |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 5006 | QualType LHSType; |
| 5007 | // PropertyRef on LHS type need be directly obtained from |
| 5008 | // its declaration as it has a PsuedoType. |
| 5009 | ObjCPropertyRefExpr *PRE |
| 5010 | = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens()); |
| 5011 | if (PRE && !PRE->isImplicitProperty()) { |
| 5012 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
| 5013 | if (PD) |
| 5014 | LHSType = PD->getType(); |
| 5015 | } |
| 5016 | |
| 5017 | if (LHSType.isNull()) |
| 5018 | LHSType = LHS->getType(); |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5019 | if (checkUnsafeAssigns(Loc, LHSType, RHS)) |
| 5020 | return; |
| 5021 | Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime(); |
| 5022 | // FIXME. Check for other life times. |
| 5023 | if (LT != Qualifiers::OCL_None) |
| 5024 | return; |
| 5025 | |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 5026 | if (PRE) { |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5027 | if (PRE->isImplicitProperty()) |
| 5028 | return; |
| 5029 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
| 5030 | if (!PD) |
| 5031 | return; |
| 5032 | |
| 5033 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 5034 | if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) { |
| 5035 | // when 'assign' attribute was not explicitly specified |
| 5036 | // by user, ignore it and rely on property type itself |
| 5037 | // for lifetime info. |
| 5038 | unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten(); |
| 5039 | if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) && |
| 5040 | LHSType->isObjCRetainableType()) |
| 5041 | return; |
| 5042 | |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5043 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5044 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5045 | Diag(Loc, diag::warn_arc_retained_property_assign) |
| 5046 | << RHS->getSourceRange(); |
| 5047 | return; |
| 5048 | } |
| 5049 | RHS = cast->getSubExpr(); |
| 5050 | } |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 5051 | } |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5052 | } |
| 5053 | } |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 5054 | |
| 5055 | //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===// |
| 5056 | |
| 5057 | namespace { |
| 5058 | bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr, |
| 5059 | SourceLocation StmtLoc, |
| 5060 | const NullStmt *Body) { |
| 5061 | // Do not warn if the body is a macro that expands to nothing, e.g: |
| 5062 | // |
| 5063 | // #define CALL(x) |
| 5064 | // if (condition) |
| 5065 | // CALL(0); |
| 5066 | // |
| 5067 | if (Body->hasLeadingEmptyMacro()) |
| 5068 | return false; |
| 5069 | |
| 5070 | // Get line numbers of statement and body. |
| 5071 | bool StmtLineInvalid; |
| 5072 | unsigned StmtLine = SourceMgr.getSpellingLineNumber(StmtLoc, |
| 5073 | &StmtLineInvalid); |
| 5074 | if (StmtLineInvalid) |
| 5075 | return false; |
| 5076 | |
| 5077 | bool BodyLineInvalid; |
| 5078 | unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(), |
| 5079 | &BodyLineInvalid); |
| 5080 | if (BodyLineInvalid) |
| 5081 | return false; |
| 5082 | |
| 5083 | // Warn if null statement and body are on the same line. |
| 5084 | if (StmtLine != BodyLine) |
| 5085 | return false; |
| 5086 | |
| 5087 | return true; |
| 5088 | } |
| 5089 | } // Unnamed namespace |
| 5090 | |
| 5091 | void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc, |
| 5092 | const Stmt *Body, |
| 5093 | unsigned DiagID) { |
| 5094 | // Since this is a syntactic check, don't emit diagnostic for template |
| 5095 | // instantiations, this just adds noise. |
| 5096 | if (CurrentInstantiationScope) |
| 5097 | return; |
| 5098 | |
| 5099 | // The body should be a null statement. |
| 5100 | const NullStmt *NBody = dyn_cast<NullStmt>(Body); |
| 5101 | if (!NBody) |
| 5102 | return; |
| 5103 | |
| 5104 | // Do the usual checks. |
| 5105 | if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) |
| 5106 | return; |
| 5107 | |
| 5108 | Diag(NBody->getSemiLoc(), DiagID); |
| 5109 | Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); |
| 5110 | } |
| 5111 | |
| 5112 | void Sema::DiagnoseEmptyLoopBody(const Stmt *S, |
| 5113 | const Stmt *PossibleBody) { |
| 5114 | assert(!CurrentInstantiationScope); // Ensured by caller |
| 5115 | |
| 5116 | SourceLocation StmtLoc; |
| 5117 | const Stmt *Body; |
| 5118 | unsigned DiagID; |
| 5119 | if (const ForStmt *FS = dyn_cast<ForStmt>(S)) { |
| 5120 | StmtLoc = FS->getRParenLoc(); |
| 5121 | Body = FS->getBody(); |
| 5122 | DiagID = diag::warn_empty_for_body; |
| 5123 | } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) { |
| 5124 | StmtLoc = WS->getCond()->getSourceRange().getEnd(); |
| 5125 | Body = WS->getBody(); |
| 5126 | DiagID = diag::warn_empty_while_body; |
| 5127 | } else |
| 5128 | return; // Neither `for' nor `while'. |
| 5129 | |
| 5130 | // The body should be a null statement. |
| 5131 | const NullStmt *NBody = dyn_cast<NullStmt>(Body); |
| 5132 | if (!NBody) |
| 5133 | return; |
| 5134 | |
| 5135 | // Skip expensive checks if diagnostic is disabled. |
| 5136 | if (Diags.getDiagnosticLevel(DiagID, NBody->getSemiLoc()) == |
| 5137 | DiagnosticsEngine::Ignored) |
| 5138 | return; |
| 5139 | |
| 5140 | // Do the usual checks. |
| 5141 | if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) |
| 5142 | return; |
| 5143 | |
| 5144 | // `for(...);' and `while(...);' are popular idioms, so in order to keep |
| 5145 | // noise level low, emit diagnostics only if for/while is followed by a |
| 5146 | // CompoundStmt, e.g.: |
| 5147 | // for (int i = 0; i < n; i++); |
| 5148 | // { |
| 5149 | // a(i); |
| 5150 | // } |
| 5151 | // or if for/while is followed by a statement with more indentation |
| 5152 | // than for/while itself: |
| 5153 | // for (int i = 0; i < n; i++); |
| 5154 | // a(i); |
| 5155 | bool ProbableTypo = isa<CompoundStmt>(PossibleBody); |
| 5156 | if (!ProbableTypo) { |
| 5157 | bool BodyColInvalid; |
| 5158 | unsigned BodyCol = SourceMgr.getPresumedColumnNumber( |
| 5159 | PossibleBody->getLocStart(), |
| 5160 | &BodyColInvalid); |
| 5161 | if (BodyColInvalid) |
| 5162 | return; |
| 5163 | |
| 5164 | bool StmtColInvalid; |
| 5165 | unsigned StmtCol = SourceMgr.getPresumedColumnNumber( |
| 5166 | S->getLocStart(), |
| 5167 | &StmtColInvalid); |
| 5168 | if (StmtColInvalid) |
| 5169 | return; |
| 5170 | |
| 5171 | if (BodyCol > StmtCol) |
| 5172 | ProbableTypo = true; |
| 5173 | } |
| 5174 | |
| 5175 | if (ProbableTypo) { |
| 5176 | Diag(NBody->getSemiLoc(), DiagID); |
| 5177 | Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); |
| 5178 | } |
| 5179 | } |