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