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 | |
Fariborz Jahanian | 3e6a0be | 2014-09-18 17:58:27 +0000 | [diff] [blame] | 114 | static void SemaBuiltinMemChkCall(Sema &S, FunctionDecl *FDecl, |
| 115 | CallExpr *TheCall, unsigned SizeIdx, |
| 116 | unsigned DstSizeIdx) { |
| 117 | if (TheCall->getNumArgs() <= SizeIdx || |
| 118 | TheCall->getNumArgs() <= DstSizeIdx) |
| 119 | return; |
| 120 | |
| 121 | const Expr *SizeArg = TheCall->getArg(SizeIdx); |
| 122 | const Expr *DstSizeArg = TheCall->getArg(DstSizeIdx); |
| 123 | |
| 124 | llvm::APSInt Size, DstSize; |
| 125 | |
| 126 | // find out if both sizes are known at compile time |
| 127 | if (!SizeArg->EvaluateAsInt(Size, S.Context) || |
| 128 | !DstSizeArg->EvaluateAsInt(DstSize, S.Context)) |
| 129 | return; |
| 130 | |
| 131 | if (Size.ule(DstSize)) |
| 132 | return; |
| 133 | |
| 134 | // confirmed overflow so generate the diagnostic. |
| 135 | IdentifierInfo *FnName = FDecl->getIdentifier(); |
| 136 | SourceLocation SL = TheCall->getLocStart(); |
| 137 | SourceRange SR = TheCall->getSourceRange(); |
| 138 | |
| 139 | S.Diag(SL, diag::warn_memcpy_chk_overflow) << SR << FnName; |
| 140 | } |
| 141 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 142 | static bool SemaBuiltinCallWithStaticChain(Sema &S, CallExpr *BuiltinCall) { |
| 143 | if (checkArgCount(S, BuiltinCall, 2)) |
| 144 | return true; |
| 145 | |
| 146 | SourceLocation BuiltinLoc = BuiltinCall->getLocStart(); |
| 147 | Expr *Builtin = BuiltinCall->getCallee()->IgnoreImpCasts(); |
| 148 | Expr *Call = BuiltinCall->getArg(0); |
| 149 | Expr *Chain = BuiltinCall->getArg(1); |
| 150 | |
| 151 | if (Call->getStmtClass() != Stmt::CallExprClass) { |
| 152 | S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_not_call) |
| 153 | << Call->getSourceRange(); |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | auto CE = cast<CallExpr>(Call); |
| 158 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 159 | S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_block_call) |
| 160 | << Call->getSourceRange(); |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | const Decl *TargetDecl = CE->getCalleeDecl(); |
| 165 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) |
| 166 | if (FD->getBuiltinID()) { |
| 167 | S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_builtin_call) |
| 168 | << Call->getSourceRange(); |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens())) { |
| 173 | S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_pdtor_call) |
| 174 | << Call->getSourceRange(); |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | ExprResult ChainResult = S.UsualUnaryConversions(Chain); |
| 179 | if (ChainResult.isInvalid()) |
| 180 | return true; |
| 181 | if (!ChainResult.get()->getType()->isPointerType()) { |
| 182 | S.Diag(BuiltinLoc, diag::err_second_argument_to_cwsc_not_pointer) |
| 183 | << Chain->getSourceRange(); |
| 184 | return true; |
| 185 | } |
| 186 | |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 187 | QualType ReturnTy = CE->getCallReturnType(S.Context); |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 188 | QualType ArgTys[2] = { ReturnTy, ChainResult.get()->getType() }; |
| 189 | QualType BuiltinTy = S.Context.getFunctionType( |
| 190 | ReturnTy, ArgTys, FunctionProtoType::ExtProtoInfo()); |
| 191 | QualType BuiltinPtrTy = S.Context.getPointerType(BuiltinTy); |
| 192 | |
| 193 | Builtin = |
| 194 | S.ImpCastExprToType(Builtin, BuiltinPtrTy, CK_BuiltinFnToFnPtr).get(); |
| 195 | |
| 196 | BuiltinCall->setType(CE->getType()); |
| 197 | BuiltinCall->setValueKind(CE->getValueKind()); |
| 198 | BuiltinCall->setObjectKind(CE->getObjectKind()); |
| 199 | BuiltinCall->setCallee(Builtin); |
| 200 | BuiltinCall->setArg(1, ChainResult.get()); |
| 201 | |
| 202 | return false; |
| 203 | } |
| 204 | |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 205 | static bool SemaBuiltinSEHScopeCheck(Sema &SemaRef, CallExpr *TheCall, |
| 206 | Scope::ScopeFlags NeededScopeFlags, |
| 207 | unsigned DiagID) { |
| 208 | // Scopes aren't available during instantiation. Fortunately, builtin |
| 209 | // functions cannot be template args so they cannot be formed through template |
| 210 | // instantiation. Therefore checking once during the parse is sufficient. |
| 211 | if (!SemaRef.ActiveTemplateInstantiations.empty()) |
| 212 | return false; |
| 213 | |
| 214 | Scope *S = SemaRef.getCurScope(); |
| 215 | while (S && !S->isSEHExceptScope()) |
| 216 | S = S->getParent(); |
| 217 | if (!S || !(S->getFlags() & NeededScopeFlags)) { |
| 218 | auto *DRE = cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 219 | SemaRef.Diag(TheCall->getExprLoc(), DiagID) |
| 220 | << DRE->getDecl()->getIdentifier(); |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | return false; |
| 225 | } |
| 226 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 227 | ExprResult |
Fariborz Jahanian | 3e6a0be | 2014-09-18 17:58:27 +0000 | [diff] [blame] | 228 | Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID, |
| 229 | CallExpr *TheCall) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 230 | ExprResult TheCallResult(TheCall); |
Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 3be167f | 2010-10-01 23:23:24 +0000 | [diff] [blame] | 232 | // Find out if any arguments are required to be integer constant expressions. |
| 233 | unsigned ICEArguments = 0; |
| 234 | ASTContext::GetBuiltinTypeError Error; |
| 235 | Context.GetBuiltinType(BuiltinID, Error, &ICEArguments); |
| 236 | if (Error != ASTContext::GE_None) |
| 237 | ICEArguments = 0; // Don't diagnose previously diagnosed errors. |
| 238 | |
| 239 | // If any arguments are required to be ICE's, check and diagnose. |
| 240 | for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) { |
| 241 | // Skip arguments not required to be ICE's. |
| 242 | if ((ICEArguments & (1 << ArgNo)) == 0) continue; |
| 243 | |
| 244 | llvm::APSInt Result; |
| 245 | if (SemaBuiltinConstantArg(TheCall, ArgNo, Result)) |
| 246 | return true; |
| 247 | ICEArguments &= ~(1 << ArgNo); |
| 248 | } |
| 249 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 250 | switch (BuiltinID) { |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 251 | case Builtin::BI__builtin___CFStringMakeConstantString: |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 252 | assert(TheCall->getNumArgs() == 1 && |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 253 | "Wrong # arguments to builtin CFStringMakeConstantString"); |
Chris Lattner | 6436fb6 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 254 | if (CheckObjCString(TheCall->getArg(0))) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 255 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 256 | break; |
Ted Kremenek | a174c52 | 2008-07-09 17:58:53 +0000 | [diff] [blame] | 257 | case Builtin::BI__builtin_stdarg_start: |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 258 | case Builtin::BI__builtin_va_start: |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 259 | if (SemaBuiltinVAStart(TheCall)) |
| 260 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 261 | break; |
Saleem Abdulrasool | 202aac1 | 2014-07-22 02:01:04 +0000 | [diff] [blame] | 262 | case Builtin::BI__va_start: { |
| 263 | switch (Context.getTargetInfo().getTriple().getArch()) { |
| 264 | case llvm::Triple::arm: |
| 265 | case llvm::Triple::thumb: |
| 266 | if (SemaBuiltinVAStartARM(TheCall)) |
| 267 | return ExprError(); |
| 268 | break; |
| 269 | default: |
| 270 | if (SemaBuiltinVAStart(TheCall)) |
| 271 | return ExprError(); |
| 272 | break; |
| 273 | } |
| 274 | break; |
| 275 | } |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 276 | case Builtin::BI__builtin_isgreater: |
| 277 | case Builtin::BI__builtin_isgreaterequal: |
| 278 | case Builtin::BI__builtin_isless: |
| 279 | case Builtin::BI__builtin_islessequal: |
| 280 | case Builtin::BI__builtin_islessgreater: |
| 281 | case Builtin::BI__builtin_isunordered: |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 282 | if (SemaBuiltinUnorderedCompare(TheCall)) |
| 283 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 284 | break; |
Benjamin Kramer | 634fc10 | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 285 | case Builtin::BI__builtin_fpclassify: |
| 286 | if (SemaBuiltinFPClassification(TheCall, 6)) |
| 287 | return ExprError(); |
| 288 | break; |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 289 | case Builtin::BI__builtin_isfinite: |
| 290 | case Builtin::BI__builtin_isinf: |
| 291 | case Builtin::BI__builtin_isinf_sign: |
| 292 | case Builtin::BI__builtin_isnan: |
| 293 | case Builtin::BI__builtin_isnormal: |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 294 | if (SemaBuiltinFPClassification(TheCall, 1)) |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 295 | return ExprError(); |
| 296 | break; |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 297 | case Builtin::BI__builtin_shufflevector: |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 298 | return SemaBuiltinShuffleVector(TheCall); |
| 299 | // TheCall will be freed by the smart pointer here, but that's fine, since |
| 300 | // SemaBuiltinShuffleVector guts it, but then doesn't release it. |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 301 | case Builtin::BI__builtin_prefetch: |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 302 | if (SemaBuiltinPrefetch(TheCall)) |
| 303 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 304 | break; |
Hal Finkel | f041733 | 2014-07-17 14:25:55 +0000 | [diff] [blame] | 305 | case Builtin::BI__assume: |
Hal Finkel | bcc0608 | 2014-09-07 22:58:14 +0000 | [diff] [blame] | 306 | case Builtin::BI__builtin_assume: |
Hal Finkel | f041733 | 2014-07-17 14:25:55 +0000 | [diff] [blame] | 307 | if (SemaBuiltinAssume(TheCall)) |
| 308 | return ExprError(); |
| 309 | break; |
Hal Finkel | bcc0608 | 2014-09-07 22:58:14 +0000 | [diff] [blame] | 310 | case Builtin::BI__builtin_assume_aligned: |
| 311 | if (SemaBuiltinAssumeAligned(TheCall)) |
| 312 | return ExprError(); |
| 313 | break; |
Daniel Dunbar | b0d34c8 | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 314 | case Builtin::BI__builtin_object_size: |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 315 | if (SemaBuiltinConstantArgRange(TheCall, 1, 0, 3)) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 316 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 317 | break; |
Eli Friedman | eed8ad2 | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 318 | case Builtin::BI__builtin_longjmp: |
| 319 | if (SemaBuiltinLongjmp(TheCall)) |
| 320 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 321 | break; |
Joerg Sonnenberger | 2717328 | 2015-03-11 23:46:32 +0000 | [diff] [blame] | 322 | case Builtin::BI__builtin_setjmp: |
| 323 | if (SemaBuiltinSetjmp(TheCall)) |
| 324 | return ExprError(); |
| 325 | break; |
David Majnemer | c403a1c | 2015-03-20 17:03:35 +0000 | [diff] [blame] | 326 | case Builtin::BI_setjmp: |
| 327 | case Builtin::BI_setjmpex: |
| 328 | if (checkArgCount(*this, TheCall, 1)) |
| 329 | return true; |
| 330 | break; |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 331 | |
| 332 | case Builtin::BI__builtin_classify_type: |
| 333 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 334 | TheCall->setType(Context.IntTy); |
| 335 | break; |
Chris Lattner | 17c0eac | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 336 | case Builtin::BI__builtin_constant_p: |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 337 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 338 | TheCall->setType(Context.IntTy); |
Chris Lattner | 17c0eac | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 339 | break; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 340 | case Builtin::BI__sync_fetch_and_add: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 341 | case Builtin::BI__sync_fetch_and_add_1: |
| 342 | case Builtin::BI__sync_fetch_and_add_2: |
| 343 | case Builtin::BI__sync_fetch_and_add_4: |
| 344 | case Builtin::BI__sync_fetch_and_add_8: |
| 345 | case Builtin::BI__sync_fetch_and_add_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 346 | case Builtin::BI__sync_fetch_and_sub: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 347 | case Builtin::BI__sync_fetch_and_sub_1: |
| 348 | case Builtin::BI__sync_fetch_and_sub_2: |
| 349 | case Builtin::BI__sync_fetch_and_sub_4: |
| 350 | case Builtin::BI__sync_fetch_and_sub_8: |
| 351 | case Builtin::BI__sync_fetch_and_sub_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 352 | case Builtin::BI__sync_fetch_and_or: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 353 | case Builtin::BI__sync_fetch_and_or_1: |
| 354 | case Builtin::BI__sync_fetch_and_or_2: |
| 355 | case Builtin::BI__sync_fetch_and_or_4: |
| 356 | case Builtin::BI__sync_fetch_and_or_8: |
| 357 | case Builtin::BI__sync_fetch_and_or_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 358 | case Builtin::BI__sync_fetch_and_and: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 359 | case Builtin::BI__sync_fetch_and_and_1: |
| 360 | case Builtin::BI__sync_fetch_and_and_2: |
| 361 | case Builtin::BI__sync_fetch_and_and_4: |
| 362 | case Builtin::BI__sync_fetch_and_and_8: |
| 363 | case Builtin::BI__sync_fetch_and_and_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 364 | case Builtin::BI__sync_fetch_and_xor: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 365 | case Builtin::BI__sync_fetch_and_xor_1: |
| 366 | case Builtin::BI__sync_fetch_and_xor_2: |
| 367 | case Builtin::BI__sync_fetch_and_xor_4: |
| 368 | case Builtin::BI__sync_fetch_and_xor_8: |
| 369 | case Builtin::BI__sync_fetch_and_xor_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 370 | case Builtin::BI__sync_fetch_and_nand: |
| 371 | case Builtin::BI__sync_fetch_and_nand_1: |
| 372 | case Builtin::BI__sync_fetch_and_nand_2: |
| 373 | case Builtin::BI__sync_fetch_and_nand_4: |
| 374 | case Builtin::BI__sync_fetch_and_nand_8: |
| 375 | case Builtin::BI__sync_fetch_and_nand_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 376 | case Builtin::BI__sync_add_and_fetch: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 377 | case Builtin::BI__sync_add_and_fetch_1: |
| 378 | case Builtin::BI__sync_add_and_fetch_2: |
| 379 | case Builtin::BI__sync_add_and_fetch_4: |
| 380 | case Builtin::BI__sync_add_and_fetch_8: |
| 381 | case Builtin::BI__sync_add_and_fetch_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 382 | case Builtin::BI__sync_sub_and_fetch: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 383 | case Builtin::BI__sync_sub_and_fetch_1: |
| 384 | case Builtin::BI__sync_sub_and_fetch_2: |
| 385 | case Builtin::BI__sync_sub_and_fetch_4: |
| 386 | case Builtin::BI__sync_sub_and_fetch_8: |
| 387 | case Builtin::BI__sync_sub_and_fetch_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 388 | case Builtin::BI__sync_and_and_fetch: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 389 | case Builtin::BI__sync_and_and_fetch_1: |
| 390 | case Builtin::BI__sync_and_and_fetch_2: |
| 391 | case Builtin::BI__sync_and_and_fetch_4: |
| 392 | case Builtin::BI__sync_and_and_fetch_8: |
| 393 | case Builtin::BI__sync_and_and_fetch_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 394 | case Builtin::BI__sync_or_and_fetch: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 395 | case Builtin::BI__sync_or_and_fetch_1: |
| 396 | case Builtin::BI__sync_or_and_fetch_2: |
| 397 | case Builtin::BI__sync_or_and_fetch_4: |
| 398 | case Builtin::BI__sync_or_and_fetch_8: |
| 399 | case Builtin::BI__sync_or_and_fetch_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 400 | case Builtin::BI__sync_xor_and_fetch: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 401 | case Builtin::BI__sync_xor_and_fetch_1: |
| 402 | case Builtin::BI__sync_xor_and_fetch_2: |
| 403 | case Builtin::BI__sync_xor_and_fetch_4: |
| 404 | case Builtin::BI__sync_xor_and_fetch_8: |
| 405 | case Builtin::BI__sync_xor_and_fetch_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 406 | case Builtin::BI__sync_nand_and_fetch: |
| 407 | case Builtin::BI__sync_nand_and_fetch_1: |
| 408 | case Builtin::BI__sync_nand_and_fetch_2: |
| 409 | case Builtin::BI__sync_nand_and_fetch_4: |
| 410 | case Builtin::BI__sync_nand_and_fetch_8: |
| 411 | case Builtin::BI__sync_nand_and_fetch_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 412 | case Builtin::BI__sync_val_compare_and_swap: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 413 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 414 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 415 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 416 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 417 | case Builtin::BI__sync_val_compare_and_swap_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 418 | case Builtin::BI__sync_bool_compare_and_swap: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 419 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 420 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 421 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 422 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 423 | case Builtin::BI__sync_bool_compare_and_swap_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 424 | case Builtin::BI__sync_lock_test_and_set: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 425 | case Builtin::BI__sync_lock_test_and_set_1: |
| 426 | case Builtin::BI__sync_lock_test_and_set_2: |
| 427 | case Builtin::BI__sync_lock_test_and_set_4: |
| 428 | case Builtin::BI__sync_lock_test_and_set_8: |
| 429 | case Builtin::BI__sync_lock_test_and_set_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 430 | case Builtin::BI__sync_lock_release: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 431 | case Builtin::BI__sync_lock_release_1: |
| 432 | case Builtin::BI__sync_lock_release_2: |
| 433 | case Builtin::BI__sync_lock_release_4: |
| 434 | case Builtin::BI__sync_lock_release_8: |
| 435 | case Builtin::BI__sync_lock_release_16: |
Chris Lattner | 9cb59fa | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 436 | case Builtin::BI__sync_swap: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 437 | case Builtin::BI__sync_swap_1: |
| 438 | case Builtin::BI__sync_swap_2: |
| 439 | case Builtin::BI__sync_swap_4: |
| 440 | case Builtin::BI__sync_swap_8: |
| 441 | case Builtin::BI__sync_swap_16: |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 442 | return SemaBuiltinAtomicOverloaded(TheCallResult); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 443 | #define BUILTIN(ID, TYPE, ATTRS) |
| 444 | #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ |
| 445 | case Builtin::BI##ID: \ |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 446 | return SemaAtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 447 | #include "clang/Basic/Builtins.def" |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 448 | case Builtin::BI__builtin_annotation: |
Julien Lerouge | 4a5b444 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 449 | if (SemaBuiltinAnnotation(*this, TheCall)) |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 450 | return ExprError(); |
| 451 | break; |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 452 | case Builtin::BI__builtin_addressof: |
| 453 | if (SemaBuiltinAddressof(*this, TheCall)) |
| 454 | return ExprError(); |
| 455 | break; |
Richard Smith | 760520b | 2014-06-03 23:27:44 +0000 | [diff] [blame] | 456 | case Builtin::BI__builtin_operator_new: |
| 457 | case Builtin::BI__builtin_operator_delete: |
| 458 | if (!getLangOpts().CPlusPlus) { |
| 459 | Diag(TheCall->getExprLoc(), diag::err_builtin_requires_language) |
| 460 | << (BuiltinID == Builtin::BI__builtin_operator_new |
| 461 | ? "__builtin_operator_new" |
| 462 | : "__builtin_operator_delete") |
| 463 | << "C++"; |
| 464 | return ExprError(); |
| 465 | } |
| 466 | // CodeGen assumes it can find the global new and delete to call, |
| 467 | // so ensure that they are declared. |
| 468 | DeclareGlobalNewDelete(); |
| 469 | break; |
Fariborz Jahanian | 3e6a0be | 2014-09-18 17:58:27 +0000 | [diff] [blame] | 470 | |
| 471 | // check secure string manipulation functions where overflows |
| 472 | // are detectable at compile time |
| 473 | case Builtin::BI__builtin___memcpy_chk: |
Fariborz Jahanian | 3e6a0be | 2014-09-18 17:58:27 +0000 | [diff] [blame] | 474 | case Builtin::BI__builtin___memmove_chk: |
| 475 | case Builtin::BI__builtin___memset_chk: |
| 476 | case Builtin::BI__builtin___strlcat_chk: |
| 477 | case Builtin::BI__builtin___strlcpy_chk: |
| 478 | case Builtin::BI__builtin___strncat_chk: |
| 479 | case Builtin::BI__builtin___strncpy_chk: |
| 480 | case Builtin::BI__builtin___stpncpy_chk: |
| 481 | SemaBuiltinMemChkCall(*this, FDecl, TheCall, 2, 3); |
| 482 | break; |
Steven Wu | 566c14e | 2014-09-24 04:37:33 +0000 | [diff] [blame] | 483 | case Builtin::BI__builtin___memccpy_chk: |
| 484 | SemaBuiltinMemChkCall(*this, FDecl, TheCall, 3, 4); |
| 485 | break; |
Fariborz Jahanian | 3e6a0be | 2014-09-18 17:58:27 +0000 | [diff] [blame] | 486 | case Builtin::BI__builtin___snprintf_chk: |
| 487 | case Builtin::BI__builtin___vsnprintf_chk: |
| 488 | SemaBuiltinMemChkCall(*this, FDecl, TheCall, 1, 3); |
| 489 | break; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 490 | |
| 491 | case Builtin::BI__builtin_call_with_static_chain: |
| 492 | if (SemaBuiltinCallWithStaticChain(*this, TheCall)) |
| 493 | return ExprError(); |
| 494 | break; |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 495 | |
| 496 | case Builtin::BI__exception_code: |
| 497 | case Builtin::BI_exception_code: { |
| 498 | if (SemaBuiltinSEHScopeCheck(*this, TheCall, Scope::SEHExceptScope, |
| 499 | diag::err_seh___except_block)) |
| 500 | return ExprError(); |
| 501 | break; |
| 502 | } |
| 503 | case Builtin::BI__exception_info: |
| 504 | case Builtin::BI_exception_info: { |
| 505 | if (SemaBuiltinSEHScopeCheck(*this, TheCall, Scope::SEHFilterScope, |
| 506 | diag::err_seh___except_filter)) |
| 507 | return ExprError(); |
| 508 | break; |
| 509 | } |
| 510 | |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 511 | case Builtin::BI__GetExceptionInfo: |
| 512 | if (checkArgCount(*this, TheCall, 1)) |
| 513 | return ExprError(); |
| 514 | |
| 515 | if (CheckCXXThrowOperand( |
| 516 | TheCall->getLocStart(), |
| 517 | Context.getExceptionObjectType(FDecl->getParamDecl(0)->getType()), |
| 518 | TheCall)) |
| 519 | return ExprError(); |
| 520 | |
| 521 | TheCall->setType(Context.VoidPtrTy); |
| 522 | break; |
| 523 | |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 524 | } |
Richard Smith | 760520b | 2014-06-03 23:27:44 +0000 | [diff] [blame] | 525 | |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 526 | // Since the target specific builtins for each arch overlap, only check those |
| 527 | // of the arch we are compiling for. |
| 528 | if (BuiltinID >= Builtin::FirstTSBuiltin) { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 529 | switch (Context.getTargetInfo().getTriple().getArch()) { |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 530 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 531 | case llvm::Triple::armeb: |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 532 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 533 | case llvm::Triple::thumbeb: |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 534 | if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall)) |
| 535 | return ExprError(); |
| 536 | break; |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 537 | case llvm::Triple::aarch64: |
| 538 | case llvm::Triple::aarch64_be: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 539 | if (CheckAArch64BuiltinFunctionCall(BuiltinID, TheCall)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 540 | return ExprError(); |
| 541 | break; |
Simon Atanasyan | ecedf3d | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 542 | case llvm::Triple::mips: |
| 543 | case llvm::Triple::mipsel: |
| 544 | case llvm::Triple::mips64: |
| 545 | case llvm::Triple::mips64el: |
| 546 | if (CheckMipsBuiltinFunctionCall(BuiltinID, TheCall)) |
| 547 | return ExprError(); |
| 548 | break; |
Ulrich Weigand | 3a610eb | 2015-04-01 12:54:25 +0000 | [diff] [blame] | 549 | case llvm::Triple::systemz: |
| 550 | if (CheckSystemZBuiltinFunctionCall(BuiltinID, TheCall)) |
| 551 | return ExprError(); |
| 552 | break; |
Warren Hunt | 20e4a5d | 2014-02-21 23:08:53 +0000 | [diff] [blame] | 553 | case llvm::Triple::x86: |
| 554 | case llvm::Triple::x86_64: |
| 555 | if (CheckX86BuiltinFunctionCall(BuiltinID, TheCall)) |
| 556 | return ExprError(); |
| 557 | break; |
Kit Barton | e50adcb | 2015-03-30 19:40:59 +0000 | [diff] [blame] | 558 | case llvm::Triple::ppc: |
| 559 | case llvm::Triple::ppc64: |
| 560 | case llvm::Triple::ppc64le: |
| 561 | if (CheckPPCBuiltinFunctionCall(BuiltinID, TheCall)) |
| 562 | return ExprError(); |
| 563 | break; |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 564 | default: |
| 565 | break; |
| 566 | } |
| 567 | } |
| 568 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 569 | return TheCallResult; |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 572 | // Get the valid immediate range for the specified NEON type code. |
Tim Northover | 3402dc7 | 2014-02-12 12:04:59 +0000 | [diff] [blame] | 573 | static unsigned RFT(unsigned t, bool shift = false, bool ForceQuad = false) { |
Bob Wilson | 98bc98c | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 574 | NeonTypeFlags Type(t); |
Tim Northover | 3402dc7 | 2014-02-12 12:04:59 +0000 | [diff] [blame] | 575 | int IsQuad = ForceQuad ? true : Type.isQuad(); |
Bob Wilson | 98bc98c | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 576 | switch (Type.getEltType()) { |
| 577 | case NeonTypeFlags::Int8: |
| 578 | case NeonTypeFlags::Poly8: |
| 579 | return shift ? 7 : (8 << IsQuad) - 1; |
| 580 | case NeonTypeFlags::Int16: |
| 581 | case NeonTypeFlags::Poly16: |
| 582 | return shift ? 15 : (4 << IsQuad) - 1; |
| 583 | case NeonTypeFlags::Int32: |
| 584 | return shift ? 31 : (2 << IsQuad) - 1; |
| 585 | case NeonTypeFlags::Int64: |
Kevin Qin | caac85e | 2013-11-14 03:29:16 +0000 | [diff] [blame] | 586 | case NeonTypeFlags::Poly64: |
Bob Wilson | 98bc98c | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 587 | return shift ? 63 : (1 << IsQuad) - 1; |
Kevin Qin | fb79d7f | 2013-12-10 06:49:01 +0000 | [diff] [blame] | 588 | case NeonTypeFlags::Poly128: |
| 589 | return shift ? 127 : (1 << IsQuad) - 1; |
Bob Wilson | 98bc98c | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 590 | case NeonTypeFlags::Float16: |
| 591 | assert(!shift && "cannot shift float types!"); |
| 592 | return (4 << IsQuad) - 1; |
| 593 | case NeonTypeFlags::Float32: |
| 594 | assert(!shift && "cannot shift float types!"); |
| 595 | return (2 << IsQuad) - 1; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 596 | case NeonTypeFlags::Float64: |
| 597 | assert(!shift && "cannot shift float types!"); |
| 598 | return (1 << IsQuad) - 1; |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 599 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 600 | llvm_unreachable("Invalid NeonTypeFlag!"); |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 603 | /// getNeonEltType - Return the QualType corresponding to the elements of |
| 604 | /// the vector type specified by the NeonTypeFlags. This is used to check |
| 605 | /// the pointer arguments for Neon load/store intrinsics. |
Kevin Qin | caac85e | 2013-11-14 03:29:16 +0000 | [diff] [blame] | 606 | static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context, |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 607 | bool IsPolyUnsigned, bool IsInt64Long) { |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 608 | switch (Flags.getEltType()) { |
| 609 | case NeonTypeFlags::Int8: |
| 610 | return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy; |
| 611 | case NeonTypeFlags::Int16: |
| 612 | return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy; |
| 613 | case NeonTypeFlags::Int32: |
| 614 | return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy; |
| 615 | case NeonTypeFlags::Int64: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 616 | if (IsInt64Long) |
Kevin Qin | ad64f6d | 2014-02-24 02:45:03 +0000 | [diff] [blame] | 617 | return Flags.isUnsigned() ? Context.UnsignedLongTy : Context.LongTy; |
| 618 | else |
| 619 | return Flags.isUnsigned() ? Context.UnsignedLongLongTy |
| 620 | : Context.LongLongTy; |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 621 | case NeonTypeFlags::Poly8: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 622 | return IsPolyUnsigned ? Context.UnsignedCharTy : Context.SignedCharTy; |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 623 | case NeonTypeFlags::Poly16: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 624 | return IsPolyUnsigned ? Context.UnsignedShortTy : Context.ShortTy; |
Kevin Qin | caac85e | 2013-11-14 03:29:16 +0000 | [diff] [blame] | 625 | case NeonTypeFlags::Poly64: |
Kevin Qin | 78b8653 | 2015-05-14 08:18:05 +0000 | [diff] [blame] | 626 | if (IsInt64Long) |
| 627 | return Context.UnsignedLongTy; |
| 628 | else |
| 629 | return Context.UnsignedLongLongTy; |
Kevin Qin | fb79d7f | 2013-12-10 06:49:01 +0000 | [diff] [blame] | 630 | case NeonTypeFlags::Poly128: |
| 631 | break; |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 632 | case NeonTypeFlags::Float16: |
Kevin Qin | caac85e | 2013-11-14 03:29:16 +0000 | [diff] [blame] | 633 | return Context.HalfTy; |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 634 | case NeonTypeFlags::Float32: |
| 635 | return Context.FloatTy; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 636 | case NeonTypeFlags::Float64: |
| 637 | return Context.DoubleTy; |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 638 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 639 | llvm_unreachable("Invalid NeonTypeFlag!"); |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 642 | bool Sema::CheckNeonBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 643 | llvm::APSInt Result; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 644 | uint64_t mask = 0; |
| 645 | unsigned TV = 0; |
| 646 | int PtrArgNum = -1; |
| 647 | bool HasConstPtr = false; |
| 648 | switch (BuiltinID) { |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 649 | #define GET_NEON_OVERLOAD_CHECK |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 650 | #include "clang/Basic/arm_neon.inc" |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 651 | #undef GET_NEON_OVERLOAD_CHECK |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | // For NEON intrinsics which are overloaded on vector element type, validate |
| 655 | // the immediate which specifies which variant to emit. |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 656 | unsigned ImmArg = TheCall->getNumArgs()-1; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 657 | if (mask) { |
| 658 | if (SemaBuiltinConstantArg(TheCall, ImmArg, Result)) |
| 659 | return true; |
| 660 | |
| 661 | TV = Result.getLimitedValue(64); |
| 662 | if ((TV > 63) || (mask & (1ULL << TV)) == 0) |
| 663 | return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code) |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 664 | << TheCall->getArg(ImmArg)->getSourceRange(); |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | if (PtrArgNum >= 0) { |
| 668 | // Check that pointer arguments have the specified type. |
| 669 | Expr *Arg = TheCall->getArg(PtrArgNum); |
| 670 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) |
| 671 | Arg = ICE->getSubExpr(); |
| 672 | ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg); |
| 673 | QualType RHSTy = RHS.get()->getType(); |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 674 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 675 | llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch(); |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 676 | bool IsPolyUnsigned = Arch == llvm::Triple::aarch64; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 677 | bool IsInt64Long = |
| 678 | Context.getTargetInfo().getInt64Type() == TargetInfo::SignedLong; |
| 679 | QualType EltTy = |
| 680 | getNeonEltType(NeonTypeFlags(TV), Context, IsPolyUnsigned, IsInt64Long); |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 681 | if (HasConstPtr) |
| 682 | EltTy = EltTy.withConst(); |
| 683 | QualType LHSTy = Context.getPointerType(EltTy); |
| 684 | AssignConvertType ConvTy; |
| 685 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
| 686 | if (RHS.isInvalid()) |
| 687 | return true; |
| 688 | if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), LHSTy, RHSTy, |
| 689 | RHS.get(), AA_Assigning)) |
| 690 | return true; |
| 691 | } |
| 692 | |
| 693 | // For NEON intrinsics which take an immediate value as part of the |
| 694 | // instruction, range check them here. |
| 695 | unsigned i = 0, l = 0, u = 0; |
| 696 | switch (BuiltinID) { |
| 697 | default: |
| 698 | return false; |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 699 | #define GET_NEON_IMMEDIATE_CHECK |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 700 | #include "clang/Basic/arm_neon.inc" |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 701 | #undef GET_NEON_IMMEDIATE_CHECK |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 702 | } |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 703 | |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 704 | return SemaBuiltinConstantArgRange(TheCall, i, l, u + l); |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 707 | bool Sema::CheckARMBuiltinExclusiveCall(unsigned BuiltinID, CallExpr *TheCall, |
| 708 | unsigned MaxWidth) { |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 709 | assert((BuiltinID == ARM::BI__builtin_arm_ldrex || |
Tim Northover | 3acd6bd | 2014-07-02 12:56:02 +0000 | [diff] [blame] | 710 | BuiltinID == ARM::BI__builtin_arm_ldaex || |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 711 | BuiltinID == ARM::BI__builtin_arm_strex || |
Tim Northover | 3acd6bd | 2014-07-02 12:56:02 +0000 | [diff] [blame] | 712 | BuiltinID == ARM::BI__builtin_arm_stlex || |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 713 | BuiltinID == AArch64::BI__builtin_arm_ldrex || |
Tim Northover | 3acd6bd | 2014-07-02 12:56:02 +0000 | [diff] [blame] | 714 | BuiltinID == AArch64::BI__builtin_arm_ldaex || |
| 715 | BuiltinID == AArch64::BI__builtin_arm_strex || |
| 716 | BuiltinID == AArch64::BI__builtin_arm_stlex) && |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 717 | "unexpected ARM builtin"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 718 | bool IsLdrex = BuiltinID == ARM::BI__builtin_arm_ldrex || |
Tim Northover | 3acd6bd | 2014-07-02 12:56:02 +0000 | [diff] [blame] | 719 | BuiltinID == ARM::BI__builtin_arm_ldaex || |
| 720 | BuiltinID == AArch64::BI__builtin_arm_ldrex || |
| 721 | BuiltinID == AArch64::BI__builtin_arm_ldaex; |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 722 | |
| 723 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 724 | |
| 725 | // Ensure that we have the proper number of arguments. |
| 726 | if (checkArgCount(*this, TheCall, IsLdrex ? 1 : 2)) |
| 727 | return true; |
| 728 | |
| 729 | // Inspect the pointer argument of the atomic builtin. This should always be |
| 730 | // a pointer type, whose element is an integral scalar or pointer type. |
| 731 | // Because it is a pointer type, we don't have to worry about any implicit |
| 732 | // casts here. |
| 733 | Expr *PointerArg = TheCall->getArg(IsLdrex ? 0 : 1); |
| 734 | ExprResult PointerArgRes = DefaultFunctionArrayLvalueConversion(PointerArg); |
| 735 | if (PointerArgRes.isInvalid()) |
| 736 | return true; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 737 | PointerArg = PointerArgRes.get(); |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 738 | |
| 739 | const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>(); |
| 740 | if (!pointerType) { |
| 741 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
| 742 | << PointerArg->getType() << PointerArg->getSourceRange(); |
| 743 | return true; |
| 744 | } |
| 745 | |
| 746 | // ldrex takes a "const volatile T*" and strex takes a "volatile T*". Our next |
| 747 | // task is to insert the appropriate casts into the AST. First work out just |
| 748 | // what the appropriate type is. |
| 749 | QualType ValType = pointerType->getPointeeType(); |
| 750 | QualType AddrType = ValType.getUnqualifiedType().withVolatile(); |
| 751 | if (IsLdrex) |
| 752 | AddrType.addConst(); |
| 753 | |
| 754 | // Issue a warning if the cast is dodgy. |
| 755 | CastKind CastNeeded = CK_NoOp; |
| 756 | if (!AddrType.isAtLeastAsQualifiedAs(ValType)) { |
| 757 | CastNeeded = CK_BitCast; |
| 758 | Diag(DRE->getLocStart(), diag::ext_typecheck_convert_discards_qualifiers) |
| 759 | << PointerArg->getType() |
| 760 | << Context.getPointerType(AddrType) |
| 761 | << AA_Passing << PointerArg->getSourceRange(); |
| 762 | } |
| 763 | |
| 764 | // Finally, do the cast and replace the argument with the corrected version. |
| 765 | AddrType = Context.getPointerType(AddrType); |
| 766 | PointerArgRes = ImpCastExprToType(PointerArg, AddrType, CastNeeded); |
| 767 | if (PointerArgRes.isInvalid()) |
| 768 | return true; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 769 | PointerArg = PointerArgRes.get(); |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 770 | |
| 771 | TheCall->setArg(IsLdrex ? 0 : 1, PointerArg); |
| 772 | |
| 773 | // In general, we allow ints, floats and pointers to be loaded and stored. |
| 774 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && |
| 775 | !ValType->isBlockPointerType() && !ValType->isFloatingType()) { |
| 776 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intfltptr) |
| 777 | << PointerArg->getType() << PointerArg->getSourceRange(); |
| 778 | return true; |
| 779 | } |
| 780 | |
| 781 | // But ARM doesn't have instructions to deal with 128-bit versions. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 782 | if (Context.getTypeSize(ValType) > MaxWidth) { |
| 783 | assert(MaxWidth == 64 && "Diagnostic unexpectedly inaccurate"); |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 784 | Diag(DRE->getLocStart(), diag::err_atomic_exclusive_builtin_pointer_size) |
| 785 | << PointerArg->getType() << PointerArg->getSourceRange(); |
| 786 | return true; |
| 787 | } |
| 788 | |
| 789 | switch (ValType.getObjCLifetime()) { |
| 790 | case Qualifiers::OCL_None: |
| 791 | case Qualifiers::OCL_ExplicitNone: |
| 792 | // okay |
| 793 | break; |
| 794 | |
| 795 | case Qualifiers::OCL_Weak: |
| 796 | case Qualifiers::OCL_Strong: |
| 797 | case Qualifiers::OCL_Autoreleasing: |
| 798 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
| 799 | << ValType << PointerArg->getSourceRange(); |
| 800 | return true; |
| 801 | } |
| 802 | |
| 803 | |
| 804 | if (IsLdrex) { |
| 805 | TheCall->setType(ValType); |
| 806 | return false; |
| 807 | } |
| 808 | |
| 809 | // Initialize the argument to be stored. |
| 810 | ExprResult ValArg = TheCall->getArg(0); |
| 811 | InitializedEntity Entity = InitializedEntity::InitializeParameter( |
| 812 | Context, ValType, /*consume*/ false); |
| 813 | ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg); |
| 814 | if (ValArg.isInvalid()) |
| 815 | return true; |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 816 | TheCall->setArg(0, ValArg.get()); |
Tim Northover | 58d2bb1 | 2013-10-29 12:32:58 +0000 | [diff] [blame] | 817 | |
| 818 | // __builtin_arm_strex always returns an int. It's marked as such in the .def, |
| 819 | // but the custom checker bypasses all default analysis. |
| 820 | TheCall->setType(Context.IntTy); |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 821 | return false; |
| 822 | } |
| 823 | |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 824 | bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
Nate Begeman | 5548309 | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 825 | llvm::APSInt Result; |
| 826 | |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 827 | if (BuiltinID == ARM::BI__builtin_arm_ldrex || |
Tim Northover | 3acd6bd | 2014-07-02 12:56:02 +0000 | [diff] [blame] | 828 | BuiltinID == ARM::BI__builtin_arm_ldaex || |
| 829 | BuiltinID == ARM::BI__builtin_arm_strex || |
| 830 | BuiltinID == ARM::BI__builtin_arm_stlex) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 831 | return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 64); |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Yi Kong | 26d104a | 2014-08-13 19:18:14 +0000 | [diff] [blame] | 834 | if (BuiltinID == ARM::BI__builtin_arm_prefetch) { |
| 835 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) || |
| 836 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 1); |
| 837 | } |
| 838 | |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 839 | if (CheckNeonBuiltinFunctionCall(BuiltinID, TheCall)) |
| 840 | return true; |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 841 | |
Yi Kong | 4efadfb | 2014-07-03 16:01:25 +0000 | [diff] [blame] | 842 | // For intrinsics which take an immediate value as part of the instruction, |
| 843 | // range check them here. |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 844 | unsigned i = 0, l = 0, u = 0; |
Nate Begeman | d773fe6 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 845 | switch (BuiltinID) { |
| 846 | default: return false; |
Nate Begeman | 1194bd2 | 2010-07-29 22:48:34 +0000 | [diff] [blame] | 847 | case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break; |
| 848 | case ARM::BI__builtin_arm_usat: i = 1; u = 31; break; |
Nate Begeman | f568b07 | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 849 | case ARM::BI__builtin_arm_vcvtr_f: |
| 850 | case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break; |
Weiming Zhao | 87bb492 | 2013-11-12 21:42:50 +0000 | [diff] [blame] | 851 | case ARM::BI__builtin_arm_dmb: |
Yi Kong | 4efadfb | 2014-07-03 16:01:25 +0000 | [diff] [blame] | 852 | case ARM::BI__builtin_arm_dsb: |
Yi Kong | 1d268af | 2014-08-26 12:48:06 +0000 | [diff] [blame] | 853 | case ARM::BI__builtin_arm_isb: |
| 854 | case ARM::BI__builtin_arm_dbg: l = 0; u = 15; break; |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 855 | } |
Nate Begeman | d773fe6 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 856 | |
Nate Begeman | f568b07 | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 857 | // FIXME: VFP Intrinsics should error if VFP not present. |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 858 | return SemaBuiltinConstantArgRange(TheCall, i, l, u + l); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 859 | } |
Daniel Dunbar | dd9b2d1 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 860 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 861 | bool Sema::CheckAArch64BuiltinFunctionCall(unsigned BuiltinID, |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 862 | CallExpr *TheCall) { |
| 863 | llvm::APSInt Result; |
| 864 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 865 | if (BuiltinID == AArch64::BI__builtin_arm_ldrex || |
Tim Northover | 3acd6bd | 2014-07-02 12:56:02 +0000 | [diff] [blame] | 866 | BuiltinID == AArch64::BI__builtin_arm_ldaex || |
| 867 | BuiltinID == AArch64::BI__builtin_arm_strex || |
| 868 | BuiltinID == AArch64::BI__builtin_arm_stlex) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 869 | return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 128); |
| 870 | } |
| 871 | |
Yi Kong | a554843 | 2014-08-13 19:18:20 +0000 | [diff] [blame] | 872 | if (BuiltinID == AArch64::BI__builtin_arm_prefetch) { |
| 873 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) || |
| 874 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 2) || |
| 875 | SemaBuiltinConstantArgRange(TheCall, 3, 0, 1) || |
| 876 | SemaBuiltinConstantArgRange(TheCall, 4, 0, 1); |
| 877 | } |
| 878 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 879 | if (CheckNeonBuiltinFunctionCall(BuiltinID, TheCall)) |
| 880 | return true; |
| 881 | |
Yi Kong | 19a29ac | 2014-07-17 10:52:06 +0000 | [diff] [blame] | 882 | // For intrinsics which take an immediate value as part of the instruction, |
| 883 | // range check them here. |
| 884 | unsigned i = 0, l = 0, u = 0; |
| 885 | switch (BuiltinID) { |
| 886 | default: return false; |
| 887 | case AArch64::BI__builtin_arm_dmb: |
| 888 | case AArch64::BI__builtin_arm_dsb: |
| 889 | case AArch64::BI__builtin_arm_isb: l = 0; u = 15; break; |
| 890 | } |
| 891 | |
Yi Kong | 19a29ac | 2014-07-17 10:52:06 +0000 | [diff] [blame] | 892 | return SemaBuiltinConstantArgRange(TheCall, i, l, u + l); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Simon Atanasyan | ecedf3d | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 895 | bool Sema::CheckMipsBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
| 896 | unsigned i = 0, l = 0, u = 0; |
| 897 | switch (BuiltinID) { |
| 898 | default: return false; |
| 899 | case Mips::BI__builtin_mips_wrdsp: i = 1; l = 0; u = 63; break; |
| 900 | 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] | 901 | case Mips::BI__builtin_mips_append: i = 2; l = 0; u = 31; break; |
| 902 | case Mips::BI__builtin_mips_balign: i = 2; l = 0; u = 3; break; |
| 903 | case Mips::BI__builtin_mips_precr_sra_ph_w: i = 2; l = 0; u = 31; break; |
| 904 | case Mips::BI__builtin_mips_precr_sra_r_ph_w: i = 2; l = 0; u = 31; break; |
| 905 | 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] | 906 | } |
Simon Atanasyan | ecedf3d | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 907 | |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 908 | return SemaBuiltinConstantArgRange(TheCall, i, l, u); |
Simon Atanasyan | ecedf3d | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Kit Barton | e50adcb | 2015-03-30 19:40:59 +0000 | [diff] [blame] | 911 | bool Sema::CheckPPCBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
| 912 | unsigned i = 0, l = 0, u = 0; |
Nemanja Ivanovic | 239eec7 | 2015-04-09 23:58:16 +0000 | [diff] [blame] | 913 | bool Is64BitBltin = BuiltinID == PPC::BI__builtin_divde || |
| 914 | BuiltinID == PPC::BI__builtin_divdeu || |
| 915 | BuiltinID == PPC::BI__builtin_bpermd; |
| 916 | bool IsTarget64Bit = Context.getTargetInfo() |
| 917 | .getTypeWidth(Context |
| 918 | .getTargetInfo() |
| 919 | .getIntPtrType()) == 64; |
| 920 | bool IsBltinExtDiv = BuiltinID == PPC::BI__builtin_divwe || |
| 921 | BuiltinID == PPC::BI__builtin_divweu || |
| 922 | BuiltinID == PPC::BI__builtin_divde || |
| 923 | BuiltinID == PPC::BI__builtin_divdeu; |
| 924 | |
| 925 | if (Is64BitBltin && !IsTarget64Bit) |
| 926 | return Diag(TheCall->getLocStart(), diag::err_64_bit_builtin_32_bit_tgt) |
| 927 | << TheCall->getSourceRange(); |
| 928 | |
| 929 | if ((IsBltinExtDiv && !Context.getTargetInfo().hasFeature("extdiv")) || |
| 930 | (BuiltinID == PPC::BI__builtin_bpermd && |
| 931 | !Context.getTargetInfo().hasFeature("bpermd"))) |
| 932 | return Diag(TheCall->getLocStart(), diag::err_ppc_builtin_only_on_pwr7) |
| 933 | << TheCall->getSourceRange(); |
| 934 | |
Kit Barton | e50adcb | 2015-03-30 19:40:59 +0000 | [diff] [blame] | 935 | switch (BuiltinID) { |
| 936 | default: return false; |
| 937 | case PPC::BI__builtin_altivec_crypto_vshasigmaw: |
| 938 | case PPC::BI__builtin_altivec_crypto_vshasigmad: |
| 939 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) || |
| 940 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 15); |
| 941 | case PPC::BI__builtin_tbegin: |
| 942 | case PPC::BI__builtin_tend: i = 0; l = 0; u = 1; break; |
| 943 | case PPC::BI__builtin_tsr: i = 0; l = 0; u = 7; break; |
| 944 | case PPC::BI__builtin_tabortwc: |
| 945 | case PPC::BI__builtin_tabortdc: i = 0; l = 0; u = 31; break; |
| 946 | case PPC::BI__builtin_tabortwci: |
| 947 | case PPC::BI__builtin_tabortdci: |
| 948 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 31) || |
| 949 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 31); |
| 950 | } |
| 951 | return SemaBuiltinConstantArgRange(TheCall, i, l, u); |
| 952 | } |
| 953 | |
Ulrich Weigand | 3a610eb | 2015-04-01 12:54:25 +0000 | [diff] [blame] | 954 | bool Sema::CheckSystemZBuiltinFunctionCall(unsigned BuiltinID, |
| 955 | CallExpr *TheCall) { |
| 956 | if (BuiltinID == SystemZ::BI__builtin_tabort) { |
| 957 | Expr *Arg = TheCall->getArg(0); |
| 958 | llvm::APSInt AbortCode(32); |
| 959 | if (Arg->isIntegerConstantExpr(AbortCode, Context) && |
| 960 | AbortCode.getSExtValue() >= 0 && AbortCode.getSExtValue() < 256) |
| 961 | return Diag(Arg->getLocStart(), diag::err_systemz_invalid_tabort_code) |
| 962 | << Arg->getSourceRange(); |
| 963 | } |
| 964 | |
Ulrich Weigand | 5722c0f | 2015-05-05 19:36:42 +0000 | [diff] [blame] | 965 | // For intrinsics which take an immediate value as part of the instruction, |
| 966 | // range check them here. |
| 967 | unsigned i = 0, l = 0, u = 0; |
| 968 | switch (BuiltinID) { |
| 969 | default: return false; |
| 970 | case SystemZ::BI__builtin_s390_lcbb: i = 1; l = 0; u = 15; break; |
| 971 | case SystemZ::BI__builtin_s390_verimb: |
| 972 | case SystemZ::BI__builtin_s390_verimh: |
| 973 | case SystemZ::BI__builtin_s390_verimf: |
| 974 | case SystemZ::BI__builtin_s390_verimg: i = 3; l = 0; u = 255; break; |
| 975 | case SystemZ::BI__builtin_s390_vfaeb: |
| 976 | case SystemZ::BI__builtin_s390_vfaeh: |
| 977 | case SystemZ::BI__builtin_s390_vfaef: |
| 978 | case SystemZ::BI__builtin_s390_vfaebs: |
| 979 | case SystemZ::BI__builtin_s390_vfaehs: |
| 980 | case SystemZ::BI__builtin_s390_vfaefs: |
| 981 | case SystemZ::BI__builtin_s390_vfaezb: |
| 982 | case SystemZ::BI__builtin_s390_vfaezh: |
| 983 | case SystemZ::BI__builtin_s390_vfaezf: |
| 984 | case SystemZ::BI__builtin_s390_vfaezbs: |
| 985 | case SystemZ::BI__builtin_s390_vfaezhs: |
| 986 | case SystemZ::BI__builtin_s390_vfaezfs: i = 2; l = 0; u = 15; break; |
| 987 | case SystemZ::BI__builtin_s390_vfidb: |
| 988 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15) || |
| 989 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 15); |
| 990 | case SystemZ::BI__builtin_s390_vftcidb: i = 1; l = 0; u = 4095; break; |
| 991 | case SystemZ::BI__builtin_s390_vlbb: i = 1; l = 0; u = 15; break; |
| 992 | case SystemZ::BI__builtin_s390_vpdi: i = 2; l = 0; u = 15; break; |
| 993 | case SystemZ::BI__builtin_s390_vsldb: i = 2; l = 0; u = 15; break; |
| 994 | case SystemZ::BI__builtin_s390_vstrcb: |
| 995 | case SystemZ::BI__builtin_s390_vstrch: |
| 996 | case SystemZ::BI__builtin_s390_vstrcf: |
| 997 | case SystemZ::BI__builtin_s390_vstrczb: |
| 998 | case SystemZ::BI__builtin_s390_vstrczh: |
| 999 | case SystemZ::BI__builtin_s390_vstrczf: |
| 1000 | case SystemZ::BI__builtin_s390_vstrcbs: |
| 1001 | case SystemZ::BI__builtin_s390_vstrchs: |
| 1002 | case SystemZ::BI__builtin_s390_vstrcfs: |
| 1003 | case SystemZ::BI__builtin_s390_vstrczbs: |
| 1004 | case SystemZ::BI__builtin_s390_vstrczhs: |
| 1005 | case SystemZ::BI__builtin_s390_vstrczfs: i = 3; l = 0; u = 15; break; |
| 1006 | } |
| 1007 | return SemaBuiltinConstantArgRange(TheCall, i, l, u); |
Ulrich Weigand | 3a610eb | 2015-04-01 12:54:25 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Warren Hunt | 20e4a5d | 2014-02-21 23:08:53 +0000 | [diff] [blame] | 1010 | bool Sema::CheckX86BuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
Craig Topper | dd84ec5 | 2014-12-27 07:00:08 +0000 | [diff] [blame] | 1011 | unsigned i = 0, l = 0, u = 0; |
Warren Hunt | 20e4a5d | 2014-02-21 23:08:53 +0000 | [diff] [blame] | 1012 | switch (BuiltinID) { |
Craig Topper | dd84ec5 | 2014-12-27 07:00:08 +0000 | [diff] [blame] | 1013 | default: return false; |
| 1014 | case X86::BI_mm_prefetch: i = 1; l = 0; u = 3; break; |
Craig Topper | 1601525 | 2015-01-31 06:31:23 +0000 | [diff] [blame] | 1015 | case X86::BI__builtin_ia32_sha1rnds4: i = 2, l = 0; u = 3; break; |
Craig Topper | 1a8b047 | 2015-01-31 08:57:52 +0000 | [diff] [blame] | 1016 | case X86::BI__builtin_ia32_vpermil2pd: |
| 1017 | case X86::BI__builtin_ia32_vpermil2pd256: |
| 1018 | case X86::BI__builtin_ia32_vpermil2ps: |
| 1019 | case X86::BI__builtin_ia32_vpermil2ps256: i = 3, l = 0; u = 3; break; |
Craig Topper | 95b0d73 | 2015-01-25 23:30:05 +0000 | [diff] [blame] | 1020 | case X86::BI__builtin_ia32_cmpb128_mask: |
| 1021 | case X86::BI__builtin_ia32_cmpw128_mask: |
| 1022 | case X86::BI__builtin_ia32_cmpd128_mask: |
| 1023 | case X86::BI__builtin_ia32_cmpq128_mask: |
| 1024 | case X86::BI__builtin_ia32_cmpb256_mask: |
| 1025 | case X86::BI__builtin_ia32_cmpw256_mask: |
| 1026 | case X86::BI__builtin_ia32_cmpd256_mask: |
| 1027 | case X86::BI__builtin_ia32_cmpq256_mask: |
| 1028 | case X86::BI__builtin_ia32_cmpb512_mask: |
| 1029 | case X86::BI__builtin_ia32_cmpw512_mask: |
| 1030 | case X86::BI__builtin_ia32_cmpd512_mask: |
| 1031 | case X86::BI__builtin_ia32_cmpq512_mask: |
| 1032 | case X86::BI__builtin_ia32_ucmpb128_mask: |
| 1033 | case X86::BI__builtin_ia32_ucmpw128_mask: |
| 1034 | case X86::BI__builtin_ia32_ucmpd128_mask: |
| 1035 | case X86::BI__builtin_ia32_ucmpq128_mask: |
| 1036 | case X86::BI__builtin_ia32_ucmpb256_mask: |
| 1037 | case X86::BI__builtin_ia32_ucmpw256_mask: |
| 1038 | case X86::BI__builtin_ia32_ucmpd256_mask: |
| 1039 | case X86::BI__builtin_ia32_ucmpq256_mask: |
| 1040 | case X86::BI__builtin_ia32_ucmpb512_mask: |
| 1041 | case X86::BI__builtin_ia32_ucmpw512_mask: |
| 1042 | case X86::BI__builtin_ia32_ucmpd512_mask: |
| 1043 | case X86::BI__builtin_ia32_ucmpq512_mask: i = 2; l = 0; u = 7; break; |
Craig Topper | 1601525 | 2015-01-31 06:31:23 +0000 | [diff] [blame] | 1044 | case X86::BI__builtin_ia32_roundps: |
| 1045 | case X86::BI__builtin_ia32_roundpd: |
| 1046 | case X86::BI__builtin_ia32_roundps256: |
| 1047 | case X86::BI__builtin_ia32_roundpd256: i = 1, l = 0; u = 15; break; |
| 1048 | case X86::BI__builtin_ia32_roundss: |
| 1049 | case X86::BI__builtin_ia32_roundsd: i = 2, l = 0; u = 15; break; |
| 1050 | case X86::BI__builtin_ia32_cmpps: |
| 1051 | case X86::BI__builtin_ia32_cmpss: |
| 1052 | case X86::BI__builtin_ia32_cmppd: |
| 1053 | case X86::BI__builtin_ia32_cmpsd: |
| 1054 | case X86::BI__builtin_ia32_cmpps256: |
| 1055 | case X86::BI__builtin_ia32_cmppd256: |
| 1056 | case X86::BI__builtin_ia32_cmpps512_mask: |
| 1057 | case X86::BI__builtin_ia32_cmppd512_mask: i = 2; l = 0; u = 31; break; |
Craig Topper | 8dd7d0d | 2015-02-13 06:04:48 +0000 | [diff] [blame] | 1058 | case X86::BI__builtin_ia32_vpcomub: |
| 1059 | case X86::BI__builtin_ia32_vpcomuw: |
| 1060 | case X86::BI__builtin_ia32_vpcomud: |
| 1061 | case X86::BI__builtin_ia32_vpcomuq: |
| 1062 | case X86::BI__builtin_ia32_vpcomb: |
| 1063 | case X86::BI__builtin_ia32_vpcomw: |
| 1064 | case X86::BI__builtin_ia32_vpcomd: |
| 1065 | case X86::BI__builtin_ia32_vpcomq: i = 2; l = 0; u = 7; break; |
Warren Hunt | 20e4a5d | 2014-02-21 23:08:53 +0000 | [diff] [blame] | 1066 | } |
Craig Topper | dd84ec5 | 2014-12-27 07:00:08 +0000 | [diff] [blame] | 1067 | return SemaBuiltinConstantArgRange(TheCall, i, l, u); |
Warren Hunt | 20e4a5d | 2014-02-21 23:08:53 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1070 | /// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo |
| 1071 | /// parameter with the FormatAttr's correct format_idx and firstDataArg. |
| 1072 | /// Returns true when the format fits the function and the FormatStringInfo has |
| 1073 | /// been populated. |
| 1074 | bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember, |
| 1075 | FormatStringInfo *FSI) { |
| 1076 | FSI->HasVAListArg = Format->getFirstArg() == 0; |
| 1077 | FSI->FormatIdx = Format->getFormatIdx() - 1; |
| 1078 | FSI->FirstDataArg = FSI->HasVAListArg ? 0 : Format->getFirstArg() - 1; |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 1079 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1080 | // The way the format attribute works in GCC, the implicit this argument |
| 1081 | // of member functions is counted. However, it doesn't appear in our own |
| 1082 | // lists, so decrement format_idx in that case. |
| 1083 | if (IsCXXMember) { |
| 1084 | if(FSI->FormatIdx == 0) |
| 1085 | return false; |
| 1086 | --FSI->FormatIdx; |
| 1087 | if (FSI->FirstDataArg != 0) |
| 1088 | --FSI->FirstDataArg; |
| 1089 | } |
| 1090 | return true; |
| 1091 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 1093 | /// Checks if a the given expression evaluates to null. |
| 1094 | /// |
| 1095 | /// \brief Returns true if the value evaluates to null. |
| 1096 | static bool CheckNonNullExpr(Sema &S, |
| 1097 | const Expr *Expr) { |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 1098 | // As a special case, transparent unions initialized with zero are |
| 1099 | // considered null for the purposes of the nonnull attribute. |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 1100 | if (const RecordType *UT = Expr->getType()->getAsUnionType()) { |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 1101 | if (UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1102 | if (const CompoundLiteralExpr *CLE = |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 1103 | dyn_cast<CompoundLiteralExpr>(Expr)) |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 1104 | if (const InitListExpr *ILE = |
| 1105 | dyn_cast<InitListExpr>(CLE->getInitializer())) |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 1106 | Expr = ILE->getInit(0); |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | bool Result; |
Artyom Skrobov | 9f21344 | 2014-01-24 11:10:39 +0000 | [diff] [blame] | 1110 | return (!Expr->isValueDependent() && |
| 1111 | Expr->EvaluateAsBooleanCondition(Result, S.Context) && |
| 1112 | !Result); |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | static void CheckNonNullArgument(Sema &S, |
| 1116 | const Expr *ArgExpr, |
| 1117 | SourceLocation CallSiteLoc) { |
| 1118 | if (CheckNonNullExpr(S, ArgExpr)) |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 1119 | S.Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); |
| 1120 | } |
| 1121 | |
Fariborz Jahanian | fba4fe6 | 2014-09-11 19:13:23 +0000 | [diff] [blame] | 1122 | bool Sema::GetFormatNSStringIdx(const FormatAttr *Format, unsigned &Idx) { |
| 1123 | FormatStringInfo FSI; |
| 1124 | if ((GetFormatStringType(Format) == FST_NSString) && |
| 1125 | getFormatStringInfo(Format, false, &FSI)) { |
| 1126 | Idx = FSI.FormatIdx; |
| 1127 | return true; |
| 1128 | } |
| 1129 | return false; |
| 1130 | } |
Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 1131 | /// \brief Diagnose use of %s directive in an NSString which is being passed |
| 1132 | /// as formatting string to formatting method. |
| 1133 | static void |
| 1134 | DiagnoseCStringFormatDirectiveInCFAPI(Sema &S, |
| 1135 | const NamedDecl *FDecl, |
| 1136 | Expr **Args, |
| 1137 | unsigned NumArgs) { |
Fariborz Jahanian | fba4fe6 | 2014-09-11 19:13:23 +0000 | [diff] [blame] | 1138 | unsigned Idx = 0; |
| 1139 | bool Format = false; |
Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 1140 | ObjCStringFormatFamily SFFamily = FDecl->getObjCFStringFormattingFamily(); |
| 1141 | if (SFFamily == ObjCStringFormatFamily::SFF_CFString) { |
Fariborz Jahanian | fba4fe6 | 2014-09-11 19:13:23 +0000 | [diff] [blame] | 1142 | Idx = 2; |
| 1143 | Format = true; |
| 1144 | } |
| 1145 | else |
| 1146 | for (const auto *I : FDecl->specific_attrs<FormatAttr>()) { |
| 1147 | if (S.GetFormatNSStringIdx(I, Idx)) { |
| 1148 | Format = true; |
| 1149 | break; |
| 1150 | } |
Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 1151 | } |
Fariborz Jahanian | fba4fe6 | 2014-09-11 19:13:23 +0000 | [diff] [blame] | 1152 | if (!Format || NumArgs <= Idx) |
| 1153 | return; |
| 1154 | const Expr *FormatExpr = Args[Idx]; |
| 1155 | if (const CStyleCastExpr *CSCE = dyn_cast<CStyleCastExpr>(FormatExpr)) |
| 1156 | FormatExpr = CSCE->getSubExpr(); |
| 1157 | const StringLiteral *FormatString; |
| 1158 | if (const ObjCStringLiteral *OSL = |
| 1159 | dyn_cast<ObjCStringLiteral>(FormatExpr->IgnoreParenImpCasts())) |
| 1160 | FormatString = OSL->getString(); |
| 1161 | else |
| 1162 | FormatString = dyn_cast<StringLiteral>(FormatExpr->IgnoreParenImpCasts()); |
| 1163 | if (!FormatString) |
| 1164 | return; |
| 1165 | if (S.FormatStringHasSArg(FormatString)) { |
| 1166 | S.Diag(FormatExpr->getExprLoc(), diag::warn_objc_cdirective_format_string) |
| 1167 | << "%s" << 1 << 1; |
| 1168 | S.Diag(FDecl->getLocation(), diag::note_entity_declared_at) |
| 1169 | << FDecl->getDeclName(); |
Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 1170 | } |
| 1171 | } |
| 1172 | |
Ted Kremenek | 2bc7333 | 2014-01-17 06:24:43 +0000 | [diff] [blame] | 1173 | static void CheckNonNullArguments(Sema &S, |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 1174 | const NamedDecl *FDecl, |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1175 | ArrayRef<const Expr *> Args, |
Ted Kremenek | 2bc7333 | 2014-01-17 06:24:43 +0000 | [diff] [blame] | 1176 | SourceLocation CallSiteLoc) { |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1177 | // Check the attributes attached to the method/function itself. |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1178 | llvm::SmallBitVector NonNullArgs; |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1179 | for (const auto *NonNull : FDecl->specific_attrs<NonNullAttr>()) { |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1180 | if (!NonNull->args_size()) { |
| 1181 | // Easy case: all pointer arguments are nonnull. |
| 1182 | for (const auto *Arg : Args) |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1183 | if (S.isValidPointerAttrType(Arg->getType())) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1184 | CheckNonNullArgument(S, Arg, CallSiteLoc); |
| 1185 | return; |
| 1186 | } |
| 1187 | |
| 1188 | for (unsigned Val : NonNull->args()) { |
| 1189 | if (Val >= Args.size()) |
| 1190 | continue; |
| 1191 | if (NonNullArgs.empty()) |
| 1192 | NonNullArgs.resize(Args.size()); |
| 1193 | NonNullArgs.set(Val); |
| 1194 | } |
Ted Kremenek | 2bc7333 | 2014-01-17 06:24:43 +0000 | [diff] [blame] | 1195 | } |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1196 | |
| 1197 | // Check the attributes on the parameters. |
| 1198 | ArrayRef<ParmVarDecl*> parms; |
| 1199 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FDecl)) |
| 1200 | parms = FD->parameters(); |
| 1201 | else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(FDecl)) |
| 1202 | parms = MD->parameters(); |
| 1203 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1204 | unsigned ArgIndex = 0; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1205 | for (ArrayRef<ParmVarDecl*>::iterator I = parms.begin(), E = parms.end(); |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1206 | I != E; ++I, ++ArgIndex) { |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1207 | const ParmVarDecl *PVD = *I; |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1208 | if (PVD->hasAttr<NonNullAttr>() || |
| 1209 | (ArgIndex < NonNullArgs.size() && NonNullArgs[ArgIndex])) |
| 1210 | CheckNonNullArgument(S, Args[ArgIndex], CallSiteLoc); |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1211 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1212 | |
| 1213 | // In case this is a variadic call, check any remaining arguments. |
| 1214 | for (/**/; ArgIndex < NonNullArgs.size(); ++ArgIndex) |
| 1215 | if (NonNullArgs[ArgIndex]) |
| 1216 | CheckNonNullArgument(S, Args[ArgIndex], CallSiteLoc); |
Ted Kremenek | 2bc7333 | 2014-01-17 06:24:43 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1219 | /// Handles the checks for format strings, non-POD arguments to vararg |
| 1220 | /// functions, and NULL arguments passed to non-NULL parameters. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1221 | void Sema::checkCall(NamedDecl *FDecl, ArrayRef<const Expr *> Args, |
| 1222 | unsigned NumParams, bool IsMemberFunction, |
| 1223 | SourceLocation Loc, SourceRange Range, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1224 | VariadicCallType CallType) { |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 1225 | // 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] | 1226 | if (CurContext->isDependentContext()) |
| 1227 | return; |
Daniel Dunbar | dd9b2d1 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 1228 | |
Ted Kremenek | b8176da | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 1229 | // Printf and scanf checking. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 1230 | llvm::SmallBitVector CheckedVarArgs; |
| 1231 | if (FDecl) { |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1232 | for (const auto *I : FDecl->specific_attrs<FormatAttr>()) { |
Benjamin Kramer | 989ab8b | 2013-08-09 09:39:17 +0000 | [diff] [blame] | 1233 | // Only create vector if there are format attributes. |
| 1234 | CheckedVarArgs.resize(Args.size()); |
| 1235 | |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1236 | CheckFormatArguments(I, Args, IsMemberFunction, CallType, Loc, Range, |
Benjamin Kramer | f62e81d | 2013-08-08 11:08:26 +0000 | [diff] [blame] | 1237 | CheckedVarArgs); |
Benjamin Kramer | 989ab8b | 2013-08-09 09:39:17 +0000 | [diff] [blame] | 1238 | } |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 1239 | } |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1240 | |
| 1241 | // Refuse POD arguments that weren't caught by the format string |
| 1242 | // checks above. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 1243 | if (CallType != VariadicDoesNotApply) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1244 | for (unsigned ArgIdx = NumParams; ArgIdx < Args.size(); ++ArgIdx) { |
Ted Kremenek | 241f1ef | 2012-10-11 19:06:43 +0000 | [diff] [blame] | 1245 | // Args[ArgIdx] can be null in malformed code. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 1246 | if (const Expr *Arg = Args[ArgIdx]) { |
| 1247 | if (CheckedVarArgs.empty() || !CheckedVarArgs[ArgIdx]) |
| 1248 | checkVariadicArgument(Arg, CallType); |
| 1249 | } |
Ted Kremenek | 241f1ef | 2012-10-11 19:06:43 +0000 | [diff] [blame] | 1250 | } |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 1251 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1252 | |
Richard Trieu | 41bc099 | 2013-06-22 00:20:41 +0000 | [diff] [blame] | 1253 | if (FDecl) { |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1254 | CheckNonNullArguments(*this, FDecl, Args, Loc); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1255 | |
Richard Trieu | 41bc099 | 2013-06-22 00:20:41 +0000 | [diff] [blame] | 1256 | // Type safety checking. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1257 | for (const auto *I : FDecl->specific_attrs<ArgumentWithTypeTagAttr>()) |
| 1258 | CheckArgumentWithTypeTag(I, Args.data()); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1259 | } |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | /// CheckConstructorCall - Check a constructor call for correctness and safety |
| 1263 | /// properties not enforced by the C type system. |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 1264 | void Sema::CheckConstructorCall(FunctionDecl *FDecl, |
| 1265 | ArrayRef<const Expr *> Args, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1266 | const FunctionProtoType *Proto, |
| 1267 | SourceLocation Loc) { |
| 1268 | VariadicCallType CallType = |
| 1269 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1270 | checkCall(FDecl, Args, Proto->getNumParams(), |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1271 | /*IsMemberFunction=*/true, Loc, SourceRange(), CallType); |
| 1272 | } |
| 1273 | |
| 1274 | /// CheckFunctionCall - Check a direct function call for various correctness |
| 1275 | /// and safety properties not strictly enforced by the C type system. |
| 1276 | bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall, |
| 1277 | const FunctionProtoType *Proto) { |
Eli Friedman | 726d11c | 2012-10-11 00:30:58 +0000 | [diff] [blame] | 1278 | bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) && |
| 1279 | isa<CXXMethodDecl>(FDecl); |
| 1280 | bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) || |
| 1281 | IsMemberOperatorCall; |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1282 | VariadicCallType CallType = getVariadicCallType(FDecl, Proto, |
| 1283 | TheCall->getCallee()); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1284 | unsigned NumParams = Proto ? Proto->getNumParams() : 0; |
Eli Friedman | 726d11c | 2012-10-11 00:30:58 +0000 | [diff] [blame] | 1285 | Expr** Args = TheCall->getArgs(); |
| 1286 | unsigned NumArgs = TheCall->getNumArgs(); |
Eli Friedman | adf4218 | 2012-10-11 00:34:15 +0000 | [diff] [blame] | 1287 | if (IsMemberOperatorCall) { |
Eli Friedman | 726d11c | 2012-10-11 00:30:58 +0000 | [diff] [blame] | 1288 | // If this is a call to a member operator, hide the first argument |
| 1289 | // from checkCall. |
| 1290 | // FIXME: Our choice of AST representation here is less than ideal. |
| 1291 | ++Args; |
| 1292 | --NumArgs; |
| 1293 | } |
Craig Topper | 8c2a2a0 | 2014-08-30 16:55:39 +0000 | [diff] [blame] | 1294 | checkCall(FDecl, llvm::makeArrayRef(Args, NumArgs), NumParams, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1295 | IsMemberFunction, TheCall->getRParenLoc(), |
| 1296 | TheCall->getCallee()->getSourceRange(), CallType); |
| 1297 | |
| 1298 | IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
| 1299 | // None of the checks below are needed for functions that don't have |
| 1300 | // simple names (e.g., C++ conversion functions). |
| 1301 | if (!FnInfo) |
| 1302 | return false; |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1303 | |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 1304 | CheckAbsoluteValueFunction(TheCall, FDecl, FnInfo); |
Fariborz Jahanian | fba4fe6 | 2014-09-11 19:13:23 +0000 | [diff] [blame] | 1305 | if (getLangOpts().ObjC1) |
| 1306 | DiagnoseCStringFormatDirectiveInCFAPI(*this, FDecl, Args, NumArgs); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 1307 | |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 1308 | unsigned CMId = FDecl->getMemoryFunctionKind(); |
| 1309 | if (CMId == 0) |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 1310 | return false; |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 1311 | |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 1312 | // Handle memory setting and copying functions. |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 1313 | if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat) |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 1314 | CheckStrlcpycatArguments(TheCall, FnInfo); |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 1315 | else if (CMId == Builtin::BIstrncat) |
| 1316 | CheckStrncatArguments(TheCall, FnInfo); |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 1317 | else |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 1318 | CheckMemaccessArguments(TheCall, CMId, FnInfo); |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 1319 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 1320 | return false; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1323 | bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac, |
Dmitri Gribenko | 1debc46 | 2013-05-05 19:42:09 +0000 | [diff] [blame] | 1324 | ArrayRef<const Expr *> Args) { |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1325 | VariadicCallType CallType = |
| 1326 | Method->isVariadic() ? VariadicMethod : VariadicDoesNotApply; |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1327 | |
Dmitri Gribenko | 1debc46 | 2013-05-05 19:42:09 +0000 | [diff] [blame] | 1328 | checkCall(Method, Args, Method->param_size(), |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1329 | /*IsMemberFunction=*/false, |
| 1330 | lbrac, Method->getSourceRange(), CallType); |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1331 | |
| 1332 | return false; |
| 1333 | } |
| 1334 | |
Richard Trieu | 664c4c6 | 2013-06-20 21:03:13 +0000 | [diff] [blame] | 1335 | bool Sema::CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall, |
| 1336 | const FunctionProtoType *Proto) { |
Aaron Ballman | b673c65 | 2015-04-23 16:14:19 +0000 | [diff] [blame] | 1337 | QualType Ty; |
| 1338 | if (const auto *V = dyn_cast<VarDecl>(NDecl)) |
| 1339 | Ty = V->getType(); |
| 1340 | else if (const auto *F = dyn_cast<FieldDecl>(NDecl)) |
| 1341 | Ty = F->getType(); |
| 1342 | else |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 1343 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1344 | |
Richard Trieu | 664c4c6 | 2013-06-20 21:03:13 +0000 | [diff] [blame] | 1345 | if (!Ty->isBlockPointerType() && !Ty->isFunctionPointerType()) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 1346 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1347 | |
Richard Trieu | 664c4c6 | 2013-06-20 21:03:13 +0000 | [diff] [blame] | 1348 | VariadicCallType CallType; |
Richard Trieu | 72ae173 | 2013-06-20 23:21:54 +0000 | [diff] [blame] | 1349 | if (!Proto || !Proto->isVariadic()) { |
Richard Trieu | 664c4c6 | 2013-06-20 21:03:13 +0000 | [diff] [blame] | 1350 | CallType = VariadicDoesNotApply; |
| 1351 | } else if (Ty->isBlockPointerType()) { |
| 1352 | CallType = VariadicBlock; |
| 1353 | } else { // Ty->isFunctionPointerType() |
| 1354 | CallType = VariadicFunction; |
| 1355 | } |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1356 | unsigned NumParams = Proto ? Proto->getNumParams() : 0; |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 1357 | |
Craig Topper | 8c2a2a0 | 2014-08-30 16:55:39 +0000 | [diff] [blame] | 1358 | checkCall(NDecl, llvm::makeArrayRef(TheCall->getArgs(), |
| 1359 | TheCall->getNumArgs()), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1360 | NumParams, /*IsMemberFunction=*/false, TheCall->getRParenLoc(), |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1361 | TheCall->getCallee()->getSourceRange(), CallType); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1362 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 1363 | return false; |
Fariborz Jahanian | c1585be | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
Richard Trieu | 41bc099 | 2013-06-22 00:20:41 +0000 | [diff] [blame] | 1366 | /// Checks function calls when a FunctionDecl or a NamedDecl is not available, |
| 1367 | /// such as function pointers returned from functions. |
| 1368 | bool Sema::CheckOtherCall(CallExpr *TheCall, const FunctionProtoType *Proto) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1369 | VariadicCallType CallType = getVariadicCallType(/*FDecl=*/nullptr, Proto, |
Richard Trieu | 41bc099 | 2013-06-22 00:20:41 +0000 | [diff] [blame] | 1370 | TheCall->getCallee()); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1371 | unsigned NumParams = Proto ? Proto->getNumParams() : 0; |
Richard Trieu | 41bc099 | 2013-06-22 00:20:41 +0000 | [diff] [blame] | 1372 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1373 | checkCall(/*FDecl=*/nullptr, |
Craig Topper | 8c2a2a0 | 2014-08-30 16:55:39 +0000 | [diff] [blame] | 1374 | llvm::makeArrayRef(TheCall->getArgs(), TheCall->getNumArgs()), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1375 | NumParams, /*IsMemberFunction=*/false, TheCall->getRParenLoc(), |
Richard Trieu | 41bc099 | 2013-06-22 00:20:41 +0000 | [diff] [blame] | 1376 | TheCall->getCallee()->getSourceRange(), CallType); |
| 1377 | |
| 1378 | return false; |
| 1379 | } |
| 1380 | |
Tim Northover | e94a34c | 2014-03-11 10:49:14 +0000 | [diff] [blame] | 1381 | static bool isValidOrderingForOp(int64_t Ordering, AtomicExpr::AtomicOp Op) { |
| 1382 | if (Ordering < AtomicExpr::AO_ABI_memory_order_relaxed || |
| 1383 | Ordering > AtomicExpr::AO_ABI_memory_order_seq_cst) |
| 1384 | return false; |
| 1385 | |
| 1386 | switch (Op) { |
| 1387 | case AtomicExpr::AO__c11_atomic_init: |
| 1388 | llvm_unreachable("There is no ordering argument for an init"); |
| 1389 | |
| 1390 | case AtomicExpr::AO__c11_atomic_load: |
| 1391 | case AtomicExpr::AO__atomic_load_n: |
| 1392 | case AtomicExpr::AO__atomic_load: |
| 1393 | return Ordering != AtomicExpr::AO_ABI_memory_order_release && |
| 1394 | Ordering != AtomicExpr::AO_ABI_memory_order_acq_rel; |
| 1395 | |
| 1396 | case AtomicExpr::AO__c11_atomic_store: |
| 1397 | case AtomicExpr::AO__atomic_store: |
| 1398 | case AtomicExpr::AO__atomic_store_n: |
| 1399 | return Ordering != AtomicExpr::AO_ABI_memory_order_consume && |
| 1400 | Ordering != AtomicExpr::AO_ABI_memory_order_acquire && |
| 1401 | Ordering != AtomicExpr::AO_ABI_memory_order_acq_rel; |
| 1402 | |
| 1403 | default: |
| 1404 | return true; |
| 1405 | } |
| 1406 | } |
| 1407 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1408 | ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult, |
| 1409 | AtomicExpr::AtomicOp Op) { |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1410 | CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); |
| 1411 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1412 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1413 | // All these operations take one of the following forms: |
| 1414 | enum { |
| 1415 | // C __c11_atomic_init(A *, C) |
| 1416 | Init, |
| 1417 | // C __c11_atomic_load(A *, int) |
| 1418 | Load, |
| 1419 | // void __atomic_load(A *, CP, int) |
| 1420 | Copy, |
| 1421 | // C __c11_atomic_add(A *, M, int) |
| 1422 | Arithmetic, |
| 1423 | // C __atomic_exchange_n(A *, CP, int) |
| 1424 | Xchg, |
| 1425 | // void __atomic_exchange(A *, C *, CP, int) |
| 1426 | GNUXchg, |
| 1427 | // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int) |
| 1428 | C11CmpXchg, |
| 1429 | // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int) |
| 1430 | GNUCmpXchg |
| 1431 | } Form = Init; |
| 1432 | const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 4, 5, 6 }; |
| 1433 | const unsigned NumVals[] = { 1, 0, 1, 1, 1, 2, 2, 3 }; |
| 1434 | // where: |
| 1435 | // C is an appropriate type, |
| 1436 | // A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins, |
| 1437 | // CP is C for __c11 builtins and GNU _n builtins and is C * otherwise, |
| 1438 | // M is C if C is an integer, and ptrdiff_t if C is a pointer, and |
| 1439 | // the int parameters are for orderings. |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1440 | |
Gabor Horvath | 98bd098 | 2015-03-16 09:59:54 +0000 | [diff] [blame] | 1441 | static_assert(AtomicExpr::AO__c11_atomic_init == 0 && |
| 1442 | AtomicExpr::AO__c11_atomic_fetch_xor + 1 == |
| 1443 | AtomicExpr::AO__atomic_load, |
| 1444 | "need to update code for modified C11 atomics"); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1445 | bool IsC11 = Op >= AtomicExpr::AO__c11_atomic_init && |
| 1446 | Op <= AtomicExpr::AO__c11_atomic_fetch_xor; |
| 1447 | bool IsN = Op == AtomicExpr::AO__atomic_load_n || |
| 1448 | Op == AtomicExpr::AO__atomic_store_n || |
| 1449 | Op == AtomicExpr::AO__atomic_exchange_n || |
| 1450 | Op == AtomicExpr::AO__atomic_compare_exchange_n; |
| 1451 | bool IsAddSub = false; |
| 1452 | |
| 1453 | switch (Op) { |
| 1454 | case AtomicExpr::AO__c11_atomic_init: |
| 1455 | Form = Init; |
| 1456 | break; |
| 1457 | |
| 1458 | case AtomicExpr::AO__c11_atomic_load: |
| 1459 | case AtomicExpr::AO__atomic_load_n: |
| 1460 | Form = Load; |
| 1461 | break; |
| 1462 | |
| 1463 | case AtomicExpr::AO__c11_atomic_store: |
| 1464 | case AtomicExpr::AO__atomic_load: |
| 1465 | case AtomicExpr::AO__atomic_store: |
| 1466 | case AtomicExpr::AO__atomic_store_n: |
| 1467 | Form = Copy; |
| 1468 | break; |
| 1469 | |
| 1470 | case AtomicExpr::AO__c11_atomic_fetch_add: |
| 1471 | case AtomicExpr::AO__c11_atomic_fetch_sub: |
| 1472 | case AtomicExpr::AO__atomic_fetch_add: |
| 1473 | case AtomicExpr::AO__atomic_fetch_sub: |
| 1474 | case AtomicExpr::AO__atomic_add_fetch: |
| 1475 | case AtomicExpr::AO__atomic_sub_fetch: |
| 1476 | IsAddSub = true; |
| 1477 | // Fall through. |
| 1478 | case AtomicExpr::AO__c11_atomic_fetch_and: |
| 1479 | case AtomicExpr::AO__c11_atomic_fetch_or: |
| 1480 | case AtomicExpr::AO__c11_atomic_fetch_xor: |
| 1481 | case AtomicExpr::AO__atomic_fetch_and: |
| 1482 | case AtomicExpr::AO__atomic_fetch_or: |
| 1483 | case AtomicExpr::AO__atomic_fetch_xor: |
Richard Smith | d65cee9 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 1484 | case AtomicExpr::AO__atomic_fetch_nand: |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1485 | case AtomicExpr::AO__atomic_and_fetch: |
| 1486 | case AtomicExpr::AO__atomic_or_fetch: |
| 1487 | case AtomicExpr::AO__atomic_xor_fetch: |
Richard Smith | d65cee9 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 1488 | case AtomicExpr::AO__atomic_nand_fetch: |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1489 | Form = Arithmetic; |
| 1490 | break; |
| 1491 | |
| 1492 | case AtomicExpr::AO__c11_atomic_exchange: |
| 1493 | case AtomicExpr::AO__atomic_exchange_n: |
| 1494 | Form = Xchg; |
| 1495 | break; |
| 1496 | |
| 1497 | case AtomicExpr::AO__atomic_exchange: |
| 1498 | Form = GNUXchg; |
| 1499 | break; |
| 1500 | |
| 1501 | case AtomicExpr::AO__c11_atomic_compare_exchange_strong: |
| 1502 | case AtomicExpr::AO__c11_atomic_compare_exchange_weak: |
| 1503 | Form = C11CmpXchg; |
| 1504 | break; |
| 1505 | |
| 1506 | case AtomicExpr::AO__atomic_compare_exchange: |
| 1507 | case AtomicExpr::AO__atomic_compare_exchange_n: |
| 1508 | Form = GNUCmpXchg; |
| 1509 | break; |
| 1510 | } |
| 1511 | |
| 1512 | // Check we have the right number of arguments. |
| 1513 | if (TheCall->getNumArgs() < NumArgs[Form]) { |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1514 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1515 | << 0 << NumArgs[Form] << TheCall->getNumArgs() |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1516 | << TheCall->getCallee()->getSourceRange(); |
| 1517 | return ExprError(); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1518 | } else if (TheCall->getNumArgs() > NumArgs[Form]) { |
| 1519 | Diag(TheCall->getArg(NumArgs[Form])->getLocStart(), |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1520 | diag::err_typecheck_call_too_many_args) |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1521 | << 0 << NumArgs[Form] << TheCall->getNumArgs() |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1522 | << TheCall->getCallee()->getSourceRange(); |
| 1523 | return ExprError(); |
| 1524 | } |
| 1525 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1526 | // Inspect the first argument of the atomic operation. |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 1527 | Expr *Ptr = TheCall->getArg(0); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1528 | Ptr = DefaultFunctionArrayLvalueConversion(Ptr).get(); |
| 1529 | const PointerType *pointerType = Ptr->getType()->getAs<PointerType>(); |
| 1530 | if (!pointerType) { |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1531 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1532 | << Ptr->getType() << Ptr->getSourceRange(); |
| 1533 | return ExprError(); |
| 1534 | } |
| 1535 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1536 | // For a __c11 builtin, this should be a pointer to an _Atomic type. |
| 1537 | QualType AtomTy = pointerType->getPointeeType(); // 'A' |
| 1538 | QualType ValType = AtomTy; // 'C' |
| 1539 | if (IsC11) { |
| 1540 | if (!AtomTy->isAtomicType()) { |
| 1541 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic) |
| 1542 | << Ptr->getType() << Ptr->getSourceRange(); |
| 1543 | return ExprError(); |
| 1544 | } |
Richard Smith | e00921a | 2012-09-15 06:09:58 +0000 | [diff] [blame] | 1545 | if (AtomTy.isConstQualified()) { |
| 1546 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_non_const_atomic) |
| 1547 | << Ptr->getType() << Ptr->getSourceRange(); |
| 1548 | return ExprError(); |
| 1549 | } |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1550 | ValType = AtomTy->getAs<AtomicType>()->getValueType(); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1551 | } |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1552 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1553 | // For an arithmetic operation, the implied arithmetic must be well-formed. |
| 1554 | if (Form == Arithmetic) { |
| 1555 | // gcc does not enforce these rules for GNU atomics, but we do so for sanity. |
| 1556 | if (IsAddSub && !ValType->isIntegerType() && !ValType->isPointerType()) { |
| 1557 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) |
| 1558 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 1559 | return ExprError(); |
| 1560 | } |
| 1561 | if (!IsAddSub && !ValType->isIntegerType()) { |
| 1562 | Diag(DRE->getLocStart(), diag::err_atomic_op_bitwise_needs_atomic_int) |
| 1563 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 1564 | return ExprError(); |
| 1565 | } |
David Majnemer | e85cff8 | 2015-01-28 05:48:06 +0000 | [diff] [blame] | 1566 | if (IsC11 && ValType->isPointerType() && |
| 1567 | RequireCompleteType(Ptr->getLocStart(), ValType->getPointeeType(), |
| 1568 | diag::err_incomplete_type)) { |
| 1569 | return ExprError(); |
| 1570 | } |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1571 | } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) { |
| 1572 | // For __atomic_*_n operations, the value type must be a scalar integral or |
| 1573 | // 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] | 1574 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1575 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 1576 | return ExprError(); |
| 1577 | } |
| 1578 | |
Eli Friedman | aa76981 | 2013-09-11 03:49:34 +0000 | [diff] [blame] | 1579 | if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context) && |
| 1580 | !AtomTy->isScalarType()) { |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1581 | // For GNU atomics, require a trivially-copyable type. This is not part of |
| 1582 | // the GNU atomics specification, but we enforce it for sanity. |
| 1583 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_trivial_copy) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1584 | << Ptr->getType() << Ptr->getSourceRange(); |
| 1585 | return ExprError(); |
| 1586 | } |
| 1587 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1588 | // FIXME: For any builtin other than a load, the ValType must not be |
| 1589 | // const-qualified. |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1590 | |
| 1591 | switch (ValType.getObjCLifetime()) { |
| 1592 | case Qualifiers::OCL_None: |
| 1593 | case Qualifiers::OCL_ExplicitNone: |
| 1594 | // okay |
| 1595 | break; |
| 1596 | |
| 1597 | case Qualifiers::OCL_Weak: |
| 1598 | case Qualifiers::OCL_Strong: |
| 1599 | case Qualifiers::OCL_Autoreleasing: |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1600 | // FIXME: Can this happen? By this point, ValType should be known |
| 1601 | // to be trivially copyable. |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1602 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
| 1603 | << ValType << Ptr->getSourceRange(); |
| 1604 | return ExprError(); |
| 1605 | } |
| 1606 | |
David Majnemer | c6eb650 | 2015-06-03 00:26:35 +0000 | [diff] [blame^] | 1607 | // atomic_fetch_or takes a pointer to a volatile 'A'. We shouldn't let the |
| 1608 | // volatile-ness of the pointee-type inject itself into the result or the |
| 1609 | // other operands. |
| 1610 | ValType.removeLocalVolatile(); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1611 | QualType ResultType = ValType; |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1612 | if (Form == Copy || Form == GNUXchg || Form == Init) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1613 | ResultType = Context.VoidTy; |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1614 | else if (Form == C11CmpXchg || Form == GNUCmpXchg) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1615 | ResultType = Context.BoolTy; |
| 1616 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1617 | // The type of a parameter passed 'by value'. In the GNU atomics, such |
| 1618 | // arguments are actually passed as pointers. |
| 1619 | QualType ByValType = ValType; // 'CP' |
| 1620 | if (!IsC11 && !IsN) |
| 1621 | ByValType = Ptr->getType(); |
| 1622 | |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1623 | // The first argument --- the pointer --- has a fixed type; we |
| 1624 | // deduce the types of the rest of the arguments accordingly. Walk |
| 1625 | // the remaining arguments, converting them to the deduced value type. |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1626 | for (unsigned i = 1; i != NumArgs[Form]; ++i) { |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1627 | QualType Ty; |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1628 | if (i < NumVals[Form] + 1) { |
| 1629 | switch (i) { |
| 1630 | case 1: |
| 1631 | // The second argument is the non-atomic operand. For arithmetic, this |
| 1632 | // is always passed by value, and for a compare_exchange it is always |
| 1633 | // passed by address. For the rest, GNU uses by-address and C11 uses |
| 1634 | // by-value. |
| 1635 | assert(Form != Load); |
| 1636 | if (Form == Init || (Form == Arithmetic && ValType->isIntegerType())) |
| 1637 | Ty = ValType; |
| 1638 | else if (Form == Copy || Form == Xchg) |
| 1639 | Ty = ByValType; |
| 1640 | else if (Form == Arithmetic) |
| 1641 | Ty = Context.getPointerDiffType(); |
| 1642 | else |
| 1643 | Ty = Context.getPointerType(ValType.getUnqualifiedType()); |
| 1644 | break; |
| 1645 | case 2: |
| 1646 | // The third argument to compare_exchange / GNU exchange is a |
| 1647 | // (pointer to a) desired value. |
| 1648 | Ty = ByValType; |
| 1649 | break; |
| 1650 | case 3: |
| 1651 | // The fourth argument to GNU compare_exchange is a 'weak' flag. |
| 1652 | Ty = Context.BoolTy; |
| 1653 | break; |
| 1654 | } |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1655 | } else { |
| 1656 | // The order(s) are always converted to int. |
| 1657 | Ty = Context.IntTy; |
| 1658 | } |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1659 | |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1660 | InitializedEntity Entity = |
| 1661 | InitializedEntity::InitializeParameter(Context, Ty, false); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1662 | ExprResult Arg = TheCall->getArg(i); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1663 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 1664 | if (Arg.isInvalid()) |
| 1665 | return true; |
| 1666 | TheCall->setArg(i, Arg.get()); |
| 1667 | } |
| 1668 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1669 | // Permute the arguments into a 'consistent' order. |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 1670 | SmallVector<Expr*, 5> SubExprs; |
| 1671 | SubExprs.push_back(Ptr); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1672 | switch (Form) { |
| 1673 | case Init: |
| 1674 | // Note, AtomicExpr::getVal1() has a special case for this atomic. |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 1675 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1676 | break; |
| 1677 | case Load: |
| 1678 | SubExprs.push_back(TheCall->getArg(1)); // Order |
| 1679 | break; |
| 1680 | case Copy: |
| 1681 | case Arithmetic: |
| 1682 | case Xchg: |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 1683 | SubExprs.push_back(TheCall->getArg(2)); // Order |
| 1684 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1685 | break; |
| 1686 | case GNUXchg: |
| 1687 | // Note, AtomicExpr::getVal2() has a special case for this atomic. |
| 1688 | SubExprs.push_back(TheCall->getArg(3)); // Order |
| 1689 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
| 1690 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
| 1691 | break; |
| 1692 | case C11CmpXchg: |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 1693 | SubExprs.push_back(TheCall->getArg(3)); // Order |
| 1694 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 1695 | SubExprs.push_back(TheCall->getArg(4)); // OrderFail |
David Chisnall | 891ec28 | 2012-03-29 17:58:59 +0000 | [diff] [blame] | 1696 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1697 | break; |
| 1698 | case GNUCmpXchg: |
| 1699 | SubExprs.push_back(TheCall->getArg(4)); // Order |
| 1700 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
| 1701 | SubExprs.push_back(TheCall->getArg(5)); // OrderFail |
| 1702 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
| 1703 | SubExprs.push_back(TheCall->getArg(3)); // Weak |
| 1704 | break; |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1705 | } |
Tim Northover | e94a34c | 2014-03-11 10:49:14 +0000 | [diff] [blame] | 1706 | |
| 1707 | if (SubExprs.size() >= 2 && Form != Init) { |
| 1708 | llvm::APSInt Result(32); |
| 1709 | if (SubExprs[1]->isIntegerConstantExpr(Result, Context) && |
| 1710 | !isValidOrderingForOp(Result.getSExtValue(), Op)) |
Tim Northover | c83472e | 2014-03-11 11:35:10 +0000 | [diff] [blame] | 1711 | Diag(SubExprs[1]->getLocStart(), |
| 1712 | diag::warn_atomic_op_has_invalid_memory_order) |
| 1713 | << SubExprs[1]->getSourceRange(); |
Tim Northover | e94a34c | 2014-03-11 10:49:14 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 1716 | AtomicExpr *AE = new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(), |
| 1717 | SubExprs, ResultType, Op, |
| 1718 | TheCall->getRParenLoc()); |
| 1719 | |
| 1720 | if ((Op == AtomicExpr::AO__c11_atomic_load || |
| 1721 | (Op == AtomicExpr::AO__c11_atomic_store)) && |
| 1722 | Context.AtomicUsesUnsupportedLibcall(AE)) |
| 1723 | Diag(AE->getLocStart(), diag::err_atomic_load_store_uses_lib) << |
| 1724 | ((Op == AtomicExpr::AO__c11_atomic_load) ? 0 : 1); |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 1725 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1726 | return AE; |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | |
John McCall | 29ad95b | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 1730 | /// checkBuiltinArgument - Given a call to a builtin function, perform |
| 1731 | /// normal type-checking on the given argument, updating the call in |
| 1732 | /// place. This is useful when a builtin function requires custom |
| 1733 | /// type-checking for some of its arguments but not necessarily all of |
| 1734 | /// them. |
| 1735 | /// |
| 1736 | /// Returns true on error. |
| 1737 | static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) { |
| 1738 | FunctionDecl *Fn = E->getDirectCallee(); |
| 1739 | assert(Fn && "builtin call without direct callee!"); |
| 1740 | |
| 1741 | ParmVarDecl *Param = Fn->getParamDecl(ArgIndex); |
| 1742 | InitializedEntity Entity = |
| 1743 | InitializedEntity::InitializeParameter(S.Context, Param); |
| 1744 | |
| 1745 | ExprResult Arg = E->getArg(0); |
| 1746 | Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 1747 | if (Arg.isInvalid()) |
| 1748 | return true; |
| 1749 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1750 | E->setArg(ArgIndex, Arg.get()); |
John McCall | 29ad95b | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 1751 | return false; |
| 1752 | } |
| 1753 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1754 | /// SemaBuiltinAtomicOverloaded - We have a call to a function like |
| 1755 | /// __sync_fetch_and_add, which is an overloaded function based on the pointer |
| 1756 | /// type of its first argument. The main ActOnCallExpr routines have already |
| 1757 | /// promoted the types of arguments because all of these calls are prototyped as |
| 1758 | /// void(...). |
| 1759 | /// |
| 1760 | /// This function goes through and does final semantic checking for these |
| 1761 | /// builtins, |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1762 | ExprResult |
| 1763 | Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) { |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1764 | CallExpr *TheCall = (CallExpr *)TheCallResult.get(); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1765 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 1766 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 1767 | |
| 1768 | // 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] | 1769 | if (TheCall->getNumArgs() < 1) { |
| 1770 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 1771 | << 0 << 1 << TheCall->getNumArgs() |
| 1772 | << TheCall->getCallee()->getSourceRange(); |
| 1773 | return ExprError(); |
| 1774 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1775 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1776 | // Inspect the first argument of the atomic builtin. This should always be |
| 1777 | // a pointer type, whose element is an integral scalar or pointer type. |
| 1778 | // Because it is a pointer type, we don't have to worry about any implicit |
| 1779 | // casts here. |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1780 | // FIXME: We don't allow floating point scalars as input. |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1781 | Expr *FirstArg = TheCall->getArg(0); |
Eli Friedman | 844f945 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 1782 | ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg); |
| 1783 | if (FirstArgResult.isInvalid()) |
| 1784 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1785 | FirstArg = FirstArgResult.get(); |
Eli Friedman | 844f945 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 1786 | TheCall->setArg(0, FirstArg); |
| 1787 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1788 | const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>(); |
| 1789 | if (!pointerType) { |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1790 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
| 1791 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 1792 | return ExprError(); |
| 1793 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1794 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1795 | QualType ValType = pointerType->getPointeeType(); |
Chris Lattner | bb3bcd8 | 2010-09-17 21:12:38 +0000 | [diff] [blame] | 1796 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1797 | !ValType->isBlockPointerType()) { |
| 1798 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr) |
| 1799 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 1800 | return ExprError(); |
| 1801 | } |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1802 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1803 | switch (ValType.getObjCLifetime()) { |
| 1804 | case Qualifiers::OCL_None: |
| 1805 | case Qualifiers::OCL_ExplicitNone: |
| 1806 | // okay |
| 1807 | break; |
| 1808 | |
| 1809 | case Qualifiers::OCL_Weak: |
| 1810 | case Qualifiers::OCL_Strong: |
| 1811 | case Qualifiers::OCL_Autoreleasing: |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 1812 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1813 | << ValType << FirstArg->getSourceRange(); |
| 1814 | return ExprError(); |
| 1815 | } |
| 1816 | |
John McCall | b50451a | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1817 | // Strip any qualifiers off ValType. |
| 1818 | ValType = ValType.getUnqualifiedType(); |
| 1819 | |
Chandler Carruth | 3973af7 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1820 | // The majority of builtins return a value, but a few have special return |
| 1821 | // types, so allow them to override appropriately below. |
| 1822 | QualType ResultType = ValType; |
| 1823 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1824 | // We need to figure out which concrete builtin this maps onto. For example, |
| 1825 | // __sync_fetch_and_add with a 2 byte object turns into |
| 1826 | // __sync_fetch_and_add_2. |
| 1827 | #define BUILTIN_ROW(x) \ |
| 1828 | { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ |
| 1829 | Builtin::BI##x##_8, Builtin::BI##x##_16 } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1830 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1831 | static const unsigned BuiltinIndices[][5] = { |
| 1832 | BUILTIN_ROW(__sync_fetch_and_add), |
| 1833 | BUILTIN_ROW(__sync_fetch_and_sub), |
| 1834 | BUILTIN_ROW(__sync_fetch_and_or), |
| 1835 | BUILTIN_ROW(__sync_fetch_and_and), |
| 1836 | BUILTIN_ROW(__sync_fetch_and_xor), |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 1837 | BUILTIN_ROW(__sync_fetch_and_nand), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1839 | BUILTIN_ROW(__sync_add_and_fetch), |
| 1840 | BUILTIN_ROW(__sync_sub_and_fetch), |
| 1841 | BUILTIN_ROW(__sync_and_and_fetch), |
| 1842 | BUILTIN_ROW(__sync_or_and_fetch), |
| 1843 | BUILTIN_ROW(__sync_xor_and_fetch), |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 1844 | BUILTIN_ROW(__sync_nand_and_fetch), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1845 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1846 | BUILTIN_ROW(__sync_val_compare_and_swap), |
| 1847 | BUILTIN_ROW(__sync_bool_compare_and_swap), |
| 1848 | BUILTIN_ROW(__sync_lock_test_and_set), |
Chris Lattner | 9cb59fa | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 1849 | BUILTIN_ROW(__sync_lock_release), |
| 1850 | BUILTIN_ROW(__sync_swap) |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1851 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1852 | #undef BUILTIN_ROW |
| 1853 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1854 | // Determine the index of the size. |
| 1855 | unsigned SizeIndex; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1856 | switch (Context.getTypeSizeInChars(ValType).getQuantity()) { |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1857 | case 1: SizeIndex = 0; break; |
| 1858 | case 2: SizeIndex = 1; break; |
| 1859 | case 4: SizeIndex = 2; break; |
| 1860 | case 8: SizeIndex = 3; break; |
| 1861 | case 16: SizeIndex = 4; break; |
| 1862 | default: |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1863 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size) |
| 1864 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 1865 | return ExprError(); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1866 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1867 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1868 | // Each of these builtins has one pointer argument, followed by some number of |
| 1869 | // values (0, 1 or 2) followed by a potentially empty varags list of stuff |
| 1870 | // that we ignore. Find out which row of BuiltinIndices to read from as well |
| 1871 | // as the number of fixed args. |
Douglas Gregor | 15fc956 | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 1872 | unsigned BuiltinID = FDecl->getBuiltinID(); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1873 | unsigned BuiltinIndex, NumFixed = 1; |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 1874 | bool WarnAboutSemanticsChange = false; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1875 | switch (BuiltinID) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1876 | default: llvm_unreachable("Unknown overloaded atomic builtin!"); |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1877 | case Builtin::BI__sync_fetch_and_add: |
| 1878 | case Builtin::BI__sync_fetch_and_add_1: |
| 1879 | case Builtin::BI__sync_fetch_and_add_2: |
| 1880 | case Builtin::BI__sync_fetch_and_add_4: |
| 1881 | case Builtin::BI__sync_fetch_and_add_8: |
| 1882 | case Builtin::BI__sync_fetch_and_add_16: |
| 1883 | BuiltinIndex = 0; |
| 1884 | break; |
| 1885 | |
| 1886 | case Builtin::BI__sync_fetch_and_sub: |
| 1887 | case Builtin::BI__sync_fetch_and_sub_1: |
| 1888 | case Builtin::BI__sync_fetch_and_sub_2: |
| 1889 | case Builtin::BI__sync_fetch_and_sub_4: |
| 1890 | case Builtin::BI__sync_fetch_and_sub_8: |
| 1891 | case Builtin::BI__sync_fetch_and_sub_16: |
| 1892 | BuiltinIndex = 1; |
| 1893 | break; |
| 1894 | |
| 1895 | case Builtin::BI__sync_fetch_and_or: |
| 1896 | case Builtin::BI__sync_fetch_and_or_1: |
| 1897 | case Builtin::BI__sync_fetch_and_or_2: |
| 1898 | case Builtin::BI__sync_fetch_and_or_4: |
| 1899 | case Builtin::BI__sync_fetch_and_or_8: |
| 1900 | case Builtin::BI__sync_fetch_and_or_16: |
| 1901 | BuiltinIndex = 2; |
| 1902 | break; |
| 1903 | |
| 1904 | case Builtin::BI__sync_fetch_and_and: |
| 1905 | case Builtin::BI__sync_fetch_and_and_1: |
| 1906 | case Builtin::BI__sync_fetch_and_and_2: |
| 1907 | case Builtin::BI__sync_fetch_and_and_4: |
| 1908 | case Builtin::BI__sync_fetch_and_and_8: |
| 1909 | case Builtin::BI__sync_fetch_and_and_16: |
| 1910 | BuiltinIndex = 3; |
| 1911 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1912 | |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1913 | case Builtin::BI__sync_fetch_and_xor: |
| 1914 | case Builtin::BI__sync_fetch_and_xor_1: |
| 1915 | case Builtin::BI__sync_fetch_and_xor_2: |
| 1916 | case Builtin::BI__sync_fetch_and_xor_4: |
| 1917 | case Builtin::BI__sync_fetch_and_xor_8: |
| 1918 | case Builtin::BI__sync_fetch_and_xor_16: |
| 1919 | BuiltinIndex = 4; |
| 1920 | break; |
| 1921 | |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 1922 | case Builtin::BI__sync_fetch_and_nand: |
| 1923 | case Builtin::BI__sync_fetch_and_nand_1: |
| 1924 | case Builtin::BI__sync_fetch_and_nand_2: |
| 1925 | case Builtin::BI__sync_fetch_and_nand_4: |
| 1926 | case Builtin::BI__sync_fetch_and_nand_8: |
| 1927 | case Builtin::BI__sync_fetch_and_nand_16: |
| 1928 | BuiltinIndex = 5; |
| 1929 | WarnAboutSemanticsChange = true; |
| 1930 | break; |
| 1931 | |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1932 | case Builtin::BI__sync_add_and_fetch: |
| 1933 | case Builtin::BI__sync_add_and_fetch_1: |
| 1934 | case Builtin::BI__sync_add_and_fetch_2: |
| 1935 | case Builtin::BI__sync_add_and_fetch_4: |
| 1936 | case Builtin::BI__sync_add_and_fetch_8: |
| 1937 | case Builtin::BI__sync_add_and_fetch_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 1938 | BuiltinIndex = 6; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1939 | break; |
| 1940 | |
| 1941 | case Builtin::BI__sync_sub_and_fetch: |
| 1942 | case Builtin::BI__sync_sub_and_fetch_1: |
| 1943 | case Builtin::BI__sync_sub_and_fetch_2: |
| 1944 | case Builtin::BI__sync_sub_and_fetch_4: |
| 1945 | case Builtin::BI__sync_sub_and_fetch_8: |
| 1946 | case Builtin::BI__sync_sub_and_fetch_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 1947 | BuiltinIndex = 7; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1948 | break; |
| 1949 | |
| 1950 | case Builtin::BI__sync_and_and_fetch: |
| 1951 | case Builtin::BI__sync_and_and_fetch_1: |
| 1952 | case Builtin::BI__sync_and_and_fetch_2: |
| 1953 | case Builtin::BI__sync_and_and_fetch_4: |
| 1954 | case Builtin::BI__sync_and_and_fetch_8: |
| 1955 | case Builtin::BI__sync_and_and_fetch_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 1956 | BuiltinIndex = 8; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1957 | break; |
| 1958 | |
| 1959 | case Builtin::BI__sync_or_and_fetch: |
| 1960 | case Builtin::BI__sync_or_and_fetch_1: |
| 1961 | case Builtin::BI__sync_or_and_fetch_2: |
| 1962 | case Builtin::BI__sync_or_and_fetch_4: |
| 1963 | case Builtin::BI__sync_or_and_fetch_8: |
| 1964 | case Builtin::BI__sync_or_and_fetch_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 1965 | BuiltinIndex = 9; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1966 | break; |
| 1967 | |
| 1968 | case Builtin::BI__sync_xor_and_fetch: |
| 1969 | case Builtin::BI__sync_xor_and_fetch_1: |
| 1970 | case Builtin::BI__sync_xor_and_fetch_2: |
| 1971 | case Builtin::BI__sync_xor_and_fetch_4: |
| 1972 | case Builtin::BI__sync_xor_and_fetch_8: |
| 1973 | case Builtin::BI__sync_xor_and_fetch_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 1974 | BuiltinIndex = 10; |
| 1975 | break; |
| 1976 | |
| 1977 | case Builtin::BI__sync_nand_and_fetch: |
| 1978 | case Builtin::BI__sync_nand_and_fetch_1: |
| 1979 | case Builtin::BI__sync_nand_and_fetch_2: |
| 1980 | case Builtin::BI__sync_nand_and_fetch_4: |
| 1981 | case Builtin::BI__sync_nand_and_fetch_8: |
| 1982 | case Builtin::BI__sync_nand_and_fetch_16: |
| 1983 | BuiltinIndex = 11; |
| 1984 | WarnAboutSemanticsChange = true; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1985 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1987 | case Builtin::BI__sync_val_compare_and_swap: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1988 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 1989 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 1990 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 1991 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 1992 | case Builtin::BI__sync_val_compare_and_swap_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 1993 | BuiltinIndex = 12; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1994 | NumFixed = 2; |
| 1995 | break; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1996 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1997 | case Builtin::BI__sync_bool_compare_and_swap: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1998 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 1999 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 2000 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 2001 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 2002 | case Builtin::BI__sync_bool_compare_and_swap_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2003 | BuiltinIndex = 13; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2004 | NumFixed = 2; |
Chandler Carruth | 3973af7 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 2005 | ResultType = Context.BoolTy; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2006 | break; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2007 | |
| 2008 | case Builtin::BI__sync_lock_test_and_set: |
| 2009 | case Builtin::BI__sync_lock_test_and_set_1: |
| 2010 | case Builtin::BI__sync_lock_test_and_set_2: |
| 2011 | case Builtin::BI__sync_lock_test_and_set_4: |
| 2012 | case Builtin::BI__sync_lock_test_and_set_8: |
| 2013 | case Builtin::BI__sync_lock_test_and_set_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2014 | BuiltinIndex = 14; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2015 | break; |
| 2016 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2017 | case Builtin::BI__sync_lock_release: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2018 | case Builtin::BI__sync_lock_release_1: |
| 2019 | case Builtin::BI__sync_lock_release_2: |
| 2020 | case Builtin::BI__sync_lock_release_4: |
| 2021 | case Builtin::BI__sync_lock_release_8: |
| 2022 | case Builtin::BI__sync_lock_release_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2023 | BuiltinIndex = 15; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2024 | NumFixed = 0; |
Chandler Carruth | 3973af7 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 2025 | ResultType = Context.VoidTy; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2026 | break; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2027 | |
| 2028 | case Builtin::BI__sync_swap: |
| 2029 | case Builtin::BI__sync_swap_1: |
| 2030 | case Builtin::BI__sync_swap_2: |
| 2031 | case Builtin::BI__sync_swap_4: |
| 2032 | case Builtin::BI__sync_swap_8: |
| 2033 | case Builtin::BI__sync_swap_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2034 | BuiltinIndex = 16; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2035 | break; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2036 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2037 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2038 | // Now that we know how many fixed arguments we expect, first check that we |
| 2039 | // have at least that many. |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 2040 | if (TheCall->getNumArgs() < 1+NumFixed) { |
| 2041 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 2042 | << 0 << 1+NumFixed << TheCall->getNumArgs() |
| 2043 | << TheCall->getCallee()->getSourceRange(); |
| 2044 | return ExprError(); |
| 2045 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2046 | |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2047 | if (WarnAboutSemanticsChange) { |
| 2048 | Diag(TheCall->getLocEnd(), diag::warn_sync_fetch_and_nand_semantics_change) |
| 2049 | << TheCall->getCallee()->getSourceRange(); |
| 2050 | } |
| 2051 | |
Chris Lattner | 5b9241b | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 2052 | // Get the decl for the concrete builtin from this, we can tell what the |
| 2053 | // concrete integer type we should convert to is. |
| 2054 | unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; |
| 2055 | const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID); |
Abramo Bagnara | 6cba23a | 2012-09-22 09:05:22 +0000 | [diff] [blame] | 2056 | FunctionDecl *NewBuiltinDecl; |
| 2057 | if (NewBuiltinID == BuiltinID) |
| 2058 | NewBuiltinDecl = FDecl; |
| 2059 | else { |
| 2060 | // Perform builtin lookup to avoid redeclaring it. |
| 2061 | DeclarationName DN(&Context.Idents.get(NewBuiltinName)); |
| 2062 | LookupResult Res(*this, DN, DRE->getLocStart(), LookupOrdinaryName); |
| 2063 | LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true); |
| 2064 | assert(Res.getFoundDecl()); |
| 2065 | NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2066 | if (!NewBuiltinDecl) |
Abramo Bagnara | 6cba23a | 2012-09-22 09:05:22 +0000 | [diff] [blame] | 2067 | return ExprError(); |
| 2068 | } |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 2069 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2070 | // The first argument --- the pointer --- has a fixed type; we |
| 2071 | // deduce the types of the rest of the arguments accordingly. Walk |
| 2072 | // the remaining arguments, converting them to the deduced value type. |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2073 | for (unsigned i = 0; i != NumFixed; ++i) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2074 | ExprResult Arg = TheCall->getArg(i+1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2075 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2076 | // GCC does an implicit conversion to the pointer or integer ValType. This |
| 2077 | // 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] | 2078 | // Initialize the argument. |
| 2079 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 2080 | ValType, /*consume*/ false); |
| 2081 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2082 | if (Arg.isInvalid()) |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 2083 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2084 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2085 | // Okay, we have something that *can* be converted to the right type. Check |
| 2086 | // to see if there is a potentially weird extension going on here. This can |
| 2087 | // happen when you do an atomic operation on something like an char* and |
| 2088 | // pass in 42. The 42 gets converted to char. This is even more strange |
| 2089 | // for things like 45.123 -> char, etc. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2090 | // FIXME: Do this check. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2091 | TheCall->setArg(i+1, Arg.get()); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2092 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2093 | |
Douglas Gregor | 6b3bcf2 | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 2094 | ASTContext& Context = this->getASTContext(); |
| 2095 | |
| 2096 | // Create a new DeclRefExpr to refer to the new decl. |
| 2097 | DeclRefExpr* NewDRE = DeclRefExpr::Create( |
| 2098 | Context, |
| 2099 | DRE->getQualifierLoc(), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2100 | SourceLocation(), |
Douglas Gregor | 6b3bcf2 | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 2101 | NewBuiltinDecl, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2102 | /*enclosing*/ false, |
Douglas Gregor | 6b3bcf2 | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 2103 | DRE->getLocation(), |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 2104 | Context.BuiltinFnTy, |
Douglas Gregor | 6b3bcf2 | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 2105 | DRE->getValueKind()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2106 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2107 | // Set the callee in the CallExpr. |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 2108 | // FIXME: This loses syntactic information. |
| 2109 | QualType CalleePtrTy = Context.getPointerType(NewBuiltinDecl->getType()); |
| 2110 | ExprResult PromotedCall = ImpCastExprToType(NewDRE, CalleePtrTy, |
| 2111 | CK_BuiltinFnToFnPtr); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2112 | TheCall->setCallee(PromotedCall.get()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2113 | |
Chandler Carruth | bc8cab1 | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 2114 | // Change the result type of the call to match the original value type. This |
| 2115 | // is arbitrary, but the codegen for these builtins ins design to handle it |
| 2116 | // gracefully. |
Chandler Carruth | 3973af7 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 2117 | TheCall->setType(ResultType); |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 2118 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2119 | return TheCallResult; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
Chris Lattner | 6436fb6 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 2122 | /// CheckObjCString - Checks that the argument to the builtin |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2123 | /// CFString constructor is correct |
Steve Naroff | fb46e86 | 2009-04-13 20:26:29 +0000 | [diff] [blame] | 2124 | /// Note: It might also make sense to do the UTF-16 conversion here (would |
| 2125 | /// simplify the backend). |
Chris Lattner | 6436fb6 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 2126 | bool Sema::CheckObjCString(Expr *Arg) { |
Chris Lattner | f266096 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 2127 | Arg = Arg->IgnoreParenCasts(); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2128 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); |
| 2129 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2130 | if (!Literal || !Literal->isAscii()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2131 | Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 2132 | << Arg->getSourceRange(); |
Anders Carlsson | a3a9c43 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 2133 | return true; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2134 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2135 | |
Fariborz Jahanian | 56603ef | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 2136 | if (Literal->containsNonAsciiOrNull()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2137 | StringRef String = Literal->getString(); |
Fariborz Jahanian | 56603ef | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 2138 | unsigned NumBytes = String.size(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2139 | SmallVector<UTF16, 128> ToBuf(NumBytes); |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 2140 | const UTF8 *FromPtr = (const UTF8 *)String.data(); |
Fariborz Jahanian | 56603ef | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 2141 | UTF16 *ToPtr = &ToBuf[0]; |
| 2142 | |
| 2143 | ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, |
| 2144 | &ToPtr, ToPtr + NumBytes, |
| 2145 | strictConversion); |
| 2146 | // Check for conversion failure. |
| 2147 | if (Result != conversionOK) |
| 2148 | Diag(Arg->getLocStart(), |
| 2149 | diag::warn_cfstring_truncated) << Arg->getSourceRange(); |
| 2150 | } |
Anders Carlsson | a3a9c43 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 2151 | return false; |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
Chris Lattner | e202e6a | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 2154 | /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity. |
| 2155 | /// Emit an error and return true on failure, return false on success. |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2156 | bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) { |
| 2157 | Expr *Fn = TheCall->getCallee(); |
| 2158 | if (TheCall->getNumArgs() > 2) { |
Chris Lattner | cedef8d | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 2159 | Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2160 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 2161 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
| 2162 | << Fn->getSourceRange() |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2163 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2164 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2165 | return true; |
| 2166 | } |
Eli Friedman | bb2b3be | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 2167 | |
| 2168 | if (TheCall->getNumArgs() < 2) { |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 2169 | return Diag(TheCall->getLocEnd(), |
| 2170 | diag::err_typecheck_call_too_few_args_at_least) |
| 2171 | << 0 /*function call*/ << 2 << TheCall->getNumArgs(); |
Eli Friedman | bb2b3be | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
John McCall | 29ad95b | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 2174 | // Type-check the first argument normally. |
| 2175 | if (checkBuiltinArgument(*this, TheCall, 0)) |
| 2176 | return true; |
| 2177 | |
Chris Lattner | e202e6a | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 2178 | // Determine whether the current function is variadic or not. |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 2179 | BlockScopeInfo *CurBlock = getCurBlock(); |
Chris Lattner | e202e6a | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 2180 | bool isVariadic; |
Steve Naroff | 439a3e4 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 2181 | if (CurBlock) |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 2182 | isVariadic = CurBlock->TheDecl->isVariadic(); |
Ted Kremenek | 186a074 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 2183 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
| 2184 | isVariadic = FD->isVariadic(); |
| 2185 | else |
Argyrios Kyrtzidis | 853fbea | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 2186 | isVariadic = getCurMethodDecl()->isVariadic(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2187 | |
Chris Lattner | e202e6a | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 2188 | if (!isVariadic) { |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2189 | Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function); |
| 2190 | return true; |
| 2191 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2192 | |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2193 | // Verify that the second argument to the builtin is the last argument of the |
| 2194 | // current function or method. |
| 2195 | bool SecondArgIsLastNamedArgument = false; |
Anders Carlsson | 73cc507 | 2008-02-13 01:22:59 +0000 | [diff] [blame] | 2196 | const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2197 | |
Nico Weber | 9eea764 | 2013-05-24 23:31:57 +0000 | [diff] [blame] | 2198 | // These are valid if SecondArgIsLastNamedArgument is false after the next |
| 2199 | // block. |
| 2200 | QualType Type; |
| 2201 | SourceLocation ParamLoc; |
| 2202 | |
Anders Carlsson | 6a8350b | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 2203 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { |
| 2204 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2205 | // FIXME: This isn't correct for methods (results in bogus warning). |
| 2206 | // Get the last formal in the current function. |
Anders Carlsson | 6a8350b | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 2207 | const ParmVarDecl *LastArg; |
Steve Naroff | 439a3e4 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 2208 | if (CurBlock) |
| 2209 | LastArg = *(CurBlock->TheDecl->param_end()-1); |
| 2210 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 2211 | LastArg = *(FD->param_end()-1); |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2212 | else |
Argyrios Kyrtzidis | 853fbea | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 2213 | LastArg = *(getCurMethodDecl()->param_end()-1); |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2214 | SecondArgIsLastNamedArgument = PV == LastArg; |
Nico Weber | 9eea764 | 2013-05-24 23:31:57 +0000 | [diff] [blame] | 2215 | |
| 2216 | Type = PV->getType(); |
| 2217 | ParamLoc = PV->getLocation(); |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2218 | } |
| 2219 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2220 | |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2221 | if (!SecondArgIsLastNamedArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2222 | Diag(TheCall->getArg(1)->getLocStart(), |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2223 | diag::warn_second_parameter_of_va_start_not_last_named_argument); |
Nico Weber | 9eea764 | 2013-05-24 23:31:57 +0000 | [diff] [blame] | 2224 | else if (Type->isReferenceType()) { |
| 2225 | Diag(Arg->getLocStart(), |
| 2226 | diag::warn_va_start_of_reference_type_is_undefined); |
| 2227 | Diag(ParamLoc, diag::note_parameter_type) << Type; |
| 2228 | } |
| 2229 | |
Enea Zaffanella | b1b1b8a | 2013-11-07 08:14:26 +0000 | [diff] [blame] | 2230 | TheCall->setType(Context.VoidTy); |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2231 | return false; |
Eli Friedman | f835303 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 2232 | } |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2233 | |
Saleem Abdulrasool | 202aac1 | 2014-07-22 02:01:04 +0000 | [diff] [blame] | 2234 | bool Sema::SemaBuiltinVAStartARM(CallExpr *Call) { |
| 2235 | // void __va_start(va_list *ap, const char *named_addr, size_t slot_size, |
| 2236 | // const char *named_addr); |
| 2237 | |
| 2238 | Expr *Func = Call->getCallee(); |
| 2239 | |
| 2240 | if (Call->getNumArgs() < 3) |
| 2241 | return Diag(Call->getLocEnd(), |
| 2242 | diag::err_typecheck_call_too_few_args_at_least) |
| 2243 | << 0 /*function call*/ << 3 << Call->getNumArgs(); |
| 2244 | |
| 2245 | // Determine whether the current function is variadic or not. |
| 2246 | bool IsVariadic; |
| 2247 | if (BlockScopeInfo *CurBlock = getCurBlock()) |
| 2248 | IsVariadic = CurBlock->TheDecl->isVariadic(); |
| 2249 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
| 2250 | IsVariadic = FD->isVariadic(); |
| 2251 | else if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 2252 | IsVariadic = MD->isVariadic(); |
| 2253 | else |
| 2254 | llvm_unreachable("unexpected statement type"); |
| 2255 | |
| 2256 | if (!IsVariadic) { |
| 2257 | Diag(Func->getLocStart(), diag::err_va_start_used_in_non_variadic_function); |
| 2258 | return true; |
| 2259 | } |
| 2260 | |
| 2261 | // Type-check the first argument normally. |
| 2262 | if (checkBuiltinArgument(*this, Call, 0)) |
| 2263 | return true; |
| 2264 | |
Benjamin Kramer | e0ca6e1 | 2015-03-01 18:09:50 +0000 | [diff] [blame] | 2265 | const struct { |
Saleem Abdulrasool | 202aac1 | 2014-07-22 02:01:04 +0000 | [diff] [blame] | 2266 | unsigned ArgNo; |
| 2267 | QualType Type; |
| 2268 | } ArgumentTypes[] = { |
| 2269 | { 1, Context.getPointerType(Context.CharTy.withConst()) }, |
| 2270 | { 2, Context.getSizeType() }, |
| 2271 | }; |
| 2272 | |
| 2273 | for (const auto &AT : ArgumentTypes) { |
| 2274 | const Expr *Arg = Call->getArg(AT.ArgNo)->IgnoreParens(); |
| 2275 | if (Arg->getType().getCanonicalType() == AT.Type.getCanonicalType()) |
| 2276 | continue; |
| 2277 | Diag(Arg->getLocStart(), diag::err_typecheck_convert_incompatible) |
| 2278 | << Arg->getType() << AT.Type << 1 /* different class */ |
| 2279 | << 0 /* qualifier difference */ << 3 /* parameter mismatch */ |
| 2280 | << AT.ArgNo + 1 << Arg->getType() << AT.Type; |
| 2281 | } |
| 2282 | |
| 2283 | return false; |
| 2284 | } |
| 2285 | |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 2286 | /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and |
| 2287 | /// friends. This is declared to take (...), so we have to check everything. |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2288 | bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { |
| 2289 | if (TheCall->getNumArgs() < 2) |
Chris Lattner | cedef8d | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 2290 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 2291 | << 0 << 2 << TheCall->getNumArgs()/*function call*/; |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2292 | if (TheCall->getNumArgs() > 2) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2293 | return Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2294 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 2295 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2296 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
| 2297 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2298 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2299 | ExprResult OrigArg0 = TheCall->getArg(0); |
| 2300 | ExprResult OrigArg1 = TheCall->getArg(1); |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 2301 | |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 2302 | // Do standard promotions between the two arguments, returning their common |
| 2303 | // type. |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2304 | QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2305 | if (OrigArg0.isInvalid() || OrigArg1.isInvalid()) |
| 2306 | return true; |
Daniel Dunbar | 96f8677 | 2009-02-19 19:28:43 +0000 | [diff] [blame] | 2307 | |
| 2308 | // Make sure any conversions are pushed back into the call; this is |
| 2309 | // type safe since unordered compare builtins are declared as "_Bool |
| 2310 | // foo(...)". |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2311 | TheCall->setArg(0, OrigArg0.get()); |
| 2312 | TheCall->setArg(1, OrigArg1.get()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2313 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2314 | if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent()) |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 2315 | return false; |
| 2316 | |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 2317 | // If the common type isn't a real floating type, then the arguments were |
| 2318 | // invalid for this operation. |
Eli Friedman | 93ee5ca | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 2319 | if (Res.isNull() || !Res->isRealFloatingType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2320 | return Diag(OrigArg0.get()->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2321 | diag::err_typecheck_call_invalid_ordered_compare) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2322 | << OrigArg0.get()->getType() << OrigArg1.get()->getType() |
| 2323 | << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2324 | |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 2325 | return false; |
| 2326 | } |
| 2327 | |
Benjamin Kramer | 634fc10 | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 2328 | /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like |
| 2329 | /// __builtin_isnan and friends. This is declared to take (...), so we have |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 2330 | /// to check everything. We expect the last argument to be a floating point |
| 2331 | /// value. |
| 2332 | bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) { |
| 2333 | if (TheCall->getNumArgs() < NumArgs) |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 2334 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 2335 | << 0 << NumArgs << TheCall->getNumArgs()/*function call*/; |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 2336 | if (TheCall->getNumArgs() > NumArgs) |
| 2337 | return Diag(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 2338 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 2339 | << 0 /*function call*/ << NumArgs << TheCall->getNumArgs() |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 2340 | << SourceRange(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 2341 | (*(TheCall->arg_end()-1))->getLocEnd()); |
| 2342 | |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 2343 | Expr *OrigArg = TheCall->getArg(NumArgs-1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2344 | |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 2345 | if (OrigArg->isTypeDependent()) |
| 2346 | return false; |
| 2347 | |
Chris Lattner | 68784ef | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 2348 | // This operation requires a non-_Complex floating-point number. |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 2349 | if (!OrigArg->getType()->isRealFloatingType()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2350 | return Diag(OrigArg->getLocStart(), |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 2351 | diag::err_typecheck_call_invalid_unary_fp) |
| 2352 | << OrigArg->getType() << OrigArg->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2353 | |
Chris Lattner | 68784ef | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 2354 | // If this is an implicit conversion from float -> double, remove it. |
| 2355 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) { |
| 2356 | Expr *CastArg = Cast->getSubExpr(); |
| 2357 | if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) { |
| 2358 | assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) && |
| 2359 | "promotion from float to double is the only expected cast here"); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2360 | Cast->setSubExpr(nullptr); |
Chris Lattner | 68784ef | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 2361 | TheCall->setArg(NumArgs-1, CastArg); |
Chris Lattner | 68784ef | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 2362 | } |
| 2363 | } |
| 2364 | |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 2365 | return false; |
| 2366 | } |
| 2367 | |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2368 | /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. |
| 2369 | // This is declared to take (...), so we have to check everything. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2370 | ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 2371 | if (TheCall->getNumArgs() < 2) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2372 | return ExprError(Diag(TheCall->getLocEnd(), |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 2373 | diag::err_typecheck_call_too_few_args_at_least) |
Craig Topper | 304602a | 2013-07-28 21:50:10 +0000 | [diff] [blame] | 2374 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
| 2375 | << TheCall->getSourceRange()); |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2376 | |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 2377 | // Determine which of the following types of shufflevector we're checking: |
| 2378 | // 1) unary, vector mask: (lhs, mask) |
| 2379 | // 2) binary, vector mask: (lhs, rhs, mask) |
| 2380 | // 3) binary, scalar mask: (lhs, rhs, index, ..., index) |
| 2381 | QualType resType = TheCall->getArg(0)->getType(); |
| 2382 | unsigned numElements = 0; |
Craig Topper | 61d01cc | 2013-07-19 04:46:31 +0000 | [diff] [blame] | 2383 | |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 2384 | if (!TheCall->getArg(0)->isTypeDependent() && |
| 2385 | !TheCall->getArg(1)->isTypeDependent()) { |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 2386 | QualType LHSType = TheCall->getArg(0)->getType(); |
| 2387 | QualType RHSType = TheCall->getArg(1)->getType(); |
Craig Topper | 61d01cc | 2013-07-19 04:46:31 +0000 | [diff] [blame] | 2388 | |
Craig Topper | baca389 | 2013-07-29 06:47:04 +0000 | [diff] [blame] | 2389 | if (!LHSType->isVectorType() || !RHSType->isVectorType()) |
| 2390 | return ExprError(Diag(TheCall->getLocStart(), |
| 2391 | diag::err_shufflevector_non_vector) |
| 2392 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
| 2393 | TheCall->getArg(1)->getLocEnd())); |
Craig Topper | 61d01cc | 2013-07-19 04:46:31 +0000 | [diff] [blame] | 2394 | |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 2395 | numElements = LHSType->getAs<VectorType>()->getNumElements(); |
| 2396 | unsigned numResElements = TheCall->getNumArgs() - 2; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2397 | |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 2398 | // Check to see if we have a call with 2 vector arguments, the unary shuffle |
| 2399 | // with mask. If so, verify that RHS is an integer vector type with the |
| 2400 | // same number of elts as lhs. |
| 2401 | if (TheCall->getNumArgs() == 2) { |
Sylvestre Ledru | 8e5d82e | 2013-07-06 08:00:09 +0000 | [diff] [blame] | 2402 | if (!RHSType->hasIntegerRepresentation() || |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 2403 | RHSType->getAs<VectorType>()->getNumElements() != numElements) |
Craig Topper | baca389 | 2013-07-29 06:47:04 +0000 | [diff] [blame] | 2404 | return ExprError(Diag(TheCall->getLocStart(), |
| 2405 | diag::err_shufflevector_incompatible_vector) |
| 2406 | << SourceRange(TheCall->getArg(1)->getLocStart(), |
| 2407 | TheCall->getArg(1)->getLocEnd())); |
Craig Topper | 61d01cc | 2013-07-19 04:46:31 +0000 | [diff] [blame] | 2408 | } else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { |
Craig Topper | baca389 | 2013-07-29 06:47:04 +0000 | [diff] [blame] | 2409 | return ExprError(Diag(TheCall->getLocStart(), |
| 2410 | diag::err_shufflevector_incompatible_vector) |
| 2411 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
| 2412 | TheCall->getArg(1)->getLocEnd())); |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 2413 | } else if (numElements != numResElements) { |
| 2414 | QualType eltType = LHSType->getAs<VectorType>()->getElementType(); |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2415 | resType = Context.getVectorType(eltType, numResElements, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2416 | VectorType::GenericVector); |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 2417 | } |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2418 | } |
| 2419 | |
| 2420 | for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 2421 | if (TheCall->getArg(i)->isTypeDependent() || |
| 2422 | TheCall->getArg(i)->isValueDependent()) |
| 2423 | continue; |
| 2424 | |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 2425 | llvm::APSInt Result(32); |
| 2426 | if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) |
| 2427 | return ExprError(Diag(TheCall->getLocStart(), |
Craig Topper | 304602a | 2013-07-28 21:50:10 +0000 | [diff] [blame] | 2428 | diag::err_shufflevector_nonconstant_argument) |
| 2429 | << TheCall->getArg(i)->getSourceRange()); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2430 | |
Craig Topper | 50ad5b7 | 2013-08-03 17:40:38 +0000 | [diff] [blame] | 2431 | // Allow -1 which will be translated to undef in the IR. |
| 2432 | if (Result.isSigned() && Result.isAllOnesValue()) |
| 2433 | continue; |
| 2434 | |
Chris Lattner | 7ab824e | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 2435 | if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2436 | return ExprError(Diag(TheCall->getLocStart(), |
Craig Topper | 304602a | 2013-07-28 21:50:10 +0000 | [diff] [blame] | 2437 | diag::err_shufflevector_argument_too_large) |
| 2438 | << TheCall->getArg(i)->getSourceRange()); |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2441 | SmallVector<Expr*, 32> exprs; |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2442 | |
Chris Lattner | 7ab824e | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 2443 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2444 | exprs.push_back(TheCall->getArg(i)); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2445 | TheCall->setArg(i, nullptr); |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2448 | return new (Context) ShuffleVectorExpr(Context, exprs, resType, |
| 2449 | TheCall->getCallee()->getLocStart(), |
| 2450 | TheCall->getRParenLoc()); |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2451 | } |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2452 | |
Hal Finkel | c4d7c82 | 2013-09-18 03:29:45 +0000 | [diff] [blame] | 2453 | /// SemaConvertVectorExpr - Handle __builtin_convertvector |
| 2454 | ExprResult Sema::SemaConvertVectorExpr(Expr *E, TypeSourceInfo *TInfo, |
| 2455 | SourceLocation BuiltinLoc, |
| 2456 | SourceLocation RParenLoc) { |
| 2457 | ExprValueKind VK = VK_RValue; |
| 2458 | ExprObjectKind OK = OK_Ordinary; |
| 2459 | QualType DstTy = TInfo->getType(); |
| 2460 | QualType SrcTy = E->getType(); |
| 2461 | |
| 2462 | if (!SrcTy->isVectorType() && !SrcTy->isDependentType()) |
| 2463 | return ExprError(Diag(BuiltinLoc, |
| 2464 | diag::err_convertvector_non_vector) |
| 2465 | << E->getSourceRange()); |
| 2466 | if (!DstTy->isVectorType() && !DstTy->isDependentType()) |
| 2467 | return ExprError(Diag(BuiltinLoc, |
| 2468 | diag::err_convertvector_non_vector_type)); |
| 2469 | |
| 2470 | if (!SrcTy->isDependentType() && !DstTy->isDependentType()) { |
| 2471 | unsigned SrcElts = SrcTy->getAs<VectorType>()->getNumElements(); |
| 2472 | unsigned DstElts = DstTy->getAs<VectorType>()->getNumElements(); |
| 2473 | if (SrcElts != DstElts) |
| 2474 | return ExprError(Diag(BuiltinLoc, |
| 2475 | diag::err_convertvector_incompatible_vector) |
| 2476 | << E->getSourceRange()); |
| 2477 | } |
| 2478 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2479 | return new (Context) |
| 2480 | ConvertVectorExpr(E, TInfo, DstTy, VK, OK, BuiltinLoc, RParenLoc); |
Hal Finkel | c4d7c82 | 2013-09-18 03:29:45 +0000 | [diff] [blame] | 2481 | } |
| 2482 | |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 2483 | /// SemaBuiltinPrefetch - Handle __builtin_prefetch. |
| 2484 | // This is declared to take (const void*, ...) and can take two |
| 2485 | // optional constant int args. |
| 2486 | bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2487 | unsigned NumArgs = TheCall->getNumArgs(); |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 2488 | |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2489 | if (NumArgs > 3) |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 2490 | return Diag(TheCall->getLocEnd(), |
| 2491 | diag::err_typecheck_call_too_many_args_at_most) |
| 2492 | << 0 /*function call*/ << 3 << NumArgs |
| 2493 | << TheCall->getSourceRange(); |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 2494 | |
| 2495 | // Argument 0 is checked for us and the remaining arguments must be |
| 2496 | // constant integers. |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 2497 | for (unsigned i = 1; i != NumArgs; ++i) |
| 2498 | if (SemaBuiltinConstantArgRange(TheCall, i, 0, i == 1 ? 1 : 3)) |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 2499 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2500 | |
Warren Hunt | 20e4a5d | 2014-02-21 23:08:53 +0000 | [diff] [blame] | 2501 | return false; |
| 2502 | } |
| 2503 | |
Hal Finkel | f041733 | 2014-07-17 14:25:55 +0000 | [diff] [blame] | 2504 | /// SemaBuiltinAssume - Handle __assume (MS Extension). |
| 2505 | // __assume does not evaluate its arguments, and should warn if its argument |
| 2506 | // has side effects. |
| 2507 | bool Sema::SemaBuiltinAssume(CallExpr *TheCall) { |
| 2508 | Expr *Arg = TheCall->getArg(0); |
| 2509 | if (Arg->isInstantiationDependent()) return false; |
| 2510 | |
| 2511 | if (Arg->HasSideEffects(Context)) |
David Majnemer | 5123664 | 2015-02-26 00:57:33 +0000 | [diff] [blame] | 2512 | Diag(Arg->getLocStart(), diag::warn_assume_side_effects) |
Hal Finkel | bcc0608 | 2014-09-07 22:58:14 +0000 | [diff] [blame] | 2513 | << Arg->getSourceRange() |
| 2514 | << cast<FunctionDecl>(TheCall->getCalleeDecl())->getIdentifier(); |
| 2515 | |
| 2516 | return false; |
| 2517 | } |
| 2518 | |
| 2519 | /// Handle __builtin_assume_aligned. This is declared |
| 2520 | /// as (const void*, size_t, ...) and can take one optional constant int arg. |
| 2521 | bool Sema::SemaBuiltinAssumeAligned(CallExpr *TheCall) { |
| 2522 | unsigned NumArgs = TheCall->getNumArgs(); |
| 2523 | |
| 2524 | if (NumArgs > 3) |
| 2525 | return Diag(TheCall->getLocEnd(), |
| 2526 | diag::err_typecheck_call_too_many_args_at_most) |
| 2527 | << 0 /*function call*/ << 3 << NumArgs |
| 2528 | << TheCall->getSourceRange(); |
| 2529 | |
| 2530 | // The alignment must be a constant integer. |
| 2531 | Expr *Arg = TheCall->getArg(1); |
| 2532 | |
| 2533 | // We can't check the value of a dependent argument. |
| 2534 | if (!Arg->isTypeDependent() && !Arg->isValueDependent()) { |
| 2535 | llvm::APSInt Result; |
| 2536 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 2537 | return true; |
| 2538 | |
| 2539 | if (!Result.isPowerOf2()) |
| 2540 | return Diag(TheCall->getLocStart(), |
| 2541 | diag::err_alignment_not_power_of_two) |
| 2542 | << Arg->getSourceRange(); |
| 2543 | } |
| 2544 | |
| 2545 | if (NumArgs > 2) { |
| 2546 | ExprResult Arg(TheCall->getArg(2)); |
| 2547 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 2548 | Context.getSizeType(), false); |
| 2549 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 2550 | if (Arg.isInvalid()) return true; |
| 2551 | TheCall->setArg(2, Arg.get()); |
| 2552 | } |
Hal Finkel | f041733 | 2014-07-17 14:25:55 +0000 | [diff] [blame] | 2553 | |
| 2554 | return false; |
| 2555 | } |
| 2556 | |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 2557 | /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr |
| 2558 | /// TheCall is a constant expression. |
| 2559 | bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum, |
| 2560 | llvm::APSInt &Result) { |
| 2561 | Expr *Arg = TheCall->getArg(ArgNum); |
| 2562 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 2563 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 2564 | |
| 2565 | if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; |
| 2566 | |
| 2567 | if (!Arg->isIntegerConstantExpr(Result, Context)) |
| 2568 | return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type) |
Eric Christopher | 63448c3 | 2010-04-19 18:23:02 +0000 | [diff] [blame] | 2569 | << FDecl->getDeclName() << Arg->getSourceRange(); |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 2570 | |
Chris Lattner | d545ad1 | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 2571 | return false; |
| 2572 | } |
| 2573 | |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 2574 | /// SemaBuiltinConstantArgRange - Handle a check if argument ArgNum of CallExpr |
| 2575 | /// TheCall is a constant expression in the range [Low, High]. |
| 2576 | bool Sema::SemaBuiltinConstantArgRange(CallExpr *TheCall, int ArgNum, |
| 2577 | int Low, int High) { |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 2578 | llvm::APSInt Result; |
Douglas Gregor | 98c3cfc | 2012-06-29 01:05:22 +0000 | [diff] [blame] | 2579 | |
| 2580 | // We can't check the value of a dependent argument. |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 2581 | Expr *Arg = TheCall->getArg(ArgNum); |
| 2582 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
Douglas Gregor | 98c3cfc | 2012-06-29 01:05:22 +0000 | [diff] [blame] | 2583 | return false; |
| 2584 | |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 2585 | // Check constant-ness first. |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 2586 | if (SemaBuiltinConstantArg(TheCall, ArgNum, Result)) |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 2587 | return true; |
| 2588 | |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 2589 | if (Result.getSExtValue() < Low || Result.getSExtValue() > High) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2590 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 2591 | << Low << High << Arg->getSourceRange(); |
Daniel Dunbar | b0d34c8 | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 2592 | |
| 2593 | return false; |
| 2594 | } |
| 2595 | |
Eli Friedman | c97d014 | 2009-05-03 06:04:26 +0000 | [diff] [blame] | 2596 | /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). |
Joerg Sonnenberger | 2717328 | 2015-03-11 23:46:32 +0000 | [diff] [blame] | 2597 | /// This checks that the target supports __builtin_longjmp and |
| 2598 | /// that val is a constant 1. |
Eli Friedman | eed8ad2 | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 2599 | bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { |
Joerg Sonnenberger | 2717328 | 2015-03-11 23:46:32 +0000 | [diff] [blame] | 2600 | if (!Context.getTargetInfo().hasSjLjLowering()) |
| 2601 | return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_unsupported) |
| 2602 | << SourceRange(TheCall->getLocStart(), TheCall->getLocEnd()); |
| 2603 | |
Eli Friedman | eed8ad2 | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 2604 | Expr *Arg = TheCall->getArg(1); |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 2605 | llvm::APSInt Result; |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 2606 | |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 2607 | // TODO: This is less than ideal. Overload this to take a value. |
| 2608 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 2609 | return true; |
| 2610 | |
| 2611 | if (Result != 1) |
Eli Friedman | eed8ad2 | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 2612 | return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val) |
| 2613 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
| 2614 | |
| 2615 | return false; |
| 2616 | } |
| 2617 | |
Joerg Sonnenberger | 2717328 | 2015-03-11 23:46:32 +0000 | [diff] [blame] | 2618 | |
| 2619 | /// SemaBuiltinSetjmp - Handle __builtin_setjmp(void *env[5]). |
| 2620 | /// This checks that the target supports __builtin_setjmp. |
| 2621 | bool Sema::SemaBuiltinSetjmp(CallExpr *TheCall) { |
| 2622 | if (!Context.getTargetInfo().hasSjLjLowering()) |
| 2623 | return Diag(TheCall->getLocStart(), diag::err_builtin_setjmp_unsupported) |
| 2624 | << SourceRange(TheCall->getLocStart(), TheCall->getLocEnd()); |
| 2625 | return false; |
| 2626 | } |
| 2627 | |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2628 | namespace { |
| 2629 | enum StringLiteralCheckType { |
| 2630 | SLCT_NotALiteral, |
| 2631 | SLCT_UncheckedLiteral, |
| 2632 | SLCT_CheckedLiteral |
| 2633 | }; |
| 2634 | } |
| 2635 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2636 | // Determine if an expression is a string literal or constant string. |
| 2637 | // If this function returns false on the arguments to a function expecting a |
| 2638 | // format string, we will usually need to emit a warning. |
| 2639 | // True string literals are then checked by CheckFormatString. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2640 | static StringLiteralCheckType |
| 2641 | checkFormatStringExpr(Sema &S, const Expr *E, ArrayRef<const Expr *> Args, |
| 2642 | bool HasVAListArg, unsigned format_idx, |
| 2643 | unsigned firstDataArg, Sema::FormatStringType Type, |
| 2644 | Sema::VariadicCallType CallType, bool InFunctionCall, |
| 2645 | llvm::SmallBitVector &CheckedVarArgs) { |
Ted Kremenek | 80882935 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 2646 | tryAgain: |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 2647 | if (E->isTypeDependent() || E->isValueDependent()) |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2648 | return SLCT_NotALiteral; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2649 | |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2650 | E = E->IgnoreParenCasts(); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2651 | |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2652 | if (E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)) |
David Blaikie | 59fe3f8 | 2012-02-10 21:07:25 +0000 | [diff] [blame] | 2653 | // Technically -Wformat-nonliteral does not warn about this case. |
| 2654 | // The behavior of printf and friends in this case is implementation |
| 2655 | // dependent. Ideally if the format string cannot be null then |
| 2656 | // it should have a 'nonnull' attribute in the function prototype. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2657 | return SLCT_UncheckedLiteral; |
David Blaikie | 59fe3f8 | 2012-02-10 21:07:25 +0000 | [diff] [blame] | 2658 | |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2659 | switch (E->getStmtClass()) { |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2660 | case Stmt::BinaryConditionalOperatorClass: |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2661 | case Stmt::ConditionalOperatorClass: { |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2662 | // The expression is a literal if both sub-expressions were, and it was |
| 2663 | // completely checked only if both sub-expressions were checked. |
| 2664 | const AbstractConditionalOperator *C = |
| 2665 | cast<AbstractConditionalOperator>(E); |
| 2666 | StringLiteralCheckType Left = |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2667 | checkFormatStringExpr(S, C->getTrueExpr(), Args, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2668 | HasVAListArg, format_idx, firstDataArg, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2669 | Type, CallType, InFunctionCall, CheckedVarArgs); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2670 | if (Left == SLCT_NotALiteral) |
| 2671 | return SLCT_NotALiteral; |
| 2672 | StringLiteralCheckType Right = |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2673 | checkFormatStringExpr(S, C->getFalseExpr(), Args, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2674 | HasVAListArg, format_idx, firstDataArg, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2675 | Type, CallType, InFunctionCall, CheckedVarArgs); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2676 | return Left < Right ? Left : Right; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | case Stmt::ImplicitCastExprClass: { |
Ted Kremenek | 80882935 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 2680 | E = cast<ImplicitCastExpr>(E)->getSubExpr(); |
| 2681 | goto tryAgain; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2684 | case Stmt::OpaqueValueExprClass: |
| 2685 | if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) { |
| 2686 | E = src; |
| 2687 | goto tryAgain; |
| 2688 | } |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2689 | return SLCT_NotALiteral; |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2690 | |
Ted Kremenek | a889083 | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 2691 | case Stmt::PredefinedExprClass: |
| 2692 | // While __func__, etc., are technically not string literals, they |
| 2693 | // cannot contain format specifiers and thus are not a security |
| 2694 | // liability. |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2695 | return SLCT_UncheckedLiteral; |
Ted Kremenek | a889083 | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 2696 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2697 | case Stmt::DeclRefExprClass: { |
| 2698 | const DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2699 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2700 | // As an exception, do not flag errors for variables binding to |
| 2701 | // const string literals. |
| 2702 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 2703 | bool isConstant = false; |
| 2704 | QualType T = DR->getType(); |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2705 | |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2706 | if (const ArrayType *AT = S.Context.getAsArrayType(T)) { |
| 2707 | isConstant = AT->getElementType().isConstant(S.Context); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2708 | } else if (const PointerType *PT = T->getAs<PointerType>()) { |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2709 | isConstant = T.isConstant(S.Context) && |
| 2710 | PT->getPointeeType().isConstant(S.Context); |
Jean-Daniel Dupas | d5f7ef4 | 2012-01-25 10:35:33 +0000 | [diff] [blame] | 2711 | } else if (T->isObjCObjectPointerType()) { |
| 2712 | // In ObjC, there is usually no "const ObjectPointer" type, |
| 2713 | // so don't check if the pointee type is constant. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2714 | isConstant = T.isConstant(S.Context); |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2715 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2716 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2717 | if (isConstant) { |
Matt Beaumont-Gay | d873508 | 2012-05-11 22:10:59 +0000 | [diff] [blame] | 2718 | if (const Expr *Init = VD->getAnyInitializer()) { |
| 2719 | // Look through initializers like const char c[] = { "foo" } |
| 2720 | if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { |
| 2721 | if (InitList->isStringLiteralInit()) |
| 2722 | Init = InitList->getInit(0)->IgnoreParenImpCasts(); |
| 2723 | } |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2724 | return checkFormatStringExpr(S, Init, Args, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2725 | HasVAListArg, format_idx, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2726 | firstDataArg, Type, CallType, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2727 | /*InFunctionCall*/false, CheckedVarArgs); |
Matt Beaumont-Gay | d873508 | 2012-05-11 22:10:59 +0000 | [diff] [blame] | 2728 | } |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2729 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2730 | |
Anders Carlsson | b012ca9 | 2009-06-28 19:55:58 +0000 | [diff] [blame] | 2731 | // For vprintf* functions (i.e., HasVAListArg==true), we add a |
| 2732 | // special check to see if the format string is a function parameter |
| 2733 | // of the function calling the printf function. If the function |
| 2734 | // has an attribute indicating it is a printf-like function, then we |
| 2735 | // should suppress warnings concerning non-literals being used in a call |
| 2736 | // to a vprintf function. For example: |
| 2737 | // |
| 2738 | // void |
| 2739 | // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){ |
| 2740 | // va_list ap; |
| 2741 | // va_start(ap, fmt); |
| 2742 | // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". |
| 2743 | // ... |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2744 | // } |
Jean-Daniel Dupas | 58dab68 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 2745 | if (HasVAListArg) { |
| 2746 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) { |
| 2747 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) { |
| 2748 | int PVIndex = PV->getFunctionScopeIndex() + 1; |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2749 | for (const auto *PVFormat : ND->specific_attrs<FormatAttr>()) { |
Jean-Daniel Dupas | 58dab68 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 2750 | // adjust for implicit parameter |
| 2751 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 2752 | if (MD->isInstance()) |
| 2753 | ++PVIndex; |
| 2754 | // We also check if the formats are compatible. |
| 2755 | // We can't pass a 'scanf' string to a 'printf' function. |
| 2756 | if (PVIndex == PVFormat->getFormatIdx() && |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2757 | Type == S.GetFormatStringType(PVFormat)) |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2758 | return SLCT_UncheckedLiteral; |
Jean-Daniel Dupas | 58dab68 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 2759 | } |
| 2760 | } |
| 2761 | } |
| 2762 | } |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2763 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2764 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2765 | return SLCT_NotALiteral; |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2766 | } |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2767 | |
Jean-Daniel Dupas | 6255bd1 | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 2768 | case Stmt::CallExprClass: |
| 2769 | case Stmt::CXXMemberCallExprClass: { |
Anders Carlsson | f0a7f3b | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 2770 | const CallExpr *CE = cast<CallExpr>(E); |
Jean-Daniel Dupas | 6255bd1 | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 2771 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) { |
| 2772 | if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) { |
| 2773 | unsigned ArgIndex = FA->getFormatIdx(); |
| 2774 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 2775 | if (MD->isInstance()) |
| 2776 | --ArgIndex; |
| 2777 | const Expr *Arg = CE->getArg(ArgIndex - 1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2778 | |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2779 | return checkFormatStringExpr(S, Arg, Args, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2780 | HasVAListArg, format_idx, firstDataArg, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2781 | Type, CallType, InFunctionCall, |
| 2782 | CheckedVarArgs); |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2783 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 2784 | unsigned BuiltinID = FD->getBuiltinID(); |
| 2785 | if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString || |
| 2786 | BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) { |
| 2787 | const Expr *Arg = CE->getArg(0); |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2788 | return checkFormatStringExpr(S, Arg, Args, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2789 | HasVAListArg, format_idx, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2790 | firstDataArg, Type, CallType, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2791 | InFunctionCall, CheckedVarArgs); |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2792 | } |
Anders Carlsson | f0a7f3b | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 2793 | } |
| 2794 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2795 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2796 | return SLCT_NotALiteral; |
Anders Carlsson | f0a7f3b | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 2797 | } |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2798 | case Stmt::ObjCStringLiteralClass: |
| 2799 | case Stmt::StringLiteralClass: { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2800 | const StringLiteral *StrE = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2801 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2802 | if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2803 | StrE = ObjCFExpr->getString(); |
| 2804 | else |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2805 | StrE = cast<StringLiteral>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2806 | |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2807 | if (StrE) { |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2808 | S.CheckFormatString(StrE, E, Args, HasVAListArg, format_idx, firstDataArg, |
| 2809 | Type, InFunctionCall, CallType, CheckedVarArgs); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2810 | return SLCT_CheckedLiteral; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2811 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2812 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2813 | return SLCT_NotALiteral; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2814 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2815 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 2816 | default: |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2817 | return SLCT_NotALiteral; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2818 | } |
| 2819 | } |
| 2820 | |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2821 | Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) { |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2822 | return llvm::StringSwitch<FormatStringType>(Format->getType()->getName()) |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2823 | .Case("scanf", FST_Scanf) |
| 2824 | .Cases("printf", "printf0", FST_Printf) |
| 2825 | .Cases("NSString", "CFString", FST_NSString) |
| 2826 | .Case("strftime", FST_Strftime) |
| 2827 | .Case("strfmon", FST_Strfmon) |
| 2828 | .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf) |
Dimitry Andric | 6b5ed34 | 2015-02-19 22:32:33 +0000 | [diff] [blame] | 2829 | .Case("freebsd_kprintf", FST_FreeBSDKPrintf) |
Fariborz Jahanian | f8dce0f | 2015-02-21 00:45:58 +0000 | [diff] [blame] | 2830 | .Case("os_trace", FST_OSTrace) |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2831 | .Default(FST_Unknown); |
| 2832 | } |
| 2833 | |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2834 | /// CheckFormatArguments - Check calls to printf and scanf (and similar |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2835 | /// functions) for correct use of format strings. |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2836 | /// Returns true if a format string has been fully checked. |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2837 | bool Sema::CheckFormatArguments(const FormatAttr *Format, |
| 2838 | ArrayRef<const Expr *> Args, |
| 2839 | bool IsCXXMember, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2840 | VariadicCallType CallType, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2841 | SourceLocation Loc, SourceRange Range, |
| 2842 | llvm::SmallBitVector &CheckedVarArgs) { |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2843 | FormatStringInfo FSI; |
| 2844 | if (getFormatStringInfo(Format, IsCXXMember, &FSI)) |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2845 | return CheckFormatArguments(Args, FSI.HasVAListArg, FSI.FormatIdx, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2846 | FSI.FirstDataArg, GetFormatStringType(Format), |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2847 | CallType, Loc, Range, CheckedVarArgs); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2848 | return false; |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2849 | } |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2850 | |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2851 | bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args, |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 2852 | bool HasVAListArg, unsigned format_idx, |
| 2853 | unsigned firstDataArg, FormatStringType Type, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2854 | VariadicCallType CallType, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2855 | SourceLocation Loc, SourceRange Range, |
| 2856 | llvm::SmallBitVector &CheckedVarArgs) { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2857 | // CHECK: printf/scanf-like function is called with no format string. |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2858 | if (format_idx >= Args.size()) { |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2859 | Diag(Loc, diag::warn_missing_format_string) << Range; |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2860 | return false; |
Ted Kremenek | e68f1aa | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 2861 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2862 | |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2863 | const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2864 | |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2865 | // CHECK: format string is not a string literal. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2866 | // |
Ted Kremenek | e68f1aa | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 2867 | // Dynamically generated format strings are difficult to |
| 2868 | // automatically vet at compile time. Requiring that format strings |
| 2869 | // are string literals: (1) permits the checking of format strings by |
| 2870 | // the compiler and thereby (2) can practically remove the source of |
| 2871 | // many format string exploits. |
Ted Kremenek | 34f664d | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 2872 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2873 | // Format string can be either ObjC string (e.g. @"%d") or |
Ted Kremenek | 34f664d | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 2874 | // C string (e.g. "%d") |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2875 | // 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] | 2876 | // the same format string checking logic for both ObjC and C strings. |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2877 | StringLiteralCheckType CT = |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2878 | checkFormatStringExpr(*this, OrigFormatExpr, Args, HasVAListArg, |
| 2879 | format_idx, firstDataArg, Type, CallType, |
| 2880 | /*IsFunctionCall*/true, CheckedVarArgs); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2881 | if (CT != SLCT_NotALiteral) |
| 2882 | // Literal format string found, check done! |
| 2883 | return CT == SLCT_CheckedLiteral; |
Ted Kremenek | 34f664d | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 2884 | |
Jean-Daniel Dupas | 6567f48 | 2012-02-07 23:10:53 +0000 | [diff] [blame] | 2885 | // Strftime is particular as it always uses a single 'time' argument, |
| 2886 | // so it is safe to pass a non-literal string. |
| 2887 | if (Type == FST_Strftime) |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2888 | return false; |
Jean-Daniel Dupas | 6567f48 | 2012-02-07 23:10:53 +0000 | [diff] [blame] | 2889 | |
Jean-Daniel Dupas | 537aa1a | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 2890 | // Do not emit diag when the string param is a macro expansion and the |
| 2891 | // format is either NSString or CFString. This is a hack to prevent |
| 2892 | // diag when using the NSLocalizedString and CFCopyLocalizedString macros |
| 2893 | // 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] | 2894 | if (Type == FST_NSString && |
| 2895 | SourceMgr.isInSystemMacro(Args[format_idx]->getLocStart())) |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2896 | return false; |
Jean-Daniel Dupas | 537aa1a | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 2897 | |
Chris Lattner | cc5d1c2 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 2898 | // If there are no arguments specified, warn with -Wformat-security, otherwise |
| 2899 | // warn only with -Wformat-nonliteral. |
Eli Friedman | 0e5d677 | 2013-06-18 18:10:01 +0000 | [diff] [blame] | 2900 | if (Args.size() == firstDataArg) |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2901 | Diag(Args[format_idx]->getLocStart(), |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2902 | diag::warn_format_nonliteral_noargs) |
Chris Lattner | cc5d1c2 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 2903 | << OrigFormatExpr->getSourceRange(); |
| 2904 | else |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2905 | Diag(Args[format_idx]->getLocStart(), |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2906 | diag::warn_format_nonliteral) |
Chris Lattner | cc5d1c2 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 2907 | << OrigFormatExpr->getSourceRange(); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2908 | return false; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 2909 | } |
Ted Kremenek | e68f1aa | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 2910 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2911 | namespace { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2912 | class CheckFormatHandler : public analyze_format_string::FormatStringHandler { |
| 2913 | protected: |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2914 | Sema &S; |
| 2915 | const StringLiteral *FExpr; |
| 2916 | const Expr *OrigFormatExpr; |
Ted Kremenek | 4d745dd | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 2917 | const unsigned FirstDataArg; |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2918 | const unsigned NumDataArgs; |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2919 | const char *Beg; // Start of format string. |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2920 | const bool HasVAListArg; |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2921 | ArrayRef<const Expr *> Args; |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2922 | unsigned FormatIdx; |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2923 | llvm::SmallBitVector CoveredArgs; |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2924 | bool usesPositionalArgs; |
| 2925 | bool atFirstArg; |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2926 | bool inFunctionCall; |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2927 | Sema::VariadicCallType CallType; |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2928 | llvm::SmallBitVector &CheckedVarArgs; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2929 | public: |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2930 | CheckFormatHandler(Sema &s, const StringLiteral *fexpr, |
Ted Kremenek | 4d745dd | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 2931 | const Expr *origFormatExpr, unsigned firstDataArg, |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2932 | unsigned numDataArgs, const char *beg, bool hasVAListArg, |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2933 | ArrayRef<const Expr *> Args, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2934 | unsigned formatIdx, bool inFunctionCall, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2935 | Sema::VariadicCallType callType, |
| 2936 | llvm::SmallBitVector &CheckedVarArgs) |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2937 | : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2938 | FirstDataArg(firstDataArg), NumDataArgs(numDataArgs), |
| 2939 | Beg(beg), HasVAListArg(hasVAListArg), |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 2940 | Args(Args), FormatIdx(formatIdx), |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2941 | usesPositionalArgs(false), atFirstArg(true), |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 2942 | inFunctionCall(inFunctionCall), CallType(callType), |
| 2943 | CheckedVarArgs(CheckedVarArgs) { |
| 2944 | CoveredArgs.resize(numDataArgs); |
| 2945 | CoveredArgs.reset(); |
| 2946 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2947 | |
Ted Kremenek | 019d224 | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 2948 | void DoneProcessing(); |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2949 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2950 | void HandleIncompleteSpecifier(const char *startSpecifier, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2951 | unsigned specifierLen) override; |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2952 | |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2953 | void HandleInvalidLengthModifier( |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2954 | const analyze_format_string::FormatSpecifier &FS, |
| 2955 | const analyze_format_string::ConversionSpecifier &CS, |
| 2956 | const char *startSpecifier, unsigned specifierLen, |
| 2957 | unsigned DiagID); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2958 | |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2959 | void HandleNonStandardLengthModifier( |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2960 | const analyze_format_string::FormatSpecifier &FS, |
| 2961 | const char *startSpecifier, unsigned specifierLen); |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2962 | |
| 2963 | void HandleNonStandardConversionSpecifier( |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2964 | const analyze_format_string::ConversionSpecifier &CS, |
| 2965 | const char *startSpecifier, unsigned specifierLen); |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2966 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2967 | void HandlePosition(const char *startPos, unsigned posLen) override; |
Hans Wennborg | aa8c61c | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 2968 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2969 | void HandleInvalidPosition(const char *startSpecifier, |
| 2970 | unsigned specifierLen, |
| 2971 | analyze_format_string::PositionContext p) override; |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2972 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2973 | void HandleZeroPosition(const char *startPos, unsigned posLen) override; |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2974 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2975 | void HandleNullChar(const char *nullCharacter) override; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2976 | |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2977 | template <typename Range> |
| 2978 | static void EmitFormatDiagnostic(Sema &S, bool inFunctionCall, |
| 2979 | const Expr *ArgumentExpr, |
| 2980 | PartialDiagnostic PDiag, |
| 2981 | SourceLocation StringLoc, |
| 2982 | bool IsStringLocation, Range StringRange, |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 2983 | ArrayRef<FixItHint> Fixit = None); |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2984 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2985 | protected: |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2986 | bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, |
| 2987 | const char *startSpec, |
| 2988 | unsigned specifierLen, |
| 2989 | const char *csStart, unsigned csLen); |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2990 | |
| 2991 | void HandlePositionalNonpositionalArgs(SourceLocation Loc, |
| 2992 | const char *startSpec, |
| 2993 | unsigned specifierLen); |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2994 | |
Ted Kremenek | 8d9842d | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2995 | SourceRange getFormatStringRange(); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2996 | CharSourceRange getSpecifierRange(const char *startSpecifier, |
| 2997 | unsigned specifierLen); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2998 | SourceLocation getLocationOfByte(const char *x); |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2999 | |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 3000 | const Expr *getDataArg(unsigned i) const; |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 3001 | |
| 3002 | bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, |
| 3003 | const analyze_format_string::ConversionSpecifier &CS, |
| 3004 | const char *startSpecifier, unsigned specifierLen, |
| 3005 | unsigned argIndex); |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3006 | |
| 3007 | template <typename Range> |
| 3008 | void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc, |
| 3009 | bool IsStringLocation, Range StringRange, |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 3010 | ArrayRef<FixItHint> Fixit = None); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3011 | }; |
| 3012 | } |
| 3013 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3014 | SourceRange CheckFormatHandler::getFormatStringRange() { |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3015 | return OrigFormatExpr->getSourceRange(); |
| 3016 | } |
| 3017 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3018 | CharSourceRange CheckFormatHandler:: |
| 3019 | getSpecifierRange(const char *startSpecifier, unsigned specifierLen) { |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 3020 | SourceLocation Start = getLocationOfByte(startSpecifier); |
| 3021 | SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1); |
| 3022 | |
| 3023 | // Advance the end SourceLocation by one due to half-open ranges. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3024 | End = End.getLocWithOffset(1); |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 3025 | |
| 3026 | return CharSourceRange::getCharRange(Start, End); |
Ted Kremenek | 8d9842d | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3029 | SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3030 | return S.getLocationOfStringLiteralByte(FExpr, x - Beg); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3033 | void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, |
| 3034 | unsigned specifierLen){ |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3035 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier), |
| 3036 | getLocationOfByte(startSpecifier), |
| 3037 | /*IsStringLocation*/true, |
| 3038 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | c22f78d | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3041 | void CheckFormatHandler::HandleInvalidLengthModifier( |
| 3042 | const analyze_format_string::FormatSpecifier &FS, |
| 3043 | const analyze_format_string::ConversionSpecifier &CS, |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3044 | const char *startSpecifier, unsigned specifierLen, unsigned DiagID) { |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3045 | using namespace analyze_format_string; |
| 3046 | |
| 3047 | const LengthModifier &LM = FS.getLengthModifier(); |
| 3048 | CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); |
| 3049 | |
| 3050 | // See if we know how to fix this length modifier. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3051 | Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3052 | if (FixedLM) { |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3053 | EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3054 | getLocationOfByte(LM.getStart()), |
| 3055 | /*IsStringLocation*/true, |
| 3056 | getSpecifierRange(startSpecifier, specifierLen)); |
| 3057 | |
| 3058 | S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) |
| 3059 | << FixedLM->toString() |
| 3060 | << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); |
| 3061 | |
| 3062 | } else { |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3063 | FixItHint Hint; |
| 3064 | if (DiagID == diag::warn_format_nonsensical_length) |
| 3065 | Hint = FixItHint::CreateRemoval(LMRange); |
| 3066 | |
| 3067 | EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3068 | getLocationOfByte(LM.getStart()), |
| 3069 | /*IsStringLocation*/true, |
| 3070 | getSpecifierRange(startSpecifier, specifierLen), |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3071 | Hint); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3072 | } |
| 3073 | } |
| 3074 | |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 3075 | void CheckFormatHandler::HandleNonStandardLengthModifier( |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3076 | const analyze_format_string::FormatSpecifier &FS, |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 3077 | const char *startSpecifier, unsigned specifierLen) { |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3078 | using namespace analyze_format_string; |
| 3079 | |
| 3080 | const LengthModifier &LM = FS.getLengthModifier(); |
| 3081 | CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); |
| 3082 | |
| 3083 | // See if we know how to fix this length modifier. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3084 | Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3085 | if (FixedLM) { |
| 3086 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
| 3087 | << LM.toString() << 0, |
| 3088 | getLocationOfByte(LM.getStart()), |
| 3089 | /*IsStringLocation*/true, |
| 3090 | getSpecifierRange(startSpecifier, specifierLen)); |
| 3091 | |
| 3092 | S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) |
| 3093 | << FixedLM->toString() |
| 3094 | << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); |
| 3095 | |
| 3096 | } else { |
| 3097 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
| 3098 | << LM.toString() << 0, |
| 3099 | getLocationOfByte(LM.getStart()), |
| 3100 | /*IsStringLocation*/true, |
| 3101 | getSpecifierRange(startSpecifier, specifierLen)); |
| 3102 | } |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 3103 | } |
| 3104 | |
| 3105 | void CheckFormatHandler::HandleNonStandardConversionSpecifier( |
| 3106 | const analyze_format_string::ConversionSpecifier &CS, |
| 3107 | const char *startSpecifier, unsigned specifierLen) { |
Jordan Rose | 4c266aa | 2012-09-13 02:11:15 +0000 | [diff] [blame] | 3108 | using namespace analyze_format_string; |
| 3109 | |
| 3110 | // See if we know how to fix this conversion specifier. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3111 | Optional<ConversionSpecifier> FixedCS = CS.getStandardSpecifier(); |
Jordan Rose | 4c266aa | 2012-09-13 02:11:15 +0000 | [diff] [blame] | 3112 | if (FixedCS) { |
| 3113 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
| 3114 | << CS.toString() << /*conversion specifier*/1, |
| 3115 | getLocationOfByte(CS.getStart()), |
| 3116 | /*IsStringLocation*/true, |
| 3117 | getSpecifierRange(startSpecifier, specifierLen)); |
| 3118 | |
| 3119 | CharSourceRange CSRange = getSpecifierRange(CS.getStart(), CS.getLength()); |
| 3120 | S.Diag(getLocationOfByte(CS.getStart()), diag::note_format_fix_specifier) |
| 3121 | << FixedCS->toString() |
| 3122 | << FixItHint::CreateReplacement(CSRange, FixedCS->toString()); |
| 3123 | } else { |
| 3124 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
| 3125 | << CS.toString() << /*conversion specifier*/1, |
| 3126 | getLocationOfByte(CS.getStart()), |
| 3127 | /*IsStringLocation*/true, |
| 3128 | getSpecifierRange(startSpecifier, specifierLen)); |
| 3129 | } |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 3130 | } |
| 3131 | |
Hans Wennborg | aa8c61c | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 3132 | void CheckFormatHandler::HandlePosition(const char *startPos, |
| 3133 | unsigned posLen) { |
| 3134 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg), |
| 3135 | getLocationOfByte(startPos), |
| 3136 | /*IsStringLocation*/true, |
| 3137 | getSpecifierRange(startPos, posLen)); |
| 3138 | } |
| 3139 | |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 3140 | void |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3141 | CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen, |
| 3142 | analyze_format_string::PositionContext p) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3143 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier) |
| 3144 | << (unsigned) p, |
| 3145 | getLocationOfByte(startPos), /*IsStringLocation*/true, |
| 3146 | getSpecifierRange(startPos, posLen)); |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3149 | void CheckFormatHandler::HandleZeroPosition(const char *startPos, |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 3150 | unsigned posLen) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3151 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier), |
| 3152 | getLocationOfByte(startPos), |
| 3153 | /*IsStringLocation*/true, |
| 3154 | getSpecifierRange(startPos, posLen)); |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3157 | void CheckFormatHandler::HandleNullChar(const char *nullCharacter) { |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 3158 | if (!isa<ObjCStringLiteral>(OrigFormatExpr)) { |
Ted Kremenek | 0d5b9ef | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 3159 | // The presence of a null character is likely an error. |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3160 | EmitFormatDiagnostic( |
| 3161 | S.PDiag(diag::warn_printf_format_string_contains_null_char), |
| 3162 | getLocationOfByte(nullCharacter), /*IsStringLocation*/true, |
| 3163 | getFormatStringRange()); |
Ted Kremenek | 0d5b9ef | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 3164 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3165 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3166 | |
Jordan Rose | 58bbe42 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 3167 | // Note that this may return NULL if there was an error parsing or building |
| 3168 | // one of the argument expressions. |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3169 | const Expr *CheckFormatHandler::getDataArg(unsigned i) const { |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3170 | return Args[FirstDataArg + i]; |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3171 | } |
| 3172 | |
| 3173 | void CheckFormatHandler::DoneProcessing() { |
| 3174 | // Does the number of data arguments exceed the number of |
| 3175 | // format conversions in the format string? |
| 3176 | if (!HasVAListArg) { |
| 3177 | // Find any arguments that weren't covered. |
| 3178 | CoveredArgs.flip(); |
| 3179 | signed notCoveredArg = CoveredArgs.find_first(); |
| 3180 | if (notCoveredArg >= 0) { |
| 3181 | assert((unsigned)notCoveredArg < NumDataArgs); |
Jordan Rose | 58bbe42 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 3182 | if (const Expr *E = getDataArg((unsigned) notCoveredArg)) { |
| 3183 | SourceLocation Loc = E->getLocStart(); |
| 3184 | if (!S.getSourceManager().isInSystemMacro(Loc)) { |
| 3185 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_data_arg_not_used), |
| 3186 | Loc, /*IsStringLocation*/false, |
| 3187 | getFormatStringRange()); |
| 3188 | } |
Bob Wilson | 23cd434 | 2012-05-03 19:47:19 +0000 | [diff] [blame] | 3189 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3190 | } |
| 3191 | } |
| 3192 | } |
| 3193 | |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 3194 | bool |
| 3195 | CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, |
| 3196 | SourceLocation Loc, |
| 3197 | const char *startSpec, |
| 3198 | unsigned specifierLen, |
| 3199 | const char *csStart, |
| 3200 | unsigned csLen) { |
| 3201 | |
| 3202 | bool keepGoing = true; |
| 3203 | if (argIndex < NumDataArgs) { |
| 3204 | // Consider the argument coverered, even though the specifier doesn't |
| 3205 | // make sense. |
| 3206 | CoveredArgs.set(argIndex); |
| 3207 | } |
| 3208 | else { |
| 3209 | // If argIndex exceeds the number of data arguments we |
| 3210 | // don't issue a warning because that is just a cascade of warnings (and |
| 3211 | // they may have intended '%%' anyway). We don't want to continue processing |
| 3212 | // the format string after this point, however, as we will like just get |
| 3213 | // gibberish when trying to match arguments. |
| 3214 | keepGoing = false; |
| 3215 | } |
| 3216 | |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3217 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_conversion) |
| 3218 | << StringRef(csStart, csLen), |
| 3219 | Loc, /*IsStringLocation*/true, |
| 3220 | getSpecifierRange(startSpec, specifierLen)); |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 3221 | |
| 3222 | return keepGoing; |
| 3223 | } |
| 3224 | |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3225 | void |
| 3226 | CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc, |
| 3227 | const char *startSpec, |
| 3228 | unsigned specifierLen) { |
| 3229 | EmitFormatDiagnostic( |
| 3230 | S.PDiag(diag::warn_format_mix_positional_nonpositional_args), |
| 3231 | Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen)); |
| 3232 | } |
| 3233 | |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 3234 | bool |
| 3235 | CheckFormatHandler::CheckNumArgs( |
| 3236 | const analyze_format_string::FormatSpecifier &FS, |
| 3237 | const analyze_format_string::ConversionSpecifier &CS, |
| 3238 | const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { |
| 3239 | |
| 3240 | if (argIndex >= NumDataArgs) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3241 | PartialDiagnostic PDiag = FS.usesPositionalArg() |
| 3242 | ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args) |
| 3243 | << (argIndex+1) << NumDataArgs) |
| 3244 | : S.PDiag(diag::warn_printf_insufficient_data_args); |
| 3245 | EmitFormatDiagnostic( |
| 3246 | PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true, |
| 3247 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 3248 | return false; |
| 3249 | } |
| 3250 | return true; |
| 3251 | } |
| 3252 | |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3253 | template<typename Range> |
| 3254 | void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag, |
| 3255 | SourceLocation Loc, |
| 3256 | bool IsStringLocation, |
| 3257 | Range StringRange, |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3258 | ArrayRef<FixItHint> FixIt) { |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3259 | EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag, |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3260 | Loc, IsStringLocation, StringRange, FixIt); |
| 3261 | } |
| 3262 | |
| 3263 | /// \brief If the format string is not within the funcion call, emit a note |
| 3264 | /// so that the function call and string are in diagnostic messages. |
| 3265 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 3266 | /// \param InFunctionCall if true, the format string is within the function |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3267 | /// call and only one diagnostic message will be produced. Otherwise, an |
| 3268 | /// extra note will be emitted pointing to location of the format string. |
| 3269 | /// |
| 3270 | /// \param ArgumentExpr the expression that is passed as the format string |
| 3271 | /// argument in the function call. Used for getting locations when two |
| 3272 | /// diagnostics are emitted. |
| 3273 | /// |
| 3274 | /// \param PDiag the callee should already have provided any strings for the |
| 3275 | /// diagnostic message. This function only adds locations and fixits |
| 3276 | /// to diagnostics. |
| 3277 | /// |
| 3278 | /// \param Loc primary location for diagnostic. If two diagnostics are |
| 3279 | /// required, one will be at Loc and a new SourceLocation will be created for |
| 3280 | /// the other one. |
| 3281 | /// |
| 3282 | /// \param IsStringLocation if true, Loc points to the format string should be |
| 3283 | /// used for the note. Otherwise, Loc points to the argument list and will |
| 3284 | /// be used with PDiag. |
| 3285 | /// |
| 3286 | /// \param StringRange some or all of the string to highlight. This is |
| 3287 | /// templated so it can accept either a CharSourceRange or a SourceRange. |
| 3288 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 3289 | /// \param FixIt optional fix it hint for the format string. |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3290 | template<typename Range> |
| 3291 | void CheckFormatHandler::EmitFormatDiagnostic(Sema &S, bool InFunctionCall, |
| 3292 | const Expr *ArgumentExpr, |
| 3293 | PartialDiagnostic PDiag, |
| 3294 | SourceLocation Loc, |
| 3295 | bool IsStringLocation, |
| 3296 | Range StringRange, |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3297 | ArrayRef<FixItHint> FixIt) { |
| 3298 | if (InFunctionCall) { |
| 3299 | const Sema::SemaDiagnosticBuilder &D = S.Diag(Loc, PDiag); |
| 3300 | D << StringRange; |
Alexander Kornienko | a9b01eb | 2015-02-25 14:40:56 +0000 | [diff] [blame] | 3301 | D << FixIt; |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3302 | } else { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3303 | S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag) |
| 3304 | << ArgumentExpr->getSourceRange(); |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3305 | |
| 3306 | const Sema::SemaDiagnosticBuilder &Note = |
| 3307 | S.Diag(IsStringLocation ? Loc : StringRange.getBegin(), |
| 3308 | diag::note_format_string_defined); |
| 3309 | |
| 3310 | Note << StringRange; |
Alexander Kornienko | a9b01eb | 2015-02-25 14:40:56 +0000 | [diff] [blame] | 3311 | Note << FixIt; |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3312 | } |
| 3313 | } |
| 3314 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3315 | //===--- CHECK: Printf format string checking ------------------------------===// |
| 3316 | |
| 3317 | namespace { |
| 3318 | class CheckPrintfHandler : public CheckFormatHandler { |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 3319 | bool ObjCContext; |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3320 | public: |
| 3321 | CheckPrintfHandler(Sema &s, const StringLiteral *fexpr, |
| 3322 | const Expr *origFormatExpr, unsigned firstDataArg, |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 3323 | unsigned numDataArgs, bool isObjC, |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3324 | const char *beg, bool hasVAListArg, |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 3325 | ArrayRef<const Expr *> Args, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3326 | unsigned formatIdx, bool inFunctionCall, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3327 | Sema::VariadicCallType CallType, |
| 3328 | llvm::SmallBitVector &CheckedVarArgs) |
| 3329 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
| 3330 | numDataArgs, beg, hasVAListArg, Args, |
| 3331 | formatIdx, inFunctionCall, CallType, CheckedVarArgs), |
| 3332 | ObjCContext(isObjC) |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3333 | {} |
| 3334 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3335 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3336 | bool HandleInvalidPrintfConversionSpecifier( |
| 3337 | const analyze_printf::PrintfSpecifier &FS, |
| 3338 | const char *startSpecifier, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3339 | unsigned specifierLen) override; |
| 3340 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3341 | bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, |
| 3342 | const char *startSpecifier, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3343 | unsigned specifierLen) override; |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3344 | bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, |
| 3345 | const char *StartSpecifier, |
| 3346 | unsigned SpecifierLen, |
| 3347 | const Expr *E); |
| 3348 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3349 | bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, |
| 3350 | const char *startSpecifier, unsigned specifierLen); |
| 3351 | void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, |
| 3352 | const analyze_printf::OptionalAmount &Amt, |
| 3353 | unsigned type, |
| 3354 | const char *startSpecifier, unsigned specifierLen); |
| 3355 | void HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
| 3356 | const analyze_printf::OptionalFlag &flag, |
| 3357 | const char *startSpecifier, unsigned specifierLen); |
| 3358 | void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, |
| 3359 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 3360 | const analyze_printf::OptionalFlag &flag, |
| 3361 | const char *startSpecifier, unsigned specifierLen); |
Hans Wennborg | c3b3da0 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 3362 | bool checkForCStrMembers(const analyze_printf::ArgType &AT, |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 3363 | const Expr *E); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3364 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3365 | }; |
| 3366 | } |
| 3367 | |
| 3368 | bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( |
| 3369 | const analyze_printf::PrintfSpecifier &FS, |
| 3370 | const char *startSpecifier, |
| 3371 | unsigned specifierLen) { |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 3372 | const analyze_printf::PrintfConversionSpecifier &CS = |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 3373 | FS.getConversionSpecifier(); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3374 | |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 3375 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 3376 | getLocationOfByte(CS.getStart()), |
| 3377 | startSpecifier, specifierLen, |
| 3378 | CS.getStart(), CS.getLength()); |
Ted Kremenek | 94af575 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 3379 | } |
| 3380 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3381 | bool CheckPrintfHandler::HandleAmount( |
| 3382 | const analyze_format_string::OptionalAmount &Amt, |
| 3383 | unsigned k, const char *startSpecifier, |
| 3384 | unsigned specifierLen) { |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 3385 | |
| 3386 | if (Amt.hasDataArgument()) { |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 3387 | if (!HasVAListArg) { |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 3388 | unsigned argIndex = Amt.getArgIndex(); |
| 3389 | if (argIndex >= NumDataArgs) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3390 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg) |
| 3391 | << k, |
| 3392 | getLocationOfByte(Amt.getStart()), |
| 3393 | /*IsStringLocation*/true, |
| 3394 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 3395 | // Don't do any more checking. We will just emit |
| 3396 | // spurious errors. |
| 3397 | return false; |
| 3398 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3399 | |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 3400 | // Type check the data argument. It should be an 'int'. |
Ted Kremenek | 605b011 | 2010-01-29 23:32:22 +0000 | [diff] [blame] | 3401 | // Although not in conformance with C99, we also allow the argument to be |
| 3402 | // an 'unsigned int' as that is a reasonably safe case. GCC also |
| 3403 | // doesn't emit a warning for that case. |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 3404 | CoveredArgs.set(argIndex); |
| 3405 | const Expr *Arg = getDataArg(argIndex); |
Jordan Rose | 58bbe42 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 3406 | if (!Arg) |
| 3407 | return false; |
| 3408 | |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 3409 | QualType T = Arg->getType(); |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3410 | |
Hans Wennborg | c3b3da0 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 3411 | const analyze_printf::ArgType &AT = Amt.getArgType(S.Context); |
| 3412 | assert(AT.isValid()); |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3413 | |
Hans Wennborg | c3b3da0 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 3414 | if (!AT.matchesType(S.Context, T)) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3415 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type) |
Hans Wennborg | c3b3da0 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 3416 | << k << AT.getRepresentativeTypeName(S.Context) |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3417 | << T << Arg->getSourceRange(), |
| 3418 | getLocationOfByte(Amt.getStart()), |
| 3419 | /*IsStringLocation*/true, |
| 3420 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 3421 | // Don't do any more checking. We will just emit |
| 3422 | // spurious errors. |
| 3423 | return false; |
| 3424 | } |
| 3425 | } |
| 3426 | } |
| 3427 | return true; |
| 3428 | } |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 3429 | |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 3430 | void CheckPrintfHandler::HandleInvalidAmount( |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3431 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 3432 | const analyze_printf::OptionalAmount &Amt, |
| 3433 | unsigned type, |
| 3434 | const char *startSpecifier, |
| 3435 | unsigned specifierLen) { |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 3436 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 3437 | FS.getConversionSpecifier(); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 3438 | |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3439 | FixItHint fixit = |
| 3440 | Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant |
| 3441 | ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), |
| 3442 | Amt.getConstantLength())) |
| 3443 | : FixItHint(); |
| 3444 | |
| 3445 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount) |
| 3446 | << type << CS.toString(), |
| 3447 | getLocationOfByte(Amt.getStart()), |
| 3448 | /*IsStringLocation*/true, |
| 3449 | getSpecifierRange(startSpecifier, specifierLen), |
| 3450 | fixit); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 3451 | } |
| 3452 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3453 | void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 3454 | const analyze_printf::OptionalFlag &flag, |
| 3455 | const char *startSpecifier, |
| 3456 | unsigned specifierLen) { |
| 3457 | // Warn about pointless flag with a fixit removal. |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 3458 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 3459 | FS.getConversionSpecifier(); |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3460 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag) |
| 3461 | << flag.toString() << CS.toString(), |
| 3462 | getLocationOfByte(flag.getPosition()), |
| 3463 | /*IsStringLocation*/true, |
| 3464 | getSpecifierRange(startSpecifier, specifierLen), |
| 3465 | FixItHint::CreateRemoval( |
| 3466 | getSpecifierRange(flag.getPosition(), 1))); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
| 3469 | void CheckPrintfHandler::HandleIgnoredFlag( |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3470 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 3471 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 3472 | const analyze_printf::OptionalFlag &flag, |
| 3473 | const char *startSpecifier, |
| 3474 | unsigned specifierLen) { |
| 3475 | // Warn about ignored flag with a fixit removal. |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3476 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag) |
| 3477 | << ignoredFlag.toString() << flag.toString(), |
| 3478 | getLocationOfByte(ignoredFlag.getPosition()), |
| 3479 | /*IsStringLocation*/true, |
| 3480 | getSpecifierRange(startSpecifier, specifierLen), |
| 3481 | FixItHint::CreateRemoval( |
| 3482 | getSpecifierRange(ignoredFlag.getPosition(), 1))); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 3483 | } |
| 3484 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3485 | // Determines if the specified is a C++ class or struct containing |
| 3486 | // a member with the specified name and kind (e.g. a CXXMethodDecl named |
| 3487 | // "c_str()"). |
| 3488 | template<typename MemberKind> |
| 3489 | static llvm::SmallPtrSet<MemberKind*, 1> |
| 3490 | CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) { |
| 3491 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3492 | llvm::SmallPtrSet<MemberKind*, 1> Results; |
| 3493 | |
| 3494 | if (!RT) |
| 3495 | return Results; |
| 3496 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 3497 | if (!RD || !RD->getDefinition()) |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3498 | return Results; |
| 3499 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 3500 | LookupResult R(S, &S.Context.Idents.get(Name), SourceLocation(), |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3501 | Sema::LookupMemberName); |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 3502 | R.suppressDiagnostics(); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3503 | |
| 3504 | // We just need to include all members of the right kind turned up by the |
| 3505 | // filter, at this point. |
| 3506 | if (S.LookupQualifiedName(R, RT->getDecl())) |
| 3507 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 3508 | NamedDecl *decl = (*I)->getUnderlyingDecl(); |
| 3509 | if (MemberKind *FK = dyn_cast<MemberKind>(decl)) |
| 3510 | Results.insert(FK); |
| 3511 | } |
| 3512 | return Results; |
| 3513 | } |
| 3514 | |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 3515 | /// Check if we could call '.c_str()' on an object. |
| 3516 | /// |
| 3517 | /// FIXME: This returns the wrong results in some cases (if cv-qualifiers don't |
| 3518 | /// allow the call, or if it would be ambiguous). |
| 3519 | bool Sema::hasCStrMethod(const Expr *E) { |
| 3520 | typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet; |
| 3521 | MethodSet Results = |
| 3522 | CXXRecordMembersNamed<CXXMethodDecl>("c_str", *this, E->getType()); |
| 3523 | for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); |
| 3524 | MI != ME; ++MI) |
| 3525 | if ((*MI)->getMinRequiredArguments() == 0) |
| 3526 | return true; |
| 3527 | return false; |
| 3528 | } |
| 3529 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3530 | // 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] | 3531 | // better diagnostic if so. AT is assumed to be valid. |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3532 | // Returns true when a c_str() conversion method is found. |
| 3533 | bool CheckPrintfHandler::checkForCStrMembers( |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 3534 | const analyze_printf::ArgType &AT, const Expr *E) { |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3535 | typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet; |
| 3536 | |
| 3537 | MethodSet Results = |
| 3538 | CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType()); |
| 3539 | |
| 3540 | for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); |
| 3541 | MI != ME; ++MI) { |
| 3542 | const CXXMethodDecl *Method = *MI; |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 3543 | if (Method->getMinRequiredArguments() == 0 && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3544 | AT.matchesType(S.Context, Method->getReturnType())) { |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3545 | // FIXME: Suggest parens if the expression needs them. |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 3546 | SourceLocation EndLoc = S.getLocForEndOfToken(E->getLocEnd()); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3547 | S.Diag(E->getLocStart(), diag::note_printf_c_str) |
| 3548 | << "c_str()" |
| 3549 | << FixItHint::CreateInsertion(EndLoc, ".c_str()"); |
| 3550 | return true; |
| 3551 | } |
| 3552 | } |
| 3553 | |
| 3554 | return false; |
| 3555 | } |
| 3556 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3557 | bool |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3558 | CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier |
Ted Kremenek | d31b263 | 2010-02-11 09:27:41 +0000 | [diff] [blame] | 3559 | &FS, |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3560 | const char *startSpecifier, |
| 3561 | unsigned specifierLen) { |
| 3562 | |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 3563 | using namespace analyze_format_string; |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 3564 | using namespace analyze_printf; |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 3565 | const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3566 | |
Ted Kremenek | 6cd6942 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 3567 | if (FS.consumesDataArgument()) { |
| 3568 | if (atFirstArg) { |
| 3569 | atFirstArg = false; |
| 3570 | usesPositionalArgs = FS.usesPositionalArg(); |
| 3571 | } |
| 3572 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3573 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), |
| 3574 | startSpecifier, specifierLen); |
Ted Kremenek | 6cd6942 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 3575 | return false; |
| 3576 | } |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 3577 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3578 | |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 3579 | // First check if the field width, precision, and conversion specifier |
| 3580 | // have matching data arguments. |
| 3581 | if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, |
| 3582 | startSpecifier, specifierLen)) { |
| 3583 | return false; |
| 3584 | } |
| 3585 | |
| 3586 | if (!HandleAmount(FS.getPrecision(), /* precision */ 1, |
| 3587 | startSpecifier, specifierLen)) { |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 3588 | return false; |
| 3589 | } |
| 3590 | |
Ted Kremenek | 8d9842d | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 3591 | if (!CS.consumesDataArgument()) { |
| 3592 | // FIXME: Technically specifying a precision or field width here |
| 3593 | // makes no sense. Worth issuing a warning at some point. |
Ted Kremenek | fb45d35 | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 3594 | return true; |
Ted Kremenek | 8d9842d | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 3595 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3596 | |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 3597 | // Consume the argument. |
| 3598 | unsigned argIndex = FS.getArgIndex(); |
Ted Kremenek | 09597b4 | 2010-02-27 08:34:51 +0000 | [diff] [blame] | 3599 | if (argIndex < NumDataArgs) { |
| 3600 | // The check to see if the argIndex is valid will come later. |
| 3601 | // We set the bit here because we may exit early from this |
| 3602 | // function if we encounter some other error. |
| 3603 | CoveredArgs.set(argIndex); |
| 3604 | } |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 3605 | |
Dimitry Andric | 6b5ed34 | 2015-02-19 22:32:33 +0000 | [diff] [blame] | 3606 | // FreeBSD kernel extensions. |
| 3607 | if (CS.getKind() == ConversionSpecifier::FreeBSDbArg || |
| 3608 | CS.getKind() == ConversionSpecifier::FreeBSDDArg) { |
| 3609 | // We need at least two arguments. |
| 3610 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex + 1)) |
| 3611 | return false; |
| 3612 | |
| 3613 | // Claim the second argument. |
| 3614 | CoveredArgs.set(argIndex + 1); |
| 3615 | |
| 3616 | // Type check the first argument (int for %b, pointer for %D) |
| 3617 | const Expr *Ex = getDataArg(argIndex); |
| 3618 | const analyze_printf::ArgType &AT = |
| 3619 | (CS.getKind() == ConversionSpecifier::FreeBSDbArg) ? |
| 3620 | ArgType(S.Context.IntTy) : ArgType::CPointerTy; |
| 3621 | if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType())) |
| 3622 | EmitFormatDiagnostic( |
| 3623 | S.PDiag(diag::warn_format_conversion_argument_type_mismatch) |
| 3624 | << AT.getRepresentativeTypeName(S.Context) << Ex->getType() |
| 3625 | << false << Ex->getSourceRange(), |
| 3626 | Ex->getLocStart(), /*IsStringLocation*/false, |
| 3627 | getSpecifierRange(startSpecifier, specifierLen)); |
| 3628 | |
| 3629 | // Type check the second argument (char * for both %b and %D) |
| 3630 | Ex = getDataArg(argIndex + 1); |
| 3631 | const analyze_printf::ArgType &AT2 = ArgType::CStrTy; |
| 3632 | if (AT2.isValid() && !AT2.matchesType(S.Context, Ex->getType())) |
| 3633 | EmitFormatDiagnostic( |
| 3634 | S.PDiag(diag::warn_format_conversion_argument_type_mismatch) |
| 3635 | << AT2.getRepresentativeTypeName(S.Context) << Ex->getType() |
| 3636 | << false << Ex->getSourceRange(), |
| 3637 | Ex->getLocStart(), /*IsStringLocation*/false, |
| 3638 | getSpecifierRange(startSpecifier, specifierLen)); |
| 3639 | |
| 3640 | return true; |
| 3641 | } |
| 3642 | |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 3643 | // Check for using an Objective-C specific conversion specifier |
| 3644 | // in a non-ObjC literal. |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 3645 | if (!ObjCContext && CS.isObjCArg()) { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3646 | return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, |
| 3647 | specifierLen); |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 3648 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3649 | |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 3650 | // Check for invalid use of field width |
| 3651 | if (!FS.hasValidFieldWidth()) { |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 3652 | HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 3653 | startSpecifier, specifierLen); |
| 3654 | } |
| 3655 | |
| 3656 | // Check for invalid use of precision |
| 3657 | if (!FS.hasValidPrecision()) { |
| 3658 | HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, |
| 3659 | startSpecifier, specifierLen); |
| 3660 | } |
| 3661 | |
| 3662 | // Check each flag does not conflict with any other component. |
Ted Kremenek | bf4832c | 2011-01-08 05:28:46 +0000 | [diff] [blame] | 3663 | if (!FS.hasValidThousandsGroupingPrefix()) |
| 3664 | HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 3665 | if (!FS.hasValidLeadingZeros()) |
| 3666 | HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); |
| 3667 | if (!FS.hasValidPlusPrefix()) |
| 3668 | HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 3669 | if (!FS.hasValidSpacePrefix()) |
| 3670 | HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 3671 | if (!FS.hasValidAlternativeForm()) |
| 3672 | HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); |
| 3673 | if (!FS.hasValidLeftJustified()) |
| 3674 | HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); |
| 3675 | |
| 3676 | // Check that flags are not ignored by another flag |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 3677 | if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' |
| 3678 | HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), |
| 3679 | startSpecifier, specifierLen); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 3680 | if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' |
| 3681 | HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), |
| 3682 | startSpecifier, specifierLen); |
| 3683 | |
| 3684 | // Check the length modifier is valid with the given conversion specifier. |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3685 | if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo())) |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3686 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
| 3687 | diag::warn_format_nonsensical_length); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3688 | else if (!FS.hasStandardLengthModifier()) |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3689 | HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3690 | else if (!FS.hasStandardLengthConversionCombination()) |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3691 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
| 3692 | diag::warn_format_non_standard_conversion_spec); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 3693 | |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3694 | if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) |
| 3695 | HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); |
| 3696 | |
Ted Kremenek | 9fcd830 | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 3697 | // The remaining checks depend on the data arguments. |
| 3698 | if (HasVAListArg) |
| 3699 | return true; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3700 | |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 3701 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | 9fcd830 | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 3702 | return false; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3703 | |
Jordan Rose | 58bbe42 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 3704 | const Expr *Arg = getDataArg(argIndex); |
| 3705 | if (!Arg) |
| 3706 | return true; |
| 3707 | |
| 3708 | return checkFormatExpr(FS, startSpecifier, specifierLen, Arg); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3711 | static bool requiresParensToAddCast(const Expr *E) { |
| 3712 | // FIXME: We should have a general way to reason about operator |
| 3713 | // precedence and whether parens are actually needed here. |
| 3714 | // Take care of a few common cases where they aren't. |
| 3715 | const Expr *Inside = E->IgnoreImpCasts(); |
| 3716 | if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Inside)) |
| 3717 | Inside = POE->getSyntacticForm()->IgnoreImpCasts(); |
| 3718 | |
| 3719 | switch (Inside->getStmtClass()) { |
| 3720 | case Stmt::ArraySubscriptExprClass: |
| 3721 | case Stmt::CallExprClass: |
Jordan Rose | ea0fdfe | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 3722 | case Stmt::CharacterLiteralClass: |
| 3723 | case Stmt::CXXBoolLiteralExprClass: |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3724 | case Stmt::DeclRefExprClass: |
Jordan Rose | ea0fdfe | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 3725 | case Stmt::FloatingLiteralClass: |
| 3726 | case Stmt::IntegerLiteralClass: |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3727 | case Stmt::MemberExprClass: |
Jordan Rose | ea0fdfe | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 3728 | case Stmt::ObjCArrayLiteralClass: |
| 3729 | case Stmt::ObjCBoolLiteralExprClass: |
| 3730 | case Stmt::ObjCBoxedExprClass: |
| 3731 | case Stmt::ObjCDictionaryLiteralClass: |
| 3732 | case Stmt::ObjCEncodeExprClass: |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3733 | case Stmt::ObjCIvarRefExprClass: |
| 3734 | case Stmt::ObjCMessageExprClass: |
| 3735 | case Stmt::ObjCPropertyRefExprClass: |
Jordan Rose | ea0fdfe | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 3736 | case Stmt::ObjCStringLiteralClass: |
| 3737 | case Stmt::ObjCSubscriptRefExprClass: |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3738 | case Stmt::ParenExprClass: |
Jordan Rose | ea0fdfe | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 3739 | case Stmt::StringLiteralClass: |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3740 | case Stmt::UnaryOperatorClass: |
| 3741 | return false; |
| 3742 | default: |
| 3743 | return true; |
| 3744 | } |
| 3745 | } |
| 3746 | |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 3747 | static std::pair<QualType, StringRef> |
| 3748 | shouldNotPrintDirectly(const ASTContext &Context, |
| 3749 | QualType IntendedTy, |
| 3750 | const Expr *E) { |
| 3751 | // Use a 'while' to peel off layers of typedefs. |
| 3752 | QualType TyTy = IntendedTy; |
| 3753 | while (const TypedefType *UserTy = TyTy->getAs<TypedefType>()) { |
| 3754 | StringRef Name = UserTy->getDecl()->getName(); |
| 3755 | QualType CastTy = llvm::StringSwitch<QualType>(Name) |
| 3756 | .Case("NSInteger", Context.LongTy) |
| 3757 | .Case("NSUInteger", Context.UnsignedLongTy) |
| 3758 | .Case("SInt32", Context.IntTy) |
| 3759 | .Case("UInt32", Context.UnsignedIntTy) |
| 3760 | .Default(QualType()); |
| 3761 | |
| 3762 | if (!CastTy.isNull()) |
| 3763 | return std::make_pair(CastTy, Name); |
| 3764 | |
| 3765 | TyTy = UserTy->desugar(); |
| 3766 | } |
| 3767 | |
| 3768 | // Strip parens if necessary. |
| 3769 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
| 3770 | return shouldNotPrintDirectly(Context, |
| 3771 | PE->getSubExpr()->getType(), |
| 3772 | PE->getSubExpr()); |
| 3773 | |
| 3774 | // If this is a conditional expression, then its result type is constructed |
| 3775 | // via usual arithmetic conversions and thus there might be no necessary |
| 3776 | // typedef sugar there. Recurse to operands to check for NSInteger & |
| 3777 | // Co. usage condition. |
| 3778 | if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 3779 | QualType TrueTy, FalseTy; |
| 3780 | StringRef TrueName, FalseName; |
| 3781 | |
| 3782 | std::tie(TrueTy, TrueName) = |
| 3783 | shouldNotPrintDirectly(Context, |
| 3784 | CO->getTrueExpr()->getType(), |
| 3785 | CO->getTrueExpr()); |
| 3786 | std::tie(FalseTy, FalseName) = |
| 3787 | shouldNotPrintDirectly(Context, |
| 3788 | CO->getFalseExpr()->getType(), |
| 3789 | CO->getFalseExpr()); |
| 3790 | |
| 3791 | if (TrueTy == FalseTy) |
| 3792 | return std::make_pair(TrueTy, TrueName); |
| 3793 | else if (TrueTy.isNull()) |
| 3794 | return std::make_pair(FalseTy, FalseName); |
| 3795 | else if (FalseTy.isNull()) |
| 3796 | return std::make_pair(TrueTy, TrueName); |
| 3797 | } |
| 3798 | |
| 3799 | return std::make_pair(QualType(), StringRef()); |
| 3800 | } |
| 3801 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3802 | bool |
| 3803 | CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, |
| 3804 | const char *StartSpecifier, |
| 3805 | unsigned SpecifierLen, |
| 3806 | const Expr *E) { |
| 3807 | using namespace analyze_format_string; |
| 3808 | using namespace analyze_printf; |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 3809 | // Now type check the data expression that matches the |
| 3810 | // format specifier. |
Hans Wennborg | c3b3da0 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 3811 | const analyze_printf::ArgType &AT = FS.getArgType(S.Context, |
| 3812 | ObjCContext); |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3813 | if (!AT.isValid()) |
| 3814 | return true; |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3815 | |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 3816 | QualType ExprTy = E->getType(); |
Ted Kremenek | 3365e52 | 2013-04-10 06:26:26 +0000 | [diff] [blame] | 3817 | while (const TypeOfExprType *TET = dyn_cast<TypeOfExprType>(ExprTy)) { |
| 3818 | ExprTy = TET->getUnderlyingExpr()->getType(); |
| 3819 | } |
| 3820 | |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 3821 | analyze_printf::ArgType::MatchKind match = AT.matchesType(S.Context, ExprTy); |
| 3822 | |
| 3823 | if (match == analyze_printf::ArgType::Match) { |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3824 | return true; |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 3825 | } |
Jordan Rose | 9870998 | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 3826 | |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3827 | // Look through argument promotions for our error message's reported type. |
| 3828 | // This includes the integral and floating promotions, but excludes array |
| 3829 | // and function pointer decay; seeing that an argument intended to be a |
| 3830 | // string has type 'char [6]' is probably more confusing than 'char *'. |
| 3831 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
| 3832 | if (ICE->getCastKind() == CK_IntegralCast || |
| 3833 | ICE->getCastKind() == CK_FloatingCast) { |
| 3834 | E = ICE->getSubExpr(); |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 3835 | ExprTy = E->getType(); |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3836 | |
| 3837 | // Check if we didn't match because of an implicit cast from a 'char' |
| 3838 | // or 'short' to an 'int'. This is done because printf is a varargs |
| 3839 | // function. |
| 3840 | if (ICE->getType() == S.Context.IntTy || |
| 3841 | ICE->getType() == S.Context.UnsignedIntTy) { |
| 3842 | // All further checking is done on the subexpression. |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 3843 | if (AT.matchesType(S.Context, ExprTy)) |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3844 | return true; |
Ted Kremenek | 12a37de | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 3845 | } |
Jordan Rose | 9870998 | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 3846 | } |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 3847 | } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) { |
| 3848 | // Special case for 'a', which has type 'int' in C. |
| 3849 | // Note, however, that we do /not/ want to treat multibyte constants like |
| 3850 | // 'MooV' as characters! This form is deprecated but still exists. |
| 3851 | if (ExprTy == S.Context.IntTy) |
| 3852 | if (llvm::isUIntN(S.Context.getCharWidth(), CL->getValue())) |
| 3853 | ExprTy = S.Context.CharTy; |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3854 | } |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 3855 | |
Jordan Rose | bc53ed1 | 2014-05-31 04:12:14 +0000 | [diff] [blame] | 3856 | // Look through enums to their underlying type. |
| 3857 | bool IsEnum = false; |
| 3858 | if (auto EnumTy = ExprTy->getAs<EnumType>()) { |
| 3859 | ExprTy = EnumTy->getDecl()->getIntegerType(); |
| 3860 | IsEnum = true; |
| 3861 | } |
| 3862 | |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3863 | // %C in an Objective-C context prints a unichar, not a wchar_t. |
| 3864 | // If the argument is an integer of some kind, believe the %C and suggest |
| 3865 | // a cast instead of changing the conversion specifier. |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 3866 | QualType IntendedTy = ExprTy; |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3867 | if (ObjCContext && |
| 3868 | FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) { |
| 3869 | if (ExprTy->isIntegralOrUnscopedEnumerationType() && |
| 3870 | !ExprTy->isCharType()) { |
| 3871 | // 'unichar' is defined as a typedef of unsigned short, but we should |
| 3872 | // prefer using the typedef if it is visible. |
| 3873 | IntendedTy = S.Context.UnsignedShortTy; |
Ted Kremenek | da2f405 | 2013-10-15 05:25:17 +0000 | [diff] [blame] | 3874 | |
| 3875 | // While we are here, check if the value is an IntegerLiteral that happens |
| 3876 | // to be within the valid range. |
| 3877 | if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) { |
| 3878 | const llvm::APInt &V = IL->getValue(); |
| 3879 | if (V.getActiveBits() <= S.Context.getTypeSize(IntendedTy)) |
| 3880 | return true; |
| 3881 | } |
| 3882 | |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3883 | LookupResult Result(S, &S.Context.Idents.get("unichar"), E->getLocStart(), |
| 3884 | Sema::LookupOrdinaryName); |
| 3885 | if (S.LookupName(Result, S.getCurScope())) { |
| 3886 | NamedDecl *ND = Result.getFoundDecl(); |
| 3887 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND)) |
| 3888 | if (TD->getUnderlyingType() == IntendedTy) |
| 3889 | IntendedTy = S.Context.getTypedefType(TD); |
| 3890 | } |
| 3891 | } |
| 3892 | } |
| 3893 | |
| 3894 | // Special-case some of Darwin's platform-independence types by suggesting |
| 3895 | // casts to primitive types that are known to be large enough. |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 3896 | bool ShouldNotPrintDirectly = false; StringRef CastTyName; |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3897 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 3898 | QualType CastTy; |
| 3899 | std::tie(CastTy, CastTyName) = shouldNotPrintDirectly(S.Context, IntendedTy, E); |
| 3900 | if (!CastTy.isNull()) { |
| 3901 | IntendedTy = CastTy; |
| 3902 | ShouldNotPrintDirectly = true; |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3903 | } |
| 3904 | } |
| 3905 | |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3906 | // We may be able to offer a FixItHint if it is a supported type. |
| 3907 | PrintfSpecifier fixedFS = FS; |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3908 | bool success = fixedFS.fixType(IntendedTy, S.getLangOpts(), |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3909 | S.Context, ObjCContext); |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 3910 | |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3911 | if (success) { |
| 3912 | // Get the fix string from the fixed format specifier |
| 3913 | SmallString<16> buf; |
| 3914 | llvm::raw_svector_ostream os(buf); |
| 3915 | fixedFS.toString(os); |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 3916 | |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3917 | CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen); |
| 3918 | |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 3919 | if (IntendedTy == ExprTy && !ShouldNotPrintDirectly) { |
Daniel Jasper | ad8d849 | 2015-03-04 14:18:20 +0000 | [diff] [blame] | 3920 | unsigned diag = diag::warn_format_conversion_argument_type_mismatch; |
| 3921 | if (match == analyze_format_string::ArgType::NoMatchPedantic) { |
| 3922 | diag = diag::warn_format_conversion_argument_type_mismatch_pedantic; |
| 3923 | } |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3924 | // In this case, the specifier is wrong and should be changed to match |
| 3925 | // the argument. |
Daniel Jasper | ad8d849 | 2015-03-04 14:18:20 +0000 | [diff] [blame] | 3926 | EmitFormatDiagnostic(S.PDiag(diag) |
| 3927 | << AT.getRepresentativeTypeName(S.Context) |
| 3928 | << IntendedTy << IsEnum << E->getSourceRange(), |
| 3929 | E->getLocStart(), |
| 3930 | /*IsStringLocation*/ false, SpecRange, |
| 3931 | FixItHint::CreateReplacement(SpecRange, os.str())); |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3932 | |
| 3933 | } else { |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3934 | // The canonical type for formatting this value is different from the |
| 3935 | // actual type of the expression. (This occurs, for example, with Darwin's |
| 3936 | // NSInteger on 32-bit platforms, where it is typedef'd as 'int', but |
| 3937 | // should be printed as 'long' for 64-bit compatibility.) |
| 3938 | // Rather than emitting a normal format/argument mismatch, we want to |
| 3939 | // add a cast to the recommended type (and correct the format string |
| 3940 | // if necessary). |
| 3941 | SmallString<16> CastBuf; |
| 3942 | llvm::raw_svector_ostream CastFix(CastBuf); |
| 3943 | CastFix << "("; |
| 3944 | IntendedTy.print(CastFix, S.Context.getPrintingPolicy()); |
| 3945 | CastFix << ")"; |
| 3946 | |
| 3947 | SmallVector<FixItHint,4> Hints; |
| 3948 | if (!AT.matchesType(S.Context, IntendedTy)) |
| 3949 | Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str())); |
| 3950 | |
| 3951 | if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) { |
| 3952 | // If there's already a cast present, just replace it. |
| 3953 | SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc()); |
| 3954 | Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str())); |
| 3955 | |
| 3956 | } else if (!requiresParensToAddCast(E)) { |
| 3957 | // If the expression has high enough precedence, |
| 3958 | // just write the C-style cast. |
| 3959 | Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(), |
| 3960 | CastFix.str())); |
| 3961 | } else { |
| 3962 | // Otherwise, add parens around the expression as well as the cast. |
| 3963 | CastFix << "("; |
| 3964 | Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(), |
| 3965 | CastFix.str())); |
| 3966 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 3967 | SourceLocation After = S.getLocForEndOfToken(E->getLocEnd()); |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3968 | Hints.push_back(FixItHint::CreateInsertion(After, ")")); |
| 3969 | } |
| 3970 | |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3971 | if (ShouldNotPrintDirectly) { |
| 3972 | // The expression has a type that should not be printed directly. |
| 3973 | // We extract the name from the typedef because we don't want to show |
| 3974 | // the underlying type in the diagnostic. |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 3975 | StringRef Name; |
| 3976 | if (const TypedefType *TypedefTy = dyn_cast<TypedefType>(ExprTy)) |
| 3977 | Name = TypedefTy->getDecl()->getName(); |
| 3978 | else |
| 3979 | Name = CastTyName; |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3980 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_argument_needs_cast) |
Jordan Rose | bc53ed1 | 2014-05-31 04:12:14 +0000 | [diff] [blame] | 3981 | << Name << IntendedTy << IsEnum |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3982 | << E->getSourceRange(), |
| 3983 | E->getLocStart(), /*IsStringLocation=*/false, |
| 3984 | SpecRange, Hints); |
| 3985 | } else { |
| 3986 | // In this case, the expression could be printed using a different |
| 3987 | // specifier, but we've decided that the specifier is probably correct |
| 3988 | // and we should cast instead. Just use the normal warning message. |
| 3989 | EmitFormatDiagnostic( |
Jordan Rose | bc53ed1 | 2014-05-31 04:12:14 +0000 | [diff] [blame] | 3990 | S.PDiag(diag::warn_format_conversion_argument_type_mismatch) |
| 3991 | << AT.getRepresentativeTypeName(S.Context) << ExprTy << IsEnum |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 3992 | << E->getSourceRange(), |
| 3993 | E->getLocStart(), /*IsStringLocation*/false, |
| 3994 | SpecRange, Hints); |
| 3995 | } |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3996 | } |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 3997 | } else { |
| 3998 | const CharSourceRange &CSR = getSpecifierRange(StartSpecifier, |
| 3999 | SpecifierLen); |
| 4000 | // Since the warning for passing non-POD types to variadic functions |
| 4001 | // was deferred until now, we emit a warning for non-POD |
| 4002 | // arguments here. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 4003 | switch (S.isValidVarArgType(ExprTy)) { |
| 4004 | case Sema::VAK_Valid: |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4005 | case Sema::VAK_ValidInCXX11: { |
| 4006 | unsigned diag = diag::warn_format_conversion_argument_type_mismatch; |
| 4007 | if (match == analyze_printf::ArgType::NoMatchPedantic) { |
| 4008 | diag = diag::warn_format_conversion_argument_type_mismatch_pedantic; |
| 4009 | } |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 4010 | |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4011 | EmitFormatDiagnostic( |
| 4012 | S.PDiag(diag) << AT.getRepresentativeTypeName(S.Context) << ExprTy |
| 4013 | << IsEnum << CSR << E->getSourceRange(), |
| 4014 | E->getLocStart(), /*IsStringLocation*/ false, CSR); |
| 4015 | break; |
| 4016 | } |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 4017 | case Sema::VAK_Undefined: |
Hans Wennborg | d9dd4d2 | 2014-09-29 23:06:57 +0000 | [diff] [blame] | 4018 | case Sema::VAK_MSVCUndefined: |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 4019 | EmitFormatDiagnostic( |
| 4020 | S.PDiag(diag::warn_non_pod_vararg_with_format_string) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4021 | << S.getLangOpts().CPlusPlus11 |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 4022 | << ExprTy |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 4023 | << CallType |
| 4024 | << AT.getRepresentativeTypeName(S.Context) |
| 4025 | << CSR |
| 4026 | << E->getSourceRange(), |
| 4027 | E->getLocStart(), /*IsStringLocation*/false, CSR); |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 4028 | checkForCStrMembers(AT, E); |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 4029 | break; |
| 4030 | |
| 4031 | case Sema::VAK_Invalid: |
| 4032 | if (ExprTy->isObjCObjectType()) |
| 4033 | EmitFormatDiagnostic( |
| 4034 | S.PDiag(diag::err_cannot_pass_objc_interface_to_vararg_format) |
| 4035 | << S.getLangOpts().CPlusPlus11 |
| 4036 | << ExprTy |
| 4037 | << CallType |
| 4038 | << AT.getRepresentativeTypeName(S.Context) |
| 4039 | << CSR |
| 4040 | << E->getSourceRange(), |
| 4041 | E->getLocStart(), /*IsStringLocation*/false, CSR); |
| 4042 | else |
| 4043 | // FIXME: If this is an initializer list, suggest removing the braces |
| 4044 | // or inserting a cast to the target type. |
| 4045 | S.Diag(E->getLocStart(), diag::err_cannot_pass_to_vararg_format) |
| 4046 | << isa<InitListExpr>(E) << ExprTy << CallType |
| 4047 | << AT.getRepresentativeTypeName(S.Context) |
| 4048 | << E->getSourceRange(); |
| 4049 | break; |
| 4050 | } |
| 4051 | |
| 4052 | assert(FirstDataArg + FS.getArgIndex() < CheckedVarArgs.size() && |
| 4053 | "format string specifier index out of range"); |
| 4054 | CheckedVarArgs[FirstDataArg + FS.getArgIndex()] = true; |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 4055 | } |
| 4056 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 4057 | return true; |
| 4058 | } |
| 4059 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4060 | //===--- CHECK: Scanf format string checking ------------------------------===// |
| 4061 | |
| 4062 | namespace { |
| 4063 | class CheckScanfHandler : public CheckFormatHandler { |
| 4064 | public: |
| 4065 | CheckScanfHandler(Sema &s, const StringLiteral *fexpr, |
| 4066 | const Expr *origFormatExpr, unsigned firstDataArg, |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 4067 | unsigned numDataArgs, const char *beg, bool hasVAListArg, |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 4068 | ArrayRef<const Expr *> Args, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 4069 | unsigned formatIdx, bool inFunctionCall, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 4070 | Sema::VariadicCallType CallType, |
| 4071 | llvm::SmallBitVector &CheckedVarArgs) |
| 4072 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
| 4073 | numDataArgs, beg, hasVAListArg, |
| 4074 | Args, formatIdx, inFunctionCall, CallType, |
| 4075 | CheckedVarArgs) |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 4076 | {} |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4077 | |
| 4078 | bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, |
| 4079 | const char *startSpecifier, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 4080 | unsigned specifierLen) override; |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 4081 | |
| 4082 | bool HandleInvalidScanfConversionSpecifier( |
| 4083 | const analyze_scanf::ScanfSpecifier &FS, |
| 4084 | const char *startSpecifier, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 4085 | unsigned specifierLen) override; |
Ted Kremenek | d7b31cc | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 4086 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 4087 | void HandleIncompleteScanList(const char *start, const char *end) override; |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4088 | }; |
Ted Kremenek | 019d224 | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 4089 | } |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 4090 | |
Ted Kremenek | d7b31cc | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 4091 | void CheckScanfHandler::HandleIncompleteScanList(const char *start, |
| 4092 | const char *end) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4093 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete), |
| 4094 | getLocationOfByte(end), /*IsStringLocation*/true, |
| 4095 | getSpecifierRange(start, end - start)); |
Ted Kremenek | d7b31cc | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 4096 | } |
| 4097 | |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 4098 | bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( |
| 4099 | const analyze_scanf::ScanfSpecifier &FS, |
| 4100 | const char *startSpecifier, |
| 4101 | unsigned specifierLen) { |
| 4102 | |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 4103 | const analyze_scanf::ScanfConversionSpecifier &CS = |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 4104 | FS.getConversionSpecifier(); |
| 4105 | |
| 4106 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 4107 | getLocationOfByte(CS.getStart()), |
| 4108 | startSpecifier, specifierLen, |
| 4109 | CS.getStart(), CS.getLength()); |
| 4110 | } |
| 4111 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4112 | bool CheckScanfHandler::HandleScanfSpecifier( |
| 4113 | const analyze_scanf::ScanfSpecifier &FS, |
| 4114 | const char *startSpecifier, |
| 4115 | unsigned specifierLen) { |
| 4116 | |
| 4117 | using namespace analyze_scanf; |
| 4118 | using namespace analyze_format_string; |
| 4119 | |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 4120 | const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4121 | |
Ted Kremenek | 6cd6942 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 4122 | // Handle case where '%' and '*' don't consume an argument. These shouldn't |
| 4123 | // be used to decide if we are using positional arguments consistently. |
| 4124 | if (FS.consumesDataArgument()) { |
| 4125 | if (atFirstArg) { |
| 4126 | atFirstArg = false; |
| 4127 | usesPositionalArgs = FS.usesPositionalArg(); |
| 4128 | } |
| 4129 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4130 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), |
| 4131 | startSpecifier, specifierLen); |
Ted Kremenek | 6cd6942 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 4132 | return false; |
| 4133 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | // Check if the field with is non-zero. |
| 4137 | const OptionalAmount &Amt = FS.getFieldWidth(); |
| 4138 | if (Amt.getHowSpecified() == OptionalAmount::Constant) { |
| 4139 | if (Amt.getConstantAmount() == 0) { |
| 4140 | const CharSourceRange &R = getSpecifierRange(Amt.getStart(), |
| 4141 | Amt.getConstantLength()); |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4142 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width), |
| 4143 | getLocationOfByte(Amt.getStart()), |
| 4144 | /*IsStringLocation*/true, R, |
| 4145 | FixItHint::CreateRemoval(R)); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4146 | } |
| 4147 | } |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4148 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4149 | if (!FS.consumesDataArgument()) { |
| 4150 | // FIXME: Technically specifying a precision or field width here |
| 4151 | // makes no sense. Worth issuing a warning at some point. |
| 4152 | return true; |
| 4153 | } |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4154 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4155 | // Consume the argument. |
| 4156 | unsigned argIndex = FS.getArgIndex(); |
| 4157 | if (argIndex < NumDataArgs) { |
| 4158 | // The check to see if the argIndex is valid will come later. |
| 4159 | // We set the bit here because we may exit early from this |
| 4160 | // function if we encounter some other error. |
| 4161 | CoveredArgs.set(argIndex); |
| 4162 | } |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4163 | |
Ted Kremenek | 4407ea4 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 4164 | // Check the length modifier is valid with the given conversion specifier. |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 4165 | if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo())) |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 4166 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
| 4167 | diag::warn_format_nonsensical_length); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 4168 | else if (!FS.hasStandardLengthModifier()) |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 4169 | HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 4170 | else if (!FS.hasStandardLengthConversionCombination()) |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 4171 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
| 4172 | diag::warn_format_non_standard_conversion_spec); |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 4173 | |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 4174 | if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) |
| 4175 | HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); |
| 4176 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4177 | // The remaining checks depend on the data arguments. |
| 4178 | if (HasVAListArg) |
| 4179 | return true; |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4180 | |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 4181 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4182 | return false; |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4183 | |
Hans Wennborg | b1a5e09 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 4184 | // Check that the argument type matches the format specifier. |
| 4185 | const Expr *Ex = getDataArg(argIndex); |
Jordan Rose | 58bbe42 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 4186 | if (!Ex) |
| 4187 | return true; |
| 4188 | |
Hans Wennborg | b1ab2a8 | 2012-08-07 08:59:46 +0000 | [diff] [blame] | 4189 | const analyze_format_string::ArgType &AT = FS.getArgType(S.Context); |
Seth Cantrell | 7934007 | 2015-03-04 05:58:08 +0000 | [diff] [blame] | 4190 | |
| 4191 | if (!AT.isValid()) { |
| 4192 | return true; |
| 4193 | } |
| 4194 | |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4195 | analyze_format_string::ArgType::MatchKind match = |
| 4196 | AT.matchesType(S.Context, Ex->getType()); |
Seth Cantrell | 7934007 | 2015-03-04 05:58:08 +0000 | [diff] [blame] | 4197 | if (match == analyze_format_string::ArgType::Match) { |
| 4198 | return true; |
| 4199 | } |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4200 | |
Seth Cantrell | 7934007 | 2015-03-04 05:58:08 +0000 | [diff] [blame] | 4201 | ScanfSpecifier fixedFS = FS; |
| 4202 | bool success = fixedFS.fixType(Ex->getType(), Ex->IgnoreImpCasts()->getType(), |
| 4203 | S.getLangOpts(), S.Context); |
Hans Wennborg | b1a5e09 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 4204 | |
Seth Cantrell | 7934007 | 2015-03-04 05:58:08 +0000 | [diff] [blame] | 4205 | unsigned diag = diag::warn_format_conversion_argument_type_mismatch; |
| 4206 | if (match == analyze_format_string::ArgType::NoMatchPedantic) { |
| 4207 | diag = diag::warn_format_conversion_argument_type_mismatch_pedantic; |
| 4208 | } |
Hans Wennborg | b1a5e09 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 4209 | |
Seth Cantrell | 7934007 | 2015-03-04 05:58:08 +0000 | [diff] [blame] | 4210 | if (success) { |
| 4211 | // Get the fix string from the fixed format specifier. |
| 4212 | SmallString<128> buf; |
| 4213 | llvm::raw_svector_ostream os(buf); |
| 4214 | fixedFS.toString(os); |
| 4215 | |
| 4216 | EmitFormatDiagnostic( |
| 4217 | S.PDiag(diag) << AT.getRepresentativeTypeName(S.Context) |
| 4218 | << Ex->getType() << false << Ex->getSourceRange(), |
| 4219 | Ex->getLocStart(), |
| 4220 | /*IsStringLocation*/ false, |
| 4221 | getSpecifierRange(startSpecifier, specifierLen), |
| 4222 | FixItHint::CreateReplacement( |
| 4223 | getSpecifierRange(startSpecifier, specifierLen), os.str())); |
| 4224 | } else { |
| 4225 | EmitFormatDiagnostic(S.PDiag(diag) |
| 4226 | << AT.getRepresentativeTypeName(S.Context) |
| 4227 | << Ex->getType() << false << Ex->getSourceRange(), |
| 4228 | Ex->getLocStart(), |
| 4229 | /*IsStringLocation*/ false, |
| 4230 | getSpecifierRange(startSpecifier, specifierLen)); |
Hans Wennborg | b1a5e09 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 4231 | } |
| 4232 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4233 | return true; |
| 4234 | } |
| 4235 | |
| 4236 | void Sema::CheckFormatString(const StringLiteral *FExpr, |
Ted Kremenek | fb45d35 | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 4237 | const Expr *OrigFormatExpr, |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 4238 | ArrayRef<const Expr *> Args, |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 4239 | bool HasVAListArg, unsigned format_idx, |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 4240 | unsigned firstDataArg, FormatStringType Type, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 4241 | bool inFunctionCall, VariadicCallType CallType, |
| 4242 | llvm::SmallBitVector &CheckedVarArgs) { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4243 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 4244 | // CHECK: is the format string a wide literal? |
Richard Smith | 4060f77 | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 4245 | if (!FExpr->isAscii() && !FExpr->isUTF8()) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4246 | CheckFormatHandler::EmitFormatDiagnostic( |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 4247 | *this, inFunctionCall, Args[format_idx], |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4248 | PDiag(diag::warn_format_string_is_wide_literal), FExpr->getLocStart(), |
| 4249 | /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 4250 | return; |
| 4251 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4252 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 4253 | // Str - The format string. NOTE: this is NOT null-terminated! |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4254 | StringRef StrRef = FExpr->getString(); |
Benjamin Kramer | 35b077e | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 4255 | const char *Str = StrRef.data(); |
Benjamin Kramer | 6c6a4f4 | 2014-02-20 17:05:38 +0000 | [diff] [blame] | 4256 | // Account for cases where the string literal is truncated in a declaration. |
| 4257 | const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType()); |
| 4258 | assert(T && "String literal not of constant array type!"); |
| 4259 | size_t TypeSize = T->getSize().getZExtValue(); |
| 4260 | 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] | 4261 | const unsigned numDataArgs = Args.size() - firstDataArg; |
Benjamin Kramer | 6c6a4f4 | 2014-02-20 17:05:38 +0000 | [diff] [blame] | 4262 | |
| 4263 | // Emit a warning if the string literal is truncated and does not contain an |
| 4264 | // embedded null character. |
| 4265 | if (TypeSize <= StrRef.size() && |
| 4266 | StrRef.substr(0, TypeSize).find('\0') == StringRef::npos) { |
| 4267 | CheckFormatHandler::EmitFormatDiagnostic( |
| 4268 | *this, inFunctionCall, Args[format_idx], |
| 4269 | PDiag(diag::warn_printf_format_string_not_null_terminated), |
| 4270 | FExpr->getLocStart(), |
| 4271 | /*IsStringLocation=*/true, OrigFormatExpr->getSourceRange()); |
| 4272 | return; |
| 4273 | } |
| 4274 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 4275 | // CHECK: empty format string? |
Ted Kremenek | 6e302b2 | 2011-09-29 05:52:16 +0000 | [diff] [blame] | 4276 | if (StrLen == 0 && numDataArgs > 0) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4277 | CheckFormatHandler::EmitFormatDiagnostic( |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 4278 | *this, inFunctionCall, Args[format_idx], |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4279 | PDiag(diag::warn_empty_format_string), FExpr->getLocStart(), |
| 4280 | /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 4281 | return; |
| 4282 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4283 | |
Dimitry Andric | 6b5ed34 | 2015-02-19 22:32:33 +0000 | [diff] [blame] | 4284 | if (Type == FST_Printf || Type == FST_NSString || |
Fariborz Jahanian | f8dce0f | 2015-02-21 00:45:58 +0000 | [diff] [blame] | 4285 | Type == FST_FreeBSDKPrintf || Type == FST_OSTrace) { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4286 | CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, |
Fariborz Jahanian | f8dce0f | 2015-02-21 00:45:58 +0000 | [diff] [blame] | 4287 | numDataArgs, (Type == FST_NSString || Type == FST_OSTrace), |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 4288 | Str, HasVAListArg, Args, format_idx, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 4289 | inFunctionCall, CallType, CheckedVarArgs); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4290 | |
Hans Wennborg | 23926bd | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 4291 | if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen, |
Jordan Rose | 510260c | 2012-09-13 02:11:03 +0000 | [diff] [blame] | 4292 | getLangOpts(), |
Dimitry Andric | 6b5ed34 | 2015-02-19 22:32:33 +0000 | [diff] [blame] | 4293 | Context.getTargetInfo(), |
| 4294 | Type == FST_FreeBSDKPrintf)) |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4295 | H.DoneProcessing(); |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 4296 | } else if (Type == FST_Scanf) { |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 4297 | CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, numDataArgs, |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 4298 | Str, HasVAListArg, Args, format_idx, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 4299 | inFunctionCall, CallType, CheckedVarArgs); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4300 | |
Hans Wennborg | 23926bd | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 4301 | if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen, |
Jordan Rose | 510260c | 2012-09-13 02:11:03 +0000 | [diff] [blame] | 4302 | getLangOpts(), |
| 4303 | Context.getTargetInfo())) |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4304 | H.DoneProcessing(); |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 4305 | } // TODO: handle other formats |
Ted Kremenek | c70ee86 | 2010-01-28 01:18:22 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 4308 | bool Sema::FormatStringHasSArg(const StringLiteral *FExpr) { |
| 4309 | // Str - The format string. NOTE: this is NOT null-terminated! |
| 4310 | StringRef StrRef = FExpr->getString(); |
| 4311 | const char *Str = StrRef.data(); |
| 4312 | // Account for cases where the string literal is truncated in a declaration. |
| 4313 | const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType()); |
| 4314 | assert(T && "String literal not of constant array type!"); |
| 4315 | size_t TypeSize = T->getSize().getZExtValue(); |
| 4316 | size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size()); |
| 4317 | return analyze_format_string::ParseFormatStringHasSArg(Str, Str + StrLen, |
| 4318 | getLangOpts(), |
| 4319 | Context.getTargetInfo()); |
| 4320 | } |
| 4321 | |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 4322 | //===--- CHECK: Warn on use of wrong absolute value function. -------------===// |
| 4323 | |
| 4324 | // Returns the related absolute value function that is larger, of 0 if one |
| 4325 | // does not exist. |
| 4326 | static unsigned getLargerAbsoluteValueFunction(unsigned AbsFunction) { |
| 4327 | switch (AbsFunction) { |
| 4328 | default: |
| 4329 | return 0; |
| 4330 | |
| 4331 | case Builtin::BI__builtin_abs: |
| 4332 | return Builtin::BI__builtin_labs; |
| 4333 | case Builtin::BI__builtin_labs: |
| 4334 | return Builtin::BI__builtin_llabs; |
| 4335 | case Builtin::BI__builtin_llabs: |
| 4336 | return 0; |
| 4337 | |
| 4338 | case Builtin::BI__builtin_fabsf: |
| 4339 | return Builtin::BI__builtin_fabs; |
| 4340 | case Builtin::BI__builtin_fabs: |
| 4341 | return Builtin::BI__builtin_fabsl; |
| 4342 | case Builtin::BI__builtin_fabsl: |
| 4343 | return 0; |
| 4344 | |
| 4345 | case Builtin::BI__builtin_cabsf: |
| 4346 | return Builtin::BI__builtin_cabs; |
| 4347 | case Builtin::BI__builtin_cabs: |
| 4348 | return Builtin::BI__builtin_cabsl; |
| 4349 | case Builtin::BI__builtin_cabsl: |
| 4350 | return 0; |
| 4351 | |
| 4352 | case Builtin::BIabs: |
| 4353 | return Builtin::BIlabs; |
| 4354 | case Builtin::BIlabs: |
| 4355 | return Builtin::BIllabs; |
| 4356 | case Builtin::BIllabs: |
| 4357 | return 0; |
| 4358 | |
| 4359 | case Builtin::BIfabsf: |
| 4360 | return Builtin::BIfabs; |
| 4361 | case Builtin::BIfabs: |
| 4362 | return Builtin::BIfabsl; |
| 4363 | case Builtin::BIfabsl: |
| 4364 | return 0; |
| 4365 | |
| 4366 | case Builtin::BIcabsf: |
| 4367 | return Builtin::BIcabs; |
| 4368 | case Builtin::BIcabs: |
| 4369 | return Builtin::BIcabsl; |
| 4370 | case Builtin::BIcabsl: |
| 4371 | return 0; |
| 4372 | } |
| 4373 | } |
| 4374 | |
| 4375 | // Returns the argument type of the absolute value function. |
| 4376 | static QualType getAbsoluteValueArgumentType(ASTContext &Context, |
| 4377 | unsigned AbsType) { |
| 4378 | if (AbsType == 0) |
| 4379 | return QualType(); |
| 4380 | |
| 4381 | ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None; |
| 4382 | QualType BuiltinType = Context.GetBuiltinType(AbsType, Error); |
| 4383 | if (Error != ASTContext::GE_None) |
| 4384 | return QualType(); |
| 4385 | |
| 4386 | const FunctionProtoType *FT = BuiltinType->getAs<FunctionProtoType>(); |
| 4387 | if (!FT) |
| 4388 | return QualType(); |
| 4389 | |
| 4390 | if (FT->getNumParams() != 1) |
| 4391 | return QualType(); |
| 4392 | |
| 4393 | return FT->getParamType(0); |
| 4394 | } |
| 4395 | |
| 4396 | // Returns the best absolute value function, or zero, based on type and |
| 4397 | // current absolute value function. |
| 4398 | static unsigned getBestAbsFunction(ASTContext &Context, QualType ArgType, |
| 4399 | unsigned AbsFunctionKind) { |
| 4400 | unsigned BestKind = 0; |
| 4401 | uint64_t ArgSize = Context.getTypeSize(ArgType); |
| 4402 | for (unsigned Kind = AbsFunctionKind; Kind != 0; |
| 4403 | Kind = getLargerAbsoluteValueFunction(Kind)) { |
| 4404 | QualType ParamType = getAbsoluteValueArgumentType(Context, Kind); |
| 4405 | if (Context.getTypeSize(ParamType) >= ArgSize) { |
| 4406 | if (BestKind == 0) |
| 4407 | BestKind = Kind; |
| 4408 | else if (Context.hasSameType(ParamType, ArgType)) { |
| 4409 | BestKind = Kind; |
| 4410 | break; |
| 4411 | } |
| 4412 | } |
| 4413 | } |
| 4414 | return BestKind; |
| 4415 | } |
| 4416 | |
| 4417 | enum AbsoluteValueKind { |
| 4418 | AVK_Integer, |
| 4419 | AVK_Floating, |
| 4420 | AVK_Complex |
| 4421 | }; |
| 4422 | |
| 4423 | static AbsoluteValueKind getAbsoluteValueKind(QualType T) { |
| 4424 | if (T->isIntegralOrEnumerationType()) |
| 4425 | return AVK_Integer; |
| 4426 | if (T->isRealFloatingType()) |
| 4427 | return AVK_Floating; |
| 4428 | if (T->isAnyComplexType()) |
| 4429 | return AVK_Complex; |
| 4430 | |
| 4431 | llvm_unreachable("Type not integer, floating, or complex"); |
| 4432 | } |
| 4433 | |
| 4434 | // Changes the absolute value function to a different type. Preserves whether |
| 4435 | // the function is a builtin. |
| 4436 | static unsigned changeAbsFunction(unsigned AbsKind, |
| 4437 | AbsoluteValueKind ValueKind) { |
| 4438 | switch (ValueKind) { |
| 4439 | case AVK_Integer: |
| 4440 | switch (AbsKind) { |
| 4441 | default: |
| 4442 | return 0; |
| 4443 | case Builtin::BI__builtin_fabsf: |
| 4444 | case Builtin::BI__builtin_fabs: |
| 4445 | case Builtin::BI__builtin_fabsl: |
| 4446 | case Builtin::BI__builtin_cabsf: |
| 4447 | case Builtin::BI__builtin_cabs: |
| 4448 | case Builtin::BI__builtin_cabsl: |
| 4449 | return Builtin::BI__builtin_abs; |
| 4450 | case Builtin::BIfabsf: |
| 4451 | case Builtin::BIfabs: |
| 4452 | case Builtin::BIfabsl: |
| 4453 | case Builtin::BIcabsf: |
| 4454 | case Builtin::BIcabs: |
| 4455 | case Builtin::BIcabsl: |
| 4456 | return Builtin::BIabs; |
| 4457 | } |
| 4458 | case AVK_Floating: |
| 4459 | switch (AbsKind) { |
| 4460 | default: |
| 4461 | return 0; |
| 4462 | case Builtin::BI__builtin_abs: |
| 4463 | case Builtin::BI__builtin_labs: |
| 4464 | case Builtin::BI__builtin_llabs: |
| 4465 | case Builtin::BI__builtin_cabsf: |
| 4466 | case Builtin::BI__builtin_cabs: |
| 4467 | case Builtin::BI__builtin_cabsl: |
| 4468 | return Builtin::BI__builtin_fabsf; |
| 4469 | case Builtin::BIabs: |
| 4470 | case Builtin::BIlabs: |
| 4471 | case Builtin::BIllabs: |
| 4472 | case Builtin::BIcabsf: |
| 4473 | case Builtin::BIcabs: |
| 4474 | case Builtin::BIcabsl: |
| 4475 | return Builtin::BIfabsf; |
| 4476 | } |
| 4477 | case AVK_Complex: |
| 4478 | switch (AbsKind) { |
| 4479 | default: |
| 4480 | return 0; |
| 4481 | case Builtin::BI__builtin_abs: |
| 4482 | case Builtin::BI__builtin_labs: |
| 4483 | case Builtin::BI__builtin_llabs: |
| 4484 | case Builtin::BI__builtin_fabsf: |
| 4485 | case Builtin::BI__builtin_fabs: |
| 4486 | case Builtin::BI__builtin_fabsl: |
| 4487 | return Builtin::BI__builtin_cabsf; |
| 4488 | case Builtin::BIabs: |
| 4489 | case Builtin::BIlabs: |
| 4490 | case Builtin::BIllabs: |
| 4491 | case Builtin::BIfabsf: |
| 4492 | case Builtin::BIfabs: |
| 4493 | case Builtin::BIfabsl: |
| 4494 | return Builtin::BIcabsf; |
| 4495 | } |
| 4496 | } |
| 4497 | llvm_unreachable("Unable to convert function"); |
| 4498 | } |
| 4499 | |
Benjamin Kramer | 3d6220d | 2014-03-01 17:21:22 +0000 | [diff] [blame] | 4500 | static unsigned getAbsoluteValueFunctionKind(const FunctionDecl *FDecl) { |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 4501 | const IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
| 4502 | if (!FnInfo) |
| 4503 | return 0; |
| 4504 | |
| 4505 | switch (FDecl->getBuiltinID()) { |
| 4506 | default: |
| 4507 | return 0; |
| 4508 | case Builtin::BI__builtin_abs: |
| 4509 | case Builtin::BI__builtin_fabs: |
| 4510 | case Builtin::BI__builtin_fabsf: |
| 4511 | case Builtin::BI__builtin_fabsl: |
| 4512 | case Builtin::BI__builtin_labs: |
| 4513 | case Builtin::BI__builtin_llabs: |
| 4514 | case Builtin::BI__builtin_cabs: |
| 4515 | case Builtin::BI__builtin_cabsf: |
| 4516 | case Builtin::BI__builtin_cabsl: |
| 4517 | case Builtin::BIabs: |
| 4518 | case Builtin::BIlabs: |
| 4519 | case Builtin::BIllabs: |
| 4520 | case Builtin::BIfabs: |
| 4521 | case Builtin::BIfabsf: |
| 4522 | case Builtin::BIfabsl: |
| 4523 | case Builtin::BIcabs: |
| 4524 | case Builtin::BIcabsf: |
| 4525 | case Builtin::BIcabsl: |
| 4526 | return FDecl->getBuiltinID(); |
| 4527 | } |
| 4528 | llvm_unreachable("Unknown Builtin type"); |
| 4529 | } |
| 4530 | |
| 4531 | // If the replacement is valid, emit a note with replacement function. |
| 4532 | // Additionally, suggest including the proper header if not already included. |
| 4533 | static void emitReplacement(Sema &S, SourceLocation Loc, SourceRange Range, |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 4534 | unsigned AbsKind, QualType ArgType) { |
| 4535 | bool EmitHeaderHint = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4536 | const char *HeaderName = nullptr; |
| 4537 | const char *FunctionName = nullptr; |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 4538 | if (S.getLangOpts().CPlusPlus && !ArgType->isAnyComplexType()) { |
| 4539 | FunctionName = "std::abs"; |
| 4540 | if (ArgType->isIntegralOrEnumerationType()) { |
| 4541 | HeaderName = "cstdlib"; |
| 4542 | } else if (ArgType->isRealFloatingType()) { |
| 4543 | HeaderName = "cmath"; |
| 4544 | } else { |
| 4545 | llvm_unreachable("Invalid Type"); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 4546 | } |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 4547 | |
| 4548 | // Lookup all std::abs |
| 4549 | if (NamespaceDecl *Std = S.getStdNamespace()) { |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4550 | LookupResult R(S, &S.Context.Idents.get("abs"), Loc, Sema::LookupAnyName); |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 4551 | R.suppressDiagnostics(); |
| 4552 | S.LookupQualifiedName(R, Std); |
| 4553 | |
| 4554 | for (const auto *I : R) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4555 | const FunctionDecl *FDecl = nullptr; |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 4556 | if (const UsingShadowDecl *UsingD = dyn_cast<UsingShadowDecl>(I)) { |
| 4557 | FDecl = dyn_cast<FunctionDecl>(UsingD->getTargetDecl()); |
| 4558 | } else { |
| 4559 | FDecl = dyn_cast<FunctionDecl>(I); |
| 4560 | } |
| 4561 | if (!FDecl) |
| 4562 | continue; |
| 4563 | |
| 4564 | // Found std::abs(), check that they are the right ones. |
| 4565 | if (FDecl->getNumParams() != 1) |
| 4566 | continue; |
| 4567 | |
| 4568 | // Check that the parameter type can handle the argument. |
| 4569 | QualType ParamType = FDecl->getParamDecl(0)->getType(); |
| 4570 | if (getAbsoluteValueKind(ArgType) == getAbsoluteValueKind(ParamType) && |
| 4571 | S.Context.getTypeSize(ArgType) <= |
| 4572 | S.Context.getTypeSize(ParamType)) { |
| 4573 | // Found a function, don't need the header hint. |
| 4574 | EmitHeaderHint = false; |
| 4575 | break; |
| 4576 | } |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 4577 | } |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 4578 | } |
| 4579 | } else { |
| 4580 | FunctionName = S.Context.BuiltinInfo.GetName(AbsKind); |
| 4581 | HeaderName = S.Context.BuiltinInfo.getHeaderName(AbsKind); |
| 4582 | |
| 4583 | if (HeaderName) { |
| 4584 | DeclarationName DN(&S.Context.Idents.get(FunctionName)); |
| 4585 | LookupResult R(S, DN, Loc, Sema::LookupAnyName); |
| 4586 | R.suppressDiagnostics(); |
| 4587 | S.LookupName(R, S.getCurScope()); |
| 4588 | |
| 4589 | if (R.isSingleResult()) { |
| 4590 | FunctionDecl *FD = dyn_cast<FunctionDecl>(R.getFoundDecl()); |
| 4591 | if (FD && FD->getBuiltinID() == AbsKind) { |
| 4592 | EmitHeaderHint = false; |
| 4593 | } else { |
| 4594 | return; |
| 4595 | } |
| 4596 | } else if (!R.empty()) { |
| 4597 | return; |
| 4598 | } |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 4599 | } |
| 4600 | } |
| 4601 | |
| 4602 | S.Diag(Loc, diag::note_replace_abs_function) |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 4603 | << FunctionName << FixItHint::CreateReplacement(Range, FunctionName); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 4604 | |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 4605 | if (!HeaderName) |
| 4606 | return; |
| 4607 | |
| 4608 | if (!EmitHeaderHint) |
| 4609 | return; |
| 4610 | |
Alp Toker | 5d96e0a | 2014-07-11 20:53:51 +0000 | [diff] [blame] | 4611 | S.Diag(Loc, diag::note_include_header_or_declare) << HeaderName |
| 4612 | << FunctionName; |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 4613 | } |
| 4614 | |
| 4615 | static bool IsFunctionStdAbs(const FunctionDecl *FDecl) { |
| 4616 | if (!FDecl) |
| 4617 | return false; |
| 4618 | |
| 4619 | if (!FDecl->getIdentifier() || !FDecl->getIdentifier()->isStr("abs")) |
| 4620 | return false; |
| 4621 | |
| 4622 | const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(FDecl->getDeclContext()); |
| 4623 | |
| 4624 | while (ND && ND->isInlineNamespace()) { |
| 4625 | ND = dyn_cast<NamespaceDecl>(ND->getDeclContext()); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 4626 | } |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 4627 | |
| 4628 | if (!ND || !ND->getIdentifier() || !ND->getIdentifier()->isStr("std")) |
| 4629 | return false; |
| 4630 | |
| 4631 | if (!isa<TranslationUnitDecl>(ND->getDeclContext())) |
| 4632 | return false; |
| 4633 | |
| 4634 | return true; |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | // Warn when using the wrong abs() function. |
| 4638 | void Sema::CheckAbsoluteValueFunction(const CallExpr *Call, |
| 4639 | const FunctionDecl *FDecl, |
| 4640 | IdentifierInfo *FnInfo) { |
| 4641 | if (Call->getNumArgs() != 1) |
| 4642 | return; |
| 4643 | |
| 4644 | unsigned AbsKind = getAbsoluteValueFunctionKind(FDecl); |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 4645 | bool IsStdAbs = IsFunctionStdAbs(FDecl); |
| 4646 | if (AbsKind == 0 && !IsStdAbs) |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 4647 | return; |
| 4648 | |
| 4649 | QualType ArgType = Call->getArg(0)->IgnoreParenImpCasts()->getType(); |
| 4650 | QualType ParamType = Call->getArg(0)->getType(); |
| 4651 | |
Alp Toker | 5d96e0a | 2014-07-11 20:53:51 +0000 | [diff] [blame] | 4652 | // Unsigned types cannot be negative. Suggest removing the absolute value |
| 4653 | // function call. |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 4654 | if (ArgType->isUnsignedIntegerType()) { |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 4655 | const char *FunctionName = |
| 4656 | IsStdAbs ? "std::abs" : Context.BuiltinInfo.GetName(AbsKind); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 4657 | Diag(Call->getExprLoc(), diag::warn_unsigned_abs) << ArgType << ParamType; |
| 4658 | Diag(Call->getExprLoc(), diag::note_remove_abs) |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 4659 | << FunctionName |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 4660 | << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange()); |
| 4661 | return; |
| 4662 | } |
| 4663 | |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 4664 | // std::abs has overloads which prevent most of the absolute value problems |
| 4665 | // from occurring. |
| 4666 | if (IsStdAbs) |
| 4667 | return; |
| 4668 | |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 4669 | AbsoluteValueKind ArgValueKind = getAbsoluteValueKind(ArgType); |
| 4670 | AbsoluteValueKind ParamValueKind = getAbsoluteValueKind(ParamType); |
| 4671 | |
| 4672 | // The argument and parameter are the same kind. Check if they are the right |
| 4673 | // size. |
| 4674 | if (ArgValueKind == ParamValueKind) { |
| 4675 | if (Context.getTypeSize(ArgType) <= Context.getTypeSize(ParamType)) |
| 4676 | return; |
| 4677 | |
| 4678 | unsigned NewAbsKind = getBestAbsFunction(Context, ArgType, AbsKind); |
| 4679 | Diag(Call->getExprLoc(), diag::warn_abs_too_small) |
| 4680 | << FDecl << ArgType << ParamType; |
| 4681 | |
| 4682 | if (NewAbsKind == 0) |
| 4683 | return; |
| 4684 | |
| 4685 | emitReplacement(*this, Call->getExprLoc(), |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 4686 | Call->getCallee()->getSourceRange(), NewAbsKind, ArgType); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 4687 | return; |
| 4688 | } |
| 4689 | |
| 4690 | // ArgValueKind != ParamValueKind |
| 4691 | // The wrong type of absolute value function was used. Attempt to find the |
| 4692 | // proper one. |
| 4693 | unsigned NewAbsKind = changeAbsFunction(AbsKind, ArgValueKind); |
| 4694 | NewAbsKind = getBestAbsFunction(Context, ArgType, NewAbsKind); |
| 4695 | if (NewAbsKind == 0) |
| 4696 | return; |
| 4697 | |
| 4698 | Diag(Call->getExprLoc(), diag::warn_wrong_absolute_value_type) |
| 4699 | << FDecl << ParamValueKind << ArgValueKind; |
| 4700 | |
| 4701 | emitReplacement(*this, Call->getExprLoc(), |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 4702 | Call->getCallee()->getSourceRange(), NewAbsKind, ArgType); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 4703 | return; |
| 4704 | } |
| 4705 | |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 4706 | //===--- CHECK: Standard memory functions ---------------------------------===// |
| 4707 | |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 4708 | /// \brief Takes the expression passed to the size_t parameter of functions |
| 4709 | /// such as memcmp, strncat, etc and warns if it's a comparison. |
| 4710 | /// |
| 4711 | /// This is to catch typos like `if (memcmp(&a, &b, sizeof(a) > 0))`. |
| 4712 | static bool CheckMemorySizeofForComparison(Sema &S, const Expr *E, |
| 4713 | IdentifierInfo *FnName, |
| 4714 | SourceLocation FnLoc, |
| 4715 | SourceLocation RParenLoc) { |
| 4716 | const BinaryOperator *Size = dyn_cast<BinaryOperator>(E); |
| 4717 | if (!Size) |
| 4718 | return false; |
| 4719 | |
| 4720 | // if E is binop and op is >, <, >=, <=, ==, &&, ||: |
| 4721 | if (!Size->isComparisonOp() && !Size->isEqualityOp() && !Size->isLogicalOp()) |
| 4722 | return false; |
| 4723 | |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 4724 | SourceRange SizeRange = Size->getSourceRange(); |
| 4725 | S.Diag(Size->getOperatorLoc(), diag::warn_memsize_comparison) |
| 4726 | << SizeRange << FnName; |
Alp Toker | b086903 | 2014-05-17 01:13:18 +0000 | [diff] [blame] | 4727 | S.Diag(FnLoc, diag::note_memsize_comparison_paren) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4728 | << FnName << FixItHint::CreateInsertion( |
| 4729 | S.getLocForEndOfToken(Size->getLHS()->getLocEnd()), ")") |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 4730 | << FixItHint::CreateRemoval(RParenLoc); |
Alp Toker | b086903 | 2014-05-17 01:13:18 +0000 | [diff] [blame] | 4731 | S.Diag(SizeRange.getBegin(), diag::note_memsize_comparison_cast_silence) |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 4732 | << FixItHint::CreateInsertion(SizeRange.getBegin(), "(size_t)(") |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4733 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(SizeRange.getEnd()), |
| 4734 | ")"); |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 4735 | |
| 4736 | return true; |
| 4737 | } |
| 4738 | |
Reid Kleckner | 5fb5b12 | 2014-06-27 23:58:21 +0000 | [diff] [blame] | 4739 | /// \brief Determine whether the given type is or contains a dynamic class type |
| 4740 | /// (e.g., whether it has a vtable). |
| 4741 | static const CXXRecordDecl *getContainedDynamicClass(QualType T, |
| 4742 | bool &IsContained) { |
| 4743 | // Look through array types while ignoring qualifiers. |
| 4744 | const Type *Ty = T->getBaseElementTypeUnsafe(); |
| 4745 | IsContained = false; |
| 4746 | |
| 4747 | const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
| 4748 | RD = RD ? RD->getDefinition() : nullptr; |
| 4749 | if (!RD) |
| 4750 | return nullptr; |
| 4751 | |
| 4752 | if (RD->isDynamicClass()) |
| 4753 | return RD; |
| 4754 | |
| 4755 | // Check all the fields. If any bases were dynamic, the class is dynamic. |
| 4756 | // It's impossible for a class to transitively contain itself by value, so |
| 4757 | // infinite recursion is impossible. |
| 4758 | for (auto *FD : RD->fields()) { |
| 4759 | bool SubContained; |
| 4760 | if (const CXXRecordDecl *ContainedRD = |
| 4761 | getContainedDynamicClass(FD->getType(), SubContained)) { |
| 4762 | IsContained = true; |
| 4763 | return ContainedRD; |
| 4764 | } |
| 4765 | } |
| 4766 | |
| 4767 | return nullptr; |
Douglas Gregor | a74926b | 2011-05-03 20:05:22 +0000 | [diff] [blame] | 4768 | } |
| 4769 | |
Chandler Carruth | 889ed86 | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 4770 | /// \brief If E is a sizeof expression, returns its argument expression, |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4771 | /// otherwise returns NULL. |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 4772 | static const Expr *getSizeOfExprArg(const Expr *E) { |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 4773 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4774 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
| 4775 | if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType()) |
| 4776 | return SizeOf->getArgumentExpr()->IgnoreParenImpCasts(); |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 4777 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4778 | return nullptr; |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4779 | } |
| 4780 | |
Chandler Carruth | 889ed86 | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 4781 | /// \brief If E is a sizeof expression, returns its argument type. |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 4782 | static QualType getSizeOfArgType(const Expr *E) { |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4783 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
| 4784 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
| 4785 | if (SizeOf->getKind() == clang::UETT_SizeOf) |
| 4786 | return SizeOf->getTypeOfArgument(); |
| 4787 | |
| 4788 | return QualType(); |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 4789 | } |
| 4790 | |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 4791 | /// \brief Check for dangerous or invalid arguments to memset(). |
| 4792 | /// |
Chandler Carruth | ac68726 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 4793 | /// This issues warnings on known problematic, dangerous or unspecified |
Matt Beaumont-Gay | 3c48990 | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 4794 | /// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp' |
| 4795 | /// function calls. |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 4796 | /// |
| 4797 | /// \param Call The call expression to diagnose. |
Matt Beaumont-Gay | 3c48990 | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 4798 | void Sema::CheckMemaccessArguments(const CallExpr *Call, |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 4799 | unsigned BId, |
Matt Beaumont-Gay | 3c48990 | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 4800 | IdentifierInfo *FnName) { |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 4801 | assert(BId != 0); |
| 4802 | |
Ted Kremenek | b5fabb2 | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 4803 | // It is possible to have a non-standard definition of memset. Validate |
Douglas Gregor | 18739c3 | 2011-06-16 17:56:04 +0000 | [diff] [blame] | 4804 | // we have enough arguments, and if not, abort further checking. |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 4805 | unsigned ExpectedNumArgs = (BId == Builtin::BIstrndup ? 2 : 3); |
Nico Weber | 39bfed8 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 4806 | if (Call->getNumArgs() < ExpectedNumArgs) |
Ted Kremenek | b5fabb2 | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 4807 | return; |
| 4808 | |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 4809 | unsigned LastArg = (BId == Builtin::BImemset || |
| 4810 | BId == Builtin::BIstrndup ? 1 : 2); |
| 4811 | unsigned LenArg = (BId == Builtin::BIstrndup ? 1 : 2); |
Nico Weber | 39bfed8 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 4812 | const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts(); |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4813 | |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 4814 | if (CheckMemorySizeofForComparison(*this, LenExpr, FnName, |
| 4815 | Call->getLocStart(), Call->getRParenLoc())) |
| 4816 | return; |
| 4817 | |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4818 | // We have special checking when the length is a sizeof expression. |
| 4819 | QualType SizeOfArgTy = getSizeOfArgType(LenExpr); |
| 4820 | const Expr *SizeOfArg = getSizeOfExprArg(LenExpr); |
| 4821 | llvm::FoldingSetNodeID SizeOfArgID; |
| 4822 | |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 4823 | for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) { |
| 4824 | const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts(); |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 4825 | SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange(); |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 4826 | |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 4827 | QualType DestTy = Dest->getType(); |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 4828 | QualType PointeeTy; |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 4829 | if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) { |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 4830 | PointeeTy = DestPtrTy->getPointeeType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4831 | |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4832 | // Never warn about void type pointers. This can be used to suppress |
| 4833 | // false positives. |
| 4834 | if (PointeeTy->isVoidType()) |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 4835 | continue; |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 4836 | |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4837 | // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by |
| 4838 | // actually comparing the expressions for equality. Because computing the |
| 4839 | // expression IDs can be expensive, we only do this if the diagnostic is |
| 4840 | // enabled. |
| 4841 | if (SizeOfArg && |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 4842 | !Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, |
| 4843 | SizeOfArg->getExprLoc())) { |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4844 | // We only compute IDs for expressions if the warning is enabled, and |
| 4845 | // cache the sizeof arg's ID. |
| 4846 | if (SizeOfArgID == llvm::FoldingSetNodeID()) |
| 4847 | SizeOfArg->Profile(SizeOfArgID, Context, true); |
| 4848 | llvm::FoldingSetNodeID DestID; |
| 4849 | Dest->Profile(DestID, Context, true); |
| 4850 | if (DestID == SizeOfArgID) { |
Nico Weber | 39bfed8 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 4851 | // TODO: For strncpy() and friends, this could suggest sizeof(dst) |
| 4852 | // over sizeof(src) as well. |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4853 | unsigned ActionIdx = 0; // Default is to suggest dereferencing. |
Anna Zaks | 869aecc | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 4854 | StringRef ReadableName = FnName->getName(); |
| 4855 | |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4856 | if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest)) |
Anna Zaks | d08d915 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 4857 | if (UnaryOp->getOpcode() == UO_AddrOf) |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4858 | ActionIdx = 1; // If its an address-of operator, just remove it. |
Fariborz Jahanian | 4d365ba | 2013-01-30 01:12:44 +0000 | [diff] [blame] | 4859 | if (!PointeeTy->isIncompleteType() && |
| 4860 | (Context.getTypeSize(PointeeTy) == Context.getCharWidth())) |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4861 | ActionIdx = 2; // If the pointee's size is sizeof(char), |
| 4862 | // suggest an explicit length. |
Anna Zaks | 869aecc | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 4863 | |
| 4864 | // If the function is defined as a builtin macro, do not show macro |
| 4865 | // expansion. |
| 4866 | SourceLocation SL = SizeOfArg->getExprLoc(); |
| 4867 | SourceRange DSR = Dest->getSourceRange(); |
| 4868 | SourceRange SSR = SizeOfArg->getSourceRange(); |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4869 | SourceManager &SM = getSourceManager(); |
Anna Zaks | 869aecc | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 4870 | |
| 4871 | if (SM.isMacroArgExpansion(SL)) { |
| 4872 | ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts); |
| 4873 | SL = SM.getSpellingLoc(SL); |
| 4874 | DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()), |
| 4875 | SM.getSpellingLoc(DSR.getEnd())); |
| 4876 | SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()), |
| 4877 | SM.getSpellingLoc(SSR.getEnd())); |
| 4878 | } |
| 4879 | |
Anna Zaks | d08d915 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 4880 | DiagRuntimeBehavior(SL, SizeOfArg, |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4881 | PDiag(diag::warn_sizeof_pointer_expr_memaccess) |
Anna Zaks | 869aecc | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 4882 | << ReadableName |
Anna Zaks | d08d915 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 4883 | << PointeeTy |
| 4884 | << DestTy |
Anna Zaks | 869aecc | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 4885 | << DSR |
Anna Zaks | d08d915 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 4886 | << SSR); |
| 4887 | DiagRuntimeBehavior(SL, SizeOfArg, |
| 4888 | PDiag(diag::warn_sizeof_pointer_expr_memaccess_note) |
| 4889 | << ActionIdx |
| 4890 | << SSR); |
| 4891 | |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 4892 | break; |
| 4893 | } |
| 4894 | } |
| 4895 | |
| 4896 | // Also check for cases where the sizeof argument is the exact same |
| 4897 | // type as the memory argument, and where it points to a user-defined |
| 4898 | // record type. |
| 4899 | if (SizeOfArgTy != QualType()) { |
| 4900 | if (PointeeTy->isRecordType() && |
| 4901 | Context.typesAreCompatible(SizeOfArgTy, DestTy)) { |
| 4902 | DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest, |
| 4903 | PDiag(diag::warn_sizeof_pointer_type_memaccess) |
| 4904 | << FnName << SizeOfArgTy << ArgIdx |
| 4905 | << PointeeTy << Dest->getSourceRange() |
| 4906 | << LenExpr->getSourceRange()); |
| 4907 | break; |
| 4908 | } |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 4909 | } |
Nico Weber | bac8b6b | 2015-03-21 17:56:44 +0000 | [diff] [blame] | 4910 | } else if (DestTy->isArrayType()) { |
| 4911 | PointeeTy = DestTy; |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 4912 | } |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 4913 | |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 4914 | if (PointeeTy == QualType()) |
| 4915 | continue; |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 4916 | |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 4917 | // Always complain about dynamic classes. |
| 4918 | bool IsContained; |
| 4919 | if (const CXXRecordDecl *ContainedRD = |
| 4920 | getContainedDynamicClass(PointeeTy, IsContained)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4921 | |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 4922 | unsigned OperationType = 0; |
| 4923 | // "overwritten" if we're warning about the destination for any call |
| 4924 | // but memcmp; otherwise a verb appropriate to the call. |
| 4925 | if (ArgIdx != 0 || BId == Builtin::BImemcmp) { |
| 4926 | if (BId == Builtin::BImemcpy) |
| 4927 | OperationType = 1; |
| 4928 | else if(BId == Builtin::BImemmove) |
| 4929 | OperationType = 2; |
| 4930 | else if (BId == Builtin::BImemcmp) |
| 4931 | OperationType = 3; |
| 4932 | } |
| 4933 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4934 | DiagRuntimeBehavior( |
| 4935 | Dest->getExprLoc(), Dest, |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 4936 | PDiag(diag::warn_dyn_class_memaccess) |
| 4937 | << (BId == Builtin::BImemcmp ? ArgIdx + 2 : ArgIdx) |
| 4938 | << FnName << IsContained << ContainedRD << OperationType |
| 4939 | << Call->getCallee()->getSourceRange()); |
| 4940 | } else if (PointeeTy.hasNonTrivialObjCLifetime() && |
| 4941 | BId != Builtin::BImemset) |
| 4942 | DiagRuntimeBehavior( |
| 4943 | Dest->getExprLoc(), Dest, |
| 4944 | PDiag(diag::warn_arc_object_memaccess) |
| 4945 | << ArgIdx << FnName << PointeeTy |
| 4946 | << Call->getCallee()->getSourceRange()); |
| 4947 | else |
| 4948 | continue; |
| 4949 | |
| 4950 | DiagRuntimeBehavior( |
| 4951 | Dest->getExprLoc(), Dest, |
| 4952 | PDiag(diag::note_bad_memaccess_silence) |
| 4953 | << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)")); |
| 4954 | break; |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 4955 | } |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 4956 | |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 4957 | } |
| 4958 | |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 4959 | // A little helper routine: ignore addition and subtraction of integer literals. |
| 4960 | // This intentionally does not ignore all integer constant expressions because |
| 4961 | // we don't want to remove sizeof(). |
| 4962 | static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) { |
| 4963 | Ex = Ex->IgnoreParenCasts(); |
| 4964 | |
| 4965 | for (;;) { |
| 4966 | const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex); |
| 4967 | if (!BO || !BO->isAdditiveOp()) |
| 4968 | break; |
| 4969 | |
| 4970 | const Expr *RHS = BO->getRHS()->IgnoreParenCasts(); |
| 4971 | const Expr *LHS = BO->getLHS()->IgnoreParenCasts(); |
| 4972 | |
| 4973 | if (isa<IntegerLiteral>(RHS)) |
| 4974 | Ex = LHS; |
| 4975 | else if (isa<IntegerLiteral>(LHS)) |
| 4976 | Ex = RHS; |
| 4977 | else |
| 4978 | break; |
| 4979 | } |
| 4980 | |
| 4981 | return Ex; |
| 4982 | } |
| 4983 | |
Anna Zaks | 13b0857 | 2012-08-08 21:42:23 +0000 | [diff] [blame] | 4984 | static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty, |
| 4985 | ASTContext &Context) { |
| 4986 | // Only handle constant-sized or VLAs, but not flexible members. |
| 4987 | if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) { |
| 4988 | // Only issue the FIXIT for arrays of size > 1. |
| 4989 | if (CAT->getSize().getSExtValue() <= 1) |
| 4990 | return false; |
| 4991 | } else if (!Ty->isVariableArrayType()) { |
| 4992 | return false; |
| 4993 | } |
| 4994 | return true; |
| 4995 | } |
| 4996 | |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 4997 | // Warn if the user has made the 'size' argument to strlcpy or strlcat |
| 4998 | // be the size of the source, instead of the destination. |
| 4999 | void Sema::CheckStrlcpycatArguments(const CallExpr *Call, |
| 5000 | IdentifierInfo *FnName) { |
| 5001 | |
| 5002 | // Don't crash if the user has the wrong number of arguments |
Fariborz Jahanian | ab4fe98 | 2014-09-12 18:44:36 +0000 | [diff] [blame] | 5003 | unsigned NumArgs = Call->getNumArgs(); |
| 5004 | if ((NumArgs != 3) && (NumArgs != 4)) |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 5005 | return; |
| 5006 | |
| 5007 | const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context); |
| 5008 | const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5009 | const Expr *CompareWithSrc = nullptr; |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 5010 | |
| 5011 | if (CheckMemorySizeofForComparison(*this, SizeArg, FnName, |
| 5012 | Call->getLocStart(), Call->getRParenLoc())) |
| 5013 | return; |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 5014 | |
| 5015 | // Look for 'strlcpy(dst, x, sizeof(x))' |
| 5016 | if (const Expr *Ex = getSizeOfExprArg(SizeArg)) |
| 5017 | CompareWithSrc = Ex; |
| 5018 | else { |
| 5019 | // Look for 'strlcpy(dst, x, strlen(x))' |
| 5020 | if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) { |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 5021 | if (SizeCall->getBuiltinCallee() == Builtin::BIstrlen && |
| 5022 | SizeCall->getNumArgs() == 1) |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 5023 | CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context); |
| 5024 | } |
| 5025 | } |
| 5026 | |
| 5027 | if (!CompareWithSrc) |
| 5028 | return; |
| 5029 | |
| 5030 | // Determine if the argument to sizeof/strlen is equal to the source |
| 5031 | // argument. In principle there's all kinds of things you could do |
| 5032 | // here, for instance creating an == expression and evaluating it with |
| 5033 | // EvaluateAsBooleanCondition, but this uses a more direct technique: |
| 5034 | const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg); |
| 5035 | if (!SrcArgDRE) |
| 5036 | return; |
| 5037 | |
| 5038 | const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc); |
| 5039 | if (!CompareWithSrcDRE || |
| 5040 | SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl()) |
| 5041 | return; |
| 5042 | |
| 5043 | const Expr *OriginalSizeArg = Call->getArg(2); |
| 5044 | Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size) |
| 5045 | << OriginalSizeArg->getSourceRange() << FnName; |
| 5046 | |
| 5047 | // Output a FIXIT hint if the destination is an array (rather than a |
| 5048 | // pointer to an array). This could be enhanced to handle some |
| 5049 | // pointers if we know the actual size, like if DstArg is 'array+2' |
| 5050 | // we could say 'sizeof(array)-2'. |
| 5051 | const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts(); |
Anna Zaks | 13b0857 | 2012-08-08 21:42:23 +0000 | [diff] [blame] | 5052 | if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context)) |
Ted Kremenek | 18db5d4 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 5053 | return; |
Ted Kremenek | 18db5d4 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 5054 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 5055 | SmallString<128> sizeString; |
Ted Kremenek | 18db5d4 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 5056 | llvm::raw_svector_ostream OS(sizeString); |
| 5057 | OS << "sizeof("; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5058 | DstArg->printPretty(OS, nullptr, getPrintingPolicy()); |
Ted Kremenek | 18db5d4 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 5059 | OS << ")"; |
| 5060 | |
| 5061 | Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size) |
| 5062 | << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(), |
| 5063 | OS.str()); |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 5064 | } |
| 5065 | |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5066 | /// Check if two expressions refer to the same declaration. |
| 5067 | static bool referToTheSameDecl(const Expr *E1, const Expr *E2) { |
| 5068 | if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1)) |
| 5069 | if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2)) |
| 5070 | return D1->getDecl() == D2->getDecl(); |
| 5071 | return false; |
| 5072 | } |
| 5073 | |
| 5074 | static const Expr *getStrlenExprArg(const Expr *E) { |
| 5075 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { |
| 5076 | const FunctionDecl *FD = CE->getDirectCallee(); |
| 5077 | if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5078 | return nullptr; |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5079 | return CE->getArg(0)->IgnoreParenCasts(); |
| 5080 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5081 | return nullptr; |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5082 | } |
| 5083 | |
| 5084 | // Warn on anti-patterns as the 'size' argument to strncat. |
| 5085 | // The correct size argument should look like following: |
| 5086 | // strncat(dst, src, sizeof(dst) - strlen(dest) - 1); |
| 5087 | void Sema::CheckStrncatArguments(const CallExpr *CE, |
| 5088 | IdentifierInfo *FnName) { |
| 5089 | // Don't crash if the user has the wrong number of arguments. |
| 5090 | if (CE->getNumArgs() < 3) |
| 5091 | return; |
| 5092 | const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts(); |
| 5093 | const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts(); |
| 5094 | const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts(); |
| 5095 | |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 5096 | if (CheckMemorySizeofForComparison(*this, LenArg, FnName, CE->getLocStart(), |
| 5097 | CE->getRParenLoc())) |
| 5098 | return; |
| 5099 | |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5100 | // Identify common expressions, which are wrongly used as the size argument |
| 5101 | // to strncat and may lead to buffer overflows. |
| 5102 | unsigned PatternType = 0; |
| 5103 | if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) { |
| 5104 | // - sizeof(dst) |
| 5105 | if (referToTheSameDecl(SizeOfArg, DstArg)) |
| 5106 | PatternType = 1; |
| 5107 | // - sizeof(src) |
| 5108 | else if (referToTheSameDecl(SizeOfArg, SrcArg)) |
| 5109 | PatternType = 2; |
| 5110 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) { |
| 5111 | if (BE->getOpcode() == BO_Sub) { |
| 5112 | const Expr *L = BE->getLHS()->IgnoreParenCasts(); |
| 5113 | const Expr *R = BE->getRHS()->IgnoreParenCasts(); |
| 5114 | // - sizeof(dst) - strlen(dst) |
| 5115 | if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) && |
| 5116 | referToTheSameDecl(DstArg, getStrlenExprArg(R))) |
| 5117 | PatternType = 1; |
| 5118 | // - sizeof(src) - (anything) |
| 5119 | else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L))) |
| 5120 | PatternType = 2; |
| 5121 | } |
| 5122 | } |
| 5123 | |
| 5124 | if (PatternType == 0) |
| 5125 | return; |
| 5126 | |
Anna Zaks | 5069aa3 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 5127 | // Generate the diagnostic. |
| 5128 | SourceLocation SL = LenArg->getLocStart(); |
| 5129 | SourceRange SR = LenArg->getSourceRange(); |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 5130 | SourceManager &SM = getSourceManager(); |
Anna Zaks | 5069aa3 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 5131 | |
| 5132 | // If the function is defined as a builtin macro, do not show macro expansion. |
| 5133 | if (SM.isMacroArgExpansion(SL)) { |
| 5134 | SL = SM.getSpellingLoc(SL); |
| 5135 | SR = SourceRange(SM.getSpellingLoc(SR.getBegin()), |
| 5136 | SM.getSpellingLoc(SR.getEnd())); |
| 5137 | } |
| 5138 | |
Anna Zaks | 13b0857 | 2012-08-08 21:42:23 +0000 | [diff] [blame] | 5139 | // Check if the destination is an array (rather than a pointer to an array). |
| 5140 | QualType DstTy = DstArg->getType(); |
| 5141 | bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy, |
| 5142 | Context); |
| 5143 | if (!isKnownSizeArray) { |
| 5144 | if (PatternType == 1) |
| 5145 | Diag(SL, diag::warn_strncat_wrong_size) << SR; |
| 5146 | else |
| 5147 | Diag(SL, diag::warn_strncat_src_size) << SR; |
| 5148 | return; |
| 5149 | } |
| 5150 | |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5151 | if (PatternType == 1) |
Anna Zaks | 5069aa3 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 5152 | Diag(SL, diag::warn_strncat_large_size) << SR; |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5153 | else |
Anna Zaks | 5069aa3 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 5154 | Diag(SL, diag::warn_strncat_src_size) << SR; |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5155 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 5156 | SmallString<128> sizeString; |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5157 | llvm::raw_svector_ostream OS(sizeString); |
| 5158 | OS << "sizeof("; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5159 | DstArg->printPretty(OS, nullptr, getPrintingPolicy()); |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5160 | OS << ") - "; |
| 5161 | OS << "strlen("; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5162 | DstArg->printPretty(OS, nullptr, getPrintingPolicy()); |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5163 | OS << ") - 1"; |
| 5164 | |
Anna Zaks | 5069aa3 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 5165 | Diag(SL, diag::note_strncat_wrong_size) |
| 5166 | << FixItHint::CreateReplacement(SR, OS.str()); |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5167 | } |
| 5168 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5169 | //===--- CHECK: Return Address of Stack Variable --------------------------===// |
| 5170 | |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5171 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 5172 | Decl *ParentDecl); |
| 5173 | static Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 5174 | Decl *ParentDecl); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5175 | |
| 5176 | /// CheckReturnStackAddr - Check if a return statement returns the address |
| 5177 | /// of a stack variable. |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 5178 | static void |
| 5179 | CheckReturnStackAddr(Sema &S, Expr *RetValExp, QualType lhsType, |
| 5180 | SourceLocation ReturnLoc) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5181 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5182 | Expr *stackE = nullptr; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5183 | SmallVector<DeclRefExpr *, 8> refVars; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5184 | |
| 5185 | // Perform checking for returned stack addresses, local blocks, |
| 5186 | // label addresses or references to temporaries. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5187 | if (lhsType->isPointerType() || |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 5188 | (!S.getLangOpts().ObjCAutoRefCount && lhsType->isBlockPointerType())) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5189 | stackE = EvalAddr(RetValExp, refVars, /*ParentDecl=*/nullptr); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 5190 | } else if (lhsType->isReferenceType()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5191 | stackE = EvalVal(RetValExp, refVars, /*ParentDecl=*/nullptr); |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5192 | } |
| 5193 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5194 | if (!stackE) |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5195 | return; // Nothing suspicious was found. |
| 5196 | |
| 5197 | SourceLocation diagLoc; |
| 5198 | SourceRange diagRange; |
| 5199 | if (refVars.empty()) { |
| 5200 | diagLoc = stackE->getLocStart(); |
| 5201 | diagRange = stackE->getSourceRange(); |
| 5202 | } else { |
| 5203 | // We followed through a reference variable. 'stackE' contains the |
| 5204 | // problematic expression but we will warn at the return statement pointing |
| 5205 | // at the reference variable. We will later display the "trail" of |
| 5206 | // reference variables using notes. |
| 5207 | diagLoc = refVars[0]->getLocStart(); |
| 5208 | diagRange = refVars[0]->getSourceRange(); |
| 5209 | } |
| 5210 | |
| 5211 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var. |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 5212 | S.Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5213 | : diag::warn_ret_stack_addr) |
| 5214 | << DR->getDecl()->getDeclName() << diagRange; |
| 5215 | } else if (isa<BlockExpr>(stackE)) { // local block. |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 5216 | S.Diag(diagLoc, diag::err_ret_local_block) << diagRange; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5217 | } else if (isa<AddrLabelExpr>(stackE)) { // address of label. |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 5218 | S.Diag(diagLoc, diag::warn_ret_addr_label) << diagRange; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5219 | } else { // local temporary. |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 5220 | S.Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref |
| 5221 | : diag::warn_ret_local_temp_addr) |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5222 | << diagRange; |
| 5223 | } |
| 5224 | |
| 5225 | // Display the "trail" of reference variables that we followed until we |
| 5226 | // found the problematic expression using notes. |
| 5227 | for (unsigned i = 0, e = refVars.size(); i != e; ++i) { |
| 5228 | VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl()); |
| 5229 | // If this var binds to another reference var, show the range of the next |
| 5230 | // var, otherwise the var binds to the problematic expression, in which case |
| 5231 | // show the range of the expression. |
| 5232 | SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange() |
| 5233 | : stackE->getSourceRange(); |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 5234 | S.Diag(VD->getLocation(), diag::note_ref_var_local_bind) |
| 5235 | << VD->getDeclName() << range; |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5236 | } |
| 5237 | } |
| 5238 | |
| 5239 | /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that |
| 5240 | /// check if the expression in a return statement evaluates to an address |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5241 | /// to a location on the stack, a local block, an address of a label, or a |
| 5242 | /// reference to local temporary. The recursion is used to traverse the |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5243 | /// AST of the return expression, with recursion backtracking when we |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5244 | /// encounter a subexpression that (1) clearly does not lead to one of the |
| 5245 | /// above problematic expressions (2) is something we cannot determine leads to |
| 5246 | /// a problematic expression based on such local checking. |
| 5247 | /// |
| 5248 | /// Both EvalAddr and EvalVal follow through reference variables to evaluate |
| 5249 | /// the expression that they point to. Such variables are added to the |
| 5250 | /// 'refVars' vector so that we know what the reference variable "trail" was. |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5251 | /// |
Ted Kremenek | e07a8cd | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 5252 | /// EvalAddr processes expressions that are pointers that are used as |
| 5253 | /// references (and not L-values). EvalVal handles all other values. |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5254 | /// At the base case of the recursion is a check for the above problematic |
| 5255 | /// expressions. |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5256 | /// |
| 5257 | /// This implementation handles: |
| 5258 | /// |
| 5259 | /// * pointer-to-pointer casts |
| 5260 | /// * implicit conversions from array references to pointers |
| 5261 | /// * taking the address of fields |
| 5262 | /// * arbitrary interplay between "&" and "*" operators |
| 5263 | /// * pointer arithmetic from an address of a stack variable |
| 5264 | /// * 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] | 5265 | static Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 5266 | Decl *ParentDecl) { |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5267 | if (E->isTypeDependent()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5268 | return nullptr; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5269 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5270 | // We should only be called for evaluating pointer expressions. |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5271 | assert((E->getType()->isAnyPointerType() || |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 5272 | E->getType()->isBlockPointerType() || |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 5273 | E->getType()->isObjCQualifiedIdType()) && |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5274 | "EvalAddr only works on pointers"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5275 | |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 5276 | E = E->IgnoreParens(); |
| 5277 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5278 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 5279 | // viewed AST node. We then recursively traverse the AST by calling |
| 5280 | // EvalAddr and EvalVal appropriately. |
| 5281 | switch (E->getStmtClass()) { |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5282 | case Stmt::DeclRefExprClass: { |
| 5283 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
| 5284 | |
Richard Smith | 40f08eb | 2014-01-30 22:05:38 +0000 | [diff] [blame] | 5285 | // If we leave the immediate function, the lifetime isn't about to end. |
Alexey Bataev | 19acc3d | 2015-01-12 10:17:46 +0000 | [diff] [blame] | 5286 | if (DR->refersToEnclosingVariableOrCapture()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5287 | return nullptr; |
Richard Smith | 40f08eb | 2014-01-30 22:05:38 +0000 | [diff] [blame] | 5288 | |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5289 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
| 5290 | // If this is a reference variable, follow through to the expression that |
| 5291 | // it points to. |
| 5292 | if (V->hasLocalStorage() && |
| 5293 | V->getType()->isReferenceType() && V->hasInit()) { |
| 5294 | // Add the reference variable to the "trail". |
| 5295 | refVars.push_back(DR); |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5296 | return EvalAddr(V->getInit(), refVars, ParentDecl); |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5297 | } |
| 5298 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5299 | return nullptr; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5300 | } |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5301 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5302 | case Stmt::UnaryOperatorClass: { |
| 5303 | // The only unary operator that make sense to handle here |
| 5304 | // is AddrOf. All others don't make sense as pointers. |
| 5305 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5306 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5307 | if (U->getOpcode() == UO_AddrOf) |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5308 | return EvalVal(U->getSubExpr(), refVars, ParentDecl); |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5309 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5310 | return nullptr; |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5311 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5312 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5313 | case Stmt::BinaryOperatorClass: { |
| 5314 | // Handle pointer arithmetic. All other binary operators are not valid |
| 5315 | // in this context. |
| 5316 | BinaryOperator *B = cast<BinaryOperator>(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5317 | BinaryOperatorKind op = B->getOpcode(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5318 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5319 | if (op != BO_Add && op != BO_Sub) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5320 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5321 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5322 | Expr *Base = B->getLHS(); |
| 5323 | |
| 5324 | // Determine which argument is the real pointer base. It could be |
| 5325 | // the RHS argument instead of the LHS. |
| 5326 | if (!Base->getType()->isPointerType()) Base = B->getRHS(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5327 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5328 | assert (Base->getType()->isPointerType()); |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5329 | return EvalAddr(Base, refVars, ParentDecl); |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5330 | } |
Steve Naroff | 2752a17 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 5331 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5332 | // For conditional operators we need to see if either the LHS or RHS are |
| 5333 | // valid DeclRefExpr*s. If one of them is valid, we return it. |
| 5334 | case Stmt::ConditionalOperatorClass: { |
| 5335 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5336 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5337 | // Handle the GNU extension for missing LHS. |
Richard Smith | 6a6a4bb | 2014-01-27 04:19:56 +0000 | [diff] [blame] | 5338 | // FIXME: That isn't a ConditionalOperator, so doesn't get here. |
| 5339 | if (Expr *LHSExpr = C->getLHS()) { |
| 5340 | // In C++, we can have a throw-expression, which has 'void' type. |
| 5341 | if (!LHSExpr->getType()->isVoidType()) |
| 5342 | if (Expr *LHS = EvalAddr(LHSExpr, refVars, ParentDecl)) |
Douglas Gregor | 270b2ef | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 5343 | return LHS; |
| 5344 | } |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5345 | |
Douglas Gregor | 270b2ef | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 5346 | // In C++, we can have a throw-expression, which has 'void' type. |
| 5347 | if (C->getRHS()->getType()->isVoidType()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5348 | return nullptr; |
Douglas Gregor | 270b2ef | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 5349 | |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5350 | return EvalAddr(C->getRHS(), refVars, ParentDecl); |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5351 | } |
Richard Smith | 6a6a4bb | 2014-01-27 04:19:56 +0000 | [diff] [blame] | 5352 | |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5353 | case Stmt::BlockExprClass: |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 5354 | if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures()) |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5355 | return E; // local block. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5356 | return nullptr; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5357 | |
| 5358 | case Stmt::AddrLabelExprClass: |
| 5359 | return E; // address of label. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5360 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 5361 | case Stmt::ExprWithCleanupsClass: |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5362 | return EvalAddr(cast<ExprWithCleanups>(E)->getSubExpr(), refVars, |
| 5363 | ParentDecl); |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 5364 | |
Ted Kremenek | c3b4c52 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 5365 | // For casts, we need to handle conversions from arrays to |
| 5366 | // pointer values, and pointer-to-pointer conversions. |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 5367 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 5368 | case Stmt::CStyleCastExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5369 | case Stmt::CXXFunctionalCastExprClass: |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 5370 | case Stmt::ObjCBridgedCastExprClass: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5371 | case Stmt::CXXStaticCastExprClass: |
| 5372 | case Stmt::CXXDynamicCastExprClass: |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 5373 | case Stmt::CXXConstCastExprClass: |
| 5374 | case Stmt::CXXReinterpretCastExprClass: { |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 5375 | Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
| 5376 | switch (cast<CastExpr>(E)->getCastKind()) { |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 5377 | case CK_LValueToRValue: |
| 5378 | case CK_NoOp: |
| 5379 | case CK_BaseToDerived: |
| 5380 | case CK_DerivedToBase: |
| 5381 | case CK_UncheckedDerivedToBase: |
| 5382 | case CK_Dynamic: |
| 5383 | case CK_CPointerToObjCPointerCast: |
| 5384 | case CK_BlockPointerToObjCPointerCast: |
| 5385 | case CK_AnyPointerToBlockPointerCast: |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5386 | return EvalAddr(SubExpr, refVars, ParentDecl); |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 5387 | |
| 5388 | case CK_ArrayToPointerDecay: |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5389 | return EvalVal(SubExpr, refVars, ParentDecl); |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 5390 | |
Richard Trieu | dadefde | 2014-07-02 04:39:38 +0000 | [diff] [blame] | 5391 | case CK_BitCast: |
| 5392 | if (SubExpr->getType()->isAnyPointerType() || |
| 5393 | SubExpr->getType()->isBlockPointerType() || |
| 5394 | SubExpr->getType()->isObjCQualifiedIdType()) |
| 5395 | return EvalAddr(SubExpr, refVars, ParentDecl); |
| 5396 | else |
| 5397 | return nullptr; |
| 5398 | |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 5399 | default: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5400 | return nullptr; |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 5401 | } |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5402 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5403 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5404 | case Stmt::MaterializeTemporaryExprClass: |
| 5405 | if (Expr *Result = EvalAddr( |
| 5406 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5407 | refVars, ParentDecl)) |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5408 | return Result; |
| 5409 | |
| 5410 | return E; |
| 5411 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5412 | // Everything else: we simply don't reason about them. |
| 5413 | default: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5414 | return nullptr; |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5415 | } |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5416 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5417 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5418 | |
| 5419 | /// EvalVal - This function is complements EvalAddr in the mutual recursion. |
| 5420 | /// See the comments for EvalAddr for more details. |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5421 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 5422 | Decl *ParentDecl) { |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 5423 | do { |
Ted Kremenek | e07a8cd | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 5424 | // We should only be called for evaluating non-pointer expressions, or |
| 5425 | // expressions with a pointer type that are not used as references but instead |
| 5426 | // are l-values (e.g., DeclRefExpr with a pointer type). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5427 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5428 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 5429 | // viewed AST node. We then recursively traverse the AST by calling |
| 5430 | // EvalAddr and EvalVal appropriately. |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 5431 | |
| 5432 | E = E->IgnoreParens(); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5433 | switch (E->getStmtClass()) { |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 5434 | case Stmt::ImplicitCastExprClass: { |
| 5435 | ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E); |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5436 | if (IE->getValueKind() == VK_LValue) { |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 5437 | E = IE->getSubExpr(); |
| 5438 | continue; |
| 5439 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5440 | return nullptr; |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 5441 | } |
| 5442 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 5443 | case Stmt::ExprWithCleanupsClass: |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5444 | return EvalVal(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,ParentDecl); |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 5445 | |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5446 | case Stmt::DeclRefExprClass: { |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5447 | // When we hit a DeclRefExpr we are looking at code that refers to a |
| 5448 | // variable's name. If it's not a reference variable we check if it has |
| 5449 | // local storage within the function, and if so, return the expression. |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5450 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5451 | |
Richard Smith | 40f08eb | 2014-01-30 22:05:38 +0000 | [diff] [blame] | 5452 | // If we leave the immediate function, the lifetime isn't about to end. |
Alexey Bataev | 19acc3d | 2015-01-12 10:17:46 +0000 | [diff] [blame] | 5453 | if (DR->refersToEnclosingVariableOrCapture()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5454 | return nullptr; |
Richard Smith | 40f08eb | 2014-01-30 22:05:38 +0000 | [diff] [blame] | 5455 | |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5456 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) { |
| 5457 | // Check if it refers to itself, e.g. "int& i = i;". |
| 5458 | if (V == ParentDecl) |
| 5459 | return DR; |
| 5460 | |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5461 | if (V->hasLocalStorage()) { |
| 5462 | if (!V->getType()->isReferenceType()) |
| 5463 | return DR; |
| 5464 | |
| 5465 | // Reference variable, follow through to the expression that |
| 5466 | // it points to. |
| 5467 | if (V->hasInit()) { |
| 5468 | // Add the reference variable to the "trail". |
| 5469 | refVars.push_back(DR); |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5470 | return EvalVal(V->getInit(), refVars, V); |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5471 | } |
| 5472 | } |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5473 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5474 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5475 | return nullptr; |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5476 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5477 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5478 | case Stmt::UnaryOperatorClass: { |
| 5479 | // The only unary operator that make sense to handle here |
| 5480 | // is Deref. All others don't resolve to a "name." This includes |
| 5481 | // handling all sorts of rvalues passed to a unary operator. |
| 5482 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5483 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5484 | if (U->getOpcode() == UO_Deref) |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5485 | return EvalAddr(U->getSubExpr(), refVars, ParentDecl); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5486 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5487 | return nullptr; |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5488 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5489 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5490 | case Stmt::ArraySubscriptExprClass: { |
| 5491 | // Array subscripts are potential references to data on the stack. We |
| 5492 | // retrieve the DeclRefExpr* for the array variable if it indeed |
| 5493 | // has local storage. |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5494 | return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars,ParentDecl); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5495 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5496 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5497 | case Stmt::ConditionalOperatorClass: { |
| 5498 | // 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] | 5499 | // non-NULL Expr's. If one is non-NULL, we return it. |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5500 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 5501 | |
Anders Carlsson | 801c5c7 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 5502 | // Handle the GNU extension for missing LHS. |
Richard Smith | 6a6a4bb | 2014-01-27 04:19:56 +0000 | [diff] [blame] | 5503 | if (Expr *LHSExpr = C->getLHS()) { |
| 5504 | // In C++, we can have a throw-expression, which has 'void' type. |
| 5505 | if (!LHSExpr->getType()->isVoidType()) |
| 5506 | if (Expr *LHS = EvalVal(LHSExpr, refVars, ParentDecl)) |
| 5507 | return LHS; |
| 5508 | } |
| 5509 | |
| 5510 | // In C++, we can have a throw-expression, which has 'void' type. |
| 5511 | if (C->getRHS()->getType()->isVoidType()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5512 | return nullptr; |
Anders Carlsson | 801c5c7 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 5513 | |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5514 | return EvalVal(C->getRHS(), refVars, ParentDecl); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5515 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5516 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5517 | // Accesses to members are potential references to data on the stack. |
Douglas Gregor | f405d7e | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 5518 | case Stmt::MemberExprClass: { |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5519 | MemberExpr *M = cast<MemberExpr>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5520 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5521 | // Check for indirect access. We only want direct field accesses. |
Ted Kremenek | cbe6b0b | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 5522 | if (M->isArrow()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5523 | return nullptr; |
Ted Kremenek | cbe6b0b | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 5524 | |
| 5525 | // Check whether the member type is itself a reference, in which case |
| 5526 | // we're not going to refer to the member, but to what the member refers to. |
| 5527 | if (M->getMemberDecl()->getType()->isReferenceType()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5528 | return nullptr; |
Ted Kremenek | cbe6b0b | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 5529 | |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5530 | return EvalVal(M->getBase(), refVars, ParentDecl); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5531 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5532 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5533 | case Stmt::MaterializeTemporaryExprClass: |
| 5534 | if (Expr *Result = EvalVal( |
| 5535 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5536 | refVars, ParentDecl)) |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5537 | return Result; |
| 5538 | |
| 5539 | return E; |
| 5540 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5541 | default: |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5542 | // Check that we don't return or take the address of a reference to a |
| 5543 | // temporary. This is only useful in C++. |
| 5544 | if (!E->isTypeDependent() && E->isRValue()) |
| 5545 | return E; |
| 5546 | |
| 5547 | // Everything else: we simply don't reason about them. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5548 | return nullptr; |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5549 | } |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 5550 | } while (true); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5551 | } |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 5552 | |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 5553 | void |
| 5554 | Sema::CheckReturnValExpr(Expr *RetValExp, QualType lhsType, |
| 5555 | SourceLocation ReturnLoc, |
| 5556 | bool isObjCMethod, |
Artyom Skrobov | 9f21344 | 2014-01-24 11:10:39 +0000 | [diff] [blame] | 5557 | const AttrVec *Attrs, |
| 5558 | const FunctionDecl *FD) { |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 5559 | CheckReturnStackAddr(*this, RetValExp, lhsType, ReturnLoc); |
| 5560 | |
| 5561 | // Check if the return value is null but should not be. |
Benjamin Kramer | ae852a6 | 2014-02-23 14:34:50 +0000 | [diff] [blame] | 5562 | if (Attrs && hasSpecificAttr<ReturnsNonNullAttr>(*Attrs) && |
| 5563 | CheckNonNullExpr(*this, RetValExp)) |
| 5564 | Diag(ReturnLoc, diag::warn_null_ret) |
| 5565 | << (isObjCMethod ? 1 : 0) << RetValExp->getSourceRange(); |
Artyom Skrobov | 9f21344 | 2014-01-24 11:10:39 +0000 | [diff] [blame] | 5566 | |
| 5567 | // C++11 [basic.stc.dynamic.allocation]p4: |
| 5568 | // If an allocation function declared with a non-throwing |
| 5569 | // exception-specification fails to allocate storage, it shall return |
| 5570 | // a null pointer. Any other allocation function that fails to allocate |
| 5571 | // storage shall indicate failure only by throwing an exception [...] |
| 5572 | if (FD) { |
| 5573 | OverloadedOperatorKind Op = FD->getOverloadedOperator(); |
| 5574 | if (Op == OO_New || Op == OO_Array_New) { |
| 5575 | const FunctionProtoType *Proto |
| 5576 | = FD->getType()->castAs<FunctionProtoType>(); |
| 5577 | if (!Proto->isNothrow(Context, /*ResultIfDependent*/true) && |
| 5578 | CheckNonNullExpr(*this, RetValExp)) |
| 5579 | Diag(ReturnLoc, diag::warn_operator_new_returns_null) |
| 5580 | << FD << getLangOpts().CPlusPlus11; |
| 5581 | } |
| 5582 | } |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 5583 | } |
| 5584 | |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 5585 | //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// |
| 5586 | |
| 5587 | /// Check for comparisons of floating point operands using != and ==. |
| 5588 | /// Issue a warning if these are no self-comparisons, as they are not likely |
| 5589 | /// to do what the programmer intended. |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5590 | void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5591 | Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts(); |
| 5592 | Expr* RightExprSansParen = RHS->IgnoreParenImpCasts(); |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 5593 | |
| 5594 | // Special case: check for x == x (which is OK). |
| 5595 | // Do not emit warnings for such cases. |
| 5596 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) |
| 5597 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) |
| 5598 | if (DRL->getDecl() == DRR->getDecl()) |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 5599 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5600 | |
| 5601 | |
Ted Kremenek | eda40e2 | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 5602 | // Special case: check for comparisons against literals that can be exactly |
| 5603 | // represented by APFloat. In such cases, do not emit a warning. This |
| 5604 | // is a heuristic: often comparison against such literals are used to |
| 5605 | // detect if a value in a variable has not changed. This clearly can |
| 5606 | // lead to false negatives. |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 5607 | if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { |
| 5608 | if (FLL->isExact()) |
| 5609 | return; |
| 5610 | } else |
| 5611 | if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)) |
| 5612 | if (FLR->isExact()) |
| 5613 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5614 | |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 5615 | // Check for comparisons with builtin types. |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 5616 | if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 5617 | if (CL->getBuiltinCallee()) |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 5618 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5619 | |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 5620 | if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 5621 | if (CR->getBuiltinCallee()) |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 5622 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5623 | |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 5624 | // Emit the diagnostic. |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 5625 | Diag(Loc, diag::warn_floatingpoint_eq) |
| 5626 | << LHS->getSourceRange() << RHS->getSourceRange(); |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 5627 | } |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 5628 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5629 | //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// |
| 5630 | //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 5631 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5632 | namespace { |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 5633 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5634 | /// Structure recording the 'active' range of an integer-valued |
| 5635 | /// expression. |
| 5636 | struct IntRange { |
| 5637 | /// The number of bits active in the int. |
| 5638 | unsigned Width; |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 5639 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5640 | /// True if the int is known not to have negative values. |
| 5641 | bool NonNegative; |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 5642 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5643 | IntRange(unsigned Width, bool NonNegative) |
| 5644 | : Width(Width), NonNegative(NonNegative) |
| 5645 | {} |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 5646 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 5647 | /// Returns the range of the bool type. |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5648 | static IntRange forBoolType() { |
| 5649 | return IntRange(1, true); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5650 | } |
| 5651 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 5652 | /// Returns the range of an opaque value of the given integral type. |
| 5653 | static IntRange forValueOfType(ASTContext &C, QualType T) { |
| 5654 | return forValueOfCanonicalType(C, |
| 5655 | T->getCanonicalTypeInternal().getTypePtr()); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5656 | } |
| 5657 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 5658 | /// Returns the range of an opaque value of a canonical integral type. |
| 5659 | static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5660 | assert(T->isCanonicalUnqualified()); |
| 5661 | |
| 5662 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 5663 | T = VT->getElementType().getTypePtr(); |
| 5664 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 5665 | T = CT->getElementType().getTypePtr(); |
Justin Bogner | 4f42fc4 | 2014-07-21 18:01:53 +0000 | [diff] [blame] | 5666 | if (const AtomicType *AT = dyn_cast<AtomicType>(T)) |
| 5667 | T = AT->getValueType().getTypePtr(); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5668 | |
David Majnemer | 6a42665 | 2013-06-07 22:07:20 +0000 | [diff] [blame] | 5669 | // For enum types, use the known bit width of the enumerators. |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5670 | if (const EnumType *ET = dyn_cast<EnumType>(T)) { |
David Majnemer | 6a42665 | 2013-06-07 22:07:20 +0000 | [diff] [blame] | 5671 | EnumDecl *Enum = ET->getDecl(); |
| 5672 | if (!Enum->isCompleteDefinition()) |
| 5673 | return IntRange(C.getIntWidth(QualType(T, 0)), false); |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 5674 | |
David Majnemer | 6a42665 | 2013-06-07 22:07:20 +0000 | [diff] [blame] | 5675 | unsigned NumPositive = Enum->getNumPositiveBits(); |
| 5676 | unsigned NumNegative = Enum->getNumNegativeBits(); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5677 | |
David Majnemer | 6a42665 | 2013-06-07 22:07:20 +0000 | [diff] [blame] | 5678 | if (NumNegative == 0) |
| 5679 | return IntRange(NumPositive, true/*NonNegative*/); |
| 5680 | else |
| 5681 | return IntRange(std::max(NumPositive + 1, NumNegative), |
| 5682 | false/*NonNegative*/); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5683 | } |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5684 | |
| 5685 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 5686 | assert(BT->isInteger()); |
| 5687 | |
| 5688 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 5689 | } |
| 5690 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 5691 | /// Returns the "target" range of a canonical integral type, i.e. |
| 5692 | /// the range of values expressible in the type. |
| 5693 | /// |
| 5694 | /// This matches forValueOfCanonicalType except that enums have the |
| 5695 | /// full range of their type, not the range of their enumerators. |
| 5696 | static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) { |
| 5697 | assert(T->isCanonicalUnqualified()); |
| 5698 | |
| 5699 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 5700 | T = VT->getElementType().getTypePtr(); |
| 5701 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 5702 | T = CT->getElementType().getTypePtr(); |
Justin Bogner | 4f42fc4 | 2014-07-21 18:01:53 +0000 | [diff] [blame] | 5703 | if (const AtomicType *AT = dyn_cast<AtomicType>(T)) |
| 5704 | T = AT->getValueType().getTypePtr(); |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 5705 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Douglas Gregor | 3168dcf | 2011-09-08 23:29:05 +0000 | [diff] [blame] | 5706 | T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr(); |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 5707 | |
| 5708 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 5709 | assert(BT->isInteger()); |
| 5710 | |
| 5711 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 5712 | } |
| 5713 | |
| 5714 | /// Returns the supremum of two ranges: i.e. their conservative merge. |
John McCall | ff96ccd | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 5715 | static IntRange join(IntRange L, IntRange R) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5716 | return IntRange(std::max(L.Width, R.Width), |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 5717 | L.NonNegative && R.NonNegative); |
| 5718 | } |
| 5719 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 5720 | /// Returns the infinum of two ranges: i.e. their aggressive merge. |
John McCall | ff96ccd | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 5721 | static IntRange meet(IntRange L, IntRange R) { |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 5722 | return IntRange(std::min(L.Width, R.Width), |
| 5723 | L.NonNegative || R.NonNegative); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5724 | } |
| 5725 | }; |
| 5726 | |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 5727 | static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, |
| 5728 | unsigned MaxWidth) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5729 | if (value.isSigned() && value.isNegative()) |
| 5730 | return IntRange(value.getMinSignedBits(), false); |
| 5731 | |
| 5732 | if (value.getBitWidth() > MaxWidth) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5733 | value = value.trunc(MaxWidth); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5734 | |
| 5735 | // isNonNegative() just checks the sign bit without considering |
| 5736 | // signedness. |
| 5737 | return IntRange(value.getActiveBits(), true); |
| 5738 | } |
| 5739 | |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 5740 | static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, |
| 5741 | unsigned MaxWidth) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5742 | if (result.isInt()) |
| 5743 | return GetValueRange(C, result.getInt(), MaxWidth); |
| 5744 | |
| 5745 | if (result.isVector()) { |
John McCall | 7443052 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 5746 | IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); |
| 5747 | for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { |
| 5748 | IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); |
| 5749 | R = IntRange::join(R, El); |
| 5750 | } |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5751 | return R; |
| 5752 | } |
| 5753 | |
| 5754 | if (result.isComplexInt()) { |
| 5755 | IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); |
| 5756 | IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); |
| 5757 | return IntRange::join(R, I); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5758 | } |
| 5759 | |
| 5760 | // This can happen with lossless casts to intptr_t of "based" lvalues. |
| 5761 | // Assume it might use arbitrary bits. |
John McCall | 7443052 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 5762 | // FIXME: The only reason we need to pass the type in here is to get |
| 5763 | // the sign right on this one case. It would be nice if APValue |
| 5764 | // preserved this. |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 5765 | assert(result.isLValue() || result.isAddrLabelDiff()); |
Douglas Gregor | 61b6e49 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 5766 | return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType()); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5767 | } |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5768 | |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5769 | static QualType GetExprType(Expr *E) { |
| 5770 | QualType Ty = E->getType(); |
| 5771 | if (const AtomicType *AtomicRHS = Ty->getAs<AtomicType>()) |
| 5772 | Ty = AtomicRHS->getValueType(); |
| 5773 | return Ty; |
| 5774 | } |
| 5775 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5776 | /// Pseudo-evaluate the given integer expression, estimating the |
| 5777 | /// range of values it might take. |
| 5778 | /// |
| 5779 | /// \param MaxWidth - the width to which the value will be truncated |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 5780 | static IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5781 | E = E->IgnoreParens(); |
| 5782 | |
| 5783 | // Try a full evaluation first. |
| 5784 | Expr::EvalResult result; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5785 | if (E->EvaluateAsRValue(result, C)) |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5786 | return GetValueRange(C, result.Val, GetExprType(E), MaxWidth); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5787 | |
| 5788 | // I think we only want to look through implicit casts here; if the |
| 5789 | // user has an explicit widening cast, we should treat the value as |
| 5790 | // being of the new, wider type. |
| 5791 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) { |
Eli Friedman | 8349dc1 | 2011-12-15 02:41:52 +0000 | [diff] [blame] | 5792 | if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue) |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5793 | return GetExprRange(C, CE->getSubExpr(), MaxWidth); |
| 5794 | |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5795 | IntRange OutputTypeRange = IntRange::forValueOfType(C, GetExprType(CE)); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5796 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5797 | bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast); |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 5798 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5799 | // 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] | 5800 | if (!isIntegerCast) |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5801 | return OutputTypeRange; |
| 5802 | |
| 5803 | IntRange SubRange |
| 5804 | = GetExprRange(C, CE->getSubExpr(), |
| 5805 | std::min(MaxWidth, OutputTypeRange.Width)); |
| 5806 | |
| 5807 | // Bail out if the subexpr's range is as wide as the cast type. |
| 5808 | if (SubRange.Width >= OutputTypeRange.Width) |
| 5809 | return OutputTypeRange; |
| 5810 | |
| 5811 | // Otherwise, we take the smaller width, and we're non-negative if |
| 5812 | // either the output type or the subexpr is. |
| 5813 | return IntRange(SubRange.Width, |
| 5814 | SubRange.NonNegative || OutputTypeRange.NonNegative); |
| 5815 | } |
| 5816 | |
| 5817 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 5818 | // If we can fold the condition, just take that operand. |
| 5819 | bool CondResult; |
| 5820 | if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) |
| 5821 | return GetExprRange(C, CondResult ? CO->getTrueExpr() |
| 5822 | : CO->getFalseExpr(), |
| 5823 | MaxWidth); |
| 5824 | |
| 5825 | // Otherwise, conservatively merge. |
| 5826 | IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth); |
| 5827 | IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth); |
| 5828 | return IntRange::join(L, R); |
| 5829 | } |
| 5830 | |
| 5831 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 5832 | switch (BO->getOpcode()) { |
| 5833 | |
| 5834 | // Boolean-valued operations are single-bit and positive. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5835 | case BO_LAnd: |
| 5836 | case BO_LOr: |
| 5837 | case BO_LT: |
| 5838 | case BO_GT: |
| 5839 | case BO_LE: |
| 5840 | case BO_GE: |
| 5841 | case BO_EQ: |
| 5842 | case BO_NE: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5843 | return IntRange::forBoolType(); |
| 5844 | |
John McCall | c368838 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 5845 | // The type of the assignments is the type of the LHS, so the RHS |
| 5846 | // is not necessarily the same type. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5847 | case BO_MulAssign: |
| 5848 | case BO_DivAssign: |
| 5849 | case BO_RemAssign: |
| 5850 | case BO_AddAssign: |
| 5851 | case BO_SubAssign: |
John McCall | c368838 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 5852 | case BO_XorAssign: |
| 5853 | case BO_OrAssign: |
| 5854 | // TODO: bitfields? |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5855 | return IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | ff96ccd | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 5856 | |
John McCall | c368838 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 5857 | // Simple assignments just pass through the RHS, which will have |
| 5858 | // been coerced to the LHS type. |
| 5859 | case BO_Assign: |
| 5860 | // TODO: bitfields? |
| 5861 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 5862 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5863 | // Operations with opaque sources are black-listed. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5864 | case BO_PtrMemD: |
| 5865 | case BO_PtrMemI: |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5866 | return IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5867 | |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 5868 | // Bitwise-and uses the *infinum* of the two source ranges. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5869 | case BO_And: |
| 5870 | case BO_AndAssign: |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 5871 | return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth), |
| 5872 | GetExprRange(C, BO->getRHS(), MaxWidth)); |
| 5873 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5874 | // Left shift gets black-listed based on a judgement call. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5875 | case BO_Shl: |
John McCall | 1bff993 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 5876 | // ...except that we want to treat '1 << (blah)' as logically |
| 5877 | // positive. It's an important idiom. |
| 5878 | if (IntegerLiteral *I |
| 5879 | = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { |
| 5880 | if (I->getValue() == 1) { |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5881 | IntRange R = IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | 1bff993 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 5882 | return IntRange(R.Width, /*NonNegative*/ true); |
| 5883 | } |
| 5884 | } |
| 5885 | // fallthrough |
| 5886 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5887 | case BO_ShlAssign: |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5888 | return IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5889 | |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 5890 | // Right shift by a constant can narrow its left argument. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5891 | case BO_Shr: |
| 5892 | case BO_ShrAssign: { |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 5893 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 5894 | |
| 5895 | // If the shift amount is a positive constant, drop the width by |
| 5896 | // that much. |
| 5897 | llvm::APSInt shift; |
| 5898 | if (BO->getRHS()->isIntegerConstantExpr(shift, C) && |
| 5899 | shift.isNonNegative()) { |
| 5900 | unsigned zext = shift.getZExtValue(); |
| 5901 | if (zext >= L.Width) |
| 5902 | L.Width = (L.NonNegative ? 0 : 1); |
| 5903 | else |
| 5904 | L.Width -= zext; |
| 5905 | } |
| 5906 | |
| 5907 | return L; |
| 5908 | } |
| 5909 | |
| 5910 | // Comma acts as its right operand. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5911 | case BO_Comma: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5912 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 5913 | |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 5914 | // Black-list pointer subtractions. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5915 | case BO_Sub: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5916 | if (BO->getLHS()->getType()->isPointerType()) |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5917 | return IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | 5143181 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 5918 | break; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 5919 | |
John McCall | 5143181 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 5920 | // The width of a division result is mostly determined by the size |
| 5921 | // of the LHS. |
| 5922 | case BO_Div: { |
| 5923 | // Don't 'pre-truncate' the operands. |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5924 | unsigned opWidth = C.getIntWidth(GetExprType(E)); |
John McCall | 5143181 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 5925 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); |
| 5926 | |
| 5927 | // If the divisor is constant, use that. |
| 5928 | llvm::APSInt divisor; |
| 5929 | if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) { |
| 5930 | unsigned log2 = divisor.logBase2(); // floor(log_2(divisor)) |
| 5931 | if (log2 >= L.Width) |
| 5932 | L.Width = (L.NonNegative ? 0 : 1); |
| 5933 | else |
| 5934 | L.Width = std::min(L.Width - log2, MaxWidth); |
| 5935 | return L; |
| 5936 | } |
| 5937 | |
| 5938 | // Otherwise, just use the LHS's width. |
| 5939 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); |
| 5940 | return IntRange(L.Width, L.NonNegative && R.NonNegative); |
| 5941 | } |
| 5942 | |
| 5943 | // The result of a remainder can't be larger than the result of |
| 5944 | // either side. |
| 5945 | case BO_Rem: { |
| 5946 | // Don't 'pre-truncate' the operands. |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5947 | unsigned opWidth = C.getIntWidth(GetExprType(E)); |
John McCall | 5143181 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 5948 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); |
| 5949 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); |
| 5950 | |
| 5951 | IntRange meet = IntRange::meet(L, R); |
| 5952 | meet.Width = std::min(meet.Width, MaxWidth); |
| 5953 | return meet; |
| 5954 | } |
| 5955 | |
| 5956 | // The default behavior is okay for these. |
| 5957 | case BO_Mul: |
| 5958 | case BO_Add: |
| 5959 | case BO_Xor: |
| 5960 | case BO_Or: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5961 | break; |
| 5962 | } |
| 5963 | |
John McCall | 5143181 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 5964 | // The default case is to treat the operation as if it were closed |
| 5965 | // on the narrowest type that encompasses both operands. |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5966 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 5967 | IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth); |
| 5968 | return IntRange::join(L, R); |
| 5969 | } |
| 5970 | |
| 5971 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 5972 | switch (UO->getOpcode()) { |
| 5973 | // Boolean-valued operations are white-listed. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5974 | case UO_LNot: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5975 | return IntRange::forBoolType(); |
| 5976 | |
| 5977 | // Operations with opaque sources are black-listed. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5978 | case UO_Deref: |
| 5979 | case UO_AddrOf: // should be impossible |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5980 | return IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5981 | |
| 5982 | default: |
| 5983 | return GetExprRange(C, UO->getSubExpr(), MaxWidth); |
| 5984 | } |
| 5985 | } |
| 5986 | |
Ted Kremenek | a553fbf | 2013-10-14 18:55:27 +0000 | [diff] [blame] | 5987 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) |
| 5988 | return GetExprRange(C, OVE->getSourceExpr(), MaxWidth); |
| 5989 | |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5990 | if (FieldDecl *BitField = E->getSourceBitField()) |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5991 | return IntRange(BitField->getBitWidthValue(C), |
Douglas Gregor | 61b6e49 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 5992 | BitField->getType()->isUnsignedIntegerOrEnumerationType()); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5993 | |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5994 | return IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 5995 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 5996 | |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 5997 | static IntRange GetExprRange(ASTContext &C, Expr *E) { |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 5998 | return GetExprRange(C, E, C.getIntWidth(GetExprType(E))); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5999 | } |
| 6000 | |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6001 | /// Checks whether the given value, which currently has the given |
| 6002 | /// source semantics, has the same value when coerced through the |
| 6003 | /// target semantics. |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 6004 | static bool IsSameFloatAfterCast(const llvm::APFloat &value, |
| 6005 | const llvm::fltSemantics &Src, |
| 6006 | const llvm::fltSemantics &Tgt) { |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6007 | llvm::APFloat truncated = value; |
| 6008 | |
| 6009 | bool ignored; |
| 6010 | truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 6011 | truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 6012 | |
| 6013 | return truncated.bitwiseIsEqual(value); |
| 6014 | } |
| 6015 | |
| 6016 | /// Checks whether the given value, which currently has the given |
| 6017 | /// source semantics, has the same value when coerced through the |
| 6018 | /// target semantics. |
| 6019 | /// |
| 6020 | /// The value might be a vector of floats (or a complex number). |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 6021 | static bool IsSameFloatAfterCast(const APValue &value, |
| 6022 | const llvm::fltSemantics &Src, |
| 6023 | const llvm::fltSemantics &Tgt) { |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6024 | if (value.isFloat()) |
| 6025 | return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); |
| 6026 | |
| 6027 | if (value.isVector()) { |
| 6028 | for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) |
| 6029 | if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) |
| 6030 | return false; |
| 6031 | return true; |
| 6032 | } |
| 6033 | |
| 6034 | assert(value.isComplexFloat()); |
| 6035 | return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && |
| 6036 | IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); |
| 6037 | } |
| 6038 | |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 6039 | static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6040 | |
Ted Kremenek | 6274be4 | 2010-09-23 21:43:44 +0000 | [diff] [blame] | 6041 | static bool IsZero(Sema &S, Expr *E) { |
| 6042 | // Suppress cases where we are comparing against an enum constant. |
| 6043 | if (const DeclRefExpr *DR = |
| 6044 | dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) |
| 6045 | if (isa<EnumConstantDecl>(DR->getDecl())) |
| 6046 | return false; |
| 6047 | |
| 6048 | // Suppress cases where the '0' value is expanded from a macro. |
| 6049 | if (E->getLocStart().isMacroID()) |
| 6050 | return false; |
| 6051 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6052 | llvm::APSInt Value; |
| 6053 | return E->isIntegerConstantExpr(Value, S.Context) && Value == 0; |
| 6054 | } |
| 6055 | |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 6056 | static bool HasEnumType(Expr *E) { |
| 6057 | // Strip off implicit integral promotions. |
| 6058 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
Argyrios Kyrtzidis | 15a9edc | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 6059 | if (ICE->getCastKind() != CK_IntegralCast && |
| 6060 | ICE->getCastKind() != CK_NoOp) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 6061 | break; |
Argyrios Kyrtzidis | 15a9edc | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 6062 | E = ICE->getSubExpr(); |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 6063 | } |
| 6064 | |
| 6065 | return E->getType()->isEnumeralType(); |
| 6066 | } |
| 6067 | |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 6068 | static void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) { |
Richard Trieu | 3659456 | 2013-11-01 21:47:19 +0000 | [diff] [blame] | 6069 | // Disable warning in template instantiations. |
| 6070 | if (!S.ActiveTemplateInstantiations.empty()) |
| 6071 | return; |
| 6072 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6073 | BinaryOperatorKind op = E->getOpcode(); |
Douglas Gregor | b14dbd7 | 2010-12-21 07:22:56 +0000 | [diff] [blame] | 6074 | if (E->isValueDependent()) |
| 6075 | return; |
| 6076 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6077 | if (op == BO_LT && IsZero(S, E->getRHS())) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6078 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 6079 | << "< 0" << "false" << HasEnumType(E->getLHS()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6080 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6081 | } else if (op == BO_GE && IsZero(S, E->getRHS())) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6082 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 6083 | << ">= 0" << "true" << HasEnumType(E->getLHS()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6084 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6085 | } else if (op == BO_GT && IsZero(S, E->getLHS())) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6086 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 6087 | << "0 >" << "false" << HasEnumType(E->getRHS()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6088 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6089 | } else if (op == BO_LE && IsZero(S, E->getLHS())) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6090 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 6091 | << "0 <=" << "true" << HasEnumType(E->getRHS()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6092 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
| 6093 | } |
| 6094 | } |
| 6095 | |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 6096 | static void DiagnoseOutOfRangeComparison(Sema &S, BinaryOperator *E, |
Fariborz Jahanian | 2f4e33a | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 6097 | Expr *Constant, Expr *Other, |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 6098 | llvm::APSInt Value, |
Fariborz Jahanian | 2f4e33a | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 6099 | bool RhsConstant) { |
Richard Trieu | dd51d74 | 2013-11-01 21:19:43 +0000 | [diff] [blame] | 6100 | // Disable warning in template instantiations. |
| 6101 | if (!S.ActiveTemplateInstantiations.empty()) |
| 6102 | return; |
| 6103 | |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6104 | // TODO: Investigate using GetExprRange() to get tighter bounds |
| 6105 | // on the bit ranges. |
| 6106 | QualType OtherT = Other->getType(); |
David Majnemer | 7800f1f | 2015-05-23 01:32:17 +0000 | [diff] [blame] | 6107 | if (const auto *AT = OtherT->getAs<AtomicType>()) |
Justin Bogner | 4f42fc4 | 2014-07-21 18:01:53 +0000 | [diff] [blame] | 6108 | OtherT = AT->getValueType(); |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6109 | IntRange OtherRange = IntRange::forValueOfType(S.Context, OtherT); |
| 6110 | unsigned OtherWidth = OtherRange.Width; |
| 6111 | |
| 6112 | bool OtherIsBooleanType = Other->isKnownToHaveBooleanValue(); |
| 6113 | |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 6114 | // 0 values are handled later by CheckTrivialUnsignedComparison(). |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6115 | if ((Value == 0) && (!OtherIsBooleanType)) |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 6116 | return; |
| 6117 | |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 6118 | BinaryOperatorKind op = E->getOpcode(); |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6119 | bool IsTrue = true; |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 6120 | |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6121 | // Used for diagnostic printout. |
| 6122 | enum { |
| 6123 | LiteralConstant = 0, |
| 6124 | CXXBoolLiteralTrue, |
| 6125 | CXXBoolLiteralFalse |
| 6126 | } LiteralOrBoolConstant = LiteralConstant; |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 6127 | |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6128 | if (!OtherIsBooleanType) { |
| 6129 | QualType ConstantT = Constant->getType(); |
| 6130 | QualType CommonT = E->getLHS()->getType(); |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 6131 | |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6132 | if (S.Context.hasSameUnqualifiedType(OtherT, ConstantT)) |
| 6133 | return; |
| 6134 | assert((OtherT->isIntegerType() && ConstantT->isIntegerType()) && |
| 6135 | "comparison with non-integer type"); |
| 6136 | |
| 6137 | bool ConstantSigned = ConstantT->isSignedIntegerType(); |
| 6138 | bool CommonSigned = CommonT->isSignedIntegerType(); |
| 6139 | |
| 6140 | bool EqualityOnly = false; |
| 6141 | |
| 6142 | if (CommonSigned) { |
| 6143 | // The common type is signed, therefore no signed to unsigned conversion. |
| 6144 | if (!OtherRange.NonNegative) { |
| 6145 | // Check that the constant is representable in type OtherT. |
| 6146 | if (ConstantSigned) { |
| 6147 | if (OtherWidth >= Value.getMinSignedBits()) |
| 6148 | return; |
| 6149 | } else { // !ConstantSigned |
| 6150 | if (OtherWidth >= Value.getActiveBits() + 1) |
| 6151 | return; |
| 6152 | } |
| 6153 | } else { // !OtherSigned |
| 6154 | // Check that the constant is representable in type OtherT. |
| 6155 | // Negative values are out of range. |
| 6156 | if (ConstantSigned) { |
| 6157 | if (Value.isNonNegative() && OtherWidth >= Value.getActiveBits()) |
| 6158 | return; |
| 6159 | } else { // !ConstantSigned |
| 6160 | if (OtherWidth >= Value.getActiveBits()) |
| 6161 | return; |
| 6162 | } |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 6163 | } |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6164 | } else { // !CommonSigned |
| 6165 | if (OtherRange.NonNegative) { |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 6166 | if (OtherWidth >= Value.getActiveBits()) |
| 6167 | return; |
Craig Topper | cf36016 | 2014-06-18 05:13:11 +0000 | [diff] [blame] | 6168 | } else { // OtherSigned |
| 6169 | assert(!ConstantSigned && |
| 6170 | "Two signed types converted to unsigned types."); |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6171 | // Check to see if the constant is representable in OtherT. |
| 6172 | if (OtherWidth > Value.getActiveBits()) |
| 6173 | return; |
| 6174 | // Check to see if the constant is equivalent to a negative value |
| 6175 | // cast to CommonT. |
| 6176 | if (S.Context.getIntWidth(ConstantT) == |
| 6177 | S.Context.getIntWidth(CommonT) && |
| 6178 | Value.isNegative() && Value.getMinSignedBits() <= OtherWidth) |
| 6179 | return; |
| 6180 | // The constant value rests between values that OtherT can represent |
| 6181 | // after conversion. Relational comparison still works, but equality |
| 6182 | // comparisons will be tautological. |
| 6183 | EqualityOnly = true; |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 6184 | } |
| 6185 | } |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6186 | |
| 6187 | bool PositiveConstant = !ConstantSigned || Value.isNonNegative(); |
| 6188 | |
| 6189 | if (op == BO_EQ || op == BO_NE) { |
| 6190 | IsTrue = op == BO_NE; |
| 6191 | } else if (EqualityOnly) { |
| 6192 | return; |
| 6193 | } else if (RhsConstant) { |
| 6194 | if (op == BO_GT || op == BO_GE) |
| 6195 | IsTrue = !PositiveConstant; |
| 6196 | else // op == BO_LT || op == BO_LE |
| 6197 | IsTrue = PositiveConstant; |
| 6198 | } else { |
| 6199 | if (op == BO_LT || op == BO_LE) |
| 6200 | IsTrue = !PositiveConstant; |
| 6201 | else // op == BO_GT || op == BO_GE |
| 6202 | IsTrue = PositiveConstant; |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 6203 | } |
Fariborz Jahanian | 2f4e33a | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 6204 | } else { |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6205 | // Other isKnownToHaveBooleanValue |
| 6206 | enum CompareBoolWithConstantResult { AFals, ATrue, Unkwn }; |
| 6207 | enum ConstantValue { LT_Zero, Zero, One, GT_One, SizeOfConstVal }; |
| 6208 | enum ConstantSide { Lhs, Rhs, SizeOfConstSides }; |
| 6209 | |
| 6210 | static const struct LinkedConditions { |
| 6211 | CompareBoolWithConstantResult BO_LT_OP[SizeOfConstSides][SizeOfConstVal]; |
| 6212 | CompareBoolWithConstantResult BO_GT_OP[SizeOfConstSides][SizeOfConstVal]; |
| 6213 | CompareBoolWithConstantResult BO_LE_OP[SizeOfConstSides][SizeOfConstVal]; |
| 6214 | CompareBoolWithConstantResult BO_GE_OP[SizeOfConstSides][SizeOfConstVal]; |
| 6215 | CompareBoolWithConstantResult BO_EQ_OP[SizeOfConstSides][SizeOfConstVal]; |
| 6216 | CompareBoolWithConstantResult BO_NE_OP[SizeOfConstSides][SizeOfConstVal]; |
| 6217 | |
| 6218 | } TruthTable = { |
| 6219 | // Constant on LHS. | Constant on RHS. | |
| 6220 | // LT_Zero| Zero | One |GT_One| LT_Zero| Zero | One |GT_One| |
| 6221 | { { ATrue, Unkwn, AFals, AFals }, { AFals, AFals, Unkwn, ATrue } }, |
| 6222 | { { AFals, AFals, Unkwn, ATrue }, { ATrue, Unkwn, AFals, AFals } }, |
| 6223 | { { ATrue, ATrue, Unkwn, AFals }, { AFals, Unkwn, ATrue, ATrue } }, |
| 6224 | { { AFals, Unkwn, ATrue, ATrue }, { ATrue, ATrue, Unkwn, AFals } }, |
| 6225 | { { AFals, Unkwn, Unkwn, AFals }, { AFals, Unkwn, Unkwn, AFals } }, |
| 6226 | { { ATrue, Unkwn, Unkwn, ATrue }, { ATrue, Unkwn, Unkwn, ATrue } } |
| 6227 | }; |
| 6228 | |
| 6229 | bool ConstantIsBoolLiteral = isa<CXXBoolLiteralExpr>(Constant); |
| 6230 | |
| 6231 | enum ConstantValue ConstVal = Zero; |
| 6232 | if (Value.isUnsigned() || Value.isNonNegative()) { |
| 6233 | if (Value == 0) { |
| 6234 | LiteralOrBoolConstant = |
| 6235 | ConstantIsBoolLiteral ? CXXBoolLiteralFalse : LiteralConstant; |
| 6236 | ConstVal = Zero; |
| 6237 | } else if (Value == 1) { |
| 6238 | LiteralOrBoolConstant = |
| 6239 | ConstantIsBoolLiteral ? CXXBoolLiteralTrue : LiteralConstant; |
| 6240 | ConstVal = One; |
| 6241 | } else { |
| 6242 | LiteralOrBoolConstant = LiteralConstant; |
| 6243 | ConstVal = GT_One; |
| 6244 | } |
| 6245 | } else { |
| 6246 | ConstVal = LT_Zero; |
| 6247 | } |
| 6248 | |
| 6249 | CompareBoolWithConstantResult CmpRes; |
| 6250 | |
| 6251 | switch (op) { |
| 6252 | case BO_LT: |
| 6253 | CmpRes = TruthTable.BO_LT_OP[RhsConstant][ConstVal]; |
| 6254 | break; |
| 6255 | case BO_GT: |
| 6256 | CmpRes = TruthTable.BO_GT_OP[RhsConstant][ConstVal]; |
| 6257 | break; |
| 6258 | case BO_LE: |
| 6259 | CmpRes = TruthTable.BO_LE_OP[RhsConstant][ConstVal]; |
| 6260 | break; |
| 6261 | case BO_GE: |
| 6262 | CmpRes = TruthTable.BO_GE_OP[RhsConstant][ConstVal]; |
| 6263 | break; |
| 6264 | case BO_EQ: |
| 6265 | CmpRes = TruthTable.BO_EQ_OP[RhsConstant][ConstVal]; |
| 6266 | break; |
| 6267 | case BO_NE: |
| 6268 | CmpRes = TruthTable.BO_NE_OP[RhsConstant][ConstVal]; |
| 6269 | break; |
| 6270 | default: |
| 6271 | CmpRes = Unkwn; |
| 6272 | break; |
| 6273 | } |
| 6274 | |
| 6275 | if (CmpRes == AFals) { |
| 6276 | IsTrue = false; |
| 6277 | } else if (CmpRes == ATrue) { |
| 6278 | IsTrue = true; |
| 6279 | } else { |
| 6280 | return; |
| 6281 | } |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 6282 | } |
Ted Kremenek | b7d7dd4 | 2013-03-15 21:50:10 +0000 | [diff] [blame] | 6283 | |
| 6284 | // If this is a comparison to an enum constant, include that |
| 6285 | // constant in the diagnostic. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6286 | const EnumConstantDecl *ED = nullptr; |
Ted Kremenek | b7d7dd4 | 2013-03-15 21:50:10 +0000 | [diff] [blame] | 6287 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Constant)) |
| 6288 | ED = dyn_cast<EnumConstantDecl>(DR->getDecl()); |
| 6289 | |
| 6290 | SmallString<64> PrettySourceValue; |
| 6291 | llvm::raw_svector_ostream OS(PrettySourceValue); |
| 6292 | if (ED) |
Ted Kremenek | e943ce1 | 2013-03-15 22:02:46 +0000 | [diff] [blame] | 6293 | OS << '\'' << *ED << "' (" << Value << ")"; |
Ted Kremenek | b7d7dd4 | 2013-03-15 21:50:10 +0000 | [diff] [blame] | 6294 | else |
| 6295 | OS << Value; |
| 6296 | |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6297 | S.DiagRuntimeBehavior( |
| 6298 | E->getOperatorLoc(), E, |
| 6299 | S.PDiag(diag::warn_out_of_range_compare) |
| 6300 | << OS.str() << LiteralOrBoolConstant |
| 6301 | << OtherT << (OtherIsBooleanType && !OtherT->isBooleanType()) << IsTrue |
| 6302 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange()); |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 6303 | } |
| 6304 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6305 | /// Analyze the operands of the given comparison. Implements the |
| 6306 | /// fallback case from AnalyzeComparison. |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 6307 | static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6308 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 6309 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6310 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6311 | |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 6312 | /// \brief Implements -Wsign-compare. |
| 6313 | /// |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 6314 | /// \param E the binary operator to check for warnings |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 6315 | static void AnalyzeComparison(Sema &S, BinaryOperator *E) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6316 | // The type the comparison is being performed in. |
| 6317 | QualType T = E->getLHS()->getType(); |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 6318 | |
| 6319 | // Only analyze comparison operators where both sides have been converted to |
| 6320 | // the same type. |
| 6321 | if (!S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType())) |
| 6322 | return AnalyzeImpConvsInComparison(S, E); |
| 6323 | |
| 6324 | // Don't analyze value-dependent comparisons directly. |
Fariborz Jahanian | 282071e | 2012-09-18 17:46:26 +0000 | [diff] [blame] | 6325 | if (E->isValueDependent()) |
| 6326 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 6327 | |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 6328 | Expr *LHS = E->getLHS()->IgnoreParenImpCasts(); |
| 6329 | Expr *RHS = E->getRHS()->IgnoreParenImpCasts(); |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 6330 | |
| 6331 | bool IsComparisonConstant = false; |
| 6332 | |
Fariborz Jahanian | 2f4e33a | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 6333 | // Check whether an integer constant comparison results in a value |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 6334 | // of 'true' or 'false'. |
| 6335 | if (T->isIntegralType(S.Context)) { |
| 6336 | llvm::APSInt RHSValue; |
| 6337 | bool IsRHSIntegralLiteral = |
| 6338 | RHS->isIntegerConstantExpr(RHSValue, S.Context); |
| 6339 | llvm::APSInt LHSValue; |
| 6340 | bool IsLHSIntegralLiteral = |
| 6341 | LHS->isIntegerConstantExpr(LHSValue, S.Context); |
| 6342 | if (IsRHSIntegralLiteral && !IsLHSIntegralLiteral) |
| 6343 | DiagnoseOutOfRangeComparison(S, E, RHS, LHS, RHSValue, true); |
| 6344 | else if (!IsRHSIntegralLiteral && IsLHSIntegralLiteral) |
| 6345 | DiagnoseOutOfRangeComparison(S, E, LHS, RHS, LHSValue, false); |
| 6346 | else |
| 6347 | IsComparisonConstant = |
| 6348 | (IsRHSIntegralLiteral && IsLHSIntegralLiteral); |
Fariborz Jahanian | 2f4e33a | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 6349 | } else if (!T->hasUnsignedIntegerRepresentation()) |
| 6350 | IsComparisonConstant = E->isIntegerConstantExpr(S.Context); |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 6351 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6352 | // We don't do anything special if this isn't an unsigned integral |
| 6353 | // comparison: we're only interested in integral comparisons, and |
| 6354 | // 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] | 6355 | // |
| 6356 | // We also don't care about value-dependent expressions or expressions |
| 6357 | // whose result is a constant. |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 6358 | if (!T->hasUnsignedIntegerRepresentation() || IsComparisonConstant) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6359 | return AnalyzeImpConvsInComparison(S, E); |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 6360 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6361 | // Check to see if one of the (unmodified) operands is of different |
| 6362 | // signedness. |
| 6363 | Expr *signedOperand, *unsignedOperand; |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 6364 | if (LHS->getType()->hasSignedIntegerRepresentation()) { |
| 6365 | assert(!RHS->getType()->hasSignedIntegerRepresentation() && |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6366 | "unsigned comparison between two signed integer expressions?"); |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 6367 | signedOperand = LHS; |
| 6368 | unsignedOperand = RHS; |
| 6369 | } else if (RHS->getType()->hasSignedIntegerRepresentation()) { |
| 6370 | signedOperand = RHS; |
| 6371 | unsignedOperand = LHS; |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 6372 | } else { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6373 | CheckTrivialUnsignedComparison(S, E); |
| 6374 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 6375 | } |
| 6376 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6377 | // Otherwise, calculate the effective range of the signed operand. |
| 6378 | IntRange signedRange = GetExprRange(S.Context, signedOperand); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6379 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6380 | // Go ahead and analyze implicit conversions in the operands. Note |
| 6381 | // that we skip the implicit conversions on both sides. |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 6382 | AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc()); |
| 6383 | AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc()); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 6384 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6385 | // If the signed range is non-negative, -Wsign-compare won't fire, |
| 6386 | // but we should still check for comparisons which are always true |
| 6387 | // or false. |
| 6388 | if (signedRange.NonNegative) |
| 6389 | return CheckTrivialUnsignedComparison(S, E); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 6390 | |
| 6391 | // For (in)equality comparisons, if the unsigned operand is a |
| 6392 | // constant which cannot collide with a overflowed signed operand, |
| 6393 | // then reinterpreting the signed operand as unsigned will not |
| 6394 | // change the result of the comparison. |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6395 | if (E->isEqualityOp()) { |
| 6396 | unsigned comparisonWidth = S.Context.getIntWidth(T); |
| 6397 | IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 6398 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6399 | // We should never be unable to prove that the unsigned operand is |
| 6400 | // non-negative. |
| 6401 | assert(unsignedRange.NonNegative && "unsigned range includes negative?"); |
| 6402 | |
| 6403 | if (unsignedRange.Width < comparisonWidth) |
| 6404 | return; |
| 6405 | } |
| 6406 | |
Douglas Gregor | bfb4a21 | 2012-05-01 01:53:49 +0000 | [diff] [blame] | 6407 | S.DiagRuntimeBehavior(E->getOperatorLoc(), E, |
| 6408 | S.PDiag(diag::warn_mixed_sign_comparison) |
| 6409 | << LHS->getType() << RHS->getType() |
| 6410 | << LHS->getSourceRange() << RHS->getSourceRange()); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 6411 | } |
| 6412 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6413 | /// Analyzes an attempt to assign the given value to a bitfield. |
| 6414 | /// |
| 6415 | /// Returns true if there was something fishy about the attempt. |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 6416 | static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, |
| 6417 | SourceLocation InitLoc) { |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6418 | assert(Bitfield->isBitField()); |
| 6419 | if (Bitfield->isInvalidDecl()) |
| 6420 | return false; |
| 6421 | |
John McCall | deebbcf | 2010-11-11 05:33:51 +0000 | [diff] [blame] | 6422 | // White-list bool bitfields. |
| 6423 | if (Bitfield->getType()->isBooleanType()) |
| 6424 | return false; |
| 6425 | |
Douglas Gregor | 789adec | 2011-02-04 13:09:01 +0000 | [diff] [blame] | 6426 | // Ignore value- or type-dependent expressions. |
| 6427 | if (Bitfield->getBitWidth()->isValueDependent() || |
| 6428 | Bitfield->getBitWidth()->isTypeDependent() || |
| 6429 | Init->isValueDependent() || |
| 6430 | Init->isTypeDependent()) |
| 6431 | return false; |
| 6432 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6433 | Expr *OriginalInit = Init->IgnoreParenImpCasts(); |
| 6434 | |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 6435 | llvm::APSInt Value; |
| 6436 | if (!OriginalInit->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects)) |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6437 | return false; |
| 6438 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6439 | unsigned OriginalWidth = Value.getBitWidth(); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6440 | unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context); |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6441 | |
| 6442 | if (OriginalWidth <= FieldWidth) |
| 6443 | return false; |
| 6444 | |
Eli Friedman | c267a32 | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 6445 | // Compute the value which the bitfield will contain. |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 6446 | llvm::APSInt TruncatedValue = Value.trunc(FieldWidth); |
Eli Friedman | c267a32 | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 6447 | TruncatedValue.setIsSigned(Bitfield->getType()->isSignedIntegerType()); |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6448 | |
Eli Friedman | c267a32 | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 6449 | // Check whether the stored value is equal to the original value. |
| 6450 | TruncatedValue = TruncatedValue.extend(OriginalWidth); |
Richard Trieu | c320c74 | 2012-07-23 20:21:35 +0000 | [diff] [blame] | 6451 | if (llvm::APSInt::isSameValue(Value, TruncatedValue)) |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6452 | return false; |
| 6453 | |
Eli Friedman | c267a32 | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 6454 | // Special-case bitfields of width 1: booleans are naturally 0/1, and |
Eli Friedman | e1ffd49 | 2012-02-02 00:40:20 +0000 | [diff] [blame] | 6455 | // therefore don't strictly fit into a signed bitfield of width 1. |
| 6456 | if (FieldWidth == 1 && Value == 1) |
Eli Friedman | c267a32 | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 6457 | return false; |
| 6458 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6459 | std::string PrettyValue = Value.toString(10); |
| 6460 | std::string PrettyTrunc = TruncatedValue.toString(10); |
| 6461 | |
| 6462 | S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant) |
| 6463 | << PrettyValue << PrettyTrunc << OriginalInit->getType() |
| 6464 | << Init->getSourceRange(); |
| 6465 | |
| 6466 | return true; |
| 6467 | } |
| 6468 | |
John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 6469 | /// Analyze the given simple or compound assignment for warning-worthy |
| 6470 | /// operations. |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 6471 | static void AnalyzeAssignment(Sema &S, BinaryOperator *E) { |
John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 6472 | // Just recurse on the LHS. |
| 6473 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 6474 | |
| 6475 | // We want to recurse on the RHS as normal unless we're assigning to |
| 6476 | // a bitfield. |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 6477 | if (FieldDecl *Bitfield = E->getLHS()->getSourceBitField()) { |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 6478 | if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(), |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6479 | E->getOperatorLoc())) { |
| 6480 | // Recurse, ignoring any implicit conversions on the RHS. |
| 6481 | return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(), |
| 6482 | E->getOperatorLoc()); |
John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 6483 | } |
| 6484 | } |
| 6485 | |
| 6486 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
| 6487 | } |
| 6488 | |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6489 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 6490 | static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 6491 | SourceLocation CContext, unsigned diag, |
| 6492 | bool pruneControlFlow = false) { |
| 6493 | if (pruneControlFlow) { |
| 6494 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
| 6495 | S.PDiag(diag) |
| 6496 | << SourceType << T << E->getSourceRange() |
| 6497 | << SourceRange(CContext)); |
| 6498 | return; |
| 6499 | } |
Douglas Gregor | 364f7db | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 6500 | S.Diag(E->getExprLoc(), diag) |
| 6501 | << SourceType << T << E->getSourceRange() << SourceRange(CContext); |
| 6502 | } |
| 6503 | |
Chandler Carruth | 7f3654f | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 6504 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 6505 | static void DiagnoseImpCast(Sema &S, Expr *E, QualType T, |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 6506 | SourceLocation CContext, unsigned diag, |
| 6507 | bool pruneControlFlow = false) { |
| 6508 | DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow); |
Chandler Carruth | 7f3654f | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 6509 | } |
| 6510 | |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 6511 | /// Diagnose an implicit cast from a literal expression. Does not warn when the |
| 6512 | /// cast wouldn't lose information. |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 6513 | void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T, |
| 6514 | SourceLocation CContext) { |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 6515 | // 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] | 6516 | bool isExact = false; |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 6517 | const llvm::APFloat &Value = FL->getValue(); |
Jeffrey Yasskin | d0f079d | 2011-07-15 17:03:07 +0000 | [diff] [blame] | 6518 | llvm::APSInt IntegerValue(S.Context.getIntWidth(T), |
| 6519 | T->hasUnsignedIntegerRepresentation()); |
| 6520 | if (Value.convertToInteger(IntegerValue, |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 6521 | llvm::APFloat::rmTowardZero, &isExact) |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 6522 | == llvm::APFloat::opOK && isExact) |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 6523 | return; |
| 6524 | |
Eli Friedman | 0718591 | 2013-08-29 23:44:43 +0000 | [diff] [blame] | 6525 | // FIXME: Force the precision of the source value down so we don't print |
| 6526 | // digits which are usually useless (we don't really care here if we |
| 6527 | // truncate a digit by accident in edge cases). Ideally, APFloat::toString |
| 6528 | // would automatically print the shortest representation, but it's a bit |
| 6529 | // tricky to implement. |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 6530 | SmallString<16> PrettySourceValue; |
Eli Friedman | 0718591 | 2013-08-29 23:44:43 +0000 | [diff] [blame] | 6531 | unsigned precision = llvm::APFloat::semanticsPrecision(Value.getSemantics()); |
| 6532 | precision = (precision * 59 + 195) / 196; |
| 6533 | Value.toString(PrettySourceValue, precision); |
| 6534 | |
David Blaikie | 9b88cc0 | 2012-05-15 17:18:27 +0000 | [diff] [blame] | 6535 | SmallString<16> PrettyTargetValue; |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 6536 | if (T->isSpecificBuiltinType(BuiltinType::Bool)) |
| 6537 | PrettyTargetValue = IntegerValue == 0 ? "false" : "true"; |
| 6538 | else |
David Blaikie | 9b88cc0 | 2012-05-15 17:18:27 +0000 | [diff] [blame] | 6539 | IntegerValue.toString(PrettyTargetValue); |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 6540 | |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 6541 | S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer) |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 6542 | << FL->getType() << T.getUnqualifiedType() << PrettySourceValue |
| 6543 | << PrettyTargetValue << FL->getSourceRange() << SourceRange(CContext); |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 6544 | } |
| 6545 | |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 6546 | std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) { |
| 6547 | if (!Range.Width) return "0"; |
| 6548 | |
| 6549 | llvm::APSInt ValueInRange = Value; |
| 6550 | ValueInRange.setIsSigned(!Range.NonNegative); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 6551 | ValueInRange = ValueInRange.trunc(Range.Width); |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 6552 | return ValueInRange.toString(10); |
| 6553 | } |
| 6554 | |
Hans Wennborg | f4ad232 | 2012-08-28 15:44:30 +0000 | [diff] [blame] | 6555 | static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) { |
| 6556 | if (!isa<ImplicitCastExpr>(Ex)) |
| 6557 | return false; |
| 6558 | |
| 6559 | Expr *InnerE = Ex->IgnoreParenImpCasts(); |
| 6560 | const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr(); |
| 6561 | const Type *Source = |
| 6562 | S.Context.getCanonicalType(InnerE->getType()).getTypePtr(); |
| 6563 | if (Target->isDependentType()) |
| 6564 | return false; |
| 6565 | |
| 6566 | const BuiltinType *FloatCandidateBT = |
| 6567 | dyn_cast<BuiltinType>(ToBool ? Source : Target); |
| 6568 | const Type *BoolCandidateType = ToBool ? Target : Source; |
| 6569 | |
| 6570 | return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) && |
| 6571 | FloatCandidateBT && (FloatCandidateBT->isFloatingPoint())); |
| 6572 | } |
| 6573 | |
| 6574 | void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall, |
| 6575 | SourceLocation CC) { |
| 6576 | unsigned NumArgs = TheCall->getNumArgs(); |
| 6577 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 6578 | Expr *CurrA = TheCall->getArg(i); |
| 6579 | if (!IsImplicitBoolFloatConversion(S, CurrA, true)) |
| 6580 | continue; |
| 6581 | |
| 6582 | bool IsSwapped = ((i > 0) && |
| 6583 | IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false)); |
| 6584 | IsSwapped |= ((i < (NumArgs - 1)) && |
| 6585 | IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false)); |
| 6586 | if (IsSwapped) { |
| 6587 | // Warn on this floating-point to bool conversion. |
| 6588 | DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(), |
| 6589 | CurrA->getType(), CC, |
| 6590 | diag::warn_impcast_floating_point_to_bool); |
| 6591 | } |
| 6592 | } |
| 6593 | } |
| 6594 | |
Richard Trieu | 5b99350 | 2014-10-15 03:42:06 +0000 | [diff] [blame] | 6595 | static void DiagnoseNullConversion(Sema &S, Expr *E, QualType T, |
| 6596 | SourceLocation CC) { |
| 6597 | if (S.Diags.isIgnored(diag::warn_impcast_null_pointer_to_integer, |
| 6598 | E->getExprLoc())) |
| 6599 | return; |
| 6600 | |
| 6601 | // Check for NULL (GNUNull) or nullptr (CXX11_nullptr). |
| 6602 | const Expr::NullPointerConstantKind NullKind = |
| 6603 | E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull); |
| 6604 | if (NullKind != Expr::NPCK_GNUNull && NullKind != Expr::NPCK_CXX11_nullptr) |
| 6605 | return; |
| 6606 | |
| 6607 | // Return if target type is a safe conversion. |
| 6608 | if (T->isAnyPointerType() || T->isBlockPointerType() || |
| 6609 | T->isMemberPointerType() || !T->isScalarType() || T->isNullPtrType()) |
| 6610 | return; |
| 6611 | |
| 6612 | SourceLocation Loc = E->getSourceRange().getBegin(); |
| 6613 | |
| 6614 | // __null is usually wrapped in a macro. Go up a macro if that is the case. |
| 6615 | if (NullKind == Expr::NPCK_GNUNull) { |
| 6616 | if (Loc.isMacroID()) |
| 6617 | Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first; |
| 6618 | } |
| 6619 | |
| 6620 | // Only warn if the null and context location are in the same macro expansion. |
| 6621 | if (S.SourceMgr.getFileID(Loc) != S.SourceMgr.getFileID(CC)) |
| 6622 | return; |
| 6623 | |
| 6624 | S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer) |
| 6625 | << (NullKind == Expr::NPCK_CXX11_nullptr) << T << clang::SourceRange(CC) |
| 6626 | << FixItHint::CreateReplacement(Loc, |
| 6627 | S.getFixItZeroLiteralForType(T, Loc)); |
| 6628 | } |
| 6629 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6630 | void CheckImplicitConversion(Sema &S, Expr *E, QualType T, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6631 | SourceLocation CC, bool *ICContext = nullptr) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6632 | if (E->isTypeDependent() || E->isValueDependent()) return; |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6633 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6634 | const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr(); |
| 6635 | const Type *Target = S.Context.getCanonicalType(T).getTypePtr(); |
| 6636 | if (Source == Target) return; |
| 6637 | if (Target->isDependentType()) return; |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6638 | |
Chandler Carruth | c22845a | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 6639 | // If the conversion context location is invalid don't complain. We also |
| 6640 | // don't want to emit a warning if the issue occurs from the expansion of |
| 6641 | // a system macro. The problem is that 'getSpellingLoc()' is slow, so we |
| 6642 | // delay this check as long as possible. Once we detect we are in that |
| 6643 | // scenario, we just return. |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6644 | if (CC.isInvalid()) |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6645 | return; |
| 6646 | |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 6647 | // Diagnose implicit casts to bool. |
| 6648 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) { |
| 6649 | if (isa<StringLiteral>(E)) |
| 6650 | // Warn on string literal to bool. Checks for string literals in logical |
Richard Trieu | 955231d | 2014-01-25 01:10:35 +0000 | [diff] [blame] | 6651 | // and expressions, for instance, assert(0 && "error here"), are |
| 6652 | // prevented by a check in AnalyzeImplicitConversions(). |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 6653 | return DiagnoseImpCast(S, E, T, CC, |
| 6654 | diag::warn_impcast_string_literal_to_bool); |
Richard Trieu | 1e632af | 2014-01-28 23:40:26 +0000 | [diff] [blame] | 6655 | if (isa<ObjCStringLiteral>(E) || isa<ObjCArrayLiteral>(E) || |
| 6656 | isa<ObjCDictionaryLiteral>(E) || isa<ObjCBoxedExpr>(E)) { |
| 6657 | // This covers the literal expressions that evaluate to Objective-C |
| 6658 | // objects. |
| 6659 | return DiagnoseImpCast(S, E, T, CC, |
| 6660 | diag::warn_impcast_objective_c_literal_to_bool); |
| 6661 | } |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 6662 | if (Source->isPointerType() || Source->canDecayToPointerType()) { |
| 6663 | // Warn on pointer to bool conversion that is always true. |
| 6664 | S.DiagnoseAlwaysNonNullPointer(E, Expr::NPCK_NotNull, /*IsEqual*/ false, |
| 6665 | SourceRange(CC)); |
Lang Hames | df5c121 | 2011-12-05 20:49:50 +0000 | [diff] [blame] | 6666 | } |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 6667 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6668 | |
| 6669 | // Strip vector types. |
| 6670 | if (isa<VectorType>(Source)) { |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6671 | if (!isa<VectorType>(Target)) { |
Matt Beaumont-Gay | 7a57ada | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 6672 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6673 | return; |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6674 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar); |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6675 | } |
Chris Lattner | ee7286f | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 6676 | |
| 6677 | // If the vector cast is cast between two vectors of the same size, it is |
| 6678 | // a bitcast, not a conversion. |
| 6679 | if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target)) |
| 6680 | return; |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6681 | |
| 6682 | Source = cast<VectorType>(Source)->getElementType().getTypePtr(); |
| 6683 | Target = cast<VectorType>(Target)->getElementType().getTypePtr(); |
| 6684 | } |
Stephen Canon | 3ba640d | 2014-04-03 10:33:25 +0000 | [diff] [blame] | 6685 | if (auto VecTy = dyn_cast<VectorType>(Target)) |
| 6686 | Target = VecTy->getElementType().getTypePtr(); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6687 | |
| 6688 | // Strip complex types. |
| 6689 | if (isa<ComplexType>(Source)) { |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6690 | if (!isa<ComplexType>(Target)) { |
Matt Beaumont-Gay | 7a57ada | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 6691 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6692 | return; |
| 6693 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6694 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar); |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6695 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6696 | |
| 6697 | Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); |
| 6698 | Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); |
| 6699 | } |
| 6700 | |
| 6701 | const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); |
| 6702 | const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); |
| 6703 | |
| 6704 | // If the source is floating point... |
| 6705 | if (SourceBT && SourceBT->isFloatingPoint()) { |
| 6706 | // ...and the target is floating point... |
| 6707 | if (TargetBT && TargetBT->isFloatingPoint()) { |
| 6708 | // ...then warn if we're dropping FP rank. |
| 6709 | |
| 6710 | // Builtin FP kinds are ordered by increasing FP rank. |
| 6711 | if (SourceBT->getKind() > TargetBT->getKind()) { |
| 6712 | // Don't warn about float constants that are precisely |
| 6713 | // representable in the target type. |
| 6714 | Expr::EvalResult result; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6715 | if (E->EvaluateAsRValue(result, S.Context)) { |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6716 | // Value might be a float, a float vector, or a float complex. |
| 6717 | if (IsSameFloatAfterCast(result.Val, |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6718 | S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)), |
| 6719 | S.Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6720 | return; |
| 6721 | } |
| 6722 | |
Matt Beaumont-Gay | 7a57ada | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 6723 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6724 | return; |
| 6725 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6726 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6727 | } |
| 6728 | return; |
| 6729 | } |
| 6730 | |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6731 | // If the target is integral, always warn. |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 6732 | if (TargetBT && TargetBT->isInteger()) { |
Matt Beaumont-Gay | 7a57ada | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 6733 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6734 | return; |
| 6735 | |
Chandler Carruth | 22c7a79 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 6736 | Expr *InnerE = E->IgnoreParenImpCasts(); |
Matt Beaumont-Gay | 042ce8e | 2011-09-08 22:30:47 +0000 | [diff] [blame] | 6737 | // We also want to warn on, e.g., "int i = -1.234" |
| 6738 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE)) |
| 6739 | if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus) |
| 6740 | InnerE = UOp->getSubExpr()->IgnoreParenImpCasts(); |
| 6741 | |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 6742 | if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) { |
| 6743 | DiagnoseFloatingLiteralImpCast(S, FL, T, CC); |
Chandler Carruth | 22c7a79 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 6744 | } else { |
| 6745 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer); |
| 6746 | } |
| 6747 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6748 | |
Hans Wennborg | f4ad232 | 2012-08-28 15:44:30 +0000 | [diff] [blame] | 6749 | // If the target is bool, warn if expr is a function or method call. |
| 6750 | if (Target->isSpecificBuiltinType(BuiltinType::Bool) && |
| 6751 | isa<CallExpr>(E)) { |
| 6752 | // Check last argument of function call to see if it is an |
| 6753 | // implicit cast from a type matching the type the result |
| 6754 | // is being cast to. |
| 6755 | CallExpr *CEx = cast<CallExpr>(E); |
| 6756 | unsigned NumArgs = CEx->getNumArgs(); |
| 6757 | if (NumArgs > 0) { |
| 6758 | Expr *LastA = CEx->getArg(NumArgs - 1); |
| 6759 | Expr *InnerE = LastA->IgnoreParenImpCasts(); |
| 6760 | const Type *InnerType = |
| 6761 | S.Context.getCanonicalType(InnerE->getType()).getTypePtr(); |
| 6762 | if (isa<ImplicitCastExpr>(LastA) && (InnerType == Target)) { |
| 6763 | // Warn on this floating-point to bool conversion |
| 6764 | DiagnoseImpCast(S, E, T, CC, |
| 6765 | diag::warn_impcast_floating_point_to_bool); |
| 6766 | } |
| 6767 | } |
| 6768 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6769 | return; |
| 6770 | } |
| 6771 | |
Richard Trieu | 5b99350 | 2014-10-15 03:42:06 +0000 | [diff] [blame] | 6772 | DiagnoseNullConversion(S, E, T, CC); |
Richard Trieu | beaf345 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 6773 | |
David Blaikie | 9366d2b | 2012-06-19 21:19:06 +0000 | [diff] [blame] | 6774 | if (!Source->isIntegerType() || !Target->isIntegerType()) |
| 6775 | return; |
| 6776 | |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 6777 | // TODO: remove this early return once the false positives for constant->bool |
| 6778 | // in templates, macros, etc, are reduced or removed. |
| 6779 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) |
| 6780 | return; |
| 6781 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6782 | IntRange SourceRange = GetExprRange(S.Context, E); |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 6783 | IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6784 | |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 6785 | if (SourceRange.Width > TargetRange.Width) { |
Sam Panzer | 6fffec6 | 2013-03-28 19:07:11 +0000 | [diff] [blame] | 6786 | // If the source is a constant, use a default-on diagnostic. |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 6787 | // TODO: this should happen for bitfield stores, too. |
| 6788 | llvm::APSInt Value(32); |
| 6789 | if (E->isIntegerConstantExpr(Value, S.Context)) { |
| 6790 | if (S.SourceMgr.isInSystemMacro(CC)) |
| 6791 | return; |
| 6792 | |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 6793 | std::string PrettySourceValue = Value.toString(10); |
| 6794 | std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 6795 | |
Ted Kremenek | 33ba995 | 2011-10-22 02:37:33 +0000 | [diff] [blame] | 6796 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
| 6797 | S.PDiag(diag::warn_impcast_integer_precision_constant) |
| 6798 | << PrettySourceValue << PrettyTargetValue |
| 6799 | << E->getType() << T << E->getSourceRange() |
| 6800 | << clang::SourceRange(CC)); |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 6801 | return; |
| 6802 | } |
| 6803 | |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 6804 | // People want to build with -Wshorten-64-to-32 and not -Wconversion. |
| 6805 | if (S.SourceMgr.isInSystemMacro(CC)) |
| 6806 | return; |
| 6807 | |
David Blaikie | 9455da0 | 2012-04-12 22:40:54 +0000 | [diff] [blame] | 6808 | if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64) |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 6809 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32, |
| 6810 | /* pruneControlFlow */ true); |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6811 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6812 | } |
| 6813 | |
| 6814 | if ((TargetRange.NonNegative && !SourceRange.NonNegative) || |
| 6815 | (!TargetRange.NonNegative && SourceRange.NonNegative && |
| 6816 | SourceRange.Width == TargetRange.Width)) { |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6817 | |
Matt Beaumont-Gay | 7a57ada | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 6818 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6819 | return; |
| 6820 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6821 | unsigned DiagID = diag::warn_impcast_integer_sign; |
| 6822 | |
| 6823 | // Traditionally, gcc has warned about this under -Wsign-compare. |
| 6824 | // We also want to warn about it in -Wconversion. |
| 6825 | // So if -Wconversion is off, use a completely identical diagnostic |
| 6826 | // in the sign-compare group. |
| 6827 | // The conditional-checking code will |
| 6828 | if (ICContext) { |
| 6829 | DiagID = diag::warn_impcast_integer_sign_conditional; |
| 6830 | *ICContext = true; |
| 6831 | } |
| 6832 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6833 | return DiagnoseImpCast(S, E, T, CC, DiagID); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6834 | } |
| 6835 | |
Douglas Gregor | a78f193 | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 6836 | // Diagnose conversions between different enumeration types. |
Douglas Gregor | 364f7db | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 6837 | // In C, we pretend that the type of an EnumConstantDecl is its enumeration |
| 6838 | // type, to give us better diagnostics. |
| 6839 | QualType SourceType = E->getType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6840 | if (!S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 364f7db | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 6841 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 6842 | if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 6843 | EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext()); |
| 6844 | SourceType = S.Context.getTypeDeclType(Enum); |
| 6845 | Source = S.Context.getCanonicalType(SourceType).getTypePtr(); |
| 6846 | } |
| 6847 | } |
| 6848 | |
Douglas Gregor | a78f193 | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 6849 | if (const EnumType *SourceEnum = Source->getAs<EnumType>()) |
| 6850 | if (const EnumType *TargetEnum = Target->getAs<EnumType>()) |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 6851 | if (SourceEnum->getDecl()->hasNameForLinkage() && |
| 6852 | TargetEnum->getDecl()->hasNameForLinkage() && |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6853 | SourceEnum != TargetEnum) { |
Matt Beaumont-Gay | 7a57ada | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 6854 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6855 | return; |
| 6856 | |
Douglas Gregor | 364f7db | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 6857 | return DiagnoseImpCast(S, E, SourceType, T, CC, |
Douglas Gregor | a78f193 | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 6858 | diag::warn_impcast_different_enum_types); |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 6859 | } |
Douglas Gregor | a78f193 | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 6860 | |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6861 | return; |
| 6862 | } |
| 6863 | |
David Blaikie | 18e9ac7 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 6864 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, |
| 6865 | SourceLocation CC, QualType T); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6866 | |
| 6867 | void CheckConditionalOperand(Sema &S, Expr *E, QualType T, |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6868 | SourceLocation CC, bool &ICContext) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6869 | E = E->IgnoreParenImpCasts(); |
| 6870 | |
| 6871 | if (isa<ConditionalOperator>(E)) |
David Blaikie | 18e9ac7 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 6872 | return CheckConditionalOperator(S, cast<ConditionalOperator>(E), CC, T); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6873 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6874 | AnalyzeImplicitConversions(S, E, CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6875 | if (E->getType() != T) |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6876 | return CheckImplicitConversion(S, E, T, CC, &ICContext); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6877 | return; |
| 6878 | } |
| 6879 | |
David Blaikie | 18e9ac7 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 6880 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, |
| 6881 | SourceLocation CC, QualType T) { |
Richard Trieu | bd3305b | 2014-08-07 02:09:05 +0000 | [diff] [blame] | 6882 | AnalyzeImplicitConversions(S, E->getCond(), E->getQuestionLoc()); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6883 | |
| 6884 | bool Suspicious = false; |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6885 | CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious); |
| 6886 | CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6887 | |
| 6888 | // If -Wconversion would have warned about either of the candidates |
| 6889 | // for a signedness conversion to the context type... |
| 6890 | if (!Suspicious) return; |
| 6891 | |
| 6892 | // ...but it's currently ignored... |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 6893 | if (!S.Diags.isIgnored(diag::warn_impcast_integer_sign_conditional, CC)) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6894 | return; |
| 6895 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6896 | // ...then check whether it would have warned about either of the |
| 6897 | // candidates for a signedness conversion to the condition type. |
Richard Trieu | bb43dec | 2011-07-21 02:46:28 +0000 | [diff] [blame] | 6898 | if (E->getType() == T) return; |
| 6899 | |
| 6900 | Suspicious = false; |
| 6901 | CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(), |
| 6902 | E->getType(), CC, &Suspicious); |
| 6903 | if (!Suspicious) |
| 6904 | CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(), |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6905 | E->getType(), CC, &Suspicious); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6906 | } |
| 6907 | |
Richard Trieu | 6572489 | 2014-11-15 06:37:39 +0000 | [diff] [blame] | 6908 | /// CheckBoolLikeConversion - Check conversion of given expression to boolean. |
| 6909 | /// Input argument E is a logical expression. |
| 6910 | static void CheckBoolLikeConversion(Sema &S, Expr *E, SourceLocation CC) { |
| 6911 | if (S.getLangOpts().Bool) |
| 6912 | return; |
| 6913 | CheckImplicitConversion(S, E->IgnoreParenImpCasts(), S.Context.BoolTy, CC); |
| 6914 | } |
| 6915 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6916 | /// AnalyzeImplicitConversions - Find and report any interesting |
| 6917 | /// implicit conversions in the given expression. There are a couple |
| 6918 | /// of competing diagnostics here, -Wconversion and -Wsign-compare. |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6919 | void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) { |
Fariborz Jahanian | 148c8c8 | 2014-04-07 16:32:54 +0000 | [diff] [blame] | 6920 | QualType T = OrigE->getType(); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6921 | Expr *E = OrigE->IgnoreParenImpCasts(); |
| 6922 | |
Douglas Gregor | 6e8da6a | 2011-10-10 17:38:18 +0000 | [diff] [blame] | 6923 | if (E->isTypeDependent() || E->isValueDependent()) |
| 6924 | return; |
Fariborz Jahanian | ad95da7 | 2014-04-04 19:33:39 +0000 | [diff] [blame] | 6925 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6926 | // For conditional operators, we analyze the arguments as if they |
| 6927 | // were being fed directly into the output. |
| 6928 | if (isa<ConditionalOperator>(E)) { |
| 6929 | ConditionalOperator *CO = cast<ConditionalOperator>(E); |
David Blaikie | 18e9ac7 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 6930 | CheckConditionalOperator(S, CO, CC, T); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6931 | return; |
| 6932 | } |
| 6933 | |
Hans Wennborg | f4ad232 | 2012-08-28 15:44:30 +0000 | [diff] [blame] | 6934 | // Check implicit argument conversions for function calls. |
| 6935 | if (CallExpr *Call = dyn_cast<CallExpr>(E)) |
| 6936 | CheckImplicitArgumentConversions(S, Call, CC); |
| 6937 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6938 | // Go ahead and check any implicit conversions we might have skipped. |
| 6939 | // The non-canonical typecheck is just an optimization; |
| 6940 | // CheckImplicitConversion will filter out dead implicit conversions. |
| 6941 | if (E->getType() != T) |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6942 | CheckImplicitConversion(S, E, T, CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6943 | |
| 6944 | // Now continue drilling into this expression. |
Fariborz Jahanian | 2cb4a95 | 2013-05-15 19:03:04 +0000 | [diff] [blame] | 6945 | |
| 6946 | if (PseudoObjectExpr * POE = dyn_cast<PseudoObjectExpr>(E)) { |
Fariborz Jahanian | 0b11ef2 | 2013-05-15 22:25:03 +0000 | [diff] [blame] | 6947 | if (POE->getResultExpr()) |
| 6948 | E = POE->getResultExpr(); |
Fariborz Jahanian | 2cb4a95 | 2013-05-15 19:03:04 +0000 | [diff] [blame] | 6949 | } |
| 6950 | |
Fariborz Jahanian | 947efbc | 2015-02-26 17:59:54 +0000 | [diff] [blame] | 6951 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) { |
| 6952 | if (OVE->getSourceExpr()) |
| 6953 | AnalyzeImplicitConversions(S, OVE->getSourceExpr(), CC); |
| 6954 | return; |
| 6955 | } |
Fariborz Jahanian | 0b11ef2 | 2013-05-15 22:25:03 +0000 | [diff] [blame] | 6956 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6957 | // Skip past explicit casts. |
| 6958 | if (isa<ExplicitCastExpr>(E)) { |
| 6959 | E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts(); |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6960 | return AnalyzeImplicitConversions(S, E, CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6961 | } |
| 6962 | |
John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 6963 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 6964 | // Do a somewhat different check with comparison operators. |
| 6965 | if (BO->isComparisonOp()) |
| 6966 | return AnalyzeComparison(S, BO); |
| 6967 | |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 6968 | // And with simple assignments. |
| 6969 | if (BO->getOpcode() == BO_Assign) |
John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 6970 | return AnalyzeAssignment(S, BO); |
| 6971 | } |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6972 | |
| 6973 | // These break the otherwise-useful invariant below. Fortunately, |
| 6974 | // we don't really need to recurse into them, because any internal |
| 6975 | // expressions should have been analyzed already when they were |
| 6976 | // built into statements. |
| 6977 | if (isa<StmtExpr>(E)) return; |
| 6978 | |
| 6979 | // Don't descend into unevaluated contexts. |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6980 | if (isa<UnaryExprOrTypeTraitExpr>(E)) return; |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6981 | |
| 6982 | // Now just recurse over the expression's children. |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6983 | CC = E->getExprLoc(); |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 6984 | BinaryOperator *BO = dyn_cast<BinaryOperator>(E); |
Richard Trieu | 955231d | 2014-01-25 01:10:35 +0000 | [diff] [blame] | 6985 | bool IsLogicalAndOperator = BO && BO->getOpcode() == BO_LAnd; |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 6986 | for (Stmt::child_range I = E->children(); I; ++I) { |
Douglas Gregor | 7345626 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 6987 | Expr *ChildExpr = dyn_cast_or_null<Expr>(*I); |
Douglas Gregor | 8c50e7c | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 6988 | if (!ChildExpr) |
| 6989 | continue; |
| 6990 | |
Richard Trieu | 955231d | 2014-01-25 01:10:35 +0000 | [diff] [blame] | 6991 | if (IsLogicalAndOperator && |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 6992 | isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts())) |
Richard Trieu | 955231d | 2014-01-25 01:10:35 +0000 | [diff] [blame] | 6993 | // Ignore checking string literals that are in logical and operators. |
| 6994 | // This is a common pattern for asserts. |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 6995 | continue; |
| 6996 | AnalyzeImplicitConversions(S, ChildExpr, CC); |
| 6997 | } |
Richard Trieu | 791b86e | 2014-11-19 06:08:18 +0000 | [diff] [blame] | 6998 | |
Fariborz Jahanian | b7859dd | 2014-11-14 17:12:50 +0000 | [diff] [blame] | 6999 | if (BO && BO->isLogicalOp()) { |
Richard Trieu | 791b86e | 2014-11-19 06:08:18 +0000 | [diff] [blame] | 7000 | Expr *SubExpr = BO->getLHS()->IgnoreParenImpCasts(); |
| 7001 | if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr)) |
Fariborz Jahanian | 0fc95ad | 2014-12-18 23:14:51 +0000 | [diff] [blame] | 7002 | ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc()); |
Richard Trieu | 791b86e | 2014-11-19 06:08:18 +0000 | [diff] [blame] | 7003 | |
| 7004 | SubExpr = BO->getRHS()->IgnoreParenImpCasts(); |
| 7005 | if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr)) |
Fariborz Jahanian | 0fc95ad | 2014-12-18 23:14:51 +0000 | [diff] [blame] | 7006 | ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc()); |
Fariborz Jahanian | b7859dd | 2014-11-14 17:12:50 +0000 | [diff] [blame] | 7007 | } |
Richard Trieu | 791b86e | 2014-11-19 06:08:18 +0000 | [diff] [blame] | 7008 | |
Fariborz Jahanian | b7859dd | 2014-11-14 17:12:50 +0000 | [diff] [blame] | 7009 | if (const UnaryOperator *U = dyn_cast<UnaryOperator>(E)) |
| 7010 | if (U->getOpcode() == UO_LNot) |
Richard Trieu | 6572489 | 2014-11-15 06:37:39 +0000 | [diff] [blame] | 7011 | ::CheckBoolLikeConversion(S, U->getSubExpr(), CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7012 | } |
| 7013 | |
| 7014 | } // end anonymous namespace |
| 7015 | |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7016 | enum { |
| 7017 | AddressOf, |
| 7018 | FunctionPointer, |
| 7019 | ArrayPointer |
| 7020 | }; |
| 7021 | |
Richard Trieu | c1888e0 | 2014-06-28 23:25:37 +0000 | [diff] [blame] | 7022 | // Helper function for Sema::DiagnoseAlwaysNonNullPointer. |
| 7023 | // Returns true when emitting a warning about taking the address of a reference. |
| 7024 | static bool CheckForReference(Sema &SemaRef, const Expr *E, |
| 7025 | PartialDiagnostic PD) { |
| 7026 | E = E->IgnoreParenImpCasts(); |
| 7027 | |
| 7028 | const FunctionDecl *FD = nullptr; |
| 7029 | |
| 7030 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 7031 | if (!DRE->getDecl()->getType()->isReferenceType()) |
| 7032 | return false; |
| 7033 | } else if (const MemberExpr *M = dyn_cast<MemberExpr>(E)) { |
| 7034 | if (!M->getMemberDecl()->getType()->isReferenceType()) |
| 7035 | return false; |
| 7036 | } else if (const CallExpr *Call = dyn_cast<CallExpr>(E)) { |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 7037 | if (!Call->getCallReturnType(SemaRef.Context)->isReferenceType()) |
Richard Trieu | c1888e0 | 2014-06-28 23:25:37 +0000 | [diff] [blame] | 7038 | return false; |
| 7039 | FD = Call->getDirectCallee(); |
| 7040 | } else { |
| 7041 | return false; |
| 7042 | } |
| 7043 | |
| 7044 | SemaRef.Diag(E->getExprLoc(), PD); |
| 7045 | |
| 7046 | // If possible, point to location of function. |
| 7047 | if (FD) { |
| 7048 | SemaRef.Diag(FD->getLocation(), diag::note_reference_is_return_value) << FD; |
| 7049 | } |
| 7050 | |
| 7051 | return true; |
| 7052 | } |
| 7053 | |
Richard Trieu | 4cbff5c | 2014-08-08 22:41:43 +0000 | [diff] [blame] | 7054 | // Returns true if the SourceLocation is expanded from any macro body. |
| 7055 | // Returns false if the SourceLocation is invalid, is from not in a macro |
| 7056 | // expansion, or is from expanded from a top-level macro argument. |
| 7057 | static bool IsInAnyMacroBody(const SourceManager &SM, SourceLocation Loc) { |
| 7058 | if (Loc.isInvalid()) |
| 7059 | return false; |
| 7060 | |
| 7061 | while (Loc.isMacroID()) { |
| 7062 | if (SM.isMacroBodyExpansion(Loc)) |
| 7063 | return true; |
| 7064 | Loc = SM.getImmediateMacroCallerLoc(Loc); |
| 7065 | } |
| 7066 | |
| 7067 | return false; |
| 7068 | } |
| 7069 | |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7070 | /// \brief Diagnose pointers that are always non-null. |
| 7071 | /// \param E the expression containing the pointer |
| 7072 | /// \param NullKind NPCK_NotNull if E is a cast to bool, otherwise, E is |
| 7073 | /// compared to a null pointer |
| 7074 | /// \param IsEqual True when the comparison is equal to a null pointer |
| 7075 | /// \param Range Extra SourceRange to highlight in the diagnostic |
| 7076 | void Sema::DiagnoseAlwaysNonNullPointer(Expr *E, |
| 7077 | Expr::NullPointerConstantKind NullKind, |
| 7078 | bool IsEqual, SourceRange Range) { |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 7079 | if (!E) |
| 7080 | return; |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7081 | |
| 7082 | // Don't warn inside macros. |
Richard Trieu | 4cbff5c | 2014-08-08 22:41:43 +0000 | [diff] [blame] | 7083 | if (E->getExprLoc().isMacroID()) { |
| 7084 | const SourceManager &SM = getSourceManager(); |
| 7085 | if (IsInAnyMacroBody(SM, E->getExprLoc()) || |
| 7086 | IsInAnyMacroBody(SM, Range.getBegin())) |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7087 | return; |
Richard Trieu | 4cbff5c | 2014-08-08 22:41:43 +0000 | [diff] [blame] | 7088 | } |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7089 | E = E->IgnoreImpCasts(); |
| 7090 | |
| 7091 | const bool IsCompare = NullKind != Expr::NPCK_NotNull; |
| 7092 | |
Richard Trieu | f743275 | 2014-06-06 21:39:26 +0000 | [diff] [blame] | 7093 | if (isa<CXXThisExpr>(E)) { |
| 7094 | unsigned DiagID = IsCompare ? diag::warn_this_null_compare |
| 7095 | : diag::warn_this_bool_conversion; |
| 7096 | Diag(E->getExprLoc(), DiagID) << E->getSourceRange() << Range << IsEqual; |
| 7097 | return; |
| 7098 | } |
| 7099 | |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7100 | bool IsAddressOf = false; |
| 7101 | |
| 7102 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 7103 | if (UO->getOpcode() != UO_AddrOf) |
| 7104 | return; |
| 7105 | IsAddressOf = true; |
| 7106 | E = UO->getSubExpr(); |
| 7107 | } |
| 7108 | |
Richard Trieu | c1888e0 | 2014-06-28 23:25:37 +0000 | [diff] [blame] | 7109 | if (IsAddressOf) { |
| 7110 | unsigned DiagID = IsCompare |
| 7111 | ? diag::warn_address_of_reference_null_compare |
| 7112 | : diag::warn_address_of_reference_bool_conversion; |
| 7113 | PartialDiagnostic PD = PDiag(DiagID) << E->getSourceRange() << Range |
| 7114 | << IsEqual; |
| 7115 | if (CheckForReference(*this, E, PD)) { |
| 7116 | return; |
| 7117 | } |
| 7118 | } |
| 7119 | |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7120 | // Expect to find a single Decl. Skip anything more complicated. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7121 | ValueDecl *D = nullptr; |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7122 | if (DeclRefExpr *R = dyn_cast<DeclRefExpr>(E)) { |
| 7123 | D = R->getDecl(); |
| 7124 | } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) { |
| 7125 | D = M->getMemberDecl(); |
| 7126 | } |
| 7127 | |
| 7128 | // Weak Decls can be null. |
| 7129 | if (!D || D->isWeak()) |
| 7130 | return; |
Fariborz Jahanian | ef202d9 | 2014-11-18 21:57:54 +0000 | [diff] [blame] | 7131 | |
| 7132 | // Check for parameter decl with nonnull attribute |
| 7133 | if (const ParmVarDecl* PV = dyn_cast<ParmVarDecl>(D)) { |
| 7134 | if (getCurFunction() && !getCurFunction()->ModifiedNonNullParams.count(PV)) |
| 7135 | if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) { |
| 7136 | unsigned NumArgs = FD->getNumParams(); |
| 7137 | llvm::SmallBitVector AttrNonNull(NumArgs); |
| 7138 | for (const auto *NonNull : FD->specific_attrs<NonNullAttr>()) { |
| 7139 | if (!NonNull->args_size()) { |
| 7140 | AttrNonNull.set(0, NumArgs); |
| 7141 | break; |
| 7142 | } |
| 7143 | for (unsigned Val : NonNull->args()) { |
| 7144 | if (Val >= NumArgs) |
| 7145 | continue; |
| 7146 | AttrNonNull.set(Val); |
| 7147 | } |
| 7148 | } |
| 7149 | if (!AttrNonNull.empty()) |
| 7150 | for (unsigned i = 0; i < NumArgs; ++i) |
Aaron Ballman | 2521f36 | 2014-12-11 19:35:42 +0000 | [diff] [blame] | 7151 | if (FD->getParamDecl(i) == PV && |
| 7152 | (AttrNonNull[i] || PV->hasAttr<NonNullAttr>())) { |
Fariborz Jahanian | ef202d9 | 2014-11-18 21:57:54 +0000 | [diff] [blame] | 7153 | std::string Str; |
| 7154 | llvm::raw_string_ostream S(Str); |
| 7155 | E->printPretty(S, nullptr, getPrintingPolicy()); |
| 7156 | unsigned DiagID = IsCompare ? diag::warn_nonnull_parameter_compare |
| 7157 | : diag::warn_cast_nonnull_to_bool; |
| 7158 | Diag(E->getExprLoc(), DiagID) << S.str() << E->getSourceRange() |
| 7159 | << Range << IsEqual; |
| 7160 | return; |
| 7161 | } |
| 7162 | } |
| 7163 | } |
| 7164 | |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7165 | QualType T = D->getType(); |
| 7166 | const bool IsArray = T->isArrayType(); |
| 7167 | const bool IsFunction = T->isFunctionType(); |
| 7168 | |
Richard Trieu | c1888e0 | 2014-06-28 23:25:37 +0000 | [diff] [blame] | 7169 | // Address of function is used to silence the function warning. |
| 7170 | if (IsAddressOf && IsFunction) { |
| 7171 | return; |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7172 | } |
| 7173 | |
| 7174 | // Found nothing. |
| 7175 | if (!IsAddressOf && !IsFunction && !IsArray) |
| 7176 | return; |
| 7177 | |
| 7178 | // Pretty print the expression for the diagnostic. |
| 7179 | std::string Str; |
| 7180 | llvm::raw_string_ostream S(Str); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7181 | E->printPretty(S, nullptr, getPrintingPolicy()); |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7182 | |
| 7183 | unsigned DiagID = IsCompare ? diag::warn_null_pointer_compare |
| 7184 | : diag::warn_impcast_pointer_to_bool; |
| 7185 | unsigned DiagType; |
| 7186 | if (IsAddressOf) |
| 7187 | DiagType = AddressOf; |
| 7188 | else if (IsFunction) |
| 7189 | DiagType = FunctionPointer; |
| 7190 | else if (IsArray) |
| 7191 | DiagType = ArrayPointer; |
| 7192 | else |
| 7193 | llvm_unreachable("Could not determine diagnostic."); |
| 7194 | Diag(E->getExprLoc(), DiagID) << DiagType << S.str() << E->getSourceRange() |
| 7195 | << Range << IsEqual; |
| 7196 | |
| 7197 | if (!IsFunction) |
| 7198 | return; |
| 7199 | |
| 7200 | // Suggest '&' to silence the function warning. |
| 7201 | Diag(E->getExprLoc(), diag::note_function_warning_silence) |
| 7202 | << FixItHint::CreateInsertion(E->getLocStart(), "&"); |
| 7203 | |
| 7204 | // Check to see if '()' fixit should be emitted. |
| 7205 | QualType ReturnType; |
| 7206 | UnresolvedSet<4> NonTemplateOverloads; |
| 7207 | tryExprAsCall(*E, ReturnType, NonTemplateOverloads); |
| 7208 | if (ReturnType.isNull()) |
| 7209 | return; |
| 7210 | |
| 7211 | if (IsCompare) { |
| 7212 | // There are two cases here. If there is null constant, the only suggest |
| 7213 | // for a pointer return type. If the null is 0, then suggest if the return |
| 7214 | // type is a pointer or an integer type. |
| 7215 | if (!ReturnType->isPointerType()) { |
| 7216 | if (NullKind == Expr::NPCK_ZeroExpression || |
| 7217 | NullKind == Expr::NPCK_ZeroLiteral) { |
| 7218 | if (!ReturnType->isIntegerType()) |
| 7219 | return; |
| 7220 | } else { |
| 7221 | return; |
| 7222 | } |
| 7223 | } |
| 7224 | } else { // !IsCompare |
| 7225 | // For function to bool, only suggest if the function pointer has bool |
| 7226 | // return type. |
| 7227 | if (!ReturnType->isSpecificBuiltinType(BuiltinType::Bool)) |
| 7228 | return; |
| 7229 | } |
| 7230 | Diag(E->getExprLoc(), diag::note_function_to_function_call) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 7231 | << FixItHint::CreateInsertion(getLocForEndOfToken(E->getLocEnd()), "()"); |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7232 | } |
| 7233 | |
| 7234 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7235 | /// Diagnoses "dangerous" implicit conversions within the given |
| 7236 | /// expression (which is a full expression). Implements -Wconversion |
| 7237 | /// and -Wsign-compare. |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7238 | /// |
| 7239 | /// \param CC the "context" location of the implicit conversion, i.e. |
| 7240 | /// the most location of the syntactic entity requiring the implicit |
| 7241 | /// conversion |
| 7242 | void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7243 | // Don't diagnose in unevaluated contexts. |
David Blaikie | 131fcb4 | 2012-08-06 22:47:24 +0000 | [diff] [blame] | 7244 | if (isUnevaluatedContext()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7245 | return; |
| 7246 | |
| 7247 | // Don't diagnose for value- or type-dependent expressions. |
| 7248 | if (E->isTypeDependent() || E->isValueDependent()) |
| 7249 | return; |
| 7250 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7251 | // Check for array bounds violations in cases where the check isn't triggered |
| 7252 | // elsewhere for other Expr types (like BinaryOperators), e.g. when an |
| 7253 | // ArraySubscriptExpr is on the RHS of a variable initialization. |
| 7254 | CheckArrayAccess(E); |
| 7255 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7256 | // This is not the right CC for (e.g.) a variable initialization. |
| 7257 | AnalyzeImplicitConversions(*this, E, CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7258 | } |
| 7259 | |
Richard Trieu | 6572489 | 2014-11-15 06:37:39 +0000 | [diff] [blame] | 7260 | /// CheckBoolLikeConversion - Check conversion of given expression to boolean. |
| 7261 | /// Input argument E is a logical expression. |
| 7262 | void Sema::CheckBoolLikeConversion(Expr *E, SourceLocation CC) { |
| 7263 | ::CheckBoolLikeConversion(*this, E, CC); |
| 7264 | } |
| 7265 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 7266 | /// Diagnose when expression is an integer constant expression and its evaluation |
| 7267 | /// results in integer overflow |
| 7268 | void Sema::CheckForIntOverflow (Expr *E) { |
Fariborz Jahanian | c694e69 | 2014-10-14 20:27:05 +0000 | [diff] [blame] | 7269 | if (isa<BinaryOperator>(E->IgnoreParenCasts())) |
| 7270 | E->IgnoreParenCasts()->EvaluateForOverflow(Context); |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 7271 | } |
| 7272 | |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7273 | namespace { |
| 7274 | /// \brief Visitor for expressions which looks for unsequenced operations on the |
| 7275 | /// same object. |
| 7276 | class SequenceChecker : public EvaluatedExprVisitor<SequenceChecker> { |
Richard Smith | e3dbfe0 | 2013-06-30 10:40:20 +0000 | [diff] [blame] | 7277 | typedef EvaluatedExprVisitor<SequenceChecker> Base; |
| 7278 | |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7279 | /// \brief A tree of sequenced regions within an expression. Two regions are |
| 7280 | /// unsequenced if one is an ancestor or a descendent of the other. When we |
| 7281 | /// finish processing an expression with sequencing, such as a comma |
| 7282 | /// expression, we fold its tree nodes into its parent, since they are |
| 7283 | /// unsequenced with respect to nodes we will visit later. |
| 7284 | class SequenceTree { |
| 7285 | struct Value { |
| 7286 | explicit Value(unsigned Parent) : Parent(Parent), Merged(false) {} |
| 7287 | unsigned Parent : 31; |
| 7288 | bool Merged : 1; |
| 7289 | }; |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 7290 | SmallVector<Value, 8> Values; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7291 | |
| 7292 | public: |
| 7293 | /// \brief A region within an expression which may be sequenced with respect |
| 7294 | /// to some other region. |
| 7295 | class Seq { |
| 7296 | explicit Seq(unsigned N) : Index(N) {} |
| 7297 | unsigned Index; |
| 7298 | friend class SequenceTree; |
| 7299 | public: |
| 7300 | Seq() : Index(0) {} |
| 7301 | }; |
| 7302 | |
| 7303 | SequenceTree() { Values.push_back(Value(0)); } |
| 7304 | Seq root() const { return Seq(0); } |
| 7305 | |
| 7306 | /// \brief Create a new sequence of operations, which is an unsequenced |
| 7307 | /// subset of \p Parent. This sequence of operations is sequenced with |
| 7308 | /// respect to other children of \p Parent. |
| 7309 | Seq allocate(Seq Parent) { |
| 7310 | Values.push_back(Value(Parent.Index)); |
| 7311 | return Seq(Values.size() - 1); |
| 7312 | } |
| 7313 | |
| 7314 | /// \brief Merge a sequence of operations into its parent. |
| 7315 | void merge(Seq S) { |
| 7316 | Values[S.Index].Merged = true; |
| 7317 | } |
| 7318 | |
| 7319 | /// \brief Determine whether two operations are unsequenced. This operation |
| 7320 | /// is asymmetric: \p Cur should be the more recent sequence, and \p Old |
| 7321 | /// should have been merged into its parent as appropriate. |
| 7322 | bool isUnsequenced(Seq Cur, Seq Old) { |
| 7323 | unsigned C = representative(Cur.Index); |
| 7324 | unsigned Target = representative(Old.Index); |
| 7325 | while (C >= Target) { |
| 7326 | if (C == Target) |
| 7327 | return true; |
| 7328 | C = Values[C].Parent; |
| 7329 | } |
| 7330 | return false; |
| 7331 | } |
| 7332 | |
| 7333 | private: |
| 7334 | /// \brief Pick a representative for a sequence. |
| 7335 | unsigned representative(unsigned K) { |
| 7336 | if (Values[K].Merged) |
| 7337 | // Perform path compression as we go. |
| 7338 | return Values[K].Parent = representative(Values[K].Parent); |
| 7339 | return K; |
| 7340 | } |
| 7341 | }; |
| 7342 | |
| 7343 | /// An object for which we can track unsequenced uses. |
| 7344 | typedef NamedDecl *Object; |
| 7345 | |
| 7346 | /// Different flavors of object usage which we track. We only track the |
| 7347 | /// least-sequenced usage of each kind. |
| 7348 | enum UsageKind { |
| 7349 | /// A read of an object. Multiple unsequenced reads are OK. |
| 7350 | UK_Use, |
| 7351 | /// A modification of an object which is sequenced before the value |
Richard Smith | 83e37bee | 2013-06-26 23:16:51 +0000 | [diff] [blame] | 7352 | /// computation of the expression, such as ++n in C++. |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7353 | UK_ModAsValue, |
| 7354 | /// A modification of an object which is not sequenced before the value |
| 7355 | /// computation of the expression, such as n++. |
| 7356 | UK_ModAsSideEffect, |
| 7357 | |
| 7358 | UK_Count = UK_ModAsSideEffect + 1 |
| 7359 | }; |
| 7360 | |
| 7361 | struct Usage { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7362 | Usage() : Use(nullptr), Seq() {} |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7363 | Expr *Use; |
| 7364 | SequenceTree::Seq Seq; |
| 7365 | }; |
| 7366 | |
| 7367 | struct UsageInfo { |
| 7368 | UsageInfo() : Diagnosed(false) {} |
| 7369 | Usage Uses[UK_Count]; |
| 7370 | /// Have we issued a diagnostic for this variable already? |
| 7371 | bool Diagnosed; |
| 7372 | }; |
| 7373 | typedef llvm::SmallDenseMap<Object, UsageInfo, 16> UsageInfoMap; |
| 7374 | |
| 7375 | Sema &SemaRef; |
| 7376 | /// Sequenced regions within the expression. |
| 7377 | SequenceTree Tree; |
| 7378 | /// Declaration modifications and references which we have seen. |
| 7379 | UsageInfoMap UsageMap; |
| 7380 | /// The region we are currently within. |
| 7381 | SequenceTree::Seq Region; |
| 7382 | /// Filled in with declarations which were modified as a side-effect |
| 7383 | /// (that is, post-increment operations). |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 7384 | SmallVectorImpl<std::pair<Object, Usage> > *ModAsSideEffect; |
Richard Smith | d33f520 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 7385 | /// Expressions to check later. We defer checking these to reduce |
| 7386 | /// stack usage. |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 7387 | SmallVectorImpl<Expr *> &WorkList; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7388 | |
| 7389 | /// RAII object wrapping the visitation of a sequenced subexpression of an |
| 7390 | /// expression. At the end of this process, the side-effects of the evaluation |
| 7391 | /// become sequenced with respect to the value computation of the result, so |
| 7392 | /// we downgrade any UK_ModAsSideEffect within the evaluation to |
| 7393 | /// UK_ModAsValue. |
| 7394 | struct SequencedSubexpression { |
| 7395 | SequencedSubexpression(SequenceChecker &Self) |
| 7396 | : Self(Self), OldModAsSideEffect(Self.ModAsSideEffect) { |
| 7397 | Self.ModAsSideEffect = &ModAsSideEffect; |
| 7398 | } |
| 7399 | ~SequencedSubexpression() { |
Richard Smith | e8efd99 | 2014-12-03 01:05:50 +0000 | [diff] [blame] | 7400 | for (auto MI = ModAsSideEffect.rbegin(), ME = ModAsSideEffect.rend(); |
| 7401 | MI != ME; ++MI) { |
| 7402 | UsageInfo &U = Self.UsageMap[MI->first]; |
| 7403 | auto &SideEffectUsage = U.Uses[UK_ModAsSideEffect]; |
| 7404 | Self.addUsage(U, MI->first, SideEffectUsage.Use, UK_ModAsValue); |
| 7405 | SideEffectUsage = MI->second; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7406 | } |
| 7407 | Self.ModAsSideEffect = OldModAsSideEffect; |
| 7408 | } |
| 7409 | |
| 7410 | SequenceChecker &Self; |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 7411 | SmallVector<std::pair<Object, Usage>, 4> ModAsSideEffect; |
| 7412 | SmallVectorImpl<std::pair<Object, Usage> > *OldModAsSideEffect; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7413 | }; |
| 7414 | |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 7415 | /// RAII object wrapping the visitation of a subexpression which we might |
| 7416 | /// choose to evaluate as a constant. If any subexpression is evaluated and |
| 7417 | /// found to be non-constant, this allows us to suppress the evaluation of |
| 7418 | /// the outer expression. |
| 7419 | class EvaluationTracker { |
| 7420 | public: |
| 7421 | EvaluationTracker(SequenceChecker &Self) |
| 7422 | : Self(Self), Prev(Self.EvalTracker), EvalOK(true) { |
| 7423 | Self.EvalTracker = this; |
| 7424 | } |
| 7425 | ~EvaluationTracker() { |
| 7426 | Self.EvalTracker = Prev; |
| 7427 | if (Prev) |
| 7428 | Prev->EvalOK &= EvalOK; |
| 7429 | } |
| 7430 | |
| 7431 | bool evaluate(const Expr *E, bool &Result) { |
| 7432 | if (!EvalOK || E->isValueDependent()) |
| 7433 | return false; |
| 7434 | EvalOK = E->EvaluateAsBooleanCondition(Result, Self.SemaRef.Context); |
| 7435 | return EvalOK; |
| 7436 | } |
| 7437 | |
| 7438 | private: |
| 7439 | SequenceChecker &Self; |
| 7440 | EvaluationTracker *Prev; |
| 7441 | bool EvalOK; |
| 7442 | } *EvalTracker; |
| 7443 | |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7444 | /// \brief Find the object which is produced by the specified expression, |
| 7445 | /// if any. |
| 7446 | Object getObject(Expr *E, bool Mod) const { |
| 7447 | E = E->IgnoreParenCasts(); |
| 7448 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 7449 | if (Mod && (UO->getOpcode() == UO_PreInc || UO->getOpcode() == UO_PreDec)) |
| 7450 | return getObject(UO->getSubExpr(), Mod); |
| 7451 | } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 7452 | if (BO->getOpcode() == BO_Comma) |
| 7453 | return getObject(BO->getRHS(), Mod); |
| 7454 | if (Mod && BO->isAssignmentOp()) |
| 7455 | return getObject(BO->getLHS(), Mod); |
| 7456 | } else if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 7457 | // FIXME: Check for more interesting cases, like "x.n = ++x.n". |
| 7458 | if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenCasts())) |
| 7459 | return ME->getMemberDecl(); |
| 7460 | } else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 7461 | // FIXME: If this is a reference, map through to its value. |
| 7462 | return DRE->getDecl(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7463 | return nullptr; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7464 | } |
| 7465 | |
| 7466 | /// \brief Note that an object was modified or used by an expression. |
| 7467 | void addUsage(UsageInfo &UI, Object O, Expr *Ref, UsageKind UK) { |
| 7468 | Usage &U = UI.Uses[UK]; |
| 7469 | if (!U.Use || !Tree.isUnsequenced(Region, U.Seq)) { |
| 7470 | if (UK == UK_ModAsSideEffect && ModAsSideEffect) |
| 7471 | ModAsSideEffect->push_back(std::make_pair(O, U)); |
| 7472 | U.Use = Ref; |
| 7473 | U.Seq = Region; |
| 7474 | } |
| 7475 | } |
| 7476 | /// \brief Check whether a modification or use conflicts with a prior usage. |
| 7477 | void checkUsage(Object O, UsageInfo &UI, Expr *Ref, UsageKind OtherKind, |
| 7478 | bool IsModMod) { |
| 7479 | if (UI.Diagnosed) |
| 7480 | return; |
| 7481 | |
| 7482 | const Usage &U = UI.Uses[OtherKind]; |
| 7483 | if (!U.Use || !Tree.isUnsequenced(Region, U.Seq)) |
| 7484 | return; |
| 7485 | |
| 7486 | Expr *Mod = U.Use; |
| 7487 | Expr *ModOrUse = Ref; |
| 7488 | if (OtherKind == UK_Use) |
| 7489 | std::swap(Mod, ModOrUse); |
| 7490 | |
| 7491 | SemaRef.Diag(Mod->getExprLoc(), |
| 7492 | IsModMod ? diag::warn_unsequenced_mod_mod |
| 7493 | : diag::warn_unsequenced_mod_use) |
| 7494 | << O << SourceRange(ModOrUse->getExprLoc()); |
| 7495 | UI.Diagnosed = true; |
| 7496 | } |
| 7497 | |
| 7498 | void notePreUse(Object O, Expr *Use) { |
| 7499 | UsageInfo &U = UsageMap[O]; |
| 7500 | // Uses conflict with other modifications. |
| 7501 | checkUsage(O, U, Use, UK_ModAsValue, false); |
| 7502 | } |
| 7503 | void notePostUse(Object O, Expr *Use) { |
| 7504 | UsageInfo &U = UsageMap[O]; |
| 7505 | checkUsage(O, U, Use, UK_ModAsSideEffect, false); |
| 7506 | addUsage(U, O, Use, UK_Use); |
| 7507 | } |
| 7508 | |
| 7509 | void notePreMod(Object O, Expr *Mod) { |
| 7510 | UsageInfo &U = UsageMap[O]; |
| 7511 | // Modifications conflict with other modifications and with uses. |
| 7512 | checkUsage(O, U, Mod, UK_ModAsValue, true); |
| 7513 | checkUsage(O, U, Mod, UK_Use, false); |
| 7514 | } |
| 7515 | void notePostMod(Object O, Expr *Use, UsageKind UK) { |
| 7516 | UsageInfo &U = UsageMap[O]; |
| 7517 | checkUsage(O, U, Use, UK_ModAsSideEffect, true); |
| 7518 | addUsage(U, O, Use, UK); |
| 7519 | } |
| 7520 | |
| 7521 | public: |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 7522 | SequenceChecker(Sema &S, Expr *E, SmallVectorImpl<Expr *> &WorkList) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7523 | : Base(S.Context), SemaRef(S), Region(Tree.root()), |
| 7524 | ModAsSideEffect(nullptr), WorkList(WorkList), EvalTracker(nullptr) { |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7525 | Visit(E); |
| 7526 | } |
| 7527 | |
| 7528 | void VisitStmt(Stmt *S) { |
| 7529 | // Skip all statements which aren't expressions for now. |
| 7530 | } |
| 7531 | |
| 7532 | void VisitExpr(Expr *E) { |
| 7533 | // By default, just recurse to evaluated subexpressions. |
Richard Smith | e3dbfe0 | 2013-06-30 10:40:20 +0000 | [diff] [blame] | 7534 | Base::VisitStmt(E); |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7535 | } |
| 7536 | |
| 7537 | void VisitCastExpr(CastExpr *E) { |
| 7538 | Object O = Object(); |
| 7539 | if (E->getCastKind() == CK_LValueToRValue) |
| 7540 | O = getObject(E->getSubExpr(), false); |
| 7541 | |
| 7542 | if (O) |
| 7543 | notePreUse(O, E); |
| 7544 | VisitExpr(E); |
| 7545 | if (O) |
| 7546 | notePostUse(O, E); |
| 7547 | } |
| 7548 | |
| 7549 | void VisitBinComma(BinaryOperator *BO) { |
| 7550 | // C++11 [expr.comma]p1: |
| 7551 | // Every value computation and side effect associated with the left |
| 7552 | // expression is sequenced before every value computation and side |
| 7553 | // effect associated with the right expression. |
| 7554 | SequenceTree::Seq LHS = Tree.allocate(Region); |
| 7555 | SequenceTree::Seq RHS = Tree.allocate(Region); |
| 7556 | SequenceTree::Seq OldRegion = Region; |
| 7557 | |
| 7558 | { |
| 7559 | SequencedSubexpression SeqLHS(*this); |
| 7560 | Region = LHS; |
| 7561 | Visit(BO->getLHS()); |
| 7562 | } |
| 7563 | |
| 7564 | Region = RHS; |
| 7565 | Visit(BO->getRHS()); |
| 7566 | |
| 7567 | Region = OldRegion; |
| 7568 | |
| 7569 | // Forget that LHS and RHS are sequenced. They are both unsequenced |
| 7570 | // with respect to other stuff. |
| 7571 | Tree.merge(LHS); |
| 7572 | Tree.merge(RHS); |
| 7573 | } |
| 7574 | |
| 7575 | void VisitBinAssign(BinaryOperator *BO) { |
| 7576 | // The modification is sequenced after the value computation of the LHS |
| 7577 | // and RHS, so check it before inspecting the operands and update the |
| 7578 | // map afterwards. |
| 7579 | Object O = getObject(BO->getLHS(), true); |
| 7580 | if (!O) |
| 7581 | return VisitExpr(BO); |
| 7582 | |
| 7583 | notePreMod(O, BO); |
| 7584 | |
| 7585 | // C++11 [expr.ass]p7: |
| 7586 | // E1 op= E2 is equivalent to E1 = E1 op E2, except that E1 is evaluated |
| 7587 | // only once. |
| 7588 | // |
| 7589 | // Therefore, for a compound assignment operator, O is considered used |
| 7590 | // everywhere except within the evaluation of E1 itself. |
| 7591 | if (isa<CompoundAssignOperator>(BO)) |
| 7592 | notePreUse(O, BO); |
| 7593 | |
| 7594 | Visit(BO->getLHS()); |
| 7595 | |
| 7596 | if (isa<CompoundAssignOperator>(BO)) |
| 7597 | notePostUse(O, BO); |
| 7598 | |
| 7599 | Visit(BO->getRHS()); |
| 7600 | |
Richard Smith | 83e37bee | 2013-06-26 23:16:51 +0000 | [diff] [blame] | 7601 | // C++11 [expr.ass]p1: |
| 7602 | // the assignment is sequenced [...] before the value computation of the |
| 7603 | // assignment expression. |
| 7604 | // C11 6.5.16/3 has no such rule. |
| 7605 | notePostMod(O, BO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue |
| 7606 | : UK_ModAsSideEffect); |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7607 | } |
| 7608 | void VisitCompoundAssignOperator(CompoundAssignOperator *CAO) { |
| 7609 | VisitBinAssign(CAO); |
| 7610 | } |
| 7611 | |
| 7612 | void VisitUnaryPreInc(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } |
| 7613 | void VisitUnaryPreDec(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } |
| 7614 | void VisitUnaryPreIncDec(UnaryOperator *UO) { |
| 7615 | Object O = getObject(UO->getSubExpr(), true); |
| 7616 | if (!O) |
| 7617 | return VisitExpr(UO); |
| 7618 | |
| 7619 | notePreMod(O, UO); |
| 7620 | Visit(UO->getSubExpr()); |
Richard Smith | 83e37bee | 2013-06-26 23:16:51 +0000 | [diff] [blame] | 7621 | // C++11 [expr.pre.incr]p1: |
| 7622 | // the expression ++x is equivalent to x+=1 |
| 7623 | notePostMod(O, UO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue |
| 7624 | : UK_ModAsSideEffect); |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7625 | } |
| 7626 | |
| 7627 | void VisitUnaryPostInc(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } |
| 7628 | void VisitUnaryPostDec(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } |
| 7629 | void VisitUnaryPostIncDec(UnaryOperator *UO) { |
| 7630 | Object O = getObject(UO->getSubExpr(), true); |
| 7631 | if (!O) |
| 7632 | return VisitExpr(UO); |
| 7633 | |
| 7634 | notePreMod(O, UO); |
| 7635 | Visit(UO->getSubExpr()); |
| 7636 | notePostMod(O, UO, UK_ModAsSideEffect); |
| 7637 | } |
| 7638 | |
| 7639 | /// Don't visit the RHS of '&&' or '||' if it might not be evaluated. |
| 7640 | void VisitBinLOr(BinaryOperator *BO) { |
| 7641 | // The side-effects of the LHS of an '&&' are sequenced before the |
| 7642 | // value computation of the RHS, and hence before the value computation |
| 7643 | // of the '&&' itself, unless the LHS evaluates to zero. We treat them |
| 7644 | // as if they were unconditionally sequenced. |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 7645 | EvaluationTracker Eval(*this); |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7646 | { |
| 7647 | SequencedSubexpression Sequenced(*this); |
| 7648 | Visit(BO->getLHS()); |
| 7649 | } |
| 7650 | |
| 7651 | bool Result; |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 7652 | if (Eval.evaluate(BO->getLHS(), Result)) { |
Richard Smith | 01a7fba | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 7653 | if (!Result) |
| 7654 | Visit(BO->getRHS()); |
| 7655 | } else { |
| 7656 | // Check for unsequenced operations in the RHS, treating it as an |
| 7657 | // entirely separate evaluation. |
| 7658 | // |
| 7659 | // FIXME: If there are operations in the RHS which are unsequenced |
| 7660 | // with respect to operations outside the RHS, and those operations |
| 7661 | // are unconditionally evaluated, diagnose them. |
Richard Smith | d33f520 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 7662 | WorkList.push_back(BO->getRHS()); |
Richard Smith | 01a7fba | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 7663 | } |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7664 | } |
| 7665 | void VisitBinLAnd(BinaryOperator *BO) { |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 7666 | EvaluationTracker Eval(*this); |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7667 | { |
| 7668 | SequencedSubexpression Sequenced(*this); |
| 7669 | Visit(BO->getLHS()); |
| 7670 | } |
| 7671 | |
| 7672 | bool Result; |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 7673 | if (Eval.evaluate(BO->getLHS(), Result)) { |
Richard Smith | 01a7fba | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 7674 | if (Result) |
| 7675 | Visit(BO->getRHS()); |
| 7676 | } else { |
Richard Smith | d33f520 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 7677 | WorkList.push_back(BO->getRHS()); |
Richard Smith | 01a7fba | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 7678 | } |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7679 | } |
| 7680 | |
| 7681 | // Only visit the condition, unless we can be sure which subexpression will |
| 7682 | // be chosen. |
| 7683 | void VisitAbstractConditionalOperator(AbstractConditionalOperator *CO) { |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 7684 | EvaluationTracker Eval(*this); |
Richard Smith | 83e37bee | 2013-06-26 23:16:51 +0000 | [diff] [blame] | 7685 | { |
| 7686 | SequencedSubexpression Sequenced(*this); |
| 7687 | Visit(CO->getCond()); |
| 7688 | } |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7689 | |
| 7690 | bool Result; |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 7691 | if (Eval.evaluate(CO->getCond(), Result)) |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7692 | Visit(Result ? CO->getTrueExpr() : CO->getFalseExpr()); |
Richard Smith | 01a7fba | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 7693 | else { |
Richard Smith | d33f520 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 7694 | WorkList.push_back(CO->getTrueExpr()); |
| 7695 | WorkList.push_back(CO->getFalseExpr()); |
Richard Smith | 01a7fba | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 7696 | } |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7697 | } |
| 7698 | |
Richard Smith | e3dbfe0 | 2013-06-30 10:40:20 +0000 | [diff] [blame] | 7699 | void VisitCallExpr(CallExpr *CE) { |
| 7700 | // C++11 [intro.execution]p15: |
| 7701 | // When calling a function [...], every value computation and side effect |
| 7702 | // associated with any argument expression, or with the postfix expression |
| 7703 | // designating the called function, is sequenced before execution of every |
| 7704 | // expression or statement in the body of the function [and thus before |
| 7705 | // the value computation of its result]. |
| 7706 | SequencedSubexpression Sequenced(*this); |
| 7707 | Base::VisitCallExpr(CE); |
| 7708 | |
| 7709 | // FIXME: CXXNewExpr and CXXDeleteExpr implicitly call functions. |
| 7710 | } |
| 7711 | |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7712 | void VisitCXXConstructExpr(CXXConstructExpr *CCE) { |
Richard Smith | e3dbfe0 | 2013-06-30 10:40:20 +0000 | [diff] [blame] | 7713 | // This is a call, so all subexpressions are sequenced before the result. |
| 7714 | SequencedSubexpression Sequenced(*this); |
| 7715 | |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7716 | if (!CCE->isListInitialization()) |
| 7717 | return VisitExpr(CCE); |
| 7718 | |
| 7719 | // In C++11, list initializations are sequenced. |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 7720 | SmallVector<SequenceTree::Seq, 32> Elts; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7721 | SequenceTree::Seq Parent = Region; |
| 7722 | for (CXXConstructExpr::arg_iterator I = CCE->arg_begin(), |
| 7723 | E = CCE->arg_end(); |
| 7724 | I != E; ++I) { |
| 7725 | Region = Tree.allocate(Parent); |
| 7726 | Elts.push_back(Region); |
| 7727 | Visit(*I); |
| 7728 | } |
| 7729 | |
| 7730 | // Forget that the initializers are sequenced. |
| 7731 | Region = Parent; |
| 7732 | for (unsigned I = 0; I < Elts.size(); ++I) |
| 7733 | Tree.merge(Elts[I]); |
| 7734 | } |
| 7735 | |
| 7736 | void VisitInitListExpr(InitListExpr *ILE) { |
| 7737 | if (!SemaRef.getLangOpts().CPlusPlus11) |
| 7738 | return VisitExpr(ILE); |
| 7739 | |
| 7740 | // In C++11, list initializations are sequenced. |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 7741 | SmallVector<SequenceTree::Seq, 32> Elts; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7742 | SequenceTree::Seq Parent = Region; |
| 7743 | for (unsigned I = 0; I < ILE->getNumInits(); ++I) { |
| 7744 | Expr *E = ILE->getInit(I); |
| 7745 | if (!E) continue; |
| 7746 | Region = Tree.allocate(Parent); |
| 7747 | Elts.push_back(Region); |
| 7748 | Visit(E); |
| 7749 | } |
| 7750 | |
| 7751 | // Forget that the initializers are sequenced. |
| 7752 | Region = Parent; |
| 7753 | for (unsigned I = 0; I < Elts.size(); ++I) |
| 7754 | Tree.merge(Elts[I]); |
| 7755 | } |
| 7756 | }; |
| 7757 | } |
| 7758 | |
| 7759 | void Sema::CheckUnsequencedOperations(Expr *E) { |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 7760 | SmallVector<Expr *, 8> WorkList; |
Richard Smith | d33f520 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 7761 | WorkList.push_back(E); |
| 7762 | while (!WorkList.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 7763 | Expr *Item = WorkList.pop_back_val(); |
Richard Smith | d33f520 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 7764 | SequenceChecker(*this, Item, WorkList); |
| 7765 | } |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7766 | } |
| 7767 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 7768 | void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc, |
| 7769 | bool IsConstexpr) { |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7770 | CheckImplicitConversions(E, CheckLoc); |
| 7771 | CheckUnsequencedOperations(E); |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 7772 | if (!IsConstexpr && !E->isValueDependent()) |
| 7773 | CheckForIntOverflow(E); |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 7774 | } |
| 7775 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7776 | void Sema::CheckBitFieldInitialization(SourceLocation InitLoc, |
| 7777 | FieldDecl *BitField, |
| 7778 | Expr *Init) { |
| 7779 | (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc); |
| 7780 | } |
| 7781 | |
David Majnemer | 61a5bbf | 2015-04-07 22:08:51 +0000 | [diff] [blame] | 7782 | static void diagnoseArrayStarInParamType(Sema &S, QualType PType, |
| 7783 | SourceLocation Loc) { |
| 7784 | if (!PType->isVariablyModifiedType()) |
| 7785 | return; |
| 7786 | if (const auto *PointerTy = dyn_cast<PointerType>(PType)) { |
| 7787 | diagnoseArrayStarInParamType(S, PointerTy->getPointeeType(), Loc); |
| 7788 | return; |
| 7789 | } |
David Majnemer | df8f73f | 2015-04-09 19:53:25 +0000 | [diff] [blame] | 7790 | if (const auto *ReferenceTy = dyn_cast<ReferenceType>(PType)) { |
| 7791 | diagnoseArrayStarInParamType(S, ReferenceTy->getPointeeType(), Loc); |
| 7792 | return; |
| 7793 | } |
David Majnemer | 61a5bbf | 2015-04-07 22:08:51 +0000 | [diff] [blame] | 7794 | if (const auto *ParenTy = dyn_cast<ParenType>(PType)) { |
| 7795 | diagnoseArrayStarInParamType(S, ParenTy->getInnerType(), Loc); |
| 7796 | return; |
| 7797 | } |
| 7798 | |
| 7799 | const ArrayType *AT = S.Context.getAsArrayType(PType); |
| 7800 | if (!AT) |
| 7801 | return; |
| 7802 | |
| 7803 | if (AT->getSizeModifier() != ArrayType::Star) { |
| 7804 | diagnoseArrayStarInParamType(S, AT->getElementType(), Loc); |
| 7805 | return; |
| 7806 | } |
| 7807 | |
| 7808 | S.Diag(Loc, diag::err_array_star_in_function_definition); |
| 7809 | } |
| 7810 | |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 7811 | /// CheckParmsForFunctionDef - Check that the parameters of the given |
| 7812 | /// function are appropriate for the definition of a function. This |
| 7813 | /// takes care of any checks that cannot be performed on the |
| 7814 | /// declaration itself, e.g., that the types of each of the function |
| 7815 | /// parameters are complete. |
Reid Kleckner | 5a11580 | 2013-06-24 14:38:26 +0000 | [diff] [blame] | 7816 | bool Sema::CheckParmsForFunctionDef(ParmVarDecl *const *P, |
| 7817 | ParmVarDecl *const *PEnd, |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 7818 | bool CheckParameterNames) { |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 7819 | bool HasInvalidParm = false; |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 7820 | for (; P != PEnd; ++P) { |
| 7821 | ParmVarDecl *Param = *P; |
| 7822 | |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 7823 | // C99 6.7.5.3p4: the parameters in a parameter type list in a |
| 7824 | // function declarator that is part of a function definition of |
| 7825 | // that function shall not have incomplete type. |
| 7826 | // |
| 7827 | // This is also C++ [dcl.fct]p6. |
| 7828 | if (!Param->isInvalidDecl() && |
| 7829 | RequireCompleteType(Param->getLocation(), Param->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 7830 | diag::err_typecheck_decl_incomplete_type)) { |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 7831 | Param->setInvalidDecl(); |
| 7832 | HasInvalidParm = true; |
| 7833 | } |
| 7834 | |
| 7835 | // C99 6.9.1p5: If the declarator includes a parameter type list, the |
| 7836 | // declaration of each parameter shall include an identifier. |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 7837 | if (CheckParameterNames && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7838 | Param->getIdentifier() == nullptr && |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 7839 | !Param->isImplicit() && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7840 | !getLangOpts().CPlusPlus) |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 7841 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
Sam Weinig | deb55d5 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 7842 | |
| 7843 | // C99 6.7.5.3p12: |
| 7844 | // If the function declarator is not part of a definition of that |
| 7845 | // function, parameters may have incomplete type and may use the [*] |
| 7846 | // notation in their sequences of declarator specifiers to specify |
| 7847 | // variable length array types. |
| 7848 | QualType PType = Param->getOriginalType(); |
David Majnemer | 61a5bbf | 2015-04-07 22:08:51 +0000 | [diff] [blame] | 7849 | // FIXME: This diagnostic should point the '[*]' if source-location |
| 7850 | // information is added for it. |
| 7851 | diagnoseArrayStarInParamType(*this, PType, Param->getLocation()); |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 7852 | |
| 7853 | // MSVC destroys objects passed by value in the callee. Therefore a |
| 7854 | // 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] | 7855 | // object's destructor. However, we don't perform any direct access check |
| 7856 | // on the dtor. |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 7857 | if (getLangOpts().CPlusPlus && Context.getTargetInfo() |
| 7858 | .getCXXABI() |
| 7859 | .areArgsDestroyedLeftToRightInCallee()) { |
Hans Wennborg | 13ac4bd | 2014-01-13 19:24:31 +0000 | [diff] [blame] | 7860 | if (!Param->isInvalidDecl()) { |
| 7861 | if (const RecordType *RT = Param->getType()->getAs<RecordType>()) { |
| 7862 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 7863 | if (!ClassDecl->isInvalidDecl() && |
| 7864 | !ClassDecl->hasIrrelevantDestructor() && |
| 7865 | !ClassDecl->isDependentContext()) { |
| 7866 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
| 7867 | MarkFunctionReferenced(Param->getLocation(), Destructor); |
| 7868 | DiagnoseUseOfDecl(Destructor, Param->getLocation()); |
| 7869 | } |
Hans Wennborg | 0f3c10c | 2014-01-13 17:23:24 +0000 | [diff] [blame] | 7870 | } |
| 7871 | } |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 7872 | } |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 7873 | } |
| 7874 | |
| 7875 | return HasInvalidParm; |
| 7876 | } |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 7877 | |
| 7878 | /// CheckCastAlign - Implements -Wcast-align, which warns when a |
| 7879 | /// pointer cast increases the alignment requirements. |
| 7880 | void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { |
| 7881 | // This is actually a lot of work to potentially be doing on every |
| 7882 | // 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] | 7883 | if (getDiagnostics().isIgnored(diag::warn_cast_align, TRange.getBegin())) |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 7884 | return; |
| 7885 | |
| 7886 | // Ignore dependent types. |
| 7887 | if (T->isDependentType() || Op->getType()->isDependentType()) |
| 7888 | return; |
| 7889 | |
| 7890 | // Require that the destination be a pointer type. |
| 7891 | const PointerType *DestPtr = T->getAs<PointerType>(); |
| 7892 | if (!DestPtr) return; |
| 7893 | |
| 7894 | // If the destination has alignment 1, we're done. |
| 7895 | QualType DestPointee = DestPtr->getPointeeType(); |
| 7896 | if (DestPointee->isIncompleteType()) return; |
| 7897 | CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); |
| 7898 | if (DestAlign.isOne()) return; |
| 7899 | |
| 7900 | // Require that the source be a pointer type. |
| 7901 | const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); |
| 7902 | if (!SrcPtr) return; |
| 7903 | QualType SrcPointee = SrcPtr->getPointeeType(); |
| 7904 | |
| 7905 | // Whitelist casts from cv void*. We already implicitly |
| 7906 | // whitelisted casts to cv void*, since they have alignment 1. |
| 7907 | // Also whitelist casts involving incomplete types, which implicitly |
| 7908 | // includes 'void'. |
| 7909 | if (SrcPointee->isIncompleteType()) return; |
| 7910 | |
| 7911 | CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee); |
| 7912 | if (SrcAlign >= DestAlign) return; |
| 7913 | |
| 7914 | Diag(TRange.getBegin(), diag::warn_cast_align) |
| 7915 | << Op->getType() << T |
| 7916 | << static_cast<unsigned>(SrcAlign.getQuantity()) |
| 7917 | << static_cast<unsigned>(DestAlign.getQuantity()) |
| 7918 | << TRange << Op->getSourceRange(); |
| 7919 | } |
| 7920 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7921 | static const Type* getElementType(const Expr *BaseExpr) { |
| 7922 | const Type* EltType = BaseExpr->getType().getTypePtr(); |
| 7923 | if (EltType->isAnyPointerType()) |
| 7924 | return EltType->getPointeeType().getTypePtr(); |
| 7925 | else if (EltType->isArrayType()) |
| 7926 | return EltType->getBaseElementTypeUnsafe(); |
| 7927 | return EltType; |
| 7928 | } |
| 7929 | |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 7930 | /// \brief Check whether this array fits the idiom of a size-one tail padded |
| 7931 | /// array member of a struct. |
| 7932 | /// |
| 7933 | /// We avoid emitting out-of-bounds access warnings for such arrays as they are |
| 7934 | /// commonly used to emulate flexible arrays in C89 code. |
| 7935 | static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size, |
| 7936 | const NamedDecl *ND) { |
| 7937 | if (Size != 1 || !ND) return false; |
| 7938 | |
| 7939 | const FieldDecl *FD = dyn_cast<FieldDecl>(ND); |
| 7940 | if (!FD) return false; |
| 7941 | |
| 7942 | // Don't consider sizes resulting from macro expansions or template argument |
| 7943 | // substitution to form C89 tail-padded arrays. |
Sean Callanan | 06a48a6 | 2012-05-04 18:22:53 +0000 | [diff] [blame] | 7944 | |
| 7945 | TypeSourceInfo *TInfo = FD->getTypeSourceInfo(); |
Ted Kremenek | 7ebb493 | 2012-05-09 05:35:08 +0000 | [diff] [blame] | 7946 | while (TInfo) { |
| 7947 | TypeLoc TL = TInfo->getTypeLoc(); |
| 7948 | // Look through typedefs. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7949 | if (TypedefTypeLoc TTL = TL.getAs<TypedefTypeLoc>()) { |
| 7950 | const TypedefNameDecl *TDL = TTL.getTypedefNameDecl(); |
Ted Kremenek | 7ebb493 | 2012-05-09 05:35:08 +0000 | [diff] [blame] | 7951 | TInfo = TDL->getTypeSourceInfo(); |
| 7952 | continue; |
| 7953 | } |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7954 | if (ConstantArrayTypeLoc CTL = TL.getAs<ConstantArrayTypeLoc>()) { |
| 7955 | const Expr *SizeExpr = dyn_cast<IntegerLiteral>(CTL.getSizeExpr()); |
Chad Rosier | 7029992 | 2013-02-06 00:58:34 +0000 | [diff] [blame] | 7956 | if (!SizeExpr || SizeExpr->getExprLoc().isMacroID()) |
| 7957 | return false; |
| 7958 | } |
Ted Kremenek | 7ebb493 | 2012-05-09 05:35:08 +0000 | [diff] [blame] | 7959 | break; |
Sean Callanan | 06a48a6 | 2012-05-04 18:22:53 +0000 | [diff] [blame] | 7960 | } |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 7961 | |
| 7962 | const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext()); |
Matt Beaumont-Gay | c93b489 | 2011-11-29 22:43:53 +0000 | [diff] [blame] | 7963 | if (!RD) return false; |
| 7964 | if (RD->isUnion()) return false; |
| 7965 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 7966 | if (!CRD->isStandardLayout()) return false; |
| 7967 | } |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 7968 | |
Benjamin Kramer | 8c54367 | 2011-08-06 03:04:42 +0000 | [diff] [blame] | 7969 | // See if this is the last field decl in the record. |
| 7970 | const Decl *D = FD; |
| 7971 | while ((D = D->getNextDeclInContext())) |
| 7972 | if (isa<FieldDecl>(D)) |
| 7973 | return false; |
| 7974 | return true; |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 7975 | } |
| 7976 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7977 | void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 7978 | const ArraySubscriptExpr *ASE, |
Richard Smith | 13f6718 | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 7979 | bool AllowOnePastEnd, bool IndexNegated) { |
Eli Friedman | 84e6e5c | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 7980 | IndexExpr = IndexExpr->IgnoreParenImpCasts(); |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 7981 | if (IndexExpr->isValueDependent()) |
| 7982 | return; |
| 7983 | |
Matt Beaumont-Gay | 9d570c4 | 2011-12-12 22:35:02 +0000 | [diff] [blame] | 7984 | const Type *EffectiveType = getElementType(BaseExpr); |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7985 | BaseExpr = BaseExpr->IgnoreParenCasts(); |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 7986 | const ConstantArrayType *ArrayTy = |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7987 | Context.getAsConstantArrayType(BaseExpr->getType()); |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 7988 | if (!ArrayTy) |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 7989 | return; |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 7990 | |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 7991 | llvm::APSInt index; |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 7992 | if (!IndexExpr->EvaluateAsInt(index, Context)) |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 7993 | return; |
Richard Smith | 13f6718 | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 7994 | if (IndexNegated) |
| 7995 | index = -index; |
Ted Kremenek | 108b2d5 | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 7996 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7997 | const NamedDecl *ND = nullptr; |
Chandler Carruth | 126b155 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 7998 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
| 7999 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 8000 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) |
Chandler Carruth | 126b155 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 8001 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); |
Chandler Carruth | 126b155 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 8002 | |
Ted Kremenek | e4b316c | 2011-02-23 23:06:04 +0000 | [diff] [blame] | 8003 | if (index.isUnsigned() || !index.isNegative()) { |
Ted Kremenek | a7ced2c | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 8004 | llvm::APInt size = ArrayTy->getSize(); |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 8005 | if (!size.isStrictlyPositive()) |
| 8006 | return; |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8007 | |
| 8008 | const Type* BaseType = getElementType(BaseExpr); |
Nico Weber | 7c29980 | 2011-09-17 22:59:41 +0000 | [diff] [blame] | 8009 | if (BaseType != EffectiveType) { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8010 | // Make sure we're comparing apples to apples when comparing index to size |
| 8011 | uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); |
| 8012 | uint64_t array_typesize = Context.getTypeSize(BaseType); |
Kaelyn Uhrain | 0fb0bb1 | 2011-08-10 19:47:25 +0000 | [diff] [blame] | 8013 | // Handle ptrarith_typesize being zero, such as when casting to void* |
Kaelyn Uhrain | e535376 | 2011-08-10 18:49:28 +0000 | [diff] [blame] | 8014 | if (!ptrarith_typesize) ptrarith_typesize = 1; |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8015 | if (ptrarith_typesize != array_typesize) { |
| 8016 | // There's a cast to a different size type involved |
| 8017 | uint64_t ratio = array_typesize / ptrarith_typesize; |
| 8018 | // TODO: Be smarter about handling cases where array_typesize is not a |
| 8019 | // multiple of ptrarith_typesize |
| 8020 | if (ptrarith_typesize * ratio == array_typesize) |
| 8021 | size *= llvm::APInt(size.getBitWidth(), ratio); |
| 8022 | } |
| 8023 | } |
| 8024 | |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 8025 | if (size.getBitWidth() > index.getBitWidth()) |
Eli Friedman | 84e6e5c | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 8026 | index = index.zext(size.getBitWidth()); |
Ted Kremenek | a7ced2c | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 8027 | else if (size.getBitWidth() < index.getBitWidth()) |
Eli Friedman | 84e6e5c | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 8028 | size = size.zext(index.getBitWidth()); |
Ted Kremenek | a7ced2c | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 8029 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8030 | // For array subscripting the index must be less than size, but for pointer |
| 8031 | // arithmetic also allow the index (offset) to be equal to size since |
| 8032 | // computing the next address after the end of the array is legal and |
| 8033 | // commonly done e.g. in C++ iterators and range-based for loops. |
Eli Friedman | 84e6e5c | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 8034 | if (AllowOnePastEnd ? index.ule(size) : index.ult(size)) |
Chandler Carruth | 126b155 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 8035 | return; |
| 8036 | |
| 8037 | // Also don't warn for arrays of size 1 which are members of some |
| 8038 | // structure. These are often used to approximate flexible arrays in C89 |
| 8039 | // code. |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8040 | if (IsTailPaddedMemberArray(*this, size, ND)) |
Ted Kremenek | 108b2d5 | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 8041 | return; |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 8042 | |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 8043 | // Suppress the warning if the subscript expression (as identified by the |
| 8044 | // ']' location) and the index expression are both from macro expansions |
| 8045 | // within a system header. |
| 8046 | if (ASE) { |
| 8047 | SourceLocation RBracketLoc = SourceMgr.getSpellingLoc( |
| 8048 | ASE->getRBracketLoc()); |
| 8049 | if (SourceMgr.isInSystemHeader(RBracketLoc)) { |
| 8050 | SourceLocation IndexLoc = SourceMgr.getSpellingLoc( |
| 8051 | IndexExpr->getLocStart()); |
Eli Friedman | 5ba37d5 | 2013-08-22 00:27:10 +0000 | [diff] [blame] | 8052 | if (SourceMgr.isWrittenInSameFile(RBracketLoc, IndexLoc)) |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 8053 | return; |
| 8054 | } |
| 8055 | } |
| 8056 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8057 | unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds; |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 8058 | if (ASE) |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8059 | DiagID = diag::warn_array_index_exceeds_bounds; |
| 8060 | |
| 8061 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, |
| 8062 | PDiag(DiagID) << index.toString(10, true) |
| 8063 | << size.toString(10, true) |
| 8064 | << (unsigned)size.getLimitedValue(~0U) |
| 8065 | << IndexExpr->getSourceRange()); |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 8066 | } else { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8067 | unsigned DiagID = diag::warn_array_index_precedes_bounds; |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 8068 | if (!ASE) { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8069 | DiagID = diag::warn_ptr_arith_precedes_bounds; |
| 8070 | if (index.isNegative()) index = -index; |
| 8071 | } |
| 8072 | |
| 8073 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, |
| 8074 | PDiag(DiagID) << index.toString(10, true) |
| 8075 | << IndexExpr->getSourceRange()); |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 8076 | } |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 8077 | |
Matt Beaumont-Gay | b233982 | 2011-11-29 19:27:11 +0000 | [diff] [blame] | 8078 | if (!ND) { |
| 8079 | // Try harder to find a NamedDecl to point at in the note. |
| 8080 | while (const ArraySubscriptExpr *ASE = |
| 8081 | dyn_cast<ArraySubscriptExpr>(BaseExpr)) |
| 8082 | BaseExpr = ASE->getBase()->IgnoreParenCasts(); |
| 8083 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
| 8084 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); |
| 8085 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) |
| 8086 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); |
| 8087 | } |
| 8088 | |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 8089 | if (ND) |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8090 | DiagRuntimeBehavior(ND->getLocStart(), BaseExpr, |
| 8091 | PDiag(diag::note_array_index_out_of_bounds) |
| 8092 | << ND->getDeclName()); |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 8093 | } |
| 8094 | |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 8095 | void Sema::CheckArrayAccess(const Expr *expr) { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8096 | int AllowOnePastEnd = 0; |
| 8097 | while (expr) { |
| 8098 | expr = expr->IgnoreParenImpCasts(); |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 8099 | switch (expr->getStmtClass()) { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8100 | case Stmt::ArraySubscriptExprClass: { |
| 8101 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr); |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 8102 | CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE, |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8103 | AllowOnePastEnd > 0); |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 8104 | return; |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8105 | } |
| 8106 | case Stmt::UnaryOperatorClass: { |
| 8107 | // Only unwrap the * and & unary operators |
| 8108 | const UnaryOperator *UO = cast<UnaryOperator>(expr); |
| 8109 | expr = UO->getSubExpr(); |
| 8110 | switch (UO->getOpcode()) { |
| 8111 | case UO_AddrOf: |
| 8112 | AllowOnePastEnd++; |
| 8113 | break; |
| 8114 | case UO_Deref: |
| 8115 | AllowOnePastEnd--; |
| 8116 | break; |
| 8117 | default: |
| 8118 | return; |
| 8119 | } |
| 8120 | break; |
| 8121 | } |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 8122 | case Stmt::ConditionalOperatorClass: { |
| 8123 | const ConditionalOperator *cond = cast<ConditionalOperator>(expr); |
| 8124 | if (const Expr *lhs = cond->getLHS()) |
| 8125 | CheckArrayAccess(lhs); |
| 8126 | if (const Expr *rhs = cond->getRHS()) |
| 8127 | CheckArrayAccess(rhs); |
| 8128 | return; |
| 8129 | } |
| 8130 | default: |
| 8131 | return; |
| 8132 | } |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 8133 | } |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 8134 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8135 | |
| 8136 | //===--- CHECK: Objective-C retain cycles ----------------------------------// |
| 8137 | |
| 8138 | namespace { |
| 8139 | struct RetainCycleOwner { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8140 | RetainCycleOwner() : Variable(nullptr), Indirect(false) {} |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8141 | VarDecl *Variable; |
| 8142 | SourceRange Range; |
| 8143 | SourceLocation Loc; |
| 8144 | bool Indirect; |
| 8145 | |
| 8146 | void setLocsFrom(Expr *e) { |
| 8147 | Loc = e->getExprLoc(); |
| 8148 | Range = e->getSourceRange(); |
| 8149 | } |
| 8150 | }; |
| 8151 | } |
| 8152 | |
| 8153 | /// Consider whether capturing the given variable can possibly lead to |
| 8154 | /// a retain cycle. |
| 8155 | static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) { |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 8156 | // In ARC, it's captured strongly iff the variable has __strong |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8157 | // lifetime. In MRR, it's captured strongly if the variable is |
| 8158 | // __block and has an appropriate type. |
| 8159 | if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 8160 | return false; |
| 8161 | |
| 8162 | owner.Variable = var; |
Jordan Rose | fa9e4ba | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 8163 | if (ref) |
| 8164 | owner.setLocsFrom(ref); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8165 | return true; |
| 8166 | } |
| 8167 | |
Fariborz Jahanian | edbc345 | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 8168 | static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8169 | while (true) { |
| 8170 | e = e->IgnoreParens(); |
| 8171 | if (CastExpr *cast = dyn_cast<CastExpr>(e)) { |
| 8172 | switch (cast->getCastKind()) { |
| 8173 | case CK_BitCast: |
| 8174 | case CK_LValueBitCast: |
| 8175 | case CK_LValueToRValue: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 8176 | case CK_ARCReclaimReturnedObject: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8177 | e = cast->getSubExpr(); |
| 8178 | continue; |
| 8179 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8180 | default: |
| 8181 | return false; |
| 8182 | } |
| 8183 | } |
| 8184 | |
| 8185 | if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) { |
| 8186 | ObjCIvarDecl *ivar = ref->getDecl(); |
| 8187 | if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 8188 | return false; |
| 8189 | |
| 8190 | // Try to find a retain cycle in the base. |
Fariborz Jahanian | edbc345 | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 8191 | if (!findRetainCycleOwner(S, ref->getBase(), owner)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8192 | return false; |
| 8193 | |
| 8194 | if (ref->isFreeIvar()) owner.setLocsFrom(ref); |
| 8195 | owner.Indirect = true; |
| 8196 | return true; |
| 8197 | } |
| 8198 | |
| 8199 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 8200 | VarDecl *var = dyn_cast<VarDecl>(ref->getDecl()); |
| 8201 | if (!var) return false; |
| 8202 | return considerVariable(var, ref, owner); |
| 8203 | } |
| 8204 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8205 | if (MemberExpr *member = dyn_cast<MemberExpr>(e)) { |
| 8206 | if (member->isArrow()) return false; |
| 8207 | |
| 8208 | // Don't count this as an indirect ownership. |
| 8209 | e = member->getBase(); |
| 8210 | continue; |
| 8211 | } |
| 8212 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 8213 | if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { |
| 8214 | // Only pay attention to pseudo-objects on property references. |
| 8215 | ObjCPropertyRefExpr *pre |
| 8216 | = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm() |
| 8217 | ->IgnoreParens()); |
| 8218 | if (!pre) return false; |
| 8219 | if (pre->isImplicitProperty()) return false; |
| 8220 | ObjCPropertyDecl *property = pre->getExplicitProperty(); |
| 8221 | if (!property->isRetaining() && |
| 8222 | !(property->getPropertyIvarDecl() && |
| 8223 | property->getPropertyIvarDecl()->getType() |
| 8224 | .getObjCLifetime() == Qualifiers::OCL_Strong)) |
| 8225 | return false; |
| 8226 | |
| 8227 | owner.Indirect = true; |
Fariborz Jahanian | edbc345 | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 8228 | if (pre->isSuperReceiver()) { |
| 8229 | owner.Variable = S.getCurMethodDecl()->getSelfDecl(); |
| 8230 | if (!owner.Variable) |
| 8231 | return false; |
| 8232 | owner.Loc = pre->getLocation(); |
| 8233 | owner.Range = pre->getSourceRange(); |
| 8234 | return true; |
| 8235 | } |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 8236 | e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase()) |
| 8237 | ->getSourceExpr()); |
| 8238 | continue; |
| 8239 | } |
| 8240 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8241 | // Array ivars? |
| 8242 | |
| 8243 | return false; |
| 8244 | } |
| 8245 | } |
| 8246 | |
| 8247 | namespace { |
| 8248 | struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> { |
| 8249 | FindCaptureVisitor(ASTContext &Context, VarDecl *variable) |
| 8250 | : EvaluatedExprVisitor<FindCaptureVisitor>(Context), |
Fariborz Jahanian | 8df9e24 | 2014-06-12 20:57:14 +0000 | [diff] [blame] | 8251 | Context(Context), Variable(variable), Capturer(nullptr), |
| 8252 | VarWillBeReased(false) {} |
| 8253 | ASTContext &Context; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8254 | VarDecl *Variable; |
| 8255 | Expr *Capturer; |
Fariborz Jahanian | 8df9e24 | 2014-06-12 20:57:14 +0000 | [diff] [blame] | 8256 | bool VarWillBeReased; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8257 | |
| 8258 | void VisitDeclRefExpr(DeclRefExpr *ref) { |
| 8259 | if (ref->getDecl() == Variable && !Capturer) |
| 8260 | Capturer = ref; |
| 8261 | } |
| 8262 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8263 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) { |
| 8264 | if (Capturer) return; |
| 8265 | Visit(ref->getBase()); |
| 8266 | if (Capturer && ref->isFreeIvar()) |
| 8267 | Capturer = ref; |
| 8268 | } |
| 8269 | |
| 8270 | void VisitBlockExpr(BlockExpr *block) { |
| 8271 | // Look inside nested blocks |
| 8272 | if (block->getBlockDecl()->capturesVariable(Variable)) |
| 8273 | Visit(block->getBlockDecl()->getBody()); |
| 8274 | } |
Fariborz Jahanian | 0e33754 | 2012-08-31 20:04:47 +0000 | [diff] [blame] | 8275 | |
| 8276 | void VisitOpaqueValueExpr(OpaqueValueExpr *OVE) { |
| 8277 | if (Capturer) return; |
| 8278 | if (OVE->getSourceExpr()) |
| 8279 | Visit(OVE->getSourceExpr()); |
| 8280 | } |
Fariborz Jahanian | 8df9e24 | 2014-06-12 20:57:14 +0000 | [diff] [blame] | 8281 | void VisitBinaryOperator(BinaryOperator *BinOp) { |
| 8282 | if (!Variable || VarWillBeReased || BinOp->getOpcode() != BO_Assign) |
| 8283 | return; |
| 8284 | Expr *LHS = BinOp->getLHS(); |
| 8285 | if (const DeclRefExpr *DRE = dyn_cast_or_null<DeclRefExpr>(LHS)) { |
| 8286 | if (DRE->getDecl() != Variable) |
| 8287 | return; |
| 8288 | if (Expr *RHS = BinOp->getRHS()) { |
| 8289 | RHS = RHS->IgnoreParenCasts(); |
| 8290 | llvm::APSInt Value; |
| 8291 | VarWillBeReased = |
| 8292 | (RHS && RHS->isIntegerConstantExpr(Value, Context) && Value == 0); |
| 8293 | } |
| 8294 | } |
| 8295 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8296 | }; |
| 8297 | } |
| 8298 | |
| 8299 | /// Check whether the given argument is a block which captures a |
| 8300 | /// variable. |
| 8301 | static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) { |
| 8302 | assert(owner.Variable && owner.Loc.isValid()); |
| 8303 | |
| 8304 | e = e->IgnoreParenCasts(); |
Jordan Rose | 67e887c | 2012-09-17 17:54:30 +0000 | [diff] [blame] | 8305 | |
| 8306 | // Look through [^{...} copy] and Block_copy(^{...}). |
| 8307 | if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(e)) { |
| 8308 | Selector Cmd = ME->getSelector(); |
| 8309 | if (Cmd.isUnarySelector() && Cmd.getNameForSlot(0) == "copy") { |
| 8310 | e = ME->getInstanceReceiver(); |
| 8311 | if (!e) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8312 | return nullptr; |
Jordan Rose | 67e887c | 2012-09-17 17:54:30 +0000 | [diff] [blame] | 8313 | e = e->IgnoreParenCasts(); |
| 8314 | } |
| 8315 | } else if (CallExpr *CE = dyn_cast<CallExpr>(e)) { |
| 8316 | if (CE->getNumArgs() == 1) { |
| 8317 | FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(CE->getCalleeDecl()); |
Ted Kremenek | b67c6cc | 2012-10-02 04:36:54 +0000 | [diff] [blame] | 8318 | if (Fn) { |
| 8319 | const IdentifierInfo *FnI = Fn->getIdentifier(); |
| 8320 | if (FnI && FnI->isStr("_Block_copy")) { |
| 8321 | e = CE->getArg(0)->IgnoreParenCasts(); |
| 8322 | } |
| 8323 | } |
Jordan Rose | 67e887c | 2012-09-17 17:54:30 +0000 | [diff] [blame] | 8324 | } |
| 8325 | } |
| 8326 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8327 | BlockExpr *block = dyn_cast<BlockExpr>(e); |
| 8328 | if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8329 | return nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8330 | |
| 8331 | FindCaptureVisitor visitor(S.Context, owner.Variable); |
| 8332 | visitor.Visit(block->getBlockDecl()->getBody()); |
Fariborz Jahanian | 8df9e24 | 2014-06-12 20:57:14 +0000 | [diff] [blame] | 8333 | return visitor.VarWillBeReased ? nullptr : visitor.Capturer; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8334 | } |
| 8335 | |
| 8336 | static void diagnoseRetainCycle(Sema &S, Expr *capturer, |
| 8337 | RetainCycleOwner &owner) { |
| 8338 | assert(capturer); |
| 8339 | assert(owner.Variable && owner.Loc.isValid()); |
| 8340 | |
| 8341 | S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle) |
| 8342 | << owner.Variable << capturer->getSourceRange(); |
| 8343 | S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner) |
| 8344 | << owner.Indirect << owner.Range; |
| 8345 | } |
| 8346 | |
| 8347 | /// Check for a keyword selector that starts with the word 'add' or |
| 8348 | /// 'set'. |
| 8349 | static bool isSetterLikeSelector(Selector sel) { |
| 8350 | if (sel.isUnarySelector()) return false; |
| 8351 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8352 | StringRef str = sel.getNameForSlot(0); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8353 | while (!str.empty() && str.front() == '_') str = str.substr(1); |
Ted Kremenek | 764d63a | 2011-12-01 00:59:21 +0000 | [diff] [blame] | 8354 | if (str.startswith("set")) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8355 | str = str.substr(3); |
Ted Kremenek | 764d63a | 2011-12-01 00:59:21 +0000 | [diff] [blame] | 8356 | else if (str.startswith("add")) { |
| 8357 | // Specially whitelist 'addOperationWithBlock:'. |
| 8358 | if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock")) |
| 8359 | return false; |
| 8360 | str = str.substr(3); |
| 8361 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8362 | else |
| 8363 | return false; |
| 8364 | |
| 8365 | if (str.empty()) return true; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 8366 | return !isLowercase(str.front()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8367 | } |
| 8368 | |
Benjamin Kramer | 3a74345 | 2015-03-09 15:03:32 +0000 | [diff] [blame] | 8369 | static Optional<int> GetNSMutableArrayArgumentIndex(Sema &S, |
| 8370 | ObjCMessageExpr *Message) { |
Alex Denisov | e1d882c | 2015-03-04 17:55:52 +0000 | [diff] [blame] | 8371 | if (S.NSMutableArrayPointer.isNull()) { |
| 8372 | IdentifierInfo *NSMutableArrayId = |
| 8373 | S.NSAPIObj->getNSClassId(NSAPI::ClassId_NSMutableArray); |
| 8374 | NamedDecl *IF = S.LookupSingleName(S.TUScope, NSMutableArrayId, |
| 8375 | Message->getLocStart(), |
| 8376 | Sema::LookupOrdinaryName); |
| 8377 | ObjCInterfaceDecl *InterfaceDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
| 8378 | if (!InterfaceDecl) { |
| 8379 | return None; |
| 8380 | } |
| 8381 | QualType NSMutableArrayObject = |
| 8382 | S.Context.getObjCInterfaceType(InterfaceDecl); |
| 8383 | S.NSMutableArrayPointer = |
| 8384 | S.Context.getObjCObjectPointerType(NSMutableArrayObject); |
| 8385 | } |
| 8386 | |
| 8387 | if (S.NSMutableArrayPointer != Message->getReceiverType()) { |
| 8388 | return None; |
| 8389 | } |
| 8390 | |
| 8391 | Selector Sel = Message->getSelector(); |
| 8392 | |
| 8393 | Optional<NSAPI::NSArrayMethodKind> MKOpt = |
| 8394 | S.NSAPIObj->getNSArrayMethodKind(Sel); |
| 8395 | if (!MKOpt) { |
| 8396 | return None; |
| 8397 | } |
| 8398 | |
| 8399 | NSAPI::NSArrayMethodKind MK = *MKOpt; |
| 8400 | |
| 8401 | switch (MK) { |
| 8402 | case NSAPI::NSMutableArr_addObject: |
| 8403 | case NSAPI::NSMutableArr_insertObjectAtIndex: |
| 8404 | case NSAPI::NSMutableArr_setObjectAtIndexedSubscript: |
| 8405 | return 0; |
| 8406 | case NSAPI::NSMutableArr_replaceObjectAtIndex: |
| 8407 | return 1; |
| 8408 | |
| 8409 | default: |
| 8410 | return None; |
| 8411 | } |
| 8412 | |
| 8413 | return None; |
| 8414 | } |
| 8415 | |
| 8416 | static |
| 8417 | Optional<int> GetNSMutableDictionaryArgumentIndex(Sema &S, |
| 8418 | ObjCMessageExpr *Message) { |
| 8419 | |
| 8420 | if (S.NSMutableDictionaryPointer.isNull()) { |
| 8421 | IdentifierInfo *NSMutableDictionaryId = |
| 8422 | S.NSAPIObj->getNSClassId(NSAPI::ClassId_NSMutableDictionary); |
| 8423 | NamedDecl *IF = S.LookupSingleName(S.TUScope, NSMutableDictionaryId, |
| 8424 | Message->getLocStart(), |
| 8425 | Sema::LookupOrdinaryName); |
| 8426 | ObjCInterfaceDecl *InterfaceDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
| 8427 | if (!InterfaceDecl) { |
| 8428 | return None; |
| 8429 | } |
| 8430 | QualType NSMutableDictionaryObject = |
| 8431 | S.Context.getObjCInterfaceType(InterfaceDecl); |
| 8432 | S.NSMutableDictionaryPointer = |
| 8433 | S.Context.getObjCObjectPointerType(NSMutableDictionaryObject); |
| 8434 | } |
| 8435 | |
| 8436 | if (S.NSMutableDictionaryPointer != Message->getReceiverType()) { |
| 8437 | return None; |
| 8438 | } |
| 8439 | |
| 8440 | Selector Sel = Message->getSelector(); |
| 8441 | |
| 8442 | Optional<NSAPI::NSDictionaryMethodKind> MKOpt = |
| 8443 | S.NSAPIObj->getNSDictionaryMethodKind(Sel); |
| 8444 | if (!MKOpt) { |
| 8445 | return None; |
| 8446 | } |
| 8447 | |
| 8448 | NSAPI::NSDictionaryMethodKind MK = *MKOpt; |
| 8449 | |
| 8450 | switch (MK) { |
| 8451 | case NSAPI::NSMutableDict_setObjectForKey: |
| 8452 | case NSAPI::NSMutableDict_setValueForKey: |
| 8453 | case NSAPI::NSMutableDict_setObjectForKeyedSubscript: |
| 8454 | return 0; |
| 8455 | |
| 8456 | default: |
| 8457 | return None; |
| 8458 | } |
| 8459 | |
| 8460 | return None; |
| 8461 | } |
| 8462 | |
| 8463 | static Optional<int> GetNSSetArgumentIndex(Sema &S, ObjCMessageExpr *Message) { |
| 8464 | |
| 8465 | ObjCInterfaceDecl *InterfaceDecl; |
| 8466 | if (S.NSMutableSetPointer.isNull()) { |
| 8467 | IdentifierInfo *NSMutableSetId = |
| 8468 | S.NSAPIObj->getNSClassId(NSAPI::ClassId_NSMutableSet); |
| 8469 | NamedDecl *IF = S.LookupSingleName(S.TUScope, NSMutableSetId, |
| 8470 | Message->getLocStart(), |
| 8471 | Sema::LookupOrdinaryName); |
| 8472 | InterfaceDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
| 8473 | if (InterfaceDecl) { |
| 8474 | QualType NSMutableSetObject = |
| 8475 | S.Context.getObjCInterfaceType(InterfaceDecl); |
| 8476 | S.NSMutableSetPointer = |
| 8477 | S.Context.getObjCObjectPointerType(NSMutableSetObject); |
| 8478 | } |
| 8479 | } |
| 8480 | |
| 8481 | if (S.NSCountedSetPointer.isNull()) { |
| 8482 | IdentifierInfo *NSCountedSetId = |
| 8483 | S.NSAPIObj->getNSClassId(NSAPI::ClassId_NSCountedSet); |
| 8484 | NamedDecl *IF = S.LookupSingleName(S.TUScope, NSCountedSetId, |
| 8485 | Message->getLocStart(), |
| 8486 | Sema::LookupOrdinaryName); |
| 8487 | InterfaceDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
| 8488 | if (InterfaceDecl) { |
| 8489 | QualType NSCountedSetObject = |
| 8490 | S.Context.getObjCInterfaceType(InterfaceDecl); |
| 8491 | S.NSCountedSetPointer = |
| 8492 | S.Context.getObjCObjectPointerType(NSCountedSetObject); |
| 8493 | } |
| 8494 | } |
| 8495 | |
| 8496 | if (S.NSMutableOrderedSetPointer.isNull()) { |
| 8497 | IdentifierInfo *NSOrderedSetId = |
| 8498 | S.NSAPIObj->getNSClassId(NSAPI::ClassId_NSMutableOrderedSet); |
| 8499 | NamedDecl *IF = S.LookupSingleName(S.TUScope, NSOrderedSetId, |
| 8500 | Message->getLocStart(), |
| 8501 | Sema::LookupOrdinaryName); |
| 8502 | InterfaceDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
| 8503 | if (InterfaceDecl) { |
| 8504 | QualType NSOrderedSetObject = |
| 8505 | S.Context.getObjCInterfaceType(InterfaceDecl); |
| 8506 | S.NSMutableOrderedSetPointer = |
| 8507 | S.Context.getObjCObjectPointerType(NSOrderedSetObject); |
| 8508 | } |
| 8509 | } |
| 8510 | |
| 8511 | QualType ReceiverType = Message->getReceiverType(); |
| 8512 | |
| 8513 | bool IsMutableSet = !S.NSMutableSetPointer.isNull() && |
| 8514 | ReceiverType == S.NSMutableSetPointer; |
| 8515 | bool IsMutableOrderedSet = !S.NSMutableOrderedSetPointer.isNull() && |
| 8516 | ReceiverType == S.NSMutableOrderedSetPointer; |
| 8517 | bool IsCountedSet = !S.NSCountedSetPointer.isNull() && |
| 8518 | ReceiverType == S.NSCountedSetPointer; |
| 8519 | |
| 8520 | if (!IsMutableSet && !IsMutableOrderedSet && !IsCountedSet) { |
| 8521 | return None; |
| 8522 | } |
| 8523 | |
| 8524 | Selector Sel = Message->getSelector(); |
| 8525 | |
| 8526 | Optional<NSAPI::NSSetMethodKind> MKOpt = S.NSAPIObj->getNSSetMethodKind(Sel); |
| 8527 | if (!MKOpt) { |
| 8528 | return None; |
| 8529 | } |
| 8530 | |
| 8531 | NSAPI::NSSetMethodKind MK = *MKOpt; |
| 8532 | |
| 8533 | switch (MK) { |
| 8534 | case NSAPI::NSMutableSet_addObject: |
| 8535 | case NSAPI::NSOrderedSet_setObjectAtIndex: |
| 8536 | case NSAPI::NSOrderedSet_setObjectAtIndexedSubscript: |
| 8537 | case NSAPI::NSOrderedSet_insertObjectAtIndex: |
| 8538 | return 0; |
| 8539 | case NSAPI::NSOrderedSet_replaceObjectAtIndexWithObject: |
| 8540 | return 1; |
| 8541 | } |
| 8542 | |
| 8543 | return None; |
| 8544 | } |
| 8545 | |
| 8546 | void Sema::CheckObjCCircularContainer(ObjCMessageExpr *Message) { |
| 8547 | if (!Message->isInstanceMessage()) { |
| 8548 | return; |
| 8549 | } |
| 8550 | |
| 8551 | Optional<int> ArgOpt; |
| 8552 | |
| 8553 | if (!(ArgOpt = GetNSMutableArrayArgumentIndex(*this, Message)) && |
| 8554 | !(ArgOpt = GetNSMutableDictionaryArgumentIndex(*this, Message)) && |
| 8555 | !(ArgOpt = GetNSSetArgumentIndex(*this, Message))) { |
| 8556 | return; |
| 8557 | } |
| 8558 | |
| 8559 | int ArgIndex = *ArgOpt; |
| 8560 | |
| 8561 | Expr *Receiver = Message->getInstanceReceiver()->IgnoreImpCasts(); |
| 8562 | if (OpaqueValueExpr *OE = dyn_cast<OpaqueValueExpr>(Receiver)) { |
| 8563 | Receiver = OE->getSourceExpr()->IgnoreImpCasts(); |
| 8564 | } |
| 8565 | |
| 8566 | Expr *Arg = Message->getArg(ArgIndex)->IgnoreImpCasts(); |
| 8567 | if (OpaqueValueExpr *OE = dyn_cast<OpaqueValueExpr>(Arg)) { |
| 8568 | Arg = OE->getSourceExpr()->IgnoreImpCasts(); |
| 8569 | } |
| 8570 | |
| 8571 | if (DeclRefExpr *ReceiverRE = dyn_cast<DeclRefExpr>(Receiver)) { |
| 8572 | if (DeclRefExpr *ArgRE = dyn_cast<DeclRefExpr>(Arg)) { |
| 8573 | if (ReceiverRE->getDecl() == ArgRE->getDecl()) { |
| 8574 | ValueDecl *Decl = ReceiverRE->getDecl(); |
| 8575 | Diag(Message->getSourceRange().getBegin(), |
| 8576 | diag::warn_objc_circular_container) |
| 8577 | << Decl->getName(); |
| 8578 | Diag(Decl->getLocation(), |
| 8579 | diag::note_objc_circular_container_declared_here) |
| 8580 | << Decl->getName(); |
| 8581 | } |
| 8582 | } |
| 8583 | } else if (ObjCIvarRefExpr *IvarRE = dyn_cast<ObjCIvarRefExpr>(Receiver)) { |
| 8584 | if (ObjCIvarRefExpr *IvarArgRE = dyn_cast<ObjCIvarRefExpr>(Arg)) { |
| 8585 | if (IvarRE->getDecl() == IvarArgRE->getDecl()) { |
| 8586 | ObjCIvarDecl *Decl = IvarRE->getDecl(); |
| 8587 | Diag(Message->getSourceRange().getBegin(), |
| 8588 | diag::warn_objc_circular_container) |
| 8589 | << Decl->getName(); |
| 8590 | Diag(Decl->getLocation(), |
| 8591 | diag::note_objc_circular_container_declared_here) |
| 8592 | << Decl->getName(); |
| 8593 | } |
| 8594 | } |
| 8595 | } |
| 8596 | |
| 8597 | } |
| 8598 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8599 | /// Check a message send to see if it's likely to cause a retain cycle. |
| 8600 | void Sema::checkRetainCycles(ObjCMessageExpr *msg) { |
| 8601 | // Only check instance methods whose selector looks like a setter. |
| 8602 | if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector())) |
| 8603 | return; |
| 8604 | |
| 8605 | // Try to find a variable that the receiver is strongly owned by. |
| 8606 | RetainCycleOwner owner; |
| 8607 | if (msg->getReceiverKind() == ObjCMessageExpr::Instance) { |
Fariborz Jahanian | edbc345 | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 8608 | if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8609 | return; |
| 8610 | } else { |
| 8611 | assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance); |
| 8612 | owner.Variable = getCurMethodDecl()->getSelfDecl(); |
| 8613 | owner.Loc = msg->getSuperLoc(); |
| 8614 | owner.Range = msg->getSuperLoc(); |
| 8615 | } |
| 8616 | |
| 8617 | // Check whether the receiver is captured by any of the arguments. |
| 8618 | for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) |
| 8619 | if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) |
| 8620 | return diagnoseRetainCycle(*this, capturer, owner); |
| 8621 | } |
| 8622 | |
| 8623 | /// Check a property assign to see if it's likely to cause a retain cycle. |
| 8624 | void Sema::checkRetainCycles(Expr *receiver, Expr *argument) { |
| 8625 | RetainCycleOwner owner; |
Fariborz Jahanian | edbc345 | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 8626 | if (!findRetainCycleOwner(*this, receiver, owner)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8627 | return; |
| 8628 | |
| 8629 | if (Expr *capturer = findCapturingExpr(*this, argument, owner)) |
| 8630 | diagnoseRetainCycle(*this, capturer, owner); |
| 8631 | } |
| 8632 | |
Jordan Rose | fa9e4ba | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 8633 | void Sema::checkRetainCycles(VarDecl *Var, Expr *Init) { |
| 8634 | RetainCycleOwner Owner; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8635 | if (!considerVariable(Var, /*DeclRefExpr=*/nullptr, Owner)) |
Jordan Rose | fa9e4ba | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 8636 | return; |
| 8637 | |
| 8638 | // Because we don't have an expression for the variable, we have to set the |
| 8639 | // location explicitly here. |
| 8640 | Owner.Loc = Var->getLocation(); |
| 8641 | Owner.Range = Var->getSourceRange(); |
| 8642 | |
| 8643 | if (Expr *Capturer = findCapturingExpr(*this, Init, Owner)) |
| 8644 | diagnoseRetainCycle(*this, Capturer, Owner); |
| 8645 | } |
| 8646 | |
Ted Kremenek | 9304da9 | 2012-12-21 08:04:28 +0000 | [diff] [blame] | 8647 | static bool checkUnsafeAssignLiteral(Sema &S, SourceLocation Loc, |
| 8648 | Expr *RHS, bool isProperty) { |
| 8649 | // Check if RHS is an Objective-C object literal, which also can get |
| 8650 | // immediately zapped in a weak reference. Note that we explicitly |
| 8651 | // allow ObjCStringLiterals, since those are designed to never really die. |
| 8652 | RHS = RHS->IgnoreParenImpCasts(); |
Ted Kremenek | 44c2a2a | 2012-12-21 21:59:39 +0000 | [diff] [blame] | 8653 | |
Ted Kremenek | 6487335 | 2012-12-21 22:46:35 +0000 | [diff] [blame] | 8654 | // This enum needs to match with the 'select' in |
| 8655 | // warn_objc_arc_literal_assign (off-by-1). |
| 8656 | Sema::ObjCLiteralKind Kind = S.CheckLiteralKind(RHS); |
| 8657 | if (Kind == Sema::LK_String || Kind == Sema::LK_None) |
| 8658 | return false; |
Ted Kremenek | 44c2a2a | 2012-12-21 21:59:39 +0000 | [diff] [blame] | 8659 | |
| 8660 | S.Diag(Loc, diag::warn_arc_literal_assign) |
Ted Kremenek | 6487335 | 2012-12-21 22:46:35 +0000 | [diff] [blame] | 8661 | << (unsigned) Kind |
Ted Kremenek | 9304da9 | 2012-12-21 08:04:28 +0000 | [diff] [blame] | 8662 | << (isProperty ? 0 : 1) |
| 8663 | << RHS->getSourceRange(); |
Ted Kremenek | 44c2a2a | 2012-12-21 21:59:39 +0000 | [diff] [blame] | 8664 | |
| 8665 | return true; |
Ted Kremenek | 9304da9 | 2012-12-21 08:04:28 +0000 | [diff] [blame] | 8666 | } |
| 8667 | |
Ted Kremenek | c1f014a | 2012-12-21 19:45:30 +0000 | [diff] [blame] | 8668 | static bool checkUnsafeAssignObject(Sema &S, SourceLocation Loc, |
| 8669 | Qualifiers::ObjCLifetime LT, |
| 8670 | Expr *RHS, bool isProperty) { |
| 8671 | // Strip off any implicit cast added to get to the one ARC-specific. |
| 8672 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
| 8673 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
| 8674 | S.Diag(Loc, diag::warn_arc_retained_assign) |
| 8675 | << (LT == Qualifiers::OCL_ExplicitNone) |
| 8676 | << (isProperty ? 0 : 1) |
| 8677 | << RHS->getSourceRange(); |
| 8678 | return true; |
| 8679 | } |
| 8680 | RHS = cast->getSubExpr(); |
| 8681 | } |
| 8682 | |
| 8683 | if (LT == Qualifiers::OCL_Weak && |
| 8684 | checkUnsafeAssignLiteral(S, Loc, RHS, isProperty)) |
| 8685 | return true; |
| 8686 | |
| 8687 | return false; |
| 8688 | } |
| 8689 | |
Ted Kremenek | b36234d | 2012-12-21 08:04:20 +0000 | [diff] [blame] | 8690 | bool Sema::checkUnsafeAssigns(SourceLocation Loc, |
| 8691 | QualType LHS, Expr *RHS) { |
| 8692 | Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime(); |
| 8693 | |
| 8694 | if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone) |
| 8695 | return false; |
| 8696 | |
| 8697 | if (checkUnsafeAssignObject(*this, Loc, LT, RHS, false)) |
| 8698 | return true; |
| 8699 | |
| 8700 | return false; |
| 8701 | } |
| 8702 | |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 8703 | void Sema::checkUnsafeExprAssigns(SourceLocation Loc, |
| 8704 | Expr *LHS, Expr *RHS) { |
Fariborz Jahanian | c72a807 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 8705 | QualType LHSType; |
| 8706 | // PropertyRef on LHS type need be directly obtained from |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 8707 | // its declaration as it has a PseudoType. |
Fariborz Jahanian | c72a807 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 8708 | ObjCPropertyRefExpr *PRE |
| 8709 | = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens()); |
| 8710 | if (PRE && !PRE->isImplicitProperty()) { |
| 8711 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
| 8712 | if (PD) |
| 8713 | LHSType = PD->getType(); |
| 8714 | } |
| 8715 | |
| 8716 | if (LHSType.isNull()) |
| 8717 | LHSType = LHS->getType(); |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 8718 | |
| 8719 | Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime(); |
| 8720 | |
| 8721 | if (LT == Qualifiers::OCL_Weak) { |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 8722 | if (!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, Loc)) |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 8723 | getCurFunction()->markSafeWeakUse(LHS); |
| 8724 | } |
| 8725 | |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 8726 | if (checkUnsafeAssigns(Loc, LHSType, RHS)) |
| 8727 | return; |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 8728 | |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 8729 | // FIXME. Check for other life times. |
| 8730 | if (LT != Qualifiers::OCL_None) |
| 8731 | return; |
| 8732 | |
Fariborz Jahanian | c72a807 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 8733 | if (PRE) { |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 8734 | if (PRE->isImplicitProperty()) |
| 8735 | return; |
| 8736 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
| 8737 | if (!PD) |
| 8738 | return; |
| 8739 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 8740 | unsigned Attributes = PD->getPropertyAttributes(); |
| 8741 | if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) { |
Fariborz Jahanian | c72a807 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 8742 | // when 'assign' attribute was not explicitly specified |
| 8743 | // by user, ignore it and rely on property type itself |
| 8744 | // for lifetime info. |
| 8745 | unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten(); |
| 8746 | if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) && |
| 8747 | LHSType->isObjCRetainableType()) |
| 8748 | return; |
| 8749 | |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 8750 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 8751 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 8752 | Diag(Loc, diag::warn_arc_retained_property_assign) |
| 8753 | << RHS->getSourceRange(); |
| 8754 | return; |
| 8755 | } |
| 8756 | RHS = cast->getSubExpr(); |
| 8757 | } |
Fariborz Jahanian | c72a807 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 8758 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 8759 | else if (Attributes & ObjCPropertyDecl::OBJC_PR_weak) { |
Ted Kremenek | b36234d | 2012-12-21 08:04:20 +0000 | [diff] [blame] | 8760 | if (checkUnsafeAssignObject(*this, Loc, Qualifiers::OCL_Weak, RHS, true)) |
| 8761 | return; |
Fariborz Jahanian | dabd133 | 2012-07-06 21:09:27 +0000 | [diff] [blame] | 8762 | } |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 8763 | } |
| 8764 | } |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 8765 | |
| 8766 | //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===// |
| 8767 | |
| 8768 | namespace { |
| 8769 | bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr, |
| 8770 | SourceLocation StmtLoc, |
| 8771 | const NullStmt *Body) { |
| 8772 | // Do not warn if the body is a macro that expands to nothing, e.g: |
| 8773 | // |
| 8774 | // #define CALL(x) |
| 8775 | // if (condition) |
| 8776 | // CALL(0); |
| 8777 | // |
| 8778 | if (Body->hasLeadingEmptyMacro()) |
| 8779 | return false; |
| 8780 | |
| 8781 | // Get line numbers of statement and body. |
| 8782 | bool StmtLineInvalid; |
Dmitri Gribenko | ad80af8 | 2015-03-15 01:08:23 +0000 | [diff] [blame] | 8783 | unsigned StmtLine = SourceMgr.getPresumedLineNumber(StmtLoc, |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 8784 | &StmtLineInvalid); |
| 8785 | if (StmtLineInvalid) |
| 8786 | return false; |
| 8787 | |
| 8788 | bool BodyLineInvalid; |
| 8789 | unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(), |
| 8790 | &BodyLineInvalid); |
| 8791 | if (BodyLineInvalid) |
| 8792 | return false; |
| 8793 | |
| 8794 | // Warn if null statement and body are on the same line. |
| 8795 | if (StmtLine != BodyLine) |
| 8796 | return false; |
| 8797 | |
| 8798 | return true; |
| 8799 | } |
| 8800 | } // Unnamed namespace |
| 8801 | |
| 8802 | void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc, |
| 8803 | const Stmt *Body, |
| 8804 | unsigned DiagID) { |
| 8805 | // Since this is a syntactic check, don't emit diagnostic for template |
| 8806 | // instantiations, this just adds noise. |
| 8807 | if (CurrentInstantiationScope) |
| 8808 | return; |
| 8809 | |
| 8810 | // The body should be a null statement. |
| 8811 | const NullStmt *NBody = dyn_cast<NullStmt>(Body); |
| 8812 | if (!NBody) |
| 8813 | return; |
| 8814 | |
| 8815 | // Do the usual checks. |
| 8816 | if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) |
| 8817 | return; |
| 8818 | |
| 8819 | Diag(NBody->getSemiLoc(), DiagID); |
| 8820 | Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); |
| 8821 | } |
| 8822 | |
| 8823 | void Sema::DiagnoseEmptyLoopBody(const Stmt *S, |
| 8824 | const Stmt *PossibleBody) { |
| 8825 | assert(!CurrentInstantiationScope); // Ensured by caller |
| 8826 | |
| 8827 | SourceLocation StmtLoc; |
| 8828 | const Stmt *Body; |
| 8829 | unsigned DiagID; |
| 8830 | if (const ForStmt *FS = dyn_cast<ForStmt>(S)) { |
| 8831 | StmtLoc = FS->getRParenLoc(); |
| 8832 | Body = FS->getBody(); |
| 8833 | DiagID = diag::warn_empty_for_body; |
| 8834 | } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) { |
| 8835 | StmtLoc = WS->getCond()->getSourceRange().getEnd(); |
| 8836 | Body = WS->getBody(); |
| 8837 | DiagID = diag::warn_empty_while_body; |
| 8838 | } else |
| 8839 | return; // Neither `for' nor `while'. |
| 8840 | |
| 8841 | // The body should be a null statement. |
| 8842 | const NullStmt *NBody = dyn_cast<NullStmt>(Body); |
| 8843 | if (!NBody) |
| 8844 | return; |
| 8845 | |
| 8846 | // Skip expensive checks if diagnostic is disabled. |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 8847 | if (Diags.isIgnored(DiagID, NBody->getSemiLoc())) |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 8848 | return; |
| 8849 | |
| 8850 | // Do the usual checks. |
| 8851 | if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) |
| 8852 | return; |
| 8853 | |
| 8854 | // `for(...);' and `while(...);' are popular idioms, so in order to keep |
| 8855 | // noise level low, emit diagnostics only if for/while is followed by a |
| 8856 | // CompoundStmt, e.g.: |
| 8857 | // for (int i = 0; i < n; i++); |
| 8858 | // { |
| 8859 | // a(i); |
| 8860 | // } |
| 8861 | // or if for/while is followed by a statement with more indentation |
| 8862 | // than for/while itself: |
| 8863 | // for (int i = 0; i < n; i++); |
| 8864 | // a(i); |
| 8865 | bool ProbableTypo = isa<CompoundStmt>(PossibleBody); |
| 8866 | if (!ProbableTypo) { |
| 8867 | bool BodyColInvalid; |
| 8868 | unsigned BodyCol = SourceMgr.getPresumedColumnNumber( |
| 8869 | PossibleBody->getLocStart(), |
| 8870 | &BodyColInvalid); |
| 8871 | if (BodyColInvalid) |
| 8872 | return; |
| 8873 | |
| 8874 | bool StmtColInvalid; |
| 8875 | unsigned StmtCol = SourceMgr.getPresumedColumnNumber( |
| 8876 | S->getLocStart(), |
| 8877 | &StmtColInvalid); |
| 8878 | if (StmtColInvalid) |
| 8879 | return; |
| 8880 | |
| 8881 | if (BodyCol > StmtCol) |
| 8882 | ProbableTypo = true; |
| 8883 | } |
| 8884 | |
| 8885 | if (ProbableTypo) { |
| 8886 | Diag(NBody->getSemiLoc(), DiagID); |
| 8887 | Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); |
| 8888 | } |
| 8889 | } |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 8890 | |
Richard Trieu | 36d0b2b | 2015-01-13 02:32:02 +0000 | [diff] [blame] | 8891 | //===--- CHECK: Warn on self move with std::move. -------------------------===// |
| 8892 | |
| 8893 | /// DiagnoseSelfMove - Emits a warning if a value is moved to itself. |
| 8894 | void Sema::DiagnoseSelfMove(const Expr *LHSExpr, const Expr *RHSExpr, |
| 8895 | SourceLocation OpLoc) { |
| 8896 | |
| 8897 | if (Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, OpLoc)) |
| 8898 | return; |
| 8899 | |
| 8900 | if (!ActiveTemplateInstantiations.empty()) |
| 8901 | return; |
| 8902 | |
| 8903 | // Strip parens and casts away. |
| 8904 | LHSExpr = LHSExpr->IgnoreParenImpCasts(); |
| 8905 | RHSExpr = RHSExpr->IgnoreParenImpCasts(); |
| 8906 | |
| 8907 | // Check for a call expression |
| 8908 | const CallExpr *CE = dyn_cast<CallExpr>(RHSExpr); |
| 8909 | if (!CE || CE->getNumArgs() != 1) |
| 8910 | return; |
| 8911 | |
| 8912 | // Check for a call to std::move |
| 8913 | const FunctionDecl *FD = CE->getDirectCallee(); |
| 8914 | if (!FD || !FD->isInStdNamespace() || !FD->getIdentifier() || |
| 8915 | !FD->getIdentifier()->isStr("move")) |
| 8916 | return; |
| 8917 | |
| 8918 | // Get argument from std::move |
| 8919 | RHSExpr = CE->getArg(0); |
| 8920 | |
| 8921 | const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr); |
| 8922 | const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr); |
| 8923 | |
| 8924 | // Two DeclRefExpr's, check that the decls are the same. |
| 8925 | if (LHSDeclRef && RHSDeclRef) { |
| 8926 | if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl()) |
| 8927 | return; |
| 8928 | if (LHSDeclRef->getDecl()->getCanonicalDecl() != |
| 8929 | RHSDeclRef->getDecl()->getCanonicalDecl()) |
| 8930 | return; |
| 8931 | |
| 8932 | Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType() |
| 8933 | << LHSExpr->getSourceRange() |
| 8934 | << RHSExpr->getSourceRange(); |
| 8935 | return; |
| 8936 | } |
| 8937 | |
| 8938 | // Member variables require a different approach to check for self moves. |
| 8939 | // MemberExpr's are the same if every nested MemberExpr refers to the same |
| 8940 | // Decl and that the base Expr's are DeclRefExpr's with the same Decl or |
| 8941 | // the base Expr's are CXXThisExpr's. |
| 8942 | const Expr *LHSBase = LHSExpr; |
| 8943 | const Expr *RHSBase = RHSExpr; |
| 8944 | const MemberExpr *LHSME = dyn_cast<MemberExpr>(LHSExpr); |
| 8945 | const MemberExpr *RHSME = dyn_cast<MemberExpr>(RHSExpr); |
| 8946 | if (!LHSME || !RHSME) |
| 8947 | return; |
| 8948 | |
| 8949 | while (LHSME && RHSME) { |
| 8950 | if (LHSME->getMemberDecl()->getCanonicalDecl() != |
| 8951 | RHSME->getMemberDecl()->getCanonicalDecl()) |
| 8952 | return; |
| 8953 | |
| 8954 | LHSBase = LHSME->getBase(); |
| 8955 | RHSBase = RHSME->getBase(); |
| 8956 | LHSME = dyn_cast<MemberExpr>(LHSBase); |
| 8957 | RHSME = dyn_cast<MemberExpr>(RHSBase); |
| 8958 | } |
| 8959 | |
| 8960 | LHSDeclRef = dyn_cast<DeclRefExpr>(LHSBase); |
| 8961 | RHSDeclRef = dyn_cast<DeclRefExpr>(RHSBase); |
| 8962 | if (LHSDeclRef && RHSDeclRef) { |
| 8963 | if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl()) |
| 8964 | return; |
| 8965 | if (LHSDeclRef->getDecl()->getCanonicalDecl() != |
| 8966 | RHSDeclRef->getDecl()->getCanonicalDecl()) |
| 8967 | return; |
| 8968 | |
| 8969 | Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType() |
| 8970 | << LHSExpr->getSourceRange() |
| 8971 | << RHSExpr->getSourceRange(); |
| 8972 | return; |
| 8973 | } |
| 8974 | |
| 8975 | if (isa<CXXThisExpr>(LHSBase) && isa<CXXThisExpr>(RHSBase)) |
| 8976 | Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType() |
| 8977 | << LHSExpr->getSourceRange() |
| 8978 | << RHSExpr->getSourceRange(); |
| 8979 | } |
| 8980 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 8981 | //===--- Layout compatibility ----------------------------------------------// |
| 8982 | |
| 8983 | namespace { |
| 8984 | |
| 8985 | bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2); |
| 8986 | |
| 8987 | /// \brief Check if two enumeration types are layout-compatible. |
| 8988 | bool isLayoutCompatible(ASTContext &C, EnumDecl *ED1, EnumDecl *ED2) { |
| 8989 | // C++11 [dcl.enum] p8: |
| 8990 | // Two enumeration types are layout-compatible if they have the same |
| 8991 | // underlying type. |
| 8992 | return ED1->isComplete() && ED2->isComplete() && |
| 8993 | C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType()); |
| 8994 | } |
| 8995 | |
| 8996 | /// \brief Check if two fields are layout-compatible. |
| 8997 | bool isLayoutCompatible(ASTContext &C, FieldDecl *Field1, FieldDecl *Field2) { |
| 8998 | if (!isLayoutCompatible(C, Field1->getType(), Field2->getType())) |
| 8999 | return false; |
| 9000 | |
| 9001 | if (Field1->isBitField() != Field2->isBitField()) |
| 9002 | return false; |
| 9003 | |
| 9004 | if (Field1->isBitField()) { |
| 9005 | // Make sure that the bit-fields are the same length. |
| 9006 | unsigned Bits1 = Field1->getBitWidthValue(C); |
| 9007 | unsigned Bits2 = Field2->getBitWidthValue(C); |
| 9008 | |
| 9009 | if (Bits1 != Bits2) |
| 9010 | return false; |
| 9011 | } |
| 9012 | |
| 9013 | return true; |
| 9014 | } |
| 9015 | |
| 9016 | /// \brief Check if two standard-layout structs are layout-compatible. |
| 9017 | /// (C++11 [class.mem] p17) |
| 9018 | bool isLayoutCompatibleStruct(ASTContext &C, |
| 9019 | RecordDecl *RD1, |
| 9020 | RecordDecl *RD2) { |
| 9021 | // If both records are C++ classes, check that base classes match. |
| 9022 | if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1)) { |
| 9023 | // If one of records is a CXXRecordDecl we are in C++ mode, |
| 9024 | // thus the other one is a CXXRecordDecl, too. |
| 9025 | const CXXRecordDecl *D2CXX = cast<CXXRecordDecl>(RD2); |
| 9026 | // Check number of base classes. |
| 9027 | if (D1CXX->getNumBases() != D2CXX->getNumBases()) |
| 9028 | return false; |
| 9029 | |
| 9030 | // Check the base classes. |
| 9031 | for (CXXRecordDecl::base_class_const_iterator |
| 9032 | Base1 = D1CXX->bases_begin(), |
| 9033 | BaseEnd1 = D1CXX->bases_end(), |
| 9034 | Base2 = D2CXX->bases_begin(); |
| 9035 | Base1 != BaseEnd1; |
| 9036 | ++Base1, ++Base2) { |
| 9037 | if (!isLayoutCompatible(C, Base1->getType(), Base2->getType())) |
| 9038 | return false; |
| 9039 | } |
| 9040 | } else if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2)) { |
| 9041 | // If only RD2 is a C++ class, it should have zero base classes. |
| 9042 | if (D2CXX->getNumBases() > 0) |
| 9043 | return false; |
| 9044 | } |
| 9045 | |
| 9046 | // Check the fields. |
| 9047 | RecordDecl::field_iterator Field2 = RD2->field_begin(), |
| 9048 | Field2End = RD2->field_end(), |
| 9049 | Field1 = RD1->field_begin(), |
| 9050 | Field1End = RD1->field_end(); |
| 9051 | for ( ; Field1 != Field1End && Field2 != Field2End; ++Field1, ++Field2) { |
| 9052 | if (!isLayoutCompatible(C, *Field1, *Field2)) |
| 9053 | return false; |
| 9054 | } |
| 9055 | if (Field1 != Field1End || Field2 != Field2End) |
| 9056 | return false; |
| 9057 | |
| 9058 | return true; |
| 9059 | } |
| 9060 | |
| 9061 | /// \brief Check if two standard-layout unions are layout-compatible. |
| 9062 | /// (C++11 [class.mem] p18) |
| 9063 | bool isLayoutCompatibleUnion(ASTContext &C, |
| 9064 | RecordDecl *RD1, |
| 9065 | RecordDecl *RD2) { |
| 9066 | llvm::SmallPtrSet<FieldDecl *, 8> UnmatchedFields; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 9067 | for (auto *Field2 : RD2->fields()) |
| 9068 | UnmatchedFields.insert(Field2); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9069 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 9070 | for (auto *Field1 : RD1->fields()) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9071 | llvm::SmallPtrSet<FieldDecl *, 8>::iterator |
| 9072 | I = UnmatchedFields.begin(), |
| 9073 | E = UnmatchedFields.end(); |
| 9074 | |
| 9075 | for ( ; I != E; ++I) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 9076 | if (isLayoutCompatible(C, Field1, *I)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9077 | bool Result = UnmatchedFields.erase(*I); |
| 9078 | (void) Result; |
| 9079 | assert(Result); |
| 9080 | break; |
| 9081 | } |
| 9082 | } |
| 9083 | if (I == E) |
| 9084 | return false; |
| 9085 | } |
| 9086 | |
| 9087 | return UnmatchedFields.empty(); |
| 9088 | } |
| 9089 | |
| 9090 | bool isLayoutCompatible(ASTContext &C, RecordDecl *RD1, RecordDecl *RD2) { |
| 9091 | if (RD1->isUnion() != RD2->isUnion()) |
| 9092 | return false; |
| 9093 | |
| 9094 | if (RD1->isUnion()) |
| 9095 | return isLayoutCompatibleUnion(C, RD1, RD2); |
| 9096 | else |
| 9097 | return isLayoutCompatibleStruct(C, RD1, RD2); |
| 9098 | } |
| 9099 | |
| 9100 | /// \brief Check if two types are layout-compatible in C++11 sense. |
| 9101 | bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2) { |
| 9102 | if (T1.isNull() || T2.isNull()) |
| 9103 | return false; |
| 9104 | |
| 9105 | // C++11 [basic.types] p11: |
| 9106 | // If two types T1 and T2 are the same type, then T1 and T2 are |
| 9107 | // layout-compatible types. |
| 9108 | if (C.hasSameType(T1, T2)) |
| 9109 | return true; |
| 9110 | |
| 9111 | T1 = T1.getCanonicalType().getUnqualifiedType(); |
| 9112 | T2 = T2.getCanonicalType().getUnqualifiedType(); |
| 9113 | |
| 9114 | const Type::TypeClass TC1 = T1->getTypeClass(); |
| 9115 | const Type::TypeClass TC2 = T2->getTypeClass(); |
| 9116 | |
| 9117 | if (TC1 != TC2) |
| 9118 | return false; |
| 9119 | |
| 9120 | if (TC1 == Type::Enum) { |
| 9121 | return isLayoutCompatible(C, |
| 9122 | cast<EnumType>(T1)->getDecl(), |
| 9123 | cast<EnumType>(T2)->getDecl()); |
| 9124 | } else if (TC1 == Type::Record) { |
| 9125 | if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType()) |
| 9126 | return false; |
| 9127 | |
| 9128 | return isLayoutCompatible(C, |
| 9129 | cast<RecordType>(T1)->getDecl(), |
| 9130 | cast<RecordType>(T2)->getDecl()); |
| 9131 | } |
| 9132 | |
| 9133 | return false; |
| 9134 | } |
| 9135 | } |
| 9136 | |
| 9137 | //===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----// |
| 9138 | |
| 9139 | namespace { |
| 9140 | /// \brief Given a type tag expression find the type tag itself. |
| 9141 | /// |
| 9142 | /// \param TypeExpr Type tag expression, as it appears in user's code. |
| 9143 | /// |
| 9144 | /// \param VD Declaration of an identifier that appears in a type tag. |
| 9145 | /// |
| 9146 | /// \param MagicValue Type tag magic value. |
| 9147 | bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx, |
| 9148 | const ValueDecl **VD, uint64_t *MagicValue) { |
| 9149 | while(true) { |
| 9150 | if (!TypeExpr) |
| 9151 | return false; |
| 9152 | |
| 9153 | TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts(); |
| 9154 | |
| 9155 | switch (TypeExpr->getStmtClass()) { |
| 9156 | case Stmt::UnaryOperatorClass: { |
| 9157 | const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr); |
| 9158 | if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) { |
| 9159 | TypeExpr = UO->getSubExpr(); |
| 9160 | continue; |
| 9161 | } |
| 9162 | return false; |
| 9163 | } |
| 9164 | |
| 9165 | case Stmt::DeclRefExprClass: { |
| 9166 | const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr); |
| 9167 | *VD = DRE->getDecl(); |
| 9168 | return true; |
| 9169 | } |
| 9170 | |
| 9171 | case Stmt::IntegerLiteralClass: { |
| 9172 | const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr); |
| 9173 | llvm::APInt MagicValueAPInt = IL->getValue(); |
| 9174 | if (MagicValueAPInt.getActiveBits() <= 64) { |
| 9175 | *MagicValue = MagicValueAPInt.getZExtValue(); |
| 9176 | return true; |
| 9177 | } else |
| 9178 | return false; |
| 9179 | } |
| 9180 | |
| 9181 | case Stmt::BinaryConditionalOperatorClass: |
| 9182 | case Stmt::ConditionalOperatorClass: { |
| 9183 | const AbstractConditionalOperator *ACO = |
| 9184 | cast<AbstractConditionalOperator>(TypeExpr); |
| 9185 | bool Result; |
| 9186 | if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx)) { |
| 9187 | if (Result) |
| 9188 | TypeExpr = ACO->getTrueExpr(); |
| 9189 | else |
| 9190 | TypeExpr = ACO->getFalseExpr(); |
| 9191 | continue; |
| 9192 | } |
| 9193 | return false; |
| 9194 | } |
| 9195 | |
| 9196 | case Stmt::BinaryOperatorClass: { |
| 9197 | const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr); |
| 9198 | if (BO->getOpcode() == BO_Comma) { |
| 9199 | TypeExpr = BO->getRHS(); |
| 9200 | continue; |
| 9201 | } |
| 9202 | return false; |
| 9203 | } |
| 9204 | |
| 9205 | default: |
| 9206 | return false; |
| 9207 | } |
| 9208 | } |
| 9209 | } |
| 9210 | |
| 9211 | /// \brief Retrieve the C type corresponding to type tag TypeExpr. |
| 9212 | /// |
| 9213 | /// \param TypeExpr Expression that specifies a type tag. |
| 9214 | /// |
| 9215 | /// \param MagicValues Registered magic values. |
| 9216 | /// |
| 9217 | /// \param FoundWrongKind Set to true if a type tag was found, but of a wrong |
| 9218 | /// kind. |
| 9219 | /// |
| 9220 | /// \param TypeInfo Information about the corresponding C type. |
| 9221 | /// |
| 9222 | /// \returns true if the corresponding C type was found. |
| 9223 | bool GetMatchingCType( |
| 9224 | const IdentifierInfo *ArgumentKind, |
| 9225 | const Expr *TypeExpr, const ASTContext &Ctx, |
| 9226 | const llvm::DenseMap<Sema::TypeTagMagicValue, |
| 9227 | Sema::TypeTagData> *MagicValues, |
| 9228 | bool &FoundWrongKind, |
| 9229 | Sema::TypeTagData &TypeInfo) { |
| 9230 | FoundWrongKind = false; |
| 9231 | |
| 9232 | // Variable declaration that has type_tag_for_datatype attribute. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9233 | const ValueDecl *VD = nullptr; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9234 | |
| 9235 | uint64_t MagicValue; |
| 9236 | |
| 9237 | if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue)) |
| 9238 | return false; |
| 9239 | |
| 9240 | if (VD) { |
Benjamin Kramer | ae852a6 | 2014-02-23 14:34:50 +0000 | [diff] [blame] | 9241 | if (TypeTagForDatatypeAttr *I = VD->getAttr<TypeTagForDatatypeAttr>()) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9242 | if (I->getArgumentKind() != ArgumentKind) { |
| 9243 | FoundWrongKind = true; |
| 9244 | return false; |
| 9245 | } |
| 9246 | TypeInfo.Type = I->getMatchingCType(); |
| 9247 | TypeInfo.LayoutCompatible = I->getLayoutCompatible(); |
| 9248 | TypeInfo.MustBeNull = I->getMustBeNull(); |
| 9249 | return true; |
| 9250 | } |
| 9251 | return false; |
| 9252 | } |
| 9253 | |
| 9254 | if (!MagicValues) |
| 9255 | return false; |
| 9256 | |
| 9257 | llvm::DenseMap<Sema::TypeTagMagicValue, |
| 9258 | Sema::TypeTagData>::const_iterator I = |
| 9259 | MagicValues->find(std::make_pair(ArgumentKind, MagicValue)); |
| 9260 | if (I == MagicValues->end()) |
| 9261 | return false; |
| 9262 | |
| 9263 | TypeInfo = I->second; |
| 9264 | return true; |
| 9265 | } |
| 9266 | } // unnamed namespace |
| 9267 | |
| 9268 | void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind, |
| 9269 | uint64_t MagicValue, QualType Type, |
| 9270 | bool LayoutCompatible, |
| 9271 | bool MustBeNull) { |
| 9272 | if (!TypeTagForDatatypeMagicValues) |
| 9273 | TypeTagForDatatypeMagicValues.reset( |
| 9274 | new llvm::DenseMap<TypeTagMagicValue, TypeTagData>); |
| 9275 | |
| 9276 | TypeTagMagicValue Magic(ArgumentKind, MagicValue); |
| 9277 | (*TypeTagForDatatypeMagicValues)[Magic] = |
| 9278 | TypeTagData(Type, LayoutCompatible, MustBeNull); |
| 9279 | } |
| 9280 | |
| 9281 | namespace { |
| 9282 | bool IsSameCharType(QualType T1, QualType T2) { |
| 9283 | const BuiltinType *BT1 = T1->getAs<BuiltinType>(); |
| 9284 | if (!BT1) |
| 9285 | return false; |
| 9286 | |
| 9287 | const BuiltinType *BT2 = T2->getAs<BuiltinType>(); |
| 9288 | if (!BT2) |
| 9289 | return false; |
| 9290 | |
| 9291 | BuiltinType::Kind T1Kind = BT1->getKind(); |
| 9292 | BuiltinType::Kind T2Kind = BT2->getKind(); |
| 9293 | |
| 9294 | return (T1Kind == BuiltinType::SChar && T2Kind == BuiltinType::Char_S) || |
| 9295 | (T1Kind == BuiltinType::UChar && T2Kind == BuiltinType::Char_U) || |
| 9296 | (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) || |
| 9297 | (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar); |
| 9298 | } |
| 9299 | } // unnamed namespace |
| 9300 | |
| 9301 | void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr, |
| 9302 | const Expr * const *ExprArgs) { |
| 9303 | const IdentifierInfo *ArgumentKind = Attr->getArgumentKind(); |
| 9304 | bool IsPointerAttr = Attr->getIsPointer(); |
| 9305 | |
| 9306 | const Expr *TypeTagExpr = ExprArgs[Attr->getTypeTagIdx()]; |
| 9307 | bool FoundWrongKind; |
| 9308 | TypeTagData TypeInfo; |
| 9309 | if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context, |
| 9310 | TypeTagForDatatypeMagicValues.get(), |
| 9311 | FoundWrongKind, TypeInfo)) { |
| 9312 | if (FoundWrongKind) |
| 9313 | Diag(TypeTagExpr->getExprLoc(), |
| 9314 | diag::warn_type_tag_for_datatype_wrong_kind) |
| 9315 | << TypeTagExpr->getSourceRange(); |
| 9316 | return; |
| 9317 | } |
| 9318 | |
| 9319 | const Expr *ArgumentExpr = ExprArgs[Attr->getArgumentIdx()]; |
| 9320 | if (IsPointerAttr) { |
| 9321 | // Skip implicit cast of pointer to `void *' (as a function argument). |
| 9322 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr)) |
Dmitri Gribenko | 5ac744e | 2012-11-03 16:07:49 +0000 | [diff] [blame] | 9323 | if (ICE->getType()->isVoidPointerType() && |
Dmitri Gribenko | f21203b | 2012-11-03 22:10:18 +0000 | [diff] [blame] | 9324 | ICE->getCastKind() == CK_BitCast) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9325 | ArgumentExpr = ICE->getSubExpr(); |
| 9326 | } |
| 9327 | QualType ArgumentType = ArgumentExpr->getType(); |
| 9328 | |
| 9329 | // Passing a `void*' pointer shouldn't trigger a warning. |
| 9330 | if (IsPointerAttr && ArgumentType->isVoidPointerType()) |
| 9331 | return; |
| 9332 | |
| 9333 | if (TypeInfo.MustBeNull) { |
| 9334 | // Type tag with matching void type requires a null pointer. |
| 9335 | if (!ArgumentExpr->isNullPointerConstant(Context, |
| 9336 | Expr::NPC_ValueDependentIsNotNull)) { |
| 9337 | Diag(ArgumentExpr->getExprLoc(), |
| 9338 | diag::warn_type_safety_null_pointer_required) |
| 9339 | << ArgumentKind->getName() |
| 9340 | << ArgumentExpr->getSourceRange() |
| 9341 | << TypeTagExpr->getSourceRange(); |
| 9342 | } |
| 9343 | return; |
| 9344 | } |
| 9345 | |
| 9346 | QualType RequiredType = TypeInfo.Type; |
| 9347 | if (IsPointerAttr) |
| 9348 | RequiredType = Context.getPointerType(RequiredType); |
| 9349 | |
| 9350 | bool mismatch = false; |
| 9351 | if (!TypeInfo.LayoutCompatible) { |
| 9352 | mismatch = !Context.hasSameType(ArgumentType, RequiredType); |
| 9353 | |
| 9354 | // C++11 [basic.fundamental] p1: |
| 9355 | // Plain char, signed char, and unsigned char are three distinct types. |
| 9356 | // |
| 9357 | // But we treat plain `char' as equivalent to `signed char' or `unsigned |
| 9358 | // char' depending on the current char signedness mode. |
| 9359 | if (mismatch) |
| 9360 | if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(), |
| 9361 | RequiredType->getPointeeType())) || |
| 9362 | (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType))) |
| 9363 | mismatch = false; |
| 9364 | } else |
| 9365 | if (IsPointerAttr) |
| 9366 | mismatch = !isLayoutCompatible(Context, |
| 9367 | ArgumentType->getPointeeType(), |
| 9368 | RequiredType->getPointeeType()); |
| 9369 | else |
| 9370 | mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType); |
| 9371 | |
| 9372 | if (mismatch) |
| 9373 | Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch) |
Aaron Ballman | 25dc1e1 | 2014-01-03 02:14:08 +0000 | [diff] [blame] | 9374 | << ArgumentType << ArgumentKind |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9375 | << TypeInfo.LayoutCompatible << RequiredType |
| 9376 | << ArgumentExpr->getSourceRange() |
| 9377 | << TypeTagExpr->getSourceRange(); |
| 9378 | } |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 9379 | |