Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1 | //===--- SemaChecking.cpp - Extra Semantic Checking -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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 | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10 | // This file implements extra semantic analysis beyond what is enforced |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 11 | // by the C type system. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 15 | #include "clang/Sema/SemaInternal.h" |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 17 | #include "clang/AST/CharUnits.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/EvaluatedExprVisitor.h" |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Ted Kremenek | c81614d | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
Ted Kremenek | 34f664d | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprObjC.h" |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 24 | #include "clang/AST/StmtCXX.h" |
| 25 | #include "clang/AST/StmtObjC.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 26 | #include "clang/Analysis/Analyses/FormatString.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 27 | #include "clang/Basic/CharInfo.h" |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 28 | #include "clang/Basic/TargetBuiltins.h" |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 29 | #include "clang/Basic/TargetInfo.h" |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 30 | #include "clang/Lex/Lexer.h" // TODO: Extract static functions to fix layering. |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 31 | #include "clang/Sema/Initialization.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 32 | #include "clang/Sema/Lookup.h" |
| 33 | #include "clang/Sema/ScopeInfo.h" |
| 34 | #include "clang/Sema/Sema.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/STLExtras.h" |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/SmallBitVector.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/SmallString.h" |
Dmitri Gribenko | 9feeef4 | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 38 | #include "llvm/Support/ConvertUTF.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 39 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 050379b | 2009-05-20 01:55:10 +0000 | [diff] [blame] | 40 | #include <limits> |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 41 | using namespace clang; |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 42 | using namespace sema; |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 43 | |
Chris Lattner | a26fb34 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 44 | SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, |
| 45 | unsigned ByteNo) const { |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 46 | return SL->getLocationOfByte(ByteNo, getSourceManager(), LangOpts, |
| 47 | Context.getTargetInfo()); |
Chris Lattner | a26fb34 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 48 | } |
| 49 | |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 50 | /// Checks that a call expression's argument count is the desired number. |
| 51 | /// This is useful when doing custom type-checking. Returns true on error. |
| 52 | static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) { |
| 53 | unsigned argCount = call->getNumArgs(); |
| 54 | if (argCount == desiredArgCount) return false; |
| 55 | |
| 56 | if (argCount < desiredArgCount) |
| 57 | return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 58 | << 0 /*function call*/ << desiredArgCount << argCount |
| 59 | << call->getSourceRange(); |
| 60 | |
| 61 | // Highlight all the excess arguments. |
| 62 | SourceRange range(call->getArg(desiredArgCount)->getLocStart(), |
| 63 | call->getArg(argCount - 1)->getLocEnd()); |
| 64 | |
| 65 | return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args) |
| 66 | << 0 /*function call*/ << desiredArgCount << argCount |
| 67 | << call->getArg(1)->getSourceRange(); |
| 68 | } |
| 69 | |
Julien Lerouge | 4a5b444 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 70 | /// Check that the first argument to __builtin_annotation is an integer |
| 71 | /// and the second argument is a non-wide string literal. |
| 72 | static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) { |
| 73 | if (checkArgCount(S, TheCall, 2)) |
| 74 | return true; |
| 75 | |
| 76 | // First argument should be an integer. |
| 77 | Expr *ValArg = TheCall->getArg(0); |
| 78 | QualType Ty = ValArg->getType(); |
| 79 | if (!Ty->isIntegerType()) { |
| 80 | S.Diag(ValArg->getLocStart(), diag::err_builtin_annotation_first_arg) |
| 81 | << ValArg->getSourceRange(); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 82 | return true; |
| 83 | } |
Julien Lerouge | 4a5b444 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 84 | |
| 85 | // Second argument should be a constant string. |
| 86 | Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts(); |
| 87 | StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg); |
| 88 | if (!Literal || !Literal->isAscii()) { |
| 89 | S.Diag(StrArg->getLocStart(), diag::err_builtin_annotation_second_arg) |
| 90 | << StrArg->getSourceRange(); |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | TheCall->setType(Ty); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 98 | /// Check that the argument to __builtin_addressof is a glvalue, and set the |
| 99 | /// result type to the corresponding pointer type. |
| 100 | static bool SemaBuiltinAddressof(Sema &S, CallExpr *TheCall) { |
| 101 | if (checkArgCount(S, TheCall, 1)) |
| 102 | return true; |
| 103 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 104 | ExprResult Arg(TheCall->getArg(0)); |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 105 | QualType ResultType = S.CheckAddressOfOperand(Arg, TheCall->getLocStart()); |
| 106 | if (ResultType.isNull()) |
| 107 | return true; |
| 108 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 109 | TheCall->setArg(0, Arg.get()); |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 110 | TheCall->setType(ResultType); |
| 111 | return false; |
| 112 | } |
| 113 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 114 | ExprResult |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 115 | Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 116 | ExprResult TheCallResult(TheCall); |
Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 3be167f | 2010-10-01 23:23:24 +0000 | [diff] [blame] | 118 | // Find out if any arguments are required to be integer constant expressions. |
| 119 | unsigned ICEArguments = 0; |
| 120 | ASTContext::GetBuiltinTypeError Error; |
| 121 | Context.GetBuiltinType(BuiltinID, Error, &ICEArguments); |
| 122 | if (Error != ASTContext::GE_None) |
| 123 | ICEArguments = 0; // Don't diagnose previously diagnosed errors. |
| 124 | |
| 125 | // If any arguments are required to be ICE's, check and diagnose. |
| 126 | for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) { |
| 127 | // Skip arguments not required to be ICE's. |
| 128 | if ((ICEArguments & (1 << ArgNo)) == 0) continue; |
| 129 | |
| 130 | llvm::APSInt Result; |
| 131 | if (SemaBuiltinConstantArg(TheCall, ArgNo, Result)) |
| 132 | return true; |
| 133 | ICEArguments &= ~(1 << ArgNo); |
| 134 | } |
| 135 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 136 | switch (BuiltinID) { |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 137 | case Builtin::BI__builtin___CFStringMakeConstantString: |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 138 | assert(TheCall->getNumArgs() == 1 && |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 139 | "Wrong # arguments to builtin CFStringMakeConstantString"); |
Chris Lattner | 6436fb6 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 140 | if (CheckObjCString(TheCall->getArg(0))) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 141 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 142 | break; |
Ted Kremenek | a174c52 | 2008-07-09 17:58:53 +0000 | [diff] [blame] | 143 | case Builtin::BI__builtin_stdarg_start: |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 144 | case Builtin::BI__builtin_va_start: |
Reid Kleckner | 597e81d | 2014-03-26 15:38:33 +0000 | [diff] [blame] | 145 | case Builtin::BI__va_start: |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 146 | if (SemaBuiltinVAStart(TheCall)) |
| 147 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 148 | break; |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 149 | case Builtin::BI__builtin_isgreater: |
| 150 | case Builtin::BI__builtin_isgreaterequal: |
| 151 | case Builtin::BI__builtin_isless: |
| 152 | case Builtin::BI__builtin_islessequal: |
| 153 | case Builtin::BI__builtin_islessgreater: |
| 154 | case Builtin::BI__builtin_isunordered: |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 155 | if (SemaBuiltinUnorderedCompare(TheCall)) |
| 156 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 157 | break; |
Benjamin Kramer | 634fc10 | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 158 | case Builtin::BI__builtin_fpclassify: |
| 159 | if (SemaBuiltinFPClassification(TheCall, 6)) |
| 160 | return ExprError(); |
| 161 | break; |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 162 | case Builtin::BI__builtin_isfinite: |
| 163 | case Builtin::BI__builtin_isinf: |
| 164 | case Builtin::BI__builtin_isinf_sign: |
| 165 | case Builtin::BI__builtin_isnan: |
| 166 | case Builtin::BI__builtin_isnormal: |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 167 | if (SemaBuiltinFPClassification(TheCall, 1)) |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 168 | return ExprError(); |
| 169 | break; |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 170 | case Builtin::BI__builtin_shufflevector: |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 171 | return SemaBuiltinShuffleVector(TheCall); |
| 172 | // TheCall will be freed by the smart pointer here, but that's fine, since |
| 173 | // SemaBuiltinShuffleVector guts it, but then doesn't release it. |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 174 | case Builtin::BI__builtin_prefetch: |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 175 | if (SemaBuiltinPrefetch(TheCall)) |
| 176 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 177 | break; |
Daniel Dunbar | b0d34c8 | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 178 | case Builtin::BI__builtin_object_size: |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 179 | if (SemaBuiltinConstantArgRange(TheCall, 1, 0, 3)) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 180 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 181 | break; |
Eli Friedman | eed8ad2 | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 182 | case Builtin::BI__builtin_longjmp: |
| 183 | if (SemaBuiltinLongjmp(TheCall)) |
| 184 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 185 | break; |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 186 | |
| 187 | case Builtin::BI__builtin_classify_type: |
| 188 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 189 | TheCall->setType(Context.IntTy); |
| 190 | break; |
Chris Lattner | 17c0eac | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 191 | case Builtin::BI__builtin_constant_p: |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 192 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 193 | TheCall->setType(Context.IntTy); |
Chris Lattner | 17c0eac | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 194 | break; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 195 | case Builtin::BI__sync_fetch_and_add: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 196 | case Builtin::BI__sync_fetch_and_add_1: |
| 197 | case Builtin::BI__sync_fetch_and_add_2: |
| 198 | case Builtin::BI__sync_fetch_and_add_4: |
| 199 | case Builtin::BI__sync_fetch_and_add_8: |
| 200 | case Builtin::BI__sync_fetch_and_add_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 201 | case Builtin::BI__sync_fetch_and_sub: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 202 | case Builtin::BI__sync_fetch_and_sub_1: |
| 203 | case Builtin::BI__sync_fetch_and_sub_2: |
| 204 | case Builtin::BI__sync_fetch_and_sub_4: |
| 205 | case Builtin::BI__sync_fetch_and_sub_8: |
| 206 | case Builtin::BI__sync_fetch_and_sub_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 207 | case Builtin::BI__sync_fetch_and_or: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 208 | case Builtin::BI__sync_fetch_and_or_1: |
| 209 | case Builtin::BI__sync_fetch_and_or_2: |
| 210 | case Builtin::BI__sync_fetch_and_or_4: |
| 211 | case Builtin::BI__sync_fetch_and_or_8: |
| 212 | case Builtin::BI__sync_fetch_and_or_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 213 | case Builtin::BI__sync_fetch_and_and: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 214 | case Builtin::BI__sync_fetch_and_and_1: |
| 215 | case Builtin::BI__sync_fetch_and_and_2: |
| 216 | case Builtin::BI__sync_fetch_and_and_4: |
| 217 | case Builtin::BI__sync_fetch_and_and_8: |
| 218 | case Builtin::BI__sync_fetch_and_and_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 219 | case Builtin::BI__sync_fetch_and_xor: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 220 | case Builtin::BI__sync_fetch_and_xor_1: |
| 221 | case Builtin::BI__sync_fetch_and_xor_2: |
| 222 | case Builtin::BI__sync_fetch_and_xor_4: |
| 223 | case Builtin::BI__sync_fetch_and_xor_8: |
| 224 | case Builtin::BI__sync_fetch_and_xor_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 225 | case Builtin::BI__sync_add_and_fetch: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 226 | case Builtin::BI__sync_add_and_fetch_1: |
| 227 | case Builtin::BI__sync_add_and_fetch_2: |
| 228 | case Builtin::BI__sync_add_and_fetch_4: |
| 229 | case Builtin::BI__sync_add_and_fetch_8: |
| 230 | case Builtin::BI__sync_add_and_fetch_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 231 | case Builtin::BI__sync_sub_and_fetch: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 232 | case Builtin::BI__sync_sub_and_fetch_1: |
| 233 | case Builtin::BI__sync_sub_and_fetch_2: |
| 234 | case Builtin::BI__sync_sub_and_fetch_4: |
| 235 | case Builtin::BI__sync_sub_and_fetch_8: |
| 236 | case Builtin::BI__sync_sub_and_fetch_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 237 | case Builtin::BI__sync_and_and_fetch: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 238 | case Builtin::BI__sync_and_and_fetch_1: |
| 239 | case Builtin::BI__sync_and_and_fetch_2: |
| 240 | case Builtin::BI__sync_and_and_fetch_4: |
| 241 | case Builtin::BI__sync_and_and_fetch_8: |
| 242 | case Builtin::BI__sync_and_and_fetch_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 243 | case Builtin::BI__sync_or_and_fetch: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 244 | case Builtin::BI__sync_or_and_fetch_1: |
| 245 | case Builtin::BI__sync_or_and_fetch_2: |
| 246 | case Builtin::BI__sync_or_and_fetch_4: |
| 247 | case Builtin::BI__sync_or_and_fetch_8: |
| 248 | case Builtin::BI__sync_or_and_fetch_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 249 | case Builtin::BI__sync_xor_and_fetch: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 250 | case Builtin::BI__sync_xor_and_fetch_1: |
| 251 | case Builtin::BI__sync_xor_and_fetch_2: |
| 252 | case Builtin::BI__sync_xor_and_fetch_4: |
| 253 | case Builtin::BI__sync_xor_and_fetch_8: |
| 254 | case Builtin::BI__sync_xor_and_fetch_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 255 | case Builtin::BI__sync_val_compare_and_swap: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 256 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 257 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 258 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 259 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 260 | case Builtin::BI__sync_val_compare_and_swap_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 261 | case Builtin::BI__sync_bool_compare_and_swap: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 262 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 263 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 264 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 265 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 266 | case Builtin::BI__sync_bool_compare_and_swap_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 267 | case Builtin::BI__sync_lock_test_and_set: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 268 | case Builtin::BI__sync_lock_test_and_set_1: |
| 269 | case Builtin::BI__sync_lock_test_and_set_2: |
| 270 | case Builtin::BI__sync_lock_test_and_set_4: |
| 271 | case Builtin::BI__sync_lock_test_and_set_8: |
| 272 | case Builtin::BI__sync_lock_test_and_set_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 273 | case Builtin::BI__sync_lock_release: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 274 | case Builtin::BI__sync_lock_release_1: |
| 275 | case Builtin::BI__sync_lock_release_2: |
| 276 | case Builtin::BI__sync_lock_release_4: |
| 277 | case Builtin::BI__sync_lock_release_8: |
| 278 | case Builtin::BI__sync_lock_release_16: |
Chris Lattner | 9cb59fa | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 279 | case Builtin::BI__sync_swap: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 280 | case Builtin::BI__sync_swap_1: |
| 281 | case Builtin::BI__sync_swap_2: |
| 282 | case Builtin::BI__sync_swap_4: |
| 283 | case Builtin::BI__sync_swap_8: |
| 284 | case Builtin::BI__sync_swap_16: |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 285 | return SemaBuiltinAtomicOverloaded(TheCallResult); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 286 | #define BUILTIN(ID, TYPE, ATTRS) |
| 287 | #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ |
| 288 | case Builtin::BI##ID: \ |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 289 | return SemaAtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 290 | #include "clang/Basic/Builtins.def" |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 291 | case Builtin::BI__builtin_annotation: |
Julien Lerouge | 4a5b444 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 292 | if (SemaBuiltinAnnotation(*this, TheCall)) |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 293 | return ExprError(); |
| 294 | break; |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 295 | case Builtin::BI__builtin_addressof: |
| 296 | if (SemaBuiltinAddressof(*this, TheCall)) |
| 297 | return ExprError(); |
| 298 | break; |
Richard Smith | 760520b | 2014-06-03 23:27:44 +0000 | [diff] [blame] | 299 | case Builtin::BI__builtin_operator_new: |
| 300 | case Builtin::BI__builtin_operator_delete: |
| 301 | if (!getLangOpts().CPlusPlus) { |
| 302 | Diag(TheCall->getExprLoc(), diag::err_builtin_requires_language) |
| 303 | << (BuiltinID == Builtin::BI__builtin_operator_new |
| 304 | ? "__builtin_operator_new" |
| 305 | : "__builtin_operator_delete") |
| 306 | << "C++"; |
| 307 | return ExprError(); |
| 308 | } |
| 309 | // CodeGen assumes it can find the global new and delete to call, |
| 310 | // so ensure that they are declared. |
| 311 | DeclareGlobalNewDelete(); |
| 312 | break; |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 313 | } |
Richard Smith | 760520b | 2014-06-03 23:27:44 +0000 | [diff] [blame] | 314 | |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 315 | // Since the target specific builtins for each arch overlap, only check those |
| 316 | // of the arch we are compiling for. |
| 317 | if (BuiltinID >= Builtin::FirstTSBuiltin) { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 318 | switch (Context.getTargetInfo().getTriple().getArch()) { |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 319 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 320 | case llvm::Triple::armeb: |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 321 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 322 | case llvm::Triple::thumbeb: |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 323 | if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall)) |
| 324 | return ExprError(); |
| 325 | break; |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 326 | case llvm::Triple::aarch64: |
| 327 | case llvm::Triple::aarch64_be: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 328 | case llvm::Triple::arm64: |
James Molloy | fa40368 | 2014-04-30 10:11:40 +0000 | [diff] [blame] | 329 | case llvm::Triple::arm64_be: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 330 | if (CheckAArch64BuiltinFunctionCall(BuiltinID, TheCall)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 331 | return ExprError(); |
| 332 | break; |
Simon Atanasyan | ecedf3d | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 333 | case llvm::Triple::mips: |
| 334 | case llvm::Triple::mipsel: |
| 335 | case llvm::Triple::mips64: |
| 336 | case llvm::Triple::mips64el: |
| 337 | if (CheckMipsBuiltinFunctionCall(BuiltinID, TheCall)) |
| 338 | return ExprError(); |
| 339 | break; |
Warren Hunt | 20e4a5d | 2014-02-21 23:08:53 +0000 | [diff] [blame] | 340 | case llvm::Triple::x86: |
| 341 | case llvm::Triple::x86_64: |
| 342 | if (CheckX86BuiltinFunctionCall(BuiltinID, TheCall)) |
| 343 | return ExprError(); |
| 344 | break; |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 345 | default: |
| 346 | break; |
| 347 | } |
| 348 | } |
| 349 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 350 | return TheCallResult; |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 353 | // Get the valid immediate range for the specified NEON type code. |
Tim Northover | 3402dc7 | 2014-02-12 12:04:59 +0000 | [diff] [blame] | 354 | static unsigned RFT(unsigned t, bool shift = false, bool ForceQuad = false) { |
Bob Wilson | 98bc98c | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 355 | NeonTypeFlags Type(t); |
Tim Northover | 3402dc7 | 2014-02-12 12:04:59 +0000 | [diff] [blame] | 356 | int IsQuad = ForceQuad ? true : Type.isQuad(); |
Bob Wilson | 98bc98c | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 357 | switch (Type.getEltType()) { |
| 358 | case NeonTypeFlags::Int8: |
| 359 | case NeonTypeFlags::Poly8: |
| 360 | return shift ? 7 : (8 << IsQuad) - 1; |
| 361 | case NeonTypeFlags::Int16: |
| 362 | case NeonTypeFlags::Poly16: |
| 363 | return shift ? 15 : (4 << IsQuad) - 1; |
| 364 | case NeonTypeFlags::Int32: |
| 365 | return shift ? 31 : (2 << IsQuad) - 1; |
| 366 | case NeonTypeFlags::Int64: |
Kevin Qin | caac85e | 2013-11-14 03:29:16 +0000 | [diff] [blame] | 367 | case NeonTypeFlags::Poly64: |
Bob Wilson | 98bc98c | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 368 | return shift ? 63 : (1 << IsQuad) - 1; |
Kevin Qin | fb79d7f | 2013-12-10 06:49:01 +0000 | [diff] [blame] | 369 | case NeonTypeFlags::Poly128: |
| 370 | return shift ? 127 : (1 << IsQuad) - 1; |
Bob Wilson | 98bc98c | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 371 | case NeonTypeFlags::Float16: |
| 372 | assert(!shift && "cannot shift float types!"); |
| 373 | return (4 << IsQuad) - 1; |
| 374 | case NeonTypeFlags::Float32: |
| 375 | assert(!shift && "cannot shift float types!"); |
| 376 | return (2 << IsQuad) - 1; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 377 | case NeonTypeFlags::Float64: |
| 378 | assert(!shift && "cannot shift float types!"); |
| 379 | return (1 << IsQuad) - 1; |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 380 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 381 | llvm_unreachable("Invalid NeonTypeFlag!"); |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 384 | /// getNeonEltType - Return the QualType corresponding to the elements of |
| 385 | /// the vector type specified by the NeonTypeFlags. This is used to check |
| 386 | /// the pointer arguments for Neon load/store intrinsics. |
Kevin Qin | caac85e | 2013-11-14 03:29:16 +0000 | [diff] [blame] | 387 | static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context, |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 388 | bool IsPolyUnsigned, bool IsInt64Long) { |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 389 | switch (Flags.getEltType()) { |
| 390 | case NeonTypeFlags::Int8: |
| 391 | return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy; |
| 392 | case NeonTypeFlags::Int16: |
| 393 | return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy; |
| 394 | case NeonTypeFlags::Int32: |
| 395 | return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy; |
| 396 | case NeonTypeFlags::Int64: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 397 | if (IsInt64Long) |
Kevin Qin | ad64f6d | 2014-02-24 02:45:03 +0000 | [diff] [blame] | 398 | return Flags.isUnsigned() ? Context.UnsignedLongTy : Context.LongTy; |
| 399 | else |
| 400 | return Flags.isUnsigned() ? Context.UnsignedLongLongTy |
| 401 | : Context.LongLongTy; |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 402 | case NeonTypeFlags::Poly8: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 403 | return IsPolyUnsigned ? Context.UnsignedCharTy : Context.SignedCharTy; |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 404 | case NeonTypeFlags::Poly16: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 405 | return IsPolyUnsigned ? Context.UnsignedShortTy : Context.ShortTy; |
Kevin Qin | caac85e | 2013-11-14 03:29:16 +0000 | [diff] [blame] | 406 | case NeonTypeFlags::Poly64: |
Kevin Qin | ad64f6d | 2014-02-24 02:45:03 +0000 | [diff] [blame] | 407 | return Context.UnsignedLongTy; |
Kevin Qin | fb79d7f | 2013-12-10 06:49:01 +0000 | [diff] [blame] | 408 | case NeonTypeFlags::Poly128: |
| 409 | break; |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 410 | case NeonTypeFlags::Float16: |
Kevin Qin | caac85e | 2013-11-14 03:29:16 +0000 | [diff] [blame] | 411 | return Context.HalfTy; |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 412 | case NeonTypeFlags::Float32: |
| 413 | return Context.FloatTy; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 414 | case NeonTypeFlags::Float64: |
| 415 | return Context.DoubleTy; |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 416 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 417 | llvm_unreachable("Invalid NeonTypeFlag!"); |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 420 | bool Sema::CheckNeonBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 421 | llvm::APSInt Result; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 422 | uint64_t mask = 0; |
| 423 | unsigned TV = 0; |
| 424 | int PtrArgNum = -1; |
| 425 | bool HasConstPtr = false; |
| 426 | switch (BuiltinID) { |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 427 | #define GET_NEON_OVERLOAD_CHECK |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 428 | #include "clang/Basic/arm_neon.inc" |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 429 | #undef GET_NEON_OVERLOAD_CHECK |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | // For NEON intrinsics which are overloaded on vector element type, validate |
| 433 | // the immediate which specifies which variant to emit. |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 434 | unsigned ImmArg = TheCall->getNumArgs()-1; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 435 | if (mask) { |
| 436 | if (SemaBuiltinConstantArg(TheCall, ImmArg, Result)) |
| 437 | return true; |
| 438 | |
| 439 | TV = Result.getLimitedValue(64); |
| 440 | if ((TV > 63) || (mask & (1ULL << TV)) == 0) |
| 441 | return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code) |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 442 | << TheCall->getArg(ImmArg)->getSourceRange(); |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | if (PtrArgNum >= 0) { |
| 446 | // Check that pointer arguments have the specified type. |
| 447 | Expr *Arg = TheCall->getArg(PtrArgNum); |
| 448 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) |
| 449 | Arg = ICE->getSubExpr(); |
| 450 | ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg); |
| 451 | QualType RHSTy = RHS.get()->getType(); |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 452 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 453 | llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch(); |
| 454 | bool IsPolyUnsigned = |
| 455 | Arch == llvm::Triple::aarch64 || Arch == llvm::Triple::arm64; |
| 456 | bool IsInt64Long = |
| 457 | Context.getTargetInfo().getInt64Type() == TargetInfo::SignedLong; |
| 458 | QualType EltTy = |
| 459 | getNeonEltType(NeonTypeFlags(TV), Context, IsPolyUnsigned, IsInt64Long); |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 460 | if (HasConstPtr) |
| 461 | EltTy = EltTy.withConst(); |
| 462 | QualType LHSTy = Context.getPointerType(EltTy); |
| 463 | AssignConvertType ConvTy; |
| 464 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
| 465 | if (RHS.isInvalid()) |
| 466 | return true; |
| 467 | if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), LHSTy, RHSTy, |
| 468 | RHS.get(), AA_Assigning)) |
| 469 | return true; |
| 470 | } |
| 471 | |
| 472 | // For NEON intrinsics which take an immediate value as part of the |
| 473 | // instruction, range check them here. |
| 474 | unsigned i = 0, l = 0, u = 0; |
| 475 | switch (BuiltinID) { |
| 476 | default: |
| 477 | return false; |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 478 | #define GET_NEON_IMMEDIATE_CHECK |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 479 | #include "clang/Basic/arm_neon.inc" |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 480 | #undef GET_NEON_IMMEDIATE_CHECK |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 481 | } |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 482 | |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 483 | return SemaBuiltinConstantArgRange(TheCall, i, l, u + l); |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 486 | bool Sema::CheckARMBuiltinExclusiveCall(unsigned BuiltinID, CallExpr *TheCall, |
| 487 | unsigned MaxWidth) { |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 488 | assert((BuiltinID == ARM::BI__builtin_arm_ldrex || |
Tim Northover | 3acd6bd | 2014-07-02 12:56:02 +0000 | [diff] [blame] | 489 | BuiltinID == ARM::BI__builtin_arm_ldaex || |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 490 | BuiltinID == ARM::BI__builtin_arm_strex || |
Tim Northover | 3acd6bd | 2014-07-02 12:56:02 +0000 | [diff] [blame] | 491 | BuiltinID == ARM::BI__builtin_arm_stlex || |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 492 | BuiltinID == AArch64::BI__builtin_arm_ldrex || |
Tim Northover | 3acd6bd | 2014-07-02 12:56:02 +0000 | [diff] [blame] | 493 | BuiltinID == AArch64::BI__builtin_arm_ldaex || |
| 494 | BuiltinID == AArch64::BI__builtin_arm_strex || |
| 495 | BuiltinID == AArch64::BI__builtin_arm_stlex) && |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 496 | "unexpected ARM builtin"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 497 | bool IsLdrex = BuiltinID == ARM::BI__builtin_arm_ldrex || |
Tim Northover | 3acd6bd | 2014-07-02 12:56:02 +0000 | [diff] [blame] | 498 | BuiltinID == ARM::BI__builtin_arm_ldaex || |
| 499 | BuiltinID == AArch64::BI__builtin_arm_ldrex || |
| 500 | BuiltinID == AArch64::BI__builtin_arm_ldaex; |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 501 | |
| 502 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 503 | |
| 504 | // Ensure that we have the proper number of arguments. |
| 505 | if (checkArgCount(*this, TheCall, IsLdrex ? 1 : 2)) |
| 506 | return true; |
| 507 | |
| 508 | // Inspect the pointer argument of the atomic builtin. This should always be |
| 509 | // a pointer type, whose element is an integral scalar or pointer type. |
| 510 | // Because it is a pointer type, we don't have to worry about any implicit |
| 511 | // casts here. |
| 512 | Expr *PointerArg = TheCall->getArg(IsLdrex ? 0 : 1); |
| 513 | ExprResult PointerArgRes = DefaultFunctionArrayLvalueConversion(PointerArg); |
| 514 | if (PointerArgRes.isInvalid()) |
| 515 | return true; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 516 | PointerArg = PointerArgRes.get(); |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 517 | |
| 518 | const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>(); |
| 519 | if (!pointerType) { |
| 520 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
| 521 | << PointerArg->getType() << PointerArg->getSourceRange(); |
| 522 | return true; |
| 523 | } |
| 524 | |
| 525 | // ldrex takes a "const volatile T*" and strex takes a "volatile T*". Our next |
| 526 | // task is to insert the appropriate casts into the AST. First work out just |
| 527 | // what the appropriate type is. |
| 528 | QualType ValType = pointerType->getPointeeType(); |
| 529 | QualType AddrType = ValType.getUnqualifiedType().withVolatile(); |
| 530 | if (IsLdrex) |
| 531 | AddrType.addConst(); |
| 532 | |
| 533 | // Issue a warning if the cast is dodgy. |
| 534 | CastKind CastNeeded = CK_NoOp; |
| 535 | if (!AddrType.isAtLeastAsQualifiedAs(ValType)) { |
| 536 | CastNeeded = CK_BitCast; |
| 537 | Diag(DRE->getLocStart(), diag::ext_typecheck_convert_discards_qualifiers) |
| 538 | << PointerArg->getType() |
| 539 | << Context.getPointerType(AddrType) |
| 540 | << AA_Passing << PointerArg->getSourceRange(); |
| 541 | } |
| 542 | |
| 543 | // Finally, do the cast and replace the argument with the corrected version. |
| 544 | AddrType = Context.getPointerType(AddrType); |
| 545 | PointerArgRes = ImpCastExprToType(PointerArg, AddrType, CastNeeded); |
| 546 | if (PointerArgRes.isInvalid()) |
| 547 | return true; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 548 | PointerArg = PointerArgRes.get(); |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 549 | |
| 550 | TheCall->setArg(IsLdrex ? 0 : 1, PointerArg); |
| 551 | |
| 552 | // In general, we allow ints, floats and pointers to be loaded and stored. |
| 553 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && |
| 554 | !ValType->isBlockPointerType() && !ValType->isFloatingType()) { |
| 555 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intfltptr) |
| 556 | << PointerArg->getType() << PointerArg->getSourceRange(); |
| 557 | return true; |
| 558 | } |
| 559 | |
| 560 | // But ARM doesn't have instructions to deal with 128-bit versions. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 561 | if (Context.getTypeSize(ValType) > MaxWidth) { |
| 562 | assert(MaxWidth == 64 && "Diagnostic unexpectedly inaccurate"); |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 563 | Diag(DRE->getLocStart(), diag::err_atomic_exclusive_builtin_pointer_size) |
| 564 | << PointerArg->getType() << PointerArg->getSourceRange(); |
| 565 | return true; |
| 566 | } |
| 567 | |
| 568 | switch (ValType.getObjCLifetime()) { |
| 569 | case Qualifiers::OCL_None: |
| 570 | case Qualifiers::OCL_ExplicitNone: |
| 571 | // okay |
| 572 | break; |
| 573 | |
| 574 | case Qualifiers::OCL_Weak: |
| 575 | case Qualifiers::OCL_Strong: |
| 576 | case Qualifiers::OCL_Autoreleasing: |
| 577 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
| 578 | << ValType << PointerArg->getSourceRange(); |
| 579 | return true; |
| 580 | } |
| 581 | |
| 582 | |
| 583 | if (IsLdrex) { |
| 584 | TheCall->setType(ValType); |
| 585 | return false; |
| 586 | } |
| 587 | |
| 588 | // Initialize the argument to be stored. |
| 589 | ExprResult ValArg = TheCall->getArg(0); |
| 590 | InitializedEntity Entity = InitializedEntity::InitializeParameter( |
| 591 | Context, ValType, /*consume*/ false); |
| 592 | ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg); |
| 593 | if (ValArg.isInvalid()) |
| 594 | return true; |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 595 | TheCall->setArg(0, ValArg.get()); |
Tim Northover | 58d2bb1 | 2013-10-29 12:32:58 +0000 | [diff] [blame] | 596 | |
| 597 | // __builtin_arm_strex always returns an int. It's marked as such in the .def, |
| 598 | // but the custom checker bypasses all default analysis. |
| 599 | TheCall->setType(Context.IntTy); |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 600 | return false; |
| 601 | } |
| 602 | |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 603 | bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
Nate Begeman | 5548309 | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 604 | llvm::APSInt Result; |
| 605 | |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 606 | if (BuiltinID == ARM::BI__builtin_arm_ldrex || |
Tim Northover | 3acd6bd | 2014-07-02 12:56:02 +0000 | [diff] [blame] | 607 | BuiltinID == ARM::BI__builtin_arm_ldaex || |
| 608 | BuiltinID == ARM::BI__builtin_arm_strex || |
| 609 | BuiltinID == ARM::BI__builtin_arm_stlex) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 610 | return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 64); |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 613 | if (CheckNeonBuiltinFunctionCall(BuiltinID, TheCall)) |
| 614 | return true; |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 615 | |
Yi Kong | 4efadfb | 2014-07-03 16:01:25 +0000 | [diff] [blame] | 616 | // For intrinsics which take an immediate value as part of the instruction, |
| 617 | // range check them here. |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 618 | unsigned i = 0, l = 0, u = 0; |
Nate Begeman | d773fe6 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 619 | switch (BuiltinID) { |
| 620 | default: return false; |
Nate Begeman | 1194bd2 | 2010-07-29 22:48:34 +0000 | [diff] [blame] | 621 | case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break; |
| 622 | case ARM::BI__builtin_arm_usat: i = 1; u = 31; break; |
Nate Begeman | f568b07 | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 623 | case ARM::BI__builtin_arm_vcvtr_f: |
| 624 | case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break; |
Weiming Zhao | 87bb492 | 2013-11-12 21:42:50 +0000 | [diff] [blame] | 625 | case ARM::BI__builtin_arm_dmb: |
Yi Kong | 4efadfb | 2014-07-03 16:01:25 +0000 | [diff] [blame] | 626 | case ARM::BI__builtin_arm_dsb: |
| 627 | case ARM::BI__builtin_arm_isb: l = 0; u = 15; break; |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 628 | } |
Nate Begeman | d773fe6 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 629 | |
Nate Begeman | f568b07 | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 630 | // FIXME: VFP Intrinsics should error if VFP not present. |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 631 | return SemaBuiltinConstantArgRange(TheCall, i, l, u + l); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 632 | } |
Daniel Dunbar | dd9b2d1 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 633 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 634 | bool Sema::CheckAArch64BuiltinFunctionCall(unsigned BuiltinID, |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 635 | CallExpr *TheCall) { |
| 636 | llvm::APSInt Result; |
| 637 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 638 | if (BuiltinID == AArch64::BI__builtin_arm_ldrex || |
Tim Northover | 3acd6bd | 2014-07-02 12:56:02 +0000 | [diff] [blame] | 639 | BuiltinID == AArch64::BI__builtin_arm_ldaex || |
| 640 | BuiltinID == AArch64::BI__builtin_arm_strex || |
| 641 | BuiltinID == AArch64::BI__builtin_arm_stlex) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 642 | return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 128); |
| 643 | } |
| 644 | |
| 645 | if (CheckNeonBuiltinFunctionCall(BuiltinID, TheCall)) |
| 646 | return true; |
| 647 | |
| 648 | return false; |
| 649 | } |
| 650 | |
Simon Atanasyan | ecedf3d | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 651 | bool Sema::CheckMipsBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
| 652 | unsigned i = 0, l = 0, u = 0; |
| 653 | switch (BuiltinID) { |
| 654 | default: return false; |
| 655 | case Mips::BI__builtin_mips_wrdsp: i = 1; l = 0; u = 63; break; |
| 656 | case Mips::BI__builtin_mips_rddsp: i = 0; l = 0; u = 63; break; |
Simon Atanasyan | 8f06f2f | 2012-08-27 12:29:20 +0000 | [diff] [blame] | 657 | case Mips::BI__builtin_mips_append: i = 2; l = 0; u = 31; break; |
| 658 | case Mips::BI__builtin_mips_balign: i = 2; l = 0; u = 3; break; |
| 659 | case Mips::BI__builtin_mips_precr_sra_ph_w: i = 2; l = 0; u = 31; break; |
| 660 | case Mips::BI__builtin_mips_precr_sra_r_ph_w: i = 2; l = 0; u = 31; break; |
| 661 | case Mips::BI__builtin_mips_prepend: i = 2; l = 0; u = 31; break; |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 662 | } |
Simon Atanasyan | ecedf3d | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 663 | |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 664 | return SemaBuiltinConstantArgRange(TheCall, i, l, u); |
Simon Atanasyan | ecedf3d | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Warren Hunt | 20e4a5d | 2014-02-21 23:08:53 +0000 | [diff] [blame] | 667 | bool Sema::CheckX86BuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
| 668 | switch (BuiltinID) { |
| 669 | case X86::BI_mm_prefetch: |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 670 | // This is declared to take (const char*, int) |
| 671 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 3); |
Warren Hunt | 20e4a5d | 2014-02-21 23:08:53 +0000 | [diff] [blame] | 672 | } |
| 673 | return false; |
| 674 | } |
| 675 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 676 | /// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo |
| 677 | /// parameter with the FormatAttr's correct format_idx and firstDataArg. |
| 678 | /// Returns true when the format fits the function and the FormatStringInfo has |
| 679 | /// been populated. |
| 680 | bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember, |
| 681 | FormatStringInfo *FSI) { |
| 682 | FSI->HasVAListArg = Format->getFirstArg() == 0; |
| 683 | FSI->FormatIdx = Format->getFormatIdx() - 1; |
| 684 | FSI->FirstDataArg = FSI->HasVAListArg ? 0 : Format->getFirstArg() - 1; |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 685 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 686 | // The way the format attribute works in GCC, the implicit this argument |
| 687 | // of member functions is counted. However, it doesn't appear in our own |
| 688 | // lists, so decrement format_idx in that case. |
| 689 | if (IsCXXMember) { |
| 690 | if(FSI->FormatIdx == 0) |
| 691 | return false; |
| 692 | --FSI->FormatIdx; |
| 693 | if (FSI->FirstDataArg != 0) |
| 694 | --FSI->FirstDataArg; |
| 695 | } |
| 696 | return true; |
| 697 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 698 | |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 699 | /// Checks if a the given expression evaluates to null. |
| 700 | /// |
| 701 | /// \brief Returns true if the value evaluates to null. |
| 702 | static bool CheckNonNullExpr(Sema &S, |
| 703 | const Expr *Expr) { |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 704 | // As a special case, transparent unions initialized with zero are |
| 705 | // considered null for the purposes of the nonnull attribute. |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 706 | if (const RecordType *UT = Expr->getType()->getAsUnionType()) { |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 707 | if (UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 708 | if (const CompoundLiteralExpr *CLE = |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 709 | dyn_cast<CompoundLiteralExpr>(Expr)) |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 710 | if (const InitListExpr *ILE = |
| 711 | dyn_cast<InitListExpr>(CLE->getInitializer())) |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 712 | Expr = ILE->getInit(0); |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | bool Result; |
Artyom Skrobov | 9f21344 | 2014-01-24 11:10:39 +0000 | [diff] [blame] | 716 | return (!Expr->isValueDependent() && |
| 717 | Expr->EvaluateAsBooleanCondition(Result, S.Context) && |
| 718 | !Result); |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | static void CheckNonNullArgument(Sema &S, |
| 722 | const Expr *ArgExpr, |
| 723 | SourceLocation CallSiteLoc) { |
| 724 | if (CheckNonNullExpr(S, ArgExpr)) |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 725 | S.Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); |
| 726 | } |
| 727 | |
Ted Kremenek | 2bc7333 | 2014-01-17 06:24:43 +0000 | [diff] [blame] | 728 | static void CheckNonNullArguments(Sema &S, |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 729 | const NamedDecl *FDecl, |
Ted Kremenek | 2bc7333 | 2014-01-17 06:24:43 +0000 | [diff] [blame] | 730 | const Expr * const *ExprArgs, |
| 731 | SourceLocation CallSiteLoc) { |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 732 | // Check the attributes attached to the method/function itself. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 733 | for (const auto *NonNull : FDecl->specific_attrs<NonNullAttr>()) { |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 734 | for (const auto &Val : NonNull->args()) |
| 735 | CheckNonNullArgument(S, ExprArgs[Val], CallSiteLoc); |
Ted Kremenek | 2bc7333 | 2014-01-17 06:24:43 +0000 | [diff] [blame] | 736 | } |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 737 | |
| 738 | // Check the attributes on the parameters. |
| 739 | ArrayRef<ParmVarDecl*> parms; |
| 740 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FDecl)) |
| 741 | parms = FD->parameters(); |
| 742 | else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(FDecl)) |
| 743 | parms = MD->parameters(); |
| 744 | |
| 745 | unsigned argIndex = 0; |
| 746 | for (ArrayRef<ParmVarDecl*>::iterator I = parms.begin(), E = parms.end(); |
| 747 | I != E; ++I, ++argIndex) { |
| 748 | const ParmVarDecl *PVD = *I; |
| 749 | if (PVD->hasAttr<NonNullAttr>()) |
| 750 | CheckNonNullArgument(S, ExprArgs[argIndex], CallSiteLoc); |
| 751 | } |
Ted Kremenek | 2bc7333 | 2014-01-17 06:24:43 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 754 | /// Handles the checks for format strings, non-POD arguments to vararg |
| 755 | /// functions, and NULL arguments passed to non-NULL parameters. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 756 | void Sema::checkCall(NamedDecl *FDecl, ArrayRef<const Expr *> Args, |
| 757 | unsigned NumParams, bool IsMemberFunction, |
| 758 | SourceLocation Loc, SourceRange Range, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 759 | VariadicCallType CallType) { |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 760 | // FIXME: We should check as much as we can in the template definition. |
Jordan Rose | 3c14b23 | 2012-10-02 01:49:54 +0000 | [diff] [blame] | 761 | if (CurContext->isDependentContext()) |
| 762 | return; |
Daniel Dunbar | dd9b2d1 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 763 | |
Ted Kremenek | b8176da | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 764 | // Printf and scanf checking. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 765 | llvm::SmallBitVector CheckedVarArgs; |
| 766 | if (FDecl) { |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 767 | for (const auto *I : FDecl->specific_attrs<FormatAttr>()) { |
Benjamin Kramer | 989ab8b | 2013-08-09 09:39:17 +0000 | [diff] [blame] | 768 | // Only create vector if there are format attributes. |
| 769 | CheckedVarArgs.resize(Args.size()); |
| 770 | |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 771 | CheckFormatArguments(I, Args, IsMemberFunction, CallType, Loc, Range, |
Benjamin Kramer | f62e81d | 2013-08-08 11:08:26 +0000 | [diff] [blame] | 772 | CheckedVarArgs); |
Benjamin Kramer | 989ab8b | 2013-08-09 09:39:17 +0000 | [diff] [blame] | 773 | } |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 774 | } |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 775 | |
| 776 | // Refuse POD arguments that weren't caught by the format string |
| 777 | // checks above. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 778 | if (CallType != VariadicDoesNotApply) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 779 | for (unsigned ArgIdx = NumParams; ArgIdx < Args.size(); ++ArgIdx) { |
Ted Kremenek | 241f1ef | 2012-10-11 19:06:43 +0000 | [diff] [blame] | 780 | // Args[ArgIdx] can be null in malformed code. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 781 | if (const Expr *Arg = Args[ArgIdx]) { |
| 782 | if (CheckedVarArgs.empty() || !CheckedVarArgs[ArgIdx]) |
| 783 | checkVariadicArgument(Arg, CallType); |
| 784 | } |
Ted Kremenek | 241f1ef | 2012-10-11 19:06:43 +0000 | [diff] [blame] | 785 | } |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 786 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 787 | |
Richard Trieu | 41bc099 | 2013-06-22 00:20:41 +0000 | [diff] [blame] | 788 | if (FDecl) { |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 789 | CheckNonNullArguments(*this, FDecl, Args.data(), Loc); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 790 | |
Richard Trieu | 41bc099 | 2013-06-22 00:20:41 +0000 | [diff] [blame] | 791 | // Type safety checking. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 792 | for (const auto *I : FDecl->specific_attrs<ArgumentWithTypeTagAttr>()) |
| 793 | CheckArgumentWithTypeTag(I, Args.data()); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 794 | } |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | /// CheckConstructorCall - Check a constructor call for correctness and safety |
| 798 | /// properties not enforced by the C type system. |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 799 | void Sema::CheckConstructorCall(FunctionDecl *FDecl, |
| 800 | ArrayRef<const Expr *> Args, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 801 | const FunctionProtoType *Proto, |
| 802 | SourceLocation Loc) { |
| 803 | VariadicCallType CallType = |
| 804 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 805 | checkCall(FDecl, Args, Proto->getNumParams(), |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 806 | /*IsMemberFunction=*/true, Loc, SourceRange(), CallType); |
| 807 | } |
| 808 | |
| 809 | /// CheckFunctionCall - Check a direct function call for various correctness |
| 810 | /// and safety properties not strictly enforced by the C type system. |
| 811 | bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall, |
| 812 | const FunctionProtoType *Proto) { |
Eli Friedman | 726d11c | 2012-10-11 00:30:58 +0000 | [diff] [blame] | 813 | bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) && |
| 814 | isa<CXXMethodDecl>(FDecl); |
| 815 | bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) || |
| 816 | IsMemberOperatorCall; |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 817 | VariadicCallType CallType = getVariadicCallType(FDecl, Proto, |
| 818 | TheCall->getCallee()); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 819 | unsigned NumParams = Proto ? Proto->getNumParams() : 0; |
Eli Friedman | 726d11c | 2012-10-11 00:30:58 +0000 | [diff] [blame] | 820 | Expr** Args = TheCall->getArgs(); |
| 821 | unsigned NumArgs = TheCall->getNumArgs(); |
Eli Friedman | adf4218 | 2012-10-11 00:34:15 +0000 | [diff] [blame] | 822 | if (IsMemberOperatorCall) { |
Eli Friedman | 726d11c | 2012-10-11 00:30:58 +0000 | [diff] [blame] | 823 | // If this is a call to a member operator, hide the first argument |
| 824 | // from checkCall. |
| 825 | // FIXME: Our choice of AST representation here is less than ideal. |
| 826 | ++Args; |
| 827 | --NumArgs; |
| 828 | } |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 829 | checkCall(FDecl, llvm::makeArrayRef<const Expr *>(Args, NumArgs), NumParams, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 830 | IsMemberFunction, TheCall->getRParenLoc(), |
| 831 | TheCall->getCallee()->getSourceRange(), CallType); |
| 832 | |
| 833 | IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
| 834 | // None of the checks below are needed for functions that don't have |
| 835 | // simple names (e.g., C++ conversion functions). |
| 836 | if (!FnInfo) |
| 837 | return false; |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 838 | |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 839 | CheckAbsoluteValueFunction(TheCall, FDecl, FnInfo); |
| 840 | |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 841 | unsigned CMId = FDecl->getMemoryFunctionKind(); |
| 842 | if (CMId == 0) |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 843 | return false; |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 844 | |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 845 | // Handle memory setting and copying functions. |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 846 | if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat) |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 847 | CheckStrlcpycatArguments(TheCall, FnInfo); |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 848 | else if (CMId == Builtin::BIstrncat) |
| 849 | CheckStrncatArguments(TheCall, FnInfo); |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 850 | else |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 851 | CheckMemaccessArguments(TheCall, CMId, FnInfo); |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 852 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 853 | return false; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 856 | bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac, |
Dmitri Gribenko | 1debc46 | 2013-05-05 19:42:09 +0000 | [diff] [blame] | 857 | ArrayRef<const Expr *> Args) { |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 858 | VariadicCallType CallType = |
| 859 | Method->isVariadic() ? VariadicMethod : VariadicDoesNotApply; |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 860 | |
Dmitri Gribenko | 1debc46 | 2013-05-05 19:42:09 +0000 | [diff] [blame] | 861 | checkCall(Method, Args, Method->param_size(), |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 862 | /*IsMemberFunction=*/false, |
| 863 | lbrac, Method->getSourceRange(), CallType); |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 864 | |
| 865 | return false; |
| 866 | } |
| 867 | |
Richard Trieu | 664c4c6 | 2013-06-20 21:03:13 +0000 | [diff] [blame] | 868 | bool Sema::CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall, |
| 869 | const FunctionProtoType *Proto) { |
Fariborz Jahanian | c1585be | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 870 | const VarDecl *V = dyn_cast<VarDecl>(NDecl); |
| 871 | if (!V) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 872 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | |
Fariborz Jahanian | c1585be | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 874 | QualType Ty = V->getType(); |
Richard Trieu | 664c4c6 | 2013-06-20 21:03:13 +0000 | [diff] [blame] | 875 | if (!Ty->isBlockPointerType() && !Ty->isFunctionPointerType()) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 876 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 877 | |
Richard Trieu | 664c4c6 | 2013-06-20 21:03:13 +0000 | [diff] [blame] | 878 | VariadicCallType CallType; |
Richard Trieu | 72ae173 | 2013-06-20 23:21:54 +0000 | [diff] [blame] | 879 | if (!Proto || !Proto->isVariadic()) { |
Richard Trieu | 664c4c6 | 2013-06-20 21:03:13 +0000 | [diff] [blame] | 880 | CallType = VariadicDoesNotApply; |
| 881 | } else if (Ty->isBlockPointerType()) { |
| 882 | CallType = VariadicBlock; |
| 883 | } else { // Ty->isFunctionPointerType() |
| 884 | CallType = VariadicFunction; |
| 885 | } |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 886 | unsigned NumParams = Proto ? Proto->getNumParams() : 0; |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 887 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 888 | checkCall(NDecl, llvm::makeArrayRef<const Expr *>(TheCall->getArgs(), |
| 889 | TheCall->getNumArgs()), |
| 890 | NumParams, /*IsMemberFunction=*/false, TheCall->getRParenLoc(), |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 891 | TheCall->getCallee()->getSourceRange(), CallType); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 892 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 893 | return false; |
Fariborz Jahanian | c1585be | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Richard Trieu | 41bc099 | 2013-06-22 00:20:41 +0000 | [diff] [blame] | 896 | /// Checks function calls when a FunctionDecl or a NamedDecl is not available, |
| 897 | /// such as function pointers returned from functions. |
| 898 | bool Sema::CheckOtherCall(CallExpr *TheCall, const FunctionProtoType *Proto) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 899 | VariadicCallType CallType = getVariadicCallType(/*FDecl=*/nullptr, Proto, |
Richard Trieu | 41bc099 | 2013-06-22 00:20:41 +0000 | [diff] [blame] | 900 | TheCall->getCallee()); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 901 | unsigned NumParams = Proto ? Proto->getNumParams() : 0; |
Richard Trieu | 41bc099 | 2013-06-22 00:20:41 +0000 | [diff] [blame] | 902 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 903 | checkCall(/*FDecl=*/nullptr, |
| 904 | llvm::makeArrayRef<const Expr *>(TheCall->getArgs(), |
| 905 | TheCall->getNumArgs()), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 906 | NumParams, /*IsMemberFunction=*/false, TheCall->getRParenLoc(), |
Richard Trieu | 41bc099 | 2013-06-22 00:20:41 +0000 | [diff] [blame] | 907 | TheCall->getCallee()->getSourceRange(), CallType); |
| 908 | |
| 909 | return false; |
| 910 | } |
| 911 | |
Tim Northover | e94a34c | 2014-03-11 10:49:14 +0000 | [diff] [blame] | 912 | static bool isValidOrderingForOp(int64_t Ordering, AtomicExpr::AtomicOp Op) { |
| 913 | if (Ordering < AtomicExpr::AO_ABI_memory_order_relaxed || |
| 914 | Ordering > AtomicExpr::AO_ABI_memory_order_seq_cst) |
| 915 | return false; |
| 916 | |
| 917 | switch (Op) { |
| 918 | case AtomicExpr::AO__c11_atomic_init: |
| 919 | llvm_unreachable("There is no ordering argument for an init"); |
| 920 | |
| 921 | case AtomicExpr::AO__c11_atomic_load: |
| 922 | case AtomicExpr::AO__atomic_load_n: |
| 923 | case AtomicExpr::AO__atomic_load: |
| 924 | return Ordering != AtomicExpr::AO_ABI_memory_order_release && |
| 925 | Ordering != AtomicExpr::AO_ABI_memory_order_acq_rel; |
| 926 | |
| 927 | case AtomicExpr::AO__c11_atomic_store: |
| 928 | case AtomicExpr::AO__atomic_store: |
| 929 | case AtomicExpr::AO__atomic_store_n: |
| 930 | return Ordering != AtomicExpr::AO_ABI_memory_order_consume && |
| 931 | Ordering != AtomicExpr::AO_ABI_memory_order_acquire && |
| 932 | Ordering != AtomicExpr::AO_ABI_memory_order_acq_rel; |
| 933 | |
| 934 | default: |
| 935 | return true; |
| 936 | } |
| 937 | } |
| 938 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 939 | ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult, |
| 940 | AtomicExpr::AtomicOp Op) { |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 941 | CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); |
| 942 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 943 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 944 | // All these operations take one of the following forms: |
| 945 | enum { |
| 946 | // C __c11_atomic_init(A *, C) |
| 947 | Init, |
| 948 | // C __c11_atomic_load(A *, int) |
| 949 | Load, |
| 950 | // void __atomic_load(A *, CP, int) |
| 951 | Copy, |
| 952 | // C __c11_atomic_add(A *, M, int) |
| 953 | Arithmetic, |
| 954 | // C __atomic_exchange_n(A *, CP, int) |
| 955 | Xchg, |
| 956 | // void __atomic_exchange(A *, C *, CP, int) |
| 957 | GNUXchg, |
| 958 | // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int) |
| 959 | C11CmpXchg, |
| 960 | // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int) |
| 961 | GNUCmpXchg |
| 962 | } Form = Init; |
| 963 | const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 4, 5, 6 }; |
| 964 | const unsigned NumVals[] = { 1, 0, 1, 1, 1, 2, 2, 3 }; |
| 965 | // where: |
| 966 | // C is an appropriate type, |
| 967 | // A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins, |
| 968 | // CP is C for __c11 builtins and GNU _n builtins and is C * otherwise, |
| 969 | // M is C if C is an integer, and ptrdiff_t if C is a pointer, and |
| 970 | // the int parameters are for orderings. |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 971 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 972 | assert(AtomicExpr::AO__c11_atomic_init == 0 && |
| 973 | AtomicExpr::AO__c11_atomic_fetch_xor + 1 == AtomicExpr::AO__atomic_load |
| 974 | && "need to update code for modified C11 atomics"); |
| 975 | bool IsC11 = Op >= AtomicExpr::AO__c11_atomic_init && |
| 976 | Op <= AtomicExpr::AO__c11_atomic_fetch_xor; |
| 977 | bool IsN = Op == AtomicExpr::AO__atomic_load_n || |
| 978 | Op == AtomicExpr::AO__atomic_store_n || |
| 979 | Op == AtomicExpr::AO__atomic_exchange_n || |
| 980 | Op == AtomicExpr::AO__atomic_compare_exchange_n; |
| 981 | bool IsAddSub = false; |
| 982 | |
| 983 | switch (Op) { |
| 984 | case AtomicExpr::AO__c11_atomic_init: |
| 985 | Form = Init; |
| 986 | break; |
| 987 | |
| 988 | case AtomicExpr::AO__c11_atomic_load: |
| 989 | case AtomicExpr::AO__atomic_load_n: |
| 990 | Form = Load; |
| 991 | break; |
| 992 | |
| 993 | case AtomicExpr::AO__c11_atomic_store: |
| 994 | case AtomicExpr::AO__atomic_load: |
| 995 | case AtomicExpr::AO__atomic_store: |
| 996 | case AtomicExpr::AO__atomic_store_n: |
| 997 | Form = Copy; |
| 998 | break; |
| 999 | |
| 1000 | case AtomicExpr::AO__c11_atomic_fetch_add: |
| 1001 | case AtomicExpr::AO__c11_atomic_fetch_sub: |
| 1002 | case AtomicExpr::AO__atomic_fetch_add: |
| 1003 | case AtomicExpr::AO__atomic_fetch_sub: |
| 1004 | case AtomicExpr::AO__atomic_add_fetch: |
| 1005 | case AtomicExpr::AO__atomic_sub_fetch: |
| 1006 | IsAddSub = true; |
| 1007 | // Fall through. |
| 1008 | case AtomicExpr::AO__c11_atomic_fetch_and: |
| 1009 | case AtomicExpr::AO__c11_atomic_fetch_or: |
| 1010 | case AtomicExpr::AO__c11_atomic_fetch_xor: |
| 1011 | case AtomicExpr::AO__atomic_fetch_and: |
| 1012 | case AtomicExpr::AO__atomic_fetch_or: |
| 1013 | case AtomicExpr::AO__atomic_fetch_xor: |
Richard Smith | d65cee9 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 1014 | case AtomicExpr::AO__atomic_fetch_nand: |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1015 | case AtomicExpr::AO__atomic_and_fetch: |
| 1016 | case AtomicExpr::AO__atomic_or_fetch: |
| 1017 | case AtomicExpr::AO__atomic_xor_fetch: |
Richard Smith | d65cee9 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 1018 | case AtomicExpr::AO__atomic_nand_fetch: |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1019 | Form = Arithmetic; |
| 1020 | break; |
| 1021 | |
| 1022 | case AtomicExpr::AO__c11_atomic_exchange: |
| 1023 | case AtomicExpr::AO__atomic_exchange_n: |
| 1024 | Form = Xchg; |
| 1025 | break; |
| 1026 | |
| 1027 | case AtomicExpr::AO__atomic_exchange: |
| 1028 | Form = GNUXchg; |
| 1029 | break; |
| 1030 | |
| 1031 | case AtomicExpr::AO__c11_atomic_compare_exchange_strong: |
| 1032 | case AtomicExpr::AO__c11_atomic_compare_exchange_weak: |
| 1033 | Form = C11CmpXchg; |
| 1034 | break; |
| 1035 | |
| 1036 | case AtomicExpr::AO__atomic_compare_exchange: |
| 1037 | case AtomicExpr::AO__atomic_compare_exchange_n: |
| 1038 | Form = GNUCmpXchg; |
| 1039 | break; |
| 1040 | } |
| 1041 | |
| 1042 | // Check we have the right number of arguments. |
| 1043 | if (TheCall->getNumArgs() < NumArgs[Form]) { |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1044 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1045 | << 0 << NumArgs[Form] << TheCall->getNumArgs() |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1046 | << TheCall->getCallee()->getSourceRange(); |
| 1047 | return ExprError(); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1048 | } else if (TheCall->getNumArgs() > NumArgs[Form]) { |
| 1049 | Diag(TheCall->getArg(NumArgs[Form])->getLocStart(), |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1050 | diag::err_typecheck_call_too_many_args) |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1051 | << 0 << NumArgs[Form] << TheCall->getNumArgs() |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1052 | << TheCall->getCallee()->getSourceRange(); |
| 1053 | return ExprError(); |
| 1054 | } |
| 1055 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1056 | // Inspect the first argument of the atomic operation. |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 1057 | Expr *Ptr = TheCall->getArg(0); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1058 | Ptr = DefaultFunctionArrayLvalueConversion(Ptr).get(); |
| 1059 | const PointerType *pointerType = Ptr->getType()->getAs<PointerType>(); |
| 1060 | if (!pointerType) { |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1061 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1062 | << Ptr->getType() << Ptr->getSourceRange(); |
| 1063 | return ExprError(); |
| 1064 | } |
| 1065 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1066 | // For a __c11 builtin, this should be a pointer to an _Atomic type. |
| 1067 | QualType AtomTy = pointerType->getPointeeType(); // 'A' |
| 1068 | QualType ValType = AtomTy; // 'C' |
| 1069 | if (IsC11) { |
| 1070 | if (!AtomTy->isAtomicType()) { |
| 1071 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic) |
| 1072 | << Ptr->getType() << Ptr->getSourceRange(); |
| 1073 | return ExprError(); |
| 1074 | } |
Richard Smith | e00921a | 2012-09-15 06:09:58 +0000 | [diff] [blame] | 1075 | if (AtomTy.isConstQualified()) { |
| 1076 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_non_const_atomic) |
| 1077 | << Ptr->getType() << Ptr->getSourceRange(); |
| 1078 | return ExprError(); |
| 1079 | } |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1080 | ValType = AtomTy->getAs<AtomicType>()->getValueType(); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1081 | } |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1082 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1083 | // For an arithmetic operation, the implied arithmetic must be well-formed. |
| 1084 | if (Form == Arithmetic) { |
| 1085 | // gcc does not enforce these rules for GNU atomics, but we do so for sanity. |
| 1086 | if (IsAddSub && !ValType->isIntegerType() && !ValType->isPointerType()) { |
| 1087 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) |
| 1088 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 1089 | return ExprError(); |
| 1090 | } |
| 1091 | if (!IsAddSub && !ValType->isIntegerType()) { |
| 1092 | Diag(DRE->getLocStart(), diag::err_atomic_op_bitwise_needs_atomic_int) |
| 1093 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 1094 | return ExprError(); |
| 1095 | } |
| 1096 | } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) { |
| 1097 | // For __atomic_*_n operations, the value type must be a scalar integral or |
| 1098 | // pointer type which is 1, 2, 4, 8 or 16 bytes in length. |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1099 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1100 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 1101 | return ExprError(); |
| 1102 | } |
| 1103 | |
Eli Friedman | aa76981 | 2013-09-11 03:49:34 +0000 | [diff] [blame] | 1104 | if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context) && |
| 1105 | !AtomTy->isScalarType()) { |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1106 | // For GNU atomics, require a trivially-copyable type. This is not part of |
| 1107 | // the GNU atomics specification, but we enforce it for sanity. |
| 1108 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_trivial_copy) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1109 | << Ptr->getType() << Ptr->getSourceRange(); |
| 1110 | return ExprError(); |
| 1111 | } |
| 1112 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1113 | // FIXME: For any builtin other than a load, the ValType must not be |
| 1114 | // const-qualified. |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1115 | |
| 1116 | switch (ValType.getObjCLifetime()) { |
| 1117 | case Qualifiers::OCL_None: |
| 1118 | case Qualifiers::OCL_ExplicitNone: |
| 1119 | // okay |
| 1120 | break; |
| 1121 | |
| 1122 | case Qualifiers::OCL_Weak: |
| 1123 | case Qualifiers::OCL_Strong: |
| 1124 | case Qualifiers::OCL_Autoreleasing: |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1125 | // FIXME: Can this happen? By this point, ValType should be known |
| 1126 | // to be trivially copyable. |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1127 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
| 1128 | << ValType << Ptr->getSourceRange(); |
| 1129 | return ExprError(); |
| 1130 | } |
| 1131 | |
| 1132 | QualType ResultType = ValType; |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1133 | if (Form == Copy || Form == GNUXchg || Form == Init) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1134 | ResultType = Context.VoidTy; |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1135 | else if (Form == C11CmpXchg || Form == GNUCmpXchg) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1136 | ResultType = Context.BoolTy; |
| 1137 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1138 | // The type of a parameter passed 'by value'. In the GNU atomics, such |
| 1139 | // arguments are actually passed as pointers. |
| 1140 | QualType ByValType = ValType; // 'CP' |
| 1141 | if (!IsC11 && !IsN) |
| 1142 | ByValType = Ptr->getType(); |
| 1143 | |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1144 | // The first argument --- the pointer --- has a fixed type; we |
| 1145 | // deduce the types of the rest of the arguments accordingly. Walk |
| 1146 | // the remaining arguments, converting them to the deduced value type. |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1147 | for (unsigned i = 1; i != NumArgs[Form]; ++i) { |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1148 | QualType Ty; |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1149 | if (i < NumVals[Form] + 1) { |
| 1150 | switch (i) { |
| 1151 | case 1: |
| 1152 | // The second argument is the non-atomic operand. For arithmetic, this |
| 1153 | // is always passed by value, and for a compare_exchange it is always |
| 1154 | // passed by address. For the rest, GNU uses by-address and C11 uses |
| 1155 | // by-value. |
| 1156 | assert(Form != Load); |
| 1157 | if (Form == Init || (Form == Arithmetic && ValType->isIntegerType())) |
| 1158 | Ty = ValType; |
| 1159 | else if (Form == Copy || Form == Xchg) |
| 1160 | Ty = ByValType; |
| 1161 | else if (Form == Arithmetic) |
| 1162 | Ty = Context.getPointerDiffType(); |
| 1163 | else |
| 1164 | Ty = Context.getPointerType(ValType.getUnqualifiedType()); |
| 1165 | break; |
| 1166 | case 2: |
| 1167 | // The third argument to compare_exchange / GNU exchange is a |
| 1168 | // (pointer to a) desired value. |
| 1169 | Ty = ByValType; |
| 1170 | break; |
| 1171 | case 3: |
| 1172 | // The fourth argument to GNU compare_exchange is a 'weak' flag. |
| 1173 | Ty = Context.BoolTy; |
| 1174 | break; |
| 1175 | } |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1176 | } else { |
| 1177 | // The order(s) are always converted to int. |
| 1178 | Ty = Context.IntTy; |
| 1179 | } |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1180 | |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1181 | InitializedEntity Entity = |
| 1182 | InitializedEntity::InitializeParameter(Context, Ty, false); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1183 | ExprResult Arg = TheCall->getArg(i); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1184 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 1185 | if (Arg.isInvalid()) |
| 1186 | return true; |
| 1187 | TheCall->setArg(i, Arg.get()); |
| 1188 | } |
| 1189 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1190 | // Permute the arguments into a 'consistent' order. |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 1191 | SmallVector<Expr*, 5> SubExprs; |
| 1192 | SubExprs.push_back(Ptr); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1193 | switch (Form) { |
| 1194 | case Init: |
| 1195 | // Note, AtomicExpr::getVal1() has a special case for this atomic. |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 1196 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1197 | break; |
| 1198 | case Load: |
| 1199 | SubExprs.push_back(TheCall->getArg(1)); // Order |
| 1200 | break; |
| 1201 | case Copy: |
| 1202 | case Arithmetic: |
| 1203 | case Xchg: |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 1204 | SubExprs.push_back(TheCall->getArg(2)); // Order |
| 1205 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1206 | break; |
| 1207 | case GNUXchg: |
| 1208 | // Note, AtomicExpr::getVal2() has a special case for this atomic. |
| 1209 | SubExprs.push_back(TheCall->getArg(3)); // Order |
| 1210 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
| 1211 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
| 1212 | break; |
| 1213 | case C11CmpXchg: |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 1214 | SubExprs.push_back(TheCall->getArg(3)); // Order |
| 1215 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 1216 | SubExprs.push_back(TheCall->getArg(4)); // OrderFail |
David Chisnall | 891ec28 | 2012-03-29 17:58:59 +0000 | [diff] [blame] | 1217 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1218 | break; |
| 1219 | case GNUCmpXchg: |
| 1220 | SubExprs.push_back(TheCall->getArg(4)); // Order |
| 1221 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
| 1222 | SubExprs.push_back(TheCall->getArg(5)); // OrderFail |
| 1223 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
| 1224 | SubExprs.push_back(TheCall->getArg(3)); // Weak |
| 1225 | break; |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1226 | } |
Tim Northover | e94a34c | 2014-03-11 10:49:14 +0000 | [diff] [blame] | 1227 | |
| 1228 | if (SubExprs.size() >= 2 && Form != Init) { |
| 1229 | llvm::APSInt Result(32); |
| 1230 | if (SubExprs[1]->isIntegerConstantExpr(Result, Context) && |
| 1231 | !isValidOrderingForOp(Result.getSExtValue(), Op)) |
Tim Northover | c83472e | 2014-03-11 11:35:10 +0000 | [diff] [blame] | 1232 | Diag(SubExprs[1]->getLocStart(), |
| 1233 | diag::warn_atomic_op_has_invalid_memory_order) |
| 1234 | << SubExprs[1]->getSourceRange(); |
Tim Northover | e94a34c | 2014-03-11 10:49:14 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 1237 | AtomicExpr *AE = new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(), |
| 1238 | SubExprs, ResultType, Op, |
| 1239 | TheCall->getRParenLoc()); |
| 1240 | |
| 1241 | if ((Op == AtomicExpr::AO__c11_atomic_load || |
| 1242 | (Op == AtomicExpr::AO__c11_atomic_store)) && |
| 1243 | Context.AtomicUsesUnsupportedLibcall(AE)) |
| 1244 | Diag(AE->getLocStart(), diag::err_atomic_load_store_uses_lib) << |
| 1245 | ((Op == AtomicExpr::AO__c11_atomic_load) ? 0 : 1); |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 1246 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1247 | return AE; |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | |
John McCall | 29ad95b | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 1251 | /// checkBuiltinArgument - Given a call to a builtin function, perform |
| 1252 | /// normal type-checking on the given argument, updating the call in |
| 1253 | /// place. This is useful when a builtin function requires custom |
| 1254 | /// type-checking for some of its arguments but not necessarily all of |
| 1255 | /// them. |
| 1256 | /// |
| 1257 | /// Returns true on error. |
| 1258 | static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) { |
| 1259 | FunctionDecl *Fn = E->getDirectCallee(); |
| 1260 | assert(Fn && "builtin call without direct callee!"); |
| 1261 | |
| 1262 | ParmVarDecl *Param = Fn->getParamDecl(ArgIndex); |
| 1263 | InitializedEntity Entity = |
| 1264 | InitializedEntity::InitializeParameter(S.Context, Param); |
| 1265 | |
| 1266 | ExprResult Arg = E->getArg(0); |
| 1267 | Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 1268 | if (Arg.isInvalid()) |
| 1269 | return true; |
| 1270 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1271 | E->setArg(ArgIndex, Arg.get()); |
John McCall | 29ad95b | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 1272 | return false; |
| 1273 | } |
| 1274 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1275 | /// SemaBuiltinAtomicOverloaded - We have a call to a function like |
| 1276 | /// __sync_fetch_and_add, which is an overloaded function based on the pointer |
| 1277 | /// type of its first argument. The main ActOnCallExpr routines have already |
| 1278 | /// promoted the types of arguments because all of these calls are prototyped as |
| 1279 | /// void(...). |
| 1280 | /// |
| 1281 | /// This function goes through and does final semantic checking for these |
| 1282 | /// builtins, |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1283 | ExprResult |
| 1284 | Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) { |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1285 | CallExpr *TheCall = (CallExpr *)TheCallResult.get(); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1286 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 1287 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 1288 | |
| 1289 | // Ensure that we have at least one argument to do type inference from. |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1290 | if (TheCall->getNumArgs() < 1) { |
| 1291 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 1292 | << 0 << 1 << TheCall->getNumArgs() |
| 1293 | << TheCall->getCallee()->getSourceRange(); |
| 1294 | return ExprError(); |
| 1295 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1296 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1297 | // Inspect the first argument of the atomic builtin. This should always be |
| 1298 | // a pointer type, whose element is an integral scalar or pointer type. |
| 1299 | // Because it is a pointer type, we don't have to worry about any implicit |
| 1300 | // casts here. |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1301 | // FIXME: We don't allow floating point scalars as input. |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1302 | Expr *FirstArg = TheCall->getArg(0); |
Eli Friedman | 844f945 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 1303 | ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg); |
| 1304 | if (FirstArgResult.isInvalid()) |
| 1305 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1306 | FirstArg = FirstArgResult.get(); |
Eli Friedman | 844f945 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 1307 | TheCall->setArg(0, FirstArg); |
| 1308 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1309 | const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>(); |
| 1310 | if (!pointerType) { |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1311 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
| 1312 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 1313 | return ExprError(); |
| 1314 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1316 | QualType ValType = pointerType->getPointeeType(); |
Chris Lattner | bb3bcd8 | 2010-09-17 21:12:38 +0000 | [diff] [blame] | 1317 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1318 | !ValType->isBlockPointerType()) { |
| 1319 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr) |
| 1320 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 1321 | return ExprError(); |
| 1322 | } |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1323 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1324 | switch (ValType.getObjCLifetime()) { |
| 1325 | case Qualifiers::OCL_None: |
| 1326 | case Qualifiers::OCL_ExplicitNone: |
| 1327 | // okay |
| 1328 | break; |
| 1329 | |
| 1330 | case Qualifiers::OCL_Weak: |
| 1331 | case Qualifiers::OCL_Strong: |
| 1332 | case Qualifiers::OCL_Autoreleasing: |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 1333 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1334 | << ValType << FirstArg->getSourceRange(); |
| 1335 | return ExprError(); |
| 1336 | } |
| 1337 | |
John McCall | b50451a | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1338 | // Strip any qualifiers off ValType. |
| 1339 | ValType = ValType.getUnqualifiedType(); |
| 1340 | |
Chandler Carruth | 3973af7 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1341 | // The majority of builtins return a value, but a few have special return |
| 1342 | // types, so allow them to override appropriately below. |
| 1343 | QualType ResultType = ValType; |
| 1344 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1345 | // We need to figure out which concrete builtin this maps onto. For example, |
| 1346 | // __sync_fetch_and_add with a 2 byte object turns into |
| 1347 | // __sync_fetch_and_add_2. |
| 1348 | #define BUILTIN_ROW(x) \ |
| 1349 | { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ |
| 1350 | Builtin::BI##x##_8, Builtin::BI##x##_16 } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1351 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1352 | static const unsigned BuiltinIndices[][5] = { |
| 1353 | BUILTIN_ROW(__sync_fetch_and_add), |
| 1354 | BUILTIN_ROW(__sync_fetch_and_sub), |
| 1355 | BUILTIN_ROW(__sync_fetch_and_or), |
| 1356 | BUILTIN_ROW(__sync_fetch_and_and), |
| 1357 | BUILTIN_ROW(__sync_fetch_and_xor), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1358 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1359 | BUILTIN_ROW(__sync_add_and_fetch), |
| 1360 | BUILTIN_ROW(__sync_sub_and_fetch), |
| 1361 | BUILTIN_ROW(__sync_and_and_fetch), |
| 1362 | BUILTIN_ROW(__sync_or_and_fetch), |
| 1363 | BUILTIN_ROW(__sync_xor_and_fetch), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1364 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1365 | BUILTIN_ROW(__sync_val_compare_and_swap), |
| 1366 | BUILTIN_ROW(__sync_bool_compare_and_swap), |
| 1367 | BUILTIN_ROW(__sync_lock_test_and_set), |
Chris Lattner | 9cb59fa | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 1368 | BUILTIN_ROW(__sync_lock_release), |
| 1369 | BUILTIN_ROW(__sync_swap) |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1370 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 | #undef BUILTIN_ROW |
| 1372 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1373 | // Determine the index of the size. |
| 1374 | unsigned SizeIndex; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1375 | switch (Context.getTypeSizeInChars(ValType).getQuantity()) { |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1376 | case 1: SizeIndex = 0; break; |
| 1377 | case 2: SizeIndex = 1; break; |
| 1378 | case 4: SizeIndex = 2; break; |
| 1379 | case 8: SizeIndex = 3; break; |
| 1380 | case 16: SizeIndex = 4; break; |
| 1381 | default: |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1382 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size) |
| 1383 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 1384 | return ExprError(); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1385 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1386 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1387 | // Each of these builtins has one pointer argument, followed by some number of |
| 1388 | // values (0, 1 or 2) followed by a potentially empty varags list of stuff |
| 1389 | // that we ignore. Find out which row of BuiltinIndices to read from as well |
| 1390 | // as the number of fixed args. |
Douglas Gregor | 15fc956 | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 1391 | unsigned BuiltinID = FDecl->getBuiltinID(); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1392 | unsigned BuiltinIndex, NumFixed = 1; |
| 1393 | switch (BuiltinID) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1394 | default: llvm_unreachable("Unknown overloaded atomic builtin!"); |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1395 | case Builtin::BI__sync_fetch_and_add: |
| 1396 | case Builtin::BI__sync_fetch_and_add_1: |
| 1397 | case Builtin::BI__sync_fetch_and_add_2: |
| 1398 | case Builtin::BI__sync_fetch_and_add_4: |
| 1399 | case Builtin::BI__sync_fetch_and_add_8: |
| 1400 | case Builtin::BI__sync_fetch_and_add_16: |
| 1401 | BuiltinIndex = 0; |
| 1402 | break; |
| 1403 | |
| 1404 | case Builtin::BI__sync_fetch_and_sub: |
| 1405 | case Builtin::BI__sync_fetch_and_sub_1: |
| 1406 | case Builtin::BI__sync_fetch_and_sub_2: |
| 1407 | case Builtin::BI__sync_fetch_and_sub_4: |
| 1408 | case Builtin::BI__sync_fetch_and_sub_8: |
| 1409 | case Builtin::BI__sync_fetch_and_sub_16: |
| 1410 | BuiltinIndex = 1; |
| 1411 | break; |
| 1412 | |
| 1413 | case Builtin::BI__sync_fetch_and_or: |
| 1414 | case Builtin::BI__sync_fetch_and_or_1: |
| 1415 | case Builtin::BI__sync_fetch_and_or_2: |
| 1416 | case Builtin::BI__sync_fetch_and_or_4: |
| 1417 | case Builtin::BI__sync_fetch_and_or_8: |
| 1418 | case Builtin::BI__sync_fetch_and_or_16: |
| 1419 | BuiltinIndex = 2; |
| 1420 | break; |
| 1421 | |
| 1422 | case Builtin::BI__sync_fetch_and_and: |
| 1423 | case Builtin::BI__sync_fetch_and_and_1: |
| 1424 | case Builtin::BI__sync_fetch_and_and_2: |
| 1425 | case Builtin::BI__sync_fetch_and_and_4: |
| 1426 | case Builtin::BI__sync_fetch_and_and_8: |
| 1427 | case Builtin::BI__sync_fetch_and_and_16: |
| 1428 | BuiltinIndex = 3; |
| 1429 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1430 | |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1431 | case Builtin::BI__sync_fetch_and_xor: |
| 1432 | case Builtin::BI__sync_fetch_and_xor_1: |
| 1433 | case Builtin::BI__sync_fetch_and_xor_2: |
| 1434 | case Builtin::BI__sync_fetch_and_xor_4: |
| 1435 | case Builtin::BI__sync_fetch_and_xor_8: |
| 1436 | case Builtin::BI__sync_fetch_and_xor_16: |
| 1437 | BuiltinIndex = 4; |
| 1438 | break; |
| 1439 | |
| 1440 | case Builtin::BI__sync_add_and_fetch: |
| 1441 | case Builtin::BI__sync_add_and_fetch_1: |
| 1442 | case Builtin::BI__sync_add_and_fetch_2: |
| 1443 | case Builtin::BI__sync_add_and_fetch_4: |
| 1444 | case Builtin::BI__sync_add_and_fetch_8: |
| 1445 | case Builtin::BI__sync_add_and_fetch_16: |
| 1446 | BuiltinIndex = 5; |
| 1447 | break; |
| 1448 | |
| 1449 | case Builtin::BI__sync_sub_and_fetch: |
| 1450 | case Builtin::BI__sync_sub_and_fetch_1: |
| 1451 | case Builtin::BI__sync_sub_and_fetch_2: |
| 1452 | case Builtin::BI__sync_sub_and_fetch_4: |
| 1453 | case Builtin::BI__sync_sub_and_fetch_8: |
| 1454 | case Builtin::BI__sync_sub_and_fetch_16: |
| 1455 | BuiltinIndex = 6; |
| 1456 | break; |
| 1457 | |
| 1458 | case Builtin::BI__sync_and_and_fetch: |
| 1459 | case Builtin::BI__sync_and_and_fetch_1: |
| 1460 | case Builtin::BI__sync_and_and_fetch_2: |
| 1461 | case Builtin::BI__sync_and_and_fetch_4: |
| 1462 | case Builtin::BI__sync_and_and_fetch_8: |
| 1463 | case Builtin::BI__sync_and_and_fetch_16: |
| 1464 | BuiltinIndex = 7; |
| 1465 | break; |
| 1466 | |
| 1467 | case Builtin::BI__sync_or_and_fetch: |
| 1468 | case Builtin::BI__sync_or_and_fetch_1: |
| 1469 | case Builtin::BI__sync_or_and_fetch_2: |
| 1470 | case Builtin::BI__sync_or_and_fetch_4: |
| 1471 | case Builtin::BI__sync_or_and_fetch_8: |
| 1472 | case Builtin::BI__sync_or_and_fetch_16: |
| 1473 | BuiltinIndex = 8; |
| 1474 | break; |
| 1475 | |
| 1476 | case Builtin::BI__sync_xor_and_fetch: |
| 1477 | case Builtin::BI__sync_xor_and_fetch_1: |
| 1478 | case Builtin::BI__sync_xor_and_fetch_2: |
| 1479 | case Builtin::BI__sync_xor_and_fetch_4: |
| 1480 | case Builtin::BI__sync_xor_and_fetch_8: |
| 1481 | case Builtin::BI__sync_xor_and_fetch_16: |
| 1482 | BuiltinIndex = 9; |
| 1483 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1485 | case Builtin::BI__sync_val_compare_and_swap: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1486 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 1487 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 1488 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 1489 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 1490 | case Builtin::BI__sync_val_compare_and_swap_16: |
Daniel Dunbar | 3f540c0d | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 1491 | BuiltinIndex = 10; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1492 | NumFixed = 2; |
| 1493 | break; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1494 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1495 | case Builtin::BI__sync_bool_compare_and_swap: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1496 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 1497 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 1498 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 1499 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 1500 | case Builtin::BI__sync_bool_compare_and_swap_16: |
Daniel Dunbar | 3f540c0d | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 1501 | BuiltinIndex = 11; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1502 | NumFixed = 2; |
Chandler Carruth | 3973af7 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1503 | ResultType = Context.BoolTy; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1504 | break; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1505 | |
| 1506 | case Builtin::BI__sync_lock_test_and_set: |
| 1507 | case Builtin::BI__sync_lock_test_and_set_1: |
| 1508 | case Builtin::BI__sync_lock_test_and_set_2: |
| 1509 | case Builtin::BI__sync_lock_test_and_set_4: |
| 1510 | case Builtin::BI__sync_lock_test_and_set_8: |
| 1511 | case Builtin::BI__sync_lock_test_and_set_16: |
| 1512 | BuiltinIndex = 12; |
| 1513 | break; |
| 1514 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1515 | case Builtin::BI__sync_lock_release: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1516 | case Builtin::BI__sync_lock_release_1: |
| 1517 | case Builtin::BI__sync_lock_release_2: |
| 1518 | case Builtin::BI__sync_lock_release_4: |
| 1519 | case Builtin::BI__sync_lock_release_8: |
| 1520 | case Builtin::BI__sync_lock_release_16: |
Daniel Dunbar | 3f540c0d | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 1521 | BuiltinIndex = 13; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1522 | NumFixed = 0; |
Chandler Carruth | 3973af7 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1523 | ResultType = Context.VoidTy; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1524 | break; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1525 | |
| 1526 | case Builtin::BI__sync_swap: |
| 1527 | case Builtin::BI__sync_swap_1: |
| 1528 | case Builtin::BI__sync_swap_2: |
| 1529 | case Builtin::BI__sync_swap_4: |
| 1530 | case Builtin::BI__sync_swap_8: |
| 1531 | case Builtin::BI__sync_swap_16: |
| 1532 | BuiltinIndex = 14; |
| 1533 | break; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1534 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1536 | // Now that we know how many fixed arguments we expect, first check that we |
| 1537 | // have at least that many. |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1538 | if (TheCall->getNumArgs() < 1+NumFixed) { |
| 1539 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 1540 | << 0 << 1+NumFixed << TheCall->getNumArgs() |
| 1541 | << TheCall->getCallee()->getSourceRange(); |
| 1542 | return ExprError(); |
| 1543 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1544 | |
Chris Lattner | 5b9241b | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 1545 | // Get the decl for the concrete builtin from this, we can tell what the |
| 1546 | // concrete integer type we should convert to is. |
| 1547 | unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; |
| 1548 | const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID); |
Abramo Bagnara | 6cba23a | 2012-09-22 09:05:22 +0000 | [diff] [blame] | 1549 | FunctionDecl *NewBuiltinDecl; |
| 1550 | if (NewBuiltinID == BuiltinID) |
| 1551 | NewBuiltinDecl = FDecl; |
| 1552 | else { |
| 1553 | // Perform builtin lookup to avoid redeclaring it. |
| 1554 | DeclarationName DN(&Context.Idents.get(NewBuiltinName)); |
| 1555 | LookupResult Res(*this, DN, DRE->getLocStart(), LookupOrdinaryName); |
| 1556 | LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true); |
| 1557 | assert(Res.getFoundDecl()); |
| 1558 | NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1559 | if (!NewBuiltinDecl) |
Abramo Bagnara | 6cba23a | 2012-09-22 09:05:22 +0000 | [diff] [blame] | 1560 | return ExprError(); |
| 1561 | } |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1562 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1563 | // The first argument --- the pointer --- has a fixed type; we |
| 1564 | // deduce the types of the rest of the arguments accordingly. Walk |
| 1565 | // the remaining arguments, converting them to the deduced value type. |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1566 | for (unsigned i = 0; i != NumFixed; ++i) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1567 | ExprResult Arg = TheCall->getArg(i+1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1568 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1569 | // GCC does an implicit conversion to the pointer or integer ValType. This |
| 1570 | // can fail in some cases (1i -> int**), check for this error case now. |
John McCall | b50451a | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1571 | // Initialize the argument. |
| 1572 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 1573 | ValType, /*consume*/ false); |
| 1574 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1575 | if (Arg.isInvalid()) |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1576 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1577 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1578 | // Okay, we have something that *can* be converted to the right type. Check |
| 1579 | // to see if there is a potentially weird extension going on here. This can |
| 1580 | // happen when you do an atomic operation on something like an char* and |
| 1581 | // pass in 42. The 42 gets converted to char. This is even more strange |
| 1582 | // for things like 45.123 -> char, etc. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1583 | // FIXME: Do this check. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1584 | TheCall->setArg(i+1, Arg.get()); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1585 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | |
Douglas Gregor | 6b3bcf2 | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1587 | ASTContext& Context = this->getASTContext(); |
| 1588 | |
| 1589 | // Create a new DeclRefExpr to refer to the new decl. |
| 1590 | DeclRefExpr* NewDRE = DeclRefExpr::Create( |
| 1591 | Context, |
| 1592 | DRE->getQualifierLoc(), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1593 | SourceLocation(), |
Douglas Gregor | 6b3bcf2 | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1594 | NewBuiltinDecl, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1595 | /*enclosing*/ false, |
Douglas Gregor | 6b3bcf2 | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1596 | DRE->getLocation(), |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 1597 | Context.BuiltinFnTy, |
Douglas Gregor | 6b3bcf2 | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1598 | DRE->getValueKind()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1599 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1600 | // Set the callee in the CallExpr. |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 1601 | // FIXME: This loses syntactic information. |
| 1602 | QualType CalleePtrTy = Context.getPointerType(NewBuiltinDecl->getType()); |
| 1603 | ExprResult PromotedCall = ImpCastExprToType(NewDRE, CalleePtrTy, |
| 1604 | CK_BuiltinFnToFnPtr); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1605 | TheCall->setCallee(PromotedCall.get()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1606 | |
Chandler Carruth | bc8cab1 | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 1607 | // Change the result type of the call to match the original value type. This |
| 1608 | // is arbitrary, but the codegen for these builtins ins design to handle it |
| 1609 | // gracefully. |
Chandler Carruth | 3973af7 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1610 | TheCall->setType(ResultType); |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1611 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1612 | return TheCallResult; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
Chris Lattner | 6436fb6 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 1615 | /// CheckObjCString - Checks that the argument to the builtin |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1616 | /// CFString constructor is correct |
Steve Naroff | fb46e86 | 2009-04-13 20:26:29 +0000 | [diff] [blame] | 1617 | /// Note: It might also make sense to do the UTF-16 conversion here (would |
| 1618 | /// simplify the backend). |
Chris Lattner | 6436fb6 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 1619 | bool Sema::CheckObjCString(Expr *Arg) { |
Chris Lattner | f266096 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1620 | Arg = Arg->IgnoreParenCasts(); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1621 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); |
| 1622 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1623 | if (!Literal || !Literal->isAscii()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1624 | Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 1625 | << Arg->getSourceRange(); |
Anders Carlsson | a3a9c43 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 1626 | return true; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1627 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | |
Fariborz Jahanian | 56603ef | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1629 | if (Literal->containsNonAsciiOrNull()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1630 | StringRef String = Literal->getString(); |
Fariborz Jahanian | 56603ef | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1631 | unsigned NumBytes = String.size(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1632 | SmallVector<UTF16, 128> ToBuf(NumBytes); |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 1633 | const UTF8 *FromPtr = (const UTF8 *)String.data(); |
Fariborz Jahanian | 56603ef | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1634 | UTF16 *ToPtr = &ToBuf[0]; |
| 1635 | |
| 1636 | ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, |
| 1637 | &ToPtr, ToPtr + NumBytes, |
| 1638 | strictConversion); |
| 1639 | // Check for conversion failure. |
| 1640 | if (Result != conversionOK) |
| 1641 | Diag(Arg->getLocStart(), |
| 1642 | diag::warn_cfstring_truncated) << Arg->getSourceRange(); |
| 1643 | } |
Anders Carlsson | a3a9c43 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 1644 | return false; |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
Chris Lattner | e202e6a | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1647 | /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity. |
| 1648 | /// Emit an error and return true on failure, return false on success. |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1649 | bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) { |
| 1650 | Expr *Fn = TheCall->getCallee(); |
| 1651 | if (TheCall->getNumArgs() > 2) { |
Chris Lattner | cedef8d | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 1652 | Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1653 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1654 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
| 1655 | << Fn->getSourceRange() |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1656 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1657 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1658 | return true; |
| 1659 | } |
Eli Friedman | bb2b3be | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 1660 | |
| 1661 | if (TheCall->getNumArgs() < 2) { |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1662 | return Diag(TheCall->getLocEnd(), |
| 1663 | diag::err_typecheck_call_too_few_args_at_least) |
| 1664 | << 0 /*function call*/ << 2 << TheCall->getNumArgs(); |
Eli Friedman | bb2b3be | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
John McCall | 29ad95b | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 1667 | // Type-check the first argument normally. |
| 1668 | if (checkBuiltinArgument(*this, TheCall, 0)) |
| 1669 | return true; |
| 1670 | |
Chris Lattner | e202e6a | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1671 | // Determine whether the current function is variadic or not. |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1672 | BlockScopeInfo *CurBlock = getCurBlock(); |
Chris Lattner | e202e6a | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1673 | bool isVariadic; |
Steve Naroff | 439a3e4 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 1674 | if (CurBlock) |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 1675 | isVariadic = CurBlock->TheDecl->isVariadic(); |
Ted Kremenek | 186a074 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 1676 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
| 1677 | isVariadic = FD->isVariadic(); |
| 1678 | else |
Argyrios Kyrtzidis | 853fbea | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1679 | isVariadic = getCurMethodDecl()->isVariadic(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1680 | |
Chris Lattner | e202e6a | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1681 | if (!isVariadic) { |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1682 | Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function); |
| 1683 | return true; |
| 1684 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1685 | |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1686 | // Verify that the second argument to the builtin is the last argument of the |
| 1687 | // current function or method. |
| 1688 | bool SecondArgIsLastNamedArgument = false; |
Anders Carlsson | 73cc507 | 2008-02-13 01:22:59 +0000 | [diff] [blame] | 1689 | const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1690 | |
Nico Weber | 9eea764 | 2013-05-24 23:31:57 +0000 | [diff] [blame] | 1691 | // These are valid if SecondArgIsLastNamedArgument is false after the next |
| 1692 | // block. |
| 1693 | QualType Type; |
| 1694 | SourceLocation ParamLoc; |
| 1695 | |
Anders Carlsson | 6a8350b | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 1696 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { |
| 1697 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1698 | // FIXME: This isn't correct for methods (results in bogus warning). |
| 1699 | // Get the last formal in the current function. |
Anders Carlsson | 6a8350b | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 1700 | const ParmVarDecl *LastArg; |
Steve Naroff | 439a3e4 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 1701 | if (CurBlock) |
| 1702 | LastArg = *(CurBlock->TheDecl->param_end()-1); |
| 1703 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1704 | LastArg = *(FD->param_end()-1); |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1705 | else |
Argyrios Kyrtzidis | 853fbea | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1706 | LastArg = *(getCurMethodDecl()->param_end()-1); |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1707 | SecondArgIsLastNamedArgument = PV == LastArg; |
Nico Weber | 9eea764 | 2013-05-24 23:31:57 +0000 | [diff] [blame] | 1708 | |
| 1709 | Type = PV->getType(); |
| 1710 | ParamLoc = PV->getLocation(); |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1711 | } |
| 1712 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1713 | |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1714 | if (!SecondArgIsLastNamedArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1715 | Diag(TheCall->getArg(1)->getLocStart(), |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1716 | diag::warn_second_parameter_of_va_start_not_last_named_argument); |
Nico Weber | 9eea764 | 2013-05-24 23:31:57 +0000 | [diff] [blame] | 1717 | else if (Type->isReferenceType()) { |
| 1718 | Diag(Arg->getLocStart(), |
| 1719 | diag::warn_va_start_of_reference_type_is_undefined); |
| 1720 | Diag(ParamLoc, diag::note_parameter_type) << Type; |
| 1721 | } |
| 1722 | |
Enea Zaffanella | b1b1b8a | 2013-11-07 08:14:26 +0000 | [diff] [blame] | 1723 | TheCall->setType(Context.VoidTy); |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1724 | return false; |
Eli Friedman | f835303 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 1725 | } |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1726 | |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1727 | /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and |
| 1728 | /// friends. This is declared to take (...), so we have to check everything. |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1729 | bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { |
| 1730 | if (TheCall->getNumArgs() < 2) |
Chris Lattner | cedef8d | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 1731 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1732 | << 0 << 2 << TheCall->getNumArgs()/*function call*/; |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1733 | if (TheCall->getNumArgs() > 2) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1734 | return Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1735 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1736 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1737 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
| 1738 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1739 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1740 | ExprResult OrigArg0 = TheCall->getArg(0); |
| 1741 | ExprResult OrigArg1 = TheCall->getArg(1); |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1742 | |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1743 | // Do standard promotions between the two arguments, returning their common |
| 1744 | // type. |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1745 | QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1746 | if (OrigArg0.isInvalid() || OrigArg1.isInvalid()) |
| 1747 | return true; |
Daniel Dunbar | 96f8677 | 2009-02-19 19:28:43 +0000 | [diff] [blame] | 1748 | |
| 1749 | // Make sure any conversions are pushed back into the call; this is |
| 1750 | // type safe since unordered compare builtins are declared as "_Bool |
| 1751 | // foo(...)". |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1752 | TheCall->setArg(0, OrigArg0.get()); |
| 1753 | TheCall->setArg(1, OrigArg1.get()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1754 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1755 | if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent()) |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1756 | return false; |
| 1757 | |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1758 | // If the common type isn't a real floating type, then the arguments were |
| 1759 | // invalid for this operation. |
Eli Friedman | 93ee5ca | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 1760 | if (Res.isNull() || !Res->isRealFloatingType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1761 | return Diag(OrigArg0.get()->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1762 | diag::err_typecheck_call_invalid_ordered_compare) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1763 | << OrigArg0.get()->getType() << OrigArg1.get()->getType() |
| 1764 | << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1765 | |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1766 | return false; |
| 1767 | } |
| 1768 | |
Benjamin Kramer | 634fc10 | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 1769 | /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like |
| 1770 | /// __builtin_isnan and friends. This is declared to take (...), so we have |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1771 | /// to check everything. We expect the last argument to be a floating point |
| 1772 | /// value. |
| 1773 | bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) { |
| 1774 | if (TheCall->getNumArgs() < NumArgs) |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1775 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1776 | << 0 << NumArgs << TheCall->getNumArgs()/*function call*/; |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1777 | if (TheCall->getNumArgs() > NumArgs) |
| 1778 | return Diag(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1779 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1780 | << 0 /*function call*/ << NumArgs << TheCall->getNumArgs() |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1781 | << SourceRange(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1782 | (*(TheCall->arg_end()-1))->getLocEnd()); |
| 1783 | |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1784 | Expr *OrigArg = TheCall->getArg(NumArgs-1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1786 | if (OrigArg->isTypeDependent()) |
| 1787 | return false; |
| 1788 | |
Chris Lattner | 68784ef | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1789 | // This operation requires a non-_Complex floating-point number. |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1790 | if (!OrigArg->getType()->isRealFloatingType()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | return Diag(OrigArg->getLocStart(), |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1792 | diag::err_typecheck_call_invalid_unary_fp) |
| 1793 | << OrigArg->getType() << OrigArg->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1794 | |
Chris Lattner | 68784ef | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1795 | // If this is an implicit conversion from float -> double, remove it. |
| 1796 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) { |
| 1797 | Expr *CastArg = Cast->getSubExpr(); |
| 1798 | if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) { |
| 1799 | assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) && |
| 1800 | "promotion from float to double is the only expected cast here"); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1801 | Cast->setSubExpr(nullptr); |
Chris Lattner | 68784ef | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1802 | TheCall->setArg(NumArgs-1, CastArg); |
Chris Lattner | 68784ef | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1803 | } |
| 1804 | } |
| 1805 | |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1806 | return false; |
| 1807 | } |
| 1808 | |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1809 | /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. |
| 1810 | // This is declared to take (...), so we have to check everything. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1811 | ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1812 | if (TheCall->getNumArgs() < 2) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1813 | return ExprError(Diag(TheCall->getLocEnd(), |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1814 | diag::err_typecheck_call_too_few_args_at_least) |
Craig Topper | 304602a | 2013-07-28 21:50:10 +0000 | [diff] [blame] | 1815 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
| 1816 | << TheCall->getSourceRange()); |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1817 | |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1818 | // Determine which of the following types of shufflevector we're checking: |
| 1819 | // 1) unary, vector mask: (lhs, mask) |
| 1820 | // 2) binary, vector mask: (lhs, rhs, mask) |
| 1821 | // 3) binary, scalar mask: (lhs, rhs, index, ..., index) |
| 1822 | QualType resType = TheCall->getArg(0)->getType(); |
| 1823 | unsigned numElements = 0; |
Craig Topper | 61d01cc | 2013-07-19 04:46:31 +0000 | [diff] [blame] | 1824 | |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1825 | if (!TheCall->getArg(0)->isTypeDependent() && |
| 1826 | !TheCall->getArg(1)->isTypeDependent()) { |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1827 | QualType LHSType = TheCall->getArg(0)->getType(); |
| 1828 | QualType RHSType = TheCall->getArg(1)->getType(); |
Craig Topper | 61d01cc | 2013-07-19 04:46:31 +0000 | [diff] [blame] | 1829 | |
Craig Topper | baca389 | 2013-07-29 06:47:04 +0000 | [diff] [blame] | 1830 | if (!LHSType->isVectorType() || !RHSType->isVectorType()) |
| 1831 | return ExprError(Diag(TheCall->getLocStart(), |
| 1832 | diag::err_shufflevector_non_vector) |
| 1833 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
| 1834 | TheCall->getArg(1)->getLocEnd())); |
Craig Topper | 61d01cc | 2013-07-19 04:46:31 +0000 | [diff] [blame] | 1835 | |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1836 | numElements = LHSType->getAs<VectorType>()->getNumElements(); |
| 1837 | unsigned numResElements = TheCall->getNumArgs() - 2; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1839 | // Check to see if we have a call with 2 vector arguments, the unary shuffle |
| 1840 | // with mask. If so, verify that RHS is an integer vector type with the |
| 1841 | // same number of elts as lhs. |
| 1842 | if (TheCall->getNumArgs() == 2) { |
Sylvestre Ledru | 8e5d82e | 2013-07-06 08:00:09 +0000 | [diff] [blame] | 1843 | if (!RHSType->hasIntegerRepresentation() || |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1844 | RHSType->getAs<VectorType>()->getNumElements() != numElements) |
Craig Topper | baca389 | 2013-07-29 06:47:04 +0000 | [diff] [blame] | 1845 | return ExprError(Diag(TheCall->getLocStart(), |
| 1846 | diag::err_shufflevector_incompatible_vector) |
| 1847 | << SourceRange(TheCall->getArg(1)->getLocStart(), |
| 1848 | TheCall->getArg(1)->getLocEnd())); |
Craig Topper | 61d01cc | 2013-07-19 04:46:31 +0000 | [diff] [blame] | 1849 | } else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { |
Craig Topper | baca389 | 2013-07-29 06:47:04 +0000 | [diff] [blame] | 1850 | return ExprError(Diag(TheCall->getLocStart(), |
| 1851 | diag::err_shufflevector_incompatible_vector) |
| 1852 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
| 1853 | TheCall->getArg(1)->getLocEnd())); |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1854 | } else if (numElements != numResElements) { |
| 1855 | QualType eltType = LHSType->getAs<VectorType>()->getElementType(); |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1856 | resType = Context.getVectorType(eltType, numResElements, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1857 | VectorType::GenericVector); |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1858 | } |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1862 | if (TheCall->getArg(i)->isTypeDependent() || |
| 1863 | TheCall->getArg(i)->isValueDependent()) |
| 1864 | continue; |
| 1865 | |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1866 | llvm::APSInt Result(32); |
| 1867 | if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) |
| 1868 | return ExprError(Diag(TheCall->getLocStart(), |
Craig Topper | 304602a | 2013-07-28 21:50:10 +0000 | [diff] [blame] | 1869 | diag::err_shufflevector_nonconstant_argument) |
| 1870 | << TheCall->getArg(i)->getSourceRange()); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1871 | |
Craig Topper | 50ad5b7 | 2013-08-03 17:40:38 +0000 | [diff] [blame] | 1872 | // Allow -1 which will be translated to undef in the IR. |
| 1873 | if (Result.isSigned() && Result.isAllOnesValue()) |
| 1874 | continue; |
| 1875 | |
Chris Lattner | 7ab824e | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 1876 | if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1877 | return ExprError(Diag(TheCall->getLocStart(), |
Craig Topper | 304602a | 2013-07-28 21:50:10 +0000 | [diff] [blame] | 1878 | diag::err_shufflevector_argument_too_large) |
| 1879 | << TheCall->getArg(i)->getSourceRange()); |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1882 | SmallVector<Expr*, 32> exprs; |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1883 | |
Chris Lattner | 7ab824e | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 1884 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1885 | exprs.push_back(TheCall->getArg(i)); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1886 | TheCall->setArg(i, nullptr); |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1889 | return new (Context) ShuffleVectorExpr(Context, exprs, resType, |
| 1890 | TheCall->getCallee()->getLocStart(), |
| 1891 | TheCall->getRParenLoc()); |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1892 | } |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1893 | |
Hal Finkel | c4d7c82 | 2013-09-18 03:29:45 +0000 | [diff] [blame] | 1894 | /// SemaConvertVectorExpr - Handle __builtin_convertvector |
| 1895 | ExprResult Sema::SemaConvertVectorExpr(Expr *E, TypeSourceInfo *TInfo, |
| 1896 | SourceLocation BuiltinLoc, |
| 1897 | SourceLocation RParenLoc) { |
| 1898 | ExprValueKind VK = VK_RValue; |
| 1899 | ExprObjectKind OK = OK_Ordinary; |
| 1900 | QualType DstTy = TInfo->getType(); |
| 1901 | QualType SrcTy = E->getType(); |
| 1902 | |
| 1903 | if (!SrcTy->isVectorType() && !SrcTy->isDependentType()) |
| 1904 | return ExprError(Diag(BuiltinLoc, |
| 1905 | diag::err_convertvector_non_vector) |
| 1906 | << E->getSourceRange()); |
| 1907 | if (!DstTy->isVectorType() && !DstTy->isDependentType()) |
| 1908 | return ExprError(Diag(BuiltinLoc, |
| 1909 | diag::err_convertvector_non_vector_type)); |
| 1910 | |
| 1911 | if (!SrcTy->isDependentType() && !DstTy->isDependentType()) { |
| 1912 | unsigned SrcElts = SrcTy->getAs<VectorType>()->getNumElements(); |
| 1913 | unsigned DstElts = DstTy->getAs<VectorType>()->getNumElements(); |
| 1914 | if (SrcElts != DstElts) |
| 1915 | return ExprError(Diag(BuiltinLoc, |
| 1916 | diag::err_convertvector_incompatible_vector) |
| 1917 | << E->getSourceRange()); |
| 1918 | } |
| 1919 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1920 | return new (Context) |
| 1921 | ConvertVectorExpr(E, TInfo, DstTy, VK, OK, BuiltinLoc, RParenLoc); |
Hal Finkel | c4d7c82 | 2013-09-18 03:29:45 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1924 | /// SemaBuiltinPrefetch - Handle __builtin_prefetch. |
| 1925 | // This is declared to take (const void*, ...) and can take two |
| 1926 | // optional constant int args. |
| 1927 | bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1928 | unsigned NumArgs = TheCall->getNumArgs(); |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1929 | |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1930 | if (NumArgs > 3) |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1931 | return Diag(TheCall->getLocEnd(), |
| 1932 | diag::err_typecheck_call_too_many_args_at_most) |
| 1933 | << 0 /*function call*/ << 3 << NumArgs |
| 1934 | << TheCall->getSourceRange(); |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1935 | |
| 1936 | // Argument 0 is checked for us and the remaining arguments must be |
| 1937 | // constant integers. |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 1938 | for (unsigned i = 1; i != NumArgs; ++i) |
| 1939 | if (SemaBuiltinConstantArgRange(TheCall, i, 0, i == 1 ? 1 : 3)) |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1940 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1941 | |
Warren Hunt | 20e4a5d | 2014-02-21 23:08:53 +0000 | [diff] [blame] | 1942 | return false; |
| 1943 | } |
| 1944 | |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1945 | /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr |
| 1946 | /// TheCall is a constant expression. |
| 1947 | bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum, |
| 1948 | llvm::APSInt &Result) { |
| 1949 | Expr *Arg = TheCall->getArg(ArgNum); |
| 1950 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 1951 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 1952 | |
| 1953 | if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; |
| 1954 | |
| 1955 | if (!Arg->isIntegerConstantExpr(Result, Context)) |
| 1956 | return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type) |
Eric Christopher | 63448c3 | 2010-04-19 18:23:02 +0000 | [diff] [blame] | 1957 | << FDecl->getDeclName() << Arg->getSourceRange(); |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1958 | |
Chris Lattner | d545ad1 | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1959 | return false; |
| 1960 | } |
| 1961 | |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 1962 | /// SemaBuiltinConstantArgRange - Handle a check if argument ArgNum of CallExpr |
| 1963 | /// TheCall is a constant expression in the range [Low, High]. |
| 1964 | bool Sema::SemaBuiltinConstantArgRange(CallExpr *TheCall, int ArgNum, |
| 1965 | int Low, int High) { |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1966 | llvm::APSInt Result; |
Douglas Gregor | 98c3cfc | 2012-06-29 01:05:22 +0000 | [diff] [blame] | 1967 | |
| 1968 | // We can't check the value of a dependent argument. |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 1969 | Expr *Arg = TheCall->getArg(ArgNum); |
| 1970 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
Douglas Gregor | 98c3cfc | 2012-06-29 01:05:22 +0000 | [diff] [blame] | 1971 | return false; |
| 1972 | |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1973 | // Check constant-ness first. |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 1974 | if (SemaBuiltinConstantArg(TheCall, ArgNum, Result)) |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1975 | return true; |
| 1976 | |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 1977 | if (Result.getSExtValue() < Low || Result.getSExtValue() > High) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1978 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 1979 | << Low << High << Arg->getSourceRange(); |
Daniel Dunbar | b0d34c8 | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1980 | |
| 1981 | return false; |
| 1982 | } |
| 1983 | |
Eli Friedman | c97d014 | 2009-05-03 06:04:26 +0000 | [diff] [blame] | 1984 | /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). |
Eli Friedman | eed8ad2 | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 1985 | /// This checks that val is a constant 1. |
| 1986 | bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { |
| 1987 | Expr *Arg = TheCall->getArg(1); |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1988 | llvm::APSInt Result; |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1989 | |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1990 | // TODO: This is less than ideal. Overload this to take a value. |
| 1991 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 1992 | return true; |
| 1993 | |
| 1994 | if (Result != 1) |
Eli Friedman | eed8ad2 | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 1995 | return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val) |
| 1996 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
| 1997 | |
| 1998 | return false; |
| 1999 | } |
| 2000 | |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2001 | namespace { |
| 2002 | enum StringLiteralCheckType { |
| 2003 | SLCT_NotALiteral, |
| 2004 | SLCT_UncheckedLiteral, |
| 2005 | SLCT_CheckedLiteral |
| 2006 | }; |
| 2007 | } |
| 2008 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2009 | // Determine if an expression is a string literal or constant string. |
| 2010 | // If this function returns false on the arguments to a function expecting a |
| 2011 | // format string, we will usually need to emit a warning. |
| 2012 | // True string literals are then checked by CheckFormatString. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2013 | static StringLiteralCheckType |
| 2014 | checkFormatStringExpr(Sema &S, const Expr *E, ArrayRef<const Expr *> Args, |
| 2015 | bool HasVAListArg, unsigned format_idx, |
| 2016 | unsigned firstDataArg, Sema::FormatStringType Type, |
| 2017 | Sema::VariadicCallType CallType, bool InFunctionCall, |
| 2018 | llvm::SmallBitVector &CheckedVarArgs) { |
Ted Kremenek | 80882935 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 2019 | tryAgain: |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 2020 | if (E->isTypeDependent() || E->isValueDependent()) |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2021 | return SLCT_NotALiteral; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2022 | |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2023 | E = E->IgnoreParenCasts(); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2024 | |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2025 | if (E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)) |
David Blaikie | 59fe3f8 | 2012-02-10 21:07:25 +0000 | [diff] [blame] | 2026 | // Technically -Wformat-nonliteral does not warn about this case. |
| 2027 | // The behavior of printf and friends in this case is implementation |
| 2028 | // dependent. Ideally if the format string cannot be null then |
| 2029 | // it should have a 'nonnull' attribute in the function prototype. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2030 | return SLCT_UncheckedLiteral; |
David Blaikie | 59fe3f8 | 2012-02-10 21:07:25 +0000 | [diff] [blame] | 2031 | |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2032 | switch (E->getStmtClass()) { |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2033 | case Stmt::BinaryConditionalOperatorClass: |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2034 | case Stmt::ConditionalOperatorClass: { |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2035 | // The expression is a literal if both sub-expressions were, and it was |
| 2036 | // completely checked only if both sub-expressions were checked. |
| 2037 | const AbstractConditionalOperator *C = |
| 2038 | cast<AbstractConditionalOperator>(E); |
| 2039 | StringLiteralCheckType Left = |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2040 | checkFormatStringExpr(S, C->getTrueExpr(), Args, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2041 | HasVAListArg, format_idx, firstDataArg, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2042 | Type, CallType, InFunctionCall, CheckedVarArgs); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2043 | if (Left == SLCT_NotALiteral) |
| 2044 | return SLCT_NotALiteral; |
| 2045 | StringLiteralCheckType Right = |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2046 | checkFormatStringExpr(S, C->getFalseExpr(), Args, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2047 | HasVAListArg, format_idx, firstDataArg, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2048 | Type, CallType, InFunctionCall, CheckedVarArgs); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2049 | return Left < Right ? Left : Right; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | case Stmt::ImplicitCastExprClass: { |
Ted Kremenek | 80882935 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 2053 | E = cast<ImplicitCastExpr>(E)->getSubExpr(); |
| 2054 | goto tryAgain; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2057 | case Stmt::OpaqueValueExprClass: |
| 2058 | if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) { |
| 2059 | E = src; |
| 2060 | goto tryAgain; |
| 2061 | } |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2062 | return SLCT_NotALiteral; |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2063 | |
Ted Kremenek | a889083 | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 2064 | case Stmt::PredefinedExprClass: |
| 2065 | // While __func__, etc., are technically not string literals, they |
| 2066 | // cannot contain format specifiers and thus are not a security |
| 2067 | // liability. |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2068 | return SLCT_UncheckedLiteral; |
Ted Kremenek | a889083 | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 2069 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2070 | case Stmt::DeclRefExprClass: { |
| 2071 | const DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2072 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2073 | // As an exception, do not flag errors for variables binding to |
| 2074 | // const string literals. |
| 2075 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 2076 | bool isConstant = false; |
| 2077 | QualType T = DR->getType(); |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2078 | |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2079 | if (const ArrayType *AT = S.Context.getAsArrayType(T)) { |
| 2080 | isConstant = AT->getElementType().isConstant(S.Context); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2081 | } else if (const PointerType *PT = T->getAs<PointerType>()) { |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2082 | isConstant = T.isConstant(S.Context) && |
| 2083 | PT->getPointeeType().isConstant(S.Context); |
Jean-Daniel Dupas | d5f7ef4 | 2012-01-25 10:35:33 +0000 | [diff] [blame] | 2084 | } else if (T->isObjCObjectPointerType()) { |
| 2085 | // In ObjC, there is usually no "const ObjectPointer" type, |
| 2086 | // so don't check if the pointee type is constant. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2087 | isConstant = T.isConstant(S.Context); |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2088 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2089 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2090 | if (isConstant) { |
Matt Beaumont-Gay | d873508 | 2012-05-11 22:10:59 +0000 | [diff] [blame] | 2091 | if (const Expr *Init = VD->getAnyInitializer()) { |
| 2092 | // Look through initializers like const char c[] = { "foo" } |
| 2093 | if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { |
| 2094 | if (InitList->isStringLiteralInit()) |
| 2095 | Init = InitList->getInit(0)->IgnoreParenImpCasts(); |
| 2096 | } |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2097 | return checkFormatStringExpr(S, Init, Args, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2098 | HasVAListArg, format_idx, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2099 | firstDataArg, Type, CallType, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2100 | /*InFunctionCall*/false, CheckedVarArgs); |
Matt Beaumont-Gay | d873508 | 2012-05-11 22:10:59 +0000 | [diff] [blame] | 2101 | } |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2102 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2103 | |
Anders Carlsson | b012ca9 | 2009-06-28 19:55:58 +0000 | [diff] [blame] | 2104 | // For vprintf* functions (i.e., HasVAListArg==true), we add a |
| 2105 | // special check to see if the format string is a function parameter |
| 2106 | // of the function calling the printf function. If the function |
| 2107 | // has an attribute indicating it is a printf-like function, then we |
| 2108 | // should suppress warnings concerning non-literals being used in a call |
| 2109 | // to a vprintf function. For example: |
| 2110 | // |
| 2111 | // void |
| 2112 | // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){ |
| 2113 | // va_list ap; |
| 2114 | // va_start(ap, fmt); |
| 2115 | // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". |
| 2116 | // ... |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2117 | // } |
Jean-Daniel Dupas | 58dab68 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 2118 | if (HasVAListArg) { |
| 2119 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) { |
| 2120 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) { |
| 2121 | int PVIndex = PV->getFunctionScopeIndex() + 1; |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2122 | for (const auto *PVFormat : ND->specific_attrs<FormatAttr>()) { |
Jean-Daniel Dupas | 58dab68 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 2123 | // adjust for implicit parameter |
| 2124 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 2125 | if (MD->isInstance()) |
| 2126 | ++PVIndex; |
| 2127 | // We also check if the formats are compatible. |
| 2128 | // We can't pass a 'scanf' string to a 'printf' function. |
| 2129 | if (PVIndex == PVFormat->getFormatIdx() && |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2130 | Type == S.GetFormatStringType(PVFormat)) |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2131 | return SLCT_UncheckedLiteral; |
Jean-Daniel Dupas | 58dab68 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 2132 | } |
| 2133 | } |
| 2134 | } |
| 2135 | } |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2136 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2137 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2138 | return SLCT_NotALiteral; |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2139 | } |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2140 | |
Jean-Daniel Dupas | 6255bd1 | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 2141 | case Stmt::CallExprClass: |
| 2142 | case Stmt::CXXMemberCallExprClass: { |
Anders Carlsson | f0a7f3b | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 2143 | const CallExpr *CE = cast<CallExpr>(E); |
Jean-Daniel Dupas | 6255bd1 | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 2144 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) { |
| 2145 | if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) { |
| 2146 | unsigned ArgIndex = FA->getFormatIdx(); |
| 2147 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 2148 | if (MD->isInstance()) |
| 2149 | --ArgIndex; |
| 2150 | const Expr *Arg = CE->getArg(ArgIndex - 1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2151 | |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2152 | return checkFormatStringExpr(S, Arg, Args, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2153 | HasVAListArg, format_idx, firstDataArg, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2154 | Type, CallType, InFunctionCall, |
| 2155 | CheckedVarArgs); |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2156 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 2157 | unsigned BuiltinID = FD->getBuiltinID(); |
| 2158 | if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString || |
| 2159 | BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) { |
| 2160 | const Expr *Arg = CE->getArg(0); |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2161 | return checkFormatStringExpr(S, Arg, Args, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2162 | HasVAListArg, format_idx, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2163 | firstDataArg, Type, CallType, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2164 | InFunctionCall, CheckedVarArgs); |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2165 | } |
Anders Carlsson | f0a7f3b | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 2166 | } |
| 2167 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2169 | return SLCT_NotALiteral; |
Anders Carlsson | f0a7f3b | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 2170 | } |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2171 | case Stmt::ObjCStringLiteralClass: |
| 2172 | case Stmt::StringLiteralClass: { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2173 | const StringLiteral *StrE = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2174 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2175 | if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2176 | StrE = ObjCFExpr->getString(); |
| 2177 | else |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2178 | StrE = cast<StringLiteral>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2179 | |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2180 | if (StrE) { |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2181 | S.CheckFormatString(StrE, E, Args, HasVAListArg, format_idx, firstDataArg, |
| 2182 | Type, InFunctionCall, CallType, CheckedVarArgs); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2183 | return SLCT_CheckedLiteral; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2184 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2185 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2186 | return SLCT_NotALiteral; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2187 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2188 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2189 | default: |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2190 | return SLCT_NotALiteral; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2191 | } |
| 2192 | } |
| 2193 | |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2194 | Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) { |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2195 | return llvm::StringSwitch<FormatStringType>(Format->getType()->getName()) |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2196 | .Case("scanf", FST_Scanf) |
| 2197 | .Cases("printf", "printf0", FST_Printf) |
| 2198 | .Cases("NSString", "CFString", FST_NSString) |
| 2199 | .Case("strftime", FST_Strftime) |
| 2200 | .Case("strfmon", FST_Strfmon) |
| 2201 | .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf) |
| 2202 | .Default(FST_Unknown); |
| 2203 | } |
| 2204 | |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2205 | /// CheckFormatArguments - Check calls to printf and scanf (and similar |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2206 | /// functions) for correct use of format strings. |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2207 | /// Returns true if a format string has been fully checked. |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2208 | bool Sema::CheckFormatArguments(const FormatAttr *Format, |
| 2209 | ArrayRef<const Expr *> Args, |
| 2210 | bool IsCXXMember, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2211 | VariadicCallType CallType, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2212 | SourceLocation Loc, SourceRange Range, |
| 2213 | llvm::SmallBitVector &CheckedVarArgs) { |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2214 | FormatStringInfo FSI; |
| 2215 | if (getFormatStringInfo(Format, IsCXXMember, &FSI)) |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2216 | return CheckFormatArguments(Args, FSI.HasVAListArg, FSI.FormatIdx, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2217 | FSI.FirstDataArg, GetFormatStringType(Format), |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2218 | CallType, Loc, Range, CheckedVarArgs); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2219 | return false; |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2220 | } |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2221 | |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2222 | bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args, |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2223 | bool HasVAListArg, unsigned format_idx, |
| 2224 | unsigned firstDataArg, FormatStringType Type, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2225 | VariadicCallType CallType, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2226 | SourceLocation Loc, SourceRange Range, |
| 2227 | llvm::SmallBitVector &CheckedVarArgs) { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2228 | // CHECK: printf/scanf-like function is called with no format string. |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2229 | if (format_idx >= Args.size()) { |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2230 | Diag(Loc, diag::warn_missing_format_string) << Range; |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2231 | return false; |
Ted Kremenek | e68f1aa | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 2232 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2234 | const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2235 | |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2236 | // CHECK: format string is not a string literal. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2237 | // |
Ted Kremenek | e68f1aa | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 2238 | // Dynamically generated format strings are difficult to |
| 2239 | // automatically vet at compile time. Requiring that format strings |
| 2240 | // are string literals: (1) permits the checking of format strings by |
| 2241 | // the compiler and thereby (2) can practically remove the source of |
| 2242 | // many format string exploits. |
Ted Kremenek | 34f664d | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 2243 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2244 | // Format string can be either ObjC string (e.g. @"%d") or |
Ted Kremenek | 34f664d | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 2245 | // C string (e.g. "%d") |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2246 | // ObjC string uses the same format specifiers as C string, so we can use |
Ted Kremenek | 34f664d | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 2247 | // the same format string checking logic for both ObjC and C strings. |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2248 | StringLiteralCheckType CT = |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2249 | checkFormatStringExpr(*this, OrigFormatExpr, Args, HasVAListArg, |
| 2250 | format_idx, firstDataArg, Type, CallType, |
| 2251 | /*IsFunctionCall*/true, CheckedVarArgs); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2252 | if (CT != SLCT_NotALiteral) |
| 2253 | // Literal format string found, check done! |
| 2254 | return CT == SLCT_CheckedLiteral; |
Ted Kremenek | 34f664d | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 2255 | |
Jean-Daniel Dupas | 6567f48 | 2012-02-07 23:10:53 +0000 | [diff] [blame] | 2256 | // Strftime is particular as it always uses a single 'time' argument, |
| 2257 | // so it is safe to pass a non-literal string. |
| 2258 | if (Type == FST_Strftime) |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2259 | return false; |
Jean-Daniel Dupas | 6567f48 | 2012-02-07 23:10:53 +0000 | [diff] [blame] | 2260 | |
Jean-Daniel Dupas | 537aa1a | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 2261 | // Do not emit diag when the string param is a macro expansion and the |
| 2262 | // format is either NSString or CFString. This is a hack to prevent |
| 2263 | // diag when using the NSLocalizedString and CFCopyLocalizedString macros |
| 2264 | // which are usually used in place of NS and CF string literals. |
Jean-Daniel Dupas | 2b7da83 | 2012-05-04 21:08:08 +0000 | [diff] [blame] | 2265 | if (Type == FST_NSString && |
| 2266 | SourceMgr.isInSystemMacro(Args[format_idx]->getLocStart())) |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2267 | return false; |
Jean-Daniel Dupas | 537aa1a | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 2268 | |
Chris Lattner | cc5d1c2 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 2269 | // If there are no arguments specified, warn with -Wformat-security, otherwise |
| 2270 | // warn only with -Wformat-nonliteral. |
Eli Friedman | 0e5d677 | 2013-06-18 18:10:01 +0000 | [diff] [blame] | 2271 | if (Args.size() == firstDataArg) |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2272 | Diag(Args[format_idx]->getLocStart(), |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2273 | diag::warn_format_nonliteral_noargs) |
Chris Lattner | cc5d1c2 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 2274 | << OrigFormatExpr->getSourceRange(); |
| 2275 | else |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2276 | Diag(Args[format_idx]->getLocStart(), |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2277 | diag::warn_format_nonliteral) |
Chris Lattner | cc5d1c2 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 2278 | << OrigFormatExpr->getSourceRange(); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2279 | return false; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2280 | } |
Ted Kremenek | e68f1aa | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 2281 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2282 | namespace { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2283 | class CheckFormatHandler : public analyze_format_string::FormatStringHandler { |
| 2284 | protected: |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2285 | Sema &S; |
| 2286 | const StringLiteral *FExpr; |
| 2287 | const Expr *OrigFormatExpr; |
Ted Kremenek | 4d745dd | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 2288 | const unsigned FirstDataArg; |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2289 | const unsigned NumDataArgs; |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2290 | const char *Beg; // Start of format string. |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2291 | const bool HasVAListArg; |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2292 | ArrayRef<const Expr *> Args; |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2293 | unsigned FormatIdx; |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2294 | llvm::SmallBitVector CoveredArgs; |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2295 | bool usesPositionalArgs; |
| 2296 | bool atFirstArg; |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2297 | bool inFunctionCall; |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2298 | Sema::VariadicCallType CallType; |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2299 | llvm::SmallBitVector &CheckedVarArgs; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2300 | public: |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2301 | CheckFormatHandler(Sema &s, const StringLiteral *fexpr, |
Ted Kremenek | 4d745dd | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 2302 | const Expr *origFormatExpr, unsigned firstDataArg, |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2303 | unsigned numDataArgs, const char *beg, bool hasVAListArg, |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2304 | ArrayRef<const Expr *> Args, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2305 | unsigned formatIdx, bool inFunctionCall, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2306 | Sema::VariadicCallType callType, |
| 2307 | llvm::SmallBitVector &CheckedVarArgs) |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2308 | : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2309 | FirstDataArg(firstDataArg), NumDataArgs(numDataArgs), |
| 2310 | Beg(beg), HasVAListArg(hasVAListArg), |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2311 | Args(Args), FormatIdx(formatIdx), |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2312 | usesPositionalArgs(false), atFirstArg(true), |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2313 | inFunctionCall(inFunctionCall), CallType(callType), |
| 2314 | CheckedVarArgs(CheckedVarArgs) { |
| 2315 | CoveredArgs.resize(numDataArgs); |
| 2316 | CoveredArgs.reset(); |
| 2317 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2318 | |
Ted Kremenek | 019d224 | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 2319 | void DoneProcessing(); |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2320 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2321 | void HandleIncompleteSpecifier(const char *startSpecifier, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2322 | unsigned specifierLen) override; |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2323 | |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2324 | void HandleInvalidLengthModifier( |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2325 | const analyze_format_string::FormatSpecifier &FS, |
| 2326 | const analyze_format_string::ConversionSpecifier &CS, |
| 2327 | const char *startSpecifier, unsigned specifierLen, |
| 2328 | unsigned DiagID); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2329 | |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2330 | void HandleNonStandardLengthModifier( |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2331 | const analyze_format_string::FormatSpecifier &FS, |
| 2332 | const char *startSpecifier, unsigned specifierLen); |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2333 | |
| 2334 | void HandleNonStandardConversionSpecifier( |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2335 | const analyze_format_string::ConversionSpecifier &CS, |
| 2336 | const char *startSpecifier, unsigned specifierLen); |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2337 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2338 | void HandlePosition(const char *startPos, unsigned posLen) override; |
Hans Wennborg | aa8c61c | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 2339 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2340 | void HandleInvalidPosition(const char *startSpecifier, |
| 2341 | unsigned specifierLen, |
| 2342 | analyze_format_string::PositionContext p) override; |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2343 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2344 | void HandleZeroPosition(const char *startPos, unsigned posLen) override; |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2345 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2346 | void HandleNullChar(const char *nullCharacter) override; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2347 | |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2348 | template <typename Range> |
| 2349 | static void EmitFormatDiagnostic(Sema &S, bool inFunctionCall, |
| 2350 | const Expr *ArgumentExpr, |
| 2351 | PartialDiagnostic PDiag, |
| 2352 | SourceLocation StringLoc, |
| 2353 | bool IsStringLocation, Range StringRange, |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 2354 | ArrayRef<FixItHint> Fixit = None); |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2355 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2356 | protected: |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2357 | bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, |
| 2358 | const char *startSpec, |
| 2359 | unsigned specifierLen, |
| 2360 | const char *csStart, unsigned csLen); |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2361 | |
| 2362 | void HandlePositionalNonpositionalArgs(SourceLocation Loc, |
| 2363 | const char *startSpec, |
| 2364 | unsigned specifierLen); |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2365 | |
Ted Kremenek | 8d9842d | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2366 | SourceRange getFormatStringRange(); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2367 | CharSourceRange getSpecifierRange(const char *startSpecifier, |
| 2368 | unsigned specifierLen); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2369 | SourceLocation getLocationOfByte(const char *x); |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2370 | |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2371 | const Expr *getDataArg(unsigned i) const; |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2372 | |
| 2373 | bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, |
| 2374 | const analyze_format_string::ConversionSpecifier &CS, |
| 2375 | const char *startSpecifier, unsigned specifierLen, |
| 2376 | unsigned argIndex); |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2377 | |
| 2378 | template <typename Range> |
| 2379 | void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc, |
| 2380 | bool IsStringLocation, Range StringRange, |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 2381 | ArrayRef<FixItHint> Fixit = None); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2382 | }; |
| 2383 | } |
| 2384 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2385 | SourceRange CheckFormatHandler::getFormatStringRange() { |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2386 | return OrigFormatExpr->getSourceRange(); |
| 2387 | } |
| 2388 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2389 | CharSourceRange CheckFormatHandler:: |
| 2390 | getSpecifierRange(const char *startSpecifier, unsigned specifierLen) { |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2391 | SourceLocation Start = getLocationOfByte(startSpecifier); |
| 2392 | SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1); |
| 2393 | |
| 2394 | // Advance the end SourceLocation by one due to half-open ranges. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2395 | End = End.getLocWithOffset(1); |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2396 | |
| 2397 | return CharSourceRange::getCharRange(Start, End); |
Ted Kremenek | 8d9842d | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2400 | SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2401 | return S.getLocationOfStringLiteralByte(FExpr, x - Beg); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2404 | void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, |
| 2405 | unsigned specifierLen){ |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2406 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier), |
| 2407 | getLocationOfByte(startSpecifier), |
| 2408 | /*IsStringLocation*/true, |
| 2409 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | c22f78d | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2412 | void CheckFormatHandler::HandleInvalidLengthModifier( |
| 2413 | const analyze_format_string::FormatSpecifier &FS, |
| 2414 | const analyze_format_string::ConversionSpecifier &CS, |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2415 | const char *startSpecifier, unsigned specifierLen, unsigned DiagID) { |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2416 | using namespace analyze_format_string; |
| 2417 | |
| 2418 | const LengthModifier &LM = FS.getLengthModifier(); |
| 2419 | CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); |
| 2420 | |
| 2421 | // See if we know how to fix this length modifier. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2422 | Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2423 | if (FixedLM) { |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2424 | EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2425 | getLocationOfByte(LM.getStart()), |
| 2426 | /*IsStringLocation*/true, |
| 2427 | getSpecifierRange(startSpecifier, specifierLen)); |
| 2428 | |
| 2429 | S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) |
| 2430 | << FixedLM->toString() |
| 2431 | << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); |
| 2432 | |
| 2433 | } else { |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2434 | FixItHint Hint; |
| 2435 | if (DiagID == diag::warn_format_nonsensical_length) |
| 2436 | Hint = FixItHint::CreateRemoval(LMRange); |
| 2437 | |
| 2438 | EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2439 | getLocationOfByte(LM.getStart()), |
| 2440 | /*IsStringLocation*/true, |
| 2441 | getSpecifierRange(startSpecifier, specifierLen), |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2442 | Hint); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2443 | } |
| 2444 | } |
| 2445 | |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2446 | void CheckFormatHandler::HandleNonStandardLengthModifier( |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2447 | const analyze_format_string::FormatSpecifier &FS, |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2448 | const char *startSpecifier, unsigned specifierLen) { |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2449 | using namespace analyze_format_string; |
| 2450 | |
| 2451 | const LengthModifier &LM = FS.getLengthModifier(); |
| 2452 | CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); |
| 2453 | |
| 2454 | // See if we know how to fix this length modifier. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2455 | Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2456 | if (FixedLM) { |
| 2457 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
| 2458 | << LM.toString() << 0, |
| 2459 | getLocationOfByte(LM.getStart()), |
| 2460 | /*IsStringLocation*/true, |
| 2461 | getSpecifierRange(startSpecifier, specifierLen)); |
| 2462 | |
| 2463 | S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) |
| 2464 | << FixedLM->toString() |
| 2465 | << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); |
| 2466 | |
| 2467 | } else { |
| 2468 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
| 2469 | << LM.toString() << 0, |
| 2470 | getLocationOfByte(LM.getStart()), |
| 2471 | /*IsStringLocation*/true, |
| 2472 | getSpecifierRange(startSpecifier, specifierLen)); |
| 2473 | } |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | void CheckFormatHandler::HandleNonStandardConversionSpecifier( |
| 2477 | const analyze_format_string::ConversionSpecifier &CS, |
| 2478 | const char *startSpecifier, unsigned specifierLen) { |
Jordan Rose | 4c266aa | 2012-09-13 02:11:15 +0000 | [diff] [blame] | 2479 | using namespace analyze_format_string; |
| 2480 | |
| 2481 | // See if we know how to fix this conversion specifier. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2482 | Optional<ConversionSpecifier> FixedCS = CS.getStandardSpecifier(); |
Jordan Rose | 4c266aa | 2012-09-13 02:11:15 +0000 | [diff] [blame] | 2483 | if (FixedCS) { |
| 2484 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
| 2485 | << CS.toString() << /*conversion specifier*/1, |
| 2486 | getLocationOfByte(CS.getStart()), |
| 2487 | /*IsStringLocation*/true, |
| 2488 | getSpecifierRange(startSpecifier, specifierLen)); |
| 2489 | |
| 2490 | CharSourceRange CSRange = getSpecifierRange(CS.getStart(), CS.getLength()); |
| 2491 | S.Diag(getLocationOfByte(CS.getStart()), diag::note_format_fix_specifier) |
| 2492 | << FixedCS->toString() |
| 2493 | << FixItHint::CreateReplacement(CSRange, FixedCS->toString()); |
| 2494 | } else { |
| 2495 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
| 2496 | << CS.toString() << /*conversion specifier*/1, |
| 2497 | getLocationOfByte(CS.getStart()), |
| 2498 | /*IsStringLocation*/true, |
| 2499 | getSpecifierRange(startSpecifier, specifierLen)); |
| 2500 | } |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2501 | } |
| 2502 | |
Hans Wennborg | aa8c61c | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 2503 | void CheckFormatHandler::HandlePosition(const char *startPos, |
| 2504 | unsigned posLen) { |
| 2505 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg), |
| 2506 | getLocationOfByte(startPos), |
| 2507 | /*IsStringLocation*/true, |
| 2508 | getSpecifierRange(startPos, posLen)); |
| 2509 | } |
| 2510 | |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2511 | void |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2512 | CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen, |
| 2513 | analyze_format_string::PositionContext p) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2514 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier) |
| 2515 | << (unsigned) p, |
| 2516 | getLocationOfByte(startPos), /*IsStringLocation*/true, |
| 2517 | getSpecifierRange(startPos, posLen)); |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2520 | void CheckFormatHandler::HandleZeroPosition(const char *startPos, |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2521 | unsigned posLen) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2522 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier), |
| 2523 | getLocationOfByte(startPos), |
| 2524 | /*IsStringLocation*/true, |
| 2525 | getSpecifierRange(startPos, posLen)); |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2528 | void CheckFormatHandler::HandleNullChar(const char *nullCharacter) { |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2529 | if (!isa<ObjCStringLiteral>(OrigFormatExpr)) { |
Ted Kremenek | 0d5b9ef | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 2530 | // The presence of a null character is likely an error. |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2531 | EmitFormatDiagnostic( |
| 2532 | S.PDiag(diag::warn_printf_format_string_contains_null_char), |
| 2533 | getLocationOfByte(nullCharacter), /*IsStringLocation*/true, |
| 2534 | getFormatStringRange()); |
Ted Kremenek | 0d5b9ef | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 2535 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2536 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2537 | |
Jordan Rose | 58bbe42 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2538 | // Note that this may return NULL if there was an error parsing or building |
| 2539 | // one of the argument expressions. |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2540 | const Expr *CheckFormatHandler::getDataArg(unsigned i) const { |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2541 | return Args[FirstDataArg + i]; |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2542 | } |
| 2543 | |
| 2544 | void CheckFormatHandler::DoneProcessing() { |
| 2545 | // Does the number of data arguments exceed the number of |
| 2546 | // format conversions in the format string? |
| 2547 | if (!HasVAListArg) { |
| 2548 | // Find any arguments that weren't covered. |
| 2549 | CoveredArgs.flip(); |
| 2550 | signed notCoveredArg = CoveredArgs.find_first(); |
| 2551 | if (notCoveredArg >= 0) { |
| 2552 | assert((unsigned)notCoveredArg < NumDataArgs); |
Jordan Rose | 58bbe42 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2553 | if (const Expr *E = getDataArg((unsigned) notCoveredArg)) { |
| 2554 | SourceLocation Loc = E->getLocStart(); |
| 2555 | if (!S.getSourceManager().isInSystemMacro(Loc)) { |
| 2556 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_data_arg_not_used), |
| 2557 | Loc, /*IsStringLocation*/false, |
| 2558 | getFormatStringRange()); |
| 2559 | } |
Bob Wilson | 23cd434 | 2012-05-03 19:47:19 +0000 | [diff] [blame] | 2560 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2561 | } |
| 2562 | } |
| 2563 | } |
| 2564 | |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2565 | bool |
| 2566 | CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, |
| 2567 | SourceLocation Loc, |
| 2568 | const char *startSpec, |
| 2569 | unsigned specifierLen, |
| 2570 | const char *csStart, |
| 2571 | unsigned csLen) { |
| 2572 | |
| 2573 | bool keepGoing = true; |
| 2574 | if (argIndex < NumDataArgs) { |
| 2575 | // Consider the argument coverered, even though the specifier doesn't |
| 2576 | // make sense. |
| 2577 | CoveredArgs.set(argIndex); |
| 2578 | } |
| 2579 | else { |
| 2580 | // If argIndex exceeds the number of data arguments we |
| 2581 | // don't issue a warning because that is just a cascade of warnings (and |
| 2582 | // they may have intended '%%' anyway). We don't want to continue processing |
| 2583 | // the format string after this point, however, as we will like just get |
| 2584 | // gibberish when trying to match arguments. |
| 2585 | keepGoing = false; |
| 2586 | } |
| 2587 | |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2588 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_conversion) |
| 2589 | << StringRef(csStart, csLen), |
| 2590 | Loc, /*IsStringLocation*/true, |
| 2591 | getSpecifierRange(startSpec, specifierLen)); |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2592 | |
| 2593 | return keepGoing; |
| 2594 | } |
| 2595 | |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2596 | void |
| 2597 | CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc, |
| 2598 | const char *startSpec, |
| 2599 | unsigned specifierLen) { |
| 2600 | EmitFormatDiagnostic( |
| 2601 | S.PDiag(diag::warn_format_mix_positional_nonpositional_args), |
| 2602 | Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen)); |
| 2603 | } |
| 2604 | |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2605 | bool |
| 2606 | CheckFormatHandler::CheckNumArgs( |
| 2607 | const analyze_format_string::FormatSpecifier &FS, |
| 2608 | const analyze_format_string::ConversionSpecifier &CS, |
| 2609 | const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { |
| 2610 | |
| 2611 | if (argIndex >= NumDataArgs) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2612 | PartialDiagnostic PDiag = FS.usesPositionalArg() |
| 2613 | ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args) |
| 2614 | << (argIndex+1) << NumDataArgs) |
| 2615 | : S.PDiag(diag::warn_printf_insufficient_data_args); |
| 2616 | EmitFormatDiagnostic( |
| 2617 | PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true, |
| 2618 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2619 | return false; |
| 2620 | } |
| 2621 | return true; |
| 2622 | } |
| 2623 | |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2624 | template<typename Range> |
| 2625 | void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag, |
| 2626 | SourceLocation Loc, |
| 2627 | bool IsStringLocation, |
| 2628 | Range StringRange, |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2629 | ArrayRef<FixItHint> FixIt) { |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2630 | EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag, |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2631 | Loc, IsStringLocation, StringRange, FixIt); |
| 2632 | } |
| 2633 | |
| 2634 | /// \brief If the format string is not within the funcion call, emit a note |
| 2635 | /// so that the function call and string are in diagnostic messages. |
| 2636 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 2637 | /// \param InFunctionCall if true, the format string is within the function |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2638 | /// call and only one diagnostic message will be produced. Otherwise, an |
| 2639 | /// extra note will be emitted pointing to location of the format string. |
| 2640 | /// |
| 2641 | /// \param ArgumentExpr the expression that is passed as the format string |
| 2642 | /// argument in the function call. Used for getting locations when two |
| 2643 | /// diagnostics are emitted. |
| 2644 | /// |
| 2645 | /// \param PDiag the callee should already have provided any strings for the |
| 2646 | /// diagnostic message. This function only adds locations and fixits |
| 2647 | /// to diagnostics. |
| 2648 | /// |
| 2649 | /// \param Loc primary location for diagnostic. If two diagnostics are |
| 2650 | /// required, one will be at Loc and a new SourceLocation will be created for |
| 2651 | /// the other one. |
| 2652 | /// |
| 2653 | /// \param IsStringLocation if true, Loc points to the format string should be |
| 2654 | /// used for the note. Otherwise, Loc points to the argument list and will |
| 2655 | /// be used with PDiag. |
| 2656 | /// |
| 2657 | /// \param StringRange some or all of the string to highlight. This is |
| 2658 | /// templated so it can accept either a CharSourceRange or a SourceRange. |
| 2659 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 2660 | /// \param FixIt optional fix it hint for the format string. |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2661 | template<typename Range> |
| 2662 | void CheckFormatHandler::EmitFormatDiagnostic(Sema &S, bool InFunctionCall, |
| 2663 | const Expr *ArgumentExpr, |
| 2664 | PartialDiagnostic PDiag, |
| 2665 | SourceLocation Loc, |
| 2666 | bool IsStringLocation, |
| 2667 | Range StringRange, |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2668 | ArrayRef<FixItHint> FixIt) { |
| 2669 | if (InFunctionCall) { |
| 2670 | const Sema::SemaDiagnosticBuilder &D = S.Diag(Loc, PDiag); |
| 2671 | D << StringRange; |
| 2672 | for (ArrayRef<FixItHint>::iterator I = FixIt.begin(), E = FixIt.end(); |
| 2673 | I != E; ++I) { |
| 2674 | D << *I; |
| 2675 | } |
| 2676 | } else { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2677 | S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag) |
| 2678 | << ArgumentExpr->getSourceRange(); |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2679 | |
| 2680 | const Sema::SemaDiagnosticBuilder &Note = |
| 2681 | S.Diag(IsStringLocation ? Loc : StringRange.getBegin(), |
| 2682 | diag::note_format_string_defined); |
| 2683 | |
| 2684 | Note << StringRange; |
| 2685 | for (ArrayRef<FixItHint>::iterator I = FixIt.begin(), E = FixIt.end(); |
| 2686 | I != E; ++I) { |
| 2687 | Note << *I; |
| 2688 | } |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2689 | } |
| 2690 | } |
| 2691 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2692 | //===--- CHECK: Printf format string checking ------------------------------===// |
| 2693 | |
| 2694 | namespace { |
| 2695 | class CheckPrintfHandler : public CheckFormatHandler { |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2696 | bool ObjCContext; |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2697 | public: |
| 2698 | CheckPrintfHandler(Sema &s, const StringLiteral *fexpr, |
| 2699 | const Expr *origFormatExpr, unsigned firstDataArg, |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2700 | unsigned numDataArgs, bool isObjC, |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2701 | const char *beg, bool hasVAListArg, |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2702 | ArrayRef<const Expr *> Args, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2703 | unsigned formatIdx, bool inFunctionCall, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2704 | Sema::VariadicCallType CallType, |
| 2705 | llvm::SmallBitVector &CheckedVarArgs) |
| 2706 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
| 2707 | numDataArgs, beg, hasVAListArg, Args, |
| 2708 | formatIdx, inFunctionCall, CallType, CheckedVarArgs), |
| 2709 | ObjCContext(isObjC) |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2710 | {} |
| 2711 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2712 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2713 | bool HandleInvalidPrintfConversionSpecifier( |
| 2714 | const analyze_printf::PrintfSpecifier &FS, |
| 2715 | const char *startSpecifier, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2716 | unsigned specifierLen) override; |
| 2717 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2718 | bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, |
| 2719 | const char *startSpecifier, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2720 | unsigned specifierLen) override; |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2721 | bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, |
| 2722 | const char *StartSpecifier, |
| 2723 | unsigned SpecifierLen, |
| 2724 | const Expr *E); |
| 2725 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2726 | bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, |
| 2727 | const char *startSpecifier, unsigned specifierLen); |
| 2728 | void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, |
| 2729 | const analyze_printf::OptionalAmount &Amt, |
| 2730 | unsigned type, |
| 2731 | const char *startSpecifier, unsigned specifierLen); |
| 2732 | void HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
| 2733 | const analyze_printf::OptionalFlag &flag, |
| 2734 | const char *startSpecifier, unsigned specifierLen); |
| 2735 | void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, |
| 2736 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 2737 | const analyze_printf::OptionalFlag &flag, |
| 2738 | const char *startSpecifier, unsigned specifierLen); |
Hans Wennborg | c3b3da0 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2739 | bool checkForCStrMembers(const analyze_printf::ArgType &AT, |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 2740 | const Expr *E); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2741 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2742 | }; |
| 2743 | } |
| 2744 | |
| 2745 | bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( |
| 2746 | const analyze_printf::PrintfSpecifier &FS, |
| 2747 | const char *startSpecifier, |
| 2748 | unsigned specifierLen) { |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2749 | const analyze_printf::PrintfConversionSpecifier &CS = |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2750 | FS.getConversionSpecifier(); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2751 | |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2752 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 2753 | getLocationOfByte(CS.getStart()), |
| 2754 | startSpecifier, specifierLen, |
| 2755 | CS.getStart(), CS.getLength()); |
Ted Kremenek | 94af575 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2758 | bool CheckPrintfHandler::HandleAmount( |
| 2759 | const analyze_format_string::OptionalAmount &Amt, |
| 2760 | unsigned k, const char *startSpecifier, |
| 2761 | unsigned specifierLen) { |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2762 | |
| 2763 | if (Amt.hasDataArgument()) { |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2764 | if (!HasVAListArg) { |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2765 | unsigned argIndex = Amt.getArgIndex(); |
| 2766 | if (argIndex >= NumDataArgs) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2767 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg) |
| 2768 | << k, |
| 2769 | getLocationOfByte(Amt.getStart()), |
| 2770 | /*IsStringLocation*/true, |
| 2771 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2772 | // Don't do any more checking. We will just emit |
| 2773 | // spurious errors. |
| 2774 | return false; |
| 2775 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2776 | |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2777 | // Type check the data argument. It should be an 'int'. |
Ted Kremenek | 605b011 | 2010-01-29 23:32:22 +0000 | [diff] [blame] | 2778 | // Although not in conformance with C99, we also allow the argument to be |
| 2779 | // an 'unsigned int' as that is a reasonably safe case. GCC also |
| 2780 | // doesn't emit a warning for that case. |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2781 | CoveredArgs.set(argIndex); |
| 2782 | const Expr *Arg = getDataArg(argIndex); |
Jordan Rose | 58bbe42 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2783 | if (!Arg) |
| 2784 | return false; |
| 2785 | |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2786 | QualType T = Arg->getType(); |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2787 | |
Hans Wennborg | c3b3da0 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2788 | const analyze_printf::ArgType &AT = Amt.getArgType(S.Context); |
| 2789 | assert(AT.isValid()); |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2790 | |
Hans Wennborg | c3b3da0 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2791 | if (!AT.matchesType(S.Context, T)) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2792 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type) |
Hans Wennborg | c3b3da0 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2793 | << k << AT.getRepresentativeTypeName(S.Context) |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2794 | << T << Arg->getSourceRange(), |
| 2795 | getLocationOfByte(Amt.getStart()), |
| 2796 | /*IsStringLocation*/true, |
| 2797 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2798 | // Don't do any more checking. We will just emit |
| 2799 | // spurious errors. |
| 2800 | return false; |
| 2801 | } |
| 2802 | } |
| 2803 | } |
| 2804 | return true; |
| 2805 | } |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2806 | |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2807 | void CheckPrintfHandler::HandleInvalidAmount( |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2808 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2809 | const analyze_printf::OptionalAmount &Amt, |
| 2810 | unsigned type, |
| 2811 | const char *startSpecifier, |
| 2812 | unsigned specifierLen) { |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2813 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 2814 | FS.getConversionSpecifier(); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2815 | |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2816 | FixItHint fixit = |
| 2817 | Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant |
| 2818 | ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), |
| 2819 | Amt.getConstantLength())) |
| 2820 | : FixItHint(); |
| 2821 | |
| 2822 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount) |
| 2823 | << type << CS.toString(), |
| 2824 | getLocationOfByte(Amt.getStart()), |
| 2825 | /*IsStringLocation*/true, |
| 2826 | getSpecifierRange(startSpecifier, specifierLen), |
| 2827 | fixit); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2830 | void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2831 | const analyze_printf::OptionalFlag &flag, |
| 2832 | const char *startSpecifier, |
| 2833 | unsigned specifierLen) { |
| 2834 | // Warn about pointless flag with a fixit removal. |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2835 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 2836 | FS.getConversionSpecifier(); |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2837 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag) |
| 2838 | << flag.toString() << CS.toString(), |
| 2839 | getLocationOfByte(flag.getPosition()), |
| 2840 | /*IsStringLocation*/true, |
| 2841 | getSpecifierRange(startSpecifier, specifierLen), |
| 2842 | FixItHint::CreateRemoval( |
| 2843 | getSpecifierRange(flag.getPosition(), 1))); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
| 2846 | void CheckPrintfHandler::HandleIgnoredFlag( |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2847 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2848 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 2849 | const analyze_printf::OptionalFlag &flag, |
| 2850 | const char *startSpecifier, |
| 2851 | unsigned specifierLen) { |
| 2852 | // Warn about ignored flag with a fixit removal. |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2853 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag) |
| 2854 | << ignoredFlag.toString() << flag.toString(), |
| 2855 | getLocationOfByte(ignoredFlag.getPosition()), |
| 2856 | /*IsStringLocation*/true, |
| 2857 | getSpecifierRange(startSpecifier, specifierLen), |
| 2858 | FixItHint::CreateRemoval( |
| 2859 | getSpecifierRange(ignoredFlag.getPosition(), 1))); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2862 | // Determines if the specified is a C++ class or struct containing |
| 2863 | // a member with the specified name and kind (e.g. a CXXMethodDecl named |
| 2864 | // "c_str()"). |
| 2865 | template<typename MemberKind> |
| 2866 | static llvm::SmallPtrSet<MemberKind*, 1> |
| 2867 | CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) { |
| 2868 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 2869 | llvm::SmallPtrSet<MemberKind*, 1> Results; |
| 2870 | |
| 2871 | if (!RT) |
| 2872 | return Results; |
| 2873 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 2874 | if (!RD || !RD->getDefinition()) |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2875 | return Results; |
| 2876 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 2877 | LookupResult R(S, &S.Context.Idents.get(Name), SourceLocation(), |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2878 | Sema::LookupMemberName); |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 2879 | R.suppressDiagnostics(); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2880 | |
| 2881 | // We just need to include all members of the right kind turned up by the |
| 2882 | // filter, at this point. |
| 2883 | if (S.LookupQualifiedName(R, RT->getDecl())) |
| 2884 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2885 | NamedDecl *decl = (*I)->getUnderlyingDecl(); |
| 2886 | if (MemberKind *FK = dyn_cast<MemberKind>(decl)) |
| 2887 | Results.insert(FK); |
| 2888 | } |
| 2889 | return Results; |
| 2890 | } |
| 2891 | |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 2892 | /// Check if we could call '.c_str()' on an object. |
| 2893 | /// |
| 2894 | /// FIXME: This returns the wrong results in some cases (if cv-qualifiers don't |
| 2895 | /// allow the call, or if it would be ambiguous). |
| 2896 | bool Sema::hasCStrMethod(const Expr *E) { |
| 2897 | typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet; |
| 2898 | MethodSet Results = |
| 2899 | CXXRecordMembersNamed<CXXMethodDecl>("c_str", *this, E->getType()); |
| 2900 | for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); |
| 2901 | MI != ME; ++MI) |
| 2902 | if ((*MI)->getMinRequiredArguments() == 0) |
| 2903 | return true; |
| 2904 | return false; |
| 2905 | } |
| 2906 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2907 | // Check if a (w)string was passed when a (w)char* was needed, and offer a |
Hans Wennborg | c3b3da0 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2908 | // better diagnostic if so. AT is assumed to be valid. |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2909 | // Returns true when a c_str() conversion method is found. |
| 2910 | bool CheckPrintfHandler::checkForCStrMembers( |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 2911 | const analyze_printf::ArgType &AT, const Expr *E) { |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2912 | typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet; |
| 2913 | |
| 2914 | MethodSet Results = |
| 2915 | CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType()); |
| 2916 | |
| 2917 | for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); |
| 2918 | MI != ME; ++MI) { |
| 2919 | const CXXMethodDecl *Method = *MI; |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 2920 | if (Method->getMinRequiredArguments() == 0 && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2921 | AT.matchesType(S.Context, Method->getReturnType())) { |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2922 | // FIXME: Suggest parens if the expression needs them. |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 2923 | SourceLocation EndLoc = S.getLocForEndOfToken(E->getLocEnd()); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2924 | S.Diag(E->getLocStart(), diag::note_printf_c_str) |
| 2925 | << "c_str()" |
| 2926 | << FixItHint::CreateInsertion(EndLoc, ".c_str()"); |
| 2927 | return true; |
| 2928 | } |
| 2929 | } |
| 2930 | |
| 2931 | return false; |
| 2932 | } |
| 2933 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2934 | bool |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2935 | CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier |
Ted Kremenek | d31b263 | 2010-02-11 09:27:41 +0000 | [diff] [blame] | 2936 | &FS, |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2937 | const char *startSpecifier, |
| 2938 | unsigned specifierLen) { |
| 2939 | |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2940 | using namespace analyze_format_string; |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2941 | using namespace analyze_printf; |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2942 | const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2943 | |
Ted Kremenek | 6cd6942 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2944 | if (FS.consumesDataArgument()) { |
| 2945 | if (atFirstArg) { |
| 2946 | atFirstArg = false; |
| 2947 | usesPositionalArgs = FS.usesPositionalArg(); |
| 2948 | } |
| 2949 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2950 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), |
| 2951 | startSpecifier, specifierLen); |
Ted Kremenek | 6cd6942 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2952 | return false; |
| 2953 | } |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2954 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2955 | |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2956 | // First check if the field width, precision, and conversion specifier |
| 2957 | // have matching data arguments. |
| 2958 | if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, |
| 2959 | startSpecifier, specifierLen)) { |
| 2960 | return false; |
| 2961 | } |
| 2962 | |
| 2963 | if (!HandleAmount(FS.getPrecision(), /* precision */ 1, |
| 2964 | startSpecifier, specifierLen)) { |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2965 | return false; |
| 2966 | } |
| 2967 | |
Ted Kremenek | 8d9842d | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2968 | if (!CS.consumesDataArgument()) { |
| 2969 | // FIXME: Technically specifying a precision or field width here |
| 2970 | // makes no sense. Worth issuing a warning at some point. |
Ted Kremenek | fb45d35 | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 2971 | return true; |
Ted Kremenek | 8d9842d | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2972 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2973 | |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2974 | // Consume the argument. |
| 2975 | unsigned argIndex = FS.getArgIndex(); |
Ted Kremenek | 09597b4 | 2010-02-27 08:34:51 +0000 | [diff] [blame] | 2976 | if (argIndex < NumDataArgs) { |
| 2977 | // The check to see if the argIndex is valid will come later. |
| 2978 | // We set the bit here because we may exit early from this |
| 2979 | // function if we encounter some other error. |
| 2980 | CoveredArgs.set(argIndex); |
| 2981 | } |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2982 | |
| 2983 | // Check for using an Objective-C specific conversion specifier |
| 2984 | // in a non-ObjC literal. |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2985 | if (!ObjCContext && CS.isObjCArg()) { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2986 | return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, |
| 2987 | specifierLen); |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2988 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2989 | |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2990 | // Check for invalid use of field width |
| 2991 | if (!FS.hasValidFieldWidth()) { |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2992 | HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2993 | startSpecifier, specifierLen); |
| 2994 | } |
| 2995 | |
| 2996 | // Check for invalid use of precision |
| 2997 | if (!FS.hasValidPrecision()) { |
| 2998 | HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, |
| 2999 | startSpecifier, specifierLen); |
| 3000 | } |
| 3001 | |
| 3002 | // Check each flag does not conflict with any other component. |
Ted Kremenek | bf4832c | 2011-01-08 05:28:46 +0000 | [diff] [blame] | 3003 | if (!FS.hasValidThousandsGroupingPrefix()) |
| 3004 | HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 3005 | if (!FS.hasValidLeadingZeros()) |
| 3006 | HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); |
| 3007 | if (!FS.hasValidPlusPrefix()) |
| 3008 | HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 3009 | if (!FS.hasValidSpacePrefix()) |
| 3010 | HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 3011 | if (!FS.hasValidAlternativeForm()) |
| 3012 | HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); |
| 3013 | if (!FS.hasValidLeftJustified()) |
| 3014 | HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); |
| 3015 | |
| 3016 | // Check that flags are not ignored by another flag |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 3017 | if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' |
| 3018 | HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), |
| 3019 | startSpecifier, specifierLen); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 3020 | if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' |
| 3021 | HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), |
| 3022 | startSpecifier, specifierLen); |
| 3023 | |
| 3024 | // Check the length modifier is valid with the given conversion specifier. |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3025 | if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo())) |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3026 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
| 3027 | diag::warn_format_nonsensical_length); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3028 | else if (!FS.hasStandardLengthModifier()) |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3029 | HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3030 | else if (!FS.hasStandardLengthConversionCombination()) |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3031 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
| 3032 | diag::warn_format_non_standard_conversion_spec); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 3033 | |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3034 | if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) |
| 3035 | HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); |
| 3036 | |
Ted Kremenek | 9fcd830 | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 3037 | // The remaining checks depend on the data arguments. |
| 3038 | if (HasVAListArg) |
| 3039 | return true; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3040 | |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 3041 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | 9fcd830 | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 3042 | return false; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3043 | |
Jordan Rose | 58bbe42 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 3044 | const Expr *Arg = getDataArg(argIndex); |
| 3045 | if (!Arg) |
| 3046 | return true; |
| 3047 | |
| 3048 | return checkFormatExpr(FS, startSpecifier, specifierLen, Arg); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3051 | static bool requiresParensToAddCast(const Expr *E) { |
| 3052 | // FIXME: We should have a general way to reason about operator |
| 3053 | // precedence and whether parens are actually needed here. |
| 3054 | // Take care of a few common cases where they aren't. |
| 3055 | const Expr *Inside = E->IgnoreImpCasts(); |
| 3056 | if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Inside)) |
| 3057 | Inside = POE->getSyntacticForm()->IgnoreImpCasts(); |
| 3058 | |
| 3059 | switch (Inside->getStmtClass()) { |
| 3060 | case Stmt::ArraySubscriptExprClass: |
| 3061 | case Stmt::CallExprClass: |
Jordan Rose | ea0fdfe | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 3062 | case Stmt::CharacterLiteralClass: |
| 3063 | case Stmt::CXXBoolLiteralExprClass: |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3064 | case Stmt::DeclRefExprClass: |
Jordan Rose | ea0fdfe | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 3065 | case Stmt::FloatingLiteralClass: |
| 3066 | case Stmt::IntegerLiteralClass: |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3067 | case Stmt::MemberExprClass: |
Jordan Rose | ea0fdfe | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 3068 | case Stmt::ObjCArrayLiteralClass: |
| 3069 | case Stmt::ObjCBoolLiteralExprClass: |
| 3070 | case Stmt::ObjCBoxedExprClass: |
| 3071 | case Stmt::ObjCDictionaryLiteralClass: |
| 3072 | case Stmt::ObjCEncodeExprClass: |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3073 | case Stmt::ObjCIvarRefExprClass: |
| 3074 | case Stmt::ObjCMessageExprClass: |
| 3075 | case Stmt::ObjCPropertyRefExprClass: |
Jordan Rose | ea0fdfe | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 3076 | case Stmt::ObjCStringLiteralClass: |
| 3077 | case Stmt::ObjCSubscriptRefExprClass: |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3078 | case Stmt::ParenExprClass: |
Jordan Rose | ea0fdfe | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 3079 | case Stmt::StringLiteralClass: |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3080 | case Stmt::UnaryOperatorClass: |
| 3081 | return false; |
| 3082 | default: |
| 3083 | return true; |
| 3084 | } |
| 3085 | } |
| 3086 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3087 | bool |
| 3088 | CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, |
| 3089 | const char *StartSpecifier, |
| 3090 | unsigned SpecifierLen, |
| 3091 | const Expr *E) { |
| 3092 | using namespace analyze_format_string; |
| 3093 | using namespace analyze_printf; |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 3094 | // Now type check the data expression that matches the |
| 3095 | // format specifier. |
Hans Wennborg | c3b3da0 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 3096 | const analyze_printf::ArgType &AT = FS.getArgType(S.Context, |
| 3097 | ObjCContext); |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3098 | if (!AT.isValid()) |
| 3099 | return true; |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3100 | |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 3101 | QualType ExprTy = E->getType(); |
Ted Kremenek | 3365e52 | 2013-04-10 06:26:26 +0000 | [diff] [blame] | 3102 | while (const TypeOfExprType *TET = dyn_cast<TypeOfExprType>(ExprTy)) { |
| 3103 | ExprTy = TET->getUnderlyingExpr()->getType(); |
| 3104 | } |
| 3105 | |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 3106 | if (AT.matchesType(S.Context, ExprTy)) |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3107 | return true; |
Jordan Rose | 9870998 | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 3108 | |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3109 | // Look through argument promotions for our error message's reported type. |
| 3110 | // This includes the integral and floating promotions, but excludes array |
| 3111 | // and function pointer decay; seeing that an argument intended to be a |
| 3112 | // string has type 'char [6]' is probably more confusing than 'char *'. |
| 3113 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
| 3114 | if (ICE->getCastKind() == CK_IntegralCast || |
| 3115 | ICE->getCastKind() == CK_FloatingCast) { |
| 3116 | E = ICE->getSubExpr(); |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 3117 | ExprTy = E->getType(); |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3118 | |
| 3119 | // Check if we didn't match because of an implicit cast from a 'char' |
| 3120 | // or 'short' to an 'int'. This is done because printf is a varargs |
| 3121 | // function. |
| 3122 | if (ICE->getType() == S.Context.IntTy || |
| 3123 | ICE->getType() == S.Context.UnsignedIntTy) { |
| 3124 | // All further checking is done on the subexpression. |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 3125 | if (AT.matchesType(S.Context, ExprTy)) |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3126 | return true; |
Ted Kremenek | 12a37de | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 3127 | } |
Jordan Rose | 9870998 | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 3128 | } |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 3129 | } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) { |
| 3130 | // Special case for 'a', which has type 'int' in C. |
| 3131 | // Note, however, that we do /not/ want to treat multibyte constants like |
| 3132 | // 'MooV' as characters! This form is deprecated but still exists. |
| 3133 | if (ExprTy == S.Context.IntTy) |
| 3134 | if (llvm::isUIntN(S.Context.getCharWidth(), CL->getValue())) |
| 3135 | ExprTy = S.Context.CharTy; |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3136 | } |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 3137 | |
Jordan Rose | bc53ed1 | 2014-05-31 04:12:14 +0000 | [diff] [blame] | 3138 | // Look through enums to their underlying type. |
| 3139 | bool IsEnum = false; |
| 3140 | if (auto EnumTy = ExprTy->getAs<EnumType>()) { |
| 3141 | ExprTy = EnumTy->getDecl()->getIntegerType(); |
| 3142 | IsEnum = true; |
| 3143 | } |
| 3144 | |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3145 | // %C in an Objective-C context prints a unichar, not a wchar_t. |
| 3146 | // If the argument is an integer of some kind, believe the %C and suggest |
| 3147 | // a cast instead of changing the conversion specifier. |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 3148 | QualType IntendedTy = ExprTy; |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3149 | if (ObjCContext && |
| 3150 | FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) { |
| 3151 | if (ExprTy->isIntegralOrUnscopedEnumerationType() && |
| 3152 | !ExprTy->isCharType()) { |
| 3153 | // 'unichar' is defined as a typedef of unsigned short, but we should |
| 3154 | // prefer using the typedef if it is visible. |
| 3155 | IntendedTy = S.Context.UnsignedShortTy; |
Ted Kremenek | da2f405 | 2013-10-15 05:25:17 +0000 | [diff] [blame] | 3156 | |
| 3157 | // While we are here, check if the value is an IntegerLiteral that happens |
| 3158 | // to be within the valid range. |
| 3159 | if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) { |
| 3160 | const llvm::APInt &V = IL->getValue(); |
| 3161 | if (V.getActiveBits() <= S.Context.getTypeSize(IntendedTy)) |
| 3162 | return true; |
| 3163 | } |
| 3164 | |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3165 | LookupResult Result(S, &S.Context.Idents.get("unichar"), E->getLocStart(), |
| 3166 | Sema::LookupOrdinaryName); |
| 3167 | if (S.LookupName(Result, S.getCurScope())) { |
| 3168 | NamedDecl *ND = Result.getFoundDecl(); |
| 3169 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND)) |
| 3170 | if (TD->getUnderlyingType() == IntendedTy) |
| 3171 | IntendedTy = S.Context.getTypedefType(TD); |
| 3172 | } |
| 3173 | } |
| 3174 | } |
| 3175 | |
| 3176 | // Special-case some of Darwin's platform-independence types by suggesting |
| 3177 | // casts to primitive types that are known to be large enough. |
| 3178 | bool ShouldNotPrintDirectly = false; |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3179 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Ted Kremenek | cd3d440 | 2013-03-25 22:28:37 +0000 | [diff] [blame] | 3180 | // Use a 'while' to peel off layers of typedefs. |
| 3181 | QualType TyTy = IntendedTy; |
| 3182 | while (const TypedefType *UserTy = TyTy->getAs<TypedefType>()) { |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3183 | StringRef Name = UserTy->getDecl()->getName(); |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3184 | QualType CastTy = llvm::StringSwitch<QualType>(Name) |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3185 | .Case("NSInteger", S.Context.LongTy) |
| 3186 | .Case("NSUInteger", S.Context.UnsignedLongTy) |
| 3187 | .Case("SInt32", S.Context.IntTy) |
| 3188 | .Case("UInt32", S.Context.UnsignedIntTy) |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3189 | .Default(QualType()); |
| 3190 | |
| 3191 | if (!CastTy.isNull()) { |
| 3192 | ShouldNotPrintDirectly = true; |
| 3193 | IntendedTy = CastTy; |
Ted Kremenek | cd3d440 | 2013-03-25 22:28:37 +0000 | [diff] [blame] | 3194 | break; |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3195 | } |
Ted Kremenek | cd3d440 | 2013-03-25 22:28:37 +0000 | [diff] [blame] | 3196 | TyTy = UserTy->desugar(); |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3197 | } |
| 3198 | } |
| 3199 | |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3200 | // We may be able to offer a FixItHint if it is a supported type. |
| 3201 | PrintfSpecifier fixedFS = FS; |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3202 | bool success = fixedFS.fixType(IntendedTy, S.getLangOpts(), |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3203 | S.Context, ObjCContext); |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 3204 | |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3205 | if (success) { |
| 3206 | // Get the fix string from the fixed format specifier |
| 3207 | SmallString<16> buf; |
| 3208 | llvm::raw_svector_ostream os(buf); |
| 3209 | fixedFS.toString(os); |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 3210 | |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3211 | CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen); |
| 3212 | |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3213 | if (IntendedTy == ExprTy) { |
| 3214 | // In this case, the specifier is wrong and should be changed to match |
| 3215 | // the argument. |
| 3216 | EmitFormatDiagnostic( |
Jordan Rose | bc53ed1 | 2014-05-31 04:12:14 +0000 | [diff] [blame] | 3217 | S.PDiag(diag::warn_format_conversion_argument_type_mismatch) |
| 3218 | << AT.getRepresentativeTypeName(S.Context) << IntendedTy << IsEnum |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3219 | << E->getSourceRange(), |
| 3220 | E->getLocStart(), |
| 3221 | /*IsStringLocation*/false, |
| 3222 | SpecRange, |
| 3223 | FixItHint::CreateReplacement(SpecRange, os.str())); |
| 3224 | |
| 3225 | } else { |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3226 | // The canonical type for formatting this value is different from the |
| 3227 | // actual type of the expression. (This occurs, for example, with Darwin's |
| 3228 | // NSInteger on 32-bit platforms, where it is typedef'd as 'int', but |
| 3229 | // should be printed as 'long' for 64-bit compatibility.) |
| 3230 | // Rather than emitting a normal format/argument mismatch, we want to |
| 3231 | // add a cast to the recommended type (and correct the format string |
| 3232 | // if necessary). |
| 3233 | SmallString<16> CastBuf; |
| 3234 | llvm::raw_svector_ostream CastFix(CastBuf); |
| 3235 | CastFix << "("; |
| 3236 | IntendedTy.print(CastFix, S.Context.getPrintingPolicy()); |
| 3237 | CastFix << ")"; |
| 3238 | |
| 3239 | SmallVector<FixItHint,4> Hints; |
| 3240 | if (!AT.matchesType(S.Context, IntendedTy)) |
| 3241 | Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str())); |
| 3242 | |
| 3243 | if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) { |
| 3244 | // If there's already a cast present, just replace it. |
| 3245 | SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc()); |
| 3246 | Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str())); |
| 3247 | |
| 3248 | } else if (!requiresParensToAddCast(E)) { |
| 3249 | // If the expression has high enough precedence, |
| 3250 | // just write the C-style cast. |
| 3251 | Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(), |
| 3252 | CastFix.str())); |
| 3253 | } else { |
| 3254 | // Otherwise, add parens around the expression as well as the cast. |
| 3255 | CastFix << "("; |
| 3256 | Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(), |
| 3257 | CastFix.str())); |
| 3258 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 3259 | SourceLocation After = S.getLocForEndOfToken(E->getLocEnd()); |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3260 | Hints.push_back(FixItHint::CreateInsertion(After, ")")); |
| 3261 | } |
| 3262 | |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3263 | if (ShouldNotPrintDirectly) { |
| 3264 | // The expression has a type that should not be printed directly. |
| 3265 | // We extract the name from the typedef because we don't want to show |
| 3266 | // the underlying type in the diagnostic. |
| 3267 | StringRef Name = cast<TypedefType>(ExprTy)->getDecl()->getName(); |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3268 | |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3269 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_argument_needs_cast) |
Jordan Rose | bc53ed1 | 2014-05-31 04:12:14 +0000 | [diff] [blame] | 3270 | << Name << IntendedTy << IsEnum |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3271 | << E->getSourceRange(), |
| 3272 | E->getLocStart(), /*IsStringLocation=*/false, |
| 3273 | SpecRange, Hints); |
| 3274 | } else { |
| 3275 | // In this case, the expression could be printed using a different |
| 3276 | // specifier, but we've decided that the specifier is probably correct |
| 3277 | // and we should cast instead. Just use the normal warning message. |
| 3278 | EmitFormatDiagnostic( |
Jordan Rose | bc53ed1 | 2014-05-31 04:12:14 +0000 | [diff] [blame] | 3279 | S.PDiag(diag::warn_format_conversion_argument_type_mismatch) |
| 3280 | << AT.getRepresentativeTypeName(S.Context) << ExprTy << IsEnum |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3281 | << E->getSourceRange(), |
| 3282 | E->getLocStart(), /*IsStringLocation*/false, |
| 3283 | SpecRange, Hints); |
| 3284 | } |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3285 | } |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3286 | } else { |
| 3287 | const CharSourceRange &CSR = getSpecifierRange(StartSpecifier, |
| 3288 | SpecifierLen); |
| 3289 | // Since the warning for passing non-POD types to variadic functions |
| 3290 | // was deferred until now, we emit a warning for non-POD |
| 3291 | // arguments here. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3292 | switch (S.isValidVarArgType(ExprTy)) { |
| 3293 | case Sema::VAK_Valid: |
| 3294 | case Sema::VAK_ValidInCXX11: |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3295 | EmitFormatDiagnostic( |
Jordan Rose | bc53ed1 | 2014-05-31 04:12:14 +0000 | [diff] [blame] | 3296 | S.PDiag(diag::warn_format_conversion_argument_type_mismatch) |
| 3297 | << AT.getRepresentativeTypeName(S.Context) << ExprTy << IsEnum |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3298 | << CSR |
| 3299 | << E->getSourceRange(), |
| 3300 | E->getLocStart(), /*IsStringLocation*/false, CSR); |
| 3301 | break; |
| 3302 | |
| 3303 | case Sema::VAK_Undefined: |
| 3304 | EmitFormatDiagnostic( |
| 3305 | S.PDiag(diag::warn_non_pod_vararg_with_format_string) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3306 | << S.getLangOpts().CPlusPlus11 |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 3307 | << ExprTy |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3308 | << CallType |
| 3309 | << AT.getRepresentativeTypeName(S.Context) |
| 3310 | << CSR |
| 3311 | << E->getSourceRange(), |
| 3312 | E->getLocStart(), /*IsStringLocation*/false, CSR); |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 3313 | checkForCStrMembers(AT, E); |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3314 | break; |
| 3315 | |
| 3316 | case Sema::VAK_Invalid: |
| 3317 | if (ExprTy->isObjCObjectType()) |
| 3318 | EmitFormatDiagnostic( |
| 3319 | S.PDiag(diag::err_cannot_pass_objc_interface_to_vararg_format) |
| 3320 | << S.getLangOpts().CPlusPlus11 |
| 3321 | << ExprTy |
| 3322 | << CallType |
| 3323 | << AT.getRepresentativeTypeName(S.Context) |
| 3324 | << CSR |
| 3325 | << E->getSourceRange(), |
| 3326 | E->getLocStart(), /*IsStringLocation*/false, CSR); |
| 3327 | else |
| 3328 | // FIXME: If this is an initializer list, suggest removing the braces |
| 3329 | // or inserting a cast to the target type. |
| 3330 | S.Diag(E->getLocStart(), diag::err_cannot_pass_to_vararg_format) |
| 3331 | << isa<InitListExpr>(E) << ExprTy << CallType |
| 3332 | << AT.getRepresentativeTypeName(S.Context) |
| 3333 | << E->getSourceRange(); |
| 3334 | break; |
| 3335 | } |
| 3336 | |
| 3337 | assert(FirstDataArg + FS.getArgIndex() < CheckedVarArgs.size() && |
| 3338 | "format string specifier index out of range"); |
| 3339 | CheckedVarArgs[FirstDataArg + FS.getArgIndex()] = true; |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3342 | return true; |
| 3343 | } |
| 3344 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3345 | //===--- CHECK: Scanf format string checking ------------------------------===// |
| 3346 | |
| 3347 | namespace { |
| 3348 | class CheckScanfHandler : public CheckFormatHandler { |
| 3349 | public: |
| 3350 | CheckScanfHandler(Sema &s, const StringLiteral *fexpr, |
| 3351 | const Expr *origFormatExpr, unsigned firstDataArg, |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 3352 | unsigned numDataArgs, const char *beg, bool hasVAListArg, |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 3353 | ArrayRef<const Expr *> Args, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3354 | unsigned formatIdx, bool inFunctionCall, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3355 | Sema::VariadicCallType CallType, |
| 3356 | llvm::SmallBitVector &CheckedVarArgs) |
| 3357 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
| 3358 | numDataArgs, beg, hasVAListArg, |
| 3359 | Args, formatIdx, inFunctionCall, CallType, |
| 3360 | CheckedVarArgs) |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3361 | {} |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3362 | |
| 3363 | bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, |
| 3364 | const char *startSpecifier, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3365 | unsigned specifierLen) override; |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 3366 | |
| 3367 | bool HandleInvalidScanfConversionSpecifier( |
| 3368 | const analyze_scanf::ScanfSpecifier &FS, |
| 3369 | const char *startSpecifier, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3370 | unsigned specifierLen) override; |
Ted Kremenek | d7b31cc | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 3371 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3372 | void HandleIncompleteScanList(const char *start, const char *end) override; |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3373 | }; |
Ted Kremenek | 019d224 | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 3374 | } |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3375 | |
Ted Kremenek | d7b31cc | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 3376 | void CheckScanfHandler::HandleIncompleteScanList(const char *start, |
| 3377 | const char *end) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3378 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete), |
| 3379 | getLocationOfByte(end), /*IsStringLocation*/true, |
| 3380 | getSpecifierRange(start, end - start)); |
Ted Kremenek | d7b31cc | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 3381 | } |
| 3382 | |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 3383 | bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( |
| 3384 | const analyze_scanf::ScanfSpecifier &FS, |
| 3385 | const char *startSpecifier, |
| 3386 | unsigned specifierLen) { |
| 3387 | |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 3388 | const analyze_scanf::ScanfConversionSpecifier &CS = |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 3389 | FS.getConversionSpecifier(); |
| 3390 | |
| 3391 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 3392 | getLocationOfByte(CS.getStart()), |
| 3393 | startSpecifier, specifierLen, |
| 3394 | CS.getStart(), CS.getLength()); |
| 3395 | } |
| 3396 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3397 | bool CheckScanfHandler::HandleScanfSpecifier( |
| 3398 | const analyze_scanf::ScanfSpecifier &FS, |
| 3399 | const char *startSpecifier, |
| 3400 | unsigned specifierLen) { |
| 3401 | |
| 3402 | using namespace analyze_scanf; |
| 3403 | using namespace analyze_format_string; |
| 3404 | |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 3405 | const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3406 | |
Ted Kremenek | 6cd6942 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 3407 | // Handle case where '%' and '*' don't consume an argument. These shouldn't |
| 3408 | // be used to decide if we are using positional arguments consistently. |
| 3409 | if (FS.consumesDataArgument()) { |
| 3410 | if (atFirstArg) { |
| 3411 | atFirstArg = false; |
| 3412 | usesPositionalArgs = FS.usesPositionalArg(); |
| 3413 | } |
| 3414 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3415 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), |
| 3416 | startSpecifier, specifierLen); |
Ted Kremenek | 6cd6942 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 3417 | return false; |
| 3418 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3419 | } |
| 3420 | |
| 3421 | // Check if the field with is non-zero. |
| 3422 | const OptionalAmount &Amt = FS.getFieldWidth(); |
| 3423 | if (Amt.getHowSpecified() == OptionalAmount::Constant) { |
| 3424 | if (Amt.getConstantAmount() == 0) { |
| 3425 | const CharSourceRange &R = getSpecifierRange(Amt.getStart(), |
| 3426 | Amt.getConstantLength()); |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3427 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width), |
| 3428 | getLocationOfByte(Amt.getStart()), |
| 3429 | /*IsStringLocation*/true, R, |
| 3430 | FixItHint::CreateRemoval(R)); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3431 | } |
| 3432 | } |
| 3433 | |
| 3434 | if (!FS.consumesDataArgument()) { |
| 3435 | // FIXME: Technically specifying a precision or field width here |
| 3436 | // makes no sense. Worth issuing a warning at some point. |
| 3437 | return true; |
| 3438 | } |
| 3439 | |
| 3440 | // Consume the argument. |
| 3441 | unsigned argIndex = FS.getArgIndex(); |
| 3442 | if (argIndex < NumDataArgs) { |
| 3443 | // The check to see if the argIndex is valid will come later. |
| 3444 | // We set the bit here because we may exit early from this |
| 3445 | // function if we encounter some other error. |
| 3446 | CoveredArgs.set(argIndex); |
| 3447 | } |
| 3448 | |
Ted Kremenek | 4407ea4 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 3449 | // Check the length modifier is valid with the given conversion specifier. |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3450 | if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo())) |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3451 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
| 3452 | diag::warn_format_nonsensical_length); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3453 | else if (!FS.hasStandardLengthModifier()) |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3454 | HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3455 | else if (!FS.hasStandardLengthConversionCombination()) |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3456 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
| 3457 | diag::warn_format_non_standard_conversion_spec); |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 3458 | |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3459 | if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) |
| 3460 | HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); |
| 3461 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3462 | // The remaining checks depend on the data arguments. |
| 3463 | if (HasVAListArg) |
| 3464 | return true; |
| 3465 | |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 3466 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3467 | return false; |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3468 | |
Hans Wennborg | b1a5e09 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3469 | // Check that the argument type matches the format specifier. |
| 3470 | const Expr *Ex = getDataArg(argIndex); |
Jordan Rose | 58bbe42 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 3471 | if (!Ex) |
| 3472 | return true; |
| 3473 | |
Hans Wennborg | b1ab2a8 | 2012-08-07 08:59:46 +0000 | [diff] [blame] | 3474 | const analyze_format_string::ArgType &AT = FS.getArgType(S.Context); |
| 3475 | if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType())) { |
Hans Wennborg | b1a5e09 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3476 | ScanfSpecifier fixedFS = FS; |
Jordan Rose | 177b0a3 | 2014-03-20 03:32:39 +0000 | [diff] [blame] | 3477 | bool success = fixedFS.fixType(Ex->getType(), |
| 3478 | Ex->IgnoreImpCasts()->getType(), |
| 3479 | S.getLangOpts(), S.Context); |
Hans Wennborg | b1a5e09 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3480 | |
| 3481 | if (success) { |
| 3482 | // Get the fix string from the fixed format specifier. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3483 | SmallString<128> buf; |
Hans Wennborg | b1a5e09 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3484 | llvm::raw_svector_ostream os(buf); |
| 3485 | fixedFS.toString(os); |
| 3486 | |
| 3487 | EmitFormatDiagnostic( |
Jordan Rose | bc53ed1 | 2014-05-31 04:12:14 +0000 | [diff] [blame] | 3488 | S.PDiag(diag::warn_format_conversion_argument_type_mismatch) |
| 3489 | << AT.getRepresentativeTypeName(S.Context) << Ex->getType() << false |
Hans Wennborg | b1a5e09 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3490 | << Ex->getSourceRange(), |
Matt Beaumont-Gay | 32d825a | 2012-05-17 00:03:16 +0000 | [diff] [blame] | 3491 | Ex->getLocStart(), |
| 3492 | /*IsStringLocation*/false, |
Hans Wennborg | b1a5e09 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3493 | getSpecifierRange(startSpecifier, specifierLen), |
| 3494 | FixItHint::CreateReplacement( |
| 3495 | getSpecifierRange(startSpecifier, specifierLen), |
| 3496 | os.str())); |
| 3497 | } else { |
Jean-Daniel Dupas | cb197b0 | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 3498 | EmitFormatDiagnostic( |
Jordan Rose | bc53ed1 | 2014-05-31 04:12:14 +0000 | [diff] [blame] | 3499 | S.PDiag(diag::warn_format_conversion_argument_type_mismatch) |
| 3500 | << AT.getRepresentativeTypeName(S.Context) << Ex->getType() << false |
Jean-Daniel Dupas | cb197b0 | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 3501 | << Ex->getSourceRange(), |
Matt Beaumont-Gay | 32d825a | 2012-05-17 00:03:16 +0000 | [diff] [blame] | 3502 | Ex->getLocStart(), |
| 3503 | /*IsStringLocation*/false, |
Jean-Daniel Dupas | cb197b0 | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 3504 | getSpecifierRange(startSpecifier, specifierLen)); |
Hans Wennborg | b1a5e09 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3505 | } |
| 3506 | } |
| 3507 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3508 | return true; |
| 3509 | } |
| 3510 | |
| 3511 | void Sema::CheckFormatString(const StringLiteral *FExpr, |
Ted Kremenek | fb45d35 | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 3512 | const Expr *OrigFormatExpr, |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 3513 | ArrayRef<const Expr *> Args, |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3514 | bool HasVAListArg, unsigned format_idx, |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 3515 | unsigned firstDataArg, FormatStringType Type, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3516 | bool inFunctionCall, VariadicCallType CallType, |
| 3517 | llvm::SmallBitVector &CheckedVarArgs) { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3518 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3519 | // CHECK: is the format string a wide literal? |
Richard Smith | 4060f77 | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 3520 | if (!FExpr->isAscii() && !FExpr->isUTF8()) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3521 | CheckFormatHandler::EmitFormatDiagnostic( |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3522 | *this, inFunctionCall, Args[format_idx], |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3523 | PDiag(diag::warn_format_string_is_wide_literal), FExpr->getLocStart(), |
| 3524 | /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3525 | return; |
| 3526 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3527 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3528 | // Str - The format string. NOTE: this is NOT null-terminated! |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3529 | StringRef StrRef = FExpr->getString(); |
Benjamin Kramer | 35b077e | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 3530 | const char *Str = StrRef.data(); |
Benjamin Kramer | 6c6a4f4 | 2014-02-20 17:05:38 +0000 | [diff] [blame] | 3531 | // Account for cases where the string literal is truncated in a declaration. |
| 3532 | const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType()); |
| 3533 | assert(T && "String literal not of constant array type!"); |
| 3534 | size_t TypeSize = T->getSize().getZExtValue(); |
| 3535 | size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size()); |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 3536 | const unsigned numDataArgs = Args.size() - firstDataArg; |
Benjamin Kramer | 6c6a4f4 | 2014-02-20 17:05:38 +0000 | [diff] [blame] | 3537 | |
| 3538 | // Emit a warning if the string literal is truncated and does not contain an |
| 3539 | // embedded null character. |
| 3540 | if (TypeSize <= StrRef.size() && |
| 3541 | StrRef.substr(0, TypeSize).find('\0') == StringRef::npos) { |
| 3542 | CheckFormatHandler::EmitFormatDiagnostic( |
| 3543 | *this, inFunctionCall, Args[format_idx], |
| 3544 | PDiag(diag::warn_printf_format_string_not_null_terminated), |
| 3545 | FExpr->getLocStart(), |
| 3546 | /*IsStringLocation=*/true, OrigFormatExpr->getSourceRange()); |
| 3547 | return; |
| 3548 | } |
| 3549 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3550 | // CHECK: empty format string? |
Ted Kremenek | 6e302b2 | 2011-09-29 05:52:16 +0000 | [diff] [blame] | 3551 | if (StrLen == 0 && numDataArgs > 0) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3552 | CheckFormatHandler::EmitFormatDiagnostic( |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3553 | *this, inFunctionCall, Args[format_idx], |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3554 | PDiag(diag::warn_empty_format_string), FExpr->getLocStart(), |
| 3555 | /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3556 | return; |
| 3557 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3558 | |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 3559 | if (Type == FST_Printf || Type == FST_NSString) { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3560 | CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 3561 | numDataArgs, (Type == FST_NSString), |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 3562 | Str, HasVAListArg, Args, format_idx, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3563 | inFunctionCall, CallType, CheckedVarArgs); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3564 | |
Hans Wennborg | 23926bd | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 3565 | if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen, |
Jordan Rose | 510260c | 2012-09-13 02:11:03 +0000 | [diff] [blame] | 3566 | getLangOpts(), |
| 3567 | Context.getTargetInfo())) |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3568 | H.DoneProcessing(); |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 3569 | } else if (Type == FST_Scanf) { |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 3570 | CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, numDataArgs, |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 3571 | Str, HasVAListArg, Args, format_idx, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3572 | inFunctionCall, CallType, CheckedVarArgs); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3573 | |
Hans Wennborg | 23926bd | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 3574 | if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen, |
Jordan Rose | 510260c | 2012-09-13 02:11:03 +0000 | [diff] [blame] | 3575 | getLangOpts(), |
| 3576 | Context.getTargetInfo())) |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3577 | H.DoneProcessing(); |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 3578 | } // TODO: handle other formats |
Ted Kremenek | c70ee86 | 2010-01-28 01:18:22 +0000 | [diff] [blame] | 3579 | } |
| 3580 | |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 3581 | //===--- CHECK: Warn on use of wrong absolute value function. -------------===// |
| 3582 | |
| 3583 | // Returns the related absolute value function that is larger, of 0 if one |
| 3584 | // does not exist. |
| 3585 | static unsigned getLargerAbsoluteValueFunction(unsigned AbsFunction) { |
| 3586 | switch (AbsFunction) { |
| 3587 | default: |
| 3588 | return 0; |
| 3589 | |
| 3590 | case Builtin::BI__builtin_abs: |
| 3591 | return Builtin::BI__builtin_labs; |
| 3592 | case Builtin::BI__builtin_labs: |
| 3593 | return Builtin::BI__builtin_llabs; |
| 3594 | case Builtin::BI__builtin_llabs: |
| 3595 | return 0; |
| 3596 | |
| 3597 | case Builtin::BI__builtin_fabsf: |
| 3598 | return Builtin::BI__builtin_fabs; |
| 3599 | case Builtin::BI__builtin_fabs: |
| 3600 | return Builtin::BI__builtin_fabsl; |
| 3601 | case Builtin::BI__builtin_fabsl: |
| 3602 | return 0; |
| 3603 | |
| 3604 | case Builtin::BI__builtin_cabsf: |
| 3605 | return Builtin::BI__builtin_cabs; |
| 3606 | case Builtin::BI__builtin_cabs: |
| 3607 | return Builtin::BI__builtin_cabsl; |
| 3608 | case Builtin::BI__builtin_cabsl: |
| 3609 | return 0; |
| 3610 | |
| 3611 | case Builtin::BIabs: |
| 3612 | return Builtin::BIlabs; |
| 3613 | case Builtin::BIlabs: |
| 3614 | return Builtin::BIllabs; |
| 3615 | case Builtin::BIllabs: |
| 3616 | return 0; |
| 3617 | |
| 3618 | case Builtin::BIfabsf: |
| 3619 | return Builtin::BIfabs; |
| 3620 | case Builtin::BIfabs: |
| 3621 | return Builtin::BIfabsl; |
| 3622 | case Builtin::BIfabsl: |
| 3623 | return 0; |
| 3624 | |
| 3625 | case Builtin::BIcabsf: |
| 3626 | return Builtin::BIcabs; |
| 3627 | case Builtin::BIcabs: |
| 3628 | return Builtin::BIcabsl; |
| 3629 | case Builtin::BIcabsl: |
| 3630 | return 0; |
| 3631 | } |
| 3632 | } |
| 3633 | |
| 3634 | // Returns the argument type of the absolute value function. |
| 3635 | static QualType getAbsoluteValueArgumentType(ASTContext &Context, |
| 3636 | unsigned AbsType) { |
| 3637 | if (AbsType == 0) |
| 3638 | return QualType(); |
| 3639 | |
| 3640 | ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None; |
| 3641 | QualType BuiltinType = Context.GetBuiltinType(AbsType, Error); |
| 3642 | if (Error != ASTContext::GE_None) |
| 3643 | return QualType(); |
| 3644 | |
| 3645 | const FunctionProtoType *FT = BuiltinType->getAs<FunctionProtoType>(); |
| 3646 | if (!FT) |
| 3647 | return QualType(); |
| 3648 | |
| 3649 | if (FT->getNumParams() != 1) |
| 3650 | return QualType(); |
| 3651 | |
| 3652 | return FT->getParamType(0); |
| 3653 | } |
| 3654 | |
| 3655 | // Returns the best absolute value function, or zero, based on type and |
| 3656 | // current absolute value function. |
| 3657 | static unsigned getBestAbsFunction(ASTContext &Context, QualType ArgType, |
| 3658 | unsigned AbsFunctionKind) { |
| 3659 | unsigned BestKind = 0; |
| 3660 | uint64_t ArgSize = Context.getTypeSize(ArgType); |
| 3661 | for (unsigned Kind = AbsFunctionKind; Kind != 0; |
| 3662 | Kind = getLargerAbsoluteValueFunction(Kind)) { |
| 3663 | QualType ParamType = getAbsoluteValueArgumentType(Context, Kind); |
| 3664 | if (Context.getTypeSize(ParamType) >= ArgSize) { |
| 3665 | if (BestKind == 0) |
| 3666 | BestKind = Kind; |
| 3667 | else if (Context.hasSameType(ParamType, ArgType)) { |
| 3668 | BestKind = Kind; |
| 3669 | break; |
| 3670 | } |
| 3671 | } |
| 3672 | } |
| 3673 | return BestKind; |
| 3674 | } |
| 3675 | |
| 3676 | enum AbsoluteValueKind { |
| 3677 | AVK_Integer, |
| 3678 | AVK_Floating, |
| 3679 | AVK_Complex |
| 3680 | }; |
| 3681 | |
| 3682 | static AbsoluteValueKind getAbsoluteValueKind(QualType T) { |
| 3683 | if (T->isIntegralOrEnumerationType()) |
| 3684 | return AVK_Integer; |
| 3685 | if (T->isRealFloatingType()) |
| 3686 | return AVK_Floating; |
| 3687 | if (T->isAnyComplexType()) |
| 3688 | return AVK_Complex; |
| 3689 | |
| 3690 | llvm_unreachable("Type not integer, floating, or complex"); |
| 3691 | } |
| 3692 | |
| 3693 | // Changes the absolute value function to a different type. Preserves whether |
| 3694 | // the function is a builtin. |
| 3695 | static unsigned changeAbsFunction(unsigned AbsKind, |
| 3696 | AbsoluteValueKind ValueKind) { |
| 3697 | switch (ValueKind) { |
| 3698 | case AVK_Integer: |
| 3699 | switch (AbsKind) { |
| 3700 | default: |
| 3701 | return 0; |
| 3702 | case Builtin::BI__builtin_fabsf: |
| 3703 | case Builtin::BI__builtin_fabs: |
| 3704 | case Builtin::BI__builtin_fabsl: |
| 3705 | case Builtin::BI__builtin_cabsf: |
| 3706 | case Builtin::BI__builtin_cabs: |
| 3707 | case Builtin::BI__builtin_cabsl: |
| 3708 | return Builtin::BI__builtin_abs; |
| 3709 | case Builtin::BIfabsf: |
| 3710 | case Builtin::BIfabs: |
| 3711 | case Builtin::BIfabsl: |
| 3712 | case Builtin::BIcabsf: |
| 3713 | case Builtin::BIcabs: |
| 3714 | case Builtin::BIcabsl: |
| 3715 | return Builtin::BIabs; |
| 3716 | } |
| 3717 | case AVK_Floating: |
| 3718 | switch (AbsKind) { |
| 3719 | default: |
| 3720 | return 0; |
| 3721 | case Builtin::BI__builtin_abs: |
| 3722 | case Builtin::BI__builtin_labs: |
| 3723 | case Builtin::BI__builtin_llabs: |
| 3724 | case Builtin::BI__builtin_cabsf: |
| 3725 | case Builtin::BI__builtin_cabs: |
| 3726 | case Builtin::BI__builtin_cabsl: |
| 3727 | return Builtin::BI__builtin_fabsf; |
| 3728 | case Builtin::BIabs: |
| 3729 | case Builtin::BIlabs: |
| 3730 | case Builtin::BIllabs: |
| 3731 | case Builtin::BIcabsf: |
| 3732 | case Builtin::BIcabs: |
| 3733 | case Builtin::BIcabsl: |
| 3734 | return Builtin::BIfabsf; |
| 3735 | } |
| 3736 | case AVK_Complex: |
| 3737 | switch (AbsKind) { |
| 3738 | default: |
| 3739 | return 0; |
| 3740 | case Builtin::BI__builtin_abs: |
| 3741 | case Builtin::BI__builtin_labs: |
| 3742 | case Builtin::BI__builtin_llabs: |
| 3743 | case Builtin::BI__builtin_fabsf: |
| 3744 | case Builtin::BI__builtin_fabs: |
| 3745 | case Builtin::BI__builtin_fabsl: |
| 3746 | return Builtin::BI__builtin_cabsf; |
| 3747 | case Builtin::BIabs: |
| 3748 | case Builtin::BIlabs: |
| 3749 | case Builtin::BIllabs: |
| 3750 | case Builtin::BIfabsf: |
| 3751 | case Builtin::BIfabs: |
| 3752 | case Builtin::BIfabsl: |
| 3753 | return Builtin::BIcabsf; |
| 3754 | } |
| 3755 | } |
| 3756 | llvm_unreachable("Unable to convert function"); |
| 3757 | } |
| 3758 | |
Benjamin Kramer | 3d6220d | 2014-03-01 17:21:22 +0000 | [diff] [blame] | 3759 | static unsigned getAbsoluteValueFunctionKind(const FunctionDecl *FDecl) { |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 3760 | const IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
| 3761 | if (!FnInfo) |
| 3762 | return 0; |
| 3763 | |
| 3764 | switch (FDecl->getBuiltinID()) { |
| 3765 | default: |
| 3766 | return 0; |
| 3767 | case Builtin::BI__builtin_abs: |
| 3768 | case Builtin::BI__builtin_fabs: |
| 3769 | case Builtin::BI__builtin_fabsf: |
| 3770 | case Builtin::BI__builtin_fabsl: |
| 3771 | case Builtin::BI__builtin_labs: |
| 3772 | case Builtin::BI__builtin_llabs: |
| 3773 | case Builtin::BI__builtin_cabs: |
| 3774 | case Builtin::BI__builtin_cabsf: |
| 3775 | case Builtin::BI__builtin_cabsl: |
| 3776 | case Builtin::BIabs: |
| 3777 | case Builtin::BIlabs: |
| 3778 | case Builtin::BIllabs: |
| 3779 | case Builtin::BIfabs: |
| 3780 | case Builtin::BIfabsf: |
| 3781 | case Builtin::BIfabsl: |
| 3782 | case Builtin::BIcabs: |
| 3783 | case Builtin::BIcabsf: |
| 3784 | case Builtin::BIcabsl: |
| 3785 | return FDecl->getBuiltinID(); |
| 3786 | } |
| 3787 | llvm_unreachable("Unknown Builtin type"); |
| 3788 | } |
| 3789 | |
| 3790 | // If the replacement is valid, emit a note with replacement function. |
| 3791 | // Additionally, suggest including the proper header if not already included. |
| 3792 | static void emitReplacement(Sema &S, SourceLocation Loc, SourceRange Range, |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 3793 | unsigned AbsKind, QualType ArgType) { |
| 3794 | bool EmitHeaderHint = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3795 | const char *HeaderName = nullptr; |
| 3796 | const char *FunctionName = nullptr; |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 3797 | if (S.getLangOpts().CPlusPlus && !ArgType->isAnyComplexType()) { |
| 3798 | FunctionName = "std::abs"; |
| 3799 | if (ArgType->isIntegralOrEnumerationType()) { |
| 3800 | HeaderName = "cstdlib"; |
| 3801 | } else if (ArgType->isRealFloatingType()) { |
| 3802 | HeaderName = "cmath"; |
| 3803 | } else { |
| 3804 | llvm_unreachable("Invalid Type"); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 3805 | } |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 3806 | |
| 3807 | // Lookup all std::abs |
| 3808 | if (NamespaceDecl *Std = S.getStdNamespace()) { |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 3809 | LookupResult R(S, &S.Context.Idents.get("abs"), Loc, Sema::LookupAnyName); |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 3810 | R.suppressDiagnostics(); |
| 3811 | S.LookupQualifiedName(R, Std); |
| 3812 | |
| 3813 | for (const auto *I : R) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3814 | const FunctionDecl *FDecl = nullptr; |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 3815 | if (const UsingShadowDecl *UsingD = dyn_cast<UsingShadowDecl>(I)) { |
| 3816 | FDecl = dyn_cast<FunctionDecl>(UsingD->getTargetDecl()); |
| 3817 | } else { |
| 3818 | FDecl = dyn_cast<FunctionDecl>(I); |
| 3819 | } |
| 3820 | if (!FDecl) |
| 3821 | continue; |
| 3822 | |
| 3823 | // Found std::abs(), check that they are the right ones. |
| 3824 | if (FDecl->getNumParams() != 1) |
| 3825 | continue; |
| 3826 | |
| 3827 | // Check that the parameter type can handle the argument. |
| 3828 | QualType ParamType = FDecl->getParamDecl(0)->getType(); |
| 3829 | if (getAbsoluteValueKind(ArgType) == getAbsoluteValueKind(ParamType) && |
| 3830 | S.Context.getTypeSize(ArgType) <= |
| 3831 | S.Context.getTypeSize(ParamType)) { |
| 3832 | // Found a function, don't need the header hint. |
| 3833 | EmitHeaderHint = false; |
| 3834 | break; |
| 3835 | } |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 3836 | } |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 3837 | } |
| 3838 | } else { |
| 3839 | FunctionName = S.Context.BuiltinInfo.GetName(AbsKind); |
| 3840 | HeaderName = S.Context.BuiltinInfo.getHeaderName(AbsKind); |
| 3841 | |
| 3842 | if (HeaderName) { |
| 3843 | DeclarationName DN(&S.Context.Idents.get(FunctionName)); |
| 3844 | LookupResult R(S, DN, Loc, Sema::LookupAnyName); |
| 3845 | R.suppressDiagnostics(); |
| 3846 | S.LookupName(R, S.getCurScope()); |
| 3847 | |
| 3848 | if (R.isSingleResult()) { |
| 3849 | FunctionDecl *FD = dyn_cast<FunctionDecl>(R.getFoundDecl()); |
| 3850 | if (FD && FD->getBuiltinID() == AbsKind) { |
| 3851 | EmitHeaderHint = false; |
| 3852 | } else { |
| 3853 | return; |
| 3854 | } |
| 3855 | } else if (!R.empty()) { |
| 3856 | return; |
| 3857 | } |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 3858 | } |
| 3859 | } |
| 3860 | |
| 3861 | S.Diag(Loc, diag::note_replace_abs_function) |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 3862 | << FunctionName << FixItHint::CreateReplacement(Range, FunctionName); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 3863 | |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 3864 | if (!HeaderName) |
| 3865 | return; |
| 3866 | |
| 3867 | if (!EmitHeaderHint) |
| 3868 | return; |
| 3869 | |
Alp Toker | 5d96e0a | 2014-07-11 20:53:51 +0000 | [diff] [blame] | 3870 | S.Diag(Loc, diag::note_include_header_or_declare) << HeaderName |
| 3871 | << FunctionName; |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 3872 | } |
| 3873 | |
| 3874 | static bool IsFunctionStdAbs(const FunctionDecl *FDecl) { |
| 3875 | if (!FDecl) |
| 3876 | return false; |
| 3877 | |
| 3878 | if (!FDecl->getIdentifier() || !FDecl->getIdentifier()->isStr("abs")) |
| 3879 | return false; |
| 3880 | |
| 3881 | const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(FDecl->getDeclContext()); |
| 3882 | |
| 3883 | while (ND && ND->isInlineNamespace()) { |
| 3884 | ND = dyn_cast<NamespaceDecl>(ND->getDeclContext()); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 3885 | } |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 3886 | |
| 3887 | if (!ND || !ND->getIdentifier() || !ND->getIdentifier()->isStr("std")) |
| 3888 | return false; |
| 3889 | |
| 3890 | if (!isa<TranslationUnitDecl>(ND->getDeclContext())) |
| 3891 | return false; |
| 3892 | |
| 3893 | return true; |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
| 3896 | // Warn when using the wrong abs() function. |
| 3897 | void Sema::CheckAbsoluteValueFunction(const CallExpr *Call, |
| 3898 | const FunctionDecl *FDecl, |
| 3899 | IdentifierInfo *FnInfo) { |
| 3900 | if (Call->getNumArgs() != 1) |
| 3901 | return; |
| 3902 | |
| 3903 | unsigned AbsKind = getAbsoluteValueFunctionKind(FDecl); |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 3904 | bool IsStdAbs = IsFunctionStdAbs(FDecl); |
| 3905 | if (AbsKind == 0 && !IsStdAbs) |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 3906 | return; |
| 3907 | |
| 3908 | QualType ArgType = Call->getArg(0)->IgnoreParenImpCasts()->getType(); |
| 3909 | QualType ParamType = Call->getArg(0)->getType(); |
| 3910 | |
Alp Toker | 5d96e0a | 2014-07-11 20:53:51 +0000 | [diff] [blame] | 3911 | // Unsigned types cannot be negative. Suggest removing the absolute value |
| 3912 | // function call. |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 3913 | if (ArgType->isUnsignedIntegerType()) { |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 3914 | const char *FunctionName = |
| 3915 | IsStdAbs ? "std::abs" : Context.BuiltinInfo.GetName(AbsKind); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 3916 | Diag(Call->getExprLoc(), diag::warn_unsigned_abs) << ArgType << ParamType; |
| 3917 | Diag(Call->getExprLoc(), diag::note_remove_abs) |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 3918 | << FunctionName |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 3919 | << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange()); |
| 3920 | return; |
| 3921 | } |
| 3922 | |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 3923 | // std::abs has overloads which prevent most of the absolute value problems |
| 3924 | // from occurring. |
| 3925 | if (IsStdAbs) |
| 3926 | return; |
| 3927 | |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 3928 | AbsoluteValueKind ArgValueKind = getAbsoluteValueKind(ArgType); |
| 3929 | AbsoluteValueKind ParamValueKind = getAbsoluteValueKind(ParamType); |
| 3930 | |
| 3931 | // The argument and parameter are the same kind. Check if they are the right |
| 3932 | // size. |
| 3933 | if (ArgValueKind == ParamValueKind) { |
| 3934 | if (Context.getTypeSize(ArgType) <= Context.getTypeSize(ParamType)) |
| 3935 | return; |
| 3936 | |
| 3937 | unsigned NewAbsKind = getBestAbsFunction(Context, ArgType, AbsKind); |
| 3938 | Diag(Call->getExprLoc(), diag::warn_abs_too_small) |
| 3939 | << FDecl << ArgType << ParamType; |
| 3940 | |
| 3941 | if (NewAbsKind == 0) |
| 3942 | return; |
| 3943 | |
| 3944 | emitReplacement(*this, Call->getExprLoc(), |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 3945 | Call->getCallee()->getSourceRange(), NewAbsKind, ArgType); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 3946 | return; |
| 3947 | } |
| 3948 | |
| 3949 | // ArgValueKind != ParamValueKind |
| 3950 | // The wrong type of absolute value function was used. Attempt to find the |
| 3951 | // proper one. |
| 3952 | unsigned NewAbsKind = changeAbsFunction(AbsKind, ArgValueKind); |
| 3953 | NewAbsKind = getBestAbsFunction(Context, ArgType, NewAbsKind); |
| 3954 | if (NewAbsKind == 0) |
| 3955 | return; |
| 3956 | |
| 3957 | Diag(Call->getExprLoc(), diag::warn_wrong_absolute_value_type) |
| 3958 | << FDecl << ParamValueKind << ArgValueKind; |
| 3959 | |
| 3960 | emitReplacement(*this, Call->getExprLoc(), |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 3961 | Call->getCallee()->getSourceRange(), NewAbsKind, ArgType); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 3962 | return; |
| 3963 | } |
| 3964 | |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 3965 | //===--- CHECK: Standard memory functions ---------------------------------===// |
| 3966 | |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 3967 | /// \brief Takes the expression passed to the size_t parameter of functions |
| 3968 | /// such as memcmp, strncat, etc and warns if it's a comparison. |
| 3969 | /// |
| 3970 | /// This is to catch typos like `if (memcmp(&a, &b, sizeof(a) > 0))`. |
| 3971 | static bool CheckMemorySizeofForComparison(Sema &S, const Expr *E, |
| 3972 | IdentifierInfo *FnName, |
| 3973 | SourceLocation FnLoc, |
| 3974 | SourceLocation RParenLoc) { |
| 3975 | const BinaryOperator *Size = dyn_cast<BinaryOperator>(E); |
| 3976 | if (!Size) |
| 3977 | return false; |
| 3978 | |
| 3979 | // if E is binop and op is >, <, >=, <=, ==, &&, ||: |
| 3980 | if (!Size->isComparisonOp() && !Size->isEqualityOp() && !Size->isLogicalOp()) |
| 3981 | return false; |
| 3982 | |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 3983 | SourceRange SizeRange = Size->getSourceRange(); |
| 3984 | S.Diag(Size->getOperatorLoc(), diag::warn_memsize_comparison) |
| 3985 | << SizeRange << FnName; |
Alp Toker | b086903 | 2014-05-17 01:13:18 +0000 | [diff] [blame] | 3986 | S.Diag(FnLoc, diag::note_memsize_comparison_paren) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 3987 | << FnName << FixItHint::CreateInsertion( |
| 3988 | S.getLocForEndOfToken(Size->getLHS()->getLocEnd()), ")") |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 3989 | << FixItHint::CreateRemoval(RParenLoc); |
Alp Toker | b086903 | 2014-05-17 01:13:18 +0000 | [diff] [blame] | 3990 | S.Diag(SizeRange.getBegin(), diag::note_memsize_comparison_cast_silence) |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 3991 | << FixItHint::CreateInsertion(SizeRange.getBegin(), "(size_t)(") |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 3992 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(SizeRange.getEnd()), |
| 3993 | ")"); |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 3994 | |
| 3995 | return true; |
| 3996 | } |
| 3997 | |
Reid Kleckner | 5fb5b12 | 2014-06-27 23:58:21 +0000 | [diff] [blame] | 3998 | /// \brief Determine whether the given type is or contains a dynamic class type |
| 3999 | /// (e.g., whether it has a vtable). |
| 4000 | static const CXXRecordDecl *getContainedDynamicClass(QualType T, |
| 4001 | bool &IsContained) { |
| 4002 | // Look through array types while ignoring qualifiers. |
| 4003 | const Type *Ty = T->getBaseElementTypeUnsafe(); |
| 4004 | IsContained = false; |
| 4005 | |
| 4006 | const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
| 4007 | RD = RD ? RD->getDefinition() : nullptr; |
| 4008 | if (!RD) |
| 4009 | return nullptr; |
| 4010 | |
| 4011 | if (RD->isDynamicClass()) |
| 4012 | return RD; |
| 4013 | |
| 4014 | // Check all the fields. If any bases were dynamic, the class is dynamic. |
| 4015 | // It's impossible for a class to transitively contain itself by value, so |
| 4016 | // infinite recursion is impossible. |
| 4017 | for (auto *FD : RD->fields()) { |
| 4018 | bool SubContained; |
| 4019 | if (const CXXRecordDecl *ContainedRD = |
| 4020 | getContainedDynamicClass(FD->getType(), SubContained)) { |
| 4021 | IsContained = true; |
| 4022 | return ContainedRD; |
| 4023 | } |
| 4024 | } |
| 4025 | |
| 4026 | return nullptr; |
Douglas Gregor | a74926b | 2011-05-03 20:05:22 +0000 | [diff] [blame] | 4027 | } |
| 4028 | |
Chandler Carruth | 889ed86 | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 4029 | /// \brief If E is a sizeof expression, returns its argument expression, |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4030 | /// otherwise returns NULL. |
| 4031 | static const Expr *getSizeOfExprArg(const Expr* E) { |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 4032 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4033 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
| 4034 | if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType()) |
| 4035 | return SizeOf->getArgumentExpr()->IgnoreParenImpCasts(); |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 4036 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4037 | return nullptr; |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4038 | } |
| 4039 | |
Chandler Carruth | 889ed86 | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 4040 | /// \brief If E is a sizeof expression, returns its argument type. |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4041 | static QualType getSizeOfArgType(const Expr* E) { |
| 4042 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
| 4043 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
| 4044 | if (SizeOf->getKind() == clang::UETT_SizeOf) |
| 4045 | return SizeOf->getTypeOfArgument(); |
| 4046 | |
| 4047 | return QualType(); |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 4050 | /// \brief Check for dangerous or invalid arguments to memset(). |
| 4051 | /// |
Chandler Carruth | ac68726 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 4052 | /// This issues warnings on known problematic, dangerous or unspecified |
Matt Beaumont-Gay | 3c48990 | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 4053 | /// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp' |
| 4054 | /// function calls. |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 4055 | /// |
| 4056 | /// \param Call The call expression to diagnose. |
Matt Beaumont-Gay | 3c48990 | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 4057 | void Sema::CheckMemaccessArguments(const CallExpr *Call, |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 4058 | unsigned BId, |
Matt Beaumont-Gay | 3c48990 | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 4059 | IdentifierInfo *FnName) { |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 4060 | assert(BId != 0); |
| 4061 | |
Ted Kremenek | b5fabb2 | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 4062 | // It is possible to have a non-standard definition of memset. Validate |
Douglas Gregor | 18739c3 | 2011-06-16 17:56:04 +0000 | [diff] [blame] | 4063 | // we have enough arguments, and if not, abort further checking. |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 4064 | unsigned ExpectedNumArgs = (BId == Builtin::BIstrndup ? 2 : 3); |
Nico Weber | 39bfed8 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 4065 | if (Call->getNumArgs() < ExpectedNumArgs) |
Ted Kremenek | b5fabb2 | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 4066 | return; |
| 4067 | |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 4068 | unsigned LastArg = (BId == Builtin::BImemset || |
| 4069 | BId == Builtin::BIstrndup ? 1 : 2); |
| 4070 | unsigned LenArg = (BId == Builtin::BIstrndup ? 1 : 2); |
Nico Weber | 39bfed8 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 4071 | const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts(); |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4072 | |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 4073 | if (CheckMemorySizeofForComparison(*this, LenExpr, FnName, |
| 4074 | Call->getLocStart(), Call->getRParenLoc())) |
| 4075 | return; |
| 4076 | |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4077 | // We have special checking when the length is a sizeof expression. |
| 4078 | QualType SizeOfArgTy = getSizeOfArgType(LenExpr); |
| 4079 | const Expr *SizeOfArg = getSizeOfExprArg(LenExpr); |
| 4080 | llvm::FoldingSetNodeID SizeOfArgID; |
| 4081 | |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 4082 | for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) { |
| 4083 | const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts(); |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 4084 | SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange(); |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 4085 | |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 4086 | QualType DestTy = Dest->getType(); |
| 4087 | if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) { |
| 4088 | QualType PointeeTy = DestPtrTy->getPointeeType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4089 | |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4090 | // Never warn about void type pointers. This can be used to suppress |
| 4091 | // false positives. |
| 4092 | if (PointeeTy->isVoidType()) |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 4093 | continue; |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 4094 | |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4095 | // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by |
| 4096 | // actually comparing the expressions for equality. Because computing the |
| 4097 | // expression IDs can be expensive, we only do this if the diagnostic is |
| 4098 | // enabled. |
| 4099 | if (SizeOfArg && |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 4100 | !Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, |
| 4101 | SizeOfArg->getExprLoc())) { |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4102 | // We only compute IDs for expressions if the warning is enabled, and |
| 4103 | // cache the sizeof arg's ID. |
| 4104 | if (SizeOfArgID == llvm::FoldingSetNodeID()) |
| 4105 | SizeOfArg->Profile(SizeOfArgID, Context, true); |
| 4106 | llvm::FoldingSetNodeID DestID; |
| 4107 | Dest->Profile(DestID, Context, true); |
| 4108 | if (DestID == SizeOfArgID) { |
Nico Weber | 39bfed8 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 4109 | // TODO: For strncpy() and friends, this could suggest sizeof(dst) |
| 4110 | // over sizeof(src) as well. |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4111 | unsigned ActionIdx = 0; // Default is to suggest dereferencing. |
Anna Zaks | 869aecc | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 4112 | StringRef ReadableName = FnName->getName(); |
| 4113 | |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4114 | if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest)) |
Anna Zaks | d08d915 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 4115 | if (UnaryOp->getOpcode() == UO_AddrOf) |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4116 | ActionIdx = 1; // If its an address-of operator, just remove it. |
Fariborz Jahanian | 4d365ba | 2013-01-30 01:12:44 +0000 | [diff] [blame] | 4117 | if (!PointeeTy->isIncompleteType() && |
| 4118 | (Context.getTypeSize(PointeeTy) == Context.getCharWidth())) |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4119 | ActionIdx = 2; // If the pointee's size is sizeof(char), |
| 4120 | // suggest an explicit length. |
Anna Zaks | 869aecc | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 4121 | |
| 4122 | // If the function is defined as a builtin macro, do not show macro |
| 4123 | // expansion. |
| 4124 | SourceLocation SL = SizeOfArg->getExprLoc(); |
| 4125 | SourceRange DSR = Dest->getSourceRange(); |
| 4126 | SourceRange SSR = SizeOfArg->getSourceRange(); |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4127 | SourceManager &SM = getSourceManager(); |
Anna Zaks | 869aecc | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 4128 | |
| 4129 | if (SM.isMacroArgExpansion(SL)) { |
| 4130 | ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts); |
| 4131 | SL = SM.getSpellingLoc(SL); |
| 4132 | DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()), |
| 4133 | SM.getSpellingLoc(DSR.getEnd())); |
| 4134 | SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()), |
| 4135 | SM.getSpellingLoc(SSR.getEnd())); |
| 4136 | } |
| 4137 | |
Anna Zaks | d08d915 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 4138 | DiagRuntimeBehavior(SL, SizeOfArg, |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4139 | PDiag(diag::warn_sizeof_pointer_expr_memaccess) |
Anna Zaks | 869aecc | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 4140 | << ReadableName |
Anna Zaks | d08d915 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 4141 | << PointeeTy |
| 4142 | << DestTy |
Anna Zaks | 869aecc | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 4143 | << DSR |
Anna Zaks | d08d915 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 4144 | << SSR); |
| 4145 | DiagRuntimeBehavior(SL, SizeOfArg, |
| 4146 | PDiag(diag::warn_sizeof_pointer_expr_memaccess_note) |
| 4147 | << ActionIdx |
| 4148 | << SSR); |
| 4149 | |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4150 | break; |
| 4151 | } |
| 4152 | } |
| 4153 | |
| 4154 | // Also check for cases where the sizeof argument is the exact same |
| 4155 | // type as the memory argument, and where it points to a user-defined |
| 4156 | // record type. |
| 4157 | if (SizeOfArgTy != QualType()) { |
| 4158 | if (PointeeTy->isRecordType() && |
| 4159 | Context.typesAreCompatible(SizeOfArgTy, DestTy)) { |
| 4160 | DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest, |
| 4161 | PDiag(diag::warn_sizeof_pointer_type_memaccess) |
| 4162 | << FnName << SizeOfArgTy << ArgIdx |
| 4163 | << PointeeTy << Dest->getSourceRange() |
| 4164 | << LenExpr->getSourceRange()); |
| 4165 | break; |
| 4166 | } |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 4169 | // Always complain about dynamic classes. |
Reid Kleckner | 5fb5b12 | 2014-06-27 23:58:21 +0000 | [diff] [blame] | 4170 | bool IsContained; |
| 4171 | if (const CXXRecordDecl *ContainedRD = |
| 4172 | getContainedDynamicClass(PointeeTy, IsContained)) { |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 4173 | |
| 4174 | unsigned OperationType = 0; |
| 4175 | // "overwritten" if we're warning about the destination for any call |
| 4176 | // but memcmp; otherwise a verb appropriate to the call. |
| 4177 | if (ArgIdx != 0 || BId == Builtin::BImemcmp) { |
| 4178 | if (BId == Builtin::BImemcpy) |
| 4179 | OperationType = 1; |
| 4180 | else if(BId == Builtin::BImemmove) |
| 4181 | OperationType = 2; |
| 4182 | else if (BId == Builtin::BImemcmp) |
| 4183 | OperationType = 3; |
| 4184 | } |
| 4185 | |
Matt Beaumont-Gay | 335e653 | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 4186 | DiagRuntimeBehavior( |
| 4187 | Dest->getExprLoc(), Dest, |
| 4188 | PDiag(diag::warn_dyn_class_memaccess) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 4189 | << (BId == Builtin::BImemcmp ? ArgIdx + 2 : ArgIdx) |
Reid Kleckner | 5fb5b12 | 2014-06-27 23:58:21 +0000 | [diff] [blame] | 4190 | << FnName << IsContained << ContainedRD << OperationType |
Matt Beaumont-Gay | 335e653 | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 4191 | << Call->getCallee()->getSourceRange()); |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 4192 | } else if (PointeeTy.hasNonTrivialObjCLifetime() && |
| 4193 | BId != Builtin::BImemset) |
Matt Beaumont-Gay | 335e653 | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 4194 | DiagRuntimeBehavior( |
| 4195 | Dest->getExprLoc(), Dest, |
| 4196 | PDiag(diag::warn_arc_object_memaccess) |
| 4197 | << ArgIdx << FnName << PointeeTy |
| 4198 | << Call->getCallee()->getSourceRange()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4199 | else |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 4200 | continue; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4201 | |
| 4202 | DiagRuntimeBehavior( |
| 4203 | Dest->getExprLoc(), Dest, |
Chandler Carruth | ac68726 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 4204 | PDiag(diag::note_bad_memaccess_silence) |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 4205 | << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)")); |
| 4206 | break; |
| 4207 | } |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 4208 | } |
| 4209 | } |
| 4210 | |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 4211 | // A little helper routine: ignore addition and subtraction of integer literals. |
| 4212 | // This intentionally does not ignore all integer constant expressions because |
| 4213 | // we don't want to remove sizeof(). |
| 4214 | static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) { |
| 4215 | Ex = Ex->IgnoreParenCasts(); |
| 4216 | |
| 4217 | for (;;) { |
| 4218 | const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex); |
| 4219 | if (!BO || !BO->isAdditiveOp()) |
| 4220 | break; |
| 4221 | |
| 4222 | const Expr *RHS = BO->getRHS()->IgnoreParenCasts(); |
| 4223 | const Expr *LHS = BO->getLHS()->IgnoreParenCasts(); |
| 4224 | |
| 4225 | if (isa<IntegerLiteral>(RHS)) |
| 4226 | Ex = LHS; |
| 4227 | else if (isa<IntegerLiteral>(LHS)) |
| 4228 | Ex = RHS; |
| 4229 | else |
| 4230 | break; |
| 4231 | } |
| 4232 | |
| 4233 | return Ex; |
| 4234 | } |
| 4235 | |
Anna Zaks | 13b0857 | 2012-08-08 21:42:23 +0000 | [diff] [blame] | 4236 | static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty, |
| 4237 | ASTContext &Context) { |
| 4238 | // Only handle constant-sized or VLAs, but not flexible members. |
| 4239 | if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) { |
| 4240 | // Only issue the FIXIT for arrays of size > 1. |
| 4241 | if (CAT->getSize().getSExtValue() <= 1) |
| 4242 | return false; |
| 4243 | } else if (!Ty->isVariableArrayType()) { |
| 4244 | return false; |
| 4245 | } |
| 4246 | return true; |
| 4247 | } |
| 4248 | |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 4249 | // Warn if the user has made the 'size' argument to strlcpy or strlcat |
| 4250 | // be the size of the source, instead of the destination. |
| 4251 | void Sema::CheckStrlcpycatArguments(const CallExpr *Call, |
| 4252 | IdentifierInfo *FnName) { |
| 4253 | |
| 4254 | // Don't crash if the user has the wrong number of arguments |
| 4255 | if (Call->getNumArgs() != 3) |
| 4256 | return; |
| 4257 | |
| 4258 | const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context); |
| 4259 | const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4260 | const Expr *CompareWithSrc = nullptr; |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 4261 | |
| 4262 | if (CheckMemorySizeofForComparison(*this, SizeArg, FnName, |
| 4263 | Call->getLocStart(), Call->getRParenLoc())) |
| 4264 | return; |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 4265 | |
| 4266 | // Look for 'strlcpy(dst, x, sizeof(x))' |
| 4267 | if (const Expr *Ex = getSizeOfExprArg(SizeArg)) |
| 4268 | CompareWithSrc = Ex; |
| 4269 | else { |
| 4270 | // Look for 'strlcpy(dst, x, strlen(x))' |
| 4271 | if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) { |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 4272 | if (SizeCall->getBuiltinCallee() == Builtin::BIstrlen && |
| 4273 | SizeCall->getNumArgs() == 1) |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 4274 | CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context); |
| 4275 | } |
| 4276 | } |
| 4277 | |
| 4278 | if (!CompareWithSrc) |
| 4279 | return; |
| 4280 | |
| 4281 | // Determine if the argument to sizeof/strlen is equal to the source |
| 4282 | // argument. In principle there's all kinds of things you could do |
| 4283 | // here, for instance creating an == expression and evaluating it with |
| 4284 | // EvaluateAsBooleanCondition, but this uses a more direct technique: |
| 4285 | const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg); |
| 4286 | if (!SrcArgDRE) |
| 4287 | return; |
| 4288 | |
| 4289 | const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc); |
| 4290 | if (!CompareWithSrcDRE || |
| 4291 | SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl()) |
| 4292 | return; |
| 4293 | |
| 4294 | const Expr *OriginalSizeArg = Call->getArg(2); |
| 4295 | Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size) |
| 4296 | << OriginalSizeArg->getSourceRange() << FnName; |
| 4297 | |
| 4298 | // Output a FIXIT hint if the destination is an array (rather than a |
| 4299 | // pointer to an array). This could be enhanced to handle some |
| 4300 | // pointers if we know the actual size, like if DstArg is 'array+2' |
| 4301 | // we could say 'sizeof(array)-2'. |
| 4302 | const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts(); |
Anna Zaks | 13b0857 | 2012-08-08 21:42:23 +0000 | [diff] [blame] | 4303 | if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context)) |
Ted Kremenek | 18db5d4 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 4304 | return; |
Ted Kremenek | 18db5d4 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 4305 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 4306 | SmallString<128> sizeString; |
Ted Kremenek | 18db5d4 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 4307 | llvm::raw_svector_ostream OS(sizeString); |
| 4308 | OS << "sizeof("; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4309 | DstArg->printPretty(OS, nullptr, getPrintingPolicy()); |
Ted Kremenek | 18db5d4 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 4310 | OS << ")"; |
| 4311 | |
| 4312 | Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size) |
| 4313 | << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(), |
| 4314 | OS.str()); |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 4315 | } |
| 4316 | |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4317 | /// Check if two expressions refer to the same declaration. |
| 4318 | static bool referToTheSameDecl(const Expr *E1, const Expr *E2) { |
| 4319 | if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1)) |
| 4320 | if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2)) |
| 4321 | return D1->getDecl() == D2->getDecl(); |
| 4322 | return false; |
| 4323 | } |
| 4324 | |
| 4325 | static const Expr *getStrlenExprArg(const Expr *E) { |
| 4326 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { |
| 4327 | const FunctionDecl *FD = CE->getDirectCallee(); |
| 4328 | if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4329 | return nullptr; |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4330 | return CE->getArg(0)->IgnoreParenCasts(); |
| 4331 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4332 | return nullptr; |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
| 4335 | // Warn on anti-patterns as the 'size' argument to strncat. |
| 4336 | // The correct size argument should look like following: |
| 4337 | // strncat(dst, src, sizeof(dst) - strlen(dest) - 1); |
| 4338 | void Sema::CheckStrncatArguments(const CallExpr *CE, |
| 4339 | IdentifierInfo *FnName) { |
| 4340 | // Don't crash if the user has the wrong number of arguments. |
| 4341 | if (CE->getNumArgs() < 3) |
| 4342 | return; |
| 4343 | const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts(); |
| 4344 | const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts(); |
| 4345 | const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts(); |
| 4346 | |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 4347 | if (CheckMemorySizeofForComparison(*this, LenArg, FnName, CE->getLocStart(), |
| 4348 | CE->getRParenLoc())) |
| 4349 | return; |
| 4350 | |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4351 | // Identify common expressions, which are wrongly used as the size argument |
| 4352 | // to strncat and may lead to buffer overflows. |
| 4353 | unsigned PatternType = 0; |
| 4354 | if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) { |
| 4355 | // - sizeof(dst) |
| 4356 | if (referToTheSameDecl(SizeOfArg, DstArg)) |
| 4357 | PatternType = 1; |
| 4358 | // - sizeof(src) |
| 4359 | else if (referToTheSameDecl(SizeOfArg, SrcArg)) |
| 4360 | PatternType = 2; |
| 4361 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) { |
| 4362 | if (BE->getOpcode() == BO_Sub) { |
| 4363 | const Expr *L = BE->getLHS()->IgnoreParenCasts(); |
| 4364 | const Expr *R = BE->getRHS()->IgnoreParenCasts(); |
| 4365 | // - sizeof(dst) - strlen(dst) |
| 4366 | if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) && |
| 4367 | referToTheSameDecl(DstArg, getStrlenExprArg(R))) |
| 4368 | PatternType = 1; |
| 4369 | // - sizeof(src) - (anything) |
| 4370 | else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L))) |
| 4371 | PatternType = 2; |
| 4372 | } |
| 4373 | } |
| 4374 | |
| 4375 | if (PatternType == 0) |
| 4376 | return; |
| 4377 | |
Anna Zaks | 5069aa3 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 4378 | // Generate the diagnostic. |
| 4379 | SourceLocation SL = LenArg->getLocStart(); |
| 4380 | SourceRange SR = LenArg->getSourceRange(); |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4381 | SourceManager &SM = getSourceManager(); |
Anna Zaks | 5069aa3 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 4382 | |
| 4383 | // If the function is defined as a builtin macro, do not show macro expansion. |
| 4384 | if (SM.isMacroArgExpansion(SL)) { |
| 4385 | SL = SM.getSpellingLoc(SL); |
| 4386 | SR = SourceRange(SM.getSpellingLoc(SR.getBegin()), |
| 4387 | SM.getSpellingLoc(SR.getEnd())); |
| 4388 | } |
| 4389 | |
Anna Zaks | 13b0857 | 2012-08-08 21:42:23 +0000 | [diff] [blame] | 4390 | // Check if the destination is an array (rather than a pointer to an array). |
| 4391 | QualType DstTy = DstArg->getType(); |
| 4392 | bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy, |
| 4393 | Context); |
| 4394 | if (!isKnownSizeArray) { |
| 4395 | if (PatternType == 1) |
| 4396 | Diag(SL, diag::warn_strncat_wrong_size) << SR; |
| 4397 | else |
| 4398 | Diag(SL, diag::warn_strncat_src_size) << SR; |
| 4399 | return; |
| 4400 | } |
| 4401 | |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4402 | if (PatternType == 1) |
Anna Zaks | 5069aa3 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 4403 | Diag(SL, diag::warn_strncat_large_size) << SR; |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4404 | else |
Anna Zaks | 5069aa3 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 4405 | Diag(SL, diag::warn_strncat_src_size) << SR; |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4406 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 4407 | SmallString<128> sizeString; |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4408 | llvm::raw_svector_ostream OS(sizeString); |
| 4409 | OS << "sizeof("; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4410 | DstArg->printPretty(OS, nullptr, getPrintingPolicy()); |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4411 | OS << ") - "; |
| 4412 | OS << "strlen("; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4413 | DstArg->printPretty(OS, nullptr, getPrintingPolicy()); |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4414 | OS << ") - 1"; |
| 4415 | |
Anna Zaks | 5069aa3 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 4416 | Diag(SL, diag::note_strncat_wrong_size) |
| 4417 | << FixItHint::CreateReplacement(SR, OS.str()); |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4420 | //===--- CHECK: Return Address of Stack Variable --------------------------===// |
| 4421 | |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4422 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 4423 | Decl *ParentDecl); |
| 4424 | static Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 4425 | Decl *ParentDecl); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4426 | |
| 4427 | /// CheckReturnStackAddr - Check if a return statement returns the address |
| 4428 | /// of a stack variable. |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 4429 | static void |
| 4430 | CheckReturnStackAddr(Sema &S, Expr *RetValExp, QualType lhsType, |
| 4431 | SourceLocation ReturnLoc) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4432 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4433 | Expr *stackE = nullptr; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4434 | SmallVector<DeclRefExpr *, 8> refVars; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4435 | |
| 4436 | // Perform checking for returned stack addresses, local blocks, |
| 4437 | // label addresses or references to temporaries. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4438 | if (lhsType->isPointerType() || |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 4439 | (!S.getLangOpts().ObjCAutoRefCount && lhsType->isBlockPointerType())) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4440 | stackE = EvalAddr(RetValExp, refVars, /*ParentDecl=*/nullptr); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 4441 | } else if (lhsType->isReferenceType()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4442 | stackE = EvalVal(RetValExp, refVars, /*ParentDecl=*/nullptr); |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4443 | } |
| 4444 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4445 | if (!stackE) |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4446 | return; // Nothing suspicious was found. |
| 4447 | |
| 4448 | SourceLocation diagLoc; |
| 4449 | SourceRange diagRange; |
| 4450 | if (refVars.empty()) { |
| 4451 | diagLoc = stackE->getLocStart(); |
| 4452 | diagRange = stackE->getSourceRange(); |
| 4453 | } else { |
| 4454 | // We followed through a reference variable. 'stackE' contains the |
| 4455 | // problematic expression but we will warn at the return statement pointing |
| 4456 | // at the reference variable. We will later display the "trail" of |
| 4457 | // reference variables using notes. |
| 4458 | diagLoc = refVars[0]->getLocStart(); |
| 4459 | diagRange = refVars[0]->getSourceRange(); |
| 4460 | } |
| 4461 | |
| 4462 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var. |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 4463 | S.Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4464 | : diag::warn_ret_stack_addr) |
| 4465 | << DR->getDecl()->getDeclName() << diagRange; |
| 4466 | } else if (isa<BlockExpr>(stackE)) { // local block. |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 4467 | S.Diag(diagLoc, diag::err_ret_local_block) << diagRange; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4468 | } else if (isa<AddrLabelExpr>(stackE)) { // address of label. |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 4469 | S.Diag(diagLoc, diag::warn_ret_addr_label) << diagRange; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4470 | } else { // local temporary. |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 4471 | S.Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref |
| 4472 | : diag::warn_ret_local_temp_addr) |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4473 | << diagRange; |
| 4474 | } |
| 4475 | |
| 4476 | // Display the "trail" of reference variables that we followed until we |
| 4477 | // found the problematic expression using notes. |
| 4478 | for (unsigned i = 0, e = refVars.size(); i != e; ++i) { |
| 4479 | VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl()); |
| 4480 | // If this var binds to another reference var, show the range of the next |
| 4481 | // var, otherwise the var binds to the problematic expression, in which case |
| 4482 | // show the range of the expression. |
| 4483 | SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange() |
| 4484 | : stackE->getSourceRange(); |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 4485 | S.Diag(VD->getLocation(), diag::note_ref_var_local_bind) |
| 4486 | << VD->getDeclName() << range; |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4487 | } |
| 4488 | } |
| 4489 | |
| 4490 | /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that |
| 4491 | /// check if the expression in a return statement evaluates to an address |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4492 | /// to a location on the stack, a local block, an address of a label, or a |
| 4493 | /// reference to local temporary. The recursion is used to traverse the |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4494 | /// AST of the return expression, with recursion backtracking when we |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4495 | /// encounter a subexpression that (1) clearly does not lead to one of the |
| 4496 | /// above problematic expressions (2) is something we cannot determine leads to |
| 4497 | /// a problematic expression based on such local checking. |
| 4498 | /// |
| 4499 | /// Both EvalAddr and EvalVal follow through reference variables to evaluate |
| 4500 | /// the expression that they point to. Such variables are added to the |
| 4501 | /// 'refVars' vector so that we know what the reference variable "trail" was. |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4502 | /// |
Ted Kremenek | e07a8cd | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 4503 | /// EvalAddr processes expressions that are pointers that are used as |
| 4504 | /// references (and not L-values). EvalVal handles all other values. |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4505 | /// At the base case of the recursion is a check for the above problematic |
| 4506 | /// expressions. |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4507 | /// |
| 4508 | /// This implementation handles: |
| 4509 | /// |
| 4510 | /// * pointer-to-pointer casts |
| 4511 | /// * implicit conversions from array references to pointers |
| 4512 | /// * taking the address of fields |
| 4513 | /// * arbitrary interplay between "&" and "*" operators |
| 4514 | /// * pointer arithmetic from an address of a stack variable |
| 4515 | /// * taking the address of an array element where the array is on the stack |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4516 | static Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 4517 | Decl *ParentDecl) { |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4518 | if (E->isTypeDependent()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4519 | return nullptr; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4520 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4521 | // We should only be called for evaluating pointer expressions. |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 4522 | assert((E->getType()->isAnyPointerType() || |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4523 | E->getType()->isBlockPointerType() || |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4524 | E->getType()->isObjCQualifiedIdType()) && |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 4525 | "EvalAddr only works on pointers"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4526 | |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 4527 | E = E->IgnoreParens(); |
| 4528 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4529 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 4530 | // viewed AST node. We then recursively traverse the AST by calling |
| 4531 | // EvalAddr and EvalVal appropriately. |
| 4532 | switch (E->getStmtClass()) { |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4533 | case Stmt::DeclRefExprClass: { |
| 4534 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
| 4535 | |
Richard Smith | 40f08eb | 2014-01-30 22:05:38 +0000 | [diff] [blame] | 4536 | // If we leave the immediate function, the lifetime isn't about to end. |
| 4537 | if (DR->refersToEnclosingLocal()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4538 | return nullptr; |
Richard Smith | 40f08eb | 2014-01-30 22:05:38 +0000 | [diff] [blame] | 4539 | |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4540 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
| 4541 | // If this is a reference variable, follow through to the expression that |
| 4542 | // it points to. |
| 4543 | if (V->hasLocalStorage() && |
| 4544 | V->getType()->isReferenceType() && V->hasInit()) { |
| 4545 | // Add the reference variable to the "trail". |
| 4546 | refVars.push_back(DR); |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4547 | return EvalAddr(V->getInit(), refVars, ParentDecl); |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4548 | } |
| 4549 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4550 | return nullptr; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4551 | } |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4552 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 4553 | case Stmt::UnaryOperatorClass: { |
| 4554 | // The only unary operator that make sense to handle here |
| 4555 | // is AddrOf. All others don't make sense as pointers. |
| 4556 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4557 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4558 | if (U->getOpcode() == UO_AddrOf) |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4559 | return EvalVal(U->getSubExpr(), refVars, ParentDecl); |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 4560 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4561 | return nullptr; |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4562 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4563 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 4564 | case Stmt::BinaryOperatorClass: { |
| 4565 | // Handle pointer arithmetic. All other binary operators are not valid |
| 4566 | // in this context. |
| 4567 | BinaryOperator *B = cast<BinaryOperator>(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4568 | BinaryOperatorKind op = B->getOpcode(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4569 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4570 | if (op != BO_Add && op != BO_Sub) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4571 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4572 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 4573 | Expr *Base = B->getLHS(); |
| 4574 | |
| 4575 | // Determine which argument is the real pointer base. It could be |
| 4576 | // the RHS argument instead of the LHS. |
| 4577 | if (!Base->getType()->isPointerType()) Base = B->getRHS(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4578 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 4579 | assert (Base->getType()->isPointerType()); |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4580 | return EvalAddr(Base, refVars, ParentDecl); |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 4581 | } |
Steve Naroff | 2752a17 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 4582 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 4583 | // For conditional operators we need to see if either the LHS or RHS are |
| 4584 | // valid DeclRefExpr*s. If one of them is valid, we return it. |
| 4585 | case Stmt::ConditionalOperatorClass: { |
| 4586 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4587 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 4588 | // Handle the GNU extension for missing LHS. |
Richard Smith | 6a6a4bb | 2014-01-27 04:19:56 +0000 | [diff] [blame] | 4589 | // FIXME: That isn't a ConditionalOperator, so doesn't get here. |
| 4590 | if (Expr *LHSExpr = C->getLHS()) { |
| 4591 | // In C++, we can have a throw-expression, which has 'void' type. |
| 4592 | if (!LHSExpr->getType()->isVoidType()) |
| 4593 | if (Expr *LHS = EvalAddr(LHSExpr, refVars, ParentDecl)) |
Douglas Gregor | 270b2ef | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 4594 | return LHS; |
| 4595 | } |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 4596 | |
Douglas Gregor | 270b2ef | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 4597 | // In C++, we can have a throw-expression, which has 'void' type. |
| 4598 | if (C->getRHS()->getType()->isVoidType()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4599 | return nullptr; |
Douglas Gregor | 270b2ef | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 4600 | |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4601 | return EvalAddr(C->getRHS(), refVars, ParentDecl); |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 4602 | } |
Richard Smith | 6a6a4bb | 2014-01-27 04:19:56 +0000 | [diff] [blame] | 4603 | |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4604 | case Stmt::BlockExprClass: |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4605 | if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures()) |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4606 | return E; // local block. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4607 | return nullptr; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4608 | |
| 4609 | case Stmt::AddrLabelExprClass: |
| 4610 | return E; // address of label. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4611 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 4612 | case Stmt::ExprWithCleanupsClass: |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4613 | return EvalAddr(cast<ExprWithCleanups>(E)->getSubExpr(), refVars, |
| 4614 | ParentDecl); |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 4615 | |
Ted Kremenek | c3b4c52 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 4616 | // For casts, we need to handle conversions from arrays to |
| 4617 | // pointer values, and pointer-to-pointer conversions. |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 4618 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 4619 | case Stmt::CStyleCastExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4620 | case Stmt::CXXFunctionalCastExprClass: |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 4621 | case Stmt::ObjCBridgedCastExprClass: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4622 | case Stmt::CXXStaticCastExprClass: |
| 4623 | case Stmt::CXXDynamicCastExprClass: |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 4624 | case Stmt::CXXConstCastExprClass: |
| 4625 | case Stmt::CXXReinterpretCastExprClass: { |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 4626 | Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
| 4627 | switch (cast<CastExpr>(E)->getCastKind()) { |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 4628 | case CK_LValueToRValue: |
| 4629 | case CK_NoOp: |
| 4630 | case CK_BaseToDerived: |
| 4631 | case CK_DerivedToBase: |
| 4632 | case CK_UncheckedDerivedToBase: |
| 4633 | case CK_Dynamic: |
| 4634 | case CK_CPointerToObjCPointerCast: |
| 4635 | case CK_BlockPointerToObjCPointerCast: |
| 4636 | case CK_AnyPointerToBlockPointerCast: |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4637 | return EvalAddr(SubExpr, refVars, ParentDecl); |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 4638 | |
| 4639 | case CK_ArrayToPointerDecay: |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4640 | return EvalVal(SubExpr, refVars, ParentDecl); |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 4641 | |
Richard Trieu | dadefde | 2014-07-02 04:39:38 +0000 | [diff] [blame] | 4642 | case CK_BitCast: |
| 4643 | if (SubExpr->getType()->isAnyPointerType() || |
| 4644 | SubExpr->getType()->isBlockPointerType() || |
| 4645 | SubExpr->getType()->isObjCQualifiedIdType()) |
| 4646 | return EvalAddr(SubExpr, refVars, ParentDecl); |
| 4647 | else |
| 4648 | return nullptr; |
| 4649 | |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 4650 | default: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4651 | return nullptr; |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 4652 | } |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 4653 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4654 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4655 | case Stmt::MaterializeTemporaryExprClass: |
| 4656 | if (Expr *Result = EvalAddr( |
| 4657 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4658 | refVars, ParentDecl)) |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4659 | return Result; |
| 4660 | |
| 4661 | return E; |
| 4662 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 4663 | // Everything else: we simply don't reason about them. |
| 4664 | default: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4665 | return nullptr; |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 4666 | } |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4667 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4668 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4669 | |
| 4670 | /// EvalVal - This function is complements EvalAddr in the mutual recursion. |
| 4671 | /// See the comments for EvalAddr for more details. |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4672 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 4673 | Decl *ParentDecl) { |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 4674 | do { |
Ted Kremenek | e07a8cd | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 4675 | // We should only be called for evaluating non-pointer expressions, or |
| 4676 | // expressions with a pointer type that are not used as references but instead |
| 4677 | // are l-values (e.g., DeclRefExpr with a pointer type). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4678 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4679 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 4680 | // viewed AST node. We then recursively traverse the AST by calling |
| 4681 | // EvalAddr and EvalVal appropriately. |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 4682 | |
| 4683 | E = E->IgnoreParens(); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4684 | switch (E->getStmtClass()) { |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 4685 | case Stmt::ImplicitCastExprClass: { |
| 4686 | ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E); |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4687 | if (IE->getValueKind() == VK_LValue) { |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 4688 | E = IE->getSubExpr(); |
| 4689 | continue; |
| 4690 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4691 | return nullptr; |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 4692 | } |
| 4693 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 4694 | case Stmt::ExprWithCleanupsClass: |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4695 | return EvalVal(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,ParentDecl); |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 4696 | |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4697 | case Stmt::DeclRefExprClass: { |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4698 | // When we hit a DeclRefExpr we are looking at code that refers to a |
| 4699 | // variable's name. If it's not a reference variable we check if it has |
| 4700 | // local storage within the function, and if so, return the expression. |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4701 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4702 | |
Richard Smith | 40f08eb | 2014-01-30 22:05:38 +0000 | [diff] [blame] | 4703 | // If we leave the immediate function, the lifetime isn't about to end. |
| 4704 | if (DR->refersToEnclosingLocal()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4705 | return nullptr; |
Richard Smith | 40f08eb | 2014-01-30 22:05:38 +0000 | [diff] [blame] | 4706 | |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4707 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) { |
| 4708 | // Check if it refers to itself, e.g. "int& i = i;". |
| 4709 | if (V == ParentDecl) |
| 4710 | return DR; |
| 4711 | |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4712 | if (V->hasLocalStorage()) { |
| 4713 | if (!V->getType()->isReferenceType()) |
| 4714 | return DR; |
| 4715 | |
| 4716 | // Reference variable, follow through to the expression that |
| 4717 | // it points to. |
| 4718 | if (V->hasInit()) { |
| 4719 | // Add the reference variable to the "trail". |
| 4720 | refVars.push_back(DR); |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4721 | return EvalVal(V->getInit(), refVars, V); |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4722 | } |
| 4723 | } |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4724 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4725 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4726 | return nullptr; |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4727 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4728 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4729 | case Stmt::UnaryOperatorClass: { |
| 4730 | // The only unary operator that make sense to handle here |
| 4731 | // is Deref. All others don't resolve to a "name." This includes |
| 4732 | // handling all sorts of rvalues passed to a unary operator. |
| 4733 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4734 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4735 | if (U->getOpcode() == UO_Deref) |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4736 | return EvalAddr(U->getSubExpr(), refVars, ParentDecl); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4737 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4738 | return nullptr; |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4739 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4740 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4741 | case Stmt::ArraySubscriptExprClass: { |
| 4742 | // Array subscripts are potential references to data on the stack. We |
| 4743 | // retrieve the DeclRefExpr* for the array variable if it indeed |
| 4744 | // has local storage. |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4745 | return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars,ParentDecl); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4746 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4747 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4748 | case Stmt::ConditionalOperatorClass: { |
| 4749 | // For conditional operators we need to see if either the LHS or RHS are |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4750 | // non-NULL Expr's. If one is non-NULL, we return it. |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4751 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 4752 | |
Anders Carlsson | 801c5c7 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 4753 | // Handle the GNU extension for missing LHS. |
Richard Smith | 6a6a4bb | 2014-01-27 04:19:56 +0000 | [diff] [blame] | 4754 | if (Expr *LHSExpr = C->getLHS()) { |
| 4755 | // In C++, we can have a throw-expression, which has 'void' type. |
| 4756 | if (!LHSExpr->getType()->isVoidType()) |
| 4757 | if (Expr *LHS = EvalVal(LHSExpr, refVars, ParentDecl)) |
| 4758 | return LHS; |
| 4759 | } |
| 4760 | |
| 4761 | // In C++, we can have a throw-expression, which has 'void' type. |
| 4762 | if (C->getRHS()->getType()->isVoidType()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4763 | return nullptr; |
Anders Carlsson | 801c5c7 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 4764 | |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4765 | return EvalVal(C->getRHS(), refVars, ParentDecl); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4766 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4767 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4768 | // Accesses to members are potential references to data on the stack. |
Douglas Gregor | f405d7e | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 4769 | case Stmt::MemberExprClass: { |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4770 | MemberExpr *M = cast<MemberExpr>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4771 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4772 | // Check for indirect access. We only want direct field accesses. |
Ted Kremenek | cbe6b0b | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 4773 | if (M->isArrow()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4774 | return nullptr; |
Ted Kremenek | cbe6b0b | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 4775 | |
| 4776 | // Check whether the member type is itself a reference, in which case |
| 4777 | // we're not going to refer to the member, but to what the member refers to. |
| 4778 | if (M->getMemberDecl()->getType()->isReferenceType()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4779 | return nullptr; |
Ted Kremenek | cbe6b0b | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 4780 | |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4781 | return EvalVal(M->getBase(), refVars, ParentDecl); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4782 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4783 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4784 | case Stmt::MaterializeTemporaryExprClass: |
| 4785 | if (Expr *Result = EvalVal( |
| 4786 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 4787 | refVars, ParentDecl)) |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4788 | return Result; |
| 4789 | |
| 4790 | return E; |
| 4791 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4792 | default: |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 4793 | // Check that we don't return or take the address of a reference to a |
| 4794 | // temporary. This is only useful in C++. |
| 4795 | if (!E->isTypeDependent() && E->isRValue()) |
| 4796 | return E; |
| 4797 | |
| 4798 | // Everything else: we simply don't reason about them. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4799 | return nullptr; |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4800 | } |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 4801 | } while (true); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 4802 | } |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 4803 | |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 4804 | void |
| 4805 | Sema::CheckReturnValExpr(Expr *RetValExp, QualType lhsType, |
| 4806 | SourceLocation ReturnLoc, |
| 4807 | bool isObjCMethod, |
Artyom Skrobov | 9f21344 | 2014-01-24 11:10:39 +0000 | [diff] [blame] | 4808 | const AttrVec *Attrs, |
| 4809 | const FunctionDecl *FD) { |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 4810 | CheckReturnStackAddr(*this, RetValExp, lhsType, ReturnLoc); |
| 4811 | |
| 4812 | // Check if the return value is null but should not be. |
Benjamin Kramer | ae852a6 | 2014-02-23 14:34:50 +0000 | [diff] [blame] | 4813 | if (Attrs && hasSpecificAttr<ReturnsNonNullAttr>(*Attrs) && |
| 4814 | CheckNonNullExpr(*this, RetValExp)) |
| 4815 | Diag(ReturnLoc, diag::warn_null_ret) |
| 4816 | << (isObjCMethod ? 1 : 0) << RetValExp->getSourceRange(); |
Artyom Skrobov | 9f21344 | 2014-01-24 11:10:39 +0000 | [diff] [blame] | 4817 | |
| 4818 | // C++11 [basic.stc.dynamic.allocation]p4: |
| 4819 | // If an allocation function declared with a non-throwing |
| 4820 | // exception-specification fails to allocate storage, it shall return |
| 4821 | // a null pointer. Any other allocation function that fails to allocate |
| 4822 | // storage shall indicate failure only by throwing an exception [...] |
| 4823 | if (FD) { |
| 4824 | OverloadedOperatorKind Op = FD->getOverloadedOperator(); |
| 4825 | if (Op == OO_New || Op == OO_Array_New) { |
| 4826 | const FunctionProtoType *Proto |
| 4827 | = FD->getType()->castAs<FunctionProtoType>(); |
| 4828 | if (!Proto->isNothrow(Context, /*ResultIfDependent*/true) && |
| 4829 | CheckNonNullExpr(*this, RetValExp)) |
| 4830 | Diag(ReturnLoc, diag::warn_operator_new_returns_null) |
| 4831 | << FD << getLangOpts().CPlusPlus11; |
| 4832 | } |
| 4833 | } |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 4834 | } |
| 4835 | |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 4836 | //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// |
| 4837 | |
| 4838 | /// Check for comparisons of floating point operands using != and ==. |
| 4839 | /// Issue a warning if these are no self-comparisons, as they are not likely |
| 4840 | /// to do what the programmer intended. |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4841 | void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4842 | Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts(); |
| 4843 | Expr* RightExprSansParen = RHS->IgnoreParenImpCasts(); |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 4844 | |
| 4845 | // Special case: check for x == x (which is OK). |
| 4846 | // Do not emit warnings for such cases. |
| 4847 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) |
| 4848 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) |
| 4849 | if (DRL->getDecl() == DRR->getDecl()) |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 4850 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4851 | |
| 4852 | |
Ted Kremenek | eda40e2 | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 4853 | // Special case: check for comparisons against literals that can be exactly |
| 4854 | // represented by APFloat. In such cases, do not emit a warning. This |
| 4855 | // is a heuristic: often comparison against such literals are used to |
| 4856 | // detect if a value in a variable has not changed. This clearly can |
| 4857 | // lead to false negatives. |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 4858 | if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { |
| 4859 | if (FLL->isExact()) |
| 4860 | return; |
| 4861 | } else |
| 4862 | if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)) |
| 4863 | if (FLR->isExact()) |
| 4864 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4865 | |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 4866 | // Check for comparisons with builtin types. |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 4867 | if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 4868 | if (CL->getBuiltinCallee()) |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 4869 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4870 | |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 4871 | if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 4872 | if (CR->getBuiltinCallee()) |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 4873 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4874 | |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 4875 | // Emit the diagnostic. |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 4876 | Diag(Loc, diag::warn_floatingpoint_eq) |
| 4877 | << LHS->getSourceRange() << RHS->getSourceRange(); |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 4878 | } |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4879 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4880 | //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// |
| 4881 | //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4882 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4883 | namespace { |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4884 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4885 | /// Structure recording the 'active' range of an integer-valued |
| 4886 | /// expression. |
| 4887 | struct IntRange { |
| 4888 | /// The number of bits active in the int. |
| 4889 | unsigned Width; |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4890 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4891 | /// True if the int is known not to have negative values. |
| 4892 | bool NonNegative; |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4893 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4894 | IntRange(unsigned Width, bool NonNegative) |
| 4895 | : Width(Width), NonNegative(NonNegative) |
| 4896 | {} |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4897 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4898 | /// Returns the range of the bool type. |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4899 | static IntRange forBoolType() { |
| 4900 | return IntRange(1, true); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4901 | } |
| 4902 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4903 | /// Returns the range of an opaque value of the given integral type. |
| 4904 | static IntRange forValueOfType(ASTContext &C, QualType T) { |
| 4905 | return forValueOfCanonicalType(C, |
| 4906 | T->getCanonicalTypeInternal().getTypePtr()); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4907 | } |
| 4908 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4909 | /// Returns the range of an opaque value of a canonical integral type. |
| 4910 | static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4911 | assert(T->isCanonicalUnqualified()); |
| 4912 | |
| 4913 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 4914 | T = VT->getElementType().getTypePtr(); |
| 4915 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 4916 | T = CT->getElementType().getTypePtr(); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4917 | |
David Majnemer | 6a42665 | 2013-06-07 22:07:20 +0000 | [diff] [blame] | 4918 | // For enum types, use the known bit width of the enumerators. |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4919 | if (const EnumType *ET = dyn_cast<EnumType>(T)) { |
David Majnemer | 6a42665 | 2013-06-07 22:07:20 +0000 | [diff] [blame] | 4920 | EnumDecl *Enum = ET->getDecl(); |
| 4921 | if (!Enum->isCompleteDefinition()) |
| 4922 | return IntRange(C.getIntWidth(QualType(T, 0)), false); |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4923 | |
David Majnemer | 6a42665 | 2013-06-07 22:07:20 +0000 | [diff] [blame] | 4924 | unsigned NumPositive = Enum->getNumPositiveBits(); |
| 4925 | unsigned NumNegative = Enum->getNumNegativeBits(); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4926 | |
David Majnemer | 6a42665 | 2013-06-07 22:07:20 +0000 | [diff] [blame] | 4927 | if (NumNegative == 0) |
| 4928 | return IntRange(NumPositive, true/*NonNegative*/); |
| 4929 | else |
| 4930 | return IntRange(std::max(NumPositive + 1, NumNegative), |
| 4931 | false/*NonNegative*/); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4932 | } |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4933 | |
| 4934 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 4935 | assert(BT->isInteger()); |
| 4936 | |
| 4937 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 4938 | } |
| 4939 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4940 | /// Returns the "target" range of a canonical integral type, i.e. |
| 4941 | /// the range of values expressible in the type. |
| 4942 | /// |
| 4943 | /// This matches forValueOfCanonicalType except that enums have the |
| 4944 | /// full range of their type, not the range of their enumerators. |
| 4945 | static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) { |
| 4946 | assert(T->isCanonicalUnqualified()); |
| 4947 | |
| 4948 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 4949 | T = VT->getElementType().getTypePtr(); |
| 4950 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 4951 | T = CT->getElementType().getTypePtr(); |
| 4952 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Douglas Gregor | 3168dcf | 2011-09-08 23:29:05 +0000 | [diff] [blame] | 4953 | T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr(); |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4954 | |
| 4955 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 4956 | assert(BT->isInteger()); |
| 4957 | |
| 4958 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 4959 | } |
| 4960 | |
| 4961 | /// Returns the supremum of two ranges: i.e. their conservative merge. |
John McCall | ff96ccd | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 4962 | static IntRange join(IntRange L, IntRange R) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4963 | return IntRange(std::max(L.Width, R.Width), |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 4964 | L.NonNegative && R.NonNegative); |
| 4965 | } |
| 4966 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4967 | /// Returns the infinum of two ranges: i.e. their aggressive merge. |
John McCall | ff96ccd | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 4968 | static IntRange meet(IntRange L, IntRange R) { |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 4969 | return IntRange(std::min(L.Width, R.Width), |
| 4970 | L.NonNegative || R.NonNegative); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4971 | } |
| 4972 | }; |
| 4973 | |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4974 | static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, |
| 4975 | unsigned MaxWidth) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4976 | if (value.isSigned() && value.isNegative()) |
| 4977 | return IntRange(value.getMinSignedBits(), false); |
| 4978 | |
| 4979 | if (value.getBitWidth() > MaxWidth) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 4980 | value = value.trunc(MaxWidth); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4981 | |
| 4982 | // isNonNegative() just checks the sign bit without considering |
| 4983 | // signedness. |
| 4984 | return IntRange(value.getActiveBits(), true); |
| 4985 | } |
| 4986 | |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4987 | static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, |
| 4988 | unsigned MaxWidth) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4989 | if (result.isInt()) |
| 4990 | return GetValueRange(C, result.getInt(), MaxWidth); |
| 4991 | |
| 4992 | if (result.isVector()) { |
John McCall | 7443052 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 4993 | IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); |
| 4994 | for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { |
| 4995 | IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); |
| 4996 | R = IntRange::join(R, El); |
| 4997 | } |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4998 | return R; |
| 4999 | } |
| 5000 | |
| 5001 | if (result.isComplexInt()) { |
| 5002 | IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); |
| 5003 | IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); |
| 5004 | return IntRange::join(R, I); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5005 | } |
| 5006 | |
| 5007 | // This can happen with lossless casts to intptr_t of "based" lvalues. |
| 5008 | // Assume it might use arbitrary bits. |
John McCall | 7443052 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 5009 | // FIXME: The only reason we need to pass the type in here is to get |
| 5010 | // the sign right on this one case. It would be nice if APValue |
| 5011 | // preserved this. |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 5012 | assert(result.isLValue() || result.isAddrLabelDiff()); |
Douglas Gregor | 61b6e49 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 5013 | return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType()); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5014 | } |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5015 | |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5016 | static QualType GetExprType(Expr *E) { |
| 5017 | QualType Ty = E->getType(); |
| 5018 | if (const AtomicType *AtomicRHS = Ty->getAs<AtomicType>()) |
| 5019 | Ty = AtomicRHS->getValueType(); |
| 5020 | return Ty; |
| 5021 | } |
| 5022 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5023 | /// Pseudo-evaluate the given integer expression, estimating the |
| 5024 | /// range of values it might take. |
| 5025 | /// |
| 5026 | /// \param MaxWidth - the width to which the value will be truncated |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 5027 | static IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5028 | E = E->IgnoreParens(); |
| 5029 | |
| 5030 | // Try a full evaluation first. |
| 5031 | Expr::EvalResult result; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5032 | if (E->EvaluateAsRValue(result, C)) |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5033 | return GetValueRange(C, result.Val, GetExprType(E), MaxWidth); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5034 | |
| 5035 | // I think we only want to look through implicit casts here; if the |
| 5036 | // user has an explicit widening cast, we should treat the value as |
| 5037 | // being of the new, wider type. |
| 5038 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) { |
Eli Friedman | 8349dc1 | 2011-12-15 02:41:52 +0000 | [diff] [blame] | 5039 | if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue) |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5040 | return GetExprRange(C, CE->getSubExpr(), MaxWidth); |
| 5041 | |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5042 | IntRange OutputTypeRange = IntRange::forValueOfType(C, GetExprType(CE)); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5043 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5044 | bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast); |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 5045 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5046 | // Assume that non-integer casts can span the full range of the type. |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 5047 | if (!isIntegerCast) |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5048 | return OutputTypeRange; |
| 5049 | |
| 5050 | IntRange SubRange |
| 5051 | = GetExprRange(C, CE->getSubExpr(), |
| 5052 | std::min(MaxWidth, OutputTypeRange.Width)); |
| 5053 | |
| 5054 | // Bail out if the subexpr's range is as wide as the cast type. |
| 5055 | if (SubRange.Width >= OutputTypeRange.Width) |
| 5056 | return OutputTypeRange; |
| 5057 | |
| 5058 | // Otherwise, we take the smaller width, and we're non-negative if |
| 5059 | // either the output type or the subexpr is. |
| 5060 | return IntRange(SubRange.Width, |
| 5061 | SubRange.NonNegative || OutputTypeRange.NonNegative); |
| 5062 | } |
| 5063 | |
| 5064 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 5065 | // If we can fold the condition, just take that operand. |
| 5066 | bool CondResult; |
| 5067 | if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) |
| 5068 | return GetExprRange(C, CondResult ? CO->getTrueExpr() |
| 5069 | : CO->getFalseExpr(), |
| 5070 | MaxWidth); |
| 5071 | |
| 5072 | // Otherwise, conservatively merge. |
| 5073 | IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth); |
| 5074 | IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth); |
| 5075 | return IntRange::join(L, R); |
| 5076 | } |
| 5077 | |
| 5078 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 5079 | switch (BO->getOpcode()) { |
| 5080 | |
| 5081 | // Boolean-valued operations are single-bit and positive. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5082 | case BO_LAnd: |
| 5083 | case BO_LOr: |
| 5084 | case BO_LT: |
| 5085 | case BO_GT: |
| 5086 | case BO_LE: |
| 5087 | case BO_GE: |
| 5088 | case BO_EQ: |
| 5089 | case BO_NE: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5090 | return IntRange::forBoolType(); |
| 5091 | |
John McCall | c368838 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 5092 | // The type of the assignments is the type of the LHS, so the RHS |
| 5093 | // is not necessarily the same type. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5094 | case BO_MulAssign: |
| 5095 | case BO_DivAssign: |
| 5096 | case BO_RemAssign: |
| 5097 | case BO_AddAssign: |
| 5098 | case BO_SubAssign: |
John McCall | c368838 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 5099 | case BO_XorAssign: |
| 5100 | case BO_OrAssign: |
| 5101 | // TODO: bitfields? |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5102 | return IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | ff96ccd | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 5103 | |
John McCall | c368838 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 5104 | // Simple assignments just pass through the RHS, which will have |
| 5105 | // been coerced to the LHS type. |
| 5106 | case BO_Assign: |
| 5107 | // TODO: bitfields? |
| 5108 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 5109 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5110 | // Operations with opaque sources are black-listed. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5111 | case BO_PtrMemD: |
| 5112 | case BO_PtrMemI: |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5113 | return IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5114 | |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 5115 | // Bitwise-and uses the *infinum* of the two source ranges. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5116 | case BO_And: |
| 5117 | case BO_AndAssign: |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 5118 | return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth), |
| 5119 | GetExprRange(C, BO->getRHS(), MaxWidth)); |
| 5120 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5121 | // Left shift gets black-listed based on a judgement call. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5122 | case BO_Shl: |
John McCall | 1bff993 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 5123 | // ...except that we want to treat '1 << (blah)' as logically |
| 5124 | // positive. It's an important idiom. |
| 5125 | if (IntegerLiteral *I |
| 5126 | = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { |
| 5127 | if (I->getValue() == 1) { |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5128 | IntRange R = IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | 1bff993 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 5129 | return IntRange(R.Width, /*NonNegative*/ true); |
| 5130 | } |
| 5131 | } |
| 5132 | // fallthrough |
| 5133 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5134 | case BO_ShlAssign: |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5135 | return IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5136 | |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 5137 | // Right shift by a constant can narrow its left argument. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5138 | case BO_Shr: |
| 5139 | case BO_ShrAssign: { |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 5140 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 5141 | |
| 5142 | // If the shift amount is a positive constant, drop the width by |
| 5143 | // that much. |
| 5144 | llvm::APSInt shift; |
| 5145 | if (BO->getRHS()->isIntegerConstantExpr(shift, C) && |
| 5146 | shift.isNonNegative()) { |
| 5147 | unsigned zext = shift.getZExtValue(); |
| 5148 | if (zext >= L.Width) |
| 5149 | L.Width = (L.NonNegative ? 0 : 1); |
| 5150 | else |
| 5151 | L.Width -= zext; |
| 5152 | } |
| 5153 | |
| 5154 | return L; |
| 5155 | } |
| 5156 | |
| 5157 | // Comma acts as its right operand. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5158 | case BO_Comma: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5159 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 5160 | |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 5161 | // Black-list pointer subtractions. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5162 | case BO_Sub: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5163 | if (BO->getLHS()->getType()->isPointerType()) |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5164 | return IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | 5143181 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 5165 | break; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 5166 | |
John McCall | 5143181 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 5167 | // The width of a division result is mostly determined by the size |
| 5168 | // of the LHS. |
| 5169 | case BO_Div: { |
| 5170 | // Don't 'pre-truncate' the operands. |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5171 | unsigned opWidth = C.getIntWidth(GetExprType(E)); |
John McCall | 5143181 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 5172 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); |
| 5173 | |
| 5174 | // If the divisor is constant, use that. |
| 5175 | llvm::APSInt divisor; |
| 5176 | if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) { |
| 5177 | unsigned log2 = divisor.logBase2(); // floor(log_2(divisor)) |
| 5178 | if (log2 >= L.Width) |
| 5179 | L.Width = (L.NonNegative ? 0 : 1); |
| 5180 | else |
| 5181 | L.Width = std::min(L.Width - log2, MaxWidth); |
| 5182 | return L; |
| 5183 | } |
| 5184 | |
| 5185 | // Otherwise, just use the LHS's width. |
| 5186 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); |
| 5187 | return IntRange(L.Width, L.NonNegative && R.NonNegative); |
| 5188 | } |
| 5189 | |
| 5190 | // The result of a remainder can't be larger than the result of |
| 5191 | // either side. |
| 5192 | case BO_Rem: { |
| 5193 | // Don't 'pre-truncate' the operands. |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5194 | unsigned opWidth = C.getIntWidth(GetExprType(E)); |
John McCall | 5143181 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 5195 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); |
| 5196 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); |
| 5197 | |
| 5198 | IntRange meet = IntRange::meet(L, R); |
| 5199 | meet.Width = std::min(meet.Width, MaxWidth); |
| 5200 | return meet; |
| 5201 | } |
| 5202 | |
| 5203 | // The default behavior is okay for these. |
| 5204 | case BO_Mul: |
| 5205 | case BO_Add: |
| 5206 | case BO_Xor: |
| 5207 | case BO_Or: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5208 | break; |
| 5209 | } |
| 5210 | |
John McCall | 5143181 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 5211 | // The default case is to treat the operation as if it were closed |
| 5212 | // on the narrowest type that encompasses both operands. |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5213 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 5214 | IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth); |
| 5215 | return IntRange::join(L, R); |
| 5216 | } |
| 5217 | |
| 5218 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 5219 | switch (UO->getOpcode()) { |
| 5220 | // Boolean-valued operations are white-listed. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5221 | case UO_LNot: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5222 | return IntRange::forBoolType(); |
| 5223 | |
| 5224 | // Operations with opaque sources are black-listed. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5225 | case UO_Deref: |
| 5226 | case UO_AddrOf: // should be impossible |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5227 | return IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5228 | |
| 5229 | default: |
| 5230 | return GetExprRange(C, UO->getSubExpr(), MaxWidth); |
| 5231 | } |
| 5232 | } |
| 5233 | |
Ted Kremenek | a553fbf | 2013-10-14 18:55:27 +0000 | [diff] [blame] | 5234 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) |
| 5235 | return GetExprRange(C, OVE->getSourceExpr(), MaxWidth); |
| 5236 | |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5237 | if (FieldDecl *BitField = E->getSourceBitField()) |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5238 | return IntRange(BitField->getBitWidthValue(C), |
Douglas Gregor | 61b6e49 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 5239 | BitField->getType()->isUnsignedIntegerOrEnumerationType()); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5240 | |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5241 | return IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5242 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5243 | |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 5244 | static IntRange GetExprRange(ASTContext &C, Expr *E) { |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5245 | return GetExprRange(C, E, C.getIntWidth(GetExprType(E))); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5246 | } |
| 5247 | |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5248 | /// Checks whether the given value, which currently has the given |
| 5249 | /// source semantics, has the same value when coerced through the |
| 5250 | /// target semantics. |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 5251 | static bool IsSameFloatAfterCast(const llvm::APFloat &value, |
| 5252 | const llvm::fltSemantics &Src, |
| 5253 | const llvm::fltSemantics &Tgt) { |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5254 | llvm::APFloat truncated = value; |
| 5255 | |
| 5256 | bool ignored; |
| 5257 | truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 5258 | truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 5259 | |
| 5260 | return truncated.bitwiseIsEqual(value); |
| 5261 | } |
| 5262 | |
| 5263 | /// Checks whether the given value, which currently has the given |
| 5264 | /// source semantics, has the same value when coerced through the |
| 5265 | /// target semantics. |
| 5266 | /// |
| 5267 | /// The value might be a vector of floats (or a complex number). |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 5268 | static bool IsSameFloatAfterCast(const APValue &value, |
| 5269 | const llvm::fltSemantics &Src, |
| 5270 | const llvm::fltSemantics &Tgt) { |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5271 | if (value.isFloat()) |
| 5272 | return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); |
| 5273 | |
| 5274 | if (value.isVector()) { |
| 5275 | for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) |
| 5276 | if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) |
| 5277 | return false; |
| 5278 | return true; |
| 5279 | } |
| 5280 | |
| 5281 | assert(value.isComplexFloat()); |
| 5282 | return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && |
| 5283 | IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); |
| 5284 | } |
| 5285 | |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 5286 | static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5287 | |
Ted Kremenek | 6274be4 | 2010-09-23 21:43:44 +0000 | [diff] [blame] | 5288 | static bool IsZero(Sema &S, Expr *E) { |
| 5289 | // Suppress cases where we are comparing against an enum constant. |
| 5290 | if (const DeclRefExpr *DR = |
| 5291 | dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) |
| 5292 | if (isa<EnumConstantDecl>(DR->getDecl())) |
| 5293 | return false; |
| 5294 | |
| 5295 | // Suppress cases where the '0' value is expanded from a macro. |
| 5296 | if (E->getLocStart().isMacroID()) |
| 5297 | return false; |
| 5298 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5299 | llvm::APSInt Value; |
| 5300 | return E->isIntegerConstantExpr(Value, S.Context) && Value == 0; |
| 5301 | } |
| 5302 | |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 5303 | static bool HasEnumType(Expr *E) { |
| 5304 | // Strip off implicit integral promotions. |
| 5305 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
Argyrios Kyrtzidis | 15a9edc | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 5306 | if (ICE->getCastKind() != CK_IntegralCast && |
| 5307 | ICE->getCastKind() != CK_NoOp) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 5308 | break; |
Argyrios Kyrtzidis | 15a9edc | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 5309 | E = ICE->getSubExpr(); |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 5310 | } |
| 5311 | |
| 5312 | return E->getType()->isEnumeralType(); |
| 5313 | } |
| 5314 | |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 5315 | static void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) { |
Richard Trieu | 3659456 | 2013-11-01 21:47:19 +0000 | [diff] [blame] | 5316 | // Disable warning in template instantiations. |
| 5317 | if (!S.ActiveTemplateInstantiations.empty()) |
| 5318 | return; |
| 5319 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5320 | BinaryOperatorKind op = E->getOpcode(); |
Douglas Gregor | b14dbd7 | 2010-12-21 07:22:56 +0000 | [diff] [blame] | 5321 | if (E->isValueDependent()) |
| 5322 | return; |
| 5323 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5324 | if (op == BO_LT && IsZero(S, E->getRHS())) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5325 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 5326 | << "< 0" << "false" << HasEnumType(E->getLHS()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5327 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5328 | } else if (op == BO_GE && IsZero(S, E->getRHS())) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5329 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 5330 | << ">= 0" << "true" << HasEnumType(E->getLHS()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5331 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5332 | } else if (op == BO_GT && IsZero(S, E->getLHS())) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5333 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 5334 | << "0 >" << "false" << HasEnumType(E->getRHS()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5335 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5336 | } else if (op == BO_LE && IsZero(S, E->getLHS())) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5337 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 5338 | << "0 <=" << "true" << HasEnumType(E->getRHS()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5339 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
| 5340 | } |
| 5341 | } |
| 5342 | |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 5343 | static void DiagnoseOutOfRangeComparison(Sema &S, BinaryOperator *E, |
Fariborz Jahanian | 2f4e33a | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 5344 | Expr *Constant, Expr *Other, |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 5345 | llvm::APSInt Value, |
Fariborz Jahanian | 2f4e33a | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 5346 | bool RhsConstant) { |
Richard Trieu | dd51d74 | 2013-11-01 21:19:43 +0000 | [diff] [blame] | 5347 | // Disable warning in template instantiations. |
| 5348 | if (!S.ActiveTemplateInstantiations.empty()) |
| 5349 | return; |
| 5350 | |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 5351 | // TODO: Investigate using GetExprRange() to get tighter bounds |
| 5352 | // on the bit ranges. |
| 5353 | QualType OtherT = Other->getType(); |
| 5354 | IntRange OtherRange = IntRange::forValueOfType(S.Context, OtherT); |
| 5355 | unsigned OtherWidth = OtherRange.Width; |
| 5356 | |
| 5357 | bool OtherIsBooleanType = Other->isKnownToHaveBooleanValue(); |
| 5358 | |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 5359 | // 0 values are handled later by CheckTrivialUnsignedComparison(). |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 5360 | if ((Value == 0) && (!OtherIsBooleanType)) |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 5361 | return; |
| 5362 | |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 5363 | BinaryOperatorKind op = E->getOpcode(); |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 5364 | bool IsTrue = true; |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 5365 | |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 5366 | // Used for diagnostic printout. |
| 5367 | enum { |
| 5368 | LiteralConstant = 0, |
| 5369 | CXXBoolLiteralTrue, |
| 5370 | CXXBoolLiteralFalse |
| 5371 | } LiteralOrBoolConstant = LiteralConstant; |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 5372 | |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 5373 | if (!OtherIsBooleanType) { |
| 5374 | QualType ConstantT = Constant->getType(); |
| 5375 | QualType CommonT = E->getLHS()->getType(); |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 5376 | |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 5377 | if (S.Context.hasSameUnqualifiedType(OtherT, ConstantT)) |
| 5378 | return; |
| 5379 | assert((OtherT->isIntegerType() && ConstantT->isIntegerType()) && |
| 5380 | "comparison with non-integer type"); |
| 5381 | |
| 5382 | bool ConstantSigned = ConstantT->isSignedIntegerType(); |
| 5383 | bool CommonSigned = CommonT->isSignedIntegerType(); |
| 5384 | |
| 5385 | bool EqualityOnly = false; |
| 5386 | |
| 5387 | if (CommonSigned) { |
| 5388 | // The common type is signed, therefore no signed to unsigned conversion. |
| 5389 | if (!OtherRange.NonNegative) { |
| 5390 | // Check that the constant is representable in type OtherT. |
| 5391 | if (ConstantSigned) { |
| 5392 | if (OtherWidth >= Value.getMinSignedBits()) |
| 5393 | return; |
| 5394 | } else { // !ConstantSigned |
| 5395 | if (OtherWidth >= Value.getActiveBits() + 1) |
| 5396 | return; |
| 5397 | } |
| 5398 | } else { // !OtherSigned |
| 5399 | // Check that the constant is representable in type OtherT. |
| 5400 | // Negative values are out of range. |
| 5401 | if (ConstantSigned) { |
| 5402 | if (Value.isNonNegative() && OtherWidth >= Value.getActiveBits()) |
| 5403 | return; |
| 5404 | } else { // !ConstantSigned |
| 5405 | if (OtherWidth >= Value.getActiveBits()) |
| 5406 | return; |
| 5407 | } |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 5408 | } |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 5409 | } else { // !CommonSigned |
| 5410 | if (OtherRange.NonNegative) { |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 5411 | if (OtherWidth >= Value.getActiveBits()) |
| 5412 | return; |
Craig Topper | cf36016 | 2014-06-18 05:13:11 +0000 | [diff] [blame] | 5413 | } else { // OtherSigned |
| 5414 | assert(!ConstantSigned && |
| 5415 | "Two signed types converted to unsigned types."); |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 5416 | // Check to see if the constant is representable in OtherT. |
| 5417 | if (OtherWidth > Value.getActiveBits()) |
| 5418 | return; |
| 5419 | // Check to see if the constant is equivalent to a negative value |
| 5420 | // cast to CommonT. |
| 5421 | if (S.Context.getIntWidth(ConstantT) == |
| 5422 | S.Context.getIntWidth(CommonT) && |
| 5423 | Value.isNegative() && Value.getMinSignedBits() <= OtherWidth) |
| 5424 | return; |
| 5425 | // The constant value rests between values that OtherT can represent |
| 5426 | // after conversion. Relational comparison still works, but equality |
| 5427 | // comparisons will be tautological. |
| 5428 | EqualityOnly = true; |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 5429 | } |
| 5430 | } |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 5431 | |
| 5432 | bool PositiveConstant = !ConstantSigned || Value.isNonNegative(); |
| 5433 | |
| 5434 | if (op == BO_EQ || op == BO_NE) { |
| 5435 | IsTrue = op == BO_NE; |
| 5436 | } else if (EqualityOnly) { |
| 5437 | return; |
| 5438 | } else if (RhsConstant) { |
| 5439 | if (op == BO_GT || op == BO_GE) |
| 5440 | IsTrue = !PositiveConstant; |
| 5441 | else // op == BO_LT || op == BO_LE |
| 5442 | IsTrue = PositiveConstant; |
| 5443 | } else { |
| 5444 | if (op == BO_LT || op == BO_LE) |
| 5445 | IsTrue = !PositiveConstant; |
| 5446 | else // op == BO_GT || op == BO_GE |
| 5447 | IsTrue = PositiveConstant; |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 5448 | } |
Fariborz Jahanian | 2f4e33a | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 5449 | } else { |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 5450 | // Other isKnownToHaveBooleanValue |
| 5451 | enum CompareBoolWithConstantResult { AFals, ATrue, Unkwn }; |
| 5452 | enum ConstantValue { LT_Zero, Zero, One, GT_One, SizeOfConstVal }; |
| 5453 | enum ConstantSide { Lhs, Rhs, SizeOfConstSides }; |
| 5454 | |
| 5455 | static const struct LinkedConditions { |
| 5456 | CompareBoolWithConstantResult BO_LT_OP[SizeOfConstSides][SizeOfConstVal]; |
| 5457 | CompareBoolWithConstantResult BO_GT_OP[SizeOfConstSides][SizeOfConstVal]; |
| 5458 | CompareBoolWithConstantResult BO_LE_OP[SizeOfConstSides][SizeOfConstVal]; |
| 5459 | CompareBoolWithConstantResult BO_GE_OP[SizeOfConstSides][SizeOfConstVal]; |
| 5460 | CompareBoolWithConstantResult BO_EQ_OP[SizeOfConstSides][SizeOfConstVal]; |
| 5461 | CompareBoolWithConstantResult BO_NE_OP[SizeOfConstSides][SizeOfConstVal]; |
| 5462 | |
| 5463 | } TruthTable = { |
| 5464 | // Constant on LHS. | Constant on RHS. | |
| 5465 | // LT_Zero| Zero | One |GT_One| LT_Zero| Zero | One |GT_One| |
| 5466 | { { ATrue, Unkwn, AFals, AFals }, { AFals, AFals, Unkwn, ATrue } }, |
| 5467 | { { AFals, AFals, Unkwn, ATrue }, { ATrue, Unkwn, AFals, AFals } }, |
| 5468 | { { ATrue, ATrue, Unkwn, AFals }, { AFals, Unkwn, ATrue, ATrue } }, |
| 5469 | { { AFals, Unkwn, ATrue, ATrue }, { ATrue, ATrue, Unkwn, AFals } }, |
| 5470 | { { AFals, Unkwn, Unkwn, AFals }, { AFals, Unkwn, Unkwn, AFals } }, |
| 5471 | { { ATrue, Unkwn, Unkwn, ATrue }, { ATrue, Unkwn, Unkwn, ATrue } } |
| 5472 | }; |
| 5473 | |
| 5474 | bool ConstantIsBoolLiteral = isa<CXXBoolLiteralExpr>(Constant); |
| 5475 | |
| 5476 | enum ConstantValue ConstVal = Zero; |
| 5477 | if (Value.isUnsigned() || Value.isNonNegative()) { |
| 5478 | if (Value == 0) { |
| 5479 | LiteralOrBoolConstant = |
| 5480 | ConstantIsBoolLiteral ? CXXBoolLiteralFalse : LiteralConstant; |
| 5481 | ConstVal = Zero; |
| 5482 | } else if (Value == 1) { |
| 5483 | LiteralOrBoolConstant = |
| 5484 | ConstantIsBoolLiteral ? CXXBoolLiteralTrue : LiteralConstant; |
| 5485 | ConstVal = One; |
| 5486 | } else { |
| 5487 | LiteralOrBoolConstant = LiteralConstant; |
| 5488 | ConstVal = GT_One; |
| 5489 | } |
| 5490 | } else { |
| 5491 | ConstVal = LT_Zero; |
| 5492 | } |
| 5493 | |
| 5494 | CompareBoolWithConstantResult CmpRes; |
| 5495 | |
| 5496 | switch (op) { |
| 5497 | case BO_LT: |
| 5498 | CmpRes = TruthTable.BO_LT_OP[RhsConstant][ConstVal]; |
| 5499 | break; |
| 5500 | case BO_GT: |
| 5501 | CmpRes = TruthTable.BO_GT_OP[RhsConstant][ConstVal]; |
| 5502 | break; |
| 5503 | case BO_LE: |
| 5504 | CmpRes = TruthTable.BO_LE_OP[RhsConstant][ConstVal]; |
| 5505 | break; |
| 5506 | case BO_GE: |
| 5507 | CmpRes = TruthTable.BO_GE_OP[RhsConstant][ConstVal]; |
| 5508 | break; |
| 5509 | case BO_EQ: |
| 5510 | CmpRes = TruthTable.BO_EQ_OP[RhsConstant][ConstVal]; |
| 5511 | break; |
| 5512 | case BO_NE: |
| 5513 | CmpRes = TruthTable.BO_NE_OP[RhsConstant][ConstVal]; |
| 5514 | break; |
| 5515 | default: |
| 5516 | CmpRes = Unkwn; |
| 5517 | break; |
| 5518 | } |
| 5519 | |
| 5520 | if (CmpRes == AFals) { |
| 5521 | IsTrue = false; |
| 5522 | } else if (CmpRes == ATrue) { |
| 5523 | IsTrue = true; |
| 5524 | } else { |
| 5525 | return; |
| 5526 | } |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 5527 | } |
Ted Kremenek | b7d7dd4 | 2013-03-15 21:50:10 +0000 | [diff] [blame] | 5528 | |
| 5529 | // If this is a comparison to an enum constant, include that |
| 5530 | // constant in the diagnostic. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5531 | const EnumConstantDecl *ED = nullptr; |
Ted Kremenek | b7d7dd4 | 2013-03-15 21:50:10 +0000 | [diff] [blame] | 5532 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Constant)) |
| 5533 | ED = dyn_cast<EnumConstantDecl>(DR->getDecl()); |
| 5534 | |
| 5535 | SmallString<64> PrettySourceValue; |
| 5536 | llvm::raw_svector_ostream OS(PrettySourceValue); |
| 5537 | if (ED) |
Ted Kremenek | e943ce1 | 2013-03-15 22:02:46 +0000 | [diff] [blame] | 5538 | OS << '\'' << *ED << "' (" << Value << ")"; |
Ted Kremenek | b7d7dd4 | 2013-03-15 21:50:10 +0000 | [diff] [blame] | 5539 | else |
| 5540 | OS << Value; |
| 5541 | |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 5542 | S.DiagRuntimeBehavior( |
| 5543 | E->getOperatorLoc(), E, |
| 5544 | S.PDiag(diag::warn_out_of_range_compare) |
| 5545 | << OS.str() << LiteralOrBoolConstant |
| 5546 | << OtherT << (OtherIsBooleanType && !OtherT->isBooleanType()) << IsTrue |
| 5547 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange()); |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 5548 | } |
| 5549 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5550 | /// Analyze the operands of the given comparison. Implements the |
| 5551 | /// fallback case from AnalyzeComparison. |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 5552 | static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5553 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 5554 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5555 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5556 | |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 5557 | /// \brief Implements -Wsign-compare. |
| 5558 | /// |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5559 | /// \param E the binary operator to check for warnings |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 5560 | static void AnalyzeComparison(Sema &S, BinaryOperator *E) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5561 | // The type the comparison is being performed in. |
| 5562 | QualType T = E->getLHS()->getType(); |
| 5563 | assert(S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType()) |
| 5564 | && "comparison with mismatched types"); |
Fariborz Jahanian | 282071e | 2012-09-18 17:46:26 +0000 | [diff] [blame] | 5565 | if (E->isValueDependent()) |
| 5566 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 5567 | |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 5568 | Expr *LHS = E->getLHS()->IgnoreParenImpCasts(); |
| 5569 | Expr *RHS = E->getRHS()->IgnoreParenImpCasts(); |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 5570 | |
| 5571 | bool IsComparisonConstant = false; |
| 5572 | |
Fariborz Jahanian | 2f4e33a | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 5573 | // Check whether an integer constant comparison results in a value |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 5574 | // of 'true' or 'false'. |
| 5575 | if (T->isIntegralType(S.Context)) { |
| 5576 | llvm::APSInt RHSValue; |
| 5577 | bool IsRHSIntegralLiteral = |
| 5578 | RHS->isIntegerConstantExpr(RHSValue, S.Context); |
| 5579 | llvm::APSInt LHSValue; |
| 5580 | bool IsLHSIntegralLiteral = |
| 5581 | LHS->isIntegerConstantExpr(LHSValue, S.Context); |
| 5582 | if (IsRHSIntegralLiteral && !IsLHSIntegralLiteral) |
| 5583 | DiagnoseOutOfRangeComparison(S, E, RHS, LHS, RHSValue, true); |
| 5584 | else if (!IsRHSIntegralLiteral && IsLHSIntegralLiteral) |
| 5585 | DiagnoseOutOfRangeComparison(S, E, LHS, RHS, LHSValue, false); |
| 5586 | else |
| 5587 | IsComparisonConstant = |
| 5588 | (IsRHSIntegralLiteral && IsLHSIntegralLiteral); |
Fariborz Jahanian | 2f4e33a | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 5589 | } else if (!T->hasUnsignedIntegerRepresentation()) |
| 5590 | IsComparisonConstant = E->isIntegerConstantExpr(S.Context); |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 5591 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5592 | // We don't do anything special if this isn't an unsigned integral |
| 5593 | // comparison: we're only interested in integral comparisons, and |
| 5594 | // signed comparisons only happen in cases we don't care to warn about. |
Douglas Gregor | 5b05454 | 2011-02-19 22:34:59 +0000 | [diff] [blame] | 5595 | // |
| 5596 | // We also don't care about value-dependent expressions or expressions |
| 5597 | // whose result is a constant. |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 5598 | if (!T->hasUnsignedIntegerRepresentation() || IsComparisonConstant) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5599 | return AnalyzeImpConvsInComparison(S, E); |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 5600 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5601 | // Check to see if one of the (unmodified) operands is of different |
| 5602 | // signedness. |
| 5603 | Expr *signedOperand, *unsignedOperand; |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5604 | if (LHS->getType()->hasSignedIntegerRepresentation()) { |
| 5605 | assert(!RHS->getType()->hasSignedIntegerRepresentation() && |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5606 | "unsigned comparison between two signed integer expressions?"); |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5607 | signedOperand = LHS; |
| 5608 | unsignedOperand = RHS; |
| 5609 | } else if (RHS->getType()->hasSignedIntegerRepresentation()) { |
| 5610 | signedOperand = RHS; |
| 5611 | unsignedOperand = LHS; |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 5612 | } else { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5613 | CheckTrivialUnsignedComparison(S, E); |
| 5614 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 5615 | } |
| 5616 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5617 | // Otherwise, calculate the effective range of the signed operand. |
| 5618 | IntRange signedRange = GetExprRange(S.Context, signedOperand); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5619 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5620 | // Go ahead and analyze implicit conversions in the operands. Note |
| 5621 | // that we skip the implicit conversions on both sides. |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5622 | AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc()); |
| 5623 | AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc()); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 5624 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5625 | // If the signed range is non-negative, -Wsign-compare won't fire, |
| 5626 | // but we should still check for comparisons which are always true |
| 5627 | // or false. |
| 5628 | if (signedRange.NonNegative) |
| 5629 | return CheckTrivialUnsignedComparison(S, E); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 5630 | |
| 5631 | // For (in)equality comparisons, if the unsigned operand is a |
| 5632 | // constant which cannot collide with a overflowed signed operand, |
| 5633 | // then reinterpreting the signed operand as unsigned will not |
| 5634 | // change the result of the comparison. |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5635 | if (E->isEqualityOp()) { |
| 5636 | unsigned comparisonWidth = S.Context.getIntWidth(T); |
| 5637 | IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 5638 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5639 | // We should never be unable to prove that the unsigned operand is |
| 5640 | // non-negative. |
| 5641 | assert(unsignedRange.NonNegative && "unsigned range includes negative?"); |
| 5642 | |
| 5643 | if (unsignedRange.Width < comparisonWidth) |
| 5644 | return; |
| 5645 | } |
| 5646 | |
Douglas Gregor | bfb4a21 | 2012-05-01 01:53:49 +0000 | [diff] [blame] | 5647 | S.DiagRuntimeBehavior(E->getOperatorLoc(), E, |
| 5648 | S.PDiag(diag::warn_mixed_sign_comparison) |
| 5649 | << LHS->getType() << RHS->getType() |
| 5650 | << LHS->getSourceRange() << RHS->getSourceRange()); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 5651 | } |
| 5652 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5653 | /// Analyzes an attempt to assign the given value to a bitfield. |
| 5654 | /// |
| 5655 | /// Returns true if there was something fishy about the attempt. |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 5656 | static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, |
| 5657 | SourceLocation InitLoc) { |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5658 | assert(Bitfield->isBitField()); |
| 5659 | if (Bitfield->isInvalidDecl()) |
| 5660 | return false; |
| 5661 | |
John McCall | deebbcf | 2010-11-11 05:33:51 +0000 | [diff] [blame] | 5662 | // White-list bool bitfields. |
| 5663 | if (Bitfield->getType()->isBooleanType()) |
| 5664 | return false; |
| 5665 | |
Douglas Gregor | 789adec | 2011-02-04 13:09:01 +0000 | [diff] [blame] | 5666 | // Ignore value- or type-dependent expressions. |
| 5667 | if (Bitfield->getBitWidth()->isValueDependent() || |
| 5668 | Bitfield->getBitWidth()->isTypeDependent() || |
| 5669 | Init->isValueDependent() || |
| 5670 | Init->isTypeDependent()) |
| 5671 | return false; |
| 5672 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5673 | Expr *OriginalInit = Init->IgnoreParenImpCasts(); |
| 5674 | |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 5675 | llvm::APSInt Value; |
| 5676 | if (!OriginalInit->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects)) |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5677 | return false; |
| 5678 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5679 | unsigned OriginalWidth = Value.getBitWidth(); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5680 | unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context); |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5681 | |
| 5682 | if (OriginalWidth <= FieldWidth) |
| 5683 | return false; |
| 5684 | |
Eli Friedman | c267a32 | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 5685 | // Compute the value which the bitfield will contain. |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5686 | llvm::APSInt TruncatedValue = Value.trunc(FieldWidth); |
Eli Friedman | c267a32 | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 5687 | TruncatedValue.setIsSigned(Bitfield->getType()->isSignedIntegerType()); |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5688 | |
Eli Friedman | c267a32 | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 5689 | // Check whether the stored value is equal to the original value. |
| 5690 | TruncatedValue = TruncatedValue.extend(OriginalWidth); |
Richard Trieu | c320c74 | 2012-07-23 20:21:35 +0000 | [diff] [blame] | 5691 | if (llvm::APSInt::isSameValue(Value, TruncatedValue)) |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5692 | return false; |
| 5693 | |
Eli Friedman | c267a32 | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 5694 | // Special-case bitfields of width 1: booleans are naturally 0/1, and |
Eli Friedman | e1ffd49 | 2012-02-02 00:40:20 +0000 | [diff] [blame] | 5695 | // therefore don't strictly fit into a signed bitfield of width 1. |
| 5696 | if (FieldWidth == 1 && Value == 1) |
Eli Friedman | c267a32 | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 5697 | return false; |
| 5698 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5699 | std::string PrettyValue = Value.toString(10); |
| 5700 | std::string PrettyTrunc = TruncatedValue.toString(10); |
| 5701 | |
| 5702 | S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant) |
| 5703 | << PrettyValue << PrettyTrunc << OriginalInit->getType() |
| 5704 | << Init->getSourceRange(); |
| 5705 | |
| 5706 | return true; |
| 5707 | } |
| 5708 | |
John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 5709 | /// Analyze the given simple or compound assignment for warning-worthy |
| 5710 | /// operations. |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 5711 | static void AnalyzeAssignment(Sema &S, BinaryOperator *E) { |
John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 5712 | // Just recurse on the LHS. |
| 5713 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 5714 | |
| 5715 | // We want to recurse on the RHS as normal unless we're assigning to |
| 5716 | // a bitfield. |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5717 | if (FieldDecl *Bitfield = E->getLHS()->getSourceBitField()) { |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 5718 | if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(), |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5719 | E->getOperatorLoc())) { |
| 5720 | // Recurse, ignoring any implicit conversions on the RHS. |
| 5721 | return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(), |
| 5722 | E->getOperatorLoc()); |
John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 5723 | } |
| 5724 | } |
| 5725 | |
| 5726 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
| 5727 | } |
| 5728 | |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5729 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 5730 | static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5731 | SourceLocation CContext, unsigned diag, |
| 5732 | bool pruneControlFlow = false) { |
| 5733 | if (pruneControlFlow) { |
| 5734 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
| 5735 | S.PDiag(diag) |
| 5736 | << SourceType << T << E->getSourceRange() |
| 5737 | << SourceRange(CContext)); |
| 5738 | return; |
| 5739 | } |
Douglas Gregor | 364f7db | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 5740 | S.Diag(E->getExprLoc(), diag) |
| 5741 | << SourceType << T << E->getSourceRange() << SourceRange(CContext); |
| 5742 | } |
| 5743 | |
Chandler Carruth | 7f3654f | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 5744 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 5745 | static void DiagnoseImpCast(Sema &S, Expr *E, QualType T, |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5746 | SourceLocation CContext, unsigned diag, |
| 5747 | bool pruneControlFlow = false) { |
| 5748 | DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow); |
Chandler Carruth | 7f3654f | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 5749 | } |
| 5750 | |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 5751 | /// Diagnose an implicit cast from a literal expression. Does not warn when the |
| 5752 | /// cast wouldn't lose information. |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 5753 | void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T, |
| 5754 | SourceLocation CContext) { |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 5755 | // Try to convert the literal exactly to an integer. If we can, don't warn. |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 5756 | bool isExact = false; |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 5757 | const llvm::APFloat &Value = FL->getValue(); |
Jeffrey Yasskin | d0f079d | 2011-07-15 17:03:07 +0000 | [diff] [blame] | 5758 | llvm::APSInt IntegerValue(S.Context.getIntWidth(T), |
| 5759 | T->hasUnsignedIntegerRepresentation()); |
| 5760 | if (Value.convertToInteger(IntegerValue, |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 5761 | llvm::APFloat::rmTowardZero, &isExact) |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 5762 | == llvm::APFloat::opOK && isExact) |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 5763 | return; |
| 5764 | |
Eli Friedman | 0718591 | 2013-08-29 23:44:43 +0000 | [diff] [blame] | 5765 | // FIXME: Force the precision of the source value down so we don't print |
| 5766 | // digits which are usually useless (we don't really care here if we |
| 5767 | // truncate a digit by accident in edge cases). Ideally, APFloat::toString |
| 5768 | // would automatically print the shortest representation, but it's a bit |
| 5769 | // tricky to implement. |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 5770 | SmallString<16> PrettySourceValue; |
Eli Friedman | 0718591 | 2013-08-29 23:44:43 +0000 | [diff] [blame] | 5771 | unsigned precision = llvm::APFloat::semanticsPrecision(Value.getSemantics()); |
| 5772 | precision = (precision * 59 + 195) / 196; |
| 5773 | Value.toString(PrettySourceValue, precision); |
| 5774 | |
David Blaikie | 9b88cc0 | 2012-05-15 17:18:27 +0000 | [diff] [blame] | 5775 | SmallString<16> PrettyTargetValue; |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 5776 | if (T->isSpecificBuiltinType(BuiltinType::Bool)) |
| 5777 | PrettyTargetValue = IntegerValue == 0 ? "false" : "true"; |
| 5778 | else |
David Blaikie | 9b88cc0 | 2012-05-15 17:18:27 +0000 | [diff] [blame] | 5779 | IntegerValue.toString(PrettyTargetValue); |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 5780 | |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 5781 | S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer) |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 5782 | << FL->getType() << T.getUnqualifiedType() << PrettySourceValue |
| 5783 | << PrettyTargetValue << FL->getSourceRange() << SourceRange(CContext); |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 5784 | } |
| 5785 | |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 5786 | std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) { |
| 5787 | if (!Range.Width) return "0"; |
| 5788 | |
| 5789 | llvm::APSInt ValueInRange = Value; |
| 5790 | ValueInRange.setIsSigned(!Range.NonNegative); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5791 | ValueInRange = ValueInRange.trunc(Range.Width); |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 5792 | return ValueInRange.toString(10); |
| 5793 | } |
| 5794 | |
Hans Wennborg | f4ad232 | 2012-08-28 15:44:30 +0000 | [diff] [blame] | 5795 | static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) { |
| 5796 | if (!isa<ImplicitCastExpr>(Ex)) |
| 5797 | return false; |
| 5798 | |
| 5799 | Expr *InnerE = Ex->IgnoreParenImpCasts(); |
| 5800 | const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr(); |
| 5801 | const Type *Source = |
| 5802 | S.Context.getCanonicalType(InnerE->getType()).getTypePtr(); |
| 5803 | if (Target->isDependentType()) |
| 5804 | return false; |
| 5805 | |
| 5806 | const BuiltinType *FloatCandidateBT = |
| 5807 | dyn_cast<BuiltinType>(ToBool ? Source : Target); |
| 5808 | const Type *BoolCandidateType = ToBool ? Target : Source; |
| 5809 | |
| 5810 | return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) && |
| 5811 | FloatCandidateBT && (FloatCandidateBT->isFloatingPoint())); |
| 5812 | } |
| 5813 | |
| 5814 | void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall, |
| 5815 | SourceLocation CC) { |
| 5816 | unsigned NumArgs = TheCall->getNumArgs(); |
| 5817 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 5818 | Expr *CurrA = TheCall->getArg(i); |
| 5819 | if (!IsImplicitBoolFloatConversion(S, CurrA, true)) |
| 5820 | continue; |
| 5821 | |
| 5822 | bool IsSwapped = ((i > 0) && |
| 5823 | IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false)); |
| 5824 | IsSwapped |= ((i < (NumArgs - 1)) && |
| 5825 | IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false)); |
| 5826 | if (IsSwapped) { |
| 5827 | // Warn on this floating-point to bool conversion. |
| 5828 | DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(), |
| 5829 | CurrA->getType(), CC, |
| 5830 | diag::warn_impcast_floating_point_to_bool); |
| 5831 | } |
| 5832 | } |
| 5833 | } |
| 5834 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5835 | void CheckImplicitConversion(Sema &S, Expr *E, QualType T, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5836 | SourceLocation CC, bool *ICContext = nullptr) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5837 | if (E->isTypeDependent() || E->isValueDependent()) return; |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5838 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5839 | const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr(); |
| 5840 | const Type *Target = S.Context.getCanonicalType(T).getTypePtr(); |
| 5841 | if (Source == Target) return; |
| 5842 | if (Target->isDependentType()) return; |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5843 | |
Chandler Carruth | c22845a | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 5844 | // If the conversion context location is invalid don't complain. We also |
| 5845 | // don't want to emit a warning if the issue occurs from the expansion of |
| 5846 | // a system macro. The problem is that 'getSpellingLoc()' is slow, so we |
| 5847 | // delay this check as long as possible. Once we detect we are in that |
| 5848 | // scenario, we just return. |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 5849 | if (CC.isInvalid()) |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5850 | return; |
| 5851 | |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 5852 | // Diagnose implicit casts to bool. |
| 5853 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) { |
| 5854 | if (isa<StringLiteral>(E)) |
| 5855 | // Warn on string literal to bool. Checks for string literals in logical |
Richard Trieu | 955231d | 2014-01-25 01:10:35 +0000 | [diff] [blame] | 5856 | // and expressions, for instance, assert(0 && "error here"), are |
| 5857 | // prevented by a check in AnalyzeImplicitConversions(). |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 5858 | return DiagnoseImpCast(S, E, T, CC, |
| 5859 | diag::warn_impcast_string_literal_to_bool); |
Richard Trieu | 1e632af | 2014-01-28 23:40:26 +0000 | [diff] [blame] | 5860 | if (isa<ObjCStringLiteral>(E) || isa<ObjCArrayLiteral>(E) || |
| 5861 | isa<ObjCDictionaryLiteral>(E) || isa<ObjCBoxedExpr>(E)) { |
| 5862 | // This covers the literal expressions that evaluate to Objective-C |
| 5863 | // objects. |
| 5864 | return DiagnoseImpCast(S, E, T, CC, |
| 5865 | diag::warn_impcast_objective_c_literal_to_bool); |
| 5866 | } |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 5867 | if (Source->isPointerType() || Source->canDecayToPointerType()) { |
| 5868 | // Warn on pointer to bool conversion that is always true. |
| 5869 | S.DiagnoseAlwaysNonNullPointer(E, Expr::NPCK_NotNull, /*IsEqual*/ false, |
| 5870 | SourceRange(CC)); |
Lang Hames | df5c121 | 2011-12-05 20:49:50 +0000 | [diff] [blame] | 5871 | } |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 5872 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5873 | |
| 5874 | // Strip vector types. |
| 5875 | if (isa<VectorType>(Source)) { |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 5876 | if (!isa<VectorType>(Target)) { |
Matt Beaumont-Gay | 7a57ada | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 5877 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 5878 | return; |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5879 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar); |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 5880 | } |
Chris Lattner | ee7286f | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 5881 | |
| 5882 | // If the vector cast is cast between two vectors of the same size, it is |
| 5883 | // a bitcast, not a conversion. |
| 5884 | if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target)) |
| 5885 | return; |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5886 | |
| 5887 | Source = cast<VectorType>(Source)->getElementType().getTypePtr(); |
| 5888 | Target = cast<VectorType>(Target)->getElementType().getTypePtr(); |
| 5889 | } |
Stephen Canon | 3ba640d | 2014-04-03 10:33:25 +0000 | [diff] [blame] | 5890 | if (auto VecTy = dyn_cast<VectorType>(Target)) |
| 5891 | Target = VecTy->getElementType().getTypePtr(); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5892 | |
| 5893 | // Strip complex types. |
| 5894 | if (isa<ComplexType>(Source)) { |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 5895 | if (!isa<ComplexType>(Target)) { |
Matt Beaumont-Gay | 7a57ada | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 5896 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 5897 | return; |
| 5898 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5899 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar); |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 5900 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5901 | |
| 5902 | Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); |
| 5903 | Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); |
| 5904 | } |
| 5905 | |
| 5906 | const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); |
| 5907 | const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); |
| 5908 | |
| 5909 | // If the source is floating point... |
| 5910 | if (SourceBT && SourceBT->isFloatingPoint()) { |
| 5911 | // ...and the target is floating point... |
| 5912 | if (TargetBT && TargetBT->isFloatingPoint()) { |
| 5913 | // ...then warn if we're dropping FP rank. |
| 5914 | |
| 5915 | // Builtin FP kinds are ordered by increasing FP rank. |
| 5916 | if (SourceBT->getKind() > TargetBT->getKind()) { |
| 5917 | // Don't warn about float constants that are precisely |
| 5918 | // representable in the target type. |
| 5919 | Expr::EvalResult result; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5920 | if (E->EvaluateAsRValue(result, S.Context)) { |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5921 | // Value might be a float, a float vector, or a float complex. |
| 5922 | if (IsSameFloatAfterCast(result.Val, |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5923 | S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)), |
| 5924 | S.Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5925 | return; |
| 5926 | } |
| 5927 | |
Matt Beaumont-Gay | 7a57ada | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 5928 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 5929 | return; |
| 5930 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5931 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5932 | } |
| 5933 | return; |
| 5934 | } |
| 5935 | |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 5936 | // If the target is integral, always warn. |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 5937 | if (TargetBT && TargetBT->isInteger()) { |
Matt Beaumont-Gay | 7a57ada | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 5938 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 5939 | return; |
| 5940 | |
Chandler Carruth | 22c7a79 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 5941 | Expr *InnerE = E->IgnoreParenImpCasts(); |
Matt Beaumont-Gay | 042ce8e | 2011-09-08 22:30:47 +0000 | [diff] [blame] | 5942 | // We also want to warn on, e.g., "int i = -1.234" |
| 5943 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE)) |
| 5944 | if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus) |
| 5945 | InnerE = UOp->getSubExpr()->IgnoreParenImpCasts(); |
| 5946 | |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 5947 | if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) { |
| 5948 | DiagnoseFloatingLiteralImpCast(S, FL, T, CC); |
Chandler Carruth | 22c7a79 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 5949 | } else { |
| 5950 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer); |
| 5951 | } |
| 5952 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5953 | |
Hans Wennborg | f4ad232 | 2012-08-28 15:44:30 +0000 | [diff] [blame] | 5954 | // If the target is bool, warn if expr is a function or method call. |
| 5955 | if (Target->isSpecificBuiltinType(BuiltinType::Bool) && |
| 5956 | isa<CallExpr>(E)) { |
| 5957 | // Check last argument of function call to see if it is an |
| 5958 | // implicit cast from a type matching the type the result |
| 5959 | // is being cast to. |
| 5960 | CallExpr *CEx = cast<CallExpr>(E); |
| 5961 | unsigned NumArgs = CEx->getNumArgs(); |
| 5962 | if (NumArgs > 0) { |
| 5963 | Expr *LastA = CEx->getArg(NumArgs - 1); |
| 5964 | Expr *InnerE = LastA->IgnoreParenImpCasts(); |
| 5965 | const Type *InnerType = |
| 5966 | S.Context.getCanonicalType(InnerE->getType()).getTypePtr(); |
| 5967 | if (isa<ImplicitCastExpr>(LastA) && (InnerType == Target)) { |
| 5968 | // Warn on this floating-point to bool conversion |
| 5969 | DiagnoseImpCast(S, E, T, CC, |
| 5970 | diag::warn_impcast_floating_point_to_bool); |
| 5971 | } |
| 5972 | } |
| 5973 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5974 | return; |
| 5975 | } |
| 5976 | |
Richard Trieu | beaf345 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 5977 | if ((E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) |
David Blaikie | 9366d2b | 2012-06-19 21:19:06 +0000 | [diff] [blame] | 5978 | == Expr::NPCK_GNUNull) && !Target->isAnyPointerType() |
David Blaikie | a1edff0 | 2012-11-08 00:41:20 +0000 | [diff] [blame] | 5979 | && !Target->isBlockPointerType() && !Target->isMemberPointerType() |
David Blaikie | bcd4b55 | 2013-02-16 00:56:22 +0000 | [diff] [blame] | 5980 | && Target->isScalarType() && !Target->isNullPtrType()) { |
David Blaikie | ae12b18 | 2012-03-16 20:30:12 +0000 | [diff] [blame] | 5981 | SourceLocation Loc = E->getSourceRange().getBegin(); |
| 5982 | if (Loc.isMacroID()) |
| 5983 | Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first; |
David Blaikie | 18e9ac7 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 5984 | if (!Loc.isMacroID() || CC.isMacroID()) |
| 5985 | S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer) |
| 5986 | << T << clang::SourceRange(CC) |
Richard Smith | f7ec86a | 2013-09-20 00:27:40 +0000 | [diff] [blame] | 5987 | << FixItHint::CreateReplacement(Loc, |
| 5988 | S.getFixItZeroLiteralForType(T, Loc)); |
Richard Trieu | beaf345 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 5989 | } |
| 5990 | |
David Blaikie | 9366d2b | 2012-06-19 21:19:06 +0000 | [diff] [blame] | 5991 | if (!Source->isIntegerType() || !Target->isIntegerType()) |
| 5992 | return; |
| 5993 | |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 5994 | // TODO: remove this early return once the false positives for constant->bool |
| 5995 | // in templates, macros, etc, are reduced or removed. |
| 5996 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) |
| 5997 | return; |
| 5998 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5999 | IntRange SourceRange = GetExprRange(S.Context, E); |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 6000 | IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6001 | |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 6002 | if (SourceRange.Width > TargetRange.Width) { |
Sam Panzer | 6fffec6 | 2013-03-28 19:07:11 +0000 | [diff] [blame] | 6003 | // If the source is a constant, use a default-on diagnostic. |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 6004 | // TODO: this should happen for bitfield stores, too. |
| 6005 | llvm::APSInt Value(32); |
| 6006 | if (E->isIntegerConstantExpr(Value, S.Context)) { |
| 6007 | if (S.SourceMgr.isInSystemMacro(CC)) |
| 6008 | return; |
| 6009 | |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 6010 | std::string PrettySourceValue = Value.toString(10); |
| 6011 | std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 6012 | |
Ted Kremenek | 33ba995 | 2011-10-22 02:37:33 +0000 | [diff] [blame] | 6013 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
| 6014 | S.PDiag(diag::warn_impcast_integer_precision_constant) |
| 6015 | << PrettySourceValue << PrettyTargetValue |
| 6016 | << E->getType() << T << E->getSourceRange() |
| 6017 | << clang::SourceRange(CC)); |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 6018 | return; |
| 6019 | } |
| 6020 | |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 6021 | // People want to build with -Wshorten-64-to-32 and not -Wconversion. |
| 6022 | if (S.SourceMgr.isInSystemMacro(CC)) |
| 6023 | return; |
| 6024 | |
David Blaikie | 9455da0 | 2012-04-12 22:40:54 +0000 | [diff] [blame] | 6025 | if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64) |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 6026 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32, |
| 6027 | /* pruneControlFlow */ true); |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6028 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6029 | } |
| 6030 | |
| 6031 | if ((TargetRange.NonNegative && !SourceRange.NonNegative) || |
| 6032 | (!TargetRange.NonNegative && SourceRange.NonNegative && |
| 6033 | SourceRange.Width == TargetRange.Width)) { |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6034 | |
Matt Beaumont-Gay | 7a57ada | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 6035 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6036 | return; |
| 6037 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6038 | unsigned DiagID = diag::warn_impcast_integer_sign; |
| 6039 | |
| 6040 | // Traditionally, gcc has warned about this under -Wsign-compare. |
| 6041 | // We also want to warn about it in -Wconversion. |
| 6042 | // So if -Wconversion is off, use a completely identical diagnostic |
| 6043 | // in the sign-compare group. |
| 6044 | // The conditional-checking code will |
| 6045 | if (ICContext) { |
| 6046 | DiagID = diag::warn_impcast_integer_sign_conditional; |
| 6047 | *ICContext = true; |
| 6048 | } |
| 6049 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6050 | return DiagnoseImpCast(S, E, T, CC, DiagID); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6051 | } |
| 6052 | |
Douglas Gregor | a78f193 | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 6053 | // Diagnose conversions between different enumeration types. |
Douglas Gregor | 364f7db | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 6054 | // In C, we pretend that the type of an EnumConstantDecl is its enumeration |
| 6055 | // type, to give us better diagnostics. |
| 6056 | QualType SourceType = E->getType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6057 | if (!S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 364f7db | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 6058 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 6059 | if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 6060 | EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext()); |
| 6061 | SourceType = S.Context.getTypeDeclType(Enum); |
| 6062 | Source = S.Context.getCanonicalType(SourceType).getTypePtr(); |
| 6063 | } |
| 6064 | } |
| 6065 | |
Douglas Gregor | a78f193 | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 6066 | if (const EnumType *SourceEnum = Source->getAs<EnumType>()) |
| 6067 | if (const EnumType *TargetEnum = Target->getAs<EnumType>()) |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 6068 | if (SourceEnum->getDecl()->hasNameForLinkage() && |
| 6069 | TargetEnum->getDecl()->hasNameForLinkage() && |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6070 | SourceEnum != TargetEnum) { |
Matt Beaumont-Gay | 7a57ada | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 6071 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6072 | return; |
| 6073 | |
Douglas Gregor | 364f7db | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 6074 | return DiagnoseImpCast(S, E, SourceType, T, CC, |
Douglas Gregor | a78f193 | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 6075 | diag::warn_impcast_different_enum_types); |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6076 | } |
Douglas Gregor | a78f193 | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 6077 | |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6078 | return; |
| 6079 | } |
| 6080 | |
David Blaikie | 18e9ac7 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 6081 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, |
| 6082 | SourceLocation CC, QualType T); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6083 | |
| 6084 | void CheckConditionalOperand(Sema &S, Expr *E, QualType T, |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6085 | SourceLocation CC, bool &ICContext) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6086 | E = E->IgnoreParenImpCasts(); |
| 6087 | |
| 6088 | if (isa<ConditionalOperator>(E)) |
David Blaikie | 18e9ac7 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 6089 | return CheckConditionalOperator(S, cast<ConditionalOperator>(E), CC, T); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6090 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6091 | AnalyzeImplicitConversions(S, E, CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6092 | if (E->getType() != T) |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6093 | return CheckImplicitConversion(S, E, T, CC, &ICContext); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6094 | return; |
| 6095 | } |
| 6096 | |
David Blaikie | 18e9ac7 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 6097 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, |
| 6098 | SourceLocation CC, QualType T) { |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6099 | AnalyzeImplicitConversions(S, E->getCond(), CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6100 | |
| 6101 | bool Suspicious = false; |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6102 | CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious); |
| 6103 | CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6104 | |
| 6105 | // If -Wconversion would have warned about either of the candidates |
| 6106 | // for a signedness conversion to the context type... |
| 6107 | if (!Suspicious) return; |
| 6108 | |
| 6109 | // ...but it's currently ignored... |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 6110 | if (!S.Diags.isIgnored(diag::warn_impcast_integer_sign_conditional, CC)) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6111 | return; |
| 6112 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6113 | // ...then check whether it would have warned about either of the |
| 6114 | // candidates for a signedness conversion to the condition type. |
Richard Trieu | bb43dec | 2011-07-21 02:46:28 +0000 | [diff] [blame] | 6115 | if (E->getType() == T) return; |
| 6116 | |
| 6117 | Suspicious = false; |
| 6118 | CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(), |
| 6119 | E->getType(), CC, &Suspicious); |
| 6120 | if (!Suspicious) |
| 6121 | CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(), |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6122 | E->getType(), CC, &Suspicious); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6123 | } |
| 6124 | |
| 6125 | /// AnalyzeImplicitConversions - Find and report any interesting |
| 6126 | /// implicit conversions in the given expression. There are a couple |
| 6127 | /// of competing diagnostics here, -Wconversion and -Wsign-compare. |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6128 | void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) { |
Fariborz Jahanian | 148c8c8 | 2014-04-07 16:32:54 +0000 | [diff] [blame] | 6129 | QualType T = OrigE->getType(); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6130 | Expr *E = OrigE->IgnoreParenImpCasts(); |
| 6131 | |
Douglas Gregor | 6e8da6a | 2011-10-10 17:38:18 +0000 | [diff] [blame] | 6132 | if (E->isTypeDependent() || E->isValueDependent()) |
| 6133 | return; |
Fariborz Jahanian | ad95da7 | 2014-04-04 19:33:39 +0000 | [diff] [blame] | 6134 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6135 | // For conditional operators, we analyze the arguments as if they |
| 6136 | // were being fed directly into the output. |
| 6137 | if (isa<ConditionalOperator>(E)) { |
| 6138 | ConditionalOperator *CO = cast<ConditionalOperator>(E); |
David Blaikie | 18e9ac7 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 6139 | CheckConditionalOperator(S, CO, CC, T); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6140 | return; |
| 6141 | } |
| 6142 | |
Hans Wennborg | f4ad232 | 2012-08-28 15:44:30 +0000 | [diff] [blame] | 6143 | // Check implicit argument conversions for function calls. |
| 6144 | if (CallExpr *Call = dyn_cast<CallExpr>(E)) |
| 6145 | CheckImplicitArgumentConversions(S, Call, CC); |
| 6146 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6147 | // Go ahead and check any implicit conversions we might have skipped. |
| 6148 | // The non-canonical typecheck is just an optimization; |
| 6149 | // CheckImplicitConversion will filter out dead implicit conversions. |
| 6150 | if (E->getType() != T) |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6151 | CheckImplicitConversion(S, E, T, CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6152 | |
| 6153 | // Now continue drilling into this expression. |
Fariborz Jahanian | 2cb4a95 | 2013-05-15 19:03:04 +0000 | [diff] [blame] | 6154 | |
| 6155 | if (PseudoObjectExpr * POE = dyn_cast<PseudoObjectExpr>(E)) { |
Fariborz Jahanian | 0b11ef2 | 2013-05-15 22:25:03 +0000 | [diff] [blame] | 6156 | if (POE->getResultExpr()) |
| 6157 | E = POE->getResultExpr(); |
Fariborz Jahanian | 2cb4a95 | 2013-05-15 19:03:04 +0000 | [diff] [blame] | 6158 | } |
| 6159 | |
Fariborz Jahanian | 0b11ef2 | 2013-05-15 22:25:03 +0000 | [diff] [blame] | 6160 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) |
| 6161 | return AnalyzeImplicitConversions(S, OVE->getSourceExpr(), CC); |
| 6162 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6163 | // Skip past explicit casts. |
| 6164 | if (isa<ExplicitCastExpr>(E)) { |
| 6165 | E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts(); |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6166 | return AnalyzeImplicitConversions(S, E, CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6167 | } |
| 6168 | |
John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 6169 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 6170 | // Do a somewhat different check with comparison operators. |
| 6171 | if (BO->isComparisonOp()) |
| 6172 | return AnalyzeComparison(S, BO); |
| 6173 | |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 6174 | // And with simple assignments. |
| 6175 | if (BO->getOpcode() == BO_Assign) |
John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 6176 | return AnalyzeAssignment(S, BO); |
| 6177 | } |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6178 | |
| 6179 | // These break the otherwise-useful invariant below. Fortunately, |
| 6180 | // we don't really need to recurse into them, because any internal |
| 6181 | // expressions should have been analyzed already when they were |
| 6182 | // built into statements. |
| 6183 | if (isa<StmtExpr>(E)) return; |
| 6184 | |
| 6185 | // Don't descend into unevaluated contexts. |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6186 | if (isa<UnaryExprOrTypeTraitExpr>(E)) return; |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6187 | |
| 6188 | // Now just recurse over the expression's children. |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6189 | CC = E->getExprLoc(); |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 6190 | BinaryOperator *BO = dyn_cast<BinaryOperator>(E); |
Richard Trieu | 955231d | 2014-01-25 01:10:35 +0000 | [diff] [blame] | 6191 | bool IsLogicalAndOperator = BO && BO->getOpcode() == BO_LAnd; |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 6192 | for (Stmt::child_range I = E->children(); I; ++I) { |
Douglas Gregor | 7345626 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 6193 | Expr *ChildExpr = dyn_cast_or_null<Expr>(*I); |
Douglas Gregor | 8c50e7c | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 6194 | if (!ChildExpr) |
| 6195 | continue; |
| 6196 | |
Richard Trieu | 955231d | 2014-01-25 01:10:35 +0000 | [diff] [blame] | 6197 | if (IsLogicalAndOperator && |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 6198 | isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts())) |
Richard Trieu | 955231d | 2014-01-25 01:10:35 +0000 | [diff] [blame] | 6199 | // Ignore checking string literals that are in logical and operators. |
| 6200 | // This is a common pattern for asserts. |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 6201 | continue; |
| 6202 | AnalyzeImplicitConversions(S, ChildExpr, CC); |
| 6203 | } |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6204 | } |
| 6205 | |
| 6206 | } // end anonymous namespace |
| 6207 | |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 6208 | enum { |
| 6209 | AddressOf, |
| 6210 | FunctionPointer, |
| 6211 | ArrayPointer |
| 6212 | }; |
| 6213 | |
Richard Trieu | c1888e0 | 2014-06-28 23:25:37 +0000 | [diff] [blame] | 6214 | // Helper function for Sema::DiagnoseAlwaysNonNullPointer. |
| 6215 | // Returns true when emitting a warning about taking the address of a reference. |
| 6216 | static bool CheckForReference(Sema &SemaRef, const Expr *E, |
| 6217 | PartialDiagnostic PD) { |
| 6218 | E = E->IgnoreParenImpCasts(); |
| 6219 | |
| 6220 | const FunctionDecl *FD = nullptr; |
| 6221 | |
| 6222 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 6223 | if (!DRE->getDecl()->getType()->isReferenceType()) |
| 6224 | return false; |
| 6225 | } else if (const MemberExpr *M = dyn_cast<MemberExpr>(E)) { |
| 6226 | if (!M->getMemberDecl()->getType()->isReferenceType()) |
| 6227 | return false; |
| 6228 | } else if (const CallExpr *Call = dyn_cast<CallExpr>(E)) { |
| 6229 | if (!Call->getCallReturnType()->isReferenceType()) |
| 6230 | return false; |
| 6231 | FD = Call->getDirectCallee(); |
| 6232 | } else { |
| 6233 | return false; |
| 6234 | } |
| 6235 | |
| 6236 | SemaRef.Diag(E->getExprLoc(), PD); |
| 6237 | |
| 6238 | // If possible, point to location of function. |
| 6239 | if (FD) { |
| 6240 | SemaRef.Diag(FD->getLocation(), diag::note_reference_is_return_value) << FD; |
| 6241 | } |
| 6242 | |
| 6243 | return true; |
| 6244 | } |
| 6245 | |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 6246 | /// \brief Diagnose pointers that are always non-null. |
| 6247 | /// \param E the expression containing the pointer |
| 6248 | /// \param NullKind NPCK_NotNull if E is a cast to bool, otherwise, E is |
| 6249 | /// compared to a null pointer |
| 6250 | /// \param IsEqual True when the comparison is equal to a null pointer |
| 6251 | /// \param Range Extra SourceRange to highlight in the diagnostic |
| 6252 | void Sema::DiagnoseAlwaysNonNullPointer(Expr *E, |
| 6253 | Expr::NullPointerConstantKind NullKind, |
| 6254 | bool IsEqual, SourceRange Range) { |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 6255 | if (!E) |
| 6256 | return; |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 6257 | |
| 6258 | // Don't warn inside macros. |
| 6259 | if (E->getExprLoc().isMacroID()) |
| 6260 | return; |
| 6261 | E = E->IgnoreImpCasts(); |
| 6262 | |
| 6263 | const bool IsCompare = NullKind != Expr::NPCK_NotNull; |
| 6264 | |
Richard Trieu | f743275 | 2014-06-06 21:39:26 +0000 | [diff] [blame] | 6265 | if (isa<CXXThisExpr>(E)) { |
| 6266 | unsigned DiagID = IsCompare ? diag::warn_this_null_compare |
| 6267 | : diag::warn_this_bool_conversion; |
| 6268 | Diag(E->getExprLoc(), DiagID) << E->getSourceRange() << Range << IsEqual; |
| 6269 | return; |
| 6270 | } |
| 6271 | |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 6272 | bool IsAddressOf = false; |
| 6273 | |
| 6274 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 6275 | if (UO->getOpcode() != UO_AddrOf) |
| 6276 | return; |
| 6277 | IsAddressOf = true; |
| 6278 | E = UO->getSubExpr(); |
| 6279 | } |
| 6280 | |
Richard Trieu | c1888e0 | 2014-06-28 23:25:37 +0000 | [diff] [blame] | 6281 | if (IsAddressOf) { |
| 6282 | unsigned DiagID = IsCompare |
| 6283 | ? diag::warn_address_of_reference_null_compare |
| 6284 | : diag::warn_address_of_reference_bool_conversion; |
| 6285 | PartialDiagnostic PD = PDiag(DiagID) << E->getSourceRange() << Range |
| 6286 | << IsEqual; |
| 6287 | if (CheckForReference(*this, E, PD)) { |
| 6288 | return; |
| 6289 | } |
| 6290 | } |
| 6291 | |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 6292 | // Expect to find a single Decl. Skip anything more complicated. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6293 | ValueDecl *D = nullptr; |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 6294 | if (DeclRefExpr *R = dyn_cast<DeclRefExpr>(E)) { |
| 6295 | D = R->getDecl(); |
| 6296 | } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) { |
| 6297 | D = M->getMemberDecl(); |
| 6298 | } |
| 6299 | |
| 6300 | // Weak Decls can be null. |
| 6301 | if (!D || D->isWeak()) |
| 6302 | return; |
| 6303 | |
| 6304 | QualType T = D->getType(); |
| 6305 | const bool IsArray = T->isArrayType(); |
| 6306 | const bool IsFunction = T->isFunctionType(); |
| 6307 | |
Richard Trieu | c1888e0 | 2014-06-28 23:25:37 +0000 | [diff] [blame] | 6308 | // Address of function is used to silence the function warning. |
| 6309 | if (IsAddressOf && IsFunction) { |
| 6310 | return; |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 6311 | } |
| 6312 | |
| 6313 | // Found nothing. |
| 6314 | if (!IsAddressOf && !IsFunction && !IsArray) |
| 6315 | return; |
| 6316 | |
| 6317 | // Pretty print the expression for the diagnostic. |
| 6318 | std::string Str; |
| 6319 | llvm::raw_string_ostream S(Str); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6320 | E->printPretty(S, nullptr, getPrintingPolicy()); |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 6321 | |
| 6322 | unsigned DiagID = IsCompare ? diag::warn_null_pointer_compare |
| 6323 | : diag::warn_impcast_pointer_to_bool; |
| 6324 | unsigned DiagType; |
| 6325 | if (IsAddressOf) |
| 6326 | DiagType = AddressOf; |
| 6327 | else if (IsFunction) |
| 6328 | DiagType = FunctionPointer; |
| 6329 | else if (IsArray) |
| 6330 | DiagType = ArrayPointer; |
| 6331 | else |
| 6332 | llvm_unreachable("Could not determine diagnostic."); |
| 6333 | Diag(E->getExprLoc(), DiagID) << DiagType << S.str() << E->getSourceRange() |
| 6334 | << Range << IsEqual; |
| 6335 | |
| 6336 | if (!IsFunction) |
| 6337 | return; |
| 6338 | |
| 6339 | // Suggest '&' to silence the function warning. |
| 6340 | Diag(E->getExprLoc(), diag::note_function_warning_silence) |
| 6341 | << FixItHint::CreateInsertion(E->getLocStart(), "&"); |
| 6342 | |
| 6343 | // Check to see if '()' fixit should be emitted. |
| 6344 | QualType ReturnType; |
| 6345 | UnresolvedSet<4> NonTemplateOverloads; |
| 6346 | tryExprAsCall(*E, ReturnType, NonTemplateOverloads); |
| 6347 | if (ReturnType.isNull()) |
| 6348 | return; |
| 6349 | |
| 6350 | if (IsCompare) { |
| 6351 | // There are two cases here. If there is null constant, the only suggest |
| 6352 | // for a pointer return type. If the null is 0, then suggest if the return |
| 6353 | // type is a pointer or an integer type. |
| 6354 | if (!ReturnType->isPointerType()) { |
| 6355 | if (NullKind == Expr::NPCK_ZeroExpression || |
| 6356 | NullKind == Expr::NPCK_ZeroLiteral) { |
| 6357 | if (!ReturnType->isIntegerType()) |
| 6358 | return; |
| 6359 | } else { |
| 6360 | return; |
| 6361 | } |
| 6362 | } |
| 6363 | } else { // !IsCompare |
| 6364 | // For function to bool, only suggest if the function pointer has bool |
| 6365 | // return type. |
| 6366 | if (!ReturnType->isSpecificBuiltinType(BuiltinType::Bool)) |
| 6367 | return; |
| 6368 | } |
| 6369 | Diag(E->getExprLoc(), diag::note_function_to_function_call) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 6370 | << FixItHint::CreateInsertion(getLocForEndOfToken(E->getLocEnd()), "()"); |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 6371 | } |
| 6372 | |
| 6373 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6374 | /// Diagnoses "dangerous" implicit conversions within the given |
| 6375 | /// expression (which is a full expression). Implements -Wconversion |
| 6376 | /// and -Wsign-compare. |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6377 | /// |
| 6378 | /// \param CC the "context" location of the implicit conversion, i.e. |
| 6379 | /// the most location of the syntactic entity requiring the implicit |
| 6380 | /// conversion |
| 6381 | void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6382 | // Don't diagnose in unevaluated contexts. |
David Blaikie | 131fcb4 | 2012-08-06 22:47:24 +0000 | [diff] [blame] | 6383 | if (isUnevaluatedContext()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6384 | return; |
| 6385 | |
| 6386 | // Don't diagnose for value- or type-dependent expressions. |
| 6387 | if (E->isTypeDependent() || E->isValueDependent()) |
| 6388 | return; |
| 6389 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6390 | // Check for array bounds violations in cases where the check isn't triggered |
| 6391 | // elsewhere for other Expr types (like BinaryOperators), e.g. when an |
| 6392 | // ArraySubscriptExpr is on the RHS of a variable initialization. |
| 6393 | CheckArrayAccess(E); |
| 6394 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6395 | // This is not the right CC for (e.g.) a variable initialization. |
| 6396 | AnalyzeImplicitConversions(*this, E, CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6397 | } |
| 6398 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 6399 | /// Diagnose when expression is an integer constant expression and its evaluation |
| 6400 | /// results in integer overflow |
| 6401 | void Sema::CheckForIntOverflow (Expr *E) { |
Richard Smith | e9ff770 | 2013-11-05 22:23:30 +0000 | [diff] [blame] | 6402 | if (isa<BinaryOperator>(E->IgnoreParens())) |
| 6403 | E->EvaluateForOverflow(Context); |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 6404 | } |
| 6405 | |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6406 | namespace { |
| 6407 | /// \brief Visitor for expressions which looks for unsequenced operations on the |
| 6408 | /// same object. |
| 6409 | class SequenceChecker : public EvaluatedExprVisitor<SequenceChecker> { |
Richard Smith | e3dbfe0 | 2013-06-30 10:40:20 +0000 | [diff] [blame] | 6410 | typedef EvaluatedExprVisitor<SequenceChecker> Base; |
| 6411 | |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6412 | /// \brief A tree of sequenced regions within an expression. Two regions are |
| 6413 | /// unsequenced if one is an ancestor or a descendent of the other. When we |
| 6414 | /// finish processing an expression with sequencing, such as a comma |
| 6415 | /// expression, we fold its tree nodes into its parent, since they are |
| 6416 | /// unsequenced with respect to nodes we will visit later. |
| 6417 | class SequenceTree { |
| 6418 | struct Value { |
| 6419 | explicit Value(unsigned Parent) : Parent(Parent), Merged(false) {} |
| 6420 | unsigned Parent : 31; |
| 6421 | bool Merged : 1; |
| 6422 | }; |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 6423 | SmallVector<Value, 8> Values; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6424 | |
| 6425 | public: |
| 6426 | /// \brief A region within an expression which may be sequenced with respect |
| 6427 | /// to some other region. |
| 6428 | class Seq { |
| 6429 | explicit Seq(unsigned N) : Index(N) {} |
| 6430 | unsigned Index; |
| 6431 | friend class SequenceTree; |
| 6432 | public: |
| 6433 | Seq() : Index(0) {} |
| 6434 | }; |
| 6435 | |
| 6436 | SequenceTree() { Values.push_back(Value(0)); } |
| 6437 | Seq root() const { return Seq(0); } |
| 6438 | |
| 6439 | /// \brief Create a new sequence of operations, which is an unsequenced |
| 6440 | /// subset of \p Parent. This sequence of operations is sequenced with |
| 6441 | /// respect to other children of \p Parent. |
| 6442 | Seq allocate(Seq Parent) { |
| 6443 | Values.push_back(Value(Parent.Index)); |
| 6444 | return Seq(Values.size() - 1); |
| 6445 | } |
| 6446 | |
| 6447 | /// \brief Merge a sequence of operations into its parent. |
| 6448 | void merge(Seq S) { |
| 6449 | Values[S.Index].Merged = true; |
| 6450 | } |
| 6451 | |
| 6452 | /// \brief Determine whether two operations are unsequenced. This operation |
| 6453 | /// is asymmetric: \p Cur should be the more recent sequence, and \p Old |
| 6454 | /// should have been merged into its parent as appropriate. |
| 6455 | bool isUnsequenced(Seq Cur, Seq Old) { |
| 6456 | unsigned C = representative(Cur.Index); |
| 6457 | unsigned Target = representative(Old.Index); |
| 6458 | while (C >= Target) { |
| 6459 | if (C == Target) |
| 6460 | return true; |
| 6461 | C = Values[C].Parent; |
| 6462 | } |
| 6463 | return false; |
| 6464 | } |
| 6465 | |
| 6466 | private: |
| 6467 | /// \brief Pick a representative for a sequence. |
| 6468 | unsigned representative(unsigned K) { |
| 6469 | if (Values[K].Merged) |
| 6470 | // Perform path compression as we go. |
| 6471 | return Values[K].Parent = representative(Values[K].Parent); |
| 6472 | return K; |
| 6473 | } |
| 6474 | }; |
| 6475 | |
| 6476 | /// An object for which we can track unsequenced uses. |
| 6477 | typedef NamedDecl *Object; |
| 6478 | |
| 6479 | /// Different flavors of object usage which we track. We only track the |
| 6480 | /// least-sequenced usage of each kind. |
| 6481 | enum UsageKind { |
| 6482 | /// A read of an object. Multiple unsequenced reads are OK. |
| 6483 | UK_Use, |
| 6484 | /// A modification of an object which is sequenced before the value |
Richard Smith | 83e37bee | 2013-06-26 23:16:51 +0000 | [diff] [blame] | 6485 | /// computation of the expression, such as ++n in C++. |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6486 | UK_ModAsValue, |
| 6487 | /// A modification of an object which is not sequenced before the value |
| 6488 | /// computation of the expression, such as n++. |
| 6489 | UK_ModAsSideEffect, |
| 6490 | |
| 6491 | UK_Count = UK_ModAsSideEffect + 1 |
| 6492 | }; |
| 6493 | |
| 6494 | struct Usage { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6495 | Usage() : Use(nullptr), Seq() {} |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6496 | Expr *Use; |
| 6497 | SequenceTree::Seq Seq; |
| 6498 | }; |
| 6499 | |
| 6500 | struct UsageInfo { |
| 6501 | UsageInfo() : Diagnosed(false) {} |
| 6502 | Usage Uses[UK_Count]; |
| 6503 | /// Have we issued a diagnostic for this variable already? |
| 6504 | bool Diagnosed; |
| 6505 | }; |
| 6506 | typedef llvm::SmallDenseMap<Object, UsageInfo, 16> UsageInfoMap; |
| 6507 | |
| 6508 | Sema &SemaRef; |
| 6509 | /// Sequenced regions within the expression. |
| 6510 | SequenceTree Tree; |
| 6511 | /// Declaration modifications and references which we have seen. |
| 6512 | UsageInfoMap UsageMap; |
| 6513 | /// The region we are currently within. |
| 6514 | SequenceTree::Seq Region; |
| 6515 | /// Filled in with declarations which were modified as a side-effect |
| 6516 | /// (that is, post-increment operations). |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 6517 | SmallVectorImpl<std::pair<Object, Usage> > *ModAsSideEffect; |
Richard Smith | d33f520 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 6518 | /// Expressions to check later. We defer checking these to reduce |
| 6519 | /// stack usage. |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 6520 | SmallVectorImpl<Expr *> &WorkList; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6521 | |
| 6522 | /// RAII object wrapping the visitation of a sequenced subexpression of an |
| 6523 | /// expression. At the end of this process, the side-effects of the evaluation |
| 6524 | /// become sequenced with respect to the value computation of the result, so |
| 6525 | /// we downgrade any UK_ModAsSideEffect within the evaluation to |
| 6526 | /// UK_ModAsValue. |
| 6527 | struct SequencedSubexpression { |
| 6528 | SequencedSubexpression(SequenceChecker &Self) |
| 6529 | : Self(Self), OldModAsSideEffect(Self.ModAsSideEffect) { |
| 6530 | Self.ModAsSideEffect = &ModAsSideEffect; |
| 6531 | } |
| 6532 | ~SequencedSubexpression() { |
| 6533 | for (unsigned I = 0, E = ModAsSideEffect.size(); I != E; ++I) { |
| 6534 | UsageInfo &U = Self.UsageMap[ModAsSideEffect[I].first]; |
| 6535 | U.Uses[UK_ModAsSideEffect] = ModAsSideEffect[I].second; |
| 6536 | Self.addUsage(U, ModAsSideEffect[I].first, |
| 6537 | ModAsSideEffect[I].second.Use, UK_ModAsValue); |
| 6538 | } |
| 6539 | Self.ModAsSideEffect = OldModAsSideEffect; |
| 6540 | } |
| 6541 | |
| 6542 | SequenceChecker &Self; |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 6543 | SmallVector<std::pair<Object, Usage>, 4> ModAsSideEffect; |
| 6544 | SmallVectorImpl<std::pair<Object, Usage> > *OldModAsSideEffect; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6545 | }; |
| 6546 | |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 6547 | /// RAII object wrapping the visitation of a subexpression which we might |
| 6548 | /// choose to evaluate as a constant. If any subexpression is evaluated and |
| 6549 | /// found to be non-constant, this allows us to suppress the evaluation of |
| 6550 | /// the outer expression. |
| 6551 | class EvaluationTracker { |
| 6552 | public: |
| 6553 | EvaluationTracker(SequenceChecker &Self) |
| 6554 | : Self(Self), Prev(Self.EvalTracker), EvalOK(true) { |
| 6555 | Self.EvalTracker = this; |
| 6556 | } |
| 6557 | ~EvaluationTracker() { |
| 6558 | Self.EvalTracker = Prev; |
| 6559 | if (Prev) |
| 6560 | Prev->EvalOK &= EvalOK; |
| 6561 | } |
| 6562 | |
| 6563 | bool evaluate(const Expr *E, bool &Result) { |
| 6564 | if (!EvalOK || E->isValueDependent()) |
| 6565 | return false; |
| 6566 | EvalOK = E->EvaluateAsBooleanCondition(Result, Self.SemaRef.Context); |
| 6567 | return EvalOK; |
| 6568 | } |
| 6569 | |
| 6570 | private: |
| 6571 | SequenceChecker &Self; |
| 6572 | EvaluationTracker *Prev; |
| 6573 | bool EvalOK; |
| 6574 | } *EvalTracker; |
| 6575 | |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6576 | /// \brief Find the object which is produced by the specified expression, |
| 6577 | /// if any. |
| 6578 | Object getObject(Expr *E, bool Mod) const { |
| 6579 | E = E->IgnoreParenCasts(); |
| 6580 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 6581 | if (Mod && (UO->getOpcode() == UO_PreInc || UO->getOpcode() == UO_PreDec)) |
| 6582 | return getObject(UO->getSubExpr(), Mod); |
| 6583 | } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 6584 | if (BO->getOpcode() == BO_Comma) |
| 6585 | return getObject(BO->getRHS(), Mod); |
| 6586 | if (Mod && BO->isAssignmentOp()) |
| 6587 | return getObject(BO->getLHS(), Mod); |
| 6588 | } else if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 6589 | // FIXME: Check for more interesting cases, like "x.n = ++x.n". |
| 6590 | if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenCasts())) |
| 6591 | return ME->getMemberDecl(); |
| 6592 | } else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 6593 | // FIXME: If this is a reference, map through to its value. |
| 6594 | return DRE->getDecl(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6595 | return nullptr; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6596 | } |
| 6597 | |
| 6598 | /// \brief Note that an object was modified or used by an expression. |
| 6599 | void addUsage(UsageInfo &UI, Object O, Expr *Ref, UsageKind UK) { |
| 6600 | Usage &U = UI.Uses[UK]; |
| 6601 | if (!U.Use || !Tree.isUnsequenced(Region, U.Seq)) { |
| 6602 | if (UK == UK_ModAsSideEffect && ModAsSideEffect) |
| 6603 | ModAsSideEffect->push_back(std::make_pair(O, U)); |
| 6604 | U.Use = Ref; |
| 6605 | U.Seq = Region; |
| 6606 | } |
| 6607 | } |
| 6608 | /// \brief Check whether a modification or use conflicts with a prior usage. |
| 6609 | void checkUsage(Object O, UsageInfo &UI, Expr *Ref, UsageKind OtherKind, |
| 6610 | bool IsModMod) { |
| 6611 | if (UI.Diagnosed) |
| 6612 | return; |
| 6613 | |
| 6614 | const Usage &U = UI.Uses[OtherKind]; |
| 6615 | if (!U.Use || !Tree.isUnsequenced(Region, U.Seq)) |
| 6616 | return; |
| 6617 | |
| 6618 | Expr *Mod = U.Use; |
| 6619 | Expr *ModOrUse = Ref; |
| 6620 | if (OtherKind == UK_Use) |
| 6621 | std::swap(Mod, ModOrUse); |
| 6622 | |
| 6623 | SemaRef.Diag(Mod->getExprLoc(), |
| 6624 | IsModMod ? diag::warn_unsequenced_mod_mod |
| 6625 | : diag::warn_unsequenced_mod_use) |
| 6626 | << O << SourceRange(ModOrUse->getExprLoc()); |
| 6627 | UI.Diagnosed = true; |
| 6628 | } |
| 6629 | |
| 6630 | void notePreUse(Object O, Expr *Use) { |
| 6631 | UsageInfo &U = UsageMap[O]; |
| 6632 | // Uses conflict with other modifications. |
| 6633 | checkUsage(O, U, Use, UK_ModAsValue, false); |
| 6634 | } |
| 6635 | void notePostUse(Object O, Expr *Use) { |
| 6636 | UsageInfo &U = UsageMap[O]; |
| 6637 | checkUsage(O, U, Use, UK_ModAsSideEffect, false); |
| 6638 | addUsage(U, O, Use, UK_Use); |
| 6639 | } |
| 6640 | |
| 6641 | void notePreMod(Object O, Expr *Mod) { |
| 6642 | UsageInfo &U = UsageMap[O]; |
| 6643 | // Modifications conflict with other modifications and with uses. |
| 6644 | checkUsage(O, U, Mod, UK_ModAsValue, true); |
| 6645 | checkUsage(O, U, Mod, UK_Use, false); |
| 6646 | } |
| 6647 | void notePostMod(Object O, Expr *Use, UsageKind UK) { |
| 6648 | UsageInfo &U = UsageMap[O]; |
| 6649 | checkUsage(O, U, Use, UK_ModAsSideEffect, true); |
| 6650 | addUsage(U, O, Use, UK); |
| 6651 | } |
| 6652 | |
| 6653 | public: |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 6654 | SequenceChecker(Sema &S, Expr *E, SmallVectorImpl<Expr *> &WorkList) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6655 | : Base(S.Context), SemaRef(S), Region(Tree.root()), |
| 6656 | ModAsSideEffect(nullptr), WorkList(WorkList), EvalTracker(nullptr) { |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6657 | Visit(E); |
| 6658 | } |
| 6659 | |
| 6660 | void VisitStmt(Stmt *S) { |
| 6661 | // Skip all statements which aren't expressions for now. |
| 6662 | } |
| 6663 | |
| 6664 | void VisitExpr(Expr *E) { |
| 6665 | // By default, just recurse to evaluated subexpressions. |
Richard Smith | e3dbfe0 | 2013-06-30 10:40:20 +0000 | [diff] [blame] | 6666 | Base::VisitStmt(E); |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6667 | } |
| 6668 | |
| 6669 | void VisitCastExpr(CastExpr *E) { |
| 6670 | Object O = Object(); |
| 6671 | if (E->getCastKind() == CK_LValueToRValue) |
| 6672 | O = getObject(E->getSubExpr(), false); |
| 6673 | |
| 6674 | if (O) |
| 6675 | notePreUse(O, E); |
| 6676 | VisitExpr(E); |
| 6677 | if (O) |
| 6678 | notePostUse(O, E); |
| 6679 | } |
| 6680 | |
| 6681 | void VisitBinComma(BinaryOperator *BO) { |
| 6682 | // C++11 [expr.comma]p1: |
| 6683 | // Every value computation and side effect associated with the left |
| 6684 | // expression is sequenced before every value computation and side |
| 6685 | // effect associated with the right expression. |
| 6686 | SequenceTree::Seq LHS = Tree.allocate(Region); |
| 6687 | SequenceTree::Seq RHS = Tree.allocate(Region); |
| 6688 | SequenceTree::Seq OldRegion = Region; |
| 6689 | |
| 6690 | { |
| 6691 | SequencedSubexpression SeqLHS(*this); |
| 6692 | Region = LHS; |
| 6693 | Visit(BO->getLHS()); |
| 6694 | } |
| 6695 | |
| 6696 | Region = RHS; |
| 6697 | Visit(BO->getRHS()); |
| 6698 | |
| 6699 | Region = OldRegion; |
| 6700 | |
| 6701 | // Forget that LHS and RHS are sequenced. They are both unsequenced |
| 6702 | // with respect to other stuff. |
| 6703 | Tree.merge(LHS); |
| 6704 | Tree.merge(RHS); |
| 6705 | } |
| 6706 | |
| 6707 | void VisitBinAssign(BinaryOperator *BO) { |
| 6708 | // The modification is sequenced after the value computation of the LHS |
| 6709 | // and RHS, so check it before inspecting the operands and update the |
| 6710 | // map afterwards. |
| 6711 | Object O = getObject(BO->getLHS(), true); |
| 6712 | if (!O) |
| 6713 | return VisitExpr(BO); |
| 6714 | |
| 6715 | notePreMod(O, BO); |
| 6716 | |
| 6717 | // C++11 [expr.ass]p7: |
| 6718 | // E1 op= E2 is equivalent to E1 = E1 op E2, except that E1 is evaluated |
| 6719 | // only once. |
| 6720 | // |
| 6721 | // Therefore, for a compound assignment operator, O is considered used |
| 6722 | // everywhere except within the evaluation of E1 itself. |
| 6723 | if (isa<CompoundAssignOperator>(BO)) |
| 6724 | notePreUse(O, BO); |
| 6725 | |
| 6726 | Visit(BO->getLHS()); |
| 6727 | |
| 6728 | if (isa<CompoundAssignOperator>(BO)) |
| 6729 | notePostUse(O, BO); |
| 6730 | |
| 6731 | Visit(BO->getRHS()); |
| 6732 | |
Richard Smith | 83e37bee | 2013-06-26 23:16:51 +0000 | [diff] [blame] | 6733 | // C++11 [expr.ass]p1: |
| 6734 | // the assignment is sequenced [...] before the value computation of the |
| 6735 | // assignment expression. |
| 6736 | // C11 6.5.16/3 has no such rule. |
| 6737 | notePostMod(O, BO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue |
| 6738 | : UK_ModAsSideEffect); |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6739 | } |
| 6740 | void VisitCompoundAssignOperator(CompoundAssignOperator *CAO) { |
| 6741 | VisitBinAssign(CAO); |
| 6742 | } |
| 6743 | |
| 6744 | void VisitUnaryPreInc(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } |
| 6745 | void VisitUnaryPreDec(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } |
| 6746 | void VisitUnaryPreIncDec(UnaryOperator *UO) { |
| 6747 | Object O = getObject(UO->getSubExpr(), true); |
| 6748 | if (!O) |
| 6749 | return VisitExpr(UO); |
| 6750 | |
| 6751 | notePreMod(O, UO); |
| 6752 | Visit(UO->getSubExpr()); |
Richard Smith | 83e37bee | 2013-06-26 23:16:51 +0000 | [diff] [blame] | 6753 | // C++11 [expr.pre.incr]p1: |
| 6754 | // the expression ++x is equivalent to x+=1 |
| 6755 | notePostMod(O, UO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue |
| 6756 | : UK_ModAsSideEffect); |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6757 | } |
| 6758 | |
| 6759 | void VisitUnaryPostInc(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } |
| 6760 | void VisitUnaryPostDec(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } |
| 6761 | void VisitUnaryPostIncDec(UnaryOperator *UO) { |
| 6762 | Object O = getObject(UO->getSubExpr(), true); |
| 6763 | if (!O) |
| 6764 | return VisitExpr(UO); |
| 6765 | |
| 6766 | notePreMod(O, UO); |
| 6767 | Visit(UO->getSubExpr()); |
| 6768 | notePostMod(O, UO, UK_ModAsSideEffect); |
| 6769 | } |
| 6770 | |
| 6771 | /// Don't visit the RHS of '&&' or '||' if it might not be evaluated. |
| 6772 | void VisitBinLOr(BinaryOperator *BO) { |
| 6773 | // The side-effects of the LHS of an '&&' are sequenced before the |
| 6774 | // value computation of the RHS, and hence before the value computation |
| 6775 | // of the '&&' itself, unless the LHS evaluates to zero. We treat them |
| 6776 | // as if they were unconditionally sequenced. |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 6777 | EvaluationTracker Eval(*this); |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6778 | { |
| 6779 | SequencedSubexpression Sequenced(*this); |
| 6780 | Visit(BO->getLHS()); |
| 6781 | } |
| 6782 | |
| 6783 | bool Result; |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 6784 | if (Eval.evaluate(BO->getLHS(), Result)) { |
Richard Smith | 01a7fba | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 6785 | if (!Result) |
| 6786 | Visit(BO->getRHS()); |
| 6787 | } else { |
| 6788 | // Check for unsequenced operations in the RHS, treating it as an |
| 6789 | // entirely separate evaluation. |
| 6790 | // |
| 6791 | // FIXME: If there are operations in the RHS which are unsequenced |
| 6792 | // with respect to operations outside the RHS, and those operations |
| 6793 | // are unconditionally evaluated, diagnose them. |
Richard Smith | d33f520 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 6794 | WorkList.push_back(BO->getRHS()); |
Richard Smith | 01a7fba | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 6795 | } |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6796 | } |
| 6797 | void VisitBinLAnd(BinaryOperator *BO) { |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 6798 | EvaluationTracker Eval(*this); |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6799 | { |
| 6800 | SequencedSubexpression Sequenced(*this); |
| 6801 | Visit(BO->getLHS()); |
| 6802 | } |
| 6803 | |
| 6804 | bool Result; |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 6805 | if (Eval.evaluate(BO->getLHS(), Result)) { |
Richard Smith | 01a7fba | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 6806 | if (Result) |
| 6807 | Visit(BO->getRHS()); |
| 6808 | } else { |
Richard Smith | d33f520 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 6809 | WorkList.push_back(BO->getRHS()); |
Richard Smith | 01a7fba | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 6810 | } |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6811 | } |
| 6812 | |
| 6813 | // Only visit the condition, unless we can be sure which subexpression will |
| 6814 | // be chosen. |
| 6815 | void VisitAbstractConditionalOperator(AbstractConditionalOperator *CO) { |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 6816 | EvaluationTracker Eval(*this); |
Richard Smith | 83e37bee | 2013-06-26 23:16:51 +0000 | [diff] [blame] | 6817 | { |
| 6818 | SequencedSubexpression Sequenced(*this); |
| 6819 | Visit(CO->getCond()); |
| 6820 | } |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6821 | |
| 6822 | bool Result; |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 6823 | if (Eval.evaluate(CO->getCond(), Result)) |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6824 | Visit(Result ? CO->getTrueExpr() : CO->getFalseExpr()); |
Richard Smith | 01a7fba | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 6825 | else { |
Richard Smith | d33f520 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 6826 | WorkList.push_back(CO->getTrueExpr()); |
| 6827 | WorkList.push_back(CO->getFalseExpr()); |
Richard Smith | 01a7fba | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 6828 | } |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6829 | } |
| 6830 | |
Richard Smith | e3dbfe0 | 2013-06-30 10:40:20 +0000 | [diff] [blame] | 6831 | void VisitCallExpr(CallExpr *CE) { |
| 6832 | // C++11 [intro.execution]p15: |
| 6833 | // When calling a function [...], every value computation and side effect |
| 6834 | // associated with any argument expression, or with the postfix expression |
| 6835 | // designating the called function, is sequenced before execution of every |
| 6836 | // expression or statement in the body of the function [and thus before |
| 6837 | // the value computation of its result]. |
| 6838 | SequencedSubexpression Sequenced(*this); |
| 6839 | Base::VisitCallExpr(CE); |
| 6840 | |
| 6841 | // FIXME: CXXNewExpr and CXXDeleteExpr implicitly call functions. |
| 6842 | } |
| 6843 | |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6844 | void VisitCXXConstructExpr(CXXConstructExpr *CCE) { |
Richard Smith | e3dbfe0 | 2013-06-30 10:40:20 +0000 | [diff] [blame] | 6845 | // This is a call, so all subexpressions are sequenced before the result. |
| 6846 | SequencedSubexpression Sequenced(*this); |
| 6847 | |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6848 | if (!CCE->isListInitialization()) |
| 6849 | return VisitExpr(CCE); |
| 6850 | |
| 6851 | // In C++11, list initializations are sequenced. |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 6852 | SmallVector<SequenceTree::Seq, 32> Elts; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6853 | SequenceTree::Seq Parent = Region; |
| 6854 | for (CXXConstructExpr::arg_iterator I = CCE->arg_begin(), |
| 6855 | E = CCE->arg_end(); |
| 6856 | I != E; ++I) { |
| 6857 | Region = Tree.allocate(Parent); |
| 6858 | Elts.push_back(Region); |
| 6859 | Visit(*I); |
| 6860 | } |
| 6861 | |
| 6862 | // Forget that the initializers are sequenced. |
| 6863 | Region = Parent; |
| 6864 | for (unsigned I = 0; I < Elts.size(); ++I) |
| 6865 | Tree.merge(Elts[I]); |
| 6866 | } |
| 6867 | |
| 6868 | void VisitInitListExpr(InitListExpr *ILE) { |
| 6869 | if (!SemaRef.getLangOpts().CPlusPlus11) |
| 6870 | return VisitExpr(ILE); |
| 6871 | |
| 6872 | // In C++11, list initializations are sequenced. |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 6873 | SmallVector<SequenceTree::Seq, 32> Elts; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6874 | SequenceTree::Seq Parent = Region; |
| 6875 | for (unsigned I = 0; I < ILE->getNumInits(); ++I) { |
| 6876 | Expr *E = ILE->getInit(I); |
| 6877 | if (!E) continue; |
| 6878 | Region = Tree.allocate(Parent); |
| 6879 | Elts.push_back(Region); |
| 6880 | Visit(E); |
| 6881 | } |
| 6882 | |
| 6883 | // Forget that the initializers are sequenced. |
| 6884 | Region = Parent; |
| 6885 | for (unsigned I = 0; I < Elts.size(); ++I) |
| 6886 | Tree.merge(Elts[I]); |
| 6887 | } |
| 6888 | }; |
| 6889 | } |
| 6890 | |
| 6891 | void Sema::CheckUnsequencedOperations(Expr *E) { |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 6892 | SmallVector<Expr *, 8> WorkList; |
Richard Smith | d33f520 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 6893 | WorkList.push_back(E); |
| 6894 | while (!WorkList.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 6895 | Expr *Item = WorkList.pop_back_val(); |
Richard Smith | d33f520 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 6896 | SequenceChecker(*this, Item, WorkList); |
| 6897 | } |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6898 | } |
| 6899 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 6900 | void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc, |
| 6901 | bool IsConstexpr) { |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6902 | CheckImplicitConversions(E, CheckLoc); |
| 6903 | CheckUnsequencedOperations(E); |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 6904 | if (!IsConstexpr && !E->isValueDependent()) |
| 6905 | CheckForIntOverflow(E); |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 6906 | } |
| 6907 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6908 | void Sema::CheckBitFieldInitialization(SourceLocation InitLoc, |
| 6909 | FieldDecl *BitField, |
| 6910 | Expr *Init) { |
| 6911 | (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc); |
| 6912 | } |
| 6913 | |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 6914 | /// CheckParmsForFunctionDef - Check that the parameters of the given |
| 6915 | /// function are appropriate for the definition of a function. This |
| 6916 | /// takes care of any checks that cannot be performed on the |
| 6917 | /// declaration itself, e.g., that the types of each of the function |
| 6918 | /// parameters are complete. |
Reid Kleckner | 5a11580 | 2013-06-24 14:38:26 +0000 | [diff] [blame] | 6919 | bool Sema::CheckParmsForFunctionDef(ParmVarDecl *const *P, |
| 6920 | ParmVarDecl *const *PEnd, |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 6921 | bool CheckParameterNames) { |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 6922 | bool HasInvalidParm = false; |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 6923 | for (; P != PEnd; ++P) { |
| 6924 | ParmVarDecl *Param = *P; |
| 6925 | |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 6926 | // C99 6.7.5.3p4: the parameters in a parameter type list in a |
| 6927 | // function declarator that is part of a function definition of |
| 6928 | // that function shall not have incomplete type. |
| 6929 | // |
| 6930 | // This is also C++ [dcl.fct]p6. |
| 6931 | if (!Param->isInvalidDecl() && |
| 6932 | RequireCompleteType(Param->getLocation(), Param->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 6933 | diag::err_typecheck_decl_incomplete_type)) { |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 6934 | Param->setInvalidDecl(); |
| 6935 | HasInvalidParm = true; |
| 6936 | } |
| 6937 | |
| 6938 | // C99 6.9.1p5: If the declarator includes a parameter type list, the |
| 6939 | // declaration of each parameter shall include an identifier. |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 6940 | if (CheckParameterNames && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6941 | Param->getIdentifier() == nullptr && |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 6942 | !Param->isImplicit() && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6943 | !getLangOpts().CPlusPlus) |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 6944 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
Sam Weinig | deb55d5 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 6945 | |
| 6946 | // C99 6.7.5.3p12: |
| 6947 | // If the function declarator is not part of a definition of that |
| 6948 | // function, parameters may have incomplete type and may use the [*] |
| 6949 | // notation in their sequences of declarator specifiers to specify |
| 6950 | // variable length array types. |
| 6951 | QualType PType = Param->getOriginalType(); |
Fariborz Jahanian | 4289a5a | 2013-04-29 22:01:25 +0000 | [diff] [blame] | 6952 | while (const ArrayType *AT = Context.getAsArrayType(PType)) { |
Sam Weinig | deb55d5 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 6953 | if (AT->getSizeModifier() == ArrayType::Star) { |
Stefanus Du Toit | b331850 | 2013-03-01 21:41:22 +0000 | [diff] [blame] | 6954 | // FIXME: This diagnostic should point the '[*]' if source-location |
Sam Weinig | deb55d5 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 6955 | // information is added for it. |
| 6956 | Diag(Param->getLocation(), diag::err_array_star_in_function_definition); |
Fariborz Jahanian | 4289a5a | 2013-04-29 22:01:25 +0000 | [diff] [blame] | 6957 | break; |
Sam Weinig | deb55d5 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 6958 | } |
Fariborz Jahanian | 4289a5a | 2013-04-29 22:01:25 +0000 | [diff] [blame] | 6959 | PType= AT->getElementType(); |
Sam Weinig | deb55d5 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 6960 | } |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 6961 | |
| 6962 | // MSVC destroys objects passed by value in the callee. Therefore a |
| 6963 | // function definition which takes such a parameter must be able to call the |
Hans Wennborg | 0f3c10c | 2014-01-13 17:23:24 +0000 | [diff] [blame] | 6964 | // object's destructor. However, we don't perform any direct access check |
| 6965 | // on the dtor. |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 6966 | if (getLangOpts().CPlusPlus && Context.getTargetInfo() |
| 6967 | .getCXXABI() |
| 6968 | .areArgsDestroyedLeftToRightInCallee()) { |
Hans Wennborg | 13ac4bd | 2014-01-13 19:24:31 +0000 | [diff] [blame] | 6969 | if (!Param->isInvalidDecl()) { |
| 6970 | if (const RecordType *RT = Param->getType()->getAs<RecordType>()) { |
| 6971 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 6972 | if (!ClassDecl->isInvalidDecl() && |
| 6973 | !ClassDecl->hasIrrelevantDestructor() && |
| 6974 | !ClassDecl->isDependentContext()) { |
| 6975 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
| 6976 | MarkFunctionReferenced(Param->getLocation(), Destructor); |
| 6977 | DiagnoseUseOfDecl(Destructor, Param->getLocation()); |
| 6978 | } |
Hans Wennborg | 0f3c10c | 2014-01-13 17:23:24 +0000 | [diff] [blame] | 6979 | } |
| 6980 | } |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 6981 | } |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 6982 | } |
| 6983 | |
| 6984 | return HasInvalidParm; |
| 6985 | } |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 6986 | |
| 6987 | /// CheckCastAlign - Implements -Wcast-align, which warns when a |
| 6988 | /// pointer cast increases the alignment requirements. |
| 6989 | void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { |
| 6990 | // This is actually a lot of work to potentially be doing on every |
| 6991 | // cast; don't do it if we're ignoring -Wcast_align (as is the default). |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 6992 | if (getDiagnostics().isIgnored(diag::warn_cast_align, TRange.getBegin())) |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 6993 | return; |
| 6994 | |
| 6995 | // Ignore dependent types. |
| 6996 | if (T->isDependentType() || Op->getType()->isDependentType()) |
| 6997 | return; |
| 6998 | |
| 6999 | // Require that the destination be a pointer type. |
| 7000 | const PointerType *DestPtr = T->getAs<PointerType>(); |
| 7001 | if (!DestPtr) return; |
| 7002 | |
| 7003 | // If the destination has alignment 1, we're done. |
| 7004 | QualType DestPointee = DestPtr->getPointeeType(); |
| 7005 | if (DestPointee->isIncompleteType()) return; |
| 7006 | CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); |
| 7007 | if (DestAlign.isOne()) return; |
| 7008 | |
| 7009 | // Require that the source be a pointer type. |
| 7010 | const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); |
| 7011 | if (!SrcPtr) return; |
| 7012 | QualType SrcPointee = SrcPtr->getPointeeType(); |
| 7013 | |
| 7014 | // Whitelist casts from cv void*. We already implicitly |
| 7015 | // whitelisted casts to cv void*, since they have alignment 1. |
| 7016 | // Also whitelist casts involving incomplete types, which implicitly |
| 7017 | // includes 'void'. |
| 7018 | if (SrcPointee->isIncompleteType()) return; |
| 7019 | |
| 7020 | CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee); |
| 7021 | if (SrcAlign >= DestAlign) return; |
| 7022 | |
| 7023 | Diag(TRange.getBegin(), diag::warn_cast_align) |
| 7024 | << Op->getType() << T |
| 7025 | << static_cast<unsigned>(SrcAlign.getQuantity()) |
| 7026 | << static_cast<unsigned>(DestAlign.getQuantity()) |
| 7027 | << TRange << Op->getSourceRange(); |
| 7028 | } |
| 7029 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7030 | static const Type* getElementType(const Expr *BaseExpr) { |
| 7031 | const Type* EltType = BaseExpr->getType().getTypePtr(); |
| 7032 | if (EltType->isAnyPointerType()) |
| 7033 | return EltType->getPointeeType().getTypePtr(); |
| 7034 | else if (EltType->isArrayType()) |
| 7035 | return EltType->getBaseElementTypeUnsafe(); |
| 7036 | return EltType; |
| 7037 | } |
| 7038 | |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 7039 | /// \brief Check whether this array fits the idiom of a size-one tail padded |
| 7040 | /// array member of a struct. |
| 7041 | /// |
| 7042 | /// We avoid emitting out-of-bounds access warnings for such arrays as they are |
| 7043 | /// commonly used to emulate flexible arrays in C89 code. |
| 7044 | static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size, |
| 7045 | const NamedDecl *ND) { |
| 7046 | if (Size != 1 || !ND) return false; |
| 7047 | |
| 7048 | const FieldDecl *FD = dyn_cast<FieldDecl>(ND); |
| 7049 | if (!FD) return false; |
| 7050 | |
| 7051 | // Don't consider sizes resulting from macro expansions or template argument |
| 7052 | // substitution to form C89 tail-padded arrays. |
Sean Callanan | 06a48a6 | 2012-05-04 18:22:53 +0000 | [diff] [blame] | 7053 | |
| 7054 | TypeSourceInfo *TInfo = FD->getTypeSourceInfo(); |
Ted Kremenek | 7ebb493 | 2012-05-09 05:35:08 +0000 | [diff] [blame] | 7055 | while (TInfo) { |
| 7056 | TypeLoc TL = TInfo->getTypeLoc(); |
| 7057 | // Look through typedefs. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7058 | if (TypedefTypeLoc TTL = TL.getAs<TypedefTypeLoc>()) { |
| 7059 | const TypedefNameDecl *TDL = TTL.getTypedefNameDecl(); |
Ted Kremenek | 7ebb493 | 2012-05-09 05:35:08 +0000 | [diff] [blame] | 7060 | TInfo = TDL->getTypeSourceInfo(); |
| 7061 | continue; |
| 7062 | } |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7063 | if (ConstantArrayTypeLoc CTL = TL.getAs<ConstantArrayTypeLoc>()) { |
| 7064 | const Expr *SizeExpr = dyn_cast<IntegerLiteral>(CTL.getSizeExpr()); |
Chad Rosier | 7029992 | 2013-02-06 00:58:34 +0000 | [diff] [blame] | 7065 | if (!SizeExpr || SizeExpr->getExprLoc().isMacroID()) |
| 7066 | return false; |
| 7067 | } |
Ted Kremenek | 7ebb493 | 2012-05-09 05:35:08 +0000 | [diff] [blame] | 7068 | break; |
Sean Callanan | 06a48a6 | 2012-05-04 18:22:53 +0000 | [diff] [blame] | 7069 | } |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 7070 | |
| 7071 | const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext()); |
Matt Beaumont-Gay | c93b489 | 2011-11-29 22:43:53 +0000 | [diff] [blame] | 7072 | if (!RD) return false; |
| 7073 | if (RD->isUnion()) return false; |
| 7074 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 7075 | if (!CRD->isStandardLayout()) return false; |
| 7076 | } |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 7077 | |
Benjamin Kramer | 8c54367 | 2011-08-06 03:04:42 +0000 | [diff] [blame] | 7078 | // See if this is the last field decl in the record. |
| 7079 | const Decl *D = FD; |
| 7080 | while ((D = D->getNextDeclInContext())) |
| 7081 | if (isa<FieldDecl>(D)) |
| 7082 | return false; |
| 7083 | return true; |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 7084 | } |
| 7085 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7086 | void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 7087 | const ArraySubscriptExpr *ASE, |
Richard Smith | 13f6718 | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 7088 | bool AllowOnePastEnd, bool IndexNegated) { |
Eli Friedman | 84e6e5c | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 7089 | IndexExpr = IndexExpr->IgnoreParenImpCasts(); |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 7090 | if (IndexExpr->isValueDependent()) |
| 7091 | return; |
| 7092 | |
Matt Beaumont-Gay | 9d570c4 | 2011-12-12 22:35:02 +0000 | [diff] [blame] | 7093 | const Type *EffectiveType = getElementType(BaseExpr); |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7094 | BaseExpr = BaseExpr->IgnoreParenCasts(); |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 7095 | const ConstantArrayType *ArrayTy = |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7096 | Context.getAsConstantArrayType(BaseExpr->getType()); |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 7097 | if (!ArrayTy) |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 7098 | return; |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 7099 | |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 7100 | llvm::APSInt index; |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 7101 | if (!IndexExpr->EvaluateAsInt(index, Context)) |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 7102 | return; |
Richard Smith | 13f6718 | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 7103 | if (IndexNegated) |
| 7104 | index = -index; |
Ted Kremenek | 108b2d5 | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 7105 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7106 | const NamedDecl *ND = nullptr; |
Chandler Carruth | 126b155 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 7107 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
| 7108 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 7109 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) |
Chandler Carruth | 126b155 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 7110 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); |
Chandler Carruth | 126b155 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 7111 | |
Ted Kremenek | e4b316c | 2011-02-23 23:06:04 +0000 | [diff] [blame] | 7112 | if (index.isUnsigned() || !index.isNegative()) { |
Ted Kremenek | a7ced2c | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 7113 | llvm::APInt size = ArrayTy->getSize(); |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 7114 | if (!size.isStrictlyPositive()) |
| 7115 | return; |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7116 | |
| 7117 | const Type* BaseType = getElementType(BaseExpr); |
Nico Weber | 7c29980 | 2011-09-17 22:59:41 +0000 | [diff] [blame] | 7118 | if (BaseType != EffectiveType) { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7119 | // Make sure we're comparing apples to apples when comparing index to size |
| 7120 | uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); |
| 7121 | uint64_t array_typesize = Context.getTypeSize(BaseType); |
Kaelyn Uhrain | 0fb0bb1 | 2011-08-10 19:47:25 +0000 | [diff] [blame] | 7122 | // Handle ptrarith_typesize being zero, such as when casting to void* |
Kaelyn Uhrain | e535376 | 2011-08-10 18:49:28 +0000 | [diff] [blame] | 7123 | if (!ptrarith_typesize) ptrarith_typesize = 1; |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7124 | if (ptrarith_typesize != array_typesize) { |
| 7125 | // There's a cast to a different size type involved |
| 7126 | uint64_t ratio = array_typesize / ptrarith_typesize; |
| 7127 | // TODO: Be smarter about handling cases where array_typesize is not a |
| 7128 | // multiple of ptrarith_typesize |
| 7129 | if (ptrarith_typesize * ratio == array_typesize) |
| 7130 | size *= llvm::APInt(size.getBitWidth(), ratio); |
| 7131 | } |
| 7132 | } |
| 7133 | |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 7134 | if (size.getBitWidth() > index.getBitWidth()) |
Eli Friedman | 84e6e5c | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 7135 | index = index.zext(size.getBitWidth()); |
Ted Kremenek | a7ced2c | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 7136 | else if (size.getBitWidth() < index.getBitWidth()) |
Eli Friedman | 84e6e5c | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 7137 | size = size.zext(index.getBitWidth()); |
Ted Kremenek | a7ced2c | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 7138 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7139 | // For array subscripting the index must be less than size, but for pointer |
| 7140 | // arithmetic also allow the index (offset) to be equal to size since |
| 7141 | // computing the next address after the end of the array is legal and |
| 7142 | // commonly done e.g. in C++ iterators and range-based for loops. |
Eli Friedman | 84e6e5c | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 7143 | if (AllowOnePastEnd ? index.ule(size) : index.ult(size)) |
Chandler Carruth | 126b155 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 7144 | return; |
| 7145 | |
| 7146 | // Also don't warn for arrays of size 1 which are members of some |
| 7147 | // structure. These are often used to approximate flexible arrays in C89 |
| 7148 | // code. |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7149 | if (IsTailPaddedMemberArray(*this, size, ND)) |
Ted Kremenek | 108b2d5 | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 7150 | return; |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 7151 | |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 7152 | // Suppress the warning if the subscript expression (as identified by the |
| 7153 | // ']' location) and the index expression are both from macro expansions |
| 7154 | // within a system header. |
| 7155 | if (ASE) { |
| 7156 | SourceLocation RBracketLoc = SourceMgr.getSpellingLoc( |
| 7157 | ASE->getRBracketLoc()); |
| 7158 | if (SourceMgr.isInSystemHeader(RBracketLoc)) { |
| 7159 | SourceLocation IndexLoc = SourceMgr.getSpellingLoc( |
| 7160 | IndexExpr->getLocStart()); |
Eli Friedman | 5ba37d5 | 2013-08-22 00:27:10 +0000 | [diff] [blame] | 7161 | if (SourceMgr.isWrittenInSameFile(RBracketLoc, IndexLoc)) |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 7162 | return; |
| 7163 | } |
| 7164 | } |
| 7165 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7166 | unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds; |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 7167 | if (ASE) |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7168 | DiagID = diag::warn_array_index_exceeds_bounds; |
| 7169 | |
| 7170 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, |
| 7171 | PDiag(DiagID) << index.toString(10, true) |
| 7172 | << size.toString(10, true) |
| 7173 | << (unsigned)size.getLimitedValue(~0U) |
| 7174 | << IndexExpr->getSourceRange()); |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 7175 | } else { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7176 | unsigned DiagID = diag::warn_array_index_precedes_bounds; |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 7177 | if (!ASE) { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7178 | DiagID = diag::warn_ptr_arith_precedes_bounds; |
| 7179 | if (index.isNegative()) index = -index; |
| 7180 | } |
| 7181 | |
| 7182 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, |
| 7183 | PDiag(DiagID) << index.toString(10, true) |
| 7184 | << IndexExpr->getSourceRange()); |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 7185 | } |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 7186 | |
Matt Beaumont-Gay | b233982 | 2011-11-29 19:27:11 +0000 | [diff] [blame] | 7187 | if (!ND) { |
| 7188 | // Try harder to find a NamedDecl to point at in the note. |
| 7189 | while (const ArraySubscriptExpr *ASE = |
| 7190 | dyn_cast<ArraySubscriptExpr>(BaseExpr)) |
| 7191 | BaseExpr = ASE->getBase()->IgnoreParenCasts(); |
| 7192 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
| 7193 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); |
| 7194 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) |
| 7195 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); |
| 7196 | } |
| 7197 | |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 7198 | if (ND) |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7199 | DiagRuntimeBehavior(ND->getLocStart(), BaseExpr, |
| 7200 | PDiag(diag::note_array_index_out_of_bounds) |
| 7201 | << ND->getDeclName()); |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 7202 | } |
| 7203 | |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 7204 | void Sema::CheckArrayAccess(const Expr *expr) { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7205 | int AllowOnePastEnd = 0; |
| 7206 | while (expr) { |
| 7207 | expr = expr->IgnoreParenImpCasts(); |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 7208 | switch (expr->getStmtClass()) { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7209 | case Stmt::ArraySubscriptExprClass: { |
| 7210 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr); |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 7211 | CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE, |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7212 | AllowOnePastEnd > 0); |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 7213 | return; |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7214 | } |
| 7215 | case Stmt::UnaryOperatorClass: { |
| 7216 | // Only unwrap the * and & unary operators |
| 7217 | const UnaryOperator *UO = cast<UnaryOperator>(expr); |
| 7218 | expr = UO->getSubExpr(); |
| 7219 | switch (UO->getOpcode()) { |
| 7220 | case UO_AddrOf: |
| 7221 | AllowOnePastEnd++; |
| 7222 | break; |
| 7223 | case UO_Deref: |
| 7224 | AllowOnePastEnd--; |
| 7225 | break; |
| 7226 | default: |
| 7227 | return; |
| 7228 | } |
| 7229 | break; |
| 7230 | } |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 7231 | case Stmt::ConditionalOperatorClass: { |
| 7232 | const ConditionalOperator *cond = cast<ConditionalOperator>(expr); |
| 7233 | if (const Expr *lhs = cond->getLHS()) |
| 7234 | CheckArrayAccess(lhs); |
| 7235 | if (const Expr *rhs = cond->getRHS()) |
| 7236 | CheckArrayAccess(rhs); |
| 7237 | return; |
| 7238 | } |
| 7239 | default: |
| 7240 | return; |
| 7241 | } |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 7242 | } |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 7243 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7244 | |
| 7245 | //===--- CHECK: Objective-C retain cycles ----------------------------------// |
| 7246 | |
| 7247 | namespace { |
| 7248 | struct RetainCycleOwner { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7249 | RetainCycleOwner() : Variable(nullptr), Indirect(false) {} |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7250 | VarDecl *Variable; |
| 7251 | SourceRange Range; |
| 7252 | SourceLocation Loc; |
| 7253 | bool Indirect; |
| 7254 | |
| 7255 | void setLocsFrom(Expr *e) { |
| 7256 | Loc = e->getExprLoc(); |
| 7257 | Range = e->getSourceRange(); |
| 7258 | } |
| 7259 | }; |
| 7260 | } |
| 7261 | |
| 7262 | /// Consider whether capturing the given variable can possibly lead to |
| 7263 | /// a retain cycle. |
| 7264 | static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) { |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 7265 | // In ARC, it's captured strongly iff the variable has __strong |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7266 | // lifetime. In MRR, it's captured strongly if the variable is |
| 7267 | // __block and has an appropriate type. |
| 7268 | if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 7269 | return false; |
| 7270 | |
| 7271 | owner.Variable = var; |
Jordan Rose | fa9e4ba | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 7272 | if (ref) |
| 7273 | owner.setLocsFrom(ref); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7274 | return true; |
| 7275 | } |
| 7276 | |
Fariborz Jahanian | edbc345 | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 7277 | static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7278 | while (true) { |
| 7279 | e = e->IgnoreParens(); |
| 7280 | if (CastExpr *cast = dyn_cast<CastExpr>(e)) { |
| 7281 | switch (cast->getCastKind()) { |
| 7282 | case CK_BitCast: |
| 7283 | case CK_LValueBitCast: |
| 7284 | case CK_LValueToRValue: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 7285 | case CK_ARCReclaimReturnedObject: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7286 | e = cast->getSubExpr(); |
| 7287 | continue; |
| 7288 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7289 | default: |
| 7290 | return false; |
| 7291 | } |
| 7292 | } |
| 7293 | |
| 7294 | if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) { |
| 7295 | ObjCIvarDecl *ivar = ref->getDecl(); |
| 7296 | if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 7297 | return false; |
| 7298 | |
| 7299 | // Try to find a retain cycle in the base. |
Fariborz Jahanian | edbc345 | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 7300 | if (!findRetainCycleOwner(S, ref->getBase(), owner)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7301 | return false; |
| 7302 | |
| 7303 | if (ref->isFreeIvar()) owner.setLocsFrom(ref); |
| 7304 | owner.Indirect = true; |
| 7305 | return true; |
| 7306 | } |
| 7307 | |
| 7308 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 7309 | VarDecl *var = dyn_cast<VarDecl>(ref->getDecl()); |
| 7310 | if (!var) return false; |
| 7311 | return considerVariable(var, ref, owner); |
| 7312 | } |
| 7313 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7314 | if (MemberExpr *member = dyn_cast<MemberExpr>(e)) { |
| 7315 | if (member->isArrow()) return false; |
| 7316 | |
| 7317 | // Don't count this as an indirect ownership. |
| 7318 | e = member->getBase(); |
| 7319 | continue; |
| 7320 | } |
| 7321 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 7322 | if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { |
| 7323 | // Only pay attention to pseudo-objects on property references. |
| 7324 | ObjCPropertyRefExpr *pre |
| 7325 | = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm() |
| 7326 | ->IgnoreParens()); |
| 7327 | if (!pre) return false; |
| 7328 | if (pre->isImplicitProperty()) return false; |
| 7329 | ObjCPropertyDecl *property = pre->getExplicitProperty(); |
| 7330 | if (!property->isRetaining() && |
| 7331 | !(property->getPropertyIvarDecl() && |
| 7332 | property->getPropertyIvarDecl()->getType() |
| 7333 | .getObjCLifetime() == Qualifiers::OCL_Strong)) |
| 7334 | return false; |
| 7335 | |
| 7336 | owner.Indirect = true; |
Fariborz Jahanian | edbc345 | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 7337 | if (pre->isSuperReceiver()) { |
| 7338 | owner.Variable = S.getCurMethodDecl()->getSelfDecl(); |
| 7339 | if (!owner.Variable) |
| 7340 | return false; |
| 7341 | owner.Loc = pre->getLocation(); |
| 7342 | owner.Range = pre->getSourceRange(); |
| 7343 | return true; |
| 7344 | } |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 7345 | e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase()) |
| 7346 | ->getSourceExpr()); |
| 7347 | continue; |
| 7348 | } |
| 7349 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7350 | // Array ivars? |
| 7351 | |
| 7352 | return false; |
| 7353 | } |
| 7354 | } |
| 7355 | |
| 7356 | namespace { |
| 7357 | struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> { |
| 7358 | FindCaptureVisitor(ASTContext &Context, VarDecl *variable) |
| 7359 | : EvaluatedExprVisitor<FindCaptureVisitor>(Context), |
Fariborz Jahanian | 8df9e24 | 2014-06-12 20:57:14 +0000 | [diff] [blame] | 7360 | Context(Context), Variable(variable), Capturer(nullptr), |
| 7361 | VarWillBeReased(false) {} |
| 7362 | ASTContext &Context; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7363 | VarDecl *Variable; |
| 7364 | Expr *Capturer; |
Fariborz Jahanian | 8df9e24 | 2014-06-12 20:57:14 +0000 | [diff] [blame] | 7365 | bool VarWillBeReased; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7366 | |
| 7367 | void VisitDeclRefExpr(DeclRefExpr *ref) { |
| 7368 | if (ref->getDecl() == Variable && !Capturer) |
| 7369 | Capturer = ref; |
| 7370 | } |
| 7371 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7372 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) { |
| 7373 | if (Capturer) return; |
| 7374 | Visit(ref->getBase()); |
| 7375 | if (Capturer && ref->isFreeIvar()) |
| 7376 | Capturer = ref; |
| 7377 | } |
| 7378 | |
| 7379 | void VisitBlockExpr(BlockExpr *block) { |
| 7380 | // Look inside nested blocks |
| 7381 | if (block->getBlockDecl()->capturesVariable(Variable)) |
| 7382 | Visit(block->getBlockDecl()->getBody()); |
| 7383 | } |
Fariborz Jahanian | 0e33754 | 2012-08-31 20:04:47 +0000 | [diff] [blame] | 7384 | |
| 7385 | void VisitOpaqueValueExpr(OpaqueValueExpr *OVE) { |
| 7386 | if (Capturer) return; |
| 7387 | if (OVE->getSourceExpr()) |
| 7388 | Visit(OVE->getSourceExpr()); |
| 7389 | } |
Fariborz Jahanian | 8df9e24 | 2014-06-12 20:57:14 +0000 | [diff] [blame] | 7390 | void VisitBinaryOperator(BinaryOperator *BinOp) { |
| 7391 | if (!Variable || VarWillBeReased || BinOp->getOpcode() != BO_Assign) |
| 7392 | return; |
| 7393 | Expr *LHS = BinOp->getLHS(); |
| 7394 | if (const DeclRefExpr *DRE = dyn_cast_or_null<DeclRefExpr>(LHS)) { |
| 7395 | if (DRE->getDecl() != Variable) |
| 7396 | return; |
| 7397 | if (Expr *RHS = BinOp->getRHS()) { |
| 7398 | RHS = RHS->IgnoreParenCasts(); |
| 7399 | llvm::APSInt Value; |
| 7400 | VarWillBeReased = |
| 7401 | (RHS && RHS->isIntegerConstantExpr(Value, Context) && Value == 0); |
| 7402 | } |
| 7403 | } |
| 7404 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7405 | }; |
| 7406 | } |
| 7407 | |
| 7408 | /// Check whether the given argument is a block which captures a |
| 7409 | /// variable. |
| 7410 | static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) { |
| 7411 | assert(owner.Variable && owner.Loc.isValid()); |
| 7412 | |
| 7413 | e = e->IgnoreParenCasts(); |
Jordan Rose | 67e887c | 2012-09-17 17:54:30 +0000 | [diff] [blame] | 7414 | |
| 7415 | // Look through [^{...} copy] and Block_copy(^{...}). |
| 7416 | if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(e)) { |
| 7417 | Selector Cmd = ME->getSelector(); |
| 7418 | if (Cmd.isUnarySelector() && Cmd.getNameForSlot(0) == "copy") { |
| 7419 | e = ME->getInstanceReceiver(); |
| 7420 | if (!e) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7421 | return nullptr; |
Jordan Rose | 67e887c | 2012-09-17 17:54:30 +0000 | [diff] [blame] | 7422 | e = e->IgnoreParenCasts(); |
| 7423 | } |
| 7424 | } else if (CallExpr *CE = dyn_cast<CallExpr>(e)) { |
| 7425 | if (CE->getNumArgs() == 1) { |
| 7426 | FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(CE->getCalleeDecl()); |
Ted Kremenek | b67c6cc | 2012-10-02 04:36:54 +0000 | [diff] [blame] | 7427 | if (Fn) { |
| 7428 | const IdentifierInfo *FnI = Fn->getIdentifier(); |
| 7429 | if (FnI && FnI->isStr("_Block_copy")) { |
| 7430 | e = CE->getArg(0)->IgnoreParenCasts(); |
| 7431 | } |
| 7432 | } |
Jordan Rose | 67e887c | 2012-09-17 17:54:30 +0000 | [diff] [blame] | 7433 | } |
| 7434 | } |
| 7435 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7436 | BlockExpr *block = dyn_cast<BlockExpr>(e); |
| 7437 | if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7438 | return nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7439 | |
| 7440 | FindCaptureVisitor visitor(S.Context, owner.Variable); |
| 7441 | visitor.Visit(block->getBlockDecl()->getBody()); |
Fariborz Jahanian | 8df9e24 | 2014-06-12 20:57:14 +0000 | [diff] [blame] | 7442 | return visitor.VarWillBeReased ? nullptr : visitor.Capturer; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7443 | } |
| 7444 | |
| 7445 | static void diagnoseRetainCycle(Sema &S, Expr *capturer, |
| 7446 | RetainCycleOwner &owner) { |
| 7447 | assert(capturer); |
| 7448 | assert(owner.Variable && owner.Loc.isValid()); |
| 7449 | |
| 7450 | S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle) |
| 7451 | << owner.Variable << capturer->getSourceRange(); |
| 7452 | S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner) |
| 7453 | << owner.Indirect << owner.Range; |
| 7454 | } |
| 7455 | |
| 7456 | /// Check for a keyword selector that starts with the word 'add' or |
| 7457 | /// 'set'. |
| 7458 | static bool isSetterLikeSelector(Selector sel) { |
| 7459 | if (sel.isUnarySelector()) return false; |
| 7460 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7461 | StringRef str = sel.getNameForSlot(0); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7462 | while (!str.empty() && str.front() == '_') str = str.substr(1); |
Ted Kremenek | 764d63a | 2011-12-01 00:59:21 +0000 | [diff] [blame] | 7463 | if (str.startswith("set")) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7464 | str = str.substr(3); |
Ted Kremenek | 764d63a | 2011-12-01 00:59:21 +0000 | [diff] [blame] | 7465 | else if (str.startswith("add")) { |
| 7466 | // Specially whitelist 'addOperationWithBlock:'. |
| 7467 | if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock")) |
| 7468 | return false; |
| 7469 | str = str.substr(3); |
| 7470 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7471 | else |
| 7472 | return false; |
| 7473 | |
| 7474 | if (str.empty()) return true; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 7475 | return !isLowercase(str.front()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7476 | } |
| 7477 | |
| 7478 | /// Check a message send to see if it's likely to cause a retain cycle. |
| 7479 | void Sema::checkRetainCycles(ObjCMessageExpr *msg) { |
| 7480 | // Only check instance methods whose selector looks like a setter. |
| 7481 | if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector())) |
| 7482 | return; |
| 7483 | |
| 7484 | // Try to find a variable that the receiver is strongly owned by. |
| 7485 | RetainCycleOwner owner; |
| 7486 | if (msg->getReceiverKind() == ObjCMessageExpr::Instance) { |
Fariborz Jahanian | edbc345 | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 7487 | if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7488 | return; |
| 7489 | } else { |
| 7490 | assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance); |
| 7491 | owner.Variable = getCurMethodDecl()->getSelfDecl(); |
| 7492 | owner.Loc = msg->getSuperLoc(); |
| 7493 | owner.Range = msg->getSuperLoc(); |
| 7494 | } |
| 7495 | |
| 7496 | // Check whether the receiver is captured by any of the arguments. |
| 7497 | for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) |
| 7498 | if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) |
| 7499 | return diagnoseRetainCycle(*this, capturer, owner); |
| 7500 | } |
| 7501 | |
| 7502 | /// Check a property assign to see if it's likely to cause a retain cycle. |
| 7503 | void Sema::checkRetainCycles(Expr *receiver, Expr *argument) { |
| 7504 | RetainCycleOwner owner; |
Fariborz Jahanian | edbc345 | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 7505 | if (!findRetainCycleOwner(*this, receiver, owner)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7506 | return; |
| 7507 | |
| 7508 | if (Expr *capturer = findCapturingExpr(*this, argument, owner)) |
| 7509 | diagnoseRetainCycle(*this, capturer, owner); |
| 7510 | } |
| 7511 | |
Jordan Rose | fa9e4ba | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 7512 | void Sema::checkRetainCycles(VarDecl *Var, Expr *Init) { |
| 7513 | RetainCycleOwner Owner; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7514 | if (!considerVariable(Var, /*DeclRefExpr=*/nullptr, Owner)) |
Jordan Rose | fa9e4ba | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 7515 | return; |
| 7516 | |
| 7517 | // Because we don't have an expression for the variable, we have to set the |
| 7518 | // location explicitly here. |
| 7519 | Owner.Loc = Var->getLocation(); |
| 7520 | Owner.Range = Var->getSourceRange(); |
| 7521 | |
| 7522 | if (Expr *Capturer = findCapturingExpr(*this, Init, Owner)) |
| 7523 | diagnoseRetainCycle(*this, Capturer, Owner); |
| 7524 | } |
| 7525 | |
Ted Kremenek | 9304da9 | 2012-12-21 08:04:28 +0000 | [diff] [blame] | 7526 | static bool checkUnsafeAssignLiteral(Sema &S, SourceLocation Loc, |
| 7527 | Expr *RHS, bool isProperty) { |
| 7528 | // Check if RHS is an Objective-C object literal, which also can get |
| 7529 | // immediately zapped in a weak reference. Note that we explicitly |
| 7530 | // allow ObjCStringLiterals, since those are designed to never really die. |
| 7531 | RHS = RHS->IgnoreParenImpCasts(); |
Ted Kremenek | 44c2a2a | 2012-12-21 21:59:39 +0000 | [diff] [blame] | 7532 | |
Ted Kremenek | 6487335 | 2012-12-21 22:46:35 +0000 | [diff] [blame] | 7533 | // This enum needs to match with the 'select' in |
| 7534 | // warn_objc_arc_literal_assign (off-by-1). |
| 7535 | Sema::ObjCLiteralKind Kind = S.CheckLiteralKind(RHS); |
| 7536 | if (Kind == Sema::LK_String || Kind == Sema::LK_None) |
| 7537 | return false; |
Ted Kremenek | 44c2a2a | 2012-12-21 21:59:39 +0000 | [diff] [blame] | 7538 | |
| 7539 | S.Diag(Loc, diag::warn_arc_literal_assign) |
Ted Kremenek | 6487335 | 2012-12-21 22:46:35 +0000 | [diff] [blame] | 7540 | << (unsigned) Kind |
Ted Kremenek | 9304da9 | 2012-12-21 08:04:28 +0000 | [diff] [blame] | 7541 | << (isProperty ? 0 : 1) |
| 7542 | << RHS->getSourceRange(); |
Ted Kremenek | 44c2a2a | 2012-12-21 21:59:39 +0000 | [diff] [blame] | 7543 | |
| 7544 | return true; |
Ted Kremenek | 9304da9 | 2012-12-21 08:04:28 +0000 | [diff] [blame] | 7545 | } |
| 7546 | |
Ted Kremenek | c1f014a | 2012-12-21 19:45:30 +0000 | [diff] [blame] | 7547 | static bool checkUnsafeAssignObject(Sema &S, SourceLocation Loc, |
| 7548 | Qualifiers::ObjCLifetime LT, |
| 7549 | Expr *RHS, bool isProperty) { |
| 7550 | // Strip off any implicit cast added to get to the one ARC-specific. |
| 7551 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
| 7552 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
| 7553 | S.Diag(Loc, diag::warn_arc_retained_assign) |
| 7554 | << (LT == Qualifiers::OCL_ExplicitNone) |
| 7555 | << (isProperty ? 0 : 1) |
| 7556 | << RHS->getSourceRange(); |
| 7557 | return true; |
| 7558 | } |
| 7559 | RHS = cast->getSubExpr(); |
| 7560 | } |
| 7561 | |
| 7562 | if (LT == Qualifiers::OCL_Weak && |
| 7563 | checkUnsafeAssignLiteral(S, Loc, RHS, isProperty)) |
| 7564 | return true; |
| 7565 | |
| 7566 | return false; |
| 7567 | } |
| 7568 | |
Ted Kremenek | b36234d | 2012-12-21 08:04:20 +0000 | [diff] [blame] | 7569 | bool Sema::checkUnsafeAssigns(SourceLocation Loc, |
| 7570 | QualType LHS, Expr *RHS) { |
| 7571 | Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime(); |
| 7572 | |
| 7573 | if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone) |
| 7574 | return false; |
| 7575 | |
| 7576 | if (checkUnsafeAssignObject(*this, Loc, LT, RHS, false)) |
| 7577 | return true; |
| 7578 | |
| 7579 | return false; |
| 7580 | } |
| 7581 | |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 7582 | void Sema::checkUnsafeExprAssigns(SourceLocation Loc, |
| 7583 | Expr *LHS, Expr *RHS) { |
Fariborz Jahanian | c72a807 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 7584 | QualType LHSType; |
| 7585 | // PropertyRef on LHS type need be directly obtained from |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 7586 | // its declaration as it has a PseudoType. |
Fariborz Jahanian | c72a807 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 7587 | ObjCPropertyRefExpr *PRE |
| 7588 | = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens()); |
| 7589 | if (PRE && !PRE->isImplicitProperty()) { |
| 7590 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
| 7591 | if (PD) |
| 7592 | LHSType = PD->getType(); |
| 7593 | } |
| 7594 | |
| 7595 | if (LHSType.isNull()) |
| 7596 | LHSType = LHS->getType(); |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 7597 | |
| 7598 | Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime(); |
| 7599 | |
| 7600 | if (LT == Qualifiers::OCL_Weak) { |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 7601 | if (!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, Loc)) |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 7602 | getCurFunction()->markSafeWeakUse(LHS); |
| 7603 | } |
| 7604 | |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 7605 | if (checkUnsafeAssigns(Loc, LHSType, RHS)) |
| 7606 | return; |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 7607 | |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 7608 | // FIXME. Check for other life times. |
| 7609 | if (LT != Qualifiers::OCL_None) |
| 7610 | return; |
| 7611 | |
Fariborz Jahanian | c72a807 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 7612 | if (PRE) { |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 7613 | if (PRE->isImplicitProperty()) |
| 7614 | return; |
| 7615 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
| 7616 | if (!PD) |
| 7617 | return; |
| 7618 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 7619 | unsigned Attributes = PD->getPropertyAttributes(); |
| 7620 | if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) { |
Fariborz Jahanian | c72a807 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 7621 | // when 'assign' attribute was not explicitly specified |
| 7622 | // by user, ignore it and rely on property type itself |
| 7623 | // for lifetime info. |
| 7624 | unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten(); |
| 7625 | if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) && |
| 7626 | LHSType->isObjCRetainableType()) |
| 7627 | return; |
| 7628 | |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 7629 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 7630 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 7631 | Diag(Loc, diag::warn_arc_retained_property_assign) |
| 7632 | << RHS->getSourceRange(); |
| 7633 | return; |
| 7634 | } |
| 7635 | RHS = cast->getSubExpr(); |
| 7636 | } |
Fariborz Jahanian | c72a807 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 7637 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 7638 | else if (Attributes & ObjCPropertyDecl::OBJC_PR_weak) { |
Ted Kremenek | b36234d | 2012-12-21 08:04:20 +0000 | [diff] [blame] | 7639 | if (checkUnsafeAssignObject(*this, Loc, Qualifiers::OCL_Weak, RHS, true)) |
| 7640 | return; |
Fariborz Jahanian | dabd133 | 2012-07-06 21:09:27 +0000 | [diff] [blame] | 7641 | } |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 7642 | } |
| 7643 | } |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 7644 | |
| 7645 | //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===// |
| 7646 | |
| 7647 | namespace { |
| 7648 | bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr, |
| 7649 | SourceLocation StmtLoc, |
| 7650 | const NullStmt *Body) { |
| 7651 | // Do not warn if the body is a macro that expands to nothing, e.g: |
| 7652 | // |
| 7653 | // #define CALL(x) |
| 7654 | // if (condition) |
| 7655 | // CALL(0); |
| 7656 | // |
| 7657 | if (Body->hasLeadingEmptyMacro()) |
| 7658 | return false; |
| 7659 | |
| 7660 | // Get line numbers of statement and body. |
| 7661 | bool StmtLineInvalid; |
| 7662 | unsigned StmtLine = SourceMgr.getSpellingLineNumber(StmtLoc, |
| 7663 | &StmtLineInvalid); |
| 7664 | if (StmtLineInvalid) |
| 7665 | return false; |
| 7666 | |
| 7667 | bool BodyLineInvalid; |
| 7668 | unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(), |
| 7669 | &BodyLineInvalid); |
| 7670 | if (BodyLineInvalid) |
| 7671 | return false; |
| 7672 | |
| 7673 | // Warn if null statement and body are on the same line. |
| 7674 | if (StmtLine != BodyLine) |
| 7675 | return false; |
| 7676 | |
| 7677 | return true; |
| 7678 | } |
| 7679 | } // Unnamed namespace |
| 7680 | |
| 7681 | void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc, |
| 7682 | const Stmt *Body, |
| 7683 | unsigned DiagID) { |
| 7684 | // Since this is a syntactic check, don't emit diagnostic for template |
| 7685 | // instantiations, this just adds noise. |
| 7686 | if (CurrentInstantiationScope) |
| 7687 | return; |
| 7688 | |
| 7689 | // The body should be a null statement. |
| 7690 | const NullStmt *NBody = dyn_cast<NullStmt>(Body); |
| 7691 | if (!NBody) |
| 7692 | return; |
| 7693 | |
| 7694 | // Do the usual checks. |
| 7695 | if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) |
| 7696 | return; |
| 7697 | |
| 7698 | Diag(NBody->getSemiLoc(), DiagID); |
| 7699 | Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); |
| 7700 | } |
| 7701 | |
| 7702 | void Sema::DiagnoseEmptyLoopBody(const Stmt *S, |
| 7703 | const Stmt *PossibleBody) { |
| 7704 | assert(!CurrentInstantiationScope); // Ensured by caller |
| 7705 | |
| 7706 | SourceLocation StmtLoc; |
| 7707 | const Stmt *Body; |
| 7708 | unsigned DiagID; |
| 7709 | if (const ForStmt *FS = dyn_cast<ForStmt>(S)) { |
| 7710 | StmtLoc = FS->getRParenLoc(); |
| 7711 | Body = FS->getBody(); |
| 7712 | DiagID = diag::warn_empty_for_body; |
| 7713 | } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) { |
| 7714 | StmtLoc = WS->getCond()->getSourceRange().getEnd(); |
| 7715 | Body = WS->getBody(); |
| 7716 | DiagID = diag::warn_empty_while_body; |
| 7717 | } else |
| 7718 | return; // Neither `for' nor `while'. |
| 7719 | |
| 7720 | // The body should be a null statement. |
| 7721 | const NullStmt *NBody = dyn_cast<NullStmt>(Body); |
| 7722 | if (!NBody) |
| 7723 | return; |
| 7724 | |
| 7725 | // Skip expensive checks if diagnostic is disabled. |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 7726 | if (Diags.isIgnored(DiagID, NBody->getSemiLoc())) |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 7727 | return; |
| 7728 | |
| 7729 | // Do the usual checks. |
| 7730 | if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) |
| 7731 | return; |
| 7732 | |
| 7733 | // `for(...);' and `while(...);' are popular idioms, so in order to keep |
| 7734 | // noise level low, emit diagnostics only if for/while is followed by a |
| 7735 | // CompoundStmt, e.g.: |
| 7736 | // for (int i = 0; i < n; i++); |
| 7737 | // { |
| 7738 | // a(i); |
| 7739 | // } |
| 7740 | // or if for/while is followed by a statement with more indentation |
| 7741 | // than for/while itself: |
| 7742 | // for (int i = 0; i < n; i++); |
| 7743 | // a(i); |
| 7744 | bool ProbableTypo = isa<CompoundStmt>(PossibleBody); |
| 7745 | if (!ProbableTypo) { |
| 7746 | bool BodyColInvalid; |
| 7747 | unsigned BodyCol = SourceMgr.getPresumedColumnNumber( |
| 7748 | PossibleBody->getLocStart(), |
| 7749 | &BodyColInvalid); |
| 7750 | if (BodyColInvalid) |
| 7751 | return; |
| 7752 | |
| 7753 | bool StmtColInvalid; |
| 7754 | unsigned StmtCol = SourceMgr.getPresumedColumnNumber( |
| 7755 | S->getLocStart(), |
| 7756 | &StmtColInvalid); |
| 7757 | if (StmtColInvalid) |
| 7758 | return; |
| 7759 | |
| 7760 | if (BodyCol > StmtCol) |
| 7761 | ProbableTypo = true; |
| 7762 | } |
| 7763 | |
| 7764 | if (ProbableTypo) { |
| 7765 | Diag(NBody->getSemiLoc(), DiagID); |
| 7766 | Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); |
| 7767 | } |
| 7768 | } |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 7769 | |
| 7770 | //===--- Layout compatibility ----------------------------------------------// |
| 7771 | |
| 7772 | namespace { |
| 7773 | |
| 7774 | bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2); |
| 7775 | |
| 7776 | /// \brief Check if two enumeration types are layout-compatible. |
| 7777 | bool isLayoutCompatible(ASTContext &C, EnumDecl *ED1, EnumDecl *ED2) { |
| 7778 | // C++11 [dcl.enum] p8: |
| 7779 | // Two enumeration types are layout-compatible if they have the same |
| 7780 | // underlying type. |
| 7781 | return ED1->isComplete() && ED2->isComplete() && |
| 7782 | C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType()); |
| 7783 | } |
| 7784 | |
| 7785 | /// \brief Check if two fields are layout-compatible. |
| 7786 | bool isLayoutCompatible(ASTContext &C, FieldDecl *Field1, FieldDecl *Field2) { |
| 7787 | if (!isLayoutCompatible(C, Field1->getType(), Field2->getType())) |
| 7788 | return false; |
| 7789 | |
| 7790 | if (Field1->isBitField() != Field2->isBitField()) |
| 7791 | return false; |
| 7792 | |
| 7793 | if (Field1->isBitField()) { |
| 7794 | // Make sure that the bit-fields are the same length. |
| 7795 | unsigned Bits1 = Field1->getBitWidthValue(C); |
| 7796 | unsigned Bits2 = Field2->getBitWidthValue(C); |
| 7797 | |
| 7798 | if (Bits1 != Bits2) |
| 7799 | return false; |
| 7800 | } |
| 7801 | |
| 7802 | return true; |
| 7803 | } |
| 7804 | |
| 7805 | /// \brief Check if two standard-layout structs are layout-compatible. |
| 7806 | /// (C++11 [class.mem] p17) |
| 7807 | bool isLayoutCompatibleStruct(ASTContext &C, |
| 7808 | RecordDecl *RD1, |
| 7809 | RecordDecl *RD2) { |
| 7810 | // If both records are C++ classes, check that base classes match. |
| 7811 | if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1)) { |
| 7812 | // If one of records is a CXXRecordDecl we are in C++ mode, |
| 7813 | // thus the other one is a CXXRecordDecl, too. |
| 7814 | const CXXRecordDecl *D2CXX = cast<CXXRecordDecl>(RD2); |
| 7815 | // Check number of base classes. |
| 7816 | if (D1CXX->getNumBases() != D2CXX->getNumBases()) |
| 7817 | return false; |
| 7818 | |
| 7819 | // Check the base classes. |
| 7820 | for (CXXRecordDecl::base_class_const_iterator |
| 7821 | Base1 = D1CXX->bases_begin(), |
| 7822 | BaseEnd1 = D1CXX->bases_end(), |
| 7823 | Base2 = D2CXX->bases_begin(); |
| 7824 | Base1 != BaseEnd1; |
| 7825 | ++Base1, ++Base2) { |
| 7826 | if (!isLayoutCompatible(C, Base1->getType(), Base2->getType())) |
| 7827 | return false; |
| 7828 | } |
| 7829 | } else if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2)) { |
| 7830 | // If only RD2 is a C++ class, it should have zero base classes. |
| 7831 | if (D2CXX->getNumBases() > 0) |
| 7832 | return false; |
| 7833 | } |
| 7834 | |
| 7835 | // Check the fields. |
| 7836 | RecordDecl::field_iterator Field2 = RD2->field_begin(), |
| 7837 | Field2End = RD2->field_end(), |
| 7838 | Field1 = RD1->field_begin(), |
| 7839 | Field1End = RD1->field_end(); |
| 7840 | for ( ; Field1 != Field1End && Field2 != Field2End; ++Field1, ++Field2) { |
| 7841 | if (!isLayoutCompatible(C, *Field1, *Field2)) |
| 7842 | return false; |
| 7843 | } |
| 7844 | if (Field1 != Field1End || Field2 != Field2End) |
| 7845 | return false; |
| 7846 | |
| 7847 | return true; |
| 7848 | } |
| 7849 | |
| 7850 | /// \brief Check if two standard-layout unions are layout-compatible. |
| 7851 | /// (C++11 [class.mem] p18) |
| 7852 | bool isLayoutCompatibleUnion(ASTContext &C, |
| 7853 | RecordDecl *RD1, |
| 7854 | RecordDecl *RD2) { |
| 7855 | llvm::SmallPtrSet<FieldDecl *, 8> UnmatchedFields; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 7856 | for (auto *Field2 : RD2->fields()) |
| 7857 | UnmatchedFields.insert(Field2); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 7858 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 7859 | for (auto *Field1 : RD1->fields()) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 7860 | llvm::SmallPtrSet<FieldDecl *, 8>::iterator |
| 7861 | I = UnmatchedFields.begin(), |
| 7862 | E = UnmatchedFields.end(); |
| 7863 | |
| 7864 | for ( ; I != E; ++I) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 7865 | if (isLayoutCompatible(C, Field1, *I)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 7866 | bool Result = UnmatchedFields.erase(*I); |
| 7867 | (void) Result; |
| 7868 | assert(Result); |
| 7869 | break; |
| 7870 | } |
| 7871 | } |
| 7872 | if (I == E) |
| 7873 | return false; |
| 7874 | } |
| 7875 | |
| 7876 | return UnmatchedFields.empty(); |
| 7877 | } |
| 7878 | |
| 7879 | bool isLayoutCompatible(ASTContext &C, RecordDecl *RD1, RecordDecl *RD2) { |
| 7880 | if (RD1->isUnion() != RD2->isUnion()) |
| 7881 | return false; |
| 7882 | |
| 7883 | if (RD1->isUnion()) |
| 7884 | return isLayoutCompatibleUnion(C, RD1, RD2); |
| 7885 | else |
| 7886 | return isLayoutCompatibleStruct(C, RD1, RD2); |
| 7887 | } |
| 7888 | |
| 7889 | /// \brief Check if two types are layout-compatible in C++11 sense. |
| 7890 | bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2) { |
| 7891 | if (T1.isNull() || T2.isNull()) |
| 7892 | return false; |
| 7893 | |
| 7894 | // C++11 [basic.types] p11: |
| 7895 | // If two types T1 and T2 are the same type, then T1 and T2 are |
| 7896 | // layout-compatible types. |
| 7897 | if (C.hasSameType(T1, T2)) |
| 7898 | return true; |
| 7899 | |
| 7900 | T1 = T1.getCanonicalType().getUnqualifiedType(); |
| 7901 | T2 = T2.getCanonicalType().getUnqualifiedType(); |
| 7902 | |
| 7903 | const Type::TypeClass TC1 = T1->getTypeClass(); |
| 7904 | const Type::TypeClass TC2 = T2->getTypeClass(); |
| 7905 | |
| 7906 | if (TC1 != TC2) |
| 7907 | return false; |
| 7908 | |
| 7909 | if (TC1 == Type::Enum) { |
| 7910 | return isLayoutCompatible(C, |
| 7911 | cast<EnumType>(T1)->getDecl(), |
| 7912 | cast<EnumType>(T2)->getDecl()); |
| 7913 | } else if (TC1 == Type::Record) { |
| 7914 | if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType()) |
| 7915 | return false; |
| 7916 | |
| 7917 | return isLayoutCompatible(C, |
| 7918 | cast<RecordType>(T1)->getDecl(), |
| 7919 | cast<RecordType>(T2)->getDecl()); |
| 7920 | } |
| 7921 | |
| 7922 | return false; |
| 7923 | } |
| 7924 | } |
| 7925 | |
| 7926 | //===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----// |
| 7927 | |
| 7928 | namespace { |
| 7929 | /// \brief Given a type tag expression find the type tag itself. |
| 7930 | /// |
| 7931 | /// \param TypeExpr Type tag expression, as it appears in user's code. |
| 7932 | /// |
| 7933 | /// \param VD Declaration of an identifier that appears in a type tag. |
| 7934 | /// |
| 7935 | /// \param MagicValue Type tag magic value. |
| 7936 | bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx, |
| 7937 | const ValueDecl **VD, uint64_t *MagicValue) { |
| 7938 | while(true) { |
| 7939 | if (!TypeExpr) |
| 7940 | return false; |
| 7941 | |
| 7942 | TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts(); |
| 7943 | |
| 7944 | switch (TypeExpr->getStmtClass()) { |
| 7945 | case Stmt::UnaryOperatorClass: { |
| 7946 | const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr); |
| 7947 | if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) { |
| 7948 | TypeExpr = UO->getSubExpr(); |
| 7949 | continue; |
| 7950 | } |
| 7951 | return false; |
| 7952 | } |
| 7953 | |
| 7954 | case Stmt::DeclRefExprClass: { |
| 7955 | const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr); |
| 7956 | *VD = DRE->getDecl(); |
| 7957 | return true; |
| 7958 | } |
| 7959 | |
| 7960 | case Stmt::IntegerLiteralClass: { |
| 7961 | const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr); |
| 7962 | llvm::APInt MagicValueAPInt = IL->getValue(); |
| 7963 | if (MagicValueAPInt.getActiveBits() <= 64) { |
| 7964 | *MagicValue = MagicValueAPInt.getZExtValue(); |
| 7965 | return true; |
| 7966 | } else |
| 7967 | return false; |
| 7968 | } |
| 7969 | |
| 7970 | case Stmt::BinaryConditionalOperatorClass: |
| 7971 | case Stmt::ConditionalOperatorClass: { |
| 7972 | const AbstractConditionalOperator *ACO = |
| 7973 | cast<AbstractConditionalOperator>(TypeExpr); |
| 7974 | bool Result; |
| 7975 | if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx)) { |
| 7976 | if (Result) |
| 7977 | TypeExpr = ACO->getTrueExpr(); |
| 7978 | else |
| 7979 | TypeExpr = ACO->getFalseExpr(); |
| 7980 | continue; |
| 7981 | } |
| 7982 | return false; |
| 7983 | } |
| 7984 | |
| 7985 | case Stmt::BinaryOperatorClass: { |
| 7986 | const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr); |
| 7987 | if (BO->getOpcode() == BO_Comma) { |
| 7988 | TypeExpr = BO->getRHS(); |
| 7989 | continue; |
| 7990 | } |
| 7991 | return false; |
| 7992 | } |
| 7993 | |
| 7994 | default: |
| 7995 | return false; |
| 7996 | } |
| 7997 | } |
| 7998 | } |
| 7999 | |
| 8000 | /// \brief Retrieve the C type corresponding to type tag TypeExpr. |
| 8001 | /// |
| 8002 | /// \param TypeExpr Expression that specifies a type tag. |
| 8003 | /// |
| 8004 | /// \param MagicValues Registered magic values. |
| 8005 | /// |
| 8006 | /// \param FoundWrongKind Set to true if a type tag was found, but of a wrong |
| 8007 | /// kind. |
| 8008 | /// |
| 8009 | /// \param TypeInfo Information about the corresponding C type. |
| 8010 | /// |
| 8011 | /// \returns true if the corresponding C type was found. |
| 8012 | bool GetMatchingCType( |
| 8013 | const IdentifierInfo *ArgumentKind, |
| 8014 | const Expr *TypeExpr, const ASTContext &Ctx, |
| 8015 | const llvm::DenseMap<Sema::TypeTagMagicValue, |
| 8016 | Sema::TypeTagData> *MagicValues, |
| 8017 | bool &FoundWrongKind, |
| 8018 | Sema::TypeTagData &TypeInfo) { |
| 8019 | FoundWrongKind = false; |
| 8020 | |
| 8021 | // Variable declaration that has type_tag_for_datatype attribute. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8022 | const ValueDecl *VD = nullptr; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 8023 | |
| 8024 | uint64_t MagicValue; |
| 8025 | |
| 8026 | if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue)) |
| 8027 | return false; |
| 8028 | |
| 8029 | if (VD) { |
Benjamin Kramer | ae852a6 | 2014-02-23 14:34:50 +0000 | [diff] [blame] | 8030 | if (TypeTagForDatatypeAttr *I = VD->getAttr<TypeTagForDatatypeAttr>()) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 8031 | if (I->getArgumentKind() != ArgumentKind) { |
| 8032 | FoundWrongKind = true; |
| 8033 | return false; |
| 8034 | } |
| 8035 | TypeInfo.Type = I->getMatchingCType(); |
| 8036 | TypeInfo.LayoutCompatible = I->getLayoutCompatible(); |
| 8037 | TypeInfo.MustBeNull = I->getMustBeNull(); |
| 8038 | return true; |
| 8039 | } |
| 8040 | return false; |
| 8041 | } |
| 8042 | |
| 8043 | if (!MagicValues) |
| 8044 | return false; |
| 8045 | |
| 8046 | llvm::DenseMap<Sema::TypeTagMagicValue, |
| 8047 | Sema::TypeTagData>::const_iterator I = |
| 8048 | MagicValues->find(std::make_pair(ArgumentKind, MagicValue)); |
| 8049 | if (I == MagicValues->end()) |
| 8050 | return false; |
| 8051 | |
| 8052 | TypeInfo = I->second; |
| 8053 | return true; |
| 8054 | } |
| 8055 | } // unnamed namespace |
| 8056 | |
| 8057 | void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind, |
| 8058 | uint64_t MagicValue, QualType Type, |
| 8059 | bool LayoutCompatible, |
| 8060 | bool MustBeNull) { |
| 8061 | if (!TypeTagForDatatypeMagicValues) |
| 8062 | TypeTagForDatatypeMagicValues.reset( |
| 8063 | new llvm::DenseMap<TypeTagMagicValue, TypeTagData>); |
| 8064 | |
| 8065 | TypeTagMagicValue Magic(ArgumentKind, MagicValue); |
| 8066 | (*TypeTagForDatatypeMagicValues)[Magic] = |
| 8067 | TypeTagData(Type, LayoutCompatible, MustBeNull); |
| 8068 | } |
| 8069 | |
| 8070 | namespace { |
| 8071 | bool IsSameCharType(QualType T1, QualType T2) { |
| 8072 | const BuiltinType *BT1 = T1->getAs<BuiltinType>(); |
| 8073 | if (!BT1) |
| 8074 | return false; |
| 8075 | |
| 8076 | const BuiltinType *BT2 = T2->getAs<BuiltinType>(); |
| 8077 | if (!BT2) |
| 8078 | return false; |
| 8079 | |
| 8080 | BuiltinType::Kind T1Kind = BT1->getKind(); |
| 8081 | BuiltinType::Kind T2Kind = BT2->getKind(); |
| 8082 | |
| 8083 | return (T1Kind == BuiltinType::SChar && T2Kind == BuiltinType::Char_S) || |
| 8084 | (T1Kind == BuiltinType::UChar && T2Kind == BuiltinType::Char_U) || |
| 8085 | (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) || |
| 8086 | (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar); |
| 8087 | } |
| 8088 | } // unnamed namespace |
| 8089 | |
| 8090 | void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr, |
| 8091 | const Expr * const *ExprArgs) { |
| 8092 | const IdentifierInfo *ArgumentKind = Attr->getArgumentKind(); |
| 8093 | bool IsPointerAttr = Attr->getIsPointer(); |
| 8094 | |
| 8095 | const Expr *TypeTagExpr = ExprArgs[Attr->getTypeTagIdx()]; |
| 8096 | bool FoundWrongKind; |
| 8097 | TypeTagData TypeInfo; |
| 8098 | if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context, |
| 8099 | TypeTagForDatatypeMagicValues.get(), |
| 8100 | FoundWrongKind, TypeInfo)) { |
| 8101 | if (FoundWrongKind) |
| 8102 | Diag(TypeTagExpr->getExprLoc(), |
| 8103 | diag::warn_type_tag_for_datatype_wrong_kind) |
| 8104 | << TypeTagExpr->getSourceRange(); |
| 8105 | return; |
| 8106 | } |
| 8107 | |
| 8108 | const Expr *ArgumentExpr = ExprArgs[Attr->getArgumentIdx()]; |
| 8109 | if (IsPointerAttr) { |
| 8110 | // Skip implicit cast of pointer to `void *' (as a function argument). |
| 8111 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr)) |
Dmitri Gribenko | 5ac744e | 2012-11-03 16:07:49 +0000 | [diff] [blame] | 8112 | if (ICE->getType()->isVoidPointerType() && |
Dmitri Gribenko | f21203b | 2012-11-03 22:10:18 +0000 | [diff] [blame] | 8113 | ICE->getCastKind() == CK_BitCast) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 8114 | ArgumentExpr = ICE->getSubExpr(); |
| 8115 | } |
| 8116 | QualType ArgumentType = ArgumentExpr->getType(); |
| 8117 | |
| 8118 | // Passing a `void*' pointer shouldn't trigger a warning. |
| 8119 | if (IsPointerAttr && ArgumentType->isVoidPointerType()) |
| 8120 | return; |
| 8121 | |
| 8122 | if (TypeInfo.MustBeNull) { |
| 8123 | // Type tag with matching void type requires a null pointer. |
| 8124 | if (!ArgumentExpr->isNullPointerConstant(Context, |
| 8125 | Expr::NPC_ValueDependentIsNotNull)) { |
| 8126 | Diag(ArgumentExpr->getExprLoc(), |
| 8127 | diag::warn_type_safety_null_pointer_required) |
| 8128 | << ArgumentKind->getName() |
| 8129 | << ArgumentExpr->getSourceRange() |
| 8130 | << TypeTagExpr->getSourceRange(); |
| 8131 | } |
| 8132 | return; |
| 8133 | } |
| 8134 | |
| 8135 | QualType RequiredType = TypeInfo.Type; |
| 8136 | if (IsPointerAttr) |
| 8137 | RequiredType = Context.getPointerType(RequiredType); |
| 8138 | |
| 8139 | bool mismatch = false; |
| 8140 | if (!TypeInfo.LayoutCompatible) { |
| 8141 | mismatch = !Context.hasSameType(ArgumentType, RequiredType); |
| 8142 | |
| 8143 | // C++11 [basic.fundamental] p1: |
| 8144 | // Plain char, signed char, and unsigned char are three distinct types. |
| 8145 | // |
| 8146 | // But we treat plain `char' as equivalent to `signed char' or `unsigned |
| 8147 | // char' depending on the current char signedness mode. |
| 8148 | if (mismatch) |
| 8149 | if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(), |
| 8150 | RequiredType->getPointeeType())) || |
| 8151 | (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType))) |
| 8152 | mismatch = false; |
| 8153 | } else |
| 8154 | if (IsPointerAttr) |
| 8155 | mismatch = !isLayoutCompatible(Context, |
| 8156 | ArgumentType->getPointeeType(), |
| 8157 | RequiredType->getPointeeType()); |
| 8158 | else |
| 8159 | mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType); |
| 8160 | |
| 8161 | if (mismatch) |
| 8162 | Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch) |
Aaron Ballman | 25dc1e1 | 2014-01-03 02:14:08 +0000 | [diff] [blame] | 8163 | << ArgumentType << ArgumentKind |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 8164 | << TypeInfo.LayoutCompatible << RequiredType |
| 8165 | << ArgumentExpr->getSourceRange() |
| 8166 | << TypeTagExpr->getSourceRange(); |
| 8167 | } |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 8168 | |