Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1 | //===--- SemaChecking.cpp - Extra Semantic Checking -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10 | // This file implements extra semantic analysis beyond what is enforced |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 11 | // by the C type system. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Initialization.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Sema.h" |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaInternal.h" |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Initialization.h" |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 19 | #include "clang/Sema/Lookup.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 20 | #include "clang/Sema/ScopeInfo.h" |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/Analyses/FormatString.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 23 | #include "clang/AST/CharUnits.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 25 | #include "clang/AST/DeclObjC.h" |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 26 | #include "clang/AST/Expr.h" |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 27 | #include "clang/AST/ExprCXX.h" |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 28 | #include "clang/AST/ExprObjC.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 29 | #include "clang/AST/EvaluatedExprVisitor.h" |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 30 | #include "clang/AST/DeclObjC.h" |
| 31 | #include "clang/AST/StmtCXX.h" |
| 32 | #include "clang/AST/StmtObjC.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 33 | #include "clang/Lex/Preprocessor.h" |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/BitVector.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallString.h" |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/STLExtras.h" |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 38 | #include "clang/Basic/TargetBuiltins.h" |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 39 | #include "clang/Basic/TargetInfo.h" |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 40 | #include "clang/Basic/ConvertUTF.h" |
Zhongxing Xu | a1f3dba | 2009-05-20 01:55:10 +0000 | [diff] [blame] | 41 | #include <limits> |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 42 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 43 | using namespace sema; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 45 | SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, |
| 46 | unsigned ByteNo) const { |
Chris Lattner | 08f92e3 | 2010-11-17 07:37:15 +0000 | [diff] [blame] | 47 | return SL->getLocationOfByte(ByteNo, PP.getSourceManager(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 48 | PP.getLangOpts(), PP.getTargetInfo()); |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 49 | } |
| 50 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 51 | /// Checks that a call expression's argument count is the desired number. |
| 52 | /// This is useful when doing custom type-checking. Returns true on error. |
| 53 | static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) { |
| 54 | unsigned argCount = call->getNumArgs(); |
| 55 | if (argCount == desiredArgCount) return false; |
| 56 | |
| 57 | if (argCount < desiredArgCount) |
| 58 | return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 59 | << 0 /*function call*/ << desiredArgCount << argCount |
| 60 | << call->getSourceRange(); |
| 61 | |
| 62 | // Highlight all the excess arguments. |
| 63 | SourceRange range(call->getArg(desiredArgCount)->getLocStart(), |
| 64 | call->getArg(argCount - 1)->getLocEnd()); |
| 65 | |
| 66 | return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args) |
| 67 | << 0 /*function call*/ << desiredArgCount << argCount |
| 68 | << call->getArg(1)->getSourceRange(); |
| 69 | } |
| 70 | |
Julien Lerouge | e593921 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 71 | /// Check that the first argument to __builtin_annotation is an integer |
| 72 | /// and the second argument is a non-wide string literal. |
| 73 | static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) { |
| 74 | if (checkArgCount(S, TheCall, 2)) |
| 75 | return true; |
| 76 | |
| 77 | // First argument should be an integer. |
| 78 | Expr *ValArg = TheCall->getArg(0); |
| 79 | QualType Ty = ValArg->getType(); |
| 80 | if (!Ty->isIntegerType()) { |
| 81 | S.Diag(ValArg->getLocStart(), diag::err_builtin_annotation_first_arg) |
| 82 | << ValArg->getSourceRange(); |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 83 | return true; |
| 84 | } |
Julien Lerouge | e593921 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 85 | |
| 86 | // Second argument should be a constant string. |
| 87 | Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts(); |
| 88 | StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg); |
| 89 | if (!Literal || !Literal->isAscii()) { |
| 90 | S.Diag(StrArg->getLocStart(), diag::err_builtin_annotation_second_arg) |
| 91 | << StrArg->getSourceRange(); |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | TheCall->setType(Ty); |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 99 | ExprResult |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 100 | Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 101 | ExprResult TheCallResult(Owned(TheCall)); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 946928f | 2010-10-01 23:23:24 +0000 | [diff] [blame] | 103 | // Find out if any arguments are required to be integer constant expressions. |
| 104 | unsigned ICEArguments = 0; |
| 105 | ASTContext::GetBuiltinTypeError Error; |
| 106 | Context.GetBuiltinType(BuiltinID, Error, &ICEArguments); |
| 107 | if (Error != ASTContext::GE_None) |
| 108 | ICEArguments = 0; // Don't diagnose previously diagnosed errors. |
| 109 | |
| 110 | // If any arguments are required to be ICE's, check and diagnose. |
| 111 | for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) { |
| 112 | // Skip arguments not required to be ICE's. |
| 113 | if ((ICEArguments & (1 << ArgNo)) == 0) continue; |
| 114 | |
| 115 | llvm::APSInt Result; |
| 116 | if (SemaBuiltinConstantArg(TheCall, ArgNo, Result)) |
| 117 | return true; |
| 118 | ICEArguments &= ~(1 << ArgNo); |
| 119 | } |
| 120 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 121 | switch (BuiltinID) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 122 | case Builtin::BI__builtin___CFStringMakeConstantString: |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 123 | assert(TheCall->getNumArgs() == 1 && |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 124 | "Wrong # arguments to builtin CFStringMakeConstantString"); |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 125 | if (CheckObjCString(TheCall->getArg(0))) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 126 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 127 | break; |
Ted Kremenek | 49ff7a1 | 2008-07-09 17:58:53 +0000 | [diff] [blame] | 128 | case Builtin::BI__builtin_stdarg_start: |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 129 | case Builtin::BI__builtin_va_start: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 130 | if (SemaBuiltinVAStart(TheCall)) |
| 131 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 132 | break; |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 133 | case Builtin::BI__builtin_isgreater: |
| 134 | case Builtin::BI__builtin_isgreaterequal: |
| 135 | case Builtin::BI__builtin_isless: |
| 136 | case Builtin::BI__builtin_islessequal: |
| 137 | case Builtin::BI__builtin_islessgreater: |
| 138 | case Builtin::BI__builtin_isunordered: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 139 | if (SemaBuiltinUnorderedCompare(TheCall)) |
| 140 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 141 | break; |
Benjamin Kramer | e771a7a | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 142 | case Builtin::BI__builtin_fpclassify: |
| 143 | if (SemaBuiltinFPClassification(TheCall, 6)) |
| 144 | return ExprError(); |
| 145 | break; |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 146 | case Builtin::BI__builtin_isfinite: |
| 147 | case Builtin::BI__builtin_isinf: |
| 148 | case Builtin::BI__builtin_isinf_sign: |
| 149 | case Builtin::BI__builtin_isnan: |
| 150 | case Builtin::BI__builtin_isnormal: |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 151 | if (SemaBuiltinFPClassification(TheCall, 1)) |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 152 | return ExprError(); |
| 153 | break; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 154 | case Builtin::BI__builtin_shufflevector: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 155 | return SemaBuiltinShuffleVector(TheCall); |
| 156 | // TheCall will be freed by the smart pointer here, but that's fine, since |
| 157 | // SemaBuiltinShuffleVector guts it, but then doesn't release it. |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 158 | case Builtin::BI__builtin_prefetch: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 159 | if (SemaBuiltinPrefetch(TheCall)) |
| 160 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 161 | break; |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 162 | case Builtin::BI__builtin_object_size: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 163 | if (SemaBuiltinObjectSize(TheCall)) |
| 164 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 165 | break; |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 166 | case Builtin::BI__builtin_longjmp: |
| 167 | if (SemaBuiltinLongjmp(TheCall)) |
| 168 | return ExprError(); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 169 | break; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 170 | |
| 171 | case Builtin::BI__builtin_classify_type: |
| 172 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 173 | TheCall->setType(Context.IntTy); |
| 174 | break; |
Chris Lattner | 75c29a0 | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 175 | case Builtin::BI__builtin_constant_p: |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 176 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 177 | TheCall->setType(Context.IntTy); |
Chris Lattner | 75c29a0 | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 178 | break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 179 | case Builtin::BI__sync_fetch_and_add: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 180 | case Builtin::BI__sync_fetch_and_add_1: |
| 181 | case Builtin::BI__sync_fetch_and_add_2: |
| 182 | case Builtin::BI__sync_fetch_and_add_4: |
| 183 | case Builtin::BI__sync_fetch_and_add_8: |
| 184 | case Builtin::BI__sync_fetch_and_add_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 185 | case Builtin::BI__sync_fetch_and_sub: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 186 | case Builtin::BI__sync_fetch_and_sub_1: |
| 187 | case Builtin::BI__sync_fetch_and_sub_2: |
| 188 | case Builtin::BI__sync_fetch_and_sub_4: |
| 189 | case Builtin::BI__sync_fetch_and_sub_8: |
| 190 | case Builtin::BI__sync_fetch_and_sub_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 191 | case Builtin::BI__sync_fetch_and_or: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 192 | case Builtin::BI__sync_fetch_and_or_1: |
| 193 | case Builtin::BI__sync_fetch_and_or_2: |
| 194 | case Builtin::BI__sync_fetch_and_or_4: |
| 195 | case Builtin::BI__sync_fetch_and_or_8: |
| 196 | case Builtin::BI__sync_fetch_and_or_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 197 | case Builtin::BI__sync_fetch_and_and: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 198 | case Builtin::BI__sync_fetch_and_and_1: |
| 199 | case Builtin::BI__sync_fetch_and_and_2: |
| 200 | case Builtin::BI__sync_fetch_and_and_4: |
| 201 | case Builtin::BI__sync_fetch_and_and_8: |
| 202 | case Builtin::BI__sync_fetch_and_and_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 203 | case Builtin::BI__sync_fetch_and_xor: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 204 | case Builtin::BI__sync_fetch_and_xor_1: |
| 205 | case Builtin::BI__sync_fetch_and_xor_2: |
| 206 | case Builtin::BI__sync_fetch_and_xor_4: |
| 207 | case Builtin::BI__sync_fetch_and_xor_8: |
| 208 | case Builtin::BI__sync_fetch_and_xor_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 209 | case Builtin::BI__sync_add_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 210 | case Builtin::BI__sync_add_and_fetch_1: |
| 211 | case Builtin::BI__sync_add_and_fetch_2: |
| 212 | case Builtin::BI__sync_add_and_fetch_4: |
| 213 | case Builtin::BI__sync_add_and_fetch_8: |
| 214 | case Builtin::BI__sync_add_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 215 | case Builtin::BI__sync_sub_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 216 | case Builtin::BI__sync_sub_and_fetch_1: |
| 217 | case Builtin::BI__sync_sub_and_fetch_2: |
| 218 | case Builtin::BI__sync_sub_and_fetch_4: |
| 219 | case Builtin::BI__sync_sub_and_fetch_8: |
| 220 | case Builtin::BI__sync_sub_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 221 | case Builtin::BI__sync_and_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 222 | case Builtin::BI__sync_and_and_fetch_1: |
| 223 | case Builtin::BI__sync_and_and_fetch_2: |
| 224 | case Builtin::BI__sync_and_and_fetch_4: |
| 225 | case Builtin::BI__sync_and_and_fetch_8: |
| 226 | case Builtin::BI__sync_and_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 227 | case Builtin::BI__sync_or_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 228 | case Builtin::BI__sync_or_and_fetch_1: |
| 229 | case Builtin::BI__sync_or_and_fetch_2: |
| 230 | case Builtin::BI__sync_or_and_fetch_4: |
| 231 | case Builtin::BI__sync_or_and_fetch_8: |
| 232 | case Builtin::BI__sync_or_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 233 | case Builtin::BI__sync_xor_and_fetch: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 234 | case Builtin::BI__sync_xor_and_fetch_1: |
| 235 | case Builtin::BI__sync_xor_and_fetch_2: |
| 236 | case Builtin::BI__sync_xor_and_fetch_4: |
| 237 | case Builtin::BI__sync_xor_and_fetch_8: |
| 238 | case Builtin::BI__sync_xor_and_fetch_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 239 | case Builtin::BI__sync_val_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 240 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 241 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 242 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 243 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 244 | case Builtin::BI__sync_val_compare_and_swap_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 245 | case Builtin::BI__sync_bool_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 246 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 247 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 248 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 249 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 250 | case Builtin::BI__sync_bool_compare_and_swap_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 251 | case Builtin::BI__sync_lock_test_and_set: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 252 | case Builtin::BI__sync_lock_test_and_set_1: |
| 253 | case Builtin::BI__sync_lock_test_and_set_2: |
| 254 | case Builtin::BI__sync_lock_test_and_set_4: |
| 255 | case Builtin::BI__sync_lock_test_and_set_8: |
| 256 | case Builtin::BI__sync_lock_test_and_set_16: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 257 | case Builtin::BI__sync_lock_release: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 258 | case Builtin::BI__sync_lock_release_1: |
| 259 | case Builtin::BI__sync_lock_release_2: |
| 260 | case Builtin::BI__sync_lock_release_4: |
| 261 | case Builtin::BI__sync_lock_release_8: |
| 262 | case Builtin::BI__sync_lock_release_16: |
Chris Lattner | 23aa9c8 | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 263 | case Builtin::BI__sync_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 264 | case Builtin::BI__sync_swap_1: |
| 265 | case Builtin::BI__sync_swap_2: |
| 266 | case Builtin::BI__sync_swap_4: |
| 267 | case Builtin::BI__sync_swap_8: |
| 268 | case Builtin::BI__sync_swap_16: |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 269 | return SemaBuiltinAtomicOverloaded(move(TheCallResult)); |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 270 | #define BUILTIN(ID, TYPE, ATTRS) |
| 271 | #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ |
| 272 | case Builtin::BI##ID: \ |
| 273 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::AO##ID); |
| 274 | #include "clang/Basic/Builtins.def" |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 275 | case Builtin::BI__builtin_annotation: |
Julien Lerouge | e593921 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 276 | if (SemaBuiltinAnnotation(*this, TheCall)) |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 277 | return ExprError(); |
| 278 | break; |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | // Since the target specific builtins for each arch overlap, only check those |
| 282 | // of the arch we are compiling for. |
| 283 | if (BuiltinID >= Builtin::FirstTSBuiltin) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 284 | switch (Context.getTargetInfo().getTriple().getArch()) { |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 285 | case llvm::Triple::arm: |
| 286 | case llvm::Triple::thumb: |
| 287 | if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall)) |
| 288 | return ExprError(); |
| 289 | break; |
Simon Atanasyan | fad0a32 | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 290 | case llvm::Triple::mips: |
| 291 | case llvm::Triple::mipsel: |
| 292 | case llvm::Triple::mips64: |
| 293 | case llvm::Triple::mips64el: |
| 294 | if (CheckMipsBuiltinFunctionCall(BuiltinID, TheCall)) |
| 295 | return ExprError(); |
| 296 | break; |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 297 | default: |
| 298 | break; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | return move(TheCallResult); |
| 303 | } |
| 304 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 305 | // Get the valid immediate range for the specified NEON type code. |
| 306 | static unsigned RFT(unsigned t, bool shift = false) { |
Bob Wilson | da95f73 | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 307 | NeonTypeFlags Type(t); |
| 308 | int IsQuad = Type.isQuad(); |
| 309 | switch (Type.getEltType()) { |
| 310 | case NeonTypeFlags::Int8: |
| 311 | case NeonTypeFlags::Poly8: |
| 312 | return shift ? 7 : (8 << IsQuad) - 1; |
| 313 | case NeonTypeFlags::Int16: |
| 314 | case NeonTypeFlags::Poly16: |
| 315 | return shift ? 15 : (4 << IsQuad) - 1; |
| 316 | case NeonTypeFlags::Int32: |
| 317 | return shift ? 31 : (2 << IsQuad) - 1; |
| 318 | case NeonTypeFlags::Int64: |
| 319 | return shift ? 63 : (1 << IsQuad) - 1; |
| 320 | case NeonTypeFlags::Float16: |
| 321 | assert(!shift && "cannot shift float types!"); |
| 322 | return (4 << IsQuad) - 1; |
| 323 | case NeonTypeFlags::Float32: |
| 324 | assert(!shift && "cannot shift float types!"); |
| 325 | return (2 << IsQuad) - 1; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 326 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 327 | llvm_unreachable("Invalid NeonTypeFlag!"); |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 330 | /// getNeonEltType - Return the QualType corresponding to the elements of |
| 331 | /// the vector type specified by the NeonTypeFlags. This is used to check |
| 332 | /// the pointer arguments for Neon load/store intrinsics. |
| 333 | static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context) { |
| 334 | switch (Flags.getEltType()) { |
| 335 | case NeonTypeFlags::Int8: |
| 336 | return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy; |
| 337 | case NeonTypeFlags::Int16: |
| 338 | return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy; |
| 339 | case NeonTypeFlags::Int32: |
| 340 | return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy; |
| 341 | case NeonTypeFlags::Int64: |
| 342 | return Flags.isUnsigned() ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 343 | case NeonTypeFlags::Poly8: |
| 344 | return Context.SignedCharTy; |
| 345 | case NeonTypeFlags::Poly16: |
| 346 | return Context.ShortTy; |
| 347 | case NeonTypeFlags::Float16: |
| 348 | return Context.UnsignedShortTy; |
| 349 | case NeonTypeFlags::Float32: |
| 350 | return Context.FloatTy; |
| 351 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 352 | llvm_unreachable("Invalid NeonTypeFlag!"); |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 355 | bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 356 | llvm::APSInt Result; |
| 357 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 358 | unsigned mask = 0; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 359 | unsigned TV = 0; |
Bob Wilson | 4648255 | 2011-11-16 21:32:23 +0000 | [diff] [blame] | 360 | int PtrArgNum = -1; |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 361 | bool HasConstPtr = false; |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 362 | switch (BuiltinID) { |
Nate Begeman | a23326b | 2010-06-17 04:17:01 +0000 | [diff] [blame] | 363 | #define GET_NEON_OVERLOAD_CHECK |
| 364 | #include "clang/Basic/arm_neon.inc" |
| 365 | #undef GET_NEON_OVERLOAD_CHECK |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 368 | // For NEON intrinsics which are overloaded on vector element type, validate |
| 369 | // the immediate which specifies which variant to emit. |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 370 | unsigned ImmArg = TheCall->getNumArgs()-1; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 371 | if (mask) { |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 372 | if (SemaBuiltinConstantArg(TheCall, ImmArg, Result)) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 373 | return true; |
| 374 | |
Bob Wilson | da95f73 | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 375 | TV = Result.getLimitedValue(64); |
| 376 | if ((TV > 63) || (mask & (1 << TV)) == 0) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 377 | return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code) |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 378 | << TheCall->getArg(ImmArg)->getSourceRange(); |
| 379 | } |
| 380 | |
Bob Wilson | 4648255 | 2011-11-16 21:32:23 +0000 | [diff] [blame] | 381 | if (PtrArgNum >= 0) { |
Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 382 | // Check that pointer arguments have the specified type. |
Bob Wilson | 4648255 | 2011-11-16 21:32:23 +0000 | [diff] [blame] | 383 | Expr *Arg = TheCall->getArg(PtrArgNum); |
| 384 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) |
| 385 | Arg = ICE->getSubExpr(); |
| 386 | ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg); |
| 387 | QualType RHSTy = RHS.get()->getType(); |
| 388 | QualType EltTy = getNeonEltType(NeonTypeFlags(TV), Context); |
| 389 | if (HasConstPtr) |
| 390 | EltTy = EltTy.withConst(); |
| 391 | QualType LHSTy = Context.getPointerType(EltTy); |
| 392 | AssignConvertType ConvTy; |
| 393 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
| 394 | if (RHS.isInvalid()) |
| 395 | return true; |
| 396 | if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), LHSTy, RHSTy, |
| 397 | RHS.get(), AA_Assigning)) |
| 398 | return true; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 399 | } |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 400 | |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 401 | // For NEON intrinsics which take an immediate value as part of the |
| 402 | // instruction, range check them here. |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 403 | unsigned i = 0, l = 0, u = 0; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 404 | switch (BuiltinID) { |
| 405 | default: return false; |
Nate Begeman | bb37f50 | 2010-07-29 22:48:34 +0000 | [diff] [blame] | 406 | case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break; |
| 407 | case ARM::BI__builtin_arm_usat: i = 1; u = 31; break; |
Nate Begeman | 99c40bb | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 408 | case ARM::BI__builtin_arm_vcvtr_f: |
| 409 | case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break; |
Nate Begeman | a23326b | 2010-06-17 04:17:01 +0000 | [diff] [blame] | 410 | #define GET_NEON_IMMEDIATE_CHECK |
| 411 | #include "clang/Basic/arm_neon.inc" |
| 412 | #undef GET_NEON_IMMEDIATE_CHECK |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 413 | }; |
| 414 | |
Douglas Gregor | 592a423 | 2012-06-29 01:05:22 +0000 | [diff] [blame] | 415 | // We can't check the value of a dependent argument. |
| 416 | if (TheCall->getArg(i)->isTypeDependent() || |
| 417 | TheCall->getArg(i)->isValueDependent()) |
| 418 | return false; |
| 419 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 420 | // Check that the immediate argument is actually a constant. |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 421 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 422 | return true; |
| 423 | |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 424 | // Range check against the upper/lower values for this isntruction. |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 425 | unsigned Val = Result.getZExtValue(); |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 426 | if (Val < l || Val > (u + l)) |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 427 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 428 | << l << u+l << TheCall->getArg(i)->getSourceRange(); |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 429 | |
Nate Begeman | 99c40bb | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 430 | // FIXME: VFP Intrinsics should error if VFP not present. |
Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 431 | return false; |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 432 | } |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 433 | |
Simon Atanasyan | fad0a32 | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 434 | bool Sema::CheckMipsBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
| 435 | unsigned i = 0, l = 0, u = 0; |
| 436 | switch (BuiltinID) { |
| 437 | default: return false; |
| 438 | case Mips::BI__builtin_mips_wrdsp: i = 1; l = 0; u = 63; break; |
| 439 | case Mips::BI__builtin_mips_rddsp: i = 0; l = 0; u = 63; break; |
| 440 | }; |
| 441 | |
| 442 | // We can't check the value of a dependent argument. |
| 443 | if (TheCall->getArg(i)->isTypeDependent() || |
| 444 | TheCall->getArg(i)->isValueDependent()) |
| 445 | return false; |
| 446 | |
| 447 | // Check that the immediate argument is actually a constant. |
| 448 | llvm::APSInt Result; |
| 449 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 450 | return true; |
| 451 | |
| 452 | // Range check against the upper/lower values for this instruction. |
| 453 | unsigned Val = Result.getZExtValue(); |
| 454 | if (Val < l || Val > u) |
| 455 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 456 | << l << u << TheCall->getArg(i)->getSourceRange(); |
| 457 | |
| 458 | return false; |
| 459 | } |
| 460 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 461 | /// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo |
| 462 | /// parameter with the FormatAttr's correct format_idx and firstDataArg. |
| 463 | /// Returns true when the format fits the function and the FormatStringInfo has |
| 464 | /// been populated. |
| 465 | bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember, |
| 466 | FormatStringInfo *FSI) { |
| 467 | FSI->HasVAListArg = Format->getFirstArg() == 0; |
| 468 | FSI->FormatIdx = Format->getFormatIdx() - 1; |
| 469 | FSI->FirstDataArg = FSI->HasVAListArg ? 0 : Format->getFirstArg() - 1; |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 470 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 471 | // The way the format attribute works in GCC, the implicit this argument |
| 472 | // of member functions is counted. However, it doesn't appear in our own |
| 473 | // lists, so decrement format_idx in that case. |
| 474 | if (IsCXXMember) { |
| 475 | if(FSI->FormatIdx == 0) |
| 476 | return false; |
| 477 | --FSI->FormatIdx; |
| 478 | if (FSI->FirstDataArg != 0) |
| 479 | --FSI->FirstDataArg; |
| 480 | } |
| 481 | return true; |
| 482 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 484 | /// Handles the checks for format strings, non-POD arguments to vararg |
| 485 | /// functions, and NULL arguments passed to non-NULL parameters. |
| 486 | void Sema::checkCall(NamedDecl *FDecl, Expr **Args, |
| 487 | unsigned NumArgs, |
| 488 | unsigned NumProtoArgs, |
| 489 | bool IsMemberFunction, |
| 490 | SourceLocation Loc, |
| 491 | SourceRange Range, |
| 492 | VariadicCallType CallType) { |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 493 | // FIXME: This mechanism should be abstracted to be less fragile and |
| 494 | // more efficient. For example, just map function ids to custom |
| 495 | // handlers. |
| 496 | |
Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 497 | // Printf and scanf checking. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 498 | bool HandledFormatString = false; |
Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 499 | for (specific_attr_iterator<FormatAttr> |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 500 | I = FDecl->specific_attr_begin<FormatAttr>(), |
| 501 | E = FDecl->specific_attr_end<FormatAttr>(); I != E ; ++I) |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 502 | if (CheckFormatArguments(*I, Args, NumArgs, IsMemberFunction, CallType, |
| 503 | Loc, Range)) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 504 | HandledFormatString = true; |
| 505 | |
| 506 | // Refuse POD arguments that weren't caught by the format string |
| 507 | // checks above. |
| 508 | if (!HandledFormatString && CallType != VariadicDoesNotApply) |
| 509 | for (unsigned ArgIdx = NumProtoArgs; ArgIdx < NumArgs; ++ArgIdx) |
| 510 | variadicArgumentPODCheck(Args[ArgIdx], CallType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 511 | |
Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 512 | for (specific_attr_iterator<NonNullAttr> |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 513 | I = FDecl->specific_attr_begin<NonNullAttr>(), |
| 514 | E = FDecl->specific_attr_end<NonNullAttr>(); I != E; ++I) |
| 515 | CheckNonNullArguments(*I, Args, Loc); |
| 516 | } |
| 517 | |
| 518 | /// CheckConstructorCall - Check a constructor call for correctness and safety |
| 519 | /// properties not enforced by the C type system. |
| 520 | void Sema::CheckConstructorCall(FunctionDecl *FDecl, Expr **Args, |
| 521 | unsigned NumArgs, |
| 522 | const FunctionProtoType *Proto, |
| 523 | SourceLocation Loc) { |
| 524 | VariadicCallType CallType = |
| 525 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
| 526 | checkCall(FDecl, Args, NumArgs, Proto->getNumArgs(), |
| 527 | /*IsMemberFunction=*/true, Loc, SourceRange(), CallType); |
| 528 | } |
| 529 | |
| 530 | /// CheckFunctionCall - Check a direct function call for various correctness |
| 531 | /// and safety properties not strictly enforced by the C type system. |
| 532 | bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall, |
| 533 | const FunctionProtoType *Proto) { |
| 534 | bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall); |
| 535 | VariadicCallType CallType = getVariadicCallType(FDecl, Proto, |
| 536 | TheCall->getCallee()); |
| 537 | unsigned NumProtoArgs = Proto ? Proto->getNumArgs() : 0; |
| 538 | checkCall(FDecl, TheCall->getArgs(), TheCall->getNumArgs(), NumProtoArgs, |
| 539 | IsMemberFunction, TheCall->getRParenLoc(), |
| 540 | TheCall->getCallee()->getSourceRange(), CallType); |
| 541 | |
| 542 | IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
| 543 | // None of the checks below are needed for functions that don't have |
| 544 | // simple names (e.g., C++ conversion functions). |
| 545 | if (!FnInfo) |
| 546 | return false; |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 547 | |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 548 | unsigned CMId = FDecl->getMemoryFunctionKind(); |
| 549 | if (CMId == 0) |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 550 | return false; |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 551 | |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 552 | // Handle memory setting and copying functions. |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 553 | if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat) |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 554 | CheckStrlcpycatArguments(TheCall, FnInfo); |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 555 | else if (CMId == Builtin::BIstrncat) |
| 556 | CheckStrncatArguments(TheCall, FnInfo); |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 557 | else |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 558 | CheckMemaccessArguments(TheCall, CMId, FnInfo); |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 559 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 560 | return false; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 563 | bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac, |
| 564 | Expr **Args, unsigned NumArgs) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 565 | VariadicCallType CallType = |
| 566 | Method->isVariadic() ? VariadicMethod : VariadicDoesNotApply; |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 567 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 568 | checkCall(Method, Args, NumArgs, Method->param_size(), |
| 569 | /*IsMemberFunction=*/false, |
| 570 | lbrac, Method->getSourceRange(), CallType); |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 571 | |
| 572 | return false; |
| 573 | } |
| 574 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 575 | bool Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall, |
| 576 | const FunctionProtoType *Proto) { |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 577 | const VarDecl *V = dyn_cast<VarDecl>(NDecl); |
| 578 | if (!V) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 579 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 581 | QualType Ty = V->getType(); |
| 582 | if (!Ty->isBlockPointerType()) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 583 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 585 | VariadicCallType CallType = |
| 586 | Proto && Proto->isVariadic() ? VariadicBlock : VariadicDoesNotApply ; |
| 587 | unsigned NumProtoArgs = Proto ? Proto->getNumArgs() : 0; |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 588 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 589 | checkCall(NDecl, TheCall->getArgs(), TheCall->getNumArgs(), |
| 590 | NumProtoArgs, /*IsMemberFunction=*/false, |
| 591 | TheCall->getRParenLoc(), |
| 592 | TheCall->getCallee()->getSourceRange(), CallType); |
| 593 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 594 | return false; |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 597 | ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult, |
| 598 | AtomicExpr::AtomicOp Op) { |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 599 | CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); |
| 600 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 601 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 602 | // All these operations take one of the following forms: |
| 603 | enum { |
| 604 | // C __c11_atomic_init(A *, C) |
| 605 | Init, |
| 606 | // C __c11_atomic_load(A *, int) |
| 607 | Load, |
| 608 | // void __atomic_load(A *, CP, int) |
| 609 | Copy, |
| 610 | // C __c11_atomic_add(A *, M, int) |
| 611 | Arithmetic, |
| 612 | // C __atomic_exchange_n(A *, CP, int) |
| 613 | Xchg, |
| 614 | // void __atomic_exchange(A *, C *, CP, int) |
| 615 | GNUXchg, |
| 616 | // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int) |
| 617 | C11CmpXchg, |
| 618 | // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int) |
| 619 | GNUCmpXchg |
| 620 | } Form = Init; |
| 621 | const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 4, 5, 6 }; |
| 622 | const unsigned NumVals[] = { 1, 0, 1, 1, 1, 2, 2, 3 }; |
| 623 | // where: |
| 624 | // C is an appropriate type, |
| 625 | // A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins, |
| 626 | // CP is C for __c11 builtins and GNU _n builtins and is C * otherwise, |
| 627 | // M is C if C is an integer, and ptrdiff_t if C is a pointer, and |
| 628 | // the int parameters are for orderings. |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 629 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 630 | assert(AtomicExpr::AO__c11_atomic_init == 0 && |
| 631 | AtomicExpr::AO__c11_atomic_fetch_xor + 1 == AtomicExpr::AO__atomic_load |
| 632 | && "need to update code for modified C11 atomics"); |
| 633 | bool IsC11 = Op >= AtomicExpr::AO__c11_atomic_init && |
| 634 | Op <= AtomicExpr::AO__c11_atomic_fetch_xor; |
| 635 | bool IsN = Op == AtomicExpr::AO__atomic_load_n || |
| 636 | Op == AtomicExpr::AO__atomic_store_n || |
| 637 | Op == AtomicExpr::AO__atomic_exchange_n || |
| 638 | Op == AtomicExpr::AO__atomic_compare_exchange_n; |
| 639 | bool IsAddSub = false; |
| 640 | |
| 641 | switch (Op) { |
| 642 | case AtomicExpr::AO__c11_atomic_init: |
| 643 | Form = Init; |
| 644 | break; |
| 645 | |
| 646 | case AtomicExpr::AO__c11_atomic_load: |
| 647 | case AtomicExpr::AO__atomic_load_n: |
| 648 | Form = Load; |
| 649 | break; |
| 650 | |
| 651 | case AtomicExpr::AO__c11_atomic_store: |
| 652 | case AtomicExpr::AO__atomic_load: |
| 653 | case AtomicExpr::AO__atomic_store: |
| 654 | case AtomicExpr::AO__atomic_store_n: |
| 655 | Form = Copy; |
| 656 | break; |
| 657 | |
| 658 | case AtomicExpr::AO__c11_atomic_fetch_add: |
| 659 | case AtomicExpr::AO__c11_atomic_fetch_sub: |
| 660 | case AtomicExpr::AO__atomic_fetch_add: |
| 661 | case AtomicExpr::AO__atomic_fetch_sub: |
| 662 | case AtomicExpr::AO__atomic_add_fetch: |
| 663 | case AtomicExpr::AO__atomic_sub_fetch: |
| 664 | IsAddSub = true; |
| 665 | // Fall through. |
| 666 | case AtomicExpr::AO__c11_atomic_fetch_and: |
| 667 | case AtomicExpr::AO__c11_atomic_fetch_or: |
| 668 | case AtomicExpr::AO__c11_atomic_fetch_xor: |
| 669 | case AtomicExpr::AO__atomic_fetch_and: |
| 670 | case AtomicExpr::AO__atomic_fetch_or: |
| 671 | case AtomicExpr::AO__atomic_fetch_xor: |
Richard Smith | 51b9240 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 672 | case AtomicExpr::AO__atomic_fetch_nand: |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 673 | case AtomicExpr::AO__atomic_and_fetch: |
| 674 | case AtomicExpr::AO__atomic_or_fetch: |
| 675 | case AtomicExpr::AO__atomic_xor_fetch: |
Richard Smith | 51b9240 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 676 | case AtomicExpr::AO__atomic_nand_fetch: |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 677 | Form = Arithmetic; |
| 678 | break; |
| 679 | |
| 680 | case AtomicExpr::AO__c11_atomic_exchange: |
| 681 | case AtomicExpr::AO__atomic_exchange_n: |
| 682 | Form = Xchg; |
| 683 | break; |
| 684 | |
| 685 | case AtomicExpr::AO__atomic_exchange: |
| 686 | Form = GNUXchg; |
| 687 | break; |
| 688 | |
| 689 | case AtomicExpr::AO__c11_atomic_compare_exchange_strong: |
| 690 | case AtomicExpr::AO__c11_atomic_compare_exchange_weak: |
| 691 | Form = C11CmpXchg; |
| 692 | break; |
| 693 | |
| 694 | case AtomicExpr::AO__atomic_compare_exchange: |
| 695 | case AtomicExpr::AO__atomic_compare_exchange_n: |
| 696 | Form = GNUCmpXchg; |
| 697 | break; |
| 698 | } |
| 699 | |
| 700 | // Check we have the right number of arguments. |
| 701 | if (TheCall->getNumArgs() < NumArgs[Form]) { |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 702 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 703 | << 0 << NumArgs[Form] << TheCall->getNumArgs() |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 704 | << TheCall->getCallee()->getSourceRange(); |
| 705 | return ExprError(); |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 706 | } else if (TheCall->getNumArgs() > NumArgs[Form]) { |
| 707 | Diag(TheCall->getArg(NumArgs[Form])->getLocStart(), |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 708 | diag::err_typecheck_call_too_many_args) |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 709 | << 0 << NumArgs[Form] << TheCall->getNumArgs() |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 710 | << TheCall->getCallee()->getSourceRange(); |
| 711 | return ExprError(); |
| 712 | } |
| 713 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 714 | // Inspect the first argument of the atomic operation. |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 715 | Expr *Ptr = TheCall->getArg(0); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 716 | Ptr = DefaultFunctionArrayLvalueConversion(Ptr).get(); |
| 717 | const PointerType *pointerType = Ptr->getType()->getAs<PointerType>(); |
| 718 | if (!pointerType) { |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 719 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 720 | << Ptr->getType() << Ptr->getSourceRange(); |
| 721 | return ExprError(); |
| 722 | } |
| 723 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 724 | // For a __c11 builtin, this should be a pointer to an _Atomic type. |
| 725 | QualType AtomTy = pointerType->getPointeeType(); // 'A' |
| 726 | QualType ValType = AtomTy; // 'C' |
| 727 | if (IsC11) { |
| 728 | if (!AtomTy->isAtomicType()) { |
| 729 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic) |
| 730 | << Ptr->getType() << Ptr->getSourceRange(); |
| 731 | return ExprError(); |
| 732 | } |
| 733 | ValType = AtomTy->getAs<AtomicType>()->getValueType(); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 734 | } |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 735 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 736 | // For an arithmetic operation, the implied arithmetic must be well-formed. |
| 737 | if (Form == Arithmetic) { |
| 738 | // gcc does not enforce these rules for GNU atomics, but we do so for sanity. |
| 739 | if (IsAddSub && !ValType->isIntegerType() && !ValType->isPointerType()) { |
| 740 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) |
| 741 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 742 | return ExprError(); |
| 743 | } |
| 744 | if (!IsAddSub && !ValType->isIntegerType()) { |
| 745 | Diag(DRE->getLocStart(), diag::err_atomic_op_bitwise_needs_atomic_int) |
| 746 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 747 | return ExprError(); |
| 748 | } |
| 749 | } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) { |
| 750 | // For __atomic_*_n operations, the value type must be a scalar integral or |
| 751 | // 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] | 752 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 753 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 754 | return ExprError(); |
| 755 | } |
| 756 | |
| 757 | if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context)) { |
| 758 | // For GNU atomics, require a trivially-copyable type. This is not part of |
| 759 | // the GNU atomics specification, but we enforce it for sanity. |
| 760 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_trivial_copy) |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 761 | << Ptr->getType() << Ptr->getSourceRange(); |
| 762 | return ExprError(); |
| 763 | } |
| 764 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 765 | // FIXME: For any builtin other than a load, the ValType must not be |
| 766 | // const-qualified. |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 767 | |
| 768 | switch (ValType.getObjCLifetime()) { |
| 769 | case Qualifiers::OCL_None: |
| 770 | case Qualifiers::OCL_ExplicitNone: |
| 771 | // okay |
| 772 | break; |
| 773 | |
| 774 | case Qualifiers::OCL_Weak: |
| 775 | case Qualifiers::OCL_Strong: |
| 776 | case Qualifiers::OCL_Autoreleasing: |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 777 | // FIXME: Can this happen? By this point, ValType should be known |
| 778 | // to be trivially copyable. |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 779 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
| 780 | << ValType << Ptr->getSourceRange(); |
| 781 | return ExprError(); |
| 782 | } |
| 783 | |
| 784 | QualType ResultType = ValType; |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 785 | if (Form == Copy || Form == GNUXchg || Form == Init) |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 786 | ResultType = Context.VoidTy; |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 787 | else if (Form == C11CmpXchg || Form == GNUCmpXchg) |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 788 | ResultType = Context.BoolTy; |
| 789 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 790 | // The type of a parameter passed 'by value'. In the GNU atomics, such |
| 791 | // arguments are actually passed as pointers. |
| 792 | QualType ByValType = ValType; // 'CP' |
| 793 | if (!IsC11 && !IsN) |
| 794 | ByValType = Ptr->getType(); |
| 795 | |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 796 | // The first argument --- the pointer --- has a fixed type; we |
| 797 | // deduce the types of the rest of the arguments accordingly. Walk |
| 798 | // the remaining arguments, converting them to the deduced value type. |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 799 | for (unsigned i = 1; i != NumArgs[Form]; ++i) { |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 800 | QualType Ty; |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 801 | if (i < NumVals[Form] + 1) { |
| 802 | switch (i) { |
| 803 | case 1: |
| 804 | // The second argument is the non-atomic operand. For arithmetic, this |
| 805 | // is always passed by value, and for a compare_exchange it is always |
| 806 | // passed by address. For the rest, GNU uses by-address and C11 uses |
| 807 | // by-value. |
| 808 | assert(Form != Load); |
| 809 | if (Form == Init || (Form == Arithmetic && ValType->isIntegerType())) |
| 810 | Ty = ValType; |
| 811 | else if (Form == Copy || Form == Xchg) |
| 812 | Ty = ByValType; |
| 813 | else if (Form == Arithmetic) |
| 814 | Ty = Context.getPointerDiffType(); |
| 815 | else |
| 816 | Ty = Context.getPointerType(ValType.getUnqualifiedType()); |
| 817 | break; |
| 818 | case 2: |
| 819 | // The third argument to compare_exchange / GNU exchange is a |
| 820 | // (pointer to a) desired value. |
| 821 | Ty = ByValType; |
| 822 | break; |
| 823 | case 3: |
| 824 | // The fourth argument to GNU compare_exchange is a 'weak' flag. |
| 825 | Ty = Context.BoolTy; |
| 826 | break; |
| 827 | } |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 828 | } else { |
| 829 | // The order(s) are always converted to int. |
| 830 | Ty = Context.IntTy; |
| 831 | } |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 832 | |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 833 | InitializedEntity Entity = |
| 834 | InitializedEntity::InitializeParameter(Context, Ty, false); |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 835 | ExprResult Arg = TheCall->getArg(i); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 836 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 837 | if (Arg.isInvalid()) |
| 838 | return true; |
| 839 | TheCall->setArg(i, Arg.get()); |
| 840 | } |
| 841 | |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 842 | // Permute the arguments into a 'consistent' order. |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 843 | SmallVector<Expr*, 5> SubExprs; |
| 844 | SubExprs.push_back(Ptr); |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 845 | switch (Form) { |
| 846 | case Init: |
| 847 | // Note, AtomicExpr::getVal1() has a special case for this atomic. |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 848 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 849 | break; |
| 850 | case Load: |
| 851 | SubExprs.push_back(TheCall->getArg(1)); // Order |
| 852 | break; |
| 853 | case Copy: |
| 854 | case Arithmetic: |
| 855 | case Xchg: |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 856 | SubExprs.push_back(TheCall->getArg(2)); // Order |
| 857 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 858 | break; |
| 859 | case GNUXchg: |
| 860 | // Note, AtomicExpr::getVal2() has a special case for this atomic. |
| 861 | SubExprs.push_back(TheCall->getArg(3)); // Order |
| 862 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
| 863 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
| 864 | break; |
| 865 | case C11CmpXchg: |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 866 | SubExprs.push_back(TheCall->getArg(3)); // Order |
| 867 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 868 | SubExprs.push_back(TheCall->getArg(4)); // OrderFail |
David Chisnall | 2ebb98a | 2012-03-29 17:58:59 +0000 | [diff] [blame] | 869 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 870 | break; |
| 871 | case GNUCmpXchg: |
| 872 | SubExprs.push_back(TheCall->getArg(4)); // Order |
| 873 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
| 874 | SubExprs.push_back(TheCall->getArg(5)); // OrderFail |
| 875 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
| 876 | SubExprs.push_back(TheCall->getArg(3)); // Weak |
| 877 | break; |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 878 | } |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 879 | |
| 880 | return Owned(new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(), |
| 881 | SubExprs.data(), SubExprs.size(), |
| 882 | ResultType, Op, |
| 883 | TheCall->getRParenLoc())); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 887 | /// checkBuiltinArgument - Given a call to a builtin function, perform |
| 888 | /// normal type-checking on the given argument, updating the call in |
| 889 | /// place. This is useful when a builtin function requires custom |
| 890 | /// type-checking for some of its arguments but not necessarily all of |
| 891 | /// them. |
| 892 | /// |
| 893 | /// Returns true on error. |
| 894 | static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) { |
| 895 | FunctionDecl *Fn = E->getDirectCallee(); |
| 896 | assert(Fn && "builtin call without direct callee!"); |
| 897 | |
| 898 | ParmVarDecl *Param = Fn->getParamDecl(ArgIndex); |
| 899 | InitializedEntity Entity = |
| 900 | InitializedEntity::InitializeParameter(S.Context, Param); |
| 901 | |
| 902 | ExprResult Arg = E->getArg(0); |
| 903 | Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 904 | if (Arg.isInvalid()) |
| 905 | return true; |
| 906 | |
| 907 | E->setArg(ArgIndex, Arg.take()); |
| 908 | return false; |
| 909 | } |
| 910 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 911 | /// SemaBuiltinAtomicOverloaded - We have a call to a function like |
| 912 | /// __sync_fetch_and_add, which is an overloaded function based on the pointer |
| 913 | /// type of its first argument. The main ActOnCallExpr routines have already |
| 914 | /// promoted the types of arguments because all of these calls are prototyped as |
| 915 | /// void(...). |
| 916 | /// |
| 917 | /// This function goes through and does final semantic checking for these |
| 918 | /// builtins, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 919 | ExprResult |
| 920 | Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) { |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 921 | CallExpr *TheCall = (CallExpr *)TheCallResult.get(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 922 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 923 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 924 | |
| 925 | // 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] | 926 | if (TheCall->getNumArgs() < 1) { |
| 927 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 928 | << 0 << 1 << TheCall->getNumArgs() |
| 929 | << TheCall->getCallee()->getSourceRange(); |
| 930 | return ExprError(); |
| 931 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 932 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 933 | // Inspect the first argument of the atomic builtin. This should always be |
| 934 | // a pointer type, whose element is an integral scalar or pointer type. |
| 935 | // Because it is a pointer type, we don't have to worry about any implicit |
| 936 | // casts here. |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 937 | // FIXME: We don't allow floating point scalars as input. |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 938 | Expr *FirstArg = TheCall->getArg(0); |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 939 | ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg); |
| 940 | if (FirstArgResult.isInvalid()) |
| 941 | return ExprError(); |
| 942 | FirstArg = FirstArgResult.take(); |
| 943 | TheCall->setArg(0, FirstArg); |
| 944 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 945 | const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>(); |
| 946 | if (!pointerType) { |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 947 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
| 948 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 949 | return ExprError(); |
| 950 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 952 | QualType ValType = pointerType->getPointeeType(); |
Chris Lattner | dd5fa7a | 2010-09-17 21:12:38 +0000 | [diff] [blame] | 953 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 954 | !ValType->isBlockPointerType()) { |
| 955 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr) |
| 956 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 957 | return ExprError(); |
| 958 | } |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 959 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 960 | switch (ValType.getObjCLifetime()) { |
| 961 | case Qualifiers::OCL_None: |
| 962 | case Qualifiers::OCL_ExplicitNone: |
| 963 | // okay |
| 964 | break; |
| 965 | |
| 966 | case Qualifiers::OCL_Weak: |
| 967 | case Qualifiers::OCL_Strong: |
| 968 | case Qualifiers::OCL_Autoreleasing: |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 969 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 970 | << ValType << FirstArg->getSourceRange(); |
| 971 | return ExprError(); |
| 972 | } |
| 973 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 974 | // Strip any qualifiers off ValType. |
| 975 | ValType = ValType.getUnqualifiedType(); |
| 976 | |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 977 | // The majority of builtins return a value, but a few have special return |
| 978 | // types, so allow them to override appropriately below. |
| 979 | QualType ResultType = ValType; |
| 980 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 981 | // We need to figure out which concrete builtin this maps onto. For example, |
| 982 | // __sync_fetch_and_add with a 2 byte object turns into |
| 983 | // __sync_fetch_and_add_2. |
| 984 | #define BUILTIN_ROW(x) \ |
| 985 | { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ |
| 986 | Builtin::BI##x##_8, Builtin::BI##x##_16 } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 987 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 988 | static const unsigned BuiltinIndices[][5] = { |
| 989 | BUILTIN_ROW(__sync_fetch_and_add), |
| 990 | BUILTIN_ROW(__sync_fetch_and_sub), |
| 991 | BUILTIN_ROW(__sync_fetch_and_or), |
| 992 | BUILTIN_ROW(__sync_fetch_and_and), |
| 993 | BUILTIN_ROW(__sync_fetch_and_xor), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 994 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 995 | BUILTIN_ROW(__sync_add_and_fetch), |
| 996 | BUILTIN_ROW(__sync_sub_and_fetch), |
| 997 | BUILTIN_ROW(__sync_and_and_fetch), |
| 998 | BUILTIN_ROW(__sync_or_and_fetch), |
| 999 | BUILTIN_ROW(__sync_xor_and_fetch), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1000 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1001 | BUILTIN_ROW(__sync_val_compare_and_swap), |
| 1002 | BUILTIN_ROW(__sync_bool_compare_and_swap), |
| 1003 | BUILTIN_ROW(__sync_lock_test_and_set), |
Chris Lattner | 23aa9c8 | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 1004 | BUILTIN_ROW(__sync_lock_release), |
| 1005 | BUILTIN_ROW(__sync_swap) |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1006 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1007 | #undef BUILTIN_ROW |
| 1008 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1009 | // Determine the index of the size. |
| 1010 | unsigned SizeIndex; |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1011 | switch (Context.getTypeSizeInChars(ValType).getQuantity()) { |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1012 | case 1: SizeIndex = 0; break; |
| 1013 | case 2: SizeIndex = 1; break; |
| 1014 | case 4: SizeIndex = 2; break; |
| 1015 | case 8: SizeIndex = 3; break; |
| 1016 | case 16: SizeIndex = 4; break; |
| 1017 | default: |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1018 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size) |
| 1019 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 1020 | return ExprError(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1021 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1023 | // Each of these builtins has one pointer argument, followed by some number of |
| 1024 | // values (0, 1 or 2) followed by a potentially empty varags list of stuff |
| 1025 | // that we ignore. Find out which row of BuiltinIndices to read from as well |
| 1026 | // as the number of fixed args. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 1027 | unsigned BuiltinID = FDecl->getBuiltinID(); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1028 | unsigned BuiltinIndex, NumFixed = 1; |
| 1029 | switch (BuiltinID) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1030 | default: llvm_unreachable("Unknown overloaded atomic builtin!"); |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1031 | case Builtin::BI__sync_fetch_and_add: |
| 1032 | case Builtin::BI__sync_fetch_and_add_1: |
| 1033 | case Builtin::BI__sync_fetch_and_add_2: |
| 1034 | case Builtin::BI__sync_fetch_and_add_4: |
| 1035 | case Builtin::BI__sync_fetch_and_add_8: |
| 1036 | case Builtin::BI__sync_fetch_and_add_16: |
| 1037 | BuiltinIndex = 0; |
| 1038 | break; |
| 1039 | |
| 1040 | case Builtin::BI__sync_fetch_and_sub: |
| 1041 | case Builtin::BI__sync_fetch_and_sub_1: |
| 1042 | case Builtin::BI__sync_fetch_and_sub_2: |
| 1043 | case Builtin::BI__sync_fetch_and_sub_4: |
| 1044 | case Builtin::BI__sync_fetch_and_sub_8: |
| 1045 | case Builtin::BI__sync_fetch_and_sub_16: |
| 1046 | BuiltinIndex = 1; |
| 1047 | break; |
| 1048 | |
| 1049 | case Builtin::BI__sync_fetch_and_or: |
| 1050 | case Builtin::BI__sync_fetch_and_or_1: |
| 1051 | case Builtin::BI__sync_fetch_and_or_2: |
| 1052 | case Builtin::BI__sync_fetch_and_or_4: |
| 1053 | case Builtin::BI__sync_fetch_and_or_8: |
| 1054 | case Builtin::BI__sync_fetch_and_or_16: |
| 1055 | BuiltinIndex = 2; |
| 1056 | break; |
| 1057 | |
| 1058 | case Builtin::BI__sync_fetch_and_and: |
| 1059 | case Builtin::BI__sync_fetch_and_and_1: |
| 1060 | case Builtin::BI__sync_fetch_and_and_2: |
| 1061 | case Builtin::BI__sync_fetch_and_and_4: |
| 1062 | case Builtin::BI__sync_fetch_and_and_8: |
| 1063 | case Builtin::BI__sync_fetch_and_and_16: |
| 1064 | BuiltinIndex = 3; |
| 1065 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1067 | case Builtin::BI__sync_fetch_and_xor: |
| 1068 | case Builtin::BI__sync_fetch_and_xor_1: |
| 1069 | case Builtin::BI__sync_fetch_and_xor_2: |
| 1070 | case Builtin::BI__sync_fetch_and_xor_4: |
| 1071 | case Builtin::BI__sync_fetch_and_xor_8: |
| 1072 | case Builtin::BI__sync_fetch_and_xor_16: |
| 1073 | BuiltinIndex = 4; |
| 1074 | break; |
| 1075 | |
| 1076 | case Builtin::BI__sync_add_and_fetch: |
| 1077 | case Builtin::BI__sync_add_and_fetch_1: |
| 1078 | case Builtin::BI__sync_add_and_fetch_2: |
| 1079 | case Builtin::BI__sync_add_and_fetch_4: |
| 1080 | case Builtin::BI__sync_add_and_fetch_8: |
| 1081 | case Builtin::BI__sync_add_and_fetch_16: |
| 1082 | BuiltinIndex = 5; |
| 1083 | break; |
| 1084 | |
| 1085 | case Builtin::BI__sync_sub_and_fetch: |
| 1086 | case Builtin::BI__sync_sub_and_fetch_1: |
| 1087 | case Builtin::BI__sync_sub_and_fetch_2: |
| 1088 | case Builtin::BI__sync_sub_and_fetch_4: |
| 1089 | case Builtin::BI__sync_sub_and_fetch_8: |
| 1090 | case Builtin::BI__sync_sub_and_fetch_16: |
| 1091 | BuiltinIndex = 6; |
| 1092 | break; |
| 1093 | |
| 1094 | case Builtin::BI__sync_and_and_fetch: |
| 1095 | case Builtin::BI__sync_and_and_fetch_1: |
| 1096 | case Builtin::BI__sync_and_and_fetch_2: |
| 1097 | case Builtin::BI__sync_and_and_fetch_4: |
| 1098 | case Builtin::BI__sync_and_and_fetch_8: |
| 1099 | case Builtin::BI__sync_and_and_fetch_16: |
| 1100 | BuiltinIndex = 7; |
| 1101 | break; |
| 1102 | |
| 1103 | case Builtin::BI__sync_or_and_fetch: |
| 1104 | case Builtin::BI__sync_or_and_fetch_1: |
| 1105 | case Builtin::BI__sync_or_and_fetch_2: |
| 1106 | case Builtin::BI__sync_or_and_fetch_4: |
| 1107 | case Builtin::BI__sync_or_and_fetch_8: |
| 1108 | case Builtin::BI__sync_or_and_fetch_16: |
| 1109 | BuiltinIndex = 8; |
| 1110 | break; |
| 1111 | |
| 1112 | case Builtin::BI__sync_xor_and_fetch: |
| 1113 | case Builtin::BI__sync_xor_and_fetch_1: |
| 1114 | case Builtin::BI__sync_xor_and_fetch_2: |
| 1115 | case Builtin::BI__sync_xor_and_fetch_4: |
| 1116 | case Builtin::BI__sync_xor_and_fetch_8: |
| 1117 | case Builtin::BI__sync_xor_and_fetch_16: |
| 1118 | BuiltinIndex = 9; |
| 1119 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1120 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1121 | case Builtin::BI__sync_val_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1122 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 1123 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 1124 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 1125 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 1126 | case Builtin::BI__sync_val_compare_and_swap_16: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 1127 | BuiltinIndex = 10; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1128 | NumFixed = 2; |
| 1129 | break; |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1130 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1131 | case Builtin::BI__sync_bool_compare_and_swap: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1132 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 1133 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 1134 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 1135 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 1136 | case Builtin::BI__sync_bool_compare_and_swap_16: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 1137 | BuiltinIndex = 11; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1138 | NumFixed = 2; |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1139 | ResultType = Context.BoolTy; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1140 | break; |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1141 | |
| 1142 | case Builtin::BI__sync_lock_test_and_set: |
| 1143 | case Builtin::BI__sync_lock_test_and_set_1: |
| 1144 | case Builtin::BI__sync_lock_test_and_set_2: |
| 1145 | case Builtin::BI__sync_lock_test_and_set_4: |
| 1146 | case Builtin::BI__sync_lock_test_and_set_8: |
| 1147 | case Builtin::BI__sync_lock_test_and_set_16: |
| 1148 | BuiltinIndex = 12; |
| 1149 | break; |
| 1150 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1151 | case Builtin::BI__sync_lock_release: |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1152 | case Builtin::BI__sync_lock_release_1: |
| 1153 | case Builtin::BI__sync_lock_release_2: |
| 1154 | case Builtin::BI__sync_lock_release_4: |
| 1155 | case Builtin::BI__sync_lock_release_8: |
| 1156 | case Builtin::BI__sync_lock_release_16: |
Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 1157 | BuiltinIndex = 13; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1158 | NumFixed = 0; |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1159 | ResultType = Context.VoidTy; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1160 | break; |
Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1161 | |
| 1162 | case Builtin::BI__sync_swap: |
| 1163 | case Builtin::BI__sync_swap_1: |
| 1164 | case Builtin::BI__sync_swap_2: |
| 1165 | case Builtin::BI__sync_swap_4: |
| 1166 | case Builtin::BI__sync_swap_8: |
| 1167 | case Builtin::BI__sync_swap_16: |
| 1168 | BuiltinIndex = 14; |
| 1169 | break; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1170 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1171 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1172 | // Now that we know how many fixed arguments we expect, first check that we |
| 1173 | // have at least that many. |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1174 | if (TheCall->getNumArgs() < 1+NumFixed) { |
| 1175 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 1176 | << 0 << 1+NumFixed << TheCall->getNumArgs() |
| 1177 | << TheCall->getCallee()->getSourceRange(); |
| 1178 | return ExprError(); |
| 1179 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | |
Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 1181 | // Get the decl for the concrete builtin from this, we can tell what the |
| 1182 | // concrete integer type we should convert to is. |
| 1183 | unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; |
| 1184 | const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID); |
| 1185 | IdentifierInfo *NewBuiltinII = PP.getIdentifierInfo(NewBuiltinName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1186 | FunctionDecl *NewBuiltinDecl = |
Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 1187 | cast<FunctionDecl>(LazilyCreateBuiltin(NewBuiltinII, NewBuiltinID, |
| 1188 | TUScope, false, DRE->getLocStart())); |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1189 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1190 | // The first argument --- the pointer --- has a fixed type; we |
| 1191 | // deduce the types of the rest of the arguments accordingly. Walk |
| 1192 | // the remaining arguments, converting them to the deduced value type. |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1193 | for (unsigned i = 0; i != NumFixed; ++i) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1194 | ExprResult Arg = TheCall->getArg(i+1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1195 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1196 | // GCC does an implicit conversion to the pointer or integer ValType. This |
| 1197 | // 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] | 1198 | // Initialize the argument. |
| 1199 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 1200 | ValType, /*consume*/ false); |
| 1201 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1202 | if (Arg.isInvalid()) |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1203 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1205 | // Okay, we have something that *can* be converted to the right type. Check |
| 1206 | // to see if there is a potentially weird extension going on here. This can |
| 1207 | // happen when you do an atomic operation on something like an char* and |
| 1208 | // pass in 42. The 42 gets converted to char. This is even more strange |
| 1209 | // for things like 45.123 -> char, etc. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1210 | // FIXME: Do this check. |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1211 | TheCall->setArg(i+1, Arg.take()); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1212 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1214 | ASTContext& Context = this->getASTContext(); |
| 1215 | |
| 1216 | // Create a new DeclRefExpr to refer to the new decl. |
| 1217 | DeclRefExpr* NewDRE = DeclRefExpr::Create( |
| 1218 | Context, |
| 1219 | DRE->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1220 | SourceLocation(), |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1221 | NewBuiltinDecl, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1222 | /*enclosing*/ false, |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1223 | DRE->getLocation(), |
| 1224 | NewBuiltinDecl->getType(), |
| 1225 | DRE->getValueKind()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1226 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1227 | // Set the callee in the CallExpr. |
| 1228 | // FIXME: This leaks the original parens and implicit casts. |
Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1229 | ExprResult PromotedCall = UsualUnaryConversions(NewDRE); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1230 | if (PromotedCall.isInvalid()) |
| 1231 | return ExprError(); |
| 1232 | TheCall->setCallee(PromotedCall.take()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1233 | |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 1234 | // Change the result type of the call to match the original value type. This |
| 1235 | // is arbitrary, but the codegen for these builtins ins design to handle it |
| 1236 | // gracefully. |
Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1237 | TheCall->setType(ResultType); |
Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1238 | |
| 1239 | return move(TheCallResult); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 1242 | /// CheckObjCString - Checks that the argument to the builtin |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1243 | /// CFString constructor is correct |
Steve Naroff | fd94262 | 2009-04-13 20:26:29 +0000 | [diff] [blame] | 1244 | /// Note: It might also make sense to do the UTF-16 conversion here (would |
| 1245 | /// simplify the backend). |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 1246 | bool Sema::CheckObjCString(Expr *Arg) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1247 | Arg = Arg->IgnoreParenCasts(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1248 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); |
| 1249 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1250 | if (!Literal || !Literal->isAscii()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1251 | Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 1252 | << Arg->getSourceRange(); |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 1253 | return true; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1254 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1255 | |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1256 | if (Literal->containsNonAsciiOrNull()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1257 | StringRef String = Literal->getString(); |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1258 | unsigned NumBytes = String.size(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1259 | SmallVector<UTF16, 128> ToBuf(NumBytes); |
Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1260 | const UTF8 *FromPtr = (UTF8 *)String.data(); |
| 1261 | UTF16 *ToPtr = &ToBuf[0]; |
| 1262 | |
| 1263 | ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, |
| 1264 | &ToPtr, ToPtr + NumBytes, |
| 1265 | strictConversion); |
| 1266 | // Check for conversion failure. |
| 1267 | if (Result != conversionOK) |
| 1268 | Diag(Arg->getLocStart(), |
| 1269 | diag::warn_cfstring_truncated) << Arg->getSourceRange(); |
| 1270 | } |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 1271 | return false; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1274 | /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity. |
| 1275 | /// Emit an error and return true on failure, return false on success. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1276 | bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) { |
| 1277 | Expr *Fn = TheCall->getCallee(); |
| 1278 | if (TheCall->getNumArgs() > 2) { |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 1279 | Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1280 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1281 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
| 1282 | << Fn->getSourceRange() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1283 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1284 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1285 | return true; |
| 1286 | } |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 1287 | |
| 1288 | if (TheCall->getNumArgs() < 2) { |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1289 | return Diag(TheCall->getLocEnd(), |
| 1290 | diag::err_typecheck_call_too_few_args_at_least) |
| 1291 | << 0 /*function call*/ << 2 << TheCall->getNumArgs(); |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 1294 | // Type-check the first argument normally. |
| 1295 | if (checkBuiltinArgument(*this, TheCall, 0)) |
| 1296 | return true; |
| 1297 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1298 | // Determine whether the current function is variadic or not. |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1299 | BlockScopeInfo *CurBlock = getCurBlock(); |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1300 | bool isVariadic; |
Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 1301 | if (CurBlock) |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 1302 | isVariadic = CurBlock->TheDecl->isVariadic(); |
Ted Kremenek | 9498d38 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 1303 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
| 1304 | isVariadic = FD->isVariadic(); |
| 1305 | else |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1306 | isVariadic = getCurMethodDecl()->isVariadic(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1307 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1308 | if (!isVariadic) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1309 | Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function); |
| 1310 | return true; |
| 1311 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1313 | // Verify that the second argument to the builtin is the last argument of the |
| 1314 | // current function or method. |
| 1315 | bool SecondArgIsLastNamedArgument = false; |
Anders Carlsson | e2c1410 | 2008-02-13 01:22:59 +0000 | [diff] [blame] | 1316 | const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1317 | |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 1318 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { |
| 1319 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1320 | // FIXME: This isn't correct for methods (results in bogus warning). |
| 1321 | // Get the last formal in the current function. |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 1322 | const ParmVarDecl *LastArg; |
Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 1323 | if (CurBlock) |
| 1324 | LastArg = *(CurBlock->TheDecl->param_end()-1); |
| 1325 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1326 | LastArg = *(FD->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1327 | else |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1328 | LastArg = *(getCurMethodDecl()->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1329 | SecondArgIsLastNamedArgument = PV == LastArg; |
| 1330 | } |
| 1331 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1332 | |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1333 | if (!SecondArgIsLastNamedArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1334 | Diag(TheCall->getArg(1)->getLocStart(), |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1335 | diag::warn_second_parameter_of_va_start_not_last_named_argument); |
| 1336 | return false; |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 1337 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1338 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1339 | /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and |
| 1340 | /// friends. This is declared to take (...), so we have to check everything. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1341 | bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { |
| 1342 | if (TheCall->getNumArgs() < 2) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 1343 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1344 | << 0 << 2 << TheCall->getNumArgs()/*function call*/; |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1345 | if (TheCall->getNumArgs() > 2) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1346 | return Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1347 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1348 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1349 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
| 1350 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1351 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1352 | ExprResult OrigArg0 = TheCall->getArg(0); |
| 1353 | ExprResult OrigArg1 = TheCall->getArg(1); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1354 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1355 | // Do standard promotions between the two arguments, returning their common |
| 1356 | // type. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1357 | QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1358 | if (OrigArg0.isInvalid() || OrigArg1.isInvalid()) |
| 1359 | return true; |
Daniel Dunbar | 403bc2b | 2009-02-19 19:28:43 +0000 | [diff] [blame] | 1360 | |
| 1361 | // Make sure any conversions are pushed back into the call; this is |
| 1362 | // type safe since unordered compare builtins are declared as "_Bool |
| 1363 | // foo(...)". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1364 | TheCall->setArg(0, OrigArg0.get()); |
| 1365 | TheCall->setArg(1, OrigArg1.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1366 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1367 | if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent()) |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1368 | return false; |
| 1369 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1370 | // If the common type isn't a real floating type, then the arguments were |
| 1371 | // invalid for this operation. |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 1372 | if (Res.isNull() || !Res->isRealFloatingType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1373 | return Diag(OrigArg0.get()->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1374 | diag::err_typecheck_call_invalid_ordered_compare) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1375 | << OrigArg0.get()->getType() << OrigArg1.get()->getType() |
| 1376 | << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1378 | return false; |
| 1379 | } |
| 1380 | |
Benjamin Kramer | e771a7a | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 1381 | /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like |
| 1382 | /// __builtin_isnan and friends. This is declared to take (...), so we have |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1383 | /// to check everything. We expect the last argument to be a floating point |
| 1384 | /// value. |
| 1385 | bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) { |
| 1386 | if (TheCall->getNumArgs() < NumArgs) |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1387 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1388 | << 0 << NumArgs << TheCall->getNumArgs()/*function call*/; |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1389 | if (TheCall->getNumArgs() > NumArgs) |
| 1390 | return Diag(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1391 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1392 | << 0 /*function call*/ << NumArgs << TheCall->getNumArgs() |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1393 | << SourceRange(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1394 | (*(TheCall->arg_end()-1))->getLocEnd()); |
| 1395 | |
Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1396 | Expr *OrigArg = TheCall->getArg(NumArgs-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1397 | |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1398 | if (OrigArg->isTypeDependent()) |
| 1399 | return false; |
| 1400 | |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1401 | // This operation requires a non-_Complex floating-point number. |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1402 | if (!OrigArg->getType()->isRealFloatingType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1403 | return Diag(OrigArg->getLocStart(), |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1404 | diag::err_typecheck_call_invalid_unary_fp) |
| 1405 | << OrigArg->getType() << OrigArg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1406 | |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1407 | // If this is an implicit conversion from float -> double, remove it. |
| 1408 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) { |
| 1409 | Expr *CastArg = Cast->getSubExpr(); |
| 1410 | if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) { |
| 1411 | assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) && |
| 1412 | "promotion from float to double is the only expected cast here"); |
| 1413 | Cast->setSubExpr(0); |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1414 | TheCall->setArg(NumArgs-1, CastArg); |
Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1415 | } |
| 1416 | } |
| 1417 | |
Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1418 | return false; |
| 1419 | } |
| 1420 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1421 | /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. |
| 1422 | // This is declared to take (...), so we have to check everything. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1423 | ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1424 | if (TheCall->getNumArgs() < 2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1425 | return ExprError(Diag(TheCall->getLocEnd(), |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1426 | diag::err_typecheck_call_too_few_args_at_least) |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1427 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1428 | << TheCall->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1429 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1430 | // Determine which of the following types of shufflevector we're checking: |
| 1431 | // 1) unary, vector mask: (lhs, mask) |
| 1432 | // 2) binary, vector mask: (lhs, rhs, mask) |
| 1433 | // 3) binary, scalar mask: (lhs, rhs, index, ..., index) |
| 1434 | QualType resType = TheCall->getArg(0)->getType(); |
| 1435 | unsigned numElements = 0; |
| 1436 | |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1437 | if (!TheCall->getArg(0)->isTypeDependent() && |
| 1438 | !TheCall->getArg(1)->isTypeDependent()) { |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1439 | QualType LHSType = TheCall->getArg(0)->getType(); |
| 1440 | QualType RHSType = TheCall->getArg(1)->getType(); |
| 1441 | |
| 1442 | if (!LHSType->isVectorType() || !RHSType->isVectorType()) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1443 | Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1444 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1445 | TheCall->getArg(1)->getLocEnd()); |
| 1446 | return ExprError(); |
| 1447 | } |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1448 | |
| 1449 | numElements = LHSType->getAs<VectorType>()->getNumElements(); |
| 1450 | unsigned numResElements = TheCall->getNumArgs() - 2; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1451 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1452 | // Check to see if we have a call with 2 vector arguments, the unary shuffle |
| 1453 | // with mask. If so, verify that RHS is an integer vector type with the |
| 1454 | // same number of elts as lhs. |
| 1455 | if (TheCall->getNumArgs() == 2) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 1456 | if (!RHSType->hasIntegerRepresentation() || |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1457 | RHSType->getAs<VectorType>()->getNumElements() != numElements) |
| 1458 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
| 1459 | << SourceRange(TheCall->getArg(1)->getLocStart(), |
| 1460 | TheCall->getArg(1)->getLocEnd()); |
| 1461 | numResElements = numElements; |
| 1462 | } |
| 1463 | else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1464 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1465 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1466 | TheCall->getArg(1)->getLocEnd()); |
| 1467 | return ExprError(); |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1468 | } else if (numElements != numResElements) { |
| 1469 | QualType eltType = LHSType->getAs<VectorType>()->getElementType(); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1470 | resType = Context.getVectorType(eltType, numResElements, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1471 | VectorType::GenericVector); |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1472 | } |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1476 | if (TheCall->getArg(i)->isTypeDependent() || |
| 1477 | TheCall->getArg(i)->isValueDependent()) |
| 1478 | continue; |
| 1479 | |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1480 | llvm::APSInt Result(32); |
| 1481 | if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) |
| 1482 | return ExprError(Diag(TheCall->getLocStart(), |
| 1483 | diag::err_shufflevector_nonconstant_argument) |
| 1484 | << TheCall->getArg(i)->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1485 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 1486 | if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1487 | return ExprError(Diag(TheCall->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1488 | diag::err_shufflevector_argument_too_large) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1489 | << TheCall->getArg(i)->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1492 | SmallVector<Expr*, 32> exprs; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1493 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 1494 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1495 | exprs.push_back(TheCall->getArg(i)); |
| 1496 | TheCall->setArg(i, 0); |
| 1497 | } |
| 1498 | |
Nate Begeman | a88dc30 | 2009-08-12 02:10:25 +0000 | [diff] [blame] | 1499 | return Owned(new (Context) ShuffleVectorExpr(Context, exprs.begin(), |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1500 | exprs.size(), resType, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1501 | TheCall->getCallee()->getLocStart(), |
| 1502 | TheCall->getRParenLoc())); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1503 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1504 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1505 | /// SemaBuiltinPrefetch - Handle __builtin_prefetch. |
| 1506 | // This is declared to take (const void*, ...) and can take two |
| 1507 | // optional constant int args. |
| 1508 | bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1509 | unsigned NumArgs = TheCall->getNumArgs(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1510 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1511 | if (NumArgs > 3) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1512 | return Diag(TheCall->getLocEnd(), |
| 1513 | diag::err_typecheck_call_too_many_args_at_most) |
| 1514 | << 0 /*function call*/ << 3 << NumArgs |
| 1515 | << TheCall->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1516 | |
| 1517 | // Argument 0 is checked for us and the remaining arguments must be |
| 1518 | // constant integers. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1519 | for (unsigned i = 1; i != NumArgs; ++i) { |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1520 | Expr *Arg = TheCall->getArg(i); |
Douglas Gregor | 592a423 | 2012-06-29 01:05:22 +0000 | [diff] [blame] | 1521 | |
| 1522 | // We can't check the value of a dependent argument. |
| 1523 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 1524 | continue; |
| 1525 | |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1526 | llvm::APSInt Result; |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1527 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 1528 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1529 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1530 | // FIXME: gcc issues a warning and rewrites these to 0. These |
| 1531 | // seems especially odd for the third argument since the default |
| 1532 | // is 3. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1533 | if (i == 1) { |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1534 | if (Result.getLimitedValue() > 1) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1535 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1536 | << "0" << "1" << Arg->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1537 | } else { |
Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1538 | if (Result.getLimitedValue() > 3) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1539 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1540 | << "0" << "3" << Arg->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1541 | } |
| 1542 | } |
| 1543 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1544 | return false; |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1547 | /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr |
| 1548 | /// TheCall is a constant expression. |
| 1549 | bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum, |
| 1550 | llvm::APSInt &Result) { |
| 1551 | Expr *Arg = TheCall->getArg(ArgNum); |
| 1552 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 1553 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 1554 | |
| 1555 | if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; |
| 1556 | |
| 1557 | if (!Arg->isIntegerConstantExpr(Result, Context)) |
| 1558 | return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type) |
Eric Christopher | 5e89655 | 2010-04-19 18:23:02 +0000 | [diff] [blame] | 1559 | << FDecl->getDeclName() << Arg->getSourceRange(); |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1560 | |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1561 | return false; |
| 1562 | } |
| 1563 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1564 | /// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr, |
| 1565 | /// int type). This simply type checks that type is one of the defined |
| 1566 | /// constants (0-3). |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1567 | // For compatibility check 0-3, llvm only handles 0 and 2. |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1568 | bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) { |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1569 | llvm::APSInt Result; |
Douglas Gregor | 592a423 | 2012-06-29 01:05:22 +0000 | [diff] [blame] | 1570 | |
| 1571 | // We can't check the value of a dependent argument. |
| 1572 | if (TheCall->getArg(1)->isTypeDependent() || |
| 1573 | TheCall->getArg(1)->isValueDependent()) |
| 1574 | return false; |
| 1575 | |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1576 | // Check constant-ness first. |
| 1577 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 1578 | return true; |
| 1579 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1580 | Expr *Arg = TheCall->getArg(1); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1581 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1582 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 1583 | << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | return false; |
| 1587 | } |
| 1588 | |
Eli Friedman | 586d6a8 | 2009-05-03 06:04:26 +0000 | [diff] [blame] | 1589 | /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 1590 | /// This checks that val is a constant 1. |
| 1591 | bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { |
| 1592 | Expr *Arg = TheCall->getArg(1); |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1593 | llvm::APSInt Result; |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1594 | |
Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1595 | // TODO: This is less than ideal. Overload this to take a value. |
| 1596 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 1597 | return true; |
| 1598 | |
| 1599 | if (Result != 1) |
Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 1600 | return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val) |
| 1601 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
| 1602 | |
| 1603 | return false; |
| 1604 | } |
| 1605 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1606 | // Determine if an expression is a string literal or constant string. |
| 1607 | // If this function returns false on the arguments to a function expecting a |
| 1608 | // format string, we will usually need to emit a warning. |
| 1609 | // True string literals are then checked by CheckFormatString. |
| 1610 | Sema::StringLiteralCheckType |
| 1611 | Sema::checkFormatStringExpr(const Expr *E, Expr **Args, |
| 1612 | unsigned NumArgs, bool HasVAListArg, |
| 1613 | unsigned format_idx, unsigned firstDataArg, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1614 | FormatStringType Type, VariadicCallType CallType, |
| 1615 | bool inFunctionCall) { |
Ted Kremenek | 4fe6441 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 1616 | tryAgain: |
Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1617 | if (E->isTypeDependent() || E->isValueDependent()) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1618 | return SLCT_NotALiteral; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1619 | |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1620 | E = E->IgnoreParenCasts(); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1621 | |
David Blaikie | a73cdcb | 2012-02-10 21:07:25 +0000 | [diff] [blame] | 1622 | if (E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 1623 | // Technically -Wformat-nonliteral does not warn about this case. |
| 1624 | // The behavior of printf and friends in this case is implementation |
| 1625 | // dependent. Ideally if the format string cannot be null then |
| 1626 | // it should have a 'nonnull' attribute in the function prototype. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1627 | return SLCT_CheckedLiteral; |
David Blaikie | a73cdcb | 2012-02-10 21:07:25 +0000 | [diff] [blame] | 1628 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1629 | switch (E->getStmtClass()) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1630 | case Stmt::BinaryConditionalOperatorClass: |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1631 | case Stmt::ConditionalOperatorClass: { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1632 | // The expression is a literal if both sub-expressions were, and it was |
| 1633 | // completely checked only if both sub-expressions were checked. |
| 1634 | const AbstractConditionalOperator *C = |
| 1635 | cast<AbstractConditionalOperator>(E); |
| 1636 | StringLiteralCheckType Left = |
| 1637 | checkFormatStringExpr(C->getTrueExpr(), Args, NumArgs, |
| 1638 | HasVAListArg, format_idx, firstDataArg, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1639 | Type, CallType, inFunctionCall); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1640 | if (Left == SLCT_NotALiteral) |
| 1641 | return SLCT_NotALiteral; |
| 1642 | StringLiteralCheckType Right = |
| 1643 | checkFormatStringExpr(C->getFalseExpr(), Args, NumArgs, |
| 1644 | HasVAListArg, format_idx, firstDataArg, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1645 | Type, CallType, inFunctionCall); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1646 | return Left < Right ? Left : Right; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | case Stmt::ImplicitCastExprClass: { |
Ted Kremenek | 4fe6441 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 1650 | E = cast<ImplicitCastExpr>(E)->getSubExpr(); |
| 1651 | goto tryAgain; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1654 | case Stmt::OpaqueValueExprClass: |
| 1655 | if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) { |
| 1656 | E = src; |
| 1657 | goto tryAgain; |
| 1658 | } |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1659 | return SLCT_NotALiteral; |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1660 | |
Ted Kremenek | b43e8ad | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 1661 | case Stmt::PredefinedExprClass: |
| 1662 | // While __func__, etc., are technically not string literals, they |
| 1663 | // cannot contain format specifiers and thus are not a security |
| 1664 | // liability. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1665 | return SLCT_UncheckedLiteral; |
Ted Kremenek | b43e8ad | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 1666 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1667 | case Stmt::DeclRefExprClass: { |
| 1668 | const DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1669 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1670 | // As an exception, do not flag errors for variables binding to |
| 1671 | // const string literals. |
| 1672 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 1673 | bool isConstant = false; |
| 1674 | QualType T = DR->getType(); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1675 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1676 | if (const ArrayType *AT = Context.getAsArrayType(T)) { |
| 1677 | isConstant = AT->getElementType().isConstant(Context); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1678 | } else if (const PointerType *PT = T->getAs<PointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1679 | isConstant = T.isConstant(Context) && |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1680 | PT->getPointeeType().isConstant(Context); |
Jean-Daniel Dupas | e98e5b5 | 2012-01-25 10:35:33 +0000 | [diff] [blame] | 1681 | } else if (T->isObjCObjectPointerType()) { |
| 1682 | // In ObjC, there is usually no "const ObjectPointer" type, |
| 1683 | // so don't check if the pointee type is constant. |
| 1684 | isConstant = T.isConstant(Context); |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1685 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1686 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1687 | if (isConstant) { |
Matt Beaumont-Gay | e2c6066 | 2012-05-11 22:10:59 +0000 | [diff] [blame] | 1688 | if (const Expr *Init = VD->getAnyInitializer()) { |
| 1689 | // Look through initializers like const char c[] = { "foo" } |
| 1690 | if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { |
| 1691 | if (InitList->isStringLiteralInit()) |
| 1692 | Init = InitList->getInit(0)->IgnoreParenImpCasts(); |
| 1693 | } |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1694 | return checkFormatStringExpr(Init, Args, NumArgs, |
| 1695 | HasVAListArg, format_idx, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1696 | firstDataArg, Type, CallType, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1697 | /*inFunctionCall*/false); |
Matt Beaumont-Gay | e2c6066 | 2012-05-11 22:10:59 +0000 | [diff] [blame] | 1698 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1699 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1700 | |
Anders Carlsson | d966a55 | 2009-06-28 19:55:58 +0000 | [diff] [blame] | 1701 | // For vprintf* functions (i.e., HasVAListArg==true), we add a |
| 1702 | // special check to see if the format string is a function parameter |
| 1703 | // of the function calling the printf function. If the function |
| 1704 | // has an attribute indicating it is a printf-like function, then we |
| 1705 | // should suppress warnings concerning non-literals being used in a call |
| 1706 | // to a vprintf function. For example: |
| 1707 | // |
| 1708 | // void |
| 1709 | // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){ |
| 1710 | // va_list ap; |
| 1711 | // va_start(ap, fmt); |
| 1712 | // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". |
| 1713 | // ... |
| 1714 | // |
Jean-Daniel Dupas | f57c413 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 1715 | if (HasVAListArg) { |
| 1716 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) { |
| 1717 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) { |
| 1718 | int PVIndex = PV->getFunctionScopeIndex() + 1; |
| 1719 | for (specific_attr_iterator<FormatAttr> |
| 1720 | i = ND->specific_attr_begin<FormatAttr>(), |
| 1721 | e = ND->specific_attr_end<FormatAttr>(); i != e ; ++i) { |
| 1722 | FormatAttr *PVFormat = *i; |
| 1723 | // adjust for implicit parameter |
| 1724 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 1725 | if (MD->isInstance()) |
| 1726 | ++PVIndex; |
| 1727 | // We also check if the formats are compatible. |
| 1728 | // We can't pass a 'scanf' string to a 'printf' function. |
| 1729 | if (PVIndex == PVFormat->getFormatIdx() && |
| 1730 | Type == GetFormatStringType(PVFormat)) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1731 | return SLCT_UncheckedLiteral; |
Jean-Daniel Dupas | f57c413 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 1732 | } |
| 1733 | } |
| 1734 | } |
| 1735 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1736 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1737 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1738 | return SLCT_NotALiteral; |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1739 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1740 | |
Jean-Daniel Dupas | 52aabaf | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 1741 | case Stmt::CallExprClass: |
| 1742 | case Stmt::CXXMemberCallExprClass: { |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1743 | const CallExpr *CE = cast<CallExpr>(E); |
Jean-Daniel Dupas | 52aabaf | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 1744 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) { |
| 1745 | if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) { |
| 1746 | unsigned ArgIndex = FA->getFormatIdx(); |
| 1747 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 1748 | if (MD->isInstance()) |
| 1749 | --ArgIndex; |
| 1750 | const Expr *Arg = CE->getArg(ArgIndex - 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1751 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1752 | return checkFormatStringExpr(Arg, Args, NumArgs, |
| 1753 | HasVAListArg, format_idx, firstDataArg, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1754 | Type, CallType, inFunctionCall); |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 1755 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 1756 | unsigned BuiltinID = FD->getBuiltinID(); |
| 1757 | if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString || |
| 1758 | BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) { |
| 1759 | const Expr *Arg = CE->getArg(0); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1760 | return checkFormatStringExpr(Arg, Args, NumArgs, |
| 1761 | HasVAListArg, format_idx, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1762 | firstDataArg, Type, CallType, |
| 1763 | inFunctionCall); |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 1764 | } |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1765 | } |
| 1766 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1767 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1768 | return SLCT_NotALiteral; |
Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1769 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1770 | case Stmt::ObjCStringLiteralClass: |
| 1771 | case Stmt::StringLiteralClass: { |
| 1772 | const StringLiteral *StrE = NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1773 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1774 | if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1775 | StrE = ObjCFExpr->getString(); |
| 1776 | else |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1777 | StrE = cast<StringLiteral>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1778 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1779 | if (StrE) { |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1780 | CheckFormatString(StrE, E, Args, NumArgs, HasVAListArg, format_idx, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1781 | firstDataArg, Type, inFunctionCall, CallType); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1782 | return SLCT_CheckedLiteral; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1783 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1784 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1785 | return SLCT_NotALiteral; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1786 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1787 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1788 | default: |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1789 | return SLCT_NotALiteral; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1790 | } |
| 1791 | } |
| 1792 | |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1793 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1794 | Sema::CheckNonNullArguments(const NonNullAttr *NonNull, |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1795 | const Expr * const *ExprArgs, |
| 1796 | SourceLocation CallSiteLoc) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1797 | for (NonNullAttr::args_iterator i = NonNull->args_begin(), |
| 1798 | e = NonNull->args_end(); |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1799 | i != e; ++i) { |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1800 | const Expr *ArgExpr = ExprArgs[*i]; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1801 | if (ArgExpr->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1802 | Expr::NPC_ValueDependentIsNotNull)) |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1803 | Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); |
Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1804 | } |
| 1805 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1806 | |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1807 | Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) { |
| 1808 | return llvm::StringSwitch<FormatStringType>(Format->getType()) |
| 1809 | .Case("scanf", FST_Scanf) |
| 1810 | .Cases("printf", "printf0", FST_Printf) |
| 1811 | .Cases("NSString", "CFString", FST_NSString) |
| 1812 | .Case("strftime", FST_Strftime) |
| 1813 | .Case("strfmon", FST_Strfmon) |
| 1814 | .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf) |
| 1815 | .Default(FST_Unknown); |
| 1816 | } |
| 1817 | |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1818 | /// CheckFormatArguments - Check calls to printf and scanf (and similar |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1819 | /// functions) for correct use of format strings. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1820 | /// Returns true if a format string has been fully checked. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1821 | bool Sema::CheckFormatArguments(const FormatAttr *Format, Expr **Args, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1822 | unsigned NumArgs, bool IsCXXMember, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1823 | VariadicCallType CallType, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1824 | SourceLocation Loc, SourceRange Range) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1825 | FormatStringInfo FSI; |
| 1826 | if (getFormatStringInfo(Format, IsCXXMember, &FSI)) |
| 1827 | return CheckFormatArguments(Args, NumArgs, FSI.HasVAListArg, FSI.FormatIdx, |
| 1828 | FSI.FirstDataArg, GetFormatStringType(Format), |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1829 | CallType, Loc, Range); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1830 | return false; |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1831 | } |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1832 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1833 | bool Sema::CheckFormatArguments(Expr **Args, unsigned NumArgs, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1834 | bool HasVAListArg, unsigned format_idx, |
| 1835 | unsigned firstDataArg, FormatStringType Type, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1836 | VariadicCallType CallType, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1837 | SourceLocation Loc, SourceRange Range) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1838 | // CHECK: printf/scanf-like function is called with no format string. |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1839 | if (format_idx >= NumArgs) { |
| 1840 | Diag(Loc, diag::warn_missing_format_string) << Range; |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1841 | return false; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1842 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1843 | |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1844 | const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1845 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1846 | // CHECK: format string is not a string literal. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1847 | // |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1848 | // Dynamically generated format strings are difficult to |
| 1849 | // automatically vet at compile time. Requiring that format strings |
| 1850 | // are string literals: (1) permits the checking of format strings by |
| 1851 | // the compiler and thereby (2) can practically remove the source of |
| 1852 | // many format string exploits. |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1853 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | // Format string can be either ObjC string (e.g. @"%d") or |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1855 | // C string (e.g. "%d") |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | // 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] | 1857 | // the same format string checking logic for both ObjC and C strings. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1858 | StringLiteralCheckType CT = |
| 1859 | checkFormatStringExpr(OrigFormatExpr, Args, NumArgs, HasVAListArg, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1860 | format_idx, firstDataArg, Type, CallType); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1861 | if (CT != SLCT_NotALiteral) |
| 1862 | // Literal format string found, check done! |
| 1863 | return CT == SLCT_CheckedLiteral; |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1864 | |
Jean-Daniel Dupas | 2837a2f | 2012-02-07 23:10:53 +0000 | [diff] [blame] | 1865 | // Strftime is particular as it always uses a single 'time' argument, |
| 1866 | // so it is safe to pass a non-literal string. |
| 1867 | if (Type == FST_Strftime) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1868 | return false; |
Jean-Daniel Dupas | 2837a2f | 2012-02-07 23:10:53 +0000 | [diff] [blame] | 1869 | |
Jean-Daniel Dupas | ce3aa39 | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 1870 | // Do not emit diag when the string param is a macro expansion and the |
| 1871 | // format is either NSString or CFString. This is a hack to prevent |
| 1872 | // diag when using the NSLocalizedString and CFCopyLocalizedString macros |
| 1873 | // which are usually used in place of NS and CF string literals. |
Jean-Daniel Dupas | dc17020 | 2012-05-04 21:08:08 +0000 | [diff] [blame] | 1874 | if (Type == FST_NSString && |
| 1875 | SourceMgr.isInSystemMacro(Args[format_idx]->getLocStart())) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1876 | return false; |
Jean-Daniel Dupas | ce3aa39 | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 1877 | |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1878 | // If there are no arguments specified, warn with -Wformat-security, otherwise |
| 1879 | // warn only with -Wformat-nonliteral. |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1880 | if (NumArgs == format_idx+1) |
| 1881 | Diag(Args[format_idx]->getLocStart(), |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1882 | diag::warn_format_nonliteral_noargs) |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1883 | << OrigFormatExpr->getSourceRange(); |
| 1884 | else |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1885 | Diag(Args[format_idx]->getLocStart(), |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1886 | diag::warn_format_nonliteral) |
Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1887 | << OrigFormatExpr->getSourceRange(); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1888 | return false; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1889 | } |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1890 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1891 | namespace { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1892 | class CheckFormatHandler : public analyze_format_string::FormatStringHandler { |
| 1893 | protected: |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1894 | Sema &S; |
| 1895 | const StringLiteral *FExpr; |
| 1896 | const Expr *OrigFormatExpr; |
Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1897 | const unsigned FirstDataArg; |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1898 | const unsigned NumDataArgs; |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1899 | const char *Beg; // Start of format string. |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1900 | const bool HasVAListArg; |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1901 | const Expr * const *Args; |
| 1902 | const unsigned NumArgs; |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1903 | unsigned FormatIdx; |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1904 | llvm::BitVector CoveredArgs; |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1905 | bool usesPositionalArgs; |
| 1906 | bool atFirstArg; |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1907 | bool inFunctionCall; |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1908 | Sema::VariadicCallType CallType; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1909 | public: |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1910 | CheckFormatHandler(Sema &s, const StringLiteral *fexpr, |
Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1911 | const Expr *origFormatExpr, unsigned firstDataArg, |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 1912 | unsigned numDataArgs, const char *beg, bool hasVAListArg, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1913 | Expr **args, unsigned numArgs, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1914 | unsigned formatIdx, bool inFunctionCall, |
| 1915 | Sema::VariadicCallType callType) |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1916 | : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 1917 | FirstDataArg(firstDataArg), NumDataArgs(numDataArgs), |
| 1918 | Beg(beg), HasVAListArg(hasVAListArg), |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1919 | Args(args), NumArgs(numArgs), FormatIdx(formatIdx), |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1920 | usesPositionalArgs(false), atFirstArg(true), |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1921 | inFunctionCall(inFunctionCall), CallType(callType) { |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1922 | CoveredArgs.resize(numDataArgs); |
| 1923 | CoveredArgs.reset(); |
| 1924 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1925 | |
Ted Kremenek | 07d161f | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 1926 | void DoneProcessing(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1927 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1928 | void HandleIncompleteSpecifier(const char *startSpecifier, |
| 1929 | unsigned specifierLen); |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 1930 | |
| 1931 | void HandleNonStandardLengthModifier( |
| 1932 | const analyze_format_string::LengthModifier &LM, |
| 1933 | const char *startSpecifier, unsigned specifierLen); |
| 1934 | |
| 1935 | void HandleNonStandardConversionSpecifier( |
| 1936 | const analyze_format_string::ConversionSpecifier &CS, |
| 1937 | const char *startSpecifier, unsigned specifierLen); |
| 1938 | |
| 1939 | void HandleNonStandardConversionSpecification( |
| 1940 | const analyze_format_string::LengthModifier &LM, |
| 1941 | const analyze_format_string::ConversionSpecifier &CS, |
| 1942 | const char *startSpecifier, unsigned specifierLen); |
| 1943 | |
Hans Wennborg | f856264 | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 1944 | virtual void HandlePosition(const char *startPos, unsigned posLen); |
| 1945 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1946 | virtual void HandleInvalidPosition(const char *startSpecifier, |
| 1947 | unsigned specifierLen, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1948 | analyze_format_string::PositionContext p); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1949 | |
| 1950 | virtual void HandleZeroPosition(const char *startPos, unsigned posLen); |
| 1951 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1952 | void HandleNullChar(const char *nullCharacter); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1953 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1954 | template <typename Range> |
| 1955 | static void EmitFormatDiagnostic(Sema &S, bool inFunctionCall, |
| 1956 | const Expr *ArgumentExpr, |
| 1957 | PartialDiagnostic PDiag, |
| 1958 | SourceLocation StringLoc, |
| 1959 | bool IsStringLocation, Range StringRange, |
| 1960 | FixItHint Fixit = FixItHint()); |
| 1961 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1962 | protected: |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1963 | bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, |
| 1964 | const char *startSpec, |
| 1965 | unsigned specifierLen, |
| 1966 | const char *csStart, unsigned csLen); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1967 | |
| 1968 | void HandlePositionalNonpositionalArgs(SourceLocation Loc, |
| 1969 | const char *startSpec, |
| 1970 | unsigned specifierLen); |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1971 | |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 1972 | SourceRange getFormatStringRange(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1973 | CharSourceRange getSpecifierRange(const char *startSpecifier, |
| 1974 | unsigned specifierLen); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1975 | SourceLocation getLocationOfByte(const char *x); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1976 | |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1977 | const Expr *getDataArg(unsigned i) const; |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 1978 | |
| 1979 | bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, |
| 1980 | const analyze_format_string::ConversionSpecifier &CS, |
| 1981 | const char *startSpecifier, unsigned specifierLen, |
| 1982 | unsigned argIndex); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1983 | |
| 1984 | template <typename Range> |
| 1985 | void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc, |
| 1986 | bool IsStringLocation, Range StringRange, |
| 1987 | FixItHint Fixit = FixItHint()); |
| 1988 | |
| 1989 | void CheckPositionalAndNonpositionalArgs( |
| 1990 | const analyze_format_string::FormatSpecifier *FS); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1991 | }; |
| 1992 | } |
| 1993 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1994 | SourceRange CheckFormatHandler::getFormatStringRange() { |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1995 | return OrigFormatExpr->getSourceRange(); |
| 1996 | } |
| 1997 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1998 | CharSourceRange CheckFormatHandler:: |
| 1999 | getSpecifierRange(const char *startSpecifier, unsigned specifierLen) { |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2000 | SourceLocation Start = getLocationOfByte(startSpecifier); |
| 2001 | SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1); |
| 2002 | |
| 2003 | // Advance the end SourceLocation by one due to half-open ranges. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2004 | End = End.getLocWithOffset(1); |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2005 | |
| 2006 | return CharSourceRange::getCharRange(Start, End); |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2009 | SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2010 | return S.getLocationOfStringLiteralByte(FExpr, x - Beg); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2013 | void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, |
| 2014 | unsigned specifierLen){ |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2015 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier), |
| 2016 | getLocationOfByte(startSpecifier), |
| 2017 | /*IsStringLocation*/true, |
| 2018 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2021 | void CheckFormatHandler::HandleNonStandardLengthModifier( |
| 2022 | const analyze_format_string::LengthModifier &LM, |
| 2023 | const char *startSpecifier, unsigned specifierLen) { |
| 2024 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) << LM.toString() |
Hans Wennborg | f856264 | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 2025 | << 0, |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2026 | getLocationOfByte(LM.getStart()), |
| 2027 | /*IsStringLocation*/true, |
| 2028 | getSpecifierRange(startSpecifier, specifierLen)); |
| 2029 | } |
| 2030 | |
| 2031 | void CheckFormatHandler::HandleNonStandardConversionSpecifier( |
| 2032 | const analyze_format_string::ConversionSpecifier &CS, |
| 2033 | const char *startSpecifier, unsigned specifierLen) { |
| 2034 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) << CS.toString() |
Hans Wennborg | f856264 | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 2035 | << 1, |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2036 | getLocationOfByte(CS.getStart()), |
| 2037 | /*IsStringLocation*/true, |
| 2038 | getSpecifierRange(startSpecifier, specifierLen)); |
| 2039 | } |
| 2040 | |
| 2041 | void CheckFormatHandler::HandleNonStandardConversionSpecification( |
| 2042 | const analyze_format_string::LengthModifier &LM, |
| 2043 | const analyze_format_string::ConversionSpecifier &CS, |
| 2044 | const char *startSpecifier, unsigned specifierLen) { |
| 2045 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_conversion_spec) |
| 2046 | << LM.toString() << CS.toString(), |
| 2047 | getLocationOfByte(LM.getStart()), |
| 2048 | /*IsStringLocation*/true, |
| 2049 | getSpecifierRange(startSpecifier, specifierLen)); |
| 2050 | } |
| 2051 | |
Hans Wennborg | f856264 | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 2052 | void CheckFormatHandler::HandlePosition(const char *startPos, |
| 2053 | unsigned posLen) { |
| 2054 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg), |
| 2055 | getLocationOfByte(startPos), |
| 2056 | /*IsStringLocation*/true, |
| 2057 | getSpecifierRange(startPos, posLen)); |
| 2058 | } |
| 2059 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2060 | void |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2061 | CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen, |
| 2062 | analyze_format_string::PositionContext p) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2063 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier) |
| 2064 | << (unsigned) p, |
| 2065 | getLocationOfByte(startPos), /*IsStringLocation*/true, |
| 2066 | getSpecifierRange(startPos, posLen)); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2069 | void CheckFormatHandler::HandleZeroPosition(const char *startPos, |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2070 | unsigned posLen) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2071 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier), |
| 2072 | getLocationOfByte(startPos), |
| 2073 | /*IsStringLocation*/true, |
| 2074 | getSpecifierRange(startPos, posLen)); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2077 | void CheckFormatHandler::HandleNullChar(const char *nullCharacter) { |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2078 | if (!isa<ObjCStringLiteral>(OrigFormatExpr)) { |
Ted Kremenek | 0c06944 | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 2079 | // The presence of a null character is likely an error. |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2080 | EmitFormatDiagnostic( |
| 2081 | S.PDiag(diag::warn_printf_format_string_contains_null_char), |
| 2082 | getLocationOfByte(nullCharacter), /*IsStringLocation*/true, |
| 2083 | getFormatStringRange()); |
Ted Kremenek | 0c06944 | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 2084 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2085 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2086 | |
Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2087 | // Note that this may return NULL if there was an error parsing or building |
| 2088 | // one of the argument expressions. |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2089 | const Expr *CheckFormatHandler::getDataArg(unsigned i) const { |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2090 | return Args[FirstDataArg + i]; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | void CheckFormatHandler::DoneProcessing() { |
| 2094 | // Does the number of data arguments exceed the number of |
| 2095 | // format conversions in the format string? |
| 2096 | if (!HasVAListArg) { |
| 2097 | // Find any arguments that weren't covered. |
| 2098 | CoveredArgs.flip(); |
| 2099 | signed notCoveredArg = CoveredArgs.find_first(); |
| 2100 | if (notCoveredArg >= 0) { |
| 2101 | assert((unsigned)notCoveredArg < NumDataArgs); |
Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2102 | if (const Expr *E = getDataArg((unsigned) notCoveredArg)) { |
| 2103 | SourceLocation Loc = E->getLocStart(); |
| 2104 | if (!S.getSourceManager().isInSystemMacro(Loc)) { |
| 2105 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_data_arg_not_used), |
| 2106 | Loc, /*IsStringLocation*/false, |
| 2107 | getFormatStringRange()); |
| 2108 | } |
Bob Wilson | c03f2df | 2012-05-03 19:47:19 +0000 | [diff] [blame] | 2109 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2110 | } |
| 2111 | } |
| 2112 | } |
| 2113 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2114 | bool |
| 2115 | CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, |
| 2116 | SourceLocation Loc, |
| 2117 | const char *startSpec, |
| 2118 | unsigned specifierLen, |
| 2119 | const char *csStart, |
| 2120 | unsigned csLen) { |
| 2121 | |
| 2122 | bool keepGoing = true; |
| 2123 | if (argIndex < NumDataArgs) { |
| 2124 | // Consider the argument coverered, even though the specifier doesn't |
| 2125 | // make sense. |
| 2126 | CoveredArgs.set(argIndex); |
| 2127 | } |
| 2128 | else { |
| 2129 | // If argIndex exceeds the number of data arguments we |
| 2130 | // don't issue a warning because that is just a cascade of warnings (and |
| 2131 | // they may have intended '%%' anyway). We don't want to continue processing |
| 2132 | // the format string after this point, however, as we will like just get |
| 2133 | // gibberish when trying to match arguments. |
| 2134 | keepGoing = false; |
| 2135 | } |
| 2136 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2137 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_conversion) |
| 2138 | << StringRef(csStart, csLen), |
| 2139 | Loc, /*IsStringLocation*/true, |
| 2140 | getSpecifierRange(startSpec, specifierLen)); |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2141 | |
| 2142 | return keepGoing; |
| 2143 | } |
| 2144 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2145 | void |
| 2146 | CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc, |
| 2147 | const char *startSpec, |
| 2148 | unsigned specifierLen) { |
| 2149 | EmitFormatDiagnostic( |
| 2150 | S.PDiag(diag::warn_format_mix_positional_nonpositional_args), |
| 2151 | Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen)); |
| 2152 | } |
| 2153 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2154 | bool |
| 2155 | CheckFormatHandler::CheckNumArgs( |
| 2156 | const analyze_format_string::FormatSpecifier &FS, |
| 2157 | const analyze_format_string::ConversionSpecifier &CS, |
| 2158 | const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { |
| 2159 | |
| 2160 | if (argIndex >= NumDataArgs) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2161 | PartialDiagnostic PDiag = FS.usesPositionalArg() |
| 2162 | ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args) |
| 2163 | << (argIndex+1) << NumDataArgs) |
| 2164 | : S.PDiag(diag::warn_printf_insufficient_data_args); |
| 2165 | EmitFormatDiagnostic( |
| 2166 | PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true, |
| 2167 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2168 | return false; |
| 2169 | } |
| 2170 | return true; |
| 2171 | } |
| 2172 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2173 | template<typename Range> |
| 2174 | void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag, |
| 2175 | SourceLocation Loc, |
| 2176 | bool IsStringLocation, |
| 2177 | Range StringRange, |
| 2178 | FixItHint FixIt) { |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2179 | EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag, |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2180 | Loc, IsStringLocation, StringRange, FixIt); |
| 2181 | } |
| 2182 | |
| 2183 | /// \brief If the format string is not within the funcion call, emit a note |
| 2184 | /// so that the function call and string are in diagnostic messages. |
| 2185 | /// |
| 2186 | /// \param inFunctionCall if true, the format string is within the function |
| 2187 | /// call and only one diagnostic message will be produced. Otherwise, an |
| 2188 | /// extra note will be emitted pointing to location of the format string. |
| 2189 | /// |
| 2190 | /// \param ArgumentExpr the expression that is passed as the format string |
| 2191 | /// argument in the function call. Used for getting locations when two |
| 2192 | /// diagnostics are emitted. |
| 2193 | /// |
| 2194 | /// \param PDiag the callee should already have provided any strings for the |
| 2195 | /// diagnostic message. This function only adds locations and fixits |
| 2196 | /// to diagnostics. |
| 2197 | /// |
| 2198 | /// \param Loc primary location for diagnostic. If two diagnostics are |
| 2199 | /// required, one will be at Loc and a new SourceLocation will be created for |
| 2200 | /// the other one. |
| 2201 | /// |
| 2202 | /// \param IsStringLocation if true, Loc points to the format string should be |
| 2203 | /// used for the note. Otherwise, Loc points to the argument list and will |
| 2204 | /// be used with PDiag. |
| 2205 | /// |
| 2206 | /// \param StringRange some or all of the string to highlight. This is |
| 2207 | /// templated so it can accept either a CharSourceRange or a SourceRange. |
| 2208 | /// |
| 2209 | /// \param Fixit optional fix it hint for the format string. |
| 2210 | template<typename Range> |
| 2211 | void CheckFormatHandler::EmitFormatDiagnostic(Sema &S, bool InFunctionCall, |
| 2212 | const Expr *ArgumentExpr, |
| 2213 | PartialDiagnostic PDiag, |
| 2214 | SourceLocation Loc, |
| 2215 | bool IsStringLocation, |
| 2216 | Range StringRange, |
| 2217 | FixItHint FixIt) { |
| 2218 | if (InFunctionCall) |
| 2219 | S.Diag(Loc, PDiag) << StringRange << FixIt; |
| 2220 | else { |
| 2221 | S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag) |
| 2222 | << ArgumentExpr->getSourceRange(); |
| 2223 | S.Diag(IsStringLocation ? Loc : StringRange.getBegin(), |
| 2224 | diag::note_format_string_defined) |
| 2225 | << StringRange << FixIt; |
| 2226 | } |
| 2227 | } |
| 2228 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2229 | //===--- CHECK: Printf format string checking ------------------------------===// |
| 2230 | |
| 2231 | namespace { |
| 2232 | class CheckPrintfHandler : public CheckFormatHandler { |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2233 | bool ObjCContext; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2234 | public: |
| 2235 | CheckPrintfHandler(Sema &s, const StringLiteral *fexpr, |
| 2236 | const Expr *origFormatExpr, unsigned firstDataArg, |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2237 | unsigned numDataArgs, bool isObjC, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2238 | const char *beg, bool hasVAListArg, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2239 | Expr **Args, unsigned NumArgs, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2240 | unsigned formatIdx, bool inFunctionCall, |
| 2241 | Sema::VariadicCallType CallType) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2242 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2243 | numDataArgs, beg, hasVAListArg, Args, NumArgs, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2244 | formatIdx, inFunctionCall, CallType), ObjCContext(isObjC) |
| 2245 | {} |
| 2246 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2247 | |
| 2248 | bool HandleInvalidPrintfConversionSpecifier( |
| 2249 | const analyze_printf::PrintfSpecifier &FS, |
| 2250 | const char *startSpecifier, |
| 2251 | unsigned specifierLen); |
| 2252 | |
| 2253 | bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, |
| 2254 | const char *startSpecifier, |
| 2255 | unsigned specifierLen); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2256 | bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, |
| 2257 | const char *StartSpecifier, |
| 2258 | unsigned SpecifierLen, |
| 2259 | const Expr *E); |
| 2260 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2261 | bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, |
| 2262 | const char *startSpecifier, unsigned specifierLen); |
| 2263 | void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, |
| 2264 | const analyze_printf::OptionalAmount &Amt, |
| 2265 | unsigned type, |
| 2266 | const char *startSpecifier, unsigned specifierLen); |
| 2267 | void HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
| 2268 | const analyze_printf::OptionalFlag &flag, |
| 2269 | const char *startSpecifier, unsigned specifierLen); |
| 2270 | void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, |
| 2271 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 2272 | const analyze_printf::OptionalFlag &flag, |
| 2273 | const char *startSpecifier, unsigned specifierLen); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2274 | bool checkForCStrMembers(const analyze_printf::ArgTypeResult &ATR, |
| 2275 | const Expr *E, const CharSourceRange &CSR); |
| 2276 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2277 | }; |
| 2278 | } |
| 2279 | |
| 2280 | bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( |
| 2281 | const analyze_printf::PrintfSpecifier &FS, |
| 2282 | const char *startSpecifier, |
| 2283 | unsigned specifierLen) { |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2284 | const analyze_printf::PrintfConversionSpecifier &CS = |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2285 | FS.getConversionSpecifier(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2286 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2287 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 2288 | getLocationOfByte(CS.getStart()), |
| 2289 | startSpecifier, specifierLen, |
| 2290 | CS.getStart(), CS.getLength()); |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2293 | bool CheckPrintfHandler::HandleAmount( |
| 2294 | const analyze_format_string::OptionalAmount &Amt, |
| 2295 | unsigned k, const char *startSpecifier, |
| 2296 | unsigned specifierLen) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2297 | |
| 2298 | if (Amt.hasDataArgument()) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2299 | if (!HasVAListArg) { |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2300 | unsigned argIndex = Amt.getArgIndex(); |
| 2301 | if (argIndex >= NumDataArgs) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2302 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg) |
| 2303 | << k, |
| 2304 | getLocationOfByte(Amt.getStart()), |
| 2305 | /*IsStringLocation*/true, |
| 2306 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2307 | // Don't do any more checking. We will just emit |
| 2308 | // spurious errors. |
| 2309 | return false; |
| 2310 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2311 | |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2312 | // Type check the data argument. It should be an 'int'. |
Ted Kremenek | 31f8e32 | 2010-01-29 23:32:22 +0000 | [diff] [blame] | 2313 | // Although not in conformance with C99, we also allow the argument to be |
| 2314 | // an 'unsigned int' as that is a reasonably safe case. GCC also |
| 2315 | // doesn't emit a warning for that case. |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2316 | CoveredArgs.set(argIndex); |
| 2317 | const Expr *Arg = getDataArg(argIndex); |
Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2318 | if (!Arg) |
| 2319 | return false; |
| 2320 | |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2321 | QualType T = Arg->getType(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2322 | |
| 2323 | const analyze_printf::ArgTypeResult &ATR = Amt.getArgType(S.Context); |
| 2324 | assert(ATR.isValid()); |
| 2325 | |
| 2326 | if (!ATR.matchesType(S.Context, T)) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2327 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type) |
Hans Wennborg | a792aff | 2011-12-07 10:33:11 +0000 | [diff] [blame] | 2328 | << k << ATR.getRepresentativeTypeName(S.Context) |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2329 | << T << Arg->getSourceRange(), |
| 2330 | getLocationOfByte(Amt.getStart()), |
| 2331 | /*IsStringLocation*/true, |
| 2332 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2333 | // Don't do any more checking. We will just emit |
| 2334 | // spurious errors. |
| 2335 | return false; |
| 2336 | } |
| 2337 | } |
| 2338 | } |
| 2339 | return true; |
| 2340 | } |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2341 | |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2342 | void CheckPrintfHandler::HandleInvalidAmount( |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2343 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2344 | const analyze_printf::OptionalAmount &Amt, |
| 2345 | unsigned type, |
| 2346 | const char *startSpecifier, |
| 2347 | unsigned specifierLen) { |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2348 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 2349 | FS.getConversionSpecifier(); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2350 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2351 | FixItHint fixit = |
| 2352 | Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant |
| 2353 | ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), |
| 2354 | Amt.getConstantLength())) |
| 2355 | : FixItHint(); |
| 2356 | |
| 2357 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount) |
| 2358 | << type << CS.toString(), |
| 2359 | getLocationOfByte(Amt.getStart()), |
| 2360 | /*IsStringLocation*/true, |
| 2361 | getSpecifierRange(startSpecifier, specifierLen), |
| 2362 | fixit); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2365 | void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2366 | const analyze_printf::OptionalFlag &flag, |
| 2367 | const char *startSpecifier, |
| 2368 | unsigned specifierLen) { |
| 2369 | // Warn about pointless flag with a fixit removal. |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2370 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 2371 | FS.getConversionSpecifier(); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2372 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag) |
| 2373 | << flag.toString() << CS.toString(), |
| 2374 | getLocationOfByte(flag.getPosition()), |
| 2375 | /*IsStringLocation*/true, |
| 2376 | getSpecifierRange(startSpecifier, specifierLen), |
| 2377 | FixItHint::CreateRemoval( |
| 2378 | getSpecifierRange(flag.getPosition(), 1))); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
| 2381 | void CheckPrintfHandler::HandleIgnoredFlag( |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2382 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2383 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 2384 | const analyze_printf::OptionalFlag &flag, |
| 2385 | const char *startSpecifier, |
| 2386 | unsigned specifierLen) { |
| 2387 | // Warn about ignored flag with a fixit removal. |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2388 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag) |
| 2389 | << ignoredFlag.toString() << flag.toString(), |
| 2390 | getLocationOfByte(ignoredFlag.getPosition()), |
| 2391 | /*IsStringLocation*/true, |
| 2392 | getSpecifierRange(startSpecifier, specifierLen), |
| 2393 | FixItHint::CreateRemoval( |
| 2394 | getSpecifierRange(ignoredFlag.getPosition(), 1))); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2397 | // Determines if the specified is a C++ class or struct containing |
| 2398 | // a member with the specified name and kind (e.g. a CXXMethodDecl named |
| 2399 | // "c_str()"). |
| 2400 | template<typename MemberKind> |
| 2401 | static llvm::SmallPtrSet<MemberKind*, 1> |
| 2402 | CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) { |
| 2403 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 2404 | llvm::SmallPtrSet<MemberKind*, 1> Results; |
| 2405 | |
| 2406 | if (!RT) |
| 2407 | return Results; |
| 2408 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 2409 | if (!RD) |
| 2410 | return Results; |
| 2411 | |
| 2412 | LookupResult R(S, &S.PP.getIdentifierTable().get(Name), SourceLocation(), |
| 2413 | Sema::LookupMemberName); |
| 2414 | |
| 2415 | // We just need to include all members of the right kind turned up by the |
| 2416 | // filter, at this point. |
| 2417 | if (S.LookupQualifiedName(R, RT->getDecl())) |
| 2418 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2419 | NamedDecl *decl = (*I)->getUnderlyingDecl(); |
| 2420 | if (MemberKind *FK = dyn_cast<MemberKind>(decl)) |
| 2421 | Results.insert(FK); |
| 2422 | } |
| 2423 | return Results; |
| 2424 | } |
| 2425 | |
| 2426 | // Check if a (w)string was passed when a (w)char* was needed, and offer a |
| 2427 | // better diagnostic if so. ATR is assumed to be valid. |
| 2428 | // Returns true when a c_str() conversion method is found. |
| 2429 | bool CheckPrintfHandler::checkForCStrMembers( |
| 2430 | const analyze_printf::ArgTypeResult &ATR, const Expr *E, |
| 2431 | const CharSourceRange &CSR) { |
| 2432 | typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet; |
| 2433 | |
| 2434 | MethodSet Results = |
| 2435 | CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType()); |
| 2436 | |
| 2437 | for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); |
| 2438 | MI != ME; ++MI) { |
| 2439 | const CXXMethodDecl *Method = *MI; |
| 2440 | if (Method->getNumParams() == 0 && |
| 2441 | ATR.matchesType(S.Context, Method->getResultType())) { |
| 2442 | // FIXME: Suggest parens if the expression needs them. |
| 2443 | SourceLocation EndLoc = |
| 2444 | S.getPreprocessor().getLocForEndOfToken(E->getLocEnd()); |
| 2445 | S.Diag(E->getLocStart(), diag::note_printf_c_str) |
| 2446 | << "c_str()" |
| 2447 | << FixItHint::CreateInsertion(EndLoc, ".c_str()"); |
| 2448 | return true; |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | return false; |
| 2453 | } |
| 2454 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2455 | bool |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2456 | CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier |
Ted Kremenek | 5c41ee8 | 2010-02-11 09:27:41 +0000 | [diff] [blame] | 2457 | &FS, |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2458 | const char *startSpecifier, |
| 2459 | unsigned specifierLen) { |
| 2460 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2461 | using namespace analyze_format_string; |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2462 | using namespace analyze_printf; |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2463 | const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2464 | |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2465 | if (FS.consumesDataArgument()) { |
| 2466 | if (atFirstArg) { |
| 2467 | atFirstArg = false; |
| 2468 | usesPositionalArgs = FS.usesPositionalArg(); |
| 2469 | } |
| 2470 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2471 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), |
| 2472 | startSpecifier, specifierLen); |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2473 | return false; |
| 2474 | } |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2475 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2476 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2477 | // First check if the field width, precision, and conversion specifier |
| 2478 | // have matching data arguments. |
| 2479 | if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, |
| 2480 | startSpecifier, specifierLen)) { |
| 2481 | return false; |
| 2482 | } |
| 2483 | |
| 2484 | if (!HandleAmount(FS.getPrecision(), /* precision */ 1, |
| 2485 | startSpecifier, specifierLen)) { |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2486 | return false; |
| 2487 | } |
| 2488 | |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2489 | if (!CS.consumesDataArgument()) { |
| 2490 | // FIXME: Technically specifying a precision or field width here |
| 2491 | // makes no sense. Worth issuing a warning at some point. |
Ted Kremenek | 0e5675d | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 2492 | return true; |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2493 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2494 | |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2495 | // Consume the argument. |
| 2496 | unsigned argIndex = FS.getArgIndex(); |
Ted Kremenek | e3fc547 | 2010-02-27 08:34:51 +0000 | [diff] [blame] | 2497 | if (argIndex < NumDataArgs) { |
| 2498 | // The check to see if the argIndex is valid will come later. |
| 2499 | // We set the bit here because we may exit early from this |
| 2500 | // function if we encounter some other error. |
| 2501 | CoveredArgs.set(argIndex); |
| 2502 | } |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2503 | |
| 2504 | // Check for using an Objective-C specific conversion specifier |
| 2505 | // in a non-ObjC literal. |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2506 | if (!ObjCContext && CS.isObjCArg()) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2507 | return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, |
| 2508 | specifierLen); |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2509 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2510 | |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2511 | // Check for invalid use of field width |
| 2512 | if (!FS.hasValidFieldWidth()) { |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2513 | HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2514 | startSpecifier, specifierLen); |
| 2515 | } |
| 2516 | |
| 2517 | // Check for invalid use of precision |
| 2518 | if (!FS.hasValidPrecision()) { |
| 2519 | HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, |
| 2520 | startSpecifier, specifierLen); |
| 2521 | } |
| 2522 | |
| 2523 | // Check each flag does not conflict with any other component. |
Ted Kremenek | 65197b4 | 2011-01-08 05:28:46 +0000 | [diff] [blame] | 2524 | if (!FS.hasValidThousandsGroupingPrefix()) |
| 2525 | HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2526 | if (!FS.hasValidLeadingZeros()) |
| 2527 | HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); |
| 2528 | if (!FS.hasValidPlusPrefix()) |
| 2529 | HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2530 | if (!FS.hasValidSpacePrefix()) |
| 2531 | HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2532 | if (!FS.hasValidAlternativeForm()) |
| 2533 | HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); |
| 2534 | if (!FS.hasValidLeftJustified()) |
| 2535 | HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); |
| 2536 | |
| 2537 | // Check that flags are not ignored by another flag |
Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2538 | if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' |
| 2539 | HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), |
| 2540 | startSpecifier, specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2541 | if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' |
| 2542 | HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), |
| 2543 | startSpecifier, specifierLen); |
| 2544 | |
| 2545 | // Check the length modifier is valid with the given conversion specifier. |
| 2546 | const LengthModifier &LM = FS.getLengthModifier(); |
| 2547 | if (!FS.hasValidLengthModifier()) |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2548 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_nonsensical_length) |
| 2549 | << LM.toString() << CS.toString(), |
| 2550 | getLocationOfByte(LM.getStart()), |
| 2551 | /*IsStringLocation*/true, |
| 2552 | getSpecifierRange(startSpecifier, specifierLen), |
| 2553 | FixItHint::CreateRemoval( |
| 2554 | getSpecifierRange(LM.getStart(), |
| 2555 | LM.getLength()))); |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2556 | if (!FS.hasStandardLengthModifier()) |
| 2557 | HandleNonStandardLengthModifier(LM, startSpecifier, specifierLen); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2558 | if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2559 | HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); |
| 2560 | if (!FS.hasStandardLengthConversionCombination()) |
| 2561 | HandleNonStandardConversionSpecification(LM, CS, startSpecifier, |
| 2562 | specifierLen); |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2563 | |
Ted Kremenek | da51f0d | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 2564 | // The remaining checks depend on the data arguments. |
| 2565 | if (HasVAListArg) |
| 2566 | return true; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2567 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2568 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | da51f0d | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 2569 | return false; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2570 | |
Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2571 | const Expr *Arg = getDataArg(argIndex); |
| 2572 | if (!Arg) |
| 2573 | return true; |
| 2574 | |
| 2575 | return checkFormatExpr(FS, startSpecifier, specifierLen, Arg); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | bool |
| 2579 | CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, |
| 2580 | const char *StartSpecifier, |
| 2581 | unsigned SpecifierLen, |
| 2582 | const Expr *E) { |
| 2583 | using namespace analyze_format_string; |
| 2584 | using namespace analyze_printf; |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2585 | // Now type check the data expression that matches the |
| 2586 | // format specifier. |
Nico Weber | 339b907 | 2012-01-31 01:43:25 +0000 | [diff] [blame] | 2587 | const analyze_printf::ArgTypeResult &ATR = FS.getArgType(S.Context, |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2588 | ObjCContext); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2589 | if (ATR.isValid() && !ATR.matchesType(S.Context, E->getType())) { |
Jordan Rose | ee0259d | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 2590 | // Look through argument promotions for our error message's reported type. |
| 2591 | // This includes the integral and floating promotions, but excludes array |
| 2592 | // and function pointer decay; seeing that an argument intended to be a |
| 2593 | // string has type 'char [6]' is probably more confusing than 'char *'. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2594 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
Jordan Rose | ee0259d | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 2595 | if (ICE->getCastKind() == CK_IntegralCast || |
| 2596 | ICE->getCastKind() == CK_FloatingCast) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2597 | E = ICE->getSubExpr(); |
Jordan Rose | ee0259d | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 2598 | |
| 2599 | // Check if we didn't match because of an implicit cast from a 'char' |
| 2600 | // or 'short' to an 'int'. This is done because printf is a varargs |
| 2601 | // function. |
| 2602 | if (ICE->getType() == S.Context.IntTy || |
| 2603 | ICE->getType() == S.Context.UnsignedIntTy) { |
| 2604 | // All further checking is done on the subexpression. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2605 | if (ATR.matchesType(S.Context, E->getType())) |
Jordan Rose | ee0259d | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 2606 | return true; |
| 2607 | } |
Ted Kremenek | 4d8ae4d | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 2608 | } |
Jordan Rose | ee0259d | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 2609 | } |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2610 | |
| 2611 | // We may be able to offer a FixItHint if it is a supported type. |
| 2612 | PrintfSpecifier fixedFS = FS; |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2613 | bool success = fixedFS.fixType(E->getType(), S.getLangOpts(), |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2614 | S.Context, ObjCContext); |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2615 | |
| 2616 | if (success) { |
| 2617 | // Get the fix string from the fixed format specifier |
Jordan Rose | ee0259d | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 2618 | SmallString<16> buf; |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2619 | llvm::raw_svector_ostream os(buf); |
| 2620 | fixedFS.toString(os); |
| 2621 | |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2622 | EmitFormatDiagnostic( |
| 2623 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2624 | << ATR.getRepresentativeTypeName(S.Context) << E->getType() |
| 2625 | << E->getSourceRange(), |
| 2626 | E->getLocStart(), |
Matt Beaumont-Gay | abf145a | 2012-05-17 00:03:16 +0000 | [diff] [blame] | 2627 | /*IsStringLocation*/false, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2628 | getSpecifierRange(StartSpecifier, SpecifierLen), |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2629 | FixItHint::CreateReplacement( |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2630 | getSpecifierRange(StartSpecifier, SpecifierLen), |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2631 | os.str())); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2632 | } else { |
| 2633 | const CharSourceRange &CSR = getSpecifierRange(StartSpecifier, |
| 2634 | SpecifierLen); |
| 2635 | // Since the warning for passing non-POD types to variadic functions |
| 2636 | // was deferred until now, we emit a warning for non-POD |
| 2637 | // arguments here. |
| 2638 | if (S.isValidVarArgType(E->getType()) == Sema::VAK_Invalid) { |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2639 | unsigned DiagKind; |
| 2640 | if (E->getType()->isObjCObjectType()) |
| 2641 | DiagKind = diag::err_cannot_pass_objc_interface_to_vararg_format; |
| 2642 | else |
| 2643 | DiagKind = diag::warn_non_pod_vararg_with_format_string; |
| 2644 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2645 | EmitFormatDiagnostic( |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2646 | S.PDiag(DiagKind) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2647 | << S.getLangOpts().CPlusPlus0x |
| 2648 | << E->getType() |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2649 | << CallType |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2650 | << ATR.getRepresentativeTypeName(S.Context) |
| 2651 | << CSR |
| 2652 | << E->getSourceRange(), |
| 2653 | E->getLocStart(), /*IsStringLocation*/false, CSR); |
| 2654 | |
| 2655 | checkForCStrMembers(ATR, E, CSR); |
| 2656 | } else |
| 2657 | EmitFormatDiagnostic( |
| 2658 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
| 2659 | << ATR.getRepresentativeTypeName(S.Context) << E->getType() |
| 2660 | << CSR |
| 2661 | << E->getSourceRange(), |
| 2662 | E->getLocStart(), /*IsStringLocation*/false, CSR); |
Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2663 | } |
| 2664 | } |
| 2665 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2666 | return true; |
| 2667 | } |
| 2668 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2669 | //===--- CHECK: Scanf format string checking ------------------------------===// |
| 2670 | |
| 2671 | namespace { |
| 2672 | class CheckScanfHandler : public CheckFormatHandler { |
| 2673 | public: |
| 2674 | CheckScanfHandler(Sema &s, const StringLiteral *fexpr, |
| 2675 | const Expr *origFormatExpr, unsigned firstDataArg, |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2676 | unsigned numDataArgs, const char *beg, bool hasVAListArg, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2677 | Expr **Args, unsigned NumArgs, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2678 | unsigned formatIdx, bool inFunctionCall, |
| 2679 | Sema::VariadicCallType CallType) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2680 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2681 | numDataArgs, beg, hasVAListArg, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2682 | Args, NumArgs, formatIdx, inFunctionCall, CallType) |
| 2683 | {} |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2684 | |
| 2685 | bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, |
| 2686 | const char *startSpecifier, |
| 2687 | unsigned specifierLen); |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2688 | |
| 2689 | bool HandleInvalidScanfConversionSpecifier( |
| 2690 | const analyze_scanf::ScanfSpecifier &FS, |
| 2691 | const char *startSpecifier, |
| 2692 | unsigned specifierLen); |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2693 | |
| 2694 | void HandleIncompleteScanList(const char *start, const char *end); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2695 | }; |
Ted Kremenek | 07d161f | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 2696 | } |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2697 | |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2698 | void CheckScanfHandler::HandleIncompleteScanList(const char *start, |
| 2699 | const char *end) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2700 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete), |
| 2701 | getLocationOfByte(end), /*IsStringLocation*/true, |
| 2702 | getSpecifierRange(start, end - start)); |
Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2705 | bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( |
| 2706 | const analyze_scanf::ScanfSpecifier &FS, |
| 2707 | const char *startSpecifier, |
| 2708 | unsigned specifierLen) { |
| 2709 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2710 | const analyze_scanf::ScanfConversionSpecifier &CS = |
Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2711 | FS.getConversionSpecifier(); |
| 2712 | |
| 2713 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 2714 | getLocationOfByte(CS.getStart()), |
| 2715 | startSpecifier, specifierLen, |
| 2716 | CS.getStart(), CS.getLength()); |
| 2717 | } |
| 2718 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2719 | bool CheckScanfHandler::HandleScanfSpecifier( |
| 2720 | const analyze_scanf::ScanfSpecifier &FS, |
| 2721 | const char *startSpecifier, |
| 2722 | unsigned specifierLen) { |
| 2723 | |
| 2724 | using namespace analyze_scanf; |
| 2725 | using namespace analyze_format_string; |
| 2726 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2727 | const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2728 | |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2729 | // Handle case where '%' and '*' don't consume an argument. These shouldn't |
| 2730 | // be used to decide if we are using positional arguments consistently. |
| 2731 | if (FS.consumesDataArgument()) { |
| 2732 | if (atFirstArg) { |
| 2733 | atFirstArg = false; |
| 2734 | usesPositionalArgs = FS.usesPositionalArg(); |
| 2735 | } |
| 2736 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2737 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), |
| 2738 | startSpecifier, specifierLen); |
Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2739 | return false; |
| 2740 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | // Check if the field with is non-zero. |
| 2744 | const OptionalAmount &Amt = FS.getFieldWidth(); |
| 2745 | if (Amt.getHowSpecified() == OptionalAmount::Constant) { |
| 2746 | if (Amt.getConstantAmount() == 0) { |
| 2747 | const CharSourceRange &R = getSpecifierRange(Amt.getStart(), |
| 2748 | Amt.getConstantLength()); |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2749 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width), |
| 2750 | getLocationOfByte(Amt.getStart()), |
| 2751 | /*IsStringLocation*/true, R, |
| 2752 | FixItHint::CreateRemoval(R)); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2753 | } |
| 2754 | } |
| 2755 | |
| 2756 | if (!FS.consumesDataArgument()) { |
| 2757 | // FIXME: Technically specifying a precision or field width here |
| 2758 | // makes no sense. Worth issuing a warning at some point. |
| 2759 | return true; |
| 2760 | } |
| 2761 | |
| 2762 | // Consume the argument. |
| 2763 | unsigned argIndex = FS.getArgIndex(); |
| 2764 | if (argIndex < NumDataArgs) { |
| 2765 | // The check to see if the argIndex is valid will come later. |
| 2766 | // We set the bit here because we may exit early from this |
| 2767 | // function if we encounter some other error. |
| 2768 | CoveredArgs.set(argIndex); |
| 2769 | } |
| 2770 | |
Ted Kremenek | 1e51c20 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 2771 | // Check the length modifier is valid with the given conversion specifier. |
| 2772 | const LengthModifier &LM = FS.getLengthModifier(); |
| 2773 | if (!FS.hasValidLengthModifier()) { |
Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 2774 | const CharSourceRange &R = getSpecifierRange(LM.getStart(), LM.getLength()); |
| 2775 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_nonsensical_length) |
| 2776 | << LM.toString() << CS.toString() |
| 2777 | << getSpecifierRange(startSpecifier, specifierLen), |
| 2778 | getLocationOfByte(LM.getStart()), |
| 2779 | /*IsStringLocation*/true, R, |
| 2780 | FixItHint::CreateRemoval(R)); |
Ted Kremenek | 1e51c20 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2783 | if (!FS.hasStandardLengthModifier()) |
| 2784 | HandleNonStandardLengthModifier(LM, startSpecifier, specifierLen); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2785 | if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) |
Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2786 | HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); |
| 2787 | if (!FS.hasStandardLengthConversionCombination()) |
| 2788 | HandleNonStandardConversionSpecification(LM, CS, startSpecifier, |
| 2789 | specifierLen); |
| 2790 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2791 | // The remaining checks depend on the data arguments. |
| 2792 | if (HasVAListArg) |
| 2793 | return true; |
| 2794 | |
Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2795 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2796 | return false; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2797 | |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2798 | // Check that the argument type matches the format specifier. |
| 2799 | const Expr *Ex = getDataArg(argIndex); |
Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2800 | if (!Ex) |
| 2801 | return true; |
| 2802 | |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2803 | const analyze_scanf::ScanfArgTypeResult &ATR = FS.getArgType(S.Context); |
| 2804 | if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) { |
| 2805 | ScanfSpecifier fixedFS = FS; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2806 | bool success = fixedFS.fixType(Ex->getType(), S.getLangOpts(), |
Hans Wennborg | be6126a | 2012-02-15 09:59:46 +0000 | [diff] [blame] | 2807 | S.Context); |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2808 | |
| 2809 | if (success) { |
| 2810 | // Get the fix string from the fixed format specifier. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2811 | SmallString<128> buf; |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2812 | llvm::raw_svector_ostream os(buf); |
| 2813 | fixedFS.toString(os); |
| 2814 | |
| 2815 | EmitFormatDiagnostic( |
| 2816 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
| 2817 | << ATR.getRepresentativeTypeName(S.Context) << Ex->getType() |
| 2818 | << Ex->getSourceRange(), |
Matt Beaumont-Gay | abf145a | 2012-05-17 00:03:16 +0000 | [diff] [blame] | 2819 | Ex->getLocStart(), |
| 2820 | /*IsStringLocation*/false, |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2821 | getSpecifierRange(startSpecifier, specifierLen), |
| 2822 | FixItHint::CreateReplacement( |
| 2823 | getSpecifierRange(startSpecifier, specifierLen), |
| 2824 | os.str())); |
| 2825 | } else { |
Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 2826 | EmitFormatDiagnostic( |
| 2827 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2828 | << ATR.getRepresentativeTypeName(S.Context) << Ex->getType() |
Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 2829 | << Ex->getSourceRange(), |
Matt Beaumont-Gay | abf145a | 2012-05-17 00:03:16 +0000 | [diff] [blame] | 2830 | Ex->getLocStart(), |
| 2831 | /*IsStringLocation*/false, |
Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 2832 | getSpecifierRange(startSpecifier, specifierLen)); |
Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2833 | } |
| 2834 | } |
| 2835 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2836 | return true; |
| 2837 | } |
| 2838 | |
| 2839 | void Sema::CheckFormatString(const StringLiteral *FExpr, |
Ted Kremenek | 0e5675d | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 2840 | const Expr *OrigFormatExpr, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2841 | Expr **Args, unsigned NumArgs, |
| 2842 | bool HasVAListArg, unsigned format_idx, |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2843 | unsigned firstDataArg, FormatStringType Type, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2844 | bool inFunctionCall, VariadicCallType CallType) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2845 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2846 | // CHECK: is the format string a wide literal? |
Richard Smith | df9ef1b | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 2847 | if (!FExpr->isAscii() && !FExpr->isUTF8()) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2848 | CheckFormatHandler::EmitFormatDiagnostic( |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2849 | *this, inFunctionCall, Args[format_idx], |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2850 | PDiag(diag::warn_format_string_is_wide_literal), FExpr->getLocStart(), |
| 2851 | /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2852 | return; |
| 2853 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2854 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2855 | // Str - The format string. NOTE: this is NOT null-terminated! |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2856 | StringRef StrRef = FExpr->getString(); |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 2857 | const char *Str = StrRef.data(); |
| 2858 | unsigned StrLen = StrRef.size(); |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2859 | const unsigned numDataArgs = NumArgs - firstDataArg; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2860 | |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2861 | // CHECK: empty format string? |
Ted Kremenek | 4cd5791 | 2011-09-29 05:52:16 +0000 | [diff] [blame] | 2862 | if (StrLen == 0 && numDataArgs > 0) { |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2863 | CheckFormatHandler::EmitFormatDiagnostic( |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2864 | *this, inFunctionCall, Args[format_idx], |
Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2865 | PDiag(diag::warn_empty_format_string), FExpr->getLocStart(), |
| 2866 | /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); |
Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2867 | return; |
| 2868 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2869 | |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2870 | if (Type == FST_Printf || Type == FST_NSString) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2871 | CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2872 | numDataArgs, (Type == FST_NSString), |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2873 | Str, HasVAListArg, Args, NumArgs, format_idx, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2874 | inFunctionCall, CallType); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2875 | |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 2876 | if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2877 | getLangOpts())) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2878 | H.DoneProcessing(); |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2879 | } else if (Type == FST_Scanf) { |
Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2880 | CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, numDataArgs, |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2881 | Str, HasVAListArg, Args, NumArgs, format_idx, |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2882 | inFunctionCall, CallType); |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2883 | |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 2884 | if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2885 | getLangOpts())) |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2886 | H.DoneProcessing(); |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2887 | } // TODO: handle other formats |
Ted Kremenek | ce7024e | 2010-01-28 01:18:22 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2890 | //===--- CHECK: Standard memory functions ---------------------------------===// |
| 2891 | |
Douglas Gregor | 2a053a3 | 2011-05-03 20:05:22 +0000 | [diff] [blame] | 2892 | /// \brief Determine whether the given type is a dynamic class type (e.g., |
| 2893 | /// whether it has a vtable). |
| 2894 | static bool isDynamicClassType(QualType T) { |
| 2895 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
| 2896 | if (CXXRecordDecl *Definition = Record->getDefinition()) |
| 2897 | if (Definition->isDynamicClass()) |
| 2898 | return true; |
| 2899 | |
| 2900 | return false; |
| 2901 | } |
| 2902 | |
Chandler Carruth | a72a12f | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 2903 | /// \brief If E is a sizeof expression, returns its argument expression, |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2904 | /// otherwise returns NULL. |
| 2905 | static const Expr *getSizeOfExprArg(const Expr* E) { |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2906 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2907 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
| 2908 | if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType()) |
| 2909 | return SizeOf->getArgumentExpr()->IgnoreParenImpCasts(); |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2910 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2911 | return 0; |
| 2912 | } |
| 2913 | |
Chandler Carruth | a72a12f | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 2914 | /// \brief If E is a sizeof expression, returns its argument type. |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2915 | static QualType getSizeOfArgType(const Expr* E) { |
| 2916 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
| 2917 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
| 2918 | if (SizeOf->getKind() == clang::UETT_SizeOf) |
| 2919 | return SizeOf->getTypeOfArgument(); |
| 2920 | |
| 2921 | return QualType(); |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2922 | } |
| 2923 | |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2924 | /// \brief Check for dangerous or invalid arguments to memset(). |
| 2925 | /// |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 2926 | /// This issues warnings on known problematic, dangerous or unspecified |
Matt Beaumont-Gay | cc2f30c | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 2927 | /// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp' |
| 2928 | /// function calls. |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2929 | /// |
| 2930 | /// \param Call The call expression to diagnose. |
Matt Beaumont-Gay | cc2f30c | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 2931 | void Sema::CheckMemaccessArguments(const CallExpr *Call, |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2932 | unsigned BId, |
Matt Beaumont-Gay | cc2f30c | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 2933 | IdentifierInfo *FnName) { |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2934 | assert(BId != 0); |
| 2935 | |
Ted Kremenek | 1d59f7f | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 2936 | // It is possible to have a non-standard definition of memset. Validate |
Douglas Gregor | 707a23e | 2011-06-16 17:56:04 +0000 | [diff] [blame] | 2937 | // we have enough arguments, and if not, abort further checking. |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2938 | unsigned ExpectedNumArgs = (BId == Builtin::BIstrndup ? 2 : 3); |
Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2939 | if (Call->getNumArgs() < ExpectedNumArgs) |
Ted Kremenek | 1d59f7f | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 2940 | return; |
| 2941 | |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2942 | unsigned LastArg = (BId == Builtin::BImemset || |
| 2943 | BId == Builtin::BIstrndup ? 1 : 2); |
| 2944 | unsigned LenArg = (BId == Builtin::BIstrndup ? 1 : 2); |
Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2945 | const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts(); |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2946 | |
| 2947 | // We have special checking when the length is a sizeof expression. |
| 2948 | QualType SizeOfArgTy = getSizeOfArgType(LenExpr); |
| 2949 | const Expr *SizeOfArg = getSizeOfExprArg(LenExpr); |
| 2950 | llvm::FoldingSetNodeID SizeOfArgID; |
| 2951 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2952 | for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) { |
| 2953 | const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts(); |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2954 | SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange(); |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2955 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2956 | QualType DestTy = Dest->getType(); |
| 2957 | if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) { |
| 2958 | QualType PointeeTy = DestPtrTy->getPointeeType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2959 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2960 | // Never warn about void type pointers. This can be used to suppress |
| 2961 | // false positives. |
| 2962 | if (PointeeTy->isVoidType()) |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2963 | continue; |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2964 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2965 | // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by |
| 2966 | // actually comparing the expressions for equality. Because computing the |
| 2967 | // expression IDs can be expensive, we only do this if the diagnostic is |
| 2968 | // enabled. |
| 2969 | if (SizeOfArg && |
| 2970 | Diags.getDiagnosticLevel(diag::warn_sizeof_pointer_expr_memaccess, |
| 2971 | SizeOfArg->getExprLoc())) { |
| 2972 | // We only compute IDs for expressions if the warning is enabled, and |
| 2973 | // cache the sizeof arg's ID. |
| 2974 | if (SizeOfArgID == llvm::FoldingSetNodeID()) |
| 2975 | SizeOfArg->Profile(SizeOfArgID, Context, true); |
| 2976 | llvm::FoldingSetNodeID DestID; |
| 2977 | Dest->Profile(DestID, Context, true); |
| 2978 | if (DestID == SizeOfArgID) { |
Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2979 | // TODO: For strncpy() and friends, this could suggest sizeof(dst) |
| 2980 | // over sizeof(src) as well. |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2981 | unsigned ActionIdx = 0; // Default is to suggest dereferencing. |
Anna Zaks | 6fcb372 | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 2982 | StringRef ReadableName = FnName->getName(); |
| 2983 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2984 | if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest)) |
Anna Zaks | 90c7832 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 2985 | if (UnaryOp->getOpcode() == UO_AddrOf) |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2986 | ActionIdx = 1; // If its an address-of operator, just remove it. |
| 2987 | if (Context.getTypeSize(PointeeTy) == Context.getCharWidth()) |
| 2988 | ActionIdx = 2; // If the pointee's size is sizeof(char), |
| 2989 | // suggest an explicit length. |
Anna Zaks | 6fcb372 | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 2990 | |
| 2991 | // If the function is defined as a builtin macro, do not show macro |
| 2992 | // expansion. |
| 2993 | SourceLocation SL = SizeOfArg->getExprLoc(); |
| 2994 | SourceRange DSR = Dest->getSourceRange(); |
| 2995 | SourceRange SSR = SizeOfArg->getSourceRange(); |
| 2996 | SourceManager &SM = PP.getSourceManager(); |
| 2997 | |
| 2998 | if (SM.isMacroArgExpansion(SL)) { |
| 2999 | ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts); |
| 3000 | SL = SM.getSpellingLoc(SL); |
| 3001 | DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()), |
| 3002 | SM.getSpellingLoc(DSR.getEnd())); |
| 3003 | SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()), |
| 3004 | SM.getSpellingLoc(SSR.getEnd())); |
| 3005 | } |
| 3006 | |
Anna Zaks | 90c7832 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 3007 | DiagRuntimeBehavior(SL, SizeOfArg, |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3008 | PDiag(diag::warn_sizeof_pointer_expr_memaccess) |
Anna Zaks | 6fcb372 | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 3009 | << ReadableName |
Anna Zaks | 90c7832 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 3010 | << PointeeTy |
| 3011 | << DestTy |
Anna Zaks | 6fcb372 | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 3012 | << DSR |
Anna Zaks | 90c7832 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 3013 | << SSR); |
| 3014 | DiagRuntimeBehavior(SL, SizeOfArg, |
| 3015 | PDiag(diag::warn_sizeof_pointer_expr_memaccess_note) |
| 3016 | << ActionIdx |
| 3017 | << SSR); |
| 3018 | |
Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3019 | break; |
| 3020 | } |
| 3021 | } |
| 3022 | |
| 3023 | // Also check for cases where the sizeof argument is the exact same |
| 3024 | // type as the memory argument, and where it points to a user-defined |
| 3025 | // record type. |
| 3026 | if (SizeOfArgTy != QualType()) { |
| 3027 | if (PointeeTy->isRecordType() && |
| 3028 | Context.typesAreCompatible(SizeOfArgTy, DestTy)) { |
| 3029 | DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest, |
| 3030 | PDiag(diag::warn_sizeof_pointer_type_memaccess) |
| 3031 | << FnName << SizeOfArgTy << ArgIdx |
| 3032 | << PointeeTy << Dest->getSourceRange() |
| 3033 | << LenExpr->getSourceRange()); |
| 3034 | break; |
| 3035 | } |
Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 3038 | // Always complain about dynamic classes. |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3039 | if (isDynamicClassType(PointeeTy)) { |
| 3040 | |
| 3041 | unsigned OperationType = 0; |
| 3042 | // "overwritten" if we're warning about the destination for any call |
| 3043 | // but memcmp; otherwise a verb appropriate to the call. |
| 3044 | if (ArgIdx != 0 || BId == Builtin::BImemcmp) { |
| 3045 | if (BId == Builtin::BImemcpy) |
| 3046 | OperationType = 1; |
| 3047 | else if(BId == Builtin::BImemmove) |
| 3048 | OperationType = 2; |
| 3049 | else if (BId == Builtin::BImemcmp) |
| 3050 | OperationType = 3; |
| 3051 | } |
| 3052 | |
Matt Beaumont-Gay | 5c5218e | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 3053 | DiagRuntimeBehavior( |
| 3054 | Dest->getExprLoc(), Dest, |
| 3055 | PDiag(diag::warn_dyn_class_memaccess) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3056 | << (BId == Builtin::BImemcmp ? ArgIdx + 2 : ArgIdx) |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3057 | << FnName << PointeeTy |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3058 | << OperationType |
Matt Beaumont-Gay | 5c5218e | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 3059 | << Call->getCallee()->getSourceRange()); |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3060 | } else if (PointeeTy.hasNonTrivialObjCLifetime() && |
| 3061 | BId != Builtin::BImemset) |
Matt Beaumont-Gay | 5c5218e | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 3062 | DiagRuntimeBehavior( |
| 3063 | Dest->getExprLoc(), Dest, |
| 3064 | PDiag(diag::warn_arc_object_memaccess) |
| 3065 | << ArgIdx << FnName << PointeeTy |
| 3066 | << Call->getCallee()->getSourceRange()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3067 | else |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 3068 | continue; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3069 | |
| 3070 | DiagRuntimeBehavior( |
| 3071 | Dest->getExprLoc(), Dest, |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 3072 | PDiag(diag::note_bad_memaccess_silence) |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 3073 | << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)")); |
| 3074 | break; |
| 3075 | } |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 3076 | } |
| 3077 | } |
| 3078 | |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 3079 | // A little helper routine: ignore addition and subtraction of integer literals. |
| 3080 | // This intentionally does not ignore all integer constant expressions because |
| 3081 | // we don't want to remove sizeof(). |
| 3082 | static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) { |
| 3083 | Ex = Ex->IgnoreParenCasts(); |
| 3084 | |
| 3085 | for (;;) { |
| 3086 | const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex); |
| 3087 | if (!BO || !BO->isAdditiveOp()) |
| 3088 | break; |
| 3089 | |
| 3090 | const Expr *RHS = BO->getRHS()->IgnoreParenCasts(); |
| 3091 | const Expr *LHS = BO->getLHS()->IgnoreParenCasts(); |
| 3092 | |
| 3093 | if (isa<IntegerLiteral>(RHS)) |
| 3094 | Ex = LHS; |
| 3095 | else if (isa<IntegerLiteral>(LHS)) |
| 3096 | Ex = RHS; |
| 3097 | else |
| 3098 | break; |
| 3099 | } |
| 3100 | |
| 3101 | return Ex; |
| 3102 | } |
| 3103 | |
| 3104 | // Warn if the user has made the 'size' argument to strlcpy or strlcat |
| 3105 | // be the size of the source, instead of the destination. |
| 3106 | void Sema::CheckStrlcpycatArguments(const CallExpr *Call, |
| 3107 | IdentifierInfo *FnName) { |
| 3108 | |
| 3109 | // Don't crash if the user has the wrong number of arguments |
| 3110 | if (Call->getNumArgs() != 3) |
| 3111 | return; |
| 3112 | |
| 3113 | const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context); |
| 3114 | const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context); |
| 3115 | const Expr *CompareWithSrc = NULL; |
| 3116 | |
| 3117 | // Look for 'strlcpy(dst, x, sizeof(x))' |
| 3118 | if (const Expr *Ex = getSizeOfExprArg(SizeArg)) |
| 3119 | CompareWithSrc = Ex; |
| 3120 | else { |
| 3121 | // Look for 'strlcpy(dst, x, strlen(x))' |
| 3122 | if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3123 | if (SizeCall->isBuiltinCall() == Builtin::BIstrlen |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 3124 | && SizeCall->getNumArgs() == 1) |
| 3125 | CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context); |
| 3126 | } |
| 3127 | } |
| 3128 | |
| 3129 | if (!CompareWithSrc) |
| 3130 | return; |
| 3131 | |
| 3132 | // Determine if the argument to sizeof/strlen is equal to the source |
| 3133 | // argument. In principle there's all kinds of things you could do |
| 3134 | // here, for instance creating an == expression and evaluating it with |
| 3135 | // EvaluateAsBooleanCondition, but this uses a more direct technique: |
| 3136 | const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg); |
| 3137 | if (!SrcArgDRE) |
| 3138 | return; |
| 3139 | |
| 3140 | const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc); |
| 3141 | if (!CompareWithSrcDRE || |
| 3142 | SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl()) |
| 3143 | return; |
| 3144 | |
| 3145 | const Expr *OriginalSizeArg = Call->getArg(2); |
| 3146 | Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size) |
| 3147 | << OriginalSizeArg->getSourceRange() << FnName; |
| 3148 | |
| 3149 | // Output a FIXIT hint if the destination is an array (rather than a |
| 3150 | // pointer to an array). This could be enhanced to handle some |
| 3151 | // pointers if we know the actual size, like if DstArg is 'array+2' |
| 3152 | // we could say 'sizeof(array)-2'. |
| 3153 | const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts(); |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 3154 | QualType DstArgTy = DstArg->getType(); |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 3155 | |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 3156 | // Only handle constant-sized or VLAs, but not flexible members. |
| 3157 | if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(DstArgTy)) { |
| 3158 | // Only issue the FIXIT for arrays of size > 1. |
| 3159 | if (CAT->getSize().getSExtValue() <= 1) |
| 3160 | return; |
| 3161 | } else if (!DstArgTy->isVariableArrayType()) { |
| 3162 | return; |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 3163 | } |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 3164 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3165 | SmallString<128> sizeString; |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 3166 | llvm::raw_svector_ostream OS(sizeString); |
| 3167 | OS << "sizeof("; |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3168 | DstArg->printPretty(OS, Context, 0, getPrintingPolicy()); |
Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 3169 | OS << ")"; |
| 3170 | |
| 3171 | Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size) |
| 3172 | << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(), |
| 3173 | OS.str()); |
Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3176 | /// Check if two expressions refer to the same declaration. |
| 3177 | static bool referToTheSameDecl(const Expr *E1, const Expr *E2) { |
| 3178 | if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1)) |
| 3179 | if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2)) |
| 3180 | return D1->getDecl() == D2->getDecl(); |
| 3181 | return false; |
| 3182 | } |
| 3183 | |
| 3184 | static const Expr *getStrlenExprArg(const Expr *E) { |
| 3185 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { |
| 3186 | const FunctionDecl *FD = CE->getDirectCallee(); |
| 3187 | if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen) |
| 3188 | return 0; |
| 3189 | return CE->getArg(0)->IgnoreParenCasts(); |
| 3190 | } |
| 3191 | return 0; |
| 3192 | } |
| 3193 | |
| 3194 | // Warn on anti-patterns as the 'size' argument to strncat. |
| 3195 | // The correct size argument should look like following: |
| 3196 | // strncat(dst, src, sizeof(dst) - strlen(dest) - 1); |
| 3197 | void Sema::CheckStrncatArguments(const CallExpr *CE, |
| 3198 | IdentifierInfo *FnName) { |
| 3199 | // Don't crash if the user has the wrong number of arguments. |
| 3200 | if (CE->getNumArgs() < 3) |
| 3201 | return; |
| 3202 | const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts(); |
| 3203 | const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts(); |
| 3204 | const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts(); |
| 3205 | |
| 3206 | // Identify common expressions, which are wrongly used as the size argument |
| 3207 | // to strncat and may lead to buffer overflows. |
| 3208 | unsigned PatternType = 0; |
| 3209 | if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) { |
| 3210 | // - sizeof(dst) |
| 3211 | if (referToTheSameDecl(SizeOfArg, DstArg)) |
| 3212 | PatternType = 1; |
| 3213 | // - sizeof(src) |
| 3214 | else if (referToTheSameDecl(SizeOfArg, SrcArg)) |
| 3215 | PatternType = 2; |
| 3216 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) { |
| 3217 | if (BE->getOpcode() == BO_Sub) { |
| 3218 | const Expr *L = BE->getLHS()->IgnoreParenCasts(); |
| 3219 | const Expr *R = BE->getRHS()->IgnoreParenCasts(); |
| 3220 | // - sizeof(dst) - strlen(dst) |
| 3221 | if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) && |
| 3222 | referToTheSameDecl(DstArg, getStrlenExprArg(R))) |
| 3223 | PatternType = 1; |
| 3224 | // - sizeof(src) - (anything) |
| 3225 | else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L))) |
| 3226 | PatternType = 2; |
| 3227 | } |
| 3228 | } |
| 3229 | |
| 3230 | if (PatternType == 0) |
| 3231 | return; |
| 3232 | |
Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 3233 | // Generate the diagnostic. |
| 3234 | SourceLocation SL = LenArg->getLocStart(); |
| 3235 | SourceRange SR = LenArg->getSourceRange(); |
| 3236 | SourceManager &SM = PP.getSourceManager(); |
| 3237 | |
| 3238 | // If the function is defined as a builtin macro, do not show macro expansion. |
| 3239 | if (SM.isMacroArgExpansion(SL)) { |
| 3240 | SL = SM.getSpellingLoc(SL); |
| 3241 | SR = SourceRange(SM.getSpellingLoc(SR.getBegin()), |
| 3242 | SM.getSpellingLoc(SR.getEnd())); |
| 3243 | } |
| 3244 | |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3245 | if (PatternType == 1) |
Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 3246 | Diag(SL, diag::warn_strncat_large_size) << SR; |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3247 | else |
Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 3248 | Diag(SL, diag::warn_strncat_src_size) << SR; |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3249 | |
| 3250 | // Output a FIXIT hint if the destination is an array (rather than a |
| 3251 | // pointer to an array). This could be enhanced to handle some |
| 3252 | // pointers if we know the actual size, like if DstArg is 'array+2' |
| 3253 | // we could say 'sizeof(array)-2'. |
| 3254 | QualType DstArgTy = DstArg->getType(); |
| 3255 | |
| 3256 | // Only handle constant-sized or VLAs, but not flexible members. |
| 3257 | if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(DstArgTy)) { |
| 3258 | // Only issue the FIXIT for arrays of size > 1. |
| 3259 | if (CAT->getSize().getSExtValue() <= 1) |
| 3260 | return; |
| 3261 | } else if (!DstArgTy->isVariableArrayType()) { |
| 3262 | return; |
| 3263 | } |
| 3264 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3265 | SmallString<128> sizeString; |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3266 | llvm::raw_svector_ostream OS(sizeString); |
| 3267 | OS << "sizeof("; |
| 3268 | DstArg->printPretty(OS, Context, 0, getPrintingPolicy()); |
| 3269 | OS << ") - "; |
| 3270 | OS << "strlen("; |
| 3271 | DstArg->printPretty(OS, Context, 0, getPrintingPolicy()); |
| 3272 | OS << ") - 1"; |
| 3273 | |
Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 3274 | Diag(SL, diag::note_strncat_wrong_size) |
| 3275 | << FixItHint::CreateReplacement(SR, OS.str()); |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3278 | //===--- CHECK: Return Address of Stack Variable --------------------------===// |
| 3279 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3280 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 3281 | Decl *ParentDecl); |
| 3282 | static Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 3283 | Decl *ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3284 | |
| 3285 | /// CheckReturnStackAddr - Check if a return statement returns the address |
| 3286 | /// of a stack variable. |
| 3287 | void |
| 3288 | Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, |
| 3289 | SourceLocation ReturnLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3290 | |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3291 | Expr *stackE = 0; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3292 | SmallVector<DeclRefExpr *, 8> refVars; |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3293 | |
| 3294 | // Perform checking for returned stack addresses, local blocks, |
| 3295 | // label addresses or references to temporaries. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3296 | if (lhsType->isPointerType() || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3297 | (!getLangOpts().ObjCAutoRefCount && lhsType->isBlockPointerType())) { |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3298 | stackE = EvalAddr(RetValExp, refVars, /*ParentDecl=*/0); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3299 | } else if (lhsType->isReferenceType()) { |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3300 | stackE = EvalVal(RetValExp, refVars, /*ParentDecl=*/0); |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
| 3303 | if (stackE == 0) |
| 3304 | return; // Nothing suspicious was found. |
| 3305 | |
| 3306 | SourceLocation diagLoc; |
| 3307 | SourceRange diagRange; |
| 3308 | if (refVars.empty()) { |
| 3309 | diagLoc = stackE->getLocStart(); |
| 3310 | diagRange = stackE->getSourceRange(); |
| 3311 | } else { |
| 3312 | // We followed through a reference variable. 'stackE' contains the |
| 3313 | // problematic expression but we will warn at the return statement pointing |
| 3314 | // at the reference variable. We will later display the "trail" of |
| 3315 | // reference variables using notes. |
| 3316 | diagLoc = refVars[0]->getLocStart(); |
| 3317 | diagRange = refVars[0]->getSourceRange(); |
| 3318 | } |
| 3319 | |
| 3320 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var. |
| 3321 | Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref |
| 3322 | : diag::warn_ret_stack_addr) |
| 3323 | << DR->getDecl()->getDeclName() << diagRange; |
| 3324 | } else if (isa<BlockExpr>(stackE)) { // local block. |
| 3325 | Diag(diagLoc, diag::err_ret_local_block) << diagRange; |
| 3326 | } else if (isa<AddrLabelExpr>(stackE)) { // address of label. |
| 3327 | Diag(diagLoc, diag::warn_ret_addr_label) << diagRange; |
| 3328 | } else { // local temporary. |
| 3329 | Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref |
| 3330 | : diag::warn_ret_local_temp_addr) |
| 3331 | << diagRange; |
| 3332 | } |
| 3333 | |
| 3334 | // Display the "trail" of reference variables that we followed until we |
| 3335 | // found the problematic expression using notes. |
| 3336 | for (unsigned i = 0, e = refVars.size(); i != e; ++i) { |
| 3337 | VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl()); |
| 3338 | // If this var binds to another reference var, show the range of the next |
| 3339 | // var, otherwise the var binds to the problematic expression, in which case |
| 3340 | // show the range of the expression. |
| 3341 | SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange() |
| 3342 | : stackE->getSourceRange(); |
| 3343 | Diag(VD->getLocation(), diag::note_ref_var_local_bind) |
| 3344 | << VD->getDeclName() << range; |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3345 | } |
| 3346 | } |
| 3347 | |
| 3348 | /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that |
| 3349 | /// check if the expression in a return statement evaluates to an address |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3350 | /// to a location on the stack, a local block, an address of a label, or a |
| 3351 | /// reference to local temporary. The recursion is used to traverse the |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3352 | /// AST of the return expression, with recursion backtracking when we |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3353 | /// encounter a subexpression that (1) clearly does not lead to one of the |
| 3354 | /// above problematic expressions (2) is something we cannot determine leads to |
| 3355 | /// a problematic expression based on such local checking. |
| 3356 | /// |
| 3357 | /// Both EvalAddr and EvalVal follow through reference variables to evaluate |
| 3358 | /// the expression that they point to. Such variables are added to the |
| 3359 | /// 'refVars' vector so that we know what the reference variable "trail" was. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3360 | /// |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 3361 | /// EvalAddr processes expressions that are pointers that are used as |
| 3362 | /// references (and not L-values). EvalVal handles all other values. |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3363 | /// At the base case of the recursion is a check for the above problematic |
| 3364 | /// expressions. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3365 | /// |
| 3366 | /// This implementation handles: |
| 3367 | /// |
| 3368 | /// * pointer-to-pointer casts |
| 3369 | /// * implicit conversions from array references to pointers |
| 3370 | /// * taking the address of fields |
| 3371 | /// * arbitrary interplay between "&" and "*" operators |
| 3372 | /// * pointer arithmetic from an address of a stack variable |
| 3373 | /// * 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] | 3374 | static Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 3375 | Decl *ParentDecl) { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3376 | if (E->isTypeDependent()) |
| 3377 | return NULL; |
| 3378 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3379 | // We should only be called for evaluating pointer expressions. |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3380 | assert((E->getType()->isAnyPointerType() || |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3381 | E->getType()->isBlockPointerType() || |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3382 | E->getType()->isObjCQualifiedIdType()) && |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3383 | "EvalAddr only works on pointers"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3384 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3385 | E = E->IgnoreParens(); |
| 3386 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3387 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 3388 | // viewed AST node. We then recursively traverse the AST by calling |
| 3389 | // EvalAddr and EvalVal appropriately. |
| 3390 | switch (E->getStmtClass()) { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3391 | case Stmt::DeclRefExprClass: { |
| 3392 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
| 3393 | |
| 3394 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
| 3395 | // If this is a reference variable, follow through to the expression that |
| 3396 | // it points to. |
| 3397 | if (V->hasLocalStorage() && |
| 3398 | V->getType()->isReferenceType() && V->hasInit()) { |
| 3399 | // Add the reference variable to the "trail". |
| 3400 | refVars.push_back(DR); |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3401 | return EvalAddr(V->getInit(), refVars, ParentDecl); |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3402 | } |
| 3403 | |
| 3404 | return NULL; |
| 3405 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3406 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3407 | case Stmt::UnaryOperatorClass: { |
| 3408 | // The only unary operator that make sense to handle here |
| 3409 | // is AddrOf. All others don't make sense as pointers. |
| 3410 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3411 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3412 | if (U->getOpcode() == UO_AddrOf) |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3413 | return EvalVal(U->getSubExpr(), refVars, ParentDecl); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3414 | else |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3415 | return NULL; |
| 3416 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3417 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3418 | case Stmt::BinaryOperatorClass: { |
| 3419 | // Handle pointer arithmetic. All other binary operators are not valid |
| 3420 | // in this context. |
| 3421 | BinaryOperator *B = cast<BinaryOperator>(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3422 | BinaryOperatorKind op = B->getOpcode(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3423 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3424 | if (op != BO_Add && op != BO_Sub) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3425 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3426 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3427 | Expr *Base = B->getLHS(); |
| 3428 | |
| 3429 | // Determine which argument is the real pointer base. It could be |
| 3430 | // the RHS argument instead of the LHS. |
| 3431 | if (!Base->getType()->isPointerType()) Base = B->getRHS(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3432 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3433 | assert (Base->getType()->isPointerType()); |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3434 | return EvalAddr(Base, refVars, ParentDecl); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3435 | } |
Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 3436 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3437 | // For conditional operators we need to see if either the LHS or RHS are |
| 3438 | // valid DeclRefExpr*s. If one of them is valid, we return it. |
| 3439 | case Stmt::ConditionalOperatorClass: { |
| 3440 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3441 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3442 | // Handle the GNU extension for missing LHS. |
Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 3443 | if (Expr *lhsExpr = C->getLHS()) { |
| 3444 | // In C++, we can have a throw-expression, which has 'void' type. |
| 3445 | if (!lhsExpr->getType()->isVoidType()) |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3446 | if (Expr* LHS = EvalAddr(lhsExpr, refVars, ParentDecl)) |
Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 3447 | return LHS; |
| 3448 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3449 | |
Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 3450 | // In C++, we can have a throw-expression, which has 'void' type. |
| 3451 | if (C->getRHS()->getType()->isVoidType()) |
| 3452 | return NULL; |
| 3453 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3454 | return EvalAddr(C->getRHS(), refVars, ParentDecl); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3455 | } |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3456 | |
| 3457 | case Stmt::BlockExprClass: |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 3458 | if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures()) |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3459 | return E; // local block. |
| 3460 | return NULL; |
| 3461 | |
| 3462 | case Stmt::AddrLabelExprClass: |
| 3463 | return E; // address of label. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3464 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3465 | case Stmt::ExprWithCleanupsClass: |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3466 | return EvalAddr(cast<ExprWithCleanups>(E)->getSubExpr(), refVars, |
| 3467 | ParentDecl); |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3468 | |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 3469 | // For casts, we need to handle conversions from arrays to |
| 3470 | // pointer values, and pointer-to-pointer conversions. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 3471 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 3472 | case Stmt::CStyleCastExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3473 | case Stmt::CXXFunctionalCastExprClass: |
Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3474 | case Stmt::ObjCBridgedCastExprClass: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3475 | case Stmt::CXXStaticCastExprClass: |
| 3476 | case Stmt::CXXDynamicCastExprClass: |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 3477 | case Stmt::CXXConstCastExprClass: |
| 3478 | case Stmt::CXXReinterpretCastExprClass: { |
Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3479 | Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
| 3480 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 3481 | case CK_BitCast: |
| 3482 | case CK_LValueToRValue: |
| 3483 | case CK_NoOp: |
| 3484 | case CK_BaseToDerived: |
| 3485 | case CK_DerivedToBase: |
| 3486 | case CK_UncheckedDerivedToBase: |
| 3487 | case CK_Dynamic: |
| 3488 | case CK_CPointerToObjCPointerCast: |
| 3489 | case CK_BlockPointerToObjCPointerCast: |
| 3490 | case CK_AnyPointerToBlockPointerCast: |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3491 | return EvalAddr(SubExpr, refVars, ParentDecl); |
Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3492 | |
| 3493 | case CK_ArrayToPointerDecay: |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3494 | return EvalVal(SubExpr, refVars, ParentDecl); |
Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3495 | |
| 3496 | default: |
| 3497 | return 0; |
| 3498 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3499 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3500 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3501 | case Stmt::MaterializeTemporaryExprClass: |
| 3502 | if (Expr *Result = EvalAddr( |
| 3503 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3504 | refVars, ParentDecl)) |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3505 | return Result; |
| 3506 | |
| 3507 | return E; |
| 3508 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3509 | // Everything else: we simply don't reason about them. |
| 3510 | default: |
| 3511 | return NULL; |
| 3512 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3513 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3514 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3515 | |
| 3516 | /// EvalVal - This function is complements EvalAddr in the mutual recursion. |
| 3517 | /// See the comments for EvalAddr for more details. |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3518 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 3519 | Decl *ParentDecl) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3520 | do { |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 3521 | // We should only be called for evaluating non-pointer expressions, or |
| 3522 | // expressions with a pointer type that are not used as references but instead |
| 3523 | // are l-values (e.g., DeclRefExpr with a pointer type). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3524 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3525 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 3526 | // viewed AST node. We then recursively traverse the AST by calling |
| 3527 | // EvalAddr and EvalVal appropriately. |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3528 | |
| 3529 | E = E->IgnoreParens(); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3530 | switch (E->getStmtClass()) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3531 | case Stmt::ImplicitCastExprClass: { |
| 3532 | ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E); |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3533 | if (IE->getValueKind() == VK_LValue) { |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3534 | E = IE->getSubExpr(); |
| 3535 | continue; |
| 3536 | } |
| 3537 | return NULL; |
| 3538 | } |
| 3539 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3540 | case Stmt::ExprWithCleanupsClass: |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3541 | return EvalVal(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,ParentDecl); |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3542 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3543 | case Stmt::DeclRefExprClass: { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3544 | // When we hit a DeclRefExpr we are looking at code that refers to a |
| 3545 | // variable's name. If it's not a reference variable we check if it has |
| 3546 | // local storage within the function, and if so, return the expression. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3547 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3548 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3549 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) { |
| 3550 | // Check if it refers to itself, e.g. "int& i = i;". |
| 3551 | if (V == ParentDecl) |
| 3552 | return DR; |
| 3553 | |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3554 | if (V->hasLocalStorage()) { |
| 3555 | if (!V->getType()->isReferenceType()) |
| 3556 | return DR; |
| 3557 | |
| 3558 | // Reference variable, follow through to the expression that |
| 3559 | // it points to. |
| 3560 | if (V->hasInit()) { |
| 3561 | // Add the reference variable to the "trail". |
| 3562 | refVars.push_back(DR); |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3563 | return EvalVal(V->getInit(), refVars, V); |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3564 | } |
| 3565 | } |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3566 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3567 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3568 | return NULL; |
| 3569 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3570 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3571 | case Stmt::UnaryOperatorClass: { |
| 3572 | // The only unary operator that make sense to handle here |
| 3573 | // is Deref. All others don't resolve to a "name." This includes |
| 3574 | // handling all sorts of rvalues passed to a unary operator. |
| 3575 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3576 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3577 | if (U->getOpcode() == UO_Deref) |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3578 | return EvalAddr(U->getSubExpr(), refVars, ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3579 | |
| 3580 | return NULL; |
| 3581 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3582 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3583 | case Stmt::ArraySubscriptExprClass: { |
| 3584 | // Array subscripts are potential references to data on the stack. We |
| 3585 | // retrieve the DeclRefExpr* for the array variable if it indeed |
| 3586 | // has local storage. |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3587 | return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars,ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3588 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3589 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3590 | case Stmt::ConditionalOperatorClass: { |
| 3591 | // 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] | 3592 | // non-NULL Expr's. If one is non-NULL, we return it. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3593 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 3594 | |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 3595 | // Handle the GNU extension for missing LHS. |
| 3596 | if (Expr *lhsExpr = C->getLHS()) |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3597 | if (Expr *LHS = EvalVal(lhsExpr, refVars, ParentDecl)) |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 3598 | return LHS; |
| 3599 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3600 | return EvalVal(C->getRHS(), refVars, ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3601 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3602 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3603 | // Accesses to members are potential references to data on the stack. |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 3604 | case Stmt::MemberExprClass: { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3605 | MemberExpr *M = cast<MemberExpr>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3606 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3607 | // Check for indirect access. We only want direct field accesses. |
Ted Kremenek | a423e81 | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 3608 | if (M->isArrow()) |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3609 | return NULL; |
Ted Kremenek | a423e81 | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 3610 | |
| 3611 | // Check whether the member type is itself a reference, in which case |
| 3612 | // we're not going to refer to the member, but to what the member refers to. |
| 3613 | if (M->getMemberDecl()->getType()->isReferenceType()) |
| 3614 | return NULL; |
| 3615 | |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3616 | return EvalVal(M->getBase(), refVars, ParentDecl); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3617 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3618 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3619 | case Stmt::MaterializeTemporaryExprClass: |
| 3620 | if (Expr *Result = EvalVal( |
| 3621 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), |
Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3622 | refVars, ParentDecl)) |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3623 | return Result; |
| 3624 | |
| 3625 | return E; |
| 3626 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3627 | default: |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3628 | // Check that we don't return or take the address of a reference to a |
| 3629 | // temporary. This is only useful in C++. |
| 3630 | if (!E->isTypeDependent() && E->isRValue()) |
| 3631 | return E; |
| 3632 | |
| 3633 | // Everything else: we simply don't reason about them. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3634 | return NULL; |
| 3635 | } |
Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3636 | } while (true); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3637 | } |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3638 | |
| 3639 | //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// |
| 3640 | |
| 3641 | /// Check for comparisons of floating point operands using != and ==. |
| 3642 | /// Issue a warning if these are no self-comparisons, as they are not likely |
| 3643 | /// to do what the programmer intended. |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3644 | void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) { |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3645 | Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts(); |
| 3646 | Expr* RightExprSansParen = RHS->IgnoreParenImpCasts(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3647 | |
| 3648 | // Special case: check for x == x (which is OK). |
| 3649 | // Do not emit warnings for such cases. |
| 3650 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) |
| 3651 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) |
| 3652 | if (DRL->getDecl() == DRR->getDecl()) |
David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3653 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3654 | |
| 3655 | |
Ted Kremenek | 1b500bb | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 3656 | // Special case: check for comparisons against literals that can be exactly |
| 3657 | // represented by APFloat. In such cases, do not emit a warning. This |
| 3658 | // is a heuristic: often comparison against such literals are used to |
| 3659 | // detect if a value in a variable has not changed. This clearly can |
| 3660 | // lead to false negatives. |
David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3661 | if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { |
| 3662 | if (FLL->isExact()) |
| 3663 | return; |
| 3664 | } else |
| 3665 | if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)) |
| 3666 | if (FLR->isExact()) |
| 3667 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3668 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3669 | // Check for comparisons with builtin types. |
David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3670 | if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) |
| 3671 | if (CL->isBuiltinCall()) |
| 3672 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3673 | |
David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3674 | if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) |
| 3675 | if (CR->isBuiltinCall()) |
| 3676 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3677 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3678 | // Emit the diagnostic. |
David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3679 | Diag(Loc, diag::warn_floatingpoint_eq) |
| 3680 | << LHS->getSourceRange() << RHS->getSourceRange(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3681 | } |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3682 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3683 | //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// |
| 3684 | //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3685 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3686 | namespace { |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3687 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3688 | /// Structure recording the 'active' range of an integer-valued |
| 3689 | /// expression. |
| 3690 | struct IntRange { |
| 3691 | /// The number of bits active in the int. |
| 3692 | unsigned Width; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3693 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3694 | /// True if the int is known not to have negative values. |
| 3695 | bool NonNegative; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3696 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3697 | IntRange(unsigned Width, bool NonNegative) |
| 3698 | : Width(Width), NonNegative(NonNegative) |
| 3699 | {} |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3700 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3701 | /// Returns the range of the bool type. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3702 | static IntRange forBoolType() { |
| 3703 | return IntRange(1, true); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3704 | } |
| 3705 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3706 | /// Returns the range of an opaque value of the given integral type. |
| 3707 | static IntRange forValueOfType(ASTContext &C, QualType T) { |
| 3708 | return forValueOfCanonicalType(C, |
| 3709 | T->getCanonicalTypeInternal().getTypePtr()); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3712 | /// Returns the range of an opaque value of a canonical integral type. |
| 3713 | static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3714 | assert(T->isCanonicalUnqualified()); |
| 3715 | |
| 3716 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 3717 | T = VT->getElementType().getTypePtr(); |
| 3718 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 3719 | T = CT->getElementType().getTypePtr(); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3720 | |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3721 | // For enum types, use the known bit width of the enumerators. |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3722 | if (const EnumType *ET = dyn_cast<EnumType>(T)) { |
| 3723 | EnumDecl *Enum = ET->getDecl(); |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3724 | if (!Enum->isCompleteDefinition()) |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3725 | return IntRange(C.getIntWidth(QualType(T, 0)), false); |
| 3726 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3727 | unsigned NumPositive = Enum->getNumPositiveBits(); |
| 3728 | unsigned NumNegative = Enum->getNumNegativeBits(); |
| 3729 | |
| 3730 | return IntRange(std::max(NumPositive, NumNegative), NumNegative == 0); |
| 3731 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3732 | |
| 3733 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 3734 | assert(BT->isInteger()); |
| 3735 | |
| 3736 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 3737 | } |
| 3738 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3739 | /// Returns the "target" range of a canonical integral type, i.e. |
| 3740 | /// the range of values expressible in the type. |
| 3741 | /// |
| 3742 | /// This matches forValueOfCanonicalType except that enums have the |
| 3743 | /// full range of their type, not the range of their enumerators. |
| 3744 | static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) { |
| 3745 | assert(T->isCanonicalUnqualified()); |
| 3746 | |
| 3747 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 3748 | T = VT->getElementType().getTypePtr(); |
| 3749 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 3750 | T = CT->getElementType().getTypePtr(); |
| 3751 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Douglas Gregor | 69ff26b | 2011-09-08 23:29:05 +0000 | [diff] [blame] | 3752 | T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr(); |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3753 | |
| 3754 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 3755 | assert(BT->isInteger()); |
| 3756 | |
| 3757 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 3758 | } |
| 3759 | |
| 3760 | /// Returns the supremum of two ranges: i.e. their conservative merge. |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 3761 | static IntRange join(IntRange L, IntRange R) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3762 | return IntRange(std::max(L.Width, R.Width), |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3763 | L.NonNegative && R.NonNegative); |
| 3764 | } |
| 3765 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3766 | /// Returns the infinum of two ranges: i.e. their aggressive merge. |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 3767 | static IntRange meet(IntRange L, IntRange R) { |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3768 | return IntRange(std::min(L.Width, R.Width), |
| 3769 | L.NonNegative || R.NonNegative); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3770 | } |
| 3771 | }; |
| 3772 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3773 | static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, |
| 3774 | unsigned MaxWidth) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3775 | if (value.isSigned() && value.isNegative()) |
| 3776 | return IntRange(value.getMinSignedBits(), false); |
| 3777 | |
| 3778 | if (value.getBitWidth() > MaxWidth) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3779 | value = value.trunc(MaxWidth); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3780 | |
| 3781 | // isNonNegative() just checks the sign bit without considering |
| 3782 | // signedness. |
| 3783 | return IntRange(value.getActiveBits(), true); |
| 3784 | } |
| 3785 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3786 | static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, |
| 3787 | unsigned MaxWidth) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3788 | if (result.isInt()) |
| 3789 | return GetValueRange(C, result.getInt(), MaxWidth); |
| 3790 | |
| 3791 | if (result.isVector()) { |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 3792 | IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); |
| 3793 | for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { |
| 3794 | IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); |
| 3795 | R = IntRange::join(R, El); |
| 3796 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3797 | return R; |
| 3798 | } |
| 3799 | |
| 3800 | if (result.isComplexInt()) { |
| 3801 | IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); |
| 3802 | IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); |
| 3803 | return IntRange::join(R, I); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3804 | } |
| 3805 | |
| 3806 | // This can happen with lossless casts to intptr_t of "based" lvalues. |
| 3807 | // Assume it might use arbitrary bits. |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 3808 | // FIXME: The only reason we need to pass the type in here is to get |
| 3809 | // the sign right on this one case. It would be nice if APValue |
| 3810 | // preserved this. |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 3811 | assert(result.isLValue() || result.isAddrLabelDiff()); |
Douglas Gregor | 5e9ebb3 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 3812 | return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType()); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3813 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3814 | |
| 3815 | /// Pseudo-evaluate the given integer expression, estimating the |
| 3816 | /// range of values it might take. |
| 3817 | /// |
| 3818 | /// \param MaxWidth - the width to which the value will be truncated |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3819 | static IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) { |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3820 | E = E->IgnoreParens(); |
| 3821 | |
| 3822 | // Try a full evaluation first. |
| 3823 | Expr::EvalResult result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3824 | if (E->EvaluateAsRValue(result, C)) |
John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 3825 | return GetValueRange(C, result.Val, E->getType(), MaxWidth); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3826 | |
| 3827 | // I think we only want to look through implicit casts here; if the |
| 3828 | // user has an explicit widening cast, we should treat the value as |
| 3829 | // being of the new, wider type. |
| 3830 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) { |
Eli Friedman | b17ee5b | 2011-12-15 02:41:52 +0000 | [diff] [blame] | 3831 | if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue) |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3832 | return GetExprRange(C, CE->getSubExpr(), MaxWidth); |
| 3833 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3834 | IntRange OutputTypeRange = IntRange::forValueOfType(C, CE->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3835 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3836 | bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast); |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3837 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3838 | // 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] | 3839 | if (!isIntegerCast) |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3840 | return OutputTypeRange; |
| 3841 | |
| 3842 | IntRange SubRange |
| 3843 | = GetExprRange(C, CE->getSubExpr(), |
| 3844 | std::min(MaxWidth, OutputTypeRange.Width)); |
| 3845 | |
| 3846 | // Bail out if the subexpr's range is as wide as the cast type. |
| 3847 | if (SubRange.Width >= OutputTypeRange.Width) |
| 3848 | return OutputTypeRange; |
| 3849 | |
| 3850 | // Otherwise, we take the smaller width, and we're non-negative if |
| 3851 | // either the output type or the subexpr is. |
| 3852 | return IntRange(SubRange.Width, |
| 3853 | SubRange.NonNegative || OutputTypeRange.NonNegative); |
| 3854 | } |
| 3855 | |
| 3856 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 3857 | // If we can fold the condition, just take that operand. |
| 3858 | bool CondResult; |
| 3859 | if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) |
| 3860 | return GetExprRange(C, CondResult ? CO->getTrueExpr() |
| 3861 | : CO->getFalseExpr(), |
| 3862 | MaxWidth); |
| 3863 | |
| 3864 | // Otherwise, conservatively merge. |
| 3865 | IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth); |
| 3866 | IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth); |
| 3867 | return IntRange::join(L, R); |
| 3868 | } |
| 3869 | |
| 3870 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 3871 | switch (BO->getOpcode()) { |
| 3872 | |
| 3873 | // Boolean-valued operations are single-bit and positive. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3874 | case BO_LAnd: |
| 3875 | case BO_LOr: |
| 3876 | case BO_LT: |
| 3877 | case BO_GT: |
| 3878 | case BO_LE: |
| 3879 | case BO_GE: |
| 3880 | case BO_EQ: |
| 3881 | case BO_NE: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3882 | return IntRange::forBoolType(); |
| 3883 | |
John McCall | 862ff87 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 3884 | // The type of the assignments is the type of the LHS, so the RHS |
| 3885 | // is not necessarily the same type. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3886 | case BO_MulAssign: |
| 3887 | case BO_DivAssign: |
| 3888 | case BO_RemAssign: |
| 3889 | case BO_AddAssign: |
| 3890 | case BO_SubAssign: |
John McCall | 862ff87 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 3891 | case BO_XorAssign: |
| 3892 | case BO_OrAssign: |
| 3893 | // TODO: bitfields? |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3894 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 3895 | |
John McCall | 862ff87 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 3896 | // Simple assignments just pass through the RHS, which will have |
| 3897 | // been coerced to the LHS type. |
| 3898 | case BO_Assign: |
| 3899 | // TODO: bitfields? |
| 3900 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 3901 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3902 | // Operations with opaque sources are black-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3903 | case BO_PtrMemD: |
| 3904 | case BO_PtrMemI: |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3905 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3906 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3907 | // Bitwise-and uses the *infinum* of the two source ranges. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3908 | case BO_And: |
| 3909 | case BO_AndAssign: |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3910 | return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth), |
| 3911 | GetExprRange(C, BO->getRHS(), MaxWidth)); |
| 3912 | |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3913 | // Left shift gets black-listed based on a judgement call. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3914 | case BO_Shl: |
John McCall | 3aae609 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 3915 | // ...except that we want to treat '1 << (blah)' as logically |
| 3916 | // positive. It's an important idiom. |
| 3917 | if (IntegerLiteral *I |
| 3918 | = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { |
| 3919 | if (I->getValue() == 1) { |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3920 | IntRange R = IntRange::forValueOfType(C, E->getType()); |
John McCall | 3aae609 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 3921 | return IntRange(R.Width, /*NonNegative*/ true); |
| 3922 | } |
| 3923 | } |
| 3924 | // fallthrough |
| 3925 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3926 | case BO_ShlAssign: |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3927 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3928 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3929 | // Right shift by a constant can narrow its left argument. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3930 | case BO_Shr: |
| 3931 | case BO_ShrAssign: { |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3932 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 3933 | |
| 3934 | // If the shift amount is a positive constant, drop the width by |
| 3935 | // that much. |
| 3936 | llvm::APSInt shift; |
| 3937 | if (BO->getRHS()->isIntegerConstantExpr(shift, C) && |
| 3938 | shift.isNonNegative()) { |
| 3939 | unsigned zext = shift.getZExtValue(); |
| 3940 | if (zext >= L.Width) |
| 3941 | L.Width = (L.NonNegative ? 0 : 1); |
| 3942 | else |
| 3943 | L.Width -= zext; |
| 3944 | } |
| 3945 | |
| 3946 | return L; |
| 3947 | } |
| 3948 | |
| 3949 | // Comma acts as its right operand. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3950 | case BO_Comma: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3951 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 3952 | |
John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3953 | // Black-list pointer subtractions. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3954 | case BO_Sub: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3955 | if (BO->getLHS()->getType()->isPointerType()) |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3956 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | 00fe761 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 3957 | break; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3958 | |
John McCall | 00fe761 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 3959 | // The width of a division result is mostly determined by the size |
| 3960 | // of the LHS. |
| 3961 | case BO_Div: { |
| 3962 | // Don't 'pre-truncate' the operands. |
| 3963 | unsigned opWidth = C.getIntWidth(E->getType()); |
| 3964 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); |
| 3965 | |
| 3966 | // If the divisor is constant, use that. |
| 3967 | llvm::APSInt divisor; |
| 3968 | if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) { |
| 3969 | unsigned log2 = divisor.logBase2(); // floor(log_2(divisor)) |
| 3970 | if (log2 >= L.Width) |
| 3971 | L.Width = (L.NonNegative ? 0 : 1); |
| 3972 | else |
| 3973 | L.Width = std::min(L.Width - log2, MaxWidth); |
| 3974 | return L; |
| 3975 | } |
| 3976 | |
| 3977 | // Otherwise, just use the LHS's width. |
| 3978 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); |
| 3979 | return IntRange(L.Width, L.NonNegative && R.NonNegative); |
| 3980 | } |
| 3981 | |
| 3982 | // The result of a remainder can't be larger than the result of |
| 3983 | // either side. |
| 3984 | case BO_Rem: { |
| 3985 | // Don't 'pre-truncate' the operands. |
| 3986 | unsigned opWidth = C.getIntWidth(E->getType()); |
| 3987 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); |
| 3988 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); |
| 3989 | |
| 3990 | IntRange meet = IntRange::meet(L, R); |
| 3991 | meet.Width = std::min(meet.Width, MaxWidth); |
| 3992 | return meet; |
| 3993 | } |
| 3994 | |
| 3995 | // The default behavior is okay for these. |
| 3996 | case BO_Mul: |
| 3997 | case BO_Add: |
| 3998 | case BO_Xor: |
| 3999 | case BO_Or: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4000 | break; |
| 4001 | } |
| 4002 | |
John McCall | 00fe761 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 4003 | // The default case is to treat the operation as if it were closed |
| 4004 | // on the narrowest type that encompasses both operands. |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4005 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 4006 | IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth); |
| 4007 | return IntRange::join(L, R); |
| 4008 | } |
| 4009 | |
| 4010 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 4011 | switch (UO->getOpcode()) { |
| 4012 | // Boolean-valued operations are white-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4013 | case UO_LNot: |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4014 | return IntRange::forBoolType(); |
| 4015 | |
| 4016 | // Operations with opaque sources are black-listed. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4017 | case UO_Deref: |
| 4018 | case UO_AddrOf: // should be impossible |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4019 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4020 | |
| 4021 | default: |
| 4022 | return GetExprRange(C, UO->getSubExpr(), MaxWidth); |
| 4023 | } |
| 4024 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4025 | |
| 4026 | if (dyn_cast<OffsetOfExpr>(E)) { |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4027 | IntRange::forValueOfType(C, E->getType()); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4028 | } |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4029 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4030 | if (FieldDecl *BitField = E->getBitField()) |
| 4031 | return IntRange(BitField->getBitWidthValue(C), |
Douglas Gregor | 5e9ebb3 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 4032 | BitField->getType()->isUnsignedIntegerOrEnumerationType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4033 | |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4034 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4035 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4036 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4037 | static IntRange GetExprRange(ASTContext &C, Expr *E) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4038 | return GetExprRange(C, E, C.getIntWidth(E->getType())); |
| 4039 | } |
| 4040 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4041 | /// Checks whether the given value, which currently has the given |
| 4042 | /// source semantics, has the same value when coerced through the |
| 4043 | /// target semantics. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4044 | static bool IsSameFloatAfterCast(const llvm::APFloat &value, |
| 4045 | const llvm::fltSemantics &Src, |
| 4046 | const llvm::fltSemantics &Tgt) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4047 | llvm::APFloat truncated = value; |
| 4048 | |
| 4049 | bool ignored; |
| 4050 | truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 4051 | truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 4052 | |
| 4053 | return truncated.bitwiseIsEqual(value); |
| 4054 | } |
| 4055 | |
| 4056 | /// Checks whether the given value, which currently has the given |
| 4057 | /// source semantics, has the same value when coerced through the |
| 4058 | /// target semantics. |
| 4059 | /// |
| 4060 | /// The value might be a vector of floats (or a complex number). |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4061 | static bool IsSameFloatAfterCast(const APValue &value, |
| 4062 | const llvm::fltSemantics &Src, |
| 4063 | const llvm::fltSemantics &Tgt) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4064 | if (value.isFloat()) |
| 4065 | return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); |
| 4066 | |
| 4067 | if (value.isVector()) { |
| 4068 | for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) |
| 4069 | if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) |
| 4070 | return false; |
| 4071 | return true; |
| 4072 | } |
| 4073 | |
| 4074 | assert(value.isComplexFloat()); |
| 4075 | return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && |
| 4076 | IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); |
| 4077 | } |
| 4078 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4079 | static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4080 | |
Ted Kremenek | e3b159c | 2010-09-23 21:43:44 +0000 | [diff] [blame] | 4081 | static bool IsZero(Sema &S, Expr *E) { |
| 4082 | // Suppress cases where we are comparing against an enum constant. |
| 4083 | if (const DeclRefExpr *DR = |
| 4084 | dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) |
| 4085 | if (isa<EnumConstantDecl>(DR->getDecl())) |
| 4086 | return false; |
| 4087 | |
| 4088 | // Suppress cases where the '0' value is expanded from a macro. |
| 4089 | if (E->getLocStart().isMacroID()) |
| 4090 | return false; |
| 4091 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4092 | llvm::APSInt Value; |
| 4093 | return E->isIntegerConstantExpr(Value, S.Context) && Value == 0; |
| 4094 | } |
| 4095 | |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4096 | static bool HasEnumType(Expr *E) { |
| 4097 | // Strip off implicit integral promotions. |
| 4098 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
Argyrios Kyrtzidis | 63b57ae | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 4099 | if (ICE->getCastKind() != CK_IntegralCast && |
| 4100 | ICE->getCastKind() != CK_NoOp) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4101 | break; |
Argyrios Kyrtzidis | 63b57ae | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 4102 | E = ICE->getSubExpr(); |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4103 | } |
| 4104 | |
| 4105 | return E->getType()->isEnumeralType(); |
| 4106 | } |
| 4107 | |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4108 | static void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4109 | BinaryOperatorKind op = E->getOpcode(); |
Douglas Gregor | 14af91a | 2010-12-21 07:22:56 +0000 | [diff] [blame] | 4110 | if (E->isValueDependent()) |
| 4111 | return; |
| 4112 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4113 | if (op == BO_LT && IsZero(S, E->getRHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4114 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4115 | << "< 0" << "false" << HasEnumType(E->getLHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4116 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4117 | } else if (op == BO_GE && IsZero(S, E->getRHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4118 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4119 | << ">= 0" << "true" << HasEnumType(E->getLHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4120 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4121 | } else if (op == BO_GT && IsZero(S, E->getLHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4122 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4123 | << "0 >" << "false" << HasEnumType(E->getRHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4124 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4125 | } else if (op == BO_LE && IsZero(S, E->getLHS())) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4126 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4127 | << "0 <=" << "true" << HasEnumType(E->getRHS()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4128 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
| 4129 | } |
| 4130 | } |
| 4131 | |
| 4132 | /// Analyze the operands of the given comparison. Implements the |
| 4133 | /// fallback case from AnalyzeComparison. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4134 | static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4135 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 4136 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4137 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4138 | |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4139 | /// \brief Implements -Wsign-compare. |
| 4140 | /// |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4141 | /// \param E the binary operator to check for warnings |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4142 | static void AnalyzeComparison(Sema &S, BinaryOperator *E) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4143 | // The type the comparison is being performed in. |
| 4144 | QualType T = E->getLHS()->getType(); |
| 4145 | assert(S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType()) |
| 4146 | && "comparison with mismatched types"); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4147 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4148 | // We don't do anything special if this isn't an unsigned integral |
| 4149 | // comparison: we're only interested in integral comparisons, and |
| 4150 | // 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] | 4151 | // |
| 4152 | // We also don't care about value-dependent expressions or expressions |
| 4153 | // whose result is a constant. |
| 4154 | if (!T->hasUnsignedIntegerRepresentation() |
| 4155 | || E->isValueDependent() || E->isIntegerConstantExpr(S.Context)) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4156 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4157 | |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4158 | Expr *LHS = E->getLHS()->IgnoreParenImpCasts(); |
| 4159 | Expr *RHS = E->getRHS()->IgnoreParenImpCasts(); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4160 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4161 | // Check to see if one of the (unmodified) operands is of different |
| 4162 | // signedness. |
| 4163 | Expr *signedOperand, *unsignedOperand; |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4164 | if (LHS->getType()->hasSignedIntegerRepresentation()) { |
| 4165 | assert(!RHS->getType()->hasSignedIntegerRepresentation() && |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4166 | "unsigned comparison between two signed integer expressions?"); |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4167 | signedOperand = LHS; |
| 4168 | unsignedOperand = RHS; |
| 4169 | } else if (RHS->getType()->hasSignedIntegerRepresentation()) { |
| 4170 | signedOperand = RHS; |
| 4171 | unsignedOperand = LHS; |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4172 | } else { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4173 | CheckTrivialUnsignedComparison(S, E); |
| 4174 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4177 | // Otherwise, calculate the effective range of the signed operand. |
| 4178 | IntRange signedRange = GetExprRange(S.Context, signedOperand); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4179 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4180 | // Go ahead and analyze implicit conversions in the operands. Note |
| 4181 | // that we skip the implicit conversions on both sides. |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4182 | AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc()); |
| 4183 | AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc()); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4184 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4185 | // If the signed range is non-negative, -Wsign-compare won't fire, |
| 4186 | // but we should still check for comparisons which are always true |
| 4187 | // or false. |
| 4188 | if (signedRange.NonNegative) |
| 4189 | return CheckTrivialUnsignedComparison(S, E); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4190 | |
| 4191 | // For (in)equality comparisons, if the unsigned operand is a |
| 4192 | // constant which cannot collide with a overflowed signed operand, |
| 4193 | // then reinterpreting the signed operand as unsigned will not |
| 4194 | // change the result of the comparison. |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4195 | if (E->isEqualityOp()) { |
| 4196 | unsigned comparisonWidth = S.Context.getIntWidth(T); |
| 4197 | IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4198 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4199 | // We should never be unable to prove that the unsigned operand is |
| 4200 | // non-negative. |
| 4201 | assert(unsignedRange.NonNegative && "unsigned range includes negative?"); |
| 4202 | |
| 4203 | if (unsignedRange.Width < comparisonWidth) |
| 4204 | return; |
| 4205 | } |
| 4206 | |
Douglas Gregor | 6d3b93d | 2012-05-01 01:53:49 +0000 | [diff] [blame] | 4207 | S.DiagRuntimeBehavior(E->getOperatorLoc(), E, |
| 4208 | S.PDiag(diag::warn_mixed_sign_comparison) |
| 4209 | << LHS->getType() << RHS->getType() |
| 4210 | << LHS->getSourceRange() << RHS->getSourceRange()); |
John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4211 | } |
| 4212 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4213 | /// Analyzes an attempt to assign the given value to a bitfield. |
| 4214 | /// |
| 4215 | /// Returns true if there was something fishy about the attempt. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4216 | static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, |
| 4217 | SourceLocation InitLoc) { |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4218 | assert(Bitfield->isBitField()); |
| 4219 | if (Bitfield->isInvalidDecl()) |
| 4220 | return false; |
| 4221 | |
John McCall | 91b6014 | 2010-11-11 05:33:51 +0000 | [diff] [blame] | 4222 | // White-list bool bitfields. |
| 4223 | if (Bitfield->getType()->isBooleanType()) |
| 4224 | return false; |
| 4225 | |
Douglas Gregor | 46ff303 | 2011-02-04 13:09:01 +0000 | [diff] [blame] | 4226 | // Ignore value- or type-dependent expressions. |
| 4227 | if (Bitfield->getBitWidth()->isValueDependent() || |
| 4228 | Bitfield->getBitWidth()->isTypeDependent() || |
| 4229 | Init->isValueDependent() || |
| 4230 | Init->isTypeDependent()) |
| 4231 | return false; |
| 4232 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4233 | Expr *OriginalInit = Init->IgnoreParenImpCasts(); |
| 4234 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 4235 | llvm::APSInt Value; |
| 4236 | if (!OriginalInit->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects)) |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4237 | return false; |
| 4238 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4239 | unsigned OriginalWidth = Value.getBitWidth(); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4240 | unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context); |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4241 | |
| 4242 | if (OriginalWidth <= FieldWidth) |
| 4243 | return false; |
| 4244 | |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4245 | // Compute the value which the bitfield will contain. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 4246 | llvm::APSInt TruncatedValue = Value.trunc(FieldWidth); |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4247 | TruncatedValue.setIsSigned(Bitfield->getType()->isSignedIntegerType()); |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4248 | |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4249 | // Check whether the stored value is equal to the original value. |
| 4250 | TruncatedValue = TruncatedValue.extend(OriginalWidth); |
Richard Trieu | e1ecdc1 | 2012-07-23 20:21:35 +0000 | [diff] [blame] | 4251 | if (llvm::APSInt::isSameValue(Value, TruncatedValue)) |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4252 | return false; |
| 4253 | |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4254 | // Special-case bitfields of width 1: booleans are naturally 0/1, and |
Eli Friedman | 34ff062 | 2012-02-02 00:40:20 +0000 | [diff] [blame] | 4255 | // therefore don't strictly fit into a signed bitfield of width 1. |
| 4256 | if (FieldWidth == 1 && Value == 1) |
Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4257 | return false; |
| 4258 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4259 | std::string PrettyValue = Value.toString(10); |
| 4260 | std::string PrettyTrunc = TruncatedValue.toString(10); |
| 4261 | |
| 4262 | S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant) |
| 4263 | << PrettyValue << PrettyTrunc << OriginalInit->getType() |
| 4264 | << Init->getSourceRange(); |
| 4265 | |
| 4266 | return true; |
| 4267 | } |
| 4268 | |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4269 | /// Analyze the given simple or compound assignment for warning-worthy |
| 4270 | /// operations. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4271 | static void AnalyzeAssignment(Sema &S, BinaryOperator *E) { |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4272 | // Just recurse on the LHS. |
| 4273 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 4274 | |
| 4275 | // We want to recurse on the RHS as normal unless we're assigning to |
| 4276 | // a bitfield. |
| 4277 | if (FieldDecl *Bitfield = E->getLHS()->getBitField()) { |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4278 | if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(), |
| 4279 | E->getOperatorLoc())) { |
| 4280 | // Recurse, ignoring any implicit conversions on the RHS. |
| 4281 | return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(), |
| 4282 | E->getOperatorLoc()); |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4283 | } |
| 4284 | } |
| 4285 | |
| 4286 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
| 4287 | } |
| 4288 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4289 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4290 | static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4291 | SourceLocation CContext, unsigned diag, |
| 4292 | bool pruneControlFlow = false) { |
| 4293 | if (pruneControlFlow) { |
| 4294 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
| 4295 | S.PDiag(diag) |
| 4296 | << SourceType << T << E->getSourceRange() |
| 4297 | << SourceRange(CContext)); |
| 4298 | return; |
| 4299 | } |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 4300 | S.Diag(E->getExprLoc(), diag) |
| 4301 | << SourceType << T << E->getSourceRange() << SourceRange(CContext); |
| 4302 | } |
| 4303 | |
Chandler Carruth | e1b02e0 | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 4304 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4305 | static void DiagnoseImpCast(Sema &S, Expr *E, QualType T, |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4306 | SourceLocation CContext, unsigned diag, |
| 4307 | bool pruneControlFlow = false) { |
| 4308 | DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow); |
Chandler Carruth | e1b02e0 | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 4309 | } |
| 4310 | |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4311 | /// Diagnose an implicit cast from a literal expression. Does not warn when the |
| 4312 | /// cast wouldn't lose information. |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4313 | void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T, |
| 4314 | SourceLocation CContext) { |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4315 | // 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] | 4316 | bool isExact = false; |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4317 | const llvm::APFloat &Value = FL->getValue(); |
Jeffrey Yasskin | 3e1ef78 | 2011-07-15 17:03:07 +0000 | [diff] [blame] | 4318 | llvm::APSInt IntegerValue(S.Context.getIntWidth(T), |
| 4319 | T->hasUnsignedIntegerRepresentation()); |
| 4320 | if (Value.convertToInteger(IntegerValue, |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4321 | llvm::APFloat::rmTowardZero, &isExact) |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4322 | == llvm::APFloat::opOK && isExact) |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4323 | return; |
| 4324 | |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4325 | SmallString<16> PrettySourceValue; |
| 4326 | Value.toString(PrettySourceValue); |
David Blaikie | de7e7b8 | 2012-05-15 17:18:27 +0000 | [diff] [blame] | 4327 | SmallString<16> PrettyTargetValue; |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4328 | if (T->isSpecificBuiltinType(BuiltinType::Bool)) |
| 4329 | PrettyTargetValue = IntegerValue == 0 ? "false" : "true"; |
| 4330 | else |
David Blaikie | de7e7b8 | 2012-05-15 17:18:27 +0000 | [diff] [blame] | 4331 | IntegerValue.toString(PrettyTargetValue); |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4332 | |
Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4333 | S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer) |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4334 | << FL->getType() << T.getUnqualifiedType() << PrettySourceValue |
| 4335 | << PrettyTargetValue << FL->getSourceRange() << SourceRange(CContext); |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4338 | std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) { |
| 4339 | if (!Range.Width) return "0"; |
| 4340 | |
| 4341 | llvm::APSInt ValueInRange = Value; |
| 4342 | ValueInRange.setIsSigned(!Range.NonNegative); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 4343 | ValueInRange = ValueInRange.trunc(Range.Width); |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4344 | return ValueInRange.toString(10); |
| 4345 | } |
| 4346 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4347 | void CheckImplicitConversion(Sema &S, Expr *E, QualType T, |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4348 | SourceLocation CC, bool *ICContext = 0) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4349 | if (E->isTypeDependent() || E->isValueDependent()) return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4350 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4351 | const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr(); |
| 4352 | const Type *Target = S.Context.getCanonicalType(T).getTypePtr(); |
| 4353 | if (Source == Target) return; |
| 4354 | if (Target->isDependentType()) return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4355 | |
Chandler Carruth | 108f756 | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 4356 | // If the conversion context location is invalid don't complain. We also |
| 4357 | // don't want to emit a warning if the issue occurs from the expansion of |
| 4358 | // a system macro. The problem is that 'getSpellingLoc()' is slow, so we |
| 4359 | // delay this check as long as possible. Once we detect we are in that |
| 4360 | // scenario, we just return. |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4361 | if (CC.isInvalid()) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4362 | return; |
| 4363 | |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4364 | // Diagnose implicit casts to bool. |
| 4365 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) { |
| 4366 | if (isa<StringLiteral>(E)) |
| 4367 | // Warn on string literal to bool. Checks for string literals in logical |
| 4368 | // expressions, for instances, assert(0 && "error here"), is prevented |
| 4369 | // by a check in AnalyzeImplicitConversions(). |
| 4370 | return DiagnoseImpCast(S, E, T, CC, |
| 4371 | diag::warn_impcast_string_literal_to_bool); |
Lang Hames | e14ca9f | 2011-12-05 20:49:50 +0000 | [diff] [blame] | 4372 | if (Source->isFunctionType()) { |
| 4373 | // Warn on function to bool. Checks free functions and static member |
| 4374 | // functions. Weakly imported functions are excluded from the check, |
| 4375 | // since it's common to test their value to check whether the linker |
| 4376 | // found a definition for them. |
| 4377 | ValueDecl *D = 0; |
| 4378 | if (DeclRefExpr* R = dyn_cast<DeclRefExpr>(E)) { |
| 4379 | D = R->getDecl(); |
| 4380 | } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) { |
| 4381 | D = M->getMemberDecl(); |
| 4382 | } |
| 4383 | |
| 4384 | if (D && !D->isWeak()) { |
Richard Trieu | 26b45d8 | 2011-12-06 04:48:01 +0000 | [diff] [blame] | 4385 | if (FunctionDecl* F = dyn_cast<FunctionDecl>(D)) { |
| 4386 | S.Diag(E->getExprLoc(), diag::warn_impcast_function_to_bool) |
| 4387 | << F << E->getSourceRange() << SourceRange(CC); |
David Blaikie | 2def773 | 2011-12-09 21:42:37 +0000 | [diff] [blame] | 4388 | S.Diag(E->getExprLoc(), diag::note_function_to_bool_silence) |
| 4389 | << FixItHint::CreateInsertion(E->getExprLoc(), "&"); |
| 4390 | QualType ReturnType; |
| 4391 | UnresolvedSet<4> NonTemplateOverloads; |
| 4392 | S.isExprCallable(*E, ReturnType, NonTemplateOverloads); |
| 4393 | if (!ReturnType.isNull() |
| 4394 | && ReturnType->isSpecificBuiltinType(BuiltinType::Bool)) |
| 4395 | S.Diag(E->getExprLoc(), diag::note_function_to_bool_call) |
| 4396 | << FixItHint::CreateInsertion( |
| 4397 | S.getPreprocessor().getLocForEndOfToken(E->getLocEnd()), "()"); |
Richard Trieu | 26b45d8 | 2011-12-06 04:48:01 +0000 | [diff] [blame] | 4398 | return; |
| 4399 | } |
Lang Hames | e14ca9f | 2011-12-05 20:49:50 +0000 | [diff] [blame] | 4400 | } |
| 4401 | } |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4402 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4403 | |
| 4404 | // Strip vector types. |
| 4405 | if (isa<VectorType>(Source)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4406 | if (!isa<VectorType>(Target)) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4407 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4408 | return; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4409 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar); |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4410 | } |
Chris Lattner | b792b30 | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 4411 | |
| 4412 | // If the vector cast is cast between two vectors of the same size, it is |
| 4413 | // a bitcast, not a conversion. |
| 4414 | if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target)) |
| 4415 | return; |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4416 | |
| 4417 | Source = cast<VectorType>(Source)->getElementType().getTypePtr(); |
| 4418 | Target = cast<VectorType>(Target)->getElementType().getTypePtr(); |
| 4419 | } |
| 4420 | |
| 4421 | // Strip complex types. |
| 4422 | if (isa<ComplexType>(Source)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4423 | if (!isa<ComplexType>(Target)) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4424 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4425 | return; |
| 4426 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4427 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar); |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4428 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4429 | |
| 4430 | Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); |
| 4431 | Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); |
| 4432 | } |
| 4433 | |
| 4434 | const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); |
| 4435 | const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); |
| 4436 | |
| 4437 | // If the source is floating point... |
| 4438 | if (SourceBT && SourceBT->isFloatingPoint()) { |
| 4439 | // ...and the target is floating point... |
| 4440 | if (TargetBT && TargetBT->isFloatingPoint()) { |
| 4441 | // ...then warn if we're dropping FP rank. |
| 4442 | |
| 4443 | // Builtin FP kinds are ordered by increasing FP rank. |
| 4444 | if (SourceBT->getKind() > TargetBT->getKind()) { |
| 4445 | // Don't warn about float constants that are precisely |
| 4446 | // representable in the target type. |
| 4447 | Expr::EvalResult result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4448 | if (E->EvaluateAsRValue(result, S.Context)) { |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4449 | // Value might be a float, a float vector, or a float complex. |
| 4450 | if (IsSameFloatAfterCast(result.Val, |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4451 | S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)), |
| 4452 | S.Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4453 | return; |
| 4454 | } |
| 4455 | |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4456 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4457 | return; |
| 4458 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4459 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4460 | } |
| 4461 | return; |
| 4462 | } |
| 4463 | |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4464 | // If the target is integral, always warn. |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4465 | if (TargetBT && TargetBT->isInteger()) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4466 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4467 | return; |
| 4468 | |
Chandler Carruth | a5b9332 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 4469 | Expr *InnerE = E->IgnoreParenImpCasts(); |
Matt Beaumont-Gay | 634c8af | 2011-09-08 22:30:47 +0000 | [diff] [blame] | 4470 | // We also want to warn on, e.g., "int i = -1.234" |
| 4471 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE)) |
| 4472 | if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus) |
| 4473 | InnerE = UOp->getSubExpr()->IgnoreParenImpCasts(); |
| 4474 | |
Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4475 | if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) { |
| 4476 | DiagnoseFloatingLiteralImpCast(S, FL, T, CC); |
Chandler Carruth | a5b9332 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 4477 | } else { |
| 4478 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer); |
| 4479 | } |
| 4480 | } |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4481 | |
| 4482 | return; |
| 4483 | } |
| 4484 | |
Richard Trieu | 1838ca5 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 4485 | if ((E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) |
David Blaikie | b26331b | 2012-06-19 21:19:06 +0000 | [diff] [blame] | 4486 | == Expr::NPCK_GNUNull) && !Target->isAnyPointerType() |
David Blaikie | 28a5f0c | 2012-06-21 18:51:10 +0000 | [diff] [blame] | 4487 | && !Target->isBlockPointerType() && !Target->isMemberPointerType()) { |
David Blaikie | b136049 | 2012-03-16 20:30:12 +0000 | [diff] [blame] | 4488 | SourceLocation Loc = E->getSourceRange().getBegin(); |
| 4489 | if (Loc.isMacroID()) |
| 4490 | Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first; |
David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 4491 | if (!Loc.isMacroID() || CC.isMacroID()) |
| 4492 | S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer) |
| 4493 | << T << clang::SourceRange(CC) |
| 4494 | << FixItHint::CreateReplacement(Loc, S.getFixItZeroLiteralForType(T)); |
Richard Trieu | 1838ca5 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
David Blaikie | b26331b | 2012-06-19 21:19:06 +0000 | [diff] [blame] | 4497 | if (!Source->isIntegerType() || !Target->isIntegerType()) |
| 4498 | return; |
| 4499 | |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4500 | // TODO: remove this early return once the false positives for constant->bool |
| 4501 | // in templates, macros, etc, are reduced or removed. |
| 4502 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) |
| 4503 | return; |
| 4504 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4505 | IntRange SourceRange = GetExprRange(S.Context, E); |
John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4506 | IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target); |
John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4507 | |
| 4508 | if (SourceRange.Width > TargetRange.Width) { |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4509 | // If the source is a constant, use a default-on diagnostic. |
| 4510 | // TODO: this should happen for bitfield stores, too. |
| 4511 | llvm::APSInt Value(32); |
| 4512 | if (E->isIntegerConstantExpr(Value, S.Context)) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4513 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4514 | return; |
| 4515 | |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4516 | std::string PrettySourceValue = Value.toString(10); |
| 4517 | std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); |
| 4518 | |
Ted Kremenek | 5e745da | 2011-10-22 02:37:33 +0000 | [diff] [blame] | 4519 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
| 4520 | S.PDiag(diag::warn_impcast_integer_precision_constant) |
| 4521 | << PrettySourceValue << PrettyTargetValue |
| 4522 | << E->getType() << T << E->getSourceRange() |
| 4523 | << clang::SourceRange(CC)); |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4524 | return; |
| 4525 | } |
| 4526 | |
Chris Lattner | b792b30 | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 4527 | // 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] | 4528 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4529 | return; |
| 4530 | |
David Blaikie | 3705084 | 2012-04-12 22:40:54 +0000 | [diff] [blame] | 4531 | if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64) |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4532 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32, |
| 4533 | /* pruneControlFlow */ true); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4534 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4535 | } |
| 4536 | |
| 4537 | if ((TargetRange.NonNegative && !SourceRange.NonNegative) || |
| 4538 | (!TargetRange.NonNegative && SourceRange.NonNegative && |
| 4539 | SourceRange.Width == TargetRange.Width)) { |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4540 | |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4541 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4542 | return; |
| 4543 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4544 | unsigned DiagID = diag::warn_impcast_integer_sign; |
| 4545 | |
| 4546 | // Traditionally, gcc has warned about this under -Wsign-compare. |
| 4547 | // We also want to warn about it in -Wconversion. |
| 4548 | // So if -Wconversion is off, use a completely identical diagnostic |
| 4549 | // in the sign-compare group. |
| 4550 | // The conditional-checking code will |
| 4551 | if (ICContext) { |
| 4552 | DiagID = diag::warn_impcast_integer_sign_conditional; |
| 4553 | *ICContext = true; |
| 4554 | } |
| 4555 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4556 | return DiagnoseImpCast(S, E, T, CC, DiagID); |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4557 | } |
| 4558 | |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4559 | // Diagnose conversions between different enumeration types. |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 4560 | // In C, we pretend that the type of an EnumConstantDecl is its enumeration |
| 4561 | // type, to give us better diagnostics. |
| 4562 | QualType SourceType = E->getType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4563 | if (!S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 4564 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 4565 | if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 4566 | EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext()); |
| 4567 | SourceType = S.Context.getTypeDeclType(Enum); |
| 4568 | Source = S.Context.getCanonicalType(SourceType).getTypePtr(); |
| 4569 | } |
| 4570 | } |
| 4571 | |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4572 | if (const EnumType *SourceEnum = Source->getAs<EnumType>()) |
| 4573 | if (const EnumType *TargetEnum = Target->getAs<EnumType>()) |
| 4574 | if ((SourceEnum->getDecl()->getIdentifier() || |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4575 | SourceEnum->getDecl()->getTypedefNameForAnonDecl()) && |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4576 | (TargetEnum->getDecl()->getIdentifier() || |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4577 | TargetEnum->getDecl()->getTypedefNameForAnonDecl()) && |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4578 | SourceEnum != TargetEnum) { |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4579 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4580 | return; |
| 4581 | |
Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 4582 | return DiagnoseImpCast(S, E, SourceType, T, CC, |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4583 | diag::warn_impcast_different_enum_types); |
Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4584 | } |
Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4585 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4586 | return; |
| 4587 | } |
| 4588 | |
David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 4589 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, |
| 4590 | SourceLocation CC, QualType T); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4591 | |
| 4592 | void CheckConditionalOperand(Sema &S, Expr *E, QualType T, |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4593 | SourceLocation CC, bool &ICContext) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4594 | E = E->IgnoreParenImpCasts(); |
| 4595 | |
| 4596 | if (isa<ConditionalOperator>(E)) |
David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 4597 | return CheckConditionalOperator(S, cast<ConditionalOperator>(E), CC, T); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4598 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4599 | AnalyzeImplicitConversions(S, E, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4600 | if (E->getType() != T) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4601 | return CheckImplicitConversion(S, E, T, CC, &ICContext); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4602 | return; |
| 4603 | } |
| 4604 | |
David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 4605 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, |
| 4606 | SourceLocation CC, QualType T) { |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4607 | AnalyzeImplicitConversions(S, E->getCond(), CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4608 | |
| 4609 | bool Suspicious = false; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4610 | CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious); |
| 4611 | CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4612 | |
| 4613 | // If -Wconversion would have warned about either of the candidates |
| 4614 | // for a signedness conversion to the context type... |
| 4615 | if (!Suspicious) return; |
| 4616 | |
| 4617 | // ...but it's currently ignored... |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 4618 | if (S.Diags.getDiagnosticLevel(diag::warn_impcast_integer_sign_conditional, |
| 4619 | CC)) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4620 | return; |
| 4621 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4622 | // ...then check whether it would have warned about either of the |
| 4623 | // candidates for a signedness conversion to the condition type. |
Richard Trieu | 5254161 | 2011-07-21 02:46:28 +0000 | [diff] [blame] | 4624 | if (E->getType() == T) return; |
| 4625 | |
| 4626 | Suspicious = false; |
| 4627 | CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(), |
| 4628 | E->getType(), CC, &Suspicious); |
| 4629 | if (!Suspicious) |
| 4630 | CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(), |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4631 | E->getType(), CC, &Suspicious); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4632 | } |
| 4633 | |
| 4634 | /// AnalyzeImplicitConversions - Find and report any interesting |
| 4635 | /// implicit conversions in the given expression. There are a couple |
| 4636 | /// of competing diagnostics here, -Wconversion and -Wsign-compare. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4637 | void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4638 | QualType T = OrigE->getType(); |
| 4639 | Expr *E = OrigE->IgnoreParenImpCasts(); |
| 4640 | |
Douglas Gregor | f8b6e15 | 2011-10-10 17:38:18 +0000 | [diff] [blame] | 4641 | if (E->isTypeDependent() || E->isValueDependent()) |
| 4642 | return; |
| 4643 | |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4644 | // For conditional operators, we analyze the arguments as if they |
| 4645 | // were being fed directly into the output. |
| 4646 | if (isa<ConditionalOperator>(E)) { |
| 4647 | ConditionalOperator *CO = cast<ConditionalOperator>(E); |
David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 4648 | CheckConditionalOperator(S, CO, CC, T); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4649 | return; |
| 4650 | } |
| 4651 | |
| 4652 | // Go ahead and check any implicit conversions we might have skipped. |
| 4653 | // The non-canonical typecheck is just an optimization; |
| 4654 | // CheckImplicitConversion will filter out dead implicit conversions. |
| 4655 | if (E->getType() != T) |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4656 | CheckImplicitConversion(S, E, T, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4657 | |
| 4658 | // Now continue drilling into this expression. |
| 4659 | |
| 4660 | // Skip past explicit casts. |
| 4661 | if (isa<ExplicitCastExpr>(E)) { |
| 4662 | E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts(); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4663 | return AnalyzeImplicitConversions(S, E, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4664 | } |
| 4665 | |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4666 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 4667 | // Do a somewhat different check with comparison operators. |
| 4668 | if (BO->isComparisonOp()) |
| 4669 | return AnalyzeComparison(S, BO); |
| 4670 | |
Eli Friedman | 0fa0638 | 2012-01-26 23:34:06 +0000 | [diff] [blame] | 4671 | // And with simple assignments. |
| 4672 | if (BO->getOpcode() == BO_Assign) |
John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4673 | return AnalyzeAssignment(S, BO); |
| 4674 | } |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4675 | |
| 4676 | // These break the otherwise-useful invariant below. Fortunately, |
| 4677 | // we don't really need to recurse into them, because any internal |
| 4678 | // expressions should have been analyzed already when they were |
| 4679 | // built into statements. |
| 4680 | if (isa<StmtExpr>(E)) return; |
| 4681 | |
| 4682 | // Don't descend into unevaluated contexts. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4683 | if (isa<UnaryExprOrTypeTraitExpr>(E)) return; |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4684 | |
| 4685 | // Now just recurse over the expression's children. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4686 | CC = E->getExprLoc(); |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4687 | BinaryOperator *BO = dyn_cast<BinaryOperator>(E); |
| 4688 | bool IsLogicalOperator = BO && BO->isLogicalOp(); |
| 4689 | for (Stmt::child_range I = E->children(); I; ++I) { |
Douglas Gregor | 54042f1 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 4690 | Expr *ChildExpr = dyn_cast_or_null<Expr>(*I); |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 4691 | if (!ChildExpr) |
| 4692 | continue; |
| 4693 | |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4694 | if (IsLogicalOperator && |
| 4695 | isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts())) |
| 4696 | // Ignore checking string literals that are in logical operators. |
| 4697 | continue; |
| 4698 | AnalyzeImplicitConversions(S, ChildExpr, CC); |
| 4699 | } |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4700 | } |
| 4701 | |
| 4702 | } // end anonymous namespace |
| 4703 | |
| 4704 | /// Diagnoses "dangerous" implicit conversions within the given |
| 4705 | /// expression (which is a full expression). Implements -Wconversion |
| 4706 | /// and -Wsign-compare. |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4707 | /// |
| 4708 | /// \param CC the "context" location of the implicit conversion, i.e. |
| 4709 | /// the most location of the syntactic entity requiring the implicit |
| 4710 | /// conversion |
| 4711 | void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4712 | // Don't diagnose in unevaluated contexts. |
David Blaikie | 71f55f7 | 2012-08-06 22:47:24 +0000 | [diff] [blame^] | 4713 | if (isUnevaluatedContext()) |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4714 | return; |
| 4715 | |
| 4716 | // Don't diagnose for value- or type-dependent expressions. |
| 4717 | if (E->isTypeDependent() || E->isValueDependent()) |
| 4718 | return; |
| 4719 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4720 | // Check for array bounds violations in cases where the check isn't triggered |
| 4721 | // elsewhere for other Expr types (like BinaryOperators), e.g. when an |
| 4722 | // ArraySubscriptExpr is on the RHS of a variable initialization. |
| 4723 | CheckArrayAccess(E); |
| 4724 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4725 | // This is not the right CC for (e.g.) a variable initialization. |
| 4726 | AnalyzeImplicitConversions(*this, E, CC); |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4727 | } |
| 4728 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4729 | void Sema::CheckBitFieldInitialization(SourceLocation InitLoc, |
| 4730 | FieldDecl *BitField, |
| 4731 | Expr *Init) { |
| 4732 | (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc); |
| 4733 | } |
| 4734 | |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4735 | /// CheckParmsForFunctionDef - Check that the parameters of the given |
| 4736 | /// function are appropriate for the definition of a function. This |
| 4737 | /// takes care of any checks that cannot be performed on the |
| 4738 | /// declaration itself, e.g., that the types of each of the function |
| 4739 | /// parameters are complete. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 4740 | bool Sema::CheckParmsForFunctionDef(ParmVarDecl **P, ParmVarDecl **PEnd, |
| 4741 | bool CheckParameterNames) { |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4742 | bool HasInvalidParm = false; |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 4743 | for (; P != PEnd; ++P) { |
| 4744 | ParmVarDecl *Param = *P; |
| 4745 | |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4746 | // C99 6.7.5.3p4: the parameters in a parameter type list in a |
| 4747 | // function declarator that is part of a function definition of |
| 4748 | // that function shall not have incomplete type. |
| 4749 | // |
| 4750 | // This is also C++ [dcl.fct]p6. |
| 4751 | if (!Param->isInvalidDecl() && |
| 4752 | RequireCompleteType(Param->getLocation(), Param->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4753 | diag::err_typecheck_decl_incomplete_type)) { |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4754 | Param->setInvalidDecl(); |
| 4755 | HasInvalidParm = true; |
| 4756 | } |
| 4757 | |
| 4758 | // C99 6.9.1p5: If the declarator includes a parameter type list, the |
| 4759 | // declaration of each parameter shall include an identifier. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 4760 | if (CheckParameterNames && |
| 4761 | Param->getIdentifier() == 0 && |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4762 | !Param->isImplicit() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4763 | !getLangOpts().CPlusPlus) |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4764 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
Sam Weinig | d17e340 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 4765 | |
| 4766 | // C99 6.7.5.3p12: |
| 4767 | // If the function declarator is not part of a definition of that |
| 4768 | // function, parameters may have incomplete type and may use the [*] |
| 4769 | // notation in their sequences of declarator specifiers to specify |
| 4770 | // variable length array types. |
| 4771 | QualType PType = Param->getOriginalType(); |
| 4772 | if (const ArrayType *AT = Context.getAsArrayType(PType)) { |
| 4773 | if (AT->getSizeModifier() == ArrayType::Star) { |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 4774 | // FIXME: This diagnosic should point the '[*]' if source-location |
Sam Weinig | d17e340 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 4775 | // information is added for it. |
| 4776 | Diag(Param->getLocation(), diag::err_array_star_in_function_definition); |
| 4777 | } |
| 4778 | } |
Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 4779 | } |
| 4780 | |
| 4781 | return HasInvalidParm; |
| 4782 | } |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4783 | |
| 4784 | /// CheckCastAlign - Implements -Wcast-align, which warns when a |
| 4785 | /// pointer cast increases the alignment requirements. |
| 4786 | void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { |
| 4787 | // This is actually a lot of work to potentially be doing on every |
| 4788 | // 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] | 4789 | if (getDiagnostics().getDiagnosticLevel(diag::warn_cast_align, |
| 4790 | TRange.getBegin()) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 4791 | == DiagnosticsEngine::Ignored) |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4792 | return; |
| 4793 | |
| 4794 | // Ignore dependent types. |
| 4795 | if (T->isDependentType() || Op->getType()->isDependentType()) |
| 4796 | return; |
| 4797 | |
| 4798 | // Require that the destination be a pointer type. |
| 4799 | const PointerType *DestPtr = T->getAs<PointerType>(); |
| 4800 | if (!DestPtr) return; |
| 4801 | |
| 4802 | // If the destination has alignment 1, we're done. |
| 4803 | QualType DestPointee = DestPtr->getPointeeType(); |
| 4804 | if (DestPointee->isIncompleteType()) return; |
| 4805 | CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); |
| 4806 | if (DestAlign.isOne()) return; |
| 4807 | |
| 4808 | // Require that the source be a pointer type. |
| 4809 | const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); |
| 4810 | if (!SrcPtr) return; |
| 4811 | QualType SrcPointee = SrcPtr->getPointeeType(); |
| 4812 | |
| 4813 | // Whitelist casts from cv void*. We already implicitly |
| 4814 | // whitelisted casts to cv void*, since they have alignment 1. |
| 4815 | // Also whitelist casts involving incomplete types, which implicitly |
| 4816 | // includes 'void'. |
| 4817 | if (SrcPointee->isIncompleteType()) return; |
| 4818 | |
| 4819 | CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee); |
| 4820 | if (SrcAlign >= DestAlign) return; |
| 4821 | |
| 4822 | Diag(TRange.getBegin(), diag::warn_cast_align) |
| 4823 | << Op->getType() << T |
| 4824 | << static_cast<unsigned>(SrcAlign.getQuantity()) |
| 4825 | << static_cast<unsigned>(DestAlign.getQuantity()) |
| 4826 | << TRange << Op->getSourceRange(); |
| 4827 | } |
| 4828 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4829 | static const Type* getElementType(const Expr *BaseExpr) { |
| 4830 | const Type* EltType = BaseExpr->getType().getTypePtr(); |
| 4831 | if (EltType->isAnyPointerType()) |
| 4832 | return EltType->getPointeeType().getTypePtr(); |
| 4833 | else if (EltType->isArrayType()) |
| 4834 | return EltType->getBaseElementTypeUnsafe(); |
| 4835 | return EltType; |
| 4836 | } |
| 4837 | |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 4838 | /// \brief Check whether this array fits the idiom of a size-one tail padded |
| 4839 | /// array member of a struct. |
| 4840 | /// |
| 4841 | /// We avoid emitting out-of-bounds access warnings for such arrays as they are |
| 4842 | /// commonly used to emulate flexible arrays in C89 code. |
| 4843 | static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size, |
| 4844 | const NamedDecl *ND) { |
| 4845 | if (Size != 1 || !ND) return false; |
| 4846 | |
| 4847 | const FieldDecl *FD = dyn_cast<FieldDecl>(ND); |
| 4848 | if (!FD) return false; |
| 4849 | |
| 4850 | // Don't consider sizes resulting from macro expansions or template argument |
| 4851 | // substitution to form C89 tail-padded arrays. |
Sean Callanan | d2cf348 | 2012-05-04 18:22:53 +0000 | [diff] [blame] | 4852 | |
| 4853 | TypeSourceInfo *TInfo = FD->getTypeSourceInfo(); |
Ted Kremenek | 00e1f6f | 2012-05-09 05:35:08 +0000 | [diff] [blame] | 4854 | while (TInfo) { |
| 4855 | TypeLoc TL = TInfo->getTypeLoc(); |
| 4856 | // Look through typedefs. |
| 4857 | const TypedefTypeLoc *TTL = dyn_cast<TypedefTypeLoc>(&TL); |
| 4858 | if (TTL) { |
| 4859 | const TypedefNameDecl *TDL = TTL->getTypedefNameDecl(); |
| 4860 | TInfo = TDL->getTypeSourceInfo(); |
| 4861 | continue; |
| 4862 | } |
| 4863 | ConstantArrayTypeLoc CTL = cast<ConstantArrayTypeLoc>(TL); |
| 4864 | const Expr *SizeExpr = dyn_cast<IntegerLiteral>(CTL.getSizeExpr()); |
Sean Callanan | d2cf348 | 2012-05-04 18:22:53 +0000 | [diff] [blame] | 4865 | if (!SizeExpr || SizeExpr->getExprLoc().isMacroID()) |
| 4866 | return false; |
Ted Kremenek | 00e1f6f | 2012-05-09 05:35:08 +0000 | [diff] [blame] | 4867 | break; |
Sean Callanan | d2cf348 | 2012-05-04 18:22:53 +0000 | [diff] [blame] | 4868 | } |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 4869 | |
| 4870 | const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext()); |
Matt Beaumont-Gay | 381711c | 2011-11-29 22:43:53 +0000 | [diff] [blame] | 4871 | if (!RD) return false; |
| 4872 | if (RD->isUnion()) return false; |
| 4873 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4874 | if (!CRD->isStandardLayout()) return false; |
| 4875 | } |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 4876 | |
Benjamin Kramer | 22d4fed | 2011-08-06 03:04:42 +0000 | [diff] [blame] | 4877 | // See if this is the last field decl in the record. |
| 4878 | const Decl *D = FD; |
| 4879 | while ((D = D->getNextDeclInContext())) |
| 4880 | if (isa<FieldDecl>(D)) |
| 4881 | return false; |
| 4882 | return true; |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 4883 | } |
| 4884 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4885 | void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4886 | const ArraySubscriptExpr *ASE, |
Richard Smith | 25b009a | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 4887 | bool AllowOnePastEnd, bool IndexNegated) { |
Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 4888 | IndexExpr = IndexExpr->IgnoreParenImpCasts(); |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4889 | if (IndexExpr->isValueDependent()) |
| 4890 | return; |
| 4891 | |
Matt Beaumont-Gay | 8ef8f43 | 2011-12-12 22:35:02 +0000 | [diff] [blame] | 4892 | const Type *EffectiveType = getElementType(BaseExpr); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4893 | BaseExpr = BaseExpr->IgnoreParenCasts(); |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4894 | const ConstantArrayType *ArrayTy = |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4895 | Context.getAsConstantArrayType(BaseExpr->getType()); |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4896 | if (!ArrayTy) |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 4897 | return; |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 4898 | |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4899 | llvm::APSInt index; |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4900 | if (!IndexExpr->EvaluateAsInt(index, Context)) |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 4901 | return; |
Richard Smith | 25b009a | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 4902 | if (IndexNegated) |
| 4903 | index = -index; |
Ted Kremenek | 8fd0a5d | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 4904 | |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4905 | const NamedDecl *ND = NULL; |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4906 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
| 4907 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 4908 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4909 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4910 | |
Ted Kremenek | 9e060ca | 2011-02-23 23:06:04 +0000 | [diff] [blame] | 4911 | if (index.isUnsigned() || !index.isNegative()) { |
Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 4912 | llvm::APInt size = ArrayTy->getSize(); |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 4913 | if (!size.isStrictlyPositive()) |
| 4914 | return; |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4915 | |
| 4916 | const Type* BaseType = getElementType(BaseExpr); |
Nico Weber | de5998f | 2011-09-17 22:59:41 +0000 | [diff] [blame] | 4917 | if (BaseType != EffectiveType) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4918 | // Make sure we're comparing apples to apples when comparing index to size |
| 4919 | uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); |
| 4920 | uint64_t array_typesize = Context.getTypeSize(BaseType); |
Kaelyn Uhrain | d10f4bc | 2011-08-10 19:47:25 +0000 | [diff] [blame] | 4921 | // Handle ptrarith_typesize being zero, such as when casting to void* |
Kaelyn Uhrain | 18f1697 | 2011-08-10 18:49:28 +0000 | [diff] [blame] | 4922 | if (!ptrarith_typesize) ptrarith_typesize = 1; |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4923 | if (ptrarith_typesize != array_typesize) { |
| 4924 | // There's a cast to a different size type involved |
| 4925 | uint64_t ratio = array_typesize / ptrarith_typesize; |
| 4926 | // TODO: Be smarter about handling cases where array_typesize is not a |
| 4927 | // multiple of ptrarith_typesize |
| 4928 | if (ptrarith_typesize * ratio == array_typesize) |
| 4929 | size *= llvm::APInt(size.getBitWidth(), ratio); |
| 4930 | } |
| 4931 | } |
| 4932 | |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4933 | if (size.getBitWidth() > index.getBitWidth()) |
Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 4934 | index = index.zext(size.getBitWidth()); |
Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 4935 | else if (size.getBitWidth() < index.getBitWidth()) |
Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 4936 | size = size.zext(index.getBitWidth()); |
Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 4937 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4938 | // For array subscripting the index must be less than size, but for pointer |
| 4939 | // arithmetic also allow the index (offset) to be equal to size since |
| 4940 | // computing the next address after the end of the array is legal and |
| 4941 | // commonly done e.g. in C++ iterators and range-based for loops. |
Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 4942 | if (AllowOnePastEnd ? index.ule(size) : index.ult(size)) |
Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4943 | return; |
| 4944 | |
| 4945 | // Also don't warn for arrays of size 1 which are members of some |
| 4946 | // structure. These are often used to approximate flexible arrays in C89 |
| 4947 | // code. |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4948 | if (IsTailPaddedMemberArray(*this, size, ND)) |
Ted Kremenek | 8fd0a5d | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 4949 | return; |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4950 | |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4951 | // Suppress the warning if the subscript expression (as identified by the |
| 4952 | // ']' location) and the index expression are both from macro expansions |
| 4953 | // within a system header. |
| 4954 | if (ASE) { |
| 4955 | SourceLocation RBracketLoc = SourceMgr.getSpellingLoc( |
| 4956 | ASE->getRBracketLoc()); |
| 4957 | if (SourceMgr.isInSystemHeader(RBracketLoc)) { |
| 4958 | SourceLocation IndexLoc = SourceMgr.getSpellingLoc( |
| 4959 | IndexExpr->getLocStart()); |
| 4960 | if (SourceMgr.isFromSameFile(RBracketLoc, IndexLoc)) |
| 4961 | return; |
| 4962 | } |
| 4963 | } |
| 4964 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4965 | unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds; |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4966 | if (ASE) |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4967 | DiagID = diag::warn_array_index_exceeds_bounds; |
| 4968 | |
| 4969 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, |
| 4970 | PDiag(DiagID) << index.toString(10, true) |
| 4971 | << size.toString(10, true) |
| 4972 | << (unsigned)size.getLimitedValue(~0U) |
| 4973 | << IndexExpr->getSourceRange()); |
Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4974 | } else { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4975 | unsigned DiagID = diag::warn_array_index_precedes_bounds; |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 4976 | if (!ASE) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4977 | DiagID = diag::warn_ptr_arith_precedes_bounds; |
| 4978 | if (index.isNegative()) index = -index; |
| 4979 | } |
| 4980 | |
| 4981 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, |
| 4982 | PDiag(DiagID) << index.toString(10, true) |
| 4983 | << IndexExpr->getSourceRange()); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 4984 | } |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 4985 | |
Matt Beaumont-Gay | cfbc5b5 | 2011-11-29 19:27:11 +0000 | [diff] [blame] | 4986 | if (!ND) { |
| 4987 | // Try harder to find a NamedDecl to point at in the note. |
| 4988 | while (const ArraySubscriptExpr *ASE = |
| 4989 | dyn_cast<ArraySubscriptExpr>(BaseExpr)) |
| 4990 | BaseExpr = ASE->getBase()->IgnoreParenCasts(); |
| 4991 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
| 4992 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); |
| 4993 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) |
| 4994 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); |
| 4995 | } |
| 4996 | |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 4997 | if (ND) |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4998 | DiagRuntimeBehavior(ND->getLocStart(), BaseExpr, |
| 4999 | PDiag(diag::note_array_index_out_of_bounds) |
| 5000 | << ND->getDeclName()); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 5001 | } |
| 5002 | |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5003 | void Sema::CheckArrayAccess(const Expr *expr) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5004 | int AllowOnePastEnd = 0; |
| 5005 | while (expr) { |
| 5006 | expr = expr->IgnoreParenImpCasts(); |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5007 | switch (expr->getStmtClass()) { |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5008 | case Stmt::ArraySubscriptExprClass: { |
| 5009 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr); |
Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 5010 | CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE, |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5011 | AllowOnePastEnd > 0); |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5012 | return; |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5013 | } |
| 5014 | case Stmt::UnaryOperatorClass: { |
| 5015 | // Only unwrap the * and & unary operators |
| 5016 | const UnaryOperator *UO = cast<UnaryOperator>(expr); |
| 5017 | expr = UO->getSubExpr(); |
| 5018 | switch (UO->getOpcode()) { |
| 5019 | case UO_AddrOf: |
| 5020 | AllowOnePastEnd++; |
| 5021 | break; |
| 5022 | case UO_Deref: |
| 5023 | AllowOnePastEnd--; |
| 5024 | break; |
| 5025 | default: |
| 5026 | return; |
| 5027 | } |
| 5028 | break; |
| 5029 | } |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5030 | case Stmt::ConditionalOperatorClass: { |
| 5031 | const ConditionalOperator *cond = cast<ConditionalOperator>(expr); |
| 5032 | if (const Expr *lhs = cond->getLHS()) |
| 5033 | CheckArrayAccess(lhs); |
| 5034 | if (const Expr *rhs = cond->getRHS()) |
| 5035 | CheckArrayAccess(rhs); |
| 5036 | return; |
| 5037 | } |
| 5038 | default: |
| 5039 | return; |
| 5040 | } |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 5041 | } |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5042 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5043 | |
| 5044 | //===--- CHECK: Objective-C retain cycles ----------------------------------// |
| 5045 | |
| 5046 | namespace { |
| 5047 | struct RetainCycleOwner { |
| 5048 | RetainCycleOwner() : Variable(0), Indirect(false) {} |
| 5049 | VarDecl *Variable; |
| 5050 | SourceRange Range; |
| 5051 | SourceLocation Loc; |
| 5052 | bool Indirect; |
| 5053 | |
| 5054 | void setLocsFrom(Expr *e) { |
| 5055 | Loc = e->getExprLoc(); |
| 5056 | Range = e->getSourceRange(); |
| 5057 | } |
| 5058 | }; |
| 5059 | } |
| 5060 | |
| 5061 | /// Consider whether capturing the given variable can possibly lead to |
| 5062 | /// a retain cycle. |
| 5063 | static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) { |
| 5064 | // In ARC, it's captured strongly iff the variable has __strong |
| 5065 | // lifetime. In MRR, it's captured strongly if the variable is |
| 5066 | // __block and has an appropriate type. |
| 5067 | if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 5068 | return false; |
| 5069 | |
| 5070 | owner.Variable = var; |
| 5071 | owner.setLocsFrom(ref); |
| 5072 | return true; |
| 5073 | } |
| 5074 | |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 5075 | static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5076 | while (true) { |
| 5077 | e = e->IgnoreParens(); |
| 5078 | if (CastExpr *cast = dyn_cast<CastExpr>(e)) { |
| 5079 | switch (cast->getCastKind()) { |
| 5080 | case CK_BitCast: |
| 5081 | case CK_LValueBitCast: |
| 5082 | case CK_LValueToRValue: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5083 | case CK_ARCReclaimReturnedObject: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5084 | e = cast->getSubExpr(); |
| 5085 | continue; |
| 5086 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5087 | default: |
| 5088 | return false; |
| 5089 | } |
| 5090 | } |
| 5091 | |
| 5092 | if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) { |
| 5093 | ObjCIvarDecl *ivar = ref->getDecl(); |
| 5094 | if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 5095 | return false; |
| 5096 | |
| 5097 | // Try to find a retain cycle in the base. |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 5098 | if (!findRetainCycleOwner(S, ref->getBase(), owner)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5099 | return false; |
| 5100 | |
| 5101 | if (ref->isFreeIvar()) owner.setLocsFrom(ref); |
| 5102 | owner.Indirect = true; |
| 5103 | return true; |
| 5104 | } |
| 5105 | |
| 5106 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 5107 | VarDecl *var = dyn_cast<VarDecl>(ref->getDecl()); |
| 5108 | if (!var) return false; |
| 5109 | return considerVariable(var, ref, owner); |
| 5110 | } |
| 5111 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5112 | if (MemberExpr *member = dyn_cast<MemberExpr>(e)) { |
| 5113 | if (member->isArrow()) return false; |
| 5114 | |
| 5115 | // Don't count this as an indirect ownership. |
| 5116 | e = member->getBase(); |
| 5117 | continue; |
| 5118 | } |
| 5119 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 5120 | if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { |
| 5121 | // Only pay attention to pseudo-objects on property references. |
| 5122 | ObjCPropertyRefExpr *pre |
| 5123 | = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm() |
| 5124 | ->IgnoreParens()); |
| 5125 | if (!pre) return false; |
| 5126 | if (pre->isImplicitProperty()) return false; |
| 5127 | ObjCPropertyDecl *property = pre->getExplicitProperty(); |
| 5128 | if (!property->isRetaining() && |
| 5129 | !(property->getPropertyIvarDecl() && |
| 5130 | property->getPropertyIvarDecl()->getType() |
| 5131 | .getObjCLifetime() == Qualifiers::OCL_Strong)) |
| 5132 | return false; |
| 5133 | |
| 5134 | owner.Indirect = true; |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 5135 | if (pre->isSuperReceiver()) { |
| 5136 | owner.Variable = S.getCurMethodDecl()->getSelfDecl(); |
| 5137 | if (!owner.Variable) |
| 5138 | return false; |
| 5139 | owner.Loc = pre->getLocation(); |
| 5140 | owner.Range = pre->getSourceRange(); |
| 5141 | return true; |
| 5142 | } |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 5143 | e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase()) |
| 5144 | ->getSourceExpr()); |
| 5145 | continue; |
| 5146 | } |
| 5147 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5148 | // Array ivars? |
| 5149 | |
| 5150 | return false; |
| 5151 | } |
| 5152 | } |
| 5153 | |
| 5154 | namespace { |
| 5155 | struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> { |
| 5156 | FindCaptureVisitor(ASTContext &Context, VarDecl *variable) |
| 5157 | : EvaluatedExprVisitor<FindCaptureVisitor>(Context), |
| 5158 | Variable(variable), Capturer(0) {} |
| 5159 | |
| 5160 | VarDecl *Variable; |
| 5161 | Expr *Capturer; |
| 5162 | |
| 5163 | void VisitDeclRefExpr(DeclRefExpr *ref) { |
| 5164 | if (ref->getDecl() == Variable && !Capturer) |
| 5165 | Capturer = ref; |
| 5166 | } |
| 5167 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5168 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) { |
| 5169 | if (Capturer) return; |
| 5170 | Visit(ref->getBase()); |
| 5171 | if (Capturer && ref->isFreeIvar()) |
| 5172 | Capturer = ref; |
| 5173 | } |
| 5174 | |
| 5175 | void VisitBlockExpr(BlockExpr *block) { |
| 5176 | // Look inside nested blocks |
| 5177 | if (block->getBlockDecl()->capturesVariable(Variable)) |
| 5178 | Visit(block->getBlockDecl()->getBody()); |
| 5179 | } |
| 5180 | }; |
| 5181 | } |
| 5182 | |
| 5183 | /// Check whether the given argument is a block which captures a |
| 5184 | /// variable. |
| 5185 | static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) { |
| 5186 | assert(owner.Variable && owner.Loc.isValid()); |
| 5187 | |
| 5188 | e = e->IgnoreParenCasts(); |
| 5189 | BlockExpr *block = dyn_cast<BlockExpr>(e); |
| 5190 | if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable)) |
| 5191 | return 0; |
| 5192 | |
| 5193 | FindCaptureVisitor visitor(S.Context, owner.Variable); |
| 5194 | visitor.Visit(block->getBlockDecl()->getBody()); |
| 5195 | return visitor.Capturer; |
| 5196 | } |
| 5197 | |
| 5198 | static void diagnoseRetainCycle(Sema &S, Expr *capturer, |
| 5199 | RetainCycleOwner &owner) { |
| 5200 | assert(capturer); |
| 5201 | assert(owner.Variable && owner.Loc.isValid()); |
| 5202 | |
| 5203 | S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle) |
| 5204 | << owner.Variable << capturer->getSourceRange(); |
| 5205 | S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner) |
| 5206 | << owner.Indirect << owner.Range; |
| 5207 | } |
| 5208 | |
| 5209 | /// Check for a keyword selector that starts with the word 'add' or |
| 5210 | /// 'set'. |
| 5211 | static bool isSetterLikeSelector(Selector sel) { |
| 5212 | if (sel.isUnarySelector()) return false; |
| 5213 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5214 | StringRef str = sel.getNameForSlot(0); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5215 | while (!str.empty() && str.front() == '_') str = str.substr(1); |
Ted Kremenek | 968a0ee | 2011-12-01 00:59:21 +0000 | [diff] [blame] | 5216 | if (str.startswith("set")) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5217 | str = str.substr(3); |
Ted Kremenek | 968a0ee | 2011-12-01 00:59:21 +0000 | [diff] [blame] | 5218 | else if (str.startswith("add")) { |
| 5219 | // Specially whitelist 'addOperationWithBlock:'. |
| 5220 | if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock")) |
| 5221 | return false; |
| 5222 | str = str.substr(3); |
| 5223 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5224 | else |
| 5225 | return false; |
| 5226 | |
| 5227 | if (str.empty()) return true; |
| 5228 | return !islower(str.front()); |
| 5229 | } |
| 5230 | |
| 5231 | /// Check a message send to see if it's likely to cause a retain cycle. |
| 5232 | void Sema::checkRetainCycles(ObjCMessageExpr *msg) { |
| 5233 | // Only check instance methods whose selector looks like a setter. |
| 5234 | if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector())) |
| 5235 | return; |
| 5236 | |
| 5237 | // Try to find a variable that the receiver is strongly owned by. |
| 5238 | RetainCycleOwner owner; |
| 5239 | if (msg->getReceiverKind() == ObjCMessageExpr::Instance) { |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 5240 | if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5241 | return; |
| 5242 | } else { |
| 5243 | assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance); |
| 5244 | owner.Variable = getCurMethodDecl()->getSelfDecl(); |
| 5245 | owner.Loc = msg->getSuperLoc(); |
| 5246 | owner.Range = msg->getSuperLoc(); |
| 5247 | } |
| 5248 | |
| 5249 | // Check whether the receiver is captured by any of the arguments. |
| 5250 | for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) |
| 5251 | if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) |
| 5252 | return diagnoseRetainCycle(*this, capturer, owner); |
| 5253 | } |
| 5254 | |
| 5255 | /// Check a property assign to see if it's likely to cause a retain cycle. |
| 5256 | void Sema::checkRetainCycles(Expr *receiver, Expr *argument) { |
| 5257 | RetainCycleOwner owner; |
Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 5258 | if (!findRetainCycleOwner(*this, receiver, owner)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5259 | return; |
| 5260 | |
| 5261 | if (Expr *capturer = findCapturingExpr(*this, argument, owner)) |
| 5262 | diagnoseRetainCycle(*this, capturer, owner); |
| 5263 | } |
| 5264 | |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5265 | bool Sema::checkUnsafeAssigns(SourceLocation Loc, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5266 | QualType LHS, Expr *RHS) { |
| 5267 | Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime(); |
| 5268 | if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone) |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5269 | return false; |
| 5270 | // strip off any implicit cast added to get to the one arc-specific |
| 5271 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5272 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5273 | Diag(Loc, diag::warn_arc_retained_assign) |
Fariborz Jahanian | bd2e27e | 2012-07-06 21:09:27 +0000 | [diff] [blame] | 5274 | << (LT == Qualifiers::OCL_ExplicitNone) << 1 |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5275 | << RHS->getSourceRange(); |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5276 | return true; |
| 5277 | } |
| 5278 | RHS = cast->getSubExpr(); |
| 5279 | } |
| 5280 | return false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5281 | } |
| 5282 | |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5283 | void Sema::checkUnsafeExprAssigns(SourceLocation Loc, |
| 5284 | Expr *LHS, Expr *RHS) { |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 5285 | QualType LHSType; |
| 5286 | // PropertyRef on LHS type need be directly obtained from |
| 5287 | // its declaration as it has a PsuedoType. |
| 5288 | ObjCPropertyRefExpr *PRE |
| 5289 | = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens()); |
| 5290 | if (PRE && !PRE->isImplicitProperty()) { |
| 5291 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
| 5292 | if (PD) |
| 5293 | LHSType = PD->getType(); |
| 5294 | } |
| 5295 | |
| 5296 | if (LHSType.isNull()) |
| 5297 | LHSType = LHS->getType(); |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5298 | if (checkUnsafeAssigns(Loc, LHSType, RHS)) |
| 5299 | return; |
| 5300 | Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime(); |
| 5301 | // FIXME. Check for other life times. |
| 5302 | if (LT != Qualifiers::OCL_None) |
| 5303 | return; |
| 5304 | |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 5305 | if (PRE) { |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5306 | if (PRE->isImplicitProperty()) |
| 5307 | return; |
| 5308 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
| 5309 | if (!PD) |
| 5310 | return; |
| 5311 | |
| 5312 | unsigned Attributes = PD->getPropertyAttributes(); |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 5313 | if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) { |
| 5314 | // when 'assign' attribute was not explicitly specified |
| 5315 | // by user, ignore it and rely on property type itself |
| 5316 | // for lifetime info. |
| 5317 | unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten(); |
| 5318 | if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) && |
| 5319 | LHSType->isObjCRetainableType()) |
| 5320 | return; |
| 5321 | |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5322 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5323 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5324 | Diag(Loc, diag::warn_arc_retained_property_assign) |
| 5325 | << RHS->getSourceRange(); |
| 5326 | return; |
| 5327 | } |
| 5328 | RHS = cast->getSubExpr(); |
| 5329 | } |
Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 5330 | } |
Fariborz Jahanian | bd2e27e | 2012-07-06 21:09:27 +0000 | [diff] [blame] | 5331 | else if (Attributes & ObjCPropertyDecl::OBJC_PR_weak) { |
| 5332 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
| 5333 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
| 5334 | Diag(Loc, diag::warn_arc_retained_assign) |
| 5335 | << 0 << 0<< RHS->getSourceRange(); |
| 5336 | return; |
| 5337 | } |
| 5338 | RHS = cast->getSubExpr(); |
| 5339 | } |
| 5340 | } |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5341 | } |
| 5342 | } |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 5343 | |
| 5344 | //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===// |
| 5345 | |
| 5346 | namespace { |
| 5347 | bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr, |
| 5348 | SourceLocation StmtLoc, |
| 5349 | const NullStmt *Body) { |
| 5350 | // Do not warn if the body is a macro that expands to nothing, e.g: |
| 5351 | // |
| 5352 | // #define CALL(x) |
| 5353 | // if (condition) |
| 5354 | // CALL(0); |
| 5355 | // |
| 5356 | if (Body->hasLeadingEmptyMacro()) |
| 5357 | return false; |
| 5358 | |
| 5359 | // Get line numbers of statement and body. |
| 5360 | bool StmtLineInvalid; |
| 5361 | unsigned StmtLine = SourceMgr.getSpellingLineNumber(StmtLoc, |
| 5362 | &StmtLineInvalid); |
| 5363 | if (StmtLineInvalid) |
| 5364 | return false; |
| 5365 | |
| 5366 | bool BodyLineInvalid; |
| 5367 | unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(), |
| 5368 | &BodyLineInvalid); |
| 5369 | if (BodyLineInvalid) |
| 5370 | return false; |
| 5371 | |
| 5372 | // Warn if null statement and body are on the same line. |
| 5373 | if (StmtLine != BodyLine) |
| 5374 | return false; |
| 5375 | |
| 5376 | return true; |
| 5377 | } |
| 5378 | } // Unnamed namespace |
| 5379 | |
| 5380 | void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc, |
| 5381 | const Stmt *Body, |
| 5382 | unsigned DiagID) { |
| 5383 | // Since this is a syntactic check, don't emit diagnostic for template |
| 5384 | // instantiations, this just adds noise. |
| 5385 | if (CurrentInstantiationScope) |
| 5386 | return; |
| 5387 | |
| 5388 | // The body should be a null statement. |
| 5389 | const NullStmt *NBody = dyn_cast<NullStmt>(Body); |
| 5390 | if (!NBody) |
| 5391 | return; |
| 5392 | |
| 5393 | // Do the usual checks. |
| 5394 | if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) |
| 5395 | return; |
| 5396 | |
| 5397 | Diag(NBody->getSemiLoc(), DiagID); |
| 5398 | Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); |
| 5399 | } |
| 5400 | |
| 5401 | void Sema::DiagnoseEmptyLoopBody(const Stmt *S, |
| 5402 | const Stmt *PossibleBody) { |
| 5403 | assert(!CurrentInstantiationScope); // Ensured by caller |
| 5404 | |
| 5405 | SourceLocation StmtLoc; |
| 5406 | const Stmt *Body; |
| 5407 | unsigned DiagID; |
| 5408 | if (const ForStmt *FS = dyn_cast<ForStmt>(S)) { |
| 5409 | StmtLoc = FS->getRParenLoc(); |
| 5410 | Body = FS->getBody(); |
| 5411 | DiagID = diag::warn_empty_for_body; |
| 5412 | } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) { |
| 5413 | StmtLoc = WS->getCond()->getSourceRange().getEnd(); |
| 5414 | Body = WS->getBody(); |
| 5415 | DiagID = diag::warn_empty_while_body; |
| 5416 | } else |
| 5417 | return; // Neither `for' nor `while'. |
| 5418 | |
| 5419 | // The body should be a null statement. |
| 5420 | const NullStmt *NBody = dyn_cast<NullStmt>(Body); |
| 5421 | if (!NBody) |
| 5422 | return; |
| 5423 | |
| 5424 | // Skip expensive checks if diagnostic is disabled. |
| 5425 | if (Diags.getDiagnosticLevel(DiagID, NBody->getSemiLoc()) == |
| 5426 | DiagnosticsEngine::Ignored) |
| 5427 | return; |
| 5428 | |
| 5429 | // Do the usual checks. |
| 5430 | if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) |
| 5431 | return; |
| 5432 | |
| 5433 | // `for(...);' and `while(...);' are popular idioms, so in order to keep |
| 5434 | // noise level low, emit diagnostics only if for/while is followed by a |
| 5435 | // CompoundStmt, e.g.: |
| 5436 | // for (int i = 0; i < n; i++); |
| 5437 | // { |
| 5438 | // a(i); |
| 5439 | // } |
| 5440 | // or if for/while is followed by a statement with more indentation |
| 5441 | // than for/while itself: |
| 5442 | // for (int i = 0; i < n; i++); |
| 5443 | // a(i); |
| 5444 | bool ProbableTypo = isa<CompoundStmt>(PossibleBody); |
| 5445 | if (!ProbableTypo) { |
| 5446 | bool BodyColInvalid; |
| 5447 | unsigned BodyCol = SourceMgr.getPresumedColumnNumber( |
| 5448 | PossibleBody->getLocStart(), |
| 5449 | &BodyColInvalid); |
| 5450 | if (BodyColInvalid) |
| 5451 | return; |
| 5452 | |
| 5453 | bool StmtColInvalid; |
| 5454 | unsigned StmtCol = SourceMgr.getPresumedColumnNumber( |
| 5455 | S->getLocStart(), |
| 5456 | &StmtColInvalid); |
| 5457 | if (StmtColInvalid) |
| 5458 | return; |
| 5459 | |
| 5460 | if (BodyCol > StmtCol) |
| 5461 | ProbableTypo = true; |
| 5462 | } |
| 5463 | |
| 5464 | if (ProbableTypo) { |
| 5465 | Diag(NBody->getSemiLoc(), DiagID); |
| 5466 | Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); |
| 5467 | } |
| 5468 | } |